xref: /rk3399_rockchip-uboot/lib/rsa/rsa-verify.c (revision c2b765628bbc0e8f471bc9f675d893b0eb3f41c7)
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;
89008ec9b4SJoseph Chen 
90008ec9b4SJoseph Chen 	if (key_len != RSA2048_BYTES)
91008ec9b4SJoseph Chen 		return -EINVAL;
92008ec9b4SJoseph Chen 
93008ec9b4SJoseph Chen 	rsa_key.algo = CRYPTO_RSA2048;
94008ec9b4SJoseph Chen 	rsa_key.n = malloc(key_len);
95008ec9b4SJoseph Chen 	rsa_key.e = malloc(key_len);
96008ec9b4SJoseph Chen 	rsa_key.c = malloc(key_len);
97008ec9b4SJoseph Chen 	if (!rsa_key.n || !rsa_key.e || !rsa_key.c)
98008ec9b4SJoseph Chen 		return -ENOMEM;
99008ec9b4SJoseph Chen 
100008ec9b4SJoseph Chen 	rsa_convert_big_endian(rsa_key.n, (uint32_t *)prop->modulus,
1019c63859fSJason Zhu 			       key_len, key_len);
102008ec9b4SJoseph Chen 	rsa_convert_big_endian(rsa_key.e, (uint32_t *)prop->public_exponent_BN,
1039c63859fSJason Zhu 			       key_len, key_len);
104008ec9b4SJoseph Chen #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
105008ec9b4SJoseph Chen 	rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_c,
1069c63859fSJason Zhu 			       key_len, key_len);
107008ec9b4SJoseph Chen #else
108008ec9b4SJoseph Chen 	rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_np,
1099c63859fSJason Zhu 			       key_len, key_len);
110008ec9b4SJoseph Chen #endif
111f649b885SJason Zhu #if defined(CONFIG_ROCKCHIP_PRELOADER_ATAGS) && defined(CONFIG_SPL_BUILD)
112f649b885SJason Zhu 	char *rsa_key_data = malloc(3 * key_len);
113f649b885SJason Zhu 	int flag = 0;
114f649b885SJason Zhu 
115f649b885SJason Zhu 	if (rsa_key_data) {
116f649b885SJason Zhu 		memcpy(rsa_key_data, rsa_key.n, key_len);
117f649b885SJason Zhu 		memcpy(rsa_key_data + key_len, rsa_key.e, key_len);
118f649b885SJason Zhu 		memcpy(rsa_key_data + 2 * key_len, rsa_key.c, key_len);
119f649b885SJason Zhu 		if (fit_board_verify_required_sigs())
120f649b885SJason Zhu 			flag = PUBKEY_FUSE_PROGRAMMED;
121f649b885SJason Zhu 
122f649b885SJason Zhu 		if (atags_set_pub_key(rsa_key_data, 3 * key_len, flag))
123f649b885SJason Zhu 			printf("Send public key through atags fail.");
124f649b885SJason Zhu 	}
125f649b885SJason Zhu #endif
126008ec9b4SJoseph Chen 	for (i = 0; i < sig_len; i++)
127008ec9b4SJoseph Chen 		sig_reverse[sig_len-1-i] = sig[i];
128008ec9b4SJoseph Chen 
129008ec9b4SJoseph Chen 	dev = crypto_get_device(rsa_key.algo);
130008ec9b4SJoseph Chen 	if (!dev) {
131008ec9b4SJoseph Chen 		printf("No crypto device for expected RSA\n");
132008ec9b4SJoseph Chen 		return -ENODEV;
133008ec9b4SJoseph Chen 	}
134008ec9b4SJoseph Chen 
135008ec9b4SJoseph Chen 	ret = crypto_rsa_verify(dev, &rsa_key, (u8 *)sig_reverse, buf);
136008ec9b4SJoseph Chen 	if (ret)
137008ec9b4SJoseph Chen 		goto out;
138008ec9b4SJoseph Chen 
139008ec9b4SJoseph Chen 	for (i = 0; i < sig_len; i++)
140008ec9b4SJoseph Chen 		sig_reverse[sig_len-1-i] = buf[i];
141008ec9b4SJoseph Chen 
142008ec9b4SJoseph Chen 	memcpy(output, sig_reverse, sig_len);
143008ec9b4SJoseph Chen out:
144008ec9b4SJoseph Chen 	free(rsa_key.n);
145008ec9b4SJoseph Chen 	free(rsa_key.e);
146008ec9b4SJoseph Chen 	free(rsa_key.c);
147008ec9b4SJoseph Chen 
148008ec9b4SJoseph Chen 	return ret;
149008ec9b4SJoseph Chen }
150008ec9b4SJoseph Chen #endif
151008ec9b4SJoseph Chen #endif
152008ec9b4SJoseph Chen 
153219050bfSPhilippe Reynes int padding_pkcs_15_verify(struct image_sign_info *info,
154219050bfSPhilippe Reynes 			   uint8_t *msg, int msg_len,
155219050bfSPhilippe Reynes 			   const uint8_t *hash, int hash_len)
156219050bfSPhilippe Reynes {
157219050bfSPhilippe Reynes 	struct checksum_algo *checksum = info->checksum;
158219050bfSPhilippe Reynes 	int ret, pad_len = msg_len - checksum->checksum_len;
159219050bfSPhilippe Reynes 
160219050bfSPhilippe Reynes 	/* Check pkcs1.5 padding bytes. */
161219050bfSPhilippe Reynes 	ret = rsa_verify_padding(msg, pad_len, checksum);
162219050bfSPhilippe Reynes 	if (ret) {
163219050bfSPhilippe Reynes 		debug("In RSAVerify(): Padding check failed!\n");
164219050bfSPhilippe Reynes 		return -EINVAL;
165219050bfSPhilippe Reynes 	}
166219050bfSPhilippe Reynes 
167219050bfSPhilippe Reynes 	/* Check hash. */
168219050bfSPhilippe Reynes 	if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) {
169219050bfSPhilippe Reynes 		debug("In RSAVerify(): Hash check failed!\n");
170219050bfSPhilippe Reynes 		return -EACCES;
171219050bfSPhilippe Reynes 	}
172219050bfSPhilippe Reynes 
173219050bfSPhilippe Reynes 	return 0;
174219050bfSPhilippe Reynes }
175219050bfSPhilippe Reynes 
17685289e9dSPhilippe Reynes #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
17785289e9dSPhilippe Reynes static void u32_i2osp(uint32_t val, uint8_t *buf)
17885289e9dSPhilippe Reynes {
17985289e9dSPhilippe Reynes 	buf[0] = (uint8_t)((val >> 24) & 0xff);
18085289e9dSPhilippe Reynes 	buf[1] = (uint8_t)((val >> 16) & 0xff);
18185289e9dSPhilippe Reynes 	buf[2] = (uint8_t)((val >>  8) & 0xff);
18285289e9dSPhilippe Reynes 	buf[3] = (uint8_t)((val >>  0) & 0xff);
18385289e9dSPhilippe Reynes }
18485289e9dSPhilippe Reynes 
18585289e9dSPhilippe Reynes /**
18685289e9dSPhilippe Reynes  * mask_generation_function1() - generate an octet string
18785289e9dSPhilippe Reynes  *
18885289e9dSPhilippe Reynes  * Generate an octet string used to check rsa signature.
18985289e9dSPhilippe Reynes  * It use an input octet string and a hash function.
19085289e9dSPhilippe Reynes  *
19185289e9dSPhilippe Reynes  * @checksum:	A Hash function
19285289e9dSPhilippe Reynes  * @seed:	Specifies an input variable octet string
19385289e9dSPhilippe Reynes  * @seed_len:	Size of the input octet string
19485289e9dSPhilippe Reynes  * @output:	Specifies the output octet string
19585289e9dSPhilippe Reynes  * @output_len:	Size of the output octet string
19685289e9dSPhilippe Reynes  * @return 0 if the octet string was correctly generated, others on error
19785289e9dSPhilippe Reynes  */
19885289e9dSPhilippe Reynes static int mask_generation_function1(struct checksum_algo *checksum,
19985289e9dSPhilippe Reynes 				     uint8_t *seed, int seed_len,
20085289e9dSPhilippe Reynes 				     uint8_t *output, int output_len)
20185289e9dSPhilippe Reynes {
20285289e9dSPhilippe Reynes 	struct image_region region[2];
20385289e9dSPhilippe Reynes 	int ret = 0, i, i_output = 0, region_count = 2;
20485289e9dSPhilippe Reynes 	uint32_t counter = 0;
20585289e9dSPhilippe Reynes 	uint8_t buf_counter[4], *tmp;
20685289e9dSPhilippe Reynes 	int hash_len = checksum->checksum_len;
20785289e9dSPhilippe Reynes 
20885289e9dSPhilippe Reynes 	memset(output, 0, output_len);
20985289e9dSPhilippe Reynes 
21085289e9dSPhilippe Reynes 	region[0].data = seed;
21185289e9dSPhilippe Reynes 	region[0].size = seed_len;
21285289e9dSPhilippe Reynes 	region[1].data = &buf_counter[0];
21385289e9dSPhilippe Reynes 	region[1].size = 4;
21485289e9dSPhilippe Reynes 
21585289e9dSPhilippe Reynes 	tmp = malloc(hash_len);
21685289e9dSPhilippe Reynes 	if (!tmp) {
21785289e9dSPhilippe Reynes 		debug("%s: can't allocate array tmp\n", __func__);
21885289e9dSPhilippe Reynes 		ret = -ENOMEM;
21985289e9dSPhilippe Reynes 		goto out;
22085289e9dSPhilippe Reynes 	}
22185289e9dSPhilippe Reynes 
22285289e9dSPhilippe Reynes 	while (i_output < output_len) {
22385289e9dSPhilippe Reynes 		u32_i2osp(counter, &buf_counter[0]);
22485289e9dSPhilippe Reynes 
22585289e9dSPhilippe Reynes 		ret = checksum->calculate(checksum->name,
22685289e9dSPhilippe Reynes 					  region, region_count,
22785289e9dSPhilippe Reynes 					  tmp);
22885289e9dSPhilippe Reynes 		if (ret < 0) {
22985289e9dSPhilippe Reynes 			debug("%s: Error in checksum calculation\n", __func__);
23085289e9dSPhilippe Reynes 			goto out;
23185289e9dSPhilippe Reynes 		}
23285289e9dSPhilippe Reynes 
23385289e9dSPhilippe Reynes 		i = 0;
23485289e9dSPhilippe Reynes 		while ((i_output < output_len) && (i < hash_len)) {
23585289e9dSPhilippe Reynes 			output[i_output] = tmp[i];
23685289e9dSPhilippe Reynes 			i_output++;
23785289e9dSPhilippe Reynes 			i++;
23885289e9dSPhilippe Reynes 		}
23985289e9dSPhilippe Reynes 
24085289e9dSPhilippe Reynes 		counter++;
24185289e9dSPhilippe Reynes 	}
24285289e9dSPhilippe Reynes 
24385289e9dSPhilippe Reynes out:
24485289e9dSPhilippe Reynes 	free(tmp);
24585289e9dSPhilippe Reynes 
24685289e9dSPhilippe Reynes 	return ret;
24785289e9dSPhilippe Reynes }
24885289e9dSPhilippe Reynes 
24985289e9dSPhilippe Reynes static int compute_hash_prime(struct checksum_algo *checksum,
25085289e9dSPhilippe Reynes 			      uint8_t *pad, int pad_len,
25185289e9dSPhilippe Reynes 			      uint8_t *hash, int hash_len,
25285289e9dSPhilippe Reynes 			      uint8_t *salt, int salt_len,
25385289e9dSPhilippe Reynes 			      uint8_t *hprime)
25485289e9dSPhilippe Reynes {
25585289e9dSPhilippe Reynes 	struct image_region region[3];
25685289e9dSPhilippe Reynes 	int ret, region_count = 3;
25785289e9dSPhilippe Reynes 
25885289e9dSPhilippe Reynes 	region[0].data = pad;
25985289e9dSPhilippe Reynes 	region[0].size = pad_len;
26085289e9dSPhilippe Reynes 	region[1].data = hash;
26185289e9dSPhilippe Reynes 	region[1].size = hash_len;
26285289e9dSPhilippe Reynes 	region[2].data = salt;
26385289e9dSPhilippe Reynes 	region[2].size = salt_len;
26485289e9dSPhilippe Reynes 
26585289e9dSPhilippe Reynes 	ret = checksum->calculate(checksum->name, region, region_count, hprime);
26685289e9dSPhilippe Reynes 	if (ret < 0) {
26785289e9dSPhilippe Reynes 		debug("%s: Error in checksum calculation\n", __func__);
26885289e9dSPhilippe Reynes 		goto out;
26985289e9dSPhilippe Reynes 	}
27085289e9dSPhilippe Reynes 
27185289e9dSPhilippe Reynes out:
27285289e9dSPhilippe Reynes 	return ret;
27385289e9dSPhilippe Reynes }
27485289e9dSPhilippe Reynes 
27585289e9dSPhilippe Reynes int padding_pss_verify(struct image_sign_info *info,
27685289e9dSPhilippe Reynes 		       uint8_t *msg, int msg_len,
27785289e9dSPhilippe Reynes 		       const uint8_t *hash, int hash_len)
27885289e9dSPhilippe Reynes {
27985289e9dSPhilippe Reynes 	uint8_t *masked_db = NULL;
28085289e9dSPhilippe Reynes 	int masked_db_len = msg_len - hash_len - 1;
28185289e9dSPhilippe Reynes 	uint8_t *h = NULL, *hprime = NULL;
28285289e9dSPhilippe Reynes 	int h_len = hash_len;
28385289e9dSPhilippe Reynes 	uint8_t *db_mask = NULL;
28485289e9dSPhilippe Reynes 	int db_mask_len = masked_db_len;
28585289e9dSPhilippe Reynes 	uint8_t *db = NULL, *salt = NULL;
28685289e9dSPhilippe Reynes 	int db_len = masked_db_len, salt_len = msg_len - hash_len - 2;
28785289e9dSPhilippe Reynes 	uint8_t pad_zero[8] = { 0 };
28885289e9dSPhilippe Reynes 	int ret, i, leftmost_bits = 1;
28985289e9dSPhilippe Reynes 	uint8_t leftmost_mask;
29085289e9dSPhilippe Reynes 	struct checksum_algo *checksum = info->checksum;
29185289e9dSPhilippe Reynes 
29285289e9dSPhilippe Reynes 	/* first, allocate everything */
29385289e9dSPhilippe Reynes 	masked_db = malloc(masked_db_len);
29485289e9dSPhilippe Reynes 	h = malloc(h_len);
29585289e9dSPhilippe Reynes 	db_mask = malloc(db_mask_len);
29685289e9dSPhilippe Reynes 	db = malloc(db_len);
29785289e9dSPhilippe Reynes 	salt = malloc(salt_len);
29885289e9dSPhilippe Reynes 	hprime = malloc(hash_len);
29985289e9dSPhilippe Reynes 	if (!masked_db || !h || !db_mask || !db || !salt || !hprime) {
30085289e9dSPhilippe Reynes 		printf("%s: can't allocate some buffer\n", __func__);
30185289e9dSPhilippe Reynes 		ret = -ENOMEM;
30285289e9dSPhilippe Reynes 		goto out;
30385289e9dSPhilippe Reynes 	}
30485289e9dSPhilippe Reynes 
30585289e9dSPhilippe Reynes 	/* step 4: check if the last byte is 0xbc */
30685289e9dSPhilippe Reynes 	if (msg[msg_len - 1] != 0xbc) {
30785289e9dSPhilippe Reynes 		printf("%s: invalid pss padding (0xbc is missing)\n", __func__);
30885289e9dSPhilippe Reynes 		ret = -EINVAL;
30985289e9dSPhilippe Reynes 		goto out;
31085289e9dSPhilippe Reynes 	}
31185289e9dSPhilippe Reynes 
31285289e9dSPhilippe Reynes 	/* step 5 */
31385289e9dSPhilippe Reynes 	memcpy(masked_db, msg, masked_db_len);
31485289e9dSPhilippe Reynes 	memcpy(h, msg + masked_db_len, h_len);
31585289e9dSPhilippe Reynes 
31685289e9dSPhilippe Reynes 	/* step 6 */
31785289e9dSPhilippe Reynes 	leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits);
31885289e9dSPhilippe Reynes 	if (masked_db[0] & leftmost_mask) {
31985289e9dSPhilippe Reynes 		printf("%s: invalid pss padding ", __func__);
32085289e9dSPhilippe Reynes 		printf("(leftmost bit of maskedDB not zero)\n");
32185289e9dSPhilippe Reynes 		ret = -EINVAL;
32285289e9dSPhilippe Reynes 		goto out;
32385289e9dSPhilippe Reynes 	}
32485289e9dSPhilippe Reynes 
32585289e9dSPhilippe Reynes 	/* step 7 */
32685289e9dSPhilippe Reynes 	mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len);
32785289e9dSPhilippe Reynes 
32885289e9dSPhilippe Reynes 	/* step 8 */
32985289e9dSPhilippe Reynes 	for (i = 0; i < db_len; i++)
33085289e9dSPhilippe Reynes 		db[i] = masked_db[i] ^ db_mask[i];
33185289e9dSPhilippe Reynes 
33285289e9dSPhilippe Reynes 	/* step 9 */
33385289e9dSPhilippe Reynes 	db[0] &= 0xff >> leftmost_bits;
33485289e9dSPhilippe Reynes 
33585289e9dSPhilippe Reynes 	/* step 10 */
33685289e9dSPhilippe Reynes 	if (db[0] != 0x01) {
33785289e9dSPhilippe Reynes 		printf("%s: invalid pss padding ", __func__);
33885289e9dSPhilippe Reynes 		printf("(leftmost byte of db isn't 0x01)\n");
33985289e9dSPhilippe Reynes 		ret = EINVAL;
34085289e9dSPhilippe Reynes 		goto out;
34185289e9dSPhilippe Reynes 	}
34285289e9dSPhilippe Reynes 
34385289e9dSPhilippe Reynes 	/* step 11 */
34485289e9dSPhilippe Reynes 	memcpy(salt, &db[1], salt_len);
34585289e9dSPhilippe Reynes 
34685289e9dSPhilippe Reynes 	/* step 12 & 13 */
34785289e9dSPhilippe Reynes 	compute_hash_prime(checksum, pad_zero, 8,
34885289e9dSPhilippe Reynes 			   (uint8_t *)hash, hash_len,
34985289e9dSPhilippe Reynes 			   salt, salt_len, hprime);
35085289e9dSPhilippe Reynes 
35185289e9dSPhilippe Reynes 	/* step 14 */
35285289e9dSPhilippe Reynes 	ret = memcmp(h, hprime, hash_len);
35385289e9dSPhilippe Reynes 
35485289e9dSPhilippe Reynes out:
35585289e9dSPhilippe Reynes 	free(hprime);
35685289e9dSPhilippe Reynes 	free(salt);
35785289e9dSPhilippe Reynes 	free(db);
35885289e9dSPhilippe Reynes 	free(db_mask);
35985289e9dSPhilippe Reynes 	free(h);
36085289e9dSPhilippe Reynes 	free(masked_db);
36185289e9dSPhilippe Reynes 
36285289e9dSPhilippe Reynes 	return ret;
36385289e9dSPhilippe Reynes }
36485289e9dSPhilippe Reynes #endif
36585289e9dSPhilippe Reynes 
366da29f299SAndrew Duda /**
367fc2f4246SRuchika Gupta  * rsa_verify_key() - Verify a signature against some data using RSA Key
36819c402afSSimon Glass  *
369fc2f4246SRuchika Gupta  * Verify a RSA PKCS1.5 signature against an expected hash using
370fc2f4246SRuchika Gupta  * the RSA Key properties in prop structure.
371fc2f4246SRuchika Gupta  *
372219050bfSPhilippe Reynes  * @info:	Specifies key and FIT information
373fc2f4246SRuchika Gupta  * @prop:	Specifies key
374fc2f4246SRuchika Gupta  * @sig:	Signature
375fc2f4246SRuchika Gupta  * @sig_len:	Number of bytes in signature
376fc2f4246SRuchika Gupta  * @hash:	Pointer to the expected hash
3770c1d74fdSAndrew Duda  * @key_len:	Number of bytes in rsa key
378fc2f4246SRuchika Gupta  * @return 0 if verified, -ve on error
37919c402afSSimon Glass  */
380219050bfSPhilippe Reynes static int rsa_verify_key(struct image_sign_info *info,
381219050bfSPhilippe Reynes 			  struct key_prop *prop, const uint8_t *sig,
382646257d1SHeiko Schocher 			  const uint32_t sig_len, const uint8_t *hash,
383219050bfSPhilippe Reynes 			  const uint32_t key_len)
38419c402afSSimon Glass {
38519c402afSSimon Glass 	int ret;
386219050bfSPhilippe Reynes 	struct checksum_algo *checksum = info->checksum;
387219050bfSPhilippe Reynes 	struct padding_algo *padding = info->padding;
388219050bfSPhilippe Reynes 	int hash_len = checksum->checksum_len;
38919c402afSSimon Glass 
390219050bfSPhilippe Reynes 	if (!prop || !sig || !hash || !checksum)
39119c402afSSimon Glass 		return -EIO;
39219c402afSSimon Glass 
393fc2f4246SRuchika Gupta 	if (sig_len != (prop->num_bits / 8)) {
39419c402afSSimon Glass 		debug("Signature is of incorrect length %d\n", sig_len);
39519c402afSSimon Glass 		return -EINVAL;
39619c402afSSimon Glass 	}
39719c402afSSimon Glass 
398219050bfSPhilippe Reynes 	debug("Checksum algorithm: %s", checksum->name);
399646257d1SHeiko Schocher 
40019c402afSSimon Glass 	/* Sanity check for stack size */
40119c402afSSimon Glass 	if (sig_len > RSA_MAX_SIG_BITS / 8) {
40219c402afSSimon Glass 		debug("Signature length %u exceeds maximum %d\n", sig_len,
40319c402afSSimon Glass 		      RSA_MAX_SIG_BITS / 8);
40419c402afSSimon Glass 		return -EINVAL;
40519c402afSSimon Glass 	}
40619c402afSSimon Glass 
407fc2f4246SRuchika Gupta 	uint8_t buf[sig_len];
40819c402afSSimon Glass 
409c937ff6dSRuchika Gupta #if !defined(USE_HOSTCC)
410008ec9b4SJoseph Chen #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO)
4110fb93272SJoseph Chen 	ret = rsa_mod_exp_hw(prop, sig, sig_len, key_len, buf);
412008ec9b4SJoseph Chen #else
413008ec9b4SJoseph Chen 	struct udevice *mod_exp_dev;
414008ec9b4SJoseph Chen 
415c937ff6dSRuchika Gupta 	ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
416c937ff6dSRuchika Gupta 	if (ret) {
417c937ff6dSRuchika Gupta 		printf("RSA: Can't find Modular Exp implementation\n");
418c937ff6dSRuchika Gupta 		return -EINVAL;
419c937ff6dSRuchika Gupta 	}
420c937ff6dSRuchika Gupta 
421c937ff6dSRuchika Gupta 	ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
422008ec9b4SJoseph Chen #endif
423c937ff6dSRuchika Gupta #else
424fc2f4246SRuchika Gupta 	ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
425c937ff6dSRuchika Gupta #endif
426fc2f4246SRuchika Gupta 	if (ret) {
427fc2f4246SRuchika Gupta 		debug("Error in Modular exponentation\n");
42819c402afSSimon Glass 		return ret;
429fc2f4246SRuchika Gupta 	}
43019c402afSSimon Glass 
431219050bfSPhilippe Reynes 	ret = padding->verify(info, buf, key_len, hash, hash_len);
432da29f299SAndrew Duda 	if (ret) {
433219050bfSPhilippe Reynes 		debug("In RSAVerify(): padding check failed!\n");
434219050bfSPhilippe Reynes 		return ret;
43519c402afSSimon Glass 	}
43619c402afSSimon Glass 
43719c402afSSimon Glass 	return 0;
43819c402afSSimon Glass }
43919c402afSSimon Glass 
44078263d89SJason Zhu static int rsa_get_key_prop(struct key_prop *prop, struct image_sign_info *info, int node)
44178263d89SJason Zhu {
44278263d89SJason Zhu 	const void *blob = info->fdt_blob;
44378263d89SJason Zhu 	int length;
44478263d89SJason Zhu 	int hash_node;
44578263d89SJason Zhu 
44678263d89SJason Zhu 	if (node < 0) {
44778263d89SJason Zhu 		debug("%s: Skipping invalid node", __func__);
44878263d89SJason Zhu 		return -EBADF;
44978263d89SJason Zhu 	}
45078263d89SJason Zhu 
45178263d89SJason Zhu 	if (!prop) {
45278263d89SJason Zhu 		debug("%s: The prop is NULL", __func__);
45378263d89SJason Zhu 		return -EBADF;
45478263d89SJason Zhu 	}
45578263d89SJason Zhu 
45678263d89SJason Zhu 	prop->burn_key = fdtdec_get_int(blob, node, "burn-key-hash", 0);
45778263d89SJason Zhu 
45878263d89SJason Zhu 	prop->num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
45978263d89SJason Zhu 
46078263d89SJason Zhu 	prop->n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
46178263d89SJason Zhu 
46278263d89SJason Zhu 	prop->public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
46378263d89SJason Zhu 	if (!prop->public_exponent || length < sizeof(uint64_t))
46478263d89SJason Zhu 		prop->public_exponent = NULL;
46578263d89SJason Zhu 
46678263d89SJason Zhu 	prop->exp_len = sizeof(uint64_t);
46778263d89SJason Zhu 	prop->modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
46878263d89SJason Zhu 	prop->public_exponent_BN = fdt_getprop(blob, node, "rsa,exponent-BN", NULL);
46978263d89SJason Zhu 	prop->rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
47078263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
47178263d89SJason Zhu 	hash_node = fdt_subnode_offset(blob, node, "hash@c");
47278263d89SJason Zhu #else
47378263d89SJason Zhu 	hash_node = fdt_subnode_offset(blob, node, "hash@np");
47478263d89SJason Zhu #endif
47578263d89SJason Zhu 	if (hash_node >= 0)
47678263d89SJason Zhu 		prop->hash = fdt_getprop(blob, hash_node, "value", NULL);
47778263d89SJason Zhu 
47878263d89SJason Zhu 	if (!prop->num_bits || !prop->modulus) {
47978263d89SJason Zhu 		debug("%s: Missing RSA key info", __func__);
48078263d89SJason Zhu 		return -EFAULT;
48178263d89SJason Zhu 	}
48278263d89SJason Zhu 
48378263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
48478263d89SJason Zhu 	prop->factor_c = fdt_getprop(blob, node, "rsa,c", NULL);
48578263d89SJason Zhu 	if (!prop.factor_c)
48678263d89SJason Zhu 		return -EFAULT;
48778263d89SJason Zhu #else
48878263d89SJason Zhu 	prop->factor_np = fdt_getprop(blob, node, "rsa,np", NULL);
48978263d89SJason Zhu 	if (!prop->factor_np)
49078263d89SJason Zhu 		return -EFAULT;
49178263d89SJason Zhu #endif
49278263d89SJason Zhu 
49378263d89SJason Zhu 	return 0;
49478263d89SJason Zhu }
49578263d89SJason Zhu 
496fc2f4246SRuchika Gupta /**
497fc2f4246SRuchika Gupta  * rsa_verify_with_keynode() - Verify a signature against some data using
498fc2f4246SRuchika Gupta  * information in node with prperties of RSA Key like modulus, exponent etc.
499fc2f4246SRuchika Gupta  *
500fc2f4246SRuchika Gupta  * Parse sign-node and fill a key_prop structure with properties of the
501fc2f4246SRuchika Gupta  * key.  Verify a RSA PKCS1.5 signature against an expected hash using
502fc2f4246SRuchika Gupta  * the properties parsed
503fc2f4246SRuchika Gupta  *
504fc2f4246SRuchika Gupta  * @info:	Specifies key and FIT information
505fc2f4246SRuchika Gupta  * @hash:	Pointer to the expected hash
506fc2f4246SRuchika Gupta  * @sig:	Signature
507fc2f4246SRuchika Gupta  * @sig_len:	Number of bytes in signature
508fc2f4246SRuchika Gupta  * @node:	Node having the RSA Key properties
509fc2f4246SRuchika Gupta  * @return 0 if verified, -ve on error
510fc2f4246SRuchika Gupta  */
51119c402afSSimon Glass static int rsa_verify_with_keynode(struct image_sign_info *info,
512fc2f4246SRuchika Gupta 				   const void *hash, uint8_t *sig,
513fc2f4246SRuchika Gupta 				   uint sig_len, int node)
51419c402afSSimon Glass {
515fc2f4246SRuchika Gupta 	struct key_prop prop;
51619c402afSSimon Glass 
51778263d89SJason Zhu 	if (rsa_get_key_prop(&prop, info, node))
51819c402afSSimon Glass 		return -EFAULT;
51919c402afSSimon Glass 
52078263d89SJason Zhu 	return rsa_verify_key(info, &prop, sig, sig_len, hash,
521219050bfSPhilippe Reynes 			      info->crypto->key_len);
52219c402afSSimon Glass }
52319c402afSSimon Glass 
52419c402afSSimon Glass int rsa_verify(struct image_sign_info *info,
52519c402afSSimon Glass 	       const struct image_region region[], int region_count,
52619c402afSSimon Glass 	       uint8_t *sig, uint sig_len)
52719c402afSSimon Glass {
52819c402afSSimon Glass 	const void *blob = info->fdt_blob;
529646257d1SHeiko Schocher 	/* Reserve memory for maximum checksum-length */
53083dd98e0SAndrew Duda 	uint8_t hash[info->crypto->key_len];
53119c402afSSimon Glass 	int ndepth, noffset;
53219c402afSSimon Glass 	int sig_node, node;
53319c402afSSimon Glass 	char name[100];
534646257d1SHeiko Schocher 	int ret;
535646257d1SHeiko Schocher 
536646257d1SHeiko Schocher 	/*
537646257d1SHeiko Schocher 	 * Verify that the checksum-length does not exceed the
538646257d1SHeiko Schocher 	 * rsa-signature-length
539646257d1SHeiko Schocher 	 */
54083dd98e0SAndrew Duda 	if (info->checksum->checksum_len >
54183dd98e0SAndrew Duda 	    info->crypto->key_len) {
542db1b5f3dSHeiko Schocher 		debug("%s: invlaid checksum-algorithm %s for %s\n",
54383dd98e0SAndrew Duda 		      __func__, info->checksum->name, info->crypto->name);
544646257d1SHeiko Schocher 		return -EINVAL;
545646257d1SHeiko Schocher 	}
54619c402afSSimon Glass 
54719c402afSSimon Glass 	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
54819c402afSSimon Glass 	if (sig_node < 0) {
54919c402afSSimon Glass 		debug("%s: No signature node found\n", __func__);
55019c402afSSimon Glass 		return -ENOENT;
55119c402afSSimon Glass 	}
55219c402afSSimon Glass 
553646257d1SHeiko Schocher 	/* Calculate checksum with checksum-algorithm */
55483dd98e0SAndrew Duda 	ret = info->checksum->calculate(info->checksum->name,
555b37b46f0SRuchika Gupta 					region, region_count, hash);
556b37b46f0SRuchika Gupta 	if (ret < 0) {
557b37b46f0SRuchika Gupta 		debug("%s: Error in checksum calculation\n", __func__);
558b37b46f0SRuchika Gupta 		return -EINVAL;
559b37b46f0SRuchika Gupta 	}
56019c402afSSimon Glass 
56119c402afSSimon Glass 	/* See if we must use a particular key */
56219c402afSSimon Glass 	if (info->required_keynode != -1) {
56319c402afSSimon Glass 		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
56419c402afSSimon Glass 			info->required_keynode);
56519c402afSSimon Glass 		if (!ret)
56619c402afSSimon Glass 			return ret;
56719c402afSSimon Glass 	}
56819c402afSSimon Glass 
56919c402afSSimon Glass 	/* Look for a key that matches our hint */
57019c402afSSimon Glass 	snprintf(name, sizeof(name), "key-%s", info->keyname);
57119c402afSSimon Glass 	node = fdt_subnode_offset(blob, sig_node, name);
57219c402afSSimon Glass 	ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
57319c402afSSimon Glass 	if (!ret)
57419c402afSSimon Glass 		return ret;
57519c402afSSimon Glass 
57619c402afSSimon Glass 	/* No luck, so try each of the keys in turn */
57719c402afSSimon Glass 	for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
57819c402afSSimon Glass 			(noffset >= 0) && (ndepth > 0);
57919c402afSSimon Glass 			noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
58019c402afSSimon Glass 		if (ndepth == 1 && noffset != node) {
58119c402afSSimon Glass 			ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
58219c402afSSimon Glass 						      noffset);
58319c402afSSimon Glass 			if (!ret)
58419c402afSSimon Glass 				break;
58519c402afSSimon Glass 		}
58619c402afSSimon Glass 	}
58719c402afSSimon Glass 
58819c402afSSimon Glass 	return ret;
58919c402afSSimon Glass }
59078263d89SJason Zhu 
59178263d89SJason Zhu #if !defined(USE_HOSTCC)
592*c2b76562SJoseph Chen #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_FIT_HW_CRYPTO) && \
593*c2b76562SJoseph Chen     defined(CONFIG_SPL_ROCKCHIP_SECURE_OTP)
59478263d89SJason Zhu int rsa_burn_key_hash(struct image_sign_info *info)
59578263d89SJason Zhu {
59678263d89SJason Zhu 	char *rsa_key;
59778263d89SJason Zhu 	void *n, *e, *c;
59878263d89SJason Zhu 	uint32_t key_len;
59978263d89SJason Zhu 	struct udevice *dev;
60078263d89SJason Zhu 	struct key_prop prop;
60178263d89SJason Zhu 	char name[100] = {0};
6029c63859fSJason Zhu 	u16 secure_boot_enable = 0;
60378263d89SJason Zhu 	const void *blob = info->fdt_blob;
60478263d89SJason Zhu 	uint8_t digest[FIT_MAX_HASH_LEN];
60578263d89SJason Zhu 	uint8_t digest_read[FIT_MAX_HASH_LEN];
60678263d89SJason Zhu 	int sig_node, node, digest_len, i, ret = 0;
60778263d89SJason Zhu 
60878263d89SJason Zhu 	dev = misc_otp_get_device(OTP_S);
60978263d89SJason Zhu 	if (!dev)
61078263d89SJason Zhu 		return -ENODEV;
61178263d89SJason Zhu 
61278263d89SJason Zhu 	ret = misc_otp_read(dev, OTP_SECURE_BOOT_ENABLE_ADDR,
61378263d89SJason Zhu 			    &secure_boot_enable, OTP_SECURE_BOOT_ENABLE_SIZE);
61478263d89SJason Zhu 	if (ret)
61578263d89SJason Zhu 		return ret;
61678263d89SJason Zhu 
61778263d89SJason Zhu 	if (secure_boot_enable)
61878263d89SJason Zhu 		return 0;
61978263d89SJason Zhu 
62078263d89SJason Zhu 	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
62178263d89SJason Zhu 	if (sig_node < 0) {
62278263d89SJason Zhu 		debug("%s: No signature node found\n", __func__);
62378263d89SJason Zhu 		return -ENOENT;
62478263d89SJason Zhu 	}
62578263d89SJason Zhu 
62678263d89SJason Zhu 	snprintf(name, sizeof(name), "key-%s", info->keyname);
62778263d89SJason Zhu 	node = fdt_subnode_offset(blob, sig_node, name);
62878263d89SJason Zhu 
62978263d89SJason Zhu 	if (rsa_get_key_prop(&prop, info, node))
63078263d89SJason Zhu 		return -1;
63178263d89SJason Zhu 
63278263d89SJason Zhu 	if (!(prop.burn_key))
63378263d89SJason Zhu 		return -EPERM;
63478263d89SJason Zhu 
63578263d89SJason Zhu 	if (!prop.hash || !prop.modulus || !prop.public_exponent_BN)
63678263d89SJason Zhu 		return -ENOENT;
63778263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
63878263d89SJason Zhu 	if (!prop.factor_c)
63978263d89SJason Zhu 		return -ENOENT;
64078263d89SJason Zhu #else
64178263d89SJason Zhu 	if (!prop.factor_np)
64278263d89SJason Zhu 		return -ENOENT;
64378263d89SJason Zhu #endif
64478263d89SJason Zhu 	key_len = info->crypto->key_len;
64578263d89SJason Zhu 	if (info->crypto->key_len != RSA2048_BYTES)
64678263d89SJason Zhu 		return -EINVAL;
64778263d89SJason Zhu 
6489c63859fSJason Zhu 	rsa_key = calloc(key_len * 3, sizeof(char));
64978263d89SJason Zhu 	if (!rsa_key)
65078263d89SJason Zhu 		return -ENOMEM;
65178263d89SJason Zhu 
65278263d89SJason Zhu 	n = rsa_key;
6539c63859fSJason Zhu 	e = rsa_key + CONFIG_RSA_N_SIZE;
6549c63859fSJason Zhu 	c = rsa_key + CONFIG_RSA_N_SIZE + CONFIG_RSA_E_SIZE;
65578263d89SJason Zhu 	rsa_convert_big_endian(n, (uint32_t *)prop.modulus,
6569c63859fSJason Zhu 			       key_len, CONFIG_RSA_N_SIZE);
65778263d89SJason Zhu 	rsa_convert_big_endian(e, (uint32_t *)prop.public_exponent_BN,
6589c63859fSJason Zhu 			       key_len, CONFIG_RSA_E_SIZE);
65978263d89SJason Zhu #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
66078263d89SJason Zhu 	rsa_convert_big_endian(c, (uint32_t *)prop.factor_c,
6619c63859fSJason Zhu 			       key_len, CONFIG_RSA_C_SIZE);
66278263d89SJason Zhu #else
66378263d89SJason Zhu 	rsa_convert_big_endian(c, (uint32_t *)prop.factor_np,
6649c63859fSJason Zhu 			       key_len, CONFIG_RSA_C_SIZE);
66578263d89SJason Zhu #endif
66678263d89SJason Zhu 
6679c63859fSJason Zhu 	ret = calculate_hash(rsa_key, CONFIG_RSA_N_SIZE + CONFIG_RSA_E_SIZE + CONFIG_RSA_C_SIZE,
66878263d89SJason Zhu 			     info->checksum->name, digest, &digest_len);
66978263d89SJason Zhu 	if (ret)
67078263d89SJason Zhu 		goto error;
67178263d89SJason Zhu 
67278263d89SJason Zhu 	if (memcmp(digest, prop.hash, digest_len) != 0) {
673bd2c27ccSJason Zhu 		printf("RSA: Compare public key hash fail.\n");
67478263d89SJason Zhu 		goto error;
67578263d89SJason Zhu 	}
67678263d89SJason Zhu 
67778263d89SJason Zhu 	/* burn key hash here */
67878263d89SJason Zhu 	ret = misc_otp_read(dev, OTP_RSA_HASH_ADDR, digest_read, OTP_RSA_HASH_SIZE);
67978263d89SJason Zhu 	if (ret)
68078263d89SJason Zhu 		goto error;
68178263d89SJason Zhu 
68278263d89SJason Zhu 	for (i = 0; i < OTP_RSA_HASH_SIZE; i++) {
68378263d89SJason Zhu 		if (digest_read[i]) {
68478263d89SJason Zhu 			printf("RSA: The secure region has been written.\n");
68578263d89SJason Zhu 			ret = -EIO;
68678263d89SJason Zhu 			goto error;
68778263d89SJason Zhu 		}
68878263d89SJason Zhu 	}
68978263d89SJason Zhu 
69078263d89SJason Zhu 	ret = misc_otp_write(dev, OTP_RSA_HASH_ADDR, digest, OTP_RSA_HASH_SIZE);
69178263d89SJason Zhu 	if (ret)
69278263d89SJason Zhu 		goto error;
69378263d89SJason Zhu 
694bd2c27ccSJason Zhu 	memset(digest_read, 0, FIT_MAX_HASH_LEN);
695bd2c27ccSJason Zhu 	ret = misc_otp_read(dev, OTP_RSA_HASH_ADDR, digest_read, OTP_RSA_HASH_SIZE);
696bd2c27ccSJason Zhu 	if (ret)
697bd2c27ccSJason Zhu 		goto error;
698bd2c27ccSJason Zhu 
699bd2c27ccSJason Zhu 	if (memcmp(digest, digest_read, digest_len) != 0) {
700bd2c27ccSJason Zhu 		printf("RSA: Write public key hash fail.\n");
701bd2c27ccSJason Zhu 		goto error;
702bd2c27ccSJason Zhu 	}
703bd2c27ccSJason Zhu 
70478263d89SJason Zhu 	secure_boot_enable = 0xff;
70578263d89SJason Zhu 	ret = misc_otp_write(dev, OTP_SECURE_BOOT_ENABLE_ADDR,
70678263d89SJason Zhu 			     &secure_boot_enable, OTP_SECURE_BOOT_ENABLE_SIZE);
70778263d89SJason Zhu 	if (ret)
70878263d89SJason Zhu 		goto error;
70978263d89SJason Zhu 
7109c63859fSJason Zhu 	printf("RSA: Write key hash successfully\n");
71178263d89SJason Zhu 
71278263d89SJason Zhu error:
71378263d89SJason Zhu 	free(rsa_key);
71478263d89SJason Zhu 
71578263d89SJason Zhu 	return ret;
71678263d89SJason Zhu }
71778263d89SJason Zhu #endif
71878263d89SJason Zhu #endif
719