fsl_validate.c 22 KB

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