board: tbs2910: disable loadb and loads commands
[platform/kernel/u-boot.git] / common / hash.c
1 // SPDX-License-Identifier: GPL-2.0+
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.
10  */
11
12 #ifndef USE_HOSTCC
13 #include <common.h>
14 #include <command.h>
15 #include <env.h>
16 #include <log.h>
17 #include <malloc.h>
18 #include <mapmem.h>
19 #include <hw_sha.h>
20 #include <asm/cache.h>
21 #include <asm/io.h>
22 #include <linux/errno.h>
23 #include <u-boot/crc.h>
24 #else
25 #include "mkimage.h"
26 #include <time.h>
27 #endif /* !USE_HOSTCC*/
28
29 #include <hash.h>
30 #include <image.h>
31 #include <u-boot/crc.h>
32 #include <u-boot/sha1.h>
33 #include <u-boot/sha256.h>
34 #include <u-boot/sha512.h>
35 #include <u-boot/md5.h>
36
37 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
38 DECLARE_GLOBAL_DATA_PTR;
39 #endif
40
41 static void reloc_update(void);
42
43 #if defined(CONFIG_SHA1) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
44 static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
45 {
46         sha1_context *ctx = malloc(sizeof(sha1_context));
47         sha1_starts(ctx);
48         *ctxp = ctx;
49         return 0;
50 }
51
52 static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
53                             unsigned int size, int is_last)
54 {
55         sha1_update((sha1_context *)ctx, buf, size);
56         return 0;
57 }
58
59 static int hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf,
60                             int size)
61 {
62         if (size < algo->digest_size)
63                 return -1;
64
65         sha1_finish((sha1_context *)ctx, dest_buf);
66         free(ctx);
67         return 0;
68 }
69 #endif
70
71 #if defined(CONFIG_SHA256) && !defined(CONFIG_SHA_PROG_HW_ACCEL)
72 static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
73 {
74         sha256_context *ctx = malloc(sizeof(sha256_context));
75         sha256_starts(ctx);
76         *ctxp = ctx;
77         return 0;
78 }
79
80 static int hash_update_sha256(struct hash_algo *algo, void *ctx,
81                               const void *buf, unsigned int size, int is_last)
82 {
83         sha256_update((sha256_context *)ctx, buf, size);
84         return 0;
85 }
86
87 static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void
88                               *dest_buf, int size)
89 {
90         if (size < algo->digest_size)
91                 return -1;
92
93         sha256_finish((sha256_context *)ctx, dest_buf);
94         free(ctx);
95         return 0;
96 }
97 #endif
98
99 #if defined(CONFIG_SHA384)
100 static int hash_init_sha384(struct hash_algo *algo, void **ctxp)
101 {
102         sha512_context *ctx = malloc(sizeof(sha512_context));
103         sha384_starts(ctx);
104         *ctxp = ctx;
105         return 0;
106 }
107
108 static int hash_update_sha384(struct hash_algo *algo, void *ctx,
109                               const void *buf, unsigned int size, int is_last)
110 {
111         sha384_update((sha512_context *)ctx, buf, size);
112         return 0;
113 }
114
115 static int hash_finish_sha384(struct hash_algo *algo, void *ctx, void
116                               *dest_buf, int size)
117 {
118         if (size < algo->digest_size)
119                 return -1;
120
121         sha384_finish((sha512_context *)ctx, dest_buf);
122         free(ctx);
123         return 0;
124 }
125 #endif
126
127 #if defined(CONFIG_SHA512)
128 static int hash_init_sha512(struct hash_algo *algo, void **ctxp)
129 {
130         sha512_context *ctx = malloc(sizeof(sha512_context));
131         sha512_starts(ctx);
132         *ctxp = ctx;
133         return 0;
134 }
135
136 static int hash_update_sha512(struct hash_algo *algo, void *ctx,
137                               const void *buf, unsigned int size, int is_last)
138 {
139         sha512_update((sha512_context *)ctx, buf, size);
140         return 0;
141 }
142
143 static int hash_finish_sha512(struct hash_algo *algo, void *ctx, void
144                               *dest_buf, int size)
145 {
146         if (size < algo->digest_size)
147                 return -1;
148
149         printf("hello world\n");
150
151         sha512_finish((sha512_context *)ctx, dest_buf);
152         free(ctx);
153         return 0;
154 }
155 #endif
156
157
158 static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
159 {
160         uint16_t *ctx = malloc(sizeof(uint16_t));
161         *ctx = 0;
162         *ctxp = ctx;
163         return 0;
164 }
165
166 static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx,
167                                    const void *buf, unsigned int size,
168                                    int is_last)
169 {
170         *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size);
171         return 0;
172 }
173
174 static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx,
175                                    void *dest_buf, int size)
176 {
177         if (size < algo->digest_size)
178                 return -1;
179
180         *((uint16_t *)dest_buf) = *((uint16_t *)ctx);
181         free(ctx);
182         return 0;
183 }
184
185 static int hash_init_crc32(struct hash_algo *algo, void **ctxp)
186 {
187         uint32_t *ctx = malloc(sizeof(uint32_t));
188         *ctx = 0;
189         *ctxp = ctx;
190         return 0;
191 }
192
193 static int hash_update_crc32(struct hash_algo *algo, void *ctx,
194                              const void *buf, unsigned int size, int is_last)
195 {
196         *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size);
197         return 0;
198 }
199
200 static int hash_finish_crc32(struct hash_algo *algo, void *ctx, void *dest_buf,
201                              int size)
202 {
203         if (size < algo->digest_size)
204                 return -1;
205
206         *((uint32_t *)dest_buf) = *((uint32_t *)ctx);
207         free(ctx);
208         return 0;
209 }
210
211 /*
212  * These are the hash algorithms we support.  If we have hardware acceleration
213  * is enable we will use that, otherwise a software version of the algorithm.
214  * Note that algorithm names must be in lower case.
215  */
216 static struct hash_algo hash_algo[] = {
217 #ifdef CONFIG_SHA1
218         {
219                 .name           = "sha1",
220                 .digest_size    = SHA1_SUM_LEN,
221                 .chunk_size     = CHUNKSZ_SHA1,
222 #ifdef CONFIG_SHA_HW_ACCEL
223                 .hash_func_ws   = hw_sha1,
224 #else
225                 .hash_func_ws   = sha1_csum_wd,
226 #endif
227 #ifdef CONFIG_SHA_PROG_HW_ACCEL
228                 .hash_init      = hw_sha_init,
229                 .hash_update    = hw_sha_update,
230                 .hash_finish    = hw_sha_finish,
231 #else
232                 .hash_init      = hash_init_sha1,
233                 .hash_update    = hash_update_sha1,
234                 .hash_finish    = hash_finish_sha1,
235 #endif
236         },
237 #endif
238 #ifdef CONFIG_SHA256
239         {
240                 .name           = "sha256",
241                 .digest_size    = SHA256_SUM_LEN,
242                 .chunk_size     = CHUNKSZ_SHA256,
243 #ifdef CONFIG_SHA_HW_ACCEL
244                 .hash_func_ws   = hw_sha256,
245 #else
246                 .hash_func_ws   = sha256_csum_wd,
247 #endif
248 #ifdef CONFIG_SHA_PROG_HW_ACCEL
249                 .hash_init      = hw_sha_init,
250                 .hash_update    = hw_sha_update,
251                 .hash_finish    = hw_sha_finish,
252 #else
253                 .hash_init      = hash_init_sha256,
254                 .hash_update    = hash_update_sha256,
255                 .hash_finish    = hash_finish_sha256,
256 #endif
257         },
258 #endif
259 #ifdef CONFIG_SHA384
260         {
261                 .name           = "sha384",
262                 .digest_size    = SHA384_SUM_LEN,
263                 .chunk_size     = CHUNKSZ_SHA384,
264                 .hash_func_ws   = sha384_csum_wd,
265                 .hash_init      = hash_init_sha384,
266                 .hash_update    = hash_update_sha384,
267                 .hash_finish    = hash_finish_sha384,
268         },
269 #endif
270 #ifdef CONFIG_SHA512
271         {
272                 .name           = "sha512",
273                 .digest_size    = SHA512_SUM_LEN,
274                 .chunk_size     = CHUNKSZ_SHA512,
275                 .hash_func_ws   = sha512_csum_wd,
276                 .hash_init      = hash_init_sha512,
277                 .hash_update    = hash_update_sha512,
278                 .hash_finish    = hash_finish_sha512,
279         },
280 #endif
281         {
282                 .name           = "crc16-ccitt",
283                 .digest_size    = 2,
284                 .chunk_size     = CHUNKSZ,
285                 .hash_func_ws   = crc16_ccitt_wd_buf,
286                 .hash_init      = hash_init_crc16_ccitt,
287                 .hash_update    = hash_update_crc16_ccitt,
288                 .hash_finish    = hash_finish_crc16_ccitt,
289         },
290         {
291                 .name           = "crc32",
292                 .digest_size    = 4,
293                 .chunk_size     = CHUNKSZ_CRC32,
294                 .hash_func_ws   = crc32_wd_buf,
295                 .hash_init      = hash_init_crc32,
296                 .hash_update    = hash_update_crc32,
297                 .hash_finish    = hash_finish_crc32,
298         },
299 };
300
301 /* Try to minimize code size for boards that don't want much hashing */
302 #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \
303         defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) || \
304         defined(CONFIG_SHA384) || defined(CONFIG_SHA512)
305 #define multi_hash()    1
306 #else
307 #define multi_hash()    0
308 #endif
309
310 static void reloc_update(void)
311 {
312 #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
313         int i;
314         static bool done;
315
316         if (!done) {
317                 done = true;
318                 for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
319                         hash_algo[i].name += gd->reloc_off;
320                         hash_algo[i].hash_func_ws += gd->reloc_off;
321                         hash_algo[i].hash_init += gd->reloc_off;
322                         hash_algo[i].hash_update += gd->reloc_off;
323                         hash_algo[i].hash_finish += gd->reloc_off;
324                 }
325         }
326 #endif
327 }
328
329 int hash_lookup_algo(const char *algo_name, struct hash_algo **algop)
330 {
331         int i;
332
333         reloc_update();
334
335         for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
336                 if (!strcmp(algo_name, hash_algo[i].name)) {
337                         *algop = &hash_algo[i];
338                         return 0;
339                 }
340         }
341
342         debug("Unknown hash algorithm '%s'\n", algo_name);
343         return -EPROTONOSUPPORT;
344 }
345
346 int hash_progressive_lookup_algo(const char *algo_name,
347                                  struct hash_algo **algop)
348 {
349         int i;
350
351         reloc_update();
352
353         for (i = 0; i < ARRAY_SIZE(hash_algo); i++) {
354                 if (!strcmp(algo_name, hash_algo[i].name)) {
355                         if (hash_algo[i].hash_init) {
356                                 *algop = &hash_algo[i];
357                                 return 0;
358                         }
359                 }
360         }
361
362         debug("Unknown hash algorithm '%s'\n", algo_name);
363         return -EPROTONOSUPPORT;
364 }
365
366 #ifndef USE_HOSTCC
367 int hash_parse_string(const char *algo_name, const char *str, uint8_t *result)
368 {
369         struct hash_algo *algo;
370         int ret;
371         int i;
372
373         ret = hash_lookup_algo(algo_name, &algo);
374         if (ret)
375                 return ret;
376
377         for (i = 0; i < algo->digest_size; i++) {
378                 char chr[3];
379
380                 strncpy(chr, &str[i * 2], 2);
381                 result[i] = simple_strtoul(chr, NULL, 16);
382         }
383
384         return 0;
385 }
386
387 int hash_block(const char *algo_name, const void *data, unsigned int len,
388                uint8_t *output, int *output_size)
389 {
390         struct hash_algo *algo;
391         int ret;
392
393         ret = hash_lookup_algo(algo_name, &algo);
394         if (ret)
395                 return ret;
396
397         if (output_size && *output_size < algo->digest_size) {
398                 debug("Output buffer size %d too small (need %d bytes)",
399                       *output_size, algo->digest_size);
400                 return -ENOSPC;
401         }
402         if (output_size)
403                 *output_size = algo->digest_size;
404         algo->hash_func_ws(data, len, output, algo->chunk_size);
405
406         return 0;
407 }
408
409 #if defined(CONFIG_CMD_HASH) || defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)
410 /**
411  * store_result: Store the resulting sum to an address or variable
412  *
413  * @algo:               Hash algorithm being used
414  * @sum:                Hash digest (algo->digest_size bytes)
415  * @dest:               Destination, interpreted as a hex address if it starts
416  *                      with * (or allow_env_vars is 0) or otherwise as an
417  *                      environment variable.
418  * @allow_env_vars:     non-zero to permit storing the result to an
419  *                      variable environment
420  */
421 static void store_result(struct hash_algo *algo, const uint8_t *sum,
422                          const char *dest, int allow_env_vars)
423 {
424         unsigned int i;
425         int env_var = 0;
426
427         /*
428          * If environment variables are allowed, then we assume that 'dest'
429          * is an environment variable, unless it starts with *, in which
430          * case we assume it is an address. If not allowed, it is always an
431          * address. This is to support the crc32 command.
432          */
433         if (allow_env_vars) {
434                 if (*dest == '*')
435                         dest++;
436                 else
437                         env_var = 1;
438         }
439
440         if (env_var) {
441                 char str_output[HASH_MAX_DIGEST_SIZE * 2 + 1];
442                 char *str_ptr = str_output;
443
444                 for (i = 0; i < algo->digest_size; i++) {
445                         sprintf(str_ptr, "%02x", sum[i]);
446                         str_ptr += 2;
447                 }
448                 *str_ptr = '\0';
449                 env_set(dest, str_output);
450         } else {
451                 ulong addr;
452                 void *buf;
453
454                 addr = simple_strtoul(dest, NULL, 16);
455                 buf = map_sysmem(addr, algo->digest_size);
456                 memcpy(buf, sum, algo->digest_size);
457                 unmap_sysmem(buf);
458         }
459 }
460
461 /**
462  * parse_verify_sum: Parse a hash verification parameter
463  *
464  * @algo:               Hash algorithm being used
465  * @verify_str:         Argument to parse. If it starts with * then it is
466  *                      interpreted as a hex address containing the hash.
467  *                      If the length is exactly the right number of hex digits
468  *                      for the digest size, then we assume it is a hex digest.
469  *                      Otherwise we assume it is an environment variable, and
470  *                      look up its value (it must contain a hex digest).
471  * @vsum:               Returns binary digest value (algo->digest_size bytes)
472  * @allow_env_vars:     non-zero to permit storing the result to an environment
473  *                      variable. If 0 then verify_str is assumed to be an
474  *                      address, and the * prefix is not expected.
475  * @return 0 if ok, non-zero on error
476  */
477 static int parse_verify_sum(struct hash_algo *algo, char *verify_str,
478                             uint8_t *vsum, int allow_env_vars)
479 {
480         int env_var = 0;
481
482         /* See comment above in store_result() */
483         if (allow_env_vars) {
484                 if (*verify_str == '*')
485                         verify_str++;
486                 else
487                         env_var = 1;
488         }
489
490         if (!env_var) {
491                 ulong addr;
492                 void *buf;
493
494                 addr = simple_strtoul(verify_str, NULL, 16);
495                 buf = map_sysmem(addr, algo->digest_size);
496                 memcpy(vsum, buf, algo->digest_size);
497         } else {
498                 char *vsum_str;
499                 int digits = algo->digest_size * 2;
500
501                 /*
502                  * As with the original code from sha1sum.c, we assume that a
503                  * string which matches the digest size exactly is a hex
504                  * string and not an environment variable.
505                  */
506                 if (strlen(verify_str) == digits)
507                         vsum_str = verify_str;
508                 else {
509                         vsum_str = env_get(verify_str);
510                         if (vsum_str == NULL || strlen(vsum_str) != digits) {
511                                 printf("Expected %d hex digits in env var\n",
512                                        digits);
513                                 return 1;
514                         }
515                 }
516
517                 hash_parse_string(algo->name, vsum_str, vsum);
518         }
519         return 0;
520 }
521
522 static void hash_show(struct hash_algo *algo, ulong addr, ulong len, uint8_t *output)
523 {
524         int i;
525
526         printf("%s for %08lx ... %08lx ==> ", algo->name, addr, addr + len - 1);
527         for (i = 0; i < algo->digest_size; i++)
528                 printf("%02x", output[i]);
529 }
530
531 int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
532                  int flag, int argc, char *const argv[])
533 {
534         ulong addr, len;
535
536         if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3)))
537                 return CMD_RET_USAGE;
538
539         addr = simple_strtoul(*argv++, NULL, 16);
540         len = simple_strtoul(*argv++, NULL, 16);
541
542         if (multi_hash()) {
543                 struct hash_algo *algo;
544                 u8 *output;
545                 uint8_t vsum[HASH_MAX_DIGEST_SIZE];
546                 void *buf;
547
548                 if (hash_lookup_algo(algo_name, &algo)) {
549                         printf("Unknown hash algorithm '%s'\n", algo_name);
550                         return CMD_RET_USAGE;
551                 }
552                 argc -= 2;
553
554                 if (algo->digest_size > HASH_MAX_DIGEST_SIZE) {
555                         puts("HASH_MAX_DIGEST_SIZE exceeded\n");
556                         return 1;
557                 }
558
559                 output = memalign(ARCH_DMA_MINALIGN,
560                                   sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
561
562                 buf = map_sysmem(addr, len);
563                 algo->hash_func_ws(buf, len, output, algo->chunk_size);
564                 unmap_sysmem(buf);
565
566                 /* Try to avoid code bloat when verify is not needed */
567 #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \
568         defined(CONFIG_HASH_VERIFY)
569                 if (flags & HASH_FLAG_VERIFY) {
570 #else
571                 if (0) {
572 #endif
573                         if (parse_verify_sum(algo, *argv, vsum,
574                                         flags & HASH_FLAG_ENV)) {
575                                 printf("ERROR: %s does not contain a valid "
576                                         "%s sum\n", *argv, algo->name);
577                                 return 1;
578                         }
579                         if (memcmp(output, vsum, algo->digest_size) != 0) {
580                                 int i;
581
582                                 hash_show(algo, addr, len, output);
583                                 printf(" != ");
584                                 for (i = 0; i < algo->digest_size; i++)
585                                         printf("%02x", vsum[i]);
586                                 puts(" ** ERROR **\n");
587                                 return 1;
588                         }
589                 } else {
590                         hash_show(algo, addr, len, output);
591                         printf("\n");
592
593                         if (argc) {
594                                 store_result(algo, output, *argv,
595                                         flags & HASH_FLAG_ENV);
596                         }
597                 unmap_sysmem(output);
598
599                 }
600
601         /* Horrible code size hack for boards that just want crc32 */
602         } else {
603                 ulong crc;
604                 ulong *ptr;
605
606                 crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32);
607
608                 printf("CRC32 for %08lx ... %08lx ==> %08lx\n",
609                                 addr, addr + len - 1, crc);
610
611                 if (argc >= 3) {
612                         ptr = (ulong *)simple_strtoul(argv[0], NULL, 16);
613                         *ptr = crc;
614                 }
615         }
616
617         return 0;
618 }
619 #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */
620 #endif /* !USE_HOSTCC */