fsl_validate.c 25 KB

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