AKASHI Takahiro | cbb1007 | 2019-11-13 09:44:58 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* In-software asymmetric public-key crypto subtype |
| 3 | * |
| 4 | * See Documentation/crypto/asymmetric-keys.txt |
| 5 | * |
| 6 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. |
| 7 | * Written by David Howells (dhowells@redhat.com) |
| 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) "PKEY: "fmt |
| 11 | #ifdef __UBOOT__ |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 12 | #include <dm/devres.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 13 | #include <linux/bug.h> |
AKASHI Takahiro | cbb1007 | 2019-11-13 09:44:58 +0900 | [diff] [blame] | 14 | #include <linux/compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
AKASHI Takahiro | cbb1007 | 2019-11-13 09:44:58 +0900 | [diff] [blame] | 16 | #else |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/export.h> |
| 19 | #endif |
| 20 | #include <linux/kernel.h> |
| 21 | #ifndef __UBOOT__ |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | #include <linux/scatterlist.h> |
| 25 | #include <keys/asymmetric-subtype.h> |
| 26 | #endif |
| 27 | #include <crypto/public_key.h> |
AKASHI Takahiro | 697b4cc | 2020-07-21 19:35:17 +0900 | [diff] [blame] | 28 | #ifdef __UBOOT__ |
| 29 | #include <image.h> |
| 30 | #include <u-boot/rsa.h> |
| 31 | #else |
AKASHI Takahiro | cbb1007 | 2019-11-13 09:44:58 +0900 | [diff] [blame] | 32 | #include <crypto/akcipher.h> |
| 33 | #endif |
| 34 | |
| 35 | MODULE_DESCRIPTION("In-software asymmetric public-key subtype"); |
| 36 | MODULE_AUTHOR("Red Hat, Inc."); |
| 37 | MODULE_LICENSE("GPL"); |
| 38 | |
| 39 | #ifndef __UBOOT__ |
| 40 | /* |
| 41 | * Provide a part of a description of the key for /proc/keys. |
| 42 | */ |
| 43 | static void public_key_describe(const struct key *asymmetric_key, |
| 44 | struct seq_file *m) |
| 45 | { |
| 46 | struct public_key *key = asymmetric_key->payload.data[asym_crypto]; |
| 47 | |
| 48 | if (key) |
| 49 | seq_printf(m, "%s.%s", key->id_type, key->pkey_algo); |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | /* |
| 54 | * Destroy a public key algorithm key. |
| 55 | */ |
| 56 | void public_key_free(struct public_key *key) |
| 57 | { |
| 58 | if (key) { |
| 59 | kfree(key->key); |
| 60 | kfree(key->params); |
| 61 | kfree(key); |
| 62 | } |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(public_key_free); |
| 65 | |
| 66 | #ifdef __UBOOT__ |
| 67 | /* |
| 68 | * from <linux>/crypto/asymmetric_keys/signature.c |
| 69 | * |
| 70 | * Destroy a public key signature. |
| 71 | */ |
| 72 | void public_key_signature_free(struct public_key_signature *sig) |
| 73 | { |
| 74 | int i; |
| 75 | |
| 76 | if (sig) { |
| 77 | for (i = 0; i < ARRAY_SIZE(sig->auth_ids); i++) |
| 78 | free(sig->auth_ids[i]); |
| 79 | free(sig->s); |
| 80 | free(sig->digest); |
| 81 | free(sig); |
| 82 | } |
| 83 | } |
| 84 | EXPORT_SYMBOL_GPL(public_key_signature_free); |
| 85 | |
AKASHI Takahiro | 697b4cc | 2020-07-21 19:35:17 +0900 | [diff] [blame] | 86 | /** |
| 87 | * public_key_verify_signature - Verify a signature using a public key. |
| 88 | * |
| 89 | * @pkey: Public key |
| 90 | * @sig: Signature |
| 91 | * |
| 92 | * Verify a signature, @sig, using a RSA public key, @pkey. |
| 93 | * |
| 94 | * Return: 0 - verified, non-zero error code - otherwise |
| 95 | */ |
| 96 | int public_key_verify_signature(const struct public_key *pkey, |
| 97 | const struct public_key_signature *sig) |
| 98 | { |
| 99 | struct image_sign_info info; |
| 100 | struct image_region region; |
| 101 | int ret; |
| 102 | |
| 103 | pr_devel("==>%s()\n", __func__); |
| 104 | |
| 105 | if (!pkey || !sig) |
| 106 | return -EINVAL; |
| 107 | |
| 108 | if (pkey->key_is_private) |
| 109 | return -EINVAL; |
| 110 | |
| 111 | memset(&info, '\0', sizeof(info)); |
| 112 | info.padding = image_get_padding_algo("pkcs-1.5"); |
| 113 | /* |
| 114 | * Note: image_get_[checksum|crypto]_algo takes a string |
| 115 | * argument like "<checksum>,<crypto>" |
| 116 | * TODO: support other hash algorithms |
| 117 | */ |
| 118 | if (strcmp(sig->pkey_algo, "rsa") || (sig->s_size * 8) != 2048) { |
| 119 | pr_warn("Encryption is not RSA2048: %s%d\n", |
| 120 | sig->pkey_algo, sig->s_size * 8); |
| 121 | return -ENOPKG; |
| 122 | } |
| 123 | if (!strcmp(sig->hash_algo, "sha1")) { |
| 124 | info.checksum = image_get_checksum_algo("sha1,rsa2048"); |
| 125 | info.name = "sha1,rsa2048"; |
| 126 | } else if (!strcmp(sig->hash_algo, "sha256")) { |
| 127 | info.checksum = image_get_checksum_algo("sha256,rsa2048"); |
| 128 | info.name = "sha256,rsa2048"; |
| 129 | } else { |
| 130 | pr_warn("unknown msg digest algo: %s\n", sig->hash_algo); |
| 131 | return -ENOPKG; |
| 132 | } |
| 133 | info.crypto = image_get_crypto_algo(info.name); |
| 134 | if (IS_ERR(info.checksum) || IS_ERR(info.crypto)) |
| 135 | return -ENOPKG; |
| 136 | |
| 137 | info.key = pkey->key; |
| 138 | info.keylen = pkey->keylen; |
| 139 | |
| 140 | region.data = sig->digest; |
| 141 | region.size = sig->digest_size; |
| 142 | |
| 143 | if (rsa_verify_with_pkey(&info, sig->digest, sig->s, sig->s_size)) |
| 144 | ret = -EKEYREJECTED; |
| 145 | else |
| 146 | ret = 0; |
| 147 | |
| 148 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 149 | return ret; |
| 150 | } |
AKASHI Takahiro | cbb1007 | 2019-11-13 09:44:58 +0900 | [diff] [blame] | 151 | #else |
| 152 | /* |
| 153 | * Destroy a public key algorithm key. |
| 154 | */ |
| 155 | static void public_key_destroy(void *payload0, void *payload3) |
| 156 | { |
| 157 | public_key_free(payload0); |
| 158 | public_key_signature_free(payload3); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Determine the crypto algorithm name. |
| 163 | */ |
| 164 | static |
| 165 | int software_key_determine_akcipher(const char *encoding, |
| 166 | const char *hash_algo, |
| 167 | const struct public_key *pkey, |
| 168 | char alg_name[CRYPTO_MAX_ALG_NAME]) |
| 169 | { |
| 170 | int n; |
| 171 | |
| 172 | if (strcmp(encoding, "pkcs1") == 0) { |
| 173 | /* The data wangled by the RSA algorithm is typically padded |
| 174 | * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447 |
| 175 | * sec 8.2]. |
| 176 | */ |
| 177 | if (!hash_algo) |
| 178 | n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME, |
| 179 | "pkcs1pad(%s)", |
| 180 | pkey->pkey_algo); |
| 181 | else |
| 182 | n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME, |
| 183 | "pkcs1pad(%s,%s)", |
| 184 | pkey->pkey_algo, hash_algo); |
| 185 | return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0; |
| 186 | } |
| 187 | |
| 188 | if (strcmp(encoding, "raw") == 0) { |
| 189 | strcpy(alg_name, pkey->pkey_algo); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | return -ENOPKG; |
| 194 | } |
| 195 | |
| 196 | static u8 *pkey_pack_u32(u8 *dst, u32 val) |
| 197 | { |
| 198 | memcpy(dst, &val, sizeof(val)); |
| 199 | return dst + sizeof(val); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Query information about a key. |
| 204 | */ |
| 205 | static int software_key_query(const struct kernel_pkey_params *params, |
| 206 | struct kernel_pkey_query *info) |
| 207 | { |
| 208 | struct crypto_akcipher *tfm; |
| 209 | struct public_key *pkey = params->key->payload.data[asym_crypto]; |
| 210 | char alg_name[CRYPTO_MAX_ALG_NAME]; |
| 211 | u8 *key, *ptr; |
| 212 | int ret, len; |
| 213 | |
| 214 | ret = software_key_determine_akcipher(params->encoding, |
| 215 | params->hash_algo, |
| 216 | pkey, alg_name); |
| 217 | if (ret < 0) |
| 218 | return ret; |
| 219 | |
| 220 | tfm = crypto_alloc_akcipher(alg_name, 0, 0); |
| 221 | if (IS_ERR(tfm)) |
| 222 | return PTR_ERR(tfm); |
| 223 | |
| 224 | key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen, |
| 225 | GFP_KERNEL); |
| 226 | if (!key) |
| 227 | goto error_free_tfm; |
| 228 | memcpy(key, pkey->key, pkey->keylen); |
| 229 | ptr = key + pkey->keylen; |
| 230 | ptr = pkey_pack_u32(ptr, pkey->algo); |
| 231 | ptr = pkey_pack_u32(ptr, pkey->paramlen); |
| 232 | memcpy(ptr, pkey->params, pkey->paramlen); |
| 233 | |
| 234 | if (pkey->key_is_private) |
| 235 | ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen); |
| 236 | else |
| 237 | ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen); |
| 238 | if (ret < 0) |
| 239 | goto error_free_key; |
| 240 | |
| 241 | len = crypto_akcipher_maxsize(tfm); |
| 242 | info->key_size = len * 8; |
| 243 | info->max_data_size = len; |
| 244 | info->max_sig_size = len; |
| 245 | info->max_enc_size = len; |
| 246 | info->max_dec_size = len; |
| 247 | info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT | |
| 248 | KEYCTL_SUPPORTS_VERIFY); |
| 249 | if (pkey->key_is_private) |
| 250 | info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT | |
| 251 | KEYCTL_SUPPORTS_SIGN); |
| 252 | ret = 0; |
| 253 | |
| 254 | error_free_key: |
| 255 | kfree(key); |
| 256 | error_free_tfm: |
| 257 | crypto_free_akcipher(tfm); |
| 258 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Do encryption, decryption and signing ops. |
| 264 | */ |
| 265 | static int software_key_eds_op(struct kernel_pkey_params *params, |
| 266 | const void *in, void *out) |
| 267 | { |
| 268 | const struct public_key *pkey = params->key->payload.data[asym_crypto]; |
| 269 | struct akcipher_request *req; |
| 270 | struct crypto_akcipher *tfm; |
| 271 | struct crypto_wait cwait; |
| 272 | struct scatterlist in_sg, out_sg; |
| 273 | char alg_name[CRYPTO_MAX_ALG_NAME]; |
| 274 | char *key, *ptr; |
| 275 | int ret; |
| 276 | |
| 277 | pr_devel("==>%s()\n", __func__); |
| 278 | |
| 279 | ret = software_key_determine_akcipher(params->encoding, |
| 280 | params->hash_algo, |
| 281 | pkey, alg_name); |
| 282 | if (ret < 0) |
| 283 | return ret; |
| 284 | |
| 285 | tfm = crypto_alloc_akcipher(alg_name, 0, 0); |
| 286 | if (IS_ERR(tfm)) |
| 287 | return PTR_ERR(tfm); |
| 288 | |
| 289 | req = akcipher_request_alloc(tfm, GFP_KERNEL); |
| 290 | if (!req) |
| 291 | goto error_free_tfm; |
| 292 | |
| 293 | key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen, |
| 294 | GFP_KERNEL); |
| 295 | if (!key) |
| 296 | goto error_free_req; |
| 297 | |
| 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); |
| 303 | |
| 304 | if (pkey->key_is_private) |
| 305 | ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen); |
| 306 | else |
| 307 | ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen); |
| 308 | if (ret) |
| 309 | goto error_free_key; |
| 310 | |
| 311 | sg_init_one(&in_sg, in, params->in_len); |
| 312 | sg_init_one(&out_sg, out, params->out_len); |
| 313 | akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len, |
| 314 | params->out_len); |
| 315 | crypto_init_wait(&cwait); |
| 316 | akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 317 | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 318 | crypto_req_done, &cwait); |
| 319 | |
| 320 | /* Perform the encryption calculation. */ |
| 321 | switch (params->op) { |
| 322 | case kernel_pkey_encrypt: |
| 323 | ret = crypto_akcipher_encrypt(req); |
| 324 | break; |
| 325 | case kernel_pkey_decrypt: |
| 326 | ret = crypto_akcipher_decrypt(req); |
| 327 | break; |
| 328 | case kernel_pkey_sign: |
| 329 | ret = crypto_akcipher_sign(req); |
| 330 | break; |
| 331 | default: |
| 332 | BUG(); |
| 333 | } |
| 334 | |
| 335 | ret = crypto_wait_req(ret, &cwait); |
| 336 | if (ret == 0) |
| 337 | ret = req->dst_len; |
| 338 | |
| 339 | error_free_key: |
| 340 | kfree(key); |
| 341 | error_free_req: |
| 342 | akcipher_request_free(req); |
| 343 | error_free_tfm: |
| 344 | crypto_free_akcipher(tfm); |
| 345 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Verify a signature using a public key. |
| 351 | */ |
| 352 | int public_key_verify_signature(const struct public_key *pkey, |
| 353 | const struct public_key_signature *sig) |
| 354 | { |
| 355 | struct crypto_wait cwait; |
| 356 | struct crypto_akcipher *tfm; |
| 357 | struct akcipher_request *req; |
| 358 | struct scatterlist src_sg[2]; |
| 359 | char alg_name[CRYPTO_MAX_ALG_NAME]; |
| 360 | char *key, *ptr; |
| 361 | int ret; |
| 362 | |
| 363 | pr_devel("==>%s()\n", __func__); |
| 364 | |
| 365 | BUG_ON(!pkey); |
| 366 | BUG_ON(!sig); |
| 367 | BUG_ON(!sig->s); |
| 368 | |
| 369 | ret = software_key_determine_akcipher(sig->encoding, |
| 370 | sig->hash_algo, |
| 371 | pkey, alg_name); |
| 372 | if (ret < 0) |
| 373 | return ret; |
| 374 | |
| 375 | tfm = crypto_alloc_akcipher(alg_name, 0, 0); |
| 376 | if (IS_ERR(tfm)) |
| 377 | return PTR_ERR(tfm); |
| 378 | |
| 379 | ret = -ENOMEM; |
| 380 | req = akcipher_request_alloc(tfm, GFP_KERNEL); |
| 381 | if (!req) |
| 382 | goto error_free_tfm; |
| 383 | |
| 384 | key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen, |
| 385 | GFP_KERNEL); |
| 386 | if (!key) |
| 387 | goto error_free_req; |
| 388 | |
| 389 | memcpy(key, pkey->key, pkey->keylen); |
| 390 | ptr = key + pkey->keylen; |
| 391 | ptr = pkey_pack_u32(ptr, pkey->algo); |
| 392 | ptr = pkey_pack_u32(ptr, pkey->paramlen); |
| 393 | memcpy(ptr, pkey->params, pkey->paramlen); |
| 394 | |
| 395 | if (pkey->key_is_private) |
| 396 | ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen); |
| 397 | else |
| 398 | ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen); |
| 399 | if (ret) |
| 400 | goto error_free_key; |
| 401 | |
| 402 | sg_init_table(src_sg, 2); |
| 403 | sg_set_buf(&src_sg[0], sig->s, sig->s_size); |
| 404 | sg_set_buf(&src_sg[1], sig->digest, sig->digest_size); |
| 405 | akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size, |
| 406 | sig->digest_size); |
| 407 | crypto_init_wait(&cwait); |
| 408 | akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 409 | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 410 | crypto_req_done, &cwait); |
| 411 | ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait); |
| 412 | |
| 413 | error_free_key: |
| 414 | kfree(key); |
| 415 | error_free_req: |
| 416 | akcipher_request_free(req); |
| 417 | error_free_tfm: |
| 418 | crypto_free_akcipher(tfm); |
| 419 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 420 | if (WARN_ON_ONCE(ret > 0)) |
| 421 | ret = -EINVAL; |
| 422 | return ret; |
| 423 | } |
| 424 | EXPORT_SYMBOL_GPL(public_key_verify_signature); |
| 425 | |
| 426 | static int public_key_verify_signature_2(const struct key *key, |
| 427 | const struct public_key_signature *sig) |
| 428 | { |
| 429 | const struct public_key *pk = key->payload.data[asym_crypto]; |
| 430 | return public_key_verify_signature(pk, sig); |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * Public key algorithm asymmetric key subtype |
| 435 | */ |
| 436 | struct asymmetric_key_subtype public_key_subtype = { |
| 437 | .owner = THIS_MODULE, |
| 438 | .name = "public_key", |
| 439 | .name_len = sizeof("public_key") - 1, |
| 440 | .describe = public_key_describe, |
| 441 | .destroy = public_key_destroy, |
| 442 | .query = software_key_query, |
| 443 | .eds_op = software_key_eds_op, |
| 444 | .verify_signature = public_key_verify_signature_2, |
| 445 | }; |
| 446 | EXPORT_SYMBOL_GPL(public_key_subtype); |
| 447 | #endif /* !__UBOOT__ */ |