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 <command.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 14 | #include <env.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 16 | #include <malloc.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 17 | #include <mapmem.h> |
Akshay Saraswat | 1f9c928 | 2013-03-20 21:00:58 +0000 | [diff] [blame] | 18 | #include <hw_sha.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 19 | #include <asm/cache.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 20 | #include <asm/global_data.h> |
Simon Glass | bd091b6 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 21 | #include <asm/io.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 22 | #include <linux/errno.h> |
Ruchika Gupta | 2dd9002 | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 23 | #else |
| 24 | #include "mkimage.h" |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 25 | #include <linux/compiler_attributes.h> |
Ruchika Gupta | 2dd9002 | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 26 | #include <time.h> |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 27 | #include <linux/kconfig.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 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 38 | static int __maybe_unused hash_init_sha1(struct hash_algo *algo, void **ctxp) |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 39 | { |
| 40 | sha1_context *ctx = malloc(sizeof(sha1_context)); |
| 41 | sha1_starts(ctx); |
| 42 | *ctxp = ctx; |
| 43 | return 0; |
| 44 | } |
| 45 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 46 | static int __maybe_unused hash_update_sha1(struct hash_algo *algo, void *ctx, |
| 47 | const void *buf, unsigned int size, |
| 48 | int is_last) |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 49 | { |
| 50 | sha1_update((sha1_context *)ctx, buf, size); |
| 51 | return 0; |
| 52 | } |
| 53 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 54 | static int __maybe_unused hash_finish_sha1(struct hash_algo *algo, void *ctx, |
| 55 | void *dest_buf, int size) |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 56 | { |
| 57 | if (size < algo->digest_size) |
| 58 | return -1; |
| 59 | |
| 60 | sha1_finish((sha1_context *)ctx, dest_buf); |
| 61 | free(ctx); |
| 62 | return 0; |
| 63 | } |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 64 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 65 | static int __maybe_unused hash_init_sha256(struct hash_algo *algo, void **ctxp) |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 66 | { |
| 67 | sha256_context *ctx = malloc(sizeof(sha256_context)); |
| 68 | sha256_starts(ctx); |
| 69 | *ctxp = ctx; |
| 70 | return 0; |
| 71 | } |
| 72 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 73 | static int __maybe_unused hash_update_sha256(struct hash_algo *algo, void *ctx, |
| 74 | const void *buf, uint size, |
| 75 | int is_last) |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 76 | { |
| 77 | sha256_update((sha256_context *)ctx, buf, size); |
| 78 | return 0; |
| 79 | } |
| 80 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 81 | static int __maybe_unused hash_finish_sha256(struct hash_algo *algo, void *ctx, |
| 82 | void *dest_buf, int size) |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 83 | { |
| 84 | if (size < algo->digest_size) |
| 85 | return -1; |
| 86 | |
| 87 | sha256_finish((sha256_context *)ctx, dest_buf); |
| 88 | free(ctx); |
| 89 | return 0; |
| 90 | } |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 91 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 92 | static int __maybe_unused hash_init_sha384(struct hash_algo *algo, void **ctxp) |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 93 | { |
| 94 | sha512_context *ctx = malloc(sizeof(sha512_context)); |
| 95 | sha384_starts(ctx); |
| 96 | *ctxp = ctx; |
| 97 | return 0; |
| 98 | } |
| 99 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 100 | static int __maybe_unused hash_update_sha384(struct hash_algo *algo, void *ctx, |
| 101 | const void *buf, uint size, |
| 102 | int is_last) |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 103 | { |
| 104 | sha384_update((sha512_context *)ctx, buf, size); |
| 105 | return 0; |
| 106 | } |
| 107 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 108 | static int __maybe_unused hash_finish_sha384(struct hash_algo *algo, void *ctx, |
| 109 | void *dest_buf, int size) |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 110 | { |
| 111 | if (size < algo->digest_size) |
| 112 | return -1; |
| 113 | |
| 114 | sha384_finish((sha512_context *)ctx, dest_buf); |
| 115 | free(ctx); |
| 116 | return 0; |
| 117 | } |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 118 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 119 | static int __maybe_unused hash_init_sha512(struct hash_algo *algo, void **ctxp) |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 120 | { |
| 121 | sha512_context *ctx = malloc(sizeof(sha512_context)); |
| 122 | sha512_starts(ctx); |
| 123 | *ctxp = ctx; |
| 124 | return 0; |
| 125 | } |
| 126 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 127 | static int __maybe_unused hash_update_sha512(struct hash_algo *algo, void *ctx, |
| 128 | const void *buf, uint size, |
| 129 | int is_last) |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 130 | { |
| 131 | sha512_update((sha512_context *)ctx, buf, size); |
| 132 | return 0; |
| 133 | } |
| 134 | |
Simon Glass | d54f7e3 | 2021-09-25 19:43:19 -0600 | [diff] [blame] | 135 | static int __maybe_unused hash_finish_sha512(struct hash_algo *algo, void *ctx, |
| 136 | void *dest_buf, int size) |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 137 | { |
| 138 | if (size < algo->digest_size) |
| 139 | return -1; |
| 140 | |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 141 | sha512_finish((sha512_context *)ctx, dest_buf); |
| 142 | free(ctx); |
| 143 | return 0; |
| 144 | } |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 145 | |
Philipp Tomsich | 51c2345 | 2018-11-25 19:22:19 +0100 | [diff] [blame] | 146 | static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp) |
| 147 | { |
| 148 | uint16_t *ctx = malloc(sizeof(uint16_t)); |
| 149 | *ctx = 0; |
| 150 | *ctxp = ctx; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int hash_update_crc16_ccitt(struct hash_algo *algo, void *ctx, |
| 155 | const void *buf, unsigned int size, |
| 156 | int is_last) |
| 157 | { |
| 158 | *((uint16_t *)ctx) = crc16_ccitt(*((uint16_t *)ctx), buf, size); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int hash_finish_crc16_ccitt(struct hash_algo *algo, void *ctx, |
| 163 | void *dest_buf, int size) |
| 164 | { |
| 165 | if (size < algo->digest_size) |
| 166 | return -1; |
| 167 | |
| 168 | *((uint16_t *)dest_buf) = *((uint16_t *)ctx); |
| 169 | free(ctx); |
| 170 | return 0; |
| 171 | } |
| 172 | |
Simon Glass | e7d285b | 2021-09-25 19:43:24 -0600 | [diff] [blame] | 173 | static int __maybe_unused hash_init_crc32(struct hash_algo *algo, void **ctxp) |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 174 | { |
| 175 | uint32_t *ctx = malloc(sizeof(uint32_t)); |
| 176 | *ctx = 0; |
| 177 | *ctxp = ctx; |
| 178 | return 0; |
| 179 | } |
| 180 | |
Simon Glass | e7d285b | 2021-09-25 19:43:24 -0600 | [diff] [blame] | 181 | static int __maybe_unused hash_update_crc32(struct hash_algo *algo, void *ctx, |
| 182 | const void *buf, unsigned int size, |
| 183 | int is_last) |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 184 | { |
| 185 | *((uint32_t *)ctx) = crc32(*((uint32_t *)ctx), buf, size); |
| 186 | return 0; |
| 187 | } |
| 188 | |
Simon Glass | e7d285b | 2021-09-25 19:43:24 -0600 | [diff] [blame] | 189 | static int __maybe_unused hash_finish_crc32(struct hash_algo *algo, void *ctx, |
| 190 | void *dest_buf, int size) |
Hung-ying Tyan | bf007eb | 2014-03-03 12:19:28 +0100 | [diff] [blame] | 191 | { |
| 192 | if (size < algo->digest_size) |
| 193 | return -1; |
| 194 | |
| 195 | *((uint32_t *)dest_buf) = *((uint32_t *)ctx); |
| 196 | free(ctx); |
| 197 | return 0; |
| 198 | } |
| 199 | |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 200 | /* |
Tom Rini | 78eda89 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 201 | * These are the hash algorithms we support. If we have hardware acceleration |
| 202 | * is enable we will use that, otherwise a software version of the algorithm. |
| 203 | * Note that algorithm names must be in lower case. |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 204 | */ |
| 205 | static struct hash_algo hash_algo[] = { |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 206 | #if CONFIG_IS_ENABLED(MD5) |
Alexandru Gagniuc | fe54aea | 2021-09-02 19:54:20 -0500 | [diff] [blame] | 207 | { |
| 208 | .name = "md5", |
| 209 | .digest_size = MD5_SUM_LEN, |
| 210 | .chunk_size = CHUNKSZ_MD5, |
| 211 | .hash_func_ws = md5_wd, |
| 212 | }, |
| 213 | #endif |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 214 | #if CONFIG_IS_ENABLED(SHA1) |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 215 | { |
Wolfgang Denk | 0cf207e | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 216 | .name = "sha1", |
Tom Rini | 78eda89 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 217 | .digest_size = SHA1_SUM_LEN, |
| 218 | .chunk_size = CHUNKSZ_SHA1, |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 219 | #if CONFIG_IS_ENABLED(SHA_HW_ACCEL) |
Tom Rini | 78eda89 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 220 | .hash_func_ws = hw_sha1, |
| 221 | #else |
| 222 | .hash_func_ws = sha1_csum_wd, |
| 223 | #endif |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 224 | #if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL) |
Tom Rini | 78eda89 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 225 | .hash_init = hw_sha_init, |
| 226 | .hash_update = hw_sha_update, |
| 227 | .hash_finish = hw_sha_finish, |
| 228 | #else |
| 229 | .hash_init = hash_init_sha1, |
| 230 | .hash_update = hash_update_sha1, |
| 231 | .hash_finish = hash_finish_sha1, |
| 232 | #endif |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 233 | }, |
| 234 | #endif |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 235 | #if CONFIG_IS_ENABLED(SHA256) |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 236 | { |
Tom Rini | 78eda89 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 237 | .name = "sha256", |
| 238 | .digest_size = SHA256_SUM_LEN, |
| 239 | .chunk_size = CHUNKSZ_SHA256, |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 240 | #if CONFIG_IS_ENABLED(SHA_HW_ACCEL) |
Tom Rini | 78eda89 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 241 | .hash_func_ws = hw_sha256, |
| 242 | #else |
| 243 | .hash_func_ws = sha256_csum_wd, |
| 244 | #endif |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 245 | #if CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL) |
Tom Rini | 78eda89 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 246 | .hash_init = hw_sha_init, |
| 247 | .hash_update = hw_sha_update, |
| 248 | .hash_finish = hw_sha_finish, |
| 249 | #else |
| 250 | .hash_init = hash_init_sha256, |
| 251 | .hash_update = hash_update_sha256, |
| 252 | .hash_finish = hash_finish_sha256, |
| 253 | #endif |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 254 | }, |
| 255 | #endif |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 256 | #if CONFIG_IS_ENABLED(SHA384) |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 257 | { |
| 258 | .name = "sha384", |
| 259 | .digest_size = SHA384_SUM_LEN, |
| 260 | .chunk_size = CHUNKSZ_SHA384, |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 261 | #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) |
Joel Stanley | a479f10 | 2021-02-17 13:50:42 +1030 | [diff] [blame] | 262 | .hash_func_ws = hw_sha384, |
| 263 | #else |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 264 | .hash_func_ws = sha384_csum_wd, |
Joel Stanley | a479f10 | 2021-02-17 13:50:42 +1030 | [diff] [blame] | 265 | #endif |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 266 | #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL) |
Joel Stanley | a479f10 | 2021-02-17 13:50:42 +1030 | [diff] [blame] | 267 | .hash_init = hw_sha_init, |
| 268 | .hash_update = hw_sha_update, |
| 269 | .hash_finish = hw_sha_finish, |
| 270 | #else |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 271 | .hash_init = hash_init_sha384, |
| 272 | .hash_update = hash_update_sha384, |
| 273 | .hash_finish = hash_finish_sha384, |
Joel Stanley | a479f10 | 2021-02-17 13:50:42 +1030 | [diff] [blame] | 274 | #endif |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 275 | }, |
| 276 | #endif |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 277 | #if CONFIG_IS_ENABLED(SHA512) |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 278 | { |
| 279 | .name = "sha512", |
| 280 | .digest_size = SHA512_SUM_LEN, |
| 281 | .chunk_size = CHUNKSZ_SHA512, |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 282 | #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) |
Joel Stanley | a479f10 | 2021-02-17 13:50:42 +1030 | [diff] [blame] | 283 | .hash_func_ws = hw_sha512, |
| 284 | #else |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 285 | .hash_func_ws = sha512_csum_wd, |
Joel Stanley | a479f10 | 2021-02-17 13:50:42 +1030 | [diff] [blame] | 286 | #endif |
Simon Glass | 2c21256 | 2021-09-25 19:43:18 -0600 | [diff] [blame] | 287 | #if CONFIG_IS_ENABLED(SHA512_HW_ACCEL) && CONFIG_IS_ENABLED(SHA_PROG_HW_ACCEL) |
Joel Stanley | a479f10 | 2021-02-17 13:50:42 +1030 | [diff] [blame] | 288 | .hash_init = hw_sha_init, |
| 289 | .hash_update = hw_sha_update, |
| 290 | .hash_finish = hw_sha_finish, |
| 291 | #else |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 292 | .hash_init = hash_init_sha512, |
| 293 | .hash_update = hash_update_sha512, |
| 294 | .hash_finish = hash_finish_sha512, |
Joel Stanley | a479f10 | 2021-02-17 13:50:42 +1030 | [diff] [blame] | 295 | #endif |
Reuben Dowle | d16b38f | 2020-04-16 17:36:52 +1200 | [diff] [blame] | 296 | }, |
| 297 | #endif |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 298 | { |
Philipp Tomsich | 51c2345 | 2018-11-25 19:22:19 +0100 | [diff] [blame] | 299 | .name = "crc16-ccitt", |
| 300 | .digest_size = 2, |
| 301 | .chunk_size = CHUNKSZ, |
| 302 | .hash_func_ws = crc16_ccitt_wd_buf, |
| 303 | .hash_init = hash_init_crc16_ccitt, |
| 304 | .hash_update = hash_update_crc16_ccitt, |
| 305 | .hash_finish = hash_finish_crc16_ccitt, |
| 306 | }, |
Simon Glass | e7d285b | 2021-09-25 19:43:24 -0600 | [diff] [blame] | 307 | #if CONFIG_IS_ENABLED(CRC32) |
Philipp Tomsich | 51c2345 | 2018-11-25 19:22:19 +0100 | [diff] [blame] | 308 | { |
Tom Rini | 78eda89 | 2017-08-14 16:38:07 -0400 | [diff] [blame] | 309 | .name = "crc32", |
| 310 | .digest_size = 4, |
| 311 | .chunk_size = CHUNKSZ_CRC32, |
| 312 | .hash_func_ws = crc32_wd_buf, |
| 313 | .hash_init = hash_init_crc32, |
| 314 | .hash_update = hash_update_crc32, |
| 315 | .hash_finish = hash_finish_crc32, |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 316 | }, |
Simon Glass | e7d285b | 2021-09-25 19:43:24 -0600 | [diff] [blame] | 317 | #endif |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 320 | /* Try to minimize code size for boards that don't want much hashing */ |
Simon Glass | 5cbdd15 | 2023-02-05 15:36:42 -0700 | [diff] [blame] | 321 | #if CONFIG_IS_ENABLED(SHA256) || IS_ENABLED(CONFIG_CMD_SHA1SUM) || \ |
Simon Glass | bdb133a | 2023-02-05 15:36:32 -0700 | [diff] [blame] | 322 | CONFIG_IS_ENABLED(CRC32_VERIFY) || IS_ENABLED(CONFIG_CMD_HASH) || \ |
Igor Opaniuk | 6ec3f92 | 2024-03-02 16:05:48 +0100 | [diff] [blame] | 323 | CONFIG_IS_ENABLED(SHA384) || CONFIG_IS_ENABLED(SHA512) || \ |
| 324 | IS_ENABLED(CONFIG_CMD_MD5SUM) |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 325 | #define multi_hash() 1 |
| 326 | #else |
| 327 | #define multi_hash() 0 |
| 328 | #endif |
| 329 | |
Ruchika Gupta | 2dd9002 | 2015-01-23 16:01:58 +0530 | [diff] [blame] | 330 | int hash_lookup_algo(const char *algo_name, struct hash_algo **algop) |
| 331 | { |
| 332 | int i; |
| 333 | |
| 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 | |
| 350 | for (i = 0; i < ARRAY_SIZE(hash_algo); i++) { |
| 351 | if (!strcmp(algo_name, hash_algo[i].name)) { |
| 352 | if (hash_algo[i].hash_init) { |
| 353 | *algop = &hash_algo[i]; |
| 354 | return 0; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | debug("Unknown hash algorithm '%s'\n", algo_name); |
| 360 | return -EPROTONOSUPPORT; |
| 361 | } |
| 362 | |
| 363 | #ifndef USE_HOSTCC |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 364 | int hash_parse_string(const char *algo_name, const char *str, uint8_t *result) |
| 365 | { |
| 366 | struct hash_algo *algo; |
| 367 | int ret; |
| 368 | int i; |
| 369 | |
| 370 | ret = hash_lookup_algo(algo_name, &algo); |
| 371 | if (ret) |
| 372 | return ret; |
| 373 | |
| 374 | for (i = 0; i < algo->digest_size; i++) { |
| 375 | char chr[3]; |
| 376 | |
Simon Glass | 031725f | 2021-07-24 09:03:28 -0600 | [diff] [blame] | 377 | strlcpy(chr, &str[i * 2], 3); |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 378 | result[i] = hextoul(chr, NULL); |
Stefan Roese | 8f0b1e2 | 2015-05-18 14:08:24 +0200 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
Tom Rini | 48ad68d | 2016-01-05 08:47:48 -0500 | [diff] [blame] | 384 | int hash_block(const char *algo_name, const void *data, unsigned int len, |
| 385 | uint8_t *output, int *output_size) |
| 386 | { |
| 387 | struct hash_algo *algo; |
| 388 | int ret; |
| 389 | |
| 390 | ret = hash_lookup_algo(algo_name, &algo); |
| 391 | if (ret) |
| 392 | return ret; |
| 393 | |
| 394 | if (output_size && *output_size < algo->digest_size) { |
| 395 | debug("Output buffer size %d too small (need %d bytes)", |
| 396 | *output_size, algo->digest_size); |
| 397 | return -ENOSPC; |
| 398 | } |
| 399 | if (output_size) |
| 400 | *output_size = algo->digest_size; |
| 401 | algo->hash_func_ws(data, len, output, algo->chunk_size); |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
Simon Glass | 1d6132e | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 406 | #if !defined(CONFIG_XPL_BUILD) && (defined(CONFIG_CMD_HASH) || \ |
Igor Opaniuk | 6ec3f92 | 2024-03-02 16:05:48 +0100 | [diff] [blame] | 407 | defined(CONFIG_CMD_SHA1SUM) || defined(CONFIG_CMD_CRC32)) || \ |
| 408 | defined(CONFIG_CMD_MD5SUM) |
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 | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 453 | addr = hextoul(dest, NULL); |
Simon Glass | bd091b6 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 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. |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 474 | * Return: 0 if ok, non-zero on error |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 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 | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 493 | addr = hextoul(verify_str, NULL); |
Simon Glass | bd091b6 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 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 | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 538 | addr = hextoul(*argv++, NULL); |
| 539 | len = hextoul(*argv++, NULL); |
Simon Glass | d5b7667 | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 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); |
Sergei Antonov | 497e3a8 | 2023-06-12 22:59:10 +0300 | [diff] [blame] | 560 | if (!output) |
| 561 | return CMD_RET_FAILURE; |
Breno Lima | d7af2ba | 2018-01-17 10:03:45 -0200 | [diff] [blame] | 562 | |
Simon Glass | bd091b6 | 2013-02-24 17:33:31 +0000 | [diff] [blame] | 563 | buf = map_sysmem(addr, len); |
| 564 | algo->hash_func_ws(buf, len, output, algo->chunk_size); |
| 565 | unmap_sysmem(buf); |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 566 | |
| 567 | /* Try to avoid code bloat when verify is not needed */ |
Daniel Thompson | 221a949 | 2017-05-19 17:26:58 +0100 | [diff] [blame] | 568 | #if defined(CONFIG_CRC32_VERIFY) || defined(CONFIG_SHA1SUM_VERIFY) || \ |
Igor Opaniuk | 6ec3f92 | 2024-03-02 16:05:48 +0100 | [diff] [blame] | 569 | defined(CONFIG_MD5SUM_VERIFY) || defined(CONFIG_HASH_VERIFY) |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 570 | if (flags & HASH_FLAG_VERIFY) { |
| 571 | #else |
| 572 | if (0) { |
| 573 | #endif |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 574 | if (parse_verify_sum(algo, *argv, vsum, |
Simon Glass | d5b7667 | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 575 | flags & HASH_FLAG_ENV)) { |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 576 | printf("ERROR: %s does not contain a valid " |
| 577 | "%s sum\n", *argv, algo->name); |
Sergei Antonov | 497e3a8 | 2023-06-12 22:59:10 +0300 | [diff] [blame] | 578 | free(output); |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 579 | return 1; |
| 580 | } |
| 581 | if (memcmp(output, vsum, algo->digest_size) != 0) { |
| 582 | int i; |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 583 | |
Simon Glass | 31890ae | 2014-06-02 22:04:49 -0600 | [diff] [blame] | 584 | hash_show(algo, addr, len, output); |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 585 | printf(" != "); |
| 586 | for (i = 0; i < algo->digest_size; i++) |
| 587 | printf("%02x", vsum[i]); |
| 588 | puts(" ** ERROR **\n"); |
Sergei Antonov | 497e3a8 | 2023-06-12 22:59:10 +0300 | [diff] [blame] | 589 | free(output); |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 590 | return 1; |
| 591 | } |
| 592 | } else { |
Simon Glass | 31890ae | 2014-06-02 22:04:49 -0600 | [diff] [blame] | 593 | hash_show(algo, addr, len, output); |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 594 | printf("\n"); |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 595 | |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 596 | if (argc) { |
| 597 | store_result(algo, output, *argv, |
| 598 | flags & HASH_FLAG_ENV); |
| 599 | } |
| 600 | } |
| 601 | |
Sergei Antonov | 497e3a8 | 2023-06-12 22:59:10 +0300 | [diff] [blame] | 602 | free(output); |
| 603 | |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 604 | /* Horrible code size hack for boards that just want crc32 */ |
| 605 | } else { |
| 606 | ulong crc; |
| 607 | ulong *ptr; |
| 608 | |
| 609 | crc = crc32_wd(0, (const uchar *)addr, len, CHUNKSZ_CRC32); |
| 610 | |
| 611 | printf("CRC32 for %08lx ... %08lx ==> %08lx\n", |
| 612 | addr, addr + len - 1, crc); |
| 613 | |
Tom Rini | 4b756b0 | 2013-11-07 07:39:48 -0500 | [diff] [blame] | 614 | if (argc >= 3) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 615 | ptr = (ulong *)hextoul(argv[0], NULL); |
Simon Glass | d20a40d | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 616 | *ptr = crc; |
Simon Glass | d5b7667 | 2013-02-24 17:33:26 +0000 | [diff] [blame] | 617 | } |
Simon Glass | 460408e | 2012-12-05 14:46:36 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | return 0; |
| 621 | } |
Simon Glass | d70f919 | 2017-05-17 09:05:34 -0600 | [diff] [blame] | 622 | #endif /* CONFIG_CMD_HASH || CONFIG_CMD_SHA1SUM || CONFIG_CMD_CRC32) */ |
| 623 | #endif /* !USE_HOSTCC */ |