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