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> 9008ec9b4SJoseph Chen #include <crypto.h> 1019c402afSSimon Glass #include <fdtdec.h> 1178263d89SJason Zhu #include <misc.h> 1229a23f9dSHeiko Schocher #include <asm/types.h> 1329a23f9dSHeiko Schocher #include <asm/byteorder.h> 141221ce45SMasahiro Yamada #include <linux/errno.h> 1529a23f9dSHeiko Schocher #include <asm/types.h> 1629a23f9dSHeiko Schocher #include <asm/unaligned.h> 17c937ff6dSRuchika Gupta #include <dm.h> 18f649b885SJason Zhu #include <asm/arch/rk_atags.h> 1929a23f9dSHeiko Schocher #else 2029a23f9dSHeiko Schocher #include "fdt_host.h" 2129a23f9dSHeiko Schocher #include "mkimage.h" 2229a23f9dSHeiko Schocher #include <fdt_support.h> 2329a23f9dSHeiko Schocher #endif 24fc2f4246SRuchika Gupta #include <u-boot/rsa-mod-exp.h> 252b9912e6SJeroen Hofstee #include <u-boot/rsa.h> 2629a23f9dSHeiko Schocher 27e0f2f155SMichael van der Westhuizen /* Default public exponent for backward compatibility */ 28e0f2f155SMichael van der Westhuizen #define RSA_DEFAULT_PUBEXP 65537 29e0f2f155SMichael van der Westhuizen 3019c402afSSimon Glass /** 31da29f299SAndrew Duda * rsa_verify_padding() - Verify RSA message padding is valid 32da29f299SAndrew Duda * 33da29f299SAndrew Duda * Verify a RSA message's padding is consistent with PKCS1.5 34da29f299SAndrew Duda * padding as described in the RSA PKCS#1 v2.1 standard. 35da29f299SAndrew Duda * 36da29f299SAndrew Duda * @msg: Padded message 37da29f299SAndrew Duda * @pad_len: Number of expected padding bytes 38da29f299SAndrew Duda * @algo: Checksum algo structure having information on DER encoding etc. 39da29f299SAndrew Duda * @return 0 on success, != 0 on failure 40da29f299SAndrew Duda */ 41da29f299SAndrew Duda static int rsa_verify_padding(const uint8_t *msg, const int pad_len, 42da29f299SAndrew Duda struct checksum_algo *algo) 43da29f299SAndrew Duda { 44da29f299SAndrew Duda int ff_len; 45da29f299SAndrew Duda int ret; 46da29f299SAndrew Duda 47da29f299SAndrew Duda /* first byte must be 0x00 */ 48da29f299SAndrew Duda ret = *msg++; 49da29f299SAndrew Duda /* second byte must be 0x01 */ 50da29f299SAndrew Duda ret |= *msg++ ^ 0x01; 51da29f299SAndrew Duda /* next ff_len bytes must be 0xff */ 52da29f299SAndrew Duda ff_len = pad_len - algo->der_len - 3; 53da29f299SAndrew Duda ret |= *msg ^ 0xff; 54da29f299SAndrew Duda ret |= memcmp(msg, msg+1, ff_len-1); 55da29f299SAndrew Duda msg += ff_len; 56da29f299SAndrew Duda /* next byte must be 0x00 */ 57da29f299SAndrew Duda ret |= *msg++; 58da29f299SAndrew Duda /* next der_len bytes must match der_prefix */ 59da29f299SAndrew Duda ret |= memcmp(msg, algo->der_prefix, algo->der_len); 60da29f299SAndrew Duda 61da29f299SAndrew Duda return ret; 62da29f299SAndrew Duda } 63da29f299SAndrew Duda 64008ec9b4SJoseph Chen #if !defined(USE_HOSTCC) 65008ec9b4SJoseph Chen #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 669c63859fSJason Zhu static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, 679c63859fSJason Zhu int total_len, int convert_len) 68008ec9b4SJoseph Chen { 699c63859fSJason Zhu int total_wd, convert_wd, i; 70008ec9b4SJoseph Chen 719c63859fSJason Zhu if (total_len < convert_len) 729c63859fSJason Zhu convert_len = total_len; 739c63859fSJason Zhu 749c63859fSJason Zhu total_wd = total_len / sizeof(uint32_t); 759c63859fSJason Zhu convert_wd = convert_len / sizeof(uint32_t); 769c63859fSJason Zhu for (i = 0; i < convert_wd; i++) 779c63859fSJason Zhu dst[i] = fdt32_to_cpu(src[total_wd - 1 - i]); 78008ec9b4SJoseph Chen } 79008ec9b4SJoseph Chen 800fb93272SJoseph Chen static int rsa_mod_exp_hw(struct key_prop *prop, const uint8_t *sig, 81008ec9b4SJoseph Chen const uint32_t sig_len, const uint32_t key_len, 82008ec9b4SJoseph Chen uint8_t *output) 83008ec9b4SJoseph Chen { 84008ec9b4SJoseph Chen struct udevice *dev; 85008ec9b4SJoseph Chen uint8_t sig_reverse[sig_len]; 86008ec9b4SJoseph Chen uint8_t buf[sig_len]; 87008ec9b4SJoseph Chen rsa_key rsa_key; 88008ec9b4SJoseph Chen int i, ret; 89c9e2e133SXuhui Lin #ifdef CONFIG_FIT_ENABLE_RSA4096_SUPPORT 90c9e2e133SXuhui Lin if (key_len != RSA4096_BYTES) 91c9e2e133SXuhui Lin return -EINVAL; 92008ec9b4SJoseph Chen 93c9e2e133SXuhui Lin rsa_key.algo = CRYPTO_RSA4096; 94c9e2e133SXuhui Lin #else 95008ec9b4SJoseph Chen if (key_len != RSA2048_BYTES) 96008ec9b4SJoseph Chen return -EINVAL; 97008ec9b4SJoseph Chen 98008ec9b4SJoseph Chen rsa_key.algo = CRYPTO_RSA2048; 99c9e2e133SXuhui Lin #endif 100008ec9b4SJoseph Chen rsa_key.n = malloc(key_len); 101008ec9b4SJoseph Chen rsa_key.e = malloc(key_len); 102008ec9b4SJoseph Chen rsa_key.c = malloc(key_len); 103008ec9b4SJoseph Chen if (!rsa_key.n || !rsa_key.e || !rsa_key.c) 104008ec9b4SJoseph Chen return -ENOMEM; 105008ec9b4SJoseph Chen 106008ec9b4SJoseph Chen rsa_convert_big_endian(rsa_key.n, (uint32_t *)prop->modulus, 1079c63859fSJason Zhu key_len, key_len); 108008ec9b4SJoseph Chen rsa_convert_big_endian(rsa_key.e, (uint32_t *)prop->public_exponent_BN, 1099c63859fSJason Zhu key_len, key_len); 110008ec9b4SJoseph Chen #ifdef CONFIG_ROCKCHIP_CRYPTO_V1 111008ec9b4SJoseph Chen rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_c, 1129c63859fSJason Zhu key_len, key_len); 113008ec9b4SJoseph Chen #else 114008ec9b4SJoseph Chen rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_np, 1159c63859fSJason Zhu key_len, key_len); 116008ec9b4SJoseph Chen #endif 117f649b885SJason Zhu #if defined(CONFIG_ROCKCHIP_PRELOADER_ATAGS) && defined(CONFIG_SPL_BUILD) 118f649b885SJason Zhu char *rsa_key_data = malloc(3 * key_len); 119f649b885SJason Zhu int flag = 0; 120f649b885SJason Zhu 121f649b885SJason Zhu if (rsa_key_data) { 122f649b885SJason Zhu memcpy(rsa_key_data, rsa_key.n, key_len); 123f649b885SJason Zhu memcpy(rsa_key_data + key_len, rsa_key.e, key_len); 124f649b885SJason Zhu memcpy(rsa_key_data + 2 * key_len, rsa_key.c, key_len); 125f649b885SJason Zhu if (fit_board_verify_required_sigs()) 126f649b885SJason Zhu flag = PUBKEY_FUSE_PROGRAMMED; 127f649b885SJason Zhu 128f649b885SJason Zhu if (atags_set_pub_key(rsa_key_data, 3 * key_len, flag)) 129f649b885SJason Zhu printf("Send public key through atags fail."); 130f649b885SJason Zhu } 131f649b885SJason Zhu #endif 132008ec9b4SJoseph Chen for (i = 0; i < sig_len; i++) 133008ec9b4SJoseph Chen sig_reverse[sig_len-1-i] = sig[i]; 134008ec9b4SJoseph Chen 135008ec9b4SJoseph Chen dev = crypto_get_device(rsa_key.algo); 136008ec9b4SJoseph Chen if (!dev) { 137008ec9b4SJoseph Chen printf("No crypto device for expected RSA\n"); 138008ec9b4SJoseph Chen return -ENODEV; 139008ec9b4SJoseph Chen } 140008ec9b4SJoseph Chen 141008ec9b4SJoseph Chen ret = crypto_rsa_verify(dev, &rsa_key, (u8 *)sig_reverse, buf); 142008ec9b4SJoseph Chen if (ret) 143008ec9b4SJoseph Chen goto out; 144008ec9b4SJoseph Chen 145008ec9b4SJoseph Chen for (i = 0; i < sig_len; i++) 146008ec9b4SJoseph Chen sig_reverse[sig_len-1-i] = buf[i]; 147008ec9b4SJoseph Chen 148008ec9b4SJoseph Chen memcpy(output, sig_reverse, sig_len); 149008ec9b4SJoseph Chen out: 150008ec9b4SJoseph Chen free(rsa_key.n); 151008ec9b4SJoseph Chen free(rsa_key.e); 152008ec9b4SJoseph Chen free(rsa_key.c); 153008ec9b4SJoseph Chen 154008ec9b4SJoseph Chen return ret; 155008ec9b4SJoseph Chen } 156008ec9b4SJoseph Chen #endif 157008ec9b4SJoseph Chen #endif 158008ec9b4SJoseph Chen 159219050bfSPhilippe Reynes int padding_pkcs_15_verify(struct image_sign_info *info, 160219050bfSPhilippe Reynes uint8_t *msg, int msg_len, 161219050bfSPhilippe Reynes const uint8_t *hash, int hash_len) 162219050bfSPhilippe Reynes { 163219050bfSPhilippe Reynes struct checksum_algo *checksum = info->checksum; 164219050bfSPhilippe Reynes int ret, pad_len = msg_len - checksum->checksum_len; 165219050bfSPhilippe Reynes 166219050bfSPhilippe Reynes /* Check pkcs1.5 padding bytes. */ 167219050bfSPhilippe Reynes ret = rsa_verify_padding(msg, pad_len, checksum); 168219050bfSPhilippe Reynes if (ret) { 169219050bfSPhilippe Reynes debug("In RSAVerify(): Padding check failed!\n"); 170219050bfSPhilippe Reynes return -EINVAL; 171219050bfSPhilippe Reynes } 172219050bfSPhilippe Reynes 173219050bfSPhilippe Reynes /* Check hash. */ 174219050bfSPhilippe Reynes if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) { 175219050bfSPhilippe Reynes debug("In RSAVerify(): Hash check failed!\n"); 176219050bfSPhilippe Reynes return -EACCES; 177219050bfSPhilippe Reynes } 178219050bfSPhilippe Reynes 179219050bfSPhilippe Reynes return 0; 180219050bfSPhilippe Reynes } 181219050bfSPhilippe Reynes 18285289e9dSPhilippe Reynes #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT 18385289e9dSPhilippe Reynes static void u32_i2osp(uint32_t val, uint8_t *buf) 18485289e9dSPhilippe Reynes { 18585289e9dSPhilippe Reynes buf[0] = (uint8_t)((val >> 24) & 0xff); 18685289e9dSPhilippe Reynes buf[1] = (uint8_t)((val >> 16) & 0xff); 18785289e9dSPhilippe Reynes buf[2] = (uint8_t)((val >> 8) & 0xff); 18885289e9dSPhilippe Reynes buf[3] = (uint8_t)((val >> 0) & 0xff); 18985289e9dSPhilippe Reynes } 19085289e9dSPhilippe Reynes 19185289e9dSPhilippe Reynes /** 19285289e9dSPhilippe Reynes * mask_generation_function1() - generate an octet string 19385289e9dSPhilippe Reynes * 19485289e9dSPhilippe Reynes * Generate an octet string used to check rsa signature. 19585289e9dSPhilippe Reynes * It use an input octet string and a hash function. 19685289e9dSPhilippe Reynes * 19785289e9dSPhilippe Reynes * @checksum: A Hash function 19885289e9dSPhilippe Reynes * @seed: Specifies an input variable octet string 19985289e9dSPhilippe Reynes * @seed_len: Size of the input octet string 20085289e9dSPhilippe Reynes * @output: Specifies the output octet string 20185289e9dSPhilippe Reynes * @output_len: Size of the output octet string 20285289e9dSPhilippe Reynes * @return 0 if the octet string was correctly generated, others on error 20385289e9dSPhilippe Reynes */ 20485289e9dSPhilippe Reynes static int mask_generation_function1(struct checksum_algo *checksum, 20585289e9dSPhilippe Reynes uint8_t *seed, int seed_len, 20685289e9dSPhilippe Reynes uint8_t *output, int output_len) 20785289e9dSPhilippe Reynes { 20885289e9dSPhilippe Reynes struct image_region region[2]; 20985289e9dSPhilippe Reynes int ret = 0, i, i_output = 0, region_count = 2; 21085289e9dSPhilippe Reynes uint32_t counter = 0; 21185289e9dSPhilippe Reynes uint8_t buf_counter[4], *tmp; 21285289e9dSPhilippe Reynes int hash_len = checksum->checksum_len; 21385289e9dSPhilippe Reynes 21485289e9dSPhilippe Reynes memset(output, 0, output_len); 21585289e9dSPhilippe Reynes 21685289e9dSPhilippe Reynes region[0].data = seed; 21785289e9dSPhilippe Reynes region[0].size = seed_len; 21885289e9dSPhilippe Reynes region[1].data = &buf_counter[0]; 21985289e9dSPhilippe Reynes region[1].size = 4; 22085289e9dSPhilippe Reynes 22185289e9dSPhilippe Reynes tmp = malloc(hash_len); 22285289e9dSPhilippe Reynes if (!tmp) { 22385289e9dSPhilippe Reynes debug("%s: can't allocate array tmp\n", __func__); 22485289e9dSPhilippe Reynes ret = -ENOMEM; 22585289e9dSPhilippe Reynes goto out; 22685289e9dSPhilippe Reynes } 22785289e9dSPhilippe Reynes 22885289e9dSPhilippe Reynes while (i_output < output_len) { 22985289e9dSPhilippe Reynes u32_i2osp(counter, &buf_counter[0]); 23085289e9dSPhilippe Reynes 23185289e9dSPhilippe Reynes ret = checksum->calculate(checksum->name, 23285289e9dSPhilippe Reynes region, region_count, 23385289e9dSPhilippe Reynes tmp); 23485289e9dSPhilippe Reynes if (ret < 0) { 23585289e9dSPhilippe Reynes debug("%s: Error in checksum calculation\n", __func__); 23685289e9dSPhilippe Reynes goto out; 23785289e9dSPhilippe Reynes } 23885289e9dSPhilippe Reynes 23985289e9dSPhilippe Reynes i = 0; 24085289e9dSPhilippe Reynes while ((i_output < output_len) && (i < hash_len)) { 24185289e9dSPhilippe Reynes output[i_output] = tmp[i]; 24285289e9dSPhilippe Reynes i_output++; 24385289e9dSPhilippe Reynes i++; 24485289e9dSPhilippe Reynes } 24585289e9dSPhilippe Reynes 24685289e9dSPhilippe Reynes counter++; 24785289e9dSPhilippe Reynes } 24885289e9dSPhilippe Reynes 24985289e9dSPhilippe Reynes out: 25085289e9dSPhilippe Reynes free(tmp); 25185289e9dSPhilippe Reynes 25285289e9dSPhilippe Reynes return ret; 25385289e9dSPhilippe Reynes } 25485289e9dSPhilippe Reynes 25585289e9dSPhilippe Reynes static int compute_hash_prime(struct checksum_algo *checksum, 25685289e9dSPhilippe Reynes uint8_t *pad, int pad_len, 25785289e9dSPhilippe Reynes uint8_t *hash, int hash_len, 25885289e9dSPhilippe Reynes uint8_t *salt, int salt_len, 25985289e9dSPhilippe Reynes uint8_t *hprime) 26085289e9dSPhilippe Reynes { 26185289e9dSPhilippe Reynes struct image_region region[3]; 26285289e9dSPhilippe Reynes int ret, region_count = 3; 26385289e9dSPhilippe Reynes 26485289e9dSPhilippe Reynes region[0].data = pad; 26585289e9dSPhilippe Reynes region[0].size = pad_len; 26685289e9dSPhilippe Reynes region[1].data = hash; 26785289e9dSPhilippe Reynes region[1].size = hash_len; 26885289e9dSPhilippe Reynes region[2].data = salt; 26985289e9dSPhilippe Reynes region[2].size = salt_len; 27085289e9dSPhilippe Reynes 27185289e9dSPhilippe Reynes ret = checksum->calculate(checksum->name, region, region_count, hprime); 27285289e9dSPhilippe Reynes if (ret < 0) { 27385289e9dSPhilippe Reynes debug("%s: Error in checksum calculation\n", __func__); 27485289e9dSPhilippe Reynes goto out; 27585289e9dSPhilippe Reynes } 27685289e9dSPhilippe Reynes 27785289e9dSPhilippe Reynes out: 27885289e9dSPhilippe Reynes return ret; 27985289e9dSPhilippe Reynes } 28085289e9dSPhilippe Reynes 28185289e9dSPhilippe Reynes int padding_pss_verify(struct image_sign_info *info, 28285289e9dSPhilippe Reynes uint8_t *msg, int msg_len, 28385289e9dSPhilippe Reynes const uint8_t *hash, int hash_len) 28485289e9dSPhilippe Reynes { 28585289e9dSPhilippe Reynes uint8_t *masked_db = NULL; 28685289e9dSPhilippe Reynes int masked_db_len = msg_len - hash_len - 1; 28785289e9dSPhilippe Reynes uint8_t *h = NULL, *hprime = NULL; 28885289e9dSPhilippe Reynes int h_len = hash_len; 28985289e9dSPhilippe Reynes uint8_t *db_mask = NULL; 29085289e9dSPhilippe Reynes int db_mask_len = masked_db_len; 29185289e9dSPhilippe Reynes uint8_t *db = NULL, *salt = NULL; 29285289e9dSPhilippe Reynes int db_len = masked_db_len, salt_len = msg_len - hash_len - 2; 29385289e9dSPhilippe Reynes uint8_t pad_zero[8] = { 0 }; 29485289e9dSPhilippe Reynes int ret, i, leftmost_bits = 1; 29585289e9dSPhilippe Reynes uint8_t leftmost_mask; 29685289e9dSPhilippe Reynes struct checksum_algo *checksum = info->checksum; 29785289e9dSPhilippe Reynes 29885289e9dSPhilippe Reynes /* first, allocate everything */ 29985289e9dSPhilippe Reynes masked_db = malloc(masked_db_len); 30085289e9dSPhilippe Reynes h = malloc(h_len); 30185289e9dSPhilippe Reynes db_mask = malloc(db_mask_len); 30285289e9dSPhilippe Reynes db = malloc(db_len); 30385289e9dSPhilippe Reynes salt = malloc(salt_len); 30485289e9dSPhilippe Reynes hprime = malloc(hash_len); 30585289e9dSPhilippe Reynes if (!masked_db || !h || !db_mask || !db || !salt || !hprime) { 30685289e9dSPhilippe Reynes printf("%s: can't allocate some buffer\n", __func__); 30785289e9dSPhilippe Reynes ret = -ENOMEM; 30885289e9dSPhilippe Reynes goto out; 30985289e9dSPhilippe Reynes } 31085289e9dSPhilippe Reynes 31185289e9dSPhilippe Reynes /* step 4: check if the last byte is 0xbc */ 31285289e9dSPhilippe Reynes if (msg[msg_len - 1] != 0xbc) { 31385289e9dSPhilippe Reynes printf("%s: invalid pss padding (0xbc is missing)\n", __func__); 31485289e9dSPhilippe Reynes ret = -EINVAL; 31585289e9dSPhilippe Reynes goto out; 31685289e9dSPhilippe Reynes } 31785289e9dSPhilippe Reynes 31885289e9dSPhilippe Reynes /* step 5 */ 31985289e9dSPhilippe Reynes memcpy(masked_db, msg, masked_db_len); 32085289e9dSPhilippe Reynes memcpy(h, msg + masked_db_len, h_len); 32185289e9dSPhilippe Reynes 32285289e9dSPhilippe Reynes /* step 6 */ 32385289e9dSPhilippe Reynes leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits); 32485289e9dSPhilippe Reynes if (masked_db[0] & leftmost_mask) { 32585289e9dSPhilippe Reynes printf("%s: invalid pss padding ", __func__); 32685289e9dSPhilippe Reynes printf("(leftmost bit of maskedDB not zero)\n"); 32785289e9dSPhilippe Reynes ret = -EINVAL; 32885289e9dSPhilippe Reynes goto out; 32985289e9dSPhilippe Reynes } 33085289e9dSPhilippe Reynes 33185289e9dSPhilippe Reynes /* step 7 */ 33285289e9dSPhilippe Reynes mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len); 33385289e9dSPhilippe Reynes 33485289e9dSPhilippe Reynes /* step 8 */ 33585289e9dSPhilippe Reynes for (i = 0; i < db_len; i++) 33685289e9dSPhilippe Reynes db[i] = masked_db[i] ^ db_mask[i]; 33785289e9dSPhilippe Reynes 33885289e9dSPhilippe Reynes /* step 9 */ 33985289e9dSPhilippe Reynes db[0] &= 0xff >> leftmost_bits; 34085289e9dSPhilippe Reynes 34185289e9dSPhilippe Reynes /* step 10 */ 34285289e9dSPhilippe Reynes if (db[0] != 0x01) { 34385289e9dSPhilippe Reynes printf("%s: invalid pss padding ", __func__); 34485289e9dSPhilippe Reynes printf("(leftmost byte of db isn't 0x01)\n"); 34585289e9dSPhilippe Reynes ret = EINVAL; 34685289e9dSPhilippe Reynes goto out; 34785289e9dSPhilippe Reynes } 34885289e9dSPhilippe Reynes 34985289e9dSPhilippe Reynes /* step 11 */ 35085289e9dSPhilippe Reynes memcpy(salt, &db[1], salt_len); 35185289e9dSPhilippe Reynes 35285289e9dSPhilippe Reynes /* step 12 & 13 */ 35385289e9dSPhilippe Reynes compute_hash_prime(checksum, pad_zero, 8, 35485289e9dSPhilippe Reynes (uint8_t *)hash, hash_len, 35585289e9dSPhilippe Reynes salt, salt_len, hprime); 35685289e9dSPhilippe Reynes 35785289e9dSPhilippe Reynes /* step 14 */ 35885289e9dSPhilippe Reynes ret = memcmp(h, hprime, hash_len); 35985289e9dSPhilippe Reynes 36085289e9dSPhilippe Reynes out: 36185289e9dSPhilippe Reynes free(hprime); 36285289e9dSPhilippe Reynes free(salt); 36385289e9dSPhilippe Reynes free(db); 36485289e9dSPhilippe Reynes free(db_mask); 36585289e9dSPhilippe Reynes free(h); 36685289e9dSPhilippe Reynes free(masked_db); 36785289e9dSPhilippe Reynes 36885289e9dSPhilippe Reynes return ret; 36985289e9dSPhilippe Reynes } 37085289e9dSPhilippe Reynes #endif 37185289e9dSPhilippe Reynes 372da29f299SAndrew Duda /** 373fc2f4246SRuchika Gupta * rsa_verify_key() - Verify a signature against some data using RSA Key 37419c402afSSimon Glass * 375fc2f4246SRuchika Gupta * Verify a RSA PKCS1.5 signature against an expected hash using 376fc2f4246SRuchika Gupta * the RSA Key properties in prop structure. 377fc2f4246SRuchika Gupta * 378219050bfSPhilippe Reynes * @info: Specifies key and FIT information 379fc2f4246SRuchika Gupta * @prop: Specifies key 380fc2f4246SRuchika Gupta * @sig: Signature 381fc2f4246SRuchika Gupta * @sig_len: Number of bytes in signature 382fc2f4246SRuchika Gupta * @hash: Pointer to the expected hash 3830c1d74fdSAndrew Duda * @key_len: Number of bytes in rsa key 384fc2f4246SRuchika Gupta * @return 0 if verified, -ve on error 38519c402afSSimon Glass */ 386219050bfSPhilippe Reynes static int rsa_verify_key(struct image_sign_info *info, 387219050bfSPhilippe Reynes struct key_prop *prop, const uint8_t *sig, 388646257d1SHeiko Schocher const uint32_t sig_len, const uint8_t *hash, 389219050bfSPhilippe Reynes const uint32_t key_len) 39019c402afSSimon Glass { 39119c402afSSimon Glass int ret; 392219050bfSPhilippe Reynes struct checksum_algo *checksum = info->checksum; 393219050bfSPhilippe Reynes struct padding_algo *padding = info->padding; 394219050bfSPhilippe Reynes int hash_len = checksum->checksum_len; 39519c402afSSimon Glass 396219050bfSPhilippe Reynes if (!prop || !sig || !hash || !checksum) 39719c402afSSimon Glass return -EIO; 39819c402afSSimon Glass 399fc2f4246SRuchika Gupta if (sig_len != (prop->num_bits / 8)) { 40019c402afSSimon Glass debug("Signature is of incorrect length %d\n", sig_len); 40119c402afSSimon Glass return -EINVAL; 40219c402afSSimon Glass } 40319c402afSSimon Glass 404219050bfSPhilippe Reynes debug("Checksum algorithm: %s", checksum->name); 405646257d1SHeiko Schocher 40619c402afSSimon Glass /* Sanity check for stack size */ 40719c402afSSimon Glass if (sig_len > RSA_MAX_SIG_BITS / 8) { 40819c402afSSimon Glass debug("Signature length %u exceeds maximum %d\n", sig_len, 40919c402afSSimon Glass RSA_MAX_SIG_BITS / 8); 41019c402afSSimon Glass return -EINVAL; 41119c402afSSimon Glass } 41219c402afSSimon Glass 413fc2f4246SRuchika Gupta uint8_t buf[sig_len]; 41419c402afSSimon Glass 415c937ff6dSRuchika Gupta #if !defined(USE_HOSTCC) 416008ec9b4SJoseph Chen #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 4170fb93272SJoseph Chen ret = rsa_mod_exp_hw(prop, sig, sig_len, key_len, buf); 418008ec9b4SJoseph Chen #else 419008ec9b4SJoseph Chen struct udevice *mod_exp_dev; 420008ec9b4SJoseph Chen 421c937ff6dSRuchika Gupta ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev); 422c937ff6dSRuchika Gupta if (ret) { 423c937ff6dSRuchika Gupta printf("RSA: Can't find Modular Exp implementation\n"); 424c937ff6dSRuchika Gupta return -EINVAL; 425c937ff6dSRuchika Gupta } 426c937ff6dSRuchika Gupta 427c937ff6dSRuchika Gupta ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf); 428008ec9b4SJoseph Chen #endif 429c937ff6dSRuchika Gupta #else 430fc2f4246SRuchika Gupta ret = rsa_mod_exp_sw(sig, sig_len, prop, buf); 431c937ff6dSRuchika Gupta #endif 432fc2f4246SRuchika Gupta if (ret) { 433fc2f4246SRuchika Gupta debug("Error in Modular exponentation\n"); 43419c402afSSimon Glass return ret; 435fc2f4246SRuchika Gupta } 43619c402afSSimon Glass 437219050bfSPhilippe Reynes ret = padding->verify(info, buf, key_len, hash, hash_len); 438da29f299SAndrew Duda if (ret) { 439219050bfSPhilippe Reynes debug("In RSAVerify(): padding check failed!\n"); 440219050bfSPhilippe Reynes return ret; 44119c402afSSimon Glass } 44219c402afSSimon Glass 44319c402afSSimon Glass return 0; 44419c402afSSimon Glass } 44519c402afSSimon Glass 44678263d89SJason Zhu static int rsa_get_key_prop(struct key_prop *prop, struct image_sign_info *info, int node) 44778263d89SJason Zhu { 44878263d89SJason Zhu const void *blob = info->fdt_blob; 44978263d89SJason Zhu int length; 45078263d89SJason Zhu int hash_node; 45178263d89SJason Zhu 45278263d89SJason Zhu if (node < 0) { 45378263d89SJason Zhu debug("%s: Skipping invalid node", __func__); 45478263d89SJason Zhu return -EBADF; 45578263d89SJason Zhu } 45678263d89SJason Zhu 45778263d89SJason Zhu if (!prop) { 45878263d89SJason Zhu debug("%s: The prop is NULL", __func__); 45978263d89SJason Zhu return -EBADF; 46078263d89SJason Zhu } 46178263d89SJason Zhu 46278263d89SJason Zhu prop->burn_key = fdtdec_get_int(blob, node, "burn-key-hash", 0); 46378263d89SJason Zhu 46478263d89SJason Zhu prop->num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0); 46578263d89SJason Zhu 46678263d89SJason Zhu prop->n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0); 46778263d89SJason Zhu 46878263d89SJason Zhu prop->public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length); 46978263d89SJason Zhu if (!prop->public_exponent || length < sizeof(uint64_t)) 47078263d89SJason Zhu prop->public_exponent = NULL; 47178263d89SJason Zhu 47278263d89SJason Zhu prop->exp_len = sizeof(uint64_t); 47378263d89SJason Zhu prop->modulus = fdt_getprop(blob, node, "rsa,modulus", NULL); 47478263d89SJason Zhu prop->public_exponent_BN = fdt_getprop(blob, node, "rsa,exponent-BN", NULL); 47578263d89SJason Zhu prop->rr = fdt_getprop(blob, node, "rsa,r-squared", NULL); 47678263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1 47778263d89SJason Zhu hash_node = fdt_subnode_offset(blob, node, "hash@c"); 47878263d89SJason Zhu #else 47978263d89SJason Zhu hash_node = fdt_subnode_offset(blob, node, "hash@np"); 48078263d89SJason Zhu #endif 48178263d89SJason Zhu if (hash_node >= 0) 48278263d89SJason Zhu prop->hash = fdt_getprop(blob, hash_node, "value", NULL); 48378263d89SJason Zhu 48478263d89SJason Zhu if (!prop->num_bits || !prop->modulus) { 48578263d89SJason Zhu debug("%s: Missing RSA key info", __func__); 48678263d89SJason Zhu return -EFAULT; 48778263d89SJason Zhu } 48878263d89SJason Zhu 48978263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1 49078263d89SJason Zhu prop->factor_c = fdt_getprop(blob, node, "rsa,c", NULL); 49178263d89SJason Zhu if (!prop.factor_c) 49278263d89SJason Zhu return -EFAULT; 49378263d89SJason Zhu #else 49478263d89SJason Zhu prop->factor_np = fdt_getprop(blob, node, "rsa,np", NULL); 49578263d89SJason Zhu if (!prop->factor_np) 49678263d89SJason Zhu return -EFAULT; 49778263d89SJason Zhu #endif 49878263d89SJason Zhu 49978263d89SJason Zhu return 0; 50078263d89SJason Zhu } 50178263d89SJason Zhu 502fc2f4246SRuchika Gupta /** 503fc2f4246SRuchika Gupta * rsa_verify_with_keynode() - Verify a signature against some data using 504fc2f4246SRuchika Gupta * information in node with prperties of RSA Key like modulus, exponent etc. 505fc2f4246SRuchika Gupta * 506fc2f4246SRuchika Gupta * Parse sign-node and fill a key_prop structure with properties of the 507fc2f4246SRuchika Gupta * key. Verify a RSA PKCS1.5 signature against an expected hash using 508fc2f4246SRuchika Gupta * the properties parsed 509fc2f4246SRuchika Gupta * 510fc2f4246SRuchika Gupta * @info: Specifies key and FIT information 511fc2f4246SRuchika Gupta * @hash: Pointer to the expected hash 512fc2f4246SRuchika Gupta * @sig: Signature 513fc2f4246SRuchika Gupta * @sig_len: Number of bytes in signature 514fc2f4246SRuchika Gupta * @node: Node having the RSA Key properties 515fc2f4246SRuchika Gupta * @return 0 if verified, -ve on error 516fc2f4246SRuchika Gupta */ 51719c402afSSimon Glass static int rsa_verify_with_keynode(struct image_sign_info *info, 518fc2f4246SRuchika Gupta const void *hash, uint8_t *sig, 519fc2f4246SRuchika Gupta uint sig_len, int node) 52019c402afSSimon Glass { 521fc2f4246SRuchika Gupta struct key_prop prop; 52219c402afSSimon Glass 52378263d89SJason Zhu if (rsa_get_key_prop(&prop, info, node)) 52419c402afSSimon Glass return -EFAULT; 52519c402afSSimon Glass 52678263d89SJason Zhu return rsa_verify_key(info, &prop, sig, sig_len, hash, 527219050bfSPhilippe Reynes info->crypto->key_len); 52819c402afSSimon Glass } 52919c402afSSimon Glass 53019c402afSSimon Glass int rsa_verify(struct image_sign_info *info, 53119c402afSSimon Glass const struct image_region region[], int region_count, 53219c402afSSimon Glass uint8_t *sig, uint sig_len) 53319c402afSSimon Glass { 53419c402afSSimon Glass const void *blob = info->fdt_blob; 535646257d1SHeiko Schocher /* Reserve memory for maximum checksum-length */ 53683dd98e0SAndrew Duda uint8_t hash[info->crypto->key_len]; 53719c402afSSimon Glass int ndepth, noffset; 53819c402afSSimon Glass int sig_node, node; 53919c402afSSimon Glass char name[100]; 540646257d1SHeiko Schocher int ret; 541646257d1SHeiko Schocher 542646257d1SHeiko Schocher /* 543646257d1SHeiko Schocher * Verify that the checksum-length does not exceed the 544646257d1SHeiko Schocher * rsa-signature-length 545646257d1SHeiko Schocher */ 54683dd98e0SAndrew Duda if (info->checksum->checksum_len > 54783dd98e0SAndrew Duda info->crypto->key_len) { 548db1b5f3dSHeiko Schocher debug("%s: invlaid checksum-algorithm %s for %s\n", 54983dd98e0SAndrew Duda __func__, info->checksum->name, info->crypto->name); 550646257d1SHeiko Schocher return -EINVAL; 551646257d1SHeiko Schocher } 55219c402afSSimon Glass 55319c402afSSimon Glass sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); 55419c402afSSimon Glass if (sig_node < 0) { 55519c402afSSimon Glass debug("%s: No signature node found\n", __func__); 55619c402afSSimon Glass return -ENOENT; 55719c402afSSimon Glass } 55819c402afSSimon Glass 559646257d1SHeiko Schocher /* Calculate checksum with checksum-algorithm */ 56083dd98e0SAndrew Duda ret = info->checksum->calculate(info->checksum->name, 561b37b46f0SRuchika Gupta region, region_count, hash); 562b37b46f0SRuchika Gupta if (ret < 0) { 563b37b46f0SRuchika Gupta debug("%s: Error in checksum calculation\n", __func__); 564b37b46f0SRuchika Gupta return -EINVAL; 565b37b46f0SRuchika Gupta } 56619c402afSSimon Glass 56719c402afSSimon Glass /* See if we must use a particular key */ 56819c402afSSimon Glass if (info->required_keynode != -1) { 56919c402afSSimon Glass ret = rsa_verify_with_keynode(info, hash, sig, sig_len, 57019c402afSSimon Glass info->required_keynode); 57119c402afSSimon Glass if (!ret) 57219c402afSSimon Glass return ret; 57319c402afSSimon Glass } 57419c402afSSimon Glass 57519c402afSSimon Glass /* Look for a key that matches our hint */ 57619c402afSSimon Glass snprintf(name, sizeof(name), "key-%s", info->keyname); 57719c402afSSimon Glass node = fdt_subnode_offset(blob, sig_node, name); 57819c402afSSimon Glass ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node); 57919c402afSSimon Glass if (!ret) 58019c402afSSimon Glass return ret; 58119c402afSSimon Glass 58219c402afSSimon Glass /* No luck, so try each of the keys in turn */ 58319c402afSSimon Glass for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth); 58419c402afSSimon Glass (noffset >= 0) && (ndepth > 0); 58519c402afSSimon Glass noffset = fdt_next_node(info->fit, noffset, &ndepth)) { 58619c402afSSimon Glass if (ndepth == 1 && noffset != node) { 58719c402afSSimon Glass ret = rsa_verify_with_keynode(info, hash, sig, sig_len, 58819c402afSSimon Glass noffset); 58919c402afSSimon Glass if (!ret) 59019c402afSSimon Glass break; 59119c402afSSimon Glass } 59219c402afSSimon Glass } 59319c402afSSimon Glass 59419c402afSSimon Glass return ret; 59519c402afSSimon Glass } 59678263d89SJason Zhu 59778263d89SJason Zhu #if !defined(USE_HOSTCC) 598c2b76562SJoseph Chen #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_FIT_HW_CRYPTO) && \ 599c2b76562SJoseph Chen defined(CONFIG_SPL_ROCKCHIP_SECURE_OTP) 60078263d89SJason Zhu int rsa_burn_key_hash(struct image_sign_info *info) 60178263d89SJason Zhu { 60278263d89SJason Zhu char *rsa_key; 60378263d89SJason Zhu void *n, *e, *c; 60478263d89SJason Zhu uint32_t key_len; 60578263d89SJason Zhu struct udevice *dev; 60678263d89SJason Zhu struct key_prop prop; 60778263d89SJason Zhu char name[100] = {0}; 608a1300100SXuhui Lin u16 secure_flags = 0; 60978263d89SJason Zhu const void *blob = info->fdt_blob; 61078263d89SJason Zhu uint8_t digest[FIT_MAX_HASH_LEN]; 61178263d89SJason Zhu uint8_t digest_read[FIT_MAX_HASH_LEN]; 612*b77c257eSXuhui Lin int sig_node, node, digest_len, i; 613*b77c257eSXuhui Lin int ret = 0, written_size = 0; 61478263d89SJason Zhu 61578263d89SJason Zhu dev = misc_otp_get_device(OTP_S); 61678263d89SJason Zhu if (!dev) 61778263d89SJason Zhu return -ENODEV; 61878263d89SJason Zhu 61978263d89SJason Zhu ret = misc_otp_read(dev, OTP_SECURE_BOOT_ENABLE_ADDR, 620a1300100SXuhui Lin &secure_flags, OTP_SECURE_BOOT_ENABLE_SIZE); 62178263d89SJason Zhu if (ret) 62278263d89SJason Zhu return ret; 62378263d89SJason Zhu 624a1300100SXuhui Lin if (secure_flags == 0xff) 62578263d89SJason Zhu return 0; 62678263d89SJason Zhu 62778263d89SJason Zhu sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); 62878263d89SJason Zhu if (sig_node < 0) { 62978263d89SJason Zhu debug("%s: No signature node found\n", __func__); 63078263d89SJason Zhu return -ENOENT; 63178263d89SJason Zhu } 63278263d89SJason Zhu 63378263d89SJason Zhu snprintf(name, sizeof(name), "key-%s", info->keyname); 63478263d89SJason Zhu node = fdt_subnode_offset(blob, sig_node, name); 63578263d89SJason Zhu 63678263d89SJason Zhu if (rsa_get_key_prop(&prop, info, node)) 63778263d89SJason Zhu return -1; 63878263d89SJason Zhu 63978263d89SJason Zhu if (!(prop.burn_key)) 64078263d89SJason Zhu return -EPERM; 64178263d89SJason Zhu 64278263d89SJason Zhu if (!prop.hash || !prop.modulus || !prop.public_exponent_BN) 64378263d89SJason Zhu return -ENOENT; 64478263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1 64578263d89SJason Zhu if (!prop.factor_c) 64678263d89SJason Zhu return -ENOENT; 64778263d89SJason Zhu #else 64878263d89SJason Zhu if (!prop.factor_np) 64978263d89SJason Zhu return -ENOENT; 65078263d89SJason Zhu #endif 65178263d89SJason Zhu key_len = info->crypto->key_len; 65278263d89SJason Zhu if (info->crypto->key_len != RSA2048_BYTES) 65378263d89SJason Zhu return -EINVAL; 65478263d89SJason Zhu 6559c63859fSJason Zhu rsa_key = calloc(key_len * 3, sizeof(char)); 65678263d89SJason Zhu if (!rsa_key) 65778263d89SJason Zhu return -ENOMEM; 65878263d89SJason Zhu 65978263d89SJason Zhu n = rsa_key; 6609c63859fSJason Zhu e = rsa_key + CONFIG_RSA_N_SIZE; 6619c63859fSJason Zhu c = rsa_key + CONFIG_RSA_N_SIZE + CONFIG_RSA_E_SIZE; 66278263d89SJason Zhu rsa_convert_big_endian(n, (uint32_t *)prop.modulus, 6639c63859fSJason Zhu key_len, CONFIG_RSA_N_SIZE); 66478263d89SJason Zhu rsa_convert_big_endian(e, (uint32_t *)prop.public_exponent_BN, 6659c63859fSJason Zhu key_len, CONFIG_RSA_E_SIZE); 66678263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1 66778263d89SJason Zhu rsa_convert_big_endian(c, (uint32_t *)prop.factor_c, 6689c63859fSJason Zhu key_len, CONFIG_RSA_C_SIZE); 66978263d89SJason Zhu #else 67078263d89SJason Zhu rsa_convert_big_endian(c, (uint32_t *)prop.factor_np, 6719c63859fSJason Zhu key_len, CONFIG_RSA_C_SIZE); 67278263d89SJason Zhu #endif 67378263d89SJason Zhu 6749c63859fSJason Zhu ret = calculate_hash(rsa_key, CONFIG_RSA_N_SIZE + CONFIG_RSA_E_SIZE + CONFIG_RSA_C_SIZE, 67578263d89SJason Zhu info->checksum->name, digest, &digest_len); 67678263d89SJason Zhu if (ret) 67778263d89SJason Zhu goto error; 67878263d89SJason Zhu 67978263d89SJason Zhu if (memcmp(digest, prop.hash, digest_len) != 0) { 680bd2c27ccSJason Zhu printf("RSA: Compare public key hash fail.\n"); 68178263d89SJason Zhu goto error; 68278263d89SJason Zhu } 68378263d89SJason Zhu 68478263d89SJason Zhu /* burn key hash here */ 68578263d89SJason Zhu ret = misc_otp_read(dev, OTP_RSA_HASH_ADDR, digest_read, OTP_RSA_HASH_SIZE); 68678263d89SJason Zhu if (ret) 68778263d89SJason Zhu goto error; 68878263d89SJason Zhu 68978263d89SJason Zhu for (i = 0; i < OTP_RSA_HASH_SIZE; i++) { 690*b77c257eSXuhui Lin if (digest_read[i] == digest[i]) { 691*b77c257eSXuhui Lin written_size++; 692*b77c257eSXuhui Lin } else if (digest_read[i] == 0) { 693*b77c257eSXuhui Lin break; 694*b77c257eSXuhui Lin } else { 69578263d89SJason Zhu printf("RSA: The secure region has been written.\n"); 69678263d89SJason Zhu ret = -EIO; 69778263d89SJason Zhu goto error; 69878263d89SJason Zhu } 69978263d89SJason Zhu } 70078263d89SJason Zhu 701*b77c257eSXuhui Lin if (OTP_RSA_HASH_SIZE - written_size) { 702*b77c257eSXuhui Lin ret = misc_otp_write(dev, OTP_RSA_HASH_ADDR + written_size, digest + written_size, 703*b77c257eSXuhui Lin OTP_RSA_HASH_SIZE - written_size); 704*b77c257eSXuhui Lin if (ret) 705*b77c257eSXuhui Lin goto error; 706*b77c257eSXuhui Lin } 707*b77c257eSXuhui Lin 70878263d89SJason Zhu if (ret) 70978263d89SJason Zhu goto error; 71078263d89SJason Zhu 711bd2c27ccSJason Zhu memset(digest_read, 0, FIT_MAX_HASH_LEN); 712bd2c27ccSJason Zhu ret = misc_otp_read(dev, OTP_RSA_HASH_ADDR, digest_read, OTP_RSA_HASH_SIZE); 713bd2c27ccSJason Zhu if (ret) 714bd2c27ccSJason Zhu goto error; 715bd2c27ccSJason Zhu 716bd2c27ccSJason Zhu if (memcmp(digest, digest_read, digest_len) != 0) { 717*b77c257eSXuhui Lin ret = -EAGAIN; 718bd2c27ccSJason Zhu printf("RSA: Write public key hash fail.\n"); 719bd2c27ccSJason Zhu goto error; 720bd2c27ccSJason Zhu } 721bd2c27ccSJason Zhu 722a1300100SXuhui Lin secure_flags = 0xff; 72378263d89SJason Zhu ret = misc_otp_write(dev, OTP_SECURE_BOOT_ENABLE_ADDR, 724a1300100SXuhui Lin &secure_flags, OTP_SECURE_BOOT_ENABLE_SIZE); 72578263d89SJason Zhu if (ret) 72678263d89SJason Zhu goto error; 72778263d89SJason Zhu 728*b77c257eSXuhui Lin if (written_size) 729*b77c257eSXuhui Lin printf("RSA: Repair RSA key hash successfully.\n"); 730*b77c257eSXuhui Lin else 731*b77c257eSXuhui Lin printf("RSA: Write RSA key hash successfully.\n"); 73278263d89SJason Zhu 73378263d89SJason Zhu error: 73478263d89SJason Zhu free(rsa_key); 73578263d89SJason Zhu 73678263d89SJason Zhu return ret; 73778263d89SJason Zhu } 73878263d89SJason Zhu #endif 73978263d89SJason Zhu #endif 740