2 * Copyright (c) 2012 The Chromium OS Authors.
5 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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.
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.
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,
35 * These are the hash algorithms we support. Chips which support accelerated
36 * crypto could perhaps add named version of these algorithms here. Note that
37 * algorithm names must be in lower case.
39 static struct hash_algo hash_algo[] = {
41 * CONFIG_SHA_HW_ACCEL is defined if hardware acceleration is
44 #ifdef CONFIG_SHA_HW_ACCEL
58 * This is CONFIG_CMD_SHA1SUM instead of CONFIG_SHA1 since otherwise
59 * it bloats the code for boards which use SHA1 but not the 'hash'
60 * or 'sha1sum' commands.
62 #ifdef CONFIG_CMD_SHA1SUM
88 #if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
92 /* Try to minimize code size for boards that don't want much hashing */
94 #define multi_hash() 1
96 #define multi_hash() 0
100 * store_result: Store the resulting sum to an address or variable
102 * @algo: Hash algorithm being used
103 * @sum: Hash digest (algo->digest_size bytes)
104 * @dest: Destination, interpreted as a hex address if it starts
105 * with * (or allow_env_vars is 0) or otherwise as an
106 * environment variable.
107 * @allow_env_vars: non-zero to permit storing the result to an
108 * variable environment
110 static void store_result(struct hash_algo *algo, const u8 *sum,
111 const char *dest, int allow_env_vars)
117 * If environment variables are allowed, then we assume that 'dest'
118 * is an environment variable, unless it starts with *, in which
119 * case we assume it is an address. If not allowed, it is always an
120 * address. This is to support the crc32 command.
122 if (allow_env_vars) {
130 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
131 char *str_ptr = str_output;
133 for (i = 0; i < algo->digest_size; i++) {
134 sprintf(str_ptr, "%02x", sum[i]);
138 setenv(dest, str_output);
143 addr = simple_strtoul(dest, NULL, 16);
144 buf = map_sysmem(addr, algo->digest_size);
145 memcpy(buf, sum, algo->digest_size);
151 * parse_verify_sum: Parse a hash verification parameter
153 * @algo: Hash algorithm being used
154 * @verify_str: Argument to parse. If it starts with * then it is
155 * interpreted as a hex address containing the hash.
156 * If the length is exactly the right number of hex digits
157 * for the digest size, then we assume it is a hex digest.
158 * Otherwise we assume it is an environment variable, and
159 * look up its value (it must contain a hex digest).
160 * @vsum: Returns binary digest value (algo->digest_size bytes)
161 * @allow_env_vars: non-zero to permit storing the result to an environment
162 * variable. If 0 then verify_str is assumed to be an
163 * address, and the * prefix is not expected.
164 * @return 0 if ok, non-zero on error
166 static int parse_verify_sum(struct hash_algo *algo, char *verify_str, u8 *vsum,
171 /* See comment above in store_result() */
172 if (allow_env_vars) {
173 if (*verify_str == '*')
183 addr = simple_strtoul(verify_str, NULL, 16);
184 buf = map_sysmem(addr, algo->digest_size);
185 memcpy(vsum, buf, algo->digest_size);
189 int digits = algo->digest_size * 2;
192 * As with the original code from sha1sum.c, we assume that a
193 * string which matches the digest size exactly is a hex
194 * string and not an environment variable.
196 if (strlen(verify_str) == digits)
197 vsum_str = verify_str;
199 vsum_str = getenv(verify_str);
200 if (vsum_str == NULL || strlen(vsum_str) != digits) {
201 printf("Expected %d hex digits in env var\n",
207 for (i = 0; i < algo->digest_size; i++) {
208 char *nullp = vsum_str + (i + 1) * 2;
212 vsum[i] = simple_strtoul(vsum_str + (i * 2), NULL, 16);
219 static struct hash_algo *find_hash_algo(const char *name)
223 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
224 if (!strcmp(name, hash_algo[i].name))
225 return &hash_algo[i];
231 static void show_hash(struct hash_algo *algo, ulong addr, ulong len,
236 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
237 for (i = 0; i < algo->digest_size; i++)
238 printf("%02x", output[i]);
241 int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
242 int argc, char * const argv[])
247 return CMD_RET_USAGE;
249 addr = simple_strtoul(*argv++, NULL, 16);
250 len = simple_strtoul(*argv++, NULL, 16);
253 struct hash_algo *algo;
254 u8 output[HASH_MAX_DIGEST_SIZE];
255 u8 vsum[HASH_MAX_DIGEST_SIZE];
258 algo = find_hash_algo(algo_name);
260 printf("Unknown hash algorithm '%s'\n", algo_name);
261 return CMD_RET_USAGE;
265 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
266 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
270 buf = map_sysmem(addr, len);
271 algo->hash_func_ws(buf, len, output, algo->chunk_size);
274 /* Try to avoid code bloat when verify is not needed */
275 #ifdef CONFIG_HASH_VERIFY
276 if (flags & HASH_FLAG_VERIFY) {
281 return CMD_RET_USAGE;
282 if (parse_verify_sum(algo, *argv, vsum,
283 flags & HASH_FLAG_ENV)) {
284 printf("ERROR: %s does not contain a valid "
285 "%s sum\n", *argv, algo->name);
288 if (memcmp(output, vsum, algo->digest_size) != 0) {
291 show_hash(algo, addr, len, output);
293 for (i = 0; i < algo->digest_size; i++)
294 printf("%02x", vsum[i]);
295 puts(" ** ERROR **\n");
299 show_hash(algo, addr, len, output);
303 store_result(algo, output, *argv,
304 flags & HASH_FLAG_ENV);
308 /* Horrible code size hack for boards that just want crc32 */
313 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
315 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
316 addr, addr + len - 1, crc);
319 ptr = (ulong *)simple_strtoul(argv[3], NULL, 16);