Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 6 | #ifndef USE_HOSTCC |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <fdtdec.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 11 | #include <asm/types.h> |
| 12 | #include <asm/byteorder.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 14 | #include <asm/types.h> |
| 15 | #include <asm/unaligned.h> |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 16 | #include <dm.h> |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 17 | #else |
| 18 | #include "fdt_host.h" |
| 19 | #include "mkimage.h" |
| 20 | #include <fdt_support.h> |
| 21 | #endif |
AKASHI Takahiro | 0cc7a75 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 22 | #include <linux/kconfig.h> |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 23 | #include <u-boot/rsa-mod-exp.h> |
Jeroen Hofstee | 2b9912e | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 24 | #include <u-boot/rsa.h> |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 25 | |
AKASHI Takahiro | 0cc7a75 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 26 | #ifndef __UBOOT__ |
| 27 | /* |
| 28 | * NOTE: |
| 29 | * Since host tools, like mkimage, make use of openssl library for |
| 30 | * RSA encryption, rsa_verify_with_pkey()/rsa_gen_key_prop() are |
| 31 | * of no use and should not be compiled in. |
| 32 | * So just turn off CONFIG_RSA_VERIFY_WITH_PKEY. |
| 33 | */ |
| 34 | |
| 35 | #undef CONFIG_RSA_VERIFY_WITH_PKEY |
| 36 | #endif |
| 37 | |
Michael van der Westhuizen | e0f2f15 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 38 | /* Default public exponent for backward compatibility */ |
| 39 | #define RSA_DEFAULT_PUBEXP 65537 |
| 40 | |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 41 | /** |
Andrew Duda | da29f29 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 42 | * rsa_verify_padding() - Verify RSA message padding is valid |
| 43 | * |
| 44 | * Verify a RSA message's padding is consistent with PKCS1.5 |
| 45 | * padding as described in the RSA PKCS#1 v2.1 standard. |
| 46 | * |
| 47 | * @msg: Padded message |
| 48 | * @pad_len: Number of expected padding bytes |
| 49 | * @algo: Checksum algo structure having information on DER encoding etc. |
| 50 | * @return 0 on success, != 0 on failure |
| 51 | */ |
| 52 | static int rsa_verify_padding(const uint8_t *msg, const int pad_len, |
| 53 | struct checksum_algo *algo) |
| 54 | { |
| 55 | int ff_len; |
| 56 | int ret; |
| 57 | |
| 58 | /* first byte must be 0x00 */ |
| 59 | ret = *msg++; |
| 60 | /* second byte must be 0x01 */ |
| 61 | ret |= *msg++ ^ 0x01; |
| 62 | /* next ff_len bytes must be 0xff */ |
| 63 | ff_len = pad_len - algo->der_len - 3; |
| 64 | ret |= *msg ^ 0xff; |
| 65 | ret |= memcmp(msg, msg+1, ff_len-1); |
| 66 | msg += ff_len; |
| 67 | /* next byte must be 0x00 */ |
| 68 | ret |= *msg++; |
| 69 | /* next der_len bytes must match der_prefix */ |
| 70 | ret |= memcmp(msg, algo->der_prefix, algo->der_len); |
| 71 | |
| 72 | return ret; |
| 73 | } |
| 74 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 75 | int padding_pkcs_15_verify(struct image_sign_info *info, |
| 76 | uint8_t *msg, int msg_len, |
| 77 | const uint8_t *hash, int hash_len) |
| 78 | { |
| 79 | struct checksum_algo *checksum = info->checksum; |
| 80 | int ret, pad_len = msg_len - checksum->checksum_len; |
| 81 | |
| 82 | /* Check pkcs1.5 padding bytes. */ |
| 83 | ret = rsa_verify_padding(msg, pad_len, checksum); |
| 84 | if (ret) { |
| 85 | debug("In RSAVerify(): Padding check failed!\n"); |
| 86 | return -EINVAL; |
| 87 | } |
| 88 | |
| 89 | /* Check hash. */ |
| 90 | if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) { |
| 91 | debug("In RSAVerify(): Hash check failed!\n"); |
| 92 | return -EACCES; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
Philippe Reynes | 061daa0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 98 | #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT |
| 99 | static void u32_i2osp(uint32_t val, uint8_t *buf) |
| 100 | { |
| 101 | buf[0] = (uint8_t)((val >> 24) & 0xff); |
| 102 | buf[1] = (uint8_t)((val >> 16) & 0xff); |
| 103 | buf[2] = (uint8_t)((val >> 8) & 0xff); |
| 104 | buf[3] = (uint8_t)((val >> 0) & 0xff); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * mask_generation_function1() - generate an octet string |
| 109 | * |
| 110 | * Generate an octet string used to check rsa signature. |
| 111 | * It use an input octet string and a hash function. |
| 112 | * |
| 113 | * @checksum: A Hash function |
| 114 | * @seed: Specifies an input variable octet string |
| 115 | * @seed_len: Size of the input octet string |
| 116 | * @output: Specifies the output octet string |
| 117 | * @output_len: Size of the output octet string |
| 118 | * @return 0 if the octet string was correctly generated, others on error |
| 119 | */ |
| 120 | static int mask_generation_function1(struct checksum_algo *checksum, |
| 121 | uint8_t *seed, int seed_len, |
| 122 | uint8_t *output, int output_len) |
| 123 | { |
| 124 | struct image_region region[2]; |
| 125 | int ret = 0, i, i_output = 0, region_count = 2; |
| 126 | uint32_t counter = 0; |
| 127 | uint8_t buf_counter[4], *tmp; |
| 128 | int hash_len = checksum->checksum_len; |
| 129 | |
| 130 | memset(output, 0, output_len); |
| 131 | |
| 132 | region[0].data = seed; |
| 133 | region[0].size = seed_len; |
| 134 | region[1].data = &buf_counter[0]; |
| 135 | region[1].size = 4; |
| 136 | |
| 137 | tmp = malloc(hash_len); |
| 138 | if (!tmp) { |
| 139 | debug("%s: can't allocate array tmp\n", __func__); |
| 140 | ret = -ENOMEM; |
| 141 | goto out; |
| 142 | } |
| 143 | |
| 144 | while (i_output < output_len) { |
| 145 | u32_i2osp(counter, &buf_counter[0]); |
| 146 | |
| 147 | ret = checksum->calculate(checksum->name, |
| 148 | region, region_count, |
| 149 | tmp); |
| 150 | if (ret < 0) { |
| 151 | debug("%s: Error in checksum calculation\n", __func__); |
| 152 | goto out; |
| 153 | } |
| 154 | |
| 155 | i = 0; |
| 156 | while ((i_output < output_len) && (i < hash_len)) { |
| 157 | output[i_output] = tmp[i]; |
| 158 | i_output++; |
| 159 | i++; |
| 160 | } |
| 161 | |
| 162 | counter++; |
| 163 | } |
| 164 | |
| 165 | out: |
| 166 | free(tmp); |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static int compute_hash_prime(struct checksum_algo *checksum, |
| 172 | uint8_t *pad, int pad_len, |
| 173 | uint8_t *hash, int hash_len, |
| 174 | uint8_t *salt, int salt_len, |
| 175 | uint8_t *hprime) |
| 176 | { |
| 177 | struct image_region region[3]; |
| 178 | int ret, region_count = 3; |
| 179 | |
| 180 | region[0].data = pad; |
| 181 | region[0].size = pad_len; |
| 182 | region[1].data = hash; |
| 183 | region[1].size = hash_len; |
| 184 | region[2].data = salt; |
| 185 | region[2].size = salt_len; |
| 186 | |
| 187 | ret = checksum->calculate(checksum->name, region, region_count, hprime); |
| 188 | if (ret < 0) { |
| 189 | debug("%s: Error in checksum calculation\n", __func__); |
| 190 | goto out; |
| 191 | } |
| 192 | |
| 193 | out: |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | int padding_pss_verify(struct image_sign_info *info, |
| 198 | uint8_t *msg, int msg_len, |
| 199 | const uint8_t *hash, int hash_len) |
| 200 | { |
| 201 | uint8_t *masked_db = NULL; |
| 202 | int masked_db_len = msg_len - hash_len - 1; |
| 203 | uint8_t *h = NULL, *hprime = NULL; |
| 204 | int h_len = hash_len; |
| 205 | uint8_t *db_mask = NULL; |
| 206 | int db_mask_len = masked_db_len; |
| 207 | uint8_t *db = NULL, *salt = NULL; |
| 208 | int db_len = masked_db_len, salt_len = msg_len - hash_len - 2; |
| 209 | uint8_t pad_zero[8] = { 0 }; |
| 210 | int ret, i, leftmost_bits = 1; |
| 211 | uint8_t leftmost_mask; |
| 212 | struct checksum_algo *checksum = info->checksum; |
| 213 | |
| 214 | /* first, allocate everything */ |
| 215 | masked_db = malloc(masked_db_len); |
| 216 | h = malloc(h_len); |
| 217 | db_mask = malloc(db_mask_len); |
| 218 | db = malloc(db_len); |
| 219 | salt = malloc(salt_len); |
| 220 | hprime = malloc(hash_len); |
| 221 | if (!masked_db || !h || !db_mask || !db || !salt || !hprime) { |
| 222 | printf("%s: can't allocate some buffer\n", __func__); |
| 223 | ret = -ENOMEM; |
| 224 | goto out; |
| 225 | } |
| 226 | |
| 227 | /* step 4: check if the last byte is 0xbc */ |
| 228 | if (msg[msg_len - 1] != 0xbc) { |
| 229 | printf("%s: invalid pss padding (0xbc is missing)\n", __func__); |
| 230 | ret = -EINVAL; |
| 231 | goto out; |
| 232 | } |
| 233 | |
| 234 | /* step 5 */ |
| 235 | memcpy(masked_db, msg, masked_db_len); |
| 236 | memcpy(h, msg + masked_db_len, h_len); |
| 237 | |
| 238 | /* step 6 */ |
| 239 | leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits); |
| 240 | if (masked_db[0] & leftmost_mask) { |
| 241 | printf("%s: invalid pss padding ", __func__); |
| 242 | printf("(leftmost bit of maskedDB not zero)\n"); |
| 243 | ret = -EINVAL; |
| 244 | goto out; |
| 245 | } |
| 246 | |
| 247 | /* step 7 */ |
| 248 | mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len); |
| 249 | |
| 250 | /* step 8 */ |
| 251 | for (i = 0; i < db_len; i++) |
| 252 | db[i] = masked_db[i] ^ db_mask[i]; |
| 253 | |
| 254 | /* step 9 */ |
| 255 | db[0] &= 0xff >> leftmost_bits; |
| 256 | |
| 257 | /* step 10 */ |
| 258 | if (db[0] != 0x01) { |
| 259 | printf("%s: invalid pss padding ", __func__); |
| 260 | printf("(leftmost byte of db isn't 0x01)\n"); |
| 261 | ret = EINVAL; |
| 262 | goto out; |
| 263 | } |
| 264 | |
| 265 | /* step 11 */ |
| 266 | memcpy(salt, &db[1], salt_len); |
| 267 | |
| 268 | /* step 12 & 13 */ |
| 269 | compute_hash_prime(checksum, pad_zero, 8, |
| 270 | (uint8_t *)hash, hash_len, |
| 271 | salt, salt_len, hprime); |
| 272 | |
| 273 | /* step 14 */ |
| 274 | ret = memcmp(h, hprime, hash_len); |
| 275 | |
| 276 | out: |
| 277 | free(hprime); |
| 278 | free(salt); |
| 279 | free(db); |
| 280 | free(db_mask); |
| 281 | free(h); |
| 282 | free(masked_db); |
| 283 | |
| 284 | return ret; |
| 285 | } |
| 286 | #endif |
| 287 | |
Heiko Stuebner | 447b1d7 | 2020-06-18 16:23:22 +0200 | [diff] [blame] | 288 | #if CONFIG_IS_ENABLED(FIT_SIGNATURE) || CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY) |
Andrew Duda | da29f29 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 289 | /** |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 290 | * rsa_verify_key() - Verify a signature against some data using RSA Key |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 291 | * |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 292 | * Verify a RSA PKCS1.5 signature against an expected hash using |
| 293 | * the RSA Key properties in prop structure. |
| 294 | * |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 295 | * @info: Specifies key and FIT information |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 296 | * @prop: Specifies key |
| 297 | * @sig: Signature |
| 298 | * @sig_len: Number of bytes in signature |
| 299 | * @hash: Pointer to the expected hash |
Andrew Duda | 0c1d74f | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 300 | * @key_len: Number of bytes in rsa key |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 301 | * @return 0 if verified, -ve on error |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 302 | */ |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 303 | static int rsa_verify_key(struct image_sign_info *info, |
| 304 | struct key_prop *prop, const uint8_t *sig, |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 305 | const uint32_t sig_len, const uint8_t *hash, |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 306 | const uint32_t key_len) |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 307 | { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 308 | int ret; |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 309 | #if !defined(USE_HOSTCC) |
| 310 | struct udevice *mod_exp_dev; |
| 311 | #endif |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 312 | struct checksum_algo *checksum = info->checksum; |
| 313 | struct padding_algo *padding = info->padding; |
Philippe Reynes | b02f2e7 | 2019-03-19 10:55:40 +0100 | [diff] [blame] | 314 | int hash_len; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 315 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 316 | if (!prop || !sig || !hash || !checksum) |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 317 | return -EIO; |
| 318 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 319 | if (sig_len != (prop->num_bits / 8)) { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 320 | debug("Signature is of incorrect length %d\n", sig_len); |
| 321 | return -EINVAL; |
| 322 | } |
| 323 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 324 | debug("Checksum algorithm: %s", checksum->name); |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 325 | |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 326 | /* Sanity check for stack size */ |
| 327 | if (sig_len > RSA_MAX_SIG_BITS / 8) { |
| 328 | debug("Signature length %u exceeds maximum %d\n", sig_len, |
| 329 | RSA_MAX_SIG_BITS / 8); |
| 330 | return -EINVAL; |
| 331 | } |
| 332 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 333 | uint8_t buf[sig_len]; |
Philippe Reynes | b02f2e7 | 2019-03-19 10:55:40 +0100 | [diff] [blame] | 334 | hash_len = checksum->checksum_len; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 335 | |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 336 | #if !defined(USE_HOSTCC) |
| 337 | ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev); |
| 338 | if (ret) { |
| 339 | printf("RSA: Can't find Modular Exp implementation\n"); |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | |
| 343 | ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf); |
| 344 | #else |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 345 | ret = rsa_mod_exp_sw(sig, sig_len, prop, buf); |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 346 | #endif |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 347 | if (ret) { |
| 348 | debug("Error in Modular exponentation\n"); |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 349 | return ret; |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 350 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 351 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 352 | ret = padding->verify(info, buf, key_len, hash, hash_len); |
Andrew Duda | da29f29 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 353 | if (ret) { |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 354 | debug("In RSAVerify(): padding check failed!\n"); |
| 355 | return ret; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
AKASHI Takahiro | b983cc2 | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 360 | #endif |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 361 | |
Heiko Stuebner | 447b1d7 | 2020-06-18 16:23:22 +0200 | [diff] [blame] | 362 | #if CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY) |
AKASHI Takahiro | 0cc7a75 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 363 | /** |
| 364 | * rsa_verify_with_pkey() - Verify a signature against some data using |
| 365 | * only modulus and exponent as RSA key properties. |
| 366 | * @info: Specifies key information |
| 367 | * @hash: Pointer to the expected hash |
| 368 | * @sig: Signature |
| 369 | * @sig_len: Number of bytes in signature |
| 370 | * |
| 371 | * Parse a RSA public key blob in DER format pointed to in @info and fill |
| 372 | * a key_prop structure with properties of the key. Then verify a RSA PKCS1.5 |
| 373 | * signature against an expected hash using the calculated properties. |
| 374 | * |
| 375 | * Return 0 if verified, -ve on error |
| 376 | */ |
| 377 | static int rsa_verify_with_pkey(struct image_sign_info *info, |
| 378 | const void *hash, uint8_t *sig, uint sig_len) |
| 379 | { |
| 380 | struct key_prop *prop; |
| 381 | int ret; |
| 382 | |
| 383 | /* Public key is self-described to fill key_prop */ |
| 384 | ret = rsa_gen_key_prop(info->key, info->keylen, &prop); |
| 385 | if (ret) { |
| 386 | debug("Generating necessary parameter for decoding failed\n"); |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | ret = rsa_verify_key(info, prop, sig, sig_len, hash, |
| 391 | info->crypto->key_len); |
| 392 | |
| 393 | rsa_free_key_prop(prop); |
| 394 | |
| 395 | return ret; |
| 396 | } |
| 397 | #else |
| 398 | static int rsa_verify_with_pkey(struct image_sign_info *info, |
| 399 | const void *hash, uint8_t *sig, uint sig_len) |
| 400 | { |
| 401 | return -EACCES; |
| 402 | } |
| 403 | #endif |
| 404 | |
AKASHI Takahiro | b983cc2 | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 405 | #if CONFIG_IS_ENABLED(FIT_SIGNATURE) |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 406 | /** |
| 407 | * rsa_verify_with_keynode() - Verify a signature against some data using |
| 408 | * information in node with prperties of RSA Key like modulus, exponent etc. |
| 409 | * |
| 410 | * Parse sign-node and fill a key_prop structure with properties of the |
| 411 | * key. Verify a RSA PKCS1.5 signature against an expected hash using |
| 412 | * the properties parsed |
| 413 | * |
| 414 | * @info: Specifies key and FIT information |
| 415 | * @hash: Pointer to the expected hash |
| 416 | * @sig: Signature |
| 417 | * @sig_len: Number of bytes in signature |
| 418 | * @node: Node having the RSA Key properties |
| 419 | * @return 0 if verified, -ve on error |
| 420 | */ |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 421 | static int rsa_verify_with_keynode(struct image_sign_info *info, |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 422 | const void *hash, uint8_t *sig, |
| 423 | uint sig_len, int node) |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 424 | { |
| 425 | const void *blob = info->fdt_blob; |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 426 | struct key_prop prop; |
Michael van der Westhuizen | e0f2f15 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 427 | int length; |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 428 | int ret = 0; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 429 | |
| 430 | if (node < 0) { |
| 431 | debug("%s: Skipping invalid node", __func__); |
| 432 | return -EBADF; |
| 433 | } |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 434 | |
| 435 | prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0); |
| 436 | |
| 437 | prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0); |
| 438 | |
| 439 | prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length); |
| 440 | if (!prop.public_exponent || length < sizeof(uint64_t)) |
| 441 | prop.public_exponent = NULL; |
| 442 | |
| 443 | prop.exp_len = sizeof(uint64_t); |
| 444 | |
| 445 | prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL); |
| 446 | |
| 447 | prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL); |
| 448 | |
Jan Kiszka | 48069ff | 2020-05-07 20:36:13 +0200 | [diff] [blame] | 449 | if (!prop.num_bits || !prop.modulus || !prop.rr) { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 450 | debug("%s: Missing RSA key info", __func__); |
| 451 | return -EFAULT; |
| 452 | } |
| 453 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 454 | ret = rsa_verify_key(info, &prop, sig, sig_len, hash, |
| 455 | info->crypto->key_len); |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 456 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 457 | return ret; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 458 | } |
AKASHI Takahiro | b983cc2 | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 459 | #else |
| 460 | static int rsa_verify_with_keynode(struct image_sign_info *info, |
| 461 | const void *hash, uint8_t *sig, |
| 462 | uint sig_len, int node) |
| 463 | { |
| 464 | return -EACCES; |
| 465 | } |
| 466 | #endif |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 467 | |
| 468 | int rsa_verify(struct image_sign_info *info, |
| 469 | const struct image_region region[], int region_count, |
| 470 | uint8_t *sig, uint sig_len) |
| 471 | { |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 472 | /* Reserve memory for maximum checksum-length */ |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 473 | uint8_t hash[info->crypto->key_len]; |
AKASHI Takahiro | b983cc2 | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 474 | int ret = -EACCES; |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 475 | |
| 476 | /* |
| 477 | * Verify that the checksum-length does not exceed the |
| 478 | * rsa-signature-length |
| 479 | */ |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 480 | if (info->checksum->checksum_len > |
| 481 | info->crypto->key_len) { |
Heiko Schocher | db1b5f3 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 482 | debug("%s: invlaid checksum-algorithm %s for %s\n", |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 483 | __func__, info->checksum->name, info->crypto->name); |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 484 | return -EINVAL; |
| 485 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 486 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 487 | /* Calculate checksum with checksum-algorithm */ |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 488 | ret = info->checksum->calculate(info->checksum->name, |
Ruchika Gupta | b37b46f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 489 | region, region_count, hash); |
| 490 | if (ret < 0) { |
| 491 | debug("%s: Error in checksum calculation\n", __func__); |
| 492 | return -EINVAL; |
| 493 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 494 | |
Heiko Stuebner | 447b1d7 | 2020-06-18 16:23:22 +0200 | [diff] [blame] | 495 | if (CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) { |
AKASHI Takahiro | 0cc7a75 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 496 | /* don't rely on fdt properties */ |
| 497 | ret = rsa_verify_with_pkey(info, hash, sig, sig_len); |
| 498 | |
| 499 | return ret; |
| 500 | } |
| 501 | |
AKASHI Takahiro | b983cc2 | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 502 | if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) { |
| 503 | const void *blob = info->fdt_blob; |
| 504 | int ndepth, noffset; |
| 505 | int sig_node, node; |
| 506 | char name[100]; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 507 | |
AKASHI Takahiro | b983cc2 | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 508 | sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); |
| 509 | if (sig_node < 0) { |
| 510 | debug("%s: No signature node found\n", __func__); |
| 511 | return -ENOENT; |
| 512 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 513 | |
AKASHI Takahiro | b983cc2 | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 514 | /* See if we must use a particular key */ |
| 515 | if (info->required_keynode != -1) { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 516 | ret = rsa_verify_with_keynode(info, hash, sig, sig_len, |
AKASHI Takahiro | b983cc2 | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 517 | info->required_keynode); |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | /* Look for a key that matches our hint */ |
| 522 | snprintf(name, sizeof(name), "key-%s", info->keyname); |
| 523 | node = fdt_subnode_offset(blob, sig_node, name); |
| 524 | ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node); |
| 525 | if (!ret) |
| 526 | return ret; |
| 527 | |
| 528 | /* No luck, so try each of the keys in turn */ |
| 529 | for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, |
| 530 | &ndepth); |
| 531 | (noffset >= 0) && (ndepth > 0); |
| 532 | noffset = fdt_next_node(info->fit, noffset, &ndepth)) { |
| 533 | if (ndepth == 1 && noffset != node) { |
| 534 | ret = rsa_verify_with_keynode(info, hash, |
| 535 | sig, sig_len, |
| 536 | noffset); |
| 537 | if (!ret) |
| 538 | break; |
| 539 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | return ret; |
| 544 | } |