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 <linux/asn1.h>
18 #include <keys/asymmetric-subtype.h>
19 #include <crypto/public_key.h>
20 #include <crypto/akcipher.h>
21 #include <crypto/sm2.h>
22 #include <crypto/sm3_base.h>
24 MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
25 MODULE_AUTHOR("Red Hat, Inc.");
26 MODULE_LICENSE("GPL");
29 * Provide a part of a description of the key for /proc/keys.
31 static void public_key_describe(const struct key *asymmetric_key,
34 struct public_key *key = asymmetric_key->payload.data[asym_crypto];
37 seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
41 * Destroy a public key algorithm key.
43 void public_key_free(struct public_key *key)
51 EXPORT_SYMBOL_GPL(public_key_free);
54 * Destroy a public key algorithm key.
56 static void public_key_destroy(void *payload0, void *payload3)
58 public_key_free(payload0);
59 public_key_signature_free(payload3);
63 * Given a public_key, and an encoding and hash_algo to be used for signing
64 * and/or verification with that key, determine the name of the corresponding
65 * akcipher algorithm. Also check that encoding and hash_algo are allowed.
68 software_key_determine_akcipher(const struct public_key *pkey,
69 const char *encoding, const char *hash_algo,
70 char alg_name[CRYPTO_MAX_ALG_NAME])
77 if (strcmp(pkey->pkey_algo, "rsa") == 0) {
79 * RSA signatures usually use EMSA-PKCS1-1_5 [RFC3447 sec 8.2].
81 if (strcmp(encoding, "pkcs1") == 0) {
83 n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
87 n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
89 pkey->pkey_algo, hash_algo);
90 return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
92 if (strcmp(encoding, "raw") != 0)
95 * Raw RSA cannot differentiate between different hash
100 } else if (strncmp(pkey->pkey_algo, "ecdsa", 5) == 0) {
101 if (strcmp(encoding, "x962") != 0)
104 * ECDSA signatures are taken over a raw hash, so they don't
105 * differentiate between different hash algorithms. That means
106 * that the verifier should hard-code a specific hash algorithm.
107 * Unfortunately, in practice ECDSA is used with multiple SHAs,
108 * so we have to allow all of them and not just one.
112 if (strcmp(hash_algo, "sha1") != 0 &&
113 strcmp(hash_algo, "sha224") != 0 &&
114 strcmp(hash_algo, "sha256") != 0 &&
115 strcmp(hash_algo, "sha384") != 0 &&
116 strcmp(hash_algo, "sha512") != 0)
118 } else if (strcmp(pkey->pkey_algo, "sm2") == 0) {
119 if (strcmp(encoding, "raw") != 0)
123 if (strcmp(hash_algo, "sm3") != 0)
125 } else if (strcmp(pkey->pkey_algo, "ecrdsa") == 0) {
126 if (strcmp(encoding, "raw") != 0)
130 if (strcmp(hash_algo, "streebog256") != 0 &&
131 strcmp(hash_algo, "streebog512") != 0)
134 /* Unknown public key algorithm */
137 if (strscpy(alg_name, pkey->pkey_algo, CRYPTO_MAX_ALG_NAME) < 0)
142 static u8 *pkey_pack_u32(u8 *dst, u32 val)
144 memcpy(dst, &val, sizeof(val));
145 return dst + sizeof(val);
149 * Query information about a key.
151 static int software_key_query(const struct kernel_pkey_params *params,
152 struct kernel_pkey_query *info)
154 struct crypto_akcipher *tfm;
155 struct public_key *pkey = params->key->payload.data[asym_crypto];
156 char alg_name[CRYPTO_MAX_ALG_NAME];
160 ret = software_key_determine_akcipher(pkey, params->encoding,
161 params->hash_algo, alg_name);
165 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
170 key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
174 memcpy(key, pkey->key, pkey->keylen);
175 ptr = key + pkey->keylen;
176 ptr = pkey_pack_u32(ptr, pkey->algo);
177 ptr = pkey_pack_u32(ptr, pkey->paramlen);
178 memcpy(ptr, pkey->params, pkey->paramlen);
180 if (pkey->key_is_private)
181 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
183 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
187 len = crypto_akcipher_maxsize(tfm);
188 info->key_size = len * 8;
189 info->max_data_size = len;
190 info->max_sig_size = len;
191 info->max_enc_size = len;
192 info->max_dec_size = len;
193 info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
194 KEYCTL_SUPPORTS_VERIFY);
195 if (pkey->key_is_private)
196 info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
197 KEYCTL_SUPPORTS_SIGN);
203 crypto_free_akcipher(tfm);
204 pr_devel("<==%s() = %d\n", __func__, ret);
209 * Do encryption, decryption and signing ops.
211 static int software_key_eds_op(struct kernel_pkey_params *params,
212 const void *in, void *out)
214 const struct public_key *pkey = params->key->payload.data[asym_crypto];
215 struct akcipher_request *req;
216 struct crypto_akcipher *tfm;
217 struct crypto_wait cwait;
218 struct scatterlist in_sg, out_sg;
219 char alg_name[CRYPTO_MAX_ALG_NAME];
223 pr_devel("==>%s()\n", __func__);
225 ret = software_key_determine_akcipher(pkey, params->encoding,
226 params->hash_algo, alg_name);
230 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
235 req = akcipher_request_alloc(tfm, GFP_KERNEL);
239 key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
244 memcpy(key, pkey->key, pkey->keylen);
245 ptr = key + pkey->keylen;
246 ptr = pkey_pack_u32(ptr, pkey->algo);
247 ptr = pkey_pack_u32(ptr, pkey->paramlen);
248 memcpy(ptr, pkey->params, pkey->paramlen);
250 if (pkey->key_is_private)
251 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
253 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
257 sg_init_one(&in_sg, in, params->in_len);
258 sg_init_one(&out_sg, out, params->out_len);
259 akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
261 crypto_init_wait(&cwait);
262 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
263 CRYPTO_TFM_REQ_MAY_SLEEP,
264 crypto_req_done, &cwait);
266 /* Perform the encryption calculation. */
267 switch (params->op) {
268 case kernel_pkey_encrypt:
269 ret = crypto_akcipher_encrypt(req);
271 case kernel_pkey_decrypt:
272 ret = crypto_akcipher_decrypt(req);
274 case kernel_pkey_sign:
275 ret = crypto_akcipher_sign(req);
281 ret = crypto_wait_req(ret, &cwait);
288 akcipher_request_free(req);
290 crypto_free_akcipher(tfm);
291 pr_devel("<==%s() = %d\n", __func__, ret);
295 #if IS_REACHABLE(CONFIG_CRYPTO_SM2)
296 static int cert_sig_digest_update(const struct public_key_signature *sig,
297 struct crypto_akcipher *tfm_pkey)
299 struct crypto_shash *tfm;
300 struct shash_desc *desc;
302 unsigned char dgst[SM3_DIGEST_SIZE];
307 /* SM2 signatures always use the SM3 hash algorithm */
308 if (!sig->hash_algo || strcmp(sig->hash_algo, "sm3") != 0)
311 ret = sm2_compute_z_digest(tfm_pkey, SM2_DEFAULT_USERID,
312 SM2_DEFAULT_USERID_LEN, dgst);
316 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
320 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
321 desc = kzalloc(desc_size, GFP_KERNEL);
329 ret = crypto_shash_init(desc);
331 goto error_free_desc;
333 ret = crypto_shash_update(desc, dgst, SM3_DIGEST_SIZE);
335 goto error_free_desc;
337 ret = crypto_shash_finup(desc, sig->data, sig->data_size, sig->digest);
342 crypto_free_shash(tfm);
346 static inline int cert_sig_digest_update(
347 const struct public_key_signature *sig,
348 struct crypto_akcipher *tfm_pkey)
352 #endif /* ! IS_REACHABLE(CONFIG_CRYPTO_SM2) */
355 * Verify a signature using a public key.
357 int public_key_verify_signature(const struct public_key *pkey,
358 const struct public_key_signature *sig)
360 struct crypto_wait cwait;
361 struct crypto_akcipher *tfm;
362 struct akcipher_request *req;
363 struct scatterlist src_sg[2];
364 char alg_name[CRYPTO_MAX_ALG_NAME];
368 pr_devel("==>%s()\n", __func__);
375 * If the signature specifies a public key algorithm, it *must* match
376 * the key's actual public key algorithm.
378 * Small exception: ECDSA signatures don't specify the curve, but ECDSA
379 * keys do. So the strings can mismatch slightly in that case:
380 * "ecdsa-nist-*" for the key, but "ecdsa" for the signature.
382 if (sig->pkey_algo) {
383 if (strcmp(pkey->pkey_algo, sig->pkey_algo) != 0 &&
384 (strncmp(pkey->pkey_algo, "ecdsa-", 6) != 0 ||
385 strcmp(sig->pkey_algo, "ecdsa") != 0))
386 return -EKEYREJECTED;
389 ret = software_key_determine_akcipher(pkey, sig->encoding,
390 sig->hash_algo, alg_name);
394 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
399 req = akcipher_request_alloc(tfm, GFP_KERNEL);
403 key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
408 memcpy(key, pkey->key, pkey->keylen);
409 ptr = key + pkey->keylen;
410 ptr = pkey_pack_u32(ptr, pkey->algo);
411 ptr = pkey_pack_u32(ptr, pkey->paramlen);
412 memcpy(ptr, pkey->params, pkey->paramlen);
414 if (pkey->key_is_private)
415 ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
417 ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
421 if (strcmp(pkey->pkey_algo, "sm2") == 0 && sig->data_size) {
422 ret = cert_sig_digest_update(sig, tfm);
427 sg_init_table(src_sg, 2);
428 sg_set_buf(&src_sg[0], sig->s, sig->s_size);
429 sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
430 akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
432 crypto_init_wait(&cwait);
433 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
434 CRYPTO_TFM_REQ_MAY_SLEEP,
435 crypto_req_done, &cwait);
436 ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
441 akcipher_request_free(req);
443 crypto_free_akcipher(tfm);
444 pr_devel("<==%s() = %d\n", __func__, ret);
445 if (WARN_ON_ONCE(ret > 0))
449 EXPORT_SYMBOL_GPL(public_key_verify_signature);
451 static int public_key_verify_signature_2(const struct key *key,
452 const struct public_key_signature *sig)
454 const struct public_key *pk = key->payload.data[asym_crypto];
455 return public_key_verify_signature(pk, sig);
459 * Public key algorithm asymmetric key subtype
461 struct asymmetric_key_subtype public_key_subtype = {
462 .owner = THIS_MODULE,
463 .name = "public_key",
464 .name_len = sizeof("public_key") - 1,
465 .describe = public_key_describe,
466 .destroy = public_key_destroy,
467 .query = software_key_query,
468 .eds_op = software_key_eds_op,
469 .verify_signature = public_key_verify_signature_2,
471 EXPORT_SYMBOL_GPL(public_key_subtype);