wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue May 30 01:44:10 2017 +0000
Revision:
11:cee25a834751
wolfSSL 3.11.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 11:cee25a834751 1 /* sha512.c
wolfSSL 11:cee25a834751 2 *
wolfSSL 11:cee25a834751 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 11:cee25a834751 4 *
wolfSSL 11:cee25a834751 5 * This file is part of wolfSSL.
wolfSSL 11:cee25a834751 6 *
wolfSSL 11:cee25a834751 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 11:cee25a834751 8 * it under the terms of the GNU General Public License as published by
wolfSSL 11:cee25a834751 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 11:cee25a834751 10 * (at your option) any later version.
wolfSSL 11:cee25a834751 11 *
wolfSSL 11:cee25a834751 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 11:cee25a834751 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 11:cee25a834751 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 11:cee25a834751 15 * GNU General Public License for more details.
wolfSSL 11:cee25a834751 16 *
wolfSSL 11:cee25a834751 17 * You should have received a copy of the GNU General Public License
wolfSSL 11:cee25a834751 18 * along with this program; if not, write to the Free Software
wolfSSL 11:cee25a834751 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 11:cee25a834751 20 */
wolfSSL 11:cee25a834751 21
wolfSSL 11:cee25a834751 22
wolfSSL 11:cee25a834751 23 #ifdef HAVE_CONFIG_H
wolfSSL 11:cee25a834751 24 #include <config.h>
wolfSSL 11:cee25a834751 25 #endif
wolfSSL 11:cee25a834751 26
wolfSSL 11:cee25a834751 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 11:cee25a834751 28
wolfSSL 11:cee25a834751 29 #ifdef WOLFSSL_SHA512
wolfSSL 11:cee25a834751 30
wolfSSL 11:cee25a834751 31 #include <wolfssl/wolfcrypt/sha512.h>
wolfSSL 11:cee25a834751 32 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 11:cee25a834751 33
wolfSSL 11:cee25a834751 34 /* fips wrapper calls, user can call direct */
wolfSSL 11:cee25a834751 35 #ifdef HAVE_FIPS
wolfSSL 11:cee25a834751 36 int wc_InitSha512(Sha512* sha)
wolfSSL 11:cee25a834751 37 {
wolfSSL 11:cee25a834751 38 return InitSha512_fips(sha);
wolfSSL 11:cee25a834751 39 }
wolfSSL 11:cee25a834751 40 int wc_InitSha512_ex(Sha512* sha, void* heap, int devId)
wolfSSL 11:cee25a834751 41 {
wolfSSL 11:cee25a834751 42 (void)heap;
wolfSSL 11:cee25a834751 43 (void)devId;
wolfSSL 11:cee25a834751 44 return InitSha512_fips(sha);
wolfSSL 11:cee25a834751 45 }
wolfSSL 11:cee25a834751 46 int wc_Sha512Update(Sha512* sha, const byte* data, word32 len)
wolfSSL 11:cee25a834751 47 {
wolfSSL 11:cee25a834751 48 return Sha512Update_fips(sha, data, len);
wolfSSL 11:cee25a834751 49 }
wolfSSL 11:cee25a834751 50 int wc_Sha512Final(Sha512* sha, byte* out)
wolfSSL 11:cee25a834751 51 {
wolfSSL 11:cee25a834751 52 return Sha512Final_fips(sha, out);
wolfSSL 11:cee25a834751 53 }
wolfSSL 11:cee25a834751 54 void wc_Sha512Free(Sha512* sha)
wolfSSL 11:cee25a834751 55 {
wolfSSL 11:cee25a834751 56 (void)sha;
wolfSSL 11:cee25a834751 57 /* Not supported in FIPS */
wolfSSL 11:cee25a834751 58 }
wolfSSL 11:cee25a834751 59
wolfSSL 11:cee25a834751 60 #if defined(WOLFSSL_SHA384) || defined(HAVE_AESGCM)
wolfSSL 11:cee25a834751 61 int wc_InitSha384(Sha384* sha)
wolfSSL 11:cee25a834751 62 {
wolfSSL 11:cee25a834751 63 return InitSha384_fips(sha);
wolfSSL 11:cee25a834751 64 }
wolfSSL 11:cee25a834751 65 int wc_InitSha384_ex(Sha384* sha, void* heap, int devId)
wolfSSL 11:cee25a834751 66 {
wolfSSL 11:cee25a834751 67 (void)heap;
wolfSSL 11:cee25a834751 68 (void)devId;
wolfSSL 11:cee25a834751 69 return InitSha384_fips(sha);
wolfSSL 11:cee25a834751 70 }
wolfSSL 11:cee25a834751 71 int wc_Sha384Update(Sha384* sha, const byte* data, word32 len)
wolfSSL 11:cee25a834751 72 {
wolfSSL 11:cee25a834751 73 return Sha384Update_fips(sha, data, len);
wolfSSL 11:cee25a834751 74 }
wolfSSL 11:cee25a834751 75 int wc_Sha384Final(Sha384* sha, byte* out)
wolfSSL 11:cee25a834751 76 {
wolfSSL 11:cee25a834751 77 return Sha384Final_fips(sha, out);
wolfSSL 11:cee25a834751 78 }
wolfSSL 11:cee25a834751 79 void wc_Sha384Free(Sha384* sha)
wolfSSL 11:cee25a834751 80 {
wolfSSL 11:cee25a834751 81 (void)sha;
wolfSSL 11:cee25a834751 82 /* Not supported in FIPS */
wolfSSL 11:cee25a834751 83 }
wolfSSL 11:cee25a834751 84 #endif /* WOLFSSL_SHA384 || HAVE_AESGCM */
wolfSSL 11:cee25a834751 85
wolfSSL 11:cee25a834751 86 #else /* else build without using fips */
wolfSSL 11:cee25a834751 87
wolfSSL 11:cee25a834751 88 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 11:cee25a834751 89
wolfSSL 11:cee25a834751 90 #ifdef NO_INLINE
wolfSSL 11:cee25a834751 91 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 11:cee25a834751 92 #else
wolfSSL 11:cee25a834751 93 #define WOLFSSL_MISC_INCLUDED
wolfSSL 11:cee25a834751 94 #include <wolfcrypt/src/misc.c>
wolfSSL 11:cee25a834751 95 #endif
wolfSSL 11:cee25a834751 96
wolfSSL 11:cee25a834751 97
wolfSSL 11:cee25a834751 98 #if defined(USE_INTEL_SPEEDUP)
wolfSSL 11:cee25a834751 99 #define HAVE_INTEL_AVX1
wolfSSL 11:cee25a834751 100 #define HAVE_INTEL_AVX2
wolfSSL 11:cee25a834751 101 #endif
wolfSSL 11:cee25a834751 102
wolfSSL 11:cee25a834751 103 #if defined(HAVE_INTEL_AVX1)
wolfSSL 11:cee25a834751 104 /* #define DEBUG_XMM */
wolfSSL 11:cee25a834751 105 #endif
wolfSSL 11:cee25a834751 106
wolfSSL 11:cee25a834751 107 #if defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 108 #define HAVE_INTEL_RORX
wolfSSL 11:cee25a834751 109 /* #define DEBUG_YMM */
wolfSSL 11:cee25a834751 110 #endif
wolfSSL 11:cee25a834751 111
wolfSSL 11:cee25a834751 112
wolfSSL 11:cee25a834751 113 #if defined(HAVE_INTEL_RORX)
wolfSSL 11:cee25a834751 114 #define ROTR(func, bits, x) \
wolfSSL 11:cee25a834751 115 word64 func(word64 x) { word64 ret ;\
wolfSSL 11:cee25a834751 116 __asm__ ("rorx $"#bits", %1, %0\n\t":"=r"(ret):"r"(x):) ;\
wolfSSL 11:cee25a834751 117 return ret ;\
wolfSSL 11:cee25a834751 118 }
wolfSSL 11:cee25a834751 119
wolfSSL 11:cee25a834751 120 static INLINE ROTR(rotrFixed64_28, 28, x);
wolfSSL 11:cee25a834751 121 static INLINE ROTR(rotrFixed64_34, 34, x);
wolfSSL 11:cee25a834751 122 static INLINE ROTR(rotrFixed64_39, 39, x);
wolfSSL 11:cee25a834751 123 static INLINE ROTR(rotrFixed64_14, 14, x);
wolfSSL 11:cee25a834751 124 static INLINE ROTR(rotrFixed64_18, 18, x);
wolfSSL 11:cee25a834751 125 static INLINE ROTR(rotrFixed64_41, 41, x);
wolfSSL 11:cee25a834751 126
wolfSSL 11:cee25a834751 127 #define S0_RORX(x) (rotrFixed64_28(x)^rotrFixed64_34(x)^rotrFixed64_39(x))
wolfSSL 11:cee25a834751 128 #define S1_RORX(x) (rotrFixed64_14(x)^rotrFixed64_18(x)^rotrFixed64_41(x))
wolfSSL 11:cee25a834751 129 #endif /* HAVE_INTEL_RORX */
wolfSSL 11:cee25a834751 130
wolfSSL 11:cee25a834751 131 #if defined(HAVE_BYTEREVERSE64) && \
wolfSSL 11:cee25a834751 132 !defined(HAVE_INTEL_AVX1) && !defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 133 #define ByteReverseWords64(out, in, size) ByteReverseWords64_1(out, size)
wolfSSL 11:cee25a834751 134 #define ByteReverseWords64_1(buf, size) \
wolfSSL 11:cee25a834751 135 { unsigned int i ;\
wolfSSL 11:cee25a834751 136 for(i=0; i< size/sizeof(word64); i++){\
wolfSSL 11:cee25a834751 137 __asm__ volatile("bswapq %0":"+r"(buf[i])::) ;\
wolfSSL 11:cee25a834751 138 }\
wolfSSL 11:cee25a834751 139 }
wolfSSL 11:cee25a834751 140 #endif
wolfSSL 11:cee25a834751 141
wolfSSL 11:cee25a834751 142 static int InitSha512(Sha512* sha512)
wolfSSL 11:cee25a834751 143 {
wolfSSL 11:cee25a834751 144 if (sha512 == NULL)
wolfSSL 11:cee25a834751 145 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 146
wolfSSL 11:cee25a834751 147 sha512->digest[0] = W64LIT(0x6a09e667f3bcc908);
wolfSSL 11:cee25a834751 148 sha512->digest[1] = W64LIT(0xbb67ae8584caa73b);
wolfSSL 11:cee25a834751 149 sha512->digest[2] = W64LIT(0x3c6ef372fe94f82b);
wolfSSL 11:cee25a834751 150 sha512->digest[3] = W64LIT(0xa54ff53a5f1d36f1);
wolfSSL 11:cee25a834751 151 sha512->digest[4] = W64LIT(0x510e527fade682d1);
wolfSSL 11:cee25a834751 152 sha512->digest[5] = W64LIT(0x9b05688c2b3e6c1f);
wolfSSL 11:cee25a834751 153 sha512->digest[6] = W64LIT(0x1f83d9abfb41bd6b);
wolfSSL 11:cee25a834751 154 sha512->digest[7] = W64LIT(0x5be0cd19137e2179);
wolfSSL 11:cee25a834751 155
wolfSSL 11:cee25a834751 156 sha512->buffLen = 0;
wolfSSL 11:cee25a834751 157 sha512->loLen = 0;
wolfSSL 11:cee25a834751 158 sha512->hiLen = 0;
wolfSSL 11:cee25a834751 159
wolfSSL 11:cee25a834751 160 return 0;
wolfSSL 11:cee25a834751 161 }
wolfSSL 11:cee25a834751 162
wolfSSL 11:cee25a834751 163
wolfSSL 11:cee25a834751 164 /* Hardware Acceleration */
wolfSSL 11:cee25a834751 165 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 166
wolfSSL 11:cee25a834751 167 /*****
wolfSSL 11:cee25a834751 168 Intel AVX1/AVX2 Macro Control Structure
wolfSSL 11:cee25a834751 169
wolfSSL 11:cee25a834751 170 #if defined(HAVE_INteL_SPEEDUP)
wolfSSL 11:cee25a834751 171 #define HAVE_INTEL_AVX1
wolfSSL 11:cee25a834751 172 #define HAVE_INTEL_AVX2
wolfSSL 11:cee25a834751 173 #endif
wolfSSL 11:cee25a834751 174
wolfSSL 11:cee25a834751 175 int InitSha512(Sha512* sha512) {
wolfSSL 11:cee25a834751 176 Save/Recover XMM, YMM
wolfSSL 11:cee25a834751 177 ...
wolfSSL 11:cee25a834751 178
wolfSSL 11:cee25a834751 179 Check Intel AVX cpuid flags
wolfSSL 11:cee25a834751 180 }
wolfSSL 11:cee25a834751 181
wolfSSL 11:cee25a834751 182 #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 183 Transform_AVX1(); # Function prototype
wolfSSL 11:cee25a834751 184 Transform_AVX2(); #
wolfSSL 11:cee25a834751 185 #endif
wolfSSL 11:cee25a834751 186
wolfSSL 11:cee25a834751 187 _Transform() { # Native Transform Function body
wolfSSL 11:cee25a834751 188
wolfSSL 11:cee25a834751 189 }
wolfSSL 11:cee25a834751 190
wolfSSL 11:cee25a834751 191 int Sha512Update() {
wolfSSL 11:cee25a834751 192 Save/Recover XMM, YMM
wolfSSL 11:cee25a834751 193 ...
wolfSSL 11:cee25a834751 194 }
wolfSSL 11:cee25a834751 195
wolfSSL 11:cee25a834751 196 int Sha512Final() {
wolfSSL 11:cee25a834751 197 Save/Recover XMM, YMM
wolfSSL 11:cee25a834751 198 ...
wolfSSL 11:cee25a834751 199 }
wolfSSL 11:cee25a834751 200
wolfSSL 11:cee25a834751 201
wolfSSL 11:cee25a834751 202 #if defined(HAVE_INTEL_AVX1)
wolfSSL 11:cee25a834751 203
wolfSSL 11:cee25a834751 204 XMM Instructions/INLINE asm Definitions
wolfSSL 11:cee25a834751 205
wolfSSL 11:cee25a834751 206 #endif
wolfSSL 11:cee25a834751 207
wolfSSL 11:cee25a834751 208 #if defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 209
wolfSSL 11:cee25a834751 210 YMM Instructions/INLINE asm Definitions
wolfSSL 11:cee25a834751 211
wolfSSL 11:cee25a834751 212 #endif
wolfSSL 11:cee25a834751 213
wolfSSL 11:cee25a834751 214 #if defnied(HAVE_INTEL_AVX1)
wolfSSL 11:cee25a834751 215
wolfSSL 11:cee25a834751 216 int Transform_AVX1() {
wolfSSL 11:cee25a834751 217 Stitched Message Sched/Round
wolfSSL 11:cee25a834751 218 }
wolfSSL 11:cee25a834751 219
wolfSSL 11:cee25a834751 220 #endif
wolfSSL 11:cee25a834751 221
wolfSSL 11:cee25a834751 222 #if defnied(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 223
wolfSSL 11:cee25a834751 224 int Transform_AVX2() {
wolfSSL 11:cee25a834751 225 Stitched Message Sched/Round
wolfSSL 11:cee25a834751 226 }
wolfSSL 11:cee25a834751 227 #endif
wolfSSL 11:cee25a834751 228
wolfSSL 11:cee25a834751 229 */
wolfSSL 11:cee25a834751 230
wolfSSL 11:cee25a834751 231
wolfSSL 11:cee25a834751 232 /* Each platform needs to query info type 1 from cpuid to see if aesni is
wolfSSL 11:cee25a834751 233 * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
wolfSSL 11:cee25a834751 234 */
wolfSSL 11:cee25a834751 235
wolfSSL 11:cee25a834751 236 #ifndef _MSC_VER
wolfSSL 11:cee25a834751 237 #define cpuid(reg, leaf, sub)\
wolfSSL 11:cee25a834751 238 __asm__ __volatile__ ("cpuid":\
wolfSSL 11:cee25a834751 239 "=a" (reg[0]), "=b" (reg[1]), "=c" (reg[2]), "=d" (reg[3]) :\
wolfSSL 11:cee25a834751 240 "a" (leaf), "c"(sub));
wolfSSL 11:cee25a834751 241
wolfSSL 11:cee25a834751 242 #define XASM_LINK(f) asm(f)
wolfSSL 11:cee25a834751 243 #else
wolfSSL 11:cee25a834751 244
wolfSSL 11:cee25a834751 245 #include <intrin.h>
wolfSSL 11:cee25a834751 246 #define cpuid(a,b) __cpuid((int*)a,b)
wolfSSL 11:cee25a834751 247
wolfSSL 11:cee25a834751 248 #define XASM_LINK(f)
wolfSSL 11:cee25a834751 249 #endif /* _MSC_VER */
wolfSSL 11:cee25a834751 250
wolfSSL 11:cee25a834751 251 #define EAX 0
wolfSSL 11:cee25a834751 252 #define EBX 1
wolfSSL 11:cee25a834751 253 #define ECX 2
wolfSSL 11:cee25a834751 254 #define EDX 3
wolfSSL 11:cee25a834751 255
wolfSSL 11:cee25a834751 256 #define CPUID_AVX1 0x1
wolfSSL 11:cee25a834751 257 #define CPUID_AVX2 0x2
wolfSSL 11:cee25a834751 258 #define CPUID_RDRAND 0x4
wolfSSL 11:cee25a834751 259 #define CPUID_RDSEED 0x8
wolfSSL 11:cee25a834751 260 #define CPUID_BMI2 0x10 /* MULX, RORX */
wolfSSL 11:cee25a834751 261
wolfSSL 11:cee25a834751 262 #define IS_INTEL_AVX1 (cpuid_flags & CPUID_AVX1)
wolfSSL 11:cee25a834751 263 #define IS_INTEL_AVX2 (cpuid_flags & CPUID_AVX2)
wolfSSL 11:cee25a834751 264 #define IS_INTEL_BMI2 (cpuid_flags & CPUID_BMI2)
wolfSSL 11:cee25a834751 265 #define IS_INTEL_RDRAND (cpuid_flags & CPUID_RDRAND)
wolfSSL 11:cee25a834751 266 #define IS_INTEL_RDSEED (cpuid_flags & CPUID_RDSEED)
wolfSSL 11:cee25a834751 267
wolfSSL 11:cee25a834751 268 static word32 cpuid_check = 0;
wolfSSL 11:cee25a834751 269 static word32 cpuid_flags = 0;
wolfSSL 11:cee25a834751 270
wolfSSL 11:cee25a834751 271 static word32 cpuid_flag(word32 leaf, word32 sub, word32 num, word32 bit) {
wolfSSL 11:cee25a834751 272 int got_intel_cpu = 0;
wolfSSL 11:cee25a834751 273 unsigned int reg[5];
wolfSSL 11:cee25a834751 274
wolfSSL 11:cee25a834751 275 reg[4] = '\0';
wolfSSL 11:cee25a834751 276 cpuid(reg, 0, 0);
wolfSSL 11:cee25a834751 277 if (XMEMCMP((char *)&(reg[EBX]), "Genu", 4) == 0 &&
wolfSSL 11:cee25a834751 278 XMEMCMP((char *)&(reg[EDX]), "ineI", 4) == 0 &&
wolfSSL 11:cee25a834751 279 XMEMCMP((char *)&(reg[ECX]), "ntel", 4) == 0) {
wolfSSL 11:cee25a834751 280 got_intel_cpu = 1;
wolfSSL 11:cee25a834751 281 }
wolfSSL 11:cee25a834751 282 if (got_intel_cpu) {
wolfSSL 11:cee25a834751 283 cpuid(reg, leaf, sub);
wolfSSL 11:cee25a834751 284 return ((reg[num] >> bit) & 0x1);
wolfSSL 11:cee25a834751 285 }
wolfSSL 11:cee25a834751 286 return 0;
wolfSSL 11:cee25a834751 287 }
wolfSSL 11:cee25a834751 288
wolfSSL 11:cee25a834751 289
wolfSSL 11:cee25a834751 290 static int set_cpuid_flags() {
wolfSSL 11:cee25a834751 291 if(cpuid_check ==0) {
wolfSSL 11:cee25a834751 292 if(cpuid_flag(1, 0, ECX, 28)){ cpuid_flags |= CPUID_AVX1 ;}
wolfSSL 11:cee25a834751 293 if(cpuid_flag(7, 0, EBX, 5)){ cpuid_flags |= CPUID_AVX2 ; }
wolfSSL 11:cee25a834751 294 if(cpuid_flag(7, 0, EBX, 8)) { cpuid_flags |= CPUID_BMI2 ; }
wolfSSL 11:cee25a834751 295 if(cpuid_flag(1, 0, ECX, 30)){ cpuid_flags |= CPUID_RDRAND ; }
wolfSSL 11:cee25a834751 296 if(cpuid_flag(7, 0, EBX, 18)){ cpuid_flags |= CPUID_RDSEED ; }
wolfSSL 11:cee25a834751 297 cpuid_check = 1 ;
wolfSSL 11:cee25a834751 298 return 0 ;
wolfSSL 11:cee25a834751 299 }
wolfSSL 11:cee25a834751 300 return 1 ;
wolfSSL 11:cee25a834751 301 }
wolfSSL 11:cee25a834751 302
wolfSSL 11:cee25a834751 303
wolfSSL 11:cee25a834751 304 #if defined(HAVE_INTEL_AVX1)
wolfSSL 11:cee25a834751 305 static int Transform_AVX1(Sha512 *sha512);
wolfSSL 11:cee25a834751 306 #endif
wolfSSL 11:cee25a834751 307 #if defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 308 static int Transform_AVX2(Sha512 *sha512);
wolfSSL 11:cee25a834751 309 #if defined(HAVE_INTEL_AVX1) && defined(HAVE_INTEL_AVX2) && defined(HAVE_INTEL_RORX)
wolfSSL 11:cee25a834751 310 static int Transform_AVX1_RORX(Sha512 *sha512);
wolfSSL 11:cee25a834751 311 #endif
wolfSSL 11:cee25a834751 312 #endif
wolfSSL 11:cee25a834751 313 static int _Transform(Sha512 *sha512);
wolfSSL 11:cee25a834751 314 static int (*Transform_p)(Sha512* sha512) = _Transform;
wolfSSL 11:cee25a834751 315 #define Transform(sha512) (*Transform_p)(sha512)
wolfSSL 11:cee25a834751 316
wolfSSL 11:cee25a834751 317 /* Dummy for saving MM_REGs on behalf of Transform */
wolfSSL 11:cee25a834751 318 /* #if defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 319 #define SAVE_XMM_YMM __asm__ volatile("orq %%r8, %%r8":::\
wolfSSL 11:cee25a834751 320 "%ymm0","%ymm1","%ymm2","%ymm3","%ymm4","%ymm5","%ymm6","%ymm7","%ymm8","%ymm9","%ymm10","%ymm11",\
wolfSSL 11:cee25a834751 321 "%ymm12","%ymm13","%ymm14","%ymm15")
wolfSSL 11:cee25a834751 322 */
wolfSSL 11:cee25a834751 323 #if defined(HAVE_INTEL_AVX1)
wolfSSL 11:cee25a834751 324 #define SAVE_XMM_YMM __asm__ volatile("orq %%r8, %%r8":::\
wolfSSL 11:cee25a834751 325 "xmm0","xmm1","xmm2","xmm3","xmm4","xmm5","xmm6","xmm7","xmm8","xmm9","xmm10","xmm11","xmm12","xmm13","xmm14","xmm15")
wolfSSL 11:cee25a834751 326 #endif
wolfSSL 11:cee25a834751 327
wolfSSL 11:cee25a834751 328
wolfSSL 11:cee25a834751 329 int wc_InitSha512_ex(Sha512* sha512, void* heap, int devId)
wolfSSL 11:cee25a834751 330 {
wolfSSL 11:cee25a834751 331 int ret = InitSha512(sha512);
wolfSSL 11:cee25a834751 332
wolfSSL 11:cee25a834751 333 (void)heap;
wolfSSL 11:cee25a834751 334 (void)devId;
wolfSSL 11:cee25a834751 335
wolfSSL 11:cee25a834751 336 if (set_cpuid_flags())
wolfSSL 11:cee25a834751 337 return ret;
wolfSSL 11:cee25a834751 338
wolfSSL 11:cee25a834751 339 #if defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 340 if (IS_INTEL_AVX2 && IS_INTEL_BMI2) {
wolfSSL 11:cee25a834751 341 Transform_p = Transform_AVX1_RORX; return ret;
wolfSSL 11:cee25a834751 342 Transform_p = Transform_AVX2;
wolfSSL 11:cee25a834751 343 /* for avoiding warning,"not used" */
wolfSSL 11:cee25a834751 344 }
wolfSSL 11:cee25a834751 345 #endif
wolfSSL 11:cee25a834751 346 #if defined(HAVE_INTEL_AVX1)
wolfSSL 11:cee25a834751 347 Transform_p = ((IS_INTEL_AVX1) ? Transform_AVX1 : _Transform); return ret;
wolfSSL 11:cee25a834751 348 #endif
wolfSSL 11:cee25a834751 349 Transform_p = _Transform;
wolfSSL 11:cee25a834751 350
wolfSSL 11:cee25a834751 351 return ret;
wolfSSL 11:cee25a834751 352 }
wolfSSL 11:cee25a834751 353
wolfSSL 11:cee25a834751 354 #else
wolfSSL 11:cee25a834751 355 #define Transform(sha512) _Transform(sha512)
wolfSSL 11:cee25a834751 356
wolfSSL 11:cee25a834751 357 int wc_InitSha512_ex(Sha512* sha512, void* heap, int devId)
wolfSSL 11:cee25a834751 358 {
wolfSSL 11:cee25a834751 359 int ret = 0;
wolfSSL 11:cee25a834751 360
wolfSSL 11:cee25a834751 361 if (sha512 == NULL)
wolfSSL 11:cee25a834751 362 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 363
wolfSSL 11:cee25a834751 364 sha512->heap = heap;
wolfSSL 11:cee25a834751 365
wolfSSL 11:cee25a834751 366 ret = InitSha512(sha512);
wolfSSL 11:cee25a834751 367 if (ret != 0)
wolfSSL 11:cee25a834751 368 return ret;
wolfSSL 11:cee25a834751 369
wolfSSL 11:cee25a834751 370 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
wolfSSL 11:cee25a834751 371 ret = wolfAsync_DevCtxInit(&sha512->asyncDev,
wolfSSL 11:cee25a834751 372 WOLFSSL_ASYNC_MARKER_SHA512, sha512->heap, devId);
wolfSSL 11:cee25a834751 373 #else
wolfSSL 11:cee25a834751 374 (void)devId;
wolfSSL 11:cee25a834751 375 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 376
wolfSSL 11:cee25a834751 377 return ret;
wolfSSL 11:cee25a834751 378 }
wolfSSL 11:cee25a834751 379
wolfSSL 11:cee25a834751 380 #endif /* Hardware Acceleration */
wolfSSL 11:cee25a834751 381
wolfSSL 11:cee25a834751 382 #ifndef SAVE_XMM_YMM
wolfSSL 11:cee25a834751 383 #define SAVE_XMM_YMM
wolfSSL 11:cee25a834751 384 #endif
wolfSSL 11:cee25a834751 385
wolfSSL 11:cee25a834751 386 static const word64 K512[80] = {
wolfSSL 11:cee25a834751 387 W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd),
wolfSSL 11:cee25a834751 388 W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc),
wolfSSL 11:cee25a834751 389 W64LIT(0x3956c25bf348b538), W64LIT(0x59f111f1b605d019),
wolfSSL 11:cee25a834751 390 W64LIT(0x923f82a4af194f9b), W64LIT(0xab1c5ed5da6d8118),
wolfSSL 11:cee25a834751 391 W64LIT(0xd807aa98a3030242), W64LIT(0x12835b0145706fbe),
wolfSSL 11:cee25a834751 392 W64LIT(0x243185be4ee4b28c), W64LIT(0x550c7dc3d5ffb4e2),
wolfSSL 11:cee25a834751 393 W64LIT(0x72be5d74f27b896f), W64LIT(0x80deb1fe3b1696b1),
wolfSSL 11:cee25a834751 394 W64LIT(0x9bdc06a725c71235), W64LIT(0xc19bf174cf692694),
wolfSSL 11:cee25a834751 395 W64LIT(0xe49b69c19ef14ad2), W64LIT(0xefbe4786384f25e3),
wolfSSL 11:cee25a834751 396 W64LIT(0x0fc19dc68b8cd5b5), W64LIT(0x240ca1cc77ac9c65),
wolfSSL 11:cee25a834751 397 W64LIT(0x2de92c6f592b0275), W64LIT(0x4a7484aa6ea6e483),
wolfSSL 11:cee25a834751 398 W64LIT(0x5cb0a9dcbd41fbd4), W64LIT(0x76f988da831153b5),
wolfSSL 11:cee25a834751 399 W64LIT(0x983e5152ee66dfab), W64LIT(0xa831c66d2db43210),
wolfSSL 11:cee25a834751 400 W64LIT(0xb00327c898fb213f), W64LIT(0xbf597fc7beef0ee4),
wolfSSL 11:cee25a834751 401 W64LIT(0xc6e00bf33da88fc2), W64LIT(0xd5a79147930aa725),
wolfSSL 11:cee25a834751 402 W64LIT(0x06ca6351e003826f), W64LIT(0x142929670a0e6e70),
wolfSSL 11:cee25a834751 403 W64LIT(0x27b70a8546d22ffc), W64LIT(0x2e1b21385c26c926),
wolfSSL 11:cee25a834751 404 W64LIT(0x4d2c6dfc5ac42aed), W64LIT(0x53380d139d95b3df),
wolfSSL 11:cee25a834751 405 W64LIT(0x650a73548baf63de), W64LIT(0x766a0abb3c77b2a8),
wolfSSL 11:cee25a834751 406 W64LIT(0x81c2c92e47edaee6), W64LIT(0x92722c851482353b),
wolfSSL 11:cee25a834751 407 W64LIT(0xa2bfe8a14cf10364), W64LIT(0xa81a664bbc423001),
wolfSSL 11:cee25a834751 408 W64LIT(0xc24b8b70d0f89791), W64LIT(0xc76c51a30654be30),
wolfSSL 11:cee25a834751 409 W64LIT(0xd192e819d6ef5218), W64LIT(0xd69906245565a910),
wolfSSL 11:cee25a834751 410 W64LIT(0xf40e35855771202a), W64LIT(0x106aa07032bbd1b8),
wolfSSL 11:cee25a834751 411 W64LIT(0x19a4c116b8d2d0c8), W64LIT(0x1e376c085141ab53),
wolfSSL 11:cee25a834751 412 W64LIT(0x2748774cdf8eeb99), W64LIT(0x34b0bcb5e19b48a8),
wolfSSL 11:cee25a834751 413 W64LIT(0x391c0cb3c5c95a63), W64LIT(0x4ed8aa4ae3418acb),
wolfSSL 11:cee25a834751 414 W64LIT(0x5b9cca4f7763e373), W64LIT(0x682e6ff3d6b2b8a3),
wolfSSL 11:cee25a834751 415 W64LIT(0x748f82ee5defb2fc), W64LIT(0x78a5636f43172f60),
wolfSSL 11:cee25a834751 416 W64LIT(0x84c87814a1f0ab72), W64LIT(0x8cc702081a6439ec),
wolfSSL 11:cee25a834751 417 W64LIT(0x90befffa23631e28), W64LIT(0xa4506cebde82bde9),
wolfSSL 11:cee25a834751 418 W64LIT(0xbef9a3f7b2c67915), W64LIT(0xc67178f2e372532b),
wolfSSL 11:cee25a834751 419 W64LIT(0xca273eceea26619c), W64LIT(0xd186b8c721c0c207),
wolfSSL 11:cee25a834751 420 W64LIT(0xeada7dd6cde0eb1e), W64LIT(0xf57d4f7fee6ed178),
wolfSSL 11:cee25a834751 421 W64LIT(0x06f067aa72176fba), W64LIT(0x0a637dc5a2c898a6),
wolfSSL 11:cee25a834751 422 W64LIT(0x113f9804bef90dae), W64LIT(0x1b710b35131c471b),
wolfSSL 11:cee25a834751 423 W64LIT(0x28db77f523047d84), W64LIT(0x32caab7b40c72493),
wolfSSL 11:cee25a834751 424 W64LIT(0x3c9ebe0a15c9bebc), W64LIT(0x431d67c49c100d4c),
wolfSSL 11:cee25a834751 425 W64LIT(0x4cc5d4becb3e42b6), W64LIT(0x597f299cfc657e2a),
wolfSSL 11:cee25a834751 426 W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817)
wolfSSL 11:cee25a834751 427 };
wolfSSL 11:cee25a834751 428
wolfSSL 11:cee25a834751 429
wolfSSL 11:cee25a834751 430
wolfSSL 11:cee25a834751 431 #define blk0(i) (W[i] = sha512->buffer[i])
wolfSSL 11:cee25a834751 432
wolfSSL 11:cee25a834751 433 #define blk2(i) (W[i&15]+=s1(W[(i-2)&15])+W[(i-7)&15]+s0(W[(i-15)&15]))
wolfSSL 11:cee25a834751 434
wolfSSL 11:cee25a834751 435 #define Ch(x,y,z) (z^(x&(y^z)))
wolfSSL 11:cee25a834751 436 #define Maj(x,y,z) ((x&y)|(z&(x|y)))
wolfSSL 11:cee25a834751 437
wolfSSL 11:cee25a834751 438 #define a(i) T[(0-i)&7]
wolfSSL 11:cee25a834751 439 #define b(i) T[(1-i)&7]
wolfSSL 11:cee25a834751 440 #define c(i) T[(2-i)&7]
wolfSSL 11:cee25a834751 441 #define d(i) T[(3-i)&7]
wolfSSL 11:cee25a834751 442 #define e(i) T[(4-i)&7]
wolfSSL 11:cee25a834751 443 #define f(i) T[(5-i)&7]
wolfSSL 11:cee25a834751 444 #define g(i) T[(6-i)&7]
wolfSSL 11:cee25a834751 445 #define h(i) T[(7-i)&7]
wolfSSL 11:cee25a834751 446
wolfSSL 11:cee25a834751 447 #define S0(x) (rotrFixed64(x,28)^rotrFixed64(x,34)^rotrFixed64(x,39))
wolfSSL 11:cee25a834751 448 #define S1(x) (rotrFixed64(x,14)^rotrFixed64(x,18)^rotrFixed64(x,41))
wolfSSL 11:cee25a834751 449 #define s0(x) (rotrFixed64(x,1)^rotrFixed64(x,8)^(x>>7))
wolfSSL 11:cee25a834751 450 #define s1(x) (rotrFixed64(x,19)^rotrFixed64(x,61)^(x>>6))
wolfSSL 11:cee25a834751 451
wolfSSL 11:cee25a834751 452 #define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j]+(j?blk2(i):blk0(i));\
wolfSSL 11:cee25a834751 453 d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
wolfSSL 11:cee25a834751 454
wolfSSL 11:cee25a834751 455 static int _Transform(Sha512* sha512)
wolfSSL 11:cee25a834751 456 {
wolfSSL 11:cee25a834751 457 const word64* K = K512;
wolfSSL 11:cee25a834751 458
wolfSSL 11:cee25a834751 459 word32 j;
wolfSSL 11:cee25a834751 460 word64 T[8];
wolfSSL 11:cee25a834751 461
wolfSSL 11:cee25a834751 462
wolfSSL 11:cee25a834751 463 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 464 word64* W;
wolfSSL 11:cee25a834751 465 W = (word64*) XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 466 if (W == NULL)
wolfSSL 11:cee25a834751 467 return MEMORY_E;
wolfSSL 11:cee25a834751 468 #else
wolfSSL 11:cee25a834751 469 word64 W[16];
wolfSSL 11:cee25a834751 470 #endif
wolfSSL 11:cee25a834751 471
wolfSSL 11:cee25a834751 472 /* Copy digest to working vars */
wolfSSL 11:cee25a834751 473 XMEMCPY(T, sha512->digest, sizeof(T));
wolfSSL 11:cee25a834751 474
wolfSSL 11:cee25a834751 475 #ifdef USE_SLOW_SHA2
wolfSSL 11:cee25a834751 476 /* over twice as small, but 50% slower */
wolfSSL 11:cee25a834751 477 /* 80 operations, not unrolled */
wolfSSL 11:cee25a834751 478 for (j = 0; j < 80; j += 16) {
wolfSSL 11:cee25a834751 479 int m;
wolfSSL 11:cee25a834751 480 for (m = 0; m < 16; m++) { /* braces needed here for macros {} */
wolfSSL 11:cee25a834751 481 R(m);
wolfSSL 11:cee25a834751 482 }
wolfSSL 11:cee25a834751 483 }
wolfSSL 11:cee25a834751 484 #else
wolfSSL 11:cee25a834751 485 /* 80 operations, partially loop unrolled */
wolfSSL 11:cee25a834751 486 for (j = 0; j < 80; j += 16) {
wolfSSL 11:cee25a834751 487 R( 0); R( 1); R( 2); R( 3);
wolfSSL 11:cee25a834751 488 R( 4); R( 5); R( 6); R( 7);
wolfSSL 11:cee25a834751 489 R( 8); R( 9); R(10); R(11);
wolfSSL 11:cee25a834751 490 R(12); R(13); R(14); R(15);
wolfSSL 11:cee25a834751 491 }
wolfSSL 11:cee25a834751 492 #endif /* USE_SLOW_SHA2 */
wolfSSL 11:cee25a834751 493
wolfSSL 11:cee25a834751 494 /* Add the working vars back into digest */
wolfSSL 11:cee25a834751 495
wolfSSL 11:cee25a834751 496 sha512->digest[0] += a(0);
wolfSSL 11:cee25a834751 497 sha512->digest[1] += b(0);
wolfSSL 11:cee25a834751 498 sha512->digest[2] += c(0);
wolfSSL 11:cee25a834751 499 sha512->digest[3] += d(0);
wolfSSL 11:cee25a834751 500 sha512->digest[4] += e(0);
wolfSSL 11:cee25a834751 501 sha512->digest[5] += f(0);
wolfSSL 11:cee25a834751 502 sha512->digest[6] += g(0);
wolfSSL 11:cee25a834751 503 sha512->digest[7] += h(0);
wolfSSL 11:cee25a834751 504
wolfSSL 11:cee25a834751 505 /* Wipe variables */
wolfSSL 11:cee25a834751 506 ForceZero(W, sizeof(word64) * 16);
wolfSSL 11:cee25a834751 507 ForceZero(T, sizeof(T));
wolfSSL 11:cee25a834751 508
wolfSSL 11:cee25a834751 509 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 510 XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 511 #endif
wolfSSL 11:cee25a834751 512
wolfSSL 11:cee25a834751 513 return 0;
wolfSSL 11:cee25a834751 514 }
wolfSSL 11:cee25a834751 515
wolfSSL 11:cee25a834751 516
wolfSSL 11:cee25a834751 517 static INLINE void AddLength(Sha512* sha512, word32 len)
wolfSSL 11:cee25a834751 518 {
wolfSSL 11:cee25a834751 519 word64 tmp = sha512->loLen;
wolfSSL 11:cee25a834751 520 if ( (sha512->loLen += len) < tmp)
wolfSSL 11:cee25a834751 521 sha512->hiLen++; /* carry low to high */
wolfSSL 11:cee25a834751 522 }
wolfSSL 11:cee25a834751 523
wolfSSL 11:cee25a834751 524 static INLINE int Sha512Update(Sha512* sha512, const byte* data, word32 len)
wolfSSL 11:cee25a834751 525 {
wolfSSL 11:cee25a834751 526 int ret = 0;
wolfSSL 11:cee25a834751 527
wolfSSL 11:cee25a834751 528 /* do block size increments */
wolfSSL 11:cee25a834751 529 byte* local = (byte*)sha512->buffer;
wolfSSL 11:cee25a834751 530
wolfSSL 11:cee25a834751 531 /* check that internal buffLen is valid */
wolfSSL 11:cee25a834751 532 if (sha512->buffLen >= SHA512_BLOCK_SIZE)
wolfSSL 11:cee25a834751 533 return BUFFER_E;
wolfSSL 11:cee25a834751 534
wolfSSL 11:cee25a834751 535 SAVE_XMM_YMM; /* for Intel AVX */
wolfSSL 11:cee25a834751 536
wolfSSL 11:cee25a834751 537 while (len) {
wolfSSL 11:cee25a834751 538 word32 add = min(len, SHA512_BLOCK_SIZE - sha512->buffLen);
wolfSSL 11:cee25a834751 539 XMEMCPY(&local[sha512->buffLen], data, add);
wolfSSL 11:cee25a834751 540
wolfSSL 11:cee25a834751 541 sha512->buffLen += add;
wolfSSL 11:cee25a834751 542 data += add;
wolfSSL 11:cee25a834751 543 len -= add;
wolfSSL 11:cee25a834751 544
wolfSSL 11:cee25a834751 545 if (sha512->buffLen == SHA512_BLOCK_SIZE) {
wolfSSL 11:cee25a834751 546 #if defined(LITTLE_ENDIAN_ORDER)
wolfSSL 11:cee25a834751 547 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 548 if(!IS_INTEL_AVX1 && !IS_INTEL_AVX2)
wolfSSL 11:cee25a834751 549 #endif
wolfSSL 11:cee25a834751 550 ByteReverseWords64(sha512->buffer, sha512->buffer,
wolfSSL 11:cee25a834751 551 SHA512_BLOCK_SIZE);
wolfSSL 11:cee25a834751 552 #endif
wolfSSL 11:cee25a834751 553 ret = Transform(sha512);
wolfSSL 11:cee25a834751 554 if (ret != 0)
wolfSSL 11:cee25a834751 555 break;
wolfSSL 11:cee25a834751 556
wolfSSL 11:cee25a834751 557 AddLength(sha512, SHA512_BLOCK_SIZE);
wolfSSL 11:cee25a834751 558 sha512->buffLen = 0;
wolfSSL 11:cee25a834751 559 }
wolfSSL 11:cee25a834751 560 }
wolfSSL 11:cee25a834751 561
wolfSSL 11:cee25a834751 562 return ret;
wolfSSL 11:cee25a834751 563 }
wolfSSL 11:cee25a834751 564
wolfSSL 11:cee25a834751 565 int wc_Sha512Update(Sha512* sha512, const byte* data, word32 len)
wolfSSL 11:cee25a834751 566 {
wolfSSL 11:cee25a834751 567 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
wolfSSL 11:cee25a834751 568 if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
wolfSSL 11:cee25a834751 569 #if defined(HAVE_INTEL_QA)
wolfSSL 11:cee25a834751 570 return IntelQaSymSha512(&sha512->asyncDev, NULL, data, len);
wolfSSL 11:cee25a834751 571 #endif
wolfSSL 11:cee25a834751 572 }
wolfSSL 11:cee25a834751 573 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 574
wolfSSL 11:cee25a834751 575 return Sha512Update(sha512, data, len);
wolfSSL 11:cee25a834751 576 }
wolfSSL 11:cee25a834751 577
wolfSSL 11:cee25a834751 578
wolfSSL 11:cee25a834751 579 static INLINE int Sha512Final(Sha512* sha512)
wolfSSL 11:cee25a834751 580 {
wolfSSL 11:cee25a834751 581 byte* local = (byte*)sha512->buffer;
wolfSSL 11:cee25a834751 582 int ret;
wolfSSL 11:cee25a834751 583
wolfSSL 11:cee25a834751 584 SAVE_XMM_YMM ; /* for Intel AVX */
wolfSSL 11:cee25a834751 585 AddLength(sha512, sha512->buffLen); /* before adding pads */
wolfSSL 11:cee25a834751 586
wolfSSL 11:cee25a834751 587 local[sha512->buffLen++] = 0x80; /* add 1 */
wolfSSL 11:cee25a834751 588
wolfSSL 11:cee25a834751 589 /* pad with zeros */
wolfSSL 11:cee25a834751 590 if (sha512->buffLen > SHA512_PAD_SIZE) {
wolfSSL 11:cee25a834751 591 XMEMSET(&local[sha512->buffLen], 0, SHA512_BLOCK_SIZE - sha512->buffLen);
wolfSSL 11:cee25a834751 592 sha512->buffLen += SHA512_BLOCK_SIZE - sha512->buffLen;
wolfSSL 11:cee25a834751 593 #if defined(LITTLE_ENDIAN_ORDER)
wolfSSL 11:cee25a834751 594 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 595 if (!IS_INTEL_AVX1 && !IS_INTEL_AVX2)
wolfSSL 11:cee25a834751 596 #endif
wolfSSL 11:cee25a834751 597 ByteReverseWords64(sha512->buffer,sha512->buffer,SHA512_BLOCK_SIZE);
wolfSSL 11:cee25a834751 598
wolfSSL 11:cee25a834751 599 #endif /* LITTLE_ENDIAN_ORDER */
wolfSSL 11:cee25a834751 600 ret = Transform(sha512);
wolfSSL 11:cee25a834751 601 if (ret != 0)
wolfSSL 11:cee25a834751 602 return ret;
wolfSSL 11:cee25a834751 603
wolfSSL 11:cee25a834751 604 sha512->buffLen = 0;
wolfSSL 11:cee25a834751 605 }
wolfSSL 11:cee25a834751 606 XMEMSET(&local[sha512->buffLen], 0, SHA512_PAD_SIZE - sha512->buffLen);
wolfSSL 11:cee25a834751 607
wolfSSL 11:cee25a834751 608 /* put lengths in bits */
wolfSSL 11:cee25a834751 609 sha512->hiLen = (sha512->loLen >> (8 * sizeof(sha512->loLen) - 3)) +
wolfSSL 11:cee25a834751 610 (sha512->hiLen << 3);
wolfSSL 11:cee25a834751 611 sha512->loLen = sha512->loLen << 3;
wolfSSL 11:cee25a834751 612
wolfSSL 11:cee25a834751 613 /* store lengths */
wolfSSL 11:cee25a834751 614 #if defined(LITTLE_ENDIAN_ORDER)
wolfSSL 11:cee25a834751 615 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 616 if (!IS_INTEL_AVX1 && !IS_INTEL_AVX2)
wolfSSL 11:cee25a834751 617 #endif
wolfSSL 11:cee25a834751 618 ByteReverseWords64(sha512->buffer, sha512->buffer, SHA512_PAD_SIZE);
wolfSSL 11:cee25a834751 619 #endif
wolfSSL 11:cee25a834751 620 /* ! length ordering dependent on digest endian type ! */
wolfSSL 11:cee25a834751 621
wolfSSL 11:cee25a834751 622 sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
wolfSSL 11:cee25a834751 623 sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
wolfSSL 11:cee25a834751 624 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 625 if (IS_INTEL_AVX1 || IS_INTEL_AVX2)
wolfSSL 11:cee25a834751 626 ByteReverseWords64(&(sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
wolfSSL 11:cee25a834751 627 &(sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
wolfSSL 11:cee25a834751 628 SHA512_BLOCK_SIZE - SHA512_PAD_SIZE);
wolfSSL 11:cee25a834751 629 #endif
wolfSSL 11:cee25a834751 630 ret = Transform(sha512);
wolfSSL 11:cee25a834751 631 if (ret != 0)
wolfSSL 11:cee25a834751 632 return ret;
wolfSSL 11:cee25a834751 633
wolfSSL 11:cee25a834751 634 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 635 ByteReverseWords64(sha512->digest, sha512->digest, SHA512_DIGEST_SIZE);
wolfSSL 11:cee25a834751 636 #endif
wolfSSL 11:cee25a834751 637
wolfSSL 11:cee25a834751 638 return 0;
wolfSSL 11:cee25a834751 639 }
wolfSSL 11:cee25a834751 640
wolfSSL 11:cee25a834751 641 int wc_Sha512Final(Sha512* sha512, byte* hash)
wolfSSL 11:cee25a834751 642 {
wolfSSL 11:cee25a834751 643 int ret;
wolfSSL 11:cee25a834751 644
wolfSSL 11:cee25a834751 645 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
wolfSSL 11:cee25a834751 646 if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
wolfSSL 11:cee25a834751 647 #if defined(HAVE_INTEL_QA)
wolfSSL 11:cee25a834751 648 return IntelQaSymSha512(&sha512->asyncDev, hash, NULL,
wolfSSL 11:cee25a834751 649 SHA512_DIGEST_SIZE);
wolfSSL 11:cee25a834751 650 #endif
wolfSSL 11:cee25a834751 651 }
wolfSSL 11:cee25a834751 652 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 653
wolfSSL 11:cee25a834751 654 ret = Sha512Final(sha512);
wolfSSL 11:cee25a834751 655 if (ret != 0)
wolfSSL 11:cee25a834751 656 return ret;
wolfSSL 11:cee25a834751 657
wolfSSL 11:cee25a834751 658 XMEMCPY(hash, sha512->digest, SHA512_DIGEST_SIZE);
wolfSSL 11:cee25a834751 659
wolfSSL 11:cee25a834751 660 return InitSha512(sha512); /* reset state */
wolfSSL 11:cee25a834751 661 }
wolfSSL 11:cee25a834751 662
wolfSSL 11:cee25a834751 663
wolfSSL 11:cee25a834751 664 int wc_InitSha512(Sha512* sha512)
wolfSSL 11:cee25a834751 665 {
wolfSSL 11:cee25a834751 666 return wc_InitSha512_ex(sha512, NULL, INVALID_DEVID);
wolfSSL 11:cee25a834751 667 }
wolfSSL 11:cee25a834751 668
wolfSSL 11:cee25a834751 669 void wc_Sha512Free(Sha512* sha512)
wolfSSL 11:cee25a834751 670 {
wolfSSL 11:cee25a834751 671 if (sha512 == NULL)
wolfSSL 11:cee25a834751 672 return;
wolfSSL 11:cee25a834751 673
wolfSSL 11:cee25a834751 674 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
wolfSSL 11:cee25a834751 675 wolfAsync_DevCtxFree(&sha512->asyncDev, WOLFSSL_ASYNC_MARKER_SHA512);
wolfSSL 11:cee25a834751 676 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 677 }
wolfSSL 11:cee25a834751 678
wolfSSL 11:cee25a834751 679
wolfSSL 11:cee25a834751 680 #if defined(HAVE_INTEL_AVX1)
wolfSSL 11:cee25a834751 681
wolfSSL 11:cee25a834751 682 #define Rx_1(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j] + W_X[i];
wolfSSL 11:cee25a834751 683 #define Rx_2(i) d(i)+=h(i);
wolfSSL 11:cee25a834751 684 #define Rx_3(i) h(i)+=S0(a(i))+Maj(a(i),b(i),c(i));
wolfSSL 11:cee25a834751 685
wolfSSL 11:cee25a834751 686 #if defined(HAVE_INTEL_RORX)
wolfSSL 11:cee25a834751 687
wolfSSL 11:cee25a834751 688 #define Rx_RORX_1(i) h(i)+=S1_RORX(e(i))+Ch(e(i),f(i),g(i))+K[i+j] + W_X[i];
wolfSSL 11:cee25a834751 689 #define Rx_RORX_2(i) d(i)+=h(i);
wolfSSL 11:cee25a834751 690 #define Rx_RORX_3(i) h(i)+=S0_RORX(a(i))+Maj(a(i),b(i),c(i));
wolfSSL 11:cee25a834751 691 #endif /* HAVE_INTEL_RORX */
wolfSSL 11:cee25a834751 692
wolfSSL 11:cee25a834751 693 #endif /* HAVE_INTEL_AVX1 */
wolfSSL 11:cee25a834751 694
wolfSSL 11:cee25a834751 695 #if defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 696 #define Ry_1(i, w) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j] + w;
wolfSSL 11:cee25a834751 697 #define Ry_2(i, w) d(i)+=h(i);
wolfSSL 11:cee25a834751 698 #define Ry_3(i, w) h(i)+=S0(a(i))+Maj(a(i),b(i),c(i));
wolfSSL 11:cee25a834751 699 #endif /* HAVE_INTEL_AVX2 */
wolfSSL 11:cee25a834751 700
wolfSSL 11:cee25a834751 701 /* INLINE Assember for Intel AVX1 instructions */
wolfSSL 11:cee25a834751 702 #if defined(HAVE_INTEL_AVX1)
wolfSSL 11:cee25a834751 703 #if defined(DEBUG_XMM)
wolfSSL 11:cee25a834751 704 #define SAVE_REG(i) __asm__ volatile("vmovdqu %%xmm"#i", %0 \n\t":"=m"(reg[i][0])::XMM_REGs);
wolfSSL 11:cee25a834751 705 #define RECV_REG(i) __asm__ volatile("vmovdqu %0, %%xmm"#i" \n\t"::"m"(reg[i][0]):XMM_REGs);
wolfSSL 11:cee25a834751 706
wolfSSL 11:cee25a834751 707 #define _DUMP_REG(REG, name)\
wolfSSL 11:cee25a834751 708 { word64 buf[16];word64 reg[16][2];int k;\
wolfSSL 11:cee25a834751 709 SAVE_REG(0); SAVE_REG(1); SAVE_REG(2); SAVE_REG(3); SAVE_REG(4); \
wolfSSL 11:cee25a834751 710 SAVE_REG(5); SAVE_REG(6); SAVE_REG(7);SAVE_REG(8); SAVE_REG(9); SAVE_REG(10);\
wolfSSL 11:cee25a834751 711 SAVE_REG(11); SAVE_REG(12); SAVE_REG(13); SAVE_REG(14); SAVE_REG(15); \
wolfSSL 11:cee25a834751 712 __asm__ volatile("vmovdqu %%"#REG", %0 \n\t":"=m"(buf[0])::XMM_REGs);\
wolfSSL 11:cee25a834751 713 printf(" "#name":\t"); for(k=0; k<2; k++) printf("%016lx.", (word64)(buf[k])); printf("\n"); \
wolfSSL 11:cee25a834751 714 RECV_REG(0); RECV_REG(1); RECV_REG(2); RECV_REG(3); RECV_REG(4);\
wolfSSL 11:cee25a834751 715 RECV_REG(5); RECV_REG(6); RECV_REG(7); RECV_REG(8); RECV_REG(9);\
wolfSSL 11:cee25a834751 716 RECV_REG(10); RECV_REG(11); RECV_REG(12); RECV_REG(13); RECV_REG(14); RECV_REG(15);\
wolfSSL 11:cee25a834751 717 }
wolfSSL 11:cee25a834751 718
wolfSSL 11:cee25a834751 719 #define DUMP_REG(REG) _DUMP_REG(REG, #REG)
wolfSSL 11:cee25a834751 720 #define PRINTF(fmt, ...)
wolfSSL 11:cee25a834751 721 #else
wolfSSL 11:cee25a834751 722 #define DUMP_REG(REG)
wolfSSL 11:cee25a834751 723 #define PRINTF(fmt, ...)
wolfSSL 11:cee25a834751 724 #endif /* DEBUG_XMM */
wolfSSL 11:cee25a834751 725
wolfSSL 11:cee25a834751 726 #define _MOVE_to_REG(xymm, mem) __asm__ volatile("vmovdqu %0, %%"#xymm" "\
wolfSSL 11:cee25a834751 727 :: "m"(mem):XMM_REGs);
wolfSSL 11:cee25a834751 728 #define _MOVE_to_MEM(mem,i, xymm) __asm__ volatile("vmovdqu %%"#xymm", %0" :\
wolfSSL 11:cee25a834751 729 "=m"(mem[i]),"=m"(mem[i+1]),"=m"(mem[i+2]),"=m"(mem[i+3])::XMM_REGs);
wolfSSL 11:cee25a834751 730 #define _MOVE(dest, src) __asm__ volatile("vmovdqu %%"#src", %%"\
wolfSSL 11:cee25a834751 731 #dest" ":::XMM_REGs);
wolfSSL 11:cee25a834751 732
wolfSSL 11:cee25a834751 733 #define _S_TEMP(dest, src, bits, temp) __asm__ volatile("vpsrlq $"#bits", %%"\
wolfSSL 11:cee25a834751 734 #src", %%"#dest"\n\tvpsllq $64-"#bits", %%"#src", %%"#temp"\n\tvpor %%"\
wolfSSL 11:cee25a834751 735 #temp",%%"#dest", %%"#dest" ":::XMM_REGs);
wolfSSL 11:cee25a834751 736 #define _AVX1_R(dest, src, bits) __asm__ volatile("vpsrlq $"#bits", %%"\
wolfSSL 11:cee25a834751 737 #src", %%"#dest" ":::XMM_REGs);
wolfSSL 11:cee25a834751 738 #define _XOR(dest, src1, src2) __asm__ volatile("vpxor %%"#src1", %%"\
wolfSSL 11:cee25a834751 739 #src2", %%"#dest" ":::XMM_REGs);
wolfSSL 11:cee25a834751 740 #define _OR(dest, src1, src2) __asm__ volatile("vpor %%"#src1", %%"\
wolfSSL 11:cee25a834751 741 #src2", %%"#dest" ":::XMM_REGs);
wolfSSL 11:cee25a834751 742 #define _ADD(dest, src1, src2) __asm__ volatile("vpaddq %%"#src1", %%"\
wolfSSL 11:cee25a834751 743 #src2", %%"#dest" ":::XMM_REGs);
wolfSSL 11:cee25a834751 744 #define _ADD_MEM(dest, src1, mem) __asm__ volatile("vpaddq %0, %%"#src1", %%"\
wolfSSL 11:cee25a834751 745 #dest" "::"m"(mem):XMM_REGs);
wolfSSL 11:cee25a834751 746
wolfSSL 11:cee25a834751 747 #define MOVE_to_REG(xymm, mem) _MOVE_to_REG(xymm, mem)
wolfSSL 11:cee25a834751 748 #define MOVE_to_MEM(mem, i, xymm) _MOVE_to_MEM(mem, i, xymm)
wolfSSL 11:cee25a834751 749 #define MOVE(dest, src) _MOVE(dest, src)
wolfSSL 11:cee25a834751 750
wolfSSL 11:cee25a834751 751 #define XOR(dest, src1, src2) _XOR(dest, src1, src2)
wolfSSL 11:cee25a834751 752 #define OR(dest, src1, src2) _OR(dest, src1, src2)
wolfSSL 11:cee25a834751 753 #define ADD(dest, src1, src2) _ADD(dest, src1, src2)
wolfSSL 11:cee25a834751 754
wolfSSL 11:cee25a834751 755 #define S_TMP(dest, src, bits, temp) _S_TEMP(dest, src, bits, temp);
wolfSSL 11:cee25a834751 756 #define AVX1_S(dest, src, bits) S_TMP(dest, src, bits, S_TEMP)
wolfSSL 11:cee25a834751 757 #define AVX1_R(dest, src, bits) _AVX1_R(dest, src, bits)
wolfSSL 11:cee25a834751 758
wolfSSL 11:cee25a834751 759 #define Init_Mask(mask) \
wolfSSL 11:cee25a834751 760 __asm__ volatile("vmovdqu %0, %%xmm1\n\t"::"m"(mask):"%xmm1");
wolfSSL 11:cee25a834751 761
wolfSSL 11:cee25a834751 762 #define _W_from_buff1(w, buff, xmm) \
wolfSSL 11:cee25a834751 763 /* X0..3(xmm4..7), W[0..15] = sha512->buffer[0.15]; */\
wolfSSL 11:cee25a834751 764 __asm__ volatile("vmovdqu %1, %%"#xmm"\n\t"\
wolfSSL 11:cee25a834751 765 "vpshufb %%xmm1, %%"#xmm", %%"#xmm"\n\t"\
wolfSSL 11:cee25a834751 766 "vmovdqu %%"#xmm", %0"\
wolfSSL 11:cee25a834751 767 :"=m"(w): "m"(buff):"%xmm0");
wolfSSL 11:cee25a834751 768
wolfSSL 11:cee25a834751 769 #define W_from_buff1(w, buff, xmm) _W_from_buff1(w, buff, xmm)
wolfSSL 11:cee25a834751 770
wolfSSL 11:cee25a834751 771 #define W_from_buff(w, buff)\
wolfSSL 11:cee25a834751 772 Init_Mask(mBYTE_FLIP_MASK[0]);\
wolfSSL 11:cee25a834751 773 W_from_buff1(w[0], buff[0], W_0);\
wolfSSL 11:cee25a834751 774 W_from_buff1(w[2], buff[2], W_2);\
wolfSSL 11:cee25a834751 775 W_from_buff1(w[4], buff[4], W_4);\
wolfSSL 11:cee25a834751 776 W_from_buff1(w[6], buff[6], W_6);\
wolfSSL 11:cee25a834751 777 W_from_buff1(w[8], buff[8], W_8);\
wolfSSL 11:cee25a834751 778 W_from_buff1(w[10],buff[10],W_10);\
wolfSSL 11:cee25a834751 779 W_from_buff1(w[12],buff[12],W_12);\
wolfSSL 11:cee25a834751 780 W_from_buff1(w[14],buff[14],W_14);
wolfSSL 11:cee25a834751 781
wolfSSL 11:cee25a834751 782 static word64 mBYTE_FLIP_MASK[] = { 0x0001020304050607, 0x08090a0b0c0d0e0f };
wolfSSL 11:cee25a834751 783
wolfSSL 11:cee25a834751 784 #define W_I_15 xmm14
wolfSSL 11:cee25a834751 785 #define W_I_7 xmm11
wolfSSL 11:cee25a834751 786 #define W_I_2 xmm13
wolfSSL 11:cee25a834751 787 #define W_I xmm12
wolfSSL 11:cee25a834751 788 #define G_TEMP xmm0
wolfSSL 11:cee25a834751 789 #define S_TEMP xmm1
wolfSSL 11:cee25a834751 790 #define XMM_TEMP0 xmm2
wolfSSL 11:cee25a834751 791
wolfSSL 11:cee25a834751 792 #define W_0 xmm12
wolfSSL 11:cee25a834751 793 #define W_2 xmm3
wolfSSL 11:cee25a834751 794 #define W_4 xmm4
wolfSSL 11:cee25a834751 795 #define W_6 xmm5
wolfSSL 11:cee25a834751 796 #define W_8 xmm6
wolfSSL 11:cee25a834751 797 #define W_10 xmm7
wolfSSL 11:cee25a834751 798 #define W_12 xmm8
wolfSSL 11:cee25a834751 799 #define W_14 xmm9
wolfSSL 11:cee25a834751 800
wolfSSL 11:cee25a834751 801 #define XMM_REGs
wolfSSL 11:cee25a834751 802
wolfSSL 11:cee25a834751 803 #define s0_1(dest, src) AVX1_S(dest, src, 1);
wolfSSL 11:cee25a834751 804 #define s0_2(dest, src) AVX1_S(G_TEMP, src, 8); XOR(dest, G_TEMP, dest);
wolfSSL 11:cee25a834751 805 #define s0_3(dest, src) AVX1_R(G_TEMP, src, 7); XOR(dest, G_TEMP, dest);
wolfSSL 11:cee25a834751 806
wolfSSL 11:cee25a834751 807 #define s1_1(dest, src) AVX1_S(dest, src, 19);
wolfSSL 11:cee25a834751 808 #define s1_2(dest, src) AVX1_S(G_TEMP, src, 61); XOR(dest, G_TEMP, dest);
wolfSSL 11:cee25a834751 809 #define s1_3(dest, src) AVX1_R(G_TEMP, src, 6); XOR(dest, G_TEMP, dest);
wolfSSL 11:cee25a834751 810
wolfSSL 11:cee25a834751 811 #define s0_(dest, src) s0_1(dest, src); s0_2(dest, src); s0_3(dest, src)
wolfSSL 11:cee25a834751 812 #define s1_(dest, src) s1_1(dest, src); s1_2(dest, src); s1_3(dest, src)
wolfSSL 11:cee25a834751 813
wolfSSL 11:cee25a834751 814 #define Block_xx_1(i) \
wolfSSL 11:cee25a834751 815 MOVE_to_REG(W_I_15, W_X[(i-15)&15]);\
wolfSSL 11:cee25a834751 816 MOVE_to_REG(W_I_7, W_X[(i- 7)&15]);\
wolfSSL 11:cee25a834751 817
wolfSSL 11:cee25a834751 818 #define Block_xx_2(i) \
wolfSSL 11:cee25a834751 819 MOVE_to_REG(W_I_2, W_X[(i- 2)&15]);\
wolfSSL 11:cee25a834751 820 MOVE_to_REG(W_I, W_X[(i)]);\
wolfSSL 11:cee25a834751 821
wolfSSL 11:cee25a834751 822 #define Block_xx_3(i) \
wolfSSL 11:cee25a834751 823 s0_ (XMM_TEMP0, W_I_15);\
wolfSSL 11:cee25a834751 824
wolfSSL 11:cee25a834751 825 #define Block_xx_4(i) \
wolfSSL 11:cee25a834751 826 ADD(W_I, W_I, XMM_TEMP0);\
wolfSSL 11:cee25a834751 827 ADD(W_I, W_I, W_I_7);\
wolfSSL 11:cee25a834751 828
wolfSSL 11:cee25a834751 829 #define Block_xx_5(i) \
wolfSSL 11:cee25a834751 830 s1_ (XMM_TEMP0, W_I_2);\
wolfSSL 11:cee25a834751 831
wolfSSL 11:cee25a834751 832 #define Block_xx_6(i) \
wolfSSL 11:cee25a834751 833 ADD(W_I, W_I, XMM_TEMP0);\
wolfSSL 11:cee25a834751 834 MOVE_to_MEM(W_X,i, W_I);\
wolfSSL 11:cee25a834751 835 if (i==0)\
wolfSSL 11:cee25a834751 836 MOVE_to_MEM(W_X,16, W_I);\
wolfSSL 11:cee25a834751 837
wolfSSL 11:cee25a834751 838 #define Block_xx_7(i) \
wolfSSL 11:cee25a834751 839 MOVE_to_REG(W_I_15, W_X[(i-15)&15]);\
wolfSSL 11:cee25a834751 840 MOVE_to_REG(W_I_7, W_X[(i- 7)&15]);\
wolfSSL 11:cee25a834751 841
wolfSSL 11:cee25a834751 842 #define Block_xx_8(i) \
wolfSSL 11:cee25a834751 843 MOVE_to_REG(W_I_2, W_X[(i- 2)&15]);\
wolfSSL 11:cee25a834751 844 MOVE_to_REG(W_I, W_X[(i)]);\
wolfSSL 11:cee25a834751 845
wolfSSL 11:cee25a834751 846 #define Block_xx_9(i) \
wolfSSL 11:cee25a834751 847 s0_ (XMM_TEMP0, W_I_15);\
wolfSSL 11:cee25a834751 848
wolfSSL 11:cee25a834751 849 #define Block_xx_10(i) \
wolfSSL 11:cee25a834751 850 ADD(W_I, W_I, XMM_TEMP0);\
wolfSSL 11:cee25a834751 851 ADD(W_I, W_I, W_I_7);\
wolfSSL 11:cee25a834751 852
wolfSSL 11:cee25a834751 853 #define Block_xx_11(i) \
wolfSSL 11:cee25a834751 854 s1_ (XMM_TEMP0, W_I_2);\
wolfSSL 11:cee25a834751 855
wolfSSL 11:cee25a834751 856 #define Block_xx_12(i) \
wolfSSL 11:cee25a834751 857 ADD(W_I, W_I, XMM_TEMP0);\
wolfSSL 11:cee25a834751 858 MOVE_to_MEM(W_X,i, W_I);\
wolfSSL 11:cee25a834751 859 if ((i)==0)\
wolfSSL 11:cee25a834751 860 MOVE_to_MEM(W_X,16, W_I);\
wolfSSL 11:cee25a834751 861
wolfSSL 11:cee25a834751 862 static INLINE void Block_0_1(word64 *W_X) { Block_xx_1(0); }
wolfSSL 11:cee25a834751 863 static INLINE void Block_0_2(word64 *W_X) { Block_xx_2(0); }
wolfSSL 11:cee25a834751 864 static INLINE void Block_0_3(void) { Block_xx_3(0); }
wolfSSL 11:cee25a834751 865 static INLINE void Block_0_4(void) { Block_xx_4(0); }
wolfSSL 11:cee25a834751 866 static INLINE void Block_0_5(void) { Block_xx_5(0); }
wolfSSL 11:cee25a834751 867 static INLINE void Block_0_6(word64 *W_X) { Block_xx_6(0); }
wolfSSL 11:cee25a834751 868 static INLINE void Block_0_7(word64 *W_X) { Block_xx_7(2); }
wolfSSL 11:cee25a834751 869 static INLINE void Block_0_8(word64 *W_X) { Block_xx_8(2); }
wolfSSL 11:cee25a834751 870 static INLINE void Block_0_9(void) { Block_xx_9(2); }
wolfSSL 11:cee25a834751 871 static INLINE void Block_0_10(void){ Block_xx_10(2); }
wolfSSL 11:cee25a834751 872 static INLINE void Block_0_11(void){ Block_xx_11(2); }
wolfSSL 11:cee25a834751 873 static INLINE void Block_0_12(word64 *W_X){ Block_xx_12(2); }
wolfSSL 11:cee25a834751 874
wolfSSL 11:cee25a834751 875 static INLINE void Block_4_1(word64 *W_X) { Block_xx_1(4); }
wolfSSL 11:cee25a834751 876 static INLINE void Block_4_2(word64 *W_X) { Block_xx_2(4); }
wolfSSL 11:cee25a834751 877 static INLINE void Block_4_3(void) { Block_xx_3(4); }
wolfSSL 11:cee25a834751 878 static INLINE void Block_4_4(void) { Block_xx_4(4); }
wolfSSL 11:cee25a834751 879 static INLINE void Block_4_5(void) { Block_xx_5(4); }
wolfSSL 11:cee25a834751 880 static INLINE void Block_4_6(word64 *W_X) { Block_xx_6(4); }
wolfSSL 11:cee25a834751 881 static INLINE void Block_4_7(word64 *W_X) { Block_xx_7(6); }
wolfSSL 11:cee25a834751 882 static INLINE void Block_4_8(word64 *W_X) { Block_xx_8(6); }
wolfSSL 11:cee25a834751 883 static INLINE void Block_4_9(void) { Block_xx_9(6); }
wolfSSL 11:cee25a834751 884 static INLINE void Block_4_10(void){ Block_xx_10(6); }
wolfSSL 11:cee25a834751 885 static INLINE void Block_4_11(void){ Block_xx_11(6); }
wolfSSL 11:cee25a834751 886 static INLINE void Block_4_12(word64 *W_X){ Block_xx_12(6); }
wolfSSL 11:cee25a834751 887
wolfSSL 11:cee25a834751 888 static INLINE void Block_8_1(word64 *W_X) { Block_xx_1(8); }
wolfSSL 11:cee25a834751 889 static INLINE void Block_8_2(word64 *W_X) { Block_xx_2(8); }
wolfSSL 11:cee25a834751 890 static INLINE void Block_8_3(void) { Block_xx_3(8); }
wolfSSL 11:cee25a834751 891 static INLINE void Block_8_4(void) { Block_xx_4(8); }
wolfSSL 11:cee25a834751 892 static INLINE void Block_8_5(void) { Block_xx_5(8); }
wolfSSL 11:cee25a834751 893 static INLINE void Block_8_6(word64 *W_X) { Block_xx_6(8); }
wolfSSL 11:cee25a834751 894 static INLINE void Block_8_7(word64 *W_X) { Block_xx_7(10); }
wolfSSL 11:cee25a834751 895 static INLINE void Block_8_8(word64 *W_X) { Block_xx_8(10); }
wolfSSL 11:cee25a834751 896 static INLINE void Block_8_9(void) { Block_xx_9(10); }
wolfSSL 11:cee25a834751 897 static INLINE void Block_8_10(void){ Block_xx_10(10); }
wolfSSL 11:cee25a834751 898 static INLINE void Block_8_11(void){ Block_xx_11(10); }
wolfSSL 11:cee25a834751 899 static INLINE void Block_8_12(word64 *W_X){ Block_xx_12(10); }
wolfSSL 11:cee25a834751 900
wolfSSL 11:cee25a834751 901 static INLINE void Block_12_1(word64 *W_X) { Block_xx_1(12); }
wolfSSL 11:cee25a834751 902 static INLINE void Block_12_2(word64 *W_X) { Block_xx_2(12); }
wolfSSL 11:cee25a834751 903 static INLINE void Block_12_3(void) { Block_xx_3(12); }
wolfSSL 11:cee25a834751 904 static INLINE void Block_12_4(void) { Block_xx_4(12); }
wolfSSL 11:cee25a834751 905 static INLINE void Block_12_5(void) { Block_xx_5(12); }
wolfSSL 11:cee25a834751 906 static INLINE void Block_12_6(word64 *W_X) { Block_xx_6(12); }
wolfSSL 11:cee25a834751 907 static INLINE void Block_12_7(word64 *W_X) { Block_xx_7(14); }
wolfSSL 11:cee25a834751 908 static INLINE void Block_12_8(word64 *W_X) { Block_xx_8(14); }
wolfSSL 11:cee25a834751 909 static INLINE void Block_12_9(void) { Block_xx_9(14); }
wolfSSL 11:cee25a834751 910 static INLINE void Block_12_10(void){ Block_xx_10(14); }
wolfSSL 11:cee25a834751 911 static INLINE void Block_12_11(void){ Block_xx_11(14); }
wolfSSL 11:cee25a834751 912 static INLINE void Block_12_12(word64 *W_X){ Block_xx_12(14); }
wolfSSL 11:cee25a834751 913
wolfSSL 11:cee25a834751 914 #endif /* HAVE_INTEL_AVX1 */
wolfSSL 11:cee25a834751 915
wolfSSL 11:cee25a834751 916 #if defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 917 static const unsigned long mBYTE_FLIP_MASK_Y[] =
wolfSSL 11:cee25a834751 918 { 0x0001020304050607, 0x08090a0b0c0d0e0f, 0x0001020304050607, 0x08090a0b0c0d0e0f };
wolfSSL 11:cee25a834751 919
wolfSSL 11:cee25a834751 920 #define W_from_buff_Y(buff)\
wolfSSL 11:cee25a834751 921 { /* X0..3(ymm9..12), W_X[0..15] = sha512->buffer[0.15]; */\
wolfSSL 11:cee25a834751 922 __asm__ volatile("vmovdqu %0, %%ymm8\n\t"::"m"(mBYTE_FLIP_MASK_Y[0]):YMM_REGs);\
wolfSSL 11:cee25a834751 923 __asm__ volatile("vmovdqu %0, %%ymm12\n\t"\
wolfSSL 11:cee25a834751 924 "vmovdqu %1, %%ymm4\n\t"\
wolfSSL 11:cee25a834751 925 "vpshufb %%ymm8, %%ymm12, %%ymm12\n\t"\
wolfSSL 11:cee25a834751 926 "vpshufb %%ymm8, %%ymm4, %%ymm4\n\t"\
wolfSSL 11:cee25a834751 927 :: "m"(buff[0]), "m"(buff[4]):YMM_REGs);\
wolfSSL 11:cee25a834751 928 __asm__ volatile("vmovdqu %0, %%ymm5\n\t"\
wolfSSL 11:cee25a834751 929 "vmovdqu %1, %%ymm6\n\t"\
wolfSSL 11:cee25a834751 930 "vpshufb %%ymm8, %%ymm5, %%ymm5\n\t"\
wolfSSL 11:cee25a834751 931 "vpshufb %%ymm8, %%ymm6, %%ymm6\n\t"\
wolfSSL 11:cee25a834751 932 :: "m"(buff[8]), "m"(buff[12]):YMM_REGs);\
wolfSSL 11:cee25a834751 933 }
wolfSSL 11:cee25a834751 934
wolfSSL 11:cee25a834751 935 #if defined(DEBUG_YMM)
wolfSSL 11:cee25a834751 936 #define SAVE_REG_Y(i) __asm__ volatile("vmovdqu %%ymm"#i", %0 \n\t":"=m"(reg[i-4][0])::YMM_REGs);
wolfSSL 11:cee25a834751 937 #define RECV_REG_Y(i) __asm__ volatile("vmovdqu %0, %%ymm"#i" \n\t"::"m"(reg[i-4][0]):YMM_REGs);
wolfSSL 11:cee25a834751 938
wolfSSL 11:cee25a834751 939 #define _DUMP_REG_Y(REG, name)\
wolfSSL 11:cee25a834751 940 { word64 buf[16];word64 reg[16][2];int k;\
wolfSSL 11:cee25a834751 941 SAVE_REG_Y(4); SAVE_REG_Y(5); SAVE_REG_Y(6); SAVE_REG_Y(7); \
wolfSSL 11:cee25a834751 942 SAVE_REG_Y(8); SAVE_REG_Y(9); SAVE_REG_Y(10); SAVE_REG_Y(11); SAVE_REG_Y(12);\
wolfSSL 11:cee25a834751 943 SAVE_REG_Y(13); SAVE_REG_Y(14); SAVE_REG_Y(15); \
wolfSSL 11:cee25a834751 944 __asm__ volatile("vmovdqu %%"#REG", %0 \n\t":"=m"(buf[0])::YMM_REGs);\
wolfSSL 11:cee25a834751 945 printf(" "#name":\t"); for(k=0; k<4; k++) printf("%016lx.", (word64)buf[k]); printf("\n"); \
wolfSSL 11:cee25a834751 946 RECV_REG_Y(4); RECV_REG_Y(5); RECV_REG_Y(6); RECV_REG_Y(7); \
wolfSSL 11:cee25a834751 947 RECV_REG_Y(8); RECV_REG_Y(9); RECV_REG_Y(10); RECV_REG_Y(11); RECV_REG_Y(12); \
wolfSSL 11:cee25a834751 948 RECV_REG_Y(13); RECV_REG_Y(14); RECV_REG_Y(15);\
wolfSSL 11:cee25a834751 949 }
wolfSSL 11:cee25a834751 950
wolfSSL 11:cee25a834751 951 #define DUMP_REG_Y(REG) _DUMP_REG_Y(REG, #REG)
wolfSSL 11:cee25a834751 952 #define DUMP_REG2_Y(REG) _DUMP_REG_Y(REG, #REG)
wolfSSL 11:cee25a834751 953 #define PRINTF_Y(fmt, ...)
wolfSSL 11:cee25a834751 954 #else
wolfSSL 11:cee25a834751 955 #define DUMP_REG_Y(REG)
wolfSSL 11:cee25a834751 956 #define DUMP_REG2_Y(REG)
wolfSSL 11:cee25a834751 957 #define PRINTF_Y(fmt, ...)
wolfSSL 11:cee25a834751 958 #endif /* DEBUG_YMM */
wolfSSL 11:cee25a834751 959
wolfSSL 11:cee25a834751 960 #define _MOVE_to_REGy(ymm, mem) __asm__ volatile("vmovdqu %0, %%"#ymm" "\
wolfSSL 11:cee25a834751 961 :: "m"(mem):YMM_REGs);
wolfSSL 11:cee25a834751 962 #define _MOVE_to_MEMy(mem,i, ymm) __asm__ volatile("vmovdqu %%"#ymm", %0" \
wolfSSL 11:cee25a834751 963 : "=m"(mem[i]),"=m"(mem[i+1]),"=m"(mem[i+2]),"=m"(mem[i+3])::YMM_REGs);
wolfSSL 11:cee25a834751 964 #define _MOVE_128y(ymm0, ymm1, ymm2, map) __asm__ volatile("vperm2i128 $"\
wolfSSL 11:cee25a834751 965 #map", %%"#ymm2", %%"#ymm1", %%"#ymm0" ":::YMM_REGs);
wolfSSL 11:cee25a834751 966 #define _S_TEMPy(dest, src, bits, temp) \
wolfSSL 11:cee25a834751 967 __asm__ volatile("vpsrlq $"#bits", %%"#src", %%"#dest"\n\tvpsllq $64-"#bits\
wolfSSL 11:cee25a834751 968 ", %%"#src", %%"#temp"\n\tvpor %%"#temp",%%"#dest", %%"#dest" ":::YMM_REGs);
wolfSSL 11:cee25a834751 969 #define _AVX2_R(dest, src, bits) __asm__ volatile("vpsrlq $"#bits", %%"\
wolfSSL 11:cee25a834751 970 #src", %%"#dest" ":::YMM_REGs);
wolfSSL 11:cee25a834751 971 #define _XORy(dest, src1, src2) __asm__ volatile("vpxor %%"#src1", %%"\
wolfSSL 11:cee25a834751 972 #src2", %%"#dest" ":::YMM_REGs);
wolfSSL 11:cee25a834751 973 #define _ADDy(dest, src1, src2) __asm__ volatile("vpaddq %%"#src1", %%"\
wolfSSL 11:cee25a834751 974 #src2", %%"#dest" ":::YMM_REGs);
wolfSSL 11:cee25a834751 975 #define _BLENDy(map, dest, src1, src2) __asm__ volatile("vpblendd $"#map", %%"\
wolfSSL 11:cee25a834751 976 #src1", %%"#src2", %%"#dest" ":::YMM_REGs);
wolfSSL 11:cee25a834751 977 #define _BLENDQy(map, dest, src1, src2) __asm__ volatile("vblendpd $"#map", %%"\
wolfSSL 11:cee25a834751 978 #src1", %%"#src2", %%"#dest" ":::YMM_REGs);
wolfSSL 11:cee25a834751 979 #define _PERMQy(map, dest, src) __asm__ volatile("vpermq $"#map", %%"\
wolfSSL 11:cee25a834751 980 #src", %%"#dest" ":::YMM_REGs);
wolfSSL 11:cee25a834751 981
wolfSSL 11:cee25a834751 982 #define MOVE_to_REGy(ymm, mem) _MOVE_to_REGy(ymm, mem)
wolfSSL 11:cee25a834751 983 #define MOVE_to_MEMy(mem, i, ymm) _MOVE_to_MEMy(mem, i, ymm)
wolfSSL 11:cee25a834751 984
wolfSSL 11:cee25a834751 985 #define MOVE_128y(ymm0, ymm1, ymm2, map) _MOVE_128y(ymm0, ymm1, ymm2, map)
wolfSSL 11:cee25a834751 986 #define XORy(dest, src1, src2) _XORy(dest, src1, src2)
wolfSSL 11:cee25a834751 987 #define ADDy(dest, src1, src2) _ADDy(dest, src1, src2)
wolfSSL 11:cee25a834751 988 #define BLENDy(map, dest, src1, src2) _BLENDy(map, dest, src1, src2)
wolfSSL 11:cee25a834751 989 #define BLENDQy(map, dest, src1, src2) _BLENDQy(map, dest, src1, src2)
wolfSSL 11:cee25a834751 990 #define PERMQy(map, dest, src) _PERMQy(map, dest, src)
wolfSSL 11:cee25a834751 991
wolfSSL 11:cee25a834751 992
wolfSSL 11:cee25a834751 993 #define S_TMPy(dest, src, bits, temp) _S_TEMPy(dest, src, bits, temp);
wolfSSL 11:cee25a834751 994 #define AVX2_S(dest, src, bits) S_TMPy(dest, src, bits, S_TEMPy)
wolfSSL 11:cee25a834751 995 #define AVX2_R(dest, src, bits) _AVX2_R(dest, src, bits)
wolfSSL 11:cee25a834751 996
wolfSSL 11:cee25a834751 997
wolfSSL 11:cee25a834751 998 #define FEEDBACK1_to_W_I_2(w_i_2, w_i) MOVE_128y(YMM_TEMP0, w_i, w_i, 0x08);\
wolfSSL 11:cee25a834751 999 BLENDy(0xf0, w_i_2, YMM_TEMP0, w_i_2);
wolfSSL 11:cee25a834751 1000
wolfSSL 11:cee25a834751 1001 #define MOVE_W_to_W_I_15(w_i_15, w_0, w_4) BLENDQy(0x1, w_i_15, w_4, w_0);\
wolfSSL 11:cee25a834751 1002 PERMQy(0x39, w_i_15, w_i_15);
wolfSSL 11:cee25a834751 1003 #define MOVE_W_to_W_I_7(w_i_7, w_8, w_12) BLENDQy(0x1, w_i_7, w_12, w_8);\
wolfSSL 11:cee25a834751 1004 PERMQy(0x39, w_i_7, w_i_7);
wolfSSL 11:cee25a834751 1005 #define MOVE_W_to_W_I_2(w_i_2, w_12) BLENDQy(0xc, w_i_2, w_12, w_i_2);\
wolfSSL 11:cee25a834751 1006 PERMQy(0x0e, w_i_2, w_i_2);
wolfSSL 11:cee25a834751 1007
wolfSSL 11:cee25a834751 1008
wolfSSL 11:cee25a834751 1009 #define W_I_16y ymm8
wolfSSL 11:cee25a834751 1010 #define W_I_15y ymm9
wolfSSL 11:cee25a834751 1011 #define W_I_7y ymm10
wolfSSL 11:cee25a834751 1012 #define W_I_2y ymm11
wolfSSL 11:cee25a834751 1013 #define W_Iy ymm12
wolfSSL 11:cee25a834751 1014 #define G_TEMPy ymm13
wolfSSL 11:cee25a834751 1015 #define S_TEMPy ymm14
wolfSSL 11:cee25a834751 1016 #define YMM_TEMP0 ymm15
wolfSSL 11:cee25a834751 1017 #define YMM_TEMP0x xmm15
wolfSSL 11:cee25a834751 1018 #define W_I_TEMPy ymm7
wolfSSL 11:cee25a834751 1019 #define W_K_TEMPy ymm15
wolfSSL 11:cee25a834751 1020 #define W_K_TEMPx xmm15
wolfSSL 11:cee25a834751 1021 #define W_0y ymm12
wolfSSL 11:cee25a834751 1022 #define W_4y ymm4
wolfSSL 11:cee25a834751 1023 #define W_8y ymm5
wolfSSL 11:cee25a834751 1024 #define W_12y ymm6
wolfSSL 11:cee25a834751 1025
wolfSSL 11:cee25a834751 1026 #define YMM_REGs
wolfSSL 11:cee25a834751 1027 /* Registers are saved in Sha512Update/Final */
wolfSSL 11:cee25a834751 1028 /* "%ymm7","%ymm8","%ymm9","%ymm10","%ymm11","%ymm12","%ymm13","%ymm14","%ymm15"*/
wolfSSL 11:cee25a834751 1029
wolfSSL 11:cee25a834751 1030 #define MOVE_15_to_16(w_i_16, w_i_15, w_i_7)\
wolfSSL 11:cee25a834751 1031 __asm__ volatile("vperm2i128 $0x01, %%"#w_i_15", %%"#w_i_15", %%"#w_i_15" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1032 __asm__ volatile("vpblendd $0x08, %%"#w_i_15", %%"#w_i_7", %%"#w_i_16" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1033 __asm__ volatile("vperm2i128 $0x01, %%"#w_i_7", %%"#w_i_7", %%"#w_i_15" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1034 __asm__ volatile("vpblendd $0x80, %%"#w_i_15", %%"#w_i_16", %%"#w_i_16" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1035 __asm__ volatile("vpshufd $0x93, %%"#w_i_16", %%"#w_i_16" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1036
wolfSSL 11:cee25a834751 1037 #define MOVE_7_to_15(w_i_15, w_i_7)\
wolfSSL 11:cee25a834751 1038 __asm__ volatile("vmovdqu %%"#w_i_7", %%"#w_i_15" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1039
wolfSSL 11:cee25a834751 1040 #define MOVE_I_to_7(w_i_7, w_i)\
wolfSSL 11:cee25a834751 1041 __asm__ volatile("vperm2i128 $0x01, %%"#w_i", %%"#w_i", %%"#w_i_7" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1042 __asm__ volatile("vpblendd $0x01, %%"#w_i_7", %%"#w_i", %%"#w_i_7" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1043 __asm__ volatile("vpshufd $0x39, %%"#w_i_7", %%"#w_i_7" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1044
wolfSSL 11:cee25a834751 1045 #define MOVE_I_to_2(w_i_2, w_i)\
wolfSSL 11:cee25a834751 1046 __asm__ volatile("vperm2i128 $0x01, %%"#w_i", %%"#w_i", %%"#w_i_2" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1047 __asm__ volatile("vpshufd $0x0e, %%"#w_i_2", %%"#w_i_2" ":::YMM_REGs);\
wolfSSL 11:cee25a834751 1048
wolfSSL 11:cee25a834751 1049 #endif /* HAVE_INTEL_AVX2 */
wolfSSL 11:cee25a834751 1050
wolfSSL 11:cee25a834751 1051
wolfSSL 11:cee25a834751 1052 /*** Transform Body ***/
wolfSSL 11:cee25a834751 1053 #if defined(HAVE_INTEL_AVX1)
wolfSSL 11:cee25a834751 1054 static int Transform_AVX1(Sha512* sha512)
wolfSSL 11:cee25a834751 1055 {
wolfSSL 11:cee25a834751 1056 const word64* K = K512;
wolfSSL 11:cee25a834751 1057 word64 W_X[16+4] = {0};
wolfSSL 11:cee25a834751 1058 word32 j;
wolfSSL 11:cee25a834751 1059 word64 T[8];
wolfSSL 11:cee25a834751 1060
wolfSSL 11:cee25a834751 1061 /* Copy digest to working vars */
wolfSSL 11:cee25a834751 1062 XMEMCPY(T, sha512->digest, sizeof(T));
wolfSSL 11:cee25a834751 1063
wolfSSL 11:cee25a834751 1064 W_from_buff(W_X, sha512->buffer);
wolfSSL 11:cee25a834751 1065 for (j = 0; j < 80; j += 16) {
wolfSSL 11:cee25a834751 1066 Rx_1( 0); Block_0_1(W_X); Rx_2( 0); Block_0_2(W_X); Rx_3( 0); Block_0_3();
wolfSSL 11:cee25a834751 1067 Rx_1( 1); Block_0_4(); Rx_2( 1); Block_0_5(); Rx_3( 1); Block_0_6(W_X);
wolfSSL 11:cee25a834751 1068 Rx_1( 2); Block_0_7(W_X); Rx_2( 2); Block_0_8(W_X); Rx_3( 2); Block_0_9();
wolfSSL 11:cee25a834751 1069 Rx_1( 3); Block_0_10();Rx_2( 3); Block_0_11();Rx_3( 3); Block_0_12(W_X);
wolfSSL 11:cee25a834751 1070
wolfSSL 11:cee25a834751 1071 Rx_1( 4); Block_4_1(W_X); Rx_2( 4); Block_4_2(W_X); Rx_3( 4); Block_4_3();
wolfSSL 11:cee25a834751 1072 Rx_1( 5); Block_4_4(); Rx_2( 5); Block_4_5(); Rx_3( 5); Block_4_6(W_X);
wolfSSL 11:cee25a834751 1073 Rx_1( 6); Block_4_7(W_X); Rx_2( 6); Block_4_8(W_X); Rx_3( 6); Block_4_9();
wolfSSL 11:cee25a834751 1074 Rx_1( 7); Block_4_10();Rx_2( 7); Block_4_11();Rx_3( 7); Block_4_12(W_X);
wolfSSL 11:cee25a834751 1075
wolfSSL 11:cee25a834751 1076 Rx_1( 8); Block_8_1(W_X); Rx_2( 8); Block_8_2(W_X); Rx_3( 8); Block_8_3();
wolfSSL 11:cee25a834751 1077 Rx_1( 9); Block_8_4(); Rx_2( 9); Block_8_5(); Rx_3( 9); Block_8_6(W_X);
wolfSSL 11:cee25a834751 1078 Rx_1(10); Block_8_7(W_X); Rx_2(10); Block_8_8(W_X); Rx_3(10); Block_8_9();
wolfSSL 11:cee25a834751 1079 Rx_1(11); Block_8_10();Rx_2(11); Block_8_11();Rx_3(11); Block_8_12(W_X);
wolfSSL 11:cee25a834751 1080
wolfSSL 11:cee25a834751 1081 Rx_1(12); Block_12_1(W_X); Rx_2(12); Block_12_2(W_X); Rx_3(12); Block_12_3();
wolfSSL 11:cee25a834751 1082 Rx_1(13); Block_12_4(); Rx_2(13); Block_12_5(); Rx_3(13); Block_12_6(W_X);
wolfSSL 11:cee25a834751 1083 Rx_1(14); Block_12_7(W_X); Rx_2(14); Block_12_8(W_X); Rx_3(14); Block_12_9();
wolfSSL 11:cee25a834751 1084 Rx_1(15); Block_12_10();Rx_2(15); Block_12_11();Rx_3(15); Block_12_12(W_X);
wolfSSL 11:cee25a834751 1085 }
wolfSSL 11:cee25a834751 1086
wolfSSL 11:cee25a834751 1087 /* Add the working vars back into digest */
wolfSSL 11:cee25a834751 1088 sha512->digest[0] += a(0);
wolfSSL 11:cee25a834751 1089 sha512->digest[1] += b(0);
wolfSSL 11:cee25a834751 1090 sha512->digest[2] += c(0);
wolfSSL 11:cee25a834751 1091 sha512->digest[3] += d(0);
wolfSSL 11:cee25a834751 1092 sha512->digest[4] += e(0);
wolfSSL 11:cee25a834751 1093 sha512->digest[5] += f(0);
wolfSSL 11:cee25a834751 1094 sha512->digest[6] += g(0);
wolfSSL 11:cee25a834751 1095 sha512->digest[7] += h(0);
wolfSSL 11:cee25a834751 1096
wolfSSL 11:cee25a834751 1097 /* Wipe variables */
wolfSSL 11:cee25a834751 1098 #if !defined(HAVE_INTEL_AVX1) && !defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 1099 XMEMSET(W_X, 0, sizeof(word64) * 16);
wolfSSL 11:cee25a834751 1100 #endif
wolfSSL 11:cee25a834751 1101 XMEMSET(T, 0, sizeof(T));
wolfSSL 11:cee25a834751 1102
wolfSSL 11:cee25a834751 1103 return 0;
wolfSSL 11:cee25a834751 1104 }
wolfSSL 11:cee25a834751 1105 #endif /* HAVE_INTEL_AVX1 */
wolfSSL 11:cee25a834751 1106
wolfSSL 11:cee25a834751 1107 #if defined(HAVE_INTEL_AVX2) && defined(HAVE_INTEL_AVX1) && defined(HAVE_INTEL_RORX)
wolfSSL 11:cee25a834751 1108 static int Transform_AVX1_RORX(Sha512* sha512)
wolfSSL 11:cee25a834751 1109 {
wolfSSL 11:cee25a834751 1110 const word64* K = K512;
wolfSSL 11:cee25a834751 1111 word64 W_X[16+4] = {0};
wolfSSL 11:cee25a834751 1112 word32 j;
wolfSSL 11:cee25a834751 1113 word64 T[8];
wolfSSL 11:cee25a834751 1114
wolfSSL 11:cee25a834751 1115 /* Copy digest to working vars */
wolfSSL 11:cee25a834751 1116 XMEMCPY(T, sha512->digest, sizeof(T));
wolfSSL 11:cee25a834751 1117
wolfSSL 11:cee25a834751 1118 W_from_buff(W_X, sha512->buffer);
wolfSSL 11:cee25a834751 1119 for (j = 0; j < 80; j += 16) {
wolfSSL 11:cee25a834751 1120 Rx_RORX_1( 0); Block_0_1(W_X); Rx_RORX_2( 0); Block_0_2(W_X);
wolfSSL 11:cee25a834751 1121 Rx_RORX_3( 0); Block_0_3();
wolfSSL 11:cee25a834751 1122 Rx_RORX_1( 1); Block_0_4(); Rx_RORX_2( 1); Block_0_5();
wolfSSL 11:cee25a834751 1123 Rx_RORX_3( 1); Block_0_6(W_X);
wolfSSL 11:cee25a834751 1124 Rx_RORX_1( 2); Block_0_7(W_X); Rx_RORX_2( 2); Block_0_8(W_X);
wolfSSL 11:cee25a834751 1125 Rx_RORX_3( 2); Block_0_9();
wolfSSL 11:cee25a834751 1126 Rx_RORX_1( 3); Block_0_10();Rx_RORX_2( 3); Block_0_11();
wolfSSL 11:cee25a834751 1127 Rx_RORX_3( 3); Block_0_12(W_X);
wolfSSL 11:cee25a834751 1128
wolfSSL 11:cee25a834751 1129 Rx_RORX_1( 4); Block_4_1(W_X); Rx_RORX_2( 4); Block_4_2(W_X);
wolfSSL 11:cee25a834751 1130 Rx_RORX_3( 4); Block_4_3();
wolfSSL 11:cee25a834751 1131 Rx_RORX_1( 5); Block_4_4(); Rx_RORX_2( 5); Block_4_5();
wolfSSL 11:cee25a834751 1132 Rx_RORX_3( 5); Block_4_6(W_X);
wolfSSL 11:cee25a834751 1133 Rx_RORX_1( 6); Block_4_7(W_X); Rx_RORX_2( 6); Block_4_8(W_X);
wolfSSL 11:cee25a834751 1134 Rx_RORX_3( 6); Block_4_9();
wolfSSL 11:cee25a834751 1135 Rx_RORX_1( 7); Block_4_10();Rx_RORX_2( 7); Block_4_11();
wolfSSL 11:cee25a834751 1136 Rx_RORX_3( 7); Block_4_12(W_X);
wolfSSL 11:cee25a834751 1137
wolfSSL 11:cee25a834751 1138 Rx_RORX_1( 8); Block_8_1(W_X); Rx_RORX_2( 8); Block_8_2(W_X);
wolfSSL 11:cee25a834751 1139 Rx_RORX_3( 8); Block_8_3();
wolfSSL 11:cee25a834751 1140 Rx_RORX_1( 9); Block_8_4(); Rx_RORX_2( 9); Block_8_5();
wolfSSL 11:cee25a834751 1141 Rx_RORX_3( 9); Block_8_6(W_X);
wolfSSL 11:cee25a834751 1142 Rx_RORX_1(10); Block_8_7(W_X); Rx_RORX_2(10); Block_8_8(W_X);
wolfSSL 11:cee25a834751 1143 Rx_RORX_3(10); Block_8_9();
wolfSSL 11:cee25a834751 1144 Rx_RORX_1(11); Block_8_10();Rx_RORX_2(11); Block_8_11();
wolfSSL 11:cee25a834751 1145 Rx_RORX_3(11); Block_8_12(W_X);
wolfSSL 11:cee25a834751 1146
wolfSSL 11:cee25a834751 1147 Rx_RORX_1(12); Block_12_1(W_X); Rx_RORX_2(12); Block_12_2(W_X);
wolfSSL 11:cee25a834751 1148 Rx_RORX_3(12); Block_12_3();
wolfSSL 11:cee25a834751 1149 Rx_RORX_1(13); Block_12_4(); Rx_RORX_2(13); Block_12_5();
wolfSSL 11:cee25a834751 1150 Rx_RORX_3(13); Block_12_6(W_X);
wolfSSL 11:cee25a834751 1151 Rx_RORX_1(14); Block_12_7(W_X); Rx_RORX_2(14); Block_12_8(W_X);
wolfSSL 11:cee25a834751 1152 Rx_RORX_3(14); Block_12_9();
wolfSSL 11:cee25a834751 1153 Rx_RORX_1(15); Block_12_10();Rx_RORX_2(15); Block_12_11();
wolfSSL 11:cee25a834751 1154 Rx_RORX_3(15); Block_12_12(W_X);
wolfSSL 11:cee25a834751 1155 }
wolfSSL 11:cee25a834751 1156
wolfSSL 11:cee25a834751 1157 /* Add the working vars back into digest */
wolfSSL 11:cee25a834751 1158 sha512->digest[0] += a(0);
wolfSSL 11:cee25a834751 1159 sha512->digest[1] += b(0);
wolfSSL 11:cee25a834751 1160 sha512->digest[2] += c(0);
wolfSSL 11:cee25a834751 1161 sha512->digest[3] += d(0);
wolfSSL 11:cee25a834751 1162 sha512->digest[4] += e(0);
wolfSSL 11:cee25a834751 1163 sha512->digest[5] += f(0);
wolfSSL 11:cee25a834751 1164 sha512->digest[6] += g(0);
wolfSSL 11:cee25a834751 1165 sha512->digest[7] += h(0);
wolfSSL 11:cee25a834751 1166
wolfSSL 11:cee25a834751 1167 /* Wipe variables */
wolfSSL 11:cee25a834751 1168 #if !defined(HAVE_INTEL_AVX1)&&!defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 1169 XMEMSET(W_X, 0, sizeof(word64) * 16);
wolfSSL 11:cee25a834751 1170 #endif
wolfSSL 11:cee25a834751 1171 XMEMSET(T, 0, sizeof(T));
wolfSSL 11:cee25a834751 1172
wolfSSL 11:cee25a834751 1173 return 0;
wolfSSL 11:cee25a834751 1174 }
wolfSSL 11:cee25a834751 1175 #endif /* HAVE_INTEL_AVX2 && HAVE_INTEL_AVX1 && HAVE_INTEL_RORX */
wolfSSL 11:cee25a834751 1176
wolfSSL 11:cee25a834751 1177 #if defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 1178
wolfSSL 11:cee25a834751 1179 #define s0_1y(dest, src) AVX2_S(dest, src, 1);
wolfSSL 11:cee25a834751 1180 #define s0_2y(dest, src) AVX2_S(G_TEMPy, src, 8); XORy(dest, G_TEMPy, dest);
wolfSSL 11:cee25a834751 1181 #define s0_3y(dest, src) AVX2_R(G_TEMPy, src, 7); XORy(dest, G_TEMPy, dest);
wolfSSL 11:cee25a834751 1182
wolfSSL 11:cee25a834751 1183 #define s1_1y(dest, src) AVX2_S(dest, src, 19);
wolfSSL 11:cee25a834751 1184 #define s1_2y(dest, src) AVX2_S(G_TEMPy, src, 61); XORy(dest, G_TEMPy, dest);
wolfSSL 11:cee25a834751 1185 #define s1_3y(dest, src) AVX2_R(G_TEMPy, src, 6); XORy(dest, G_TEMPy, dest);
wolfSSL 11:cee25a834751 1186
wolfSSL 11:cee25a834751 1187 #define s0_y(dest, src) s0_1y(dest, src); s0_2y(dest, src); s0_3y(dest, src)
wolfSSL 11:cee25a834751 1188 #define s1_y(dest, src) s1_1y(dest, src); s1_2y(dest, src); s1_3y(dest, src)
wolfSSL 11:cee25a834751 1189
wolfSSL 11:cee25a834751 1190
wolfSSL 11:cee25a834751 1191 #define Block_Y_xx_1(i, w_0, w_4, w_8, w_12)\
wolfSSL 11:cee25a834751 1192 MOVE_W_to_W_I_15(W_I_15y, w_0, w_4);\
wolfSSL 11:cee25a834751 1193 MOVE_W_to_W_I_7 (W_I_7y, w_8, w_12);\
wolfSSL 11:cee25a834751 1194 MOVE_W_to_W_I_2 (W_I_2y, w_12);\
wolfSSL 11:cee25a834751 1195
wolfSSL 11:cee25a834751 1196 #define Block_Y_xx_2(i, w_0, w_4, w_8, w_12)\
wolfSSL 11:cee25a834751 1197 s0_1y (YMM_TEMP0, W_I_15y);\
wolfSSL 11:cee25a834751 1198
wolfSSL 11:cee25a834751 1199 #define Block_Y_xx_3(i, w_0, w_4, w_8, w_12)\
wolfSSL 11:cee25a834751 1200 s0_2y (YMM_TEMP0, W_I_15y);\
wolfSSL 11:cee25a834751 1201
wolfSSL 11:cee25a834751 1202 #define Block_Y_xx_4(i, w_0, w_4, w_8, w_12)\
wolfSSL 11:cee25a834751 1203 s0_3y (YMM_TEMP0, W_I_15y);\
wolfSSL 11:cee25a834751 1204
wolfSSL 11:cee25a834751 1205 #define Block_Y_xx_5(i, w_0, w_4, w_8, w_12)\
wolfSSL 11:cee25a834751 1206 ADDy(W_I_TEMPy, w_0, YMM_TEMP0);\
wolfSSL 11:cee25a834751 1207
wolfSSL 11:cee25a834751 1208 #define Block_Y_xx_6(i, w_0, w_4, w_8, w_12)\
wolfSSL 11:cee25a834751 1209 ADDy(W_I_TEMPy, W_I_TEMPy, W_I_7y);\
wolfSSL 11:cee25a834751 1210 s1_1y (YMM_TEMP0, W_I_2y);\
wolfSSL 11:cee25a834751 1211
wolfSSL 11:cee25a834751 1212 #define Block_Y_xx_7(i, w_0, w_4, w_8, w_12)\
wolfSSL 11:cee25a834751 1213 s1_2y (YMM_TEMP0, W_I_2y);\
wolfSSL 11:cee25a834751 1214
wolfSSL 11:cee25a834751 1215 #define Block_Y_xx_8(i, w_0, w_4, w_8, w_12)\
wolfSSL 11:cee25a834751 1216 s1_3y (YMM_TEMP0, W_I_2y);\
wolfSSL 11:cee25a834751 1217 ADDy(w_0, W_I_TEMPy, YMM_TEMP0);\
wolfSSL 11:cee25a834751 1218
wolfSSL 11:cee25a834751 1219 #define Block_Y_xx_9(i, w_0, w_4, w_8, w_12)\
wolfSSL 11:cee25a834751 1220 FEEDBACK1_to_W_I_2(W_I_2y, w_0);\
wolfSSL 11:cee25a834751 1221
wolfSSL 11:cee25a834751 1222 #define Block_Y_xx_10(i, w_0, w_4, w_8, w_12) \
wolfSSL 11:cee25a834751 1223 s1_1y (YMM_TEMP0, W_I_2y);\
wolfSSL 11:cee25a834751 1224
wolfSSL 11:cee25a834751 1225 #define Block_Y_xx_11(i, w_0, w_4, w_8, w_12) \
wolfSSL 11:cee25a834751 1226 s1_2y (YMM_TEMP0, W_I_2y);\
wolfSSL 11:cee25a834751 1227
wolfSSL 11:cee25a834751 1228 #define Block_Y_xx_12(i, w_0, w_4, w_8, w_12)\
wolfSSL 11:cee25a834751 1229 s1_3y (YMM_TEMP0, W_I_2y);\
wolfSSL 11:cee25a834751 1230 ADDy(w_0, W_I_TEMPy, YMM_TEMP0);\
wolfSSL 11:cee25a834751 1231 MOVE_to_MEMy(w,0, w_4);\
wolfSSL 11:cee25a834751 1232
wolfSSL 11:cee25a834751 1233
wolfSSL 11:cee25a834751 1234 static INLINE void Block_Y_0_1(void) { Block_Y_xx_1(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1235 static INLINE void Block_Y_0_2(void) { Block_Y_xx_2(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1236 static INLINE void Block_Y_0_3(void) { Block_Y_xx_3(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1237 static INLINE void Block_Y_0_4(void) { Block_Y_xx_4(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1238 static INLINE void Block_Y_0_5(void) { Block_Y_xx_5(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1239 static INLINE void Block_Y_0_6(void) { Block_Y_xx_6(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1240 static INLINE void Block_Y_0_7(void) { Block_Y_xx_7(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1241 static INLINE void Block_Y_0_8(void) { Block_Y_xx_8(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1242 static INLINE void Block_Y_0_9(void) { Block_Y_xx_9(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1243 static INLINE void Block_Y_0_10(void){ Block_Y_xx_10(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1244 static INLINE void Block_Y_0_11(void){ Block_Y_xx_11(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1245 static INLINE void Block_Y_0_12(word64 *w){ Block_Y_xx_12(0, W_0y, W_4y, W_8y, W_12y); }
wolfSSL 11:cee25a834751 1246
wolfSSL 11:cee25a834751 1247 static INLINE void Block_Y_4_1(void) { Block_Y_xx_1(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1248 static INLINE void Block_Y_4_2(void) { Block_Y_xx_2(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1249 static INLINE void Block_Y_4_3(void) { Block_Y_xx_3(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1250 static INLINE void Block_Y_4_4(void) { Block_Y_xx_4(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1251 static INLINE void Block_Y_4_5(void) { Block_Y_xx_5(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1252 static INLINE void Block_Y_4_6(void) { Block_Y_xx_6(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1253 static INLINE void Block_Y_4_7(void) { Block_Y_xx_7(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1254 static INLINE void Block_Y_4_8(void) { Block_Y_xx_8(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1255 static INLINE void Block_Y_4_9(void) { Block_Y_xx_9(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1256 static INLINE void Block_Y_4_10(void) { Block_Y_xx_10(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1257 static INLINE void Block_Y_4_11(void) { Block_Y_xx_11(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1258 static INLINE void Block_Y_4_12(word64 *w) { Block_Y_xx_12(4, W_4y, W_8y, W_12y, W_0y); }
wolfSSL 11:cee25a834751 1259
wolfSSL 11:cee25a834751 1260 static INLINE void Block_Y_8_1(void) { Block_Y_xx_1(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1261 static INLINE void Block_Y_8_2(void) { Block_Y_xx_2(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1262 static INLINE void Block_Y_8_3(void) { Block_Y_xx_3(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1263 static INLINE void Block_Y_8_4(void) { Block_Y_xx_4(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1264 static INLINE void Block_Y_8_5(void) { Block_Y_xx_5(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1265 static INLINE void Block_Y_8_6(void) { Block_Y_xx_6(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1266 static INLINE void Block_Y_8_7(void) { Block_Y_xx_7(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1267 static INLINE void Block_Y_8_8(void) { Block_Y_xx_8(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1268 static INLINE void Block_Y_8_9(void) { Block_Y_xx_9(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1269 static INLINE void Block_Y_8_10(void) { Block_Y_xx_10(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1270 static INLINE void Block_Y_8_11(void) { Block_Y_xx_11(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1271 static INLINE void Block_Y_8_12(word64 *w) { Block_Y_xx_12(8, W_8y, W_12y, W_0y, W_4y); }
wolfSSL 11:cee25a834751 1272
wolfSSL 11:cee25a834751 1273 static INLINE void Block_Y_12_1(void) { Block_Y_xx_1(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1274 static INLINE void Block_Y_12_2(void) { Block_Y_xx_2(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1275 static INLINE void Block_Y_12_3(void) { Block_Y_xx_3(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1276 static INLINE void Block_Y_12_4(void) { Block_Y_xx_4(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1277 static INLINE void Block_Y_12_5(void) { Block_Y_xx_5(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1278 static INLINE void Block_Y_12_6(void) { Block_Y_xx_6(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1279 static INLINE void Block_Y_12_7(void) { Block_Y_xx_7(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1280 static INLINE void Block_Y_12_8(void) { Block_Y_xx_8(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1281 static INLINE void Block_Y_12_9(void) { Block_Y_xx_9(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1282 static INLINE void Block_Y_12_10(void) { Block_Y_xx_10(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1283 static INLINE void Block_Y_12_11(void) { Block_Y_xx_11(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1284 static INLINE void Block_Y_12_12(word64 *w) { Block_Y_xx_12(12, W_12y, W_0y, W_4y, W_8y); }
wolfSSL 11:cee25a834751 1285
wolfSSL 11:cee25a834751 1286
wolfSSL 11:cee25a834751 1287 static int Transform_AVX2(Sha512* sha512)
wolfSSL 11:cee25a834751 1288 {
wolfSSL 11:cee25a834751 1289 const word64* K = K512;
wolfSSL 11:cee25a834751 1290 word64 w[4];
wolfSSL 11:cee25a834751 1291 word32 j;
wolfSSL 11:cee25a834751 1292 word64 T[8];
wolfSSL 11:cee25a834751 1293
wolfSSL 11:cee25a834751 1294 /* Copy digest to working vars */
wolfSSL 11:cee25a834751 1295 XMEMCPY(T, sha512->digest, sizeof(T));
wolfSSL 11:cee25a834751 1296
wolfSSL 11:cee25a834751 1297 W_from_buff_Y(sha512->buffer);
wolfSSL 11:cee25a834751 1298 MOVE_to_MEMy(w,0, W_0y);
wolfSSL 11:cee25a834751 1299 for (j = 0; j < 80; j += 16) {
wolfSSL 11:cee25a834751 1300 Ry_1( 0, w[0]); Block_Y_0_1(); Ry_2( 0, w[0]); Block_Y_0_2();
wolfSSL 11:cee25a834751 1301 Ry_3( 0, w[0]); Block_Y_0_3();
wolfSSL 11:cee25a834751 1302 Ry_1( 1, w[1]); Block_Y_0_4(); Ry_2( 1, w[1]); Block_Y_0_5();
wolfSSL 11:cee25a834751 1303 Ry_3( 1, w[1]); Block_Y_0_6();
wolfSSL 11:cee25a834751 1304 Ry_1( 2, w[2]); Block_Y_0_7(); Ry_2( 2, w[2]); Block_Y_0_8();
wolfSSL 11:cee25a834751 1305 Ry_3( 2, w[2]); Block_Y_0_9();
wolfSSL 11:cee25a834751 1306 Ry_1( 3, w[3]); Block_Y_0_10();Ry_2( 3, w[3]); Block_Y_0_11();
wolfSSL 11:cee25a834751 1307 Ry_3( 3, w[3]); Block_Y_0_12(w);
wolfSSL 11:cee25a834751 1308
wolfSSL 11:cee25a834751 1309 Ry_1( 4, w[0]); Block_Y_4_1(); Ry_2( 4, w[0]); Block_Y_4_2();
wolfSSL 11:cee25a834751 1310 Ry_3( 4, w[0]); Block_Y_4_3();
wolfSSL 11:cee25a834751 1311 Ry_1( 5, w[1]); Block_Y_4_4(); Ry_2( 5, w[1]); Block_Y_4_5();
wolfSSL 11:cee25a834751 1312 Ry_3( 5, w[1]); Block_Y_4_6();
wolfSSL 11:cee25a834751 1313 Ry_1( 6, w[2]); Block_Y_4_7(); Ry_2( 6, w[2]); Block_Y_4_8();
wolfSSL 11:cee25a834751 1314 Ry_3( 6, w[2]); Block_Y_4_9();
wolfSSL 11:cee25a834751 1315 Ry_1( 7, w[3]); Block_Y_4_10(); Ry_2( 7, w[3]);Block_Y_4_11();
wolfSSL 11:cee25a834751 1316 Ry_3( 7, w[3]);Block_Y_4_12(w);
wolfSSL 11:cee25a834751 1317
wolfSSL 11:cee25a834751 1318 Ry_1( 8, w[0]); Block_Y_8_1(); Ry_2( 8, w[0]); Block_Y_8_2();
wolfSSL 11:cee25a834751 1319 Ry_3( 8, w[0]); Block_Y_8_3();
wolfSSL 11:cee25a834751 1320 Ry_1( 9, w[1]); Block_Y_8_4(); Ry_2( 9, w[1]); Block_Y_8_5();
wolfSSL 11:cee25a834751 1321 Ry_3( 9, w[1]); Block_Y_8_6();
wolfSSL 11:cee25a834751 1322 Ry_1(10, w[2]); Block_Y_8_7(); Ry_2(10, w[2]); Block_Y_8_8();
wolfSSL 11:cee25a834751 1323 Ry_3(10, w[2]); Block_Y_8_9();
wolfSSL 11:cee25a834751 1324 Ry_1(11, w[3]); Block_Y_8_10();Ry_2(11, w[3]); Block_Y_8_11();
wolfSSL 11:cee25a834751 1325 Ry_3(11, w[3]); Block_Y_8_12(w);
wolfSSL 11:cee25a834751 1326
wolfSSL 11:cee25a834751 1327 Ry_1(12, w[0]); Block_Y_12_1(); Ry_2(12, w[0]); Block_Y_12_2();
wolfSSL 11:cee25a834751 1328 Ry_3(12, w[0]); Block_Y_12_3();
wolfSSL 11:cee25a834751 1329 Ry_1(13, w[1]); Block_Y_12_4(); Ry_2(13, w[1]); Block_Y_12_5();
wolfSSL 11:cee25a834751 1330 Ry_3(13, w[1]); Block_Y_12_6();
wolfSSL 11:cee25a834751 1331 Ry_1(14, w[2]); Block_Y_12_7(); Ry_2(14, w[2]); Block_Y_12_8();
wolfSSL 11:cee25a834751 1332 Ry_3(14, w[2]); Block_Y_12_9();
wolfSSL 11:cee25a834751 1333 Ry_1(15, w[3]); Block_Y_12_10();Ry_2(15, w[3]); Block_Y_12_11();
wolfSSL 11:cee25a834751 1334 Ry_3(15, w[3]);Block_Y_12_12(w);
wolfSSL 11:cee25a834751 1335 }
wolfSSL 11:cee25a834751 1336
wolfSSL 11:cee25a834751 1337 /* Add the working vars back into digest */
wolfSSL 11:cee25a834751 1338 sha512->digest[0] += a(0);
wolfSSL 11:cee25a834751 1339 sha512->digest[1] += b(0);
wolfSSL 11:cee25a834751 1340 sha512->digest[2] += c(0);
wolfSSL 11:cee25a834751 1341 sha512->digest[3] += d(0);
wolfSSL 11:cee25a834751 1342 sha512->digest[4] += e(0);
wolfSSL 11:cee25a834751 1343 sha512->digest[5] += f(0);
wolfSSL 11:cee25a834751 1344 sha512->digest[6] += g(0);
wolfSSL 11:cee25a834751 1345 sha512->digest[7] += h(0);
wolfSSL 11:cee25a834751 1346
wolfSSL 11:cee25a834751 1347 /* Wipe variables */
wolfSSL 11:cee25a834751 1348 #if !defined(HAVE_INTEL_AVX1) && !defined(HAVE_INTEL_AVX2)
wolfSSL 11:cee25a834751 1349 XMEMSET(W, 0, sizeof(word64) * 16);
wolfSSL 11:cee25a834751 1350 #endif
wolfSSL 11:cee25a834751 1351 XMEMSET(T, 0, sizeof(T));
wolfSSL 11:cee25a834751 1352
wolfSSL 11:cee25a834751 1353 return 0;
wolfSSL 11:cee25a834751 1354 }
wolfSSL 11:cee25a834751 1355 #endif /* HAVE_INTEL_AVX2 */
wolfSSL 11:cee25a834751 1356
wolfSSL 11:cee25a834751 1357
wolfSSL 11:cee25a834751 1358
wolfSSL 11:cee25a834751 1359 /* -------------------------------------------------------------------------- */
wolfSSL 11:cee25a834751 1360 /* SHA384 */
wolfSSL 11:cee25a834751 1361 /* -------------------------------------------------------------------------- */
wolfSSL 11:cee25a834751 1362 #ifdef WOLFSSL_SHA384
wolfSSL 11:cee25a834751 1363 static int InitSha384(Sha384* sha384)
wolfSSL 11:cee25a834751 1364 {
wolfSSL 11:cee25a834751 1365 sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8);
wolfSSL 11:cee25a834751 1366 sha384->digest[1] = W64LIT(0x629a292a367cd507);
wolfSSL 11:cee25a834751 1367 sha384->digest[2] = W64LIT(0x9159015a3070dd17);
wolfSSL 11:cee25a834751 1368 sha384->digest[3] = W64LIT(0x152fecd8f70e5939);
wolfSSL 11:cee25a834751 1369 sha384->digest[4] = W64LIT(0x67332667ffc00b31);
wolfSSL 11:cee25a834751 1370 sha384->digest[5] = W64LIT(0x8eb44a8768581511);
wolfSSL 11:cee25a834751 1371 sha384->digest[6] = W64LIT(0xdb0c2e0d64f98fa7);
wolfSSL 11:cee25a834751 1372 sha384->digest[7] = W64LIT(0x47b5481dbefa4fa4);
wolfSSL 11:cee25a834751 1373
wolfSSL 11:cee25a834751 1374 sha384->buffLen = 0;
wolfSSL 11:cee25a834751 1375 sha384->loLen = 0;
wolfSSL 11:cee25a834751 1376 sha384->hiLen = 0;
wolfSSL 11:cee25a834751 1377
wolfSSL 11:cee25a834751 1378 return 0;
wolfSSL 11:cee25a834751 1379 }
wolfSSL 11:cee25a834751 1380
wolfSSL 11:cee25a834751 1381 int wc_Sha384Update(Sha384* sha384, const byte* data, word32 len)
wolfSSL 11:cee25a834751 1382 {
wolfSSL 11:cee25a834751 1383
wolfSSL 11:cee25a834751 1384 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
wolfSSL 11:cee25a834751 1385 if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
wolfSSL 11:cee25a834751 1386 #if defined(HAVE_INTEL_QA)
wolfSSL 11:cee25a834751 1387 return IntelQaSymSha384(&sha384->asyncDev, NULL, data, len);
wolfSSL 11:cee25a834751 1388 #endif
wolfSSL 11:cee25a834751 1389 }
wolfSSL 11:cee25a834751 1390 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 1391
wolfSSL 11:cee25a834751 1392 return Sha512Update((Sha512*)sha384, data, len);
wolfSSL 11:cee25a834751 1393 }
wolfSSL 11:cee25a834751 1394
wolfSSL 11:cee25a834751 1395
wolfSSL 11:cee25a834751 1396 int wc_Sha384Final(Sha384* sha384, byte* hash)
wolfSSL 11:cee25a834751 1397 {
wolfSSL 11:cee25a834751 1398 int ret;
wolfSSL 11:cee25a834751 1399
wolfSSL 11:cee25a834751 1400 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
wolfSSL 11:cee25a834751 1401 if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
wolfSSL 11:cee25a834751 1402 #if defined(HAVE_INTEL_QA)
wolfSSL 11:cee25a834751 1403 return IntelQaSymSha384(&sha384->asyncDev, hash, NULL,
wolfSSL 11:cee25a834751 1404 SHA384_DIGEST_SIZE);
wolfSSL 11:cee25a834751 1405 #endif
wolfSSL 11:cee25a834751 1406 }
wolfSSL 11:cee25a834751 1407 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 1408
wolfSSL 11:cee25a834751 1409 ret = Sha512Final((Sha512*)sha384);
wolfSSL 11:cee25a834751 1410 if (ret != 0)
wolfSSL 11:cee25a834751 1411 return ret;
wolfSSL 11:cee25a834751 1412
wolfSSL 11:cee25a834751 1413 XMEMCPY(hash, sha384->digest, SHA384_DIGEST_SIZE);
wolfSSL 11:cee25a834751 1414
wolfSSL 11:cee25a834751 1415 return InitSha384(sha384); /* reset state */
wolfSSL 11:cee25a834751 1416 }
wolfSSL 11:cee25a834751 1417
wolfSSL 11:cee25a834751 1418
wolfSSL 11:cee25a834751 1419 int wc_InitSha384_ex(Sha384* sha384, void* heap, int devId)
wolfSSL 11:cee25a834751 1420 {
wolfSSL 11:cee25a834751 1421 int ret;
wolfSSL 11:cee25a834751 1422
wolfSSL 11:cee25a834751 1423 if (sha384 == NULL) {
wolfSSL 11:cee25a834751 1424 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1425 }
wolfSSL 11:cee25a834751 1426
wolfSSL 11:cee25a834751 1427 sha384->heap = heap;
wolfSSL 11:cee25a834751 1428 ret = InitSha384(sha384);
wolfSSL 11:cee25a834751 1429 if (ret != 0)
wolfSSL 11:cee25a834751 1430 return ret;
wolfSSL 11:cee25a834751 1431
wolfSSL 11:cee25a834751 1432 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
wolfSSL 11:cee25a834751 1433 ret = wolfAsync_DevCtxInit(&sha384->asyncDev, WOLFSSL_ASYNC_MARKER_SHA384,
wolfSSL 11:cee25a834751 1434 sha384->heap, devId);
wolfSSL 11:cee25a834751 1435 #else
wolfSSL 11:cee25a834751 1436 (void)devId;
wolfSSL 11:cee25a834751 1437 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 1438
wolfSSL 11:cee25a834751 1439 return ret;
wolfSSL 11:cee25a834751 1440 }
wolfSSL 11:cee25a834751 1441
wolfSSL 11:cee25a834751 1442 int wc_InitSha384(Sha384* sha384)
wolfSSL 11:cee25a834751 1443 {
wolfSSL 11:cee25a834751 1444 return wc_InitSha384_ex(sha384, NULL, INVALID_DEVID);
wolfSSL 11:cee25a834751 1445 }
wolfSSL 11:cee25a834751 1446
wolfSSL 11:cee25a834751 1447 void wc_Sha384Free(Sha384* sha384)
wolfSSL 11:cee25a834751 1448 {
wolfSSL 11:cee25a834751 1449 if (sha384 == NULL)
wolfSSL 11:cee25a834751 1450 return;
wolfSSL 11:cee25a834751 1451
wolfSSL 11:cee25a834751 1452 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
wolfSSL 11:cee25a834751 1453 wolfAsync_DevCtxFree(&sha384->asyncDev, WOLFSSL_ASYNC_MARKER_SHA384);
wolfSSL 11:cee25a834751 1454 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 1455 }
wolfSSL 11:cee25a834751 1456
wolfSSL 11:cee25a834751 1457 #endif /* WOLFSSL_SHA384 */
wolfSSL 11:cee25a834751 1458
wolfSSL 11:cee25a834751 1459 #endif /* HAVE_FIPS */
wolfSSL 11:cee25a834751 1460
wolfSSL 11:cee25a834751 1461
wolfSSL 11:cee25a834751 1462 int wc_Sha512GetHash(Sha512* sha512, byte* hash)
wolfSSL 11:cee25a834751 1463 {
wolfSSL 11:cee25a834751 1464 int ret;
wolfSSL 11:cee25a834751 1465 Sha512 tmpSha512;
wolfSSL 11:cee25a834751 1466
wolfSSL 11:cee25a834751 1467 if (sha512 == NULL || hash == NULL)
wolfSSL 11:cee25a834751 1468 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1469
wolfSSL 11:cee25a834751 1470 ret = wc_Sha512Copy(sha512, &tmpSha512);
wolfSSL 11:cee25a834751 1471 if (ret == 0) {
wolfSSL 11:cee25a834751 1472 ret = wc_Sha512Final(&tmpSha512, hash);
wolfSSL 11:cee25a834751 1473 }
wolfSSL 11:cee25a834751 1474 return ret;
wolfSSL 11:cee25a834751 1475 }
wolfSSL 11:cee25a834751 1476
wolfSSL 11:cee25a834751 1477 int wc_Sha512Copy(Sha512* src, Sha512* dst)
wolfSSL 11:cee25a834751 1478 {
wolfSSL 11:cee25a834751 1479 int ret = 0;
wolfSSL 11:cee25a834751 1480
wolfSSL 11:cee25a834751 1481 if (src == NULL || dst == NULL)
wolfSSL 11:cee25a834751 1482 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1483
wolfSSL 11:cee25a834751 1484 XMEMCPY(dst, src, sizeof(Sha512));
wolfSSL 11:cee25a834751 1485
wolfSSL 11:cee25a834751 1486 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 11:cee25a834751 1487 ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
wolfSSL 11:cee25a834751 1488 #endif
wolfSSL 11:cee25a834751 1489
wolfSSL 11:cee25a834751 1490 return ret;
wolfSSL 11:cee25a834751 1491 }
wolfSSL 11:cee25a834751 1492
wolfSSL 11:cee25a834751 1493 #ifdef WOLFSSL_SHA384
wolfSSL 11:cee25a834751 1494 int wc_Sha384GetHash(Sha384* sha384, byte* hash)
wolfSSL 11:cee25a834751 1495 {
wolfSSL 11:cee25a834751 1496 int ret;
wolfSSL 11:cee25a834751 1497 Sha384 tmpSha384;
wolfSSL 11:cee25a834751 1498
wolfSSL 11:cee25a834751 1499 if (sha384 == NULL || hash == NULL)
wolfSSL 11:cee25a834751 1500 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1501
wolfSSL 11:cee25a834751 1502 ret = wc_Sha384Copy(sha384, &tmpSha384);
wolfSSL 11:cee25a834751 1503 if (ret == 0) {
wolfSSL 11:cee25a834751 1504 ret = wc_Sha384Final(&tmpSha384, hash);
wolfSSL 11:cee25a834751 1505 }
wolfSSL 11:cee25a834751 1506 return ret;
wolfSSL 11:cee25a834751 1507 }
wolfSSL 11:cee25a834751 1508 int wc_Sha384Copy(Sha384* src, Sha384* dst)
wolfSSL 11:cee25a834751 1509 {
wolfSSL 11:cee25a834751 1510 int ret = 0;
wolfSSL 11:cee25a834751 1511
wolfSSL 11:cee25a834751 1512 if (src == NULL || dst == NULL)
wolfSSL 11:cee25a834751 1513 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1514
wolfSSL 11:cee25a834751 1515 XMEMCPY(dst, src, sizeof(Sha384));
wolfSSL 11:cee25a834751 1516
wolfSSL 11:cee25a834751 1517 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 11:cee25a834751 1518 ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
wolfSSL 11:cee25a834751 1519 #endif
wolfSSL 11:cee25a834751 1520
wolfSSL 11:cee25a834751 1521 return ret;
wolfSSL 11:cee25a834751 1522 }
wolfSSL 11:cee25a834751 1523 #endif /* WOLFSSL_SHA384 */
wolfSSL 11:cee25a834751 1524
wolfSSL 11:cee25a834751 1525 #endif /* WOLFSSL_SHA512 */
wolfSSL 11:cee25a834751 1526