]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - crypto/asymmetric_keys/public_key.c
Merge tag 'kvm-s390-master-4.15-3' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-bionic-kernel.git] / crypto / asymmetric_keys / public_key.c
CommitLineData
a9681bf3
DH
1/* In-software asymmetric public-key crypto subtype
2 *
3 * See Documentation/crypto/asymmetric-keys.txt
4 *
5 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public Licence
10 * as published by the Free Software Foundation; either version
11 * 2 of the Licence, or (at your option) any later version.
12 */
13
14#define pr_fmt(fmt) "PKEY: "fmt
15#include <linux/module.h>
16#include <linux/export.h>
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/seq_file.h>
d43de6c7 20#include <linux/scatterlist.h>
a9681bf3 21#include <keys/asymmetric-subtype.h>
db6c43bd 22#include <crypto/public_key.h>
d43de6c7 23#include <crypto/akcipher.h>
a9681bf3 24
1e684d38
DH
25MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
26MODULE_AUTHOR("Red Hat, Inc.");
a9681bf3
DH
27MODULE_LICENSE("GPL");
28
a9681bf3
DH
29/*
30 * Provide a part of a description of the key for /proc/keys.
31 */
32static void public_key_describe(const struct key *asymmetric_key,
33 struct seq_file *m)
34{
146aa8b1 35 struct public_key *key = asymmetric_key->payload.data[asym_crypto];
a9681bf3
DH
36
37 if (key)
4e8ae72a 38 seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
a9681bf3
DH
39}
40
41/*
42 * Destroy a public key algorithm key.
43 */
3b764563 44void public_key_free(struct public_key *key)
a9681bf3 45{
3b764563 46 if (key) {
db6c43bd 47 kfree(key->key);
3b764563
DH
48 kfree(key);
49 }
50}
51EXPORT_SYMBOL_GPL(public_key_free);
52
53/*
54 * Destroy a public key algorithm key.
55 */
56static void public_key_destroy(void *payload0, void *payload3)
57{
58 public_key_free(payload0);
59 public_key_signature_free(payload3);
a9681bf3 60}
a9681bf3
DH
61
62/*
63 * Verify a signature using a public key.
64 */
db6c43bd 65int public_key_verify_signature(const struct public_key *pkey,
3d167d68 66 const struct public_key_signature *sig)
a9681bf3 67{
0ca2a04a 68 struct crypto_wait cwait;
d43de6c7
DH
69 struct crypto_akcipher *tfm;
70 struct akcipher_request *req;
71 struct scatterlist sig_sg, digest_sg;
72 const char *alg_name;
73 char alg_name_buf[CRYPTO_MAX_ALG_NAME];
74 void *output;
75 unsigned int outlen;
72f9a07b 76 int ret;
d43de6c7
DH
77
78 pr_devel("==>%s()\n", __func__);
79
db6c43bd 80 BUG_ON(!pkey);
3d167d68
DH
81 BUG_ON(!sig);
82 BUG_ON(!sig->digest);
db6c43bd 83 BUG_ON(!sig->s);
a9681bf3 84
4e8ae72a
DH
85 alg_name = sig->pkey_algo;
86 if (strcmp(sig->pkey_algo, "rsa") == 0) {
d43de6c7
DH
87 /* The data wangled by the RSA algorithm is typically padded
88 * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
89 * sec 8.2].
90 */
91 if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME,
4e8ae72a 92 "pkcs1pad(rsa,%s)", sig->hash_algo
d43de6c7
DH
93 ) >= CRYPTO_MAX_ALG_NAME)
94 return -EINVAL;
95 alg_name = alg_name_buf;
96 }
97
98 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
99 if (IS_ERR(tfm))
100 return PTR_ERR(tfm);
101
72f9a07b 102 ret = -ENOMEM;
d43de6c7
DH
103 req = akcipher_request_alloc(tfm, GFP_KERNEL);
104 if (!req)
105 goto error_free_tfm;
106
107 ret = crypto_akcipher_set_pub_key(tfm, pkey->key, pkey->keylen);
108 if (ret)
109 goto error_free_req;
110
fbb72630 111 ret = -ENOMEM;
d43de6c7
DH
112 outlen = crypto_akcipher_maxsize(tfm);
113 output = kmalloc(outlen, GFP_KERNEL);
114 if (!output)
115 goto error_free_req;
116
117 sg_init_one(&sig_sg, sig->s, sig->s_size);
118 sg_init_one(&digest_sg, output, outlen);
119 akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size,
120 outlen);
0ca2a04a 121 crypto_init_wait(&cwait);
d43de6c7
DH
122 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
123 CRYPTO_TFM_REQ_MAY_SLEEP,
0ca2a04a 124 crypto_req_done, &cwait);
d43de6c7
DH
125
126 /* Perform the verification calculation. This doesn't actually do the
127 * verification, but rather calculates the hash expected by the
128 * signature and returns that to us.
129 */
0ca2a04a 130 ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
72f9a07b 131 if (ret)
d43de6c7 132 goto out_free_output;
a9681bf3 133
d43de6c7
DH
134 /* Do the actual verification step. */
135 if (req->dst_len != sig->digest_size ||
136 memcmp(sig->digest, output, sig->digest_size) != 0)
137 ret = -EKEYREJECTED;
a9681bf3 138
d43de6c7
DH
139out_free_output:
140 kfree(output);
141error_free_req:
142 akcipher_request_free(req);
143error_free_tfm:
144 crypto_free_akcipher(tfm);
145 pr_devel("<==%s() = %d\n", __func__, ret);
72f9a07b
EB
146 if (WARN_ON_ONCE(ret > 0))
147 ret = -EINVAL;
d43de6c7 148 return ret;
3d167d68
DH
149}
150EXPORT_SYMBOL_GPL(public_key_verify_signature);
151
152static int public_key_verify_signature_2(const struct key *key,
153 const struct public_key_signature *sig)
154{
146aa8b1 155 const struct public_key *pk = key->payload.data[asym_crypto];
3d167d68 156 return public_key_verify_signature(pk, sig);
a9681bf3
DH
157}
158
159/*
160 * Public key algorithm asymmetric key subtype
161 */
162struct asymmetric_key_subtype public_key_subtype = {
163 .owner = THIS_MODULE,
164 .name = "public_key",
876c6e3e 165 .name_len = sizeof("public_key") - 1,
a9681bf3
DH
166 .describe = public_key_describe,
167 .destroy = public_key_destroy,
3d167d68 168 .verify_signature = public_key_verify_signature_2,
a9681bf3
DH
169};
170EXPORT_SYMBOL_GPL(public_key_subtype);