1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* In-software asymmetric public-key crypto subtype
3  *
4  * See Documentation/crypto/asymmetric-keys.txt
5  *
6  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
7  * Written by David Howells (dhowells@redhat.com)
8  */
9 
10 #define pr_fmt(fmt) "PKEY: "fmt
11 #ifdef __UBOOT__
12 #include <dm/devres.h>
13 #include <linux/bug.h>
14 #include <linux/compat.h>
15 #include <linux/err.h>
16 #include <linux/printk.h>
17 #else
18 #include <linux/module.h>
19 #include <linux/export.h>
20 #endif
21 #include <linux/kernel.h>
22 #ifndef __UBOOT__
23 #include <linux/slab.h>
24 #include <linux/seq_file.h>
25 #include <linux/scatterlist.h>
26 #include <keys/asymmetric-subtype.h>
27 #endif
28 #include <crypto/public_key.h>
29 #ifdef __UBOOT__
30 #include <image.h>
31 #include <u-boot/rsa.h>
32 #else
33 #include <crypto/akcipher.h>
34 #endif
35 
36 MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
37 MODULE_AUTHOR("Red Hat, Inc.");
38 MODULE_LICENSE("GPL");
39 
40 #ifndef __UBOOT__
41 /*
42  * Provide a part of a description of the key for /proc/keys.
43  */
public_key_describe(const struct key * asymmetric_key,struct seq_file * m)44 static void public_key_describe(const struct key *asymmetric_key,
45 				struct seq_file *m)
46 {
47 	struct public_key *key = asymmetric_key->payload.data[asym_crypto];
48 
49 	if (key)
50 		seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
51 }
52 #endif
53 
54 #ifdef __UBOOT__
55 
56 /**
57  * public_key_verify_signature - Verify a signature using a public key.
58  *
59  * @pkey:	Public key
60  * @sig:	Signature
61  *
62  * Verify a signature, @sig, using a RSA public key, @pkey.
63  *
64  * Return:	0 - verified, non-zero error code - otherwise
65  */
public_key_verify_signature(const struct public_key * pkey,const struct public_key_signature * sig)66 int public_key_verify_signature(const struct public_key *pkey,
67 				const struct public_key_signature *sig)
68 {
69 	struct image_sign_info info;
70 	char algo[256];
71 	int ret;
72 
73 	pr_devel("==>%s()\n", __func__);
74 
75 	if (!pkey || !sig)
76 		return -EINVAL;
77 
78 	if (pkey->key_is_private)
79 		return -EINVAL;
80 
81 	memset(&info, '\0', sizeof(info));
82 	memset(algo, 0, sizeof(algo));
83 	info.padding = image_get_padding_algo("pkcs-1.5");
84 	if (strcmp(sig->pkey_algo, "rsa")) {
85 		pr_err("Encryption is not RSA: %s\n", sig->pkey_algo);
86 		return -ENOPKG;
87 	}
88 	ret = snprintf(algo, sizeof(algo), "%s,%s%d", sig->hash_algo,
89 		       sig->pkey_algo, sig->s_size * 8);
90 
91 	if (ret >= sizeof(algo))
92 		return -EINVAL;
93 
94 	info.checksum = image_get_checksum_algo((const char *)algo);
95 	info.name = (const char *)algo;
96 	info.crypto = image_get_crypto_algo(info.name);
97 	if (!info.checksum || !info.crypto) {
98 		pr_err("<%s> not supported on image_get_(checksum|crypto)_algo()\n",
99 		       algo);
100 		return -ENOPKG;
101 	}
102 
103 	info.key = pkey->key;
104 	info.keylen = pkey->keylen;
105 
106 	if (rsa_verify_with_pkey(&info, sig->digest, sig->s, sig->s_size))
107 		ret = -EKEYREJECTED;
108 	else
109 		ret = 0;
110 
111 	pr_devel("<==%s() = %d\n", __func__, ret);
112 	return ret;
113 }
114 #else
115 /*
116  * Destroy a public key algorithm key.
117  */
public_key_destroy(void * payload0,void * payload3)118 static void public_key_destroy(void *payload0, void *payload3)
119 {
120 	public_key_free(payload0);
121 	public_key_signature_free(payload3);
122 }
123 
124 /*
125  * Determine the crypto algorithm name.
126  */
127 static
software_key_determine_akcipher(const char * encoding,const char * hash_algo,const struct public_key * pkey,char alg_name[CRYPTO_MAX_ALG_NAME])128 int software_key_determine_akcipher(const char *encoding,
129 				    const char *hash_algo,
130 				    const struct public_key *pkey,
131 				    char alg_name[CRYPTO_MAX_ALG_NAME])
132 {
133 	int n;
134 
135 	if (strcmp(encoding, "pkcs1") == 0) {
136 		/* The data wangled by the RSA algorithm is typically padded
137 		 * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
138 		 * sec 8.2].
139 		 */
140 		if (!hash_algo)
141 			n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
142 				     "pkcs1pad(%s)",
143 				     pkey->pkey_algo);
144 		else
145 			n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
146 				     "pkcs1pad(%s,%s)",
147 				     pkey->pkey_algo, hash_algo);
148 		return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
149 	}
150 
151 	if (strcmp(encoding, "raw") == 0) {
152 		strcpy(alg_name, pkey->pkey_algo);
153 		return 0;
154 	}
155 
156 	return -ENOPKG;
157 }
158 
pkey_pack_u32(u8 * dst,u32 val)159 static u8 *pkey_pack_u32(u8 *dst, u32 val)
160 {
161 	memcpy(dst, &val, sizeof(val));
162 	return dst + sizeof(val);
163 }
164 
165 /*
166  * Query information about a key.
167  */
software_key_query(const struct kernel_pkey_params * params,struct kernel_pkey_query * info)168 static int software_key_query(const struct kernel_pkey_params *params,
169 			      struct kernel_pkey_query *info)
170 {
171 	struct crypto_akcipher *tfm;
172 	struct public_key *pkey = params->key->payload.data[asym_crypto];
173 	char alg_name[CRYPTO_MAX_ALG_NAME];
174 	u8 *key, *ptr;
175 	int ret, len;
176 
177 	ret = software_key_determine_akcipher(params->encoding,
178 					      params->hash_algo,
179 					      pkey, alg_name);
180 	if (ret < 0)
181 		return ret;
182 
183 	tfm = crypto_alloc_akcipher(alg_name, 0, 0);
184 	if (IS_ERR(tfm))
185 		return PTR_ERR(tfm);
186 
187 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
188 		      GFP_KERNEL);
189 	if (!key)
190 		goto error_free_tfm;
191 	memcpy(key, pkey->key, pkey->keylen);
192 	ptr = key + pkey->keylen;
193 	ptr = pkey_pack_u32(ptr, pkey->algo);
194 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
195 	memcpy(ptr, pkey->params, pkey->paramlen);
196 
197 	if (pkey->key_is_private)
198 		ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
199 	else
200 		ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
201 	if (ret < 0)
202 		goto error_free_key;
203 
204 	len = crypto_akcipher_maxsize(tfm);
205 	info->key_size = len * 8;
206 	info->max_data_size = len;
207 	info->max_sig_size = len;
208 	info->max_enc_size = len;
209 	info->max_dec_size = len;
210 	info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
211 			       KEYCTL_SUPPORTS_VERIFY);
212 	if (pkey->key_is_private)
213 		info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
214 					KEYCTL_SUPPORTS_SIGN);
215 	ret = 0;
216 
217 error_free_key:
218 	kfree(key);
219 error_free_tfm:
220 	crypto_free_akcipher(tfm);
221 	pr_devel("<==%s() = %d\n", __func__, ret);
222 	return ret;
223 }
224 
225 /*
226  * Do encryption, decryption and signing ops.
227  */
software_key_eds_op(struct kernel_pkey_params * params,const void * in,void * out)228 static int software_key_eds_op(struct kernel_pkey_params *params,
229 			       const void *in, void *out)
230 {
231 	const struct public_key *pkey = params->key->payload.data[asym_crypto];
232 	struct akcipher_request *req;
233 	struct crypto_akcipher *tfm;
234 	struct crypto_wait cwait;
235 	struct scatterlist in_sg, out_sg;
236 	char alg_name[CRYPTO_MAX_ALG_NAME];
237 	char *key, *ptr;
238 	int ret;
239 
240 	pr_devel("==>%s()\n", __func__);
241 
242 	ret = software_key_determine_akcipher(params->encoding,
243 					      params->hash_algo,
244 					      pkey, alg_name);
245 	if (ret < 0)
246 		return ret;
247 
248 	tfm = crypto_alloc_akcipher(alg_name, 0, 0);
249 	if (IS_ERR(tfm))
250 		return PTR_ERR(tfm);
251 
252 	req = akcipher_request_alloc(tfm, GFP_KERNEL);
253 	if (!req)
254 		goto error_free_tfm;
255 
256 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
257 		      GFP_KERNEL);
258 	if (!key)
259 		goto error_free_req;
260 
261 	memcpy(key, pkey->key, pkey->keylen);
262 	ptr = key + pkey->keylen;
263 	ptr = pkey_pack_u32(ptr, pkey->algo);
264 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
265 	memcpy(ptr, pkey->params, pkey->paramlen);
266 
267 	if (pkey->key_is_private)
268 		ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
269 	else
270 		ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
271 	if (ret)
272 		goto error_free_key;
273 
274 	sg_init_one(&in_sg, in, params->in_len);
275 	sg_init_one(&out_sg, out, params->out_len);
276 	akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
277 				   params->out_len);
278 	crypto_init_wait(&cwait);
279 	akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
280 				      CRYPTO_TFM_REQ_MAY_SLEEP,
281 				      crypto_req_done, &cwait);
282 
283 	/* Perform the encryption calculation. */
284 	switch (params->op) {
285 	case kernel_pkey_encrypt:
286 		ret = crypto_akcipher_encrypt(req);
287 		break;
288 	case kernel_pkey_decrypt:
289 		ret = crypto_akcipher_decrypt(req);
290 		break;
291 	case kernel_pkey_sign:
292 		ret = crypto_akcipher_sign(req);
293 		break;
294 	default:
295 		BUG();
296 	}
297 
298 	ret = crypto_wait_req(ret, &cwait);
299 	if (ret == 0)
300 		ret = req->dst_len;
301 
302 error_free_key:
303 	kfree(key);
304 error_free_req:
305 	akcipher_request_free(req);
306 error_free_tfm:
307 	crypto_free_akcipher(tfm);
308 	pr_devel("<==%s() = %d\n", __func__, ret);
309 	return ret;
310 }
311 
312 /*
313  * Verify a signature using a public key.
314  */
public_key_verify_signature(const struct public_key * pkey,const struct public_key_signature * sig)315 int public_key_verify_signature(const struct public_key *pkey,
316 				const struct public_key_signature *sig)
317 {
318 	struct crypto_wait cwait;
319 	struct crypto_akcipher *tfm;
320 	struct akcipher_request *req;
321 	struct scatterlist src_sg[2];
322 	char alg_name[CRYPTO_MAX_ALG_NAME];
323 	char *key, *ptr;
324 	int ret;
325 
326 	pr_devel("==>%s()\n", __func__);
327 
328 	BUG_ON(!pkey);
329 	BUG_ON(!sig);
330 	BUG_ON(!sig->s);
331 
332 	ret = software_key_determine_akcipher(sig->encoding,
333 					      sig->hash_algo,
334 					      pkey, alg_name);
335 	if (ret < 0)
336 		return ret;
337 
338 	tfm = crypto_alloc_akcipher(alg_name, 0, 0);
339 	if (IS_ERR(tfm))
340 		return PTR_ERR(tfm);
341 
342 	ret = -ENOMEM;
343 	req = akcipher_request_alloc(tfm, GFP_KERNEL);
344 	if (!req)
345 		goto error_free_tfm;
346 
347 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
348 		      GFP_KERNEL);
349 	if (!key)
350 		goto error_free_req;
351 
352 	memcpy(key, pkey->key, pkey->keylen);
353 	ptr = key + pkey->keylen;
354 	ptr = pkey_pack_u32(ptr, pkey->algo);
355 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
356 	memcpy(ptr, pkey->params, pkey->paramlen);
357 
358 	if (pkey->key_is_private)
359 		ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
360 	else
361 		ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
362 	if (ret)
363 		goto error_free_key;
364 
365 	sg_init_table(src_sg, 2);
366 	sg_set_buf(&src_sg[0], sig->s, sig->s_size);
367 	sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
368 	akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
369 				   sig->digest_size);
370 	crypto_init_wait(&cwait);
371 	akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
372 				      CRYPTO_TFM_REQ_MAY_SLEEP,
373 				      crypto_req_done, &cwait);
374 	ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
375 
376 error_free_key:
377 	kfree(key);
378 error_free_req:
379 	akcipher_request_free(req);
380 error_free_tfm:
381 	crypto_free_akcipher(tfm);
382 	pr_devel("<==%s() = %d\n", __func__, ret);
383 	if (WARN_ON_ONCE(ret > 0))
384 		ret = -EINVAL;
385 	return ret;
386 }
387 EXPORT_SYMBOL_GPL(public_key_verify_signature);
388 
public_key_verify_signature_2(const struct key * key,const struct public_key_signature * sig)389 static int public_key_verify_signature_2(const struct key *key,
390 					 const struct public_key_signature *sig)
391 {
392 	const struct public_key *pk = key->payload.data[asym_crypto];
393 	return public_key_verify_signature(pk, sig);
394 }
395 
396 /*
397  * Public key algorithm asymmetric key subtype
398  */
399 struct asymmetric_key_subtype public_key_subtype = {
400 	.owner			= THIS_MODULE,
401 	.name			= "public_key",
402 	.name_len		= sizeof("public_key") - 1,
403 	.describe		= public_key_describe,
404 	.destroy		= public_key_destroy,
405 	.query			= software_key_query,
406 	.eds_op			= software_key_eds_op,
407 	.verify_signature	= public_key_verify_signature_2,
408 };
409 EXPORT_SYMBOL_GPL(public_key_subtype);
410 #endif /* !__UBOOT__ */
411