rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | From 37c917d965c6dcbc3509b8776d79ad75a9102678 Mon Sep 17 00:00:00 2001 |
| 2 | From: HC Yen <hc.yen@mediatek.com> |
| 3 | Date: Tue, 6 Dec 2016 16:38:46 +0800 |
| 4 | Subject: [PATCH 1/3] rsa: add sha{256,384,512},rsa{2048,3072,4096} algorithms |
| 5 | |
| 6 | Add support for "sha256,rsa3072", "sha384,rsa2048", "sha384,rsa3072", |
| 7 | "sha384,rsa4096", "sha512,rsa2048", "sha512,rsa3072", and |
| 8 | "sha512,rsa4096" signatures in u-boot. |
| 9 | --- |
| 10 | common/hash.c | 71 +++++++ |
| 11 | common/image-fit.c | 9 + |
| 12 | common/image-sig.c | 121 +++++++++++- |
| 13 | include/hash.h | 2 +- |
| 14 | include/image.h | 18 ++ |
| 15 | include/u-boot/rsa-checksum.h | 12 +- |
| 16 | include/u-boot/rsa.h | 1 + |
| 17 | include/u-boot/sha4.h | 159 +++++++++++++++ |
| 18 | lib/Kconfig | 16 ++ |
| 19 | lib/Makefile | 2 + |
| 20 | lib/rsa/rsa-checksum.c | 312 +++++++++++++++++++++++++++++ |
| 21 | lib/sha4.c | 447 ++++++++++++++++++++++++++++++++++++++++++ |
| 22 | tools/Makefile | 1 + |
| 23 | 13 files changed, 1167 insertions(+), 4 deletions(-) |
| 24 | create mode 100644 include/u-boot/sha4.h |
| 25 | create mode 100644 lib/sha4.c |
| 26 | |
| 27 | diff --git a/common/hash.c b/common/hash.c |
| 28 | index b645298..d563292 100644 |
| 29 | --- a/common/hash.c |
| 30 | +++ b/common/hash.c |
| 31 | @@ -28,6 +28,7 @@ |
| 32 | #include <u-boot/crc.h> |
| 33 | #include <u-boot/sha1.h> |
| 34 | #include <u-boot/sha256.h> |
| 35 | +#include <u-boot/sha4.h> |
| 36 | #include <u-boot/md5.h> |
| 37 | |
| 38 | #ifdef CONFIG_SHA1 |
| 39 | @@ -84,6 +85,58 @@ static int hash_finish_sha256(struct hash_algo *algo, void *ctx, void |
| 40 | free(ctx); |
| 41 | return 0; |
| 42 | } |
| 43 | + |
| 44 | +static int hash_init_sha384(struct hash_algo *algo, void **ctxp) |
| 45 | +{ |
| 46 | + sha4_context *ctx = malloc(sizeof(sha4_context)); |
| 47 | + sha4_starts(ctx, 1); |
| 48 | + *ctxp = ctx; |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +static int hash_update_sha384(struct hash_algo *algo, void *ctx, |
| 53 | + const void *buf, unsigned int size, int is_last) |
| 54 | +{ |
| 55 | + sha4_update((sha4_context *)ctx, (unsigned char *)buf, size); |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +static int hash_finish_sha384(struct hash_algo *algo, void *ctx, void |
| 60 | + *dest_buf, int size) |
| 61 | +{ |
| 62 | + if (size < algo->digest_size) |
| 63 | + return -1; |
| 64 | + |
| 65 | + sha4_finish((sha4_context *)ctx, dest_buf); |
| 66 | + free(ctx); |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +static int hash_init_sha512(struct hash_algo *algo, void **ctxp) |
| 71 | +{ |
| 72 | + sha4_context *ctx = malloc(sizeof(sha4_context)); |
| 73 | + sha4_starts(ctx, 0); |
| 74 | + *ctxp = ctx; |
| 75 | + return 0; |
| 76 | +} |
| 77 | + |
| 78 | +static int hash_update_sha512(struct hash_algo *algo, void *ctx, |
| 79 | + const void *buf, unsigned int size, int is_last) |
| 80 | +{ |
| 81 | + sha4_update((sha4_context *)ctx, (unsigned char *)buf, size); |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static int hash_finish_sha512(struct hash_algo *algo, void *ctx, void |
| 86 | + *dest_buf, int size) |
| 87 | +{ |
| 88 | + if (size < algo->digest_size) |
| 89 | + return -1; |
| 90 | + |
| 91 | + sha4_finish((sha4_context *)ctx, dest_buf); |
| 92 | + free(ctx); |
| 93 | + return 0; |
| 94 | +} |
| 95 | #endif |
| 96 | |
| 97 | static int hash_init_crc32(struct hash_algo *algo, void **ctxp) |
| 98 | @@ -166,6 +219,24 @@ static struct hash_algo hash_algo[] = { |
| 99 | hash_update_sha256, |
| 100 | hash_finish_sha256, |
| 101 | }, |
| 102 | + { |
| 103 | + "sha384", |
| 104 | + SHA384_SUM_LEN, |
| 105 | + sha384_csum_wd, |
| 106 | + CHUNKSZ_SHA384, |
| 107 | + hash_init_sha384, |
| 108 | + hash_update_sha384, |
| 109 | + hash_finish_sha384, |
| 110 | + }, |
| 111 | + { |
| 112 | + "sha512", |
| 113 | + SHA512_SUM_LEN, |
| 114 | + sha512_csum_wd, |
| 115 | + CHUNKSZ_SHA512, |
| 116 | + hash_init_sha512, |
| 117 | + hash_update_sha512, |
| 118 | + hash_finish_sha512, |
| 119 | + }, |
| 120 | #endif |
| 121 | { |
| 122 | "crc32", |
| 123 | diff --git a/common/image-fit.c b/common/image-fit.c |
| 124 | index 77dc011..2d19e75 100644 |
| 125 | --- a/common/image-fit.c |
| 126 | +++ b/common/image-fit.c |
| 127 | @@ -27,6 +27,7 @@ DECLARE_GLOBAL_DATA_PTR; |
| 128 | #include <u-boot/md5.h> |
| 129 | #include <u-boot/sha1.h> |
| 130 | #include <u-boot/sha256.h> |
| 131 | +#include <u-boot/sha4.h> |
| 132 | |
| 133 | /*****************************************************************************/ |
| 134 | /* New uImage format routines */ |
| 135 | @@ -931,6 +932,14 @@ int calculate_hash(const void *data, int data_len, const char *algo, |
| 136 | sha256_csum_wd((unsigned char *)data, data_len, |
| 137 | (unsigned char *)value, CHUNKSZ_SHA256); |
| 138 | *value_len = SHA256_SUM_LEN; |
| 139 | + } else if (IMAGE_ENABLE_SHA384 && strcmp(algo, "sha384") == 0) { |
| 140 | + sha384_csum_wd((unsigned char *)data, data_len, |
| 141 | + (unsigned char *)value, CHUNKSZ_SHA384); |
| 142 | + *value_len = SHA384_SUM_LEN; |
| 143 | + } else if (IMAGE_ENABLE_SHA512 && strcmp(algo, "sha512") == 0) { |
| 144 | + sha512_csum_wd((unsigned char *)data, data_len, |
| 145 | + (unsigned char *)value, CHUNKSZ_SHA512); |
| 146 | + *value_len = SHA512_SUM_LEN; |
| 147 | } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { |
| 148 | md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); |
| 149 | *value_len = 16; |
| 150 | diff --git a/common/image-sig.c b/common/image-sig.c |
| 151 | index 28f7a20..57eab9f 100644 |
| 152 | --- a/common/image-sig.c |
| 153 | +++ b/common/image-sig.c |
| 154 | @@ -54,12 +54,82 @@ struct checksum_algo checksum_algos[] = { |
| 155 | { |
| 156 | "sha256", |
| 157 | SHA256_SUM_LEN, |
| 158 | + RSA3072_BYTES, |
| 159 | +#if IMAGE_ENABLE_SIGN |
| 160 | + EVP_sha256, |
| 161 | +#endif |
| 162 | + hash_calculate, |
| 163 | + padding_sha256_rsa3072, |
| 164 | + }, |
| 165 | + { |
| 166 | + "sha256", |
| 167 | + SHA256_SUM_LEN, |
| 168 | RSA4096_BYTES, |
| 169 | #if IMAGE_ENABLE_SIGN |
| 170 | EVP_sha256, |
| 171 | #endif |
| 172 | hash_calculate, |
| 173 | padding_sha256_rsa4096, |
| 174 | + }, |
| 175 | + { |
| 176 | + "sha384", |
| 177 | + SHA384_SUM_LEN, |
| 178 | + RSA2048_BYTES, |
| 179 | +#if IMAGE_ENABLE_SIGN |
| 180 | + EVP_sha384, |
| 181 | +#endif |
| 182 | + hash_calculate, |
| 183 | + padding_sha384_rsa2048, |
| 184 | + }, |
| 185 | + { |
| 186 | + "sha384", |
| 187 | + SHA384_SUM_LEN, |
| 188 | + RSA3072_BYTES, |
| 189 | +#if IMAGE_ENABLE_SIGN |
| 190 | + EVP_sha384, |
| 191 | +#endif |
| 192 | + hash_calculate, |
| 193 | + padding_sha384_rsa3072, |
| 194 | + }, |
| 195 | + { |
| 196 | + "sha384", |
| 197 | + SHA384_SUM_LEN, |
| 198 | + RSA4096_BYTES, |
| 199 | +#if IMAGE_ENABLE_SIGN |
| 200 | + EVP_sha384, |
| 201 | +#endif |
| 202 | + hash_calculate, |
| 203 | + padding_sha384_rsa4096, |
| 204 | + }, |
| 205 | + { |
| 206 | + "sha512", |
| 207 | + SHA512_SUM_LEN, |
| 208 | + RSA2048_BYTES, |
| 209 | +#if IMAGE_ENABLE_SIGN |
| 210 | + EVP_sha512, |
| 211 | +#endif |
| 212 | + hash_calculate, |
| 213 | + padding_sha512_rsa2048, |
| 214 | + }, |
| 215 | + { |
| 216 | + "sha512", |
| 217 | + SHA512_SUM_LEN, |
| 218 | + RSA3072_BYTES, |
| 219 | +#if IMAGE_ENABLE_SIGN |
| 220 | + EVP_sha512, |
| 221 | +#endif |
| 222 | + hash_calculate, |
| 223 | + padding_sha512_rsa3072, |
| 224 | + }, |
| 225 | + { |
| 226 | + "sha512", |
| 227 | + SHA512_SUM_LEN, |
| 228 | + RSA4096_BYTES, |
| 229 | +#if IMAGE_ENABLE_SIGN |
| 230 | + EVP_sha512, |
| 231 | +#endif |
| 232 | + hash_calculate, |
| 233 | + padding_sha512_rsa4096, |
| 234 | } |
| 235 | |
| 236 | }; |
| 237 | @@ -80,11 +150,60 @@ struct image_sig_algo image_sig_algos[] = { |
| 238 | &checksum_algos[1], |
| 239 | }, |
| 240 | { |
| 241 | - "sha256,rsa4096", |
| 242 | + "sha256,rsa3072", |
| 243 | rsa_sign, |
| 244 | rsa_add_verify_data, |
| 245 | rsa_verify, |
| 246 | &checksum_algos[2], |
| 247 | + }, |
| 248 | + { |
| 249 | + "sha256,rsa4096", |
| 250 | + rsa_sign, |
| 251 | + rsa_add_verify_data, |
| 252 | + rsa_verify, |
| 253 | + &checksum_algos[3], |
| 254 | + }, |
| 255 | + { |
| 256 | + "sha384,rsa2048", |
| 257 | + rsa_sign, |
| 258 | + rsa_add_verify_data, |
| 259 | + rsa_verify, |
| 260 | + &checksum_algos[4], |
| 261 | + }, |
| 262 | + { |
| 263 | + "sha384,rsa3072", |
| 264 | + rsa_sign, |
| 265 | + rsa_add_verify_data, |
| 266 | + rsa_verify, |
| 267 | + &checksum_algos[5], |
| 268 | + }, |
| 269 | + { |
| 270 | + "sha384,rsa4096", |
| 271 | + rsa_sign, |
| 272 | + rsa_add_verify_data, |
| 273 | + rsa_verify, |
| 274 | + &checksum_algos[6], |
| 275 | + }, |
| 276 | + { |
| 277 | + "sha512,rsa2048", |
| 278 | + rsa_sign, |
| 279 | + rsa_add_verify_data, |
| 280 | + rsa_verify, |
| 281 | + &checksum_algos[7], |
| 282 | + }, |
| 283 | + { |
| 284 | + "sha512,rsa3072", |
| 285 | + rsa_sign, |
| 286 | + rsa_add_verify_data, |
| 287 | + rsa_verify, |
| 288 | + &checksum_algos[8], |
| 289 | + }, |
| 290 | + { |
| 291 | + "sha512,rsa4096", |
| 292 | + rsa_sign, |
| 293 | + rsa_add_verify_data, |
| 294 | + rsa_verify, |
| 295 | + &checksum_algos[9], |
| 296 | } |
| 297 | |
| 298 | }; |
| 299 | diff --git a/include/hash.h b/include/hash.h |
| 300 | index d814337..59ba707 100644 |
| 301 | --- a/include/hash.h |
| 302 | +++ b/include/hash.h |
| 303 | @@ -10,7 +10,7 @@ |
| 304 | * Maximum digest size for all algorithms we support. Having this value |
| 305 | * avoids a malloc() or C99 local declaration in common/cmd_hash.c. |
| 306 | */ |
| 307 | -#define HASH_MAX_DIGEST_SIZE 32 |
| 308 | +#define HASH_MAX_DIGEST_SIZE 64 |
| 309 | |
| 310 | enum { |
| 311 | HASH_FLAG_VERIFY = 1 << 0, /* Enable verify mode */ |
| 312 | diff --git a/include/image.h b/include/image.h |
| 313 | index 2b1296c..81735e8 100644 |
| 314 | --- a/include/image.h |
| 315 | +++ b/include/image.h |
| 316 | @@ -65,15 +65,25 @@ struct lmb; |
| 317 | # ifdef CONFIG_SPL_SHA256_SUPPORT |
| 318 | # define IMAGE_ENABLE_SHA256 1 |
| 319 | # endif |
| 320 | +# ifdef CONFIG_SPL_SHA384_SUPPORT |
| 321 | +# define IMAGE_ENABLE_SHA384 1 |
| 322 | +# endif |
| 323 | +# ifdef CONFIG_SPL_SHA512_SUPPORT |
| 324 | +# define IMAGE_ENABLE_SHA512 1 |
| 325 | +# endif |
| 326 | # else |
| 327 | # define CONFIG_CRC32 /* FIT images need CRC32 support */ |
| 328 | # define CONFIG_MD5 /* and MD5 */ |
| 329 | # define CONFIG_SHA1 /* and SHA1 */ |
| 330 | # define CONFIG_SHA256 /* and SHA256 */ |
| 331 | +# define CONFIG_SHA384 /* and SHA384 */ |
| 332 | +# define CONFIG_SHA512 /* and SHA512 */ |
| 333 | # define IMAGE_ENABLE_CRC32 1 |
| 334 | # define IMAGE_ENABLE_MD5 1 |
| 335 | # define IMAGE_ENABLE_SHA1 1 |
| 336 | # define IMAGE_ENABLE_SHA256 1 |
| 337 | +# define IMAGE_ENABLE_SHA384 1 |
| 338 | +# define IMAGE_ENABLE_SHA512 1 |
| 339 | # endif |
| 340 | |
| 341 | #ifdef CONFIG_FIT_DISABLE_SHA256 |
| 342 | @@ -97,6 +107,14 @@ struct lmb; |
| 343 | #define IMAGE_ENABLE_SHA256 0 |
| 344 | #endif |
| 345 | |
| 346 | +#ifndef IMAGE_ENABLE_SHA384 |
| 347 | +#define IMAGE_ENABLE_SHA384 0 |
| 348 | +#endif |
| 349 | + |
| 350 | +#ifndef IMAGE_ENABLE_SHA512 |
| 351 | +#define IMAGE_ENABLE_SHA512 0 |
| 352 | +#endif |
| 353 | + |
| 354 | #endif /* IMAGE_ENABLE_FIT */ |
| 355 | |
| 356 | #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH |
| 357 | diff --git a/include/u-boot/rsa-checksum.h b/include/u-boot/rsa-checksum.h |
| 358 | index 3c69d85..d8c584a 100644 |
| 359 | --- a/include/u-boot/rsa-checksum.h |
| 360 | +++ b/include/u-boot/rsa-checksum.h |
| 361 | @@ -11,10 +11,18 @@ |
| 362 | #include <image.h> |
| 363 | #include <u-boot/sha1.h> |
| 364 | #include <u-boot/sha256.h> |
| 365 | +#include <u-boot/sha4.h> |
| 366 | |
| 367 | -extern const uint8_t padding_sha256_rsa4096[]; |
| 368 | -extern const uint8_t padding_sha256_rsa2048[]; |
| 369 | extern const uint8_t padding_sha1_rsa2048[]; |
| 370 | +extern const uint8_t padding_sha256_rsa2048[]; |
| 371 | +extern const uint8_t padding_sha256_rsa3072[]; |
| 372 | +extern const uint8_t padding_sha256_rsa4096[]; |
| 373 | +extern const uint8_t padding_sha384_rsa2048[]; |
| 374 | +extern const uint8_t padding_sha384_rsa3072[]; |
| 375 | +extern const uint8_t padding_sha384_rsa4096[]; |
| 376 | +extern const uint8_t padding_sha512_rsa2048[]; |
| 377 | +extern const uint8_t padding_sha512_rsa3072[]; |
| 378 | +extern const uint8_t padding_sha512_rsa4096[]; |
| 379 | |
| 380 | /** |
| 381 | * hash_calculate() - Calculate hash over the data |
| 382 | diff --git a/include/u-boot/rsa.h b/include/u-boot/rsa.h |
| 383 | index 0e96c38..3921250 100644 |
| 384 | --- a/include/u-boot/rsa.h |
| 385 | +++ b/include/u-boot/rsa.h |
| 386 | @@ -108,6 +108,7 @@ static inline int rsa_verify(struct image_sign_info *info, |
| 387 | #endif |
| 388 | |
| 389 | #define RSA2048_BYTES (2048 / 8) |
| 390 | +#define RSA3072_BYTES (3072 / 8) |
| 391 | #define RSA4096_BYTES (4096 / 8) |
| 392 | |
| 393 | /* This is the minimum/maximum key size we support, in bits */ |
| 394 | diff --git a/include/u-boot/sha4.h b/include/u-boot/sha4.h |
| 395 | new file mode 100644 |
| 396 | index 0000000..e9e5865 |
| 397 | --- /dev/null |
| 398 | +++ b/include/u-boot/sha4.h |
| 399 | @@ -0,0 +1,159 @@ |
| 400 | +/** |
| 401 | + * \file sha4.h |
| 402 | + * |
| 403 | + * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 404 | + * |
| 405 | + * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
| 406 | + * |
| 407 | + * All rights reserved. |
| 408 | + * |
| 409 | + * Redistribution and use in source and binary forms, with or without |
| 410 | + * modification, are permitted provided that the following conditions |
| 411 | + * are met: |
| 412 | + * |
| 413 | + * * Redistributions of source code must retain the above copyright |
| 414 | + * notice, this list of conditions and the following disclaimer. |
| 415 | + * * Redistributions in binary form must reproduce the above copyright |
| 416 | + * notice, this list of conditions and the following disclaimer in the |
| 417 | + * documentation and/or other materials provided with the distribution. |
| 418 | + * * Neither the names of PolarSSL or XySSL nor the names of its contributors |
| 419 | + * may be used to endorse or promote products derived from this software |
| 420 | + * without specific prior written permission. |
| 421 | + * |
| 422 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 423 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 424 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 425 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 426 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 427 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 428 | + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 429 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 430 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 431 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 432 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 433 | + */ |
| 434 | +#ifndef _SHA4_H |
| 435 | +#define _SHA4_H |
| 436 | + |
| 437 | +#define SHA384_SUM_LEN 48 |
| 438 | +#define SHA512_SUM_LEN 64 |
| 439 | + |
| 440 | +/* Reset watchdog each time we process this many bytes */ |
| 441 | +#define CHUNKSZ_SHA384 (64 * 1024) |
| 442 | +#define CHUNKSZ_SHA512 (64 * 1024) |
| 443 | + |
| 444 | +#define UL64(x) x##ULL |
| 445 | +#define int64 long long |
| 446 | + |
| 447 | +/** |
| 448 | + * \brief SHA-512 context structure |
| 449 | + */ |
| 450 | +typedef struct { |
| 451 | + unsigned int64 total[2]; /*!< number of bytes processed */ |
| 452 | + unsigned int64 state[8]; /*!< intermediate digest state */ |
| 453 | + unsigned char buffer[128]; /*!< data block being processed */ |
| 454 | + |
| 455 | + unsigned char ipad[128]; /*!< HMAC: inner padding */ |
| 456 | + unsigned char opad[128]; /*!< HMAC: outer padding */ |
| 457 | + int is384; /*!< 0 => SHA-512, else SHA-384 */ |
| 458 | +} sha4_context; |
| 459 | + |
| 460 | +#ifdef __cplusplus |
| 461 | +extern "C" { |
| 462 | +#endif |
| 463 | + |
| 464 | + /** |
| 465 | + * \brief SHA-512 context setup |
| 466 | + * |
| 467 | + * \param ctx context to be initialized |
| 468 | + * \param is384 0 = use SHA512, 1 = use SHA384 |
| 469 | + */ |
| 470 | + void sha4_starts(sha4_context * ctx, int is384); |
| 471 | + |
| 472 | + /** |
| 473 | + * \brief SHA-512 process buffer |
| 474 | + * |
| 475 | + * \param ctx SHA-512 context |
| 476 | + * \param input buffer holding the data |
| 477 | + * \param ilen length of the input data |
| 478 | + */ |
| 479 | + void sha4_update(sha4_context * ctx, unsigned char *input, int ilen); |
| 480 | + |
| 481 | + /** |
| 482 | + * \brief SHA-512 final digest |
| 483 | + * |
| 484 | + * \param ctx SHA-512 context |
| 485 | + * \param output SHA-384/512 checksum result |
| 486 | + */ |
| 487 | + void sha4_finish(sha4_context * ctx, unsigned char output[64]); |
| 488 | + |
| 489 | + /** |
| 490 | + * \brief Output = SHA-512( input buffer ) |
| 491 | + * |
| 492 | + * \param input buffer holding the data |
| 493 | + * \param ilen length of the input data |
| 494 | + * \param output SHA-384/512 checksum result |
| 495 | + * \param is384 0 = use SHA512, 1 = use SHA384 |
| 496 | + */ |
| 497 | + void sha4(unsigned char *input, int ilen, |
| 498 | + unsigned char output[64], int is384); |
| 499 | + |
| 500 | +void sha384_csum_wd(const unsigned char *input, unsigned int ilen, |
| 501 | + unsigned char *output, unsigned int chunk_sz); |
| 502 | +void sha512_csum_wd(const unsigned char *input, unsigned int ilen, |
| 503 | + unsigned char *output, unsigned int chunk_sz); |
| 504 | + |
| 505 | + /** |
| 506 | + * \brief SHA-512 HMAC context setup |
| 507 | + * |
| 508 | + * \param ctx HMAC context to be initialized |
| 509 | + * \param is384 0 = use SHA512, 1 = use SHA384 |
| 510 | + * \param key HMAC secret key |
| 511 | + * \param keylen length of the HMAC key |
| 512 | + */ |
| 513 | + void sha4_hmac_starts(sha4_context * ctx, unsigned char *key, |
| 514 | + int keylen, int is384); |
| 515 | + |
| 516 | + /** |
| 517 | + * \brief SHA-512 HMAC process buffer |
| 518 | + * |
| 519 | + * \param ctx HMAC context |
| 520 | + * \param input buffer holding the data |
| 521 | + * \param ilen length of the input data |
| 522 | + */ |
| 523 | + void sha4_hmac_update(sha4_context * ctx, unsigned char *input, |
| 524 | + int ilen); |
| 525 | + |
| 526 | + /** |
| 527 | + * \brief SHA-512 HMAC final digest |
| 528 | + * |
| 529 | + * \param ctx HMAC context |
| 530 | + * \param output SHA-384/512 HMAC checksum result |
| 531 | + */ |
| 532 | + void sha4_hmac_finish(sha4_context * ctx, unsigned char output[64]); |
| 533 | + |
| 534 | + /** |
| 535 | + * \brief Output = HMAC-SHA-512( hmac key, input buffer ) |
| 536 | + * |
| 537 | + * \param key HMAC secret key |
| 538 | + * \param keylen length of the HMAC key |
| 539 | + * \param input buffer holding the data |
| 540 | + * \param ilen length of the input data |
| 541 | + * \param output HMAC-SHA-384/512 result |
| 542 | + * \param is384 0 = use SHA512, 1 = use SHA384 |
| 543 | + */ |
| 544 | + void sha4_hmac(unsigned char *key, int keylen, |
| 545 | + unsigned char *input, int ilen, |
| 546 | + unsigned char output[64], int is384); |
| 547 | + |
| 548 | + /** |
| 549 | + * \brief Checkup routine |
| 550 | + * |
| 551 | + * \return 0 if successful, or 1 if the test failed |
| 552 | + */ |
| 553 | + int sha4_self_test(int verbose); |
| 554 | + |
| 555 | +#ifdef __cplusplus |
| 556 | +} |
| 557 | +#endif |
| 558 | +#endif /* sha4.h */ |
| 559 | diff --git a/lib/Kconfig b/lib/Kconfig |
| 560 | index b16062f..1cba1b5 100644 |
| 561 | --- a/lib/Kconfig |
| 562 | +++ b/lib/Kconfig |
| 563 | @@ -85,6 +85,22 @@ config SHA256 |
| 564 | The SHA256 algorithm produces a 256-bit (32-byte) hash value |
| 565 | (digest). |
| 566 | |
| 567 | +config SHA384 |
| 568 | + bool "Enable SHA384 support" |
| 569 | + help |
| 570 | + This option enables support of hashing using SHA384 algorithm. |
| 571 | + The hash is calculated in software. |
| 572 | + The SHA384 algorithm produces a 384-bit (48-byte) hash value |
| 573 | + (digest). |
| 574 | + |
| 575 | +config SHA512 |
| 576 | + bool "Enable SHA512 support" |
| 577 | + help |
| 578 | + This option enables support of hashing using SHA512 algorithm. |
| 579 | + The hash is calculated in software. |
| 580 | + The SHA512 algorithm produces a 512-bit (64-byte) hash value |
| 581 | + (digest). |
| 582 | + |
| 583 | config SHA_HW_ACCEL |
| 584 | bool "Enable hashing using hardware" |
| 585 | help |
| 586 | diff --git a/lib/Makefile b/lib/Makefile |
| 587 | index 23e9f1e..ba5ba04 100644 |
| 588 | --- a/lib/Makefile |
| 589 | +++ b/lib/Makefile |
| 590 | @@ -47,6 +47,8 @@ endif |
| 591 | obj-$(CONFIG_$(SPL_)RSA) += rsa/ |
| 592 | obj-$(CONFIG_$(SPL_)SHA1) += sha1.o |
| 593 | obj-$(CONFIG_$(SPL_)SHA256) += sha256.o |
| 594 | +obj-$(CONFIG_$(SPL_)SHA384) += sha4.o |
| 595 | +obj-$(CONFIG_$(SPL_)SHA512) += sha4.o |
| 596 | |
| 597 | obj-$(CONFIG_SPL_SAVEENV) += qsort.o |
| 598 | obj-$(CONFIG_$(SPL_)OF_LIBFDT) += libfdt/ |
| 599 | diff --git a/lib/rsa/rsa-checksum.c b/lib/rsa/rsa-checksum.c |
| 600 | index db183ff..74e289c 100644 |
| 601 | --- a/lib/rsa/rsa-checksum.c |
| 602 | +++ b/lib/rsa/rsa-checksum.c |
| 603 | @@ -15,6 +15,7 @@ |
| 604 | #include "fdt_host.h" |
| 605 | #include <u-boot/sha1.h> |
| 606 | #include <u-boot/sha256.h> |
| 607 | +#include <u-boot/sha4.h> |
| 608 | #endif |
| 609 | #include <u-boot/rsa.h> |
| 610 | |
| 611 | @@ -74,6 +75,53 @@ const uint8_t padding_sha1_rsa2048[RSA2048_BYTES - SHA1_SUM_LEN] = { |
| 612 | 0x05, 0x00, 0x04, 0x14 |
| 613 | }; |
| 614 | |
| 615 | +const uint8_t padding_sha256_rsa3072[RSA3072_BYTES - SHA256_SUM_LEN] = { |
| 616 | + 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 617 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 618 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 619 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 620 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 621 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 622 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 623 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 624 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 625 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 626 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 627 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 628 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 629 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 630 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 631 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 632 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 633 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 634 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 635 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 636 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 637 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 638 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 639 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 640 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 641 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 642 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 643 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 644 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 645 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 646 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 647 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 648 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 649 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 650 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 651 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 652 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 653 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 654 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 655 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 656 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 657 | + 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x31, 0x30, |
| 658 | + 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, |
| 659 | + 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 |
| 660 | +}; |
| 661 | + |
| 662 | const uint8_t padding_sha256_rsa4096[RSA4096_BYTES - SHA256_SUM_LEN] = { |
| 663 | 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 664 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 665 | @@ -137,6 +185,270 @@ const uint8_t padding_sha256_rsa4096[RSA4096_BYTES - SHA256_SUM_LEN] = { |
| 666 | 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 |
| 667 | }; |
| 668 | |
| 669 | +const uint8_t padding_sha384_rsa2048[RSA2048_BYTES - SHA384_SUM_LEN] = { |
| 670 | + 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 671 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 672 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 673 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 674 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 675 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 676 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 677 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 678 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 679 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 680 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 681 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 682 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 683 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 684 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 685 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 686 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 687 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 688 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 689 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 690 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 691 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 692 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 693 | + 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x41, 0x30, |
| 694 | + 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, |
| 695 | + 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 |
| 696 | +}; |
| 697 | + |
| 698 | +const uint8_t padding_sha384_rsa3072[RSA3072_BYTES - SHA384_SUM_LEN] = { |
| 699 | + 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 700 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 701 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 702 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 703 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 704 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 705 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 706 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 707 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 708 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 709 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 710 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 711 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 712 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 713 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 714 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 715 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 716 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 717 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 718 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 719 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 720 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 721 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 722 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 723 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 724 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 725 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 726 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 727 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 728 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 729 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 730 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 731 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 732 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 733 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 734 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 735 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 736 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 737 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 738 | + 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x41, 0x30, |
| 739 | + 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, |
| 740 | + 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 |
| 741 | +}; |
| 742 | + |
| 743 | +const uint8_t padding_sha384_rsa4096[RSA4096_BYTES - SHA384_SUM_LEN] = { |
| 744 | + 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 745 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 746 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 747 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 748 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 749 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 750 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 751 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 752 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 753 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 754 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 755 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 756 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 757 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 758 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 759 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 760 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 761 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 762 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 763 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 764 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 765 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 766 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 767 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 768 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 769 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 770 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 771 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 772 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 773 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 774 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 775 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 776 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 777 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 778 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 779 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 780 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 781 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 782 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 783 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 784 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 785 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 786 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 787 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 788 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 789 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 790 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 791 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 792 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 793 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 794 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 795 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 796 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 797 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 798 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 799 | + 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x41, 0x30, |
| 800 | + 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, |
| 801 | + 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 |
| 802 | +}; |
| 803 | + |
| 804 | +const uint8_t padding_sha512_rsa2048[RSA2048_BYTES - SHA512_SUM_LEN] = { |
| 805 | + 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 806 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 807 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 808 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 809 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 810 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 811 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 812 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 813 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 814 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 815 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 816 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 817 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 818 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 819 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 820 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 821 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 822 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 823 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 824 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 825 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 826 | + 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x51, 0x30, |
| 827 | + 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, |
| 828 | + 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 |
| 829 | +}; |
| 830 | + |
| 831 | +const uint8_t padding_sha512_rsa3072[RSA3072_BYTES - SHA512_SUM_LEN] = { |
| 832 | + 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 833 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 834 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 835 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 836 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 837 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 838 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 839 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 840 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 841 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 842 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 843 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 844 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 845 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 846 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 847 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 848 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 849 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 850 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 851 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 852 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 853 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 854 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 855 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 856 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 857 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 858 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 859 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 860 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 861 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 862 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 863 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 864 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 865 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 866 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 867 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 868 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 869 | + 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x51, 0x30, |
| 870 | + 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, |
| 871 | + 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 |
| 872 | +}; |
| 873 | + |
| 874 | +const uint8_t padding_sha512_rsa4096[RSA4096_BYTES - SHA512_SUM_LEN] = { |
| 875 | + 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 876 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 877 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 878 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 879 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 880 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 881 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 882 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 883 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 884 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 885 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 886 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 887 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 888 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 889 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 890 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 891 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 892 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 893 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 894 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 895 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 896 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 897 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 898 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 899 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 900 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 901 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 902 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 903 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 904 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 905 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 906 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 907 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 908 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 909 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 910 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 911 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 912 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 913 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 914 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 915 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 916 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 917 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 918 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 919 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 920 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 921 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 922 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 923 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 924 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 925 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 926 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 927 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 928 | + 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x51, 0x30, |
| 929 | + 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, |
| 930 | + 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 |
| 931 | +}; |
| 932 | + |
| 933 | int hash_calculate(const char *name, |
| 934 | const struct image_region region[], |
| 935 | int region_count, uint8_t *checksum) |
| 936 | diff --git a/lib/sha4.c b/lib/sha4.c |
| 937 | new file mode 100644 |
| 938 | index 0000000..929d267 |
| 939 | --- /dev/null |
| 940 | +++ b/lib/sha4.c |
| 941 | @@ -0,0 +1,447 @@ |
| 942 | +/* |
| 943 | + * FIPS-180-2 compliant SHA-384/512 implementation |
| 944 | + * |
| 945 | + * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 946 | + * |
| 947 | + * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
| 948 | + * |
| 949 | + * All rights reserved. |
| 950 | + * |
| 951 | + * Redistribution and use in source and binary forms, with or without |
| 952 | + * modification, are permitted provided that the following conditions |
| 953 | + * are met: |
| 954 | + * |
| 955 | + * * Redistributions of source code must retain the above copyright |
| 956 | + * notice, this list of conditions and the following disclaimer. |
| 957 | + * * Redistributions in binary form must reproduce the above copyright |
| 958 | + * notice, this list of conditions and the following disclaimer in the |
| 959 | + * documentation and/or other materials provided with the distribution. |
| 960 | + * * Neither the names of PolarSSL or XySSL nor the names of its contributors |
| 961 | + * may be used to endorse or promote products derived from this software |
| 962 | + * without specific prior written permission. |
| 963 | + * |
| 964 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 965 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 966 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 967 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 968 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 969 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 970 | + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 971 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 972 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 973 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 974 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 975 | + */ |
| 976 | +/* |
| 977 | + * The SHA-512 Secure Hash Standard was published by NIST in 2002. |
| 978 | + * |
| 979 | + * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
| 980 | + */ |
| 981 | + |
| 982 | +#ifndef USE_HOSTCC |
| 983 | +#include <common.h> |
| 984 | +#include <linux/string.h> |
| 985 | +#else |
| 986 | +#include <string.h> |
| 987 | +#endif /* USE_HOSTCC */ |
| 988 | +#include <watchdog.h> |
| 989 | +#include <u-boot/sha4.h> |
| 990 | + |
| 991 | +/* |
| 992 | + * 64-bit integer manipulation macros (big endian) |
| 993 | + */ |
| 994 | +#ifndef GET_UINT64_BE |
| 995 | +#define GET_UINT64_BE(n,b,i) \ |
| 996 | + { \ |
| 997 | + (n) = ( (unsigned int64) (b)[(i) ] << 56 ) \ |
| 998 | + | ( (unsigned int64) (b)[(i) + 1] << 48 ) \ |
| 999 | + | ( (unsigned int64) (b)[(i) + 2] << 40 ) \ |
| 1000 | + | ( (unsigned int64) (b)[(i) + 3] << 32 ) \ |
| 1001 | + | ( (unsigned int64) (b)[(i) + 4] << 24 ) \ |
| 1002 | + | ( (unsigned int64) (b)[(i) + 5] << 16 ) \ |
| 1003 | + | ( (unsigned int64) (b)[(i) + 6] << 8 ) \ |
| 1004 | + | ( (unsigned int64) (b)[(i) + 7] ); \ |
| 1005 | + } |
| 1006 | +#endif |
| 1007 | + |
| 1008 | +#ifndef PUT_UINT64_BE |
| 1009 | +#define PUT_UINT64_BE(n,b,i) \ |
| 1010 | + { \ |
| 1011 | + (b)[(i) ] = (unsigned char) ( (n) >> 56 ); \ |
| 1012 | + (b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \ |
| 1013 | + (b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \ |
| 1014 | + (b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \ |
| 1015 | + (b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \ |
| 1016 | + (b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \ |
| 1017 | + (b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \ |
| 1018 | + (b)[(i) + 7] = (unsigned char) ( (n) ); \ |
| 1019 | + } |
| 1020 | +#endif |
| 1021 | + |
| 1022 | +/* |
| 1023 | + * Round constants |
| 1024 | + */ |
| 1025 | +static const unsigned int64 K[80] = { |
| 1026 | + UL64(0x428A2F98D728AE22), UL64(0x7137449123EF65CD), |
| 1027 | + UL64(0xB5C0FBCFEC4D3B2F), UL64(0xE9B5DBA58189DBBC), |
| 1028 | + UL64(0x3956C25BF348B538), UL64(0x59F111F1B605D019), |
| 1029 | + UL64(0x923F82A4AF194F9B), UL64(0xAB1C5ED5DA6D8118), |
| 1030 | + UL64(0xD807AA98A3030242), UL64(0x12835B0145706FBE), |
| 1031 | + UL64(0x243185BE4EE4B28C), UL64(0x550C7DC3D5FFB4E2), |
| 1032 | + UL64(0x72BE5D74F27B896F), UL64(0x80DEB1FE3B1696B1), |
| 1033 | + UL64(0x9BDC06A725C71235), UL64(0xC19BF174CF692694), |
| 1034 | + UL64(0xE49B69C19EF14AD2), UL64(0xEFBE4786384F25E3), |
| 1035 | + UL64(0x0FC19DC68B8CD5B5), UL64(0x240CA1CC77AC9C65), |
| 1036 | + UL64(0x2DE92C6F592B0275), UL64(0x4A7484AA6EA6E483), |
| 1037 | + UL64(0x5CB0A9DCBD41FBD4), UL64(0x76F988DA831153B5), |
| 1038 | + UL64(0x983E5152EE66DFAB), UL64(0xA831C66D2DB43210), |
| 1039 | + UL64(0xB00327C898FB213F), UL64(0xBF597FC7BEEF0EE4), |
| 1040 | + UL64(0xC6E00BF33DA88FC2), UL64(0xD5A79147930AA725), |
| 1041 | + UL64(0x06CA6351E003826F), UL64(0x142929670A0E6E70), |
| 1042 | + UL64(0x27B70A8546D22FFC), UL64(0x2E1B21385C26C926), |
| 1043 | + UL64(0x4D2C6DFC5AC42AED), UL64(0x53380D139D95B3DF), |
| 1044 | + UL64(0x650A73548BAF63DE), UL64(0x766A0ABB3C77B2A8), |
| 1045 | + UL64(0x81C2C92E47EDAEE6), UL64(0x92722C851482353B), |
| 1046 | + UL64(0xA2BFE8A14CF10364), UL64(0xA81A664BBC423001), |
| 1047 | + UL64(0xC24B8B70D0F89791), UL64(0xC76C51A30654BE30), |
| 1048 | + UL64(0xD192E819D6EF5218), UL64(0xD69906245565A910), |
| 1049 | + UL64(0xF40E35855771202A), UL64(0x106AA07032BBD1B8), |
| 1050 | + UL64(0x19A4C116B8D2D0C8), UL64(0x1E376C085141AB53), |
| 1051 | + UL64(0x2748774CDF8EEB99), UL64(0x34B0BCB5E19B48A8), |
| 1052 | + UL64(0x391C0CB3C5C95A63), UL64(0x4ED8AA4AE3418ACB), |
| 1053 | + UL64(0x5B9CCA4F7763E373), UL64(0x682E6FF3D6B2B8A3), |
| 1054 | + UL64(0x748F82EE5DEFB2FC), UL64(0x78A5636F43172F60), |
| 1055 | + UL64(0x84C87814A1F0AB72), UL64(0x8CC702081A6439EC), |
| 1056 | + UL64(0x90BEFFFA23631E28), UL64(0xA4506CEBDE82BDE9), |
| 1057 | + UL64(0xBEF9A3F7B2C67915), UL64(0xC67178F2E372532B), |
| 1058 | + UL64(0xCA273ECEEA26619C), UL64(0xD186B8C721C0C207), |
| 1059 | + UL64(0xEADA7DD6CDE0EB1E), UL64(0xF57D4F7FEE6ED178), |
| 1060 | + UL64(0x06F067AA72176FBA), UL64(0x0A637DC5A2C898A6), |
| 1061 | + UL64(0x113F9804BEF90DAE), UL64(0x1B710B35131C471B), |
| 1062 | + UL64(0x28DB77F523047D84), UL64(0x32CAAB7B40C72493), |
| 1063 | + UL64(0x3C9EBE0A15C9BEBC), UL64(0x431D67C49C100D4C), |
| 1064 | + UL64(0x4CC5D4BECB3E42B6), UL64(0x597F299CFC657E2A), |
| 1065 | + UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817) |
| 1066 | +}; |
| 1067 | + |
| 1068 | +/* |
| 1069 | + * SHA-512 context setup |
| 1070 | + */ |
| 1071 | +void sha4_starts(sha4_context * ctx, int is384) |
| 1072 | +{ |
| 1073 | + ctx->total[0] = 0; |
| 1074 | + ctx->total[1] = 0; |
| 1075 | + |
| 1076 | + if (is384 == 0) { |
| 1077 | + /* SHA-512 */ |
| 1078 | + ctx->state[0] = UL64(0x6A09E667F3BCC908); |
| 1079 | + ctx->state[1] = UL64(0xBB67AE8584CAA73B); |
| 1080 | + ctx->state[2] = UL64(0x3C6EF372FE94F82B); |
| 1081 | + ctx->state[3] = UL64(0xA54FF53A5F1D36F1); |
| 1082 | + ctx->state[4] = UL64(0x510E527FADE682D1); |
| 1083 | + ctx->state[5] = UL64(0x9B05688C2B3E6C1F); |
| 1084 | + ctx->state[6] = UL64(0x1F83D9ABFB41BD6B); |
| 1085 | + ctx->state[7] = UL64(0x5BE0CD19137E2179); |
| 1086 | + } else { |
| 1087 | + /* SHA-384 */ |
| 1088 | + ctx->state[0] = UL64(0xCBBB9D5DC1059ED8); |
| 1089 | + ctx->state[1] = UL64(0x629A292A367CD507); |
| 1090 | + ctx->state[2] = UL64(0x9159015A3070DD17); |
| 1091 | + ctx->state[3] = UL64(0x152FECD8F70E5939); |
| 1092 | + ctx->state[4] = UL64(0x67332667FFC00B31); |
| 1093 | + ctx->state[5] = UL64(0x8EB44A8768581511); |
| 1094 | + ctx->state[6] = UL64(0xDB0C2E0D64F98FA7); |
| 1095 | + ctx->state[7] = UL64(0x47B5481DBEFA4FA4); |
| 1096 | + } |
| 1097 | + |
| 1098 | + ctx->is384 = is384; |
| 1099 | +} |
| 1100 | + |
| 1101 | +static void sha4_process(sha4_context * ctx, unsigned char data[128]) |
| 1102 | +{ |
| 1103 | + int i; |
| 1104 | + unsigned int64 temp1, temp2, W[80]; |
| 1105 | + unsigned int64 A, B, C, D, E, F, G, H; |
| 1106 | + |
| 1107 | +#define SHR(x,n) (x >> n) |
| 1108 | +#define ROTR(x,n) (SHR(x,n) | (x << (64 - n))) |
| 1109 | + |
| 1110 | +#define S0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7)) |
| 1111 | +#define S1(x) (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6)) |
| 1112 | + |
| 1113 | +#define S2(x) (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39)) |
| 1114 | +#define S3(x) (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41)) |
| 1115 | + |
| 1116 | +#define F0(x,y,z) ((x & y) | (z & (x | y))) |
| 1117 | +#define F1(x,y,z) (z ^ (x & (y ^ z))) |
| 1118 | + |
| 1119 | +#define P(a,b,c,d,e,f,g,h,x,K) \ |
| 1120 | + { \ |
| 1121 | + temp1 = h + S3(e) + F1(e,f,g) + K + x; \ |
| 1122 | + temp2 = S2(a) + F0(a,b,c); \ |
| 1123 | + d += temp1; h = temp1 + temp2; \ |
| 1124 | + } |
| 1125 | + |
| 1126 | + for (i = 0; i < 16; i++) { |
| 1127 | + GET_UINT64_BE(W[i], data, i << 3); |
| 1128 | + } |
| 1129 | + |
| 1130 | + for (; i < 80; i++) { |
| 1131 | + W[i] = S1(W[i - 2]) + W[i - 7] + S0(W[i - 15]) + W[i - 16]; |
| 1132 | + } |
| 1133 | + |
| 1134 | + A = ctx->state[0]; |
| 1135 | + B = ctx->state[1]; |
| 1136 | + C = ctx->state[2]; |
| 1137 | + D = ctx->state[3]; |
| 1138 | + E = ctx->state[4]; |
| 1139 | + F = ctx->state[5]; |
| 1140 | + G = ctx->state[6]; |
| 1141 | + H = ctx->state[7]; |
| 1142 | + i = 0; |
| 1143 | + |
| 1144 | + do { |
| 1145 | + P(A, B, C, D, E, F, G, H, W[i], K[i]); |
| 1146 | + i++; |
| 1147 | + P(H, A, B, C, D, E, F, G, W[i], K[i]); |
| 1148 | + i++; |
| 1149 | + P(G, H, A, B, C, D, E, F, W[i], K[i]); |
| 1150 | + i++; |
| 1151 | + P(F, G, H, A, B, C, D, E, W[i], K[i]); |
| 1152 | + i++; |
| 1153 | + P(E, F, G, H, A, B, C, D, W[i], K[i]); |
| 1154 | + i++; |
| 1155 | + P(D, E, F, G, H, A, B, C, W[i], K[i]); |
| 1156 | + i++; |
| 1157 | + P(C, D, E, F, G, H, A, B, W[i], K[i]); |
| 1158 | + i++; |
| 1159 | + P(B, C, D, E, F, G, H, A, W[i], K[i]); |
| 1160 | + i++; |
| 1161 | + } while (i < 80); |
| 1162 | + |
| 1163 | + ctx->state[0] += A; |
| 1164 | + ctx->state[1] += B; |
| 1165 | + ctx->state[2] += C; |
| 1166 | + ctx->state[3] += D; |
| 1167 | + ctx->state[4] += E; |
| 1168 | + ctx->state[5] += F; |
| 1169 | + ctx->state[6] += G; |
| 1170 | + ctx->state[7] += H; |
| 1171 | +} |
| 1172 | + |
| 1173 | +/* |
| 1174 | + * SHA-512 process buffer |
| 1175 | + */ |
| 1176 | +void sha4_update(sha4_context * ctx, unsigned char *input, int ilen) |
| 1177 | +{ |
| 1178 | + int fill; |
| 1179 | + unsigned int64 left; |
| 1180 | + |
| 1181 | + if (ilen <= 0) |
| 1182 | + return; |
| 1183 | + |
| 1184 | + left = ctx->total[0] & 0x7F; |
| 1185 | + fill = (int)(128 - left); |
| 1186 | + |
| 1187 | + ctx->total[0] += ilen; |
| 1188 | + |
| 1189 | + if (ctx->total[0] < (unsigned int64)ilen) |
| 1190 | + ctx->total[1]++; |
| 1191 | + |
| 1192 | + if (left && ilen >= fill) { |
| 1193 | + memcpy((void *)(ctx->buffer + left), (void *)input, fill); |
| 1194 | + sha4_process(ctx, ctx->buffer); |
| 1195 | + input += fill; |
| 1196 | + ilen -= fill; |
| 1197 | + left = 0; |
| 1198 | + } |
| 1199 | + |
| 1200 | + while (ilen >= 128) { |
| 1201 | + sha4_process(ctx, input); |
| 1202 | + input += 128; |
| 1203 | + ilen -= 128; |
| 1204 | + } |
| 1205 | + |
| 1206 | + if (ilen > 0) { |
| 1207 | + memcpy((void *)(ctx->buffer + left), (void *)input, ilen); |
| 1208 | + } |
| 1209 | +} |
| 1210 | + |
| 1211 | +static const unsigned char sha4_padding[128] = { |
| 1212 | + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1213 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1214 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1215 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1216 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1217 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1218 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1219 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 1220 | +}; |
| 1221 | + |
| 1222 | +/* |
| 1223 | + * SHA-512 final digest |
| 1224 | + */ |
| 1225 | +void sha4_finish(sha4_context * ctx, unsigned char output[64]) |
| 1226 | +{ |
| 1227 | + int last, padn; |
| 1228 | + unsigned int64 high, low; |
| 1229 | + unsigned char msglen[16]; |
| 1230 | + |
| 1231 | + high = (ctx->total[0] >> 61) |
| 1232 | + | (ctx->total[1] << 3); |
| 1233 | + low = (ctx->total[0] << 3); |
| 1234 | + |
| 1235 | + PUT_UINT64_BE(high, msglen, 0); |
| 1236 | + PUT_UINT64_BE(low, msglen, 8); |
| 1237 | + |
| 1238 | + last = (int)(ctx->total[0] & 0x7F); |
| 1239 | + padn = (last < 112) ? (112 - last) : (240 - last); |
| 1240 | + |
| 1241 | + sha4_update(ctx, (unsigned char *)sha4_padding, padn); |
| 1242 | + sha4_update(ctx, msglen, 16); |
| 1243 | + |
| 1244 | + PUT_UINT64_BE(ctx->state[0], output, 0); |
| 1245 | + PUT_UINT64_BE(ctx->state[1], output, 8); |
| 1246 | + PUT_UINT64_BE(ctx->state[2], output, 16); |
| 1247 | + PUT_UINT64_BE(ctx->state[3], output, 24); |
| 1248 | + PUT_UINT64_BE(ctx->state[4], output, 32); |
| 1249 | + PUT_UINT64_BE(ctx->state[5], output, 40); |
| 1250 | + |
| 1251 | + if (ctx->is384 == 0) { |
| 1252 | + PUT_UINT64_BE(ctx->state[6], output, 48); |
| 1253 | + PUT_UINT64_BE(ctx->state[7], output, 56); |
| 1254 | + } |
| 1255 | +} |
| 1256 | + |
| 1257 | +/* |
| 1258 | + * output = SHA-512( input buffer ) |
| 1259 | + */ |
| 1260 | +void sha4(unsigned char *input, int ilen, unsigned char output[64], int is384) |
| 1261 | +{ |
| 1262 | + sha4_context ctx; |
| 1263 | + |
| 1264 | + sha4_starts(&ctx, is384); |
| 1265 | + sha4_update(&ctx, input, ilen); |
| 1266 | + sha4_finish(&ctx, output); |
| 1267 | + |
| 1268 | + memset(&ctx, 0, sizeof(sha4_context)); |
| 1269 | +} |
| 1270 | + |
| 1271 | +/* |
| 1272 | + * Output = SHA-4( input buffer ). Trigger the watchdog every 'chunk_sz' |
| 1273 | + * bytes of input processed. |
| 1274 | + */ |
| 1275 | +static void sha4_csum_wd(const unsigned char *input, unsigned int ilen, |
| 1276 | + unsigned char *output, unsigned int chunk_sz, int is384) |
| 1277 | +{ |
| 1278 | + sha4_context ctx; |
| 1279 | +#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
| 1280 | + const unsigned char *end; |
| 1281 | + unsigned char *curr; |
| 1282 | + int chunk; |
| 1283 | +#endif |
| 1284 | + |
| 1285 | + sha4_starts(&ctx, is384); |
| 1286 | + |
| 1287 | +#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
| 1288 | + curr = (unsigned char *)input; |
| 1289 | + end = input + ilen; |
| 1290 | + while (curr < end) { |
| 1291 | + chunk = end - curr; |
| 1292 | + if (chunk > chunk_sz) |
| 1293 | + chunk = chunk_sz; |
| 1294 | + sha4_update(&ctx, curr, chunk); |
| 1295 | + curr += chunk; |
| 1296 | + WATCHDOG_RESET(); |
| 1297 | + } |
| 1298 | +#else |
| 1299 | + sha4_update(&ctx, input, ilen); |
| 1300 | +#endif |
| 1301 | + |
| 1302 | + sha4_finish(&ctx, output); |
| 1303 | +} |
| 1304 | + |
| 1305 | +void sha384_csum_wd(const unsigned char *input, unsigned int ilen, |
| 1306 | + unsigned char *output, unsigned int chunk_sz) |
| 1307 | +{ |
| 1308 | + sha4_csum_wd(input, ilen, output, chunk_sz, 1); |
| 1309 | +} |
| 1310 | + |
| 1311 | +void sha512_csum_wd(const unsigned char *input, unsigned int ilen, |
| 1312 | + unsigned char *output, unsigned int chunk_sz) |
| 1313 | +{ |
| 1314 | + sha4_csum_wd(input, ilen, output, chunk_sz, 0); |
| 1315 | +} |
| 1316 | + |
| 1317 | +/* |
| 1318 | + * SHA-512 HMAC context setup |
| 1319 | + */ |
| 1320 | +void sha4_hmac_starts(sha4_context * ctx, unsigned char *key, int keylen, |
| 1321 | + int is384) |
| 1322 | +{ |
| 1323 | + int i; |
| 1324 | + unsigned char sum[64]; |
| 1325 | + |
| 1326 | + if (keylen > 128) { |
| 1327 | + sha4(key, keylen, sum, is384); |
| 1328 | + keylen = (is384) ? 48 : 64; |
| 1329 | + key = sum; |
| 1330 | + } |
| 1331 | + |
| 1332 | + memset(ctx->ipad, 0x36, 128); |
| 1333 | + memset(ctx->opad, 0x5C, 128); |
| 1334 | + |
| 1335 | + for (i = 0; i < keylen; i++) { |
| 1336 | + ctx->ipad[i] = (unsigned char)(ctx->ipad[i] ^ key[i]); |
| 1337 | + ctx->opad[i] = (unsigned char)(ctx->opad[i] ^ key[i]); |
| 1338 | + } |
| 1339 | + |
| 1340 | + sha4_starts(ctx, is384); |
| 1341 | + sha4_update(ctx, ctx->ipad, 128); |
| 1342 | + |
| 1343 | + memset(sum, 0, sizeof(sum)); |
| 1344 | +} |
| 1345 | + |
| 1346 | +/* |
| 1347 | + * SHA-512 HMAC process buffer |
| 1348 | + */ |
| 1349 | +void sha4_hmac_update(sha4_context * ctx, unsigned char *input, int ilen) |
| 1350 | +{ |
| 1351 | + sha4_update(ctx, input, ilen); |
| 1352 | +} |
| 1353 | + |
| 1354 | +/* |
| 1355 | + * SHA-512 HMAC final digest |
| 1356 | + */ |
| 1357 | +void sha4_hmac_finish(sha4_context * ctx, unsigned char output[64]) |
| 1358 | +{ |
| 1359 | + int is384, hlen; |
| 1360 | + unsigned char tmpbuf[64]; |
| 1361 | + |
| 1362 | + is384 = ctx->is384; |
| 1363 | + hlen = (is384 == 0) ? 64 : 48; |
| 1364 | + |
| 1365 | + sha4_finish(ctx, tmpbuf); |
| 1366 | + sha4_starts(ctx, is384); |
| 1367 | + sha4_update(ctx, ctx->opad, 128); |
| 1368 | + sha4_update(ctx, tmpbuf, hlen); |
| 1369 | + sha4_finish(ctx, output); |
| 1370 | + |
| 1371 | + memset(tmpbuf, 0, sizeof(tmpbuf)); |
| 1372 | +} |
| 1373 | + |
| 1374 | +/* |
| 1375 | + * output = HMAC-SHA-512( hmac key, input buffer ) |
| 1376 | + */ |
| 1377 | +void sha4_hmac(unsigned char *key, int keylen, |
| 1378 | + unsigned char *input, int ilen, |
| 1379 | + unsigned char output[64], int is384) |
| 1380 | +{ |
| 1381 | + sha4_context ctx; |
| 1382 | + |
| 1383 | + sha4_hmac_starts(&ctx, key, keylen, is384); |
| 1384 | + sha4_hmac_update(&ctx, input, ilen); |
| 1385 | + sha4_hmac_finish(&ctx, output); |
| 1386 | + |
| 1387 | + memset(&ctx, 0, sizeof(sha4_context)); |
| 1388 | +} |
| 1389 | diff --git a/tools/Makefile b/tools/Makefile |
| 1390 | index 9edb504..59f4911 100644 |
| 1391 | --- a/tools/Makefile |
| 1392 | +++ b/tools/Makefile |
| 1393 | @@ -95,6 +95,7 @@ dumpimage-mkimage-objs := aisimage.o \ |
| 1394 | socfpgaimage.o \ |
| 1395 | lib/sha1.o \ |
| 1396 | lib/sha256.o \ |
| 1397 | + lib/sha4.o \ |
| 1398 | common/hash.o \ |
| 1399 | ublimage.o \ |
| 1400 | zynqimage.o \ |
| 1401 | -- |
| 1402 | 1.9.1 |
| 1403 | |