hash.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 otherwise as an environment variable.
  59. */
  60. static void store_result(struct hash_algo *algo, const u8 *sum,
  61. const char *dest)
  62. {
  63. unsigned int i;
  64. if (*dest == '*') {
  65. u8 *ptr;
  66. ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
  67. memcpy(ptr, sum, algo->digest_size);
  68. } else {
  69. char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
  70. char *str_ptr = str_output;
  71. for (i = 0; i < algo->digest_size; i++) {
  72. sprintf(str_ptr, "%02x", sum[i]);
  73. str_ptr += 2;
  74. }
  75. str_ptr = '\0';
  76. setenv(dest, str_output);
  77. }
  78. }
  79. /**
  80. * parse_verify_sum: Parse a hash verification parameter
  81. *
  82. * @algo: Hash algorithm being used
  83. * @verify_str: Argument to parse. If it starts with * then it is
  84. * interpreted as a hex address containing the hash.
  85. * If the length is exactly the right number of hex digits
  86. * for the digest size, then we assume it is a hex digest.
  87. * Otherwise we assume it is an environment variable, and
  88. * look up its value (it must contain a hex digest).
  89. * @vsum: Returns binary digest value (algo->digest_size bytes)
  90. * @return 0 if ok, non-zero on error
  91. */
  92. static int parse_verify_sum(struct hash_algo *algo, char *verify_str, u8 *vsum)
  93. {
  94. if (*verify_str == '*') {
  95. u8 *ptr;
  96. ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
  97. memcpy(vsum, ptr, algo->digest_size);
  98. } else {
  99. unsigned int i;
  100. char *vsum_str;
  101. int digits = algo->digest_size * 2;
  102. /*
  103. * As with the original code from sha1sum.c, we assume that a
  104. * string which matches the digest size exactly is a hex
  105. * string and not an environment variable.
  106. */
  107. if (strlen(verify_str) == digits)
  108. vsum_str = verify_str;
  109. else {
  110. vsum_str = getenv(verify_str);
  111. if (vsum_str == NULL || strlen(vsum_str) != digits) {
  112. printf("Expected %d hex digits in env var\n",
  113. digits);
  114. return 1;
  115. }
  116. }
  117. for (i = 0; i < algo->digest_size; i++) {
  118. char *nullp = vsum_str + (i + 1) * 2;
  119. char end = *nullp;
  120. *nullp = '\0';
  121. vsum[i] = simple_strtoul(vsum_str + (i * 2), NULL, 16);
  122. *nullp = end;
  123. }
  124. }
  125. return 0;
  126. }
  127. static struct hash_algo *find_hash_algo(const char *name)
  128. {
  129. int i;
  130. for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
  131. if (!strcasecmp(name, hash_algo[i].name))
  132. return &hash_algo[i];
  133. }
  134. return NULL;
  135. }
  136. static void show_hash(struct hash_algo *algo, ulong addr, ulong len,
  137. u8 *output)
  138. {
  139. int i;
  140. printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
  141. for (i = 0; i < algo->digest_size; i++)
  142. printf("%02x", output[i]);
  143. }
  144. int hash_command(const char *algo_name, int verify, cmd_tbl_t *cmdtp, int flag,
  145. int argc, char * const argv[])
  146. {
  147. struct hash_algo *algo;
  148. ulong addr, len;
  149. u8 output[HASH_MAX_DIGEST_SIZE];
  150. u8 vsum[HASH_MAX_DIGEST_SIZE];
  151. if (argc < 2)
  152. return CMD_RET_USAGE;
  153. algo = find_hash_algo(algo_name);
  154. if (!algo) {
  155. printf("Unknown hash algorithm '%s'\n", algo_name);
  156. return CMD_RET_USAGE;
  157. }
  158. addr = simple_strtoul(*argv++, NULL, 16);
  159. len = simple_strtoul(*argv++, NULL, 16);
  160. argc -= 2;
  161. if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
  162. puts("HASH_MAX_DIGEST_SIZE exceeded\n");
  163. return 1;
  164. }
  165. algo->hash_func_ws((const unsigned char *)addr, len, output,
  166. algo->chunk_size);
  167. /* Try to avoid code bloat when verify is not needed */
  168. #ifdef CONFIG_HASH_VERIFY
  169. if (verify) {
  170. #else
  171. if (0) {
  172. #endif
  173. if (!argc)
  174. return CMD_RET_USAGE;
  175. if (parse_verify_sum(algo, *argv, vsum)) {
  176. printf("ERROR: %s does not contain a valid %s sum\n",
  177. *argv, algo->name);
  178. return 1;
  179. }
  180. if (memcmp(output, vsum, algo->digest_size) != 0) {
  181. int i;
  182. show_hash(algo, addr, len, output);
  183. printf(" != ");
  184. for (i = 0; i < algo->digest_size; i++)
  185. printf("%02x", vsum[i]);
  186. puts(" ** ERROR **\n");
  187. return 1;
  188. }
  189. } else {
  190. show_hash(algo, addr, len, output);
  191. printf("\n");
  192. if (argc)
  193. store_result(algo, output, *argv);
  194. }
  195. return 0;
  196. }