1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012 The Chromium OS Authors.
19 #include <asm/cache.h>
20 #include <asm/global_data.h>
22 #include <linux/errno.h>
25 #include <linux/compiler_attributes.h>
27 #include <linux/kconfig.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 static int __maybe_unused hash_init_sha1(struct hash_algo *algo, void **ctxp)
40 sha1_context *ctx = malloc(sizeof(sha1_context));
46 static int __maybe_unused hash_update_sha1(struct hash_algo *algo, void *ctx,
47 const void *buf, unsigned int size,
50 sha1_update((sha1_context *)ctx, buf, size);
54 static int __maybe_unused hash_finish_sha1(struct hash_algo *algo, void *ctx,
55 void *dest_buf, int size)
57 if (size < algo->digest_size)
60 sha1_finish((sha1_context *)ctx, dest_buf);
65 static int __maybe_unused hash_init_sha256(struct hash_algo *algo, void **ctxp)
67 sha256_context *ctx = malloc(sizeof(sha256_context));
73 static int __maybe_unused hash_update_sha256(struct hash_algo *algo, void *ctx,
74 const void *buf, uint size,
77 sha256_update((sha256_context *)ctx, buf, size);
81 static int __maybe_unused hash_finish_sha256(struct hash_algo *algo, void *ctx,
82 void *dest_buf, int size)
84 if (size < algo->digest_size)
87 sha256_finish((sha256_context *)ctx, dest_buf);
92 static int __maybe_unused hash_init_sha384(struct hash_algo *algo, void **ctxp)
94 sha512_context *ctx = malloc(sizeof(sha512_context));
100 static int __maybe_unused hash_update_sha384(struct hash_algo *algo, void *ctx,
101 const void *buf, uint size,
104 sha384_update((sha512_context *)ctx, buf, size);
108 static int __maybe_unused hash_finish_sha384(struct hash_algo *algo, void *ctx,
109 void *dest_buf, int size)
111 if (size < algo->digest_size)
114 sha384_finish((sha512_context *)ctx, dest_buf);
119 static int __maybe_unused hash_init_sha512(struct hash_algo *algo, void **ctxp)
121 sha512_context *ctx = malloc(sizeof(sha512_context));
127 static int __maybe_unused hash_update_sha512(struct hash_algo *algo, void *ctx,
128 const void *buf, uint size,
131 sha512_update((sha512_context *)ctx, buf, size);
135 static int __maybe_unused hash_finish_sha512(struct hash_algo *algo, void *ctx,
136 void *dest_buf, int size)
138 if (size < algo->digest_size)
141 sha512_finish((sha512_context *)ctx, dest_buf);
146 static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
148 uint16_t *ctx = malloc(sizeof(uint16_t));
154 static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
155 const void *buf, unsigned int size,
158 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
162 static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
163 void *dest_buf, int size)
165 if (size < algo->digest_size)
168 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
173 static int __maybe_unused hash_init_crc32(struct hash_algo *algo, void **ctxp)
175 uint32_t *ctx = malloc(sizeof(uint32_t));
181 static int __maybe_unused hash_update_crc32(struct hash_algo *algo, void *ctx,
182 const void *buf, unsigned int size,
185 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
189 static int __maybe_unused hash_finish_crc32(struct hash_algo *algo, void *ctx,
190 void *dest_buf, int size)
192 if (size < algo->digest_size)
195 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
201 * These are the hash algorithms we support. If we have hardware acceleration
202 * is enable we will use that, otherwise a software version of the algorithm.
203 * Note that algorithm names must be in lower case.
205 static struct hash_algo hash_algo[] = {
206 #if CONFIG_IS_ENABLED(MD5)
209 .digest_size = MD5_SUM_LEN,
210 .chunk_size = CHUNKSZ_MD5,
211 .hash_func_ws = md5_wd,
214 #if CONFIG_IS_ENABLED(SHA1)
217 .digest_size = SHA1_SUM_LEN,
218 .chunk_size = CHUNKSZ_SHA1,
219 #if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
220 .hash_func_ws = hw_sha1,
222 .hash_func_ws = sha1_csum_wd,
224 #if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
225 .hash_init = hw_sha_init,
226 .hash_update = hw_sha_update,
227 .hash_finish = hw_sha_finish,
229 .hash_init = hash_init_sha1,
230 .hash_update = hash_update_sha1,
231 .hash_finish = hash_finish_sha1,
235 #if CONFIG_IS_ENABLED(SHA256)
238 .digest_size = SHA256_SUM_LEN,
239 .chunk_size = CHUNKSZ_SHA256,
240 #if CONFIG_IS_ENABLED(SHA_HW_ACCEL)
241 .hash_func_ws = hw_sha256,
243 .hash_func_ws = sha256_csum_wd,
245 #if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
246 .hash_init = hw_sha_init,
247 .hash_update = hw_sha_update,
248 .hash_finish = hw_sha_finish,
250 .hash_init = hash_init_sha256,
251 .hash_update = hash_update_sha256,
252 .hash_finish = hash_finish_sha256,
256 #if CONFIG_IS_ENABLED(SHA384)
259 .digest_size = SHA384_SUM_LEN,
260 .chunk_size = CHUNKSZ_SHA384,
261 #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
262 .hash_func_ws = hw_sha384,
264 .hash_func_ws = sha384_csum_wd,
266 #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
267 .hash_init = hw_sha_init,
268 .hash_update = hw_sha_update,
269 .hash_finish = hw_sha_finish,
271 .hash_init = hash_init_sha384,
272 .hash_update = hash_update_sha384,
273 .hash_finish = hash_finish_sha384,
277 #if CONFIG_IS_ENABLED(SHA512)
280 .digest_size = SHA512_SUM_LEN,
281 .chunk_size = CHUNKSZ_SHA512,
282 #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL)
283 .hash_func_ws = hw_sha512,
285 .hash_func_ws = sha512_csum_wd,
287 #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL)
288 .hash_init = hw_sha_init,
289 .hash_update = hw_sha_update,
290 .hash_finish = hw_sha_finish,
292 .hash_init = hash_init_sha512,
293 .hash_update = hash_update_sha512,
294 .hash_finish = hash_finish_sha512,
299 .name = "crc16-ccitt",
301 .chunk_size = CHUNKSZ,
302 .hash_func_ws = crc16_ccitt_wd_buf,
303 .hash_init = hash_init_crc16_ccitt,
304 .hash_update = hash_update_crc16_ccitt,
305 .hash_finish = hash_finish_crc16_ccitt,
307 #if CONFIG_IS_ENABLED(CRC32)
311 .chunk_size = CHUNKSZ_CRC32,
312 .hash_func_ws = crc32_wd_buf,
313 .hash_init = hash_init_crc32,
314 .hash_update = hash_update_crc32,
315 .hash_finish = hash_finish_crc32,
320 /* Try to minimize code size for boards that don't want much hashing */
321 #if CONFIG_IS_ENABLED(SHA256) || IS_ENABLED(CONFIG_CMD_SHA1SUM) || \
322 CONFIG_IS_ENABLED(CRC32_VERIFY) || IS_ENABLED(CONFIG_CMD_HASH) || \
323 CONFIG_IS_ENABLED(SHA384) || CONFIG_IS_ENABLED(SHA512) || \
324 IS_ENABLED(CONFIG_CMD_MD5SUM)
325 #define multi_hash() 1
327 #define multi_hash() 0
330 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
334 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
335 if (!strcmp(algo_name, hash_algo[i].name)) {
336 *algop = &hash_algo[i];
341 debug("Unknown hash algorithm '%s'\n", algo_name);
342 return -EPROTONOSUPPORT;
345 int hash_progressive_lookup_algo(const char *algo_name,
346 struct hash_algo **algop)
350 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
351 if (!strcmp(algo_name, hash_algo[i].name)) {
352 if (hash_algo[i].hash_init) {
353 *algop = &hash_algo[i];
359 debug("Unknown hash algorithm '%s'\n", algo_name);
360 return -EPROTONOSUPPORT;
364 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
366 struct hash_algo *algo;
370 ret = hash_lookup_algo(algo_name, &algo);
374 for (i = 0; i < algo->digest_size; i++) {
377 strlcpy(chr, &str[i * 2], 3);
378 result[i] = hextoul(chr, NULL);
384 int hash_block(const char *algo_name, const void *data, unsigned int len,
385 uint8_t *output, int *output_size)
387 struct hash_algo *algo;
390 ret = hash_lookup_algo(algo_name, &algo);
394 if (output_size && *output_size < algo->digest_size) {
395 debug("Output buffer size %d too small (need %d bytes)",
396 *output_size, algo->digest_size);
400 *output_size = algo->digest_size;
401 algo->hash_func_ws(data, len, output, algo->chunk_size);
406 #if !defined(CONFIG_SPL_BUILD) && (defined(CONFIG_CMD_HASH) || \
407 defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)) || \
408 defined(CONFIG_CMD_MD5SUM)
410 * store_result: Store the resulting sum to an address or variable
412 * @algo: Hash algorithm being used
413 * @sum: Hash digest (algo->digest_size bytes)
414 * @dest: Destination, interpreted as a hex address if it starts
415 * with * (or allow_env_vars is 0) or otherwise as an
416 * environment variable.
417 * @allow_env_vars: non-zero to permit storing the result to an
418 * variable environment
420 static void store_result(struct hash_algo *algo, const uint8_t *sum,
421 const char *dest, int allow_env_vars)
427 * If environment variables are allowed, then we assume that 'dest'
428 * is an environment variable, unless it starts with *, in which
429 * case we assume it is an address. If not allowed, it is always an
430 * address. This is to support the crc32 command.
432 if (allow_env_vars) {
440 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
441 char *str_ptr = str_output;
443 for (i = 0; i < algo->digest_size; i++) {
444 sprintf(str_ptr, "%02x", sum[i]);
448 env_set(dest, str_output);
453 addr = hextoul(dest, NULL);
454 buf = map_sysmem(addr, algo->digest_size);
455 memcpy(buf, sum, algo->digest_size);
461 * parse_verify_sum: Parse a hash verification parameter
463 * @algo: Hash algorithm being used
464 * @verify_str: Argument to parse. If it starts with * then it is
465 * interpreted as a hex address containing the hash.
466 * If the length is exactly the right number of hex digits
467 * for the digest size, then we assume it is a hex digest.
468 * Otherwise we assume it is an environment variable, and
469 * look up its value (it must contain a hex digest).
470 * @vsum: Returns binary digest value (algo->digest_size bytes)
471 * @allow_env_vars: non-zero to permit storing the result to an environment
472 * variable. If 0 then verify_str is assumed to be an
473 * address, and the * prefix is not expected.
474 * Return: 0 if ok, non-zero on error
476 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
477 uint8_t *vsum, int allow_env_vars)
481 /* See comment above in store_result() */
482 if (allow_env_vars) {
483 if (*verify_str == '*')
493 addr = hextoul(verify_str, NULL);
494 buf = map_sysmem(addr, algo->digest_size);
495 memcpy(vsum, buf, algo->digest_size);
498 int digits = algo->digest_size * 2;
501 * As with the original code from sha1sum.c, we assume that a
502 * string which matches the digest size exactly is a hex
503 * string and not an environment variable.
505 if (strlen(verify_str) == digits)
506 vsum_str = verify_str;
508 vsum_str = env_get(verify_str);
509 if (vsum_str == NULL || strlen(vsum_str) != digits) {
510 printf("Expected %d hex digits in env var\n",
516 hash_parse_string(algo->name, vsum_str, vsum);
521 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
525 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
526 for (i = 0; i < algo->digest_size; i++)
527 printf("%02x", output[i]);
530 int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
531 int flag, int argc, char *const argv[])
535 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
536 return CMD_RET_USAGE;
538 addr = hextoul(*argv++, NULL);
539 len = hextoul(*argv++, NULL);
542 struct hash_algo *algo;
544 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
547 if (hash_lookup_algo(algo_name, &algo)) {
548 printf("Unknown hash algorithm '%s'\n", algo_name);
549 return CMD_RET_USAGE;
553 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
554 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
558 output = memalign(ARCH_DMA_MINALIGN,
559 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
561 return CMD_RET_FAILURE;
563 buf = map_sysmem(addr, len);
564 algo->hash_func_ws(buf, len, output, algo->chunk_size);
567 /* Try to avoid code bloat when verify is not needed */
568 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
569 defined(CONFIG_MD5SUM_VERIFY) || defined(CONFIG_HASH_VERIFY)
570 if (flags & HASH_FLAG_VERIFY) {
574 if (parse_verify_sum(algo, *argv, vsum,
575 flags & HASH_FLAG_ENV)) {
576 printf("ERROR: %s does not contain a valid "
577 "%s sum\n", *argv, algo->name);
581 if (memcmp(output, vsum, algo->digest_size) != 0) {
584 hash_show(algo, addr, len, output);
586 for (i = 0; i < algo->digest_size; i++)
587 printf("%02x", vsum[i]);
588 puts(" ** ERROR **\n");
593 hash_show(algo, addr, len, output);
597 store_result(algo, output, *argv,
598 flags & HASH_FLAG_ENV);
604 /* Horrible code size hack for boards that just want crc32 */
609 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
611 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
612 addr, addr + len - 1, crc);
615 ptr = (ulong *)hextoul(argv[0], NULL);
622 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
623 #endif /* !USE_HOSTCC */