hash.c 15 KB

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