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