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