fsl_validate.c 25 KB

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