fsl_validate.c 24 KB

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