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 sha512_finish((sha512_context *)ctx, dest_buf);
156 static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
158 uint16_t *ctx = malloc(sizeof(uint16_t));
164 static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
165 const void *buf, unsigned int size,
168 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
172 static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
173 void *dest_buf, int size)
175 if (size < algo->digest_size)
178 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
183 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
185 uint32_t *ctx = malloc(sizeof(uint32_t));
191 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
192 const void *buf, unsigned int size, int is_last)
194 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
198 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
201 if (size < algo->digest_size)
204 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
210 * These are the hash algorithms we support. If we have hardware acceleration
211 * is enable we will use that, otherwise a software version of the algorithm.
212 * Note that algorithm names must be in lower case.
214 static struct hash_algo hash_algo[] = {
218 .digest_size = SHA1_SUM_LEN,
219 .chunk_size = CHUNKSZ_SHA1,
220 #ifdef CONFIG_SHA_HW_ACCEL
221 .hash_func_ws = hw_sha1,
223 .hash_func_ws = sha1_csum_wd,
225 #ifdef CONFIG_SHA_PROG_HW_ACCEL
226 .hash_init = hw_sha_init,
227 .hash_update = hw_sha_update,
228 .hash_finish = hw_sha_finish,
230 .hash_init = hash_init_sha1,
231 .hash_update = hash_update_sha1,
232 .hash_finish = hash_finish_sha1,
239 .digest_size = SHA256_SUM_LEN,
240 .chunk_size = CHUNKSZ_SHA256,
241 #ifdef CONFIG_SHA_HW_ACCEL
242 .hash_func_ws = hw_sha256,
244 .hash_func_ws = sha256_csum_wd,
246 #ifdef CONFIG_SHA_PROG_HW_ACCEL
247 .hash_init = hw_sha_init,
248 .hash_update = hw_sha_update,
249 .hash_finish = hw_sha_finish,
251 .hash_init = hash_init_sha256,
252 .hash_update = hash_update_sha256,
253 .hash_finish = hash_finish_sha256,
260 .digest_size = SHA384_SUM_LEN,
261 .chunk_size = CHUNKSZ_SHA384,
262 .hash_func_ws = sha384_csum_wd,
263 .hash_init = hash_init_sha384,
264 .hash_update = hash_update_sha384,
265 .hash_finish = hash_finish_sha384,
271 .digest_size = SHA512_SUM_LEN,
272 .chunk_size = CHUNKSZ_SHA512,
273 .hash_func_ws = sha512_csum_wd,
274 .hash_init = hash_init_sha512,
275 .hash_update = hash_update_sha512,
276 .hash_finish = hash_finish_sha512,
280 .name = "crc16-ccitt",
282 .chunk_size = CHUNKSZ,
283 .hash_func_ws = crc16_ccitt_wd_buf,
284 .hash_init = hash_init_crc16_ccitt,
285 .hash_update = hash_update_crc16_ccitt,
286 .hash_finish = hash_finish_crc16_ccitt,
291 .chunk_size = CHUNKSZ_CRC32,
292 .hash_func_ws = crc32_wd_buf,
293 .hash_init = hash_init_crc32,
294 .hash_update = hash_update_crc32,
295 .hash_finish = hash_finish_crc32,
299 /* Try to minimize code size for boards that don't want much hashing */
300 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
301 defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) || \
302 defined(CONFIG_SHA384) || defined(CONFIG_SHA512)
303 #define multi_hash() 1
305 #define multi_hash() 0
308 static void reloc_update(void)
310 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
316 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
317 hash_algo[i].name += gd->reloc_off;
318 hash_algo[i].hash_func_ws += gd->reloc_off;
319 hash_algo[i].hash_init += gd->reloc_off;
320 hash_algo[i].hash_update += gd->reloc_off;
321 hash_algo[i].hash_finish += gd->reloc_off;
327 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
333 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
334 if (!strcmp(algo_name, hash_algo[i].name)) {
335 *algop = &hash_algo[i];
340 debug("Unknown hash algorithm '%s'\n", algo_name);
341 return -EPROTONOSUPPORT;
344 int hash_progressive_lookup_algo(const char *algo_name,
345 struct hash_algo **algop)
351 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
352 if (!strcmp(algo_name, hash_algo[i].name)) {
353 if (hash_algo[i].hash_init) {
354 *algop = &hash_algo[i];
360 debug("Unknown hash algorithm '%s'\n", algo_name);
361 return -EPROTONOSUPPORT;
365 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
367 struct hash_algo *algo;
371 ret = hash_lookup_algo(algo_name, &algo);
375 for (i = 0; i < algo->digest_size; i++) {
378 strncpy(chr, &str[i * 2], 2);
379 result[i] = simple_strtoul(chr, NULL, 16);
385 int hash_block(const char *algo_name, const void *data, unsigned int len,
386 uint8_t *output, int *output_size)
388 struct hash_algo *algo;
391 ret = hash_lookup_algo(algo_name, &algo);
395 if (output_size && *output_size < algo->digest_size) {
396 debug("Output buffer size %d too small (need %d bytes)",
397 *output_size, algo->digest_size);
401 *output_size = algo->digest_size;
402 algo->hash_func_ws(data, len, output, algo->chunk_size);
407 #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
409 * store_result: Store the resulting sum to an address or variable
411 * @algo: Hash algorithm being used
412 * @sum: Hash digest (algo->digest_size bytes)
413 * @dest: Destination, interpreted as a hex address if it starts
414 * with * (or allow_env_vars is 0) or otherwise as an
415 * environment variable.
416 * @allow_env_vars: non-zero to permit storing the result to an
417 * variable environment
419 static void store_result(struct hash_algo *algo, const uint8_t *sum,
420 const char *dest, int allow_env_vars)
426 * If environment variables are allowed, then we assume that 'dest'
427 * is an environment variable, unless it starts with *, in which
428 * case we assume it is an address. If not allowed, it is always an
429 * address. This is to support the crc32 command.
431 if (allow_env_vars) {
439 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
440 char *str_ptr = str_output;
442 for (i = 0; i < algo->digest_size; i++) {
443 sprintf(str_ptr, "%02x", sum[i]);
447 env_set(dest, str_output);
452 addr = simple_strtoul(dest, NULL, 16);
453 buf = map_sysmem(addr, algo->digest_size);
454 memcpy(buf, sum, algo->digest_size);
460 * parse_verify_sum: Parse a hash verification parameter
462 * @algo: Hash algorithm being used
463 * @verify_str: Argument to parse. If it starts with * then it is
464 * interpreted as a hex address containing the hash.
465 * If the length is exactly the right number of hex digits
466 * for the digest size, then we assume it is a hex digest.
467 * Otherwise we assume it is an environment variable, and
468 * look up its value (it must contain a hex digest).
469 * @vsum: Returns binary digest value (algo->digest_size bytes)
470 * @allow_env_vars: non-zero to permit storing the result to an environment
471 * variable. If 0 then verify_str is assumed to be an
472 * address, and the * prefix is not expected.
473 * @return 0 if ok, non-zero on error
475 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
476 uint8_t *vsum, int allow_env_vars)
480 /* See comment above in store_result() */
481 if (allow_env_vars) {
482 if (*verify_str == '*')
492 addr = simple_strtoul(verify_str, NULL, 16);
493 buf = map_sysmem(addr, algo->digest_size);
494 memcpy(vsum, buf, algo->digest_size);
497 int digits = algo->digest_size * 2;
500 * As with the original code from sha1sum.c, we assume that a
501 * string which matches the digest size exactly is a hex
502 * string and not an environment variable.
504 if (strlen(verify_str) == digits)
505 vsum_str = verify_str;
507 vsum_str = env_get(verify_str);
508 if (vsum_str == NULL || strlen(vsum_str) != digits) {
509 printf("Expected %d hex digits in env var\n",
515 hash_parse_string(algo->name, vsum_str, vsum);
520 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
524 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
525 for (i = 0; i < algo->digest_size; i++)
526 printf("%02x", output[i]);
529 int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
530 int flag, int argc, char *const argv[])
534 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
535 return CMD_RET_USAGE;
537 addr = simple_strtoul(*argv++, NULL, 16);
538 len = simple_strtoul(*argv++, NULL, 16);
541 struct hash_algo *algo;
543 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
546 if (hash_lookup_algo(algo_name, &algo)) {
547 printf("Unknown hash algorithm '%s'\n", algo_name);
548 return CMD_RET_USAGE;
552 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
553 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
557 output = memalign(ARCH_DMA_MINALIGN,
558 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
560 buf = map_sysmem(addr, len);
561 algo->hash_func_ws(buf, len, output, algo->chunk_size);
564 /* Try to avoid code bloat when verify is not needed */
565 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
566 defined(CONFIG_HASH_VERIFY)
567 if (flags & HASH_FLAG_VERIFY) {
571 if (parse_verify_sum(algo, *argv, vsum,
572 flags & HASH_FLAG_ENV)) {
573 printf("ERROR: %s does not contain a valid "
574 "%s sum\n", *argv, algo->name);
577 if (memcmp(output, vsum, algo->digest_size) != 0) {
580 hash_show(algo, addr, len, output);
582 for (i = 0; i < algo->digest_size; i++)
583 printf("%02x", vsum[i]);
584 puts(" ** ERROR **\n");
588 hash_show(algo, addr, len, output);
592 store_result(algo, output, *argv,
593 flags & HASH_FLAG_ENV);
595 unmap_sysmem(output);
599 /* Horrible code size hack for boards that just want crc32 */
604 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
606 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
607 addr, addr + len - 1, crc);
610 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
617 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
618 #endif /* !USE_HOSTCC */