hash.c 11 KB

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