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>
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
161 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM)
165 #if defined(CONFIG_HASH_VERIFY) || defined(CONFIG_CMD_HASH)
169 /* Try to minimize code size for boards that don't want much hashing */
171 #define multi_hash() 1
173 #define multi_hash() 0
177 * store_result: Store the resulting sum to an address or variable
179 * @algo: Hash algorithm being used
180 * @sum: Hash digest (algo->digest_size bytes)
181 * @dest: Destination, interpreted as a hex address if it starts
182 * with * (or allow_env_vars is 0) or otherwise as an
183 * environment variable.
184 * @allow_env_vars: non-zero to permit storing the result to an
185 * variable environment
187 static void store_result(struct hash_algo *algo, const uint8_t *sum,
188 const char *dest, int allow_env_vars)
194 * If environment variables are allowed, then we assume that 'dest'
195 * is an environment variable, unless it starts with *, in which
196 * case we assume it is an address. If not allowed, it is always an
197 * address. This is to support the crc32 command.
199 if (allow_env_vars) {
207 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
208 char *str_ptr = str_output;
210 for (i = 0; i < algo->digest_size; i++) {
211 sprintf(str_ptr, "%02x", sum[i]);
215 setenv(dest, str_output);
220 addr = simple_strtoul(dest, NULL, 16);
221 buf = map_sysmem(addr, algo->digest_size);
222 memcpy(buf, sum, algo->digest_size);
228 * parse_verify_sum: Parse a hash verification parameter
230 * @algo: Hash algorithm being used
231 * @verify_str: Argument to parse. If it starts with * then it is
232 * interpreted as a hex address containing the hash.
233 * If the length is exactly the right number of hex digits
234 * for the digest size, then we assume it is a hex digest.
235 * Otherwise we assume it is an environment variable, and
236 * look up its value (it must contain a hex digest).
237 * @vsum: Returns binary digest value (algo->digest_size bytes)
238 * @allow_env_vars: non-zero to permit storing the result to an environment
239 * variable. If 0 then verify_str is assumed to be an
240 * address, and the * prefix is not expected.
241 * @return 0 if ok, non-zero on error
243 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
244 uint8_t *vsum, int allow_env_vars)
248 /* See comment above in store_result() */
249 if (allow_env_vars) {
250 if (*verify_str == '*')
260 addr = simple_strtoul(verify_str, NULL, 16);
261 buf = map_sysmem(addr, algo->digest_size);
262 memcpy(vsum, buf, algo->digest_size);
266 int digits = algo->digest_size * 2;
269 * As with the original code from sha1sum.c, we assume that a
270 * string which matches the digest size exactly is a hex
271 * string and not an environment variable.
273 if (strlen(verify_str) == digits)
274 vsum_str = verify_str;
276 vsum_str = getenv(verify_str);
277 if (vsum_str == NULL || strlen(vsum_str) != digits) {
278 printf("Expected %d hex digits in env var\n",
284 for (i = 0; i < algo->digest_size; i++) {
285 char *nullp = vsum_str + (i + 1) * 2;
289 vsum[i] = simple_strtoul(vsum_str + (i * 2), NULL, 16);
296 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
300 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
301 if (!strcmp(algo_name, hash_algo[i].name)) {
302 *algop = &hash_algo[i];
307 debug("Unknown hash algorithm '%s'\n", algo_name);
308 return -EPROTONOSUPPORT;
311 int hash_progressive_lookup_algo(const char *algo_name,
312 struct hash_algo **algop)
316 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
317 if (!strcmp(algo_name, hash_algo[i].name)) {
318 if (hash_algo[i].hash_init) {
319 *algop = &hash_algo[i];
325 debug("Unknown hash algorithm '%s'\n", algo_name);
326 return -EPROTONOSUPPORT;
329 void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
333 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
334 for (i = 0; i < algo->digest_size; i++)
335 printf("%02x", output[i]);
338 int hash_block(const char *algo_name, const void *data, unsigned int len,
339 uint8_t *output, int *output_size)
341 struct hash_algo *algo;
344 ret = hash_lookup_algo(algo_name, &algo);
348 if (output_size && *output_size < algo->digest_size) {
349 debug("Output buffer size %d too small (need %d bytes)",
350 *output_size, algo->digest_size);
354 *output_size = algo->digest_size;
355 algo->hash_func_ws(data, len, output, algo->chunk_size);
360 int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
361 int argc, char * const argv[])
365 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
366 return CMD_RET_USAGE;
368 addr = simple_strtoul(*argv++, NULL, 16);
369 len = simple_strtoul(*argv++, NULL, 16);
372 struct hash_algo *algo;
373 uint8_t output[HASH_MAX_DIGEST_SIZE];
374 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
377 if (hash_lookup_algo(algo_name, &algo)) {
378 printf("Unknown hash algorithm '%s'\n", algo_name);
379 return CMD_RET_USAGE;
383 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
384 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
388 buf = map_sysmem(addr, len);
389 algo->hash_func_ws(buf, len, output, algo->chunk_size);
392 /* Try to avoid code bloat when verify is not needed */
393 #ifdef CONFIG_HASH_VERIFY
394 if (flags & HASH_FLAG_VERIFY) {
398 if (parse_verify_sum(algo, *argv, vsum,
399 flags & HASH_FLAG_ENV)) {
400 printf("ERROR: %s does not contain a valid "
401 "%s sum\n", *argv, algo->name);
404 if (memcmp(output, vsum, algo->digest_size) != 0) {
407 hash_show(algo, addr, len, output);
409 for (i = 0; i < algo->digest_size; i++)
410 printf("%02x", vsum[i]);
411 puts(" ** ERROR **\n");
415 hash_show(algo, addr, len, output);
419 store_result(algo, output, *argv,
420 flags & HASH_FLAG_ENV);
424 /* Horrible code size hack for boards that just want crc32 */
429 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
431 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
432 addr, addr + len - 1, crc);
435 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);