hash.c 10 KB

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