hash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2012 The Chromium OS Authors.
  4. *
  5. * (C) Copyright 2011
  6. * Joe Hershberger, National Instruments, joe.hershberger@ni.com
  7. *
  8. * (C) Copyright 2000
  9. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  10. */
  11. #ifndef USE_HOSTCC
  12. #include <common.h>
  13. #include <command.h>
  14. #include <env.h>
  15. #include <log.h>
  16. #include <malloc.h>
  17. #include <mapmem.h>
  18. #include <hw_sha.h>
  19. #include <asm/cache.h>
  20. #include <asm/global_data.h>
  21. #include <asm/io.h>
  22. #include <linux/errno.h>
  23. #include <u-boot/crc.h>
  24. #else
  25. #include "mkimage.h"
  26. #include <linux/compiler_attributes.h>
  27. #include <time.h>
  28. #include <linux/kconfig.h>
  29. #endif /* !USE_HOSTCC*/
  30. #include <hash.h>
  31. #include <image.h>
  32. #include <u-boot/crc.h>
  33. #include <u-boot/sha1.h>
  34. #include <u-boot/sha256.h>
  35. #include <u-boot/sha512.h>
  36. #include <u-boot/md5.h>
  37. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  38. DECLARE_GLOBAL_DATA_PTR;
  39. #endif
  40. static void reloc_update(void);
  41. static int __maybe_unused hash_init_sha1(struct hash_algo *algo, void **ctxp)
  42. {
  43. sha1_context *ctx = malloc(sizeof(sha1_context));
  44. sha1_starts(ctx);
  45. *ctxp = ctx;
  46. return 0;
  47. }
  48. static int __maybe_unused hash_update_sha1(struct hash_algo *algo, void *ctx,
  49. const void *buf, unsigned int size,
  50. int is_last)
  51. {
  52. sha1_update((sha1_context *)ctx, buf, size);
  53. return 0;
  54. }
  55. static int __maybe_unused hash_finish_sha1(struct hash_algo *algo, void *ctx,
  56. void *dest_buf, int size)
  57. {
  58. if (size < algo->digest_size)
  59. return -1;
  60. sha1_finish((sha1_context *)ctx, dest_buf);
  61. free(ctx);
  62. return 0;
  63. }
  64. static int __maybe_unused hash_init_sha256(struct hash_algo *algo, void **ctxp)
  65. {
  66. sha256_context *ctx = malloc(sizeof(sha256_context));
  67. sha256_starts(ctx);
  68. *ctxp = ctx;
  69. return 0;
  70. }
  71. static int __maybe_unused hash_update_sha256(struct hash_algo *algo, void *ctx,
  72. const void *buf, uint size,
  73. int is_last)
  74. {
  75. sha256_update((sha256_context *)ctx, buf, size);
  76. return 0;
  77. }
  78. static int __maybe_unused hash_finish_sha256(struct hash_algo *algo, void *ctx,
  79. void *dest_buf, int size)
  80. {
  81. if (size < algo->digest_size)
  82. return -1;
  83. sha256_finish((sha256_context *)ctx, dest_buf);
  84. free(ctx);
  85. return 0;
  86. }
  87. static int __maybe_unused hash_init_sha384(struct hash_algo *algo, void **ctxp)
  88. {
  89. sha512_context *ctx = malloc(sizeof(sha512_context));
  90. sha384_starts(ctx);
  91. *ctxp = ctx;
  92. return 0;
  93. }
  94. static int __maybe_unused hash_update_sha384(struct hash_algo *algo, void *ctx,
  95. const void *buf, uint size,
  96. int is_last)
  97. {
  98. sha384_update((sha512_context *)ctx, buf, size);
  99. return 0;
  100. }
  101. static int __maybe_unused hash_finish_sha384(struct hash_algo *algo, void *ctx,
  102. void *dest_buf, int size)
  103. {
  104. if (size < algo->digest_size)
  105. return -1;
  106. sha384_finish((sha512_context *)ctx, dest_buf);
  107. free(ctx);
  108. return 0;
  109. }
  110. static int __maybe_unused hash_init_sha512(struct hash_algo *algo, void **ctxp)
  111. {
  112. sha512_context *ctx = malloc(sizeof(sha512_context));
  113. sha512_starts(ctx);
  114. *ctxp = ctx;
  115. return 0;
  116. }
  117. static int __maybe_unused hash_update_sha512(struct hash_algo *algo, void *ctx,
  118. const void *buf, uint size,
  119. int is_last)
  120. {
  121. sha512_update((sha512_context *)ctx, buf, size);
  122. return 0;
  123. }
  124. static int __maybe_unused hash_finish_sha512(struct hash_algo *algo, void *ctx,
  125. void *dest_buf, int size)
  126. {
  127. if (size < algo->digest_size)
  128. return -1;
  129. sha512_finish((sha512_context *)ctx, dest_buf);
  130. free(ctx);
  131. return 0;
  132. }
  133. static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
  134. {
  135. uint16_t *ctx = malloc(sizeof(uint16_t));
  136. *ctx = 0;
  137. *ctxp = ctx;
  138. return 0;
  139. }
  140. static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
  141. const void *buf, unsigned int size,
  142. int is_last)
  143. {
  144. *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
  145. return 0;
  146. }
  147. static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
  148. void *dest_buf, int size)
  149. {
  150. if (size < algo->digest_size)
  151. return -1;
  152. *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
  153. free(ctx);
  154. return 0;
  155. }
  156. static int __maybe_unused hash_init_crc32(struct hash_algo *algo, void **ctxp)
  157. {
  158. uint32_t *ctx = malloc(sizeof(uint32_t));
  159. *ctx = 0;
  160. *ctxp = ctx;
  161. return 0;
  162. }
  163. static int __maybe_unused hash_update_crc32(struct hash_algo *algo, void *ctx,
  164. const void *buf, unsigned int size,
  165. int is_last)
  166. {
  167. *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
  168. return 0;
  169. }
  170. static int __maybe_unused hash_finish_crc32(struct hash_algo *algo, void *ctx,
  171. void *dest_buf, int size)
  172. {
  173. if (size < algo->digest_size)
  174. return -1;
  175. *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
  176. free(ctx);
  177. return 0;
  178. }
  179. /*
  180. * These are the hash algorithms we support. If we have hardware acceleration
  181. * is enable we will use that, otherwise a software version of the algorithm.
  182. * Note that algorithm names must be in lower case.
  183. */
  184. static struct hash_algo hash_algo[] = {
  185. #if CONFIG_IS_ENABLED(MD5)
  186. {
  187. .name = "md5",
  188. .digest_size = MD5_SUM_LEN,
  189. .chunk_size = CHUNKSZ_MD5,
  190. .hash_func_ws = md5_wd,
  191. },
  192. #endif
  193. #if CONFIG_IS_ENABLED(SHA1)
  194. {
  195. .name = "sha1",
  196. .digest_size = SHA1_SUM_LEN,
  197. .chunk_size = CHUNKSZ_SHA1,
  198. #if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
  199. .hash_func_ws = hw_sha1,
  200. #else
  201. .hash_func_ws = sha1_csum_wd,
  202. #endif
  203. #if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
  204. .hash_init = hw_sha_init,
  205. .hash_update = hw_sha_update,
  206. .hash_finish = hw_sha_finish,
  207. #else
  208. .hash_init = hash_init_sha1,
  209. .hash_update = hash_update_sha1,
  210. .hash_finish = hash_finish_sha1,
  211. #endif
  212. },
  213. #endif
  214. #if CONFIG_IS_ENABLED(SHA256)
  215. {
  216. .name = "sha256",
  217. .digest_size = SHA256_SUM_LEN,
  218. .chunk_size = CHUNKSZ_SHA256,
  219. #if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
  220. .hash_func_ws = hw_sha256,
  221. #else
  222. .hash_func_ws = sha256_csum_wd,
  223. #endif
  224. #if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
  225. .hash_init = hw_sha_init,
  226. .hash_update = hw_sha_update,
  227. .hash_finish = hw_sha_finish,
  228. #else
  229. .hash_init = hash_init_sha256,
  230. .hash_update = hash_update_sha256,
  231. .hash_finish = hash_finish_sha256,
  232. #endif
  233. },
  234. #endif
  235. #if CONFIG_IS_ENABLED(SHA384)
  236. {
  237. .name = "sha384",
  238. .digest_size = SHA384_SUM_LEN,
  239. .chunk_size = CHUNKSZ_SHA384,
  240. #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
  241. .hash_func_ws = hw_sha384,
  242. #else
  243. .hash_func_ws = sha384_csum_wd,
  244. #endif
  245. #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
  246. .hash_init = hw_sha_init,
  247. .hash_update = hw_sha_update,
  248. .hash_finish = hw_sha_finish,
  249. #else
  250. .hash_init = hash_init_sha384,
  251. .hash_update = hash_update_sha384,
  252. .hash_finish = hash_finish_sha384,
  253. #endif
  254. },
  255. #endif
  256. #if CONFIG_IS_ENABLED(SHA512)
  257. {
  258. .name = "sha512",
  259. .digest_size = SHA512_SUM_LEN,
  260. .chunk_size = CHUNKSZ_SHA512,
  261. #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
  262. .hash_func_ws = hw_sha512,
  263. #else
  264. .hash_func_ws = sha512_csum_wd,
  265. #endif
  266. #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
  267. .hash_init = hw_sha_init,
  268. .hash_update = hw_sha_update,
  269. .hash_finish = hw_sha_finish,
  270. #else
  271. .hash_init = hash_init_sha512,
  272. .hash_update = hash_update_sha512,
  273. .hash_finish = hash_finish_sha512,
  274. #endif
  275. },
  276. #endif
  277. {
  278. .name = "crc16-ccitt",
  279. .digest_size = 2,
  280. .chunk_size = CHUNKSZ,
  281. .hash_func_ws = crc16_ccitt_wd_buf,
  282. .hash_init = hash_init_crc16_ccitt,
  283. .hash_update = hash_update_crc16_ccitt,
  284. .hash_finish = hash_finish_crc16_ccitt,
  285. },
  286. #if CONFIG_IS_ENABLED(CRC32)
  287. {
  288. .name = "crc32",
  289. .digest_size = 4,
  290. .chunk_size = CHUNKSZ_CRC32,
  291. .hash_func_ws = crc32_wd_buf,
  292. .hash_init = hash_init_crc32,
  293. .hash_update = hash_update_crc32,
  294. .hash_finish = hash_finish_crc32,
  295. },
  296. #endif
  297. };
  298. /* Try to minimize code size for boards that don't want much hashing */
  299. #if CONFIG_IS_ENABLED(SHA256) || CONFIG_IS_ENABLED(CMD_SHA1SUM) || \
  300. CONFIG_IS_ENABLED(CRC32_VERIFY) || CONFIG_IS_ENABLED(CMD_HASH) || \
  301. CONFIG_IS_ENABLED(SHA384) || CONFIG_IS_ENABLED(SHA512)
  302. #define multi_hash() 1
  303. #else
  304. #define multi_hash() 0
  305. #endif
  306. static void reloc_update(void)
  307. {
  308. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  309. int i;
  310. static bool done;
  311. if (!done) {
  312. done = true;
  313. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  314. hash_algo[i].name += gd->reloc_off;
  315. hash_algo[i].hash_func_ws += gd->reloc_off;
  316. hash_algo[i].hash_init += gd->reloc_off;
  317. hash_algo[i].hash_update += gd->reloc_off;
  318. hash_algo[i].hash_finish += gd->reloc_off;
  319. }
  320. }
  321. #endif
  322. }
  323. int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
  324. {
  325. int i;
  326. reloc_update();
  327. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  328. if (!strcmp(algo_name, hash_algo[i].name)) {
  329. *algop = &hash_algo[i];
  330. return 0;
  331. }
  332. }
  333. debug("Unknown hash algorithm '%s'\n", algo_name);
  334. return -EPROTONOSUPPORT;
  335. }
  336. int hash_progressive_lookup_algo(const char *algo_name,
  337. struct hash_algo **algop)
  338. {
  339. int i;
  340. reloc_update();
  341. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  342. if (!strcmp(algo_name, hash_algo[i].name)) {
  343. if (hash_algo[i].hash_init) {
  344. *algop = &hash_algo[i];
  345. return 0;
  346. }
  347. }
  348. }
  349. debug("Unknown hash algorithm '%s'\n", algo_name);
  350. return -EPROTONOSUPPORT;
  351. }
  352. #ifndef USE_HOSTCC
  353. int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
  354. {
  355. struct hash_algo *algo;
  356. int ret;
  357. int i;
  358. ret = hash_lookup_algo(algo_name, &algo);
  359. if (ret)
  360. return ret;
  361. for (i = 0; i < algo->digest_size; i++) {
  362. char chr[3];
  363. strlcpy(chr, &str[i * 2], 3);
  364. result[i] = hextoul(chr, NULL);
  365. }
  366. return 0;
  367. }
  368. int hash_block(const char *algo_name, const void *data, unsigned int len,
  369. uint8_t *output, int *output_size)
  370. {
  371. struct hash_algo *algo;
  372. int ret;
  373. ret = hash_lookup_algo(algo_name, &algo);
  374. if (ret)
  375. return ret;
  376. if (output_size && *output_size < algo->digest_size) {
  377. debug("Output buffer size %d too small (need %d bytes)",
  378. *output_size, algo->digest_size);
  379. return -ENOSPC;
  380. }
  381. if (output_size)
  382. *output_size = algo->digest_size;
  383. algo->hash_func_ws(data, len, output, algo->chunk_size);
  384. return 0;
  385. }
  386. #if !defined(CONFIG_SPL_BUILD) && (defined(CONFIG_CMD_HASH) || \
  387. defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32))
  388. /**
  389. * store_result: Store the resulting sum to an address or variable
  390. *
  391. * @algo: Hash algorithm being used
  392. * @sum: Hash digest (algo->digest_size bytes)
  393. * @dest: Destination, interpreted as a hex address if it starts
  394. * with * (or allow_env_vars is 0) or otherwise as an
  395. * environment variable.
  396. * @allow_env_vars: non-zero to permit storing the result to an
  397. * variable environment
  398. */
  399. static void store_result(struct hash_algo *algo, const uint8_t *sum,
  400. const char *dest, int allow_env_vars)
  401. {
  402. unsigned int i;
  403. int env_var = 0;
  404. /*
  405. * If environment variables are allowed, then we assume that 'dest'
  406. * is an environment variable, unless it starts with *, in which
  407. * case we assume it is an address. If not allowed, it is always an
  408. * address. This is to support the crc32 command.
  409. */
  410. if (allow_env_vars) {
  411. if (*dest == '*')
  412. dest++;
  413. else
  414. env_var = 1;
  415. }
  416. if (env_var) {
  417. char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
  418. char *str_ptr = str_output;
  419. for (i = 0; i < algo->digest_size; i++) {
  420. sprintf(str_ptr, "%02x", sum[i]);
  421. str_ptr += 2;
  422. }
  423. *str_ptr = '\0';
  424. env_set(dest, str_output);
  425. } else {
  426. ulong addr;
  427. void *buf;
  428. addr = hextoul(dest, NULL);
  429. buf = map_sysmem(addr, algo->digest_size);
  430. memcpy(buf, sum, algo->digest_size);
  431. unmap_sysmem(buf);
  432. }
  433. }
  434. /**
  435. * parse_verify_sum: Parse a hash verification parameter
  436. *
  437. * @algo: Hash algorithm being used
  438. * @verify_str: Argument to parse. If it starts with * then it is
  439. * interpreted as a hex address containing the hash.
  440. * If the length is exactly the right number of hex digits
  441. * for the digest size, then we assume it is a hex digest.
  442. * Otherwise we assume it is an environment variable, and
  443. * look up its value (it must contain a hex digest).
  444. * @vsum: Returns binary digest value (algo->digest_size bytes)
  445. * @allow_env_vars: non-zero to permit storing the result to an environment
  446. * variable. If 0 then verify_str is assumed to be an
  447. * address, and the * prefix is not expected.
  448. * @return 0 if ok, non-zero on error
  449. */
  450. static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
  451. uint8_t *vsum, int allow_env_vars)
  452. {
  453. int env_var = 0;
  454. /* See comment above in store_result() */
  455. if (allow_env_vars) {
  456. if (*verify_str == '*')
  457. verify_str++;
  458. else
  459. env_var = 1;
  460. }
  461. if (!env_var) {
  462. ulong addr;
  463. void *buf;
  464. addr = hextoul(verify_str, NULL);
  465. buf = map_sysmem(addr, algo->digest_size);
  466. memcpy(vsum, buf, algo->digest_size);
  467. } else {
  468. char *vsum_str;
  469. int digits = algo->digest_size * 2;
  470. /*
  471. * As with the original code from sha1sum.c, we assume that a
  472. * string which matches the digest size exactly is a hex
  473. * string and not an environment variable.
  474. */
  475. if (strlen(verify_str) == digits)
  476. vsum_str = verify_str;
  477. else {
  478. vsum_str = env_get(verify_str);
  479. if (vsum_str == NULL || strlen(vsum_str) != digits) {
  480. printf("Expected %d hex digits in env var\n",
  481. digits);
  482. return 1;
  483. }
  484. }
  485. hash_parse_string(algo->name, vsum_str, vsum);
  486. }
  487. return 0;
  488. }
  489. static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
  490. {
  491. int i;
  492. printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
  493. for (i = 0; i < algo->digest_size; i++)
  494. printf("%02x", output[i]);
  495. }
  496. int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
  497. int flag, int argc, char *const argv[])
  498. {
  499. ulong addr, len;
  500. if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
  501. return CMD_RET_USAGE;
  502. addr = hextoul(*argv++, NULL);
  503. len = hextoul(*argv++, NULL);
  504. if (multi_hash()) {
  505. struct hash_algo *algo;
  506. u8 *output;
  507. uint8_t vsum[HASH_MAX_DIGEST_SIZE];
  508. void *buf;
  509. if (hash_lookup_algo(algo_name, &algo)) {
  510. printf("Unknown hash algorithm '%s'\n", algo_name);
  511. return CMD_RET_USAGE;
  512. }
  513. argc -= 2;
  514. if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
  515. puts("HASH_MAX_DIGEST_SIZE exceeded\n");
  516. return 1;
  517. }
  518. output = memalign(ARCH_DMA_MINALIGN,
  519. sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
  520. buf = map_sysmem(addr, len);
  521. algo->hash_func_ws(buf, len, output, algo->chunk_size);
  522. unmap_sysmem(buf);
  523. /* Try to avoid code bloat when verify is not needed */
  524. #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
  525. defined(CONFIG_HASH_VERIFY)
  526. if (flags & HASH_FLAG_VERIFY) {
  527. #else
  528. if (0) {
  529. #endif
  530. if (parse_verify_sum(algo, *argv, vsum,
  531. flags & HASH_FLAG_ENV)) {
  532. printf("ERROR: %s does not contain a valid "
  533. "%s sum\n", *argv, algo->name);
  534. return 1;
  535. }
  536. if (memcmp(output, vsum, algo->digest_size) != 0) {
  537. int i;
  538. hash_show(algo, addr, len, output);
  539. printf(" != ");
  540. for (i = 0; i < algo->digest_size; i++)
  541. printf("%02x", vsum[i]);
  542. puts(" ** ERROR **\n");
  543. return 1;
  544. }
  545. } else {
  546. hash_show(algo, addr, len, output);
  547. printf("\n");
  548. if (argc) {
  549. store_result(algo, output, *argv,
  550. flags & HASH_FLAG_ENV);
  551. }
  552. unmap_sysmem(output);
  553. }
  554. /* Horrible code size hack for boards that just want crc32 */
  555. } else {
  556. ulong crc;
  557. ulong *ptr;
  558. crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
  559. printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
  560. addr, addr + len - 1, crc);
  561. if (argc >= 3) {
  562. ptr = (ulong *)hextoul(argv[0], NULL);
  563. *ptr = crc;
  564. }
  565. }
  566. return 0;
  567. }
  568. #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
  569. #endif /* !USE_HOSTCC */