blob: 3d6b84de4738223ebb83048dd394b422f7157b94 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb8790452012-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 Glassb8790452012-12-05 14:46:36 +000010 */
11
Ruchika Guptac915ceb2015-01-23 16:01:58 +053012#ifndef USE_HOSTCC
Simon Glassb8790452012-12-05 14:46:36 +000013#include <common.h>
14#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060015#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010017#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050018#include <mapmem.h>
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +000019#include <hw_sha.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053022#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090023#include <linux/errno.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053024#else
25#include "mkimage.h"
Simon Glass866178e2021-09-25 19:43:19 -060026#include <linux/compiler_attributes.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053027#include <time.h>
Simon Glass383dd572021-09-25 19:43:18 -060028#include <linux/kconfig.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053029#endif /* !USE_HOSTCC*/
30
Simon Glassb8790452012-12-05 14:46:36 +000031#include <hash.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060032#include <image.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053033#include <u-boot/crc.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020034#include <u-boot/sha1.h>
35#include <u-boot/sha256.h>
Reuben Dowle1908fd92020-04-16 17:36:52 +120036#include <u-boot/sha512.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053037#include <u-boot/md5.h>
Simon Glassb8790452012-12-05 14:46:36 +000038
Simon Glass866178e2021-09-25 19:43:19 -060039static int __maybe_unused hash_init_sha1(struct hash_algo *algo, void **ctxp)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010040{
41 sha1_context *ctx = malloc(sizeof(sha1_context));
42 sha1_starts(ctx);
43 *ctxp = ctx;
44 return 0;
45}
46
Simon Glass866178e2021-09-25 19:43:19 -060047static int __maybe_unused hash_update_sha1(struct hash_algo *algo, void *ctx,
48 const void *buf, unsigned int size,
49 int is_last)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010050{
51 sha1_update((sha1_context *)ctx, buf, size);
52 return 0;
53}
54
Simon Glass866178e2021-09-25 19:43:19 -060055static int __maybe_unused hash_finish_sha1(struct hash_algo *algo, void *ctx,
56 void *dest_buf, int size)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010057{
58 if (size < algo->digest_size)
59 return -1;
60
61 sha1_finish((sha1_context *)ctx, dest_buf);
62 free(ctx);
63 return 0;
64}
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010065
Simon Glass866178e2021-09-25 19:43:19 -060066static int __maybe_unused hash_init_sha256(struct hash_algo *algo, void **ctxp)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010067{
68 sha256_context *ctx = malloc(sizeof(sha256_context));
69 sha256_starts(ctx);
70 *ctxp = ctx;
71 return 0;
72}
73
Simon Glass866178e2021-09-25 19:43:19 -060074static int __maybe_unused hash_update_sha256(struct hash_algo *algo, void *ctx,
75 const void *buf, uint size,
76 int is_last)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010077{
78 sha256_update((sha256_context *)ctx, buf, size);
79 return 0;
80}
81
Simon Glass866178e2021-09-25 19:43:19 -060082static int __maybe_unused hash_finish_sha256(struct hash_algo *algo, void *ctx,
83 void *dest_buf, int size)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010084{
85 if (size < algo->digest_size)
86 return -1;
87
88 sha256_finish((sha256_context *)ctx, dest_buf);
89 free(ctx);
90 return 0;
91}
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010092
Simon Glass866178e2021-09-25 19:43:19 -060093static int __maybe_unused hash_init_sha384(struct hash_algo *algo, void **ctxp)
Reuben Dowle1908fd92020-04-16 17:36:52 +120094{
95 sha512_context *ctx = malloc(sizeof(sha512_context));
96 sha384_starts(ctx);
97 *ctxp = ctx;
98 return 0;
99}
100
Simon Glass866178e2021-09-25 19:43:19 -0600101static int __maybe_unused hash_update_sha384(struct hash_algo *algo, void *ctx,
102 const void *buf, uint size,
103 int is_last)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200104{
105 sha384_update((sha512_context *)ctx, buf, size);
106 return 0;
107}
108
Simon Glass866178e2021-09-25 19:43:19 -0600109static int __maybe_unused hash_finish_sha384(struct hash_algo *algo, void *ctx,
110 void *dest_buf, int size)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200111{
112 if (size < algo->digest_size)
113 return -1;
114
115 sha384_finish((sha512_context *)ctx, dest_buf);
116 free(ctx);
117 return 0;
118}
Reuben Dowle1908fd92020-04-16 17:36:52 +1200119
Simon Glass866178e2021-09-25 19:43:19 -0600120static int __maybe_unused hash_init_sha512(struct hash_algo *algo, void **ctxp)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200121{
122 sha512_context *ctx = malloc(sizeof(sha512_context));
123 sha512_starts(ctx);
124 *ctxp = ctx;
125 return 0;
126}
127
Simon Glass866178e2021-09-25 19:43:19 -0600128static int __maybe_unused hash_update_sha512(struct hash_algo *algo, void *ctx,
129 const void *buf, uint size,
130 int is_last)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200131{
132 sha512_update((sha512_context *)ctx, buf, size);
133 return 0;
134}
135
Simon Glass866178e2021-09-25 19:43:19 -0600136static int __maybe_unused hash_finish_sha512(struct hash_algo *algo, void *ctx,
137 void *dest_buf, int size)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200138{
139 if (size < algo->digest_size)
140 return -1;
141
Reuben Dowle1908fd92020-04-16 17:36:52 +1200142 sha512_finish((sha512_context *)ctx, dest_buf);
143 free(ctx);
144 return 0;
145}
Reuben Dowle1908fd92020-04-16 17:36:52 +1200146
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100147static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
148{
149 uint16_t *ctx = malloc(sizeof(uint16_t));
150 *ctx = 0;
151 *ctxp = ctx;
152 return 0;
153}
154
155static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
156 const void *buf, unsigned int size,
157 int is_last)
158{
159 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
160 return 0;
161}
162
163static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
164 void *dest_buf, int size)
165{
166 if (size < algo->digest_size)
167 return -1;
168
169 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
170 free(ctx);
171 return 0;
172}
173
Simon Glass577226c2021-09-25 19:43:24 -0600174static int __maybe_unused hash_init_crc32(struct hash_algo *algo, void **ctxp)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100175{
176 uint32_t *ctx = malloc(sizeof(uint32_t));
177 *ctx = 0;
178 *ctxp = ctx;
179 return 0;
180}
181
Simon Glass577226c2021-09-25 19:43:24 -0600182static int __maybe_unused hash_update_crc32(struct hash_algo *algo, void *ctx,
183 const void *buf, unsigned int size,
184 int is_last)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100185{
186 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
187 return 0;
188}
189
Simon Glass577226c2021-09-25 19:43:24 -0600190static int __maybe_unused hash_finish_crc32(struct hash_algo *algo, void *ctx,
191 void *dest_buf, int size)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100192{
193 if (size < algo->digest_size)
194 return -1;
195
196 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
197 free(ctx);
198 return 0;
199}
200
Simon Glassb8790452012-12-05 14:46:36 +0000201/*
Tom Rini03361c72017-08-14 16:38:07 -0400202 * These are the hash algorithms we support. If we have hardware acceleration
203 * is enable we will use that, otherwise a software version of the algorithm.
204 * Note that algorithm names must be in lower case.
Simon Glassb8790452012-12-05 14:46:36 +0000205 */
206static struct hash_algo hash_algo[] = {
Simon Glass383dd572021-09-25 19:43:18 -0600207#if CONFIG_IS_ENABLED(MD5)
Alexandru Gagniucf3016582021-09-02 19:54:20 -0500208 {
209 .name = "md5",
210 .digest_size = MD5_SUM_LEN,
211 .chunk_size = CHUNKSZ_MD5,
212 .hash_func_ws = md5_wd,
213 },
214#endif
Simon Glass383dd572021-09-25 19:43:18 -0600215#if CONFIG_IS_ENABLED(SHA1)
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +0000216 {
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200217 .name = "sha1",
Tom Rini03361c72017-08-14 16:38:07 -0400218 .digest_size = SHA1_SUM_LEN,
219 .chunk_size = CHUNKSZ_SHA1,
Simon Glass383dd572021-09-25 19:43:18 -0600220#if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400221 .hash_func_ws = hw_sha1,
222#else
223 .hash_func_ws = sha1_csum_wd,
gaurav ranaef201592015-02-20 12:51:46 +0530224#endif
Simon Glass383dd572021-09-25 19:43:18 -0600225#if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400226 .hash_init = hw_sha_init,
227 .hash_update = hw_sha_update,
228 .hash_finish = hw_sha_finish,
229#else
230 .hash_init = hash_init_sha1,
231 .hash_update = hash_update_sha1,
232 .hash_finish = hash_finish_sha1,
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +0000233#endif
Simon Glassb8790452012-12-05 14:46:36 +0000234 },
235#endif
Simon Glass383dd572021-09-25 19:43:18 -0600236#if CONFIG_IS_ENABLED(SHA256)
Simon Glassb8790452012-12-05 14:46:36 +0000237 {
Tom Rini03361c72017-08-14 16:38:07 -0400238 .name = "sha256",
239 .digest_size = SHA256_SUM_LEN,
240 .chunk_size = CHUNKSZ_SHA256,
Simon Glass383dd572021-09-25 19:43:18 -0600241#if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400242 .hash_func_ws = hw_sha256,
243#else
244 .hash_func_ws = sha256_csum_wd,
245#endif
Simon Glass383dd572021-09-25 19:43:18 -0600246#if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400247 .hash_init = hw_sha_init,
248 .hash_update = hw_sha_update,
249 .hash_finish = hw_sha_finish,
250#else
251 .hash_init = hash_init_sha256,
252 .hash_update = hash_update_sha256,
253 .hash_finish = hash_finish_sha256,
254#endif
Simon Glassb8790452012-12-05 14:46:36 +0000255 },
256#endif
Simon Glass383dd572021-09-25 19:43:18 -0600257#if CONFIG_IS_ENABLED(SHA384)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200258 {
259 .name = "sha384",
260 .digest_size = SHA384_SUM_LEN,
261 .chunk_size = CHUNKSZ_SHA384,
Simon Glass383dd572021-09-25 19:43:18 -0600262#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030263 .hash_func_ws = hw_sha384,
264#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200265 .hash_func_ws = sha384_csum_wd,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030266#endif
Simon Glass383dd572021-09-25 19:43:18 -0600267#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030268 .hash_init = hw_sha_init,
269 .hash_update = hw_sha_update,
270 .hash_finish = hw_sha_finish,
271#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200272 .hash_init = hash_init_sha384,
273 .hash_update = hash_update_sha384,
274 .hash_finish = hash_finish_sha384,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030275#endif
Reuben Dowle1908fd92020-04-16 17:36:52 +1200276 },
277#endif
Simon Glass383dd572021-09-25 19:43:18 -0600278#if CONFIG_IS_ENABLED(SHA512)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200279 {
280 .name = "sha512",
281 .digest_size = SHA512_SUM_LEN,
282 .chunk_size = CHUNKSZ_SHA512,
Simon Glass383dd572021-09-25 19:43:18 -0600283#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030284 .hash_func_ws = hw_sha512,
285#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200286 .hash_func_ws = sha512_csum_wd,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030287#endif
Simon Glass383dd572021-09-25 19:43:18 -0600288#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030289 .hash_init = hw_sha_init,
290 .hash_update = hw_sha_update,
291 .hash_finish = hw_sha_finish,
292#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200293 .hash_init = hash_init_sha512,
294 .hash_update = hash_update_sha512,
295 .hash_finish = hash_finish_sha512,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030296#endif
Reuben Dowle1908fd92020-04-16 17:36:52 +1200297 },
298#endif
Simon Glass0bbd76f2013-02-24 20:30:22 +0000299 {
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100300 .name = "crc16-ccitt",
301 .digest_size = 2,
302 .chunk_size = CHUNKSZ,
303 .hash_func_ws = crc16_ccitt_wd_buf,
304 .hash_init = hash_init_crc16_ccitt,
305 .hash_update = hash_update_crc16_ccitt,
306 .hash_finish = hash_finish_crc16_ccitt,
307 },
Simon Glass577226c2021-09-25 19:43:24 -0600308#if CONFIG_IS_ENABLED(CRC32)
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100309 {
Tom Rini03361c72017-08-14 16:38:07 -0400310 .name = "crc32",
311 .digest_size = 4,
312 .chunk_size = CHUNKSZ_CRC32,
313 .hash_func_ws = crc32_wd_buf,
314 .hash_init = hash_init_crc32,
315 .hash_update = hash_update_crc32,
316 .hash_finish = hash_finish_crc32,
Simon Glass0bbd76f2013-02-24 20:30:22 +0000317 },
Simon Glass577226c2021-09-25 19:43:24 -0600318#endif
Simon Glassb8790452012-12-05 14:46:36 +0000319};
320
Simon Glass0bbd76f2013-02-24 20:30:22 +0000321/* Try to minimize code size for boards that don't want much hashing */
Simon Glass905ea882023-02-05 15:36:42 -0700322#if CONFIG_IS_ENABLED(SHA256) || IS_ENABLED(CONFIG_CMD_SHA1SUM) || \
Simon Glass778451e2023-02-05 15:36:32 -0700323 CONFIG_IS_ENABLED(CRC32_VERIFY) || IS_ENABLED(CONFIG_CMD_HASH) || \
Igor Opaniuk489a5782024-03-02 16:05:48 +0100324 CONFIG_IS_ENABLED(SHA384) || CONFIG_IS_ENABLED(SHA512) || \
325 IS_ENABLED(CONFIG_CMD_MD5SUM)
Simon Glass0bbd76f2013-02-24 20:30:22 +0000326#define multi_hash() 1
327#else
328#define multi_hash() 0
329#endif
330
Ruchika Guptac915ceb2015-01-23 16:01:58 +0530331int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
332{
333 int i;
334
335 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
336 if (!strcmp(algo_name, hash_algo[i].name)) {
337 *algop = &hash_algo[i];
338 return 0;
339 }
340 }
341
342 debug("Unknown hash algorithm '%s'\n", algo_name);
343 return -EPROTONOSUPPORT;
344}
345
346int hash_progressive_lookup_algo(const char *algo_name,
347 struct hash_algo **algop)
348{
349 int i;
350
351 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
352 if (!strcmp(algo_name, hash_algo[i].name)) {
353 if (hash_algo[i].hash_init) {
354 *algop = &hash_algo[i];
355 return 0;
356 }
357 }
358 }
359
360 debug("Unknown hash algorithm '%s'\n", algo_name);
361 return -EPROTONOSUPPORT;
362}
363
364#ifndef USE_HOSTCC
Stefan Roese0ed2e462015-05-18 14:08:24 +0200365int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
366{
367 struct hash_algo *algo;
368 int ret;
369 int i;
370
371 ret = hash_lookup_algo(algo_name, &algo);
372 if (ret)
373 return ret;
374
375 for (i = 0; i < algo->digest_size; i++) {
376 char chr[3];
377
Simon Glassb827f392021-07-24 09:03:28 -0600378 strlcpy(chr, &str[i * 2], 3);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600379 result[i] = hextoul(chr, NULL);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200380 }
381
382 return 0;
383}
384
Tom Rini61738382016-01-05 08:47:48 -0500385int hash_block(const char *algo_name, const void *data, unsigned int len,
386 uint8_t *output, int *output_size)
387{
388 struct hash_algo *algo;
389 int ret;
390
391 ret = hash_lookup_algo(algo_name, &algo);
392 if (ret)
393 return ret;
394
395 if (output_size && *output_size < algo->digest_size) {
396 debug("Output buffer size %d too small (need %d bytes)",
397 *output_size, algo->digest_size);
398 return -ENOSPC;
399 }
400 if (output_size)
401 *output_size = algo->digest_size;
402 algo->hash_func_ws(data, len, output, algo->chunk_size);
403
404 return 0;
405}
406
Simon Glass383dd572021-09-25 19:43:18 -0600407#if !defined(CONFIG_SPL_BUILD) && (defined(CONFIG_CMD_HASH) || \
Igor Opaniuk489a5782024-03-02 16:05:48 +0100408 defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)) || \
409 defined(CONFIG_CMD_MD5SUM)
Simon Glassb8790452012-12-05 14:46:36 +0000410/**
411 * store_result: Store the resulting sum to an address or variable
412 *
413 * @algo: Hash algorithm being used
414 * @sum: Hash digest (algo->digest_size bytes)
415 * @dest: Destination, interpreted as a hex address if it starts
Simon Glass80e345a2013-02-24 17:33:26 +0000416 * with * (or allow_env_vars is 0) or otherwise as an
417 * environment variable.
418 * @allow_env_vars: non-zero to permit storing the result to an
419 * variable environment
Simon Glassb8790452012-12-05 14:46:36 +0000420 */
Simon Glassa3b240b2014-06-12 07:24:41 -0600421static void store_result(struct hash_algo *algo, const uint8_t *sum,
Simon Glass80e345a2013-02-24 17:33:26 +0000422 const char *dest, int allow_env_vars)
Simon Glassb8790452012-12-05 14:46:36 +0000423{
424 unsigned int i;
Simon Glass80e345a2013-02-24 17:33:26 +0000425 int env_var = 0;
Simon Glassb8790452012-12-05 14:46:36 +0000426
Simon Glass80e345a2013-02-24 17:33:26 +0000427 /*
428 * If environment variables are allowed, then we assume that 'dest'
429 * is an environment variable, unless it starts with *, in which
430 * case we assume it is an address. If not allowed, it is always an
431 * address. This is to support the crc32 command.
432 */
433 if (allow_env_vars) {
434 if (*dest == '*')
435 dest++;
436 else
437 env_var = 1;
438 }
Simon Glassb8790452012-12-05 14:46:36 +0000439
Simon Glass80e345a2013-02-24 17:33:26 +0000440 if (env_var) {
Simon Glassb8790452012-12-05 14:46:36 +0000441 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
442 char *str_ptr = str_output;
443
444 for (i = 0; i < algo->digest_size; i++) {
445 sprintf(str_ptr, "%02x", sum[i]);
446 str_ptr += 2;
447 }
Jeroen Hofsteeb14c0522014-06-09 11:02:02 +0200448 *str_ptr = '\0';
Simon Glass6a38e412017-08-03 12:22:09 -0600449 env_set(dest, str_output);
Simon Glass80e345a2013-02-24 17:33:26 +0000450 } else {
Simon Glass68f15562013-02-24 17:33:31 +0000451 ulong addr;
452 void *buf;
Simon Glass80e345a2013-02-24 17:33:26 +0000453
Simon Glass3ff49ec2021-07-24 09:03:29 -0600454 addr = hextoul(dest, NULL);
Simon Glass68f15562013-02-24 17:33:31 +0000455 buf = map_sysmem(addr, algo->digest_size);
456 memcpy(buf, sum, algo->digest_size);
457 unmap_sysmem(buf);
Simon Glassb8790452012-12-05 14:46:36 +0000458 }
459}
460
461/**
462 * parse_verify_sum: Parse a hash verification parameter
463 *
464 * @algo: Hash algorithm being used
465 * @verify_str: Argument to parse. If it starts with * then it is
466 * interpreted as a hex address containing the hash.
467 * If the length is exactly the right number of hex digits
468 * for the digest size, then we assume it is a hex digest.
469 * Otherwise we assume it is an environment variable, and
470 * look up its value (it must contain a hex digest).
471 * @vsum: Returns binary digest value (algo->digest_size bytes)
Simon Glass80e345a2013-02-24 17:33:26 +0000472 * @allow_env_vars: non-zero to permit storing the result to an environment
473 * variable. If 0 then verify_str is assumed to be an
474 * address, and the * prefix is not expected.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100475 * Return: 0 if ok, non-zero on error
Simon Glassb8790452012-12-05 14:46:36 +0000476 */
Simon Glassa3b240b2014-06-12 07:24:41 -0600477static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
478 uint8_t *vsum, int allow_env_vars)
Simon Glassb8790452012-12-05 14:46:36 +0000479{
Simon Glass80e345a2013-02-24 17:33:26 +0000480 int env_var = 0;
481
482 /* See comment above in store_result() */
483 if (allow_env_vars) {
484 if (*verify_str == '*')
485 verify_str++;
486 else
487 env_var = 1;
488 }
489
Nikolay Dimitrov810b84d2014-12-12 20:01:23 +0200490 if (!env_var) {
Simon Glass68f15562013-02-24 17:33:31 +0000491 ulong addr;
492 void *buf;
Simon Glassb8790452012-12-05 14:46:36 +0000493
Simon Glass3ff49ec2021-07-24 09:03:29 -0600494 addr = hextoul(verify_str, NULL);
Simon Glass68f15562013-02-24 17:33:31 +0000495 buf = map_sysmem(addr, algo->digest_size);
496 memcpy(vsum, buf, algo->digest_size);
Simon Glassb8790452012-12-05 14:46:36 +0000497 } else {
Simon Glassb8790452012-12-05 14:46:36 +0000498 char *vsum_str;
499 int digits = algo->digest_size * 2;
500
501 /*
502 * As with the original code from sha1sum.c, we assume that a
503 * string which matches the digest size exactly is a hex
504 * string and not an environment variable.
505 */
506 if (strlen(verify_str) == digits)
507 vsum_str = verify_str;
508 else {
Simon Glass64b723f2017-08-03 12:22:12 -0600509 vsum_str = env_get(verify_str);
Simon Glassb8790452012-12-05 14:46:36 +0000510 if (vsum_str == NULL || strlen(vsum_str) != digits) {
511 printf("Expected %d hex digits in env var\n",
512 digits);
513 return 1;
514 }
515 }
516
Stefan Roese0ed2e462015-05-18 14:08:24 +0200517 hash_parse_string(algo->name, vsum_str, vsum);
Simon Glassb8790452012-12-05 14:46:36 +0000518 }
519 return 0;
520}
521
Tom Rini61738382016-01-05 08:47:48 -0500522static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
Simon Glassb8790452012-12-05 14:46:36 +0000523{
524 int i;
525
526 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
527 for (i = 0; i < algo->digest_size; i++)
528 printf("%02x", output[i]);
529}
530
Simon Glassed38aef2020-05-10 11:40:03 -0600531int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
532 int flag, int argc, char *const argv[])
Simon Glassb8790452012-12-05 14:46:36 +0000533{
Simon Glassb8790452012-12-05 14:46:36 +0000534 ulong addr, len;
Simon Glassb8790452012-12-05 14:46:36 +0000535
Nikolay Dimitrov810b84d2014-12-12 20:01:23 +0200536 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
Simon Glassb8790452012-12-05 14:46:36 +0000537 return CMD_RET_USAGE;
538
Simon Glass3ff49ec2021-07-24 09:03:29 -0600539 addr = hextoul(*argv++, NULL);
540 len = hextoul(*argv++, NULL);
Simon Glass80e345a2013-02-24 17:33:26 +0000541
Simon Glass0bbd76f2013-02-24 20:30:22 +0000542 if (multi_hash()) {
543 struct hash_algo *algo;
Breno Lima45e65122018-01-17 10:03:45 -0200544 u8 *output;
Simon Glassa3b240b2014-06-12 07:24:41 -0600545 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
Simon Glass68f15562013-02-24 17:33:31 +0000546 void *buf;
Simon Glassb8790452012-12-05 14:46:36 +0000547
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100548 if (hash_lookup_algo(algo_name, &algo)) {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000549 printf("Unknown hash algorithm '%s'\n", algo_name);
550 return CMD_RET_USAGE;
551 }
552 argc -= 2;
553
554 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
555 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
556 return 1;
557 }
Simon Glassb8790452012-12-05 14:46:36 +0000558
Breno Lima45e65122018-01-17 10:03:45 -0200559 output = memalign(ARCH_DMA_MINALIGN,
560 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300561 if (!output)
562 return CMD_RET_FAILURE;
Breno Lima45e65122018-01-17 10:03:45 -0200563
Simon Glass68f15562013-02-24 17:33:31 +0000564 buf = map_sysmem(addr, len);
565 algo->hash_func_ws(buf, len, output, algo->chunk_size);
566 unmap_sysmem(buf);
Simon Glassb8790452012-12-05 14:46:36 +0000567
Simon Glass0bbd76f2013-02-24 20:30:22 +0000568 /* Try to avoid code bloat when verify is not needed */
Daniel Thompsona9e2c672017-05-19 17:26:58 +0100569#if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
Igor Opaniuk489a5782024-03-02 16:05:48 +0100570 defined(CONFIG_MD5SUM_VERIFY) || defined(CONFIG_HASH_VERIFY)
Simon Glass0bbd76f2013-02-24 20:30:22 +0000571 if (flags & HASH_FLAG_VERIFY) {
Simon Glassb8790452012-12-05 14:46:36 +0000572#else
Simon Glass0bbd76f2013-02-24 20:30:22 +0000573 if (0) {
Simon Glassb8790452012-12-05 14:46:36 +0000574#endif
Simon Glass0bbd76f2013-02-24 20:30:22 +0000575 if (parse_verify_sum(algo, *argv, vsum,
Simon Glass80e345a2013-02-24 17:33:26 +0000576 flags & HASH_FLAG_ENV)) {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000577 printf("ERROR: %s does not contain a valid "
578 "%s sum\n", *argv, algo->name);
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300579 free(output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000580 return 1;
581 }
582 if (memcmp(output, vsum, algo->digest_size) != 0) {
583 int i;
Simon Glassb8790452012-12-05 14:46:36 +0000584
Simon Glass7f3fa542014-06-02 22:04:49 -0600585 hash_show(algo, addr, len, output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000586 printf(" != ");
587 for (i = 0; i < algo->digest_size; i++)
588 printf("%02x", vsum[i]);
589 puts(" ** ERROR **\n");
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300590 free(output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000591 return 1;
592 }
593 } else {
Simon Glass7f3fa542014-06-02 22:04:49 -0600594 hash_show(algo, addr, len, output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000595 printf("\n");
596
597 if (argc) {
598 store_result(algo, output, *argv,
599 flags & HASH_FLAG_ENV);
600 }
Simon Glassb8790452012-12-05 14:46:36 +0000601 }
Simon Glass0bbd76f2013-02-24 20:30:22 +0000602
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300603 free(output);
604
Simon Glass0bbd76f2013-02-24 20:30:22 +0000605 /* Horrible code size hack for boards that just want crc32 */
Simon Glassb8790452012-12-05 14:46:36 +0000606 } else {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000607 ulong crc;
608 ulong *ptr;
609
610 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
611
612 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
613 addr, addr + len - 1, crc);
Simon Glassb8790452012-12-05 14:46:36 +0000614
Tom Rini62780f02013-11-07 07:39:48 -0500615 if (argc >= 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600616 ptr = (ulong *)hextoul(argv[0], NULL);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000617 *ptr = crc;
Simon Glass80e345a2013-02-24 17:33:26 +0000618 }
Simon Glassb8790452012-12-05 14:46:36 +0000619 }
620
621 return 0;
622}
Simon Glassb22ec7a2017-05-17 09:05:34 -0600623#endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
624#endif /* !USE_HOSTCC */