blob: 4fefb219bfdc86318f33b822eef7bc8214a2a106 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsa9681bf2012-09-21 23:24:55 +01002/* In-software asymmetric public-key crypto subtype
3 *
Mauro Carvalho Chehab0efaaa82020-06-15 08:50:08 +02004 * See Documentation/crypto/asymmetric-keys.rst
David Howellsa9681bf2012-09-21 23:24:55 +01005 *
6 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
David Howellsa9681bf2012-09-21 23:24:55 +01008 */
9
10#define pr_fmt(fmt) "PKEY: "fmt
11#include <linux/module.h>
12#include <linux/export.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/seq_file.h>
David Howellsd43de6c2016-03-03 21:49:27 +000016#include <linux/scatterlist.h>
Stefan Berger299f5612021-03-16 17:07:37 -040017#include <linux/asn1.h>
David Howellsa9681bf2012-09-21 23:24:55 +010018#include <keys/asymmetric-subtype.h>
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080019#include <crypto/public_key.h>
David Howellsd43de6c2016-03-03 21:49:27 +000020#include <crypto/akcipher.h>
Herbert Xu3093e7c2020-10-07 17:05:45 +110021#include <crypto/sm2.h>
22#include <crypto/sm3_base.h>
David Howellsa9681bf2012-09-21 23:24:55 +010023
David Howells1e684d32017-11-15 16:38:45 +000024MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
25MODULE_AUTHOR("Red Hat, Inc.");
David Howellsa9681bf2012-09-21 23:24:55 +010026MODULE_LICENSE("GPL");
27
David Howellsa9681bf2012-09-21 23:24:55 +010028/*
29 * Provide a part of a description of the key for /proc/keys.
30 */
31static void public_key_describe(const struct key *asymmetric_key,
32 struct seq_file *m)
33{
David Howells146aa8b2015-10-21 14:04:48 +010034 struct public_key *key = asymmetric_key->payload.data[asym_crypto];
David Howellsa9681bf2012-09-21 23:24:55 +010035
36 if (key)
David Howells4e8ae722016-03-03 21:49:27 +000037 seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
David Howellsa9681bf2012-09-21 23:24:55 +010038}
39
40/*
41 * Destroy a public key algorithm key.
42 */
David Howells3b764562016-04-06 16:13:33 +010043void public_key_free(struct public_key *key)
David Howellsa9681bf2012-09-21 23:24:55 +010044{
David Howells3b764562016-04-06 16:13:33 +010045 if (key) {
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080046 kfree(key->key);
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +030047 kfree(key->params);
David Howells3b764562016-04-06 16:13:33 +010048 kfree(key);
49 }
David Howellsa9681bf2012-09-21 23:24:55 +010050}
David Howells3b764562016-04-06 16:13:33 +010051EXPORT_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);
60}
David Howellsa9681bf2012-09-21 23:24:55 +010061
62/*
David Howells82f94f22018-10-09 17:47:23 +010063 * Determine the crypto algorithm name.
64 */
65static
66int software_key_determine_akcipher(const char *encoding,
67 const char *hash_algo,
68 const struct public_key *pkey,
69 char alg_name[CRYPTO_MAX_ALG_NAME])
70{
71 int n;
72
73 if (strcmp(encoding, "pkcs1") == 0) {
74 /* The data wangled by the RSA algorithm is typically padded
75 * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
76 * sec 8.2].
77 */
78 if (!hash_algo)
79 n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
80 "pkcs1pad(%s)",
81 pkey->pkey_algo);
82 else
83 n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
84 "pkcs1pad(%s,%s)",
85 pkey->pkey_algo, hash_algo);
86 return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
87 }
88
Stefan Berger299f5612021-03-16 17:07:37 -040089 if (strcmp(encoding, "raw") == 0 ||
90 strcmp(encoding, "x962") == 0) {
David Howells82f94f22018-10-09 17:47:23 +010091 strcpy(alg_name, pkey->pkey_algo);
92 return 0;
93 }
94
95 return -ENOPKG;
96}
97
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +030098static u8 *pkey_pack_u32(u8 *dst, u32 val)
99{
100 memcpy(dst, &val, sizeof(val));
101 return dst + sizeof(val);
102}
103
David Howells82f94f22018-10-09 17:47:23 +0100104/*
105 * Query information about a key.
106 */
107static int software_key_query(const struct kernel_pkey_params *params,
108 struct kernel_pkey_query *info)
109{
110 struct crypto_akcipher *tfm;
111 struct public_key *pkey = params->key->payload.data[asym_crypto];
112 char alg_name[CRYPTO_MAX_ALG_NAME];
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300113 u8 *key, *ptr;
David Howells82f94f22018-10-09 17:47:23 +0100114 int ret, len;
115
116 ret = software_key_determine_akcipher(params->encoding,
117 params->hash_algo,
118 pkey, alg_name);
119 if (ret < 0)
120 return ret;
121
122 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
123 if (IS_ERR(tfm))
124 return PTR_ERR(tfm);
125
Wei Yongjun6cbba1f2020-07-15 23:28:38 +0100126 ret = -ENOMEM;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300127 key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
128 GFP_KERNEL);
129 if (!key)
David Howells82f94f22018-10-09 17:47:23 +0100130 goto error_free_tfm;
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300131 memcpy(key, pkey->key, pkey->keylen);
132 ptr = key + pkey->keylen;
133 ptr = pkey_pack_u32(ptr, pkey->algo);
134 ptr = pkey_pack_u32(ptr, pkey->paramlen);
135 memcpy(ptr, pkey->params, pkey->paramlen);
136
137 if (pkey->key_is_private)
138 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
139 else
140 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
141 if (ret < 0)
142 goto error_free_key;
David Howells82f94f22018-10-09 17:47:23 +0100143
144 len = crypto_akcipher_maxsize(tfm);
145 info->key_size = len * 8;
146 info->max_data_size = len;
147 info->max_sig_size = len;
148 info->max_enc_size = len;
149 info->max_dec_size = len;
David Howellsc08fed72018-10-09 17:47:38 +0100150 info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
151 KEYCTL_SUPPORTS_VERIFY);
152 if (pkey->key_is_private)
153 info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
154 KEYCTL_SUPPORTS_SIGN);
David Howells82f94f22018-10-09 17:47:23 +0100155 ret = 0;
156
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300157error_free_key:
158 kfree(key);
David Howells82f94f22018-10-09 17:47:23 +0100159error_free_tfm:
160 crypto_free_akcipher(tfm);
161 pr_devel("<==%s() = %d\n", __func__, ret);
162 return ret;
163}
164
165/*
David Howellsc08fed72018-10-09 17:47:38 +0100166 * Do encryption, decryption and signing ops.
167 */
168static int software_key_eds_op(struct kernel_pkey_params *params,
169 const void *in, void *out)
170{
171 const struct public_key *pkey = params->key->payload.data[asym_crypto];
172 struct akcipher_request *req;
173 struct crypto_akcipher *tfm;
174 struct crypto_wait cwait;
175 struct scatterlist in_sg, out_sg;
176 char alg_name[CRYPTO_MAX_ALG_NAME];
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300177 char *key, *ptr;
David Howellsc08fed72018-10-09 17:47:38 +0100178 int ret;
179
180 pr_devel("==>%s()\n", __func__);
181
182 ret = software_key_determine_akcipher(params->encoding,
183 params->hash_algo,
184 pkey, alg_name);
185 if (ret < 0)
186 return ret;
187
188 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
189 if (IS_ERR(tfm))
190 return PTR_ERR(tfm);
191
Eric Biggersbea37412019-10-09 16:03:49 -0700192 ret = -ENOMEM;
David Howellsc08fed72018-10-09 17:47:38 +0100193 req = akcipher_request_alloc(tfm, GFP_KERNEL);
194 if (!req)
195 goto error_free_tfm;
196
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300197 key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
198 GFP_KERNEL);
199 if (!key)
David Howellsc08fed72018-10-09 17:47:38 +0100200 goto error_free_req;
201
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300202 memcpy(key, pkey->key, pkey->keylen);
203 ptr = key + pkey->keylen;
204 ptr = pkey_pack_u32(ptr, pkey->algo);
205 ptr = pkey_pack_u32(ptr, pkey->paramlen);
206 memcpy(ptr, pkey->params, pkey->paramlen);
207
208 if (pkey->key_is_private)
209 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
210 else
211 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
212 if (ret)
213 goto error_free_key;
214
David Howellsc08fed72018-10-09 17:47:38 +0100215 sg_init_one(&in_sg, in, params->in_len);
216 sg_init_one(&out_sg, out, params->out_len);
217 akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
218 params->out_len);
219 crypto_init_wait(&cwait);
220 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
221 CRYPTO_TFM_REQ_MAY_SLEEP,
222 crypto_req_done, &cwait);
223
224 /* Perform the encryption calculation. */
225 switch (params->op) {
226 case kernel_pkey_encrypt:
227 ret = crypto_akcipher_encrypt(req);
228 break;
229 case kernel_pkey_decrypt:
230 ret = crypto_akcipher_decrypt(req);
231 break;
232 case kernel_pkey_sign:
233 ret = crypto_akcipher_sign(req);
234 break;
235 default:
236 BUG();
237 }
238
239 ret = crypto_wait_req(ret, &cwait);
240 if (ret == 0)
241 ret = req->dst_len;
242
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300243error_free_key:
244 kfree(key);
David Howellsc08fed72018-10-09 17:47:38 +0100245error_free_req:
246 akcipher_request_free(req);
247error_free_tfm:
248 crypto_free_akcipher(tfm);
249 pr_devel("<==%s() = %d\n", __func__, ret);
250 return ret;
251}
252
Herbert Xu3093e7c2020-10-07 17:05:45 +1100253#if IS_REACHABLE(CONFIG_CRYPTO_SM2)
254static int cert_sig_digest_update(const struct public_key_signature *sig,
255 struct crypto_akcipher *tfm_pkey)
256{
257 struct crypto_shash *tfm;
258 struct shash_desc *desc;
259 size_t desc_size;
260 unsigned char dgst[SM3_DIGEST_SIZE];
261 int ret;
262
263 BUG_ON(!sig->data);
264
265 ret = sm2_compute_z_digest(tfm_pkey, SM2_DEFAULT_USERID,
266 SM2_DEFAULT_USERID_LEN, dgst);
267 if (ret)
268 return ret;
269
270 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
271 if (IS_ERR(tfm))
272 return PTR_ERR(tfm);
273
274 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
275 desc = kzalloc(desc_size, GFP_KERNEL);
276 if (!desc) {
277 ret = -ENOMEM;
278 goto error_free_tfm;
279 }
280
281 desc->tfm = tfm;
282
283 ret = crypto_shash_init(desc);
284 if (ret < 0)
285 goto error_free_desc;
286
287 ret = crypto_shash_update(desc, dgst, SM3_DIGEST_SIZE);
288 if (ret < 0)
289 goto error_free_desc;
290
291 ret = crypto_shash_finup(desc, sig->data, sig->data_size, sig->digest);
292
293error_free_desc:
294 kfree(desc);
295error_free_tfm:
296 crypto_free_shash(tfm);
297 return ret;
298}
299#else
300static inline int cert_sig_digest_update(
301 const struct public_key_signature *sig,
302 struct crypto_akcipher *tfm_pkey)
303{
304 return -ENOTSUPP;
305}
306#endif /* ! IS_REACHABLE(CONFIG_CRYPTO_SM2) */
307
David Howellsc08fed72018-10-09 17:47:38 +0100308/*
David Howellsa9681bf2012-09-21 23:24:55 +0100309 * Verify a signature using a public key.
310 */
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -0800311int public_key_verify_signature(const struct public_key *pkey,
David Howells3d167d62013-08-30 16:15:30 +0100312 const struct public_key_signature *sig)
David Howellsa9681bf2012-09-21 23:24:55 +0100313{
Gilad Ben-Yossef0ca2a042017-10-18 08:00:40 +0100314 struct crypto_wait cwait;
David Howellsd43de6c2016-03-03 21:49:27 +0000315 struct crypto_akcipher *tfm;
316 struct akcipher_request *req;
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300317 struct scatterlist src_sg[2];
David Howells82f94f22018-10-09 17:47:23 +0100318 char alg_name[CRYPTO_MAX_ALG_NAME];
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300319 char *key, *ptr;
Eric Biggers72f9a072017-12-08 15:13:29 +0000320 int ret;
David Howellsd43de6c2016-03-03 21:49:27 +0000321
322 pr_devel("==>%s()\n", __func__);
323
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -0800324 BUG_ON(!pkey);
David Howells3d167d62013-08-30 16:15:30 +0100325 BUG_ON(!sig);
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -0800326 BUG_ON(!sig->s);
David Howells3d167d62013-08-30 16:15:30 +0100327
David Howells82f94f22018-10-09 17:47:23 +0100328 ret = software_key_determine_akcipher(sig->encoding,
329 sig->hash_algo,
330 pkey, alg_name);
331 if (ret < 0)
332 return ret;
David Howells3d167d62013-08-30 16:15:30 +0100333
David Howellsd43de6c2016-03-03 21:49:27 +0000334 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
335 if (IS_ERR(tfm))
336 return PTR_ERR(tfm);
David Howellsa9681bf2012-09-21 23:24:55 +0100337
Eric Biggers72f9a072017-12-08 15:13:29 +0000338 ret = -ENOMEM;
David Howellsd43de6c2016-03-03 21:49:27 +0000339 req = akcipher_request_alloc(tfm, GFP_KERNEL);
340 if (!req)
341 goto error_free_tfm;
342
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300343 key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
344 GFP_KERNEL);
345 if (!key)
David Howellsd43de6c2016-03-03 21:49:27 +0000346 goto error_free_req;
347
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300348 memcpy(key, pkey->key, pkey->keylen);
349 ptr = key + pkey->keylen;
350 ptr = pkey_pack_u32(ptr, pkey->algo);
351 ptr = pkey_pack_u32(ptr, pkey->paramlen);
352 memcpy(ptr, pkey->params, pkey->paramlen);
353
354 if (pkey->key_is_private)
355 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
356 else
357 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
358 if (ret)
359 goto error_free_key;
360
Tianjia Zhang7178a102021-01-19 00:13:19 +0000361 if (sig->pkey_algo && strcmp(sig->pkey_algo, "sm2") == 0 &&
362 sig->data_size) {
Tianjia Zhang21552562020-09-21 00:21:02 +0800363 ret = cert_sig_digest_update(sig, tfm);
364 if (ret)
365 goto error_free_key;
366 }
367
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300368 sg_init_table(src_sg, 2);
369 sg_set_buf(&src_sg[0], sig->s, sig->s_size);
Vitaly Chikunov83bc0292019-04-11 18:51:16 +0300370 sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
Vitaly Chikunovc7381b02019-04-11 18:51:15 +0300371 akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
372 sig->digest_size);
Gilad Ben-Yossef0ca2a042017-10-18 08:00:40 +0100373 crypto_init_wait(&cwait);
David Howellsd43de6c2016-03-03 21:49:27 +0000374 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
375 CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossef0ca2a042017-10-18 08:00:40 +0100376 crypto_req_done, &cwait);
Gilad Ben-Yossef0ca2a042017-10-18 08:00:40 +0100377 ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
David Howellsd43de6c2016-03-03 21:49:27 +0000378
Vitaly Chikunovf1774cb2019-04-11 18:51:17 +0300379error_free_key:
380 kfree(key);
David Howellsd43de6c2016-03-03 21:49:27 +0000381error_free_req:
382 akcipher_request_free(req);
383error_free_tfm:
384 crypto_free_akcipher(tfm);
385 pr_devel("<==%s() = %d\n", __func__, ret);
Eric Biggers72f9a072017-12-08 15:13:29 +0000386 if (WARN_ON_ONCE(ret > 0))
387 ret = -EINVAL;
David Howellsd43de6c2016-03-03 21:49:27 +0000388 return ret;
David Howells3d167d62013-08-30 16:15:30 +0100389}
390EXPORT_SYMBOL_GPL(public_key_verify_signature);
391
392static int public_key_verify_signature_2(const struct key *key,
393 const struct public_key_signature *sig)
394{
David Howells146aa8b2015-10-21 14:04:48 +0100395 const struct public_key *pk = key->payload.data[asym_crypto];
David Howells3d167d62013-08-30 16:15:30 +0100396 return public_key_verify_signature(pk, sig);
David Howellsa9681bf2012-09-21 23:24:55 +0100397}
398
399/*
400 * Public key algorithm asymmetric key subtype
401 */
402struct asymmetric_key_subtype public_key_subtype = {
403 .owner = THIS_MODULE,
404 .name = "public_key",
David Howells876c6e32014-09-02 13:52:10 +0100405 .name_len = sizeof("public_key") - 1,
David Howellsa9681bf2012-09-21 23:24:55 +0100406 .describe = public_key_describe,
407 .destroy = public_key_destroy,
David Howells82f94f22018-10-09 17:47:23 +0100408 .query = software_key_query,
David Howellsc08fed72018-10-09 17:47:38 +0100409 .eds_op = software_key_eds_op,
David Howells3d167d62013-08-30 16:15:30 +0100410 .verify_signature = public_key_verify_signature_2,
David Howellsa9681bf2012-09-21 23:24:55 +0100411};
412EXPORT_SYMBOL_GPL(public_key_subtype);