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