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 | |
| 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 | |
Philipp Tomsich | 6e2ac3e | 2018-11-25 19:22:19 +0100 | [diff] [blame] | 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 | |
Hung-ying Tyan | f061b1a | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 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 | |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 211 | /* |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 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. |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 215 | */ |
| 216 | static struct hash_algo hash_algo[] = { |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 217 | #ifdef CONFIG_SHA1 |
Akshay Saraswat | cbd8f2c | 2013-03-20 21:00:58 +0000 | [diff] [blame] | 218 | { |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 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, |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 226 | #endif |
gaurav rana | ef20159 | 2015-02-20 12:51:46 +0530 | [diff] [blame] | 227 | #ifdef CONFIG_SHA_PROG_HW_ACCEL |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 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, |
Akshay Saraswat | cbd8f2c | 2013-03-20 21:00:58 +0000 | [diff] [blame] | 235 | #endif |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 236 | }, |
| 237 | #endif |
| 238 | #ifdef CONFIG_SHA256 |
| 239 | { |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 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 |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 257 | }, |
| 258 | #endif |
Reuben Dowle | 1908fd9 | 2020-04-16 17:36:52 +1200 | [diff] [blame^] | 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 |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 281 | { |
Philipp Tomsich | 6e2ac3e | 2018-11-25 19:22:19 +0100 | [diff] [blame] | 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 | { |
Tom Rini | 03361c7 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 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, |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 298 | }, |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 301 | /* 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] | 302 | #if defined(CONFIG_SHA256) || defined(CONFIG_CMD_SHA1SUM) || \ |
Reuben Dowle | 1908fd9 | 2020-04-16 17:36:52 +1200 | [diff] [blame^] | 303 | defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_CMD_HASH) || \ |
| 304 | defined(CONFIG_SHA384) || defined(CONFIG_SHA512) |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 305 | #define multi_hash() 1 |
| 306 | #else |
| 307 | #define multi_hash() 0 |
| 308 | #endif |
| 309 | |
T Karthik Reddy | a098b43 | 2019-03-16 15:23:01 +0530 | [diff] [blame] | 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 | |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 329 | int hash_lookup_algo(const char *algo_name, struct hash_algo **algop) |
| 330 | { |
| 331 | int i; |
| 332 | |
T Karthik Reddy | a098b43 | 2019-03-16 15:23:01 +0530 | [diff] [blame] | 333 | reloc_update(); |
| 334 | |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 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 | |
T Karthik Reddy | a098b43 | 2019-03-16 15:23:01 +0530 | [diff] [blame] | 351 | reloc_update(); |
| 352 | |
Ruchika Gupta | c915ceb | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 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 |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 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 | |
Tom Rini | 6173838 | 2016-01-05 08:47:48 -0500 | [diff] [blame] | 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) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 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 |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 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 |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 420 | */ |
Simon Glass | a3b240b | 2014-06-12 07:24:41 -0600 | [diff] [blame] | 421 | static void store_result(struct hash_algo *algo, const uint8_t *sum, |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 422 | const char *dest, int allow_env_vars) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 423 | { |
| 424 | unsigned int i; |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 425 | int env_var = 0; |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 426 | |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 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 | } |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 439 | |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 440 | if (env_var) { |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 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 | } |
Jeroen Hofstee | b14c052 | 2014-06-09 11:02:02 +0200 | [diff] [blame] | 448 | *str_ptr = '\0'; |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 449 | env_set(dest, str_output); |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 450 | } else { |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 451 | ulong addr; |
| 452 | void *buf; |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 453 | |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 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); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 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) |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 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. |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 475 | * @return 0 if ok, non-zero on error |
| 476 | */ |
Simon Glass | a3b240b | 2014-06-12 07:24:41 -0600 | [diff] [blame] | 477 | static int parse_verify_sum(struct hash_algo *algo, char *verify_str, |
| 478 | uint8_t *vsum, int allow_env_vars) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 479 | { |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 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 | |
Nikolay Dimitrov | 810b84d | 2014-12-12 20:01:23 +0200 | [diff] [blame] | 490 | if (!env_var) { |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 491 | ulong addr; |
| 492 | void *buf; |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 493 | |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 494 | addr = simple_strtoul(verify_str, NULL, 16); |
| 495 | buf = map_sysmem(addr, algo->digest_size); |
| 496 | memcpy(vsum, buf, algo->digest_size); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 497 | } else { |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 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 { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 509 | vsum_str = env_get(verify_str); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 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 | |
Stefan Roese | 0ed2e46 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 517 | hash_parse_string(algo->name, vsum_str, vsum); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 518 | } |
| 519 | return 0; |
| 520 | } |
| 521 | |
Tom Rini | 6173838 | 2016-01-05 08:47:48 -0500 | [diff] [blame] | 522 | 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] | 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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 531 | int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp, |
| 532 | int flag, int argc, char *const argv[]) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 533 | { |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 534 | ulong addr, len; |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 535 | |
Nikolay Dimitrov | 810b84d | 2014-12-12 20:01:23 +0200 | [diff] [blame] | 536 | if ((argc < 2) || ((flags & HASH_FLAG_VERIFY) && (argc < 3))) |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 537 | return CMD_RET_USAGE; |
| 538 | |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 539 | addr = simple_strtoul(*argv++, NULL, 16); |
| 540 | len = simple_strtoul(*argv++, NULL, 16); |
| 541 | |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 542 | if (multi_hash()) { |
| 543 | struct hash_algo *algo; |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 544 | u8 *output; |
Simon Glass | a3b240b | 2014-06-12 07:24:41 -0600 | [diff] [blame] | 545 | uint8_t vsum[HASH_MAX_DIGEST_SIZE]; |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 546 | void *buf; |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 547 | |
Hung-ying Tyan | f061b1a | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 548 | if (hash_lookup_algo(algo_name, &algo)) { |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 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 | } |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 558 | |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 559 | output = memalign(ARCH_DMA_MINALIGN, |
| 560 | sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE); |
| 561 | |
Simon Glass | 68f1556 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 562 | buf = map_sysmem(addr, len); |
| 563 | algo->hash_func_ws(buf, len, output, algo->chunk_size); |
| 564 | unmap_sysmem(buf); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 565 | |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 566 | /* Try to avoid code bloat when verify is not needed */ |
Daniel Thompson | a9e2c67 | 2017-05-19 17:26:58 +0100 | [diff] [blame] | 567 | #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \ |
| 568 | defined(CONFIG_HASH_VERIFY) |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 569 | if (flags & HASH_FLAG_VERIFY) { |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 570 | #else |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 571 | if (0) { |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 572 | #endif |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 573 | if (parse_verify_sum(algo, *argv, vsum, |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 574 | flags & HASH_FLAG_ENV)) { |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 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; |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 581 | |
Simon Glass | 7f3fa54 | 2014-06-02 22:04:49 -0600 | [diff] [blame] | 582 | hash_show(algo, addr, len, output); |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 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 { |
Simon Glass | 7f3fa54 | 2014-06-02 22:04:49 -0600 | [diff] [blame] | 590 | hash_show(algo, addr, len, output); |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 591 | printf("\n"); |
| 592 | |
| 593 | if (argc) { |
| 594 | store_result(algo, output, *argv, |
| 595 | flags & HASH_FLAG_ENV); |
| 596 | } |
Breno Lima | 45e6512 | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 597 | unmap_sysmem(output); |
| 598 | |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 599 | } |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 600 | |
| 601 | /* Horrible code size hack for boards that just want crc32 */ |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 602 | } else { |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 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); |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 610 | |
Tom Rini | 62780f0 | 2013-11-07 07:39:48 -0500 | [diff] [blame] | 611 | if (argc >= 3) { |
| 612 | ptr = (ulong *)simple_strtoul(argv[0], NULL, 16); |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 613 | *ptr = crc; |
Simon Glass | 80e345a | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 614 | } |
Simon Glass | b879045 | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
Simon Glass | b22ec7a | 2017-05-17 09:05:34 -0600 | [diff] [blame] | 619 | #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */ |
| 620 | #endif /* !USE_HOSTCC */ |