]> git.ipfire.org Git - thirdparty/u-boot.git/blame - common/hash.c
Merge tag 'u-boot-amlogic-20200529' of https://gitlab.denx.de/u-boot/custodians/u...
[thirdparty/u-boot.git] / common / hash.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
460408ef
SG
2/*
3 * Copyright (c) 2012 The Chromium OS Authors.
4 *
5 * (C) Copyright 2011
6 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
7 *
8 * (C) Copyright 2000
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
460408ef
SG
10 */
11
2dd90027 12#ifndef USE_HOSTCC
460408ef
SG
13#include <common.h>
14#include <command.h>
9fb625ce 15#include <env.h>
f7ae49fc 16#include <log.h>
bf007ebb 17#include <malloc.h>
0eb25b61 18#include <mapmem.h>
1f9c9280 19#include <hw_sha.h>
90526e9f 20#include <asm/cache.h>
2dd90027 21#include <asm/io.h>
1221ce45 22#include <linux/errno.h>
3db71108 23#include <u-boot/crc.h>
2dd90027
RG
24#else
25#include "mkimage.h"
26#include <time.h>
2dd90027
RG
27#endif /* !USE_HOSTCC*/
28
460408ef 29#include <hash.h>
4d72caa5 30#include <image.h>
2dd90027 31#include <u-boot/crc.h>
2b9912e6
JH
32#include <u-boot/sha1.h>
33#include <u-boot/sha256.h>
2dd90027 34#include <u-boot/md5.h>
460408ef 35
10088fb0
KR
36#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
37DECLARE_GLOBAL_DATA_PTR;
38#endif
39
40static void reloc_update(void);
41
78eda89e 42#if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
bf007ebb
HT
43static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
44{
45 sha1_context *ctx = malloc(sizeof(sha1_context));
46 sha1_starts(ctx);
47 *ctxp = ctx;
48 return 0;
49}
50
51static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
52 unsigned int size, int is_last)
53{
54 sha1_update((sha1_context *)ctx, buf, size);
55 return 0;
56}
57
58static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
59 int size)
60{
61 if (size < algo->digest_size)
62 return -1;
63
64 sha1_finish((sha1_context *)ctx, dest_buf);
65 free(ctx);
66 return 0;
67}
68#endif
69
78eda89e 70#if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
bf007ebb
HT
71static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
72{
73 sha256_context *ctx = malloc(sizeof(sha256_context));
74 sha256_starts(ctx);
75 *ctxp = ctx;
76 return 0;
77}
78
79static int hash_update_sha256(struct hash_algo *algo, void *ctx,
80 const void *buf, unsigned int size, int is_last)
81{
82 sha256_update((sha256_context *)ctx, buf, size);
83 return 0;
84}
85
86static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
87 *dest_buf, int size)
88{
89 if (size < algo->digest_size)
90 return -1;
91
92 sha256_finish((sha256_context *)ctx, dest_buf);
93 free(ctx);
94 return 0;
95}
96#endif
97
51c2345b
PT
98static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
99{
100 uint16_t *ctx = malloc(sizeof(uint16_t));
101 *ctx = 0;
102 *ctxp = ctx;
103 return 0;
104}
105
106static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
107 const void *buf, unsigned int size,
108 int is_last)
109{
110 *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
111 return 0;
112}
113
114static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
115 void *dest_buf, int size)
116{
117 if (size < algo->digest_size)
118 return -1;
119
120 *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
121 free(ctx);
122 return 0;
123}
124
bf007ebb
HT
125static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
126{
127 uint32_t *ctx = malloc(sizeof(uint32_t));
128 *ctx = 0;
129 *ctxp = ctx;
130 return 0;
131}
132
133static int hash_update_crc32(struct hash_algo *algo, void *ctx,
134 const void *buf, unsigned int size, int is_last)
135{
136 *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
137 return 0;
138}
139
140static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
141 int size)
142{
143 if (size < algo->digest_size)
144 return -1;
145
146 *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
147 free(ctx);
148 return 0;
149}
150
460408ef 151/*
78eda89e
TR
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.
460408ef
SG
155 */
156static struct hash_algo hash_algo[] = {
78eda89e 157#ifdef CONFIG_SHA1
1f9c9280 158 {
78eda89e
TR
159 .name = "sha1",
160 .digest_size = SHA1_SUM_LEN,
161 .chunk_size = CHUNKSZ_SHA1,
162#ifdef CONFIG_SHA_HW_ACCEL
163 .hash_func_ws = hw_sha1,
164#else
165 .hash_func_ws = sha1_csum_wd,
94e3c8c4 166#endif
94e3c8c4 167#ifdef CONFIG_SHA_PROG_HW_ACCEL
78eda89e
TR
168 .hash_init = hw_sha_init,
169 .hash_update = hw_sha_update,
170 .hash_finish = hw_sha_finish,
171#else
172 .hash_init = hash_init_sha1,
173 .hash_update = hash_update_sha1,
174 .hash_finish = hash_finish_sha1,
1f9c9280 175#endif
460408ef
SG
176 },
177#endif
178#ifdef CONFIG_SHA256
179 {
78eda89e
TR
180 .name = "sha256",
181 .digest_size = SHA256_SUM_LEN,
182 .chunk_size = CHUNKSZ_SHA256,
183#ifdef CONFIG_SHA_HW_ACCEL
184 .hash_func_ws = hw_sha256,
185#else
186 .hash_func_ws = sha256_csum_wd,
187#endif
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,
192#else
193 .hash_init = hash_init_sha256,
194 .hash_update = hash_update_sha256,
195 .hash_finish = hash_finish_sha256,
196#endif
460408ef
SG
197 },
198#endif
51c2345b
PT
199 {
200 .name = "crc16-ccitt",
201 .digest_size = 2,
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,
207 },
d20a40de 208 {
78eda89e
TR
209 .name = "crc32",
210 .digest_size = 4,
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,
d20a40de 216 },
460408ef
SG
217};
218
d20a40de 219/* Try to minimize code size for boards that don't want much hashing */
221a949e
DT
220#if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
221 defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH)
d20a40de
SG
222#define multi_hash() 1
223#else
224#define multi_hash() 0
225#endif
226
10088fb0
KR
227static void reloc_update(void)
228{
229#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
230 int i;
231 static bool done;
232
233 if (!done) {
234 done = true;
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;
241 }
242 }
243#endif
244}
245
2dd90027
RG
246int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
247{
248 int i;
249
10088fb0
KR
250 reloc_update();
251
2dd90027
RG
252 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
253 if (!strcmp(algo_name, hash_algo[i].name)) {
254 *algop = &hash_algo[i];
255 return 0;
256 }
257 }
258
259 debug("Unknown hash algorithm '%s'\n", algo_name);
260 return -EPROTONOSUPPORT;
261}
262
263int hash_progressive_lookup_algo(const char *algo_name,
264 struct hash_algo **algop)
265{
266 int i;
267
10088fb0
KR
268 reloc_update();
269
2dd90027
RG
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];
274 return 0;
275 }
276 }
277 }
278
279 debug("Unknown hash algorithm '%s'\n", algo_name);
280 return -EPROTONOSUPPORT;
281}
282
283#ifndef USE_HOSTCC
8f0b1e24
SR
284int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
285{
286 struct hash_algo *algo;
287 int ret;
288 int i;
289
290 ret = hash_lookup_algo(algo_name, &algo);
291 if (ret)
292 return ret;
293
294 for (i = 0; i < algo->digest_size; i++) {
295 char chr[3];
296
297 strncpy(chr, &str[i * 2], 2);
298 result[i] = simple_strtoul(chr, NULL, 16);
299 }
300
301 return 0;
302}
303
48ad68de
TR
304int hash_block(const char *algo_name, const void *data, unsigned int len,
305 uint8_t *output, int *output_size)
306{
307 struct hash_algo *algo;
308 int ret;
309
310 ret = hash_lookup_algo(algo_name, &algo);
311 if (ret)
312 return ret;
313
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);
317 return -ENOSPC;
318 }
319 if (output_size)
320 *output_size = algo->digest_size;
321 algo->hash_func_ws(data, len, output, algo->chunk_size);
322
323 return 0;
324}
325
326#if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
460408ef
SG
327/**
328 * store_result: Store the resulting sum to an address or variable
329 *
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
d5b76673
SG
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
460408ef 337 */
04819a4f 338static void store_result(struct hash_algo *algo, const uint8_t *sum,
d5b76673 339 const char *dest, int allow_env_vars)
460408ef
SG
340{
341 unsigned int i;
d5b76673 342 int env_var = 0;
460408ef 343
d5b76673
SG
344 /*
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.
349 */
350 if (allow_env_vars) {
351 if (*dest == '*')
352 dest++;
353 else
354 env_var = 1;
355 }
460408ef 356
d5b76673 357 if (env_var) {
460408ef
SG
358 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
359 char *str_ptr = str_output;
360
361 for (i = 0; i < algo->digest_size; i++) {
362 sprintf(str_ptr, "%02x", sum[i]);
363 str_ptr += 2;
364 }
8b9cc866 365 *str_ptr = '\0';
382bee57 366 env_set(dest, str_output);
d5b76673 367 } else {
bd091b67
SG
368 ulong addr;
369 void *buf;
d5b76673 370
bd091b67
SG
371 addr = simple_strtoul(dest, NULL, 16);
372 buf = map_sysmem(addr, algo->digest_size);
373 memcpy(buf, sum, algo->digest_size);
374 unmap_sysmem(buf);
460408ef
SG
375 }
376}
377
378/**
379 * parse_verify_sum: Parse a hash verification parameter
380 *
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)
d5b76673
SG
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.
460408ef
SG
392 * @return 0 if ok, non-zero on error
393 */
04819a4f
SG
394static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
395 uint8_t *vsum, int allow_env_vars)
460408ef 396{
d5b76673
SG
397 int env_var = 0;
398
399 /* See comment above in store_result() */
400 if (allow_env_vars) {
401 if (*verify_str == '*')
402 verify_str++;
403 else
404 env_var = 1;
405 }
406
3ef46a99 407 if (!env_var) {
bd091b67
SG
408 ulong addr;
409 void *buf;
460408ef 410
bd091b67
SG
411 addr = simple_strtoul(verify_str, NULL, 16);
412 buf = map_sysmem(addr, algo->digest_size);
413 memcpy(vsum, buf, algo->digest_size);
460408ef 414 } else {
460408ef
SG
415 char *vsum_str;
416 int digits = algo->digest_size * 2;
417
418 /*
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.
422 */
423 if (strlen(verify_str) == digits)
424 vsum_str = verify_str;
425 else {
00caae6d 426 vsum_str = env_get(verify_str);
460408ef
SG
427 if (vsum_str == NULL || strlen(vsum_str) != digits) {
428 printf("Expected %d hex digits in env var\n",
429 digits);
430 return 1;
431 }
432 }
433
8f0b1e24 434 hash_parse_string(algo->name, vsum_str, vsum);
460408ef
SG
435 }
436 return 0;
437}
438
48ad68de 439static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
460408ef
SG
440{
441 int i;
442
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]);
446}
447
09140113
SG
448int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
449 int flag, int argc, char *const argv[])
460408ef 450{
460408ef 451 ulong addr, len;
460408ef 452
3ef46a99 453 if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
460408ef
SG
454 return CMD_RET_USAGE;
455
d5b76673
SG
456 addr = simple_strtoul(*argv++, NULL, 16);
457 len = simple_strtoul(*argv++, NULL, 16);
458
d20a40de
SG
459 if (multi_hash()) {
460 struct hash_algo *algo;
d7af2baa 461 u8 *output;
04819a4f 462 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
bd091b67 463 void *buf;
460408ef 464
bf007ebb 465 if (hash_lookup_algo(algo_name, &algo)) {
d20a40de
SG
466 printf("Unknown hash algorithm '%s'\n", algo_name);
467 return CMD_RET_USAGE;
468 }
469 argc -= 2;
470
471 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
472 puts("HASH_MAX_DIGEST_SIZE exceeded\n");
473 return 1;
474 }
460408ef 475
d7af2baa
BL
476 output = memalign(ARCH_DMA_MINALIGN,
477 sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
478
bd091b67
SG
479 buf = map_sysmem(addr, len);
480 algo->hash_func_ws(buf, len, output, algo->chunk_size);
481 unmap_sysmem(buf);
460408ef 482
d20a40de 483 /* Try to avoid code bloat when verify is not needed */
221a949e
DT
484#if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
485 defined(CONFIG_HASH_VERIFY)
d20a40de 486 if (flags & HASH_FLAG_VERIFY) {
460408ef 487#else
d20a40de 488 if (0) {
460408ef 489#endif
d20a40de 490 if (parse_verify_sum(algo, *argv, vsum,
d5b76673 491 flags & HASH_FLAG_ENV)) {
d20a40de
SG
492 printf("ERROR: %s does not contain a valid "
493 "%s sum\n", *argv, algo->name);
494 return 1;
495 }
496 if (memcmp(output, vsum, algo->digest_size) != 0) {
497 int i;
460408ef 498
31890ae2 499 hash_show(algo, addr, len, output);
d20a40de
SG
500 printf(" != ");
501 for (i = 0; i < algo->digest_size; i++)
502 printf("%02x", vsum[i]);
503 puts(" ** ERROR **\n");
504 return 1;
505 }
506 } else {
31890ae2 507 hash_show(algo, addr, len, output);
d20a40de
SG
508 printf("\n");
509
510 if (argc) {
511 store_result(algo, output, *argv,
512 flags & HASH_FLAG_ENV);
513 }
d7af2baa
BL
514 unmap_sysmem(output);
515
460408ef 516 }
d20a40de
SG
517
518 /* Horrible code size hack for boards that just want crc32 */
460408ef 519 } else {
d20a40de
SG
520 ulong crc;
521 ulong *ptr;
522
523 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
524
525 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
526 addr, addr + len - 1, crc);
460408ef 527
4b756b01
TR
528 if (argc >= 3) {
529 ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
d20a40de 530 *ptr = crc;
d5b76673 531 }
460408ef
SG
532 }
533
534 return 0;
535}
d70f919e
SG
536#endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
537#endif /* !USE_HOSTCC */