blob: 0d04c4c9a0ce381f879ba48bca2e153d6d40e4ef [file] [log] [blame]
Simon Glass460408e2012-12-05 14:46:36 +00001/*
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>
Simon Glassbd091b62013-02-24 17:33:31 +000031#include <asm/io.h>
Simon Glass460408e2012-12-05 14:46:36 +000032
33/*
34 * These are the hash algorithms we support. Chips which support accelerated
35 * crypto could perhaps add named version of these algorithms here.
36 */
37static struct hash_algo hash_algo[] = {
Simon Glassd20a40d2013-02-24 20:30:22 +000038 /*
39 * This is CONFIG_CMD_SHA1SUM instead of CONFIG_SHA1 since otherwise
40 * it bloats the code for boards which use SHA1 but not the 'hash'
41 * or 'sha1sum' commands.
42 */
43#ifdef CONFIG_CMD_SHA1SUM
Simon Glass460408e2012-12-05 14:46:36 +000044 {
45 "SHA1",
46 SHA1_SUM_LEN,
47 sha1_csum_wd,
48 CHUNKSZ_SHA1,
49 },
Simon Glassd20a40d2013-02-24 20:30:22 +000050#define MULTI_HASH
Simon Glass460408e2012-12-05 14:46:36 +000051#endif
52#ifdef CONFIG_SHA256
53 {
54 "SHA256",
55 SHA256_SUM_LEN,
56 sha256_csum_wd,
57 CHUNKSZ_SHA256,
58 },
Simon Glassd20a40d2013-02-24 20:30:22 +000059#define MULTI_HASH
Simon Glass460408e2012-12-05 14:46:36 +000060#endif
Simon Glassd20a40d2013-02-24 20:30:22 +000061 {
62 "CRC32",
63 4,
64 crc32_wd_buf,
65 CHUNKSZ_CRC32,
66 },
Simon Glass460408e2012-12-05 14:46:36 +000067};
68
Simon Glassd20a40d2013-02-24 20:30:22 +000069#if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
70#define MULTI_HASH
71#endif
72
73/* Try to minimize code size for boards that don't want much hashing */
74#ifdef MULTI_HASH
75#define multi_hash() 1
76#else
77#define multi_hash() 0
78#endif
79
Simon Glass460408e2012-12-05 14:46:36 +000080/**
81 * store_result: Store the resulting sum to an address or variable
82 *
83 * @algo: Hash algorithm being used
84 * @sum: Hash digest (algo->digest_size bytes)
85 * @dest: Destination, interpreted as a hex address if it starts
Simon Glassd5b76672013-02-24 17:33:26 +000086 * with * (or allow_env_vars is 0) or otherwise as an
87 * environment variable.
88 * @allow_env_vars: non-zero to permit storing the result to an
89 * variable environment
Simon Glass460408e2012-12-05 14:46:36 +000090 */
91static void store_result(struct hash_algo *algo, const u8 *sum,
Simon Glassd5b76672013-02-24 17:33:26 +000092 const char *dest, int allow_env_vars)
Simon Glass460408e2012-12-05 14:46:36 +000093{
94 unsigned int i;
Simon Glassd5b76672013-02-24 17:33:26 +000095 int env_var = 0;
Simon Glass460408e2012-12-05 14:46:36 +000096
Simon Glassd5b76672013-02-24 17:33:26 +000097 /*
98 * If environment variables are allowed, then we assume that 'dest'
99 * is an environment variable, unless it starts with *, in which
100 * case we assume it is an address. If not allowed, it is always an
101 * address. This is to support the crc32 command.
102 */
103 if (allow_env_vars) {
104 if (*dest == '*')
105 dest++;
106 else
107 env_var = 1;
108 }
Simon Glass460408e2012-12-05 14:46:36 +0000109
Simon Glassd5b76672013-02-24 17:33:26 +0000110 if (env_var) {
Simon Glass460408e2012-12-05 14:46:36 +0000111 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
112 char *str_ptr = str_output;
113
114 for (i = 0; i < algo->digest_size; i++) {
115 sprintf(str_ptr, "%02x", sum[i]);
116 str_ptr += 2;
117 }
118 str_ptr = '\0';
119 setenv(dest, str_output);
Simon Glassd5b76672013-02-24 17:33:26 +0000120 } else {
Simon Glassbd091b62013-02-24 17:33:31 +0000121 ulong addr;
122 void *buf;
Simon Glassd5b76672013-02-24 17:33:26 +0000123
Simon Glassbd091b62013-02-24 17:33:31 +0000124 addr = simple_strtoul(dest, NULL, 16);
125 buf = map_sysmem(addr, algo->digest_size);
126 memcpy(buf, sum, algo->digest_size);
127 unmap_sysmem(buf);
Simon Glass460408e2012-12-05 14:46:36 +0000128 }
129}
130
131/**
132 * parse_verify_sum: Parse a hash verification parameter
133 *
134 * @algo: Hash algorithm being used
135 * @verify_str: Argument to parse. If it starts with * then it is
136 * interpreted as a hex address containing the hash.
137 * If the length is exactly the right number of hex digits
138 * for the digest size, then we assume it is a hex digest.
139 * Otherwise we assume it is an environment variable, and
140 * look up its value (it must contain a hex digest).
141 * @vsum: Returns binary digest value (algo->digest_size bytes)
Simon Glassd5b76672013-02-24 17:33:26 +0000142 * @allow_env_vars: non-zero to permit storing the result to an environment
143 * variable. If 0 then verify_str is assumed to be an
144 * address, and the * prefix is not expected.
Simon Glass460408e2012-12-05 14:46:36 +0000145 * @return 0 if ok, non-zero on error
146 */
Simon Glassd5b76672013-02-24 17:33:26 +0000147static int parse_verify_sum(struct hash_algo *algo, char *verify_str, u8 *vsum,
148 int allow_env_vars)
Simon Glass460408e2012-12-05 14:46:36 +0000149{
Simon Glassd5b76672013-02-24 17:33:26 +0000150 int env_var = 0;
151
152 /* See comment above in store_result() */
153 if (allow_env_vars) {
154 if (*verify_str == '*')
155 verify_str++;
156 else
157 env_var = 1;
158 }
159
160 if (env_var) {
Simon Glassbd091b62013-02-24 17:33:31 +0000161 ulong addr;
162 void *buf;
Simon Glass460408e2012-12-05 14:46:36 +0000163
Simon Glassbd091b62013-02-24 17:33:31 +0000164 addr = simple_strtoul(verify_str, NULL, 16);
165 buf = map_sysmem(addr, algo->digest_size);
166 memcpy(vsum, buf, algo->digest_size);
Simon Glass460408e2012-12-05 14:46:36 +0000167 } else {
168 unsigned int i;
169 char *vsum_str;
170 int digits = algo->digest_size * 2;
171
172 /*
173 * As with the original code from sha1sum.c, we assume that a
174 * string which matches the digest size exactly is a hex
175 * string and not an environment variable.
176 */
177 if (strlen(verify_str) == digits)
178 vsum_str = verify_str;
179 else {
180 vsum_str = getenv(verify_str);
181 if (vsum_str == NULL || strlen(vsum_str) != digits) {
182 printf("Expected %d hex digits in env var\n",
183 digits);
184 return 1;
185 }
186 }
187
188 for (i = 0; i < algo->digest_size; i++) {
189 char *nullp = vsum_str + (i + 1) * 2;
190 char end = *nullp;
191
192 *nullp = '\0';
193 vsum[i] = simple_strtoul(vsum_str + (i * 2), NULL, 16);
194 *nullp = end;
195 }
196 }
197 return 0;
198}
199
200static struct hash_algo *find_hash_algo(const char *name)
201{
202 int i;
203
204 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
205 if (!strcasecmp(name, hash_algo[i].name))
206 return &hash_algo[i];
207 }
208
209 return NULL;
210}
211
212static void show_hash(struct hash_algo *algo, ulong addr, ulong len,
213 u8 *output)
214{
215 int i;
216
217 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
218 for (i = 0; i < algo->digest_size; i++)
219 printf("%02x", output[i]);
220}
221
Simon Glassd5b76672013-02-24 17:33:26 +0000222int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
Simon Glass460408e2012-12-05 14:46:36 +0000223 int argc, char * const argv[])
224{
Simon Glass460408e2012-12-05 14:46:36 +0000225 ulong addr, len;
Simon Glass460408e2012-12-05 14:46:36 +0000226
227 if (argc < 2)
228 return CMD_RET_USAGE;
229
Simon Glassd5b76672013-02-24 17:33:26 +0000230 addr = simple_strtoul(*argv++, NULL, 16);
231 len = simple_strtoul(*argv++, NULL, 16);
232
Simon Glassd20a40d2013-02-24 20:30:22 +0000233 if (multi_hash()) {
234 struct hash_algo *algo;
235 u8 output[HASH_MAX_DIGEST_SIZE];
236 u8 vsum[HASH_MAX_DIGEST_SIZE];
Simon Glassbd091b62013-02-24 17:33:31 +0000237 void *buf;
Simon Glass460408e2012-12-05 14:46:36 +0000238
Simon Glassd20a40d2013-02-24 20:30:22 +0000239 algo = find_hash_algo(algo_name);
240 if (!algo) {
241 printf("Unknown hash algorithm '%s'\n", algo_name);
Simon Glass460408e2012-12-05 14:46:36 +0000242 return CMD_RET_USAGE;
Simon Glassd20a40d2013-02-24 20:30:22 +0000243 }
244 argc -= 2;
245
246 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
247 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
248 return 1;
249 }
250
Simon Glassbd091b62013-02-24 17:33:31 +0000251 buf = map_sysmem(addr, len);
252 algo->hash_func_ws(buf, len, output, algo->chunk_size);
253 unmap_sysmem(buf);
Simon Glassd20a40d2013-02-24 20:30:22 +0000254
255 /* Try to avoid code bloat when verify is not needed */
256#ifdef CONFIG_HASH_VERIFY
257 if (flags & HASH_FLAG_VERIFY) {
258#else
259 if (0) {
260#endif
261 if (!argc)
262 return CMD_RET_USAGE;
263 if (parse_verify_sum(algo, *argv, vsum,
Simon Glassd5b76672013-02-24 17:33:26 +0000264 flags & HASH_FLAG_ENV)) {
Simon Glassd20a40d2013-02-24 20:30:22 +0000265 printf("ERROR: %s does not contain a valid "
266 "%s sum\n", *argv, algo->name);
267 return 1;
268 }
269 if (memcmp(output, vsum, algo->digest_size) != 0) {
270 int i;
Simon Glass460408e2012-12-05 14:46:36 +0000271
Simon Glassd20a40d2013-02-24 20:30:22 +0000272 show_hash(algo, addr, len, output);
273 printf(" != ");
274 for (i = 0; i < algo->digest_size; i++)
275 printf("%02x", vsum[i]);
276 puts(" ** ERROR **\n");
277 return 1;
278 }
279 } else {
Simon Glass460408e2012-12-05 14:46:36 +0000280 show_hash(algo, addr, len, output);
Simon Glassd20a40d2013-02-24 20:30:22 +0000281 printf("\n");
Simon Glass460408e2012-12-05 14:46:36 +0000282
Simon Glassd20a40d2013-02-24 20:30:22 +0000283 if (argc) {
284 store_result(algo, output, *argv,
285 flags & HASH_FLAG_ENV);
286 }
287 }
288
289 /* Horrible code size hack for boards that just want crc32 */
290 } else {
291 ulong crc;
292 ulong *ptr;
293
294 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
295
296 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
297 addr, addr + len - 1, crc);
298
299 if (argc > 3) {
300 ptr = (ulong *)simple_strtoul(argv[3], NULL, 16);
301 *ptr = crc;
Simon Glassd5b76672013-02-24 17:33:26 +0000302 }
Simon Glass460408e2012-12-05 14:46:36 +0000303 }
304
305 return 0;
306}