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+
19 #include <asm/errno.h>
24 #endif /* !USE_HOSTCC*/
27 #include <u-boot/crc.h>
28 #include <u-boot/sha1.h>
29 #include <u-boot/sha256.h>
30 #include <u-boot/md5.h>
33 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
35 sha1_context *ctx = malloc(sizeof(sha1_context));
41 static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
42 unsigned int size, int is_last)
44 sha1_update((sha1_context *)ctx, buf, size);
48 static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
51 if (size < algo->digest_size)
54 sha1_finish((sha1_context *)ctx, dest_buf);
61 static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
63 sha256_context *ctx = malloc(sizeof(sha256_context));
69 static int hash_update_sha256(struct hash_algo *algo, void *ctx,
70 const void *buf, unsigned int size, int is_last)
72 sha256_update((sha256_context *)ctx, buf, size);
76 static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
79 if (size < algo->digest_size)
82 sha256_finish((sha256_context *)ctx, dest_buf);
88 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
90 uint32_t *ctx = malloc(sizeof(uint32_t));
96 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
97 const void *buf, unsigned int size, int is_last)
99 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
103 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
106 if (size < algo->digest_size)
109 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
115 * These are the hash algorithms we support. Chips which support accelerated
116 * crypto could perhaps add named version of these algorithms here. Note that
117 * algorithm names must be in lower case.
119 static struct hash_algo hash_algo[] = {
121 * CONFIG_SHA_HW_ACCEL is defined if hardware acceleration is
124 #ifdef CONFIG_SHA_HW_ACCEL
130 #ifdef CONFIG_SHA_PROG_HW_ACCEL
140 #ifdef CONFIG_SHA_PROG_HW_ACCEL
180 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM)
184 #if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
188 /* Try to minimize code size for boards that don't want much hashing */
190 #define multi_hash() 1
192 #define multi_hash() 0
195 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
199 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
200 if (!strcmp(algo_name, hash_algo[i].name)) {
201 *algop = &hash_algo[i];
206 debug("Unknown hash algorithm '%s'\n", algo_name);
207 return -EPROTONOSUPPORT;
210 int hash_progressive_lookup_algo(const char *algo_name,
211 struct hash_algo **algop)
215 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
216 if (!strcmp(algo_name, hash_algo[i].name)) {
217 if (hash_algo[i].hash_init) {
218 *algop = &hash_algo[i];
224 debug("Unknown hash algorithm '%s'\n", algo_name);
225 return -EPROTONOSUPPORT;
230 * store_result: Store the resulting sum to an address or variable
232 * @algo: Hash algorithm being used
233 * @sum: Hash digest (algo->digest_size bytes)
234 * @dest: Destination, interpreted as a hex address if it starts
235 * with * (or allow_env_vars is 0) or otherwise as an
236 * environment variable.
237 * @allow_env_vars: non-zero to permit storing the result to an
238 * variable environment
240 static void store_result(struct hash_algo *algo, const uint8_t *sum,
241 const char *dest, int allow_env_vars)
247 * If environment variables are allowed, then we assume that 'dest'
248 * is an environment variable, unless it starts with *, in which
249 * case we assume it is an address. If not allowed, it is always an
250 * address. This is to support the crc32 command.
252 if (allow_env_vars) {
260 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
261 char *str_ptr = str_output;
263 for (i = 0; i < algo->digest_size; i++) {
264 sprintf(str_ptr, "%02x", sum[i]);
268 setenv(dest, str_output);
273 addr = simple_strtoul(dest, NULL, 16);
274 buf = map_sysmem(addr, algo->digest_size);
275 memcpy(buf, sum, algo->digest_size);
281 * parse_verify_sum: Parse a hash verification parameter
283 * @algo: Hash algorithm being used
284 * @verify_str: Argument to parse. If it starts with * then it is
285 * interpreted as a hex address containing the hash.
286 * If the length is exactly the right number of hex digits
287 * for the digest size, then we assume it is a hex digest.
288 * Otherwise we assume it is an environment variable, and
289 * look up its value (it must contain a hex digest).
290 * @vsum: Returns binary digest value (algo->digest_size bytes)
291 * @allow_env_vars: non-zero to permit storing the result to an environment
292 * variable. If 0 then verify_str is assumed to be an
293 * address, and the * prefix is not expected.
294 * @return 0 if ok, non-zero on error
296 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
297 uint8_t *vsum, int allow_env_vars)
301 /* See comment above in store_result() */
302 if (allow_env_vars) {
303 if (*verify_str == '*')
313 addr = simple_strtoul(verify_str, NULL, 16);
314 buf = map_sysmem(addr, algo->digest_size);
315 memcpy(vsum, buf, algo->digest_size);
319 int digits = algo->digest_size * 2;
322 * As with the original code from sha1sum.c, we assume that a
323 * string which matches the digest size exactly is a hex
324 * string and not an environment variable.
326 if (strlen(verify_str) == digits)
327 vsum_str = verify_str;
329 vsum_str = getenv(verify_str);
330 if (vsum_str == NULL || strlen(vsum_str) != digits) {
331 printf("Expected %d hex digits in env var\n",
337 for (i = 0; i < algo->digest_size; i++) {
338 char *nullp = vsum_str + (i + 1) * 2;
342 vsum[i] = simple_strtoul(vsum_str + (i * 2), NULL, 16);
349 void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
353 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
354 for (i = 0; i < algo->digest_size; i++)
355 printf("%02x", output[i]);
358 int hash_block(const char *algo_name, const void *data, unsigned int len,
359 uint8_t *output, int *output_size)
361 struct hash_algo *algo;
364 ret = hash_lookup_algo(algo_name, &algo);
368 if (output_size && *output_size < algo->digest_size) {
369 debug("Output buffer size %d too small (need %d bytes)",
370 *output_size, algo->digest_size);
374 *output_size = algo->digest_size;
375 algo->hash_func_ws(data, len, output, algo->chunk_size);
380 int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
381 int argc, char * const argv[])
385 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
386 return CMD_RET_USAGE;
388 addr = simple_strtoul(*argv++, NULL, 16);
389 len = simple_strtoul(*argv++, NULL, 16);
392 struct hash_algo *algo;
393 uint8_t output[HASH_MAX_DIGEST_SIZE];
394 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
397 if (hash_lookup_algo(algo_name, &algo)) {
398 printf("Unknown hash algorithm '%s'\n", algo_name);
399 return CMD_RET_USAGE;
403 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
404 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
408 buf = map_sysmem(addr, len);
409 algo->hash_func_ws(buf, len, output, algo->chunk_size);
412 /* Try to avoid code bloat when verify is not needed */
413 #ifdef CONFIG_HASH_VERIFY
414 if (flags & HASH_FLAG_VERIFY) {
418 if (parse_verify_sum(algo, *argv, vsum,
419 flags & HASH_FLAG_ENV)) {
420 printf("ERROR: %s does not contain a valid "
421 "%s sum\n", *argv, algo->name);
424 if (memcmp(output, vsum, algo->digest_size) != 0) {
427 hash_show(algo, addr, len, output);
429 for (i = 0; i < algo->digest_size; i++)
430 printf("%02x", vsum[i]);
431 puts(" ** ERROR **\n");
435 hash_show(algo, addr, len, output);
439 store_result(algo, output, *argv,
440 flags & HASH_FLAG_ENV);
444 /* Horrible code size hack for boards that just want crc32 */
449 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
451 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
452 addr, addr + len - 1, crc);
455 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);