]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/hash.c
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+
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(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
34 static int hash_init_sha1(struct hash_algo
*algo
, void **ctxp
)
36 sha1_context
*ctx
= malloc(sizeof(sha1_context
));
42 static int hash_update_sha1(struct hash_algo
*algo
, void *ctx
, const void *buf
,
43 unsigned int size
, int is_last
)
45 sha1_update((sha1_context
*)ctx
, buf
, size
);
49 static int hash_finish_sha1(struct hash_algo
*algo
, void *ctx
, void *dest_buf
,
52 if (size
< algo
->digest_size
)
55 sha1_finish((sha1_context
*)ctx
, dest_buf
);
61 #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
62 static int hash_init_sha256(struct hash_algo
*algo
, void **ctxp
)
64 sha256_context
*ctx
= malloc(sizeof(sha256_context
));
70 static int hash_update_sha256(struct hash_algo
*algo
, void *ctx
,
71 const void *buf
, unsigned int size
, int is_last
)
73 sha256_update((sha256_context
*)ctx
, buf
, size
);
77 static int hash_finish_sha256(struct hash_algo
*algo
, void *ctx
, void
80 if (size
< algo
->digest_size
)
83 sha256_finish((sha256_context
*)ctx
, dest_buf
);
89 static int hash_init_crc32(struct hash_algo
*algo
, void **ctxp
)
91 uint32_t *ctx
= malloc(sizeof(uint32_t));
97 static int hash_update_crc32(struct hash_algo
*algo
, void *ctx
,
98 const void *buf
, unsigned int size
, int is_last
)
100 *((uint32_t *)ctx
) = crc32(*((uint32_t *)ctx
), buf
, size
);
104 static int hash_finish_crc32(struct hash_algo
*algo
, void *ctx
, void *dest_buf
,
107 if (size
< algo
->digest_size
)
110 *((uint32_t *)dest_buf
) = *((uint32_t *)ctx
);
116 * These are the hash algorithms we support. If we have hardware acceleration
117 * is enable we will use that, otherwise a software version of the algorithm.
118 * Note that algorithm names must be in lower case.
120 static struct hash_algo hash_algo
[] = {
124 .digest_size
= SHA1_SUM_LEN
,
125 .chunk_size
= CHUNKSZ_SHA1
,
126 #ifdef CONFIG_SHA_HW_ACCEL
127 .hash_func_ws
= hw_sha1
,
129 .hash_func_ws
= sha1_csum_wd
,
131 #ifdef CONFIG_SHA_PROG_HW_ACCEL
132 .hash_init
= hw_sha_init
,
133 .hash_update
= hw_sha_update
,
134 .hash_finish
= hw_sha_finish
,
136 .hash_init
= hash_init_sha1
,
137 .hash_update
= hash_update_sha1
,
138 .hash_finish
= hash_finish_sha1
,
145 .digest_size
= SHA256_SUM_LEN
,
146 .chunk_size
= CHUNKSZ_SHA256
,
147 #ifdef CONFIG_SHA_HW_ACCEL
148 .hash_func_ws
= hw_sha256
,
150 .hash_func_ws
= sha256_csum_wd
,
152 #ifdef CONFIG_SHA_PROG_HW_ACCEL
153 .hash_init
= hw_sha_init
,
154 .hash_update
= hw_sha_update
,
155 .hash_finish
= hw_sha_finish
,
157 .hash_init
= hash_init_sha256
,
158 .hash_update
= hash_update_sha256
,
159 .hash_finish
= hash_finish_sha256
,
166 .chunk_size
= CHUNKSZ_CRC32
,
167 .hash_func_ws
= crc32_wd_buf
,
168 .hash_init
= hash_init_crc32
,
169 .hash_update
= hash_update_crc32
,
170 .hash_finish
= hash_finish_crc32
,
174 /* Try to minimize code size for boards that don't want much hashing */
175 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
176 defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH)
177 #define multi_hash() 1
179 #define multi_hash() 0
182 int hash_lookup_algo(const char *algo_name
, struct hash_algo
**algop
)
186 for (i
= 0; i
< ARRAY_SIZE(hash_algo
); i
++) {
187 if (!strcmp(algo_name
, hash_algo
[i
].name
)) {
188 *algop
= &hash_algo
[i
];
193 debug("Unknown hash algorithm '%s'\n", algo_name
);
194 return -EPROTONOSUPPORT
;
197 int hash_progressive_lookup_algo(const char *algo_name
,
198 struct hash_algo
**algop
)
202 for (i
= 0; i
< ARRAY_SIZE(hash_algo
); i
++) {
203 if (!strcmp(algo_name
, hash_algo
[i
].name
)) {
204 if (hash_algo
[i
].hash_init
) {
205 *algop
= &hash_algo
[i
];
211 debug("Unknown hash algorithm '%s'\n", algo_name
);
212 return -EPROTONOSUPPORT
;
216 int hash_parse_string(const char *algo_name
, const char *str
, uint8_t *result
)
218 struct hash_algo
*algo
;
222 ret
= hash_lookup_algo(algo_name
, &algo
);
226 for (i
= 0; i
< algo
->digest_size
; i
++) {
229 strncpy(chr
, &str
[i
* 2], 2);
230 result
[i
] = simple_strtoul(chr
, NULL
, 16);
236 int hash_block(const char *algo_name
, const void *data
, unsigned int len
,
237 uint8_t *output
, int *output_size
)
239 struct hash_algo
*algo
;
242 ret
= hash_lookup_algo(algo_name
, &algo
);
246 if (output_size
&& *output_size
< algo
->digest_size
) {
247 debug("Output buffer size %d too small (need %d bytes)",
248 *output_size
, algo
->digest_size
);
252 *output_size
= algo
->digest_size
;
253 algo
->hash_func_ws(data
, len
, output
, algo
->chunk_size
);
258 #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
260 * store_result: Store the resulting sum to an address or variable
262 * @algo: Hash algorithm being used
263 * @sum: Hash digest (algo->digest_size bytes)
264 * @dest: Destination, interpreted as a hex address if it starts
265 * with * (or allow_env_vars is 0) or otherwise as an
266 * environment variable.
267 * @allow_env_vars: non-zero to permit storing the result to an
268 * variable environment
270 static void store_result(struct hash_algo
*algo
, const uint8_t *sum
,
271 const char *dest
, int allow_env_vars
)
277 * If environment variables are allowed, then we assume that 'dest'
278 * is an environment variable, unless it starts with *, in which
279 * case we assume it is an address. If not allowed, it is always an
280 * address. This is to support the crc32 command.
282 if (allow_env_vars
) {
290 char str_output
[HASH_MAX_DIGEST_SIZE
* 2 + 1];
291 char *str_ptr
= str_output
;
293 for (i
= 0; i
< algo
->digest_size
; i
++) {
294 sprintf(str_ptr
, "%02x", sum
[i
]);
298 env_set(dest
, str_output
);
303 addr
= simple_strtoul(dest
, NULL
, 16);
304 buf
= map_sysmem(addr
, algo
->digest_size
);
305 memcpy(buf
, sum
, algo
->digest_size
);
311 * parse_verify_sum: Parse a hash verification parameter
313 * @algo: Hash algorithm being used
314 * @verify_str: Argument to parse. If it starts with * then it is
315 * interpreted as a hex address containing the hash.
316 * If the length is exactly the right number of hex digits
317 * for the digest size, then we assume it is a hex digest.
318 * Otherwise we assume it is an environment variable, and
319 * look up its value (it must contain a hex digest).
320 * @vsum: Returns binary digest value (algo->digest_size bytes)
321 * @allow_env_vars: non-zero to permit storing the result to an environment
322 * variable. If 0 then verify_str is assumed to be an
323 * address, and the * prefix is not expected.
324 * @return 0 if ok, non-zero on error
326 static int parse_verify_sum(struct hash_algo
*algo
, char *verify_str
,
327 uint8_t *vsum
, int allow_env_vars
)
331 /* See comment above in store_result() */
332 if (allow_env_vars
) {
333 if (*verify_str
== '*')
343 addr
= simple_strtoul(verify_str
, NULL
, 16);
344 buf
= map_sysmem(addr
, algo
->digest_size
);
345 memcpy(vsum
, buf
, algo
->digest_size
);
348 int digits
= algo
->digest_size
* 2;
351 * As with the original code from sha1sum.c, we assume that a
352 * string which matches the digest size exactly is a hex
353 * string and not an environment variable.
355 if (strlen(verify_str
) == digits
)
356 vsum_str
= verify_str
;
358 vsum_str
= env_get(verify_str
);
359 if (vsum_str
== NULL
|| strlen(vsum_str
) != digits
) {
360 printf("Expected %d hex digits in env var\n",
366 hash_parse_string(algo
->name
, vsum_str
, vsum
);
371 static void hash_show(struct hash_algo
*algo
, ulong addr
, ulong len
, uint8_t *output
)
375 printf("%s for %08lx ... %08lx ==> ", algo
->name
, addr
, addr
+ len
- 1);
376 for (i
= 0; i
< algo
->digest_size
; i
++)
377 printf("%02x", output
[i
]);
380 int hash_command(const char *algo_name
, int flags
, cmd_tbl_t
*cmdtp
, int flag
,
381 int argc
, char * const argv
[])
385 if ((argc
< 2) || ((flags
& HASH_FLAG_VERIFY
) && (argc
< 3)))
386 return CMD_RET_USAGE
;
388 addr
= simple_strtoul(*argv
++, NULL
, 16);
389 len
= simple_strtoul(*argv
++, NULL
, 16);
392 struct hash_algo
*algo
;
394 uint8_t vsum
[HASH_MAX_DIGEST_SIZE
];
397 if (hash_lookup_algo(algo_name
, &algo
)) {
398 printf("Unknown hash algorithm '%s'\n", algo_name
);
399 return CMD_RET_USAGE
;
403 if (algo
->digest_size
> HASH_MAX_DIGEST_SIZE
) {
404 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
408 output
= memalign(ARCH_DMA_MINALIGN
,
409 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE
);
411 buf
= map_sysmem(addr
, len
);
412 algo
->hash_func_ws(buf
, len
, output
, algo
->chunk_size
);
415 /* Try to avoid code bloat when verify is not needed */
416 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
417 defined(CONFIG_HASH_VERIFY)
418 if (flags
& HASH_FLAG_VERIFY
) {
422 if (parse_verify_sum(algo
, *argv
, vsum
,
423 flags
& HASH_FLAG_ENV
)) {
424 printf("ERROR: %s does not contain a valid "
425 "%s sum\n", *argv
, algo
->name
);
428 if (memcmp(output
, vsum
, algo
->digest_size
) != 0) {
431 hash_show(algo
, addr
, len
, output
);
433 for (i
= 0; i
< algo
->digest_size
; i
++)
434 printf("%02x", vsum
[i
]);
435 puts(" ** ERROR **\n");
439 hash_show(algo
, addr
, len
, output
);
443 store_result(algo
, output
, *argv
,
444 flags
& HASH_FLAG_ENV
);
446 unmap_sysmem(output
);
450 /* Horrible code size hack for boards that just want crc32 */
455 crc
= crc32_wd(0, (const uchar
*)addr
, len
, CHUNKSZ_CRC32
);
457 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
458 addr
, addr
+ len
- 1, crc
);
461 ptr
= (ulong
*)simple_strtoul(argv
[0], NULL
, 16);
468 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
469 #endif /* !USE_HOSTCC */