fsl_validate.c 25 KB

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