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>
22 #include <linux/errno.h>
23 #include <u-boot/crc.h>
27 #endif /* !USE_HOSTCC*/
31 #include <u-boot/crc.h>
32 #include <u-boot/sha1.h>
33 #include <u-boot/sha256.h>
34 #include <u-boot/sha512.h>
35 #include <u-boot/md5.h>
37 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
38 DECLARE_GLOBAL_DATA_PTR;
41 static void reloc_update(void);
43 #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
44 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
46 sha1_context *ctx = malloc(sizeof(sha1_context));
52 static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
53 unsigned int size, int is_last)
55 sha1_update((sha1_context *)ctx, buf, size);
59 static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
62 if (size < algo->digest_size)
65 sha1_finish((sha1_context *)ctx, dest_buf);
71 #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
72 static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
74 sha256_context *ctx = malloc(sizeof(sha256_context));
80 static int hash_update_sha256(struct hash_algo *algo, void *ctx,
81 const void *buf, unsigned int size, int is_last)
83 sha256_update((sha256_context *)ctx, buf, size);
87 static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
90 if (size < algo->digest_size)
93 sha256_finish((sha256_context *)ctx, dest_buf);
99 #if defined(CONFIG_SHA384)
100 static int hash_init_sha384(struct hash_algo *algo, void **ctxp)
102 sha512_context *ctx = malloc(sizeof(sha512_context));
108 static int hash_update_sha384(struct hash_algo *algo, void *ctx,
109 const void *buf, unsigned int size, int is_last)
111 sha384_update((sha512_context *)ctx, buf, size);
115 static int hash_finish_sha384(struct hash_algo *algo, void *ctx, void
118 if (size < algo->digest_size)
121 sha384_finish((sha512_context *)ctx, dest_buf);
127 #if defined(CONFIG_SHA512)
128 static int hash_init_sha512(struct hash_algo *algo, void **ctxp)
130 sha512_context *ctx = malloc(sizeof(sha512_context));
136 static int hash_update_sha512(struct hash_algo *algo, void *ctx,
137 const void *buf, unsigned int size, int is_last)
139 sha512_update((sha512_context *)ctx, buf, size);
143 static int hash_finish_sha512(struct hash_algo *algo, void *ctx, void
146 if (size < algo->digest_size)
149 printf("hello world\n");
151 sha512_finish((sha512_context *)ctx, dest_buf);
158 static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
160 uint16_t *ctx = malloc(sizeof(uint16_t));
166 static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
167 const void *buf, unsigned int size,
170 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
174 static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
175 void *dest_buf, int size)
177 if (size < algo->digest_size)
180 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
185 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
187 uint32_t *ctx = malloc(sizeof(uint32_t));
193 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
194 const void *buf, unsigned int size, int is_last)
196 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
200 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
203 if (size < algo->digest_size)
206 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
212 * These are the hash algorithms we support. If we have hardware acceleration
213 * is enable we will use that, otherwise a software version of the algorithm.
214 * Note that algorithm names must be in lower case.
216 static struct hash_algo hash_algo[] = {
220 .digest_size = SHA1_SUM_LEN,
221 .chunk_size = CHUNKSZ_SHA1,
222 #ifdef CONFIG_SHA_HW_ACCEL
223 .hash_func_ws = hw_sha1,
225 .hash_func_ws = sha1_csum_wd,
227 #ifdef CONFIG_SHA_PROG_HW_ACCEL
228 .hash_init = hw_sha_init,
229 .hash_update = hw_sha_update,
230 .hash_finish = hw_sha_finish,
232 .hash_init = hash_init_sha1,
233 .hash_update = hash_update_sha1,
234 .hash_finish = hash_finish_sha1,
241 .digest_size = SHA256_SUM_LEN,
242 .chunk_size = CHUNKSZ_SHA256,
243 #ifdef CONFIG_SHA_HW_ACCEL
244 .hash_func_ws = hw_sha256,
246 .hash_func_ws = sha256_csum_wd,
248 #ifdef CONFIG_SHA_PROG_HW_ACCEL
249 .hash_init = hw_sha_init,
250 .hash_update = hw_sha_update,
251 .hash_finish = hw_sha_finish,
253 .hash_init = hash_init_sha256,
254 .hash_update = hash_update_sha256,
255 .hash_finish = hash_finish_sha256,
262 .digest_size = SHA384_SUM_LEN,
263 .chunk_size = CHUNKSZ_SHA384,
264 .hash_func_ws = sha384_csum_wd,
265 .hash_init = hash_init_sha384,
266 .hash_update = hash_update_sha384,
267 .hash_finish = hash_finish_sha384,
273 .digest_size = SHA512_SUM_LEN,
274 .chunk_size = CHUNKSZ_SHA512,
275 .hash_func_ws = sha512_csum_wd,
276 .hash_init = hash_init_sha512,
277 .hash_update = hash_update_sha512,
278 .hash_finish = hash_finish_sha512,
282 .name = "crc16-ccitt",
284 .chunk_size = CHUNKSZ,
285 .hash_func_ws = crc16_ccitt_wd_buf,
286 .hash_init = hash_init_crc16_ccitt,
287 .hash_update = hash_update_crc16_ccitt,
288 .hash_finish = hash_finish_crc16_ccitt,
293 .chunk_size = CHUNKSZ_CRC32,
294 .hash_func_ws = crc32_wd_buf,
295 .hash_init = hash_init_crc32,
296 .hash_update = hash_update_crc32,
297 .hash_finish = hash_finish_crc32,
301 /* Try to minimize code size for boards that don't want much hashing */
302 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
303 defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) || \
304 defined(CONFIG_SHA384) || defined(CONFIG_SHA512)
305 #define multi_hash() 1
307 #define multi_hash() 0
310 static void reloc_update(void)
312 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
318 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
319 hash_algo[i].name += gd->reloc_off;
320 hash_algo[i].hash_func_ws += gd->reloc_off;
321 hash_algo[i].hash_init += gd->reloc_off;
322 hash_algo[i].hash_update += gd->reloc_off;
323 hash_algo[i].hash_finish += gd->reloc_off;
329 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
335 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
336 if (!strcmp(algo_name, hash_algo[i].name)) {
337 *algop = &hash_algo[i];
342 debug("Unknown hash algorithm '%s'\n", algo_name);
343 return -EPROTONOSUPPORT;
346 int hash_progressive_lookup_algo(const char *algo_name,
347 struct hash_algo **algop)
353 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
354 if (!strcmp(algo_name, hash_algo[i].name)) {
355 if (hash_algo[i].hash_init) {
356 *algop = &hash_algo[i];
362 debug("Unknown hash algorithm '%s'\n", algo_name);
363 return -EPROTONOSUPPORT;
367 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
369 struct hash_algo *algo;
373 ret = hash_lookup_algo(algo_name, &algo);
377 for (i = 0; i < algo->digest_size; i++) {
380 strncpy(chr, &str[i * 2], 2);
381 result[i] = simple_strtoul(chr, NULL, 16);
387 int hash_block(const char *algo_name, const void *data, unsigned int len,
388 uint8_t *output, int *output_size)
390 struct hash_algo *algo;
393 ret = hash_lookup_algo(algo_name, &algo);
397 if (output_size && *output_size < algo->digest_size) {
398 debug("Output buffer size %d too small (need %d bytes)",
399 *output_size, algo->digest_size);
403 *output_size = algo->digest_size;
404 algo->hash_func_ws(data, len, output, algo->chunk_size);
409 #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
411 * store_result: Store the resulting sum to an address or variable
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
416 * 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
421 static void store_result(struct hash_algo *algo, const uint8_t *sum,
422 const char *dest, int allow_env_vars)
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.
433 if (allow_env_vars) {
441 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
442 char *str_ptr = str_output;
444 for (i = 0; i < algo->digest_size; i++) {
445 sprintf(str_ptr, "%02x", sum[i]);
449 env_set(dest, str_output);
454 addr = simple_strtoul(dest, NULL, 16);
455 buf = map_sysmem(addr, algo->digest_size);
456 memcpy(buf, sum, algo->digest_size);
462 * parse_verify_sum: Parse a hash verification parameter
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)
472 * @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.
475 * @return 0 if ok, non-zero on error
477 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
478 uint8_t *vsum, int allow_env_vars)
482 /* See comment above in store_result() */
483 if (allow_env_vars) {
484 if (*verify_str == '*')
494 addr = simple_strtoul(verify_str, NULL, 16);
495 buf = map_sysmem(addr, algo->digest_size);
496 memcpy(vsum, buf, algo->digest_size);
499 int digits = algo->digest_size * 2;
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.
506 if (strlen(verify_str) == digits)
507 vsum_str = verify_str;
509 vsum_str = env_get(verify_str);
510 if (vsum_str == NULL || strlen(vsum_str) != digits) {
511 printf("Expected %d hex digits in env var\n",
517 hash_parse_string(algo->name, vsum_str, vsum);
522 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
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]);
531 int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
532 int flag, int argc, char *const argv[])
536 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
537 return CMD_RET_USAGE;
539 addr = simple_strtoul(*argv++, NULL, 16);
540 len = simple_strtoul(*argv++, NULL, 16);
543 struct hash_algo *algo;
545 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
548 if (hash_lookup_algo(algo_name, &algo)) {
549 printf("Unknown hash algorithm '%s'\n", algo_name);
550 return CMD_RET_USAGE;
554 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
555 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
559 output = memalign(ARCH_DMA_MINALIGN,
560 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
562 buf = map_sysmem(addr, len);
563 algo->hash_func_ws(buf, len, output, algo->chunk_size);
566 /* Try to avoid code bloat when verify is not needed */
567 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
568 defined(CONFIG_HASH_VERIFY)
569 if (flags & HASH_FLAG_VERIFY) {
573 if (parse_verify_sum(algo, *argv, vsum,
574 flags & HASH_FLAG_ENV)) {
575 printf("ERROR: %s does not contain a valid "
576 "%s sum\n", *argv, algo->name);
579 if (memcmp(output, vsum, algo->digest_size) != 0) {
582 hash_show(algo, addr, len, output);
584 for (i = 0; i < algo->digest_size; i++)
585 printf("%02x", vsum[i]);
586 puts(" ** ERROR **\n");
590 hash_show(algo, addr, len, output);
594 store_result(algo, output, *argv,
595 flags & HASH_FLAG_ENV);
597 unmap_sysmem(output);
601 /* Horrible code size hack for boards that just want crc32 */
606 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
608 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
609 addr, addr + len - 1, crc);
612 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
619 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
620 #endif /* !USE_HOSTCC */