hash.c 6.3 KB

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