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