hash.c 12 KB

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