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 <linux/asn1.h>
18 #include <keys/asymmetric-subtype.h>
19 #include <crypto/public_key.h>
20 #include <crypto/akcipher.h>
21 #include <crypto/sm2.h>
22 #include <crypto/sm3_base.h>
23 
24 MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
25 MODULE_AUTHOR("Red Hat, Inc.");
26 MODULE_LICENSE("GPL");
27 
28 /*
29  * Provide a part of a description of the key for /proc/keys.
30  */
public_key_describe(const struct key * asymmetric_key,struct seq_file * m)31 static void public_key_describe(const struct key *asymmetric_key,
32 				struct seq_file *m)
33 {
34 	struct public_key *key = asymmetric_key->payload.data[asym_crypto];
35 
36 	if (key)
37 		seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
38 }
39 
40 /*
41  * Destroy a public key algorithm key.
42  */
public_key_free(struct public_key * key)43 void public_key_free(struct public_key *key)
44 {
45 	if (key) {
46 		kfree(key->key);
47 		kfree(key->params);
48 		kfree(key);
49 	}
50 }
51 EXPORT_SYMBOL_GPL(public_key_free);
52 
53 /*
54  * Destroy a public key algorithm key.
55  */
public_key_destroy(void * payload0,void * payload3)56 static void public_key_destroy(void *payload0, void *payload3)
57 {
58 	public_key_free(payload0);
59 	public_key_signature_free(payload3);
60 }
61 
62 /*
63  * Determine the crypto algorithm name.
64  */
65 static
software_key_determine_akcipher(const char * encoding,const char * hash_algo,const struct public_key * pkey,char alg_name[CRYPTO_MAX_ALG_NAME])66 int 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 
89 	if (strcmp(encoding, "raw") == 0 ||
90 	    strcmp(encoding, "x962") == 0) {
91 		strcpy(alg_name, pkey->pkey_algo);
92 		return 0;
93 	}
94 
95 	return -ENOPKG;
96 }
97 
pkey_pack_u32(u8 * dst,u32 val)98 static u8 *pkey_pack_u32(u8 *dst, u32 val)
99 {
100 	memcpy(dst, &val, sizeof(val));
101 	return dst + sizeof(val);
102 }
103 
104 /*
105  * Query information about a key.
106  */
software_key_query(const struct kernel_pkey_params * params,struct kernel_pkey_query * info)107 static 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];
113 	u8 *key, *ptr;
114 	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 
126 	ret = -ENOMEM;
127 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
128 		      GFP_KERNEL);
129 	if (!key)
130 		goto error_free_tfm;
131 	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;
143 
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;
150 	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);
155 	ret = 0;
156 
157 error_free_key:
158 	kfree(key);
159 error_free_tfm:
160 	crypto_free_akcipher(tfm);
161 	pr_devel("<==%s() = %d\n", __func__, ret);
162 	return ret;
163 }
164 
165 /*
166  * Do encryption, decryption and signing ops.
167  */
software_key_eds_op(struct kernel_pkey_params * params,const void * in,void * out)168 static 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];
177 	char *key, *ptr;
178 	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 
192 	ret = -ENOMEM;
193 	req = akcipher_request_alloc(tfm, GFP_KERNEL);
194 	if (!req)
195 		goto error_free_tfm;
196 
197 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
198 		      GFP_KERNEL);
199 	if (!key)
200 		goto error_free_req;
201 
202 	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 
215 	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 
243 error_free_key:
244 	kfree(key);
245 error_free_req:
246 	akcipher_request_free(req);
247 error_free_tfm:
248 	crypto_free_akcipher(tfm);
249 	pr_devel("<==%s() = %d\n", __func__, ret);
250 	return ret;
251 }
252 
253 #if IS_REACHABLE(CONFIG_CRYPTO_SM2)
cert_sig_digest_update(const struct public_key_signature * sig,struct crypto_akcipher * tfm_pkey)254 static 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 
293 error_free_desc:
294 	kfree(desc);
295 error_free_tfm:
296 	crypto_free_shash(tfm);
297 	return ret;
298 }
299 #else
cert_sig_digest_update(const struct public_key_signature * sig,struct crypto_akcipher * tfm_pkey)300 static 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 
308 /*
309  * Verify a signature using a public key.
310  */
public_key_verify_signature(const struct public_key * pkey,const struct public_key_signature * sig)311 int public_key_verify_signature(const struct public_key *pkey,
312 				const struct public_key_signature *sig)
313 {
314 	struct crypto_wait cwait;
315 	struct crypto_akcipher *tfm;
316 	struct akcipher_request *req;
317 	struct scatterlist src_sg[2];
318 	char alg_name[CRYPTO_MAX_ALG_NAME];
319 	char *key, *ptr;
320 	int ret;
321 
322 	pr_devel("==>%s()\n", __func__);
323 
324 	BUG_ON(!pkey);
325 	BUG_ON(!sig);
326 	BUG_ON(!sig->s);
327 
328 	ret = software_key_determine_akcipher(sig->encoding,
329 					      sig->hash_algo,
330 					      pkey, alg_name);
331 	if (ret < 0)
332 		return ret;
333 
334 	tfm = crypto_alloc_akcipher(alg_name, 0, 0);
335 	if (IS_ERR(tfm))
336 		return PTR_ERR(tfm);
337 
338 	ret = -ENOMEM;
339 	req = akcipher_request_alloc(tfm, GFP_KERNEL);
340 	if (!req)
341 		goto error_free_tfm;
342 
343 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
344 		      GFP_KERNEL);
345 	if (!key)
346 		goto error_free_req;
347 
348 	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 
361 	if (sig->pkey_algo && strcmp(sig->pkey_algo, "sm2") == 0 &&
362 	    sig->data_size) {
363 		ret = cert_sig_digest_update(sig, tfm);
364 		if (ret)
365 			goto error_free_key;
366 	}
367 
368 	sg_init_table(src_sg, 2);
369 	sg_set_buf(&src_sg[0], sig->s, sig->s_size);
370 	sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
371 	akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
372 				   sig->digest_size);
373 	crypto_init_wait(&cwait);
374 	akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
375 				      CRYPTO_TFM_REQ_MAY_SLEEP,
376 				      crypto_req_done, &cwait);
377 	ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
378 
379 error_free_key:
380 	kfree(key);
381 error_free_req:
382 	akcipher_request_free(req);
383 error_free_tfm:
384 	crypto_free_akcipher(tfm);
385 	pr_devel("<==%s() = %d\n", __func__, ret);
386 	if (WARN_ON_ONCE(ret > 0))
387 		ret = -EINVAL;
388 	return ret;
389 }
390 EXPORT_SYMBOL_GPL(public_key_verify_signature);
391 
public_key_verify_signature_2(const struct key * key,const struct public_key_signature * sig)392 static int public_key_verify_signature_2(const struct key *key,
393 					 const struct public_key_signature *sig)
394 {
395 	const struct public_key *pk = key->payload.data[asym_crypto];
396 	return public_key_verify_signature(pk, sig);
397 }
398 
399 /*
400  * Public key algorithm asymmetric key subtype
401  */
402 struct asymmetric_key_subtype public_key_subtype = {
403 	.owner			= THIS_MODULE,
404 	.name			= "public_key",
405 	.name_len		= sizeof("public_key") - 1,
406 	.describe		= public_key_describe,
407 	.destroy		= public_key_destroy,
408 	.query			= software_key_query,
409 	.eds_op			= software_key_eds_op,
410 	.verify_signature	= public_key_verify_signature_2,
411 };
412 EXPORT_SYMBOL_GPL(public_key_subtype);
413