2 * Copyright (c) 2012 The Chromium OS Authors.
5 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 * SPDX-License-Identifier: GPL-2.0+
20 #include <linux/errno.h>
25 #endif /* !USE_HOSTCC*/
28 #include <u-boot/crc.h>
29 #include <u-boot/sha1.h>
30 #include <u-boot/sha256.h>
31 #include <u-boot/md5.h>
34 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
36 sha1_context *ctx = malloc(sizeof(sha1_context));
42 static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
43 unsigned int size, int is_last)
45 sha1_update((sha1_context *)ctx, buf, size);
49 static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
52 if (size < algo->digest_size)
55 sha1_finish((sha1_context *)ctx, dest_buf);
62 static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
64 sha256_context *ctx = malloc(sizeof(sha256_context));
70 static int hash_update_sha256(struct hash_algo *algo, void *ctx,
71 const void *buf, unsigned int size, int is_last)
73 sha256_update((sha256_context *)ctx, buf, size);
77 static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
80 if (size < algo->digest_size)
83 sha256_finish((sha256_context *)ctx, dest_buf);
89 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
91 uint32_t *ctx = malloc(sizeof(uint32_t));
97 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
98 const void *buf, unsigned int size, int is_last)
100 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
104 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
107 if (size < algo->digest_size)
110 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
116 * These are the hash algorithms we support. Chips which support accelerated
117 * crypto could perhaps add named version of these algorithms here. Note that
118 * algorithm names must be in lower case.
120 static struct hash_algo hash_algo[] = {
122 * CONFIG_SHA_HW_ACCEL is defined if hardware acceleration is
125 #ifdef CONFIG_SHA_HW_ACCEL
131 #ifdef CONFIG_SHA_PROG_HW_ACCEL
141 #ifdef CONFIG_SHA_PROG_HW_ACCEL
181 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM)
185 #if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
189 /* Try to minimize code size for boards that don't want much hashing */
191 #define multi_hash() 1
193 #define multi_hash() 0
196 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
200 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
201 if (!strcmp(algo_name, hash_algo[i].name)) {
202 *algop = &hash_algo[i];
207 debug("Unknown hash algorithm '%s'\n", algo_name);
208 return -EPROTONOSUPPORT;
211 int hash_progressive_lookup_algo(const char *algo_name,
212 struct hash_algo **algop)
216 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
217 if (!strcmp(algo_name, hash_algo[i].name)) {
218 if (hash_algo[i].hash_init) {
219 *algop = &hash_algo[i];
225 debug("Unknown hash algorithm '%s'\n", algo_name);
226 return -EPROTONOSUPPORT;
230 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
232 struct hash_algo *algo;
236 ret = hash_lookup_algo(algo_name, &algo);
240 for (i = 0; i < algo->digest_size; i++) {
243 strncpy(chr, &str[i * 2], 2);
244 result[i] = simple_strtoul(chr, NULL, 16);
250 int hash_block(const char *algo_name, const void *data, unsigned int len,
251 uint8_t *output, int *output_size)
253 struct hash_algo *algo;
256 ret = hash_lookup_algo(algo_name, &algo);
260 if (output_size && *output_size < algo->digest_size) {
261 debug("Output buffer size %d too small (need %d bytes)",
262 *output_size, algo->digest_size);
266 *output_size = algo->digest_size;
267 algo->hash_func_ws(data, len, output, algo->chunk_size);
272 #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
274 * store_result: Store the resulting sum to an address or variable
276 * @algo: Hash algorithm being used
277 * @sum: Hash digest (algo->digest_size bytes)
278 * @dest: Destination, interpreted as a hex address if it starts
279 * with * (or allow_env_vars is 0) or otherwise as an
280 * environment variable.
281 * @allow_env_vars: non-zero to permit storing the result to an
282 * variable environment
284 static void store_result(struct hash_algo *algo, const uint8_t *sum,
285 const char *dest, int allow_env_vars)
291 * If environment variables are allowed, then we assume that 'dest'
292 * is an environment variable, unless it starts with *, in which
293 * case we assume it is an address. If not allowed, it is always an
294 * address. This is to support the crc32 command.
296 if (allow_env_vars) {
304 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
305 char *str_ptr = str_output;
307 for (i = 0; i < algo->digest_size; i++) {
308 sprintf(str_ptr, "%02x", sum[i]);
312 setenv(dest, str_output);
317 addr = simple_strtoul(dest, NULL, 16);
318 buf = map_sysmem(addr, algo->digest_size);
319 memcpy(buf, sum, algo->digest_size);
325 * parse_verify_sum: Parse a hash verification parameter
327 * @algo: Hash algorithm being used
328 * @verify_str: Argument to parse. If it starts with * then it is
329 * interpreted as a hex address containing the hash.
330 * If the length is exactly the right number of hex digits
331 * for the digest size, then we assume it is a hex digest.
332 * Otherwise we assume it is an environment variable, and
333 * look up its value (it must contain a hex digest).
334 * @vsum: Returns binary digest value (algo->digest_size bytes)
335 * @allow_env_vars: non-zero to permit storing the result to an environment
336 * variable. If 0 then verify_str is assumed to be an
337 * address, and the * prefix is not expected.
338 * @return 0 if ok, non-zero on error
340 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
341 uint8_t *vsum, int allow_env_vars)
345 /* See comment above in store_result() */
346 if (allow_env_vars) {
347 if (*verify_str == '*')
357 addr = simple_strtoul(verify_str, NULL, 16);
358 buf = map_sysmem(addr, algo->digest_size);
359 memcpy(vsum, buf, algo->digest_size);
362 int digits = algo->digest_size * 2;
365 * As with the original code from sha1sum.c, we assume that a
366 * string which matches the digest size exactly is a hex
367 * string and not an environment variable.
369 if (strlen(verify_str) == digits)
370 vsum_str = verify_str;
372 vsum_str = getenv(verify_str);
373 if (vsum_str == NULL || strlen(vsum_str) != digits) {
374 printf("Expected %d hex digits in env var\n",
380 hash_parse_string(algo->name, vsum_str, vsum);
385 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
389 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
390 for (i = 0; i < algo->digest_size; i++)
391 printf("%02x", output[i]);
394 int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
395 int argc, char * const argv[])
399 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
400 return CMD_RET_USAGE;
402 addr = simple_strtoul(*argv++, NULL, 16);
403 len = simple_strtoul(*argv++, NULL, 16);
406 struct hash_algo *algo;
407 uint8_t output[HASH_MAX_DIGEST_SIZE];
408 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
411 if (hash_lookup_algo(algo_name, &algo)) {
412 printf("Unknown hash algorithm '%s'\n", algo_name);
413 return CMD_RET_USAGE;
417 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
418 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
422 buf = map_sysmem(addr, len);
423 algo->hash_func_ws(buf, len, output, algo->chunk_size);
426 /* Try to avoid code bloat when verify is not needed */
427 #ifdef CONFIG_HASH_VERIFY
428 if (flags & HASH_FLAG_VERIFY) {
432 if (parse_verify_sum(algo, *argv, vsum,
433 flags & HASH_FLAG_ENV)) {
434 printf("ERROR: %s does not contain a valid "
435 "%s sum\n", *argv, algo->name);
438 if (memcmp(output, vsum, algo->digest_size) != 0) {
441 hash_show(algo, addr, len, output);
443 for (i = 0; i < algo->digest_size; i++)
444 printf("%02x", vsum[i]);
445 puts(" ** ERROR **\n");
449 hash_show(algo, addr, len, output);
453 store_result(algo, output, *argv,
454 flags & HASH_FLAG_ENV);
458 /* Horrible code size hack for boards that just want crc32 */
463 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
465 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
466 addr, addr + len - 1, crc);
469 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
476 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
477 #endif /* !USE_HOSTCC */