| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/sha.h> |
| 5 |
|
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include <openssl/mem.h> |
| 9 |
|
| 10 |
#include "internal.h" |
| 11 |
#include "../../internal.h" |
| 12 |
|
| 13 |
|
| 14 |
// The 32-bit hash algorithms share a common byte-order neutral collector and |
| 15 |
// padding function implementations that operate on unaligned data, |
| 16 |
// ../digest/md32_common.h. SHA-512 is the only 64-bit hash algorithm, as of |
| 17 |
// this writing, so there is no need for a common collector/padding |
| 18 |
// implementation yet. |
| 19 |
|
| 20 |
static int sha512_final_impl(uint8_t *out, size_t md_len, SHA512_CTX *sha); |
| 21 |
|
| 22 |
int SHA384_Init(SHA512_CTX *sha) { |
| 23 |
sha->h[0] = UINT64_C(0xcbbb9d5dc1059ed8); |
| 24 |
sha->h[1] = UINT64_C(0x629a292a367cd507); |
| 25 |
sha->h[2] = UINT64_C(0x9159015a3070dd17); |
| 26 |
sha->h[3] = UINT64_C(0x152fecd8f70e5939); |
| 27 |
sha->h[4] = UINT64_C(0x67332667ffc00b31); |
| 28 |
sha->h[5] = UINT64_C(0x8eb44a8768581511); |
| 29 |
sha->h[6] = UINT64_C(0xdb0c2e0d64f98fa7); |
| 30 |
sha->h[7] = UINT64_C(0x47b5481dbefa4fa4); |
| 31 |
|
| 32 |
sha->Nl = 0; |
| 33 |
sha->Nh = 0; |
| 34 |
sha->num = 0; |
| 35 |
sha->md_len = SHA384_DIGEST_LENGTH; |
| 36 |
return 1; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
int SHA512_Init(SHA512_CTX *sha) { |
| 41 |
sha->h[0] = UINT64_C(0x6a09e667f3bcc908); |
| 42 |
sha->h[1] = UINT64_C(0xbb67ae8584caa73b); |
| 43 |
sha->h[2] = UINT64_C(0x3c6ef372fe94f82b); |
| 44 |
sha->h[3] = UINT64_C(0xa54ff53a5f1d36f1); |
| 45 |
sha->h[4] = UINT64_C(0x510e527fade682d1); |
| 46 |
sha->h[5] = UINT64_C(0x9b05688c2b3e6c1f); |
| 47 |
sha->h[6] = UINT64_C(0x1f83d9abfb41bd6b); |
| 48 |
sha->h[7] = UINT64_C(0x5be0cd19137e2179); |
| 49 |
|
| 50 |
sha->Nl = 0; |
| 51 |
sha->Nh = 0; |
| 52 |
sha->num = 0; |
| 53 |
sha->md_len = SHA512_DIGEST_LENGTH; |
| 54 |
return 1; |
| 55 |
} |
| 56 |
|
| 57 |
int SHA512_224_Init(SHA512_CTX *sha) { |
| 58 |
sha->h[0] = UINT64_C(0x8c3d37c819544da2); |
| 59 |
sha->h[1] = UINT64_C(0x73e1996689dcd4d6); |
| 60 |
sha->h[2] = UINT64_C(0x1dfab7ae32ff9c82); |
| 61 |
sha->h[3] = UINT64_C(0x679dd514582f9fcf); |
| 62 |
sha->h[4] = UINT64_C(0x0f6d2b697bd44da8); |
| 63 |
sha->h[5] = UINT64_C(0x77e36f7304c48942); |
| 64 |
sha->h[6] = UINT64_C(0x3f9d85a86a1d36c8); |
| 65 |
sha->h[7] = UINT64_C(0x1112e6ad91d692a1); |
| 66 |
|
| 67 |
sha->Nl = 0; |
| 68 |
sha->Nh = 0; |
| 69 |
sha->num = 0; |
| 70 |
sha->md_len = SHA512_224_DIGEST_LENGTH; |
| 71 |
return 1; |
| 72 |
} |
| 73 |
|
| 74 |
int SHA512_256_Init(SHA512_CTX *sha) { |
| 75 |
sha->h[0] = UINT64_C(0x22312194fc2bf72c); |
| 76 |
sha->h[1] = UINT64_C(0x9f555fa3c84c64c2); |
| 77 |
sha->h[2] = UINT64_C(0x2393b86b6f53b151); |
| 78 |
sha->h[3] = UINT64_C(0x963877195940eabd); |
| 79 |
sha->h[4] = UINT64_C(0x96283ee2a88effe3); |
| 80 |
sha->h[5] = UINT64_C(0xbe5e1e2553863992); |
| 81 |
sha->h[6] = UINT64_C(0x2b0199fc2c85b8aa); |
| 82 |
sha->h[7] = UINT64_C(0x0eb72ddc81c52ca2); |
| 83 |
|
| 84 |
sha->Nl = 0; |
| 85 |
sha->Nh = 0; |
| 86 |
sha->num = 0; |
| 87 |
sha->md_len = SHA512_256_DIGEST_LENGTH; |
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
OPENSSL_STATIC_ASSERT(SHA512_CHAINING_LENGTH==SHA384_CHAINING_LENGTH, |
| 92 |
sha512_and_sha384_have_same_chaining_length) |
| 93 |
OPENSSL_STATIC_ASSERT(SHA512_CHAINING_LENGTH==SHA512_224_CHAINING_LENGTH, |
| 94 |
sha512_and_sha512_224_have_same_chaining_length) |
| 95 |
OPENSSL_STATIC_ASSERT(SHA512_CHAINING_LENGTH==SHA512_256_CHAINING_LENGTH, |
| 96 |
sha512_and_sha512_256_have_same_chaining_length) |
| 97 |
|
| 98 |
// sha512_init_from_state_impl is the implementation of |
| 99 |
// SHA512_Init_from_state and SHA224_Init_from_state |
| 100 |
// Note that the state h is always SHA512_CHAINING_LENGTH-byte long |
| 101 |
static int sha512_init_from_state_impl(SHA512_CTX *sha, int md_len, |
| 102 |
const uint8_t h[SHA512_CHAINING_LENGTH], |
| 103 |
uint64_t n) { |
| 104 |
if(n % ((uint64_t) SHA512_CBLOCK * 8) != 0) { |
| 105 |
// n is not a multiple of the block size in bits, so it fails |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
OPENSSL_memset(sha, 0, sizeof(SHA512_CTX)); |
| 110 |
sha->md_len = md_len; |
| 111 |
|
| 112 |
const size_t out_words = SHA512_CHAINING_LENGTH / 8; |
| 113 |
for (size_t i = 0; i < out_words; i++) { |
| 114 |
sha->h[i] = CRYPTO_load_u64_be(h); |
| 115 |
h += 8; |
| 116 |
} |
| 117 |
|
| 118 |
sha->Nh = 0; |
| 119 |
sha->Nl = n; |
| 120 |
|
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
int SHA384_Init_from_state(SHA512_CTX *sha, |
| 125 |
const uint8_t h[SHA384_CHAINING_LENGTH], |
| 126 |
uint64_t n) { |
| 127 |
return sha512_init_from_state_impl(sha, SHA384_DIGEST_LENGTH, h, n); |
| 128 |
} |
| 129 |
|
| 130 |
int SHA512_Init_from_state(SHA512_CTX *sha, |
| 131 |
const uint8_t h[SHA512_CHAINING_LENGTH], |
| 132 |
uint64_t n) { |
| 133 |
return sha512_init_from_state_impl(sha, SHA512_DIGEST_LENGTH, h, n); |
| 134 |
} |
| 135 |
|
| 136 |
int SHA512_224_Init_from_state(SHA512_CTX *sha, |
| 137 |
const uint8_t h[SHA512_224_CHAINING_LENGTH], |
| 138 |
uint64_t n) { |
| 139 |
return sha512_init_from_state_impl(sha, SHA512_224_DIGEST_LENGTH, h, n); |
| 140 |
} |
| 141 |
|
| 142 |
int SHA512_256_Init_from_state(SHA512_CTX *sha, |
| 143 |
const uint8_t h[SHA512_256_CHAINING_LENGTH], |
| 144 |
uint64_t n) { |
| 145 |
return sha512_init_from_state_impl(sha, SHA512_256_DIGEST_LENGTH, h, n); |
| 146 |
} |
| 147 |
|
| 148 |
uint8_t *SHA384(const uint8_t *data, size_t len, |
| 149 |
uint8_t out[SHA384_DIGEST_LENGTH]) { |
| 150 |
// We have to verify that all the SHA services actually succeed before |
| 151 |
// updating the indicator state, so we lock the state here. |
| 152 |
FIPS_service_indicator_lock_state(); |
| 153 |
SHA512_CTX ctx; |
| 154 |
const int ok = SHA384_Init(&ctx) && |
| 155 |
SHA384_Update(&ctx, data, len) && |
| 156 |
SHA384_Final(out, &ctx); |
| 157 |
FIPS_service_indicator_unlock_state(); |
| 158 |
if(ok) { |
| 159 |
FIPS_service_indicator_update_state(); |
| 160 |
} |
| 161 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 162 |
return out; |
| 163 |
} |
| 164 |
|
| 165 |
uint8_t *SHA512(const uint8_t *data, size_t len, |
| 166 |
uint8_t out[SHA512_DIGEST_LENGTH]) { |
| 167 |
// We have to verify that all the SHA services actually succeed before |
| 168 |
// updating the indicator state, so we lock the state here. |
| 169 |
FIPS_service_indicator_lock_state(); |
| 170 |
SHA512_CTX ctx; |
| 171 |
const int ok = SHA512_Init(&ctx) && |
| 172 |
SHA512_Update(&ctx, data, len) && |
| 173 |
SHA512_Final(out, &ctx); |
| 174 |
FIPS_service_indicator_unlock_state(); |
| 175 |
if(ok) { |
| 176 |
FIPS_service_indicator_update_state(); |
| 177 |
} |
| 178 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 179 |
return out; |
| 180 |
} |
| 181 |
|
| 182 |
uint8_t *SHA512_224(const uint8_t *data, size_t len, |
| 183 |
uint8_t out[SHA512_224_DIGEST_LENGTH]) { |
| 184 |
// We have to verify that all the SHA services actually succeed before |
| 185 |
// updating the indicator state, so we lock the state here. |
| 186 |
FIPS_service_indicator_lock_state(); |
| 187 |
SHA512_CTX ctx; |
| 188 |
const int ok = SHA512_224_Init(&ctx) && |
| 189 |
SHA512_224_Update(&ctx, data, len) && |
| 190 |
SHA512_224_Final(out, &ctx); |
| 191 |
FIPS_service_indicator_unlock_state(); |
| 192 |
if(ok) { |
| 193 |
FIPS_service_indicator_update_state(); |
| 194 |
} |
| 195 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 196 |
return out; |
| 197 |
} |
| 198 |
|
| 199 |
uint8_t *SHA512_256(const uint8_t *data, size_t len, |
| 200 |
uint8_t out[SHA512_256_DIGEST_LENGTH]) { |
| 201 |
// We have to verify that all the SHA services actually succeed before |
| 202 |
// updating the indicator state, so we lock the state here. |
| 203 |
FIPS_service_indicator_lock_state(); |
| 204 |
SHA512_CTX ctx; |
| 205 |
const int ok = SHA512_256_Init(&ctx) && |
| 206 |
SHA512_256_Update(&ctx, data, len) && |
| 207 |
SHA512_256_Final(out, &ctx); |
| 208 |
FIPS_service_indicator_unlock_state(); |
| 209 |
if(ok) { |
| 210 |
FIPS_service_indicator_update_state(); |
| 211 |
} |
| 212 |
OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 213 |
return out; |
| 214 |
} |
| 215 |
|
| 216 |
#if !defined(SHA512_ASM) |
| 217 |
static void sha512_block_data_order(uint64_t state[8], const uint8_t *in, |
| 218 |
size_t num_blocks); |
| 219 |
#endif |
| 220 |
|
| 221 |
|
| 222 |
int SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH], SHA512_CTX *sha) { |
| 223 |
// This function must be paired with |SHA384_Init|, which sets |sha->md_len| |
| 224 |
// to |SHA384_DIGEST_LENGTH|. |
| 225 |
assert(sha->md_len == SHA384_DIGEST_LENGTH); |
| 226 |
return sha512_final_impl(out, SHA384_DIGEST_LENGTH, sha); |
| 227 |
} |
| 228 |
|
| 229 |
int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len) { |
| 230 |
return SHA512_Update(sha, data, len); |
| 231 |
} |
| 232 |
|
| 233 |
int SHA512_224_Update(SHA512_CTX *sha, const void *data, size_t len) { |
| 234 |
return SHA512_Update(sha, data, len); |
| 235 |
} |
| 236 |
|
| 237 |
int SHA512_224_Final(uint8_t out[SHA512_224_DIGEST_LENGTH], SHA512_CTX *sha) { |
| 238 |
// This function must be paired with |SHA512_224_Init|, which sets |
| 239 |
// |sha->md_len| to |SHA512_224_DIGEST_LENGTH|. |
| 240 |
assert(sha->md_len == SHA512_224_DIGEST_LENGTH); |
| 241 |
return sha512_final_impl(out, SHA512_224_DIGEST_LENGTH, sha); |
| 242 |
} |
| 243 |
|
| 244 |
int SHA512_256_Update(SHA512_CTX *sha, const void *data, size_t len) { |
| 245 |
return SHA512_Update(sha, data, len); |
| 246 |
} |
| 247 |
|
| 248 |
int SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH], SHA512_CTX *sha) { |
| 249 |
// This function must be paired with |SHA512_256_Init|, which sets |
| 250 |
// |sha->md_len| to |SHA512_256_DIGEST_LENGTH|. |
| 251 |
assert(sha->md_len == SHA512_256_DIGEST_LENGTH); |
| 252 |
return sha512_final_impl(out, SHA512_256_DIGEST_LENGTH, sha); |
| 253 |
} |
| 254 |
|
| 255 |
void SHA512_Transform(SHA512_CTX *c, const uint8_t block[SHA512_CBLOCK]) { |
| 256 |
sha512_block_data_order(c->h, block, 1); |
| 257 |
} |
| 258 |
|
| 259 |
int SHA512_Update(SHA512_CTX *c, const void *in_data, size_t len) { |
| 260 |
uint64_t l; |
| 261 |
uint8_t *p = c->p; |
| 262 |
const uint8_t *data = in_data; |
| 263 |
|
| 264 |
if (len == 0) { |
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
l = (c->Nl + (((uint64_t)len) << 3)) & UINT64_C(0xffffffffffffffff); |
| 269 |
if (l < c->Nl) { |
| 270 |
c->Nh++; |
| 271 |
} |
| 272 |
if (sizeof(len) >= 8) { |
| 273 |
c->Nh += (((uint64_t)len) >> 61); |
| 274 |
} |
| 275 |
c->Nl = l; |
| 276 |
|
| 277 |
if (c->num != 0) { |
| 278 |
size_t n = sizeof(c->p) - c->num; |
| 279 |
|
| 280 |
if (len < n) { |
| 281 |
OPENSSL_memcpy(p + c->num, data, len); |
| 282 |
c->num += (unsigned int)len; |
| 283 |
return 1; |
| 284 |
} else { |
| 285 |
OPENSSL_memcpy(p + c->num, data, n), c->num = 0; |
| 286 |
len -= n; |
| 287 |
data += n; |
| 288 |
sha512_block_data_order(c->h, p, 1); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
if (len >= sizeof(c->p)) { |
| 293 |
sha512_block_data_order(c->h, data, len / sizeof(c->p)); |
| 294 |
data += len; |
| 295 |
len %= sizeof(c->p); |
| 296 |
data -= len; |
| 297 |
} |
| 298 |
|
| 299 |
if (len != 0) { |
| 300 |
OPENSSL_memcpy(p, data, len); |
| 301 |
c->num = (int)len; |
| 302 |
} |
| 303 |
|
| 304 |
return 1; |
| 305 |
} |
| 306 |
|
| 307 |
int SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH], SHA512_CTX *sha) { |
| 308 |
// Ideally we would assert |sha->md_len| is |SHA512_DIGEST_LENGTH| to match |
| 309 |
// the size hint, but calling code often pairs |SHA384_Init| with |
| 310 |
// |SHA512_Final| and expects |sha->md_len| to carry the size over. |
| 311 |
// |
| 312 |
// TODO(davidben): Add an assert and fix code to match them up. |
| 313 |
return sha512_final_impl(out, sha->md_len, sha); |
| 314 |
} |
| 315 |
|
| 316 |
static int sha512_final_impl(uint8_t *out, size_t md_len, SHA512_CTX *sha) { |
| 317 |
uint8_t *p = sha->p; |
| 318 |
size_t n = sha->num; |
| 319 |
|
| 320 |
p[n] = 0x80; // There always is a room for one |
| 321 |
n++; |
| 322 |
if (n > (sizeof(sha->p) - 16)) { |
| 323 |
OPENSSL_memset(p + n, 0, sizeof(sha->p) - n); |
| 324 |
n = 0; |
| 325 |
sha512_block_data_order(sha->h, p, 1); |
| 326 |
} |
| 327 |
|
| 328 |
OPENSSL_memset(p + n, 0, sizeof(sha->p) - 16 - n); |
| 329 |
CRYPTO_store_u64_be(p + sizeof(sha->p) - 16, sha->Nh); |
| 330 |
CRYPTO_store_u64_be(p + sizeof(sha->p) - 8, sha->Nl); |
| 331 |
|
| 332 |
sha512_block_data_order(sha->h, p, 1); |
| 333 |
|
| 334 |
if (out == NULL) { |
| 335 |
// TODO(davidben): This NULL check is absent in other low-level hash 'final' |
| 336 |
// functions and is one of the few places one can fail. |
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
const size_t out_words = md_len / 8; |
| 341 |
assert(md_len % 8 == 0 || md_len == SHA512_224_DIGEST_LENGTH); |
| 342 |
for (size_t i = 0; i < out_words; i++) { |
| 343 |
CRYPTO_store_u64_be(out, sha->h[i]); |
| 344 |
out += 8; |
| 345 |
} |
| 346 |
|
| 347 |
// SHA-512 and SHA-512/256 are aligned to 8-byte words, SHA-512/224 is not. |
| 348 |
// If the digest size is not aligned to 8-byte words, we need to process the |
| 349 |
// non-word-aligned "trailer". |
| 350 |
if (md_len == SHA512_224_DIGEST_LENGTH) { |
| 351 |
uint64_t trailer; |
| 352 |
CRYPTO_store_u64_be(&trailer, sha->h[out_words]); |
| 353 |
OPENSSL_memcpy(out, &trailer, SHA512_224_DIGEST_LENGTH % 8); |
| 354 |
} |
| 355 |
|
| 356 |
FIPS_service_indicator_update_state(); |
| 357 |
return 1; |
| 358 |
} |
| 359 |
|
| 360 |
// sha512_get_state_impl is the implementation of |
| 361 |
// SHA512_get_state and SHA224_get_state |
| 362 |
// Note that the state out_h is always SHA512_CHAINING_LENGTH-byte long |
| 363 |
static int sha512_get_state_impl(SHA512_CTX *ctx, |
| 364 |
uint8_t out_h[SHA512_CHAINING_LENGTH], |
| 365 |
uint64_t *out_n) { |
| 366 |
if (ctx->Nl % ((uint64_t)SHA512_CBLOCK * 8) != 0) { |
| 367 |
// ctx->Nl is not a multiple of the block size in bits, so it fails |
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
if (ctx->Nh != 0) { |
| 372 |
// |sha512_get_state_impl| assumes that at most 2^64 bits have been |
| 373 |
// processed by the hash function |
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
const size_t out_words = SHA512_CHAINING_LENGTH / 8; |
| 378 |
for (size_t i = 0; i < out_words; i++) { |
| 379 |
CRYPTO_store_u64_be(out_h, ctx->h[i]); |
| 380 |
out_h += 8; |
| 381 |
} |
| 382 |
|
| 383 |
*out_n = ctx->Nl; // we know that ctx->Nh = 0 |
| 384 |
|
| 385 |
return 1; |
| 386 |
} |
| 387 |
|
| 388 |
int SHA384_get_state(SHA512_CTX *ctx, uint8_t out_h[SHA384_CHAINING_LENGTH], |
| 389 |
uint64_t *out_n) { |
| 390 |
return sha512_get_state_impl(ctx, out_h, out_n); |
| 391 |
} |
| 392 |
|
| 393 |
int SHA512_get_state(SHA512_CTX *ctx, uint8_t out_h[SHA512_CHAINING_LENGTH], |
| 394 |
uint64_t *out_n) { |
| 395 |
return sha512_get_state_impl(ctx, out_h, out_n); |
| 396 |
} |
| 397 |
|
| 398 |
int SHA512_224_get_state(SHA512_CTX *ctx, uint8_t out_h[SHA512_224_CHAINING_LENGTH], |
| 399 |
uint64_t *out_n) { |
| 400 |
return sha512_get_state_impl(ctx, out_h, out_n); |
| 401 |
} |
| 402 |
|
| 403 |
int SHA512_256_get_state(SHA512_CTX *ctx, uint8_t out_h[SHA512_256_CHAINING_LENGTH], |
| 404 |
uint64_t *out_n) { |
| 405 |
return sha512_get_state_impl(ctx, out_h, out_n); |
| 406 |
} |
| 407 |
|
| 408 |
#if !defined(SHA512_ASM) |
| 409 |
|
| 410 |
#if !defined(SHA512_ASM_NOHW) |
| 411 |
static const uint64_t K512[80] = { |
| 412 |
UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), |
| 413 |
UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc), |
| 414 |
UINT64_C(0x3956c25bf348b538), UINT64_C(0x59f111f1b605d019), |
| 415 |
UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118), |
| 416 |
UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe), |
| 417 |
UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2), |
| 418 |
UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1), |
| 419 |
UINT64_C(0x9bdc06a725c71235), UINT64_C(0xc19bf174cf692694), |
| 420 |
UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3), |
| 421 |
UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65), |
| 422 |
UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483), |
| 423 |
UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5), |
| 424 |
UINT64_C(0x983e5152ee66dfab), UINT64_C(0xa831c66d2db43210), |
| 425 |
UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4), |
| 426 |
UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725), |
| 427 |
UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70), |
| 428 |
UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926), |
| 429 |
UINT64_C(0x4d2c6dfc5ac42aed), UINT64_C(0x53380d139d95b3df), |
| 430 |
UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8), |
| 431 |
UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b), |
| 432 |
UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001), |
| 433 |
UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30), |
| 434 |
UINT64_C(0xd192e819d6ef5218), UINT64_C(0xd69906245565a910), |
| 435 |
UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8), |
| 436 |
UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53), |
| 437 |
UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8), |
| 438 |
UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb), |
| 439 |
UINT64_C(0x5b9cca4f7763e373), UINT64_C(0x682e6ff3d6b2b8a3), |
| 440 |
UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60), |
| 441 |
UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec), |
| 442 |
UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9), |
| 443 |
UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b), |
| 444 |
UINT64_C(0xca273eceea26619c), UINT64_C(0xd186b8c721c0c207), |
| 445 |
UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178), |
| 446 |
UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6), |
| 447 |
UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b), |
| 448 |
UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493), |
| 449 |
UINT64_C(0x3c9ebe0a15c9bebc), UINT64_C(0x431d67c49c100d4c), |
| 450 |
UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a), |
| 451 |
UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817), |
| 452 |
}; |
| 453 |
|
| 454 |
#define Sigma0(x) \ |
| 455 |
(CRYPTO_rotr_u64((x), 28) ^ CRYPTO_rotr_u64((x), 34) ^ \ |
| 456 |
CRYPTO_rotr_u64((x), 39)) |
| 457 |
#define Sigma1(x) \ |
| 458 |
(CRYPTO_rotr_u64((x), 14) ^ CRYPTO_rotr_u64((x), 18) ^ \ |
| 459 |
CRYPTO_rotr_u64((x), 41)) |
| 460 |
#define sigma0(x) \ |
| 461 |
(CRYPTO_rotr_u64((x), 1) ^ CRYPTO_rotr_u64((x), 8) ^ ((x) >> 7)) |
| 462 |
#define sigma1(x) \ |
| 463 |
(CRYPTO_rotr_u64((x), 19) ^ CRYPTO_rotr_u64((x), 61) ^ ((x) >> 6)) |
| 464 |
|
| 465 |
#define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z))) |
| 466 |
#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
| 467 |
|
| 468 |
|
| 469 |
#if defined(__i386) || defined(__i386__) || defined(_M_IX86) |
| 470 |
// This code should give better results on 32-bit CPU with less than |
| 471 |
// ~24 registers, both size and performance wise... |
| 472 |
static void sha512_block_data_order_nohw(uint64_t state[8], const uint8_t *in, |
| 473 |
size_t num) { |
| 474 |
uint64_t A, E, T; |
| 475 |
uint64_t X[9 + 80], *F; |
| 476 |
int i; |
| 477 |
|
| 478 |
while (num--) { |
| 479 |
F = X + 80; |
| 480 |
A = state[0]; |
| 481 |
F[1] = state[1]; |
| 482 |
F[2] = state[2]; |
| 483 |
F[3] = state[3]; |
| 484 |
E = state[4]; |
| 485 |
F[5] = state[5]; |
| 486 |
F[6] = state[6]; |
| 487 |
F[7] = state[7]; |
| 488 |
|
| 489 |
for (i = 0; i < 16; i++, F--) { |
| 490 |
T = CRYPTO_load_u64_be(in + i * 8); |
| 491 |
F[0] = A; |
| 492 |
F[4] = E; |
| 493 |
F[8] = T; |
| 494 |
T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i]; |
| 495 |
E = F[3] + T; |
| 496 |
A = T + Sigma0(A) + Maj(A, F[1], F[2]); |
| 497 |
} |
| 498 |
|
| 499 |
for (; i < 80; i++, F--) { |
| 500 |
T = sigma0(F[8 + 16 - 1]); |
| 501 |
T += sigma1(F[8 + 16 - 14]); |
| 502 |
T += F[8 + 16] + F[8 + 16 - 9]; |
| 503 |
|
| 504 |
F[0] = A; |
| 505 |
F[4] = E; |
| 506 |
F[8] = T; |
| 507 |
T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i]; |
| 508 |
E = F[3] + T; |
| 509 |
A = T + Sigma0(A) + Maj(A, F[1], F[2]); |
| 510 |
} |
| 511 |
|
| 512 |
state[0] += A; |
| 513 |
state[1] += F[1]; |
| 514 |
state[2] += F[2]; |
| 515 |
state[3] += F[3]; |
| 516 |
state[4] += E; |
| 517 |
state[5] += F[5]; |
| 518 |
state[6] += F[6]; |
| 519 |
state[7] += F[7]; |
| 520 |
|
| 521 |
in += 16 * 8; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
#else |
| 526 |
|
| 527 |
#define ROUND_00_15(i, a, b, c, d, e, f, g, h) \ |
| 528 |
do { \ |
| 529 |
T1 += h + Sigma1(e) + Ch(e, f, g) + K512[i]; \ |
| 530 |
h = Sigma0(a) + Maj(a, b, c); \ |
| 531 |
d += T1; \ |
| 532 |
h += T1; \ |
| 533 |
} while (0) |
| 534 |
|
| 535 |
#define ROUND_16_80(i, j, a, b, c, d, e, f, g, h, X) \ |
| 536 |
do { \ |
| 537 |
s0 = X[(j + 1) & 0x0f]; \ |
| 538 |
s0 = sigma0(s0); \ |
| 539 |
s1 = X[(j + 14) & 0x0f]; \ |
| 540 |
s1 = sigma1(s1); \ |
| 541 |
T1 = X[(j) & 0x0f] += s0 + s1 + X[(j + 9) & 0x0f]; \ |
| 542 |
ROUND_00_15(i + j, a, b, c, d, e, f, g, h); \ |
| 543 |
} while (0) |
| 544 |
|
| 545 |
static void sha512_block_data_order_nohw(uint64_t state[8], const uint8_t *in, |
| 546 |
size_t num) { |
| 547 |
uint64_t a, b, c, d, e, f, g, h, s0, s1, T1; |
| 548 |
uint64_t X[16]; |
| 549 |
int i; |
| 550 |
|
| 551 |
while (num--) { |
| 552 |
|
| 553 |
a = state[0]; |
| 554 |
b = state[1]; |
| 555 |
c = state[2]; |
| 556 |
d = state[3]; |
| 557 |
e = state[4]; |
| 558 |
f = state[5]; |
| 559 |
g = state[6]; |
| 560 |
h = state[7]; |
| 561 |
|
| 562 |
T1 = X[0] = CRYPTO_load_u64_be(in); |
| 563 |
ROUND_00_15(0, a, b, c, d, e, f, g, h); |
| 564 |
T1 = X[1] = CRYPTO_load_u64_be(in + 8); |
| 565 |
ROUND_00_15(1, h, a, b, c, d, e, f, g); |
| 566 |
T1 = X[2] = CRYPTO_load_u64_be(in + 2 * 8); |
| 567 |
ROUND_00_15(2, g, h, a, b, c, d, e, f); |
| 568 |
T1 = X[3] = CRYPTO_load_u64_be(in + 3 * 8); |
| 569 |
ROUND_00_15(3, f, g, h, a, b, c, d, e); |
| 570 |
T1 = X[4] = CRYPTO_load_u64_be(in + 4 * 8); |
| 571 |
ROUND_00_15(4, e, f, g, h, a, b, c, d); |
| 572 |
T1 = X[5] = CRYPTO_load_u64_be(in + 5 * 8); |
| 573 |
ROUND_00_15(5, d, e, f, g, h, a, b, c); |
| 574 |
T1 = X[6] = CRYPTO_load_u64_be(in + 6 * 8); |
| 575 |
ROUND_00_15(6, c, d, e, f, g, h, a, b); |
| 576 |
T1 = X[7] = CRYPTO_load_u64_be(in + 7 * 8); |
| 577 |
ROUND_00_15(7, b, c, d, e, f, g, h, a); |
| 578 |
T1 = X[8] = CRYPTO_load_u64_be(in + 8 * 8); |
| 579 |
ROUND_00_15(8, a, b, c, d, e, f, g, h); |
| 580 |
T1 = X[9] = CRYPTO_load_u64_be(in + 9 * 8); |
| 581 |
ROUND_00_15(9, h, a, b, c, d, e, f, g); |
| 582 |
T1 = X[10] = CRYPTO_load_u64_be(in + 10 * 8); |
| 583 |
ROUND_00_15(10, g, h, a, b, c, d, e, f); |
| 584 |
T1 = X[11] = CRYPTO_load_u64_be(in + 11 * 8); |
| 585 |
ROUND_00_15(11, f, g, h, a, b, c, d, e); |
| 586 |
T1 = X[12] = CRYPTO_load_u64_be(in + 12 * 8); |
| 587 |
ROUND_00_15(12, e, f, g, h, a, b, c, d); |
| 588 |
T1 = X[13] = CRYPTO_load_u64_be(in + 13 * 8); |
| 589 |
ROUND_00_15(13, d, e, f, g, h, a, b, c); |
| 590 |
T1 = X[14] = CRYPTO_load_u64_be(in + 14 * 8); |
| 591 |
ROUND_00_15(14, c, d, e, f, g, h, a, b); |
| 592 |
T1 = X[15] = CRYPTO_load_u64_be(in + 15 * 8); |
| 593 |
ROUND_00_15(15, b, c, d, e, f, g, h, a); |
| 594 |
|
| 595 |
for (i = 16; i < 80; i += 16) { |
| 596 |
ROUND_16_80(i, 0, a, b, c, d, e, f, g, h, X); |
| 597 |
ROUND_16_80(i, 1, h, a, b, c, d, e, f, g, X); |
| 598 |
ROUND_16_80(i, 2, g, h, a, b, c, d, e, f, X); |
| 599 |
ROUND_16_80(i, 3, f, g, h, a, b, c, d, e, X); |
| 600 |
ROUND_16_80(i, 4, e, f, g, h, a, b, c, d, X); |
| 601 |
ROUND_16_80(i, 5, d, e, f, g, h, a, b, c, X); |
| 602 |
ROUND_16_80(i, 6, c, d, e, f, g, h, a, b, X); |
| 603 |
ROUND_16_80(i, 7, b, c, d, e, f, g, h, a, X); |
| 604 |
ROUND_16_80(i, 8, a, b, c, d, e, f, g, h, X); |
| 605 |
ROUND_16_80(i, 9, h, a, b, c, d, e, f, g, X); |
| 606 |
ROUND_16_80(i, 10, g, h, a, b, c, d, e, f, X); |
| 607 |
ROUND_16_80(i, 11, f, g, h, a, b, c, d, e, X); |
| 608 |
ROUND_16_80(i, 12, e, f, g, h, a, b, c, d, X); |
| 609 |
ROUND_16_80(i, 13, d, e, f, g, h, a, b, c, X); |
| 610 |
ROUND_16_80(i, 14, c, d, e, f, g, h, a, b, X); |
| 611 |
ROUND_16_80(i, 15, b, c, d, e, f, g, h, a, X); |
| 612 |
} |
| 613 |
|
| 614 |
state[0] += a; |
| 615 |
state[1] += b; |
| 616 |
state[2] += c; |
| 617 |
state[3] += d; |
| 618 |
state[4] += e; |
| 619 |
state[5] += f; |
| 620 |
state[6] += g; |
| 621 |
state[7] += h; |
| 622 |
|
| 623 |
in += 16 * 8; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
#endif |
| 628 |
|
| 629 |
#endif // !SHA512_ASM_NOHW |
| 630 |
|
| 631 |
static void sha512_block_data_order(uint64_t state[8], const uint8_t *data, |
| 632 |
size_t num) { |
| 633 |
#if defined(SHA512_ASM_HW) |
| 634 |
if (sha512_hw_capable()) { |
| 635 |
sha512_block_data_order_hw(state, data, num); |
| 636 |
return; |
| 637 |
} |
| 638 |
#endif |
| 639 |
#if defined(SHA512_ASM_AVX) && !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_AVX) |
| 640 |
if (sha512_avx_capable()) { |
| 641 |
sha512_block_data_order_avx(state, data, num); |
| 642 |
return; |
| 643 |
} |
| 644 |
#endif |
| 645 |
#if defined(SHA512_ASM_NEON) |
| 646 |
if (CRYPTO_is_NEON_capable()) { |
| 647 |
sha512_block_data_order_neon(state, data, num); |
| 648 |
return; |
| 649 |
} |
| 650 |
#endif |
| 651 |
sha512_block_data_order_nohw(state, data, num); |
| 652 |
} |
| 653 |
|
| 654 |
#endif // !SHA512_ASM |
| 655 |
|
| 656 |
#undef Sigma0 |
| 657 |
#undef Sigma1 |
| 658 |
#undef sigma0 |
| 659 |
#undef sigma1 |
| 660 |
#undef Ch |
| 661 |
#undef Maj |
| 662 |
#undef ROUND_00_15 |
| 663 |
#undef ROUND_16_80 |