blob: 2b8e7a4d5c31b8cea1496a35be38646867f1efb7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass460408e2012-12-05 14:46:36 +00002/*
3 * Copyright (c) 2012 The Chromium OS Authors.
4 *
5 * (C) Copyright 2011
6 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
7 *
8 * (C) Copyright 2000
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass460408e2012-12-05 14:46:36 +000010 */
11
Ruchika Gupta2dd90022015-01-23 16:01:58 +053012#ifndef USE_HOSTCC
Simon Glass460408e2012-12-05 14:46:36 +000013#include <common.h>
14#include <command.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060015#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Hung-ying Tyanbf007eb2014-03-03 12:19:28 +010017#include <malloc.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050018#include <mapmem.h>
Akshay Saraswat1f9c9282013-03-20 21:00:58 +000019#include <hw_sha.h>
Simon Glass90526e92020-05-10 11:39:56 -060020#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glassbd091b62013-02-24 17:33:31 +000022#include <asm/io.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090023#include <linux/errno.h>
Simon Glass3db71102019-11-14 12:57:16 -070024#include <u-boot/crc.h>
Ruchika Gupta2dd90022015-01-23 16:01:58 +053025#else
26#include "mkimage.h"
27#include <time.h>
Simon Glass2c212562021-09-25 19:43:18 -060028#include <linux/kconfig.h>
Ruchika Gupta2dd90022015-01-23 16:01:58 +053029#endif /* !USE_HOSTCC*/
30
31#include <hash.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060032#include <image.h>
Ruchika Gupta2dd90022015-01-23 16:01:58 +053033#include <u-boot/crc.h>
34#include <u-boot/sha1.h>
35#include <u-boot/sha256.h>
Reuben Dowled16b38f2020-04-16 17:36:52 +120036#include <u-boot/sha512.h>
Ruchika Gupta2dd90022015-01-23 16:01:58 +053037#include <u-boot/md5.h>
Simon Glass460408e2012-12-05 14:46:36 +000038
T Karthik Reddy10088fb2019-03-16 15:23:01 +053039#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
40DECLARE_GLOBAL_DATA_PTR;
41#endif
42
43static void reloc_update(void);
44
Simon Glass2c212562021-09-25 19:43:18 -060045#if CONFIG_IS_ENABLED(SHA1) && !CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Hung-ying Tyanbf007eb2014-03-03 12:19:28 +010046static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
47{
48 sha1_context *ctx = malloc(sizeof(sha1_context));
49 sha1_starts(ctx);
50 *ctxp = ctx;
51 return 0;
52}
53
54static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
55 unsigned int size, int is_last)
56{
57 sha1_update((sha1_context *)ctx, buf, size);
58 return 0;
59}
60
61static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
62 int size)
63{
64 if (size < algo->digest_size)
65 return -1;
66
67 sha1_finish((sha1_context *)ctx, dest_buf);
68 free(ctx);
69 return 0;
70}
71#endif
72
Simon Glass2c212562021-09-25 19:43:18 -060073#if CONFIG_IS_ENABLED(SHA256) && !CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Hung-ying Tyanbf007eb2014-03-03 12:19:28 +010074static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
75{
76 sha256_context *ctx = malloc(sizeof(sha256_context));
77 sha256_starts(ctx);
78 *ctxp = ctx;
79 return 0;
80}
81
82static int hash_update_sha256(struct hash_algo *algo, void *ctx,
83 const void *buf, unsigned int size, int is_last)
84{
85 sha256_update((sha256_context *)ctx, buf, size);
86 return 0;
87}
88
89static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
90 *dest_buf, int size)
91{
92 if (size < algo->digest_size)
93 return -1;
94
95 sha256_finish((sha256_context *)ctx, dest_buf);
96 free(ctx);
97 return 0;
98}
99#endif
100
Simon Glass2c212562021-09-25 19:43:18 -0600101#if CONFIG_IS_ENABLED(SHA384) && !CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Reuben Dowled16b38f2020-04-16 17:36:52 +1200102static int hash_init_sha384(struct hash_algo *algo, void **ctxp)
103{
104 sha512_context *ctx = malloc(sizeof(sha512_context));
105 sha384_starts(ctx);
106 *ctxp = ctx;
107 return 0;
108}
109
110static int hash_update_sha384(struct hash_algo *algo, void *ctx,
111 const void *buf, unsigned int size, int is_last)
112{
113 sha384_update((sha512_context *)ctx, buf, size);
114 return 0;
115}
116
117static int hash_finish_sha384(struct hash_algo *algo, void *ctx, void
118 *dest_buf, int size)
119{
120 if (size < algo->digest_size)
121 return -1;
122
123 sha384_finish((sha512_context *)ctx, dest_buf);
124 free(ctx);
125 return 0;
126}
127#endif
128
Simon Glass2c212562021-09-25 19:43:18 -0600129#if CONFIG_IS_ENABLED(SHA512) && !CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Reuben Dowled16b38f2020-04-16 17:36:52 +1200130static int hash_init_sha512(struct hash_algo *algo, void **ctxp)
131{
132 sha512_context *ctx = malloc(sizeof(sha512_context));
133 sha512_starts(ctx);
134 *ctxp = ctx;
135 return 0;
136}
137
138static int hash_update_sha512(struct hash_algo *algo, void *ctx,
139 const void *buf, unsigned int size, int is_last)
140{
141 sha512_update((sha512_context *)ctx, buf, size);
142 return 0;
143}
144
145static int hash_finish_sha512(struct hash_algo *algo, void *ctx, void
146 *dest_buf, int size)
147{
148 if (size < algo->digest_size)
149 return -1;
150
Reuben Dowled16b38f2020-04-16 17:36:52 +1200151 sha512_finish((sha512_context *)ctx, dest_buf);
152 free(ctx);
153 return 0;
154}
155#endif
156
157
Philipp Tomsich51c23452018-11-25 19:22:19 +0100158static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
159{
160 uint16_t *ctx = malloc(sizeof(uint16_t));
161 *ctx = 0;
162 *ctxp = ctx;
163 return 0;
164}
165
166static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
167 const void *buf, unsigned int size,
168 int is_last)
169{
170 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
171 return 0;
172}
173
174static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
175 void *dest_buf, int size)
176{
177 if (size < algo->digest_size)
178 return -1;
179
180 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
181 free(ctx);
182 return 0;
183}
184
Hung-ying Tyanbf007eb2014-03-03 12:19:28 +0100185static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
186{
187 uint32_t *ctx = malloc(sizeof(uint32_t));
188 *ctx = 0;
189 *ctxp = ctx;
190 return 0;
191}
192
193static int hash_update_crc32(struct hash_algo *algo, void *ctx,
194 const void *buf, unsigned int size, int is_last)
195{
196 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
197 return 0;
198}
199
200static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
201 int size)
202{
203 if (size < algo->digest_size)
204 return -1;
205
206 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
207 free(ctx);
208 return 0;
209}
210
Simon Glass460408e2012-12-05 14:46:36 +0000211/*
Tom Rini78eda892017-08-14 16:38:07 -0400212 * These are the hash algorithms we support. If we have hardware acceleration
213 * is enable we will use that, otherwise a software version of the algorithm.
214 * Note that algorithm names must be in lower case.
Simon Glass460408e2012-12-05 14:46:36 +0000215 */
216static struct hash_algo hash_algo[] = {
Simon Glass2c212562021-09-25 19:43:18 -0600217#if CONFIG_IS_ENABLED(MD5)
Alexandru Gagniucfe54aea2021-09-02 19:54:20 -0500218 {
219 .name = "md5",
220 .digest_size = MD5_SUM_LEN,
221 .chunk_size = CHUNKSZ_MD5,
222 .hash_func_ws = md5_wd,
223 },
224#endif
Simon Glass2c212562021-09-25 19:43:18 -0600225#if CONFIG_IS_ENABLED(SHA1)
Simon Glass460408e2012-12-05 14:46:36 +0000226 {
Wolfgang Denk0cf207e2021-09-27 17:42:39 +0200227 .name = "sha1",
Tom Rini78eda892017-08-14 16:38:07 -0400228 .digest_size = SHA1_SUM_LEN,
229 .chunk_size = CHUNKSZ_SHA1,
Simon Glass2c212562021-09-25 19:43:18 -0600230#if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
Tom Rini78eda892017-08-14 16:38:07 -0400231 .hash_func_ws = hw_sha1,
232#else
233 .hash_func_ws = sha1_csum_wd,
234#endif
Simon Glass2c212562021-09-25 19:43:18 -0600235#if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Tom Rini78eda892017-08-14 16:38:07 -0400236 .hash_init = hw_sha_init,
237 .hash_update = hw_sha_update,
238 .hash_finish = hw_sha_finish,
239#else
240 .hash_init = hash_init_sha1,
241 .hash_update = hash_update_sha1,
242 .hash_finish = hash_finish_sha1,
243#endif
Simon Glass460408e2012-12-05 14:46:36 +0000244 },
245#endif
Simon Glass2c212562021-09-25 19:43:18 -0600246#if CONFIG_IS_ENABLED(SHA256)
Simon Glass460408e2012-12-05 14:46:36 +0000247 {
Tom Rini78eda892017-08-14 16:38:07 -0400248 .name = "sha256",
249 .digest_size = SHA256_SUM_LEN,
250 .chunk_size = CHUNKSZ_SHA256,
Simon Glass2c212562021-09-25 19:43:18 -0600251#if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
Tom Rini78eda892017-08-14 16:38:07 -0400252 .hash_func_ws = hw_sha256,
253#else
254 .hash_func_ws = sha256_csum_wd,
255#endif
Simon Glass2c212562021-09-25 19:43:18 -0600256#if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Tom Rini78eda892017-08-14 16:38:07 -0400257 .hash_init = hw_sha_init,
258 .hash_update = hw_sha_update,
259 .hash_finish = hw_sha_finish,
260#else
261 .hash_init = hash_init_sha256,
262 .hash_update = hash_update_sha256,
263 .hash_finish = hash_finish_sha256,
264#endif
Simon Glass460408e2012-12-05 14:46:36 +0000265 },
266#endif
Simon Glass2c212562021-09-25 19:43:18 -0600267#if CONFIG_IS_ENABLED(SHA384)
Reuben Dowled16b38f2020-04-16 17:36:52 +1200268 {
269 .name = "sha384",
270 .digest_size = SHA384_SUM_LEN,
271 .chunk_size = CHUNKSZ_SHA384,
Simon Glass2c212562021-09-25 19:43:18 -0600272#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
Joel Stanleya479f102021-02-17 13:50:42 +1030273 .hash_func_ws = hw_sha384,
274#else
Reuben Dowled16b38f2020-04-16 17:36:52 +1200275 .hash_func_ws = sha384_csum_wd,
Joel Stanleya479f102021-02-17 13:50:42 +1030276#endif
Simon Glass2c212562021-09-25 19:43:18 -0600277#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Joel Stanleya479f102021-02-17 13:50:42 +1030278 .hash_init = hw_sha_init,
279 .hash_update = hw_sha_update,
280 .hash_finish = hw_sha_finish,
281#else
Reuben Dowled16b38f2020-04-16 17:36:52 +1200282 .hash_init = hash_init_sha384,
283 .hash_update = hash_update_sha384,
284 .hash_finish = hash_finish_sha384,
Joel Stanleya479f102021-02-17 13:50:42 +1030285#endif
Reuben Dowled16b38f2020-04-16 17:36:52 +1200286 },
287#endif
Simon Glass2c212562021-09-25 19:43:18 -0600288#if CONFIG_IS_ENABLED(SHA512)
Reuben Dowled16b38f2020-04-16 17:36:52 +1200289 {
290 .name = "sha512",
291 .digest_size = SHA512_SUM_LEN,
292 .chunk_size = CHUNKSZ_SHA512,
Simon Glass2c212562021-09-25 19:43:18 -0600293#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
Joel Stanleya479f102021-02-17 13:50:42 +1030294 .hash_func_ws = hw_sha512,
295#else
Reuben Dowled16b38f2020-04-16 17:36:52 +1200296 .hash_func_ws = sha512_csum_wd,
Joel Stanleya479f102021-02-17 13:50:42 +1030297#endif
Simon Glass2c212562021-09-25 19:43:18 -0600298#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Joel Stanleya479f102021-02-17 13:50:42 +1030299 .hash_init = hw_sha_init,
300 .hash_update = hw_sha_update,
301 .hash_finish = hw_sha_finish,
302#else
Reuben Dowled16b38f2020-04-16 17:36:52 +1200303 .hash_init = hash_init_sha512,
304 .hash_update = hash_update_sha512,
305 .hash_finish = hash_finish_sha512,
Joel Stanleya479f102021-02-17 13:50:42 +1030306#endif
Reuben Dowled16b38f2020-04-16 17:36:52 +1200307 },
308#endif
Simon Glassd20a40d2013-02-24 20:30:22 +0000309 {
Philipp Tomsich51c23452018-11-25 19:22:19 +0100310 .name = "crc16-ccitt",
311 .digest_size = 2,
312 .chunk_size = CHUNKSZ,
313 .hash_func_ws = crc16_ccitt_wd_buf,
314 .hash_init = hash_init_crc16_ccitt,
315 .hash_update = hash_update_crc16_ccitt,
316 .hash_finish = hash_finish_crc16_ccitt,
317 },
318 {
Tom Rini78eda892017-08-14 16:38:07 -0400319 .name = "crc32",
320 .digest_size = 4,
321 .chunk_size = CHUNKSZ_CRC32,
322 .hash_func_ws = crc32_wd_buf,
323 .hash_init = hash_init_crc32,
324 .hash_update = hash_update_crc32,
325 .hash_finish = hash_finish_crc32,
Simon Glassd20a40d2013-02-24 20:30:22 +0000326 },
Simon Glass460408e2012-12-05 14:46:36 +0000327};
328
Simon Glassd20a40d2013-02-24 20:30:22 +0000329/* Try to minimize code size for boards that don't want much hashing */
Simon Glass2c212562021-09-25 19:43:18 -0600330#if CONFIG_IS_ENABLED(SHA256) || CONFIG_IS_ENABLED(CMD_SHA1SUM) || \
331 CONFIG_IS_ENABLED(CRC32_VERIFY) || CONFIG_IS_ENABLED(CMD_HASH) || \
332 CONFIG_IS_ENABLED(SHA384) || CONFIG_IS_ENABLED(SHA512)
Simon Glassd20a40d2013-02-24 20:30:22 +0000333#define multi_hash() 1
334#else
335#define multi_hash() 0
336#endif
337
T Karthik Reddy10088fb2019-03-16 15:23:01 +0530338static void reloc_update(void)
339{
340#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
341 int i;
342 static bool done;
343
344 if (!done) {
345 done = true;
346 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
347 hash_algo[i].name += gd->reloc_off;
348 hash_algo[i].hash_func_ws += gd->reloc_off;
349 hash_algo[i].hash_init += gd->reloc_off;
350 hash_algo[i].hash_update += gd->reloc_off;
351 hash_algo[i].hash_finish += gd->reloc_off;
352 }
353 }
354#endif
355}
356
Ruchika Gupta2dd90022015-01-23 16:01:58 +0530357int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
358{
359 int i;
360
T Karthik Reddy10088fb2019-03-16 15:23:01 +0530361 reloc_update();
362
Ruchika Gupta2dd90022015-01-23 16:01:58 +0530363 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
364 if (!strcmp(algo_name, hash_algo[i].name)) {
365 *algop = &hash_algo[i];
366 return 0;
367 }
368 }
369
370 debug("Unknown hash algorithm '%s'\n", algo_name);
371 return -EPROTONOSUPPORT;
372}
373
374int hash_progressive_lookup_algo(const char *algo_name,
375 struct hash_algo **algop)
376{
377 int i;
378
T Karthik Reddy10088fb2019-03-16 15:23:01 +0530379 reloc_update();
380
Ruchika Gupta2dd90022015-01-23 16:01:58 +0530381 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
382 if (!strcmp(algo_name, hash_algo[i].name)) {
383 if (hash_algo[i].hash_init) {
384 *algop = &hash_algo[i];
385 return 0;
386 }
387 }
388 }
389
390 debug("Unknown hash algorithm '%s'\n", algo_name);
391 return -EPROTONOSUPPORT;
392}
393
394#ifndef USE_HOSTCC
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200395int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
396{
397 struct hash_algo *algo;
398 int ret;
399 int i;
400
401 ret = hash_lookup_algo(algo_name, &algo);
402 if (ret)
403 return ret;
404
405 for (i = 0; i < algo->digest_size; i++) {
406 char chr[3];
407
Simon Glass031725f2021-07-24 09:03:28 -0600408 strlcpy(chr, &str[i * 2], 3);
Simon Glass7e5f4602021-07-24 09:03:29 -0600409 result[i] = hextoul(chr, NULL);
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200410 }
411
412 return 0;
413}
414
Tom Rini48ad68d2016-01-05 08:47:48 -0500415int hash_block(const char *algo_name, const void *data, unsigned int len,
416 uint8_t *output, int *output_size)
417{
418 struct hash_algo *algo;
419 int ret;
420
421 ret = hash_lookup_algo(algo_name, &algo);
422 if (ret)
423 return ret;
424
425 if (output_size && *output_size < algo->digest_size) {
426 debug("Output buffer size %d too small (need %d bytes)",
427 *output_size, algo->digest_size);
428 return -ENOSPC;
429 }
430 if (output_size)
431 *output_size = algo->digest_size;
432 algo->hash_func_ws(data, len, output, algo->chunk_size);
433
434 return 0;
435}
436
Simon Glass2c212562021-09-25 19:43:18 -0600437#if !defined(CONFIG_SPL_BUILD) && (defined(CONFIG_CMD_HASH) || \
438 defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32))
Simon Glass460408e2012-12-05 14:46:36 +0000439/**
440 * store_result: Store the resulting sum to an address or variable
441 *
442 * @algo: Hash algorithm being used
443 * @sum: Hash digest (algo->digest_size bytes)
444 * @dest: Destination, interpreted as a hex address if it starts
Simon Glassd5b76672013-02-24 17:33:26 +0000445 * with * (or allow_env_vars is 0) or otherwise as an
446 * environment variable.
447 * @allow_env_vars: non-zero to permit storing the result to an
448 * variable environment
Simon Glass460408e2012-12-05 14:46:36 +0000449 */
Simon Glass04819a42014-06-12 07:24:41 -0600450static void store_result(struct hash_algo *algo, const uint8_t *sum,
Simon Glassd5b76672013-02-24 17:33:26 +0000451 const char *dest, int allow_env_vars)
Simon Glass460408e2012-12-05 14:46:36 +0000452{
453 unsigned int i;
Simon Glassd5b76672013-02-24 17:33:26 +0000454 int env_var = 0;
Simon Glass460408e2012-12-05 14:46:36 +0000455
Simon Glassd5b76672013-02-24 17:33:26 +0000456 /*
457 * If environment variables are allowed, then we assume that 'dest'
458 * is an environment variable, unless it starts with *, in which
459 * case we assume it is an address. If not allowed, it is always an
460 * address. This is to support the crc32 command.
461 */
462 if (allow_env_vars) {
463 if (*dest == '*')
464 dest++;
465 else
466 env_var = 1;
467 }
Simon Glass460408e2012-12-05 14:46:36 +0000468
Simon Glassd5b76672013-02-24 17:33:26 +0000469 if (env_var) {
Simon Glass460408e2012-12-05 14:46:36 +0000470 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
471 char *str_ptr = str_output;
472
473 for (i = 0; i < algo->digest_size; i++) {
474 sprintf(str_ptr, "%02x", sum[i]);
475 str_ptr += 2;
476 }
Jeroen Hofstee8b9cc862014-06-09 11:02:02 +0200477 *str_ptr = '\0';
Simon Glass382bee52017-08-03 12:22:09 -0600478 env_set(dest, str_output);
Simon Glassd5b76672013-02-24 17:33:26 +0000479 } else {
Simon Glassbd091b62013-02-24 17:33:31 +0000480 ulong addr;
481 void *buf;
Simon Glassd5b76672013-02-24 17:33:26 +0000482
Simon Glass7e5f4602021-07-24 09:03:29 -0600483 addr = hextoul(dest, NULL);
Simon Glassbd091b62013-02-24 17:33:31 +0000484 buf = map_sysmem(addr, algo->digest_size);
485 memcpy(buf, sum, algo->digest_size);
486 unmap_sysmem(buf);
Simon Glass460408e2012-12-05 14:46:36 +0000487 }
488}
489
490/**
491 * parse_verify_sum: Parse a hash verification parameter
492 *
493 * @algo: Hash algorithm being used
494 * @verify_str: Argument to parse. If it starts with * then it is
495 * interpreted as a hex address containing the hash.
496 * If the length is exactly the right number of hex digits
497 * for the digest size, then we assume it is a hex digest.
498 * Otherwise we assume it is an environment variable, and
499 * look up its value (it must contain a hex digest).
500 * @vsum: Returns binary digest value (algo->digest_size bytes)
Simon Glassd5b76672013-02-24 17:33:26 +0000501 * @allow_env_vars: non-zero to permit storing the result to an environment
502 * variable. If 0 then verify_str is assumed to be an
503 * address, and the * prefix is not expected.
Simon Glass460408e2012-12-05 14:46:36 +0000504 * @return 0 if ok, non-zero on error
505 */
Simon Glass04819a42014-06-12 07:24:41 -0600506static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
507 uint8_t *vsum, int allow_env_vars)
Simon Glass460408e2012-12-05 14:46:36 +0000508{
Simon Glassd5b76672013-02-24 17:33:26 +0000509 int env_var = 0;
510
511 /* See comment above in store_result() */
512 if (allow_env_vars) {
513 if (*verify_str == '*')
514 verify_str++;
515 else
516 env_var = 1;
517 }
518
Nikolay Dimitrov3ef46a92014-12-12 20:01:23 +0200519 if (!env_var) {
Simon Glassbd091b62013-02-24 17:33:31 +0000520 ulong addr;
521 void *buf;
Simon Glass460408e2012-12-05 14:46:36 +0000522
Simon Glass7e5f4602021-07-24 09:03:29 -0600523 addr = hextoul(verify_str, NULL);
Simon Glassbd091b62013-02-24 17:33:31 +0000524 buf = map_sysmem(addr, algo->digest_size);
525 memcpy(vsum, buf, algo->digest_size);
Simon Glass460408e2012-12-05 14:46:36 +0000526 } else {
Simon Glass460408e2012-12-05 14:46:36 +0000527 char *vsum_str;
528 int digits = algo->digest_size * 2;
529
530 /*
531 * As with the original code from sha1sum.c, we assume that a
532 * string which matches the digest size exactly is a hex
533 * string and not an environment variable.
534 */
535 if (strlen(verify_str) == digits)
536 vsum_str = verify_str;
537 else {
Simon Glass00caae62017-08-03 12:22:12 -0600538 vsum_str = env_get(verify_str);
Simon Glass460408e2012-12-05 14:46:36 +0000539 if (vsum_str == NULL || strlen(vsum_str) != digits) {
540 printf("Expected %d hex digits in env var\n",
541 digits);
542 return 1;
543 }
544 }
545
Stefan Roese8f0b1e22015-05-18 14:08:24 +0200546 hash_parse_string(algo->name, vsum_str, vsum);
Simon Glass460408e2012-12-05 14:46:36 +0000547 }
548 return 0;
549}
550
Tom Rini48ad68d2016-01-05 08:47:48 -0500551static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
Simon Glass460408e2012-12-05 14:46:36 +0000552{
553 int i;
554
555 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
556 for (i = 0; i < algo->digest_size; i++)
557 printf("%02x", output[i]);
558}
559
Simon Glass09140112020-05-10 11:40:03 -0600560int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
561 int flag, int argc, char *const argv[])
Simon Glass460408e2012-12-05 14:46:36 +0000562{
Simon Glass460408e2012-12-05 14:46:36 +0000563 ulong addr, len;
Simon Glass460408e2012-12-05 14:46:36 +0000564
Nikolay Dimitrov3ef46a92014-12-12 20:01:23 +0200565 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
Simon Glass460408e2012-12-05 14:46:36 +0000566 return CMD_RET_USAGE;
567
Simon Glass7e5f4602021-07-24 09:03:29 -0600568 addr = hextoul(*argv++, NULL);
569 len = hextoul(*argv++, NULL);
Simon Glassd5b76672013-02-24 17:33:26 +0000570
Simon Glassd20a40d2013-02-24 20:30:22 +0000571 if (multi_hash()) {
572 struct hash_algo *algo;
Breno Limad7af2ba2018-01-17 10:03:45 -0200573 u8 *output;
Simon Glass04819a42014-06-12 07:24:41 -0600574 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
Simon Glassbd091b62013-02-24 17:33:31 +0000575 void *buf;
Simon Glass460408e2012-12-05 14:46:36 +0000576
Hung-ying Tyanbf007eb2014-03-03 12:19:28 +0100577 if (hash_lookup_algo(algo_name, &algo)) {
Simon Glassd20a40d2013-02-24 20:30:22 +0000578 printf("Unknown hash algorithm '%s'\n", algo_name);
Simon Glass460408e2012-12-05 14:46:36 +0000579 return CMD_RET_USAGE;
Simon Glassd20a40d2013-02-24 20:30:22 +0000580 }
581 argc -= 2;
582
583 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
584 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
585 return 1;
586 }
587
Breno Limad7af2ba2018-01-17 10:03:45 -0200588 output = memalign(ARCH_DMA_MINALIGN,
589 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
590
Simon Glassbd091b62013-02-24 17:33:31 +0000591 buf = map_sysmem(addr, len);
592 algo->hash_func_ws(buf, len, output, algo->chunk_size);
593 unmap_sysmem(buf);
Simon Glassd20a40d2013-02-24 20:30:22 +0000594
595 /* Try to avoid code bloat when verify is not needed */
Daniel Thompson221a9492017-05-19 17:26:58 +0100596#if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
597 defined(CONFIG_HASH_VERIFY)
Simon Glassd20a40d2013-02-24 20:30:22 +0000598 if (flags & HASH_FLAG_VERIFY) {
599#else
600 if (0) {
601#endif
Simon Glassd20a40d2013-02-24 20:30:22 +0000602 if (parse_verify_sum(algo, *argv, vsum,
Simon Glassd5b76672013-02-24 17:33:26 +0000603 flags & HASH_FLAG_ENV)) {
Simon Glassd20a40d2013-02-24 20:30:22 +0000604 printf("ERROR: %s does not contain a valid "
605 "%s sum\n", *argv, algo->name);
606 return 1;
607 }
608 if (memcmp(output, vsum, algo->digest_size) != 0) {
609 int i;
Simon Glass460408e2012-12-05 14:46:36 +0000610
Simon Glass31890ae2014-06-02 22:04:49 -0600611 hash_show(algo, addr, len, output);
Simon Glassd20a40d2013-02-24 20:30:22 +0000612 printf(" != ");
613 for (i = 0; i < algo->digest_size; i++)
614 printf("%02x", vsum[i]);
615 puts(" ** ERROR **\n");
616 return 1;
617 }
618 } else {
Simon Glass31890ae2014-06-02 22:04:49 -0600619 hash_show(algo, addr, len, output);
Simon Glassd20a40d2013-02-24 20:30:22 +0000620 printf("\n");
Simon Glass460408e2012-12-05 14:46:36 +0000621
Simon Glassd20a40d2013-02-24 20:30:22 +0000622 if (argc) {
623 store_result(algo, output, *argv,
624 flags & HASH_FLAG_ENV);
625 }
Breno Limad7af2ba2018-01-17 10:03:45 -0200626 unmap_sysmem(output);
627
Simon Glassd20a40d2013-02-24 20:30:22 +0000628 }
629
630 /* Horrible code size hack for boards that just want crc32 */
631 } else {
632 ulong crc;
633 ulong *ptr;
634
635 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
636
637 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
638 addr, addr + len - 1, crc);
639
Tom Rini4b756b02013-11-07 07:39:48 -0500640 if (argc >= 3) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600641 ptr = (ulong *)hextoul(argv[0], NULL);
Simon Glassd20a40d2013-02-24 20:30:22 +0000642 *ptr = crc;
Simon Glassd5b76672013-02-24 17:33:26 +0000643 }
Simon Glass460408e2012-12-05 14:46:36 +0000644 }
645
646 return 0;
647}
Simon Glassd70f9192017-05-17 09:05:34 -0600648#endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
649#endif /* !USE_HOSTCC */