Thomas Gleixner | b4d0d23 | 2019-05-20 19:08:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 2 | /* In-software asymmetric public-key crypto subtype |
| 3 | * |
Mauro Carvalho Chehab | 0efaaa8 | 2020-06-15 08:50:08 +0200 | [diff] [blame] | 4 | * See Documentation/crypto/asymmetric-keys.rst |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. |
| 7 | * Written by David Howells (dhowells@redhat.com) |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 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> |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 16 | #include <linux/scatterlist.h> |
Stefan Berger | 299f561 | 2021-03-16 17:07:37 -0400 | [diff] [blame] | 17 | #include <linux/asn1.h> |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 18 | #include <keys/asymmetric-subtype.h> |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 19 | #include <crypto/public_key.h> |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 20 | #include <crypto/akcipher.h> |
Herbert Xu | 3093e7c | 2020-10-07 17:05:45 +1100 | [diff] [blame] | 21 | #include <crypto/sm2.h> |
| 22 | #include <crypto/sm3_base.h> |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 23 | |
David Howells | 1e684d3 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 24 | MODULE_DESCRIPTION("In-software asymmetric public-key subtype"); |
| 25 | MODULE_AUTHOR("Red Hat, Inc."); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 26 | MODULE_LICENSE("GPL"); |
| 27 | |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 28 | /* |
| 29 | * Provide a part of a description of the key for /proc/keys. |
| 30 | */ |
| 31 | static void public_key_describe(const struct key *asymmetric_key, |
| 32 | struct seq_file *m) |
| 33 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 34 | struct public_key *key = asymmetric_key->payload.data[asym_crypto]; |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 35 | |
| 36 | if (key) |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 37 | seq_printf(m, "%s.%s", key->id_type, key->pkey_algo); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Destroy a public key algorithm key. |
| 42 | */ |
David Howells | 3b76456 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 43 | void public_key_free(struct public_key *key) |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 44 | { |
David Howells | 3b76456 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 45 | if (key) { |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 46 | kfree(key->key); |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 47 | kfree(key->params); |
David Howells | 3b76456 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 48 | kfree(key); |
| 49 | } |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 50 | } |
David Howells | 3b76456 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 51 | EXPORT_SYMBOL_GPL(public_key_free); |
| 52 | |
| 53 | /* |
| 54 | * Destroy a public key algorithm key. |
| 55 | */ |
| 56 | static void public_key_destroy(void *payload0, void *payload3) |
| 57 | { |
| 58 | public_key_free(payload0); |
| 59 | public_key_signature_free(payload3); |
| 60 | } |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 61 | |
| 62 | /* |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 63 | * Determine the crypto algorithm name. |
| 64 | */ |
| 65 | static |
| 66 | int software_key_determine_akcipher(const char *encoding, |
| 67 | const char *hash_algo, |
| 68 | const struct public_key *pkey, |
| 69 | char alg_name[CRYPTO_MAX_ALG_NAME]) |
| 70 | { |
| 71 | int n; |
| 72 | |
| 73 | if (strcmp(encoding, "pkcs1") == 0) { |
| 74 | /* The data wangled by the RSA algorithm is typically padded |
| 75 | * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447 |
| 76 | * sec 8.2]. |
| 77 | */ |
| 78 | if (!hash_algo) |
| 79 | n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME, |
| 80 | "pkcs1pad(%s)", |
| 81 | pkey->pkey_algo); |
| 82 | else |
| 83 | n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME, |
| 84 | "pkcs1pad(%s,%s)", |
| 85 | pkey->pkey_algo, hash_algo); |
| 86 | return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0; |
| 87 | } |
| 88 | |
Stefan Berger | 299f561 | 2021-03-16 17:07:37 -0400 | [diff] [blame] | 89 | if (strcmp(encoding, "raw") == 0 || |
| 90 | strcmp(encoding, "x962") == 0) { |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 91 | strcpy(alg_name, pkey->pkey_algo); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | return -ENOPKG; |
| 96 | } |
| 97 | |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 98 | static u8 *pkey_pack_u32(u8 *dst, u32 val) |
| 99 | { |
| 100 | memcpy(dst, &val, sizeof(val)); |
| 101 | return dst + sizeof(val); |
| 102 | } |
| 103 | |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 104 | /* |
| 105 | * Query information about a key. |
| 106 | */ |
| 107 | static int software_key_query(const struct kernel_pkey_params *params, |
| 108 | struct kernel_pkey_query *info) |
| 109 | { |
| 110 | struct crypto_akcipher *tfm; |
| 111 | struct public_key *pkey = params->key->payload.data[asym_crypto]; |
| 112 | char alg_name[CRYPTO_MAX_ALG_NAME]; |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 113 | u8 *key, *ptr; |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 114 | int ret, len; |
| 115 | |
| 116 | ret = software_key_determine_akcipher(params->encoding, |
| 117 | params->hash_algo, |
| 118 | pkey, alg_name); |
| 119 | if (ret < 0) |
| 120 | return ret; |
| 121 | |
| 122 | tfm = crypto_alloc_akcipher(alg_name, 0, 0); |
| 123 | if (IS_ERR(tfm)) |
| 124 | return PTR_ERR(tfm); |
| 125 | |
Wei Yongjun | 6cbba1f | 2020-07-15 23:28:38 +0100 | [diff] [blame] | 126 | ret = -ENOMEM; |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 127 | key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen, |
| 128 | GFP_KERNEL); |
| 129 | if (!key) |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 130 | goto error_free_tfm; |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 131 | memcpy(key, pkey->key, pkey->keylen); |
| 132 | ptr = key + pkey->keylen; |
| 133 | ptr = pkey_pack_u32(ptr, pkey->algo); |
| 134 | ptr = pkey_pack_u32(ptr, pkey->paramlen); |
| 135 | memcpy(ptr, pkey->params, pkey->paramlen); |
| 136 | |
| 137 | if (pkey->key_is_private) |
| 138 | ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen); |
| 139 | else |
| 140 | ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen); |
| 141 | if (ret < 0) |
| 142 | goto error_free_key; |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 143 | |
| 144 | len = crypto_akcipher_maxsize(tfm); |
| 145 | info->key_size = len * 8; |
| 146 | info->max_data_size = len; |
| 147 | info->max_sig_size = len; |
| 148 | info->max_enc_size = len; |
| 149 | info->max_dec_size = len; |
David Howells | c08fed7 | 2018-10-09 17:47:38 +0100 | [diff] [blame] | 150 | info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT | |
| 151 | KEYCTL_SUPPORTS_VERIFY); |
| 152 | if (pkey->key_is_private) |
| 153 | info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT | |
| 154 | KEYCTL_SUPPORTS_SIGN); |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 155 | ret = 0; |
| 156 | |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 157 | error_free_key: |
| 158 | kfree(key); |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 159 | error_free_tfm: |
| 160 | crypto_free_akcipher(tfm); |
| 161 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | /* |
David Howells | c08fed7 | 2018-10-09 17:47:38 +0100 | [diff] [blame] | 166 | * Do encryption, decryption and signing ops. |
| 167 | */ |
| 168 | static int software_key_eds_op(struct kernel_pkey_params *params, |
| 169 | const void *in, void *out) |
| 170 | { |
| 171 | const struct public_key *pkey = params->key->payload.data[asym_crypto]; |
| 172 | struct akcipher_request *req; |
| 173 | struct crypto_akcipher *tfm; |
| 174 | struct crypto_wait cwait; |
| 175 | struct scatterlist in_sg, out_sg; |
| 176 | char alg_name[CRYPTO_MAX_ALG_NAME]; |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 177 | char *key, *ptr; |
David Howells | c08fed7 | 2018-10-09 17:47:38 +0100 | [diff] [blame] | 178 | int ret; |
| 179 | |
| 180 | pr_devel("==>%s()\n", __func__); |
| 181 | |
| 182 | ret = software_key_determine_akcipher(params->encoding, |
| 183 | params->hash_algo, |
| 184 | pkey, alg_name); |
| 185 | if (ret < 0) |
| 186 | return ret; |
| 187 | |
| 188 | tfm = crypto_alloc_akcipher(alg_name, 0, 0); |
| 189 | if (IS_ERR(tfm)) |
| 190 | return PTR_ERR(tfm); |
| 191 | |
Eric Biggers | bea3741 | 2019-10-09 16:03:49 -0700 | [diff] [blame] | 192 | ret = -ENOMEM; |
David Howells | c08fed7 | 2018-10-09 17:47:38 +0100 | [diff] [blame] | 193 | req = akcipher_request_alloc(tfm, GFP_KERNEL); |
| 194 | if (!req) |
| 195 | goto error_free_tfm; |
| 196 | |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 197 | key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen, |
| 198 | GFP_KERNEL); |
| 199 | if (!key) |
David Howells | c08fed7 | 2018-10-09 17:47:38 +0100 | [diff] [blame] | 200 | goto error_free_req; |
| 201 | |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 202 | memcpy(key, pkey->key, pkey->keylen); |
| 203 | ptr = key + pkey->keylen; |
| 204 | ptr = pkey_pack_u32(ptr, pkey->algo); |
| 205 | ptr = pkey_pack_u32(ptr, pkey->paramlen); |
| 206 | memcpy(ptr, pkey->params, pkey->paramlen); |
| 207 | |
| 208 | if (pkey->key_is_private) |
| 209 | ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen); |
| 210 | else |
| 211 | ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen); |
| 212 | if (ret) |
| 213 | goto error_free_key; |
| 214 | |
David Howells | c08fed7 | 2018-10-09 17:47:38 +0100 | [diff] [blame] | 215 | sg_init_one(&in_sg, in, params->in_len); |
| 216 | sg_init_one(&out_sg, out, params->out_len); |
| 217 | akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len, |
| 218 | params->out_len); |
| 219 | crypto_init_wait(&cwait); |
| 220 | akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 221 | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 222 | crypto_req_done, &cwait); |
| 223 | |
| 224 | /* Perform the encryption calculation. */ |
| 225 | switch (params->op) { |
| 226 | case kernel_pkey_encrypt: |
| 227 | ret = crypto_akcipher_encrypt(req); |
| 228 | break; |
| 229 | case kernel_pkey_decrypt: |
| 230 | ret = crypto_akcipher_decrypt(req); |
| 231 | break; |
| 232 | case kernel_pkey_sign: |
| 233 | ret = crypto_akcipher_sign(req); |
| 234 | break; |
| 235 | default: |
| 236 | BUG(); |
| 237 | } |
| 238 | |
| 239 | ret = crypto_wait_req(ret, &cwait); |
| 240 | if (ret == 0) |
| 241 | ret = req->dst_len; |
| 242 | |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 243 | error_free_key: |
| 244 | kfree(key); |
David Howells | c08fed7 | 2018-10-09 17:47:38 +0100 | [diff] [blame] | 245 | error_free_req: |
| 246 | akcipher_request_free(req); |
| 247 | error_free_tfm: |
| 248 | crypto_free_akcipher(tfm); |
| 249 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 250 | return ret; |
| 251 | } |
| 252 | |
Herbert Xu | 3093e7c | 2020-10-07 17:05:45 +1100 | [diff] [blame] | 253 | #if IS_REACHABLE(CONFIG_CRYPTO_SM2) |
| 254 | static int cert_sig_digest_update(const struct public_key_signature *sig, |
| 255 | struct crypto_akcipher *tfm_pkey) |
| 256 | { |
| 257 | struct crypto_shash *tfm; |
| 258 | struct shash_desc *desc; |
| 259 | size_t desc_size; |
| 260 | unsigned char dgst[SM3_DIGEST_SIZE]; |
| 261 | int ret; |
| 262 | |
| 263 | BUG_ON(!sig->data); |
| 264 | |
| 265 | ret = sm2_compute_z_digest(tfm_pkey, SM2_DEFAULT_USERID, |
| 266 | SM2_DEFAULT_USERID_LEN, dgst); |
| 267 | if (ret) |
| 268 | return ret; |
| 269 | |
| 270 | tfm = crypto_alloc_shash(sig->hash_algo, 0, 0); |
| 271 | if (IS_ERR(tfm)) |
| 272 | return PTR_ERR(tfm); |
| 273 | |
| 274 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); |
| 275 | desc = kzalloc(desc_size, GFP_KERNEL); |
| 276 | if (!desc) { |
| 277 | ret = -ENOMEM; |
| 278 | goto error_free_tfm; |
| 279 | } |
| 280 | |
| 281 | desc->tfm = tfm; |
| 282 | |
| 283 | ret = crypto_shash_init(desc); |
| 284 | if (ret < 0) |
| 285 | goto error_free_desc; |
| 286 | |
| 287 | ret = crypto_shash_update(desc, dgst, SM3_DIGEST_SIZE); |
| 288 | if (ret < 0) |
| 289 | goto error_free_desc; |
| 290 | |
| 291 | ret = crypto_shash_finup(desc, sig->data, sig->data_size, sig->digest); |
| 292 | |
| 293 | error_free_desc: |
| 294 | kfree(desc); |
| 295 | error_free_tfm: |
| 296 | crypto_free_shash(tfm); |
| 297 | return ret; |
| 298 | } |
| 299 | #else |
| 300 | static inline int cert_sig_digest_update( |
| 301 | const struct public_key_signature *sig, |
| 302 | struct crypto_akcipher *tfm_pkey) |
| 303 | { |
| 304 | return -ENOTSUPP; |
| 305 | } |
| 306 | #endif /* ! IS_REACHABLE(CONFIG_CRYPTO_SM2) */ |
| 307 | |
David Howells | c08fed7 | 2018-10-09 17:47:38 +0100 | [diff] [blame] | 308 | /* |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 309 | * Verify a signature using a public key. |
| 310 | */ |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 311 | int public_key_verify_signature(const struct public_key *pkey, |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 312 | const struct public_key_signature *sig) |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 313 | { |
Gilad Ben-Yossef | 0ca2a04 | 2017-10-18 08:00:40 +0100 | [diff] [blame] | 314 | struct crypto_wait cwait; |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 315 | struct crypto_akcipher *tfm; |
| 316 | struct akcipher_request *req; |
Vitaly Chikunov | c7381b0 | 2019-04-11 18:51:15 +0300 | [diff] [blame] | 317 | struct scatterlist src_sg[2]; |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 318 | char alg_name[CRYPTO_MAX_ALG_NAME]; |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 319 | char *key, *ptr; |
Eric Biggers | 72f9a07 | 2017-12-08 15:13:29 +0000 | [diff] [blame] | 320 | int ret; |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 321 | |
| 322 | pr_devel("==>%s()\n", __func__); |
| 323 | |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 324 | BUG_ON(!pkey); |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 325 | BUG_ON(!sig); |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 326 | BUG_ON(!sig->s); |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 327 | |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 328 | ret = software_key_determine_akcipher(sig->encoding, |
| 329 | sig->hash_algo, |
| 330 | pkey, alg_name); |
| 331 | if (ret < 0) |
| 332 | return ret; |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 333 | |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 334 | tfm = crypto_alloc_akcipher(alg_name, 0, 0); |
| 335 | if (IS_ERR(tfm)) |
| 336 | return PTR_ERR(tfm); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 337 | |
Eric Biggers | 72f9a07 | 2017-12-08 15:13:29 +0000 | [diff] [blame] | 338 | ret = -ENOMEM; |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 339 | req = akcipher_request_alloc(tfm, GFP_KERNEL); |
| 340 | if (!req) |
| 341 | goto error_free_tfm; |
| 342 | |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 343 | key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen, |
| 344 | GFP_KERNEL); |
| 345 | if (!key) |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 346 | goto error_free_req; |
| 347 | |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 348 | memcpy(key, pkey->key, pkey->keylen); |
| 349 | ptr = key + pkey->keylen; |
| 350 | ptr = pkey_pack_u32(ptr, pkey->algo); |
| 351 | ptr = pkey_pack_u32(ptr, pkey->paramlen); |
| 352 | memcpy(ptr, pkey->params, pkey->paramlen); |
| 353 | |
| 354 | if (pkey->key_is_private) |
| 355 | ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen); |
| 356 | else |
| 357 | ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen); |
| 358 | if (ret) |
| 359 | goto error_free_key; |
| 360 | |
Tianjia Zhang | 7178a10 | 2021-01-19 00:13:19 +0000 | [diff] [blame] | 361 | if (sig->pkey_algo && strcmp(sig->pkey_algo, "sm2") == 0 && |
| 362 | sig->data_size) { |
Tianjia Zhang | 2155256 | 2020-09-21 00:21:02 +0800 | [diff] [blame] | 363 | ret = cert_sig_digest_update(sig, tfm); |
| 364 | if (ret) |
| 365 | goto error_free_key; |
| 366 | } |
| 367 | |
Vitaly Chikunov | c7381b0 | 2019-04-11 18:51:15 +0300 | [diff] [blame] | 368 | sg_init_table(src_sg, 2); |
| 369 | sg_set_buf(&src_sg[0], sig->s, sig->s_size); |
Vitaly Chikunov | 83bc029 | 2019-04-11 18:51:16 +0300 | [diff] [blame] | 370 | sg_set_buf(&src_sg[1], sig->digest, sig->digest_size); |
Vitaly Chikunov | c7381b0 | 2019-04-11 18:51:15 +0300 | [diff] [blame] | 371 | akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size, |
| 372 | sig->digest_size); |
Gilad Ben-Yossef | 0ca2a04 | 2017-10-18 08:00:40 +0100 | [diff] [blame] | 373 | crypto_init_wait(&cwait); |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 374 | akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 375 | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | 0ca2a04 | 2017-10-18 08:00:40 +0100 | [diff] [blame] | 376 | crypto_req_done, &cwait); |
Gilad Ben-Yossef | 0ca2a04 | 2017-10-18 08:00:40 +0100 | [diff] [blame] | 377 | ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait); |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 378 | |
Vitaly Chikunov | f1774cb | 2019-04-11 18:51:17 +0300 | [diff] [blame] | 379 | error_free_key: |
| 380 | kfree(key); |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 381 | error_free_req: |
| 382 | akcipher_request_free(req); |
| 383 | error_free_tfm: |
| 384 | crypto_free_akcipher(tfm); |
| 385 | pr_devel("<==%s() = %d\n", __func__, ret); |
Eric Biggers | 72f9a07 | 2017-12-08 15:13:29 +0000 | [diff] [blame] | 386 | if (WARN_ON_ONCE(ret > 0)) |
| 387 | ret = -EINVAL; |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 388 | return ret; |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 389 | } |
| 390 | EXPORT_SYMBOL_GPL(public_key_verify_signature); |
| 391 | |
| 392 | static int public_key_verify_signature_2(const struct key *key, |
| 393 | const struct public_key_signature *sig) |
| 394 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 395 | const struct public_key *pk = key->payload.data[asym_crypto]; |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 396 | return public_key_verify_signature(pk, sig); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Public key algorithm asymmetric key subtype |
| 401 | */ |
| 402 | struct asymmetric_key_subtype public_key_subtype = { |
| 403 | .owner = THIS_MODULE, |
| 404 | .name = "public_key", |
David Howells | 876c6e3 | 2014-09-02 13:52:10 +0100 | [diff] [blame] | 405 | .name_len = sizeof("public_key") - 1, |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 406 | .describe = public_key_describe, |
| 407 | .destroy = public_key_destroy, |
David Howells | 82f94f2 | 2018-10-09 17:47:23 +0100 | [diff] [blame] | 408 | .query = software_key_query, |
David Howells | c08fed7 | 2018-10-09 17:47:38 +0100 | [diff] [blame] | 409 | .eds_op = software_key_eds_op, |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 410 | .verify_signature = public_key_verify_signature_2, |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 411 | }; |
| 412 | EXPORT_SYMBOL_GPL(public_key_subtype); |