hash.c 12 KB

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