hash.c 11 KB

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