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> |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 9 | #include <asm/types.h> |
| 10 | #include <asm/byteorder.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 11 | #include <linux/errno.h> |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 12 | #include <asm/types.h> |
| 13 | #include <asm/unaligned.h> |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 14 | #include <dm.h> |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 15 | #else |
| 16 | #include "fdt_host.h" |
| 17 | #include "mkimage.h" |
| 18 | #include <fdt_support.h> |
| 19 | #endif |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 20 | #include <u-boot/rsa-mod-exp.h> |
Jeroen Hofstee | 2b9912e | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 21 | #include <u-boot/rsa.h> |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 22 | |
Michael van der Westhuizen | e0f2f15 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 23 | /* Default public exponent for backward compatibility */ |
| 24 | #define RSA_DEFAULT_PUBEXP 65537 |
| 25 | |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 26 | /** |
Andrew Duda | da29f29 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 27 | * rsa_verify_padding() - Verify RSA message padding is valid |
| 28 | * |
| 29 | * Verify a RSA message's padding is consistent with PKCS1.5 |
| 30 | * padding as described in the RSA PKCS#1 v2.1 standard. |
| 31 | * |
| 32 | * @msg: Padded message |
| 33 | * @pad_len: Number of expected padding bytes |
| 34 | * @algo: Checksum algo structure having information on DER encoding etc. |
| 35 | * @return 0 on success, != 0 on failure |
| 36 | */ |
| 37 | static int rsa_verify_padding(const uint8_t *msg, const int pad_len, |
| 38 | struct checksum_algo *algo) |
| 39 | { |
| 40 | int ff_len; |
| 41 | int ret; |
| 42 | |
| 43 | /* first byte must be 0x00 */ |
| 44 | ret = *msg++; |
| 45 | /* second byte must be 0x01 */ |
| 46 | ret |= *msg++ ^ 0x01; |
| 47 | /* next ff_len bytes must be 0xff */ |
| 48 | ff_len = pad_len - algo->der_len - 3; |
| 49 | ret |= *msg ^ 0xff; |
| 50 | ret |= memcmp(msg, msg+1, ff_len-1); |
| 51 | msg += ff_len; |
| 52 | /* next byte must be 0x00 */ |
| 53 | ret |= *msg++; |
| 54 | /* next der_len bytes must match der_prefix */ |
| 55 | ret |= memcmp(msg, algo->der_prefix, algo->der_len); |
| 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame^] | 60 | int padding_pkcs_15_verify(struct image_sign_info *info, |
| 61 | uint8_t *msg, int msg_len, |
| 62 | const uint8_t *hash, int hash_len) |
| 63 | { |
| 64 | struct checksum_algo *checksum = info->checksum; |
| 65 | int ret, pad_len = msg_len - checksum->checksum_len; |
| 66 | |
| 67 | /* Check pkcs1.5 padding bytes. */ |
| 68 | ret = rsa_verify_padding(msg, pad_len, checksum); |
| 69 | if (ret) { |
| 70 | debug("In RSAVerify(): Padding check failed!\n"); |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | |
| 74 | /* Check hash. */ |
| 75 | if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) { |
| 76 | debug("In RSAVerify(): Hash check failed!\n"); |
| 77 | return -EACCES; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
Andrew Duda | da29f29 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 83 | /** |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 84 | * rsa_verify_key() - Verify a signature against some data using RSA Key |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 85 | * |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 86 | * Verify a RSA PKCS1.5 signature against an expected hash using |
| 87 | * the RSA Key properties in prop structure. |
| 88 | * |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame^] | 89 | * @info: Specifies key and FIT information |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 90 | * @prop: Specifies key |
| 91 | * @sig: Signature |
| 92 | * @sig_len: Number of bytes in signature |
| 93 | * @hash: Pointer to the expected hash |
Andrew Duda | 0c1d74f | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 94 | * @key_len: Number of bytes in rsa key |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 95 | * @return 0 if verified, -ve on error |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 96 | */ |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame^] | 97 | static int rsa_verify_key(struct image_sign_info *info, |
| 98 | struct key_prop *prop, const uint8_t *sig, |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 99 | const uint32_t sig_len, const uint8_t *hash, |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame^] | 100 | const uint32_t key_len) |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 101 | { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 102 | int ret; |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 103 | #if !defined(USE_HOSTCC) |
| 104 | struct udevice *mod_exp_dev; |
| 105 | #endif |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame^] | 106 | struct checksum_algo *checksum = info->checksum; |
| 107 | struct padding_algo *padding = info->padding; |
| 108 | int hash_len = checksum->checksum_len; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 109 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame^] | 110 | if (!prop || !sig || !hash || !checksum) |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 111 | return -EIO; |
| 112 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 113 | if (sig_len != (prop->num_bits / 8)) { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 114 | debug("Signature is of incorrect length %d\n", sig_len); |
| 115 | return -EINVAL; |
| 116 | } |
| 117 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame^] | 118 | debug("Checksum algorithm: %s", checksum->name); |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 119 | |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 120 | /* Sanity check for stack size */ |
| 121 | if (sig_len > RSA_MAX_SIG_BITS / 8) { |
| 122 | debug("Signature length %u exceeds maximum %d\n", sig_len, |
| 123 | RSA_MAX_SIG_BITS / 8); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 127 | uint8_t buf[sig_len]; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 128 | |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 129 | #if !defined(USE_HOSTCC) |
| 130 | ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev); |
| 131 | if (ret) { |
| 132 | printf("RSA: Can't find Modular Exp implementation\n"); |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | |
| 136 | ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf); |
| 137 | #else |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 138 | ret = rsa_mod_exp_sw(sig, sig_len, prop, buf); |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 139 | #endif |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 140 | if (ret) { |
| 141 | debug("Error in Modular exponentation\n"); |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 142 | return ret; |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 143 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 144 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame^] | 145 | ret = padding->verify(info, buf, key_len, hash, hash_len); |
Andrew Duda | da29f29 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 146 | if (ret) { |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame^] | 147 | debug("In RSAVerify(): padding check failed!\n"); |
| 148 | return ret; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 154 | /** |
| 155 | * rsa_verify_with_keynode() - Verify a signature against some data using |
| 156 | * information in node with prperties of RSA Key like modulus, exponent etc. |
| 157 | * |
| 158 | * Parse sign-node and fill a key_prop structure with properties of the |
| 159 | * key. Verify a RSA PKCS1.5 signature against an expected hash using |
| 160 | * the properties parsed |
| 161 | * |
| 162 | * @info: Specifies key and FIT information |
| 163 | * @hash: Pointer to the expected hash |
| 164 | * @sig: Signature |
| 165 | * @sig_len: Number of bytes in signature |
| 166 | * @node: Node having the RSA Key properties |
| 167 | * @return 0 if verified, -ve on error |
| 168 | */ |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 169 | static int rsa_verify_with_keynode(struct image_sign_info *info, |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 170 | const void *hash, uint8_t *sig, |
| 171 | uint sig_len, int node) |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 172 | { |
| 173 | const void *blob = info->fdt_blob; |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 174 | struct key_prop prop; |
Michael van der Westhuizen | e0f2f15 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 175 | int length; |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 176 | int ret = 0; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 177 | |
| 178 | if (node < 0) { |
| 179 | debug("%s: Skipping invalid node", __func__); |
| 180 | return -EBADF; |
| 181 | } |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 182 | |
| 183 | prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0); |
| 184 | |
| 185 | prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0); |
| 186 | |
| 187 | prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length); |
| 188 | if (!prop.public_exponent || length < sizeof(uint64_t)) |
| 189 | prop.public_exponent = NULL; |
| 190 | |
| 191 | prop.exp_len = sizeof(uint64_t); |
| 192 | |
| 193 | prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL); |
| 194 | |
| 195 | prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL); |
| 196 | |
| 197 | if (!prop.num_bits || !prop.modulus) { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 198 | debug("%s: Missing RSA key info", __func__); |
| 199 | return -EFAULT; |
| 200 | } |
| 201 | |
Philippe Reynes | 2003156 | 2018-11-14 13:51:00 +0100 | [diff] [blame^] | 202 | ret = rsa_verify_key(info, &prop, sig, sig_len, hash, |
| 203 | info->crypto->key_len); |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 204 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 205 | return ret; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | int rsa_verify(struct image_sign_info *info, |
| 209 | const struct image_region region[], int region_count, |
| 210 | uint8_t *sig, uint sig_len) |
| 211 | { |
| 212 | const void *blob = info->fdt_blob; |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 213 | /* Reserve memory for maximum checksum-length */ |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 214 | uint8_t hash[info->crypto->key_len]; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 215 | int ndepth, noffset; |
| 216 | int sig_node, node; |
| 217 | char name[100]; |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 218 | int ret; |
| 219 | |
| 220 | /* |
| 221 | * Verify that the checksum-length does not exceed the |
| 222 | * rsa-signature-length |
| 223 | */ |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 224 | if (info->checksum->checksum_len > |
| 225 | info->crypto->key_len) { |
Heiko Schocher | db1b5f3 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 226 | debug("%s: invlaid checksum-algorithm %s for %s\n", |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 227 | __func__, info->checksum->name, info->crypto->name); |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 228 | return -EINVAL; |
| 229 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 230 | |
| 231 | sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); |
| 232 | if (sig_node < 0) { |
| 233 | debug("%s: No signature node found\n", __func__); |
| 234 | return -ENOENT; |
| 235 | } |
| 236 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 237 | /* Calculate checksum with checksum-algorithm */ |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 238 | ret = info->checksum->calculate(info->checksum->name, |
Ruchika Gupta | b37b46f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 239 | region, region_count, hash); |
| 240 | if (ret < 0) { |
| 241 | debug("%s: Error in checksum calculation\n", __func__); |
| 242 | return -EINVAL; |
| 243 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 244 | |
| 245 | /* See if we must use a particular key */ |
| 246 | if (info->required_keynode != -1) { |
| 247 | ret = rsa_verify_with_keynode(info, hash, sig, sig_len, |
| 248 | info->required_keynode); |
| 249 | if (!ret) |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | /* Look for a key that matches our hint */ |
| 254 | snprintf(name, sizeof(name), "key-%s", info->keyname); |
| 255 | node = fdt_subnode_offset(blob, sig_node, name); |
| 256 | ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node); |
| 257 | if (!ret) |
| 258 | return ret; |
| 259 | |
| 260 | /* No luck, so try each of the keys in turn */ |
| 261 | for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth); |
| 262 | (noffset >= 0) && (ndepth > 0); |
| 263 | noffset = fdt_next_node(info->fit, noffset, &ndepth)) { |
| 264 | if (ndepth == 1 && noffset != node) { |
| 265 | ret = rsa_verify_with_keynode(info, hash, sig, sig_len, |
| 266 | noffset); |
| 267 | if (!ret) |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return ret; |
| 273 | } |