1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2015 Freescale Semiconductor, Inc.
10 #include <fsl_validate.h>
11 #include <fsl_secboot_err.h>
17 #include <u-boot/rsa-mod-exp.h>
19 #include <fsl_secboot_err.h>
20 #ifdef CONFIG_ARCH_LS1021A
21 #include <asm/arch/immap_ls102xa.h>
24 #define SHA256_BITS 256
25 #define SHA256_BYTES (256/8)
26 #define SHA256_NIBBLES (256/4)
27 #define NUM_HEX_CHARS (sizeof(ulong) * 2)
29 #define CHECK_KEY_LEN(key_len) (((key_len) == 2 * KEY_SIZE_BYTES / 4) || \
30 ((key_len) == 2 * KEY_SIZE_BYTES / 2) || \
31 ((key_len) == 2 * KEY_SIZE_BYTES))
32 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
33 /* Global data structure */
34 static struct fsl_secboot_glb glb;
37 /* This array contains DER value for SHA-256 */
38 static const u8 hash_identifier[] = { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60,
39 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00,
43 static u8 hash_val[SHA256_BYTES];
45 #ifdef CONFIG_ESBC_HDR_LS
46 /* New Barker Code for LS ESBC Header */
47 static const u8 barker_code[ESBC_BARKER_LEN] = { 0x12, 0x19, 0x20, 0x01 };
49 static const u8 barker_code[ESBC_BARKER_LEN] = { 0x68, 0x39, 0x27, 0x81 };
52 void branch_to_self(void) __attribute__ ((noreturn));
55 * This function will put core in infinite loop.
56 * This will be called when the ESBC can not proceed further due
57 * to some unknown errors.
59 void branch_to_self(void)
61 printf("Core is in infinite loop due to errors.\n");
66 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
67 static u32 check_ie(struct fsl_secboot_img_priv *img)
69 if (img->hdr.ie_flag & IE_FLAG_MASK)
75 /* This function returns the CSF Header Address of uboot
76 * For MPC85xx based platforms, the LAW mapping for NOR
77 * flash changes in uboot code. Hence the offset needs
78 * to be calculated and added to the new NOR flash base
81 #if defined(CONFIG_MPC85xx)
82 int get_csf_base_addr(u32 *csf_addr, u32 *flash_base_addr)
84 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
85 u32 csf_hdr_addr = in_be32(&gur->scratchrw[0]);
86 u32 csf_flash_offset = csf_hdr_addr & ~(CONFIG_SYS_PBI_FLASH_BASE);
91 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
92 flash_addr = flash_info[i].start[0];
93 addr = flash_info[i].start[0] + csf_flash_offset;
94 if (memcmp((u8 *)addr, barker_code, ESBC_BARKER_LEN) == 0) {
95 debug("Barker found on addr %x\n", addr);
105 *flash_base_addr = flash_addr;
110 /* For platforms like LS1020, correct flash address is present in
111 * the header. So the function reqturns flash base address as 0
113 int get_csf_base_addr(u32 *csf_addr, u32 *flash_base_addr)
115 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
116 u32 csf_hdr_addr = in_be32(&gur->scratchrw[0]);
118 if (memcmp((u8 *)(uintptr_t)csf_hdr_addr,
119 barker_code, ESBC_BARKER_LEN))
122 *csf_addr = csf_hdr_addr;
123 *flash_base_addr = 0;
128 #if defined(CONFIG_ESBC_HDR_LS)
129 static int get_ie_info_addr(uintptr_t *ie_addr)
131 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
132 /* For LS-CH3, the address of IE Table is
133 * stated in Scratch13 and scratch14 of DCFG.
134 * Bootrom validates this table while validating uboot.
136 *ie_addr = in_le32(&gur->scratchrw[SCRATCH_IE_HIGH_ADR - 1]);
137 *ie_addr = *ie_addr << 32;
138 *ie_addr |= in_le32(&gur->scratchrw[SCRATCH_IE_LOW_ADR - 1]);
141 #else /* CONFIG_ESBC_HDR_LS */
142 static int get_ie_info_addr(uintptr_t *ie_addr)
144 struct fsl_secboot_img_hdr *hdr;
145 struct fsl_secboot_sg_table *sg_tbl;
146 u32 flash_base_addr, csf_addr;
148 if (get_csf_base_addr(&csf_addr, &flash_base_addr))
151 hdr = (struct fsl_secboot_img_hdr *)(uintptr_t)csf_addr;
153 /* For SoC's with Trust Architecture v1 with corenet bus
154 * the sg table field in CSF header has absolute address
155 * for sg table in memory. In other Trust Architecture,
156 * this field specifies the offset of sg table from the
157 * base address of CSF Header
159 #if defined(CONFIG_FSL_TRUST_ARCH_v1) && defined(CONFIG_FSL_CORENET)
160 sg_tbl = (struct fsl_secboot_sg_table *)
161 (((u32)hdr->psgtable & ~(CONFIG_SYS_PBI_FLASH_BASE)) +
164 sg_tbl = (struct fsl_secboot_sg_table *)(uintptr_t)(csf_addr +
168 /* IE Key Table is the first entry in the SG Table */
169 #if defined(CONFIG_MPC85xx)
170 *ie_addr = (uintptr_t)((sg_tbl->src_addr &
171 ~(CONFIG_SYS_PBI_FLASH_BASE)) +
174 *ie_addr = (uintptr_t)sg_tbl->src_addr;
177 debug("IE Table address is %lx\n", *ie_addr);
180 #endif /* CONFIG_ESBC_HDR_LS */
183 #ifdef CONFIG_KEY_REVOCATION
184 /* This function checks srk_table_flag in header and set/reset srk_flag.*/
185 static u32 check_srk(struct fsl_secboot_img_priv *img)
187 #ifdef CONFIG_ESBC_HDR_LS
188 /* In LS, No SRK Flag as SRK is always present if IE not present*/
189 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
190 return !check_ie(img);
194 if (img->hdr.len_kr.srk_table_flag & SRK_FLAG)
201 /* This function returns ospr's key_revoc values.*/
202 static u32 get_key_revoc(void)
204 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
205 return (sfp_in32(&sfp_regs->ospr) & OSPR_KEY_REVOC_MASK) >>
206 OSPR_KEY_REVOC_SHIFT;
209 /* This function checks if selected key is revoked or not.*/
210 static u32 is_key_revoked(u32 keynum, u32 rev_flag)
212 if (keynum == UNREVOCABLE_KEY)
215 if ((u32)(1 << (ALIGN_REVOC_KEY - keynum)) & rev_flag)
221 /* It read validates srk_table key lengths.*/
222 static u32 read_validate_srk_tbl(struct fsl_secboot_img_priv *img)
225 u32 ret, key_num, key_revoc_flag, size;
226 struct fsl_secboot_img_hdr *hdr = &img->hdr;
227 void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
229 if ((hdr->len_kr.num_srk == 0) ||
230 (hdr->len_kr.num_srk > MAX_KEY_ENTRIES))
231 return ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY;
233 key_num = hdr->len_kr.srk_sel;
234 if (key_num == 0 || key_num > hdr->len_kr.num_srk)
235 return ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM;
237 /* Get revoc key from sfp */
238 key_revoc_flag = get_key_revoc();
239 ret = is_key_revoked(key_num, key_revoc_flag);
241 return ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED;
243 size = hdr->len_kr.num_srk * sizeof(struct srk_table);
245 memcpy(&img->srk_tbl, esbc + hdr->srk_tbl_off, size);
247 for (i = 0; i < hdr->len_kr.num_srk; i++) {
248 if (!CHECK_KEY_LEN(img->srk_tbl[i].key_len))
249 return ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN;
252 img->key_len = img->srk_tbl[key_num - 1].key_len;
254 memcpy(&img->img_key, &(img->srk_tbl[key_num - 1].pkey),
261 #ifndef CONFIG_ESBC_HDR_LS
262 static u32 read_validate_single_key(struct fsl_secboot_img_priv *img)
264 struct fsl_secboot_img_hdr *hdr = &img->hdr;
265 void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
267 /* check key length */
268 if (!CHECK_KEY_LEN(hdr->key_len))
269 return ERROR_ESBC_CLIENT_HEADER_KEY_LEN;
271 memcpy(&img->img_key, esbc + hdr->pkey, hdr->key_len);
273 img->key_len = hdr->key_len;
277 #endif /* CONFIG_ESBC_HDR_LS */
279 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
281 static void install_ie_tbl(uintptr_t ie_tbl_addr,
282 struct fsl_secboot_img_priv *img)
284 /* Copy IE tbl to Global Data */
285 memcpy(&glb.ie_tbl, (u8 *)ie_tbl_addr, sizeof(struct ie_key_info));
286 img->ie_addr = (uintptr_t)&glb.ie_tbl;
287 glb.ie_addr = img->ie_addr;
290 static u32 read_validate_ie_tbl(struct fsl_secboot_img_priv *img)
292 struct fsl_secboot_img_hdr *hdr = &img->hdr;
293 u32 ie_key_len, ie_revoc_flag, ie_num;
294 struct ie_key_info *ie_info;
297 if (get_ie_info_addr(&img->ie_addr))
298 return ERROR_IE_TABLE_NOT_FOUND;
300 install_ie_tbl(img->ie_addr, img);
303 ie_info = (struct ie_key_info *)(uintptr_t)img->ie_addr;
304 if (ie_info->num_keys == 0 || ie_info->num_keys > 32)
305 return ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY;
307 ie_num = hdr->ie_key_sel;
308 if (ie_num == 0 || ie_num > ie_info->num_keys)
309 return ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM;
311 ie_revoc_flag = ie_info->key_revok;
312 if ((u32)(1 << (ie_num - 1)) & ie_revoc_flag)
313 return ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED;
315 ie_key_len = ie_info->ie_key_tbl[ie_num - 1].key_len;
317 if (!CHECK_KEY_LEN(ie_key_len))
318 return ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN;
320 memcpy(&img->img_key, &(ie_info->ie_key_tbl[ie_num - 1].pkey),
323 img->key_len = ie_key_len;
329 /* This function return length of public key.*/
330 static inline u32 get_key_len(struct fsl_secboot_img_priv *img)
336 * Handles the ESBC uboot client header verification failure.
337 * This function handles all the errors which might occur in the
338 * parsing and checking of ESBC uboot client header. It will also
339 * set the error bits in the SEC_MON.
341 static void fsl_secboot_header_verification_failure(void)
343 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
345 /* 29th bit of OSPR is ITS */
346 u32 its = sfp_in32(&sfp_regs->ospr) >> 2;
349 set_sec_mon_state(HPSR_SSM_ST_SOFT_FAIL);
351 set_sec_mon_state(HPSR_SSM_ST_NON_SECURE);
353 printf("Generating reset request\n");
354 do_reset(NULL, 0, 0, NULL);
355 /* If reset doesn't coocur, halt execution */
356 do_esbc_halt(NULL, 0, 0, NULL);
360 * Handles the ESBC uboot client image verification failure.
361 * This function handles all the errors which might occur in the
362 * public key hash comparison and signature verification of
363 * ESBC uboot client image. It will also
364 * set the error bits in the SEC_MON.
366 static void fsl_secboot_image_verification_failure(void)
368 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
370 u32 its = (sfp_in32(&sfp_regs->ospr) & ITS_MASK) >> ITS_BIT;
373 set_sec_mon_state(HPSR_SSM_ST_SOFT_FAIL);
375 printf("Generating reset request\n");
376 do_reset(NULL, 0, 0, NULL);
377 /* If reset doesn't coocur, halt execution */
378 do_esbc_halt(NULL, 0, 0, NULL);
381 set_sec_mon_state(HPSR_SSM_ST_NON_SECURE);
385 static void fsl_secboot_bootscript_parse_failure(void)
387 fsl_secboot_header_verification_failure();
391 * Handles the errors in esbc boot.
392 * This function handles all the errors which might occur in the
393 * esbc boot phase. It will call the appropriate api to log the
394 * errors and set the error bits in the SEC_MON.
396 void fsl_secboot_handle_error(int error)
398 #ifndef CONFIG_SPL_BUILD
399 const struct fsl_secboot_errcode *e;
401 for (e = fsl_secboot_errcodes; e->errcode != ERROR_ESBC_CLIENT_MAX;
403 if (e->errcode == error)
404 printf("ERROR :: %x :: %s\n", error, e->name);
407 printf("ERROR :: %x\n", error);
410 /* If Boot Mode is secure, transition the SNVS state and issue
411 * reset based on type of failure and ITS setting.
412 * If Boot mode is non-secure, return from this function.
414 if (fsl_check_boot_mode_secure() == 0)
418 case ERROR_ESBC_CLIENT_HEADER_BARKER:
419 case ERROR_ESBC_CLIENT_HEADER_IMG_SIZE:
420 case ERROR_ESBC_CLIENT_HEADER_KEY_LEN:
421 case ERROR_ESBC_CLIENT_HEADER_SIG_LEN:
422 case ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN:
423 case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1:
424 case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2:
425 case ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD:
426 case ERROR_ESBC_CLIENT_HEADER_SG_ESBC_EP:
427 case ERROR_ESBC_CLIENT_HEADER_SG_ENTIRES_BAD:
428 case ERROR_KEY_TABLE_NOT_FOUND:
429 #ifdef CONFIG_KEY_REVOCATION
430 case ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED:
431 case ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY:
432 case ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM:
433 case ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN:
435 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
437 case ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED:
438 case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY:
439 case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM:
440 case ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN:
441 case ERROR_IE_TABLE_NOT_FOUND:
443 fsl_secboot_header_verification_failure();
445 case ERROR_ESBC_SEC_RESET:
446 case ERROR_ESBC_SEC_DEQ:
447 case ERROR_ESBC_SEC_ENQ:
448 case ERROR_ESBC_SEC_DEQ_TO:
449 case ERROR_ESBC_SEC_JOBQ_STATUS:
450 case ERROR_ESBC_CLIENT_HASH_COMPARE_KEY:
451 case ERROR_ESBC_CLIENT_HASH_COMPARE_EM:
452 fsl_secboot_image_verification_failure();
454 case ERROR_ESBC_MISSING_BOOTM:
455 fsl_secboot_bootscript_parse_failure();
457 case ERROR_ESBC_WRONG_CMD:
464 static void fsl_secblk_handle_error(int error)
467 case ERROR_ESBC_SEC_ENQ:
468 fsl_secboot_handle_error(ERROR_ESBC_SEC_ENQ);
470 case ERROR_ESBC_SEC_DEQ:
471 fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ);
473 case ERROR_ESBC_SEC_DEQ_TO:
474 fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ_TO);
477 printf("Job Queue Output status %x\n", error);
478 fsl_secboot_handle_error(ERROR_ESBC_SEC_JOBQ_STATUS);
484 * Calculate hash of key obtained via offset present in ESBC uboot
485 * client hdr. This function calculates the hash of key which is obtained
486 * through offset present in ESBC uboot client header.
488 static int calc_img_key_hash(struct fsl_secboot_img_priv *img)
490 struct hash_algo *algo;
494 const char *algo_name = "sha256";
496 /* Calculate hash of the esbc key */
497 ret = hash_progressive_lookup_algo(algo_name, &algo);
501 ret = algo->hash_init(algo, &ctx);
504 /* Update hash for ESBC key */
505 #ifdef CONFIG_KEY_REVOCATION
506 if (check_srk(img)) {
507 ret = algo->hash_update(algo, ctx,
508 (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
509 img->hdr.len_kr.num_srk * sizeof(struct srk_table), 1);
514 ret = algo->hash_update(algo, ctx,
515 img->img_key, img->key_len, 1);
518 /* Copy hash at destination buffer */
519 ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
524 for (i = 0; i < SHA256_BYTES; i++)
525 img->img_key_hash[i] = hash_val[i];
531 * Calculate hash of ESBC hdr and ESBC. This function calculates the
532 * single hash of ESBC header and ESBC image. If SG flag is on, all
533 * SG entries are also hashed alongwith the complete SG table.
535 static int calc_esbchdr_esbc_hash(struct fsl_secboot_img_priv *img)
537 struct hash_algo *algo;
541 const char *algo_name = "sha256";
543 /* Calculate the hash of the ESBC */
544 ret = hash_progressive_lookup_algo(algo_name, &algo);
548 ret = algo->hash_init(algo, &ctx);
549 /* Copy hash at destination buffer */
553 /* Update hash for CSF Header */
554 ret = algo->hash_update(algo, ctx,
555 (u8 *)&img->hdr, sizeof(struct fsl_secboot_img_hdr), 0);
559 /* Update the hash with that of srk table if srk flag is 1
560 * If IE Table is selected, key is not added in the hash
561 * If neither srk table nor IE key table available, add key
562 * from header in the hash calculation
564 #ifdef CONFIG_KEY_REVOCATION
565 if (check_srk(img)) {
566 ret = algo->hash_update(algo, ctx,
567 (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
568 img->hdr.len_kr.num_srk * sizeof(struct srk_table), 0);
572 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
573 if (!key_hash && check_ie(img))
576 #ifndef CONFIG_ESBC_HDR_LS
577 /* No single key support in LS ESBC header */
579 ret = algo->hash_update(algo, ctx,
580 img->img_key, img->hdr.key_len, 0);
588 return ERROR_KEY_TABLE_NOT_FOUND;
590 /* Update hash for actual Image */
591 ret = algo->hash_update(algo, ctx,
592 (u8 *)(*(img->img_addr_ptr)), img->img_size, 1);
596 /* Copy hash at destination buffer */
597 ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
606 * Construct encoded hash EM' wrt PKCSv1.5. This function calculates the
607 * pointers for padding, DER value and hash. And finally, constructs EM'
608 * which includes hash of complete CSF header and ESBC image. If SG flag
609 * is on, hash of SG table and entries is also included.
611 static void construct_img_encoded_hash_second(struct fsl_secboot_img_priv *img)
614 * RSA PKCSv1.5 encoding format for encoded message is below
615 * EM = 0x0 || 0x1 || PS || 0x0 || DER || Hash
616 * PS is Padding String
617 * DER is DER value for SHA-256
618 * Hash is SHA-256 hash
619 * *********************************************************
620 * representative points to first byte of EM initially and is
622 * representative is incremented by 1 and second byte is filled
624 * padding points to third byte of EM
625 * digest points to full length of EM - 32 bytes
626 * hash_id (DER value) points to 19 bytes before pDigest
627 * separator is one byte which separates padding and DER
632 u8 *padding, *digest;
633 u8 *hash_id, *separator;
636 len = (get_key_len(img) / 2) - 1;
637 representative = img->img_encoded_hash_second;
638 representative[0] = 0;
639 representative[1] = 1; /* block type 1 */
641 padding = &representative[2];
642 digest = &representative[1] + len - 32;
643 hash_id = digest - sizeof(hash_identifier);
644 separator = hash_id - 1;
646 /* fill padding area pointed by padding with 0xff */
647 memset(padding, 0xff, separator - padding);
649 /* fill byte pointed by separator */
652 /* fill SHA-256 DER value pointed by HashId */
653 memcpy(hash_id, hash_identifier, sizeof(hash_identifier));
655 /* fill hash pointed by Digest */
656 for (i = 0; i < SHA256_BYTES; i++)
657 digest[i] = hash_val[i];
661 * Reads and validates the ESBC client header.
662 * This function reads key and signature from the ESBC client header.
663 * If Scatter/Gather flag is on, lengths and offsets of images
664 * present as SG entries are also read. This function also checks
665 * whether the header is valid or not.
667 static int read_validate_esbc_client_header(struct fsl_secboot_img_priv *img)
669 struct fsl_secboot_img_hdr *hdr = &img->hdr;
670 void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
676 /* check barker code */
677 if (memcmp(hdr->barker, barker_code, ESBC_BARKER_LEN))
678 return ERROR_ESBC_CLIENT_HEADER_BARKER;
680 /* If Image Address is not passed as argument to function,
681 * then Address and Size must be read from the Header.
683 if (*(img->img_addr_ptr) == 0) {
684 #ifdef CONFIG_ESBC_ADDR_64BIT
685 *(img->img_addr_ptr) = hdr->pimg64;
687 *(img->img_addr_ptr) = hdr->pimg;
692 return ERROR_ESBC_CLIENT_HEADER_IMG_SIZE;
694 img->img_size = hdr->img_size;
697 #ifdef CONFIG_KEY_REVOCATION
698 if (check_srk(img)) {
699 ret = read_validate_srk_tbl(img);
706 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
707 if (!key_found && check_ie(img)) {
708 ret = read_validate_ie_tbl(img);
714 #ifndef CONFIG_ESBC_HDR_LS
715 /* Single Key Feature not available in LS ESBC Header */
716 if (key_found == 0) {
717 ret = read_validate_single_key(img);
724 return ERROR_KEY_TABLE_NOT_FOUND;
727 if (get_key_len(img) == 2 * hdr->sign_len) {
728 /* check signature length */
729 if (!((hdr->sign_len == KEY_SIZE_BYTES / 4) ||
730 (hdr->sign_len == KEY_SIZE_BYTES / 2) ||
731 (hdr->sign_len == KEY_SIZE_BYTES)))
732 return ERROR_ESBC_CLIENT_HEADER_SIG_LEN;
734 return ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN;
737 memcpy(&img->img_sign, esbc + hdr->psign, hdr->sign_len);
738 /* No SG support in LS-CH3 */
739 #ifndef CONFIG_ESBC_HDR_LS
742 return ERROR_ESBC_CLIENT_HEADER_SG;
745 /* modulus most significant bit should be set */
746 k = (u8 *)&img->img_key;
748 if ((k[0] & 0x80) == 0)
749 return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1;
751 /* modulus value should be odd */
752 if ((k[get_key_len(img) / 2 - 1] & 0x1) == 0)
753 return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2;
755 /* Check signature value < modulus value */
756 s = (u8 *)&img->img_sign;
758 if (!(memcmp(s, k, hdr->sign_len) < 0))
759 return ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD;
761 return ESBC_VALID_HDR;
764 static inline int str2longbe(const char *p, ulong *num)
772 tmp = hextoul(p, &endptr);
773 if (sizeof(ulong) == 4)
774 *num = cpu_to_be32(tmp);
776 *num = cpu_to_be64(tmp);
779 return *p != '\0' && *endptr == '\0';
781 /* Function to calculate the ESBC Image Hash
782 * and hash from Digital signature.
783 * The Two hash's are compared to yield the
784 * result of signature validation.
786 static int calculate_cmp_img_sig(struct fsl_secboot_img_priv *img)
790 struct key_prop prop;
791 #if !defined(USE_HOSTCC)
792 struct udevice *mod_exp_dev;
794 ret = calc_esbchdr_esbc_hash(img);
798 /* Construct encoded hash EM' wrt PKCSv1.5 */
799 construct_img_encoded_hash_second(img);
801 /* Fill prop structure for public key */
802 memset(&prop, 0, sizeof(struct key_prop));
803 key_len = get_key_len(img) / 2;
804 prop.modulus = img->img_key;
805 prop.public_exponent = img->img_key + key_len;
806 prop.num_bits = key_len * 8;
807 prop.exp_len = key_len;
809 ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
811 printf("RSA: Can't find Modular Exp implementation\n");
815 ret = rsa_mod_exp(mod_exp_dev, img->img_sign, img->hdr.sign_len,
816 &prop, img->img_encoded_hash);
821 * compare the encoded messages EM' and EM wrt RSA PKCSv1.5
822 * memcmp returns zero on success
823 * memcmp returns non-zero on failure
825 ret = memcmp(&img->img_encoded_hash_second, &img->img_encoded_hash,
829 return ERROR_ESBC_CLIENT_HASH_COMPARE_EM;
833 /* Function to initialize img priv and global data structure
835 static int secboot_init(struct fsl_secboot_img_priv **img_ptr)
837 *img_ptr = malloc(sizeof(struct fsl_secboot_img_priv));
839 struct fsl_secboot_img_priv *img = *img_ptr;
843 memset(img, 0, sizeof(struct fsl_secboot_img_priv));
845 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
847 img->ie_addr = glb.ie_addr;
853 /* haddr - Address of the header of image to be validated.
854 * arg_hash_str - Option hash string. If provided, this
855 * overrides the key hash in the SFP fuses.
856 * img_addr_ptr - Optional pointer to address of image to be validated.
857 * If non zero addr, this overrides the addr of image in header,
858 * otherwise updated to image addr in header.
859 * Acts as both input and output of function.
860 * This pointer shouldn't be NULL.
862 int fsl_secboot_validate(uintptr_t haddr, char *arg_hash_str,
863 uintptr_t *img_addr_ptr)
865 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
866 ulong hash[SHA256_BYTES/sizeof(ulong)];
867 char hash_str[NUM_HEX_CHARS + 1];
868 struct fsl_secboot_img_priv *img;
869 struct fsl_secboot_img_hdr *hdr;
871 int ret, i, hash_cmd = 0;
874 if (strlen(arg_hash_str) != 0) {
875 const char *cp = arg_hash_str;
878 if (*cp == '0' && *(cp + 1) == 'x')
881 /* The input string expected is in hex, where
882 * each 4 bits would be represented by a hex
883 * sha256 hash is 256 bits long, which would mean
884 * num of characters = 256 / 4
886 if (strlen(cp) != SHA256_NIBBLES) {
887 printf("%s is not a 256 bits hex string as expected\n",
892 for (i = 0; i < sizeof(hash)/sizeof(ulong); i++) {
893 strncpy(hash_str, cp + (i * NUM_HEX_CHARS),
895 hash_str[NUM_HEX_CHARS] = '\0';
896 if (!str2longbe(hash_str, &hash[i])) {
897 printf("%s is not a 256 bits hex string ",
906 ret = secboot_init(&img);
910 /* Update the information in Private Struct */
912 img->ehdrloc = haddr;
913 img->img_addr_ptr = img_addr_ptr;
914 esbc = (u8 *)img->ehdrloc;
916 memcpy(hdr, esbc, sizeof(struct fsl_secboot_img_hdr));
918 /* read and validate esbc header */
919 ret = read_validate_esbc_client_header(img);
921 if (ret != ESBC_VALID_HDR) {
922 fsl_secboot_handle_error(ret);
926 /* SRKH present in SFP */
927 for (i = 0; i < NUM_SRKH_REGS; i++)
928 srk_hash[i] = srk_in32(&sfp_regs->srk_hash[i]);
931 * Calculate hash of key obtained via offset present in
932 * ESBC uboot client hdr
934 ret = calc_img_key_hash(img);
936 fsl_secblk_handle_error(ret);
940 /* Compare hash obtained above with SRK hash present in SFP */
942 ret = memcmp(&hash, &img->img_key_hash, SHA256_BYTES);
944 ret = memcmp(srk_hash, img->img_key_hash, SHA256_BYTES);
946 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
947 if (!hash_cmd && check_ie(img))
952 fsl_secboot_handle_error(ERROR_ESBC_CLIENT_HASH_COMPARE_KEY);
956 ret = calculate_cmp_img_sig(img);
958 fsl_secboot_handle_error(ret);
963 /* Free Img as it was malloc'ed*/