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