1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012 The Chromium OS Authors.
6 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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>
33 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
34 DECLARE_GLOBAL_DATA_PTR;
37 static void reloc_update(void);
39 #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
40 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
42 sha1_context *ctx = malloc(sizeof(sha1_context));
48 static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
49 unsigned int size, int is_last)
51 sha1_update((sha1_context *)ctx, buf, size);
55 static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
58 if (size < algo->digest_size)
61 sha1_finish((sha1_context *)ctx, dest_buf);
67 #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
68 static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
70 sha256_context *ctx = malloc(sizeof(sha256_context));
76 static int hash_update_sha256(struct hash_algo *algo, void *ctx,
77 const void *buf, unsigned int size, int is_last)
79 sha256_update((sha256_context *)ctx, buf, size);
83 static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
86 if (size < algo->digest_size)
89 sha256_finish((sha256_context *)ctx, dest_buf);
95 static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
97 uint16_t *ctx = malloc(sizeof(uint16_t));
103 static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
104 const void *buf, unsigned int size,
107 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
111 static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
112 void *dest_buf, int size)
114 if (size < algo->digest_size)
117 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
122 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
124 uint32_t *ctx = malloc(sizeof(uint32_t));
130 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
131 const void *buf, unsigned int size, int is_last)
133 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
137 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
140 if (size < algo->digest_size)
143 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
149 * These are the hash algorithms we support. If we have hardware acceleration
150 * is enable we will use that, otherwise a software version of the algorithm.
151 * Note that algorithm names must be in lower case.
153 static struct hash_algo hash_algo[] = {
157 .digest_size = SHA1_SUM_LEN,
158 .chunk_size = CHUNKSZ_SHA1,
159 #ifdef CONFIG_SHA_HW_ACCEL
160 .hash_func_ws = hw_sha1,
162 .hash_func_ws = sha1_csum_wd,
164 #ifdef CONFIG_SHA_PROG_HW_ACCEL
165 .hash_init = hw_sha_init,
166 .hash_update = hw_sha_update,
167 .hash_finish = hw_sha_finish,
169 .hash_init = hash_init_sha1,
170 .hash_update = hash_update_sha1,
171 .hash_finish = hash_finish_sha1,
178 .digest_size = SHA256_SUM_LEN,
179 .chunk_size = CHUNKSZ_SHA256,
180 #ifdef CONFIG_SHA_HW_ACCEL
181 .hash_func_ws = hw_sha256,
183 .hash_func_ws = sha256_csum_wd,
185 #ifdef CONFIG_SHA_PROG_HW_ACCEL
186 .hash_init = hw_sha_init,
187 .hash_update = hw_sha_update,
188 .hash_finish = hw_sha_finish,
190 .hash_init = hash_init_sha256,
191 .hash_update = hash_update_sha256,
192 .hash_finish = hash_finish_sha256,
197 .name = "crc16-ccitt",
199 .chunk_size = CHUNKSZ,
200 .hash_func_ws = crc16_ccitt_wd_buf,
201 .hash_init = hash_init_crc16_ccitt,
202 .hash_update = hash_update_crc16_ccitt,
203 .hash_finish = hash_finish_crc16_ccitt,
208 .chunk_size = CHUNKSZ_CRC32,
209 .hash_func_ws = crc32_wd_buf,
210 .hash_init = hash_init_crc32,
211 .hash_update = hash_update_crc32,
212 .hash_finish = hash_finish_crc32,
216 /* Try to minimize code size for boards that don't want much hashing */
217 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
218 defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH)
219 #define multi_hash() 1
221 #define multi_hash() 0
224 static void reloc_update(void)
226 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
232 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
233 hash_algo[i].name += gd->reloc_off;
234 hash_algo[i].hash_func_ws += gd->reloc_off;
235 hash_algo[i].hash_init += gd->reloc_off;
236 hash_algo[i].hash_update += gd->reloc_off;
237 hash_algo[i].hash_finish += gd->reloc_off;
243 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
249 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
250 if (!strcmp(algo_name, hash_algo[i].name)) {
251 *algop = &hash_algo[i];
256 debug("Unknown hash algorithm '%s'\n", algo_name);
257 return -EPROTONOSUPPORT;
260 int hash_progressive_lookup_algo(const char *algo_name,
261 struct hash_algo **algop)
267 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
268 if (!strcmp(algo_name, hash_algo[i].name)) {
269 if (hash_algo[i].hash_init) {
270 *algop = &hash_algo[i];
276 debug("Unknown hash algorithm '%s'\n", algo_name);
277 return -EPROTONOSUPPORT;
281 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
283 struct hash_algo *algo;
287 ret = hash_lookup_algo(algo_name, &algo);
291 for (i = 0; i < algo->digest_size; i++) {
294 strncpy(chr, &str[i * 2], 2);
295 result[i] = simple_strtoul(chr, NULL, 16);
301 int hash_block(const char *algo_name, const void *data, unsigned int len,
302 uint8_t *output, int *output_size)
304 struct hash_algo *algo;
307 ret = hash_lookup_algo(algo_name, &algo);
311 if (output_size && *output_size < algo->digest_size) {
312 debug("Output buffer size %d too small (need %d bytes)",
313 *output_size, algo->digest_size);
317 *output_size = algo->digest_size;
318 algo->hash_func_ws(data, len, output, algo->chunk_size);
323 #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
325 * store_result: Store the resulting sum to an address or variable
327 * @algo: Hash algorithm being used
328 * @sum: Hash digest (algo->digest_size bytes)
329 * @dest: Destination, interpreted as a hex address if it starts
330 * with * (or allow_env_vars is 0) or otherwise as an
331 * environment variable.
332 * @allow_env_vars: non-zero to permit storing the result to an
333 * variable environment
335 static void store_result(struct hash_algo *algo, const uint8_t *sum,
336 const char *dest, int allow_env_vars)
342 * If environment variables are allowed, then we assume that 'dest'
343 * is an environment variable, unless it starts with *, in which
344 * case we assume it is an address. If not allowed, it is always an
345 * address. This is to support the crc32 command.
347 if (allow_env_vars) {
355 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
356 char *str_ptr = str_output;
358 for (i = 0; i < algo->digest_size; i++) {
359 sprintf(str_ptr, "%02x", sum[i]);
363 env_set(dest, str_output);
368 addr = simple_strtoul(dest, NULL, 16);
369 buf = map_sysmem(addr, algo->digest_size);
370 memcpy(buf, sum, algo->digest_size);
376 * parse_verify_sum: Parse a hash verification parameter
378 * @algo: Hash algorithm being used
379 * @verify_str: Argument to parse. If it starts with * then it is
380 * interpreted as a hex address containing the hash.
381 * If the length is exactly the right number of hex digits
382 * for the digest size, then we assume it is a hex digest.
383 * Otherwise we assume it is an environment variable, and
384 * look up its value (it must contain a hex digest).
385 * @vsum: Returns binary digest value (algo->digest_size bytes)
386 * @allow_env_vars: non-zero to permit storing the result to an environment
387 * variable. If 0 then verify_str is assumed to be an
388 * address, and the * prefix is not expected.
389 * @return 0 if ok, non-zero on error
391 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
392 uint8_t *vsum, int allow_env_vars)
396 /* See comment above in store_result() */
397 if (allow_env_vars) {
398 if (*verify_str == '*')
408 addr = simple_strtoul(verify_str, NULL, 16);
409 buf = map_sysmem(addr, algo->digest_size);
410 memcpy(vsum, buf, algo->digest_size);
413 int digits = algo->digest_size * 2;
416 * As with the original code from sha1sum.c, we assume that a
417 * string which matches the digest size exactly is a hex
418 * string and not an environment variable.
420 if (strlen(verify_str) == digits)
421 vsum_str = verify_str;
423 vsum_str = env_get(verify_str);
424 if (vsum_str == NULL || strlen(vsum_str) != digits) {
425 printf("Expected %d hex digits in env var\n",
431 hash_parse_string(algo->name, vsum_str, vsum);
436 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
440 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
441 for (i = 0; i < algo->digest_size; i++)
442 printf("%02x", output[i]);
445 int hash_command(const char *algo_name, int flags, cmd_tbl_t *cmdtp, int flag,
446 int argc, char * const argv[])
450 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
451 return CMD_RET_USAGE;
453 addr = simple_strtoul(*argv++, NULL, 16);
454 len = simple_strtoul(*argv++, NULL, 16);
457 struct hash_algo *algo;
459 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
462 if (hash_lookup_algo(algo_name, &algo)) {
463 printf("Unknown hash algorithm '%s'\n", algo_name);
464 return CMD_RET_USAGE;
468 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
469 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
473 output = memalign(ARCH_DMA_MINALIGN,
474 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
476 buf = map_sysmem(addr, len);
477 algo->hash_func_ws(buf, len, output, algo->chunk_size);
480 /* Try to avoid code bloat when verify is not needed */
481 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
482 defined(CONFIG_HASH_VERIFY)
483 if (flags & HASH_FLAG_VERIFY) {
487 if (parse_verify_sum(algo, *argv, vsum,
488 flags & HASH_FLAG_ENV)) {
489 printf("ERROR: %s does not contain a valid "
490 "%s sum\n", *argv, algo->name);
493 if (memcmp(output, vsum, algo->digest_size) != 0) {
496 hash_show(algo, addr, len, output);
498 for (i = 0; i < algo->digest_size; i++)
499 printf("%02x", vsum[i]);
500 puts(" ** ERROR **\n");
504 hash_show(algo, addr, len, output);
508 store_result(algo, output, *argv,
509 flags & HASH_FLAG_ENV);
511 unmap_sysmem(output);
515 /* Horrible code size hack for boards that just want crc32 */
520 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
522 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
523 addr, addr + len - 1, crc);
526 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
533 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
534 #endif /* !USE_HOSTCC */