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