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