hash.c 10 KB

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