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.
19 #include <asm/cache.h>
21 #include <linux/errno.h>
22 #include <u-boot/crc.h>
26 #endif /* !USE_HOSTCC*/
30 #include <u-boot/crc.h>
31 #include <u-boot/sha1.h>
32 #include <u-boot/sha256.h>
33 #include <u-boot/md5.h>
35 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
36 DECLARE_GLOBAL_DATA_PTR;
39 static void reloc_update(void);
41 #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
42 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
44 sha1_context *ctx = malloc(sizeof(sha1_context));
50 static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
51 unsigned int size, int is_last)
53 sha1_update((sha1_context *)ctx, buf, size);
57 static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
60 if (size < algo->digest_size)
63 sha1_finish((sha1_context *)ctx, dest_buf);
69 #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
70 static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
72 sha256_context *ctx = malloc(sizeof(sha256_context));
78 static int hash_update_sha256(struct hash_algo *algo, void *ctx,
79 const void *buf, unsigned int size, int is_last)
81 sha256_update((sha256_context *)ctx, buf, size);
85 static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
88 if (size < algo->digest_size)
91 sha256_finish((sha256_context *)ctx, dest_buf);
97 static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
99 uint16_t *ctx = malloc(sizeof(uint16_t));
105 static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
106 const void *buf, unsigned int size,
109 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
113 static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
114 void *dest_buf, int size)
116 if (size < algo->digest_size)
119 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
124 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
126 uint32_t *ctx = malloc(sizeof(uint32_t));
132 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
133 const void *buf, unsigned int size, int is_last)
135 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
139 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
142 if (size < algo->digest_size)
145 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
151 * These are the hash algorithms we support. If we have hardware acceleration
152 * is enable we will use that, otherwise a software version of the algorithm.
153 * Note that algorithm names must be in lower case.
155 static struct hash_algo hash_algo[] = {
159 .digest_size = SHA1_SUM_LEN,
160 .chunk_size = CHUNKSZ_SHA1,
161 #ifdef CONFIG_SHA_HW_ACCEL
162 .hash_func_ws = hw_sha1,
164 .hash_func_ws = sha1_csum_wd,
166 #ifdef CONFIG_SHA_PROG_HW_ACCEL
167 .hash_init = hw_sha_init,
168 .hash_update = hw_sha_update,
169 .hash_finish = hw_sha_finish,
171 .hash_init = hash_init_sha1,
172 .hash_update = hash_update_sha1,
173 .hash_finish = hash_finish_sha1,
180 .digest_size = SHA256_SUM_LEN,
181 .chunk_size = CHUNKSZ_SHA256,
182 #ifdef CONFIG_SHA_HW_ACCEL
183 .hash_func_ws = hw_sha256,
185 .hash_func_ws = sha256_csum_wd,
187 #ifdef CONFIG_SHA_PROG_HW_ACCEL
188 .hash_init = hw_sha_init,
189 .hash_update = hw_sha_update,
190 .hash_finish = hw_sha_finish,
192 .hash_init = hash_init_sha256,
193 .hash_update = hash_update_sha256,
194 .hash_finish = hash_finish_sha256,
199 .name = "crc16-ccitt",
201 .chunk_size = CHUNKSZ,
202 .hash_func_ws = crc16_ccitt_wd_buf,
203 .hash_init = hash_init_crc16_ccitt,
204 .hash_update = hash_update_crc16_ccitt,
205 .hash_finish = hash_finish_crc16_ccitt,
210 .chunk_size = CHUNKSZ_CRC32,
211 .hash_func_ws = crc32_wd_buf,
212 .hash_init = hash_init_crc32,
213 .hash_update = hash_update_crc32,
214 .hash_finish = hash_finish_crc32,
218 /* Try to minimize code size for boards that don't want much hashing */
219 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
220 defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH)
221 #define multi_hash() 1
223 #define multi_hash() 0
226 static void reloc_update(void)
228 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
234 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
235 hash_algo[i].name += gd->reloc_off;
236 hash_algo[i].hash_func_ws += gd->reloc_off;
237 hash_algo[i].hash_init += gd->reloc_off;
238 hash_algo[i].hash_update += gd->reloc_off;
239 hash_algo[i].hash_finish += gd->reloc_off;
245 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
251 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
252 if (!strcmp(algo_name, hash_algo[i].name)) {
253 *algop = &hash_algo[i];
258 debug("Unknown hash algorithm '%s'\n", algo_name);
259 return -EPROTONOSUPPORT;
262 int hash_progressive_lookup_algo(const char *algo_name,
263 struct hash_algo **algop)
269 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
270 if (!strcmp(algo_name, hash_algo[i].name)) {
271 if (hash_algo[i].hash_init) {
272 *algop = &hash_algo[i];
278 debug("Unknown hash algorithm '%s'\n", algo_name);
279 return -EPROTONOSUPPORT;
283 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
285 struct hash_algo *algo;
289 ret = hash_lookup_algo(algo_name, &algo);
293 for (i = 0; i < algo->digest_size; i++) {
296 strncpy(chr, &str[i * 2], 2);
297 result[i] = simple_strtoul(chr, NULL, 16);
303 int hash_block(const char *algo_name, const void *data, unsigned int len,
304 uint8_t *output, int *output_size)
306 struct hash_algo *algo;
309 ret = hash_lookup_algo(algo_name, &algo);
313 if (output_size && *output_size < algo->digest_size) {
314 debug("Output buffer size %d too small (need %d bytes)",
315 *output_size, algo->digest_size);
319 *output_size = algo->digest_size;
320 algo->hash_func_ws(data, len, output, algo->chunk_size);
325 #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
327 * store_result: Store the resulting sum to an address or variable
329 * @algo: Hash algorithm being used
330 * @sum: Hash digest (algo->digest_size bytes)
331 * @dest: Destination, interpreted as a hex address if it starts
332 * with * (or allow_env_vars is 0) or otherwise as an
333 * environment variable.
334 * @allow_env_vars: non-zero to permit storing the result to an
335 * variable environment
337 static void store_result(struct hash_algo *algo, const uint8_t *sum,
338 const char *dest, int allow_env_vars)
344 * If environment variables are allowed, then we assume that 'dest'
345 * is an environment variable, unless it starts with *, in which
346 * case we assume it is an address. If not allowed, it is always an
347 * address. This is to support the crc32 command.
349 if (allow_env_vars) {
357 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
358 char *str_ptr = str_output;
360 for (i = 0; i < algo->digest_size; i++) {
361 sprintf(str_ptr, "%02x", sum[i]);
365 env_set(dest, str_output);
370 addr = simple_strtoul(dest, NULL, 16);
371 buf = map_sysmem(addr, algo->digest_size);
372 memcpy(buf, sum, algo->digest_size);
378 * parse_verify_sum: Parse a hash verification parameter
380 * @algo: Hash algorithm being used
381 * @verify_str: Argument to parse. If it starts with * then it is
382 * interpreted as a hex address containing the hash.
383 * If the length is exactly the right number of hex digits
384 * for the digest size, then we assume it is a hex digest.
385 * Otherwise we assume it is an environment variable, and
386 * look up its value (it must contain a hex digest).
387 * @vsum: Returns binary digest value (algo->digest_size bytes)
388 * @allow_env_vars: non-zero to permit storing the result to an environment
389 * variable. If 0 then verify_str is assumed to be an
390 * address, and the * prefix is not expected.
391 * @return 0 if ok, non-zero on error
393 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
394 uint8_t *vsum, int allow_env_vars)
398 /* See comment above in store_result() */
399 if (allow_env_vars) {
400 if (*verify_str == '*')
410 addr = simple_strtoul(verify_str, NULL, 16);
411 buf = map_sysmem(addr, algo->digest_size);
412 memcpy(vsum, buf, algo->digest_size);
415 int digits = algo->digest_size * 2;
418 * As with the original code from sha1sum.c, we assume that a
419 * string which matches the digest size exactly is a hex
420 * string and not an environment variable.
422 if (strlen(verify_str) == digits)
423 vsum_str = verify_str;
425 vsum_str = env_get(verify_str);
426 if (vsum_str == NULL || strlen(vsum_str) != digits) {
427 printf("Expected %d hex digits in env var\n",
433 hash_parse_string(algo->name, vsum_str, vsum);
438 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
442 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
443 for (i = 0; i < algo->digest_size; i++)
444 printf("%02x", output[i]);
447 int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
448 int flag, int argc, char *const argv[])
452 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
453 return CMD_RET_USAGE;
455 addr = simple_strtoul(*argv++, NULL, 16);
456 len = simple_strtoul(*argv++, NULL, 16);
459 struct hash_algo *algo;
461 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
464 if (hash_lookup_algo(algo_name, &algo)) {
465 printf("Unknown hash algorithm '%s'\n", algo_name);
466 return CMD_RET_USAGE;
470 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
471 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
475 output = memalign(ARCH_DMA_MINALIGN,
476 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
478 buf = map_sysmem(addr, len);
479 algo->hash_func_ws(buf, len, output, algo->chunk_size);
482 /* Try to avoid code bloat when verify is not needed */
483 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
484 defined(CONFIG_HASH_VERIFY)
485 if (flags & HASH_FLAG_VERIFY) {
489 if (parse_verify_sum(algo, *argv, vsum,
490 flags & HASH_FLAG_ENV)) {
491 printf("ERROR: %s does not contain a valid "
492 "%s sum\n", *argv, algo->name);
495 if (memcmp(output, vsum, algo->digest_size) != 0) {
498 hash_show(algo, addr, len, output);
500 for (i = 0; i < algo->digest_size; i++)
501 printf("%02x", vsum[i]);
502 puts(" ** ERROR **\n");
506 hash_show(algo, addr, len, output);
510 store_result(algo, output, *argv,
511 flags & HASH_FLAG_ENV);
513 unmap_sysmem(output);
517 /* Horrible code size hack for boards that just want crc32 */
522 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
524 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
525 addr, addr + len - 1, crc);
528 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
535 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
536 #endif /* !USE_HOSTCC */