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 <asm/cache.h>
22 #include <linux/errno.h>
23 #include <u-boot/crc.h>
27 #endif /* !USE_HOSTCC*/
31 #include <u-boot/crc.h>
32 #include <u-boot/sha1.h>
33 #include <u-boot/sha256.h>
34 #include <u-boot/md5.h>
36 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
37 DECLARE_GLOBAL_DATA_PTR;
40 static void reloc_update(void);
42 #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
43 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
45 sha1_context *ctx = malloc(sizeof(sha1_context));
51 static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
52 unsigned int size, int is_last)
54 sha1_update((sha1_context *)ctx, buf, size);
58 static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
61 if (size < algo->digest_size)
64 sha1_finish((sha1_context *)ctx, dest_buf);
70 #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
71 static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
73 sha256_context *ctx = malloc(sizeof(sha256_context));
79 static int hash_update_sha256(struct hash_algo *algo, void *ctx,
80 const void *buf, unsigned int size, int is_last)
82 sha256_update((sha256_context *)ctx, buf, size);
86 static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
89 if (size < algo->digest_size)
92 sha256_finish((sha256_context *)ctx, dest_buf);
98 static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
100 uint16_t *ctx = malloc(sizeof(uint16_t));
106 static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
107 const void *buf, unsigned int size,
110 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
114 static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
115 void *dest_buf, int size)
117 if (size < algo->digest_size)
120 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
125 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
127 uint32_t *ctx = malloc(sizeof(uint32_t));
133 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
134 const void *buf, unsigned int size, int is_last)
136 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
140 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
143 if (size < algo->digest_size)
146 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
152 * These are the hash algorithms we support. If we have hardware acceleration
153 * is enable we will use that, otherwise a software version of the algorithm.
154 * Note that algorithm names must be in lower case.
156 static struct hash_algo hash_algo[] = {
160 .digest_size = SHA1_SUM_LEN,
161 .chunk_size = CHUNKSZ_SHA1,
162 #ifdef CONFIG_SHA_HW_ACCEL
163 .hash_func_ws = hw_sha1,
165 .hash_func_ws = sha1_csum_wd,
167 #ifdef CONFIG_SHA_PROG_HW_ACCEL
168 .hash_init = hw_sha_init,
169 .hash_update = hw_sha_update,
170 .hash_finish = hw_sha_finish,
172 .hash_init = hash_init_sha1,
173 .hash_update = hash_update_sha1,
174 .hash_finish = hash_finish_sha1,
181 .digest_size = SHA256_SUM_LEN,
182 .chunk_size = CHUNKSZ_SHA256,
183 #ifdef CONFIG_SHA_HW_ACCEL
184 .hash_func_ws = hw_sha256,
186 .hash_func_ws = sha256_csum_wd,
188 #ifdef CONFIG_SHA_PROG_HW_ACCEL
189 .hash_init = hw_sha_init,
190 .hash_update = hw_sha_update,
191 .hash_finish = hw_sha_finish,
193 .hash_init = hash_init_sha256,
194 .hash_update = hash_update_sha256,
195 .hash_finish = hash_finish_sha256,
200 .name = "crc16-ccitt",
202 .chunk_size = CHUNKSZ,
203 .hash_func_ws = crc16_ccitt_wd_buf,
204 .hash_init = hash_init_crc16_ccitt,
205 .hash_update = hash_update_crc16_ccitt,
206 .hash_finish = hash_finish_crc16_ccitt,
211 .chunk_size = CHUNKSZ_CRC32,
212 .hash_func_ws = crc32_wd_buf,
213 .hash_init = hash_init_crc32,
214 .hash_update = hash_update_crc32,
215 .hash_finish = hash_finish_crc32,
219 /* Try to minimize code size for boards that don't want much hashing */
220 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
221 defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH)
222 #define multi_hash() 1
224 #define multi_hash() 0
227 static void reloc_update(void)
229 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
235 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
236 hash_algo[i].name += gd->reloc_off;
237 hash_algo[i].hash_func_ws += gd->reloc_off;
238 hash_algo[i].hash_init += gd->reloc_off;
239 hash_algo[i].hash_update += gd->reloc_off;
240 hash_algo[i].hash_finish += gd->reloc_off;
246 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
252 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
253 if (!strcmp(algo_name, hash_algo[i].name)) {
254 *algop = &hash_algo[i];
259 debug("Unknown hash algorithm '%s'\n", algo_name);
260 return -EPROTONOSUPPORT;
263 int hash_progressive_lookup_algo(const char *algo_name,
264 struct hash_algo **algop)
270 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
271 if (!strcmp(algo_name, hash_algo[i].name)) {
272 if (hash_algo[i].hash_init) {
273 *algop = &hash_algo[i];
279 debug("Unknown hash algorithm '%s'\n", algo_name);
280 return -EPROTONOSUPPORT;
284 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
286 struct hash_algo *algo;
290 ret = hash_lookup_algo(algo_name, &algo);
294 for (i = 0; i < algo->digest_size; i++) {
297 strncpy(chr, &str[i * 2], 2);
298 result[i] = simple_strtoul(chr, NULL, 16);
304 int hash_block(const char *algo_name, const void *data, unsigned int len,
305 uint8_t *output, int *output_size)
307 struct hash_algo *algo;
310 ret = hash_lookup_algo(algo_name, &algo);
314 if (output_size && *output_size < algo->digest_size) {
315 debug("Output buffer size %d too small (need %d bytes)",
316 *output_size, algo->digest_size);
320 *output_size = algo->digest_size;
321 algo->hash_func_ws(data, len, output, algo->chunk_size);
326 #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
328 * store_result: Store the resulting sum to an address or variable
330 * @algo: Hash algorithm being used
331 * @sum: Hash digest (algo->digest_size bytes)
332 * @dest: Destination, interpreted as a hex address if it starts
333 * with * (or allow_env_vars is 0) or otherwise as an
334 * environment variable.
335 * @allow_env_vars: non-zero to permit storing the result to an
336 * variable environment
338 static void store_result(struct hash_algo *algo, const uint8_t *sum,
339 const char *dest, int allow_env_vars)
345 * If environment variables are allowed, then we assume that 'dest'
346 * is an environment variable, unless it starts with *, in which
347 * case we assume it is an address. If not allowed, it is always an
348 * address. This is to support the crc32 command.
350 if (allow_env_vars) {
358 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
359 char *str_ptr = str_output;
361 for (i = 0; i < algo->digest_size; i++) {
362 sprintf(str_ptr, "%02x", sum[i]);
366 env_set(dest, str_output);
371 addr = simple_strtoul(dest, NULL, 16);
372 buf = map_sysmem(addr, algo->digest_size);
373 memcpy(buf, sum, algo->digest_size);
379 * parse_verify_sum: Parse a hash verification parameter
381 * @algo: Hash algorithm being used
382 * @verify_str: Argument to parse. If it starts with * then it is
383 * interpreted as a hex address containing the hash.
384 * If the length is exactly the right number of hex digits
385 * for the digest size, then we assume it is a hex digest.
386 * Otherwise we assume it is an environment variable, and
387 * look up its value (it must contain a hex digest).
388 * @vsum: Returns binary digest value (algo->digest_size bytes)
389 * @allow_env_vars: non-zero to permit storing the result to an environment
390 * variable. If 0 then verify_str is assumed to be an
391 * address, and the * prefix is not expected.
392 * @return 0 if ok, non-zero on error
394 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
395 uint8_t *vsum, int allow_env_vars)
399 /* See comment above in store_result() */
400 if (allow_env_vars) {
401 if (*verify_str == '*')
411 addr = simple_strtoul(verify_str, NULL, 16);
412 buf = map_sysmem(addr, algo->digest_size);
413 memcpy(vsum, buf, algo->digest_size);
416 int digits = algo->digest_size * 2;
419 * As with the original code from sha1sum.c, we assume that a
420 * string which matches the digest size exactly is a hex
421 * string and not an environment variable.
423 if (strlen(verify_str) == digits)
424 vsum_str = verify_str;
426 vsum_str = env_get(verify_str);
427 if (vsum_str == NULL || strlen(vsum_str) != digits) {
428 printf("Expected %d hex digits in env var\n",
434 hash_parse_string(algo->name, vsum_str, vsum);
439 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
443 printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
444 for (i = 0; i < algo->digest_size; i++)
445 printf("%02x", output[i]);
448 int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
449 int flag, int argc, char *const argv[])
453 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
454 return CMD_RET_USAGE;
456 addr = simple_strtoul(*argv++, NULL, 16);
457 len = simple_strtoul(*argv++, NULL, 16);
460 struct hash_algo *algo;
462 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
465 if (hash_lookup_algo(algo_name, &algo)) {
466 printf("Unknown hash algorithm '%s'\n", algo_name);
467 return CMD_RET_USAGE;
471 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
472 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
476 output = memalign(ARCH_DMA_MINALIGN,
477 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
479 buf = map_sysmem(addr, len);
480 algo->hash_func_ws(buf, len, output, algo->chunk_size);
483 /* Try to avoid code bloat when verify is not needed */
484 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
485 defined(CONFIG_HASH_VERIFY)
486 if (flags & HASH_FLAG_VERIFY) {
490 if (parse_verify_sum(algo, *argv, vsum,
491 flags & HASH_FLAG_ENV)) {
492 printf("ERROR: %s does not contain a valid "
493 "%s sum\n", *argv, algo->name);
496 if (memcmp(output, vsum, algo->digest_size) != 0) {
499 hash_show(algo, addr, len, output);
501 for (i = 0; i < algo->digest_size; i++)
502 printf("%02x", vsum[i]);
503 puts(" ** ERROR **\n");
507 hash_show(algo, addr, len, output);
511 store_result(algo, output, *argv,
512 flags & HASH_FLAG_ENV);
514 unmap_sysmem(output);
518 /* Horrible code size hack for boards that just want crc32 */
523 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
525 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
526 addr, addr + len - 1, crc);
529 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
536 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
537 #endif /* !USE_HOSTCC */