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