fsl_validate.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * Copyright 2015 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.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 <dm/uclass.h>
  14. #include <u-boot/rsa-mod-exp.h>
  15. #include <hash.h>
  16. #include <fsl_secboot_err.h>
  17. #ifdef CONFIG_LS102XA
  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. 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. /* If Boot Mode is secure, transition the SNVS state and issue
  335. * reset based on type of failure and ITS setting.
  336. * If Boot mode is non-secure, return from this function.
  337. */
  338. if (fsl_check_boot_mode_secure() == 0)
  339. return;
  340. switch (error) {
  341. case ERROR_ESBC_CLIENT_HEADER_BARKER:
  342. case ERROR_ESBC_CLIENT_HEADER_IMG_SIZE:
  343. case ERROR_ESBC_CLIENT_HEADER_KEY_LEN:
  344. case ERROR_ESBC_CLIENT_HEADER_SIG_LEN:
  345. case ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN:
  346. case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1:
  347. case ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2:
  348. case ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD:
  349. case ERROR_ESBC_CLIENT_HEADER_SG_ESBC_EP:
  350. case ERROR_ESBC_CLIENT_HEADER_SG_ENTIRES_BAD:
  351. case ERROR_KEY_TABLE_NOT_FOUND:
  352. #ifdef CONFIG_KEY_REVOCATION
  353. case ERROR_ESBC_CLIENT_HEADER_KEY_REVOKED:
  354. case ERROR_ESBC_CLIENT_HEADER_INVALID_SRK_NUM_ENTRY:
  355. case ERROR_ESBC_CLIENT_HEADER_INVALID_KEY_NUM:
  356. case ERROR_ESBC_CLIENT_HEADER_INV_SRK_ENTRY_KEYLEN:
  357. #endif
  358. #if defined(CONFIG_FSL_ISBC_KEY_EXT)
  359. /*@fallthrough@*/
  360. case ERROR_ESBC_CLIENT_HEADER_IE_KEY_REVOKED:
  361. case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_NUM_ENTRY:
  362. case ERROR_ESBC_CLIENT_HEADER_INVALID_IE_KEY_NUM:
  363. case ERROR_ESBC_CLIENT_HEADER_INV_IE_ENTRY_KEYLEN:
  364. case ERROR_IE_TABLE_NOT_FOUND:
  365. #endif
  366. fsl_secboot_header_verification_failure();
  367. break;
  368. case ERROR_ESBC_SEC_RESET:
  369. case ERROR_ESBC_SEC_DEQ:
  370. case ERROR_ESBC_SEC_ENQ:
  371. case ERROR_ESBC_SEC_DEQ_TO:
  372. case ERROR_ESBC_SEC_JOBQ_STATUS:
  373. case ERROR_ESBC_CLIENT_HASH_COMPARE_KEY:
  374. case ERROR_ESBC_CLIENT_HASH_COMPARE_EM:
  375. fsl_secboot_image_verification_failure();
  376. break;
  377. case ERROR_ESBC_MISSING_BOOTM:
  378. fsl_secboot_bootscript_parse_failure();
  379. break;
  380. case ERROR_ESBC_WRONG_CMD:
  381. default:
  382. branch_to_self();
  383. break;
  384. }
  385. }
  386. static void fsl_secblk_handle_error(int error)
  387. {
  388. switch (error) {
  389. case ERROR_ESBC_SEC_ENQ:
  390. fsl_secboot_handle_error(ERROR_ESBC_SEC_ENQ);
  391. break;
  392. case ERROR_ESBC_SEC_DEQ:
  393. fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ);
  394. break;
  395. case ERROR_ESBC_SEC_DEQ_TO:
  396. fsl_secboot_handle_error(ERROR_ESBC_SEC_DEQ_TO);
  397. break;
  398. default:
  399. printf("Job Queue Output status %x\n", error);
  400. fsl_secboot_handle_error(ERROR_ESBC_SEC_JOBQ_STATUS);
  401. break;
  402. }
  403. }
  404. /*
  405. * Calculate hash of key obtained via offset present in ESBC uboot
  406. * client hdr. This function calculates the hash of key which is obtained
  407. * through offset present in ESBC uboot client header.
  408. */
  409. static int calc_img_key_hash(struct fsl_secboot_img_priv *img)
  410. {
  411. struct hash_algo *algo;
  412. void *ctx;
  413. int i, srk = 0;
  414. int ret = 0;
  415. const char *algo_name = "sha256";
  416. /* Calculate hash of the esbc key */
  417. ret = hash_progressive_lookup_algo(algo_name, &algo);
  418. if (ret)
  419. return ret;
  420. ret = algo->hash_init(algo, &ctx);
  421. if (ret)
  422. return ret;
  423. /* Update hash for ESBC key */
  424. #ifdef CONFIG_KEY_REVOCATION
  425. if (check_srk(img)) {
  426. ret = algo->hash_update(algo, ctx,
  427. (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
  428. img->hdr.len_kr.num_srk * sizeof(struct srk_table), 1);
  429. srk = 1;
  430. }
  431. #endif
  432. if (!srk)
  433. ret = algo->hash_update(algo, ctx,
  434. img->img_key, img->key_len, 1);
  435. if (ret)
  436. return ret;
  437. /* Copy hash at destination buffer */
  438. ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
  439. if (ret)
  440. return ret;
  441. for (i = 0; i < SHA256_BYTES; i++)
  442. img->img_key_hash[i] = hash_val[i];
  443. return 0;
  444. }
  445. /*
  446. * Calculate hash of ESBC hdr and ESBC. This function calculates the
  447. * single hash of ESBC header and ESBC image. If SG flag is on, all
  448. * SG entries are also hashed alongwith the complete SG table.
  449. */
  450. static int calc_esbchdr_esbc_hash(struct fsl_secboot_img_priv *img)
  451. {
  452. struct hash_algo *algo;
  453. void *ctx;
  454. int ret = 0;
  455. int key_hash = 0;
  456. const char *algo_name = "sha256";
  457. /* Calculate the hash of the ESBC */
  458. ret = hash_progressive_lookup_algo(algo_name, &algo);
  459. if (ret)
  460. return ret;
  461. ret = algo->hash_init(algo, &ctx);
  462. /* Copy hash at destination buffer */
  463. if (ret)
  464. return ret;
  465. /* Update hash for CSF Header */
  466. ret = algo->hash_update(algo, ctx,
  467. (u8 *)&img->hdr, sizeof(struct fsl_secboot_img_hdr), 0);
  468. if (ret)
  469. return ret;
  470. /* Update the hash with that of srk table if srk flag is 1
  471. * If IE Table is selected, key is not added in the hash
  472. * If neither srk table nor IE key table available, add key
  473. * from header in the hash calculation
  474. */
  475. #ifdef CONFIG_KEY_REVOCATION
  476. if (check_srk(img)) {
  477. ret = algo->hash_update(algo, ctx,
  478. (u8 *)(uintptr_t)(img->ehdrloc + img->hdr.srk_tbl_off),
  479. img->hdr.len_kr.num_srk * sizeof(struct srk_table), 0);
  480. key_hash = 1;
  481. }
  482. #endif
  483. #if defined(CONFIG_FSL_ISBC_KEY_EXT)
  484. if (!key_hash && check_ie(img))
  485. key_hash = 1;
  486. #endif
  487. #ifndef CONFIG_ESBC_HDR_LS
  488. /* No single key support in LS ESBC header */
  489. if (!key_hash) {
  490. ret = algo->hash_update(algo, ctx,
  491. img->img_key, img->hdr.key_len, 0);
  492. key_hash = 1;
  493. }
  494. #endif
  495. if (ret)
  496. return ret;
  497. if (!key_hash)
  498. return ERROR_KEY_TABLE_NOT_FOUND;
  499. /* Update hash for actual Image */
  500. ret = algo->hash_update(algo, ctx,
  501. (u8 *)(*(img->img_addr_ptr)), img->img_size, 1);
  502. if (ret)
  503. return ret;
  504. /* Copy hash at destination buffer */
  505. ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size);
  506. if (ret)
  507. return ret;
  508. return 0;
  509. }
  510. /*
  511. * Construct encoded hash EM' wrt PKCSv1.5. This function calculates the
  512. * pointers for padding, DER value and hash. And finally, constructs EM'
  513. * which includes hash of complete CSF header and ESBC image. If SG flag
  514. * is on, hash of SG table and entries is also included.
  515. */
  516. static void construct_img_encoded_hash_second(struct fsl_secboot_img_priv *img)
  517. {
  518. /*
  519. * RSA PKCSv1.5 encoding format for encoded message is below
  520. * EM = 0x0 || 0x1 || PS || 0x0 || DER || Hash
  521. * PS is Padding String
  522. * DER is DER value for SHA-256
  523. * Hash is SHA-256 hash
  524. * *********************************************************
  525. * representative points to first byte of EM initially and is
  526. * filled with 0x0
  527. * representative is incremented by 1 and second byte is filled
  528. * with 0x1
  529. * padding points to third byte of EM
  530. * digest points to full length of EM - 32 bytes
  531. * hash_id (DER value) points to 19 bytes before pDigest
  532. * separator is one byte which separates padding and DER
  533. */
  534. size_t len;
  535. u8 *representative;
  536. u8 *padding, *digest;
  537. u8 *hash_id, *separator;
  538. int i;
  539. len = (get_key_len(img) / 2) - 1;
  540. representative = img->img_encoded_hash_second;
  541. representative[0] = 0;
  542. representative[1] = 1; /* block type 1 */
  543. padding = &representative[2];
  544. digest = &representative[1] + len - 32;
  545. hash_id = digest - sizeof(hash_identifier);
  546. separator = hash_id - 1;
  547. /* fill padding area pointed by padding with 0xff */
  548. memset(padding, 0xff, separator - padding);
  549. /* fill byte pointed by separator */
  550. *separator = 0;
  551. /* fill SHA-256 DER value pointed by HashId */
  552. memcpy(hash_id, hash_identifier, sizeof(hash_identifier));
  553. /* fill hash pointed by Digest */
  554. for (i = 0; i < SHA256_BYTES; i++)
  555. digest[i] = hash_val[i];
  556. }
  557. /*
  558. * Reads and validates the ESBC client header.
  559. * This function reads key and signature from the ESBC client header.
  560. * If Scatter/Gather flag is on, lengths and offsets of images
  561. * present as SG entries are also read. This function also checks
  562. * whether the header is valid or not.
  563. */
  564. static int read_validate_esbc_client_header(struct fsl_secboot_img_priv *img)
  565. {
  566. struct fsl_secboot_img_hdr *hdr = &img->hdr;
  567. void *esbc = (u8 *)(uintptr_t)img->ehdrloc;
  568. u8 *k, *s;
  569. u32 ret = 0;
  570. int key_found = 0;
  571. /* check barker code */
  572. if (memcmp(hdr->barker, barker_code, ESBC_BARKER_LEN))
  573. return ERROR_ESBC_CLIENT_HEADER_BARKER;
  574. /* If Image Address is not passed as argument to function,
  575. * then Address and Size must be read from the Header.
  576. */
  577. if (*(img->img_addr_ptr) == 0) {
  578. #ifdef CONFIG_ESBC_ADDR_64BIT
  579. *(img->img_addr_ptr) = hdr->pimg64;
  580. #else
  581. *(img->img_addr_ptr) = hdr->pimg;
  582. #endif
  583. }
  584. if (!hdr->img_size)
  585. return ERROR_ESBC_CLIENT_HEADER_IMG_SIZE;
  586. img->img_size = hdr->img_size;
  587. /* Key checking*/
  588. #ifdef CONFIG_KEY_REVOCATION
  589. if (check_srk(img)) {
  590. ret = read_validate_srk_tbl(img);
  591. if (ret != 0)
  592. return ret;
  593. key_found = 1;
  594. }
  595. #endif
  596. #if defined(CONFIG_FSL_ISBC_KEY_EXT)
  597. if (!key_found && check_ie(img)) {
  598. ret = read_validate_ie_tbl(img);
  599. if (ret != 0)
  600. return ret;
  601. key_found = 1;
  602. }
  603. #endif
  604. #ifndef CONFIG_ESBC_HDR_LS
  605. /* Single Key Feature not available in LS ESBC Header */
  606. if (key_found == 0) {
  607. ret = read_validate_single_key(img);
  608. if (ret != 0)
  609. return ret;
  610. key_found = 1;
  611. }
  612. #endif
  613. if (!key_found)
  614. return ERROR_KEY_TABLE_NOT_FOUND;
  615. /* check signaure */
  616. if (get_key_len(img) == 2 * hdr->sign_len) {
  617. /* check signature length */
  618. if (!((hdr->sign_len == KEY_SIZE_BYTES / 4) ||
  619. (hdr->sign_len == KEY_SIZE_BYTES / 2) ||
  620. (hdr->sign_len == KEY_SIZE_BYTES)))
  621. return ERROR_ESBC_CLIENT_HEADER_SIG_LEN;
  622. } else {
  623. return ERROR_ESBC_CLIENT_HEADER_KEY_LEN_NOT_TWICE_SIG_LEN;
  624. }
  625. memcpy(&img->img_sign, esbc + hdr->psign, hdr->sign_len);
  626. /* No SG support in LS-CH3 */
  627. #ifndef CONFIG_ESBC_HDR_LS
  628. /* No SG support */
  629. if (hdr->sg_flag)
  630. return ERROR_ESBC_CLIENT_HEADER_SG;
  631. #endif
  632. /* modulus most significant bit should be set */
  633. k = (u8 *)&img->img_key;
  634. if ((k[0] & 0x80) == 0)
  635. return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_1;
  636. /* modulus value should be odd */
  637. if ((k[get_key_len(img) / 2 - 1] & 0x1) == 0)
  638. return ERROR_ESBC_CLIENT_HEADER_KEY_MOD_2;
  639. /* Check signature value < modulus value */
  640. s = (u8 *)&img->img_sign;
  641. if (!(memcmp(s, k, hdr->sign_len) < 0))
  642. return ERROR_ESBC_CLIENT_HEADER_SIG_KEY_MOD;
  643. return ESBC_VALID_HDR;
  644. }
  645. static inline int str2longbe(const char *p, ulong *num)
  646. {
  647. char *endptr;
  648. ulong tmp;
  649. if (!p) {
  650. return 0;
  651. } else {
  652. tmp = simple_strtoul(p, &endptr, 16);
  653. if (sizeof(ulong) == 4)
  654. *num = cpu_to_be32(tmp);
  655. else
  656. *num = cpu_to_be64(tmp);
  657. }
  658. return *p != '\0' && *endptr == '\0';
  659. }
  660. /* Function to calculate the ESBC Image Hash
  661. * and hash from Digital signature.
  662. * The Two hash's are compared to yield the
  663. * result of signature validation.
  664. */
  665. static int calculate_cmp_img_sig(struct fsl_secboot_img_priv *img)
  666. {
  667. int ret;
  668. uint32_t key_len;
  669. struct key_prop prop;
  670. #if !defined(USE_HOSTCC)
  671. struct udevice *mod_exp_dev;
  672. #endif
  673. ret = calc_esbchdr_esbc_hash(img);
  674. if (ret)
  675. return ret;
  676. /* Construct encoded hash EM' wrt PKCSv1.5 */
  677. construct_img_encoded_hash_second(img);
  678. /* Fill prop structure for public key */
  679. memset(&prop, 0, sizeof(struct key_prop));
  680. key_len = get_key_len(img) / 2;
  681. prop.modulus = img->img_key;
  682. prop.public_exponent = img->img_key + key_len;
  683. prop.num_bits = key_len * 8;
  684. prop.exp_len = key_len;
  685. ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev);
  686. if (ret) {
  687. printf("RSA: Can't find Modular Exp implementation\n");
  688. return -EINVAL;
  689. }
  690. ret = rsa_mod_exp(mod_exp_dev, img->img_sign, img->hdr.sign_len,
  691. &prop, img->img_encoded_hash);
  692. if (ret)
  693. return ret;
  694. /*
  695. * compare the encoded messages EM' and EM wrt RSA PKCSv1.5
  696. * memcmp returns zero on success
  697. * memcmp returns non-zero on failure
  698. */
  699. ret = memcmp(&img->img_encoded_hash_second, &img->img_encoded_hash,
  700. img->hdr.sign_len);
  701. if (ret)
  702. return ERROR_ESBC_CLIENT_HASH_COMPARE_EM;
  703. return 0;
  704. }
  705. /* Function to initialize img priv and global data structure
  706. */
  707. static int secboot_init(struct fsl_secboot_img_priv **img_ptr)
  708. {
  709. *img_ptr = malloc(sizeof(struct fsl_secboot_img_priv));
  710. struct fsl_secboot_img_priv *img = *img_ptr;
  711. if (!img)
  712. return -ENOMEM;
  713. memset(img, 0, sizeof(struct fsl_secboot_img_priv));
  714. #if defined(CONFIG_FSL_ISBC_KEY_EXT)
  715. if (glb.ie_addr)
  716. img->ie_addr = glb.ie_addr;
  717. #endif
  718. return 0;
  719. }
  720. /* haddr - Address of the header of image to be validated.
  721. * arg_hash_str - Option hash string. If provided, this
  722. * overrides the key hash in the SFP fuses.
  723. * img_addr_ptr - Optional pointer to address of image to be validated.
  724. * If non zero addr, this overrides the addr of image in header,
  725. * otherwise updated to image addr in header.
  726. * Acts as both input and output of function.
  727. * This pointer shouldn't be NULL.
  728. */
  729. int fsl_secboot_validate(uintptr_t haddr, char *arg_hash_str,
  730. uintptr_t *img_addr_ptr)
  731. {
  732. struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
  733. ulong hash[SHA256_BYTES/sizeof(ulong)];
  734. char hash_str[NUM_HEX_CHARS + 1];
  735. struct fsl_secboot_img_priv *img;
  736. struct fsl_secboot_img_hdr *hdr;
  737. void *esbc;
  738. int ret, i, hash_cmd = 0;
  739. u32 srk_hash[8];
  740. if (arg_hash_str != NULL) {
  741. const char *cp = arg_hash_str;
  742. int i = 0;
  743. if (*cp == '0' && *(cp + 1) == 'x')
  744. cp += 2;
  745. /* The input string expected is in hex, where
  746. * each 4 bits would be represented by a hex
  747. * sha256 hash is 256 bits long, which would mean
  748. * num of characters = 256 / 4
  749. */
  750. if (strlen(cp) != SHA256_NIBBLES) {
  751. printf("%s is not a 256 bits hex string as expected\n",
  752. arg_hash_str);
  753. return -1;
  754. }
  755. for (i = 0; i < sizeof(hash)/sizeof(ulong); i++) {
  756. strncpy(hash_str, cp + (i * NUM_HEX_CHARS),
  757. NUM_HEX_CHARS);
  758. hash_str[NUM_HEX_CHARS] = '\0';
  759. if (!str2longbe(hash_str, &hash[i])) {
  760. printf("%s is not a 256 bits hex string ",
  761. arg_hash_str);
  762. return -1;
  763. }
  764. }
  765. hash_cmd = 1;
  766. }
  767. ret = secboot_init(&img);
  768. if (ret)
  769. goto exit;
  770. /* Update the information in Private Struct */
  771. hdr = &img->hdr;
  772. img->ehdrloc = haddr;
  773. img->img_addr_ptr = img_addr_ptr;
  774. esbc = (u8 *)img->ehdrloc;
  775. memcpy(hdr, esbc, sizeof(struct fsl_secboot_img_hdr));
  776. /* read and validate esbc header */
  777. ret = read_validate_esbc_client_header(img);
  778. if (ret != ESBC_VALID_HDR) {
  779. fsl_secboot_handle_error(ret);
  780. goto exit;
  781. }
  782. /* SRKH present in SFP */
  783. for (i = 0; i < NUM_SRKH_REGS; i++)
  784. srk_hash[i] = srk_in32(&sfp_regs->srk_hash[i]);
  785. /*
  786. * Calculate hash of key obtained via offset present in
  787. * ESBC uboot client hdr
  788. */
  789. ret = calc_img_key_hash(img);
  790. if (ret) {
  791. fsl_secblk_handle_error(ret);
  792. goto exit;
  793. }
  794. /* Compare hash obtained above with SRK hash present in SFP */
  795. if (hash_cmd)
  796. ret = memcmp(&hash, &img->img_key_hash, SHA256_BYTES);
  797. else
  798. ret = memcmp(srk_hash, img->img_key_hash, SHA256_BYTES);
  799. #if defined(CONFIG_FSL_ISBC_KEY_EXT)
  800. if (!hash_cmd && check_ie(img))
  801. ret = 0;
  802. #endif
  803. if (ret != 0) {
  804. fsl_secboot_handle_error(ERROR_ESBC_CLIENT_HASH_COMPARE_KEY);
  805. goto exit;
  806. }
  807. ret = calculate_cmp_img_sig(img);
  808. if (ret) {
  809. fsl_secboot_handle_error(ret);
  810. goto exit;
  811. }
  812. exit:
  813. /* Free Img as it was malloc'ed*/
  814. free(img);
  815. return ret;
  816. }