Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, Google Inc. |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 7 | #ifndef USE_HOSTCC |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <fdtdec.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 | |
| 61 | /** |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 62 | * rsa_verify_key() - Verify a signature against some data using RSA Key |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 63 | * |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 64 | * Verify a RSA PKCS1.5 signature against an expected hash using |
| 65 | * the RSA Key properties in prop structure. |
| 66 | * |
| 67 | * @prop: Specifies key |
| 68 | * @sig: Signature |
| 69 | * @sig_len: Number of bytes in signature |
| 70 | * @hash: Pointer to the expected hash |
Andrew Duda | 0c1d74f | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 71 | * @key_len: Number of bytes in rsa key |
| 72 | * @algo: Checksum algo structure having information on DER encoding etc. |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 73 | * @return 0 if verified, -ve on error |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 74 | */ |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 75 | static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig, |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 76 | const uint32_t sig_len, const uint8_t *hash, |
Andrew Duda | 0c1d74f | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 77 | const uint32_t key_len, struct checksum_algo *algo) |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 78 | { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 79 | int pad_len; |
| 80 | int ret; |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 81 | #if !defined(USE_HOSTCC) |
| 82 | struct udevice *mod_exp_dev; |
| 83 | #endif |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 84 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 85 | if (!prop || !sig || !hash || !algo) |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 86 | return -EIO; |
| 87 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 88 | if (sig_len != (prop->num_bits / 8)) { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 89 | debug("Signature is of incorrect length %d\n", sig_len); |
| 90 | return -EINVAL; |
| 91 | } |
| 92 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 93 | debug("Checksum algorithm: %s", algo->name); |
| 94 | |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 95 | /* Sanity check for stack size */ |
| 96 | if (sig_len > RSA_MAX_SIG_BITS / 8) { |
| 97 | debug("Signature length %u exceeds maximum %d\n", sig_len, |
| 98 | RSA_MAX_SIG_BITS / 8); |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 102 | uint8_t buf[sig_len]; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 103 | |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 104 | #if !defined(USE_HOSTCC) |
| 105 | ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev); |
| 106 | if (ret) { |
| 107 | printf("RSA: Can't find Modular Exp implementation\n"); |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | |
| 111 | ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf); |
| 112 | #else |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 113 | ret = rsa_mod_exp_sw(sig, sig_len, prop, buf); |
Ruchika Gupta | c937ff6 | 2015-01-23 16:01:54 +0530 | [diff] [blame] | 114 | #endif |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 115 | if (ret) { |
| 116 | debug("Error in Modular exponentation\n"); |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 117 | return ret; |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 118 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 119 | |
Andrew Duda | 0c1d74f | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 120 | pad_len = key_len - algo->checksum_len; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 121 | |
| 122 | /* Check pkcs1.5 padding bytes. */ |
Andrew Duda | da29f29 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 123 | ret = rsa_verify_padding(buf, pad_len, algo); |
| 124 | if (ret) { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 125 | debug("In RSAVerify(): Padding check failed!\n"); |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | /* Check hash. */ |
| 130 | if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) { |
| 131 | debug("In RSAVerify(): Hash check failed!\n"); |
| 132 | return -EACCES; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 138 | /** |
| 139 | * rsa_verify_with_keynode() - Verify a signature against some data using |
| 140 | * information in node with prperties of RSA Key like modulus, exponent etc. |
| 141 | * |
| 142 | * Parse sign-node and fill a key_prop structure with properties of the |
| 143 | * key. Verify a RSA PKCS1.5 signature against an expected hash using |
| 144 | * the properties parsed |
| 145 | * |
| 146 | * @info: Specifies key and FIT information |
| 147 | * @hash: Pointer to the expected hash |
| 148 | * @sig: Signature |
| 149 | * @sig_len: Number of bytes in signature |
| 150 | * @node: Node having the RSA Key properties |
| 151 | * @return 0 if verified, -ve on error |
| 152 | */ |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 153 | static int rsa_verify_with_keynode(struct image_sign_info *info, |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 154 | const void *hash, uint8_t *sig, |
| 155 | uint sig_len, int node) |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 156 | { |
| 157 | const void *blob = info->fdt_blob; |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 158 | struct key_prop prop; |
Michael van der Westhuizen | e0f2f15 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 159 | int length; |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 160 | int ret = 0; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 161 | |
| 162 | if (node < 0) { |
| 163 | debug("%s: Skipping invalid node", __func__); |
| 164 | return -EBADF; |
| 165 | } |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 166 | |
| 167 | prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0); |
| 168 | |
| 169 | prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0); |
| 170 | |
| 171 | prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length); |
| 172 | if (!prop.public_exponent || length < sizeof(uint64_t)) |
| 173 | prop.public_exponent = NULL; |
| 174 | |
| 175 | prop.exp_len = sizeof(uint64_t); |
| 176 | |
| 177 | prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL); |
| 178 | |
| 179 | prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL); |
| 180 | |
| 181 | if (!prop.num_bits || !prop.modulus) { |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 182 | debug("%s: Missing RSA key info", __func__); |
| 183 | return -EFAULT; |
| 184 | } |
| 185 | |
Andrew Duda | 0c1d74f | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 186 | ret = rsa_verify_key(&prop, sig, sig_len, hash, |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 187 | info->crypto->key_len, info->checksum); |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 188 | |
Ruchika Gupta | fc2f424 | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 189 | return ret; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int rsa_verify(struct image_sign_info *info, |
| 193 | const struct image_region region[], int region_count, |
| 194 | uint8_t *sig, uint sig_len) |
| 195 | { |
| 196 | const void *blob = info->fdt_blob; |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 197 | /* Reserve memory for maximum checksum-length */ |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 198 | uint8_t hash[info->crypto->key_len]; |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 199 | int ndepth, noffset; |
| 200 | int sig_node, node; |
| 201 | char name[100]; |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 202 | int ret; |
| 203 | |
| 204 | /* |
| 205 | * Verify that the checksum-length does not exceed the |
| 206 | * rsa-signature-length |
| 207 | */ |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 208 | if (info->checksum->checksum_len > |
| 209 | info->crypto->key_len) { |
Heiko Schocher | db1b5f3 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 210 | debug("%s: invlaid checksum-algorithm %s for %s\n", |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 211 | __func__, info->checksum->name, info->crypto->name); |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 212 | return -EINVAL; |
| 213 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 214 | |
| 215 | sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); |
| 216 | if (sig_node < 0) { |
| 217 | debug("%s: No signature node found\n", __func__); |
| 218 | return -ENOENT; |
| 219 | } |
| 220 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 221 | /* Calculate checksum with checksum-algorithm */ |
Andrew Duda | 83dd98e | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 222 | ret = info->checksum->calculate(info->checksum->name, |
Ruchika Gupta | b37b46f | 2015-01-23 16:01:59 +0530 | [diff] [blame] | 223 | region, region_count, hash); |
| 224 | if (ret < 0) { |
| 225 | debug("%s: Error in checksum calculation\n", __func__); |
| 226 | return -EINVAL; |
| 227 | } |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 228 | |
| 229 | /* See if we must use a particular key */ |
| 230 | if (info->required_keynode != -1) { |
| 231 | ret = rsa_verify_with_keynode(info, hash, sig, sig_len, |
| 232 | info->required_keynode); |
| 233 | if (!ret) |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | /* Look for a key that matches our hint */ |
| 238 | snprintf(name, sizeof(name), "key-%s", info->keyname); |
| 239 | node = fdt_subnode_offset(blob, sig_node, name); |
| 240 | ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node); |
| 241 | if (!ret) |
| 242 | return ret; |
| 243 | |
| 244 | /* No luck, so try each of the keys in turn */ |
| 245 | for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth); |
| 246 | (noffset >= 0) && (ndepth > 0); |
| 247 | noffset = fdt_next_node(info->fit, noffset, &ndepth)) { |
| 248 | if (ndepth == 1 && noffset != node) { |
| 249 | ret = rsa_verify_with_keynode(info, hash, sig, sig_len, |
| 250 | noffset); |
| 251 | if (!ret) |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return ret; |
| 257 | } |