1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2015 Freescale Semiconductor, Inc.
8 #include <fsl_validate.h>
9 #include <fsl_secboot_err.h>
14 #include <u-boot/rsa-mod-exp.h>
16 #include <fsl_secboot_err.h>
17 #ifdef CONFIG_ARCH_LS1021A
18 #include <asm/arch/immap_ls102xa.h>
21 #define SHA256_BITS 256
22 #define SHA256_BYTES (256/8)
23 #define SHA256_NIBBLES (256/4)
24 #define NUM_HEX_CHARS (sizeof(ulong) * 2)
26 #define CHECK_KEY_LEN(key_len) (((key_len) == 2 * KEY_SIZE_BYTES / 4) || \
27 ((key_len) == 2 * KEY_SIZE_BYTES / 2) || \
28 ((key_len) == 2 * KEY_SIZE_BYTES))
29 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
30 /* Global data structure */
31 static struct fsl_secboot_glb glb;
34 /* This array contains DER value for SHA-256 */
35 static const u8 hash_identifier[] = { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60,
36 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00,
40 static u8 hash_val[SHA256_BYTES];
42 #ifdef CONFIG_ESBC_HDR_LS
43 /* New Barker Code for LS ESBC Header */
44 static const u8 barker_code[ESBC_BARKER_LEN] = { 0x12, 0x19, 0x20, 0x01 };
46 static const u8 barker_code[ESBC_BARKER_LEN] = { 0x68, 0x39, 0x27, 0x81 };
49 void branch_to_self(void) __attribute__ ((noreturn));
52 * This function will put core in infinite loop.
53 * This will be called when the ESBC can not proceed further due
54 * to some unknown errors.
56 void branch_to_self(void)
58 printf("Core is in infinite loop due to errors.\n");
63 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
64 static u32 check_ie(struct fsl_secboot_img_priv *img)
66 if (img->hdr.ie_flag & IE_FLAG_MASK)
72 /* This function returns the CSF Header Address of uboot
73 * For MPC85xx based platforms, the LAW mapping for NOR
74 * flash changes in uboot code. Hence the offset needs
75 * to be calculated and added to the new NOR flash base
78 #if defined(CONFIG_MPC85xx)
79 int get_csf_base_addr(u32 *csf_addr, u32 *flash_base_addr)
81 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
82 u32 csf_hdr_addr = in_be32(&gur->scratchrw[0]);
83 u32 csf_flash_offset = csf_hdr_addr & ~(CONFIG_SYS_PBI_FLASH_BASE);
88 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
89 flash_addr = flash_info[i].start[0];
90 addr = flash_info[i].start[0] + csf_flash_offset;
91 if (memcmp((u8 *)addr, barker_code, ESBC_BARKER_LEN) == 0) {
92 debug("Barker found on addr %x\n", addr);
102 *flash_base_addr = flash_addr;
107 /* For platforms like LS1020, correct flash address is present in
108 * the header. So the function reqturns flash base address as 0
110 int get_csf_base_addr(u32 *csf_addr, u32 *flash_base_addr)
112 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
113 u32 csf_hdr_addr = in_be32(&gur->scratchrw[0]);
115 if (memcmp((u8 *)(uintptr_t)csf_hdr_addr,
116 barker_code, ESBC_BARKER_LEN))
119 *csf_addr = csf_hdr_addr;
120 *flash_base_addr = 0;
125 #if defined(CONFIG_ESBC_HDR_LS)
126 static int get_ie_info_addr(uintptr_t *ie_addr)
128 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
129 /* For LS-CH3, the address of IE Table is
130 * stated in Scratch13 and scratch14 of DCFG.
131 * Bootrom validates this table while validating uboot.
133 *ie_addr = in_le32(&gur->scratchrw[SCRATCH_IE_HIGH_ADR - 1]);
134 *ie_addr = *ie_addr << 32;
135 *ie_addr |= in_le32(&gur->scratchrw[SCRATCH_IE_LOW_ADR - 1]);
138 #else /* CONFIG_ESBC_HDR_LS */
139 static int get_ie_info_addr(uintptr_t *ie_addr)
141 struct fsl_secboot_img_hdr *hdr;
142 struct fsl_secboot_sg_table *sg_tbl;
143 u32 flash_base_addr, csf_addr;
145 if (get_csf_base_addr(&csf_addr, &flash_base_addr))
148 hdr = (struct fsl_secboot_img_hdr *)(uintptr_t)csf_addr;
150 /* For SoC's with Trust Architecture v1 with corenet bus
151 * the sg table field in CSF header has absolute address
152 * for sg table in memory. In other Trust Architecture,
153 * this field specifies the offset of sg table from the
154 * base address of CSF Header
156 #if defined(CONFIG_FSL_TRUST_ARCH_v1) && defined(CONFIG_FSL_CORENET)
157 sg_tbl = (struct fsl_secboot_sg_table *)
158 (((u32)hdr->psgtable & ~(CONFIG_SYS_PBI_FLASH_BASE)) +
161 sg_tbl = (struct fsl_secboot_sg_table *)(uintptr_t)(csf_addr +
165 /* IE Key Table is the first entry in the SG Table */
166 #if defined(CONFIG_MPC85xx)
167 *ie_addr = (uintptr_t)((sg_tbl->src_addr &
168 ~(CONFIG_SYS_PBI_FLASH_BASE)) +
171 *ie_addr = (uintptr_t)sg_tbl->src_addr;
174 debug("IE Table address is %lx\n", *ie_addr);
177 #endif /* CONFIG_ESBC_HDR_LS */
180 #ifdef CONFIG_KEY_REVOCATION
181 /* This function checks srk_table_flag in header and set/reset srk_flag.*/
182 static u32 check_srk(struct fsl_secboot_img_priv *img)
184 #ifdef CONFIG_ESBC_HDR_LS
185 /* In LS, No SRK Flag as SRK is always present if IE not present*/
186 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
187 return !check_ie(img);
191 if (img->hdr.len_kr.srk_table_flag & SRK_FLAG)
198 /* This function returns ospr's key_revoc values.*/
199 static u32 get_key_revoc(void)
201 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
202 return (sfp_in32(&sfp_regs->ospr) & OSPR_KEY_REVOC_MASK) >>
203 OSPR_KEY_REVOC_SHIFT;
206 /* This function checks if selected key is revoked or not.*/
207 static u32 is_key_revoked(u32 keynum, u32 rev_flag)
209 if (keynum == UNREVOCABLE_KEY)
212 if ((u32)(1 << (ALIGN_REVOC_KEY - keynum)) & rev_flag)
218 /* It read validates srk_table key lengths.*/
219 static u32 read_validate_srk_tbl(struct fsl_secboot_img_priv *img)
222 u32 ret, key_num, key_revoc_flag, size;
223 struct fsl_secboot_img_hdr *hdr = &img->hdr;
224 void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
226 if ((hdr->len_kr.num_srk == 0) ||
227 (hdr->len_kr.num_srk > MAX_KEY_ENTRIES))
228 return ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY;
230 key_num = hdr->len_kr.srk_sel;
231 if (key_num == 0 || key_num > hdr->len_kr.num_srk)
232 return ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM;
234 /* Get revoc key from sfp */
235 key_revoc_flag = get_key_revoc();
236 ret = is_key_revoked(key_num, key_revoc_flag);
238 return ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED;
240 size = hdr->len_kr.num_srk * sizeof(struct srk_table);
242 memcpy(&img->srk_tbl, esbc + hdr->srk_tbl_off, size);
244 for (i = 0; i < hdr->len_kr.num_srk; i++) {
245 if (!CHECK_KEY_LEN(img->srk_tbl[i].key_len))
246 return ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN;
249 img->key_len = img->srk_tbl[key_num - 1].key_len;
251 memcpy(&img->img_key, &(img->srk_tbl[key_num - 1].pkey),
258 #ifndef CONFIG_ESBC_HDR_LS
259 static u32 read_validate_single_key(struct fsl_secboot_img_priv *img)
261 struct fsl_secboot_img_hdr *hdr = &img->hdr;
262 void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
264 /* check key length */
265 if (!CHECK_KEY_LEN(hdr->key_len))
266 return ERROR_ESBC_CLIENT_HEADER_KEY_LEN;
268 memcpy(&img->img_key, esbc + hdr->pkey, hdr->key_len);
270 img->key_len = hdr->key_len;
274 #endif /* CONFIG_ESBC_HDR_LS */
276 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
278 static void install_ie_tbl(uintptr_t ie_tbl_addr,
279 struct fsl_secboot_img_priv *img)
281 /* Copy IE tbl to Global Data */
282 memcpy(&glb.ie_tbl, (u8 *)ie_tbl_addr, sizeof(struct ie_key_info));
283 img->ie_addr = (uintptr_t)&glb.ie_tbl;
284 glb.ie_addr = img->ie_addr;
287 static u32 read_validate_ie_tbl(struct fsl_secboot_img_priv *img)
289 struct fsl_secboot_img_hdr *hdr = &img->hdr;
290 u32 ie_key_len, ie_revoc_flag, ie_num;
291 struct ie_key_info *ie_info;
294 if (get_ie_info_addr(&img->ie_addr))
295 return ERROR_IE_TABLE_NOT_FOUND;
297 install_ie_tbl(img->ie_addr, img);
300 ie_info = (struct ie_key_info *)(uintptr_t)img->ie_addr;
301 if (ie_info->num_keys == 0 || ie_info->num_keys > 32)
302 return ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY;
304 ie_num = hdr->ie_key_sel;
305 if (ie_num == 0 || ie_num > ie_info->num_keys)
306 return ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM;
308 ie_revoc_flag = ie_info->key_revok;
309 if ((u32)(1 << (ie_num - 1)) & ie_revoc_flag)
310 return ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED;
312 ie_key_len = ie_info->ie_key_tbl[ie_num - 1].key_len;
314 if (!CHECK_KEY_LEN(ie_key_len))
315 return ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN;
317 memcpy(&img->img_key, &(ie_info->ie_key_tbl[ie_num - 1].pkey),
320 img->key_len = ie_key_len;
326 /* This function return length of public key.*/
327 static inline u32 get_key_len(struct fsl_secboot_img_priv *img)
333 * Handles the ESBC uboot client header verification failure.
334 * This function handles all the errors which might occur in the
335 * parsing and checking of ESBC uboot client header. It will also
336 * set the error bits in the SEC_MON.
338 static void fsl_secboot_header_verification_failure(void)
340 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
342 /* 29th bit of OSPR is ITS */
343 u32 its = sfp_in32(&sfp_regs->ospr) >> 2;
346 set_sec_mon_state(HPSR_SSM_ST_SOFT_FAIL);
348 set_sec_mon_state(HPSR_SSM_ST_NON_SECURE);
350 printf("Generating reset request\n");
351 do_reset(NULL, 0, 0, NULL);
352 /* If reset doesn't coocur, halt execution */
353 do_esbc_halt(NULL, 0, 0, NULL);
357 * Handles the ESBC uboot client image verification failure.
358 * This function handles all the errors which might occur in the
359 * public key hash comparison and signature verification of
360 * ESBC uboot client image. It will also
361 * set the error bits in the SEC_MON.
363 static void fsl_secboot_image_verification_failure(void)
365 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
367 u32 its = (sfp_in32(&sfp_regs->ospr) & ITS_MASK) >> ITS_BIT;
370 set_sec_mon_state(HPSR_SSM_ST_SOFT_FAIL);
372 printf("Generating reset request\n");
373 do_reset(NULL, 0, 0, NULL);
374 /* If reset doesn't coocur, halt execution */
375 do_esbc_halt(NULL, 0, 0, NULL);
378 set_sec_mon_state(HPSR_SSM_ST_NON_SECURE);
382 static void fsl_secboot_bootscript_parse_failure(void)
384 fsl_secboot_header_verification_failure();
388 * Handles the errors in esbc boot.
389 * This function handles all the errors which might occur in the
390 * esbc boot phase. It will call the appropriate api to log the
391 * errors and set the error bits in the SEC_MON.
393 void fsl_secboot_handle_error(int error)
395 #ifndef CONFIG_SPL_BUILD
396 const struct fsl_secboot_errcode *e;
398 for (e = fsl_secboot_errcodes; e->errcode != ERROR_ESBC_CLIENT_MAX;
400 if (e->errcode == error)
401 printf("ERROR :: %x :: %s\n", error, e->name);
404 printf("ERROR :: %x\n", error);
407 /* If Boot Mode is secure, transition the SNVS state and issue
408 * reset based on type of failure and ITS setting.
409 * If Boot mode is non-secure, return from this function.
411 if (fsl_check_boot_mode_secure() == 0)
415 case ERROR_ESBC_CLIENT_HEADER_BARKER:
416 case ERROR_ESBC_CLIENT_HEADER_IMG_SIZE:
417 case ERROR_ESBC_CLIENT_HEADER_KEY_LEN:
418 case ERROR_ESBC_CLIENT_HEADER_SIG_LEN:
419 case ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN:
420 case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1:
421 case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2:
422 case ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD:
423 case ERROR_ESBC_CLIENT_HEADER_SG_ESBC_EP:
424 case ERROR_ESBC_CLIENT_HEADER_SG_ENTIRES_BAD:
425 case ERROR_KEY_TABLE_NOT_FOUND:
426 #ifdef CONFIG_KEY_REVOCATION
427 case ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED:
428 case ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY:
429 case ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM:
430 case ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN:
432 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
434 case ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED:
435 case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY:
436 case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM:
437 case ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN:
438 case ERROR_IE_TABLE_NOT_FOUND:
440 fsl_secboot_header_verification_failure();
442 case ERROR_ESBC_SEC_RESET:
443 case ERROR_ESBC_SEC_DEQ:
444 case ERROR_ESBC_SEC_ENQ:
445 case ERROR_ESBC_SEC_DEQ_TO:
446 case ERROR_ESBC_SEC_JOBQ_STATUS:
447 case ERROR_ESBC_CLIENT_HASH_COMPARE_KEY:
448 case ERROR_ESBC_CLIENT_HASH_COMPARE_EM:
449 fsl_secboot_image_verification_failure();
451 case ERROR_ESBC_MISSING_BOOTM:
452 fsl_secboot_bootscript_parse_failure();
454 case ERROR_ESBC_WRONG_CMD:
461 static void fsl_secblk_handle_error(int error)
464 case ERROR_ESBC_SEC_ENQ:
465 fsl_secboot_handle_error(ERROR_ESBC_SEC_ENQ);
467 case ERROR_ESBC_SEC_DEQ:
468 fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ);
470 case ERROR_ESBC_SEC_DEQ_TO:
471 fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ_TO);
474 printf("Job Queue Output status %x\n", error);
475 fsl_secboot_handle_error(ERROR_ESBC_SEC_JOBQ_STATUS);
481 * Calculate hash of key obtained via offset present in ESBC uboot
482 * client hdr. This function calculates the hash of key which is obtained
483 * through offset present in ESBC uboot client header.
485 static int calc_img_key_hash(struct fsl_secboot_img_priv *img)
487 struct hash_algo *algo;
491 const char *algo_name = "sha256";
493 /* Calculate hash of the esbc key */
494 ret = hash_progressive_lookup_algo(algo_name, &algo);
498 ret = algo->hash_init(algo, &ctx);
502 /* Update hash for ESBC key */
503 #ifdef CONFIG_KEY_REVOCATION
504 if (check_srk(img)) {
505 ret = algo->hash_update(algo, ctx,
506 (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
507 img->hdr.len_kr.num_srk * sizeof(struct srk_table), 1);
512 ret = algo->hash_update(algo, ctx,
513 img->img_key, img->key_len, 1);
517 /* Copy hash at destination buffer */
518 ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
522 for (i = 0; i < SHA256_BYTES; i++)
523 img->img_key_hash[i] = hash_val[i];
529 * Calculate hash of ESBC hdr and ESBC. This function calculates the
530 * single hash of ESBC header and ESBC image. If SG flag is on, all
531 * SG entries are also hashed alongwith the complete SG table.
533 static int calc_esbchdr_esbc_hash(struct fsl_secboot_img_priv *img)
535 struct hash_algo *algo;
539 const char *algo_name = "sha256";
541 /* Calculate the hash of the ESBC */
542 ret = hash_progressive_lookup_algo(algo_name, &algo);
546 ret = algo->hash_init(algo, &ctx);
547 /* Copy hash at destination buffer */
551 /* Update hash for CSF Header */
552 ret = algo->hash_update(algo, ctx,
553 (u8 *)&img->hdr, sizeof(struct fsl_secboot_img_hdr), 0);
557 /* Update the hash with that of srk table if srk flag is 1
558 * If IE Table is selected, key is not added in the hash
559 * If neither srk table nor IE key table available, add key
560 * from header in the hash calculation
562 #ifdef CONFIG_KEY_REVOCATION
563 if (check_srk(img)) {
564 ret = algo->hash_update(algo, ctx,
565 (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
566 img->hdr.len_kr.num_srk * sizeof(struct srk_table), 0);
570 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
571 if (!key_hash && check_ie(img))
574 #ifndef CONFIG_ESBC_HDR_LS
575 /* No single key support in LS ESBC header */
577 ret = algo->hash_update(algo, ctx,
578 img->img_key, img->hdr.key_len, 0);
585 return ERROR_KEY_TABLE_NOT_FOUND;
587 /* Update hash for actual Image */
588 ret = algo->hash_update(algo, ctx,
589 (u8 *)(*(img->img_addr_ptr)), img->img_size, 1);
593 /* Copy hash at destination buffer */
594 ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
602 * Construct encoded hash EM' wrt PKCSv1.5. This function calculates the
603 * pointers for padding, DER value and hash. And finally, constructs EM'
604 * which includes hash of complete CSF header and ESBC image. If SG flag
605 * is on, hash of SG table and entries is also included.
607 static void construct_img_encoded_hash_second(struct fsl_secboot_img_priv *img)
610 * RSA PKCSv1.5 encoding format for encoded message is below
611 * EM = 0x0 || 0x1 || PS || 0x0 || DER || Hash
612 * PS is Padding String
613 * DER is DER value for SHA-256
614 * Hash is SHA-256 hash
615 * *********************************************************
616 * representative points to first byte of EM initially and is
618 * representative is incremented by 1 and second byte is filled
620 * padding points to third byte of EM
621 * digest points to full length of EM - 32 bytes
622 * hash_id (DER value) points to 19 bytes before pDigest
623 * separator is one byte which separates padding and DER
628 u8 *padding, *digest;
629 u8 *hash_id, *separator;
632 len = (get_key_len(img) / 2) - 1;
633 representative = img->img_encoded_hash_second;
634 representative[0] = 0;
635 representative[1] = 1; /* block type 1 */
637 padding = &representative[2];
638 digest = &representative[1] + len - 32;
639 hash_id = digest - sizeof(hash_identifier);
640 separator = hash_id - 1;
642 /* fill padding area pointed by padding with 0xff */
643 memset(padding, 0xff, separator - padding);
645 /* fill byte pointed by separator */
648 /* fill SHA-256 DER value pointed by HashId */
649 memcpy(hash_id, hash_identifier, sizeof(hash_identifier));
651 /* fill hash pointed by Digest */
652 for (i = 0; i < SHA256_BYTES; i++)
653 digest[i] = hash_val[i];
657 * Reads and validates the ESBC client header.
658 * This function reads key and signature from the ESBC client header.
659 * If Scatter/Gather flag is on, lengths and offsets of images
660 * present as SG entries are also read. This function also checks
661 * whether the header is valid or not.
663 static int read_validate_esbc_client_header(struct fsl_secboot_img_priv *img)
665 struct fsl_secboot_img_hdr *hdr = &img->hdr;
666 void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
672 /* check barker code */
673 if (memcmp(hdr->barker, barker_code, ESBC_BARKER_LEN))
674 return ERROR_ESBC_CLIENT_HEADER_BARKER;
676 /* If Image Address is not passed as argument to function,
677 * then Address and Size must be read from the Header.
679 if (*(img->img_addr_ptr) == 0) {
680 #ifdef CONFIG_ESBC_ADDR_64BIT
681 *(img->img_addr_ptr) = hdr->pimg64;
683 *(img->img_addr_ptr) = hdr->pimg;
688 return ERROR_ESBC_CLIENT_HEADER_IMG_SIZE;
690 img->img_size = hdr->img_size;
693 #ifdef CONFIG_KEY_REVOCATION
694 if (check_srk(img)) {
695 ret = read_validate_srk_tbl(img);
702 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
703 if (!key_found && check_ie(img)) {
704 ret = read_validate_ie_tbl(img);
710 #ifndef CONFIG_ESBC_HDR_LS
711 /* Single Key Feature not available in LS ESBC Header */
712 if (key_found == 0) {
713 ret = read_validate_single_key(img);
720 return ERROR_KEY_TABLE_NOT_FOUND;
723 if (get_key_len(img) == 2 * hdr->sign_len) {
724 /* check signature length */
725 if (!((hdr->sign_len == KEY_SIZE_BYTES / 4) ||
726 (hdr->sign_len == KEY_SIZE_BYTES / 2) ||
727 (hdr->sign_len == KEY_SIZE_BYTES)))
728 return ERROR_ESBC_CLIENT_HEADER_SIG_LEN;
730 return ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN;
733 memcpy(&img->img_sign, esbc + hdr->psign, hdr->sign_len);
734 /* No SG support in LS-CH3 */
735 #ifndef CONFIG_ESBC_HDR_LS
738 return ERROR_ESBC_CLIENT_HEADER_SG;
741 /* modulus most significant bit should be set */
742 k = (u8 *)&img->img_key;
744 if ((k[0] & 0x80) == 0)
745 return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1;
747 /* modulus value should be odd */
748 if ((k[get_key_len(img) / 2 - 1] & 0x1) == 0)
749 return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2;
751 /* Check signature value < modulus value */
752 s = (u8 *)&img->img_sign;
754 if (!(memcmp(s, k, hdr->sign_len) < 0))
755 return ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD;
757 return ESBC_VALID_HDR;
760 static inline int str2longbe(const char *p, ulong *num)
768 tmp = simple_strtoul(p, &endptr, 16);
769 if (sizeof(ulong) == 4)
770 *num = cpu_to_be32(tmp);
772 *num = cpu_to_be64(tmp);
775 return *p != '\0' && *endptr == '\0';
777 /* Function to calculate the ESBC Image Hash
778 * and hash from Digital signature.
779 * The Two hash's are compared to yield the
780 * result of signature validation.
782 static int calculate_cmp_img_sig(struct fsl_secboot_img_priv *img)
786 struct key_prop prop;
787 #if !defined(USE_HOSTCC)
788 struct udevice *mod_exp_dev;
790 ret = calc_esbchdr_esbc_hash(img);
794 /* Construct encoded hash EM' wrt PKCSv1.5 */
795 construct_img_encoded_hash_second(img);
797 /* Fill prop structure for public key */
798 memset(&prop, 0, sizeof(struct key_prop));
799 key_len = get_key_len(img) / 2;
800 prop.modulus = img->img_key;
801 prop.public_exponent = img->img_key + key_len;
802 prop.num_bits = key_len * 8;
803 prop.exp_len = key_len;
805 ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
807 printf("RSA: Can't find Modular Exp implementation\n");
811 ret = rsa_mod_exp(mod_exp_dev, img->img_sign, img->hdr.sign_len,
812 &prop, img->img_encoded_hash);
817 * compare the encoded messages EM' and EM wrt RSA PKCSv1.5
818 * memcmp returns zero on success
819 * memcmp returns non-zero on failure
821 ret = memcmp(&img->img_encoded_hash_second, &img->img_encoded_hash,
825 return ERROR_ESBC_CLIENT_HASH_COMPARE_EM;
829 /* Function to initialize img priv and global data structure
831 static int secboot_init(struct fsl_secboot_img_priv **img_ptr)
833 *img_ptr = malloc(sizeof(struct fsl_secboot_img_priv));
835 struct fsl_secboot_img_priv *img = *img_ptr;
839 memset(img, 0, sizeof(struct fsl_secboot_img_priv));
841 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
843 img->ie_addr = glb.ie_addr;
849 /* haddr - Address of the header of image to be validated.
850 * arg_hash_str - Option hash string. If provided, this
851 * overrides the key hash in the SFP fuses.
852 * img_addr_ptr - Optional pointer to address of image to be validated.
853 * If non zero addr, this overrides the addr of image in header,
854 * otherwise updated to image addr in header.
855 * Acts as both input and output of function.
856 * This pointer shouldn't be NULL.
858 int fsl_secboot_validate(uintptr_t haddr, char *arg_hash_str,
859 uintptr_t *img_addr_ptr)
861 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
862 ulong hash[SHA256_BYTES/sizeof(ulong)];
863 char hash_str[NUM_HEX_CHARS + 1];
864 struct fsl_secboot_img_priv *img;
865 struct fsl_secboot_img_hdr *hdr;
867 int ret, i, hash_cmd = 0;
870 if (arg_hash_str != NULL) {
871 const char *cp = arg_hash_str;
874 if (*cp == '0' && *(cp + 1) == 'x')
877 /* The input string expected is in hex, where
878 * each 4 bits would be represented by a hex
879 * sha256 hash is 256 bits long, which would mean
880 * num of characters = 256 / 4
882 if (strlen(cp) != SHA256_NIBBLES) {
883 printf("%s is not a 256 bits hex string as expected\n",
888 for (i = 0; i < sizeof(hash)/sizeof(ulong); i++) {
889 strncpy(hash_str, cp + (i * NUM_HEX_CHARS),
891 hash_str[NUM_HEX_CHARS] = '\0';
892 if (!str2longbe(hash_str, &hash[i])) {
893 printf("%s is not a 256 bits hex string ",
902 ret = secboot_init(&img);
906 /* Update the information in Private Struct */
908 img->ehdrloc = haddr;
909 img->img_addr_ptr = img_addr_ptr;
910 esbc = (u8 *)img->ehdrloc;
912 memcpy(hdr, esbc, sizeof(struct fsl_secboot_img_hdr));
914 /* read and validate esbc header */
915 ret = read_validate_esbc_client_header(img);
917 if (ret != ESBC_VALID_HDR) {
918 fsl_secboot_handle_error(ret);
922 /* SRKH present in SFP */
923 for (i = 0; i < NUM_SRKH_REGS; i++)
924 srk_hash[i] = srk_in32(&sfp_regs->srk_hash[i]);
927 * Calculate hash of key obtained via offset present in
928 * ESBC uboot client hdr
930 ret = calc_img_key_hash(img);
932 fsl_secblk_handle_error(ret);
936 /* Compare hash obtained above with SRK hash present in SFP */
938 ret = memcmp(&hash, &img->img_key_hash, SHA256_BYTES);
940 ret = memcmp(srk_hash, img->img_key_hash, SHA256_BYTES);
942 #if defined(CONFIG_FSL_ISBC_KEY_EXT)
943 if (!hash_cmd && check_ie(img))
948 fsl_secboot_handle_error(ERROR_ESBC_CLIENT_HASH_COMPARE_KEY);
952 ret = calculate_cmp_img_sig(img);
954 fsl_secboot_handle_error(ret);
959 /* Free Img as it was malloc'ed*/