hash.c 12 KB

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