hash.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #include <common.h>
  13. #include <command.h>
  14. #include <hw_sha.h>
  15. #include <hash.h>
  16. #include <sha1.h>
  17. #include <sha256.h>
  18. #include <asm/io.h>
  19. #include <asm/errno.h>
  20. /*
  21. * These are the hash algorithms we support. Chips which support accelerated
  22. * crypto could perhaps add named version of these algorithms here. Note that
  23. * algorithm names must be in lower case.
  24. */
  25. static struct hash_algo hash_algo[] = {
  26. /*
  27. * CONFIG_SHA_HW_ACCEL is defined if hardware acceleration is
  28. * available.
  29. */
  30. #ifdef CONFIG_SHA_HW_ACCEL
  31. {
  32. "sha1",
  33. SHA1_SUM_LEN,
  34. hw_sha1,
  35. CHUNKSZ_SHA1,
  36. }, {
  37. "sha256",
  38. SHA256_SUM_LEN,
  39. hw_sha256,
  40. CHUNKSZ_SHA256,
  41. },
  42. #endif
  43. /*
  44. * This is CONFIG_CMD_SHA1SUM instead of CONFIG_SHA1 since otherwise
  45. * it bloats the code for boards which use SHA1 but not the 'hash'
  46. * or 'sha1sum' commands.
  47. */
  48. #ifdef CONFIG_CMD_SHA1SUM
  49. {
  50. "sha1",
  51. SHA1_SUM_LEN,
  52. sha1_csum_wd,
  53. CHUNKSZ_SHA1,
  54. },
  55. #define MULTI_HASH
  56. #endif
  57. #ifdef CONFIG_SHA256
  58. {
  59. "sha256",
  60. SHA256_SUM_LEN,
  61. sha256_csum_wd,
  62. CHUNKSZ_SHA256,
  63. },
  64. #define MULTI_HASH
  65. #endif
  66. {
  67. "crc32",
  68. 4,
  69. crc32_wd_buf,
  70. CHUNKSZ_CRC32,
  71. },
  72. };
  73. #if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
  74. #define MULTI_HASH
  75. #endif
  76. /* Try to minimize code size for boards that don't want much hashing */
  77. #ifdef MULTI_HASH
  78. #define multi_hash() 1
  79. #else
  80. #define multi_hash() 0
  81. #endif
  82. /**
  83. * store_result: Store the resulting sum to an address or variable
  84. *
  85. * @algo: Hash algorithm being used
  86. * @sum: Hash digest (algo->digest_size bytes)
  87. * @dest: Destination, interpreted as a hex address if it starts
  88. * with * (or allow_env_vars is 0) or otherwise as an
  89. * environment variable.
  90. * @allow_env_vars: non-zero to permit storing the result to an
  91. * variable environment
  92. */
  93. static void store_result(struct hash_algo *algo, const u8 *sum,
  94. const char *dest, int allow_env_vars)
  95. {
  96. unsigned int i;
  97. int env_var = 0;
  98. /*
  99. * If environment variables are allowed, then we assume that 'dest'
  100. * is an environment variable, unless it starts with *, in which
  101. * case we assume it is an address. If not allowed, it is always an
  102. * address. This is to support the crc32 command.
  103. */
  104. if (allow_env_vars) {
  105. if (*dest == '*')
  106. dest++;
  107. else
  108. env_var = 1;
  109. }
  110. if (env_var) {
  111. char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
  112. char *str_ptr = str_output;
  113. for (i = 0; i < algo->digest_size; i++) {
  114. sprintf(str_ptr, "%02x", sum[i]);
  115. str_ptr += 2;
  116. }
  117. str_ptr = '\0';
  118. setenv(dest, str_output);
  119. } else {
  120. ulong addr;
  121. void *buf;
  122. addr = simple_strtoul(dest, NULL, 16);
  123. buf = map_sysmem(addr, algo->digest_size);
  124. memcpy(buf, sum, algo->digest_size);
  125. unmap_sysmem(buf);
  126. }
  127. }
  128. /**
  129. * parse_verify_sum: Parse a hash verification parameter
  130. *
  131. * @algo: Hash algorithm being used
  132. * @verify_str: Argument to parse. If it starts with * then it is
  133. * interpreted as a hex address containing the hash.
  134. * If the length is exactly the right number of hex digits
  135. * for the digest size, then we assume it is a hex digest.
  136. * Otherwise we assume it is an environment variable, and
  137. * look up its value (it must contain a hex digest).
  138. * @vsum: Returns binary digest value (algo->digest_size bytes)
  139. * @allow_env_vars: non-zero to permit storing the result to an environment
  140. * variable. If 0 then verify_str is assumed to be an
  141. * address, and the * prefix is not expected.
  142. * @return 0 if ok, non-zero on error
  143. */
  144. static int parse_verify_sum(struct hash_algo *algo, char *verify_str, u8 *vsum,
  145. int allow_env_vars)
  146. {
  147. int env_var = 0;
  148. /* See comment above in store_result() */
  149. if (allow_env_vars) {
  150. if (*verify_str == '*')
  151. verify_str++;
  152. else
  153. env_var = 1;
  154. }
  155. if (env_var) {
  156. ulong addr;
  157. void *buf;
  158. addr = simple_strtoul(verify_str, NULL, 16);
  159. buf = map_sysmem(addr, algo->digest_size);
  160. memcpy(vsum, buf, algo->digest_size);
  161. } else {
  162. unsigned int i;
  163. char *vsum_str;
  164. int digits = algo->digest_size * 2;
  165. /*
  166. * As with the original code from sha1sum.c, we assume that a
  167. * string which matches the digest size exactly is a hex
  168. * string and not an environment variable.
  169. */
  170. if (strlen(verify_str) == digits)
  171. vsum_str = verify_str;
  172. else {
  173. vsum_str = getenv(verify_str);
  174. if (vsum_str == NULL || strlen(vsum_str) != digits) {
  175. printf("Expected %d hex digits in env var\n",
  176. digits);
  177. return 1;
  178. }
  179. }
  180. for (i = 0; i < algo->digest_size; i++) {
  181. char *nullp = vsum_str + (i + 1) * 2;
  182. char end = *nullp;
  183. *nullp = '\0';
  184. vsum[i] = simple_strtoul(vsum_str + (i * 2), NULL, 16);
  185. *nullp = end;
  186. }
  187. }
  188. return 0;
  189. }
  190. static struct hash_algo *find_hash_algo(const char *name)
  191. {
  192. int i;
  193. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  194. if (!strcmp(name, hash_algo[i].name))
  195. return &hash_algo[i];
  196. }
  197. return NULL;
  198. }
  199. static void show_hash(struct hash_algo *algo, ulong addr, ulong len,
  200. u8 *output)
  201. {
  202. int i;
  203. printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
  204. for (i = 0; i < algo->digest_size; i++)
  205. printf("%02x", output[i]);
  206. }
  207. int hash_block(const char *algo_name, const void *data, unsigned int len,
  208. uint8_t *output, int *output_size)
  209. {
  210. struct hash_algo *algo;
  211. algo = find_hash_algo(algo_name);
  212. if (!algo) {
  213. debug("Unknown hash algorithm '%s'\n", algo_name);
  214. return -EPROTONOSUPPORT;
  215. }
  216. if (output_size && *output_size < algo->digest_size) {
  217. debug("Output buffer size %d too small (need %d bytes)",
  218. *output_size, algo->digest_size);
  219. return -ENOSPC;
  220. }
  221. if (output_size)
  222. *output_size = algo->digest_size;
  223. algo->hash_func_ws(data, len, output, algo->chunk_size);
  224. return 0;
  225. }
  226. int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
  227. int argc, char * const argv[])
  228. {
  229. ulong addr, len;
  230. if (argc < 2)
  231. return CMD_RET_USAGE;
  232. addr = simple_strtoul(*argv++, NULL, 16);
  233. len = simple_strtoul(*argv++, NULL, 16);
  234. if (multi_hash()) {
  235. struct hash_algo *algo;
  236. u8 output[HASH_MAX_DIGEST_SIZE];
  237. u8 vsum[HASH_MAX_DIGEST_SIZE];
  238. void *buf;
  239. algo = find_hash_algo(algo_name);
  240. if (!algo) {
  241. printf("Unknown hash algorithm '%s'\n", algo_name);
  242. return CMD_RET_USAGE;
  243. }
  244. argc -= 2;
  245. if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
  246. puts("HASH_MAX_DIGEST_SIZE exceeded\n");
  247. return 1;
  248. }
  249. buf = map_sysmem(addr, len);
  250. algo->hash_func_ws(buf, len, output, algo->chunk_size);
  251. unmap_sysmem(buf);
  252. /* Try to avoid code bloat when verify is not needed */
  253. #ifdef CONFIG_HASH_VERIFY
  254. if (flags & HASH_FLAG_VERIFY) {
  255. #else
  256. if (0) {
  257. #endif
  258. if (!argc)
  259. return CMD_RET_USAGE;
  260. if (parse_verify_sum(algo, *argv, vsum,
  261. flags & HASH_FLAG_ENV)) {
  262. printf("ERROR: %s does not contain a valid "
  263. "%s sum\n", *argv, algo->name);
  264. return 1;
  265. }
  266. if (memcmp(output, vsum, algo->digest_size) != 0) {
  267. int i;
  268. show_hash(algo, addr, len, output);
  269. printf(" != ");
  270. for (i = 0; i < algo->digest_size; i++)
  271. printf("%02x", vsum[i]);
  272. puts(" ** ERROR **\n");
  273. return 1;
  274. }
  275. } else {
  276. show_hash(algo, addr, len, output);
  277. printf("\n");
  278. if (argc) {
  279. store_result(algo, output, *argv,
  280. flags & HASH_FLAG_ENV);
  281. }
  282. }
  283. /* Horrible code size hack for boards that just want crc32 */
  284. } else {
  285. ulong crc;
  286. ulong *ptr;
  287. crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
  288. printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
  289. addr, addr + len - 1, crc);
  290. if (argc >= 3) {
  291. ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
  292. *ptr = crc;
  293. }
  294. }
  295. return 0;
  296. }