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