119c402afSSimon Glass /* 219c402afSSimon Glass * Copyright (c) 2013, Google Inc. 319c402afSSimon Glass * 41a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 519c402afSSimon Glass */ 619c402afSSimon Glass 729a23f9dSHeiko Schocher #ifndef USE_HOSTCC 819c402afSSimon Glass #include <common.h> 919c402afSSimon Glass #include <fdtdec.h> 1029a23f9dSHeiko Schocher #include <asm/types.h> 1129a23f9dSHeiko Schocher #include <asm/byteorder.h> 121221ce45SMasahiro Yamada #include <linux/errno.h> 1329a23f9dSHeiko Schocher #include <asm/types.h> 1429a23f9dSHeiko Schocher #include <asm/unaligned.h> 15c937ff6dSRuchika Gupta #include <dm.h> 1629a23f9dSHeiko Schocher #else 1729a23f9dSHeiko Schocher #include "fdt_host.h" 1829a23f9dSHeiko Schocher #include "mkimage.h" 1929a23f9dSHeiko Schocher #include <fdt_support.h> 2029a23f9dSHeiko Schocher #endif 21fc2f4246SRuchika Gupta #include <u-boot/rsa-mod-exp.h> 222b9912e6SJeroen Hofstee #include <u-boot/rsa.h> 2329a23f9dSHeiko Schocher 24e0f2f155SMichael van der Westhuizen /* Default public exponent for backward compatibility */ 25e0f2f155SMichael van der Westhuizen #define RSA_DEFAULT_PUBEXP 65537 26e0f2f155SMichael van der Westhuizen 2719c402afSSimon Glass /** 28*da29f299SAndrew Duda * rsa_verify_padding() - Verify RSA message padding is valid 29*da29f299SAndrew Duda * 30*da29f299SAndrew Duda * Verify a RSA message's padding is consistent with PKCS1.5 31*da29f299SAndrew Duda * padding as described in the RSA PKCS#1 v2.1 standard. 32*da29f299SAndrew Duda * 33*da29f299SAndrew Duda * @msg: Padded message 34*da29f299SAndrew Duda * @pad_len: Number of expected padding bytes 35*da29f299SAndrew Duda * @algo: Checksum algo structure having information on DER encoding etc. 36*da29f299SAndrew Duda * @return 0 on success, != 0 on failure 37*da29f299SAndrew Duda */ 38*da29f299SAndrew Duda static int rsa_verify_padding(const uint8_t *msg, const int pad_len, 39*da29f299SAndrew Duda struct checksum_algo *algo) 40*da29f299SAndrew Duda { 41*da29f299SAndrew Duda int ff_len; 42*da29f299SAndrew Duda int ret; 43*da29f299SAndrew Duda 44*da29f299SAndrew Duda /* first byte must be 0x00 */ 45*da29f299SAndrew Duda ret = *msg++; 46*da29f299SAndrew Duda /* second byte must be 0x01 */ 47*da29f299SAndrew Duda ret |= *msg++ ^ 0x01; 48*da29f299SAndrew Duda /* next ff_len bytes must be 0xff */ 49*da29f299SAndrew Duda ff_len = pad_len - algo->der_len - 3; 50*da29f299SAndrew Duda ret |= *msg ^ 0xff; 51*da29f299SAndrew Duda ret |= memcmp(msg, msg+1, ff_len-1); 52*da29f299SAndrew Duda msg += ff_len; 53*da29f299SAndrew Duda /* next byte must be 0x00 */ 54*da29f299SAndrew Duda ret |= *msg++; 55*da29f299SAndrew Duda /* next der_len bytes must match der_prefix */ 56*da29f299SAndrew Duda ret |= memcmp(msg, algo->der_prefix, algo->der_len); 57*da29f299SAndrew Duda 58*da29f299SAndrew Duda return ret; 59*da29f299SAndrew Duda } 60*da29f299SAndrew Duda 61*da29f299SAndrew Duda /** 62fc2f4246SRuchika Gupta * rsa_verify_key() - Verify a signature against some data using RSA Key 6319c402afSSimon Glass * 64fc2f4246SRuchika Gupta * Verify a RSA PKCS1.5 signature against an expected hash using 65fc2f4246SRuchika Gupta * the RSA Key properties in prop structure. 66fc2f4246SRuchika Gupta * 67fc2f4246SRuchika Gupta * @prop: Specifies key 68fc2f4246SRuchika Gupta * @sig: Signature 69fc2f4246SRuchika Gupta * @sig_len: Number of bytes in signature 70fc2f4246SRuchika Gupta * @hash: Pointer to the expected hash 71fc2f4246SRuchika Gupta * @algo: Checksum algo structure having information on RSA padding etc. 72fc2f4246SRuchika Gupta * @return 0 if verified, -ve on error 7319c402afSSimon Glass */ 74fc2f4246SRuchika Gupta static int rsa_verify_key(struct key_prop *prop, const uint8_t *sig, 75646257d1SHeiko Schocher const uint32_t sig_len, const uint8_t *hash, 76646257d1SHeiko Schocher struct checksum_algo *algo) 7719c402afSSimon Glass { 7819c402afSSimon Glass const uint8_t *padding; 7919c402afSSimon Glass int pad_len; 8019c402afSSimon Glass int ret; 81c937ff6dSRuchika Gupta #if !defined(USE_HOSTCC) 82c937ff6dSRuchika Gupta struct udevice *mod_exp_dev; 83c937ff6dSRuchika Gupta #endif 8419c402afSSimon Glass 85fc2f4246SRuchika Gupta if (!prop || !sig || !hash || !algo) 8619c402afSSimon Glass return -EIO; 8719c402afSSimon Glass 88fc2f4246SRuchika Gupta if (sig_len != (prop->num_bits / 8)) { 8919c402afSSimon Glass debug("Signature is of incorrect length %d\n", sig_len); 9019c402afSSimon Glass return -EINVAL; 9119c402afSSimon Glass } 9219c402afSSimon Glass 93646257d1SHeiko Schocher debug("Checksum algorithm: %s", algo->name); 94646257d1SHeiko Schocher 9519c402afSSimon Glass /* Sanity check for stack size */ 9619c402afSSimon Glass if (sig_len > RSA_MAX_SIG_BITS / 8) { 9719c402afSSimon Glass debug("Signature length %u exceeds maximum %d\n", sig_len, 9819c402afSSimon Glass RSA_MAX_SIG_BITS / 8); 9919c402afSSimon Glass return -EINVAL; 10019c402afSSimon Glass } 10119c402afSSimon Glass 102fc2f4246SRuchika Gupta uint8_t buf[sig_len]; 10319c402afSSimon Glass 104c937ff6dSRuchika Gupta #if !defined(USE_HOSTCC) 105c937ff6dSRuchika Gupta ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev); 106c937ff6dSRuchika Gupta if (ret) { 107c937ff6dSRuchika Gupta printf("RSA: Can't find Modular Exp implementation\n"); 108c937ff6dSRuchika Gupta return -EINVAL; 109c937ff6dSRuchika Gupta } 110c937ff6dSRuchika Gupta 111c937ff6dSRuchika Gupta ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf); 112c937ff6dSRuchika Gupta #else 113fc2f4246SRuchika Gupta ret = rsa_mod_exp_sw(sig, sig_len, prop, buf); 114c937ff6dSRuchika Gupta #endif 115fc2f4246SRuchika Gupta if (ret) { 116fc2f4246SRuchika Gupta debug("Error in Modular exponentation\n"); 11719c402afSSimon Glass return ret; 118fc2f4246SRuchika Gupta } 11919c402afSSimon Glass 1205300a4f9SAndrew Duda pad_len = algo->key_len - algo->checksum_len; 12119c402afSSimon Glass 12219c402afSSimon Glass /* Check pkcs1.5 padding bytes. */ 123*da29f299SAndrew Duda ret = rsa_verify_padding(buf, pad_len, algo); 124*da29f299SAndrew Duda if (ret) { 12519c402afSSimon Glass debug("In RSAVerify(): Padding check failed!\n"); 12619c402afSSimon Glass return -EINVAL; 12719c402afSSimon Glass } 12819c402afSSimon Glass 12919c402afSSimon Glass /* Check hash. */ 13019c402afSSimon Glass if (memcmp((uint8_t *)buf + pad_len, hash, sig_len - pad_len)) { 13119c402afSSimon Glass debug("In RSAVerify(): Hash check failed!\n"); 13219c402afSSimon Glass return -EACCES; 13319c402afSSimon Glass } 13419c402afSSimon Glass 13519c402afSSimon Glass return 0; 13619c402afSSimon Glass } 13719c402afSSimon Glass 138fc2f4246SRuchika Gupta /** 139fc2f4246SRuchika Gupta * rsa_verify_with_keynode() - Verify a signature against some data using 140fc2f4246SRuchika Gupta * information in node with prperties of RSA Key like modulus, exponent etc. 141fc2f4246SRuchika Gupta * 142fc2f4246SRuchika Gupta * Parse sign-node and fill a key_prop structure with properties of the 143fc2f4246SRuchika Gupta * key. Verify a RSA PKCS1.5 signature against an expected hash using 144fc2f4246SRuchika Gupta * the properties parsed 145fc2f4246SRuchika Gupta * 146fc2f4246SRuchika Gupta * @info: Specifies key and FIT information 147fc2f4246SRuchika Gupta * @hash: Pointer to the expected hash 148fc2f4246SRuchika Gupta * @sig: Signature 149fc2f4246SRuchika Gupta * @sig_len: Number of bytes in signature 150fc2f4246SRuchika Gupta * @node: Node having the RSA Key properties 151fc2f4246SRuchika Gupta * @return 0 if verified, -ve on error 152fc2f4246SRuchika Gupta */ 15319c402afSSimon Glass static int rsa_verify_with_keynode(struct image_sign_info *info, 154fc2f4246SRuchika Gupta const void *hash, uint8_t *sig, 155fc2f4246SRuchika Gupta uint sig_len, int node) 15619c402afSSimon Glass { 15719c402afSSimon Glass const void *blob = info->fdt_blob; 158fc2f4246SRuchika Gupta struct key_prop prop; 159e0f2f155SMichael van der Westhuizen int length; 160fc2f4246SRuchika Gupta int ret = 0; 16119c402afSSimon Glass 16219c402afSSimon Glass if (node < 0) { 16319c402afSSimon Glass debug("%s: Skipping invalid node", __func__); 16419c402afSSimon Glass return -EBADF; 16519c402afSSimon Glass } 166fc2f4246SRuchika Gupta 167fc2f4246SRuchika Gupta prop.num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0); 168fc2f4246SRuchika Gupta 169fc2f4246SRuchika Gupta prop.n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0); 170fc2f4246SRuchika Gupta 171fc2f4246SRuchika Gupta prop.public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length); 172fc2f4246SRuchika Gupta if (!prop.public_exponent || length < sizeof(uint64_t)) 173fc2f4246SRuchika Gupta prop.public_exponent = NULL; 174fc2f4246SRuchika Gupta 175fc2f4246SRuchika Gupta prop.exp_len = sizeof(uint64_t); 176fc2f4246SRuchika Gupta 177fc2f4246SRuchika Gupta prop.modulus = fdt_getprop(blob, node, "rsa,modulus", NULL); 178fc2f4246SRuchika Gupta 179fc2f4246SRuchika Gupta prop.rr = fdt_getprop(blob, node, "rsa,r-squared", NULL); 180fc2f4246SRuchika Gupta 181fc2f4246SRuchika Gupta if (!prop.num_bits || !prop.modulus) { 18219c402afSSimon Glass debug("%s: Missing RSA key info", __func__); 18319c402afSSimon Glass return -EFAULT; 18419c402afSSimon Glass } 18519c402afSSimon Glass 186fc2f4246SRuchika Gupta ret = rsa_verify_key(&prop, sig, sig_len, hash, info->algo->checksum); 18719c402afSSimon Glass 18819c402afSSimon Glass return ret; 18919c402afSSimon Glass } 19019c402afSSimon Glass 19119c402afSSimon Glass int rsa_verify(struct image_sign_info *info, 19219c402afSSimon Glass const struct image_region region[], int region_count, 19319c402afSSimon Glass uint8_t *sig, uint sig_len) 19419c402afSSimon Glass { 19519c402afSSimon Glass const void *blob = info->fdt_blob; 196646257d1SHeiko Schocher /* Reserve memory for maximum checksum-length */ 1975300a4f9SAndrew Duda uint8_t hash[info->algo->checksum->key_len]; 19819c402afSSimon Glass int ndepth, noffset; 19919c402afSSimon Glass int sig_node, node; 20019c402afSSimon Glass char name[100]; 201646257d1SHeiko Schocher int ret; 202646257d1SHeiko Schocher 203646257d1SHeiko Schocher /* 204646257d1SHeiko Schocher * Verify that the checksum-length does not exceed the 205646257d1SHeiko Schocher * rsa-signature-length 206646257d1SHeiko Schocher */ 207db1b5f3dSHeiko Schocher if (info->algo->checksum->checksum_len > 2085300a4f9SAndrew Duda info->algo->checksum->key_len) { 209db1b5f3dSHeiko Schocher debug("%s: invlaid checksum-algorithm %s for %s\n", 210db1b5f3dSHeiko Schocher __func__, info->algo->checksum->name, info->algo->name); 211646257d1SHeiko Schocher return -EINVAL; 212646257d1SHeiko Schocher } 21319c402afSSimon Glass 21419c402afSSimon Glass sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); 21519c402afSSimon Glass if (sig_node < 0) { 21619c402afSSimon Glass debug("%s: No signature node found\n", __func__); 21719c402afSSimon Glass return -ENOENT; 21819c402afSSimon Glass } 21919c402afSSimon Glass 220646257d1SHeiko Schocher /* Calculate checksum with checksum-algorithm */ 221b37b46f0SRuchika Gupta ret = info->algo->checksum->calculate(info->algo->checksum->name, 222b37b46f0SRuchika Gupta region, region_count, hash); 223b37b46f0SRuchika Gupta if (ret < 0) { 224b37b46f0SRuchika Gupta debug("%s: Error in checksum calculation\n", __func__); 225b37b46f0SRuchika Gupta return -EINVAL; 226b37b46f0SRuchika Gupta } 22719c402afSSimon Glass 22819c402afSSimon Glass /* See if we must use a particular key */ 22919c402afSSimon Glass if (info->required_keynode != -1) { 23019c402afSSimon Glass ret = rsa_verify_with_keynode(info, hash, sig, sig_len, 23119c402afSSimon Glass info->required_keynode); 23219c402afSSimon Glass if (!ret) 23319c402afSSimon Glass return ret; 23419c402afSSimon Glass } 23519c402afSSimon Glass 23619c402afSSimon Glass /* Look for a key that matches our hint */ 23719c402afSSimon Glass snprintf(name, sizeof(name), "key-%s", info->keyname); 23819c402afSSimon Glass node = fdt_subnode_offset(blob, sig_node, name); 23919c402afSSimon Glass ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node); 24019c402afSSimon Glass if (!ret) 24119c402afSSimon Glass return ret; 24219c402afSSimon Glass 24319c402afSSimon Glass /* No luck, so try each of the keys in turn */ 24419c402afSSimon Glass for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth); 24519c402afSSimon Glass (noffset >= 0) && (ndepth > 0); 24619c402afSSimon Glass noffset = fdt_next_node(info->fit, noffset, &ndepth)) { 24719c402afSSimon Glass if (ndepth == 1 && noffset != node) { 24819c402afSSimon Glass ret = rsa_verify_with_keynode(info, hash, sig, sig_len, 24919c402afSSimon Glass noffset); 25019c402afSSimon Glass if (!ret) 25119c402afSSimon Glass break; 25219c402afSSimon Glass } 25319c402afSSimon Glass } 25419c402afSSimon Glass 25519c402afSSimon Glass return ret; 25619c402afSSimon Glass } 257