hash.c 7.2 KB

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