2 * Copyright 2015 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <fsl_validate.h>
9 #include <fsl_secboot_err.h>
14 #include <dm/uclass.h>
15 #include <u-boot/rsa-mod-exp.h>
17 #include <fsl_secboot_err.h>
19 #include <asm/arch/immap_ls102xa.h>
22 #define SHA256_BITS 256
23 #define SHA256_BYTES (256/8)
24 #define SHA256_NIBBLES (256/4)
25 #define NUM_HEX_CHARS (sizeof(ulong) * 2)
27 /* This array contains DER value for SHA-256 */
28 static const u8 hash_identifier[] = { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60,
29 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00,
33 static u8 hash_val[SHA256_BYTES];
34 static const u8 barker_code[ESBC_BARKER_LEN] = { 0x68, 0x39, 0x27, 0x81 };
36 void branch_to_self(void) __attribute__ ((noreturn));
39 * This function will put core in infinite loop.
40 * This will be called when the ESBC can not proceed further due
41 * to some unknown errors.
43 void branch_to_self(void)
45 printf("Core is in infinite loop due to errors.\n");
50 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
51 static u32 check_ie(struct fsl_secboot_img_priv *img)
59 /* This function returns the CSF Header Address of uboot
60 * For MPC85xx based platforms, the LAW mapping for NOR
61 * flash changes in uboot code. Hence the offset needs
62 * to be calculated and added to the new NOR flash base
65 #if defined(CONFIG_MPC85xx)
66 int get_csf_base_addr(u32 *csf_addr, u32 *flash_base_addr)
68 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
69 u32 csf_hdr_addr = in_be32(&gur->scratchrw[0]);
70 u32 csf_flash_offset = csf_hdr_addr & ~(CONFIG_SYS_PBI_FLASH_BASE);
75 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
76 flash_addr = flash_info[i].start[0];
77 addr = flash_info[i].start[0] + csf_flash_offset;
78 if (memcmp((u8 *)addr, barker_code, ESBC_BARKER_LEN) == 0) {
79 debug("Barker found on addr %x\n", addr);
89 *flash_base_addr = flash_addr;
94 /* For platforms like LS1020, correct flash address is present in
95 * the header. So the function reqturns flash base address as 0
97 int get_csf_base_addr(u32 *csf_addr, u32 *flash_base_addr)
99 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
100 u32 csf_hdr_addr = in_be32(&gur->scratchrw[0]);
102 if (memcmp((u8 *)(uintptr_t)csf_hdr_addr,
103 barker_code, ESBC_BARKER_LEN))
106 *csf_addr = csf_hdr_addr;
107 *flash_base_addr = 0;
112 static int get_ie_info_addr(u32 *ie_addr)
114 struct fsl_secboot_img_hdr *hdr;
115 struct fsl_secboot_sg_table *sg_tbl;
116 u32 flash_base_addr, csf_addr;
118 if (get_csf_base_addr(&csf_addr, &flash_base_addr))
121 hdr = (struct fsl_secboot_img_hdr *)(uintptr_t)csf_addr;
123 /* For SoC's with Trust Architecture v1 with corenet bus
124 * the sg table field in CSF header has absolute address
125 * for sg table in memory. In other Trust Architecture,
126 * this field specifies the offset of sg table from the
127 * base address of CSF Header
129 #if defined(CONFIG_FSL_TRUST_ARCH_v1) && defined(CONFIG_FSL_CORENET)
130 sg_tbl = (struct fsl_secboot_sg_table *)
131 (((u32)hdr->psgtable & ~(CONFIG_SYS_PBI_FLASH_BASE)) +
134 sg_tbl = (struct fsl_secboot_sg_table *)(uintptr_t)(csf_addr +
138 /* IE Key Table is the first entry in the SG Table */
139 #if defined(CONFIG_MPC85xx)
140 *ie_addr = (sg_tbl->src_addr & ~(CONFIG_SYS_PBI_FLASH_BASE)) +
143 *ie_addr = sg_tbl->src_addr;
146 debug("IE Table address is %x\n", *ie_addr);
152 #ifdef CONFIG_KEY_REVOCATION
153 /* This function checks srk_table_flag in header and set/reset srk_flag.*/
154 static u32 check_srk(struct fsl_secboot_img_priv *img)
156 if (img->hdr.len_kr.srk_table_flag & SRK_FLAG)
162 /* This function returns ospr's key_revoc values.*/
163 static u32 get_key_revoc(void)
165 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
166 return (sfp_in32(&sfp_regs->ospr) & OSPR_KEY_REVOC_MASK) >>
167 OSPR_KEY_REVOC_SHIFT;
170 /* This function checks if selected key is revoked or not.*/
171 static u32 is_key_revoked(u32 keynum, u32 rev_flag)
173 if (keynum == UNREVOCABLE_KEY)
176 if ((u32)(1 << (ALIGN_REVOC_KEY - keynum)) & rev_flag)
182 /* It validates srk_table key lengths.*/
183 static u32 validate_srk_tbl(struct srk_table *tbl, u32 num_entries)
186 for (i = 0; i < num_entries; i++) {
187 if (!((tbl[i].key_len == 2 * KEY_SIZE_BYTES/4) ||
188 (tbl[i].key_len == 2 * KEY_SIZE_BYTES/2) ||
189 (tbl[i].key_len == 2 * KEY_SIZE_BYTES)))
190 return ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN;
196 /* This function return length of public key.*/
197 static inline u32 get_key_len(struct fsl_secboot_img_priv *img)
203 * Handles the ESBC uboot client header verification failure.
204 * This function handles all the errors which might occur in the
205 * parsing and checking of ESBC uboot client header. It will also
206 * set the error bits in the SEC_MON.
208 static void fsl_secboot_header_verification_failure(void)
210 struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
211 (CONFIG_SYS_SEC_MON_ADDR);
212 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
213 u32 sts = sec_mon_in32(&sec_mon_regs->hp_stat);
215 /* 29th bit of OSPR is ITS */
216 u32 its = sfp_in32(&sfp_regs->ospr) >> 2;
219 * Read the SEC_MON status register
222 sts = sec_mon_in32(&sec_mon_regs->hp_stat);
223 if ((sts & HPSR_SSM_ST_MASK) == HPSR_SSM_ST_TRUST) {
225 change_sec_mon_state(HPSR_SSM_ST_TRUST,
226 HPSR_SSM_ST_SOFT_FAIL);
228 change_sec_mon_state(HPSR_SSM_ST_TRUST,
229 HPSR_SSM_ST_NON_SECURE);
232 printf("Generating reset request\n");
233 do_reset(NULL, 0, 0, NULL);
237 * Handles the ESBC uboot client image verification failure.
238 * This function handles all the errors which might occur in the
239 * public key hash comparison and signature verification of
240 * ESBC uboot client image. It will also
241 * set the error bits in the SEC_MON.
243 static void fsl_secboot_image_verification_failure(void)
245 struct ccsr_sec_mon_regs *sec_mon_regs = (void *)
246 (CONFIG_SYS_SEC_MON_ADDR);
247 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
248 u32 sts = sec_mon_in32(&sec_mon_regs->hp_stat);
250 u32 its = (sfp_in32(&sfp_regs->ospr) & ITS_MASK) >> ITS_BIT;
253 * Read the SEC_MON status register
256 sts = sec_mon_in32(&sec_mon_regs->hp_stat);
257 if ((sts & HPSR_SSM_ST_MASK) == HPSR_SSM_ST_TRUST) {
259 change_sec_mon_state(HPSR_SSM_ST_TRUST,
260 HPSR_SSM_ST_SOFT_FAIL);
262 printf("Generating reset request\n");
263 do_reset(NULL, 0, 0, NULL);
265 change_sec_mon_state(HPSR_SSM_ST_TRUST,
266 HPSR_SSM_ST_NON_SECURE);
271 static void fsl_secboot_bootscript_parse_failure(void)
273 fsl_secboot_header_verification_failure();
277 * Handles the errors in esbc boot.
278 * This function handles all the errors which might occur in the
279 * esbc boot phase. It will call the appropriate api to log the
280 * errors and set the error bits in the SEC_MON.
282 void fsl_secboot_handle_error(int error)
284 const struct fsl_secboot_errcode *e;
286 for (e = fsl_secboot_errcodes; e->errcode != ERROR_ESBC_CLIENT_MAX;
288 if (e->errcode == error)
289 printf("ERROR :: %x :: %s\n", error, e->name);
293 case ERROR_ESBC_CLIENT_HEADER_BARKER:
294 case ERROR_ESBC_CLIENT_HEADER_IMG_SIZE:
295 case ERROR_ESBC_CLIENT_HEADER_KEY_LEN:
296 case ERROR_ESBC_CLIENT_HEADER_SIG_LEN:
297 case ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN:
298 case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1:
299 case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2:
300 case ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD:
301 case ERROR_ESBC_CLIENT_HEADER_SG_ESBC_EP:
302 case ERROR_ESBC_CLIENT_HEADER_SG_ENTIRES_BAD:
303 #ifdef CONFIG_KEY_REVOCATION
304 case ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED:
305 case ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY:
306 case ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM:
307 case ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN:
309 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
311 case ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED:
312 case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY:
313 case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM:
314 case ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN:
315 case ERROR_IE_TABLE_NOT_FOUND:
317 fsl_secboot_header_verification_failure();
319 case ERROR_ESBC_SEC_RESET:
320 case ERROR_ESBC_SEC_DEQ:
321 case ERROR_ESBC_SEC_ENQ:
322 case ERROR_ESBC_SEC_DEQ_TO:
323 case ERROR_ESBC_SEC_JOBQ_STATUS:
324 case ERROR_ESBC_CLIENT_HASH_COMPARE_KEY:
325 case ERROR_ESBC_CLIENT_HASH_COMPARE_EM:
326 fsl_secboot_image_verification_failure();
328 case ERROR_ESBC_MISSING_BOOTM:
329 fsl_secboot_bootscript_parse_failure();
331 case ERROR_ESBC_WRONG_CMD:
338 static void fsl_secblk_handle_error(int error)
341 case ERROR_ESBC_SEC_ENQ:
342 fsl_secboot_handle_error(ERROR_ESBC_SEC_ENQ);
344 case ERROR_ESBC_SEC_DEQ:
345 fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ);
347 case ERROR_ESBC_SEC_DEQ_TO:
348 fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ_TO);
351 printf("Job Queue Output status %x\n", error);
352 fsl_secboot_handle_error(ERROR_ESBC_SEC_JOBQ_STATUS);
358 * Calculate hash of key obtained via offset present in ESBC uboot
359 * client hdr. This function calculates the hash of key which is obtained
360 * through offset present in ESBC uboot client header.
362 static int calc_img_key_hash(struct fsl_secboot_img_priv *img)
364 struct hash_algo *algo;
368 const char *algo_name = "sha256";
370 /* Calculate hash of the esbc key */
371 ret = hash_progressive_lookup_algo(algo_name, &algo);
375 ret = algo->hash_init(algo, &ctx);
379 /* Update hash for ESBC key */
380 #ifdef CONFIG_KEY_REVOCATION
381 if (check_srk(img)) {
382 ret = algo->hash_update(algo, ctx,
383 (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
384 img->hdr.len_kr.num_srk * sizeof(struct srk_table), 1);
389 ret = algo->hash_update(algo, ctx,
390 img->img_key, img->key_len, 1);
394 /* Copy hash at destination buffer */
395 ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
399 for (i = 0; i < SHA256_BYTES; i++)
400 img->img_key_hash[i] = hash_val[i];
406 * Calculate hash of ESBC hdr and ESBC. This function calculates the
407 * single hash of ESBC header and ESBC image. If SG flag is on, all
408 * SG entries are also hashed alongwith the complete SG table.
410 static int calc_esbchdr_esbc_hash(struct fsl_secboot_img_priv *img)
412 struct hash_algo *algo;
416 const char *algo_name = "sha256";
418 /* Calculate the hash of the ESBC */
419 ret = hash_progressive_lookup_algo(algo_name, &algo);
423 ret = algo->hash_init(algo, &ctx);
424 /* Copy hash at destination buffer */
428 /* Update hash for CSF Header */
429 ret = algo->hash_update(algo, ctx,
430 (u8 *)&img->hdr, sizeof(struct fsl_secboot_img_hdr), 0);
434 /* Update the hash with that of srk table if srk flag is 1
435 * If IE Table is selected, key is not added in the hash
436 * If neither srk table nor IE key table available, add key
437 * from header in the hash calculation
439 #ifdef CONFIG_KEY_REVOCATION
440 if (check_srk(img)) {
441 ret = algo->hash_update(algo, ctx,
442 (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
443 img->hdr.len_kr.num_srk * sizeof(struct srk_table), 0);
447 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
448 if (!key_hash && check_ie(img))
452 ret = algo->hash_update(algo, ctx,
453 img->img_key, img->hdr.key_len, 0);
457 /* Update hash for actual Image */
458 #ifdef CONFIG_ESBC_ADDR_64BIT
459 ret = algo->hash_update(algo, ctx,
460 (u8 *)(uintptr_t)img->hdr.pimg64, img->hdr.img_size, 1);
462 ret = algo->hash_update(algo, ctx,
463 (u8 *)(uintptr_t)img->hdr.pimg, img->hdr.img_size, 1);
468 /* Copy hash at destination buffer */
469 ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
477 * Construct encoded hash EM' wrt PKCSv1.5. This function calculates the
478 * pointers for padding, DER value and hash. And finally, constructs EM'
479 * which includes hash of complete CSF header and ESBC image. If SG flag
480 * is on, hash of SG table and entries is also included.
482 static void construct_img_encoded_hash_second(struct fsl_secboot_img_priv *img)
485 * RSA PKCSv1.5 encoding format for encoded message is below
486 * EM = 0x0 || 0x1 || PS || 0x0 || DER || Hash
487 * PS is Padding String
488 * DER is DER value for SHA-256
489 * Hash is SHA-256 hash
490 * *********************************************************
491 * representative points to first byte of EM initially and is
493 * representative is incremented by 1 and second byte is filled
495 * padding points to third byte of EM
496 * digest points to full length of EM - 32 bytes
497 * hash_id (DER value) points to 19 bytes before pDigest
498 * separator is one byte which separates padding and DER
503 u8 *padding, *digest;
504 u8 *hash_id, *separator;
507 len = (get_key_len(img) / 2) - 1;
508 representative = img->img_encoded_hash_second;
509 representative[0] = 0;
510 representative[1] = 1; /* block type 1 */
512 padding = &representative[2];
513 digest = &representative[1] + len - 32;
514 hash_id = digest - sizeof(hash_identifier);
515 separator = hash_id - 1;
517 /* fill padding area pointed by padding with 0xff */
518 memset(padding, 0xff, separator - padding);
520 /* fill byte pointed by separator */
523 /* fill SHA-256 DER value pointed by HashId */
524 memcpy(hash_id, hash_identifier, sizeof(hash_identifier));
526 /* fill hash pointed by Digest */
527 for (i = 0; i < SHA256_BYTES; i++)
528 digest[i] = hash_val[i];
532 * Reads and validates the ESBC client header.
533 * This function reads key and signature from the ESBC client header.
534 * If Scatter/Gather flag is on, lengths and offsets of images
535 * present as SG entries are also read. This function also checks
536 * whether the header is valid or not.
538 static int read_validate_esbc_client_header(struct fsl_secboot_img_priv *img)
541 struct fsl_secboot_img_hdr *hdr = &img->hdr;
542 void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
544 #ifdef CONFIG_KEY_REVOCATION
546 u32 key_num, key_revoc_flag, size;
548 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
549 struct ie_key_info *ie_info;
550 u32 ie_num, ie_revoc_flag, ie_key_len;
554 /* check barker code */
555 if (memcmp(hdr->barker, barker_code, ESBC_BARKER_LEN))
556 return ERROR_ESBC_CLIENT_HEADER_BARKER;
558 #ifdef CONFIG_ESBC_ADDR_64BIT
559 sprintf(buf, "%llx", hdr->pimg64);
561 sprintf(buf, "%x", hdr->pimg);
563 setenv("img_addr", buf);
566 return ERROR_ESBC_CLIENT_HEADER_IMG_SIZE;
569 #ifdef CONFIG_KEY_REVOCATION
570 if (check_srk(img)) {
571 if ((hdr->len_kr.num_srk == 0) ||
572 (hdr->len_kr.num_srk > MAX_KEY_ENTRIES))
573 return ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY;
575 key_num = hdr->len_kr.srk_sel;
576 if (key_num == 0 || key_num > hdr->len_kr.num_srk)
577 return ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM;
579 /* Get revoc key from sfp */
580 key_revoc_flag = get_key_revoc();
581 ret = is_key_revoked(key_num, key_revoc_flag);
583 return ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED;
585 size = hdr->len_kr.num_srk * sizeof(struct srk_table);
587 memcpy(&img->srk_tbl, esbc + hdr->srk_tbl_off, size);
589 ret = validate_srk_tbl(img->srk_tbl, hdr->len_kr.num_srk);
594 img->key_len = img->srk_tbl[key_num - 1].key_len;
596 memcpy(&img->img_key, &(img->srk_tbl[key_num - 1].pkey),
603 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
604 if (!key_found && check_ie(img)) {
605 if (get_ie_info_addr(&img->ie_addr))
606 return ERROR_IE_TABLE_NOT_FOUND;
607 ie_info = (struct ie_key_info *)(uintptr_t)img->ie_addr;
608 if (ie_info->num_keys == 0 || ie_info->num_keys > 32)
609 return ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY;
611 ie_num = hdr->ie_key_sel;
612 if (ie_num == 0 || ie_num > ie_info->num_keys)
613 return ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM;
615 ie_revoc_flag = ie_info->key_revok;
616 if ((u32)(1 << (ie_num - 1)) & ie_revoc_flag)
617 return ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED;
619 ie_key_len = ie_info->ie_key_tbl[ie_num - 1].key_len;
621 if (!((ie_key_len == 2 * KEY_SIZE_BYTES / 4) ||
622 (ie_key_len == 2 * KEY_SIZE_BYTES / 2) ||
623 (ie_key_len == 2 * KEY_SIZE_BYTES)))
624 return ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN;
626 memcpy(&img->img_key, &(ie_info->ie_key_tbl[ie_num - 1].pkey),
629 img->key_len = ie_key_len;
634 if (key_found == 0) {
635 /* check key length */
636 if (!((hdr->key_len == 2 * KEY_SIZE_BYTES / 4) ||
637 (hdr->key_len == 2 * KEY_SIZE_BYTES / 2) ||
638 (hdr->key_len == 2 * KEY_SIZE_BYTES)))
639 return ERROR_ESBC_CLIENT_HEADER_KEY_LEN;
641 memcpy(&img->img_key, esbc + hdr->pkey, hdr->key_len);
643 img->key_len = hdr->key_len;
649 if (get_key_len(img) == 2 * hdr->sign_len) {
650 /* check signature length */
651 if (!((hdr->sign_len == KEY_SIZE_BYTES / 4) ||
652 (hdr->sign_len == KEY_SIZE_BYTES / 2) ||
653 (hdr->sign_len == KEY_SIZE_BYTES)))
654 return ERROR_ESBC_CLIENT_HEADER_SIG_LEN;
656 return ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN;
659 memcpy(&img->img_sign, esbc + hdr->psign, hdr->sign_len);
663 return ERROR_ESBC_CLIENT_HEADER_SG;
665 /* modulus most significant bit should be set */
666 k = (u8 *)&img->img_key;
668 if ((k[0] & 0x80) == 0)
669 return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1;
671 /* modulus value should be odd */
672 if ((k[get_key_len(img) / 2 - 1] & 0x1) == 0)
673 return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2;
675 /* Check signature value < modulus value */
676 s = (u8 *)&img->img_sign;
678 if (!(memcmp(s, k, hdr->sign_len) < 0))
679 return ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD;
681 return ESBC_VALID_HDR;
684 static inline int str2longbe(const char *p, ulong *num)
692 tmp = simple_strtoul(p, &endptr, 16);
693 if (sizeof(ulong) == 4)
694 *num = cpu_to_be32(tmp);
696 *num = cpu_to_be64(tmp);
699 return *p != '\0' && *endptr == '\0';
702 int fsl_secboot_validate(ulong haddr, char *arg_hash_str)
704 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
705 ulong hash[SHA256_BYTES/sizeof(ulong)];
706 char hash_str[NUM_HEX_CHARS + 1];
707 struct fsl_secboot_img_priv *img;
708 struct fsl_secboot_img_hdr *hdr;
710 int ret, i, hash_cmd = 0;
713 struct key_prop prop;
714 #if !defined(USE_HOSTCC)
715 struct udevice *mod_exp_dev;
718 if (arg_hash_str != NULL) {
719 const char *cp = arg_hash_str;
722 if (*cp == '0' && *(cp + 1) == 'x')
725 /* The input string expected is in hex, where
726 * each 4 bits would be represented by a hex
727 * sha256 hash is 256 bits long, which would mean
728 * num of characters = 256 / 4
730 if (strlen(cp) != SHA256_NIBBLES) {
731 printf("%s is not a 256 bits hex string as expected\n",
736 for (i = 0; i < sizeof(hash)/sizeof(ulong); i++) {
737 strncpy(hash_str, cp + (i * NUM_HEX_CHARS),
739 hash_str[NUM_HEX_CHARS] = '\0';
740 if (!str2longbe(hash_str, &hash[i])) {
741 printf("%s is not a 256 bits hex string ",
750 img = malloc(sizeof(struct fsl_secboot_img_priv));
755 memset(img, 0, sizeof(struct fsl_secboot_img_priv));
758 img->ehdrloc = haddr;
759 esbc = (u8 *)(uintptr_t)img->ehdrloc;
761 memcpy(hdr, esbc, sizeof(struct fsl_secboot_img_hdr));
763 /* read and validate esbc header */
764 ret = read_validate_esbc_client_header(img);
766 if (ret != ESBC_VALID_HDR) {
767 fsl_secboot_handle_error(ret);
771 /* SRKH present in SFP */
772 for (i = 0; i < NUM_SRKH_REGS; i++)
773 srk_hash[i] = srk_in32(&sfp_regs->srk_hash[i]);
776 * Calculate hash of key obtained via offset present in
777 * ESBC uboot client hdr
779 ret = calc_img_key_hash(img);
781 fsl_secblk_handle_error(ret);
785 /* Compare hash obtained above with SRK hash present in SFP */
787 ret = memcmp(&hash, &img->img_key_hash, SHA256_BYTES);
789 ret = memcmp(srk_hash, img->img_key_hash, SHA256_BYTES);
791 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
792 if (!hash_cmd && check_ie(img))
797 fsl_secboot_handle_error(ERROR_ESBC_CLIENT_HASH_COMPARE_KEY);
801 ret = calc_esbchdr_esbc_hash(img);
803 fsl_secblk_handle_error(ret);
807 /* Construct encoded hash EM' wrt PKCSv1.5 */
808 construct_img_encoded_hash_second(img);
810 /* Fill prop structure for public key */
811 memset(&prop, 0, sizeof(struct key_prop));
812 key_len = get_key_len(img) / 2;
813 prop.modulus = img->img_key;
814 prop.public_exponent = img->img_key + key_len;
815 prop.num_bits = key_len * 8;
816 prop.exp_len = key_len;
818 ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
820 printf("RSA: Can't find Modular Exp implementation\n");
824 ret = rsa_mod_exp(mod_exp_dev, img->img_sign, img->hdr.sign_len,
825 &prop, img->img_encoded_hash);
827 fsl_secblk_handle_error(ret);
832 * compare the encoded messages EM' and EM wrt RSA PKCSv1.5
833 * memcmp returns zero on success
834 * memcmp returns non-zero on failure
836 ret = memcmp(&img->img_encoded_hash_second, &img->img_encoded_hash,
840 fsl_secboot_handle_error(ERROR_ESBC_CLIENT_HASH_COMPARE_EM);