1 /* 2 * Copyright (c) 2013, Google Inc. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #ifndef USE_HOSTCC 8 #include <common.h> 9 #include <crypto.h> 10 #include <fdtdec.h> 11 #include <asm/types.h> 12 #include <asm/byteorder.h> 13 #include <linux/errno.h> 14 #include <asm/types.h> 15 #include <asm/unaligned.h> 16 #include <dm.h> 17 #else 18 #include "fdt_host.h" 19 #include "mkimage.h" 20 #include <fdt_support.h> 21 #endif 22 #include <u-boot/rsa-mod-exp.h> 23 #include <u-boot/rsa.h> 24 25 /* Default public exponent for backward compatibility */ 26 #define RSA_DEFAULT_PUBEXP 65537 27 28 /** 29 * rsa_verify_padding() - Verify RSA message padding is valid 30 * 31 * Verify a RSA message's padding is consistent with PKCS1.5 32 * padding as described in the RSA PKCS#1 v2.1 standard. 33 * 34 * @msg: Padded message 35 * @pad_len: Number of expected padding bytes 36 * @algo: Checksum algo structure having information on DER encoding etc. 37 * @return 0 on success, != 0 on failure 38 */ 39 static int rsa_verify_padding(const uint8_t *msg, const int pad_len, 40 struct checksum_algo *algo) 41 { 42 int ff_len; 43 int ret; 44 45 /* first byte must be 0x00 */ 46 ret = *msg++; 47 /* second byte must be 0x01 */ 48 ret |= *msg++ ^ 0x01; 49 /* next ff_len bytes must be 0xff */ 50 ff_len = pad_len - algo->der_len - 3; 51 ret |= *msg ^ 0xff; 52 ret |= memcmp(msg, msg+1, ff_len-1); 53 msg += ff_len; 54 /* next byte must be 0x00 */ 55 ret |= *msg++; 56 /* next der_len bytes must match der_prefix */ 57 ret |= memcmp(msg, algo->der_prefix, algo->der_len); 58 59 return ret; 60 } 61 62 #if !defined(USE_HOSTCC) 63 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 64 static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len) 65 { 66 int i; 67 68 for (i = 0; i < len; i++) 69 dst[i] = fdt32_to_cpu(src[len - 1 - i]); 70 } 71 72 static int rsa_mod_exp_hw(struct key_prop *prop, const uint8_t *sig, 73 const uint32_t sig_len, const uint32_t key_len, 74 uint8_t *output) 75 { 76 struct udevice *dev; 77 uint8_t sig_reverse[sig_len]; 78 uint8_t buf[sig_len]; 79 rsa_key rsa_key; 80 int i, ret; 81 82 if (key_len != RSA2048_BYTES) 83 return -EINVAL; 84 85 rsa_key.algo = CRYPTO_RSA2048; 86 rsa_key.n = malloc(key_len); 87 rsa_key.e = malloc(key_len); 88 rsa_key.c = malloc(key_len); 89 if (!rsa_key.n || !rsa_key.e || !rsa_key.c) 90 return -ENOMEM; 91 92 rsa_convert_big_endian(rsa_key.n, (uint32_t *)prop->modulus, 93 key_len / sizeof(uint32_t)); 94 rsa_convert_big_endian(rsa_key.e, (uint32_t *)prop->public_exponent_BN, 95 key_len / sizeof(uint32_t)); 96 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1 97 rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_c, 98 key_len / sizeof(uint32_t)); 99 #else 100 rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_np, 101 key_len / sizeof(uint32_t)); 102 #endif 103 for (i = 0; i < sig_len; i++) 104 sig_reverse[sig_len-1-i] = sig[i]; 105 106 dev = crypto_get_device(rsa_key.algo); 107 if (!dev) { 108 printf("No crypto device for expected RSA\n"); 109 return -ENODEV; 110 } 111 112 ret = crypto_rsa_verify(dev, &rsa_key, (u8 *)sig_reverse, buf); 113 if (ret) 114 goto out; 115 116 for (i = 0; i < sig_len; i++) 117 sig_reverse[sig_len-1-i] = buf[i]; 118 119 memcpy(output, sig_reverse, sig_len); 120 out: 121 free(rsa_key.n); 122 free(rsa_key.e); 123 free(rsa_key.c); 124 125 return ret; 126 } 127 #endif 128 #endif 129 130 int padding_pkcs_15_verify(struct image_sign_info *info, 131 uint8_t *msg, int msg_len, 132 const uint8_t *hash, int hash_len) 133 { 134 struct checksum_algo *checksum = info->checksum; 135 int ret, pad_len = msg_len - checksum->checksum_len; 136 137 /* Check pkcs1.5 padding bytes. */ 138 ret = rsa_verify_padding(msg, pad_len, checksum); 139 if (ret) { 140 debug("In RSAVerify(): Padding check failed!\n"); 141 return -EINVAL; 142 } 143 144 /* Check hash. */ 145 if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) { 146 debug("In RSAVerify(): Hash check failed!\n"); 147 return -EACCES; 148 } 149 150 return 0; 151 } 152 153 /** 154 * rsa_verify_key() - Verify a signature against some data using RSA Key 155 * 156 * Verify a RSA PKCS1.5 signature against an expected hash using 157 * the RSA Key properties in prop structure. 158 * 159 * @info: Specifies key and FIT information 160 * @prop: Specifies key 161 * @sig: Signature 162 * @sig_len: Number of bytes in signature 163 * @hash: Pointer to the expected hash 164 * @key_len: Number of bytes in rsa key 165 * @return 0 if verified, -ve on error 166 */ 167 static int rsa_verify_key(struct image_sign_info *info, 168 struct key_prop *prop, const uint8_t *sig, 169 const uint32_t sig_len, const uint8_t *hash, 170 const uint32_t key_len) 171 { 172 int ret; 173 struct checksum_algo *checksum = info->checksum; 174 struct padding_algo *padding = info->padding; 175 int hash_len = checksum->checksum_len; 176 177 if (!prop || !sig || !hash || !checksum) 178 return -EIO; 179 180 if (sig_len != (prop->num_bits / 8)) { 181 debug("Signature is of incorrect length %d\n", sig_len); 182 return -EINVAL; 183 } 184 185 debug("Checksum algorithm: %s", checksum->name); 186 187 /* Sanity check for stack size */ 188 if (sig_len > RSA_MAX_SIG_BITS / 8) { 189 debug("Signature length %u exceeds maximum %d\n", sig_len, 190 RSA_MAX_SIG_BITS / 8); 191 return -EINVAL; 192 } 193 194 uint8_t buf[sig_len]; 195 196 #if !defined(USE_HOSTCC) 197 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 198 ret = rsa_mod_exp_hw(prop, sig, sig_len, key_len, buf); 199 #else 200 struct udevice *mod_exp_dev; 201 202 ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev); 203 if (ret) { 204 printf("RSA: Can't find Modular Exp implementation\n"); 205 return -EINVAL; 206 } 207 208 ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf); 209 #endif 210 #else 211 ret = rsa_mod_exp_sw(sig, sig_len, prop, buf); 212 #endif 213 if (ret) { 214 debug("Error in Modular exponentation\n"); 215 return ret; 216 } 217 218 ret = padding->verify(info, buf, key_len, hash, hash_len); 219 if (ret) { 220 debug("In RSAVerify(): padding check failed!\n"); 221 return ret; 222 } 223 224 return 0; 225 } 226 227 /** 228 * rsa_verify_with_keynode() - Verify a signature against some data using 229 * information in node with prperties of RSA Key like modulus, exponent etc. 230 * 231 * Parse sign-node and fill a key_prop structure with properties of the 232 * key. Verify a RSA PKCS1.5 signature against an expected hash using 233 * the properties parsed 234 * 235 * @info: Specifies key and FIT information 236 * @hash: Pointer to the expected hash 237 * @sig: Signature 238 * @sig_len: Number of bytes in signature 239 * @node: Node having the RSA Key properties 240 * @return 0 if verified, -ve on error 241 */ 242 static int rsa_verify_with_keynode(struct image_sign_info *info, 243 const void *hash, uint8_t *sig, 244 uint sig_len, int node) 245 { 246 const void *blob = info->fdt_blob; 247 struct key_prop prop; 248 int length; 249 int ret = 0; 250 251 if (node < 0) { 252 debug("%s: Skipping invalid node", __func__); 253 return -EBADF; 254 } 255 256 prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0); 257 258 prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0); 259 260 prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length); 261 if (!prop.public_exponent || length < sizeof(uint64_t)) 262 prop.public_exponent = NULL; 263 264 prop.exp_len = sizeof(uint64_t); 265 266 prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL); 267 prop.public_exponent_BN = fdt_getprop(blob, node, "rsa,exponent-BN", NULL); 268 269 prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL); 270 271 if (!prop.num_bits || !prop.modulus) { 272 debug("%s: Missing RSA key info", __func__); 273 return -EFAULT; 274 } 275 276 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1 277 prop.factor_c = fdt_getprop(blob, node, "rsa,c", NULL); 278 if (!prop.factor_c) 279 return -EFAULT; 280 #else 281 prop.factor_np = fdt_getprop(blob, node, "rsa,np", NULL); 282 if (!prop.factor_np) 283 return -EFAULT; 284 #endif 285 ret = rsa_verify_key(info, &prop, sig, sig_len, hash, 286 info->crypto->key_len); 287 288 return ret; 289 } 290 291 int rsa_verify(struct image_sign_info *info, 292 const struct image_region region[], int region_count, 293 uint8_t *sig, uint sig_len) 294 { 295 const void *blob = info->fdt_blob; 296 /* Reserve memory for maximum checksum-length */ 297 uint8_t hash[info->crypto->key_len]; 298 int ndepth, noffset; 299 int sig_node, node; 300 char name[100]; 301 int ret; 302 303 /* 304 * Verify that the checksum-length does not exceed the 305 * rsa-signature-length 306 */ 307 if (info->checksum->checksum_len > 308 info->crypto->key_len) { 309 debug("%s: invlaid checksum-algorithm %s for %s\n", 310 __func__, info->checksum->name, info->crypto->name); 311 return -EINVAL; 312 } 313 314 sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); 315 if (sig_node < 0) { 316 debug("%s: No signature node found\n", __func__); 317 return -ENOENT; 318 } 319 320 /* Calculate checksum with checksum-algorithm */ 321 ret = info->checksum->calculate(info->checksum->name, 322 region, region_count, hash); 323 if (ret < 0) { 324 debug("%s: Error in checksum calculation\n", __func__); 325 return -EINVAL; 326 } 327 328 /* See if we must use a particular key */ 329 if (info->required_keynode != -1) { 330 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, 331 info->required_keynode); 332 if (!ret) 333 return ret; 334 } 335 336 /* Look for a key that matches our hint */ 337 snprintf(name, sizeof(name), "key-%s", info->keyname); 338 node = fdt_subnode_offset(blob, sig_node, name); 339 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node); 340 if (!ret) 341 return ret; 342 343 /* No luck, so try each of the keys in turn */ 344 for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth); 345 (noffset >= 0) && (ndepth > 0); 346 noffset = fdt_next_node(info->fit, noffset, &ndepth)) { 347 if (ndepth == 1 && noffset != node) { 348 ret = rsa_verify_with_keynode(info, hash, sig, sig_len, 349 noffset); 350 if (!ret) 351 break; 352 } 353 } 354 355 return ret; 356 } 357