hash.c 12 KB

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