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 ret
= sm2_compute_z_digest(tfm_pkey
, SM2_DEFAULT_USERID
,
264 SM2_DEFAULT_USERID_LEN
, dgst
);
268 tfm
= crypto_alloc_shash(sig
->hash_algo
, 0, 0);
272 desc_size
= crypto_shash_descsize(tfm
) + sizeof(*desc
);
273 desc
= kzalloc(desc_size
, GFP_KERNEL
);
281 ret
= crypto_shash_init(desc
);
283 goto error_free_desc
;
285 ret
= crypto_shash_update(desc
, dgst
, SM3_DIGEST_SIZE
);
287 goto error_free_desc
;
289 ret
= crypto_shash_finup(desc
, sig
->data
, sig
->data_size
, sig
->digest
);
294 crypto_free_shash(tfm
);
298 static inline int cert_sig_digest_update(
299 const struct public_key_signature
*sig
,
300 struct crypto_akcipher
*tfm_pkey
)
304 #endif /* ! IS_REACHABLE(CONFIG_CRYPTO_SM2) */
307 * Verify a signature using a public key.
309 int public_key_verify_signature(const struct public_key
*pkey
,
310 const struct public_key_signature
*sig
)
312 struct crypto_wait cwait
;
313 struct crypto_akcipher
*tfm
;
314 struct akcipher_request
*req
;
315 struct scatterlist src_sg
[2];
316 char alg_name
[CRYPTO_MAX_ALG_NAME
];
320 pr_devel("==>%s()\n", __func__
);
326 ret
= software_key_determine_akcipher(sig
->encoding
,
332 tfm
= crypto_alloc_akcipher(alg_name
, 0, 0);
337 req
= akcipher_request_alloc(tfm
, GFP_KERNEL
);
341 key
= kmalloc(pkey
->keylen
+ sizeof(u32
) * 2 + pkey
->paramlen
,
346 memcpy(key
, pkey
->key
, pkey
->keylen
);
347 ptr
= key
+ pkey
->keylen
;
348 ptr
= pkey_pack_u32(ptr
, pkey
->algo
);
349 ptr
= pkey_pack_u32(ptr
, pkey
->paramlen
);
350 memcpy(ptr
, pkey
->params
, pkey
->paramlen
);
352 if (pkey
->key_is_private
)
353 ret
= crypto_akcipher_set_priv_key(tfm
, key
, pkey
->keylen
);
355 ret
= crypto_akcipher_set_pub_key(tfm
, key
, pkey
->keylen
);
359 if (strcmp(sig
->pkey_algo
, "sm2") == 0 && sig
->data_size
) {
360 ret
= cert_sig_digest_update(sig
, tfm
);
365 sg_init_table(src_sg
, 2);
366 sg_set_buf(&src_sg
[0], sig
->s
, sig
->s_size
);
367 sg_set_buf(&src_sg
[1], sig
->digest
, sig
->digest_size
);
368 akcipher_request_set_crypt(req
, src_sg
, NULL
, sig
->s_size
,
370 crypto_init_wait(&cwait
);
371 akcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
|
372 CRYPTO_TFM_REQ_MAY_SLEEP
,
373 crypto_req_done
, &cwait
);
374 ret
= crypto_wait_req(crypto_akcipher_verify(req
), &cwait
);
379 akcipher_request_free(req
);
381 crypto_free_akcipher(tfm
);
382 pr_devel("<==%s() = %d\n", __func__
, ret
);
383 if (WARN_ON_ONCE(ret
> 0))
387 EXPORT_SYMBOL_GPL(public_key_verify_signature
);
389 static int public_key_verify_signature_2(const struct key
*key
,
390 const struct public_key_signature
*sig
)
392 const struct public_key
*pk
= key
->payload
.data
[asym_crypto
];
393 return public_key_verify_signature(pk
, sig
);
397 * Public key algorithm asymmetric key subtype
399 struct asymmetric_key_subtype public_key_subtype
= {
400 .owner
= THIS_MODULE
,
401 .name
= "public_key",
402 .name_len
= sizeof("public_key") - 1,
403 .describe
= public_key_describe
,
404 .destroy
= public_key_destroy
,
405 .query
= software_key_query
,
406 .eds_op
= software_key_eds_op
,
407 .verify_signature
= public_key_verify_signature_2
,
409 EXPORT_SYMBOL_GPL(public_key_subtype
);