Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 6 | #ifndef USE_HOSTCC |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 11 | #include <asm/types.h> |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 12 | #include <asm/byteorder.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 14 | #include <asm/types.h> |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 15 | #include <asm/unaligned.h> |
Ruchika Gupta | a0d3ca6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 16 | #include <dm.h> |
Heiko Schocher | d7b4232 | 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 | 75822f4 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 22 | #include <linux/kconfig.h> |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 23 | #include <u-boot/rsa-mod-exp.h> |
Jeroen Hofstee | bfe88fe | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 24 | #include <u-boot/rsa.h> |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 25 | |
AKASHI Takahiro | 75822f4 | 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 | 89f4ed1 | 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 | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 41 | /** |
Andrew Duda | 3db9ff0 | 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. |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 50 | * Return: 0 on success, != 0 on failure |
Andrew Duda | 3db9ff0 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 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 | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 75 | int padding_pkcs_15_verify(struct image_sign_info *info, |
SESA644425 | 1304b46 | 2022-03-09 01:27:15 -0800 | [diff] [blame] | 76 | const uint8_t *msg, int msg_len, |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 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 | |
Simon Glass | 101d349 | 2021-11-12 12:28:02 -0700 | [diff] [blame] | 82 | /* Check pkcs1.5 padding bytes */ |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 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 | |
Simon Glass | 101d349 | 2021-11-12 12:28:02 -0700 | [diff] [blame] | 89 | /* Check hash */ |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 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 | |
Alexandru Gagniuc | f3b5e58 | 2021-08-18 17:49:02 -0500 | [diff] [blame] | 98 | #ifndef USE_HOSTCC |
| 99 | U_BOOT_PADDING_ALGO(pkcs_15) = { |
| 100 | .name = "pkcs-1.5", |
| 101 | .verify = padding_pkcs_15_verify, |
| 102 | }; |
| 103 | #endif |
| 104 | |
Simon Glass | effa452 | 2021-09-25 19:43:23 -0600 | [diff] [blame] | 105 | #if CONFIG_IS_ENABLED(FIT_RSASSA_PSS) |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 106 | static void u32_i2osp(uint32_t val, uint8_t *buf) |
| 107 | { |
| 108 | buf[0] = (uint8_t)((val >> 24) & 0xff); |
| 109 | buf[1] = (uint8_t)((val >> 16) & 0xff); |
| 110 | buf[2] = (uint8_t)((val >> 8) & 0xff); |
| 111 | buf[3] = (uint8_t)((val >> 0) & 0xff); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * mask_generation_function1() - generate an octet string |
| 116 | * |
| 117 | * Generate an octet string used to check rsa signature. |
| 118 | * It use an input octet string and a hash function. |
| 119 | * |
| 120 | * @checksum: A Hash function |
| 121 | * @seed: Specifies an input variable octet string |
| 122 | * @seed_len: Size of the input octet string |
| 123 | * @output: Specifies the output octet string |
| 124 | * @output_len: Size of the output octet string |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 125 | * Return: 0 if the octet string was correctly generated, others on error |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 126 | */ |
| 127 | static int mask_generation_function1(struct checksum_algo *checksum, |
SESA644425 | 1304b46 | 2022-03-09 01:27:15 -0800 | [diff] [blame] | 128 | const uint8_t *seed, int seed_len, |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 129 | uint8_t *output, int output_len) |
| 130 | { |
| 131 | struct image_region region[2]; |
| 132 | int ret = 0, i, i_output = 0, region_count = 2; |
| 133 | uint32_t counter = 0; |
| 134 | uint8_t buf_counter[4], *tmp; |
| 135 | int hash_len = checksum->checksum_len; |
| 136 | |
| 137 | memset(output, 0, output_len); |
| 138 | |
| 139 | region[0].data = seed; |
| 140 | region[0].size = seed_len; |
| 141 | region[1].data = &buf_counter[0]; |
| 142 | region[1].size = 4; |
| 143 | |
| 144 | tmp = malloc(hash_len); |
| 145 | if (!tmp) { |
| 146 | debug("%s: can't allocate array tmp\n", __func__); |
| 147 | ret = -ENOMEM; |
| 148 | goto out; |
| 149 | } |
| 150 | |
| 151 | while (i_output < output_len) { |
| 152 | u32_i2osp(counter, &buf_counter[0]); |
| 153 | |
| 154 | ret = checksum->calculate(checksum->name, |
| 155 | region, region_count, |
| 156 | tmp); |
| 157 | if (ret < 0) { |
| 158 | debug("%s: Error in checksum calculation\n", __func__); |
| 159 | goto out; |
| 160 | } |
| 161 | |
| 162 | i = 0; |
| 163 | while ((i_output < output_len) && (i < hash_len)) { |
| 164 | output[i_output] = tmp[i]; |
| 165 | i_output++; |
| 166 | i++; |
| 167 | } |
| 168 | |
| 169 | counter++; |
| 170 | } |
| 171 | |
| 172 | out: |
| 173 | free(tmp); |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | static int compute_hash_prime(struct checksum_algo *checksum, |
SESA644425 | 1304b46 | 2022-03-09 01:27:15 -0800 | [diff] [blame] | 179 | const uint8_t *pad, int pad_len, |
| 180 | const uint8_t *hash, int hash_len, |
| 181 | const uint8_t *salt, int salt_len, |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 182 | uint8_t *hprime) |
| 183 | { |
| 184 | struct image_region region[3]; |
| 185 | int ret, region_count = 3; |
| 186 | |
| 187 | region[0].data = pad; |
| 188 | region[0].size = pad_len; |
| 189 | region[1].data = hash; |
| 190 | region[1].size = hash_len; |
| 191 | region[2].data = salt; |
| 192 | region[2].size = salt_len; |
| 193 | |
| 194 | ret = checksum->calculate(checksum->name, region, region_count, hprime); |
| 195 | if (ret < 0) { |
| 196 | debug("%s: Error in checksum calculation\n", __func__); |
| 197 | goto out; |
| 198 | } |
| 199 | |
| 200 | out: |
| 201 | return ret; |
| 202 | } |
| 203 | |
Heiko Stuebner | 1944dc1 | 2020-06-18 16:23:26 +0200 | [diff] [blame] | 204 | /* |
| 205 | * padding_pss_verify() - verify the pss padding of a signature |
| 206 | * |
SESA644425 | 38945ac | 2022-03-09 01:27:17 -0800 | [diff] [blame] | 207 | * Works with any salt length |
Heiko Stuebner | 1944dc1 | 2020-06-18 16:23:26 +0200 | [diff] [blame] | 208 | * |
SESA644425 | 732d270 | 2022-03-09 01:27:16 -0800 | [diff] [blame] | 209 | * msg is a concatenation of : masked_db + h + 0xbc |
| 210 | * Once unmasked, db is a concatenation of : [0x00]* + 0x01 + salt |
| 211 | * Length of 0-padding at begin of db depends on salt length. |
| 212 | * |
Heiko Stuebner | 1944dc1 | 2020-06-18 16:23:26 +0200 | [diff] [blame] | 213 | * @info: Specifies key and FIT information |
| 214 | * @msg: byte array of message, len equal to msg_len |
| 215 | * @msg_len: Message length |
| 216 | * @hash: Pointer to the expected hash |
| 217 | * @hash_len: Length of the hash |
Heinrich Schuchardt | df5117a | 2022-08-31 21:13:40 +0200 | [diff] [blame] | 218 | * |
| 219 | * Return: 0 if padding is correct, non-zero otherwise |
Heiko Stuebner | 1944dc1 | 2020-06-18 16:23:26 +0200 | [diff] [blame] | 220 | */ |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 221 | int padding_pss_verify(struct image_sign_info *info, |
SESA644425 | 1304b46 | 2022-03-09 01:27:15 -0800 | [diff] [blame] | 222 | const uint8_t *msg, int msg_len, |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 223 | const uint8_t *hash, int hash_len) |
| 224 | { |
SESA644425 | 732d270 | 2022-03-09 01:27:16 -0800 | [diff] [blame] | 225 | const uint8_t *masked_db = NULL; |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 226 | uint8_t *db_mask = NULL; |
SESA644425 | 732d270 | 2022-03-09 01:27:16 -0800 | [diff] [blame] | 227 | uint8_t *db = NULL; |
| 228 | int db_len = msg_len - hash_len - 1; |
| 229 | const uint8_t *h = NULL; |
| 230 | uint8_t *hprime = NULL; |
| 231 | int h_len = hash_len; |
SESA644425 | 38945ac | 2022-03-09 01:27:17 -0800 | [diff] [blame] | 232 | uint8_t *db_nopad = NULL, *salt = NULL; |
| 233 | int db_padlen, salt_len; |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 234 | uint8_t pad_zero[8] = { 0 }; |
| 235 | int ret, i, leftmost_bits = 1; |
| 236 | uint8_t leftmost_mask; |
| 237 | struct checksum_algo *checksum = info->checksum; |
| 238 | |
Heinrich Schuchardt | df5117a | 2022-08-31 21:13:40 +0200 | [diff] [blame] | 239 | if (db_len <= 0) |
| 240 | return -EINVAL; |
| 241 | |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 242 | /* first, allocate everything */ |
SESA644425 | 732d270 | 2022-03-09 01:27:16 -0800 | [diff] [blame] | 243 | db_mask = malloc(db_len); |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 244 | db = malloc(db_len); |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 245 | hprime = malloc(hash_len); |
SESA644425 | 732d270 | 2022-03-09 01:27:16 -0800 | [diff] [blame] | 246 | if (!db_mask || !db || !hprime) { |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 247 | printf("%s: can't allocate some buffer\n", __func__); |
| 248 | ret = -ENOMEM; |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | /* step 4: check if the last byte is 0xbc */ |
| 253 | if (msg[msg_len - 1] != 0xbc) { |
| 254 | printf("%s: invalid pss padding (0xbc is missing)\n", __func__); |
| 255 | ret = -EINVAL; |
| 256 | goto out; |
| 257 | } |
| 258 | |
| 259 | /* step 5 */ |
SESA644425 | 732d270 | 2022-03-09 01:27:16 -0800 | [diff] [blame] | 260 | masked_db = &msg[0]; |
| 261 | h = &msg[db_len]; |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 262 | |
| 263 | /* step 6 */ |
| 264 | leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits); |
| 265 | if (masked_db[0] & leftmost_mask) { |
| 266 | printf("%s: invalid pss padding ", __func__); |
| 267 | printf("(leftmost bit of maskedDB not zero)\n"); |
| 268 | ret = -EINVAL; |
| 269 | goto out; |
| 270 | } |
| 271 | |
| 272 | /* step 7 */ |
SESA644425 | 732d270 | 2022-03-09 01:27:16 -0800 | [diff] [blame] | 273 | mask_generation_function1(checksum, h, h_len, db_mask, db_len); |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 274 | |
| 275 | /* step 8 */ |
| 276 | for (i = 0; i < db_len; i++) |
| 277 | db[i] = masked_db[i] ^ db_mask[i]; |
| 278 | |
| 279 | /* step 9 */ |
| 280 | db[0] &= 0xff >> leftmost_bits; |
| 281 | |
| 282 | /* step 10 */ |
SESA644425 | 38945ac | 2022-03-09 01:27:17 -0800 | [diff] [blame] | 283 | db_padlen = 0; |
| 284 | while (db[db_padlen] == 0x00 && db_padlen < (db_len - 1)) |
| 285 | db_padlen++; |
| 286 | db_nopad = &db[db_padlen]; |
| 287 | if (db_nopad[0] != 0x01) { |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 288 | printf("%s: invalid pss padding ", __func__); |
SESA644425 | 38945ac | 2022-03-09 01:27:17 -0800 | [diff] [blame] | 289 | printf("(leftmost byte of db after 0-padding isn't 0x01)\n"); |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 290 | ret = EINVAL; |
| 291 | goto out; |
| 292 | } |
| 293 | |
| 294 | /* step 11 */ |
SESA644425 | 38945ac | 2022-03-09 01:27:17 -0800 | [diff] [blame] | 295 | salt_len = db_len - db_padlen - 1; |
| 296 | salt = &db_nopad[1]; |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 297 | |
| 298 | /* step 12 & 13 */ |
| 299 | compute_hash_prime(checksum, pad_zero, 8, |
SESA644425 | 1304b46 | 2022-03-09 01:27:15 -0800 | [diff] [blame] | 300 | hash, hash_len, |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 301 | salt, salt_len, hprime); |
| 302 | |
| 303 | /* step 14 */ |
| 304 | ret = memcmp(h, hprime, hash_len); |
| 305 | |
| 306 | out: |
| 307 | free(hprime); |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 308 | free(db); |
| 309 | free(db_mask); |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 310 | |
| 311 | return ret; |
| 312 | } |
Alexandru Gagniuc | f3b5e58 | 2021-08-18 17:49:02 -0500 | [diff] [blame] | 313 | |
| 314 | #ifndef USE_HOSTCC |
| 315 | U_BOOT_PADDING_ALGO(pss) = { |
| 316 | .name = "pss", |
| 317 | .verify = padding_pss_verify, |
| 318 | }; |
| 319 | #endif |
| 320 | |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 321 | #endif |
| 322 | |
Andrew Duda | 3db9ff0 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 323 | /** |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 324 | * rsa_verify_key() - Verify a signature against some data using RSA Key |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 325 | * |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 326 | * Verify a RSA PKCS1.5 signature against an expected hash using |
| 327 | * the RSA Key properties in prop structure. |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 328 | * |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 329 | * @info: Specifies key and FIT information |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 330 | * @prop: Specifies key |
| 331 | * @sig: Signature |
| 332 | * @sig_len: Number of bytes in signature |
| 333 | * @hash: Pointer to the expected hash |
Andrew Duda | 06ca6d6 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 334 | * @key_len: Number of bytes in rsa key |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 335 | * Return: 0 if verified, -ve on error |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 336 | */ |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 337 | static int rsa_verify_key(struct image_sign_info *info, |
| 338 | struct key_prop *prop, const uint8_t *sig, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 339 | const uint32_t sig_len, const uint8_t *hash, |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 340 | const uint32_t key_len) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 341 | { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 342 | int ret; |
Ruchika Gupta | a0d3ca6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 343 | #if !defined(USE_HOSTCC) |
| 344 | struct udevice *mod_exp_dev; |
| 345 | #endif |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 346 | struct checksum_algo *checksum = info->checksum; |
| 347 | struct padding_algo *padding = info->padding; |
Philippe Reynes | 4233227 | 2019-03-19 10:55:40 +0100 | [diff] [blame] | 348 | int hash_len; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 349 | |
Philippe Reynes | fd40a9b | 2021-10-15 11:28:47 +0200 | [diff] [blame] | 350 | if (!prop || !sig || !hash || !checksum || !padding) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 351 | return -EIO; |
| 352 | |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 353 | if (sig_len != (prop->num_bits / 8)) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 354 | debug("Signature is of incorrect length %d\n", sig_len); |
| 355 | return -EINVAL; |
| 356 | } |
| 357 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 358 | debug("Checksum algorithm: %s", checksum->name); |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 359 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 360 | /* Sanity check for stack size */ |
| 361 | if (sig_len > RSA_MAX_SIG_BITS / 8) { |
| 362 | debug("Signature length %u exceeds maximum %d\n", sig_len, |
| 363 | RSA_MAX_SIG_BITS / 8); |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 367 | uint8_t buf[sig_len]; |
Philippe Reynes | 4233227 | 2019-03-19 10:55:40 +0100 | [diff] [blame] | 368 | hash_len = checksum->checksum_len; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 369 | |
Ruchika Gupta | a0d3ca6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 370 | #if !defined(USE_HOSTCC) |
| 371 | ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev); |
| 372 | if (ret) { |
| 373 | printf("RSA: Can't find Modular Exp implementation\n"); |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
| 377 | ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf); |
| 378 | #else |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 379 | ret = rsa_mod_exp_sw(sig, sig_len, prop, buf); |
Ruchika Gupta | a0d3ca6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 380 | #endif |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 381 | if (ret) { |
| 382 | debug("Error in Modular exponentation\n"); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 383 | return ret; |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 384 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 385 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 386 | ret = padding->verify(info, buf, key_len, hash, hash_len); |
Andrew Duda | 3db9ff0 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 387 | if (ret) { |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 388 | debug("In RSAVerify(): padding check failed!\n"); |
| 389 | return ret; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
AKASHI Takahiro | 75822f4 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 395 | /** |
| 396 | * rsa_verify_with_pkey() - Verify a signature against some data using |
| 397 | * only modulus and exponent as RSA key properties. |
| 398 | * @info: Specifies key information |
| 399 | * @hash: Pointer to the expected hash |
| 400 | * @sig: Signature |
| 401 | * @sig_len: Number of bytes in signature |
| 402 | * |
| 403 | * Parse a RSA public key blob in DER format pointed to in @info and fill |
| 404 | * a key_prop structure with properties of the key. Then verify a RSA PKCS1.5 |
| 405 | * signature against an expected hash using the calculated properties. |
| 406 | * |
| 407 | * Return 0 if verified, -ve on error |
| 408 | */ |
AKASHI Takahiro | 30962bf | 2020-06-16 14:26:48 +0900 | [diff] [blame] | 409 | int rsa_verify_with_pkey(struct image_sign_info *info, |
| 410 | const void *hash, uint8_t *sig, uint sig_len) |
AKASHI Takahiro | 75822f4 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 411 | { |
| 412 | struct key_prop *prop; |
| 413 | int ret; |
| 414 | |
Simon Glass | effa452 | 2021-09-25 19:43:23 -0600 | [diff] [blame] | 415 | if (!CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY)) |
| 416 | return -EACCES; |
| 417 | |
AKASHI Takahiro | 75822f4 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 418 | /* Public key is self-described to fill key_prop */ |
| 419 | ret = rsa_gen_key_prop(info->key, info->keylen, &prop); |
| 420 | if (ret) { |
| 421 | debug("Generating necessary parameter for decoding failed\n"); |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | ret = rsa_verify_key(info, prop, sig, sig_len, hash, |
| 426 | info->crypto->key_len); |
| 427 | |
| 428 | rsa_free_key_prop(prop); |
| 429 | |
| 430 | return ret; |
| 431 | } |
AKASHI Takahiro | 75822f4 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 432 | |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 433 | #if CONFIG_IS_ENABLED(FIT_SIGNATURE) |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 434 | /** |
| 435 | * rsa_verify_with_keynode() - Verify a signature against some data using |
| 436 | * information in node with prperties of RSA Key like modulus, exponent etc. |
| 437 | * |
| 438 | * Parse sign-node and fill a key_prop structure with properties of the |
| 439 | * key. Verify a RSA PKCS1.5 signature against an expected hash using |
| 440 | * the properties parsed |
| 441 | * |
| 442 | * @info: Specifies key and FIT information |
| 443 | * @hash: Pointer to the expected hash |
| 444 | * @sig: Signature |
| 445 | * @sig_len: Number of bytes in signature |
| 446 | * @node: Node having the RSA Key properties |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 447 | * Return: 0 if verified, -ve on error |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 448 | */ |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 449 | static int rsa_verify_with_keynode(struct image_sign_info *info, |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 450 | const void *hash, uint8_t *sig, |
| 451 | uint sig_len, int node) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 452 | { |
| 453 | const void *blob = info->fdt_blob; |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 454 | struct key_prop prop; |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 455 | int length; |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 456 | int ret = 0; |
Matthieu CASTET | 335b01c | 2020-09-23 19:11:44 +0200 | [diff] [blame] | 457 | const char *algo; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 458 | |
| 459 | if (node < 0) { |
| 460 | debug("%s: Skipping invalid node", __func__); |
| 461 | return -EBADF; |
| 462 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 463 | |
Matthieu CASTET | 335b01c | 2020-09-23 19:11:44 +0200 | [diff] [blame] | 464 | algo = fdt_getprop(blob, node, "algo", NULL); |
Sean Anderson | b671080 | 2021-02-16 11:40:15 -0500 | [diff] [blame] | 465 | if (strcmp(info->name, algo)) { |
| 466 | debug("%s: Wrong algo: have %s, expected %s", __func__, |
| 467 | info->name, algo); |
Matthieu CASTET | 335b01c | 2020-09-23 19:11:44 +0200 | [diff] [blame] | 468 | return -EFAULT; |
Sean Anderson | b671080 | 2021-02-16 11:40:15 -0500 | [diff] [blame] | 469 | } |
Matthieu CASTET | 335b01c | 2020-09-23 19:11:44 +0200 | [diff] [blame] | 470 | |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 471 | prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 472 | |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 473 | prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 474 | |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 475 | prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length); |
| 476 | if (!prop.public_exponent || length < sizeof(uint64_t)) |
| 477 | prop.public_exponent = NULL; |
| 478 | |
| 479 | prop.exp_len = sizeof(uint64_t); |
| 480 | |
| 481 | prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL); |
| 482 | |
| 483 | prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL); |
| 484 | |
Jan Kiszka | 3637b8b | 2020-05-07 20:36:13 +0200 | [diff] [blame] | 485 | if (!prop.num_bits || !prop.modulus || !prop.rr) { |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 486 | debug("%s: Missing RSA key info", __func__); |
| 487 | return -EFAULT; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 490 | ret = rsa_verify_key(info, &prop, sig, sig_len, hash, |
| 491 | info->crypto->key_len); |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 492 | |
| 493 | return ret; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 494 | } |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 495 | #else |
| 496 | static int rsa_verify_with_keynode(struct image_sign_info *info, |
| 497 | const void *hash, uint8_t *sig, |
| 498 | uint sig_len, int node) |
| 499 | { |
| 500 | return -EACCES; |
| 501 | } |
| 502 | #endif |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 503 | |
Heiko Stuebner | b404409 | 2020-05-22 16:20:33 +0200 | [diff] [blame] | 504 | int rsa_verify_hash(struct image_sign_info *info, |
| 505 | const uint8_t *hash, uint8_t *sig, uint sig_len) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 506 | { |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 507 | int ret = -EACCES; |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 508 | |
Heiko Stuebner | 72c4680 | 2020-06-18 16:23:22 +0200 | [diff] [blame] | 509 | if (CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY) && !info->fdt_blob) { |
AKASHI Takahiro | 75822f4 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 510 | /* don't rely on fdt properties */ |
| 511 | ret = rsa_verify_with_pkey(info, hash, sig, sig_len); |
Simon Glass | 101d349 | 2021-11-12 12:28:02 -0700 | [diff] [blame] | 512 | if (ret) |
| 513 | debug("%s: rsa_verify_with_pkey() failed\n", __func__); |
AKASHI Takahiro | 75822f4 | 2020-02-21 15:12:59 +0900 | [diff] [blame] | 514 | return ret; |
| 515 | } |
| 516 | |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 517 | if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) { |
| 518 | const void *blob = info->fdt_blob; |
| 519 | int ndepth, noffset; |
| 520 | int sig_node, node; |
| 521 | char name[100]; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 522 | |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 523 | sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); |
| 524 | if (sig_node < 0) { |
| 525 | debug("%s: No signature node found\n", __func__); |
| 526 | return -ENOENT; |
| 527 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 528 | |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 529 | /* See if we must use a particular key */ |
| 530 | if (info->required_keynode != -1) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 531 | ret = rsa_verify_with_keynode(info, hash, sig, sig_len, |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 532 | info->required_keynode); |
Simon Glass | 101d349 | 2021-11-12 12:28:02 -0700 | [diff] [blame] | 533 | if (ret) |
| 534 | debug("%s: Failed to verify required_keynode\n", |
| 535 | __func__); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | /* Look for a key that matches our hint */ |
| 540 | snprintf(name, sizeof(name), "key-%s", info->keyname); |
| 541 | node = fdt_subnode_offset(blob, sig_node, name); |
| 542 | ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node); |
| 543 | if (!ret) |
| 544 | return ret; |
Simon Glass | 101d349 | 2021-11-12 12:28:02 -0700 | [diff] [blame] | 545 | debug("%s: Could not verify key '%s', trying all\n", __func__, |
| 546 | name); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 547 | |
| 548 | /* No luck, so try each of the keys in turn */ |
Philippe Reynes | 7258c3f | 2021-01-12 19:18:54 +0100 | [diff] [blame] | 549 | for (ndepth = 0, noffset = fdt_next_node(blob, sig_node, |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 550 | &ndepth); |
| 551 | (noffset >= 0) && (ndepth > 0); |
Philippe Reynes | 7258c3f | 2021-01-12 19:18:54 +0100 | [diff] [blame] | 552 | noffset = fdt_next_node(blob, noffset, &ndepth)) { |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 553 | if (ndepth == 1 && noffset != node) { |
| 554 | ret = rsa_verify_with_keynode(info, hash, |
| 555 | sig, sig_len, |
| 556 | noffset); |
| 557 | if (!ret) |
| 558 | break; |
| 559 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
Simon Glass | 101d349 | 2021-11-12 12:28:02 -0700 | [diff] [blame] | 562 | debug("%s: Failed to verify by any means\n", __func__); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 563 | |
| 564 | return ret; |
| 565 | } |
Heiko Stuebner | b404409 | 2020-05-22 16:20:33 +0200 | [diff] [blame] | 566 | |
| 567 | int rsa_verify(struct image_sign_info *info, |
| 568 | const struct image_region region[], int region_count, |
| 569 | uint8_t *sig, uint sig_len) |
| 570 | { |
| 571 | /* Reserve memory for maximum checksum-length */ |
| 572 | uint8_t hash[info->crypto->key_len]; |
Heinrich Schuchardt | 574fab9 | 2020-10-08 20:53:13 +0200 | [diff] [blame] | 573 | int ret; |
Heiko Stuebner | b404409 | 2020-05-22 16:20:33 +0200 | [diff] [blame] | 574 | |
| 575 | /* |
| 576 | * Verify that the checksum-length does not exceed the |
| 577 | * rsa-signature-length |
| 578 | */ |
| 579 | if (info->checksum->checksum_len > |
| 580 | info->crypto->key_len) { |
Thomas Perrot | 4e1e2a2 | 2021-07-19 16:04:44 +0200 | [diff] [blame] | 581 | debug("%s: invalid checksum-algorithm %s for %s\n", |
Heiko Stuebner | b404409 | 2020-05-22 16:20:33 +0200 | [diff] [blame] | 582 | __func__, info->checksum->name, info->crypto->name); |
| 583 | return -EINVAL; |
| 584 | } |
| 585 | |
| 586 | /* Calculate checksum with checksum-algorithm */ |
| 587 | ret = info->checksum->calculate(info->checksum->name, |
| 588 | region, region_count, hash); |
| 589 | if (ret < 0) { |
| 590 | debug("%s: Error in checksum calculation\n", __func__); |
| 591 | return -EINVAL; |
| 592 | } |
| 593 | |
| 594 | return rsa_verify_hash(info, hash, sig, sig_len); |
| 595 | } |
Alexandru Gagniuc | 92c5fdc | 2021-07-14 17:05:40 -0500 | [diff] [blame] | 596 | |
| 597 | #ifndef USE_HOSTCC |
| 598 | |
| 599 | U_BOOT_CRYPTO_ALGO(rsa2048) = { |
| 600 | .name = "rsa2048", |
| 601 | .key_len = RSA2048_BYTES, |
| 602 | .verify = rsa_verify, |
| 603 | }; |
| 604 | |
Jamin Lin | 5975ad7 | 2022-01-19 16:23:21 +0800 | [diff] [blame] | 605 | U_BOOT_CRYPTO_ALGO(rsa3072) = { |
| 606 | .name = "rsa3072", |
| 607 | .key_len = RSA3072_BYTES, |
| 608 | .verify = rsa_verify, |
| 609 | }; |
| 610 | |
Alexandru Gagniuc | 92c5fdc | 2021-07-14 17:05:40 -0500 | [diff] [blame] | 611 | U_BOOT_CRYPTO_ALGO(rsa4096) = { |
| 612 | .name = "rsa4096", |
| 613 | .key_len = RSA4096_BYTES, |
| 614 | .verify = rsa_verify, |
| 615 | }; |
| 616 | |
| 617 | #endif |