fsl_validate.c 25 KB

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