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