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>
28 #endif /* !USE_HOSTCC*/
32 #include <u-boot/crc.h>
33 #include <u-boot/sha1.h>
34 #include <u-boot/sha256.h>
35 #include <u-boot/sha512.h>
36 #include <u-boot/md5.h>
38 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
39 DECLARE_GLOBAL_DATA_PTR;
42 static void reloc_update(void);
44 #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
45 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
47 sha1_context *ctx = malloc(sizeof(sha1_context));
53 static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
54 unsigned int size, int is_last)
56 sha1_update((sha1_context *)ctx, buf, size);
60 static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
63 if (size < algo->digest_size)
66 sha1_finish((sha1_context *)ctx, dest_buf);
72 #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
73 static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
75 sha256_context *ctx = malloc(sizeof(sha256_context));
81 static int hash_update_sha256(struct hash_algo *algo, void *ctx,
82 const void *buf, unsigned int size, int is_last)
84 sha256_update((sha256_context *)ctx, buf, size);
88 static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
91 if (size < algo->digest_size)
94 sha256_finish((sha256_context *)ctx, dest_buf);
100 #if defined(CONFIG_SHA384) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
101 static int hash_init_sha384(struct hash_algo *algo, void **ctxp)
103 sha512_context *ctx = malloc(sizeof(sha512_context));
109 static int hash_update_sha384(struct hash_algo *algo, void *ctx,
110 const void *buf, unsigned int size, int is_last)
112 sha384_update((sha512_context *)ctx, buf, size);
116 static int hash_finish_sha384(struct hash_algo *algo, void *ctx, void
119 if (size < algo->digest_size)
122 sha384_finish((sha512_context *)ctx, dest_buf);
128 #if defined(CONFIG_SHA512) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
129 static int hash_init_sha512(struct hash_algo *algo, void **ctxp)
131 sha512_context *ctx = malloc(sizeof(sha512_context));
137 static int hash_update_sha512(struct hash_algo *algo, void *ctx,
138 const void *buf, unsigned int size, int is_last)
140 sha512_update((sha512_context *)ctx, buf, size);
144 static int hash_finish_sha512(struct hash_algo *algo, void *ctx, void
147 if (size < algo->digest_size)
150 sha512_finish((sha512_context *)ctx, dest_buf);
157 static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
159 uint16_t *ctx = malloc(sizeof(uint16_t));
165 static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
166 const void *buf, unsigned int size,
169 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
173 static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
174 void *dest_buf, int size)
176 if (size < algo->digest_size)
179 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
184 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
186 uint32_t *ctx = malloc(sizeof(uint32_t));
192 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
193 const void *buf, unsigned int size, int is_last)
195 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
199 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
202 if (size < algo->digest_size)
205 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
211 # define I_WANT_MD5 1
213 # define I_WANT_MD5 CONFIG_IS_ENABLED(MD5)
216 * These are the hash algorithms we support. If we have hardware acceleration
217 * is enable we will use that, otherwise a software version of the algorithm.
218 * Note that algorithm names must be in lower case.
220 static struct hash_algo hash_algo[] = {
224 .digest_size = MD5_SUM_LEN,
225 .chunk_size = CHUNKSZ_MD5,
226 .hash_func_ws = md5_wd,
232 .digest_size = SHA1_SUM_LEN,
233 .chunk_size = CHUNKSZ_SHA1,
234 #ifdef CONFIG_SHA_HW_ACCEL
235 .hash_func_ws = hw_sha1,
237 .hash_func_ws = sha1_csum_wd,
239 #ifdef CONFIG_SHA_PROG_HW_ACCEL
240 .hash_init = hw_sha_init,
241 .hash_update = hw_sha_update,
242 .hash_finish = hw_sha_finish,
244 .hash_init = hash_init_sha1,
245 .hash_update = hash_update_sha1,
246 .hash_finish = hash_finish_sha1,
253 .digest_size = SHA256_SUM_LEN,
254 .chunk_size = CHUNKSZ_SHA256,
255 #ifdef CONFIG_SHA_HW_ACCEL
256 .hash_func_ws = hw_sha256,
258 .hash_func_ws = sha256_csum_wd,
260 #ifdef CONFIG_SHA_PROG_HW_ACCEL
261 .hash_init = hw_sha_init,
262 .hash_update = hw_sha_update,
263 .hash_finish = hw_sha_finish,
265 .hash_init = hash_init_sha256,
266 .hash_update = hash_update_sha256,
267 .hash_finish = hash_finish_sha256,
274 .digest_size = SHA384_SUM_LEN,
275 .chunk_size = CHUNKSZ_SHA384,
276 #ifdef CONFIG_SHA512_HW_ACCEL
277 .hash_func_ws = hw_sha384,
279 .hash_func_ws = sha384_csum_wd,
281 #if defined(CONFIG_SHA512_HW_ACCEL) && defined(CONFIG_SHA_PROG_HW_ACCEL)
282 .hash_init = hw_sha_init,
283 .hash_update = hw_sha_update,
284 .hash_finish = hw_sha_finish,
286 .hash_init = hash_init_sha384,
287 .hash_update = hash_update_sha384,
288 .hash_finish = hash_finish_sha384,
295 .digest_size = SHA512_SUM_LEN,
296 .chunk_size = CHUNKSZ_SHA512,
297 #ifdef CONFIG_SHA512_HW_ACCEL
298 .hash_func_ws = hw_sha512,
300 .hash_func_ws = sha512_csum_wd,
302 #if defined(CONFIG_SHA512_HW_ACCEL) && defined(CONFIG_SHA_PROG_HW_ACCEL)
303 .hash_init = hw_sha_init,
304 .hash_update = hw_sha_update,
305 .hash_finish = hw_sha_finish,
307 .hash_init = hash_init_sha512,
308 .hash_update = hash_update_sha512,
309 .hash_finish = hash_finish_sha512,
314 .name = "crc16-ccitt",
316 .chunk_size = CHUNKSZ,
317 .hash_func_ws = crc16_ccitt_wd_buf,
318 .hash_init = hash_init_crc16_ccitt,
319 .hash_update = hash_update_crc16_ccitt,
320 .hash_finish = hash_finish_crc16_ccitt,
325 .chunk_size = CHUNKSZ_CRC32,
326 .hash_func_ws = crc32_wd_buf,
327 .hash_init = hash_init_crc32,
328 .hash_update = hash_update_crc32,
329 .hash_finish = hash_finish_crc32,
333 /* Try to minimize code size for boards that don't want much hashing */
334 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
335 defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) || \
336 defined(CONFIG_SHA384) || defined(CONFIG_SHA512)
337 #define multi_hash() 1
339 #define multi_hash() 0
342 static void reloc_update(void)
344 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
350 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
351 hash_algo[i].name += gd->reloc_off;
352 hash_algo[i].hash_func_ws += gd->reloc_off;
353 hash_algo[i].hash_init += gd->reloc_off;
354 hash_algo[i].hash_update += gd->reloc_off;
355 hash_algo[i].hash_finish += gd->reloc_off;
361 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
367 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
368 if (!strcmp(algo_name, hash_algo[i].name)) {
369 *algop = &hash_algo[i];
374 debug("Unknown hash algorithm '%s'\n", algo_name);
375 return -EPROTONOSUPPORT;
378 int hash_progressive_lookup_algo(const char *algo_name,
379 struct hash_algo **algop)
385 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
386 if (!strcmp(algo_name, hash_algo[i].name)) {
387 if (hash_algo[i].hash_init) {
388 *algop = &hash_algo[i];
394 debug("Unknown hash algorithm '%s'\n", algo_name);
395 return -EPROTONOSUPPORT;
399 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
401 struct hash_algo *algo;
405 ret = hash_lookup_algo(algo_name, &algo);
409 for (i = 0; i < algo->digest_size; i++) {
412 strlcpy(chr, &str[i * 2], 3);
413 result[i] = hextoul(chr, NULL);
419 int hash_block(const char *algo_name, const void *data, unsigned int len,
420 uint8_t *output, int *output_size)
422 struct hash_algo *algo;
425 ret = hash_lookup_algo(algo_name, &algo);
429 if (output_size && *output_size < algo->digest_size) {
430 debug("Output buffer size %d too small (need %d bytes)",
431 *output_size, algo->digest_size);
435 *output_size = algo->digest_size;
436 algo->hash_func_ws(data, len, output, algo->chunk_size);
441 #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
443 * store_result: Store the resulting sum to an address or variable
445 * @algo: Hash algorithm being used
446 * @sum: Hash digest (algo->digest_size bytes)
447 * @dest: Destination, interpreted as a hex address if it starts
448 * with * (or allow_env_vars is 0) or otherwise as an
449 * environment variable.
450 * @allow_env_vars: non-zero to permit storing the result to an
451 * variable environment
453 static void store_result(struct hash_algo *algo, const uint8_t *sum,
454 const char *dest, int allow_env_vars)
460 * If environment variables are allowed, then we assume that 'dest'
461 * is an environment variable, unless it starts with *, in which
462 * case we assume it is an address. If not allowed, it is always an
463 * address. This is to support the crc32 command.
465 if (allow_env_vars) {
473 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
474 char *str_ptr = str_output;
476 for (i = 0; i < algo->digest_size; i++) {
477 sprintf(str_ptr, "%02x", sum[i]);
481 env_set(dest, str_output);
486 addr = hextoul(dest, NULL);
487 buf = map_sysmem(addr, algo->digest_size);
488 memcpy(buf, sum, algo->digest_size);
494 * parse_verify_sum: Parse a hash verification parameter
496 * @algo: Hash algorithm being used
497 * @verify_str: Argument to parse. If it starts with * then it is
498 * interpreted as a hex address containing the hash.
499 * If the length is exactly the right number of hex digits
500 * for the digest size, then we assume it is a hex digest.
501 * Otherwise we assume it is an environment variable, and
502 * look up its value (it must contain a hex digest).
503 * @vsum: Returns binary digest value (algo->digest_size bytes)
504 * @allow_env_vars: non-zero to permit storing the result to an environment
505 * variable. If 0 then verify_str is assumed to be an
506 * address, and the * prefix is not expected.
507 * @return 0 if ok, non-zero on error
509 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
510 uint8_t *vsum, int allow_env_vars)
514 /* See comment above in store_result() */
515 if (allow_env_vars) {
516 if (*verify_str == '*')
526 addr = hextoul(verify_str, NULL);
527 buf = map_sysmem(addr, algo->digest_size);
528 memcpy(vsum, buf, algo->digest_size);
531 int digits = algo->digest_size * 2;
534 * As with the original code from sha1sum.c, we assume that a
535 * string which matches the digest size exactly is a hex
536 * string and not an environment variable.
538 if (strlen(verify_str) == digits)
539 vsum_str = verify_str;
541 vsum_str = env_get(verify_str);
542 if (vsum_str == NULL || strlen(vsum_str) != digits) {
543 printf("Expected %d hex digits in env var\n",
549 hash_parse_string(algo->name, vsum_str, vsum);
554 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
558 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
559 for (i = 0; i < algo->digest_size; i++)
560 printf("%02x", output[i]);
563 int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
564 int flag, int argc, char *const argv[])
568 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
569 return CMD_RET_USAGE;
571 addr = hextoul(*argv++, NULL);
572 len = hextoul(*argv++, NULL);
575 struct hash_algo *algo;
577 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
580 if (hash_lookup_algo(algo_name, &algo)) {
581 printf("Unknown hash algorithm '%s'\n", algo_name);
582 return CMD_RET_USAGE;
586 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
587 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
591 output = memalign(ARCH_DMA_MINALIGN,
592 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
594 buf = map_sysmem(addr, len);
595 algo->hash_func_ws(buf, len, output, algo->chunk_size);
598 /* Try to avoid code bloat when verify is not needed */
599 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
600 defined(CONFIG_HASH_VERIFY)
601 if (flags & HASH_FLAG_VERIFY) {
605 if (parse_verify_sum(algo, *argv, vsum,
606 flags & HASH_FLAG_ENV)) {
607 printf("ERROR: %s does not contain a valid "
608 "%s sum\n", *argv, algo->name);
611 if (memcmp(output, vsum, algo->digest_size) != 0) {
614 hash_show(algo, addr, len, output);
616 for (i = 0; i < algo->digest_size; i++)
617 printf("%02x", vsum[i]);
618 puts(" ** ERROR **\n");
622 hash_show(algo, addr, len, output);
626 store_result(algo, output, *argv,
627 flags & HASH_FLAG_ENV);
629 unmap_sysmem(output);
633 /* Horrible code size hack for boards that just want crc32 */
638 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
640 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
641 addr, addr + len - 1, crc);
644 ptr = (ulong *)hextoul(argv[0], NULL);
651 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
652 #endif /* !USE_HOSTCC */