1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012 The Chromium OS Authors.
6 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
20 #include <asm/cache.h>
21 #include <asm/global_data.h>
23 #include <linux/errno.h>
24 #include <u-boot/crc.h>
27 #include <linux/compiler_attributes.h>
29 #include <linux/kconfig.h>
30 #endif /* !USE_HOSTCC*/
34 #include <u-boot/crc.h>
35 #include <u-boot/sha1.h>
36 #include <u-boot/sha256.h>
37 #include <u-boot/sha512.h>
38 #include <u-boot/md5.h>
40 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
41 DECLARE_GLOBAL_DATA_PTR;
44 static void reloc_update(void);
46 static int __maybe_unused hash_init_sha1(struct hash_algo *algo, void **ctxp)
48 sha1_context *ctx = malloc(sizeof(sha1_context));
54 static int __maybe_unused hash_update_sha1(struct hash_algo *algo, void *ctx,
55 const void *buf, unsigned int size,
58 sha1_update((sha1_context *)ctx, buf, size);
62 static int __maybe_unused hash_finish_sha1(struct hash_algo *algo, void *ctx,
63 void *dest_buf, int size)
65 if (size < algo->digest_size)
68 sha1_finish((sha1_context *)ctx, dest_buf);
73 static int __maybe_unused hash_init_sha256(struct hash_algo *algo, void **ctxp)
75 sha256_context *ctx = malloc(sizeof(sha256_context));
81 static int __maybe_unused hash_update_sha256(struct hash_algo *algo, void *ctx,
82 const void *buf, uint size,
85 sha256_update((sha256_context *)ctx, buf, size);
89 static int __maybe_unused hash_finish_sha256(struct hash_algo *algo, void *ctx,
90 void *dest_buf, int size)
92 if (size < algo->digest_size)
95 sha256_finish((sha256_context *)ctx, dest_buf);
100 static int __maybe_unused hash_init_sha384(struct hash_algo *algo, void **ctxp)
102 sha512_context *ctx = malloc(sizeof(sha512_context));
108 static int __maybe_unused hash_update_sha384(struct hash_algo *algo, void *ctx,
109 const void *buf, uint size,
112 sha384_update((sha512_context *)ctx, buf, size);
116 static int __maybe_unused hash_finish_sha384(struct hash_algo *algo, void *ctx,
117 void *dest_buf, int size)
119 if (size < algo->digest_size)
122 sha384_finish((sha512_context *)ctx, dest_buf);
127 static int __maybe_unused hash_init_sha512(struct hash_algo *algo, void **ctxp)
129 sha512_context *ctx = malloc(sizeof(sha512_context));
135 static int __maybe_unused hash_update_sha512(struct hash_algo *algo, void *ctx,
136 const void *buf, uint size,
139 sha512_update((sha512_context *)ctx, buf, size);
143 static int __maybe_unused hash_finish_sha512(struct hash_algo *algo, void *ctx,
144 void *dest_buf, int size)
146 if (size < algo->digest_size)
149 sha512_finish((sha512_context *)ctx, dest_buf);
154 static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
156 uint16_t *ctx = malloc(sizeof(uint16_t));
162 static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
163 const void *buf, unsigned int size,
166 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
170 static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
171 void *dest_buf, int size)
173 if (size < algo->digest_size)
176 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
181 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
183 uint32_t *ctx = malloc(sizeof(uint32_t));
189 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
190 const void *buf, unsigned int size, int is_last)
192 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
196 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
199 if (size < algo->digest_size)
202 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
208 * 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.
212 static struct hash_algo hash_algo[] = {
213 #if CONFIG_IS_ENABLED(MD5)
216 .digest_size = MD5_SUM_LEN,
217 .chunk_size = CHUNKSZ_MD5,
218 .hash_func_ws = md5_wd,
221 #if CONFIG_IS_ENABLED(SHA1)
224 .digest_size = SHA1_SUM_LEN,
225 .chunk_size = CHUNKSZ_SHA1,
226 #if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
227 .hash_func_ws = hw_sha1,
229 .hash_func_ws = sha1_csum_wd,
231 #if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
232 .hash_init = hw_sha_init,
233 .hash_update = hw_sha_update,
234 .hash_finish = hw_sha_finish,
236 .hash_init = hash_init_sha1,
237 .hash_update = hash_update_sha1,
238 .hash_finish = hash_finish_sha1,
242 #if CONFIG_IS_ENABLED(SHA256)
245 .digest_size = SHA256_SUM_LEN,
246 .chunk_size = CHUNKSZ_SHA256,
247 #if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
248 .hash_func_ws = hw_sha256,
250 .hash_func_ws = sha256_csum_wd,
252 #if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
253 .hash_init = hw_sha_init,
254 .hash_update = hw_sha_update,
255 .hash_finish = hw_sha_finish,
257 .hash_init = hash_init_sha256,
258 .hash_update = hash_update_sha256,
259 .hash_finish = hash_finish_sha256,
263 #if CONFIG_IS_ENABLED(SHA384)
266 .digest_size = SHA384_SUM_LEN,
267 .chunk_size = CHUNKSZ_SHA384,
268 #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
269 .hash_func_ws = hw_sha384,
271 .hash_func_ws = sha384_csum_wd,
273 #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
274 .hash_init = hw_sha_init,
275 .hash_update = hw_sha_update,
276 .hash_finish = hw_sha_finish,
278 .hash_init = hash_init_sha384,
279 .hash_update = hash_update_sha384,
280 .hash_finish = hash_finish_sha384,
284 #if CONFIG_IS_ENABLED(SHA512)
287 .digest_size = SHA512_SUM_LEN,
288 .chunk_size = CHUNKSZ_SHA512,
289 #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
290 .hash_func_ws = hw_sha512,
292 .hash_func_ws = sha512_csum_wd,
294 #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
295 .hash_init = hw_sha_init,
296 .hash_update = hw_sha_update,
297 .hash_finish = hw_sha_finish,
299 .hash_init = hash_init_sha512,
300 .hash_update = hash_update_sha512,
301 .hash_finish = hash_finish_sha512,
306 .name = "crc16-ccitt",
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,
317 .chunk_size = CHUNKSZ_CRC32,
318 .hash_func_ws = crc32_wd_buf,
319 .hash_init = hash_init_crc32,
320 .hash_update = hash_update_crc32,
321 .hash_finish = hash_finish_crc32,
325 /* Try to minimize code size for boards that don't want much hashing */
326 #if CONFIG_IS_ENABLED(SHA256) || CONFIG_IS_ENABLED(CMD_SHA1SUM) || \
327 CONFIG_IS_ENABLED(CRC32_VERIFY) || CONFIG_IS_ENABLED(CMD_HASH) || \
328 CONFIG_IS_ENABLED(SHA384) || CONFIG_IS_ENABLED(SHA512)
329 #define multi_hash() 1
331 #define multi_hash() 0
334 static void reloc_update(void)
336 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
342 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
343 hash_algo[i].name += gd->reloc_off;
344 hash_algo[i].hash_func_ws += gd->reloc_off;
345 hash_algo[i].hash_init += gd->reloc_off;
346 hash_algo[i].hash_update += gd->reloc_off;
347 hash_algo[i].hash_finish += gd->reloc_off;
353 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
359 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
360 if (!strcmp(algo_name, hash_algo[i].name)) {
361 *algop = &hash_algo[i];
366 debug("Unknown hash algorithm '%s'\n", algo_name);
367 return -EPROTONOSUPPORT;
370 int hash_progressive_lookup_algo(const char *algo_name,
371 struct hash_algo **algop)
377 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
378 if (!strcmp(algo_name, hash_algo[i].name)) {
379 if (hash_algo[i].hash_init) {
380 *algop = &hash_algo[i];
386 debug("Unknown hash algorithm '%s'\n", algo_name);
387 return -EPROTONOSUPPORT;
391 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
393 struct hash_algo *algo;
397 ret = hash_lookup_algo(algo_name, &algo);
401 for (i = 0; i < algo->digest_size; i++) {
404 strlcpy(chr, &str[i * 2], 3);
405 result[i] = hextoul(chr, NULL);
411 int hash_block(const char *algo_name, const void *data, unsigned int len,
412 uint8_t *output, int *output_size)
414 struct hash_algo *algo;
417 ret = hash_lookup_algo(algo_name, &algo);
421 if (output_size && *output_size < algo->digest_size) {
422 debug("Output buffer size %d too small (need %d bytes)",
423 *output_size, algo->digest_size);
427 *output_size = algo->digest_size;
428 algo->hash_func_ws(data, len, output, algo->chunk_size);
433 #if !defined(CONFIG_SPL_BUILD) && (defined(CONFIG_CMD_HASH) || \
434 defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32))
436 * store_result: Store the resulting sum to an address or variable
438 * @algo: Hash algorithm being used
439 * @sum: Hash digest (algo->digest_size bytes)
440 * @dest: Destination, interpreted as a hex address if it starts
441 * with * (or allow_env_vars is 0) or otherwise as an
442 * environment variable.
443 * @allow_env_vars: non-zero to permit storing the result to an
444 * variable environment
446 static void store_result(struct hash_algo *algo, const uint8_t *sum,
447 const char *dest, int allow_env_vars)
453 * If environment variables are allowed, then we assume that 'dest'
454 * is an environment variable, unless it starts with *, in which
455 * case we assume it is an address. If not allowed, it is always an
456 * address. This is to support the crc32 command.
458 if (allow_env_vars) {
466 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
467 char *str_ptr = str_output;
469 for (i = 0; i < algo->digest_size; i++) {
470 sprintf(str_ptr, "%02x", sum[i]);
474 env_set(dest, str_output);
479 addr = hextoul(dest, NULL);
480 buf = map_sysmem(addr, algo->digest_size);
481 memcpy(buf, sum, algo->digest_size);
487 * parse_verify_sum: Parse a hash verification parameter
489 * @algo: Hash algorithm being used
490 * @verify_str: Argument to parse. If it starts with * then it is
491 * interpreted as a hex address containing the hash.
492 * If the length is exactly the right number of hex digits
493 * for the digest size, then we assume it is a hex digest.
494 * Otherwise we assume it is an environment variable, and
495 * look up its value (it must contain a hex digest).
496 * @vsum: Returns binary digest value (algo->digest_size bytes)
497 * @allow_env_vars: non-zero to permit storing the result to an environment
498 * variable. If 0 then verify_str is assumed to be an
499 * address, and the * prefix is not expected.
500 * @return 0 if ok, non-zero on error
502 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
503 uint8_t *vsum, int allow_env_vars)
507 /* See comment above in store_result() */
508 if (allow_env_vars) {
509 if (*verify_str == '*')
519 addr = hextoul(verify_str, NULL);
520 buf = map_sysmem(addr, algo->digest_size);
521 memcpy(vsum, buf, algo->digest_size);
524 int digits = algo->digest_size * 2;
527 * As with the original code from sha1sum.c, we assume that a
528 * string which matches the digest size exactly is a hex
529 * string and not an environment variable.
531 if (strlen(verify_str) == digits)
532 vsum_str = verify_str;
534 vsum_str = env_get(verify_str);
535 if (vsum_str == NULL || strlen(vsum_str) != digits) {
536 printf("Expected %d hex digits in env var\n",
542 hash_parse_string(algo->name, vsum_str, vsum);
547 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
551 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
552 for (i = 0; i < algo->digest_size; i++)
553 printf("%02x", output[i]);
556 int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
557 int flag, int argc, char *const argv[])
561 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
562 return CMD_RET_USAGE;
564 addr = hextoul(*argv++, NULL);
565 len = hextoul(*argv++, NULL);
568 struct hash_algo *algo;
570 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
573 if (hash_lookup_algo(algo_name, &algo)) {
574 printf("Unknown hash algorithm '%s'\n", algo_name);
575 return CMD_RET_USAGE;
579 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
580 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
584 output = memalign(ARCH_DMA_MINALIGN,
585 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
587 buf = map_sysmem(addr, len);
588 algo->hash_func_ws(buf, len, output, algo->chunk_size);
591 /* Try to avoid code bloat when verify is not needed */
592 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
593 defined(CONFIG_HASH_VERIFY)
594 if (flags & HASH_FLAG_VERIFY) {
598 if (parse_verify_sum(algo, *argv, vsum,
599 flags & HASH_FLAG_ENV)) {
600 printf("ERROR: %s does not contain a valid "
601 "%s sum\n", *argv, algo->name);
604 if (memcmp(output, vsum, algo->digest_size) != 0) {
607 hash_show(algo, addr, len, output);
609 for (i = 0; i < algo->digest_size; i++)
610 printf("%02x", vsum[i]);
611 puts(" ** ERROR **\n");
615 hash_show(algo, addr, len, output);
619 store_result(algo, output, *argv,
620 flags & HASH_FLAG_ENV);
622 unmap_sysmem(output);
626 /* Horrible code size hack for boards that just want crc32 */
631 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
633 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
634 addr, addr + len - 1, crc);
637 ptr = (ulong *)hextoul(argv[0], NULL);
644 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
645 #endif /* !USE_HOSTCC */