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