xref: /rk3399_rockchip-uboot/lib/rsa/rsa-verify.c (revision bd2c27cc923711a23a21fc9ab43e05d8ac7fc142)
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>
1829a23f9dSHeiko Schocher #else
1929a23f9dSHeiko Schocher #include "fdt_host.h"
2029a23f9dSHeiko Schocher #include "mkimage.h"
2129a23f9dSHeiko Schocher #include <fdt_support.h>
2229a23f9dSHeiko Schocher #endif
23fc2f4246SRuchika Gupta #include <u-boot/rsa-mod-exp.h>
242b9912e6SJeroen Hofstee #include <u-boot/rsa.h>
2529a23f9dSHeiko Schocher 
26e0f2f155SMichael van der Westhuizen /* Default public exponent for backward compatibility */
27e0f2f155SMichael van der Westhuizen #define RSA_DEFAULT_PUBEXP	65537
28e0f2f155SMichael van der Westhuizen 
2919c402afSSimon Glass /**
30da29f299SAndrew Duda  * rsa_verify_padding() - Verify RSA message padding is valid
31da29f299SAndrew Duda  *
32da29f299SAndrew Duda  * Verify a RSA message's padding is consistent with PKCS1.5
33da29f299SAndrew Duda  * padding as described in the RSA PKCS#1 v2.1 standard.
34da29f299SAndrew Duda  *
35da29f299SAndrew Duda  * @msg:	Padded message
36da29f299SAndrew Duda  * @pad_len:	Number of expected padding bytes
37da29f299SAndrew Duda  * @algo:	Checksum algo structure having information on DER encoding etc.
38da29f299SAndrew Duda  * @return 0 on success, != 0 on failure
39da29f299SAndrew Duda  */
40da29f299SAndrew Duda static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
41da29f299SAndrew Duda 			      struct checksum_algo *algo)
42da29f299SAndrew Duda {
43da29f299SAndrew Duda 	int ff_len;
44da29f299SAndrew Duda 	int ret;
45da29f299SAndrew Duda 
46da29f299SAndrew Duda 	/* first byte must be 0x00 */
47da29f299SAndrew Duda 	ret = *msg++;
48da29f299SAndrew Duda 	/* second byte must be 0x01 */
49da29f299SAndrew Duda 	ret |= *msg++ ^ 0x01;
50da29f299SAndrew Duda 	/* next ff_len bytes must be 0xff */
51da29f299SAndrew Duda 	ff_len = pad_len - algo->der_len - 3;
52da29f299SAndrew Duda 	ret |= *msg ^ 0xff;
53da29f299SAndrew Duda 	ret |= memcmp(msg, msg+1, ff_len-1);
54da29f299SAndrew Duda 	msg += ff_len;
55da29f299SAndrew Duda 	/* next byte must be 0x00 */
56da29f299SAndrew Duda 	ret |= *msg++;
57da29f299SAndrew Duda 	/* next der_len bytes must match der_prefix */
58da29f299SAndrew Duda 	ret |= memcmp(msg, algo->der_prefix, algo->der_len);
59da29f299SAndrew Duda 
60da29f299SAndrew Duda 	return ret;
61da29f299SAndrew Duda }
62da29f299SAndrew Duda 
63008ec9b4SJoseph Chen #if !defined(USE_HOSTCC)
64008ec9b4SJoseph Chen #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO)
65008ec9b4SJoseph Chen static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src, int len)
66008ec9b4SJoseph Chen {
67008ec9b4SJoseph Chen 	int i;
68008ec9b4SJoseph Chen 
69008ec9b4SJoseph Chen 	for (i = 0; i < len; i++)
70008ec9b4SJoseph Chen 		dst[i] = fdt32_to_cpu(src[len - 1 - i]);
71008ec9b4SJoseph Chen }
72008ec9b4SJoseph Chen 
730fb93272SJoseph Chen static int rsa_mod_exp_hw(struct key_prop *prop, const uint8_t *sig,
74008ec9b4SJoseph Chen 			  const uint32_t sig_len, const uint32_t key_len,
75008ec9b4SJoseph Chen 			  uint8_t *output)
76008ec9b4SJoseph Chen {
77008ec9b4SJoseph Chen 	struct udevice *dev;
78008ec9b4SJoseph Chen 	uint8_t sig_reverse[sig_len];
79008ec9b4SJoseph Chen 	uint8_t buf[sig_len];
80008ec9b4SJoseph Chen 	rsa_key rsa_key;
81008ec9b4SJoseph Chen 	int i, ret;
82008ec9b4SJoseph Chen 
83008ec9b4SJoseph Chen 	if (key_len != RSA2048_BYTES)
84008ec9b4SJoseph Chen 		return -EINVAL;
85008ec9b4SJoseph Chen 
86008ec9b4SJoseph Chen 	rsa_key.algo = CRYPTO_RSA2048;
87008ec9b4SJoseph Chen 	rsa_key.n = malloc(key_len);
88008ec9b4SJoseph Chen 	rsa_key.e = malloc(key_len);
89008ec9b4SJoseph Chen 	rsa_key.c = malloc(key_len);
90008ec9b4SJoseph Chen 	if (!rsa_key.n || !rsa_key.e || !rsa_key.c)
91008ec9b4SJoseph Chen 		return -ENOMEM;
92008ec9b4SJoseph Chen 
93008ec9b4SJoseph Chen 	rsa_convert_big_endian(rsa_key.n, (uint32_t *)prop->modulus,
94008ec9b4SJoseph Chen 			       key_len / sizeof(uint32_t));
95008ec9b4SJoseph Chen 	rsa_convert_big_endian(rsa_key.e, (uint32_t *)prop->public_exponent_BN,
96008ec9b4SJoseph Chen 			       key_len / sizeof(uint32_t));
97008ec9b4SJoseph Chen #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
98008ec9b4SJoseph Chen 	rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_c,
99008ec9b4SJoseph Chen 			       key_len / sizeof(uint32_t));
100008ec9b4SJoseph Chen #else
101008ec9b4SJoseph Chen 	rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_np,
102008ec9b4SJoseph Chen 			       key_len / sizeof(uint32_t));
103008ec9b4SJoseph Chen #endif
104008ec9b4SJoseph Chen 	for (i = 0; i < sig_len; i++)
105008ec9b4SJoseph Chen 		sig_reverse[sig_len-1-i] = sig[i];
106008ec9b4SJoseph Chen 
107008ec9b4SJoseph Chen 	dev = crypto_get_device(rsa_key.algo);
108008ec9b4SJoseph Chen 	if (!dev) {
109008ec9b4SJoseph Chen 		printf("No crypto device for expected RSA\n");
110008ec9b4SJoseph Chen 		return -ENODEV;
111008ec9b4SJoseph Chen 	}
112008ec9b4SJoseph Chen 
113008ec9b4SJoseph Chen 	ret = crypto_rsa_verify(dev, &rsa_key, (u8 *)sig_reverse, buf);
114008ec9b4SJoseph Chen 	if (ret)
115008ec9b4SJoseph Chen 		goto out;
116008ec9b4SJoseph Chen 
117008ec9b4SJoseph Chen 	for (i = 0; i < sig_len; i++)
118008ec9b4SJoseph Chen 		sig_reverse[sig_len-1-i] = buf[i];
119008ec9b4SJoseph Chen 
120008ec9b4SJoseph Chen 	memcpy(output, sig_reverse, sig_len);
121008ec9b4SJoseph Chen out:
122008ec9b4SJoseph Chen 	free(rsa_key.n);
123008ec9b4SJoseph Chen 	free(rsa_key.e);
124008ec9b4SJoseph Chen 	free(rsa_key.c);
125008ec9b4SJoseph Chen 
126008ec9b4SJoseph Chen 	return ret;
127008ec9b4SJoseph Chen }
128008ec9b4SJoseph Chen #endif
129008ec9b4SJoseph Chen #endif
130008ec9b4SJoseph Chen 
131219050bfSPhilippe Reynes int padding_pkcs_15_verify(struct image_sign_info *info,
132219050bfSPhilippe Reynes 			   uint8_t *msg, int msg_len,
133219050bfSPhilippe Reynes 			   const uint8_t *hash, int hash_len)
134219050bfSPhilippe Reynes {
135219050bfSPhilippe Reynes 	struct checksum_algo *checksum = info->checksum;
136219050bfSPhilippe Reynes 	int ret, pad_len = msg_len - checksum->checksum_len;
137219050bfSPhilippe Reynes 
138219050bfSPhilippe Reynes 	/* Check pkcs1.5 padding bytes. */
139219050bfSPhilippe Reynes 	ret = rsa_verify_padding(msg, pad_len, checksum);
140219050bfSPhilippe Reynes 	if (ret) {
141219050bfSPhilippe Reynes 		debug("In RSAVerify(): Padding check failed!\n");
142219050bfSPhilippe Reynes 		return -EINVAL;
143219050bfSPhilippe Reynes 	}
144219050bfSPhilippe Reynes 
145219050bfSPhilippe Reynes 	/* Check hash. */
146219050bfSPhilippe Reynes 	if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) {
147219050bfSPhilippe Reynes 		debug("In RSAVerify(): Hash check failed!\n");
148219050bfSPhilippe Reynes 		return -EACCES;
149219050bfSPhilippe Reynes 	}
150219050bfSPhilippe Reynes 
151219050bfSPhilippe Reynes 	return 0;
152219050bfSPhilippe Reynes }
153219050bfSPhilippe Reynes 
15485289e9dSPhilippe Reynes #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
15585289e9dSPhilippe Reynes static void u32_i2osp(uint32_t val, uint8_t *buf)
15685289e9dSPhilippe Reynes {
15785289e9dSPhilippe Reynes 	buf[0] = (uint8_t)((val >> 24) & 0xff);
15885289e9dSPhilippe Reynes 	buf[1] = (uint8_t)((val >> 16) & 0xff);
15985289e9dSPhilippe Reynes 	buf[2] = (uint8_t)((val >>  8) & 0xff);
16085289e9dSPhilippe Reynes 	buf[3] = (uint8_t)((val >>  0) & 0xff);
16185289e9dSPhilippe Reynes }
16285289e9dSPhilippe Reynes 
16385289e9dSPhilippe Reynes /**
16485289e9dSPhilippe Reynes  * mask_generation_function1() - generate an octet string
16585289e9dSPhilippe Reynes  *
16685289e9dSPhilippe Reynes  * Generate an octet string used to check rsa signature.
16785289e9dSPhilippe Reynes  * It use an input octet string and a hash function.
16885289e9dSPhilippe Reynes  *
16985289e9dSPhilippe Reynes  * @checksum:	A Hash function
17085289e9dSPhilippe Reynes  * @seed:	Specifies an input variable octet string
17185289e9dSPhilippe Reynes  * @seed_len:	Size of the input octet string
17285289e9dSPhilippe Reynes  * @output:	Specifies the output octet string
17385289e9dSPhilippe Reynes  * @output_len:	Size of the output octet string
17485289e9dSPhilippe Reynes  * @return 0 if the octet string was correctly generated, others on error
17585289e9dSPhilippe Reynes  */
17685289e9dSPhilippe Reynes static int mask_generation_function1(struct checksum_algo *checksum,
17785289e9dSPhilippe Reynes 				     uint8_t *seed, int seed_len,
17885289e9dSPhilippe Reynes 				     uint8_t *output, int output_len)
17985289e9dSPhilippe Reynes {
18085289e9dSPhilippe Reynes 	struct image_region region[2];
18185289e9dSPhilippe Reynes 	int ret = 0, i, i_output = 0, region_count = 2;
18285289e9dSPhilippe Reynes 	uint32_t counter = 0;
18385289e9dSPhilippe Reynes 	uint8_t buf_counter[4], *tmp;
18485289e9dSPhilippe Reynes 	int hash_len = checksum->checksum_len;
18585289e9dSPhilippe Reynes 
18685289e9dSPhilippe Reynes 	memset(output, 0, output_len);
18785289e9dSPhilippe Reynes 
18885289e9dSPhilippe Reynes 	region[0].data = seed;
18985289e9dSPhilippe Reynes 	region[0].size = seed_len;
19085289e9dSPhilippe Reynes 	region[1].data = &buf_counter[0];
19185289e9dSPhilippe Reynes 	region[1].size = 4;
19285289e9dSPhilippe Reynes 
19385289e9dSPhilippe Reynes 	tmp = malloc(hash_len);
19485289e9dSPhilippe Reynes 	if (!tmp) {
19585289e9dSPhilippe Reynes 		debug("%s: can't allocate array tmp\n", __func__);
19685289e9dSPhilippe Reynes 		ret = -ENOMEM;
19785289e9dSPhilippe Reynes 		goto out;
19885289e9dSPhilippe Reynes 	}
19985289e9dSPhilippe Reynes 
20085289e9dSPhilippe Reynes 	while (i_output < output_len) {
20185289e9dSPhilippe Reynes 		u32_i2osp(counter, &buf_counter[0]);
20285289e9dSPhilippe Reynes 
20385289e9dSPhilippe Reynes 		ret = checksum->calculate(checksum->name,
20485289e9dSPhilippe Reynes 					  region, region_count,
20585289e9dSPhilippe Reynes 					  tmp);
20685289e9dSPhilippe Reynes 		if (ret < 0) {
20785289e9dSPhilippe Reynes 			debug("%s: Error in checksum calculation\n", __func__);
20885289e9dSPhilippe Reynes 			goto out;
20985289e9dSPhilippe Reynes 		}
21085289e9dSPhilippe Reynes 
21185289e9dSPhilippe Reynes 		i = 0;
21285289e9dSPhilippe Reynes 		while ((i_output < output_len) && (i < hash_len)) {
21385289e9dSPhilippe Reynes 			output[i_output] = tmp[i];
21485289e9dSPhilippe Reynes 			i_output++;
21585289e9dSPhilippe Reynes 			i++;
21685289e9dSPhilippe Reynes 		}
21785289e9dSPhilippe Reynes 
21885289e9dSPhilippe Reynes 		counter++;
21985289e9dSPhilippe Reynes 	}
22085289e9dSPhilippe Reynes 
22185289e9dSPhilippe Reynes out:
22285289e9dSPhilippe Reynes 	free(tmp);
22385289e9dSPhilippe Reynes 
22485289e9dSPhilippe Reynes 	return ret;
22585289e9dSPhilippe Reynes }
22685289e9dSPhilippe Reynes 
22785289e9dSPhilippe Reynes static int compute_hash_prime(struct checksum_algo *checksum,
22885289e9dSPhilippe Reynes 			      uint8_t *pad, int pad_len,
22985289e9dSPhilippe Reynes 			      uint8_t *hash, int hash_len,
23085289e9dSPhilippe Reynes 			      uint8_t *salt, int salt_len,
23185289e9dSPhilippe Reynes 			      uint8_t *hprime)
23285289e9dSPhilippe Reynes {
23385289e9dSPhilippe Reynes 	struct image_region region[3];
23485289e9dSPhilippe Reynes 	int ret, region_count = 3;
23585289e9dSPhilippe Reynes 
23685289e9dSPhilippe Reynes 	region[0].data = pad;
23785289e9dSPhilippe Reynes 	region[0].size = pad_len;
23885289e9dSPhilippe Reynes 	region[1].data = hash;
23985289e9dSPhilippe Reynes 	region[1].size = hash_len;
24085289e9dSPhilippe Reynes 	region[2].data = salt;
24185289e9dSPhilippe Reynes 	region[2].size = salt_len;
24285289e9dSPhilippe Reynes 
24385289e9dSPhilippe Reynes 	ret = checksum->calculate(checksum->name, region, region_count, hprime);
24485289e9dSPhilippe Reynes 	if (ret < 0) {
24585289e9dSPhilippe Reynes 		debug("%s: Error in checksum calculation\n", __func__);
24685289e9dSPhilippe Reynes 		goto out;
24785289e9dSPhilippe Reynes 	}
24885289e9dSPhilippe Reynes 
24985289e9dSPhilippe Reynes out:
25085289e9dSPhilippe Reynes 	return ret;
25185289e9dSPhilippe Reynes }
25285289e9dSPhilippe Reynes 
25385289e9dSPhilippe Reynes int padding_pss_verify(struct image_sign_info *info,
25485289e9dSPhilippe Reynes 		       uint8_t *msg, int msg_len,
25585289e9dSPhilippe Reynes 		       const uint8_t *hash, int hash_len)
25685289e9dSPhilippe Reynes {
25785289e9dSPhilippe Reynes 	uint8_t *masked_db = NULL;
25885289e9dSPhilippe Reynes 	int masked_db_len = msg_len - hash_len - 1;
25985289e9dSPhilippe Reynes 	uint8_t *h = NULL, *hprime = NULL;
26085289e9dSPhilippe Reynes 	int h_len = hash_len;
26185289e9dSPhilippe Reynes 	uint8_t *db_mask = NULL;
26285289e9dSPhilippe Reynes 	int db_mask_len = masked_db_len;
26385289e9dSPhilippe Reynes 	uint8_t *db = NULL, *salt = NULL;
26485289e9dSPhilippe Reynes 	int db_len = masked_db_len, salt_len = msg_len - hash_len - 2;
26585289e9dSPhilippe Reynes 	uint8_t pad_zero[8] = { 0 };
26685289e9dSPhilippe Reynes 	int ret, i, leftmost_bits = 1;
26785289e9dSPhilippe Reynes 	uint8_t leftmost_mask;
26885289e9dSPhilippe Reynes 	struct checksum_algo *checksum = info->checksum;
26985289e9dSPhilippe Reynes 
27085289e9dSPhilippe Reynes 	/* first, allocate everything */
27185289e9dSPhilippe Reynes 	masked_db = malloc(masked_db_len);
27285289e9dSPhilippe Reynes 	h = malloc(h_len);
27385289e9dSPhilippe Reynes 	db_mask = malloc(db_mask_len);
27485289e9dSPhilippe Reynes 	db = malloc(db_len);
27585289e9dSPhilippe Reynes 	salt = malloc(salt_len);
27685289e9dSPhilippe Reynes 	hprime = malloc(hash_len);
27785289e9dSPhilippe Reynes 	if (!masked_db || !h || !db_mask || !db || !salt || !hprime) {
27885289e9dSPhilippe Reynes 		printf("%s: can't allocate some buffer\n", __func__);
27985289e9dSPhilippe Reynes 		ret = -ENOMEM;
28085289e9dSPhilippe Reynes 		goto out;
28185289e9dSPhilippe Reynes 	}
28285289e9dSPhilippe Reynes 
28385289e9dSPhilippe Reynes 	/* step 4: check if the last byte is 0xbc */
28485289e9dSPhilippe Reynes 	if (msg[msg_len - 1] != 0xbc) {
28585289e9dSPhilippe Reynes 		printf("%s: invalid pss padding (0xbc is missing)\n", __func__);
28685289e9dSPhilippe Reynes 		ret = -EINVAL;
28785289e9dSPhilippe Reynes 		goto out;
28885289e9dSPhilippe Reynes 	}
28985289e9dSPhilippe Reynes 
29085289e9dSPhilippe Reynes 	/* step 5 */
29185289e9dSPhilippe Reynes 	memcpy(masked_db, msg, masked_db_len);
29285289e9dSPhilippe Reynes 	memcpy(h, msg + masked_db_len, h_len);
29385289e9dSPhilippe Reynes 
29485289e9dSPhilippe Reynes 	/* step 6 */
29585289e9dSPhilippe Reynes 	leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits);
29685289e9dSPhilippe Reynes 	if (masked_db[0] & leftmost_mask) {
29785289e9dSPhilippe Reynes 		printf("%s: invalid pss padding ", __func__);
29885289e9dSPhilippe Reynes 		printf("(leftmost bit of maskedDB not zero)\n");
29985289e9dSPhilippe Reynes 		ret = -EINVAL;
30085289e9dSPhilippe Reynes 		goto out;
30185289e9dSPhilippe Reynes 	}
30285289e9dSPhilippe Reynes 
30385289e9dSPhilippe Reynes 	/* step 7 */
30485289e9dSPhilippe Reynes 	mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len);
30585289e9dSPhilippe Reynes 
30685289e9dSPhilippe Reynes 	/* step 8 */
30785289e9dSPhilippe Reynes 	for (i = 0; i < db_len; i++)
30885289e9dSPhilippe Reynes 		db[i] = masked_db[i] ^ db_mask[i];
30985289e9dSPhilippe Reynes 
31085289e9dSPhilippe Reynes 	/* step 9 */
31185289e9dSPhilippe Reynes 	db[0] &= 0xff >> leftmost_bits;
31285289e9dSPhilippe Reynes 
31385289e9dSPhilippe Reynes 	/* step 10 */
31485289e9dSPhilippe Reynes 	if (db[0] != 0x01) {
31585289e9dSPhilippe Reynes 		printf("%s: invalid pss padding ", __func__);
31685289e9dSPhilippe Reynes 		printf("(leftmost byte of db isn't 0x01)\n");
31785289e9dSPhilippe Reynes 		ret = EINVAL;
31885289e9dSPhilippe Reynes 		goto out;
31985289e9dSPhilippe Reynes 	}
32085289e9dSPhilippe Reynes 
32185289e9dSPhilippe Reynes 	/* step 11 */
32285289e9dSPhilippe Reynes 	memcpy(salt, &db[1], salt_len);
32385289e9dSPhilippe Reynes 
32485289e9dSPhilippe Reynes 	/* step 12 & 13 */
32585289e9dSPhilippe Reynes 	compute_hash_prime(checksum, pad_zero, 8,
32685289e9dSPhilippe Reynes 			   (uint8_t *)hash, hash_len,
32785289e9dSPhilippe Reynes 			   salt, salt_len, hprime);
32885289e9dSPhilippe Reynes 
32985289e9dSPhilippe Reynes 	/* step 14 */
33085289e9dSPhilippe Reynes 	ret = memcmp(h, hprime, hash_len);
33185289e9dSPhilippe Reynes 
33285289e9dSPhilippe Reynes out:
33385289e9dSPhilippe Reynes 	free(hprime);
33485289e9dSPhilippe Reynes 	free(salt);
33585289e9dSPhilippe Reynes 	free(db);
33685289e9dSPhilippe Reynes 	free(db_mask);
33785289e9dSPhilippe Reynes 	free(h);
33885289e9dSPhilippe Reynes 	free(masked_db);
33985289e9dSPhilippe Reynes 
34085289e9dSPhilippe Reynes 	return ret;
34185289e9dSPhilippe Reynes }
34285289e9dSPhilippe Reynes #endif
34385289e9dSPhilippe Reynes 
344da29f299SAndrew Duda /**
345fc2f4246SRuchika Gupta  * rsa_verify_key() - Verify a signature against some data using RSA Key
34619c402afSSimon Glass  *
347fc2f4246SRuchika Gupta  * Verify a RSA PKCS1.5 signature against an expected hash using
348fc2f4246SRuchika Gupta  * the RSA Key properties in prop structure.
349fc2f4246SRuchika Gupta  *
350219050bfSPhilippe Reynes  * @info:	Specifies key and FIT information
351fc2f4246SRuchika Gupta  * @prop:	Specifies key
352fc2f4246SRuchika Gupta  * @sig:	Signature
353fc2f4246SRuchika Gupta  * @sig_len:	Number of bytes in signature
354fc2f4246SRuchika Gupta  * @hash:	Pointer to the expected hash
3550c1d74fdSAndrew Duda  * @key_len:	Number of bytes in rsa key
356fc2f4246SRuchika Gupta  * @return 0 if verified, -ve on error
35719c402afSSimon Glass  */
358219050bfSPhilippe Reynes static int rsa_verify_key(struct image_sign_info *info,
359219050bfSPhilippe Reynes 			  struct key_prop *prop, const uint8_t *sig,
360646257d1SHeiko Schocher 			  const uint32_t sig_len, const uint8_t *hash,
361219050bfSPhilippe Reynes 			  const uint32_t key_len)
36219c402afSSimon Glass {
36319c402afSSimon Glass 	int ret;
364219050bfSPhilippe Reynes 	struct checksum_algo *checksum = info->checksum;
365219050bfSPhilippe Reynes 	struct padding_algo *padding = info->padding;
366219050bfSPhilippe Reynes 	int hash_len = checksum->checksum_len;
36719c402afSSimon Glass 
368219050bfSPhilippe Reynes 	if (!prop || !sig || !hash || !checksum)
36919c402afSSimon Glass 		return -EIO;
37019c402afSSimon Glass 
371fc2f4246SRuchika Gupta 	if (sig_len != (prop->num_bits / 8)) {
37219c402afSSimon Glass 		debug("Signature is of incorrect length %d\n", sig_len);
37319c402afSSimon Glass 		return -EINVAL;
37419c402afSSimon Glass 	}
37519c402afSSimon Glass 
376219050bfSPhilippe Reynes 	debug("Checksum algorithm: %s", checksum->name);
377646257d1SHeiko Schocher 
37819c402afSSimon Glass 	/* Sanity check for stack size */
37919c402afSSimon Glass 	if (sig_len > RSA_MAX_SIG_BITS / 8) {
38019c402afSSimon Glass 		debug("Signature length %u exceeds maximum %d\n", sig_len,
38119c402afSSimon Glass 		      RSA_MAX_SIG_BITS / 8);
38219c402afSSimon Glass 		return -EINVAL;
38319c402afSSimon Glass 	}
38419c402afSSimon Glass 
385fc2f4246SRuchika Gupta 	uint8_t buf[sig_len];
38619c402afSSimon Glass 
387c937ff6dSRuchika Gupta #if !defined(USE_HOSTCC)
388008ec9b4SJoseph Chen #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO)
3890fb93272SJoseph Chen 	ret = rsa_mod_exp_hw(prop, sig, sig_len, key_len, buf);
390008ec9b4SJoseph Chen #else
391008ec9b4SJoseph Chen 	struct udevice *mod_exp_dev;
392008ec9b4SJoseph Chen 
393c937ff6dSRuchika Gupta 	ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
394c937ff6dSRuchika Gupta 	if (ret) {
395c937ff6dSRuchika Gupta 		printf("RSA: Can't find Modular Exp implementation\n");
396c937ff6dSRuchika Gupta 		return -EINVAL;
397c937ff6dSRuchika Gupta 	}
398c937ff6dSRuchika Gupta 
399c937ff6dSRuchika Gupta 	ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
400008ec9b4SJoseph Chen #endif
401c937ff6dSRuchika Gupta #else
402fc2f4246SRuchika Gupta 	ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
403c937ff6dSRuchika Gupta #endif
404fc2f4246SRuchika Gupta 	if (ret) {
405fc2f4246SRuchika Gupta 		debug("Error in Modular exponentation\n");
40619c402afSSimon Glass 		return ret;
407fc2f4246SRuchika Gupta 	}
40819c402afSSimon Glass 
409219050bfSPhilippe Reynes 	ret = padding->verify(info, buf, key_len, hash, hash_len);
410da29f299SAndrew Duda 	if (ret) {
411219050bfSPhilippe Reynes 		debug("In RSAVerify(): padding check failed!\n");
412219050bfSPhilippe Reynes 		return ret;
41319c402afSSimon Glass 	}
41419c402afSSimon Glass 
41519c402afSSimon Glass 	return 0;
41619c402afSSimon Glass }
41719c402afSSimon Glass 
41878263d89SJason Zhu static int rsa_get_key_prop(struct key_prop *prop, struct image_sign_info *info, int node)
41978263d89SJason Zhu {
42078263d89SJason Zhu 	const void *blob = info->fdt_blob;
42178263d89SJason Zhu 	int length;
42278263d89SJason Zhu 	int hash_node;
42378263d89SJason Zhu 
42478263d89SJason Zhu 	if (node < 0) {
42578263d89SJason Zhu 		debug("%s: Skipping invalid node", __func__);
42678263d89SJason Zhu 		return -EBADF;
42778263d89SJason Zhu 	}
42878263d89SJason Zhu 
42978263d89SJason Zhu 	if (!prop) {
43078263d89SJason Zhu 		debug("%s: The prop is NULL", __func__);
43178263d89SJason Zhu 		return -EBADF;
43278263d89SJason Zhu 	}
43378263d89SJason Zhu 
43478263d89SJason Zhu 	prop->burn_key = fdtdec_get_int(blob, node, "burn-key-hash", 0);
43578263d89SJason Zhu 
43678263d89SJason Zhu 	prop->num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
43778263d89SJason Zhu 
43878263d89SJason Zhu 	prop->n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
43978263d89SJason Zhu 
44078263d89SJason Zhu 	prop->public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
44178263d89SJason Zhu 	if (!prop->public_exponent || length < sizeof(uint64_t))
44278263d89SJason Zhu 		prop->public_exponent = NULL;
44378263d89SJason Zhu 
44478263d89SJason Zhu 	prop->exp_len = sizeof(uint64_t);
44578263d89SJason Zhu 	prop->modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
44678263d89SJason Zhu 	prop->public_exponent_BN = fdt_getprop(blob, node, "rsa,exponent-BN", NULL);
44778263d89SJason Zhu 	prop->rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
44878263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
44978263d89SJason Zhu 	hash_node = fdt_subnode_offset(blob, node, "hash@c");
45078263d89SJason Zhu #else
45178263d89SJason Zhu 	hash_node = fdt_subnode_offset(blob, node, "hash@np");
45278263d89SJason Zhu #endif
45378263d89SJason Zhu 	if (hash_node >= 0)
45478263d89SJason Zhu 		prop->hash = fdt_getprop(blob, hash_node, "value", NULL);
45578263d89SJason Zhu 
45678263d89SJason Zhu 	if (!prop->num_bits || !prop->modulus) {
45778263d89SJason Zhu 		debug("%s: Missing RSA key info", __func__);
45878263d89SJason Zhu 		return -EFAULT;
45978263d89SJason Zhu 	}
46078263d89SJason Zhu 
46178263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
46278263d89SJason Zhu 	prop->factor_c = fdt_getprop(blob, node, "rsa,c", NULL);
46378263d89SJason Zhu 	if (!prop.factor_c)
46478263d89SJason Zhu 		return -EFAULT;
46578263d89SJason Zhu #else
46678263d89SJason Zhu 	prop->factor_np = fdt_getprop(blob, node, "rsa,np", NULL);
46778263d89SJason Zhu 	if (!prop->factor_np)
46878263d89SJason Zhu 		return -EFAULT;
46978263d89SJason Zhu #endif
47078263d89SJason Zhu 
47178263d89SJason Zhu 	return 0;
47278263d89SJason Zhu }
47378263d89SJason Zhu 
474fc2f4246SRuchika Gupta /**
475fc2f4246SRuchika Gupta  * rsa_verify_with_keynode() - Verify a signature against some data using
476fc2f4246SRuchika Gupta  * information in node with prperties of RSA Key like modulus, exponent etc.
477fc2f4246SRuchika Gupta  *
478fc2f4246SRuchika Gupta  * Parse sign-node and fill a key_prop structure with properties of the
479fc2f4246SRuchika Gupta  * key.  Verify a RSA PKCS1.5 signature against an expected hash using
480fc2f4246SRuchika Gupta  * the properties parsed
481fc2f4246SRuchika Gupta  *
482fc2f4246SRuchika Gupta  * @info:	Specifies key and FIT information
483fc2f4246SRuchika Gupta  * @hash:	Pointer to the expected hash
484fc2f4246SRuchika Gupta  * @sig:	Signature
485fc2f4246SRuchika Gupta  * @sig_len:	Number of bytes in signature
486fc2f4246SRuchika Gupta  * @node:	Node having the RSA Key properties
487fc2f4246SRuchika Gupta  * @return 0 if verified, -ve on error
488fc2f4246SRuchika Gupta  */
48919c402afSSimon Glass static int rsa_verify_with_keynode(struct image_sign_info *info,
490fc2f4246SRuchika Gupta 				   const void *hash, uint8_t *sig,
491fc2f4246SRuchika Gupta 				   uint sig_len, int node)
49219c402afSSimon Glass {
493fc2f4246SRuchika Gupta 	struct key_prop prop;
49419c402afSSimon Glass 
49578263d89SJason Zhu 	if (rsa_get_key_prop(&prop, info, node))
49619c402afSSimon Glass 		return -EFAULT;
49719c402afSSimon Glass 
49878263d89SJason Zhu 	return rsa_verify_key(info, &prop, sig, sig_len, hash,
499219050bfSPhilippe Reynes 			      info->crypto->key_len);
50019c402afSSimon Glass }
50119c402afSSimon Glass 
50219c402afSSimon Glass int rsa_verify(struct image_sign_info *info,
50319c402afSSimon Glass 	       const struct image_region region[], int region_count,
50419c402afSSimon Glass 	       uint8_t *sig, uint sig_len)
50519c402afSSimon Glass {
50619c402afSSimon Glass 	const void *blob = info->fdt_blob;
507646257d1SHeiko Schocher 	/* Reserve memory for maximum checksum-length */
50883dd98e0SAndrew Duda 	uint8_t hash[info->crypto->key_len];
50919c402afSSimon Glass 	int ndepth, noffset;
51019c402afSSimon Glass 	int sig_node, node;
51119c402afSSimon Glass 	char name[100];
512646257d1SHeiko Schocher 	int ret;
513646257d1SHeiko Schocher 
514646257d1SHeiko Schocher 	/*
515646257d1SHeiko Schocher 	 * Verify that the checksum-length does not exceed the
516646257d1SHeiko Schocher 	 * rsa-signature-length
517646257d1SHeiko Schocher 	 */
51883dd98e0SAndrew Duda 	if (info->checksum->checksum_len >
51983dd98e0SAndrew Duda 	    info->crypto->key_len) {
520db1b5f3dSHeiko Schocher 		debug("%s: invlaid checksum-algorithm %s for %s\n",
52183dd98e0SAndrew Duda 		      __func__, info->checksum->name, info->crypto->name);
522646257d1SHeiko Schocher 		return -EINVAL;
523646257d1SHeiko Schocher 	}
52419c402afSSimon Glass 
52519c402afSSimon Glass 	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
52619c402afSSimon Glass 	if (sig_node < 0) {
52719c402afSSimon Glass 		debug("%s: No signature node found\n", __func__);
52819c402afSSimon Glass 		return -ENOENT;
52919c402afSSimon Glass 	}
53019c402afSSimon Glass 
531646257d1SHeiko Schocher 	/* Calculate checksum with checksum-algorithm */
53283dd98e0SAndrew Duda 	ret = info->checksum->calculate(info->checksum->name,
533b37b46f0SRuchika Gupta 					region, region_count, hash);
534b37b46f0SRuchika Gupta 	if (ret < 0) {
535b37b46f0SRuchika Gupta 		debug("%s: Error in checksum calculation\n", __func__);
536b37b46f0SRuchika Gupta 		return -EINVAL;
537b37b46f0SRuchika Gupta 	}
53819c402afSSimon Glass 
53919c402afSSimon Glass 	/* See if we must use a particular key */
54019c402afSSimon Glass 	if (info->required_keynode != -1) {
54119c402afSSimon Glass 		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
54219c402afSSimon Glass 			info->required_keynode);
54319c402afSSimon Glass 		if (!ret)
54419c402afSSimon Glass 			return ret;
54519c402afSSimon Glass 	}
54619c402afSSimon Glass 
54719c402afSSimon Glass 	/* Look for a key that matches our hint */
54819c402afSSimon Glass 	snprintf(name, sizeof(name), "key-%s", info->keyname);
54919c402afSSimon Glass 	node = fdt_subnode_offset(blob, sig_node, name);
55019c402afSSimon Glass 	ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
55119c402afSSimon Glass 	if (!ret)
55219c402afSSimon Glass 		return ret;
55319c402afSSimon Glass 
55419c402afSSimon Glass 	/* No luck, so try each of the keys in turn */
55519c402afSSimon Glass 	for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
55619c402afSSimon Glass 			(noffset >= 0) && (ndepth > 0);
55719c402afSSimon Glass 			noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
55819c402afSSimon Glass 		if (ndepth == 1 && noffset != node) {
55919c402afSSimon Glass 			ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
56019c402afSSimon Glass 						      noffset);
56119c402afSSimon Glass 			if (!ret)
56219c402afSSimon Glass 				break;
56319c402afSSimon Glass 		}
56419c402afSSimon Glass 	}
56519c402afSSimon Glass 
56619c402afSSimon Glass 	return ret;
56719c402afSSimon Glass }
56878263d89SJason Zhu 
56978263d89SJason Zhu #if !defined(USE_HOSTCC)
57078263d89SJason Zhu #ifdef CONFIG_SPL_FIT_HW_CRYPTO
57178263d89SJason Zhu int rsa_burn_key_hash(struct image_sign_info *info)
57278263d89SJason Zhu {
57378263d89SJason Zhu 	char *rsa_key;
57478263d89SJason Zhu 	void *n, *e, *c;
57578263d89SJason Zhu 	uint32_t key_len;
57678263d89SJason Zhu 	struct udevice *dev;
57778263d89SJason Zhu 	struct key_prop prop;
57878263d89SJason Zhu 	char name[100] = {0};
57978263d89SJason Zhu 	char secure_boot_enable = 0;
58078263d89SJason Zhu 	const void *blob = info->fdt_blob;
58178263d89SJason Zhu 	uint8_t digest[FIT_MAX_HASH_LEN];
58278263d89SJason Zhu 	uint8_t digest_read[FIT_MAX_HASH_LEN];
58378263d89SJason Zhu 	int sig_node, node, digest_len, i, ret = 0;
58478263d89SJason Zhu 
58578263d89SJason Zhu 	dev = misc_otp_get_device(OTP_S);
58678263d89SJason Zhu 	if (!dev)
58778263d89SJason Zhu 		return -ENODEV;
58878263d89SJason Zhu 
58978263d89SJason Zhu 	ret = misc_otp_read(dev, OTP_SECURE_BOOT_ENABLE_ADDR,
59078263d89SJason Zhu 			    &secure_boot_enable, OTP_SECURE_BOOT_ENABLE_SIZE);
59178263d89SJason Zhu 	if (ret)
59278263d89SJason Zhu 		return ret;
59378263d89SJason Zhu 
59478263d89SJason Zhu 	if (secure_boot_enable)
59578263d89SJason Zhu 		return 0;
59678263d89SJason Zhu 
59778263d89SJason Zhu 	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
59878263d89SJason Zhu 	if (sig_node < 0) {
59978263d89SJason Zhu 		debug("%s: No signature node found\n", __func__);
60078263d89SJason Zhu 		return -ENOENT;
60178263d89SJason Zhu 	}
60278263d89SJason Zhu 
60378263d89SJason Zhu 	snprintf(name, sizeof(name), "key-%s", info->keyname);
60478263d89SJason Zhu 	node = fdt_subnode_offset(blob, sig_node, name);
60578263d89SJason Zhu 
60678263d89SJason Zhu 	if (rsa_get_key_prop(&prop, info, node))
60778263d89SJason Zhu 		return -1;
60878263d89SJason Zhu 
60978263d89SJason Zhu 	if (!(prop.burn_key))
61078263d89SJason Zhu 		return -EPERM;
61178263d89SJason Zhu 
61278263d89SJason Zhu 	if (!prop.hash || !prop.modulus || !prop.public_exponent_BN)
61378263d89SJason Zhu 		return -ENOENT;
61478263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
61578263d89SJason Zhu 	if (!prop.factor_c)
61678263d89SJason Zhu 		return -ENOENT;
61778263d89SJason Zhu #else
61878263d89SJason Zhu 	if (!prop.factor_np)
61978263d89SJason Zhu 		return -ENOENT;
62078263d89SJason Zhu #endif
62178263d89SJason Zhu 	key_len = info->crypto->key_len;
62278263d89SJason Zhu 	if (info->crypto->key_len != RSA2048_BYTES)
62378263d89SJason Zhu 		return -EINVAL;
62478263d89SJason Zhu 
62578263d89SJason Zhu 	rsa_key = malloc(key_len * 3);
62678263d89SJason Zhu 	if (!rsa_key)
62778263d89SJason Zhu 		return -ENOMEM;
62878263d89SJason Zhu 
62978263d89SJason Zhu 	n = rsa_key;
63078263d89SJason Zhu 	e = rsa_key + key_len;
63178263d89SJason Zhu 	c = rsa_key + key_len * 2;
63278263d89SJason Zhu 	rsa_convert_big_endian(n, (uint32_t *)prop.modulus,
63378263d89SJason Zhu 			       key_len / sizeof(uint32_t));
63478263d89SJason Zhu 	rsa_convert_big_endian(e, (uint32_t *)prop.public_exponent_BN,
63578263d89SJason Zhu 			       key_len / sizeof(uint32_t));
63678263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
63778263d89SJason Zhu 	rsa_convert_big_endian(c, (uint32_t *)prop.factor_c,
63878263d89SJason Zhu 			       key_len / sizeof(uint32_t));
63978263d89SJason Zhu #else
64078263d89SJason Zhu 	rsa_convert_big_endian(c, (uint32_t *)prop.factor_np,
64178263d89SJason Zhu 			       key_len / sizeof(uint32_t));
64278263d89SJason Zhu #endif
64378263d89SJason Zhu 
64478263d89SJason Zhu 	ret = calculate_hash(rsa_key, key_len * 2 + OTP_RSA2048_C_SIZE,
64578263d89SJason Zhu 			     info->checksum->name, digest, &digest_len);
64678263d89SJason Zhu 	if (ret)
64778263d89SJason Zhu 		goto error;
64878263d89SJason Zhu 
64978263d89SJason Zhu 	if (memcmp(digest, prop.hash, digest_len) != 0) {
650*bd2c27ccSJason Zhu 		printf("RSA: Compare public key hash fail.\n");
65178263d89SJason Zhu 		goto error;
65278263d89SJason Zhu 	}
65378263d89SJason Zhu 
65478263d89SJason Zhu 	/* burn key hash here */
65578263d89SJason Zhu 	ret = misc_otp_read(dev, OTP_RSA_HASH_ADDR, digest_read, OTP_RSA_HASH_SIZE);
65678263d89SJason Zhu 	if (ret)
65778263d89SJason Zhu 		goto error;
65878263d89SJason Zhu 
65978263d89SJason Zhu 	for (i = 0; i < OTP_RSA_HASH_SIZE; i++) {
66078263d89SJason Zhu 		if (digest_read[i]) {
66178263d89SJason Zhu 			printf("RSA: The secure region has been written.\n");
66278263d89SJason Zhu 			ret = -EIO;
66378263d89SJason Zhu 			goto error;
66478263d89SJason Zhu 		}
66578263d89SJason Zhu 	}
66678263d89SJason Zhu 
66778263d89SJason Zhu 	ret = misc_otp_write(dev, OTP_RSA_HASH_ADDR, digest, OTP_RSA_HASH_SIZE);
66878263d89SJason Zhu 	if (ret)
66978263d89SJason Zhu 		goto error;
67078263d89SJason Zhu 
671*bd2c27ccSJason Zhu 	memset(digest_read, 0, FIT_MAX_HASH_LEN);
672*bd2c27ccSJason Zhu 	ret = misc_otp_read(dev, OTP_RSA_HASH_ADDR, digest_read, OTP_RSA_HASH_SIZE);
673*bd2c27ccSJason Zhu 	if (ret)
674*bd2c27ccSJason Zhu 		goto error;
675*bd2c27ccSJason Zhu 
676*bd2c27ccSJason Zhu 	if (memcmp(digest, digest_read, digest_len) != 0) {
677*bd2c27ccSJason Zhu 		printf("RSA: Write public key hash fail.\n");
678*bd2c27ccSJason Zhu 		goto error;
679*bd2c27ccSJason Zhu 	}
680*bd2c27ccSJason Zhu 
68178263d89SJason Zhu 	secure_boot_enable = 0xff;
68278263d89SJason Zhu 	ret = misc_otp_write(dev, OTP_SECURE_BOOT_ENABLE_ADDR,
68378263d89SJason Zhu 			     &secure_boot_enable, OTP_SECURE_BOOT_ENABLE_SIZE);
68478263d89SJason Zhu 	if (ret)
68578263d89SJason Zhu 		goto error;
68678263d89SJason Zhu 
68778263d89SJason Zhu 	printf("RSA:Write key hash successfully\n");
68878263d89SJason Zhu 
68978263d89SJason Zhu error:
69078263d89SJason Zhu 	free(rsa_key);
69178263d89SJason Zhu 
69278263d89SJason Zhu 	return ret;
69378263d89SJason Zhu }
69478263d89SJason Zhu #endif
69578263d89SJason Zhu #endif
696