hash.c 11 KB

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