blob: 159179e7f214b6d3c3ab435a006973322cbba311 [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
T Karthik Reddya098b432019-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 Glass866178e2021-09-25 19:43:19 -060045static int __maybe_unused hash_init_sha1(struct hash_algo *algo, void **ctxp)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010046{
47 sha1_context *ctx = malloc(sizeof(sha1_context));
48 sha1_starts(ctx);
49 *ctxp = ctx;
50 return 0;
51}
52
Simon Glass866178e2021-09-25 19:43:19 -060053static int __maybe_unused hash_update_sha1(struct hash_algo *algo, void *ctx,
54 const void *buf, unsigned int size,
55 int is_last)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010056{
57 sha1_update((sha1_context *)ctx, buf, size);
58 return 0;
59}
60
Simon Glass866178e2021-09-25 19:43:19 -060061static int __maybe_unused hash_finish_sha1(struct hash_algo *algo, void *ctx,
62 void *dest_buf, int size)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010063{
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}
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010071
Simon Glass866178e2021-09-25 19:43:19 -060072static int __maybe_unused hash_init_sha256(struct hash_algo *algo, void **ctxp)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010073{
74 sha256_context *ctx = malloc(sizeof(sha256_context));
75 sha256_starts(ctx);
76 *ctxp = ctx;
77 return 0;
78}
79
Simon Glass866178e2021-09-25 19:43:19 -060080static int __maybe_unused hash_update_sha256(struct hash_algo *algo, void *ctx,
81 const void *buf, uint size,
82 int is_last)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010083{
84 sha256_update((sha256_context *)ctx, buf, size);
85 return 0;
86}
87
Simon Glass866178e2021-09-25 19:43:19 -060088static int __maybe_unused hash_finish_sha256(struct hash_algo *algo, void *ctx,
89 void *dest_buf, int size)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010090{
91 if (size < algo->digest_size)
92 return -1;
93
94 sha256_finish((sha256_context *)ctx, dest_buf);
95 free(ctx);
96 return 0;
97}
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +010098
Simon Glass866178e2021-09-25 19:43:19 -060099static int __maybe_unused hash_init_sha384(struct hash_algo *algo, void **ctxp)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200100{
101 sha512_context *ctx = malloc(sizeof(sha512_context));
102 sha384_starts(ctx);
103 *ctxp = ctx;
104 return 0;
105}
106
Simon Glass866178e2021-09-25 19:43:19 -0600107static int __maybe_unused hash_update_sha384(struct hash_algo *algo, void *ctx,
108 const void *buf, uint size,
109 int is_last)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200110{
111 sha384_update((sha512_context *)ctx, buf, size);
112 return 0;
113}
114
Simon Glass866178e2021-09-25 19:43:19 -0600115static int __maybe_unused hash_finish_sha384(struct hash_algo *algo, void *ctx,
116 void *dest_buf, int size)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200117{
118 if (size < algo->digest_size)
119 return -1;
120
121 sha384_finish((sha512_context *)ctx, dest_buf);
122 free(ctx);
123 return 0;
124}
Reuben Dowle1908fd92020-04-16 17:36:52 +1200125
Simon Glass866178e2021-09-25 19:43:19 -0600126static int __maybe_unused hash_init_sha512(struct hash_algo *algo, void **ctxp)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200127{
128 sha512_context *ctx = malloc(sizeof(sha512_context));
129 sha512_starts(ctx);
130 *ctxp = ctx;
131 return 0;
132}
133
Simon Glass866178e2021-09-25 19:43:19 -0600134static int __maybe_unused hash_update_sha512(struct hash_algo *algo, void *ctx,
135 const void *buf, uint size,
136 int is_last)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200137{
138 sha512_update((sha512_context *)ctx, buf, size);
139 return 0;
140}
141
Simon Glass866178e2021-09-25 19:43:19 -0600142static int __maybe_unused hash_finish_sha512(struct hash_algo *algo, void *ctx,
143 void *dest_buf, int size)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200144{
145 if (size < algo->digest_size)
146 return -1;
147
Reuben Dowle1908fd92020-04-16 17:36:52 +1200148 sha512_finish((sha512_context *)ctx, dest_buf);
149 free(ctx);
150 return 0;
151}
Reuben Dowle1908fd92020-04-16 17:36:52 +1200152
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100153static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
154{
155 uint16_t *ctx = malloc(sizeof(uint16_t));
156 *ctx = 0;
157 *ctxp = ctx;
158 return 0;
159}
160
161static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
162 const void *buf, unsigned int size,
163 int is_last)
164{
165 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
166 return 0;
167}
168
169static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
170 void *dest_buf, int size)
171{
172 if (size < algo->digest_size)
173 return -1;
174
175 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
176 free(ctx);
177 return 0;
178}
179
Simon Glass577226c2021-09-25 19:43:24 -0600180static int __maybe_unused hash_init_crc32(struct hash_algo *algo, void **ctxp)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100181{
182 uint32_t *ctx = malloc(sizeof(uint32_t));
183 *ctx = 0;
184 *ctxp = ctx;
185 return 0;
186}
187
Simon Glass577226c2021-09-25 19:43:24 -0600188static int __maybe_unused hash_update_crc32(struct hash_algo *algo, void *ctx,
189 const void *buf, unsigned int size,
190 int is_last)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100191{
192 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
193 return 0;
194}
195
Simon Glass577226c2021-09-25 19:43:24 -0600196static int __maybe_unused hash_finish_crc32(struct hash_algo *algo, void *ctx,
197 void *dest_buf, int size)
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100198{
199 if (size < algo->digest_size)
200 return -1;
201
202 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
203 free(ctx);
204 return 0;
205}
206
Simon Glassb8790452012-12-05 14:46:36 +0000207/*
Tom Rini03361c72017-08-14 16:38:07 -0400208 * These are the hash algorithms we support. If we have hardware acceleration
209 * is enable we will use that, otherwise a software version of the algorithm.
210 * Note that algorithm names must be in lower case.
Simon Glassb8790452012-12-05 14:46:36 +0000211 */
212static struct hash_algo hash_algo[] = {
Simon Glass383dd572021-09-25 19:43:18 -0600213#if CONFIG_IS_ENABLED(MD5)
Alexandru Gagniucf3016582021-09-02 19:54:20 -0500214 {
215 .name = "md5",
216 .digest_size = MD5_SUM_LEN,
217 .chunk_size = CHUNKSZ_MD5,
218 .hash_func_ws = md5_wd,
219 },
220#endif
Simon Glass383dd572021-09-25 19:43:18 -0600221#if CONFIG_IS_ENABLED(SHA1)
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +0000222 {
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200223 .name = "sha1",
Tom Rini03361c72017-08-14 16:38:07 -0400224 .digest_size = SHA1_SUM_LEN,
225 .chunk_size = CHUNKSZ_SHA1,
Simon Glass383dd572021-09-25 19:43:18 -0600226#if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400227 .hash_func_ws = hw_sha1,
228#else
229 .hash_func_ws = sha1_csum_wd,
gaurav ranaef201592015-02-20 12:51:46 +0530230#endif
Simon Glass383dd572021-09-25 19:43:18 -0600231#if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400232 .hash_init = hw_sha_init,
233 .hash_update = hw_sha_update,
234 .hash_finish = hw_sha_finish,
235#else
236 .hash_init = hash_init_sha1,
237 .hash_update = hash_update_sha1,
238 .hash_finish = hash_finish_sha1,
Akshay Saraswatcbd8f2c2013-03-20 21:00:58 +0000239#endif
Simon Glassb8790452012-12-05 14:46:36 +0000240 },
241#endif
Simon Glass383dd572021-09-25 19:43:18 -0600242#if CONFIG_IS_ENABLED(SHA256)
Simon Glassb8790452012-12-05 14:46:36 +0000243 {
Tom Rini03361c72017-08-14 16:38:07 -0400244 .name = "sha256",
245 .digest_size = SHA256_SUM_LEN,
246 .chunk_size = CHUNKSZ_SHA256,
Simon Glass383dd572021-09-25 19:43:18 -0600247#if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400248 .hash_func_ws = hw_sha256,
249#else
250 .hash_func_ws = sha256_csum_wd,
251#endif
Simon Glass383dd572021-09-25 19:43:18 -0600252#if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Tom Rini03361c72017-08-14 16:38:07 -0400253 .hash_init = hw_sha_init,
254 .hash_update = hw_sha_update,
255 .hash_finish = hw_sha_finish,
256#else
257 .hash_init = hash_init_sha256,
258 .hash_update = hash_update_sha256,
259 .hash_finish = hash_finish_sha256,
260#endif
Simon Glassb8790452012-12-05 14:46:36 +0000261 },
262#endif
Simon Glass383dd572021-09-25 19:43:18 -0600263#if CONFIG_IS_ENABLED(SHA384)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200264 {
265 .name = "sha384",
266 .digest_size = SHA384_SUM_LEN,
267 .chunk_size = CHUNKSZ_SHA384,
Simon Glass383dd572021-09-25 19:43:18 -0600268#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030269 .hash_func_ws = hw_sha384,
270#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200271 .hash_func_ws = sha384_csum_wd,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030272#endif
Simon Glass383dd572021-09-25 19:43:18 -0600273#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030274 .hash_init = hw_sha_init,
275 .hash_update = hw_sha_update,
276 .hash_finish = hw_sha_finish,
277#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200278 .hash_init = hash_init_sha384,
279 .hash_update = hash_update_sha384,
280 .hash_finish = hash_finish_sha384,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030281#endif
Reuben Dowle1908fd92020-04-16 17:36:52 +1200282 },
283#endif
Simon Glass383dd572021-09-25 19:43:18 -0600284#if CONFIG_IS_ENABLED(SHA512)
Reuben Dowle1908fd92020-04-16 17:36:52 +1200285 {
286 .name = "sha512",
287 .digest_size = SHA512_SUM_LEN,
288 .chunk_size = CHUNKSZ_SHA512,
Simon Glass383dd572021-09-25 19:43:18 -0600289#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030290 .hash_func_ws = hw_sha512,
291#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200292 .hash_func_ws = sha512_csum_wd,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030293#endif
Simon Glass383dd572021-09-25 19:43:18 -0600294#if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
Joel Stanley92efc1f2021-02-17 13:50:42 +1030295 .hash_init = hw_sha_init,
296 .hash_update = hw_sha_update,
297 .hash_finish = hw_sha_finish,
298#else
Reuben Dowle1908fd92020-04-16 17:36:52 +1200299 .hash_init = hash_init_sha512,
300 .hash_update = hash_update_sha512,
301 .hash_finish = hash_finish_sha512,
Joel Stanley92efc1f2021-02-17 13:50:42 +1030302#endif
Reuben Dowle1908fd92020-04-16 17:36:52 +1200303 },
304#endif
Simon Glass0bbd76f2013-02-24 20:30:22 +0000305 {
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100306 .name = "crc16-ccitt",
307 .digest_size = 2,
308 .chunk_size = CHUNKSZ,
309 .hash_func_ws = crc16_ccitt_wd_buf,
310 .hash_init = hash_init_crc16_ccitt,
311 .hash_update = hash_update_crc16_ccitt,
312 .hash_finish = hash_finish_crc16_ccitt,
313 },
Simon Glass577226c2021-09-25 19:43:24 -0600314#if CONFIG_IS_ENABLED(CRC32)
Philipp Tomsich6e2ac3e2018-11-25 19:22:19 +0100315 {
Tom Rini03361c72017-08-14 16:38:07 -0400316 .name = "crc32",
317 .digest_size = 4,
318 .chunk_size = CHUNKSZ_CRC32,
319 .hash_func_ws = crc32_wd_buf,
320 .hash_init = hash_init_crc32,
321 .hash_update = hash_update_crc32,
322 .hash_finish = hash_finish_crc32,
Simon Glass0bbd76f2013-02-24 20:30:22 +0000323 },
Simon Glass577226c2021-09-25 19:43:24 -0600324#endif
Simon Glassb8790452012-12-05 14:46:36 +0000325};
326
Simon Glass0bbd76f2013-02-24 20:30:22 +0000327/* Try to minimize code size for boards that don't want much hashing */
Simon Glass905ea882023-02-05 15:36:42 -0700328#if CONFIG_IS_ENABLED(SHA256) || IS_ENABLED(CONFIG_CMD_SHA1SUM) || \
Simon Glass778451e2023-02-05 15:36:32 -0700329 CONFIG_IS_ENABLED(CRC32_VERIFY) || IS_ENABLED(CONFIG_CMD_HASH) || \
Simon Glass383dd572021-09-25 19:43:18 -0600330 CONFIG_IS_ENABLED(SHA384) || CONFIG_IS_ENABLED(SHA512)
Simon Glass0bbd76f2013-02-24 20:30:22 +0000331#define multi_hash() 1
332#else
333#define multi_hash() 0
334#endif
335
T Karthik Reddya098b432019-03-16 15:23:01 +0530336static void reloc_update(void)
337{
338#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
339 int i;
340 static bool done;
341
342 if (!done) {
343 done = true;
344 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
345 hash_algo[i].name += gd->reloc_off;
346 hash_algo[i].hash_func_ws += gd->reloc_off;
347 hash_algo[i].hash_init += gd->reloc_off;
348 hash_algo[i].hash_update += gd->reloc_off;
349 hash_algo[i].hash_finish += gd->reloc_off;
350 }
351 }
352#endif
353}
354
Ruchika Guptac915ceb2015-01-23 16:01:58 +0530355int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
356{
357 int i;
358
T Karthik Reddya098b432019-03-16 15:23:01 +0530359 reloc_update();
360
Ruchika Guptac915ceb2015-01-23 16:01:58 +0530361 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
362 if (!strcmp(algo_name, hash_algo[i].name)) {
363 *algop = &hash_algo[i];
364 return 0;
365 }
366 }
367
368 debug("Unknown hash algorithm '%s'\n", algo_name);
369 return -EPROTONOSUPPORT;
370}
371
372int hash_progressive_lookup_algo(const char *algo_name,
373 struct hash_algo **algop)
374{
375 int i;
376
T Karthik Reddya098b432019-03-16 15:23:01 +0530377 reloc_update();
378
Ruchika Guptac915ceb2015-01-23 16:01:58 +0530379 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
380 if (!strcmp(algo_name, hash_algo[i].name)) {
381 if (hash_algo[i].hash_init) {
382 *algop = &hash_algo[i];
383 return 0;
384 }
385 }
386 }
387
388 debug("Unknown hash algorithm '%s'\n", algo_name);
389 return -EPROTONOSUPPORT;
390}
391
392#ifndef USE_HOSTCC
Stefan Roese0ed2e462015-05-18 14:08:24 +0200393int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
394{
395 struct hash_algo *algo;
396 int ret;
397 int i;
398
399 ret = hash_lookup_algo(algo_name, &algo);
400 if (ret)
401 return ret;
402
403 for (i = 0; i < algo->digest_size; i++) {
404 char chr[3];
405
Simon Glassb827f392021-07-24 09:03:28 -0600406 strlcpy(chr, &str[i * 2], 3);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600407 result[i] = hextoul(chr, NULL);
Stefan Roese0ed2e462015-05-18 14:08:24 +0200408 }
409
410 return 0;
411}
412
Tom Rini61738382016-01-05 08:47:48 -0500413int hash_block(const char *algo_name, const void *data, unsigned int len,
414 uint8_t *output, int *output_size)
415{
416 struct hash_algo *algo;
417 int ret;
418
419 ret = hash_lookup_algo(algo_name, &algo);
420 if (ret)
421 return ret;
422
423 if (output_size && *output_size < algo->digest_size) {
424 debug("Output buffer size %d too small (need %d bytes)",
425 *output_size, algo->digest_size);
426 return -ENOSPC;
427 }
428 if (output_size)
429 *output_size = algo->digest_size;
430 algo->hash_func_ws(data, len, output, algo->chunk_size);
431
432 return 0;
433}
434
Simon Glass383dd572021-09-25 19:43:18 -0600435#if !defined(CONFIG_SPL_BUILD) && (defined(CONFIG_CMD_HASH) || \
436 defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32))
Simon Glassb8790452012-12-05 14:46:36 +0000437/**
438 * store_result: Store the resulting sum to an address or variable
439 *
440 * @algo: Hash algorithm being used
441 * @sum: Hash digest (algo->digest_size bytes)
442 * @dest: Destination, interpreted as a hex address if it starts
Simon Glass80e345a2013-02-24 17:33:26 +0000443 * with * (or allow_env_vars is 0) or otherwise as an
444 * environment variable.
445 * @allow_env_vars: non-zero to permit storing the result to an
446 * variable environment
Simon Glassb8790452012-12-05 14:46:36 +0000447 */
Simon Glassa3b240b2014-06-12 07:24:41 -0600448static void store_result(struct hash_algo *algo, const uint8_t *sum,
Simon Glass80e345a2013-02-24 17:33:26 +0000449 const char *dest, int allow_env_vars)
Simon Glassb8790452012-12-05 14:46:36 +0000450{
451 unsigned int i;
Simon Glass80e345a2013-02-24 17:33:26 +0000452 int env_var = 0;
Simon Glassb8790452012-12-05 14:46:36 +0000453
Simon Glass80e345a2013-02-24 17:33:26 +0000454 /*
455 * If environment variables are allowed, then we assume that 'dest'
456 * is an environment variable, unless it starts with *, in which
457 * case we assume it is an address. If not allowed, it is always an
458 * address. This is to support the crc32 command.
459 */
460 if (allow_env_vars) {
461 if (*dest == '*')
462 dest++;
463 else
464 env_var = 1;
465 }
Simon Glassb8790452012-12-05 14:46:36 +0000466
Simon Glass80e345a2013-02-24 17:33:26 +0000467 if (env_var) {
Simon Glassb8790452012-12-05 14:46:36 +0000468 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
469 char *str_ptr = str_output;
470
471 for (i = 0; i < algo->digest_size; i++) {
472 sprintf(str_ptr, "%02x", sum[i]);
473 str_ptr += 2;
474 }
Jeroen Hofsteeb14c0522014-06-09 11:02:02 +0200475 *str_ptr = '\0';
Simon Glass6a38e412017-08-03 12:22:09 -0600476 env_set(dest, str_output);
Simon Glass80e345a2013-02-24 17:33:26 +0000477 } else {
Simon Glass68f15562013-02-24 17:33:31 +0000478 ulong addr;
479 void *buf;
Simon Glass80e345a2013-02-24 17:33:26 +0000480
Simon Glass3ff49ec2021-07-24 09:03:29 -0600481 addr = hextoul(dest, NULL);
Simon Glass68f15562013-02-24 17:33:31 +0000482 buf = map_sysmem(addr, algo->digest_size);
483 memcpy(buf, sum, algo->digest_size);
484 unmap_sysmem(buf);
Simon Glassb8790452012-12-05 14:46:36 +0000485 }
486}
487
488/**
489 * parse_verify_sum: Parse a hash verification parameter
490 *
491 * @algo: Hash algorithm being used
492 * @verify_str: Argument to parse. If it starts with * then it is
493 * interpreted as a hex address containing the hash.
494 * If the length is exactly the right number of hex digits
495 * for the digest size, then we assume it is a hex digest.
496 * Otherwise we assume it is an environment variable, and
497 * look up its value (it must contain a hex digest).
498 * @vsum: Returns binary digest value (algo->digest_size bytes)
Simon Glass80e345a2013-02-24 17:33:26 +0000499 * @allow_env_vars: non-zero to permit storing the result to an environment
500 * variable. If 0 then verify_str is assumed to be an
501 * address, and the * prefix is not expected.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100502 * Return: 0 if ok, non-zero on error
Simon Glassb8790452012-12-05 14:46:36 +0000503 */
Simon Glassa3b240b2014-06-12 07:24:41 -0600504static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
505 uint8_t *vsum, int allow_env_vars)
Simon Glassb8790452012-12-05 14:46:36 +0000506{
Simon Glass80e345a2013-02-24 17:33:26 +0000507 int env_var = 0;
508
509 /* See comment above in store_result() */
510 if (allow_env_vars) {
511 if (*verify_str == '*')
512 verify_str++;
513 else
514 env_var = 1;
515 }
516
Nikolay Dimitrov810b84d2014-12-12 20:01:23 +0200517 if (!env_var) {
Simon Glass68f15562013-02-24 17:33:31 +0000518 ulong addr;
519 void *buf;
Simon Glassb8790452012-12-05 14:46:36 +0000520
Simon Glass3ff49ec2021-07-24 09:03:29 -0600521 addr = hextoul(verify_str, NULL);
Simon Glass68f15562013-02-24 17:33:31 +0000522 buf = map_sysmem(addr, algo->digest_size);
523 memcpy(vsum, buf, algo->digest_size);
Simon Glassb8790452012-12-05 14:46:36 +0000524 } else {
Simon Glassb8790452012-12-05 14:46:36 +0000525 char *vsum_str;
526 int digits = algo->digest_size * 2;
527
528 /*
529 * As with the original code from sha1sum.c, we assume that a
530 * string which matches the digest size exactly is a hex
531 * string and not an environment variable.
532 */
533 if (strlen(verify_str) == digits)
534 vsum_str = verify_str;
535 else {
Simon Glass64b723f2017-08-03 12:22:12 -0600536 vsum_str = env_get(verify_str);
Simon Glassb8790452012-12-05 14:46:36 +0000537 if (vsum_str == NULL || strlen(vsum_str) != digits) {
538 printf("Expected %d hex digits in env var\n",
539 digits);
540 return 1;
541 }
542 }
543
Stefan Roese0ed2e462015-05-18 14:08:24 +0200544 hash_parse_string(algo->name, vsum_str, vsum);
Simon Glassb8790452012-12-05 14:46:36 +0000545 }
546 return 0;
547}
548
Tom Rini61738382016-01-05 08:47:48 -0500549static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
Simon Glassb8790452012-12-05 14:46:36 +0000550{
551 int i;
552
553 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
554 for (i = 0; i < algo->digest_size; i++)
555 printf("%02x", output[i]);
556}
557
Simon Glassed38aef2020-05-10 11:40:03 -0600558int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
559 int flag, int argc, char *const argv[])
Simon Glassb8790452012-12-05 14:46:36 +0000560{
Simon Glassb8790452012-12-05 14:46:36 +0000561 ulong addr, len;
Simon Glassb8790452012-12-05 14:46:36 +0000562
Nikolay Dimitrov810b84d2014-12-12 20:01:23 +0200563 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
Simon Glassb8790452012-12-05 14:46:36 +0000564 return CMD_RET_USAGE;
565
Simon Glass3ff49ec2021-07-24 09:03:29 -0600566 addr = hextoul(*argv++, NULL);
567 len = hextoul(*argv++, NULL);
Simon Glass80e345a2013-02-24 17:33:26 +0000568
Simon Glass0bbd76f2013-02-24 20:30:22 +0000569 if (multi_hash()) {
570 struct hash_algo *algo;
Breno Lima45e65122018-01-17 10:03:45 -0200571 u8 *output;
Simon Glassa3b240b2014-06-12 07:24:41 -0600572 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
Simon Glass68f15562013-02-24 17:33:31 +0000573 void *buf;
Simon Glassb8790452012-12-05 14:46:36 +0000574
Hung-ying Tyanf061b1a2014-03-03 12:19:28 +0100575 if (hash_lookup_algo(algo_name, &algo)) {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000576 printf("Unknown hash algorithm '%s'\n", algo_name);
577 return CMD_RET_USAGE;
578 }
579 argc -= 2;
580
581 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
582 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
583 return 1;
584 }
Simon Glassb8790452012-12-05 14:46:36 +0000585
Breno Lima45e65122018-01-17 10:03:45 -0200586 output = memalign(ARCH_DMA_MINALIGN,
587 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300588 if (!output)
589 return CMD_RET_FAILURE;
Breno Lima45e65122018-01-17 10:03:45 -0200590
Simon Glass68f15562013-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 Glassb8790452012-12-05 14:46:36 +0000594
Simon Glass0bbd76f2013-02-24 20:30:22 +0000595 /* Try to avoid code bloat when verify is not needed */
Daniel Thompsona9e2c672017-05-19 17:26:58 +0100596#if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
597 defined(CONFIG_HASH_VERIFY)
Simon Glass0bbd76f2013-02-24 20:30:22 +0000598 if (flags & HASH_FLAG_VERIFY) {
Simon Glassb8790452012-12-05 14:46:36 +0000599#else
Simon Glass0bbd76f2013-02-24 20:30:22 +0000600 if (0) {
Simon Glassb8790452012-12-05 14:46:36 +0000601#endif
Simon Glass0bbd76f2013-02-24 20:30:22 +0000602 if (parse_verify_sum(algo, *argv, vsum,
Simon Glass80e345a2013-02-24 17:33:26 +0000603 flags & HASH_FLAG_ENV)) {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000604 printf("ERROR: %s does not contain a valid "
605 "%s sum\n", *argv, algo->name);
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300606 free(output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000607 return 1;
608 }
609 if (memcmp(output, vsum, algo->digest_size) != 0) {
610 int i;
Simon Glassb8790452012-12-05 14:46:36 +0000611
Simon Glass7f3fa542014-06-02 22:04:49 -0600612 hash_show(algo, addr, len, output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000613 printf(" != ");
614 for (i = 0; i < algo->digest_size; i++)
615 printf("%02x", vsum[i]);
616 puts(" ** ERROR **\n");
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300617 free(output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000618 return 1;
619 }
620 } else {
Simon Glass7f3fa542014-06-02 22:04:49 -0600621 hash_show(algo, addr, len, output);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000622 printf("\n");
623
624 if (argc) {
625 store_result(algo, output, *argv,
626 flags & HASH_FLAG_ENV);
627 }
Simon Glassb8790452012-12-05 14:46:36 +0000628 }
Simon Glass0bbd76f2013-02-24 20:30:22 +0000629
Sergei Antonov1ddd8a62023-06-12 22:59:10 +0300630 free(output);
631
Simon Glass0bbd76f2013-02-24 20:30:22 +0000632 /* Horrible code size hack for boards that just want crc32 */
Simon Glassb8790452012-12-05 14:46:36 +0000633 } else {
Simon Glass0bbd76f2013-02-24 20:30:22 +0000634 ulong crc;
635 ulong *ptr;
636
637 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
638
639 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
640 addr, addr + len - 1, crc);
Simon Glassb8790452012-12-05 14:46:36 +0000641
Tom Rini62780f02013-11-07 07:39:48 -0500642 if (argc >= 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600643 ptr = (ulong *)hextoul(argv[0], NULL);
Simon Glass0bbd76f2013-02-24 20:30:22 +0000644 *ptr = crc;
Simon Glass80e345a2013-02-24 17:33:26 +0000645 }
Simon Glassb8790452012-12-05 14:46:36 +0000646 }
647
648 return 0;
649}
Simon Glassb22ec7a2017-05-17 09:05:34 -0600650#endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
651#endif /* !USE_HOSTCC */