xref: /rk3399_rockchip-uboot/lib/rsa/rsa-verify.c (revision 7ac3b0edb507765f43120c591d830e5f63fe3474)
1 /*
2  * Copyright (c) 2013, Google Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #ifndef USE_HOSTCC
8 #include <common.h>
9 #include <crypto.h>
10 #include <fdtdec.h>
11 #include <misc.h>
12 #include <asm/types.h>
13 #include <asm/byteorder.h>
14 #include <linux/errno.h>
15 #include <asm/types.h>
16 #include <asm/unaligned.h>
17 #include <dm.h>
18 #include <asm/arch/rk_atags.h>
19 #else
20 #include "fdt_host.h"
21 #include "mkimage.h"
22 #include <fdt_support.h>
23 #endif
24 #include <u-boot/rsa-mod-exp.h>
25 #include <u-boot/rsa.h>
26 
27 /* Default public exponent for backward compatibility */
28 #define RSA_DEFAULT_PUBEXP	65537
29 
30 /**
31  * rsa_verify_padding() - Verify RSA message padding is valid
32  *
33  * Verify a RSA message's padding is consistent with PKCS1.5
34  * padding as described in the RSA PKCS#1 v2.1 standard.
35  *
36  * @msg:	Padded message
37  * @pad_len:	Number of expected padding bytes
38  * @algo:	Checksum algo structure having information on DER encoding etc.
39  * @return 0 on success, != 0 on failure
40  */
41 static int rsa_verify_padding(const uint8_t *msg, const int pad_len,
42 			      struct checksum_algo *algo)
43 {
44 	int ff_len;
45 	int ret;
46 
47 	/* first byte must be 0x00 */
48 	ret = *msg++;
49 	/* second byte must be 0x01 */
50 	ret |= *msg++ ^ 0x01;
51 	/* next ff_len bytes must be 0xff */
52 	ff_len = pad_len - algo->der_len - 3;
53 	ret |= *msg ^ 0xff;
54 	ret |= memcmp(msg, msg+1, ff_len-1);
55 	msg += ff_len;
56 	/* next byte must be 0x00 */
57 	ret |= *msg++;
58 	/* next der_len bytes must match der_prefix */
59 	ret |= memcmp(msg, algo->der_prefix, algo->der_len);
60 
61 	return ret;
62 }
63 
64 #if !defined(USE_HOSTCC)
65 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO)
66 static void rsa_convert_big_endian(uint32_t *dst, const uint32_t *src,
67 				   int total_len, int convert_len)
68 {
69 	int total_wd, convert_wd, i;
70 
71 	if (total_len < convert_len)
72 		convert_len = total_len;
73 
74 	total_wd = total_len / sizeof(uint32_t);
75 	convert_wd = convert_len / sizeof(uint32_t);
76 	for (i = 0; i < convert_wd; i++)
77 		dst[i] = fdt32_to_cpu(src[total_wd - 1 - i]);
78 }
79 
80 static int rsa_mod_exp_hw(struct key_prop *prop, const uint8_t *sig,
81 			  const uint32_t sig_len, const uint32_t key_len,
82 			  uint8_t *output)
83 {
84 	struct udevice *dev;
85 	uint8_t sig_reverse[sig_len];
86 	uint8_t buf[sig_len];
87 	rsa_key rsa_key;
88 	int i, ret;
89 
90 	if (key_len != RSA2048_BYTES)
91 		return -EINVAL;
92 
93 	rsa_key.algo = CRYPTO_RSA2048;
94 	rsa_key.n = malloc(key_len);
95 	rsa_key.e = malloc(key_len);
96 	rsa_key.c = malloc(key_len);
97 	if (!rsa_key.n || !rsa_key.e || !rsa_key.c)
98 		return -ENOMEM;
99 
100 	rsa_convert_big_endian(rsa_key.n, (uint32_t *)prop->modulus,
101 			       key_len, key_len);
102 	rsa_convert_big_endian(rsa_key.e, (uint32_t *)prop->public_exponent_BN,
103 			       key_len, key_len);
104 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
105 	rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_c,
106 			       key_len, key_len);
107 #else
108 	rsa_convert_big_endian(rsa_key.c, (uint32_t *)prop->factor_np,
109 			       key_len, key_len);
110 #endif
111 #if defined(CONFIG_ROCKCHIP_PRELOADER_ATAGS) && defined(CONFIG_SPL_BUILD)
112 	char *rsa_key_data = malloc(3 * key_len);
113 	int flag = 0;
114 
115 	if (rsa_key_data) {
116 		memcpy(rsa_key_data, rsa_key.n, key_len);
117 		memcpy(rsa_key_data + key_len, rsa_key.e, key_len);
118 		memcpy(rsa_key_data + 2 * key_len, rsa_key.c, key_len);
119 		if (fit_board_verify_required_sigs())
120 			flag = PUBKEY_FUSE_PROGRAMMED;
121 
122 		if (atags_set_pub_key(rsa_key_data, 3 * key_len, flag))
123 			printf("Send public key through atags fail.");
124 	}
125 #endif
126 	for (i = 0; i < sig_len; i++)
127 		sig_reverse[sig_len-1-i] = sig[i];
128 
129 	dev = crypto_get_device(rsa_key.algo);
130 	if (!dev) {
131 		printf("No crypto device for expected RSA\n");
132 		return -ENODEV;
133 	}
134 
135 	ret = crypto_rsa_verify(dev, &rsa_key, (u8 *)sig_reverse, buf);
136 	if (ret)
137 		goto out;
138 
139 	for (i = 0; i < sig_len; i++)
140 		sig_reverse[sig_len-1-i] = buf[i];
141 
142 	memcpy(output, sig_reverse, sig_len);
143 out:
144 	free(rsa_key.n);
145 	free(rsa_key.e);
146 	free(rsa_key.c);
147 
148 	return ret;
149 }
150 #endif
151 #endif
152 
153 int padding_pkcs_15_verify(struct image_sign_info *info,
154 			   uint8_t *msg, int msg_len,
155 			   const uint8_t *hash, int hash_len)
156 {
157 	struct checksum_algo *checksum = info->checksum;
158 	int ret, pad_len = msg_len - checksum->checksum_len;
159 
160 	/* Check pkcs1.5 padding bytes. */
161 	ret = rsa_verify_padding(msg, pad_len, checksum);
162 	if (ret) {
163 		debug("In RSAVerify(): Padding check failed!\n");
164 		return -EINVAL;
165 	}
166 
167 	/* Check hash. */
168 	if (memcmp((uint8_t *)msg + pad_len, hash, msg_len - pad_len)) {
169 		debug("In RSAVerify(): Hash check failed!\n");
170 		return -EACCES;
171 	}
172 
173 	return 0;
174 }
175 
176 #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
177 static void u32_i2osp(uint32_t val, uint8_t *buf)
178 {
179 	buf[0] = (uint8_t)((val >> 24) & 0xff);
180 	buf[1] = (uint8_t)((val >> 16) & 0xff);
181 	buf[2] = (uint8_t)((val >>  8) & 0xff);
182 	buf[3] = (uint8_t)((val >>  0) & 0xff);
183 }
184 
185 /**
186  * mask_generation_function1() - generate an octet string
187  *
188  * Generate an octet string used to check rsa signature.
189  * It use an input octet string and a hash function.
190  *
191  * @checksum:	A Hash function
192  * @seed:	Specifies an input variable octet string
193  * @seed_len:	Size of the input octet string
194  * @output:	Specifies the output octet string
195  * @output_len:	Size of the output octet string
196  * @return 0 if the octet string was correctly generated, others on error
197  */
198 static int mask_generation_function1(struct checksum_algo *checksum,
199 				     uint8_t *seed, int seed_len,
200 				     uint8_t *output, int output_len)
201 {
202 	struct image_region region[2];
203 	int ret = 0, i, i_output = 0, region_count = 2;
204 	uint32_t counter = 0;
205 	uint8_t buf_counter[4], *tmp;
206 	int hash_len = checksum->checksum_len;
207 
208 	memset(output, 0, output_len);
209 
210 	region[0].data = seed;
211 	region[0].size = seed_len;
212 	region[1].data = &buf_counter[0];
213 	region[1].size = 4;
214 
215 	tmp = malloc(hash_len);
216 	if (!tmp) {
217 		debug("%s: can't allocate array tmp\n", __func__);
218 		ret = -ENOMEM;
219 		goto out;
220 	}
221 
222 	while (i_output < output_len) {
223 		u32_i2osp(counter, &buf_counter[0]);
224 
225 		ret = checksum->calculate(checksum->name,
226 					  region, region_count,
227 					  tmp);
228 		if (ret < 0) {
229 			debug("%s: Error in checksum calculation\n", __func__);
230 			goto out;
231 		}
232 
233 		i = 0;
234 		while ((i_output < output_len) && (i < hash_len)) {
235 			output[i_output] = tmp[i];
236 			i_output++;
237 			i++;
238 		}
239 
240 		counter++;
241 	}
242 
243 out:
244 	free(tmp);
245 
246 	return ret;
247 }
248 
249 static int compute_hash_prime(struct checksum_algo *checksum,
250 			      uint8_t *pad, int pad_len,
251 			      uint8_t *hash, int hash_len,
252 			      uint8_t *salt, int salt_len,
253 			      uint8_t *hprime)
254 {
255 	struct image_region region[3];
256 	int ret, region_count = 3;
257 
258 	region[0].data = pad;
259 	region[0].size = pad_len;
260 	region[1].data = hash;
261 	region[1].size = hash_len;
262 	region[2].data = salt;
263 	region[2].size = salt_len;
264 
265 	ret = checksum->calculate(checksum->name, region, region_count, hprime);
266 	if (ret < 0) {
267 		debug("%s: Error in checksum calculation\n", __func__);
268 		goto out;
269 	}
270 
271 out:
272 	return ret;
273 }
274 
275 int padding_pss_verify(struct image_sign_info *info,
276 		       uint8_t *msg, int msg_len,
277 		       const uint8_t *hash, int hash_len)
278 {
279 	uint8_t *masked_db = NULL;
280 	int masked_db_len = msg_len - hash_len - 1;
281 	uint8_t *h = NULL, *hprime = NULL;
282 	int h_len = hash_len;
283 	uint8_t *db_mask = NULL;
284 	int db_mask_len = masked_db_len;
285 	uint8_t *db = NULL, *salt = NULL;
286 	int db_len = masked_db_len, salt_len = msg_len - hash_len - 2;
287 	uint8_t pad_zero[8] = { 0 };
288 	int ret, i, leftmost_bits = 1;
289 	uint8_t leftmost_mask;
290 	struct checksum_algo *checksum = info->checksum;
291 
292 	/* first, allocate everything */
293 	masked_db = malloc(masked_db_len);
294 	h = malloc(h_len);
295 	db_mask = malloc(db_mask_len);
296 	db = malloc(db_len);
297 	salt = malloc(salt_len);
298 	hprime = malloc(hash_len);
299 	if (!masked_db || !h || !db_mask || !db || !salt || !hprime) {
300 		printf("%s: can't allocate some buffer\n", __func__);
301 		ret = -ENOMEM;
302 		goto out;
303 	}
304 
305 	/* step 4: check if the last byte is 0xbc */
306 	if (msg[msg_len - 1] != 0xbc) {
307 		printf("%s: invalid pss padding (0xbc is missing)\n", __func__);
308 		ret = -EINVAL;
309 		goto out;
310 	}
311 
312 	/* step 5 */
313 	memcpy(masked_db, msg, masked_db_len);
314 	memcpy(h, msg + masked_db_len, h_len);
315 
316 	/* step 6 */
317 	leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits);
318 	if (masked_db[0] & leftmost_mask) {
319 		printf("%s: invalid pss padding ", __func__);
320 		printf("(leftmost bit of maskedDB not zero)\n");
321 		ret = -EINVAL;
322 		goto out;
323 	}
324 
325 	/* step 7 */
326 	mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len);
327 
328 	/* step 8 */
329 	for (i = 0; i < db_len; i++)
330 		db[i] = masked_db[i] ^ db_mask[i];
331 
332 	/* step 9 */
333 	db[0] &= 0xff >> leftmost_bits;
334 
335 	/* step 10 */
336 	if (db[0] != 0x01) {
337 		printf("%s: invalid pss padding ", __func__);
338 		printf("(leftmost byte of db isn't 0x01)\n");
339 		ret = EINVAL;
340 		goto out;
341 	}
342 
343 	/* step 11 */
344 	memcpy(salt, &db[1], salt_len);
345 
346 	/* step 12 & 13 */
347 	compute_hash_prime(checksum, pad_zero, 8,
348 			   (uint8_t *)hash, hash_len,
349 			   salt, salt_len, hprime);
350 
351 	/* step 14 */
352 	ret = memcmp(h, hprime, hash_len);
353 
354 out:
355 	free(hprime);
356 	free(salt);
357 	free(db);
358 	free(db_mask);
359 	free(h);
360 	free(masked_db);
361 
362 	return ret;
363 }
364 #endif
365 
366 /**
367  * rsa_verify_key() - Verify a signature against some data using RSA Key
368  *
369  * Verify a RSA PKCS1.5 signature against an expected hash using
370  * the RSA Key properties in prop structure.
371  *
372  * @info:	Specifies key and FIT information
373  * @prop:	Specifies key
374  * @sig:	Signature
375  * @sig_len:	Number of bytes in signature
376  * @hash:	Pointer to the expected hash
377  * @key_len:	Number of bytes in rsa key
378  * @return 0 if verified, -ve on error
379  */
380 static int rsa_verify_key(struct image_sign_info *info,
381 			  struct key_prop *prop, const uint8_t *sig,
382 			  const uint32_t sig_len, const uint8_t *hash,
383 			  const uint32_t key_len)
384 {
385 	int ret;
386 	struct checksum_algo *checksum = info->checksum;
387 	struct padding_algo *padding = info->padding;
388 	int hash_len = checksum->checksum_len;
389 
390 	if (!prop || !sig || !hash || !checksum)
391 		return -EIO;
392 
393 	if (sig_len != (prop->num_bits / 8)) {
394 		debug("Signature is of incorrect length %d\n", sig_len);
395 		return -EINVAL;
396 	}
397 
398 	debug("Checksum algorithm: %s", checksum->name);
399 
400 	/* Sanity check for stack size */
401 	if (sig_len > RSA_MAX_SIG_BITS / 8) {
402 		debug("Signature length %u exceeds maximum %d\n", sig_len,
403 		      RSA_MAX_SIG_BITS / 8);
404 		return -EINVAL;
405 	}
406 
407 	uint8_t buf[sig_len];
408 
409 #if !defined(USE_HOSTCC)
410 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO)
411 	ret = rsa_mod_exp_hw(prop, sig, sig_len, key_len, buf);
412 #else
413 	struct udevice *mod_exp_dev;
414 
415 	ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
416 	if (ret) {
417 		printf("RSA: Can't find Modular Exp implementation\n");
418 		return -EINVAL;
419 	}
420 
421 	ret = rsa_mod_exp(mod_exp_dev, sig, sig_len, prop, buf);
422 #endif
423 #else
424 	ret = rsa_mod_exp_sw(sig, sig_len, prop, buf);
425 #endif
426 	if (ret) {
427 		debug("Error in Modular exponentation\n");
428 		return ret;
429 	}
430 
431 	ret = padding->verify(info, buf, key_len, hash, hash_len);
432 	if (ret) {
433 		debug("In RSAVerify(): padding check failed!\n");
434 		return ret;
435 	}
436 
437 	return 0;
438 }
439 
440 static int rsa_get_key_prop(struct key_prop *prop, struct image_sign_info *info, int node)
441 {
442 	const void *blob = info->fdt_blob;
443 	int length;
444 	int hash_node;
445 
446 	if (node < 0) {
447 		debug("%s: Skipping invalid node", __func__);
448 		return -EBADF;
449 	}
450 
451 	if (!prop) {
452 		debug("%s: The prop is NULL", __func__);
453 		return -EBADF;
454 	}
455 
456 	prop->burn_key = fdtdec_get_int(blob, node, "burn-key-hash", 0);
457 
458 	prop->num_bits = fdtdec_get_int(blob, node, "rsa,num-bits", 0);
459 
460 	prop->n0inv = fdtdec_get_int(blob, node, "rsa,n0-inverse", 0);
461 
462 	prop->public_exponent = fdt_getprop(blob, node, "rsa,exponent", &length);
463 	if (!prop->public_exponent || length < sizeof(uint64_t))
464 		prop->public_exponent = NULL;
465 
466 	prop->exp_len = sizeof(uint64_t);
467 	prop->modulus = fdt_getprop(blob, node, "rsa,modulus", NULL);
468 	prop->public_exponent_BN = fdt_getprop(blob, node, "rsa,exponent-BN", NULL);
469 	prop->rr = fdt_getprop(blob, node, "rsa,r-squared", NULL);
470 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
471 	hash_node = fdt_subnode_offset(blob, node, "hash@c");
472 #else
473 	hash_node = fdt_subnode_offset(blob, node, "hash@np");
474 #endif
475 	if (hash_node >= 0)
476 		prop->hash = fdt_getprop(blob, hash_node, "value", NULL);
477 
478 	if (!prop->num_bits || !prop->modulus) {
479 		debug("%s: Missing RSA key info", __func__);
480 		return -EFAULT;
481 	}
482 
483 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
484 	prop->factor_c = fdt_getprop(blob, node, "rsa,c", NULL);
485 	if (!prop.factor_c)
486 		return -EFAULT;
487 #else
488 	prop->factor_np = fdt_getprop(blob, node, "rsa,np", NULL);
489 	if (!prop->factor_np)
490 		return -EFAULT;
491 #endif
492 
493 	return 0;
494 }
495 
496 /**
497  * rsa_verify_with_keynode() - Verify a signature against some data using
498  * information in node with prperties of RSA Key like modulus, exponent etc.
499  *
500  * Parse sign-node and fill a key_prop structure with properties of the
501  * key.  Verify a RSA PKCS1.5 signature against an expected hash using
502  * the properties parsed
503  *
504  * @info:	Specifies key and FIT information
505  * @hash:	Pointer to the expected hash
506  * @sig:	Signature
507  * @sig_len:	Number of bytes in signature
508  * @node:	Node having the RSA Key properties
509  * @return 0 if verified, -ve on error
510  */
511 static int rsa_verify_with_keynode(struct image_sign_info *info,
512 				   const void *hash, uint8_t *sig,
513 				   uint sig_len, int node)
514 {
515 	struct key_prop prop;
516 
517 	if (rsa_get_key_prop(&prop, info, node))
518 		return -EFAULT;
519 
520 	return rsa_verify_key(info, &prop, sig, sig_len, hash,
521 			      info->crypto->key_len);
522 }
523 
524 int rsa_verify(struct image_sign_info *info,
525 	       const struct image_region region[], int region_count,
526 	       uint8_t *sig, uint sig_len)
527 {
528 	const void *blob = info->fdt_blob;
529 	/* Reserve memory for maximum checksum-length */
530 	uint8_t hash[info->crypto->key_len];
531 	int ndepth, noffset;
532 	int sig_node, node;
533 	char name[100];
534 	int ret;
535 
536 	/*
537 	 * Verify that the checksum-length does not exceed the
538 	 * rsa-signature-length
539 	 */
540 	if (info->checksum->checksum_len >
541 	    info->crypto->key_len) {
542 		debug("%s: invlaid checksum-algorithm %s for %s\n",
543 		      __func__, info->checksum->name, info->crypto->name);
544 		return -EINVAL;
545 	}
546 
547 	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
548 	if (sig_node < 0) {
549 		debug("%s: No signature node found\n", __func__);
550 		return -ENOENT;
551 	}
552 
553 	/* Calculate checksum with checksum-algorithm */
554 	ret = info->checksum->calculate(info->checksum->name,
555 					region, region_count, hash);
556 	if (ret < 0) {
557 		debug("%s: Error in checksum calculation\n", __func__);
558 		return -EINVAL;
559 	}
560 
561 	/* See if we must use a particular key */
562 	if (info->required_keynode != -1) {
563 		ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
564 			info->required_keynode);
565 		if (!ret)
566 			return ret;
567 	}
568 
569 	/* Look for a key that matches our hint */
570 	snprintf(name, sizeof(name), "key-%s", info->keyname);
571 	node = fdt_subnode_offset(blob, sig_node, name);
572 	ret = rsa_verify_with_keynode(info, hash, sig, sig_len, node);
573 	if (!ret)
574 		return ret;
575 
576 	/* No luck, so try each of the keys in turn */
577 	for (ndepth = 0, noffset = fdt_next_node(info->fit, sig_node, &ndepth);
578 			(noffset >= 0) && (ndepth > 0);
579 			noffset = fdt_next_node(info->fit, noffset, &ndepth)) {
580 		if (ndepth == 1 && noffset != node) {
581 			ret = rsa_verify_with_keynode(info, hash, sig, sig_len,
582 						      noffset);
583 			if (!ret)
584 				break;
585 		}
586 	}
587 
588 	return ret;
589 }
590 
591 #if !defined(USE_HOSTCC)
592 #ifdef CONFIG_SPL_FIT_HW_CRYPTO
593 int rsa_burn_key_hash(struct image_sign_info *info)
594 {
595 	char *rsa_key;
596 	void *n, *e, *c;
597 	uint32_t key_len;
598 	struct udevice *dev;
599 	struct key_prop prop;
600 	char name[100] = {0};
601 	u16 secure_boot_enable = 0;
602 	const void *blob = info->fdt_blob;
603 	uint8_t digest[FIT_MAX_HASH_LEN];
604 	uint8_t digest_read[FIT_MAX_HASH_LEN];
605 	int sig_node, node, digest_len, i, ret = 0;
606 
607 	dev = misc_otp_get_device(OTP_S);
608 	if (!dev)
609 		return -ENODEV;
610 
611 	ret = misc_otp_read(dev, OTP_SECURE_BOOT_ENABLE_ADDR,
612 			    &secure_boot_enable, OTP_SECURE_BOOT_ENABLE_SIZE);
613 	if (ret)
614 		return ret;
615 
616 	if (secure_boot_enable)
617 		return 0;
618 
619 	sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME);
620 	if (sig_node < 0) {
621 		debug("%s: No signature node found\n", __func__);
622 		return -ENOENT;
623 	}
624 
625 	snprintf(name, sizeof(name), "key-%s", info->keyname);
626 	node = fdt_subnode_offset(blob, sig_node, name);
627 
628 	if (rsa_get_key_prop(&prop, info, node))
629 		return -1;
630 
631 	if (!(prop.burn_key))
632 		return -EPERM;
633 
634 	if (!prop.hash || !prop.modulus || !prop.public_exponent_BN)
635 		return -ENOENT;
636 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
637 	if (!prop.factor_c)
638 		return -ENOENT;
639 #else
640 	if (!prop.factor_np)
641 		return -ENOENT;
642 #endif
643 	key_len = info->crypto->key_len;
644 	if (info->crypto->key_len != RSA2048_BYTES)
645 		return -EINVAL;
646 
647 	rsa_key = calloc(key_len * 3, sizeof(char));
648 	if (!rsa_key)
649 		return -ENOMEM;
650 
651 	n = rsa_key;
652 	e = rsa_key + CONFIG_RSA_N_SIZE;
653 	c = rsa_key + CONFIG_RSA_N_SIZE + CONFIG_RSA_E_SIZE;
654 	rsa_convert_big_endian(n, (uint32_t *)prop.modulus,
655 			       key_len, CONFIG_RSA_N_SIZE);
656 	rsa_convert_big_endian(e, (uint32_t *)prop.public_exponent_BN,
657 			       key_len, CONFIG_RSA_E_SIZE);
658 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
659 	rsa_convert_big_endian(c, (uint32_t *)prop.factor_c,
660 			       key_len, CONFIG_RSA_C_SIZE);
661 #else
662 	rsa_convert_big_endian(c, (uint32_t *)prop.factor_np,
663 			       key_len, CONFIG_RSA_C_SIZE);
664 #endif
665 
666 	ret = calculate_hash(rsa_key, CONFIG_RSA_N_SIZE + CONFIG_RSA_E_SIZE + CONFIG_RSA_C_SIZE,
667 			     info->checksum->name, digest, &digest_len);
668 	if (ret)
669 		goto error;
670 
671 	if (memcmp(digest, prop.hash, digest_len) != 0) {
672 		printf("RSA: Compare public key hash fail.\n");
673 		goto error;
674 	}
675 
676 	/* burn key hash here */
677 	ret = misc_otp_read(dev, OTP_RSA_HASH_ADDR, digest_read, OTP_RSA_HASH_SIZE);
678 	if (ret)
679 		goto error;
680 
681 	for (i = 0; i < OTP_RSA_HASH_SIZE; i++) {
682 		if (digest_read[i]) {
683 			printf("RSA: The secure region has been written.\n");
684 			ret = -EIO;
685 			goto error;
686 		}
687 	}
688 
689 	ret = misc_otp_write(dev, OTP_RSA_HASH_ADDR, digest, OTP_RSA_HASH_SIZE);
690 	if (ret)
691 		goto error;
692 
693 	memset(digest_read, 0, FIT_MAX_HASH_LEN);
694 	ret = misc_otp_read(dev, OTP_RSA_HASH_ADDR, digest_read, OTP_RSA_HASH_SIZE);
695 	if (ret)
696 		goto error;
697 
698 	if (memcmp(digest, digest_read, digest_len) != 0) {
699 		printf("RSA: Write public key hash fail.\n");
700 		goto error;
701 	}
702 
703 	secure_boot_enable = 0xff;
704 	ret = misc_otp_write(dev, OTP_SECURE_BOOT_ENABLE_ADDR,
705 			     &secure_boot_enable, OTP_SECURE_BOOT_ENABLE_SIZE);
706 	if (ret)
707 		goto error;
708 
709 	printf("RSA: Write key hash successfully\n");
710 
711 error:
712 	free(rsa_key);
713 
714 	return ret;
715 }
716 #endif
717 #endif
718