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 <crypto/akcipher.h>
12 #include <crypto/public_key.h>
13 #include <crypto/sig.h>
14 #include <keys/asymmetric-subtype.h>
15 #include <linux/asn1.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/seq_file.h>
20 #include <linux/slab.h>
21 #include <linux/string.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
)
45 kfree_sensitive(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 * Given a public_key, and an encoding and hash_algo to be used for signing
63 * and/or verification with that key, determine the name of the corresponding
64 * akcipher algorithm. Also check that encoding and hash_algo are allowed.
67 software_key_determine_akcipher(const struct public_key
*pkey
,
68 const char *encoding
, const char *hash_algo
,
69 char alg_name
[CRYPTO_MAX_ALG_NAME
], bool *sig
,
70 enum kernel_pkey_operation op
)
79 if (strcmp(pkey
->pkey_algo
, "rsa") == 0) {
81 * RSA signatures usually use EMSA-PKCS1-1_5 [RFC3447 sec 8.2].
83 if (strcmp(encoding
, "pkcs1") == 0) {
84 *sig
= op
== kernel_pkey_sign
||
85 op
== kernel_pkey_verify
;
87 n
= snprintf(alg_name
, CRYPTO_MAX_ALG_NAME
,
91 n
= snprintf(alg_name
, CRYPTO_MAX_ALG_NAME
,
93 pkey
->pkey_algo
, hash_algo
);
95 return n
>= CRYPTO_MAX_ALG_NAME
? -EINVAL
: 0;
97 if (strcmp(encoding
, "raw") != 0)
100 * Raw RSA cannot differentiate between different hash
106 } else if (strncmp(pkey
->pkey_algo
, "ecdsa", 5) == 0) {
107 if (strcmp(encoding
, "x962") != 0)
110 * ECDSA signatures are taken over a raw hash, so they don't
111 * differentiate between different hash algorithms. That means
112 * that the verifier should hard-code a specific hash algorithm.
113 * Unfortunately, in practice ECDSA is used with multiple SHAs,
114 * so we have to allow all of them and not just one.
118 if (strcmp(hash_algo
, "sha1") != 0 &&
119 strcmp(hash_algo
, "sha224") != 0 &&
120 strcmp(hash_algo
, "sha256") != 0 &&
121 strcmp(hash_algo
, "sha384") != 0 &&
122 strcmp(hash_algo
, "sha512") != 0)
124 } else if (strcmp(pkey
->pkey_algo
, "sm2") == 0) {
125 if (strcmp(encoding
, "raw") != 0)
129 if (strcmp(hash_algo
, "sm3") != 0)
131 } else if (strcmp(pkey
->pkey_algo
, "ecrdsa") == 0) {
132 if (strcmp(encoding
, "raw") != 0)
136 if (strcmp(hash_algo
, "streebog256") != 0 &&
137 strcmp(hash_algo
, "streebog512") != 0)
140 /* Unknown public key algorithm */
143 if (strscpy(alg_name
, pkey
->pkey_algo
, CRYPTO_MAX_ALG_NAME
) < 0)
148 static u8
*pkey_pack_u32(u8
*dst
, u32 val
)
150 memcpy(dst
, &val
, sizeof(val
));
151 return dst
+ sizeof(val
);
155 * Query information about a key.
157 static int software_key_query(const struct kernel_pkey_params
*params
,
158 struct kernel_pkey_query
*info
)
160 struct crypto_akcipher
*tfm
;
161 struct public_key
*pkey
= params
->key
->payload
.data
[asym_crypto
];
162 char alg_name
[CRYPTO_MAX_ALG_NAME
];
163 struct crypto_sig
*sig
;
168 ret
= software_key_determine_akcipher(pkey
, params
->encoding
,
169 params
->hash_algo
, alg_name
,
170 &issig
, kernel_pkey_sign
);
174 key
= kmalloc(pkey
->keylen
+ sizeof(u32
) * 2 + pkey
->paramlen
,
179 memcpy(key
, pkey
->key
, pkey
->keylen
);
180 ptr
= key
+ pkey
->keylen
;
181 ptr
= pkey_pack_u32(ptr
, pkey
->algo
);
182 ptr
= pkey_pack_u32(ptr
, pkey
->paramlen
);
183 memcpy(ptr
, pkey
->params
, pkey
->paramlen
);
186 sig
= crypto_alloc_sig(alg_name
, 0, 0);
192 if (pkey
->key_is_private
)
193 ret
= crypto_sig_set_privkey(sig
, key
, pkey
->keylen
);
195 ret
= crypto_sig_set_pubkey(sig
, key
, pkey
->keylen
);
199 len
= crypto_sig_maxsize(sig
);
201 info
->supported_ops
= KEYCTL_SUPPORTS_VERIFY
;
202 if (pkey
->key_is_private
)
203 info
->supported_ops
|= KEYCTL_SUPPORTS_SIGN
;
205 if (strcmp(params
->encoding
, "pkcs1") == 0) {
206 info
->supported_ops
|= KEYCTL_SUPPORTS_ENCRYPT
;
207 if (pkey
->key_is_private
)
208 info
->supported_ops
|= KEYCTL_SUPPORTS_DECRYPT
;
211 tfm
= crypto_alloc_akcipher(alg_name
, 0, 0);
217 if (pkey
->key_is_private
)
218 ret
= crypto_akcipher_set_priv_key(tfm
, key
, pkey
->keylen
);
220 ret
= crypto_akcipher_set_pub_key(tfm
, key
, pkey
->keylen
);
224 len
= crypto_akcipher_maxsize(tfm
);
226 info
->supported_ops
= KEYCTL_SUPPORTS_ENCRYPT
;
227 if (pkey
->key_is_private
)
228 info
->supported_ops
|= KEYCTL_SUPPORTS_DECRYPT
;
231 info
->key_size
= len
* 8;
233 if (strncmp(pkey
->pkey_algo
, "ecdsa", 5) == 0) {
235 * ECDSA key sizes are much smaller than RSA, and thus could
236 * operate on (hashed) inputs that are larger than key size.
237 * For example SHA384-hashed input used with secp256r1
238 * based keys. Set max_data_size to be at least as large as
239 * the largest supported hash size (SHA512)
241 info
->max_data_size
= 64;
244 * Verify takes ECDSA-Sig (described in RFC 5480) as input,
245 * which is actually 2 'key_size'-bit integers encoded in
246 * ASN.1. Account for the ASN.1 encoding overhead here.
248 info
->max_sig_size
= 2 * (len
+ 3) + 2;
250 info
->max_data_size
= len
;
251 info
->max_sig_size
= len
;
254 info
->max_enc_size
= len
;
255 info
->max_dec_size
= len
;
261 crypto_free_sig(sig
);
263 crypto_free_akcipher(tfm
);
265 kfree_sensitive(key
);
266 pr_devel("<==%s() = %d\n", __func__
, ret
);
271 * Do encryption, decryption and signing ops.
273 static int software_key_eds_op(struct kernel_pkey_params
*params
,
274 const void *in
, void *out
)
276 const struct public_key
*pkey
= params
->key
->payload
.data
[asym_crypto
];
277 char alg_name
[CRYPTO_MAX_ALG_NAME
];
278 struct crypto_akcipher
*tfm
;
279 struct crypto_sig
*sig
;
285 pr_devel("==>%s()\n", __func__
);
287 ret
= software_key_determine_akcipher(pkey
, params
->encoding
,
288 params
->hash_algo
, alg_name
,
293 key
= kmalloc(pkey
->keylen
+ sizeof(u32
) * 2 + pkey
->paramlen
,
298 memcpy(key
, pkey
->key
, pkey
->keylen
);
299 ptr
= key
+ pkey
->keylen
;
300 ptr
= pkey_pack_u32(ptr
, pkey
->algo
);
301 ptr
= pkey_pack_u32(ptr
, pkey
->paramlen
);
302 memcpy(ptr
, pkey
->params
, pkey
->paramlen
);
305 sig
= crypto_alloc_sig(alg_name
, 0, 0);
311 if (pkey
->key_is_private
)
312 ret
= crypto_sig_set_privkey(sig
, key
, pkey
->keylen
);
314 ret
= crypto_sig_set_pubkey(sig
, key
, pkey
->keylen
);
318 ksz
= crypto_sig_maxsize(sig
);
320 tfm
= crypto_alloc_akcipher(alg_name
, 0, 0);
326 if (pkey
->key_is_private
)
327 ret
= crypto_akcipher_set_priv_key(tfm
, key
, pkey
->keylen
);
329 ret
= crypto_akcipher_set_pub_key(tfm
, key
, pkey
->keylen
);
333 ksz
= crypto_akcipher_maxsize(tfm
);
338 /* Perform the encryption calculation. */
339 switch (params
->op
) {
340 case kernel_pkey_encrypt
:
343 ret
= crypto_akcipher_sync_encrypt(tfm
, in
, params
->in_len
,
344 out
, params
->out_len
);
346 case kernel_pkey_decrypt
:
349 ret
= crypto_akcipher_sync_decrypt(tfm
, in
, params
->in_len
,
350 out
, params
->out_len
);
352 case kernel_pkey_sign
:
355 ret
= crypto_sig_sign(sig
, in
, params
->in_len
,
356 out
, params
->out_len
);
367 crypto_free_sig(sig
);
369 crypto_free_akcipher(tfm
);
371 kfree_sensitive(key
);
372 pr_devel("<==%s() = %d\n", __func__
, ret
);
377 * Verify a signature using a public key.
379 int public_key_verify_signature(const struct public_key
*pkey
,
380 const struct public_key_signature
*sig
)
382 char alg_name
[CRYPTO_MAX_ALG_NAME
];
383 struct crypto_sig
*tfm
;
388 pr_devel("==>%s()\n", __func__
);
395 * If the signature specifies a public key algorithm, it *must* match
396 * the key's actual public key algorithm.
398 * Small exception: ECDSA signatures don't specify the curve, but ECDSA
399 * keys do. So the strings can mismatch slightly in that case:
400 * "ecdsa-nist-*" for the key, but "ecdsa" for the signature.
402 if (sig
->pkey_algo
) {
403 if (strcmp(pkey
->pkey_algo
, sig
->pkey_algo
) != 0 &&
404 (strncmp(pkey
->pkey_algo
, "ecdsa-", 6) != 0 ||
405 strcmp(sig
->pkey_algo
, "ecdsa") != 0))
406 return -EKEYREJECTED
;
409 ret
= software_key_determine_akcipher(pkey
, sig
->encoding
,
410 sig
->hash_algo
, alg_name
,
411 &issig
, kernel_pkey_verify
);
415 tfm
= crypto_alloc_sig(alg_name
, 0, 0);
419 key
= kmalloc(pkey
->keylen
+ sizeof(u32
) * 2 + pkey
->paramlen
,
426 memcpy(key
, pkey
->key
, pkey
->keylen
);
427 ptr
= key
+ pkey
->keylen
;
428 ptr
= pkey_pack_u32(ptr
, pkey
->algo
);
429 ptr
= pkey_pack_u32(ptr
, pkey
->paramlen
);
430 memcpy(ptr
, pkey
->params
, pkey
->paramlen
);
432 if (pkey
->key_is_private
)
433 ret
= crypto_sig_set_privkey(tfm
, key
, pkey
->keylen
);
435 ret
= crypto_sig_set_pubkey(tfm
, key
, pkey
->keylen
);
439 ret
= crypto_sig_verify(tfm
, sig
->s
, sig
->s_size
,
440 sig
->digest
, sig
->digest_size
);
443 kfree_sensitive(key
);
445 crypto_free_sig(tfm
);
446 pr_devel("<==%s() = %d\n", __func__
, ret
);
447 if (WARN_ON_ONCE(ret
> 0))
451 EXPORT_SYMBOL_GPL(public_key_verify_signature
);
453 static int public_key_verify_signature_2(const struct key
*key
,
454 const struct public_key_signature
*sig
)
456 const struct public_key
*pk
= key
->payload
.data
[asym_crypto
];
457 return public_key_verify_signature(pk
, sig
);
461 * Public key algorithm asymmetric key subtype
463 struct asymmetric_key_subtype public_key_subtype
= {
464 .owner
= THIS_MODULE
,
465 .name
= "public_key",
466 .name_len
= sizeof("public_key") - 1,
467 .describe
= public_key_describe
,
468 .destroy
= public_key_destroy
,
469 .query
= software_key_query
,
470 .eds_op
= software_key_eds_op
,
471 .verify_signature
= public_key_verify_signature_2
,
473 EXPORT_SYMBOL_GPL(public_key_subtype
);