hash.c 10 KB

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