fsl_validate.c 23 KB

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