1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the OpenSSL license (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <openssl/opensslconf.h> 11e1051a39Sopenharmony_ci/*- 12e1051a39Sopenharmony_ci * IMPLEMENTATION NOTES. 13e1051a39Sopenharmony_ci * 14e1051a39Sopenharmony_ci * As you might have noticed 32-bit hash algorithms: 15e1051a39Sopenharmony_ci * 16e1051a39Sopenharmony_ci * - permit SHA_LONG to be wider than 32-bit 17e1051a39Sopenharmony_ci * - optimized versions implement two transform functions: one operating 18e1051a39Sopenharmony_ci * on [aligned] data in host byte order and one - on data in input 19e1051a39Sopenharmony_ci * stream byte order; 20e1051a39Sopenharmony_ci * - share common byte-order neutral collector and padding function 21e1051a39Sopenharmony_ci * implementations, ../md32_common.h; 22e1051a39Sopenharmony_ci * 23e1051a39Sopenharmony_ci * Neither of the above applies to this SHA-512 implementations. Reasons 24e1051a39Sopenharmony_ci * [in reverse order] are: 25e1051a39Sopenharmony_ci * 26e1051a39Sopenharmony_ci * - it's the only 64-bit hash algorithm for the moment of this writing, 27e1051a39Sopenharmony_ci * there is no need for common collector/padding implementation [yet]; 28e1051a39Sopenharmony_ci * - by supporting only one transform function [which operates on 29e1051a39Sopenharmony_ci * *aligned* data in input stream byte order, big-endian in this case] 30e1051a39Sopenharmony_ci * we minimize burden of maintenance in two ways: a) collector/padding 31e1051a39Sopenharmony_ci * function is simpler; b) only one transform function to stare at; 32e1051a39Sopenharmony_ci * - SHA_LONG64 is required to be exactly 64-bit in order to be able to 33e1051a39Sopenharmony_ci * apply a number of optimizations to mitigate potential performance 34e1051a39Sopenharmony_ci * penalties caused by previous design decision; 35e1051a39Sopenharmony_ci * 36e1051a39Sopenharmony_ci * Caveat lector. 37e1051a39Sopenharmony_ci * 38e1051a39Sopenharmony_ci * Implementation relies on the fact that "long long" is 64-bit on 39e1051a39Sopenharmony_ci * both 32- and 64-bit platforms. If some compiler vendor comes up 40e1051a39Sopenharmony_ci * with 128-bit long long, adjustment to sha.h would be required. 41e1051a39Sopenharmony_ci * As this implementation relies on 64-bit integer type, it's totally 42e1051a39Sopenharmony_ci * inappropriate for platforms which don't support it, most notably 43e1051a39Sopenharmony_ci * 16-bit platforms. 44e1051a39Sopenharmony_ci */ 45e1051a39Sopenharmony_ci#include <stdlib.h> 46e1051a39Sopenharmony_ci#include <string.h> 47e1051a39Sopenharmony_ci 48e1051a39Sopenharmony_ci#include <openssl/crypto.h> 49e1051a39Sopenharmony_ci#include <openssl/sha.h> 50e1051a39Sopenharmony_ci#include <openssl/opensslv.h> 51e1051a39Sopenharmony_ci 52e1051a39Sopenharmony_ci#include "internal/cryptlib.h" 53e1051a39Sopenharmony_ci#include "crypto/sha.h" 54e1051a39Sopenharmony_ci 55e1051a39Sopenharmony_ci#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ 56e1051a39Sopenharmony_ci defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64) || \ 57e1051a39Sopenharmony_ci defined(__s390__) || defined(__s390x__) || \ 58e1051a39Sopenharmony_ci defined(__aarch64__) || \ 59e1051a39Sopenharmony_ci defined(SHA512_ASM) 60e1051a39Sopenharmony_ci# define SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA 61e1051a39Sopenharmony_ci#endif 62e1051a39Sopenharmony_ci 63e1051a39Sopenharmony_ciint sha512_224_init(SHA512_CTX *c) 64e1051a39Sopenharmony_ci{ 65e1051a39Sopenharmony_ci c->h[0] = U64(0x8c3d37c819544da2); 66e1051a39Sopenharmony_ci c->h[1] = U64(0x73e1996689dcd4d6); 67e1051a39Sopenharmony_ci c->h[2] = U64(0x1dfab7ae32ff9c82); 68e1051a39Sopenharmony_ci c->h[3] = U64(0x679dd514582f9fcf); 69e1051a39Sopenharmony_ci c->h[4] = U64(0x0f6d2b697bd44da8); 70e1051a39Sopenharmony_ci c->h[5] = U64(0x77e36f7304c48942); 71e1051a39Sopenharmony_ci c->h[6] = U64(0x3f9d85a86a1d36c8); 72e1051a39Sopenharmony_ci c->h[7] = U64(0x1112e6ad91d692a1); 73e1051a39Sopenharmony_ci 74e1051a39Sopenharmony_ci c->Nl = 0; 75e1051a39Sopenharmony_ci c->Nh = 0; 76e1051a39Sopenharmony_ci c->num = 0; 77e1051a39Sopenharmony_ci c->md_len = SHA224_DIGEST_LENGTH; 78e1051a39Sopenharmony_ci return 1; 79e1051a39Sopenharmony_ci} 80e1051a39Sopenharmony_ci 81e1051a39Sopenharmony_ciint sha512_256_init(SHA512_CTX *c) 82e1051a39Sopenharmony_ci{ 83e1051a39Sopenharmony_ci c->h[0] = U64(0x22312194fc2bf72c); 84e1051a39Sopenharmony_ci c->h[1] = U64(0x9f555fa3c84c64c2); 85e1051a39Sopenharmony_ci c->h[2] = U64(0x2393b86b6f53b151); 86e1051a39Sopenharmony_ci c->h[3] = U64(0x963877195940eabd); 87e1051a39Sopenharmony_ci c->h[4] = U64(0x96283ee2a88effe3); 88e1051a39Sopenharmony_ci c->h[5] = U64(0xbe5e1e2553863992); 89e1051a39Sopenharmony_ci c->h[6] = U64(0x2b0199fc2c85b8aa); 90e1051a39Sopenharmony_ci c->h[7] = U64(0x0eb72ddc81c52ca2); 91e1051a39Sopenharmony_ci 92e1051a39Sopenharmony_ci c->Nl = 0; 93e1051a39Sopenharmony_ci c->Nh = 0; 94e1051a39Sopenharmony_ci c->num = 0; 95e1051a39Sopenharmony_ci c->md_len = SHA256_DIGEST_LENGTH; 96e1051a39Sopenharmony_ci return 1; 97e1051a39Sopenharmony_ci} 98e1051a39Sopenharmony_ci 99e1051a39Sopenharmony_ciint SHA384_Init(SHA512_CTX *c) 100e1051a39Sopenharmony_ci{ 101e1051a39Sopenharmony_ci c->h[0] = U64(0xcbbb9d5dc1059ed8); 102e1051a39Sopenharmony_ci c->h[1] = U64(0x629a292a367cd507); 103e1051a39Sopenharmony_ci c->h[2] = U64(0x9159015a3070dd17); 104e1051a39Sopenharmony_ci c->h[3] = U64(0x152fecd8f70e5939); 105e1051a39Sopenharmony_ci c->h[4] = U64(0x67332667ffc00b31); 106e1051a39Sopenharmony_ci c->h[5] = U64(0x8eb44a8768581511); 107e1051a39Sopenharmony_ci c->h[6] = U64(0xdb0c2e0d64f98fa7); 108e1051a39Sopenharmony_ci c->h[7] = U64(0x47b5481dbefa4fa4); 109e1051a39Sopenharmony_ci 110e1051a39Sopenharmony_ci c->Nl = 0; 111e1051a39Sopenharmony_ci c->Nh = 0; 112e1051a39Sopenharmony_ci c->num = 0; 113e1051a39Sopenharmony_ci c->md_len = SHA384_DIGEST_LENGTH; 114e1051a39Sopenharmony_ci return 1; 115e1051a39Sopenharmony_ci} 116e1051a39Sopenharmony_ci 117e1051a39Sopenharmony_ciint SHA512_Init(SHA512_CTX *c) 118e1051a39Sopenharmony_ci{ 119e1051a39Sopenharmony_ci c->h[0] = U64(0x6a09e667f3bcc908); 120e1051a39Sopenharmony_ci c->h[1] = U64(0xbb67ae8584caa73b); 121e1051a39Sopenharmony_ci c->h[2] = U64(0x3c6ef372fe94f82b); 122e1051a39Sopenharmony_ci c->h[3] = U64(0xa54ff53a5f1d36f1); 123e1051a39Sopenharmony_ci c->h[4] = U64(0x510e527fade682d1); 124e1051a39Sopenharmony_ci c->h[5] = U64(0x9b05688c2b3e6c1f); 125e1051a39Sopenharmony_ci c->h[6] = U64(0x1f83d9abfb41bd6b); 126e1051a39Sopenharmony_ci c->h[7] = U64(0x5be0cd19137e2179); 127e1051a39Sopenharmony_ci 128e1051a39Sopenharmony_ci c->Nl = 0; 129e1051a39Sopenharmony_ci c->Nh = 0; 130e1051a39Sopenharmony_ci c->num = 0; 131e1051a39Sopenharmony_ci c->md_len = SHA512_DIGEST_LENGTH; 132e1051a39Sopenharmony_ci return 1; 133e1051a39Sopenharmony_ci} 134e1051a39Sopenharmony_ci 135e1051a39Sopenharmony_ci#ifndef SHA512_ASM 136e1051a39Sopenharmony_cistatic 137e1051a39Sopenharmony_ci#endif 138e1051a39Sopenharmony_civoid sha512_block_data_order(SHA512_CTX *ctx, const void *in, size_t num); 139e1051a39Sopenharmony_ci 140e1051a39Sopenharmony_ciint SHA512_Final(unsigned char *md, SHA512_CTX *c) 141e1051a39Sopenharmony_ci{ 142e1051a39Sopenharmony_ci unsigned char *p = (unsigned char *)c->u.p; 143e1051a39Sopenharmony_ci size_t n = c->num; 144e1051a39Sopenharmony_ci 145e1051a39Sopenharmony_ci p[n] = 0x80; /* There always is a room for one */ 146e1051a39Sopenharmony_ci n++; 147e1051a39Sopenharmony_ci if (n > (sizeof(c->u) - 16)) { 148e1051a39Sopenharmony_ci memset(p + n, 0, sizeof(c->u) - n); 149e1051a39Sopenharmony_ci n = 0; 150e1051a39Sopenharmony_ci sha512_block_data_order(c, p, 1); 151e1051a39Sopenharmony_ci } 152e1051a39Sopenharmony_ci 153e1051a39Sopenharmony_ci memset(p + n, 0, sizeof(c->u) - 16 - n); 154e1051a39Sopenharmony_ci#ifdef B_ENDIAN 155e1051a39Sopenharmony_ci c->u.d[SHA_LBLOCK - 2] = c->Nh; 156e1051a39Sopenharmony_ci c->u.d[SHA_LBLOCK - 1] = c->Nl; 157e1051a39Sopenharmony_ci#else 158e1051a39Sopenharmony_ci p[sizeof(c->u) - 1] = (unsigned char)(c->Nl); 159e1051a39Sopenharmony_ci p[sizeof(c->u) - 2] = (unsigned char)(c->Nl >> 8); 160e1051a39Sopenharmony_ci p[sizeof(c->u) - 3] = (unsigned char)(c->Nl >> 16); 161e1051a39Sopenharmony_ci p[sizeof(c->u) - 4] = (unsigned char)(c->Nl >> 24); 162e1051a39Sopenharmony_ci p[sizeof(c->u) - 5] = (unsigned char)(c->Nl >> 32); 163e1051a39Sopenharmony_ci p[sizeof(c->u) - 6] = (unsigned char)(c->Nl >> 40); 164e1051a39Sopenharmony_ci p[sizeof(c->u) - 7] = (unsigned char)(c->Nl >> 48); 165e1051a39Sopenharmony_ci p[sizeof(c->u) - 8] = (unsigned char)(c->Nl >> 56); 166e1051a39Sopenharmony_ci p[sizeof(c->u) - 9] = (unsigned char)(c->Nh); 167e1051a39Sopenharmony_ci p[sizeof(c->u) - 10] = (unsigned char)(c->Nh >> 8); 168e1051a39Sopenharmony_ci p[sizeof(c->u) - 11] = (unsigned char)(c->Nh >> 16); 169e1051a39Sopenharmony_ci p[sizeof(c->u) - 12] = (unsigned char)(c->Nh >> 24); 170e1051a39Sopenharmony_ci p[sizeof(c->u) - 13] = (unsigned char)(c->Nh >> 32); 171e1051a39Sopenharmony_ci p[sizeof(c->u) - 14] = (unsigned char)(c->Nh >> 40); 172e1051a39Sopenharmony_ci p[sizeof(c->u) - 15] = (unsigned char)(c->Nh >> 48); 173e1051a39Sopenharmony_ci p[sizeof(c->u) - 16] = (unsigned char)(c->Nh >> 56); 174e1051a39Sopenharmony_ci#endif 175e1051a39Sopenharmony_ci 176e1051a39Sopenharmony_ci sha512_block_data_order(c, p, 1); 177e1051a39Sopenharmony_ci 178e1051a39Sopenharmony_ci if (md == 0) 179e1051a39Sopenharmony_ci return 0; 180e1051a39Sopenharmony_ci 181e1051a39Sopenharmony_ci switch (c->md_len) { 182e1051a39Sopenharmony_ci /* Let compiler decide if it's appropriate to unroll... */ 183e1051a39Sopenharmony_ci case SHA224_DIGEST_LENGTH: 184e1051a39Sopenharmony_ci for (n = 0; n < SHA224_DIGEST_LENGTH / 8; n++) { 185e1051a39Sopenharmony_ci SHA_LONG64 t = c->h[n]; 186e1051a39Sopenharmony_ci 187e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 56); 188e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 48); 189e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 40); 190e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 32); 191e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 24); 192e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 16); 193e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 8); 194e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t); 195e1051a39Sopenharmony_ci } 196e1051a39Sopenharmony_ci /* 197e1051a39Sopenharmony_ci * For 224 bits, there are four bytes left over that have to be 198e1051a39Sopenharmony_ci * processed separately. 199e1051a39Sopenharmony_ci */ 200e1051a39Sopenharmony_ci { 201e1051a39Sopenharmony_ci SHA_LONG64 t = c->h[SHA224_DIGEST_LENGTH / 8]; 202e1051a39Sopenharmony_ci 203e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 56); 204e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 48); 205e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 40); 206e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 32); 207e1051a39Sopenharmony_ci } 208e1051a39Sopenharmony_ci break; 209e1051a39Sopenharmony_ci case SHA256_DIGEST_LENGTH: 210e1051a39Sopenharmony_ci for (n = 0; n < SHA256_DIGEST_LENGTH / 8; n++) { 211e1051a39Sopenharmony_ci SHA_LONG64 t = c->h[n]; 212e1051a39Sopenharmony_ci 213e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 56); 214e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 48); 215e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 40); 216e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 32); 217e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 24); 218e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 16); 219e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 8); 220e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t); 221e1051a39Sopenharmony_ci } 222e1051a39Sopenharmony_ci break; 223e1051a39Sopenharmony_ci case SHA384_DIGEST_LENGTH: 224e1051a39Sopenharmony_ci for (n = 0; n < SHA384_DIGEST_LENGTH / 8; n++) { 225e1051a39Sopenharmony_ci SHA_LONG64 t = c->h[n]; 226e1051a39Sopenharmony_ci 227e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 56); 228e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 48); 229e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 40); 230e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 32); 231e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 24); 232e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 16); 233e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 8); 234e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t); 235e1051a39Sopenharmony_ci } 236e1051a39Sopenharmony_ci break; 237e1051a39Sopenharmony_ci case SHA512_DIGEST_LENGTH: 238e1051a39Sopenharmony_ci for (n = 0; n < SHA512_DIGEST_LENGTH / 8; n++) { 239e1051a39Sopenharmony_ci SHA_LONG64 t = c->h[n]; 240e1051a39Sopenharmony_ci 241e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 56); 242e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 48); 243e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 40); 244e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 32); 245e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 24); 246e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 16); 247e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t >> 8); 248e1051a39Sopenharmony_ci *(md++) = (unsigned char)(t); 249e1051a39Sopenharmony_ci } 250e1051a39Sopenharmony_ci break; 251e1051a39Sopenharmony_ci /* ... as well as make sure md_len is not abused. */ 252e1051a39Sopenharmony_ci default: 253e1051a39Sopenharmony_ci return 0; 254e1051a39Sopenharmony_ci } 255e1051a39Sopenharmony_ci 256e1051a39Sopenharmony_ci return 1; 257e1051a39Sopenharmony_ci} 258e1051a39Sopenharmony_ci 259e1051a39Sopenharmony_ciint SHA384_Final(unsigned char *md, SHA512_CTX *c) 260e1051a39Sopenharmony_ci{ 261e1051a39Sopenharmony_ci return SHA512_Final(md, c); 262e1051a39Sopenharmony_ci} 263e1051a39Sopenharmony_ci 264e1051a39Sopenharmony_ciint SHA512_Update(SHA512_CTX *c, const void *_data, size_t len) 265e1051a39Sopenharmony_ci{ 266e1051a39Sopenharmony_ci SHA_LONG64 l; 267e1051a39Sopenharmony_ci unsigned char *p = c->u.p; 268e1051a39Sopenharmony_ci const unsigned char *data = (const unsigned char *)_data; 269e1051a39Sopenharmony_ci 270e1051a39Sopenharmony_ci if (len == 0) 271e1051a39Sopenharmony_ci return 1; 272e1051a39Sopenharmony_ci 273e1051a39Sopenharmony_ci l = (c->Nl + (((SHA_LONG64) len) << 3)) & U64(0xffffffffffffffff); 274e1051a39Sopenharmony_ci if (l < c->Nl) 275e1051a39Sopenharmony_ci c->Nh++; 276e1051a39Sopenharmony_ci if (sizeof(len) >= 8) 277e1051a39Sopenharmony_ci c->Nh += (((SHA_LONG64) len) >> 61); 278e1051a39Sopenharmony_ci c->Nl = l; 279e1051a39Sopenharmony_ci 280e1051a39Sopenharmony_ci if (c->num != 0) { 281e1051a39Sopenharmony_ci size_t n = sizeof(c->u) - c->num; 282e1051a39Sopenharmony_ci 283e1051a39Sopenharmony_ci if (len < n) { 284e1051a39Sopenharmony_ci memcpy(p + c->num, data, len), c->num += (unsigned int)len; 285e1051a39Sopenharmony_ci return 1; 286e1051a39Sopenharmony_ci } else { 287e1051a39Sopenharmony_ci memcpy(p + c->num, data, n), c->num = 0; 288e1051a39Sopenharmony_ci len -= n, data += n; 289e1051a39Sopenharmony_ci sha512_block_data_order(c, p, 1); 290e1051a39Sopenharmony_ci } 291e1051a39Sopenharmony_ci } 292e1051a39Sopenharmony_ci 293e1051a39Sopenharmony_ci if (len >= sizeof(c->u)) { 294e1051a39Sopenharmony_ci#ifndef SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA 295e1051a39Sopenharmony_ci if ((size_t)data % sizeof(c->u.d[0]) != 0) 296e1051a39Sopenharmony_ci while (len >= sizeof(c->u)) 297e1051a39Sopenharmony_ci memcpy(p, data, sizeof(c->u)), 298e1051a39Sopenharmony_ci sha512_block_data_order(c, p, 1), 299e1051a39Sopenharmony_ci len -= sizeof(c->u), data += sizeof(c->u); 300e1051a39Sopenharmony_ci else 301e1051a39Sopenharmony_ci#endif 302e1051a39Sopenharmony_ci sha512_block_data_order(c, data, len / sizeof(c->u)), 303e1051a39Sopenharmony_ci data += len, len %= sizeof(c->u), data -= len; 304e1051a39Sopenharmony_ci } 305e1051a39Sopenharmony_ci 306e1051a39Sopenharmony_ci if (len != 0) 307e1051a39Sopenharmony_ci memcpy(p, data, len), c->num = (int)len; 308e1051a39Sopenharmony_ci 309e1051a39Sopenharmony_ci return 1; 310e1051a39Sopenharmony_ci} 311e1051a39Sopenharmony_ci 312e1051a39Sopenharmony_ciint SHA384_Update(SHA512_CTX *c, const void *data, size_t len) 313e1051a39Sopenharmony_ci{ 314e1051a39Sopenharmony_ci return SHA512_Update(c, data, len); 315e1051a39Sopenharmony_ci} 316e1051a39Sopenharmony_ci 317e1051a39Sopenharmony_civoid SHA512_Transform(SHA512_CTX *c, const unsigned char *data) 318e1051a39Sopenharmony_ci{ 319e1051a39Sopenharmony_ci#ifndef SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA 320e1051a39Sopenharmony_ci if ((size_t)data % sizeof(c->u.d[0]) != 0) 321e1051a39Sopenharmony_ci memcpy(c->u.p, data, sizeof(c->u.p)), data = c->u.p; 322e1051a39Sopenharmony_ci#endif 323e1051a39Sopenharmony_ci sha512_block_data_order(c, data, 1); 324e1051a39Sopenharmony_ci} 325e1051a39Sopenharmony_ci 326e1051a39Sopenharmony_ciunsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md) 327e1051a39Sopenharmony_ci{ 328e1051a39Sopenharmony_ci SHA512_CTX c; 329e1051a39Sopenharmony_ci static unsigned char m[SHA384_DIGEST_LENGTH]; 330e1051a39Sopenharmony_ci 331e1051a39Sopenharmony_ci if (md == NULL) 332e1051a39Sopenharmony_ci md = m; 333e1051a39Sopenharmony_ci SHA384_Init(&c); 334e1051a39Sopenharmony_ci SHA512_Update(&c, d, n); 335e1051a39Sopenharmony_ci SHA512_Final(md, &c); 336e1051a39Sopenharmony_ci OPENSSL_cleanse(&c, sizeof(c)); 337e1051a39Sopenharmony_ci return md; 338e1051a39Sopenharmony_ci} 339e1051a39Sopenharmony_ci 340e1051a39Sopenharmony_ciunsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md) 341e1051a39Sopenharmony_ci{ 342e1051a39Sopenharmony_ci SHA512_CTX c; 343e1051a39Sopenharmony_ci static unsigned char m[SHA512_DIGEST_LENGTH]; 344e1051a39Sopenharmony_ci 345e1051a39Sopenharmony_ci if (md == NULL) 346e1051a39Sopenharmony_ci md = m; 347e1051a39Sopenharmony_ci SHA512_Init(&c); 348e1051a39Sopenharmony_ci SHA512_Update(&c, d, n); 349e1051a39Sopenharmony_ci SHA512_Final(md, &c); 350e1051a39Sopenharmony_ci OPENSSL_cleanse(&c, sizeof(c)); 351e1051a39Sopenharmony_ci return md; 352e1051a39Sopenharmony_ci} 353e1051a39Sopenharmony_ci 354e1051a39Sopenharmony_ci#ifndef SHA512_ASM 355e1051a39Sopenharmony_cistatic const SHA_LONG64 K512[80] = { 356e1051a39Sopenharmony_ci U64(0x428a2f98d728ae22), U64(0x7137449123ef65cd), 357e1051a39Sopenharmony_ci U64(0xb5c0fbcfec4d3b2f), U64(0xe9b5dba58189dbbc), 358e1051a39Sopenharmony_ci U64(0x3956c25bf348b538), U64(0x59f111f1b605d019), 359e1051a39Sopenharmony_ci U64(0x923f82a4af194f9b), U64(0xab1c5ed5da6d8118), 360e1051a39Sopenharmony_ci U64(0xd807aa98a3030242), U64(0x12835b0145706fbe), 361e1051a39Sopenharmony_ci U64(0x243185be4ee4b28c), U64(0x550c7dc3d5ffb4e2), 362e1051a39Sopenharmony_ci U64(0x72be5d74f27b896f), U64(0x80deb1fe3b1696b1), 363e1051a39Sopenharmony_ci U64(0x9bdc06a725c71235), U64(0xc19bf174cf692694), 364e1051a39Sopenharmony_ci U64(0xe49b69c19ef14ad2), U64(0xefbe4786384f25e3), 365e1051a39Sopenharmony_ci U64(0x0fc19dc68b8cd5b5), U64(0x240ca1cc77ac9c65), 366e1051a39Sopenharmony_ci U64(0x2de92c6f592b0275), U64(0x4a7484aa6ea6e483), 367e1051a39Sopenharmony_ci U64(0x5cb0a9dcbd41fbd4), U64(0x76f988da831153b5), 368e1051a39Sopenharmony_ci U64(0x983e5152ee66dfab), U64(0xa831c66d2db43210), 369e1051a39Sopenharmony_ci U64(0xb00327c898fb213f), U64(0xbf597fc7beef0ee4), 370e1051a39Sopenharmony_ci U64(0xc6e00bf33da88fc2), U64(0xd5a79147930aa725), 371e1051a39Sopenharmony_ci U64(0x06ca6351e003826f), U64(0x142929670a0e6e70), 372e1051a39Sopenharmony_ci U64(0x27b70a8546d22ffc), U64(0x2e1b21385c26c926), 373e1051a39Sopenharmony_ci U64(0x4d2c6dfc5ac42aed), U64(0x53380d139d95b3df), 374e1051a39Sopenharmony_ci U64(0x650a73548baf63de), U64(0x766a0abb3c77b2a8), 375e1051a39Sopenharmony_ci U64(0x81c2c92e47edaee6), U64(0x92722c851482353b), 376e1051a39Sopenharmony_ci U64(0xa2bfe8a14cf10364), U64(0xa81a664bbc423001), 377e1051a39Sopenharmony_ci U64(0xc24b8b70d0f89791), U64(0xc76c51a30654be30), 378e1051a39Sopenharmony_ci U64(0xd192e819d6ef5218), U64(0xd69906245565a910), 379e1051a39Sopenharmony_ci U64(0xf40e35855771202a), U64(0x106aa07032bbd1b8), 380e1051a39Sopenharmony_ci U64(0x19a4c116b8d2d0c8), U64(0x1e376c085141ab53), 381e1051a39Sopenharmony_ci U64(0x2748774cdf8eeb99), U64(0x34b0bcb5e19b48a8), 382e1051a39Sopenharmony_ci U64(0x391c0cb3c5c95a63), U64(0x4ed8aa4ae3418acb), 383e1051a39Sopenharmony_ci U64(0x5b9cca4f7763e373), U64(0x682e6ff3d6b2b8a3), 384e1051a39Sopenharmony_ci U64(0x748f82ee5defb2fc), U64(0x78a5636f43172f60), 385e1051a39Sopenharmony_ci U64(0x84c87814a1f0ab72), U64(0x8cc702081a6439ec), 386e1051a39Sopenharmony_ci U64(0x90befffa23631e28), U64(0xa4506cebde82bde9), 387e1051a39Sopenharmony_ci U64(0xbef9a3f7b2c67915), U64(0xc67178f2e372532b), 388e1051a39Sopenharmony_ci U64(0xca273eceea26619c), U64(0xd186b8c721c0c207), 389e1051a39Sopenharmony_ci U64(0xeada7dd6cde0eb1e), U64(0xf57d4f7fee6ed178), 390e1051a39Sopenharmony_ci U64(0x06f067aa72176fba), U64(0x0a637dc5a2c898a6), 391e1051a39Sopenharmony_ci U64(0x113f9804bef90dae), U64(0x1b710b35131c471b), 392e1051a39Sopenharmony_ci U64(0x28db77f523047d84), U64(0x32caab7b40c72493), 393e1051a39Sopenharmony_ci U64(0x3c9ebe0a15c9bebc), U64(0x431d67c49c100d4c), 394e1051a39Sopenharmony_ci U64(0x4cc5d4becb3e42b6), U64(0x597f299cfc657e2a), 395e1051a39Sopenharmony_ci U64(0x5fcb6fab3ad6faec), U64(0x6c44198c4a475817) 396e1051a39Sopenharmony_ci}; 397e1051a39Sopenharmony_ci 398e1051a39Sopenharmony_ci# ifndef PEDANTIC 399e1051a39Sopenharmony_ci# if defined(__GNUC__) && __GNUC__>=2 && \ 400e1051a39Sopenharmony_ci !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) 401e1051a39Sopenharmony_ci# if defined(__x86_64) || defined(__x86_64__) 402e1051a39Sopenharmony_ci# define ROTR(a,n) ({ SHA_LONG64 ret; \ 403e1051a39Sopenharmony_ci asm ("rorq %1,%0" \ 404e1051a39Sopenharmony_ci : "=r"(ret) \ 405e1051a39Sopenharmony_ci : "J"(n),"0"(a) \ 406e1051a39Sopenharmony_ci : "cc"); ret; }) 407e1051a39Sopenharmony_ci# if !defined(B_ENDIAN) 408e1051a39Sopenharmony_ci# define PULL64(x) ({ SHA_LONG64 ret=*((const SHA_LONG64 *)(&(x))); \ 409e1051a39Sopenharmony_ci asm ("bswapq %0" \ 410e1051a39Sopenharmony_ci : "=r"(ret) \ 411e1051a39Sopenharmony_ci : "0"(ret)); ret; }) 412e1051a39Sopenharmony_ci# endif 413e1051a39Sopenharmony_ci# elif (defined(__i386) || defined(__i386__)) && !defined(B_ENDIAN) 414e1051a39Sopenharmony_ci# if defined(I386_ONLY) 415e1051a39Sopenharmony_ci# define PULL64(x) ({ const unsigned int *p=(const unsigned int *)(&(x));\ 416e1051a39Sopenharmony_ci unsigned int hi=p[0],lo=p[1]; \ 417e1051a39Sopenharmony_ci asm("xchgb %%ah,%%al;xchgb %%dh,%%dl;"\ 418e1051a39Sopenharmony_ci "roll $16,%%eax; roll $16,%%edx; "\ 419e1051a39Sopenharmony_ci "xchgb %%ah,%%al;xchgb %%dh,%%dl;"\ 420e1051a39Sopenharmony_ci : "=a"(lo),"=d"(hi) \ 421e1051a39Sopenharmony_ci : "0"(lo),"1"(hi) : "cc"); \ 422e1051a39Sopenharmony_ci ((SHA_LONG64)hi)<<32|lo; }) 423e1051a39Sopenharmony_ci# else 424e1051a39Sopenharmony_ci# define PULL64(x) ({ const unsigned int *p=(const unsigned int *)(&(x));\ 425e1051a39Sopenharmony_ci unsigned int hi=p[0],lo=p[1]; \ 426e1051a39Sopenharmony_ci asm ("bswapl %0; bswapl %1;" \ 427e1051a39Sopenharmony_ci : "=r"(lo),"=r"(hi) \ 428e1051a39Sopenharmony_ci : "0"(lo),"1"(hi)); \ 429e1051a39Sopenharmony_ci ((SHA_LONG64)hi)<<32|lo; }) 430e1051a39Sopenharmony_ci# endif 431e1051a39Sopenharmony_ci# elif (defined(_ARCH_PPC) && defined(__64BIT__)) || defined(_ARCH_PPC64) 432e1051a39Sopenharmony_ci# define ROTR(a,n) ({ SHA_LONG64 ret; \ 433e1051a39Sopenharmony_ci asm ("rotrdi %0,%1,%2" \ 434e1051a39Sopenharmony_ci : "=r"(ret) \ 435e1051a39Sopenharmony_ci : "r"(a),"K"(n)); ret; }) 436e1051a39Sopenharmony_ci# elif defined(__aarch64__) 437e1051a39Sopenharmony_ci# define ROTR(a,n) ({ SHA_LONG64 ret; \ 438e1051a39Sopenharmony_ci asm ("ror %0,%1,%2" \ 439e1051a39Sopenharmony_ci : "=r"(ret) \ 440e1051a39Sopenharmony_ci : "r"(a),"I"(n)); ret; }) 441e1051a39Sopenharmony_ci# if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \ 442e1051a39Sopenharmony_ci __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__ 443e1051a39Sopenharmony_ci# define PULL64(x) ({ SHA_LONG64 ret; \ 444e1051a39Sopenharmony_ci asm ("rev %0,%1" \ 445e1051a39Sopenharmony_ci : "=r"(ret) \ 446e1051a39Sopenharmony_ci : "r"(*((const SHA_LONG64 *)(&(x))))); ret; }) 447e1051a39Sopenharmony_ci# endif 448e1051a39Sopenharmony_ci# endif 449e1051a39Sopenharmony_ci# elif defined(_MSC_VER) 450e1051a39Sopenharmony_ci# if defined(_WIN64) /* applies to both IA-64 and AMD64 */ 451e1051a39Sopenharmony_ci# pragma intrinsic(_rotr64) 452e1051a39Sopenharmony_ci# define ROTR(a,n) _rotr64((a),n) 453e1051a39Sopenharmony_ci# endif 454e1051a39Sopenharmony_ci# if defined(_M_IX86) && !defined(OPENSSL_NO_ASM) && \ 455e1051a39Sopenharmony_ci !defined(OPENSSL_NO_INLINE_ASM) 456e1051a39Sopenharmony_ci# if defined(I386_ONLY) 457e1051a39Sopenharmony_cistatic SHA_LONG64 __fastcall __pull64be(const void *x) 458e1051a39Sopenharmony_ci{ 459e1051a39Sopenharmony_ci _asm mov edx,[ecx + 0] 460e1051a39Sopenharmony_ci _asm mov eax,[ecx + 4] 461e1051a39Sopenharmony_ci _asm xchg dh, dl 462e1051a39Sopenharmony_ci _asm xchg ah, al 463e1051a39Sopenharmony_ci _asm rol edx, 16 464e1051a39Sopenharmony_ci _asm rol eax, 16 465e1051a39Sopenharmony_ci _asm xchg dh, dl 466e1051a39Sopenharmony_ci _asm xchg ah, al 467e1051a39Sopenharmony_ci} 468e1051a39Sopenharmony_ci# else 469e1051a39Sopenharmony_cistatic SHA_LONG64 __fastcall __pull64be(const void *x) 470e1051a39Sopenharmony_ci{ 471e1051a39Sopenharmony_ci _asm mov edx,[ecx + 0] 472e1051a39Sopenharmony_ci _asm mov eax,[ecx + 4] 473e1051a39Sopenharmony_ci _asm bswap edx 474e1051a39Sopenharmony_ci _asm bswap eax 475e1051a39Sopenharmony_ci} 476e1051a39Sopenharmony_ci# endif 477e1051a39Sopenharmony_ci# define PULL64(x) __pull64be(&(x)) 478e1051a39Sopenharmony_ci# endif 479e1051a39Sopenharmony_ci# endif 480e1051a39Sopenharmony_ci# endif 481e1051a39Sopenharmony_ci# ifndef PULL64 482e1051a39Sopenharmony_ci# define B(x,j) (((SHA_LONG64)(*(((const unsigned char *)(&x))+j)))<<((7-j)*8)) 483e1051a39Sopenharmony_ci# define PULL64(x) (B(x,0)|B(x,1)|B(x,2)|B(x,3)|B(x,4)|B(x,5)|B(x,6)|B(x,7)) 484e1051a39Sopenharmony_ci# endif 485e1051a39Sopenharmony_ci# ifndef ROTR 486e1051a39Sopenharmony_ci# define ROTR(x,s) (((x)>>s) | (x)<<(64-s)) 487e1051a39Sopenharmony_ci# endif 488e1051a39Sopenharmony_ci# define Sigma0(x) (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39)) 489e1051a39Sopenharmony_ci# define Sigma1(x) (ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41)) 490e1051a39Sopenharmony_ci# define sigma0(x) (ROTR((x),1) ^ ROTR((x),8) ^ ((x)>>7)) 491e1051a39Sopenharmony_ci# define sigma1(x) (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6)) 492e1051a39Sopenharmony_ci# define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) 493e1051a39Sopenharmony_ci# define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 494e1051a39Sopenharmony_ci 495e1051a39Sopenharmony_ci# if defined(__i386) || defined(__i386__) || defined(_M_IX86) 496e1051a39Sopenharmony_ci/* 497e1051a39Sopenharmony_ci * This code should give better results on 32-bit CPU with less than 498e1051a39Sopenharmony_ci * ~24 registers, both size and performance wise... 499e1051a39Sopenharmony_ci */ 500e1051a39Sopenharmony_ci 501e1051a39Sopenharmony_cistatic void sha512_block_data_order(SHA512_CTX *ctx, const void *in, 502e1051a39Sopenharmony_ci size_t num) 503e1051a39Sopenharmony_ci{ 504e1051a39Sopenharmony_ci const SHA_LONG64 *W = in; 505e1051a39Sopenharmony_ci SHA_LONG64 A, E, T; 506e1051a39Sopenharmony_ci SHA_LONG64 X[9 + 80], *F; 507e1051a39Sopenharmony_ci int i; 508e1051a39Sopenharmony_ci 509e1051a39Sopenharmony_ci while (num--) { 510e1051a39Sopenharmony_ci 511e1051a39Sopenharmony_ci F = X + 80; 512e1051a39Sopenharmony_ci A = ctx->h[0]; 513e1051a39Sopenharmony_ci F[1] = ctx->h[1]; 514e1051a39Sopenharmony_ci F[2] = ctx->h[2]; 515e1051a39Sopenharmony_ci F[3] = ctx->h[3]; 516e1051a39Sopenharmony_ci E = ctx->h[4]; 517e1051a39Sopenharmony_ci F[5] = ctx->h[5]; 518e1051a39Sopenharmony_ci F[6] = ctx->h[6]; 519e1051a39Sopenharmony_ci F[7] = ctx->h[7]; 520e1051a39Sopenharmony_ci 521e1051a39Sopenharmony_ci for (i = 0; i < 16; i++, F--) { 522e1051a39Sopenharmony_ci# ifdef B_ENDIAN 523e1051a39Sopenharmony_ci T = W[i]; 524e1051a39Sopenharmony_ci# else 525e1051a39Sopenharmony_ci T = PULL64(W[i]); 526e1051a39Sopenharmony_ci# endif 527e1051a39Sopenharmony_ci F[0] = A; 528e1051a39Sopenharmony_ci F[4] = E; 529e1051a39Sopenharmony_ci F[8] = T; 530e1051a39Sopenharmony_ci T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i]; 531e1051a39Sopenharmony_ci E = F[3] + T; 532e1051a39Sopenharmony_ci A = T + Sigma0(A) + Maj(A, F[1], F[2]); 533e1051a39Sopenharmony_ci } 534e1051a39Sopenharmony_ci 535e1051a39Sopenharmony_ci for (; i < 80; i++, F--) { 536e1051a39Sopenharmony_ci T = sigma0(F[8 + 16 - 1]); 537e1051a39Sopenharmony_ci T += sigma1(F[8 + 16 - 14]); 538e1051a39Sopenharmony_ci T += F[8 + 16] + F[8 + 16 - 9]; 539e1051a39Sopenharmony_ci 540e1051a39Sopenharmony_ci F[0] = A; 541e1051a39Sopenharmony_ci F[4] = E; 542e1051a39Sopenharmony_ci F[8] = T; 543e1051a39Sopenharmony_ci T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i]; 544e1051a39Sopenharmony_ci E = F[3] + T; 545e1051a39Sopenharmony_ci A = T + Sigma0(A) + Maj(A, F[1], F[2]); 546e1051a39Sopenharmony_ci } 547e1051a39Sopenharmony_ci 548e1051a39Sopenharmony_ci ctx->h[0] += A; 549e1051a39Sopenharmony_ci ctx->h[1] += F[1]; 550e1051a39Sopenharmony_ci ctx->h[2] += F[2]; 551e1051a39Sopenharmony_ci ctx->h[3] += F[3]; 552e1051a39Sopenharmony_ci ctx->h[4] += E; 553e1051a39Sopenharmony_ci ctx->h[5] += F[5]; 554e1051a39Sopenharmony_ci ctx->h[6] += F[6]; 555e1051a39Sopenharmony_ci ctx->h[7] += F[7]; 556e1051a39Sopenharmony_ci 557e1051a39Sopenharmony_ci W += SHA_LBLOCK; 558e1051a39Sopenharmony_ci } 559e1051a39Sopenharmony_ci} 560e1051a39Sopenharmony_ci 561e1051a39Sopenharmony_ci# elif defined(OPENSSL_SMALL_FOOTPRINT) 562e1051a39Sopenharmony_ci 563e1051a39Sopenharmony_cistatic void sha512_block_data_order(SHA512_CTX *ctx, const void *in, 564e1051a39Sopenharmony_ci size_t num) 565e1051a39Sopenharmony_ci{ 566e1051a39Sopenharmony_ci const SHA_LONG64 *W = in; 567e1051a39Sopenharmony_ci SHA_LONG64 a, b, c, d, e, f, g, h, s0, s1, T1, T2; 568e1051a39Sopenharmony_ci SHA_LONG64 X[16]; 569e1051a39Sopenharmony_ci int i; 570e1051a39Sopenharmony_ci 571e1051a39Sopenharmony_ci while (num--) { 572e1051a39Sopenharmony_ci 573e1051a39Sopenharmony_ci a = ctx->h[0]; 574e1051a39Sopenharmony_ci b = ctx->h[1]; 575e1051a39Sopenharmony_ci c = ctx->h[2]; 576e1051a39Sopenharmony_ci d = ctx->h[3]; 577e1051a39Sopenharmony_ci e = ctx->h[4]; 578e1051a39Sopenharmony_ci f = ctx->h[5]; 579e1051a39Sopenharmony_ci g = ctx->h[6]; 580e1051a39Sopenharmony_ci h = ctx->h[7]; 581e1051a39Sopenharmony_ci 582e1051a39Sopenharmony_ci for (i = 0; i < 16; i++) { 583e1051a39Sopenharmony_ci# ifdef B_ENDIAN 584e1051a39Sopenharmony_ci T1 = X[i] = W[i]; 585e1051a39Sopenharmony_ci# else 586e1051a39Sopenharmony_ci T1 = X[i] = PULL64(W[i]); 587e1051a39Sopenharmony_ci# endif 588e1051a39Sopenharmony_ci T1 += h + Sigma1(e) + Ch(e, f, g) + K512[i]; 589e1051a39Sopenharmony_ci T2 = Sigma0(a) + Maj(a, b, c); 590e1051a39Sopenharmony_ci h = g; 591e1051a39Sopenharmony_ci g = f; 592e1051a39Sopenharmony_ci f = e; 593e1051a39Sopenharmony_ci e = d + T1; 594e1051a39Sopenharmony_ci d = c; 595e1051a39Sopenharmony_ci c = b; 596e1051a39Sopenharmony_ci b = a; 597e1051a39Sopenharmony_ci a = T1 + T2; 598e1051a39Sopenharmony_ci } 599e1051a39Sopenharmony_ci 600e1051a39Sopenharmony_ci for (; i < 80; i++) { 601e1051a39Sopenharmony_ci s0 = X[(i + 1) & 0x0f]; 602e1051a39Sopenharmony_ci s0 = sigma0(s0); 603e1051a39Sopenharmony_ci s1 = X[(i + 14) & 0x0f]; 604e1051a39Sopenharmony_ci s1 = sigma1(s1); 605e1051a39Sopenharmony_ci 606e1051a39Sopenharmony_ci T1 = X[i & 0xf] += s0 + s1 + X[(i + 9) & 0xf]; 607e1051a39Sopenharmony_ci T1 += h + Sigma1(e) + Ch(e, f, g) + K512[i]; 608e1051a39Sopenharmony_ci T2 = Sigma0(a) + Maj(a, b, c); 609e1051a39Sopenharmony_ci h = g; 610e1051a39Sopenharmony_ci g = f; 611e1051a39Sopenharmony_ci f = e; 612e1051a39Sopenharmony_ci e = d + T1; 613e1051a39Sopenharmony_ci d = c; 614e1051a39Sopenharmony_ci c = b; 615e1051a39Sopenharmony_ci b = a; 616e1051a39Sopenharmony_ci a = T1 + T2; 617e1051a39Sopenharmony_ci } 618e1051a39Sopenharmony_ci 619e1051a39Sopenharmony_ci ctx->h[0] += a; 620e1051a39Sopenharmony_ci ctx->h[1] += b; 621e1051a39Sopenharmony_ci ctx->h[2] += c; 622e1051a39Sopenharmony_ci ctx->h[3] += d; 623e1051a39Sopenharmony_ci ctx->h[4] += e; 624e1051a39Sopenharmony_ci ctx->h[5] += f; 625e1051a39Sopenharmony_ci ctx->h[6] += g; 626e1051a39Sopenharmony_ci ctx->h[7] += h; 627e1051a39Sopenharmony_ci 628e1051a39Sopenharmony_ci W += SHA_LBLOCK; 629e1051a39Sopenharmony_ci } 630e1051a39Sopenharmony_ci} 631e1051a39Sopenharmony_ci 632e1051a39Sopenharmony_ci# else 633e1051a39Sopenharmony_ci# define ROUND_00_15(i,a,b,c,d,e,f,g,h) do { \ 634e1051a39Sopenharmony_ci T1 += h + Sigma1(e) + Ch(e,f,g) + K512[i]; \ 635e1051a39Sopenharmony_ci h = Sigma0(a) + Maj(a,b,c); \ 636e1051a39Sopenharmony_ci d += T1; h += T1; } while (0) 637e1051a39Sopenharmony_ci 638e1051a39Sopenharmony_ci# define ROUND_16_80(i,j,a,b,c,d,e,f,g,h,X) do { \ 639e1051a39Sopenharmony_ci s0 = X[(j+1)&0x0f]; s0 = sigma0(s0); \ 640e1051a39Sopenharmony_ci s1 = X[(j+14)&0x0f]; s1 = sigma1(s1); \ 641e1051a39Sopenharmony_ci T1 = X[(j)&0x0f] += s0 + s1 + X[(j+9)&0x0f]; \ 642e1051a39Sopenharmony_ci ROUND_00_15(i+j,a,b,c,d,e,f,g,h); } while (0) 643e1051a39Sopenharmony_ci 644e1051a39Sopenharmony_cistatic void sha512_block_data_order(SHA512_CTX *ctx, const void *in, 645e1051a39Sopenharmony_ci size_t num) 646e1051a39Sopenharmony_ci{ 647e1051a39Sopenharmony_ci const SHA_LONG64 *W = in; 648e1051a39Sopenharmony_ci SHA_LONG64 a, b, c, d, e, f, g, h, s0, s1, T1; 649e1051a39Sopenharmony_ci SHA_LONG64 X[16]; 650e1051a39Sopenharmony_ci int i; 651e1051a39Sopenharmony_ci 652e1051a39Sopenharmony_ci while (num--) { 653e1051a39Sopenharmony_ci 654e1051a39Sopenharmony_ci a = ctx->h[0]; 655e1051a39Sopenharmony_ci b = ctx->h[1]; 656e1051a39Sopenharmony_ci c = ctx->h[2]; 657e1051a39Sopenharmony_ci d = ctx->h[3]; 658e1051a39Sopenharmony_ci e = ctx->h[4]; 659e1051a39Sopenharmony_ci f = ctx->h[5]; 660e1051a39Sopenharmony_ci g = ctx->h[6]; 661e1051a39Sopenharmony_ci h = ctx->h[7]; 662e1051a39Sopenharmony_ci 663e1051a39Sopenharmony_ci# ifdef B_ENDIAN 664e1051a39Sopenharmony_ci T1 = X[0] = W[0]; 665e1051a39Sopenharmony_ci ROUND_00_15(0, a, b, c, d, e, f, g, h); 666e1051a39Sopenharmony_ci T1 = X[1] = W[1]; 667e1051a39Sopenharmony_ci ROUND_00_15(1, h, a, b, c, d, e, f, g); 668e1051a39Sopenharmony_ci T1 = X[2] = W[2]; 669e1051a39Sopenharmony_ci ROUND_00_15(2, g, h, a, b, c, d, e, f); 670e1051a39Sopenharmony_ci T1 = X[3] = W[3]; 671e1051a39Sopenharmony_ci ROUND_00_15(3, f, g, h, a, b, c, d, e); 672e1051a39Sopenharmony_ci T1 = X[4] = W[4]; 673e1051a39Sopenharmony_ci ROUND_00_15(4, e, f, g, h, a, b, c, d); 674e1051a39Sopenharmony_ci T1 = X[5] = W[5]; 675e1051a39Sopenharmony_ci ROUND_00_15(5, d, e, f, g, h, a, b, c); 676e1051a39Sopenharmony_ci T1 = X[6] = W[6]; 677e1051a39Sopenharmony_ci ROUND_00_15(6, c, d, e, f, g, h, a, b); 678e1051a39Sopenharmony_ci T1 = X[7] = W[7]; 679e1051a39Sopenharmony_ci ROUND_00_15(7, b, c, d, e, f, g, h, a); 680e1051a39Sopenharmony_ci T1 = X[8] = W[8]; 681e1051a39Sopenharmony_ci ROUND_00_15(8, a, b, c, d, e, f, g, h); 682e1051a39Sopenharmony_ci T1 = X[9] = W[9]; 683e1051a39Sopenharmony_ci ROUND_00_15(9, h, a, b, c, d, e, f, g); 684e1051a39Sopenharmony_ci T1 = X[10] = W[10]; 685e1051a39Sopenharmony_ci ROUND_00_15(10, g, h, a, b, c, d, e, f); 686e1051a39Sopenharmony_ci T1 = X[11] = W[11]; 687e1051a39Sopenharmony_ci ROUND_00_15(11, f, g, h, a, b, c, d, e); 688e1051a39Sopenharmony_ci T1 = X[12] = W[12]; 689e1051a39Sopenharmony_ci ROUND_00_15(12, e, f, g, h, a, b, c, d); 690e1051a39Sopenharmony_ci T1 = X[13] = W[13]; 691e1051a39Sopenharmony_ci ROUND_00_15(13, d, e, f, g, h, a, b, c); 692e1051a39Sopenharmony_ci T1 = X[14] = W[14]; 693e1051a39Sopenharmony_ci ROUND_00_15(14, c, d, e, f, g, h, a, b); 694e1051a39Sopenharmony_ci T1 = X[15] = W[15]; 695e1051a39Sopenharmony_ci ROUND_00_15(15, b, c, d, e, f, g, h, a); 696e1051a39Sopenharmony_ci# else 697e1051a39Sopenharmony_ci T1 = X[0] = PULL64(W[0]); 698e1051a39Sopenharmony_ci ROUND_00_15(0, a, b, c, d, e, f, g, h); 699e1051a39Sopenharmony_ci T1 = X[1] = PULL64(W[1]); 700e1051a39Sopenharmony_ci ROUND_00_15(1, h, a, b, c, d, e, f, g); 701e1051a39Sopenharmony_ci T1 = X[2] = PULL64(W[2]); 702e1051a39Sopenharmony_ci ROUND_00_15(2, g, h, a, b, c, d, e, f); 703e1051a39Sopenharmony_ci T1 = X[3] = PULL64(W[3]); 704e1051a39Sopenharmony_ci ROUND_00_15(3, f, g, h, a, b, c, d, e); 705e1051a39Sopenharmony_ci T1 = X[4] = PULL64(W[4]); 706e1051a39Sopenharmony_ci ROUND_00_15(4, e, f, g, h, a, b, c, d); 707e1051a39Sopenharmony_ci T1 = X[5] = PULL64(W[5]); 708e1051a39Sopenharmony_ci ROUND_00_15(5, d, e, f, g, h, a, b, c); 709e1051a39Sopenharmony_ci T1 = X[6] = PULL64(W[6]); 710e1051a39Sopenharmony_ci ROUND_00_15(6, c, d, e, f, g, h, a, b); 711e1051a39Sopenharmony_ci T1 = X[7] = PULL64(W[7]); 712e1051a39Sopenharmony_ci ROUND_00_15(7, b, c, d, e, f, g, h, a); 713e1051a39Sopenharmony_ci T1 = X[8] = PULL64(W[8]); 714e1051a39Sopenharmony_ci ROUND_00_15(8, a, b, c, d, e, f, g, h); 715e1051a39Sopenharmony_ci T1 = X[9] = PULL64(W[9]); 716e1051a39Sopenharmony_ci ROUND_00_15(9, h, a, b, c, d, e, f, g); 717e1051a39Sopenharmony_ci T1 = X[10] = PULL64(W[10]); 718e1051a39Sopenharmony_ci ROUND_00_15(10, g, h, a, b, c, d, e, f); 719e1051a39Sopenharmony_ci T1 = X[11] = PULL64(W[11]); 720e1051a39Sopenharmony_ci ROUND_00_15(11, f, g, h, a, b, c, d, e); 721e1051a39Sopenharmony_ci T1 = X[12] = PULL64(W[12]); 722e1051a39Sopenharmony_ci ROUND_00_15(12, e, f, g, h, a, b, c, d); 723e1051a39Sopenharmony_ci T1 = X[13] = PULL64(W[13]); 724e1051a39Sopenharmony_ci ROUND_00_15(13, d, e, f, g, h, a, b, c); 725e1051a39Sopenharmony_ci T1 = X[14] = PULL64(W[14]); 726e1051a39Sopenharmony_ci ROUND_00_15(14, c, d, e, f, g, h, a, b); 727e1051a39Sopenharmony_ci T1 = X[15] = PULL64(W[15]); 728e1051a39Sopenharmony_ci ROUND_00_15(15, b, c, d, e, f, g, h, a); 729e1051a39Sopenharmony_ci# endif 730e1051a39Sopenharmony_ci 731e1051a39Sopenharmony_ci for (i = 16; i < 80; i += 16) { 732e1051a39Sopenharmony_ci ROUND_16_80(i, 0, a, b, c, d, e, f, g, h, X); 733e1051a39Sopenharmony_ci ROUND_16_80(i, 1, h, a, b, c, d, e, f, g, X); 734e1051a39Sopenharmony_ci ROUND_16_80(i, 2, g, h, a, b, c, d, e, f, X); 735e1051a39Sopenharmony_ci ROUND_16_80(i, 3, f, g, h, a, b, c, d, e, X); 736e1051a39Sopenharmony_ci ROUND_16_80(i, 4, e, f, g, h, a, b, c, d, X); 737e1051a39Sopenharmony_ci ROUND_16_80(i, 5, d, e, f, g, h, a, b, c, X); 738e1051a39Sopenharmony_ci ROUND_16_80(i, 6, c, d, e, f, g, h, a, b, X); 739e1051a39Sopenharmony_ci ROUND_16_80(i, 7, b, c, d, e, f, g, h, a, X); 740e1051a39Sopenharmony_ci ROUND_16_80(i, 8, a, b, c, d, e, f, g, h, X); 741e1051a39Sopenharmony_ci ROUND_16_80(i, 9, h, a, b, c, d, e, f, g, X); 742e1051a39Sopenharmony_ci ROUND_16_80(i, 10, g, h, a, b, c, d, e, f, X); 743e1051a39Sopenharmony_ci ROUND_16_80(i, 11, f, g, h, a, b, c, d, e, X); 744e1051a39Sopenharmony_ci ROUND_16_80(i, 12, e, f, g, h, a, b, c, d, X); 745e1051a39Sopenharmony_ci ROUND_16_80(i, 13, d, e, f, g, h, a, b, c, X); 746e1051a39Sopenharmony_ci ROUND_16_80(i, 14, c, d, e, f, g, h, a, b, X); 747e1051a39Sopenharmony_ci ROUND_16_80(i, 15, b, c, d, e, f, g, h, a, X); 748e1051a39Sopenharmony_ci } 749e1051a39Sopenharmony_ci 750e1051a39Sopenharmony_ci ctx->h[0] += a; 751e1051a39Sopenharmony_ci ctx->h[1] += b; 752e1051a39Sopenharmony_ci ctx->h[2] += c; 753e1051a39Sopenharmony_ci ctx->h[3] += d; 754e1051a39Sopenharmony_ci ctx->h[4] += e; 755e1051a39Sopenharmony_ci ctx->h[5] += f; 756e1051a39Sopenharmony_ci ctx->h[6] += g; 757e1051a39Sopenharmony_ci ctx->h[7] += h; 758e1051a39Sopenharmony_ci 759e1051a39Sopenharmony_ci W += SHA_LBLOCK; 760e1051a39Sopenharmony_ci } 761e1051a39Sopenharmony_ci} 762e1051a39Sopenharmony_ci 763e1051a39Sopenharmony_ci# endif 764e1051a39Sopenharmony_ci 765e1051a39Sopenharmony_ci#endif /* SHA512_ASM */ 766