blob: f5badcb930075c3f535b162507a707492e8a2e70 [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
Simon Glass218da0f2013-02-24 17:33:32 +000035 * crypto could perhaps add named version of these algorithms here. Note that
36 * algorithm names must be in lower case.
Simon Glass460408e2012-12-05 14:46:36 +000037 */
38static struct hash_algo hash_algo[] = {
Simon Glassd20a40d2013-02-24 20:30:22 +000039 /*
40 * This is CONFIG_CMD_SHA1SUM instead of CONFIG_SHA1 since otherwise
41 * it bloats the code for boards which use SHA1 but not the 'hash'
42 * or 'sha1sum' commands.
43 */
44#ifdef CONFIG_CMD_SHA1SUM
Simon Glass460408e2012-12-05 14:46:36 +000045 {
Simon Glass218da0f2013-02-24 17:33:32 +000046 "sha1",
Simon Glass460408e2012-12-05 14:46:36 +000047 SHA1_SUM_LEN,
48 sha1_csum_wd,
49 CHUNKSZ_SHA1,
50 },
Simon Glassd20a40d2013-02-24 20:30:22 +000051#define MULTI_HASH
Simon Glass460408e2012-12-05 14:46:36 +000052#endif
53#ifdef CONFIG_SHA256
54 {
Simon Glass218da0f2013-02-24 17:33:32 +000055 "sha256",
Simon Glass460408e2012-12-05 14:46:36 +000056 SHA256_SUM_LEN,
57 sha256_csum_wd,
58 CHUNKSZ_SHA256,
59 },
Simon Glassd20a40d2013-02-24 20:30:22 +000060#define MULTI_HASH
Simon Glass460408e2012-12-05 14:46:36 +000061#endif
Simon Glassd20a40d2013-02-24 20:30:22 +000062 {
Simon Glass218da0f2013-02-24 17:33:32 +000063 "crc32",
Simon Glassd20a40d2013-02-24 20:30:22 +000064 4,
65 crc32_wd_buf,
66 CHUNKSZ_CRC32,
67 },
Simon Glass460408e2012-12-05 14:46:36 +000068};
69
Simon Glassd20a40d2013-02-24 20:30:22 +000070#if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
71#define MULTI_HASH
72#endif
73
74/* Try to minimize code size for boards that don't want much hashing */
75#ifdef MULTI_HASH
76#define multi_hash() 1
77#else
78#define multi_hash() 0
79#endif
80
Simon Glass460408e2012-12-05 14:46:36 +000081/**
82 * store_result: Store the resulting sum to an address or variable
83 *
84 * @algo: Hash algorithm being used
85 * @sum: Hash digest (algo->digest_size bytes)
86 * @dest: Destination, interpreted as a hex address if it starts
Simon Glassd5b76672013-02-24 17:33:26 +000087 * with * (or allow_env_vars is 0) or otherwise as an
88 * environment variable.
89 * @allow_env_vars: non-zero to permit storing the result to an
90 * variable environment
Simon Glass460408e2012-12-05 14:46:36 +000091 */
92static void store_result(struct hash_algo *algo, const u8 *sum,
Simon Glassd5b76672013-02-24 17:33:26 +000093 const char *dest, int allow_env_vars)
Simon Glass460408e2012-12-05 14:46:36 +000094{
95 unsigned int i;
Simon Glassd5b76672013-02-24 17:33:26 +000096 int env_var = 0;
Simon Glass460408e2012-12-05 14:46:36 +000097
Simon Glassd5b76672013-02-24 17:33:26 +000098 /*
99 * If environment variables are allowed, then we assume that 'dest'
100 * is an environment variable, unless it starts with *, in which
101 * case we assume it is an address. If not allowed, it is always an
102 * address. This is to support the crc32 command.
103 */
104 if (allow_env_vars) {
105 if (*dest == '*')
106 dest++;
107 else
108 env_var = 1;
109 }
Simon Glass460408e2012-12-05 14:46:36 +0000110
Simon Glassd5b76672013-02-24 17:33:26 +0000111 if (env_var) {
Simon Glass460408e2012-12-05 14:46:36 +0000112 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
113 char *str_ptr = str_output;
114
115 for (i = 0; i < algo->digest_size; i++) {
116 sprintf(str_ptr, "%02x", sum[i]);
117 str_ptr += 2;
118 }
119 str_ptr = '\0';
120 setenv(dest, str_output);
Simon Glassd5b76672013-02-24 17:33:26 +0000121 } else {
Simon Glassbd091b62013-02-24 17:33:31 +0000122 ulong addr;
123 void *buf;
Simon Glassd5b76672013-02-24 17:33:26 +0000124
Simon Glassbd091b62013-02-24 17:33:31 +0000125 addr = simple_strtoul(dest, NULL, 16);
126 buf = map_sysmem(addr, algo->digest_size);
127 memcpy(buf, sum, algo->digest_size);
128 unmap_sysmem(buf);
Simon Glass460408e2012-12-05 14:46:36 +0000129 }
130}
131
132/**
133 * parse_verify_sum: Parse a hash verification parameter
134 *
135 * @algo: Hash algorithm being used
136 * @verify_str: Argument to parse. If it starts with * then it is
137 * interpreted as a hex address containing the hash.
138 * If the length is exactly the right number of hex digits
139 * for the digest size, then we assume it is a hex digest.
140 * Otherwise we assume it is an environment variable, and
141 * look up its value (it must contain a hex digest).
142 * @vsum: Returns binary digest value (algo->digest_size bytes)
Simon Glassd5b76672013-02-24 17:33:26 +0000143 * @allow_env_vars: non-zero to permit storing the result to an environment
144 * variable. If 0 then verify_str is assumed to be an
145 * address, and the * prefix is not expected.
Simon Glass460408e2012-12-05 14:46:36 +0000146 * @return 0 if ok, non-zero on error
147 */
Simon Glassd5b76672013-02-24 17:33:26 +0000148static int parse_verify_sum(struct hash_algo *algo, char *verify_str, u8 *vsum,
149 int allow_env_vars)
Simon Glass460408e2012-12-05 14:46:36 +0000150{
Simon Glassd5b76672013-02-24 17:33:26 +0000151 int env_var = 0;
152
153 /* See comment above in store_result() */
154 if (allow_env_vars) {
155 if (*verify_str == '*')
156 verify_str++;
157 else
158 env_var = 1;
159 }
160
161 if (env_var) {
Simon Glassbd091b62013-02-24 17:33:31 +0000162 ulong addr;
163 void *buf;
Simon Glass460408e2012-12-05 14:46:36 +0000164
Simon Glassbd091b62013-02-24 17:33:31 +0000165 addr = simple_strtoul(verify_str, NULL, 16);
166 buf = map_sysmem(addr, algo->digest_size);
167 memcpy(vsum, buf, algo->digest_size);
Simon Glass460408e2012-12-05 14:46:36 +0000168 } else {
169 unsigned int i;
170 char *vsum_str;
171 int digits = algo->digest_size * 2;
172
173 /*
174 * As with the original code from sha1sum.c, we assume that a
175 * string which matches the digest size exactly is a hex
176 * string and not an environment variable.
177 */
178 if (strlen(verify_str) == digits)
179 vsum_str = verify_str;
180 else {
181 vsum_str = getenv(verify_str);
182 if (vsum_str == NULL || strlen(vsum_str) != digits) {
183 printf("Expected %d hex digits in env var\n",
184 digits);
185 return 1;
186 }
187 }
188
189 for (i = 0; i < algo->digest_size; i++) {
190 char *nullp = vsum_str + (i + 1) * 2;
191 char end = *nullp;
192
193 *nullp = '\0';
194 vsum[i] = simple_strtoul(vsum_str + (i * 2), NULL, 16);
195 *nullp = end;
196 }
197 }
198 return 0;
199}
200
201static struct hash_algo *find_hash_algo(const char *name)
202{
203 int i;
204
205 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
Simon Glass218da0f2013-02-24 17:33:32 +0000206 if (!strcmp(name, hash_algo[i].name))
Simon Glass460408e2012-12-05 14:46:36 +0000207 return &hash_algo[i];
208 }
209
210 return NULL;
211}
212
213static void show_hash(struct hash_algo *algo, ulong addr, ulong len,
214 u8 *output)
215{
216 int i;
217
218 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
219 for (i = 0; i < algo->digest_size; i++)
220 printf("%02x", output[i]);
221}
222
Simon Glassd5b76672013-02-24 17:33:26 +0000223int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
Simon Glass460408e2012-12-05 14:46:36 +0000224 int argc, char * const argv[])
225{
Simon Glass460408e2012-12-05 14:46:36 +0000226 ulong addr, len;
Simon Glass460408e2012-12-05 14:46:36 +0000227
228 if (argc < 2)
229 return CMD_RET_USAGE;
230
Simon Glassd5b76672013-02-24 17:33:26 +0000231 addr = simple_strtoul(*argv++, NULL, 16);
232 len = simple_strtoul(*argv++, NULL, 16);
233
Simon Glassd20a40d2013-02-24 20:30:22 +0000234 if (multi_hash()) {
235 struct hash_algo *algo;
236 u8 output[HASH_MAX_DIGEST_SIZE];
237 u8 vsum[HASH_MAX_DIGEST_SIZE];
Simon Glassbd091b62013-02-24 17:33:31 +0000238 void *buf;
Simon Glass460408e2012-12-05 14:46:36 +0000239
Simon Glassd20a40d2013-02-24 20:30:22 +0000240 algo = find_hash_algo(algo_name);
241 if (!algo) {
242 printf("Unknown hash algorithm '%s'\n", algo_name);
Simon Glass460408e2012-12-05 14:46:36 +0000243 return CMD_RET_USAGE;
Simon Glassd20a40d2013-02-24 20:30:22 +0000244 }
245 argc -= 2;
246
247 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
248 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
249 return 1;
250 }
251
Simon Glassbd091b62013-02-24 17:33:31 +0000252 buf = map_sysmem(addr, len);
253 algo->hash_func_ws(buf, len, output, algo->chunk_size);
254 unmap_sysmem(buf);
Simon Glassd20a40d2013-02-24 20:30:22 +0000255
256 /* Try to avoid code bloat when verify is not needed */
257#ifdef CONFIG_HASH_VERIFY
258 if (flags & HASH_FLAG_VERIFY) {
259#else
260 if (0) {
261#endif
262 if (!argc)
263 return CMD_RET_USAGE;
264 if (parse_verify_sum(algo, *argv, vsum,
Simon Glassd5b76672013-02-24 17:33:26 +0000265 flags & HASH_FLAG_ENV)) {
Simon Glassd20a40d2013-02-24 20:30:22 +0000266 printf("ERROR: %s does not contain a valid "
267 "%s sum\n", *argv, algo->name);
268 return 1;
269 }
270 if (memcmp(output, vsum, algo->digest_size) != 0) {
271 int i;
Simon Glass460408e2012-12-05 14:46:36 +0000272
Simon Glassd20a40d2013-02-24 20:30:22 +0000273 show_hash(algo, addr, len, output);
274 printf(" != ");
275 for (i = 0; i < algo->digest_size; i++)
276 printf("%02x", vsum[i]);
277 puts(" ** ERROR **\n");
278 return 1;
279 }
280 } else {
Simon Glass460408e2012-12-05 14:46:36 +0000281 show_hash(algo, addr, len, output);
Simon Glassd20a40d2013-02-24 20:30:22 +0000282 printf("\n");
Simon Glass460408e2012-12-05 14:46:36 +0000283
Simon Glassd20a40d2013-02-24 20:30:22 +0000284 if (argc) {
285 store_result(algo, output, *argv,
286 flags & HASH_FLAG_ENV);
287 }
288 }
289
290 /* Horrible code size hack for boards that just want crc32 */
291 } else {
292 ulong crc;
293 ulong *ptr;
294
295 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
296
297 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
298 addr, addr + len - 1, crc);
299
300 if (argc > 3) {
301 ptr = (ulong *)simple_strtoul(argv[3], NULL, 16);
302 *ptr = crc;
Simon Glassd5b76672013-02-24 17:33:26 +0000303 }
Simon Glass460408e2012-12-05 14:46:36 +0000304 }
305
306 return 0;
307}