hash.c 8.3 KB

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