blob: 8dd9da8576833d23b1e42945997a98a3468f0bd6 [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 <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060014#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010016#include <malloc.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050017#include <mapmem.h>
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +000018#include <hw_sha.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053021#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090022#include <linux/errno.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053023#else
24#include "mkimage.h"
Simon Glass866178e2021-09-25 19:43:19 -060025#include <linux/compiler_attributes.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053026#include <time.h>
Simon Glass383dd572021-09-25 19:43:18 -060027#include <linux/kconfig.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053028#endif /* !USE_HOSTCC*/
29
Simon Glassb8790452012-12-05 14:46:36 +000030#include <hash.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060031#include <image.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053032#include <u-boot/crc.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020033#include <u-boot/sha1.h>
34#include <u-boot/sha256.h>
Reuben Dowle1908fd92020-04-16 17:36:52 +120035#include <u-boot/sha512.h>
Ruchika Guptac915ceb2015-01-23 16:01:58 +053036#include <u-boot/md5.h>
Simon Glassb8790452012-12-05 14:46:36 +000037
Simon Glass866178e2021-09-25 19:43:19 -060038static int __maybe_unused hash_init_sha1(struct hash_algo *algo, void **ctxp)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010039{
40 sha1_context *ctx = malloc(sizeof(sha1_context));
41 sha1_starts(ctx);
42 *ctxp = ctx;
43 return 0;
44}
45
Simon Glass866178e2021-09-25 19:43:19 -060046static int __maybe_unused hash_update_sha1(struct hash_algo *algo, void *ctx,
47 const void *buf, unsigned int size,
48 int is_last)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010049{
50 sha1_update((sha1_context *)ctx, buf, size);
51 return 0;
52}
53
Simon Glass866178e2021-09-25 19:43:19 -060054static int __maybe_unused hash_finish_sha1(struct hash_algo *algo, void *ctx,
55 void *dest_buf, int size)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010056{
57 if (size < algo->digest_size)
58 return -1;
59
60 sha1_finish((sha1_context *)ctx, dest_buf);
61 free(ctx);
62 return 0;
63}
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010064
Simon Glass866178e2021-09-25 19:43:19 -060065static int __maybe_unused hash_init_sha256(struct hash_algo *algo, void **ctxp)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010066{
67 sha256_context *ctx = malloc(sizeof(sha256_context));
68 sha256_starts(ctx);
69 *ctxp = ctx;
70 return 0;
71}
72
Simon Glass866178e2021-09-25 19:43:19 -060073static int __maybe_unused hash_update_sha256(struct hash_algo *algo, void *ctx,
74 const void *buf, uint size,
75 int is_last)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010076{
77 sha256_update((sha256_context *)ctx, buf, size);
78 return 0;
79}
80
Simon Glass866178e2021-09-25 19:43:19 -060081static int __maybe_unused hash_finish_sha256(struct hash_algo *algo, void *ctx,
82 void *dest_buf, int size)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010083{
84 if (size < algo->digest_size)
85 return -1;
86
87 sha256_finish((sha256_context *)ctx, dest_buf);
88 free(ctx);
89 return 0;
90}
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010091
Simon Glass866178e2021-09-25 19:43:19 -060092static int __maybe_unused hash_init_sha384(struct hash_algo *algo, void **ctxp)
Reuben Dowle1908fd92020-04-16 17:36:52 +120093{
94 sha512_context *ctx = malloc(sizeof(sha512_context));
95 sha384_starts(ctx);
96 *ctxp = ctx;
97 return 0;
98}
99
Simon Glass866178e2021-09-25 19:43:19 -0600100static int __maybe_unused hash_update_sha384(struct hash_algo *algo, void *ctx,
101 const void *buf, uint size,
102 int is_last)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200103{
104 sha384_update((sha512_context *)ctx, buf, size);
105 return 0;
106}
107
Simon Glass866178e2021-09-25 19:43:19 -0600108static int __maybe_unused hash_finish_sha384(struct hash_algo *algo, void *ctx,
109 void *dest_buf, int size)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200110{
111 if (size < algo->digest_size)
112 return -1;
113
114 sha384_finish((sha512_context *)ctx, dest_buf);
115 free(ctx);
116 return 0;
117}
Reuben Dowle1908fd92020-04-16 17:36:52 +1200118
Simon Glass866178e2021-09-25 19:43:19 -0600119static int __maybe_unused hash_init_sha512(struct hash_algo *algo, void **ctxp)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200120{
121 sha512_context *ctx = malloc(sizeof(sha512_context));
122 sha512_starts(ctx);
123 *ctxp = ctx;
124 return 0;
125}
126
Simon Glass866178e2021-09-25 19:43:19 -0600127static int __maybe_unused hash_update_sha512(struct hash_algo *algo, void *ctx,
128 const void *buf, uint size,
129 int is_last)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200130{
131 sha512_update((sha512_context *)ctx, buf, size);
132 return 0;
133}
134
Simon Glass866178e2021-09-25 19:43:19 -0600135static int __maybe_unused hash_finish_sha512(struct hash_algo *algo, void *ctx,
136 void *dest_buf, int size)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200137{
138 if (size < algo->digest_size)
139 return -1;
140
Reuben Dowle1908fd92020-04-16 17:36:52 +1200141 sha512_finish((sha512_context *)ctx, dest_buf);
142 free(ctx);
143 return 0;
144}
Reuben Dowle1908fd92020-04-16 17:36:52 +1200145
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100146static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
147{
148 uint16_t *ctx = malloc(sizeof(uint16_t));
149 *ctx = 0;
150 *ctxp = ctx;
151 return 0;
152}
153
154static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
155 const void *buf, unsigned int size,
156 int is_last)
157{
158 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
159 return 0;
160}
161
162static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
163 void *dest_buf, int size)
164{
165 if (size < algo->digest_size)
166 return -1;
167
168 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
169 free(ctx);
170 return 0;
171}
172
Simon Glass577226c2021-09-25 19:43:24 -0600173static int __maybe_unused hash_init_crc32(struct hash_algo *algo, void **ctxp)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100174{
175 uint32_t *ctx = malloc(sizeof(uint32_t));
176 *ctx = 0;
177 *ctxp = ctx;
178 return 0;
179}
180
Simon Glass577226c2021-09-25 19:43:24 -0600181static int __maybe_unused hash_update_crc32(struct hash_algo *algo, void *ctx,
182 const void *buf, unsigned int size,
183 int is_last)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100184{
185 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
186 return 0;
187}
188
Simon Glass577226c2021-09-25 19:43:24 -0600189static int __maybe_unused hash_finish_crc32(struct hash_algo *algo, void *ctx,
190 void *dest_buf, int size)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100191{
192 if (size < algo->digest_size)
193 return -1;
194
195 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
196 free(ctx);
197 return 0;
198}
199
Simon Glassb8790452012-12-05 14:46:36 +0000200/*
Tom Rini03361c72017-08-14 16:38:07 -0400201 * These are the hash algorithms we support. If we have hardware acceleration
202 * is enable we will use that, otherwise a software version of the algorithm.
203 * Note that algorithm names must be in lower case.
Simon Glassb8790452012-12-05 14:46:36 +0000204 */
205static struct hash_algo hash_algo[] = {
Simon Glass383dd572021-09-25 19:43:18 -0600206#if CONFIG_IS_ENABLED(MD5)
Alexandru Gagniucf3016582021-09-02 19:54:20 -0500207 {
208 .name = "md5",
209 .digest_size = MD5_SUM_LEN,
210 .chunk_size = CHUNKSZ_MD5,
211 .hash_func_ws = md5_wd,
212 },
213#endif
Simon Glass383dd572021-09-25 19:43:18 -0600214#if CONFIG_IS_ENABLED(SHA1)
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +0000215 {
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200216 .name = "sha1",
Tom Rini03361c72017-08-14 16:38:07 -0400217 .digest_size = SHA1_SUM_LEN,
218 .chunk_size = CHUNKSZ_SHA1,
Simon Glass383dd572021-09-25 19:43:18 -0600219#if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400220 .hash_func_ws = hw_sha1,
221#else
222 .hash_func_ws = sha1_csum_wd,
gaurav ranaef201592015-02-20 12:51:46 +0530223#endif
Simon Glass383dd572021-09-25 19:43:18 -0600224#if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400225 .hash_init = hw_sha_init,
226 .hash_update = hw_sha_update,
227 .hash_finish = hw_sha_finish,
228#else
229 .hash_init = hash_init_sha1,
230 .hash_update = hash_update_sha1,
231 .hash_finish = hash_finish_sha1,
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +0000232#endif
Simon Glassb8790452012-12-05 14:46:36 +0000233 },
234#endif
Simon Glass383dd572021-09-25 19:43:18 -0600235#if CONFIG_IS_ENABLED(SHA256)
Simon Glassb8790452012-12-05 14:46:36 +0000236 {
Tom Rini03361c72017-08-14 16:38:07 -0400237 .name = "sha256",
238 .digest_size = SHA256_SUM_LEN,
239 .chunk_size = CHUNKSZ_SHA256,
Simon Glass383dd572021-09-25 19:43:18 -0600240#if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400241 .hash_func_ws = hw_sha256,
242#else
243 .hash_func_ws = sha256_csum_wd,
244#endif
Simon Glass383dd572021-09-25 19:43:18 -0600245#if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400246 .hash_init = hw_sha_init,
247 .hash_update = hw_sha_update,
248 .hash_finish = hw_sha_finish,
249#else
250 .hash_init = hash_init_sha256,
251 .hash_update = hash_update_sha256,
252 .hash_finish = hash_finish_sha256,
253#endif
Simon Glassb8790452012-12-05 14:46:36 +0000254 },
255#endif
Simon Glass383dd572021-09-25 19:43:18 -0600256#if CONFIG_IS_ENABLED(SHA384)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200257 {
258 .name = "sha384",
259 .digest_size = SHA384_SUM_LEN,
260 .chunk_size = CHUNKSZ_SHA384,
Simon Glass383dd572021-09-25 19:43:18 -0600261#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030262 .hash_func_ws = hw_sha384,
263#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200264 .hash_func_ws = sha384_csum_wd,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030265#endif
Simon Glass383dd572021-09-25 19:43:18 -0600266#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030267 .hash_init = hw_sha_init,
268 .hash_update = hw_sha_update,
269 .hash_finish = hw_sha_finish,
270#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200271 .hash_init = hash_init_sha384,
272 .hash_update = hash_update_sha384,
273 .hash_finish = hash_finish_sha384,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030274#endif
Reuben Dowle1908fd92020-04-16 17:36:52 +1200275 },
276#endif
Simon Glass383dd572021-09-25 19:43:18 -0600277#if CONFIG_IS_ENABLED(SHA512)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200278 {
279 .name = "sha512",
280 .digest_size = SHA512_SUM_LEN,
281 .chunk_size = CHUNKSZ_SHA512,
Simon Glass383dd572021-09-25 19:43:18 -0600282#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030283 .hash_func_ws = hw_sha512,
284#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200285 .hash_func_ws = sha512_csum_wd,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030286#endif
Simon Glass383dd572021-09-25 19:43:18 -0600287#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030288 .hash_init = hw_sha_init,
289 .hash_update = hw_sha_update,
290 .hash_finish = hw_sha_finish,
291#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200292 .hash_init = hash_init_sha512,
293 .hash_update = hash_update_sha512,
294 .hash_finish = hash_finish_sha512,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030295#endif
Reuben Dowle1908fd92020-04-16 17:36:52 +1200296 },
297#endif
Simon Glass0bbd76f2013-02-24 20:30:22 +0000298 {
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100299 .name = "crc16-ccitt",
300 .digest_size = 2,
301 .chunk_size = CHUNKSZ,
302 .hash_func_ws = crc16_ccitt_wd_buf,
303 .hash_init = hash_init_crc16_ccitt,
304 .hash_update = hash_update_crc16_ccitt,
305 .hash_finish = hash_finish_crc16_ccitt,
306 },
Simon Glassdc020a22024-12-19 11:29:07 -0700307#if CONFIG_IS_ENABLED(CRC8) && IS_ENABLED(CONFIG_HASH_CRC8)
308 {
309 .name = "crc8",
310 .digest_size = 1,
311 .chunk_size = CHUNKSZ_CRC32,
312 .hash_func_ws = crc8_wd_buf,
313 },
314#endif
Simon Glass577226c2021-09-25 19:43:24 -0600315#if CONFIG_IS_ENABLED(CRC32)
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100316 {
Tom Rini03361c72017-08-14 16:38:07 -0400317 .name = "crc32",
318 .digest_size = 4,
319 .chunk_size = CHUNKSZ_CRC32,
320 .hash_func_ws = crc32_wd_buf,
321 .hash_init = hash_init_crc32,
322 .hash_update = hash_update_crc32,
323 .hash_finish = hash_finish_crc32,
Simon Glass0bbd76f2013-02-24 20:30:22 +0000324 },
Simon Glass577226c2021-09-25 19:43:24 -0600325#endif
Simon Glassb8790452012-12-05 14:46:36 +0000326};
327
Simon Glass0bbd76f2013-02-24 20:30:22 +0000328/* Try to minimize code size for boards that don't want much hashing */
Simon Glass905ea882023-02-05 15:36:42 -0700329#if CONFIG_IS_ENABLED(SHA256) || IS_ENABLED(CONFIG_CMD_SHA1SUM) || \
Simon Glass778451e2023-02-05 15:36:32 -0700330 CONFIG_IS_ENABLED(CRC32_VERIFY) || IS_ENABLED(CONFIG_CMD_HASH) || \
Igor Opaniuk489a5782024-03-02 16:05:48 +0100331 CONFIG_IS_ENABLED(SHA384) || CONFIG_IS_ENABLED(SHA512) || \
332 IS_ENABLED(CONFIG_CMD_MD5SUM)
Simon Glass0bbd76f2013-02-24 20:30:22 +0000333#define multi_hash() 1
334#else
335#define multi_hash() 0
336#endif
337
Ruchika Guptac915ceb2015-01-23 16:01:58 +0530338int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
339{
340 int i;
341
342 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
343 if (!strcmp(algo_name, hash_algo[i].name)) {
344 *algop = &hash_algo[i];
345 return 0;
346 }
347 }
348
349 debug("Unknown hash algorithm '%s'\n", algo_name);
350 return -EPROTONOSUPPORT;
351}
352
353int hash_progressive_lookup_algo(const char *algo_name,
354 struct hash_algo **algop)
355{
356 int i;
357
358 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
359 if (!strcmp(algo_name, hash_algo[i].name)) {
360 if (hash_algo[i].hash_init) {
361 *algop = &hash_algo[i];
362 return 0;
363 }
364 }
365 }
366
367 debug("Unknown hash algorithm '%s'\n", algo_name);
368 return -EPROTONOSUPPORT;
369}
370
371#ifndef USE_HOSTCC
Stefan Roese0ed2e462015-05-18 14:08:24 +0200372int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
373{
374 struct hash_algo *algo;
375 int ret;
376 int i;
377
378 ret = hash_lookup_algo(algo_name, &algo);
379 if (ret)
380 return ret;
381
382 for (i = 0; i < algo->digest_size; i++) {
383 char chr[3];
384
Simon Glassb827f392021-07-24 09:03:28 -0600385 strlcpy(chr, &str[i * 2], 3);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600386 result[i] = hextoul(chr, NULL);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200387 }
388
389 return 0;
390}
391
Tom Rini61738382016-01-05 08:47:48 -0500392int hash_block(const char *algo_name, const void *data, unsigned int len,
393 uint8_t *output, int *output_size)
394{
395 struct hash_algo *algo;
396 int ret;
397
398 ret = hash_lookup_algo(algo_name, &algo);
399 if (ret)
400 return ret;
401
402 if (output_size && *output_size < algo->digest_size) {
403 debug("Output buffer size %d too small (need %d bytes)",
404 *output_size, algo->digest_size);
405 return -ENOSPC;
406 }
407 if (output_size)
408 *output_size = algo->digest_size;
409 algo->hash_func_ws(data, len, output, algo->chunk_size);
410
411 return 0;
412}
413
Simon Glass0e84d962024-09-29 19:49:50 -0600414#if !defined(CONFIG_XPL_BUILD) && (defined(CONFIG_CMD_HASH) || \
Igor Opaniuk489a5782024-03-02 16:05:48 +0100415 defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)) || \
416 defined(CONFIG_CMD_MD5SUM)
Simon Glassb8790452012-12-05 14:46:36 +0000417/**
418 * store_result: Store the resulting sum to an address or variable
419 *
420 * @algo: Hash algorithm being used
421 * @sum: Hash digest (algo->digest_size bytes)
422 * @dest: Destination, interpreted as a hex address if it starts
Simon Glass80e345a2013-02-24 17:33:26 +0000423 * with * (or allow_env_vars is 0) or otherwise as an
424 * environment variable.
425 * @allow_env_vars: non-zero to permit storing the result to an
426 * variable environment
Simon Glassb8790452012-12-05 14:46:36 +0000427 */
Simon Glassa3b240b2014-06-12 07:24:41 -0600428static void store_result(struct hash_algo *algo, const uint8_t *sum,
Simon Glass80e345a2013-02-24 17:33:26 +0000429 const char *dest, int allow_env_vars)
Simon Glassb8790452012-12-05 14:46:36 +0000430{
431 unsigned int i;
Simon Glass80e345a2013-02-24 17:33:26 +0000432 int env_var = 0;
Simon Glassb8790452012-12-05 14:46:36 +0000433
Simon Glass80e345a2013-02-24 17:33:26 +0000434 /*
435 * If environment variables are allowed, then we assume that 'dest'
436 * is an environment variable, unless it starts with *, in which
437 * case we assume it is an address. If not allowed, it is always an
438 * address. This is to support the crc32 command.
439 */
440 if (allow_env_vars) {
441 if (*dest == '*')
442 dest++;
443 else
444 env_var = 1;
445 }
Simon Glassb8790452012-12-05 14:46:36 +0000446
Simon Glass80e345a2013-02-24 17:33:26 +0000447 if (env_var) {
Simon Glassb8790452012-12-05 14:46:36 +0000448 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
449 char *str_ptr = str_output;
450
451 for (i = 0; i < algo->digest_size; i++) {
452 sprintf(str_ptr, "%02x", sum[i]);
453 str_ptr += 2;
454 }
Jeroen Hofsteeb14c0522014-06-09 11:02:02 +0200455 *str_ptr = '\0';
Simon Glass6a38e412017-08-03 12:22:09 -0600456 env_set(dest, str_output);
Simon Glass80e345a2013-02-24 17:33:26 +0000457 } else {
Simon Glass68f15562013-02-24 17:33:31 +0000458 ulong addr;
459 void *buf;
Simon Glass80e345a2013-02-24 17:33:26 +0000460
Simon Glass3ff49ec2021-07-24 09:03:29 -0600461 addr = hextoul(dest, NULL);
Simon Glass68f15562013-02-24 17:33:31 +0000462 buf = map_sysmem(addr, algo->digest_size);
463 memcpy(buf, sum, algo->digest_size);
464 unmap_sysmem(buf);
Simon Glassb8790452012-12-05 14:46:36 +0000465 }
466}
467
468/**
469 * parse_verify_sum: Parse a hash verification parameter
470 *
471 * @algo: Hash algorithm being used
472 * @verify_str: Argument to parse. If it starts with * then it is
473 * interpreted as a hex address containing the hash.
474 * If the length is exactly the right number of hex digits
475 * for the digest size, then we assume it is a hex digest.
476 * Otherwise we assume it is an environment variable, and
477 * look up its value (it must contain a hex digest).
478 * @vsum: Returns binary digest value (algo->digest_size bytes)
Simon Glass80e345a2013-02-24 17:33:26 +0000479 * @allow_env_vars: non-zero to permit storing the result to an environment
480 * variable. If 0 then verify_str is assumed to be an
481 * address, and the * prefix is not expected.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100482 * Return: 0 if ok, non-zero on error
Simon Glassb8790452012-12-05 14:46:36 +0000483 */
Simon Glassa3b240b2014-06-12 07:24:41 -0600484static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
485 uint8_t *vsum, int allow_env_vars)
Simon Glassb8790452012-12-05 14:46:36 +0000486{
Simon Glass80e345a2013-02-24 17:33:26 +0000487 int env_var = 0;
488
489 /* See comment above in store_result() */
490 if (allow_env_vars) {
491 if (*verify_str == '*')
492 verify_str++;
493 else
494 env_var = 1;
495 }
496
Nikolay Dimitrov810b84d2014-12-12 20:01:23 +0200497 if (!env_var) {
Simon Glass68f15562013-02-24 17:33:31 +0000498 ulong addr;
499 void *buf;
Simon Glassb8790452012-12-05 14:46:36 +0000500
Simon Glass3ff49ec2021-07-24 09:03:29 -0600501 addr = hextoul(verify_str, NULL);
Simon Glass68f15562013-02-24 17:33:31 +0000502 buf = map_sysmem(addr, algo->digest_size);
503 memcpy(vsum, buf, algo->digest_size);
Simon Glassb8790452012-12-05 14:46:36 +0000504 } else {
Simon Glassb8790452012-12-05 14:46:36 +0000505 char *vsum_str;
506 int digits = algo->digest_size * 2;
507
508 /*
509 * As with the original code from sha1sum.c, we assume that a
510 * string which matches the digest size exactly is a hex
511 * string and not an environment variable.
512 */
513 if (strlen(verify_str) == digits)
514 vsum_str = verify_str;
515 else {
Simon Glass64b723f2017-08-03 12:22:12 -0600516 vsum_str = env_get(verify_str);
Simon Glassb8790452012-12-05 14:46:36 +0000517 if (vsum_str == NULL || strlen(vsum_str) != digits) {
518 printf("Expected %d hex digits in env var\n",
519 digits);
520 return 1;
521 }
522 }
523
Stefan Roese0ed2e462015-05-18 14:08:24 +0200524 hash_parse_string(algo->name, vsum_str, vsum);
Simon Glassb8790452012-12-05 14:46:36 +0000525 }
526 return 0;
527}
528
Tom Rini61738382016-01-05 08:47:48 -0500529static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
Simon Glassb8790452012-12-05 14:46:36 +0000530{
531 int i;
532
533 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
534 for (i = 0; i < algo->digest_size; i++)
535 printf("%02x", output[i]);
536}
537
Simon Glassed38aef2020-05-10 11:40:03 -0600538int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
539 int flag, int argc, char *const argv[])
Simon Glassb8790452012-12-05 14:46:36 +0000540{
Simon Glassb8790452012-12-05 14:46:36 +0000541 ulong addr, len;
Simon Glassb8790452012-12-05 14:46:36 +0000542
Nikolay Dimitrov810b84d2014-12-12 20:01:23 +0200543 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
Simon Glassb8790452012-12-05 14:46:36 +0000544 return CMD_RET_USAGE;
545
Simon Glass3ff49ec2021-07-24 09:03:29 -0600546 addr = hextoul(*argv++, NULL);
547 len = hextoul(*argv++, NULL);
Simon Glass80e345a2013-02-24 17:33:26 +0000548
Simon Glass0bbd76f2013-02-24 20:30:22 +0000549 if (multi_hash()) {
550 struct hash_algo *algo;
Breno Lima45e65122018-01-17 10:03:45 -0200551 u8 *output;
Simon Glassa3b240b2014-06-12 07:24:41 -0600552 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
Simon Glass68f15562013-02-24 17:33:31 +0000553 void *buf;
Simon Glassb8790452012-12-05 14:46:36 +0000554
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100555 if (hash_lookup_algo(algo_name, &algo)) {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000556 printf("Unknown hash algorithm '%s'\n", algo_name);
557 return CMD_RET_USAGE;
558 }
559 argc -= 2;
560
561 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
562 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
563 return 1;
564 }
Simon Glassb8790452012-12-05 14:46:36 +0000565
Breno Lima45e65122018-01-17 10:03:45 -0200566 output = memalign(ARCH_DMA_MINALIGN,
567 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300568 if (!output)
569 return CMD_RET_FAILURE;
Breno Lima45e65122018-01-17 10:03:45 -0200570
Simon Glass68f15562013-02-24 17:33:31 +0000571 buf = map_sysmem(addr, len);
572 algo->hash_func_ws(buf, len, output, algo->chunk_size);
573 unmap_sysmem(buf);
Simon Glassb8790452012-12-05 14:46:36 +0000574
Simon Glass0bbd76f2013-02-24 20:30:22 +0000575 /* Try to avoid code bloat when verify is not needed */
Daniel Thompsona9e2c672017-05-19 17:26:58 +0100576#if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
Igor Opaniuk489a5782024-03-02 16:05:48 +0100577 defined(CONFIG_MD5SUM_VERIFY) || defined(CONFIG_HASH_VERIFY)
Simon Glass0bbd76f2013-02-24 20:30:22 +0000578 if (flags & HASH_FLAG_VERIFY) {
Simon Glassb8790452012-12-05 14:46:36 +0000579#else
Simon Glass0bbd76f2013-02-24 20:30:22 +0000580 if (0) {
Simon Glassb8790452012-12-05 14:46:36 +0000581#endif
Simon Glass0bbd76f2013-02-24 20:30:22 +0000582 if (parse_verify_sum(algo, *argv, vsum,
Simon Glass80e345a2013-02-24 17:33:26 +0000583 flags & HASH_FLAG_ENV)) {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000584 printf("ERROR: %s does not contain a valid "
585 "%s sum\n", *argv, algo->name);
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300586 free(output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000587 return 1;
588 }
589 if (memcmp(output, vsum, algo->digest_size) != 0) {
590 int i;
Simon Glassb8790452012-12-05 14:46:36 +0000591
Simon Glass7f3fa542014-06-02 22:04:49 -0600592 hash_show(algo, addr, len, output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000593 printf(" != ");
594 for (i = 0; i < algo->digest_size; i++)
595 printf("%02x", vsum[i]);
596 puts(" ** ERROR **\n");
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300597 free(output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000598 return 1;
599 }
600 } else {
Simon Glass7f3fa542014-06-02 22:04:49 -0600601 hash_show(algo, addr, len, output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000602 printf("\n");
603
604 if (argc) {
605 store_result(algo, output, *argv,
606 flags & HASH_FLAG_ENV);
607 }
Simon Glassb8790452012-12-05 14:46:36 +0000608 }
Simon Glass0bbd76f2013-02-24 20:30:22 +0000609
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300610 free(output);
611
Simon Glass0bbd76f2013-02-24 20:30:22 +0000612 /* Horrible code size hack for boards that just want crc32 */
Simon Glassb8790452012-12-05 14:46:36 +0000613 } else {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000614 ulong crc;
615 ulong *ptr;
616
617 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
618
619 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
620 addr, addr + len - 1, crc);
Simon Glassb8790452012-12-05 14:46:36 +0000621
Tom Rini62780f02013-11-07 07:39:48 -0500622 if (argc >= 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600623 ptr = (ulong *)hextoul(argv[0], NULL);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000624 *ptr = crc;
Simon Glass80e345a2013-02-24 17:33:26 +0000625 }
Simon Glassb8790452012-12-05 14:46:36 +0000626 }
627
628 return 0;
629}
Simon Glassb22ec7a2017-05-17 09:05:34 -0600630#endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
631#endif /* !USE_HOSTCC */