hash.c 7.4 KB

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