hash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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/io.h>
  21. #include <linux/errno.h>
  22. #include <u-boot/crc.h>
  23. #else
  24. #include "mkimage.h"
  25. #include <time.h>
  26. #endif /* !USE_HOSTCC*/
  27. #include <hash.h>
  28. #include <image.h>
  29. #include <u-boot/crc.h>
  30. #include <u-boot/sha1.h>
  31. #include <u-boot/sha256.h>
  32. #include <u-boot/sha512.h>
  33. #include <u-boot/md5.h>
  34. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  35. DECLARE_GLOBAL_DATA_PTR;
  36. #endif
  37. static void reloc_update(void);
  38. #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
  39. static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
  40. {
  41. sha1_context *ctx = malloc(sizeof(sha1_context));
  42. sha1_starts(ctx);
  43. *ctxp = ctx;
  44. return 0;
  45. }
  46. static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
  47. unsigned int size, int is_last)
  48. {
  49. sha1_update((sha1_context *)ctx, buf, size);
  50. return 0;
  51. }
  52. static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
  53. int size)
  54. {
  55. if (size < algo->digest_size)
  56. return -1;
  57. sha1_finish((sha1_context *)ctx, dest_buf);
  58. free(ctx);
  59. return 0;
  60. }
  61. #endif
  62. #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
  63. static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
  64. {
  65. sha256_context *ctx = malloc(sizeof(sha256_context));
  66. sha256_starts(ctx);
  67. *ctxp = ctx;
  68. return 0;
  69. }
  70. static int hash_update_sha256(struct hash_algo *algo, void *ctx,
  71. const void *buf, unsigned int size, int is_last)
  72. {
  73. sha256_update((sha256_context *)ctx, buf, size);
  74. return 0;
  75. }
  76. static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
  77. *dest_buf, int size)
  78. {
  79. if (size < algo->digest_size)
  80. return -1;
  81. sha256_finish((sha256_context *)ctx, dest_buf);
  82. free(ctx);
  83. return 0;
  84. }
  85. #endif
  86. #if defined(CONFIG_SHA384)
  87. static int 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 hash_update_sha384(struct hash_algo *algo, void *ctx,
  95. const void *buf, unsigned int size, int is_last)
  96. {
  97. sha384_update((sha512_context *)ctx, buf, size);
  98. return 0;
  99. }
  100. static int hash_finish_sha384(struct hash_algo *algo, void *ctx, void
  101. *dest_buf, int size)
  102. {
  103. if (size < algo->digest_size)
  104. return -1;
  105. sha384_finish((sha512_context *)ctx, dest_buf);
  106. free(ctx);
  107. return 0;
  108. }
  109. #endif
  110. #if defined(CONFIG_SHA512)
  111. static int hash_init_sha512(struct hash_algo *algo, void **ctxp)
  112. {
  113. sha512_context *ctx = malloc(sizeof(sha512_context));
  114. sha512_starts(ctx);
  115. *ctxp = ctx;
  116. return 0;
  117. }
  118. static int hash_update_sha512(struct hash_algo *algo, void *ctx,
  119. const void *buf, unsigned int size, int is_last)
  120. {
  121. sha512_update((sha512_context *)ctx, buf, size);
  122. return 0;
  123. }
  124. static int hash_finish_sha512(struct hash_algo *algo, void *ctx, void
  125. *dest_buf, int size)
  126. {
  127. if (size < algo->digest_size)
  128. return -1;
  129. printf("hello world\n");
  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. .hash_func_ws = sha384_csum_wd,
  234. .hash_init = hash_init_sha384,
  235. .hash_update = hash_update_sha384,
  236. .hash_finish = hash_finish_sha384,
  237. },
  238. #endif
  239. #ifdef CONFIG_SHA512
  240. {
  241. .name = "sha512",
  242. .digest_size = SHA512_SUM_LEN,
  243. .chunk_size = CHUNKSZ_SHA512,
  244. .hash_func_ws = sha512_csum_wd,
  245. .hash_init = hash_init_sha512,
  246. .hash_update = hash_update_sha512,
  247. .hash_finish = hash_finish_sha512,
  248. },
  249. #endif
  250. {
  251. .name = "crc16-ccitt",
  252. .digest_size = 2,
  253. .chunk_size = CHUNKSZ,
  254. .hash_func_ws = crc16_ccitt_wd_buf,
  255. .hash_init = hash_init_crc16_ccitt,
  256. .hash_update = hash_update_crc16_ccitt,
  257. .hash_finish = hash_finish_crc16_ccitt,
  258. },
  259. {
  260. .name = "crc32",
  261. .digest_size = 4,
  262. .chunk_size = CHUNKSZ_CRC32,
  263. .hash_func_ws = crc32_wd_buf,
  264. .hash_init = hash_init_crc32,
  265. .hash_update = hash_update_crc32,
  266. .hash_finish = hash_finish_crc32,
  267. },
  268. };
  269. /* Try to minimize code size for boards that don't want much hashing */
  270. #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
  271. defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) || \
  272. defined(CONFIG_SHA384) || defined(CONFIG_SHA512)
  273. #define multi_hash() 1
  274. #else
  275. #define multi_hash() 0
  276. #endif
  277. static void reloc_update(void)
  278. {
  279. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  280. int i;
  281. static bool done;
  282. if (!done) {
  283. done = true;
  284. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  285. hash_algo[i].name += gd->reloc_off;
  286. hash_algo[i].hash_func_ws += gd->reloc_off;
  287. hash_algo[i].hash_init += gd->reloc_off;
  288. hash_algo[i].hash_update += gd->reloc_off;
  289. hash_algo[i].hash_finish += gd->reloc_off;
  290. }
  291. }
  292. #endif
  293. }
  294. int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
  295. {
  296. int i;
  297. reloc_update();
  298. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  299. if (!strcmp(algo_name, hash_algo[i].name)) {
  300. *algop = &hash_algo[i];
  301. return 0;
  302. }
  303. }
  304. debug("Unknown hash algorithm '%s'\n", algo_name);
  305. return -EPROTONOSUPPORT;
  306. }
  307. int hash_progressive_lookup_algo(const char *algo_name,
  308. struct hash_algo **algop)
  309. {
  310. int i;
  311. reloc_update();
  312. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  313. if (!strcmp(algo_name, hash_algo[i].name)) {
  314. if (hash_algo[i].hash_init) {
  315. *algop = &hash_algo[i];
  316. return 0;
  317. }
  318. }
  319. }
  320. debug("Unknown hash algorithm '%s'\n", algo_name);
  321. return -EPROTONOSUPPORT;
  322. }
  323. #ifndef USE_HOSTCC
  324. int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
  325. {
  326. struct hash_algo *algo;
  327. int ret;
  328. int i;
  329. ret = hash_lookup_algo(algo_name, &algo);
  330. if (ret)
  331. return ret;
  332. for (i = 0; i < algo->digest_size; i++) {
  333. char chr[3];
  334. strncpy(chr, &str[i * 2], 2);
  335. result[i] = simple_strtoul(chr, NULL, 16);
  336. }
  337. return 0;
  338. }
  339. int hash_block(const char *algo_name, const void *data, unsigned int len,
  340. uint8_t *output, int *output_size)
  341. {
  342. struct hash_algo *algo;
  343. int ret;
  344. ret = hash_lookup_algo(algo_name, &algo);
  345. if (ret)
  346. return ret;
  347. if (output_size && *output_size < algo->digest_size) {
  348. debug("Output buffer size %d too small (need %d bytes)",
  349. *output_size, algo->digest_size);
  350. return -ENOSPC;
  351. }
  352. if (output_size)
  353. *output_size = algo->digest_size;
  354. algo->hash_func_ws(data, len, output, algo->chunk_size);
  355. return 0;
  356. }
  357. #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
  358. /**
  359. * store_result: Store the resulting sum to an address or variable
  360. *
  361. * @algo: Hash algorithm being used
  362. * @sum: Hash digest (algo->digest_size bytes)
  363. * @dest: Destination, interpreted as a hex address if it starts
  364. * with * (or allow_env_vars is 0) or otherwise as an
  365. * environment variable.
  366. * @allow_env_vars: non-zero to permit storing the result to an
  367. * variable environment
  368. */
  369. static void store_result(struct hash_algo *algo, const uint8_t *sum,
  370. const char *dest, int allow_env_vars)
  371. {
  372. unsigned int i;
  373. int env_var = 0;
  374. /*
  375. * If environment variables are allowed, then we assume that 'dest'
  376. * is an environment variable, unless it starts with *, in which
  377. * case we assume it is an address. If not allowed, it is always an
  378. * address. This is to support the crc32 command.
  379. */
  380. if (allow_env_vars) {
  381. if (*dest == '*')
  382. dest++;
  383. else
  384. env_var = 1;
  385. }
  386. if (env_var) {
  387. char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
  388. char *str_ptr = str_output;
  389. for (i = 0; i < algo->digest_size; i++) {
  390. sprintf(str_ptr, "%02x", sum[i]);
  391. str_ptr += 2;
  392. }
  393. *str_ptr = '\0';
  394. env_set(dest, str_output);
  395. } else {
  396. ulong addr;
  397. void *buf;
  398. addr = simple_strtoul(dest, NULL, 16);
  399. buf = map_sysmem(addr, algo->digest_size);
  400. memcpy(buf, sum, algo->digest_size);
  401. unmap_sysmem(buf);
  402. }
  403. }
  404. /**
  405. * parse_verify_sum: Parse a hash verification parameter
  406. *
  407. * @algo: Hash algorithm being used
  408. * @verify_str: Argument to parse. If it starts with * then it is
  409. * interpreted as a hex address containing the hash.
  410. * If the length is exactly the right number of hex digits
  411. * for the digest size, then we assume it is a hex digest.
  412. * Otherwise we assume it is an environment variable, and
  413. * look up its value (it must contain a hex digest).
  414. * @vsum: Returns binary digest value (algo->digest_size bytes)
  415. * @allow_env_vars: non-zero to permit storing the result to an environment
  416. * variable. If 0 then verify_str is assumed to be an
  417. * address, and the * prefix is not expected.
  418. * @return 0 if ok, non-zero on error
  419. */
  420. static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
  421. uint8_t *vsum, int allow_env_vars)
  422. {
  423. int env_var = 0;
  424. /* See comment above in store_result() */
  425. if (allow_env_vars) {
  426. if (*verify_str == '*')
  427. verify_str++;
  428. else
  429. env_var = 1;
  430. }
  431. if (!env_var) {
  432. ulong addr;
  433. void *buf;
  434. addr = simple_strtoul(verify_str, NULL, 16);
  435. buf = map_sysmem(addr, algo->digest_size);
  436. memcpy(vsum, buf, algo->digest_size);
  437. } else {
  438. char *vsum_str;
  439. int digits = algo->digest_size * 2;
  440. /*
  441. * As with the original code from sha1sum.c, we assume that a
  442. * string which matches the digest size exactly is a hex
  443. * string and not an environment variable.
  444. */
  445. if (strlen(verify_str) == digits)
  446. vsum_str = verify_str;
  447. else {
  448. vsum_str = env_get(verify_str);
  449. if (vsum_str == NULL || strlen(vsum_str) != digits) {
  450. printf("Expected %d hex digits in env var\n",
  451. digits);
  452. return 1;
  453. }
  454. }
  455. hash_parse_string(algo->name, vsum_str, vsum);
  456. }
  457. return 0;
  458. }
  459. static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
  460. {
  461. int i;
  462. printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
  463. for (i = 0; i < algo->digest_size; i++)
  464. printf("%02x", output[i]);
  465. }
  466. int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
  467. int flag, int argc, char *const argv[])
  468. {
  469. ulong addr, len;
  470. if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
  471. return CMD_RET_USAGE;
  472. addr = simple_strtoul(*argv++, NULL, 16);
  473. len = simple_strtoul(*argv++, NULL, 16);
  474. if (multi_hash()) {
  475. struct hash_algo *algo;
  476. u8 *output;
  477. uint8_t vsum[HASH_MAX_DIGEST_SIZE];
  478. void *buf;
  479. if (hash_lookup_algo(algo_name, &algo)) {
  480. printf("Unknown hash algorithm '%s'\n", algo_name);
  481. return CMD_RET_USAGE;
  482. }
  483. argc -= 2;
  484. if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
  485. puts("HASH_MAX_DIGEST_SIZE exceeded\n");
  486. return 1;
  487. }
  488. output = memalign(ARCH_DMA_MINALIGN,
  489. sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
  490. buf = map_sysmem(addr, len);
  491. algo->hash_func_ws(buf, len, output, algo->chunk_size);
  492. unmap_sysmem(buf);
  493. /* Try to avoid code bloat when verify is not needed */
  494. #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
  495. defined(CONFIG_HASH_VERIFY)
  496. if (flags & HASH_FLAG_VERIFY) {
  497. #else
  498. if (0) {
  499. #endif
  500. if (parse_verify_sum(algo, *argv, vsum,
  501. flags & HASH_FLAG_ENV)) {
  502. printf("ERROR: %s does not contain a valid "
  503. "%s sum\n", *argv, algo->name);
  504. return 1;
  505. }
  506. if (memcmp(output, vsum, algo->digest_size) != 0) {
  507. int i;
  508. hash_show(algo, addr, len, output);
  509. printf(" != ");
  510. for (i = 0; i < algo->digest_size; i++)
  511. printf("%02x", vsum[i]);
  512. puts(" ** ERROR **\n");
  513. return 1;
  514. }
  515. } else {
  516. hash_show(algo, addr, len, output);
  517. printf("\n");
  518. if (argc) {
  519. store_result(algo, output, *argv,
  520. flags & HASH_FLAG_ENV);
  521. }
  522. unmap_sysmem(output);
  523. }
  524. /* Horrible code size hack for boards that just want crc32 */
  525. } else {
  526. ulong crc;
  527. ulong *ptr;
  528. crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
  529. printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
  530. addr, addr + len - 1, crc);
  531. if (argc >= 3) {
  532. ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
  533. *ptr = crc;
  534. }
  535. }
  536. return 0;
  537. }
  538. #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
  539. #endif /* !USE_HOSTCC */