hash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. #ifdef USE_HOSTCC
  181. # define I_WANT_MD5 1
  182. #else
  183. # define I_WANT_MD5 CONFIG_IS_ENABLED(MD5)
  184. #endif
  185. /*
  186. * These are the hash algorithms we support. If we have hardware acceleration
  187. * is enable we will use that, otherwise a software version of the algorithm.
  188. * Note that algorithm names must be in lower case.
  189. */
  190. static struct hash_algo hash_algo[] = {
  191. #if I_WANT_MD5
  192. {
  193. .name = "md5",
  194. .digest_size = MD5_SUM_LEN,
  195. .chunk_size = CHUNKSZ_MD5,
  196. .hash_func_ws = md5_wd,
  197. },
  198. #endif
  199. #ifdef CONFIG_SHA1
  200. {
  201. .name = "sha1",
  202. .digest_size = SHA1_SUM_LEN,
  203. .chunk_size = CHUNKSZ_SHA1,
  204. #ifdef CONFIG_SHA_HW_ACCEL
  205. .hash_func_ws = hw_sha1,
  206. #else
  207. .hash_func_ws = sha1_csum_wd,
  208. #endif
  209. #ifdef CONFIG_SHA_PROG_HW_ACCEL
  210. .hash_init = hw_sha_init,
  211. .hash_update = hw_sha_update,
  212. .hash_finish = hw_sha_finish,
  213. #else
  214. .hash_init = hash_init_sha1,
  215. .hash_update = hash_update_sha1,
  216. .hash_finish = hash_finish_sha1,
  217. #endif
  218. },
  219. #endif
  220. #ifdef CONFIG_SHA256
  221. {
  222. .name = "sha256",
  223. .digest_size = SHA256_SUM_LEN,
  224. .chunk_size = CHUNKSZ_SHA256,
  225. #ifdef CONFIG_SHA_HW_ACCEL
  226. .hash_func_ws = hw_sha256,
  227. #else
  228. .hash_func_ws = sha256_csum_wd,
  229. #endif
  230. #ifdef CONFIG_SHA_PROG_HW_ACCEL
  231. .hash_init = hw_sha_init,
  232. .hash_update = hw_sha_update,
  233. .hash_finish = hw_sha_finish,
  234. #else
  235. .hash_init = hash_init_sha256,
  236. .hash_update = hash_update_sha256,
  237. .hash_finish = hash_finish_sha256,
  238. #endif
  239. },
  240. #endif
  241. #ifdef CONFIG_SHA384
  242. {
  243. .name = "sha384",
  244. .digest_size = SHA384_SUM_LEN,
  245. .chunk_size = CHUNKSZ_SHA384,
  246. #ifdef CONFIG_SHA512_HW_ACCEL
  247. .hash_func_ws = hw_sha384,
  248. #else
  249. .hash_func_ws = sha384_csum_wd,
  250. #endif
  251. #if defined(CONFIG_SHA512_HW_ACCEL) && defined(CONFIG_SHA_PROG_HW_ACCEL)
  252. .hash_init = hw_sha_init,
  253. .hash_update = hw_sha_update,
  254. .hash_finish = hw_sha_finish,
  255. #else
  256. .hash_init = hash_init_sha384,
  257. .hash_update = hash_update_sha384,
  258. .hash_finish = hash_finish_sha384,
  259. #endif
  260. },
  261. #endif
  262. #ifdef CONFIG_SHA512
  263. {
  264. .name = "sha512",
  265. .digest_size = SHA512_SUM_LEN,
  266. .chunk_size = CHUNKSZ_SHA512,
  267. #ifdef CONFIG_SHA512_HW_ACCEL
  268. .hash_func_ws = hw_sha512,
  269. #else
  270. .hash_func_ws = sha512_csum_wd,
  271. #endif
  272. #if defined(CONFIG_SHA512_HW_ACCEL) && defined(CONFIG_SHA_PROG_HW_ACCEL)
  273. .hash_init = hw_sha_init,
  274. .hash_update = hw_sha_update,
  275. .hash_finish = hw_sha_finish,
  276. #else
  277. .hash_init = hash_init_sha512,
  278. .hash_update = hash_update_sha512,
  279. .hash_finish = hash_finish_sha512,
  280. #endif
  281. },
  282. #endif
  283. {
  284. .name = "crc16-ccitt",
  285. .digest_size = 2,
  286. .chunk_size = CHUNKSZ,
  287. .hash_func_ws = crc16_ccitt_wd_buf,
  288. .hash_init = hash_init_crc16_ccitt,
  289. .hash_update = hash_update_crc16_ccitt,
  290. .hash_finish = hash_finish_crc16_ccitt,
  291. },
  292. {
  293. .name = "crc32",
  294. .digest_size = 4,
  295. .chunk_size = CHUNKSZ_CRC32,
  296. .hash_func_ws = crc32_wd_buf,
  297. .hash_init = hash_init_crc32,
  298. .hash_update = hash_update_crc32,
  299. .hash_finish = hash_finish_crc32,
  300. },
  301. };
  302. /* Try to minimize code size for boards that don't want much hashing */
  303. #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
  304. defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) || \
  305. defined(CONFIG_SHA384) || defined(CONFIG_SHA512)
  306. #define multi_hash() 1
  307. #else
  308. #define multi_hash() 0
  309. #endif
  310. static void reloc_update(void)
  311. {
  312. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  313. int i;
  314. static bool done;
  315. if (!done) {
  316. done = true;
  317. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  318. hash_algo[i].name += gd->reloc_off;
  319. hash_algo[i].hash_func_ws += gd->reloc_off;
  320. hash_algo[i].hash_init += gd->reloc_off;
  321. hash_algo[i].hash_update += gd->reloc_off;
  322. hash_algo[i].hash_finish += gd->reloc_off;
  323. }
  324. }
  325. #endif
  326. }
  327. int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
  328. {
  329. int i;
  330. reloc_update();
  331. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  332. if (!strcmp(algo_name, hash_algo[i].name)) {
  333. *algop = &hash_algo[i];
  334. return 0;
  335. }
  336. }
  337. debug("Unknown hash algorithm '%s'\n", algo_name);
  338. return -EPROTONOSUPPORT;
  339. }
  340. int hash_progressive_lookup_algo(const char *algo_name,
  341. struct hash_algo **algop)
  342. {
  343. int i;
  344. reloc_update();
  345. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  346. if (!strcmp(algo_name, hash_algo[i].name)) {
  347. if (hash_algo[i].hash_init) {
  348. *algop = &hash_algo[i];
  349. return 0;
  350. }
  351. }
  352. }
  353. debug("Unknown hash algorithm '%s'\n", algo_name);
  354. return -EPROTONOSUPPORT;
  355. }
  356. #ifndef USE_HOSTCC
  357. int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
  358. {
  359. struct hash_algo *algo;
  360. int ret;
  361. int i;
  362. ret = hash_lookup_algo(algo_name, &algo);
  363. if (ret)
  364. return ret;
  365. for (i = 0; i < algo->digest_size; i++) {
  366. char chr[3];
  367. strlcpy(chr, &str[i * 2], 3);
  368. result[i] = hextoul(chr, NULL);
  369. }
  370. return 0;
  371. }
  372. int hash_block(const char *algo_name, const void *data, unsigned int len,
  373. uint8_t *output, int *output_size)
  374. {
  375. struct hash_algo *algo;
  376. int ret;
  377. ret = hash_lookup_algo(algo_name, &algo);
  378. if (ret)
  379. return ret;
  380. if (output_size && *output_size < algo->digest_size) {
  381. debug("Output buffer size %d too small (need %d bytes)",
  382. *output_size, algo->digest_size);
  383. return -ENOSPC;
  384. }
  385. if (output_size)
  386. *output_size = algo->digest_size;
  387. algo->hash_func_ws(data, len, output, algo->chunk_size);
  388. return 0;
  389. }
  390. #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
  391. /**
  392. * store_result: Store the resulting sum to an address or variable
  393. *
  394. * @algo: Hash algorithm being used
  395. * @sum: Hash digest (algo->digest_size bytes)
  396. * @dest: Destination, interpreted as a hex address if it starts
  397. * with * (or allow_env_vars is 0) or otherwise as an
  398. * environment variable.
  399. * @allow_env_vars: non-zero to permit storing the result to an
  400. * variable environment
  401. */
  402. static void store_result(struct hash_algo *algo, const uint8_t *sum,
  403. const char *dest, int allow_env_vars)
  404. {
  405. unsigned int i;
  406. int env_var = 0;
  407. /*
  408. * If environment variables are allowed, then we assume that 'dest'
  409. * is an environment variable, unless it starts with *, in which
  410. * case we assume it is an address. If not allowed, it is always an
  411. * address. This is to support the crc32 command.
  412. */
  413. if (allow_env_vars) {
  414. if (*dest == '*')
  415. dest++;
  416. else
  417. env_var = 1;
  418. }
  419. if (env_var) {
  420. char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
  421. char *str_ptr = str_output;
  422. for (i = 0; i < algo->digest_size; i++) {
  423. sprintf(str_ptr, "%02x", sum[i]);
  424. str_ptr += 2;
  425. }
  426. *str_ptr = '\0';
  427. env_set(dest, str_output);
  428. } else {
  429. ulong addr;
  430. void *buf;
  431. addr = hextoul(dest, NULL);
  432. buf = map_sysmem(addr, algo->digest_size);
  433. memcpy(buf, sum, algo->digest_size);
  434. unmap_sysmem(buf);
  435. }
  436. }
  437. /**
  438. * parse_verify_sum: Parse a hash verification parameter
  439. *
  440. * @algo: Hash algorithm being used
  441. * @verify_str: Argument to parse. If it starts with * then it is
  442. * interpreted as a hex address containing the hash.
  443. * If the length is exactly the right number of hex digits
  444. * for the digest size, then we assume it is a hex digest.
  445. * Otherwise we assume it is an environment variable, and
  446. * look up its value (it must contain a hex digest).
  447. * @vsum: Returns binary digest value (algo->digest_size bytes)
  448. * @allow_env_vars: non-zero to permit storing the result to an environment
  449. * variable. If 0 then verify_str is assumed to be an
  450. * address, and the * prefix is not expected.
  451. * @return 0 if ok, non-zero on error
  452. */
  453. static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
  454. uint8_t *vsum, int allow_env_vars)
  455. {
  456. int env_var = 0;
  457. /* See comment above in store_result() */
  458. if (allow_env_vars) {
  459. if (*verify_str == '*')
  460. verify_str++;
  461. else
  462. env_var = 1;
  463. }
  464. if (!env_var) {
  465. ulong addr;
  466. void *buf;
  467. addr = hextoul(verify_str, NULL);
  468. buf = map_sysmem(addr, algo->digest_size);
  469. memcpy(vsum, buf, algo->digest_size);
  470. } else {
  471. char *vsum_str;
  472. int digits = algo->digest_size * 2;
  473. /*
  474. * As with the original code from sha1sum.c, we assume that a
  475. * string which matches the digest size exactly is a hex
  476. * string and not an environment variable.
  477. */
  478. if (strlen(verify_str) == digits)
  479. vsum_str = verify_str;
  480. else {
  481. vsum_str = env_get(verify_str);
  482. if (vsum_str == NULL || strlen(vsum_str) != digits) {
  483. printf("Expected %d hex digits in env var\n",
  484. digits);
  485. return 1;
  486. }
  487. }
  488. hash_parse_string(algo->name, vsum_str, vsum);
  489. }
  490. return 0;
  491. }
  492. static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
  493. {
  494. int i;
  495. printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
  496. for (i = 0; i < algo->digest_size; i++)
  497. printf("%02x", output[i]);
  498. }
  499. int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
  500. int flag, int argc, char *const argv[])
  501. {
  502. ulong addr, len;
  503. if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
  504. return CMD_RET_USAGE;
  505. addr = hextoul(*argv++, NULL);
  506. len = hextoul(*argv++, NULL);
  507. if (multi_hash()) {
  508. struct hash_algo *algo;
  509. u8 *output;
  510. uint8_t vsum[HASH_MAX_DIGEST_SIZE];
  511. void *buf;
  512. if (hash_lookup_algo(algo_name, &algo)) {
  513. printf("Unknown hash algorithm '%s'\n", algo_name);
  514. return CMD_RET_USAGE;
  515. }
  516. argc -= 2;
  517. if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
  518. puts("HASH_MAX_DIGEST_SIZE exceeded\n");
  519. return 1;
  520. }
  521. output = memalign(ARCH_DMA_MINALIGN,
  522. sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
  523. buf = map_sysmem(addr, len);
  524. algo->hash_func_ws(buf, len, output, algo->chunk_size);
  525. unmap_sysmem(buf);
  526. /* Try to avoid code bloat when verify is not needed */
  527. #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
  528. defined(CONFIG_HASH_VERIFY)
  529. if (flags & HASH_FLAG_VERIFY) {
  530. #else
  531. if (0) {
  532. #endif
  533. if (parse_verify_sum(algo, *argv, vsum,
  534. flags & HASH_FLAG_ENV)) {
  535. printf("ERROR: %s does not contain a valid "
  536. "%s sum\n", *argv, algo->name);
  537. return 1;
  538. }
  539. if (memcmp(output, vsum, algo->digest_size) != 0) {
  540. int i;
  541. hash_show(algo, addr, len, output);
  542. printf(" != ");
  543. for (i = 0; i < algo->digest_size; i++)
  544. printf("%02x", vsum[i]);
  545. puts(" ** ERROR **\n");
  546. return 1;
  547. }
  548. } else {
  549. hash_show(algo, addr, len, output);
  550. printf("\n");
  551. if (argc) {
  552. store_result(algo, output, *argv,
  553. flags & HASH_FLAG_ENV);
  554. }
  555. unmap_sysmem(output);
  556. }
  557. /* Horrible code size hack for boards that just want crc32 */
  558. } else {
  559. ulong crc;
  560. ulong *ptr;
  561. crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
  562. printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
  563. addr, addr + len - 1, crc);
  564. if (argc >= 3) {
  565. ptr = (ulong *)hextoul(argv[0], NULL);
  566. *ptr = crc;
  567. }
  568. }
  569. return 0;
  570. }
  571. #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
  572. #endif /* !USE_HOSTCC */