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