hash.c 14 KB

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