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+
18 #include <u-boot/sha1.h>
19 #include <u-boot/sha256.h>
21 #include <asm/errno.h>
23 #ifdef CONFIG_CMD_SHA1SUM
24 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
26 sha1_context *ctx = malloc(sizeof(sha1_context));
32 static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
33 unsigned int size, int is_last)
35 sha1_update((sha1_context *)ctx, buf, size);
39 static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
42 if (size < algo->digest_size)
45 sha1_finish((sha1_context *)ctx, dest_buf);
52 static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
54 sha256_context *ctx = malloc(sizeof(sha256_context));
60 static int hash_update_sha256(struct hash_algo *algo, void *ctx,
61 const void *buf, unsigned int size, int is_last)
63 sha256_update((sha256_context *)ctx, buf, size);
67 static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
70 if (size < algo->digest_size)
73 sha256_finish((sha256_context *)ctx, dest_buf);
79 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
81 uint32_t *ctx = malloc(sizeof(uint32_t));
87 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
88 const void *buf, unsigned int size, int is_last)
90 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
94 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
97 if (size < algo->digest_size)
100 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
106 * These are the hash algorithms we support. Chips which support accelerated
107 * crypto could perhaps add named version of these algorithms here. Note that
108 * algorithm names must be in lower case.
110 static struct hash_algo hash_algo[] = {
112 * CONFIG_SHA_HW_ACCEL is defined if hardware acceleration is
115 #ifdef CONFIG_SHA_HW_ACCEL
129 * This is CONFIG_CMD_SHA1SUM instead of CONFIG_SHA1 since otherwise
130 * it bloats the code for boards which use SHA1 but not the 'hash'
131 * or 'sha1sum' commands.
133 #ifdef CONFIG_CMD_SHA1SUM
168 #if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
172 /* Try to minimize code size for boards that don't want much hashing */
174 #define multi_hash() 1
176 #define multi_hash() 0
180 * store_result: Store the resulting sum to an address or variable
182 * @algo: Hash algorithm being used
183 * @sum: Hash digest (algo->digest_size bytes)
184 * @dest: Destination, interpreted as a hex address if it starts
185 * with * (or allow_env_vars is 0) or otherwise as an
186 * environment variable.
187 * @allow_env_vars: non-zero to permit storing the result to an
188 * variable environment
190 static void store_result(struct hash_algo *algo, const uint8_t *sum,
191 const char *dest, int allow_env_vars)
197 * If environment variables are allowed, then we assume that 'dest'
198 * is an environment variable, unless it starts with *, in which
199 * case we assume it is an address. If not allowed, it is always an
200 * address. This is to support the crc32 command.
202 if (allow_env_vars) {
210 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
211 char *str_ptr = str_output;
213 for (i = 0; i < algo->digest_size; i++) {
214 sprintf(str_ptr, "%02x", sum[i]);
218 setenv(dest, str_output);
223 addr = simple_strtoul(dest, NULL, 16);
224 buf = map_sysmem(addr, algo->digest_size);
225 memcpy(buf, sum, algo->digest_size);
231 * parse_verify_sum: Parse a hash verification parameter
233 * @algo: Hash algorithm being used
234 * @verify_str: Argument to parse. If it starts with * then it is
235 * interpreted as a hex address containing the hash.
236 * If the length is exactly the right number of hex digits
237 * for the digest size, then we assume it is a hex digest.
238 * Otherwise we assume it is an environment variable, and
239 * look up its value (it must contain a hex digest).
240 * @vsum: Returns binary digest value (algo->digest_size bytes)
241 * @allow_env_vars: non-zero to permit storing the result to an environment
242 * variable. If 0 then verify_str is assumed to be an
243 * address, and the * prefix is not expected.
244 * @return 0 if ok, non-zero on error
246 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
247 uint8_t *vsum, int allow_env_vars)
251 /* See comment above in store_result() */
252 if (allow_env_vars) {
253 if (*verify_str == '*')
263 addr = simple_strtoul(verify_str, NULL, 16);
264 buf = map_sysmem(addr, algo->digest_size);
265 memcpy(vsum, buf, algo->digest_size);
269 int digits = algo->digest_size * 2;
272 * As with the original code from sha1sum.c, we assume that a
273 * string which matches the digest size exactly is a hex
274 * string and not an environment variable.
276 if (strlen(verify_str) == digits)
277 vsum_str = verify_str;
279 vsum_str = getenv(verify_str);
280 if (vsum_str == NULL || strlen(vsum_str) != digits) {
281 printf("Expected %d hex digits in env var\n",
287 for (i = 0; i < algo->digest_size; i++) {
288 char *nullp = vsum_str + (i + 1) * 2;
292 vsum[i] = simple_strtoul(vsum_str + (i * 2), NULL, 16);
299 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
303 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
304 if (!strcmp(algo_name, hash_algo[i].name)) {
305 *algop = &hash_algo[i];
310 debug("Unknown hash algorithm '%s'\n", algo_name);
311 return -EPROTONOSUPPORT;
314 void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
318 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
319 for (i = 0; i < algo->digest_size; i++)
320 printf("%02x", output[i]);
323 int hash_block(const char *algo_name, const void *data, unsigned int len,
324 uint8_t *output, int *output_size)
326 struct hash_algo *algo;
329 ret = hash_lookup_algo(algo_name, &algo);
333 if (output_size && *output_size < algo->digest_size) {
334 debug("Output buffer size %d too small (need %d bytes)",
335 *output_size, algo->digest_size);
339 *output_size = algo->digest_size;
340 algo->hash_func_ws(data, len, output, algo->chunk_size);
345 int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
346 int argc, char * const argv[])
351 return CMD_RET_USAGE;
353 addr = simple_strtoul(*argv++, NULL, 16);
354 len = simple_strtoul(*argv++, NULL, 16);
357 struct hash_algo *algo;
358 uint8_t output[HASH_MAX_DIGEST_SIZE];
359 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
362 if (hash_lookup_algo(algo_name, &algo)) {
363 printf("Unknown hash algorithm '%s'\n", algo_name);
364 return CMD_RET_USAGE;
368 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
369 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
373 buf = map_sysmem(addr, len);
374 algo->hash_func_ws(buf, len, output, algo->chunk_size);
377 /* Try to avoid code bloat when verify is not needed */
378 #ifdef CONFIG_HASH_VERIFY
379 if (flags & HASH_FLAG_VERIFY) {
384 return CMD_RET_USAGE;
385 if (parse_verify_sum(algo, *argv, vsum,
386 flags & HASH_FLAG_ENV)) {
387 printf("ERROR: %s does not contain a valid "
388 "%s sum\n", *argv, algo->name);
391 if (memcmp(output, vsum, algo->digest_size) != 0) {
394 hash_show(algo, addr, len, output);
396 for (i = 0; i < algo->digest_size; i++)
397 printf("%02x", vsum[i]);
398 puts(" ** ERROR **\n");
402 hash_show(algo, addr, len, output);
406 store_result(algo, output, *argv,
407 flags & HASH_FLAG_ENV);
411 /* Horrible code size hack for boards that just want crc32 */
416 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
418 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
419 addr, addr + len - 1, crc);
422 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);