1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* In-software asymmetric public-key crypto subtype
4 * See Documentation/crypto/asymmetric-keys.rst
6 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
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 #include <crypto/sm2.h>
21 #include <crypto/sm3_base.h>
23 MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
24 MODULE_AUTHOR("Red Hat, Inc.");
25 MODULE_LICENSE("GPL");
28 * Provide a part of a description of the key for /proc/keys.
30 static void public_key_describe(const struct key *asymmetric_key,
33 struct public_key *key = asymmetric_key->payload.data[asym_crypto];
36 seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
40 * Destroy a public key algorithm key.
42 void public_key_free(struct public_key *key)
50 EXPORT_SYMBOL_GPL(public_key_free);
53 * Destroy a public key algorithm key.
55 static void public_key_destroy(void *payload0, void *payload3)
57 public_key_free(payload0);
58 public_key_signature_free(payload3);
62 * Determine the crypto algorithm name.
65 int software_key_determine_akcipher(const char *encoding,
66 const char *hash_algo,
67 const struct public_key *pkey,
68 char alg_name[CRYPTO_MAX_ALG_NAME])
72 if (strcmp(encoding, "pkcs1") == 0) {
73 /* The data wangled by the RSA algorithm is typically padded
74 * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
78 n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
82 n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
84 pkey->pkey_algo, hash_algo);
85 return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
88 if (strcmp(encoding, "raw") == 0) {
89 strcpy(alg_name, pkey->pkey_algo);
96 static u8 *pkey_pack_u32(u8 *dst, u32 val)
98 memcpy(dst, &val, sizeof(val));
99 return dst + sizeof(val);
103 * Query information about a key.
105 static int software_key_query(const struct kernel_pkey_params *params,
106 struct kernel_pkey_query *info)
108 struct crypto_akcipher *tfm;
109 struct public_key *pkey = params->key->payload.data[asym_crypto];
110 char alg_name[CRYPTO_MAX_ALG_NAME];
114 ret = software_key_determine_akcipher(params->encoding,
120 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
125 key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
129 memcpy(key, pkey->key, pkey->keylen);
130 ptr = key + pkey->keylen;
131 ptr = pkey_pack_u32(ptr, pkey->algo);
132 ptr = pkey_pack_u32(ptr, pkey->paramlen);
133 memcpy(ptr, pkey->params, pkey->paramlen);
135 if (pkey->key_is_private)
136 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
138 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
142 len = crypto_akcipher_maxsize(tfm);
143 info->key_size = len * 8;
144 info->max_data_size = len;
145 info->max_sig_size = len;
146 info->max_enc_size = len;
147 info->max_dec_size = len;
148 info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
149 KEYCTL_SUPPORTS_VERIFY);
150 if (pkey->key_is_private)
151 info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
152 KEYCTL_SUPPORTS_SIGN);
158 crypto_free_akcipher(tfm);
159 pr_devel("<==%s() = %d\n", __func__, ret);
164 * Do encryption, decryption and signing ops.
166 static int software_key_eds_op(struct kernel_pkey_params *params,
167 const void *in, void *out)
169 const struct public_key *pkey = params->key->payload.data[asym_crypto];
170 struct akcipher_request *req;
171 struct crypto_akcipher *tfm;
172 struct crypto_wait cwait;
173 struct scatterlist in_sg, out_sg;
174 char alg_name[CRYPTO_MAX_ALG_NAME];
178 pr_devel("==>%s()\n", __func__);
180 ret = software_key_determine_akcipher(params->encoding,
186 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
191 req = akcipher_request_alloc(tfm, GFP_KERNEL);
195 key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
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);
206 if (pkey->key_is_private)
207 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
209 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
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,
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);
222 /* Perform the encryption calculation. */
223 switch (params->op) {
224 case kernel_pkey_encrypt:
225 ret = crypto_akcipher_encrypt(req);
227 case kernel_pkey_decrypt:
228 ret = crypto_akcipher_decrypt(req);
230 case kernel_pkey_sign:
231 ret = crypto_akcipher_sign(req);
237 ret = crypto_wait_req(ret, &cwait);
244 akcipher_request_free(req);
246 crypto_free_akcipher(tfm);
247 pr_devel("<==%s() = %d\n", __func__, ret);
251 #if IS_REACHABLE(CONFIG_CRYPTO_SM2)
252 static int cert_sig_digest_update(const struct public_key_signature *sig,
253 struct crypto_akcipher *tfm_pkey)
255 struct crypto_shash *tfm;
256 struct shash_desc *desc;
258 unsigned char dgst[SM3_DIGEST_SIZE];
263 /* SM2 signatures always use the SM3 hash algorithm */
264 if (!sig->hash_algo || strcmp(sig->hash_algo, "sm3") != 0)
267 ret = sm2_compute_z_digest(tfm_pkey, SM2_DEFAULT_USERID,
268 SM2_DEFAULT_USERID_LEN, dgst);
272 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
276 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
277 desc = kzalloc(desc_size, GFP_KERNEL);
285 ret = crypto_shash_init(desc);
287 goto error_free_desc;
289 ret = crypto_shash_update(desc, dgst, SM3_DIGEST_SIZE);
291 goto error_free_desc;
293 ret = crypto_shash_finup(desc, sig->data, sig->data_size, sig->digest);
298 crypto_free_shash(tfm);
302 static inline int cert_sig_digest_update(
303 const struct public_key_signature *sig,
304 struct crypto_akcipher *tfm_pkey)
308 #endif /* ! IS_REACHABLE(CONFIG_CRYPTO_SM2) */
311 * Verify a signature using a public key.
313 int public_key_verify_signature(const struct public_key *pkey,
314 const struct public_key_signature *sig)
316 struct crypto_wait cwait;
317 struct crypto_akcipher *tfm;
318 struct akcipher_request *req;
319 struct scatterlist src_sg;
320 char alg_name[CRYPTO_MAX_ALG_NAME];
325 pr_devel("==>%s()\n", __func__);
331 ret = software_key_determine_akcipher(sig->encoding,
337 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
342 req = akcipher_request_alloc(tfm, GFP_KERNEL);
346 buf_len = max_t(size_t, pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
347 sig->s_size + sig->digest_size);
349 buf = kmalloc(buf_len, GFP_KERNEL);
353 memcpy(buf, pkey->key, pkey->keylen);
354 ptr = buf + pkey->keylen;
355 ptr = pkey_pack_u32(ptr, pkey->algo);
356 ptr = pkey_pack_u32(ptr, pkey->paramlen);
357 memcpy(ptr, pkey->params, pkey->paramlen);
359 if (pkey->key_is_private)
360 ret = crypto_akcipher_set_priv_key(tfm, buf, pkey->keylen);
362 ret = crypto_akcipher_set_pub_key(tfm, buf, pkey->keylen);
366 if (strcmp(pkey->pkey_algo, "sm2") == 0 && sig->data_size) {
367 ret = cert_sig_digest_update(sig, tfm);
372 memcpy(buf, sig->s, sig->s_size);
373 memcpy(buf + sig->s_size, sig->digest, sig->digest_size);
375 sg_init_one(&src_sg, buf, sig->s_size + sig->digest_size);
376 akcipher_request_set_crypt(req, &src_sg, NULL, sig->s_size,
378 crypto_init_wait(&cwait);
379 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
380 CRYPTO_TFM_REQ_MAY_SLEEP,
381 crypto_req_done, &cwait);
382 ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
387 akcipher_request_free(req);
389 crypto_free_akcipher(tfm);
390 pr_devel("<==%s() = %d\n", __func__, ret);
391 if (WARN_ON_ONCE(ret > 0))
395 EXPORT_SYMBOL_GPL(public_key_verify_signature);
397 static int public_key_verify_signature_2(const struct key *key,
398 const struct public_key_signature *sig)
400 const struct public_key *pk = key->payload.data[asym_crypto];
401 return public_key_verify_signature(pk, sig);
405 * Public key algorithm asymmetric key subtype
407 struct asymmetric_key_subtype public_key_subtype = {
408 .owner = THIS_MODULE,
409 .name = "public_key",
410 .name_len = sizeof("public_key") - 1,
411 .describe = public_key_describe,
412 .destroy = public_key_destroy,
413 .query = software_key_query,
414 .eds_op = software_key_eds_op,
415 .verify_signature = public_key_verify_signature_2,
417 EXPORT_SYMBOL_GPL(public_key_subtype);