fsl_validate.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2015 Freescale Semiconductor, Inc.
  4. * Copyright 2021-2022 NXP
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <fsl_validate.h>
  9. #include <fsl_secboot_err.h>
  10. #include <fsl_sfp.h>
  11. #include <fsl_sec.h>
  12. #include <command.h>
  13. #include <log.h>
  14. #include <malloc.h>
  15. #include <u-boot/rsa-mod-exp.h>
  16. #include <hash.h>
  17. #include <fsl_secboot_err.h>
  18. #ifdef CONFIG_ARCH_LS1021A
  19. #include <asm/arch/immap_ls102xa.h>
  20. #endif
  21. #include <dm/lists.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)
  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 CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
  30. /* Global data structure */
  31. static struct fsl_secboot_glb glb;
  32. #endif
  33. /* This array contains DER value for SHA-256 */
  34. static const u8 hash_identifier[] = { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60,
  35. 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00,
  36. 0x04, 0x20
  37. };
  38. static u8 hash_val[SHA256_BYTES];
  39. #ifdef CONFIG_ESBC_HDR_LS
  40. /* New Barker Code for LS ESBC Header */
  41. static const u8 barker_code[ESBC_BARKER_LEN] = { 0x12, 0x19, 0x20, 0x01 };
  42. #else
  43. static const u8 barker_code[ESBC_BARKER_LEN] = { 0x68, 0x39, 0x27, 0x81 };
  44. #endif
  45. void branch_to_self(void) __attribute__ ((noreturn));
  46. /*
  47. * This function will put core in infinite loop.
  48. * This will be called when the ESBC can not proceed further due
  49. * to some unknown errors.
  50. */
  51. void branch_to_self(void)
  52. {
  53. printf("Core is in infinite loop due to errors.\n");
  54. self:
  55. goto self;
  56. }
  57. #if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
  58. static u32 check_ie(struct fsl_secboot_img_priv *img)
  59. {
  60. if (img->hdr.ie_flag & IE_FLAG_MASK)
  61. return 1;
  62. return 0;
  63. }
  64. /* This function returns the CSF Header Address of uboot
  65. * For MPC85xx based platforms, the LAW mapping for NOR
  66. * flash changes in uboot code. Hence the offset needs
  67. * to be calculated and added to the new NOR flash base
  68. * address
  69. */
  70. #if defined(CONFIG_MPC85xx)
  71. #include <flash.h>
  72. int get_csf_base_addr(u32 *csf_addr, u32 *flash_base_addr)
  73. {
  74. struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_MPC85xx_GUTS_ADDR);
  75. u32 csf_hdr_addr = in_be32(&gur->scratchrw[0]);
  76. u32 csf_flash_offset = csf_hdr_addr & ~(CFG_SYS_PBI_FLASH_BASE);
  77. u32 flash_addr, addr;
  78. int found = 0;
  79. int i = 0;
  80. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  81. flash_addr = flash_info[i].start[0];
  82. addr = flash_info[i].start[0] + csf_flash_offset;
  83. if (memcmp((u8 *)addr, barker_code, ESBC_BARKER_LEN) == 0) {
  84. debug("Barker found on addr %x\n", addr);
  85. found = 1;
  86. break;
  87. }
  88. }
  89. if (!found)
  90. return -1;
  91. *csf_addr = addr;
  92. *flash_base_addr = flash_addr;
  93. return 0;
  94. }
  95. #else
  96. /* For platforms like LS1020, correct flash address is present in
  97. * the header. So the function reqturns flash base address as 0
  98. */
  99. int get_csf_base_addr(u32 *csf_addr, u32 *flash_base_addr)
  100. {
  101. struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
  102. u32 csf_hdr_addr = in_be32(&gur->scratchrw[0]);
  103. if (memcmp((u8 *)(uintptr_t)csf_hdr_addr,
  104. barker_code, ESBC_BARKER_LEN))
  105. return -1;
  106. *csf_addr = csf_hdr_addr;
  107. *flash_base_addr = 0;
  108. return 0;
  109. }
  110. #endif
  111. #if defined(CONFIG_ESBC_HDR_LS)
  112. static int get_ie_info_addr(uintptr_t *ie_addr)
  113. {
  114. struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
  115. /* For LS-CH3, the address of IE Table is
  116. * stated in Scratch13 and scratch14 of DCFG.
  117. * Bootrom validates this table while validating uboot.
  118. * DCFG is LE*/
  119. *ie_addr = in_le32(&gur->scratchrw[SCRATCH_IE_HIGH_ADR - 1]);
  120. *ie_addr = *ie_addr << 32;
  121. *ie_addr |= in_le32(&gur->scratchrw[SCRATCH_IE_LOW_ADR - 1]);
  122. return 0;
  123. }
  124. #else /* CONFIG_ESBC_HDR_LS */
  125. static int get_ie_info_addr(uintptr_t *ie_addr)
  126. {
  127. struct fsl_secboot_img_hdr *hdr;
  128. struct fsl_secboot_sg_table *sg_tbl;
  129. u32 flash_base_addr, csf_addr;
  130. if (get_csf_base_addr(&csf_addr, &flash_base_addr))
  131. return -1;
  132. hdr = (struct fsl_secboot_img_hdr *)(uintptr_t)csf_addr;
  133. /* For SoC's with Trust Architecture v1 with corenet bus
  134. * the sg table field in CSF header has absolute address
  135. * for sg table in memory. In other Trust Architecture,
  136. * this field specifies the offset of sg table from the
  137. * base address of CSF Header
  138. */
  139. #if defined(CONFIG_FSL_TRUST_ARCH_v1) && defined(CONFIG_FSL_CORENET)
  140. sg_tbl = (struct fsl_secboot_sg_table *)
  141. (((u32)hdr->psgtable & ~(CFG_SYS_PBI_FLASH_BASE)) +
  142. flash_base_addr);
  143. #else
  144. sg_tbl = (struct fsl_secboot_sg_table *)(uintptr_t)(csf_addr +
  145. (u32)hdr->psgtable);
  146. #endif
  147. /* IE Key Table is the first entry in the SG Table */
  148. #if defined(CONFIG_MPC85xx)
  149. *ie_addr = (uintptr_t)((sg_tbl->src_addr &
  150. ~(CFG_SYS_PBI_FLASH_BASE)) +
  151. flash_base_addr);
  152. #else
  153. *ie_addr = (uintptr_t)sg_tbl->src_addr;
  154. #endif
  155. debug("IE Table address is %lx\n", *ie_addr);
  156. return 0;
  157. }
  158. #endif /* CONFIG_ESBC_HDR_LS */
  159. #endif
  160. #ifdef CONFIG_KEY_REVOCATION
  161. /* This function checks srk_table_flag in header and set/reset srk_flag.*/
  162. static u32 check_srk(struct fsl_secboot_img_priv *img)
  163. {
  164. #ifdef CONFIG_ESBC_HDR_LS
  165. /* In LS, No SRK Flag as SRK is always present if IE not present*/
  166. #if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
  167. return !check_ie(img);
  168. #endif
  169. return 1;
  170. #else
  171. if (img->hdr.len_kr.srk_table_flag & SRK_FLAG)
  172. return 1;
  173. return 0;
  174. #endif
  175. }
  176. /* This function returns ospr's key_revoc values.*/
  177. static u32 get_key_revoc(void)
  178. {
  179. struct ccsr_sfp_regs *sfp_regs = (void *)(CFG_SYS_SFP_ADDR);
  180. return (sfp_in32(&sfp_regs->ospr) & OSPR_KEY_REVOC_MASK) >>
  181. OSPR_KEY_REVOC_SHIFT;
  182. }
  183. /* This function checks if selected key is revoked or not.*/
  184. static u32 is_key_revoked(u32 keynum, u32 rev_flag)
  185. {
  186. if (keynum == UNREVOCABLE_KEY)
  187. return 0;
  188. if ((u32)(1 << (ALIGN_REVOC_KEY - keynum)) & rev_flag)
  189. return 1;
  190. return 0;
  191. }
  192. /* It read validates srk_table key lengths.*/
  193. static u32 read_validate_srk_tbl(struct fsl_secboot_img_priv *img)
  194. {
  195. int i = 0;
  196. u32 ret, key_num, key_revoc_flag, size;
  197. struct fsl_secboot_img_hdr *hdr = &img->hdr;
  198. void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
  199. if ((hdr->len_kr.num_srk == 0) ||
  200. (hdr->len_kr.num_srk > MAX_KEY_ENTRIES))
  201. return ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY;
  202. key_num = hdr->len_kr.srk_sel;
  203. if (key_num == 0 || key_num > hdr->len_kr.num_srk)
  204. return ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM;
  205. /* Get revoc key from sfp */
  206. key_revoc_flag = get_key_revoc();
  207. ret = is_key_revoked(key_num, key_revoc_flag);
  208. if (ret)
  209. return ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED;
  210. size = hdr->len_kr.num_srk * sizeof(struct srk_table);
  211. memcpy(&img->srk_tbl, esbc + hdr->srk_tbl_off, size);
  212. for (i = 0; i < hdr->len_kr.num_srk; i++) {
  213. if (!CHECK_KEY_LEN(img->srk_tbl[i].key_len))
  214. return ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN;
  215. }
  216. img->key_len = img->srk_tbl[key_num - 1].key_len;
  217. memcpy(&img->img_key, &(img->srk_tbl[key_num - 1].pkey),
  218. img->key_len);
  219. return 0;
  220. }
  221. #endif
  222. #ifndef CONFIG_ESBC_HDR_LS
  223. static u32 read_validate_single_key(struct fsl_secboot_img_priv *img)
  224. {
  225. struct fsl_secboot_img_hdr *hdr = &img->hdr;
  226. void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
  227. /* check key length */
  228. if (!CHECK_KEY_LEN(hdr->key_len))
  229. return ERROR_ESBC_CLIENT_HEADER_KEY_LEN;
  230. memcpy(&img->img_key, esbc + hdr->pkey, hdr->key_len);
  231. img->key_len = hdr->key_len;
  232. return 0;
  233. }
  234. #endif /* CONFIG_ESBC_HDR_LS */
  235. #if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
  236. static void install_ie_tbl(uintptr_t ie_tbl_addr,
  237. struct fsl_secboot_img_priv *img)
  238. {
  239. /* Copy IE tbl to Global Data */
  240. memcpy(&glb.ie_tbl, (u8 *)ie_tbl_addr, sizeof(struct ie_key_info));
  241. img->ie_addr = (uintptr_t)&glb.ie_tbl;
  242. glb.ie_addr = img->ie_addr;
  243. }
  244. static u32 read_validate_ie_tbl(struct fsl_secboot_img_priv *img)
  245. {
  246. struct fsl_secboot_img_hdr *hdr = &img->hdr;
  247. u32 ie_key_len, ie_revoc_flag, ie_num;
  248. struct ie_key_info *ie_info;
  249. if (!img->ie_addr) {
  250. if (get_ie_info_addr(&img->ie_addr))
  251. return ERROR_IE_TABLE_NOT_FOUND;
  252. else
  253. install_ie_tbl(img->ie_addr, img);
  254. }
  255. ie_info = (struct ie_key_info *)(uintptr_t)img->ie_addr;
  256. if (ie_info->num_keys == 0 || ie_info->num_keys > 32)
  257. return ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY;
  258. ie_num = hdr->ie_key_sel;
  259. if (ie_num == 0 || ie_num > ie_info->num_keys)
  260. return ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM;
  261. ie_revoc_flag = ie_info->key_revok;
  262. if ((u32)(1 << (ie_num - 1)) & ie_revoc_flag)
  263. return ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED;
  264. ie_key_len = ie_info->ie_key_tbl[ie_num - 1].key_len;
  265. if (!CHECK_KEY_LEN(ie_key_len))
  266. return ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN;
  267. memcpy(&img->img_key, &(ie_info->ie_key_tbl[ie_num - 1].pkey),
  268. ie_key_len);
  269. img->key_len = ie_key_len;
  270. return 0;
  271. }
  272. #endif
  273. /* This function return length of public key.*/
  274. static inline u32 get_key_len(struct fsl_secboot_img_priv *img)
  275. {
  276. return img->key_len;
  277. }
  278. /*
  279. * Handles the ESBC uboot client header verification failure.
  280. * This function handles all the errors which might occur in the
  281. * parsing and checking of ESBC uboot client header. It will also
  282. * set the error bits in the SEC_MON.
  283. */
  284. static void fsl_secboot_header_verification_failure(void)
  285. {
  286. struct ccsr_sfp_regs *sfp_regs = (void *)(CFG_SYS_SFP_ADDR);
  287. /* 29th bit of OSPR is ITS */
  288. u32 its = sfp_in32(&sfp_regs->ospr) >> 2;
  289. if (its == 1)
  290. set_sec_mon_state(HPSR_SSM_ST_SOFT_FAIL);
  291. else
  292. set_sec_mon_state(HPSR_SSM_ST_NON_SECURE);
  293. printf("Generating reset request\n");
  294. do_reset(NULL, 0, 0, NULL);
  295. /* If reset doesn't coocur, halt execution */
  296. do_esbc_halt(NULL, 0, 0, NULL);
  297. }
  298. /*
  299. * Handles the ESBC uboot client image verification failure.
  300. * This function handles all the errors which might occur in the
  301. * public key hash comparison and signature verification of
  302. * ESBC uboot client image. It will also
  303. * set the error bits in the SEC_MON.
  304. */
  305. static void fsl_secboot_image_verification_failure(void)
  306. {
  307. struct ccsr_sfp_regs *sfp_regs = (void *)(CFG_SYS_SFP_ADDR);
  308. u32 its = (sfp_in32(&sfp_regs->ospr) & ITS_MASK) >> ITS_BIT;
  309. if (its == 1) {
  310. set_sec_mon_state(HPSR_SSM_ST_SOFT_FAIL);
  311. printf("Generating reset request\n");
  312. do_reset(NULL, 0, 0, NULL);
  313. /* If reset doesn't coocur, halt execution */
  314. do_esbc_halt(NULL, 0, 0, NULL);
  315. } else {
  316. set_sec_mon_state(HPSR_SSM_ST_NON_SECURE);
  317. }
  318. }
  319. static void fsl_secboot_bootscript_parse_failure(void)
  320. {
  321. fsl_secboot_header_verification_failure();
  322. }
  323. /*
  324. * Handles the errors in esbc boot.
  325. * This function handles all the errors which might occur in the
  326. * esbc boot phase. It will call the appropriate api to log the
  327. * errors and set the error bits in the SEC_MON.
  328. */
  329. void fsl_secboot_handle_error(int error)
  330. {
  331. #ifndef CONFIG_SPL_BUILD
  332. const struct fsl_secboot_errcode *e;
  333. for (e = fsl_secboot_errcodes; e->errcode != ERROR_ESBC_CLIENT_MAX;
  334. e++) {
  335. if (e->errcode == error)
  336. printf("ERROR :: %x :: %s\n", error, e->name);
  337. }
  338. #else
  339. printf("ERROR :: %x\n", error);
  340. #endif
  341. /* If Boot Mode is secure, transition the SNVS state and issue
  342. * reset based on type of failure and ITS setting.
  343. * If Boot mode is non-secure, return from this function.
  344. */
  345. if (fsl_check_boot_mode_secure() == 0)
  346. return;
  347. switch (error) {
  348. case ERROR_ESBC_CLIENT_HEADER_BARKER:
  349. case ERROR_ESBC_CLIENT_HEADER_IMG_SIZE:
  350. case ERROR_ESBC_CLIENT_HEADER_KEY_LEN:
  351. case ERROR_ESBC_CLIENT_HEADER_SIG_LEN:
  352. case ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN:
  353. case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1:
  354. case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2:
  355. case ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD:
  356. case ERROR_ESBC_CLIENT_HEADER_SG_ESBC_EP:
  357. case ERROR_ESBC_CLIENT_HEADER_SG_ENTIRES_BAD:
  358. case ERROR_KEY_TABLE_NOT_FOUND:
  359. #ifdef CONFIG_KEY_REVOCATION
  360. case ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED:
  361. case ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY:
  362. case ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM:
  363. case ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN:
  364. #endif
  365. #if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
  366. /*@fallthrough@*/
  367. case ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED:
  368. case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY:
  369. case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM:
  370. case ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN:
  371. case ERROR_IE_TABLE_NOT_FOUND:
  372. #endif
  373. fsl_secboot_header_verification_failure();
  374. break;
  375. case ERROR_ESBC_SEC_RESET:
  376. case ERROR_ESBC_SEC_DEQ:
  377. case ERROR_ESBC_SEC_ENQ:
  378. case ERROR_ESBC_SEC_DEQ_TO:
  379. case ERROR_ESBC_SEC_JOBQ_STATUS:
  380. case ERROR_ESBC_CLIENT_HASH_COMPARE_KEY:
  381. case ERROR_ESBC_CLIENT_HASH_COMPARE_EM:
  382. fsl_secboot_image_verification_failure();
  383. break;
  384. case ERROR_ESBC_MISSING_BOOTM:
  385. fsl_secboot_bootscript_parse_failure();
  386. break;
  387. case ERROR_ESBC_WRONG_CMD:
  388. default:
  389. branch_to_self();
  390. break;
  391. }
  392. }
  393. static void fsl_secblk_handle_error(int error)
  394. {
  395. switch (error) {
  396. case ERROR_ESBC_SEC_ENQ:
  397. fsl_secboot_handle_error(ERROR_ESBC_SEC_ENQ);
  398. break;
  399. case ERROR_ESBC_SEC_DEQ:
  400. fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ);
  401. break;
  402. case ERROR_ESBC_SEC_DEQ_TO:
  403. fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ_TO);
  404. break;
  405. default:
  406. printf("Job Queue Output status %x\n", error);
  407. fsl_secboot_handle_error(ERROR_ESBC_SEC_JOBQ_STATUS);
  408. break;
  409. }
  410. }
  411. /*
  412. * Calculate hash of key obtained via offset present in ESBC uboot
  413. * client hdr. This function calculates the hash of key which is obtained
  414. * through offset present in ESBC uboot client header.
  415. */
  416. static int calc_img_key_hash(struct fsl_secboot_img_priv *img)
  417. {
  418. struct hash_algo *algo;
  419. void *ctx;
  420. int i, srk = 0;
  421. int ret = 0;
  422. const char *algo_name = "sha256";
  423. /* Calculate hash of the esbc key */
  424. ret = hash_progressive_lookup_algo(algo_name, &algo);
  425. if (ret)
  426. return ret;
  427. ret = algo->hash_init(algo, &ctx);
  428. if (ret)
  429. return ret;
  430. /* Update hash for ESBC key */
  431. #ifdef CONFIG_KEY_REVOCATION
  432. if (check_srk(img)) {
  433. ret = algo->hash_update(algo, ctx,
  434. (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
  435. img->hdr.len_kr.num_srk * sizeof(struct srk_table), 1);
  436. srk = 1;
  437. }
  438. #endif
  439. if (!srk)
  440. ret = algo->hash_update(algo, ctx,
  441. img->img_key, img->key_len, 1);
  442. if (ret)
  443. return ret;
  444. /* Copy hash at destination buffer */
  445. ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
  446. if (ret) {
  447. free(ctx);
  448. return ret;
  449. }
  450. for (i = 0; i < SHA256_BYTES; i++)
  451. img->img_key_hash[i] = hash_val[i];
  452. return 0;
  453. }
  454. /*
  455. * Calculate hash of ESBC hdr and ESBC. This function calculates the
  456. * single hash of ESBC header and ESBC image. If SG flag is on, all
  457. * SG entries are also hashed alongwith the complete SG table.
  458. */
  459. static int calc_esbchdr_esbc_hash(struct fsl_secboot_img_priv *img)
  460. {
  461. struct hash_algo *algo;
  462. void *ctx;
  463. int ret = 0;
  464. int key_hash = 0;
  465. const char *algo_name = "sha256";
  466. /* Calculate the hash of the ESBC */
  467. ret = hash_progressive_lookup_algo(algo_name, &algo);
  468. if (ret)
  469. return ret;
  470. ret = algo->hash_init(algo, &ctx);
  471. /* Copy hash at destination buffer */
  472. if (ret)
  473. return ret;
  474. /* Update hash for CSF Header */
  475. ret = algo->hash_update(algo, ctx,
  476. (u8 *)&img->hdr, sizeof(struct fsl_secboot_img_hdr), 0);
  477. if (ret)
  478. return ret;
  479. /* Update the hash with that of srk table if srk flag is 1
  480. * If IE Table is selected, key is not added in the hash
  481. * If neither srk table nor IE key table available, add key
  482. * from header in the hash calculation
  483. */
  484. #ifdef CONFIG_KEY_REVOCATION
  485. if (check_srk(img)) {
  486. ret = algo->hash_update(algo, ctx,
  487. (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
  488. img->hdr.len_kr.num_srk * sizeof(struct srk_table), 0);
  489. key_hash = 1;
  490. }
  491. #endif
  492. #if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
  493. if (!key_hash && check_ie(img))
  494. key_hash = 1;
  495. #endif
  496. #ifndef CONFIG_ESBC_HDR_LS
  497. /* No single key support in LS ESBC header */
  498. if (!key_hash) {
  499. ret = algo->hash_update(algo, ctx,
  500. img->img_key, img->hdr.key_len, 0);
  501. key_hash = 1;
  502. }
  503. #endif
  504. if (ret)
  505. return ret;
  506. if (!key_hash) {
  507. free(ctx);
  508. return ERROR_KEY_TABLE_NOT_FOUND;
  509. }
  510. /* Update hash for actual Image */
  511. ret = algo->hash_update(algo, ctx,
  512. (u8 *)(*(img->img_addr_ptr)), img->img_size, 1);
  513. if (ret)
  514. return ret;
  515. /* Copy hash at destination buffer */
  516. ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
  517. if (ret) {
  518. free(ctx);
  519. return ret;
  520. }
  521. return 0;
  522. }
  523. /*
  524. * Construct encoded hash EM' wrt PKCSv1.5. This function calculates the
  525. * pointers for padding, DER value and hash. And finally, constructs EM'
  526. * which includes hash of complete CSF header and ESBC image. If SG flag
  527. * is on, hash of SG table and entries is also included.
  528. */
  529. static void construct_img_encoded_hash_second(struct fsl_secboot_img_priv *img)
  530. {
  531. /*
  532. * RSA PKCSv1.5 encoding format for encoded message is below
  533. * EM = 0x0 || 0x1 || PS || 0x0 || DER || Hash
  534. * PS is Padding String
  535. * DER is DER value for SHA-256
  536. * Hash is SHA-256 hash
  537. * *********************************************************
  538. * representative points to first byte of EM initially and is
  539. * filled with 0x0
  540. * representative is incremented by 1 and second byte is filled
  541. * with 0x1
  542. * padding points to third byte of EM
  543. * digest points to full length of EM - 32 bytes
  544. * hash_id (DER value) points to 19 bytes before pDigest
  545. * separator is one byte which separates padding and DER
  546. */
  547. size_t len;
  548. u8 *representative;
  549. u8 *padding, *digest;
  550. u8 *hash_id, *separator;
  551. int i;
  552. len = (get_key_len(img) / 2) - 1;
  553. representative = img->img_encoded_hash_second;
  554. representative[0] = 0;
  555. representative[1] = 1; /* block type 1 */
  556. padding = &representative[2];
  557. digest = &representative[1] + len - 32;
  558. hash_id = digest - sizeof(hash_identifier);
  559. separator = hash_id - 1;
  560. /* fill padding area pointed by padding with 0xff */
  561. memset(padding, 0xff, separator - padding);
  562. /* fill byte pointed by separator */
  563. *separator = 0;
  564. /* fill SHA-256 DER value pointed by HashId */
  565. memcpy(hash_id, hash_identifier, sizeof(hash_identifier));
  566. /* fill hash pointed by Digest */
  567. for (i = 0; i < SHA256_BYTES; i++)
  568. digest[i] = hash_val[i];
  569. }
  570. /*
  571. * Reads and validates the ESBC client header.
  572. * This function reads key and signature from the ESBC client header.
  573. * If Scatter/Gather flag is on, lengths and offsets of images
  574. * present as SG entries are also read. This function also checks
  575. * whether the header is valid or not.
  576. */
  577. static int read_validate_esbc_client_header(struct fsl_secboot_img_priv *img)
  578. {
  579. struct fsl_secboot_img_hdr *hdr = &img->hdr;
  580. void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
  581. u8 *k, *s;
  582. u32 ret = 0;
  583. int key_found = 0;
  584. /* check barker code */
  585. if (memcmp(hdr->barker, barker_code, ESBC_BARKER_LEN))
  586. return ERROR_ESBC_CLIENT_HEADER_BARKER;
  587. /* If Image Address is not passed as argument to function,
  588. * then Address and Size must be read from the Header.
  589. */
  590. if (*(img->img_addr_ptr) == 0) {
  591. #ifdef CONFIG_ESBC_ADDR_64BIT
  592. *(img->img_addr_ptr) = hdr->pimg64;
  593. #else
  594. *(img->img_addr_ptr) = hdr->pimg;
  595. #endif
  596. }
  597. if (!hdr->img_size)
  598. return ERROR_ESBC_CLIENT_HEADER_IMG_SIZE;
  599. img->img_size = hdr->img_size;
  600. /* Key checking*/
  601. #ifdef CONFIG_KEY_REVOCATION
  602. if (check_srk(img)) {
  603. ret = read_validate_srk_tbl(img);
  604. if (ret != 0)
  605. return ret;
  606. key_found = 1;
  607. }
  608. #endif
  609. #if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
  610. if (!key_found && check_ie(img)) {
  611. ret = read_validate_ie_tbl(img);
  612. if (ret != 0)
  613. return ret;
  614. key_found = 1;
  615. }
  616. #endif
  617. #ifndef CONFIG_ESBC_HDR_LS
  618. /* Single Key Feature not available in LS ESBC Header */
  619. if (key_found == 0) {
  620. ret = read_validate_single_key(img);
  621. if (ret != 0)
  622. return ret;
  623. key_found = 1;
  624. }
  625. #endif
  626. if (!key_found)
  627. return ERROR_KEY_TABLE_NOT_FOUND;
  628. /* check signaure */
  629. if (get_key_len(img) == 2 * hdr->sign_len) {
  630. /* check signature length */
  631. if (!((hdr->sign_len == KEY_SIZE_BYTES / 4) ||
  632. (hdr->sign_len == KEY_SIZE_BYTES / 2) ||
  633. (hdr->sign_len == KEY_SIZE_BYTES)))
  634. return ERROR_ESBC_CLIENT_HEADER_SIG_LEN;
  635. } else {
  636. return ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN;
  637. }
  638. memcpy(&img->img_sign, esbc + hdr->psign, hdr->sign_len);
  639. /* No SG support in LS-CH3 */
  640. #ifndef CONFIG_ESBC_HDR_LS
  641. /* No SG support */
  642. if (hdr->sg_flag)
  643. return ERROR_ESBC_CLIENT_HEADER_SG;
  644. #endif
  645. /* modulus most significant bit should be set */
  646. k = (u8 *)&img->img_key;
  647. if ((k[0] & 0x80) == 0)
  648. return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1;
  649. /* modulus value should be odd */
  650. if ((k[get_key_len(img) / 2 - 1] & 0x1) == 0)
  651. return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2;
  652. /* Check signature value < modulus value */
  653. s = (u8 *)&img->img_sign;
  654. if (!(memcmp(s, k, hdr->sign_len) < 0))
  655. return ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD;
  656. return ESBC_VALID_HDR;
  657. }
  658. static inline int str2longbe(const char *p, ulong *num)
  659. {
  660. char *endptr;
  661. ulong tmp;
  662. if (!p) {
  663. return 0;
  664. } else {
  665. tmp = hextoul(p, &endptr);
  666. if (sizeof(ulong) == 4)
  667. *num = cpu_to_be32(tmp);
  668. else
  669. *num = cpu_to_be64(tmp);
  670. }
  671. return *p != '\0' && *endptr == '\0';
  672. }
  673. /* Function to calculate the ESBC Image Hash
  674. * and hash from Digital signature.
  675. * The Two hash's are compared to yield the
  676. * result of signature validation.
  677. */
  678. static int calculate_cmp_img_sig(struct fsl_secboot_img_priv *img)
  679. {
  680. int ret;
  681. uint32_t key_len;
  682. struct key_prop prop;
  683. #if !defined(USE_HOSTCC)
  684. struct udevice *mod_exp_dev;
  685. #endif
  686. ret = calc_esbchdr_esbc_hash(img);
  687. if (ret)
  688. return ret;
  689. /* Construct encoded hash EM' wrt PKCSv1.5 */
  690. construct_img_encoded_hash_second(img);
  691. /* Fill prop structure for public key */
  692. memset(&prop, 0, sizeof(struct key_prop));
  693. key_len = get_key_len(img) / 2;
  694. prop.modulus = img->img_key;
  695. prop.public_exponent = img->img_key + key_len;
  696. prop.num_bits = key_len * 8;
  697. prop.exp_len = key_len;
  698. #if defined(CONFIG_SPL_BUILD)
  699. ret = device_bind_driver(NULL, "fsl_rsa_mod_exp", "fsl_rsa_mod_exp", NULL);
  700. if (ret) {
  701. printf("Couldn't bind fsl_rsa_mod_exp driver (%d)\n", ret);
  702. return -EINVAL;
  703. }
  704. #endif
  705. ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
  706. if (ret) {
  707. printf("RSA: Can't find Modular Exp implementation\n");
  708. return -EINVAL;
  709. }
  710. ret = rsa_mod_exp(mod_exp_dev, img->img_sign, img->hdr.sign_len,
  711. &prop, img->img_encoded_hash);
  712. if (ret)
  713. return ret;
  714. /*
  715. * compare the encoded messages EM' and EM wrt RSA PKCSv1.5
  716. * memcmp returns zero on success
  717. * memcmp returns non-zero on failure
  718. */
  719. ret = memcmp(&img->img_encoded_hash_second, &img->img_encoded_hash,
  720. img->hdr.sign_len);
  721. if (ret)
  722. return ERROR_ESBC_CLIENT_HASH_COMPARE_EM;
  723. return 0;
  724. }
  725. /* Function to initialize img priv and global data structure
  726. */
  727. static int secboot_init(struct fsl_secboot_img_priv **img_ptr)
  728. {
  729. *img_ptr = malloc(sizeof(struct fsl_secboot_img_priv));
  730. struct fsl_secboot_img_priv *img = *img_ptr;
  731. if (!img)
  732. return -ENOMEM;
  733. memset(img, 0, sizeof(struct fsl_secboot_img_priv));
  734. #if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
  735. if (glb.ie_addr)
  736. img->ie_addr = glb.ie_addr;
  737. #endif
  738. return 0;
  739. }
  740. /* haddr - Address of the header of image to be validated.
  741. * arg_hash_str - Option hash string. If provided, this
  742. * overrides the key hash in the SFP fuses.
  743. * img_addr_ptr - Optional pointer to address of image to be validated.
  744. * If non zero addr, this overrides the addr of image in header,
  745. * otherwise updated to image addr in header.
  746. * Acts as both input and output of function.
  747. * This pointer shouldn't be NULL.
  748. */
  749. int fsl_secboot_validate(uintptr_t haddr, char *arg_hash_str,
  750. uintptr_t *img_addr_ptr)
  751. {
  752. struct ccsr_sfp_regs *sfp_regs = (void *)(CFG_SYS_SFP_ADDR);
  753. ulong hash[SHA256_BYTES/sizeof(ulong)];
  754. char hash_str[NUM_HEX_CHARS + 1];
  755. struct fsl_secboot_img_priv *img;
  756. struct fsl_secboot_img_hdr *hdr;
  757. void *esbc;
  758. int ret, i, hash_cmd = 0;
  759. u32 srk_hash[8];
  760. if (strlen(arg_hash_str) != 0) {
  761. const char *cp = arg_hash_str;
  762. int i = 0;
  763. if (*cp == '0' && *(cp + 1) == 'x')
  764. cp += 2;
  765. /* The input string expected is in hex, where
  766. * each 4 bits would be represented by a hex
  767. * sha256 hash is 256 bits long, which would mean
  768. * num of characters = 256 / 4
  769. */
  770. if (strlen(cp) != SHA256_NIBBLES) {
  771. printf("%s is not a 256 bits hex string as expected\n",
  772. arg_hash_str);
  773. return -1;
  774. }
  775. for (i = 0; i < sizeof(hash)/sizeof(ulong); i++) {
  776. strncpy(hash_str, cp + (i * NUM_HEX_CHARS),
  777. NUM_HEX_CHARS);
  778. hash_str[NUM_HEX_CHARS] = '\0';
  779. if (!str2longbe(hash_str, &hash[i])) {
  780. printf("%s is not a 256 bits hex string ",
  781. arg_hash_str);
  782. return -1;
  783. }
  784. }
  785. hash_cmd = 1;
  786. }
  787. ret = secboot_init(&img);
  788. if (ret)
  789. goto exit;
  790. /* Update the information in Private Struct */
  791. hdr = &img->hdr;
  792. img->ehdrloc = haddr;
  793. img->img_addr_ptr = img_addr_ptr;
  794. esbc = (u8 *)img->ehdrloc;
  795. memcpy(hdr, esbc, sizeof(struct fsl_secboot_img_hdr));
  796. /* read and validate esbc header */
  797. ret = read_validate_esbc_client_header(img);
  798. if (ret != ESBC_VALID_HDR) {
  799. fsl_secboot_handle_error(ret);
  800. goto exit;
  801. }
  802. /* SRKH present in SFP */
  803. for (i = 0; i < NUM_SRKH_REGS; i++)
  804. srk_hash[i] = srk_in32(&sfp_regs->srk_hash[i]);
  805. /*
  806. * Calculate hash of key obtained via offset present in
  807. * ESBC uboot client hdr
  808. */
  809. ret = calc_img_key_hash(img);
  810. if (ret) {
  811. fsl_secblk_handle_error(ret);
  812. goto exit;
  813. }
  814. /* Compare hash obtained above with SRK hash present in SFP */
  815. if (hash_cmd)
  816. ret = memcmp(&hash, &img->img_key_hash, SHA256_BYTES);
  817. else
  818. ret = memcmp(srk_hash, img->img_key_hash, SHA256_BYTES);
  819. #if CONFIG_IS_ENABLED(FSL_ISBC_KEY_EXT)
  820. if (!hash_cmd && check_ie(img))
  821. ret = 0;
  822. #endif
  823. if (ret != 0) {
  824. fsl_secboot_handle_error(ERROR_ESBC_CLIENT_HASH_COMPARE_KEY);
  825. goto exit;
  826. }
  827. ret = calculate_cmp_img_sig(img);
  828. if (ret) {
  829. fsl_secboot_handle_error(ret);
  830. goto exit;
  831. }
  832. exit:
  833. /* Free Img as it was malloc'ed*/
  834. free(img);
  835. return ret;
  836. }