hash.c 7.7 KB

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