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