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

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

Committer:
wolfSSL
Date:
Fri Jun 05 00:11:07 2020 +0000
Revision:
17:a5f916481144
Parent:
16:8e0d178b1d1e
wolfSSL 4.4.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* sha512.c
wolfSSL 15:117db924cf7c 2 *
wolfSSL 16:8e0d178b1d1e 3 * Copyright (C) 2006-2020 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22
wolfSSL 15:117db924cf7c 23 #ifdef HAVE_CONFIG_H
wolfSSL 15:117db924cf7c 24 #include <config.h>
wolfSSL 15:117db924cf7c 25 #endif
wolfSSL 15:117db924cf7c 26
wolfSSL 15:117db924cf7c 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 15:117db924cf7c 28
wolfSSL 16:8e0d178b1d1e 29 #if (defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)) && !defined(WOLFSSL_ARMASM)
wolfSSL 15:117db924cf7c 30
wolfSSL 15:117db924cf7c 31 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 32 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
wolfSSL 15:117db924cf7c 33
wolfSSL 15:117db924cf7c 34 /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
wolfSSL 15:117db924cf7c 35 #define FIPS_NO_WRAPPERS
wolfSSL 15:117db924cf7c 36
wolfSSL 15:117db924cf7c 37 #ifdef USE_WINDOWS_API
wolfSSL 15:117db924cf7c 38 #pragma code_seg(".fipsA$k")
wolfSSL 15:117db924cf7c 39 #pragma const_seg(".fipsB$k")
wolfSSL 15:117db924cf7c 40 #endif
wolfSSL 15:117db924cf7c 41 #endif
wolfSSL 15:117db924cf7c 42
wolfSSL 15:117db924cf7c 43 #include <wolfssl/wolfcrypt/sha512.h>
wolfSSL 15:117db924cf7c 44 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 15:117db924cf7c 45 #include <wolfssl/wolfcrypt/cpuid.h>
wolfSSL 16:8e0d178b1d1e 46 #include <wolfssl/wolfcrypt/hash.h>
wolfSSL 15:117db924cf7c 47
wolfSSL 15:117db924cf7c 48 /* deprecated USE_SLOW_SHA2 (replaced with USE_SLOW_SHA512) */
wolfSSL 15:117db924cf7c 49 #if defined(USE_SLOW_SHA2) && !defined(USE_SLOW_SHA512)
wolfSSL 15:117db924cf7c 50 #define USE_SLOW_SHA512
wolfSSL 15:117db924cf7c 51 #endif
wolfSSL 15:117db924cf7c 52
wolfSSL 15:117db924cf7c 53 /* fips wrapper calls, user can call direct */
wolfSSL 15:117db924cf7c 54 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 55 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
wolfSSL 15:117db924cf7c 56
wolfSSL 15:117db924cf7c 57 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 58
wolfSSL 15:117db924cf7c 59 int wc_InitSha512(wc_Sha512* sha)
wolfSSL 15:117db924cf7c 60 {
wolfSSL 15:117db924cf7c 61 if (sha == NULL) {
wolfSSL 15:117db924cf7c 62 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 63 }
wolfSSL 15:117db924cf7c 64
wolfSSL 15:117db924cf7c 65 return InitSha512_fips(sha);
wolfSSL 15:117db924cf7c 66 }
wolfSSL 15:117db924cf7c 67 int wc_InitSha512_ex(wc_Sha512* sha, void* heap, int devId)
wolfSSL 15:117db924cf7c 68 {
wolfSSL 15:117db924cf7c 69 (void)heap;
wolfSSL 15:117db924cf7c 70 (void)devId;
wolfSSL 15:117db924cf7c 71 if (sha == NULL) {
wolfSSL 15:117db924cf7c 72 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 73 }
wolfSSL 15:117db924cf7c 74 return InitSha512_fips(sha);
wolfSSL 15:117db924cf7c 75 }
wolfSSL 15:117db924cf7c 76 int wc_Sha512Update(wc_Sha512* sha, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 77 {
wolfSSL 15:117db924cf7c 78 if (sha == NULL || (data == NULL && len > 0)) {
wolfSSL 15:117db924cf7c 79 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 80 }
wolfSSL 15:117db924cf7c 81
wolfSSL 15:117db924cf7c 82 return Sha512Update_fips(sha, data, len);
wolfSSL 15:117db924cf7c 83 }
wolfSSL 15:117db924cf7c 84 int wc_Sha512Final(wc_Sha512* sha, byte* out)
wolfSSL 15:117db924cf7c 85 {
wolfSSL 15:117db924cf7c 86 if (sha == NULL || out == NULL) {
wolfSSL 15:117db924cf7c 87 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 88 }
wolfSSL 15:117db924cf7c 89
wolfSSL 15:117db924cf7c 90 return Sha512Final_fips(sha, out);
wolfSSL 15:117db924cf7c 91 }
wolfSSL 15:117db924cf7c 92 void wc_Sha512Free(wc_Sha512* sha)
wolfSSL 15:117db924cf7c 93 {
wolfSSL 15:117db924cf7c 94 (void)sha;
wolfSSL 15:117db924cf7c 95 /* Not supported in FIPS */
wolfSSL 15:117db924cf7c 96 }
wolfSSL 15:117db924cf7c 97 #endif
wolfSSL 15:117db924cf7c 98
wolfSSL 15:117db924cf7c 99 #if defined(WOLFSSL_SHA384) || defined(HAVE_AESGCM)
wolfSSL 15:117db924cf7c 100 int wc_InitSha384(wc_Sha384* sha)
wolfSSL 15:117db924cf7c 101 {
wolfSSL 15:117db924cf7c 102 if (sha == NULL) {
wolfSSL 15:117db924cf7c 103 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 104 }
wolfSSL 15:117db924cf7c 105 return InitSha384_fips(sha);
wolfSSL 15:117db924cf7c 106 }
wolfSSL 15:117db924cf7c 107 int wc_InitSha384_ex(wc_Sha384* sha, void* heap, int devId)
wolfSSL 15:117db924cf7c 108 {
wolfSSL 15:117db924cf7c 109 (void)heap;
wolfSSL 15:117db924cf7c 110 (void)devId;
wolfSSL 15:117db924cf7c 111 if (sha == NULL) {
wolfSSL 15:117db924cf7c 112 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 113 }
wolfSSL 15:117db924cf7c 114 return InitSha384_fips(sha);
wolfSSL 15:117db924cf7c 115 }
wolfSSL 15:117db924cf7c 116 int wc_Sha384Update(wc_Sha384* sha, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 117 {
wolfSSL 15:117db924cf7c 118 if (sha == NULL || (data == NULL && len > 0)) {
wolfSSL 15:117db924cf7c 119 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 120 }
wolfSSL 15:117db924cf7c 121 return Sha384Update_fips(sha, data, len);
wolfSSL 15:117db924cf7c 122 }
wolfSSL 15:117db924cf7c 123 int wc_Sha384Final(wc_Sha384* sha, byte* out)
wolfSSL 15:117db924cf7c 124 {
wolfSSL 15:117db924cf7c 125 if (sha == NULL || out == NULL) {
wolfSSL 15:117db924cf7c 126 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 127 }
wolfSSL 15:117db924cf7c 128 return Sha384Final_fips(sha, out);
wolfSSL 15:117db924cf7c 129 }
wolfSSL 15:117db924cf7c 130 void wc_Sha384Free(wc_Sha384* sha)
wolfSSL 15:117db924cf7c 131 {
wolfSSL 15:117db924cf7c 132 (void)sha;
wolfSSL 15:117db924cf7c 133 /* Not supported in FIPS */
wolfSSL 15:117db924cf7c 134 }
wolfSSL 15:117db924cf7c 135 #endif /* WOLFSSL_SHA384 || HAVE_AESGCM */
wolfSSL 15:117db924cf7c 136
wolfSSL 15:117db924cf7c 137 #else /* else build without fips, or for FIPS v2 */
wolfSSL 15:117db924cf7c 138
wolfSSL 15:117db924cf7c 139 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 15:117db924cf7c 140
wolfSSL 15:117db924cf7c 141 #ifdef NO_INLINE
wolfSSL 15:117db924cf7c 142 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 15:117db924cf7c 143 #else
wolfSSL 15:117db924cf7c 144 #define WOLFSSL_MISC_INCLUDED
wolfSSL 15:117db924cf7c 145 #include <wolfcrypt/src/misc.c>
wolfSSL 15:117db924cf7c 146 #endif
wolfSSL 15:117db924cf7c 147
wolfSSL 15:117db924cf7c 148
wolfSSL 15:117db924cf7c 149 #if defined(USE_INTEL_SPEEDUP)
wolfSSL 15:117db924cf7c 150 #if defined(__GNUC__) && ((__GNUC__ < 4) || \
wolfSSL 15:117db924cf7c 151 (__GNUC__ == 4 && __GNUC_MINOR__ <= 8))
wolfSSL 16:8e0d178b1d1e 152 #undef NO_AVX2_SUPPORT
wolfSSL 15:117db924cf7c 153 #define NO_AVX2_SUPPORT
wolfSSL 15:117db924cf7c 154 #endif
wolfSSL 15:117db924cf7c 155 #if defined(__clang__) && ((__clang_major__ < 3) || \
wolfSSL 15:117db924cf7c 156 (__clang_major__ == 3 && __clang_minor__ <= 5))
wolfSSL 15:117db924cf7c 157 #define NO_AVX2_SUPPORT
wolfSSL 15:117db924cf7c 158 #elif defined(__clang__) && defined(NO_AVX2_SUPPORT)
wolfSSL 15:117db924cf7c 159 #undef NO_AVX2_SUPPORT
wolfSSL 15:117db924cf7c 160 #endif
wolfSSL 15:117db924cf7c 161
wolfSSL 15:117db924cf7c 162 #define HAVE_INTEL_AVX1
wolfSSL 15:117db924cf7c 163 #ifndef NO_AVX2_SUPPORT
wolfSSL 15:117db924cf7c 164 #define HAVE_INTEL_AVX2
wolfSSL 15:117db924cf7c 165 #endif
wolfSSL 15:117db924cf7c 166 #endif
wolfSSL 15:117db924cf7c 167
wolfSSL 15:117db924cf7c 168 #if defined(HAVE_INTEL_AVX1)
wolfSSL 15:117db924cf7c 169 /* #define DEBUG_XMM */
wolfSSL 15:117db924cf7c 170 #endif
wolfSSL 15:117db924cf7c 171
wolfSSL 15:117db924cf7c 172 #if defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 173 #define HAVE_INTEL_RORX
wolfSSL 15:117db924cf7c 174 /* #define DEBUG_YMM */
wolfSSL 15:117db924cf7c 175 #endif
wolfSSL 15:117db924cf7c 176
wolfSSL 15:117db924cf7c 177 #if defined(HAVE_BYTEREVERSE64) && \
wolfSSL 15:117db924cf7c 178 !defined(HAVE_INTEL_AVX1) && !defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 179 #define ByteReverseWords64(out, in, size) ByteReverseWords64_1(out, size)
wolfSSL 15:117db924cf7c 180 #define ByteReverseWords64_1(buf, size) \
wolfSSL 15:117db924cf7c 181 { unsigned int i ;\
wolfSSL 15:117db924cf7c 182 for(i=0; i< size/sizeof(word64); i++){\
wolfSSL 15:117db924cf7c 183 __asm__ volatile("bswapq %0":"+r"(buf[i])::) ;\
wolfSSL 15:117db924cf7c 184 }\
wolfSSL 15:117db924cf7c 185 }
wolfSSL 15:117db924cf7c 186 #endif
wolfSSL 15:117db924cf7c 187
wolfSSL 15:117db924cf7c 188 #if defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH)
wolfSSL 15:117db924cf7c 189 /* functions defined in wolfcrypt/src/port/caam/caam_sha.c */
wolfSSL 16:8e0d178b1d1e 190
wolfSSL 15:117db924cf7c 191 #else
wolfSSL 15:117db924cf7c 192
wolfSSL 15:117db924cf7c 193 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 194
wolfSSL 15:117db924cf7c 195 static int InitSha512(wc_Sha512* sha512)
wolfSSL 15:117db924cf7c 196 {
wolfSSL 15:117db924cf7c 197 if (sha512 == NULL)
wolfSSL 15:117db924cf7c 198 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 199
wolfSSL 15:117db924cf7c 200 sha512->digest[0] = W64LIT(0x6a09e667f3bcc908);
wolfSSL 15:117db924cf7c 201 sha512->digest[1] = W64LIT(0xbb67ae8584caa73b);
wolfSSL 15:117db924cf7c 202 sha512->digest[2] = W64LIT(0x3c6ef372fe94f82b);
wolfSSL 15:117db924cf7c 203 sha512->digest[3] = W64LIT(0xa54ff53a5f1d36f1);
wolfSSL 15:117db924cf7c 204 sha512->digest[4] = W64LIT(0x510e527fade682d1);
wolfSSL 15:117db924cf7c 205 sha512->digest[5] = W64LIT(0x9b05688c2b3e6c1f);
wolfSSL 15:117db924cf7c 206 sha512->digest[6] = W64LIT(0x1f83d9abfb41bd6b);
wolfSSL 15:117db924cf7c 207 sha512->digest[7] = W64LIT(0x5be0cd19137e2179);
wolfSSL 15:117db924cf7c 208
wolfSSL 15:117db924cf7c 209 sha512->buffLen = 0;
wolfSSL 15:117db924cf7c 210 sha512->loLen = 0;
wolfSSL 15:117db924cf7c 211 sha512->hiLen = 0;
wolfSSL 15:117db924cf7c 212
wolfSSL 16:8e0d178b1d1e 213 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 214 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 215
wolfSSL 16:8e0d178b1d1e 216 sha512->ctx.sha_type = SHA2_512;
wolfSSL 16:8e0d178b1d1e 217 /* always start firstblock = 1 when using hw engine */
wolfSSL 16:8e0d178b1d1e 218 sha512->ctx.isfirstblock = 1;
wolfSSL 16:8e0d178b1d1e 219 if(sha512->ctx.mode == ESP32_SHA_HW) {
wolfSSL 16:8e0d178b1d1e 220 /* release hw */
wolfSSL 16:8e0d178b1d1e 221 esp_sha_hw_unlock();
wolfSSL 16:8e0d178b1d1e 222 }
wolfSSL 16:8e0d178b1d1e 223 /* always set mode as INIT
wolfSSL 16:8e0d178b1d1e 224 * whether using HW or SW is determined at first call of update()
wolfSSL 16:8e0d178b1d1e 225 */
wolfSSL 16:8e0d178b1d1e 226 sha512->ctx.mode = ESP32_SHA_INIT;
wolfSSL 16:8e0d178b1d1e 227 #endif
wolfSSL 16:8e0d178b1d1e 228 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 229 sha512->flags = 0;
wolfSSL 16:8e0d178b1d1e 230 #endif
wolfSSL 15:117db924cf7c 231 return 0;
wolfSSL 15:117db924cf7c 232 }
wolfSSL 15:117db924cf7c 233
wolfSSL 15:117db924cf7c 234 #endif /* WOLFSSL_SHA512 */
wolfSSL 15:117db924cf7c 235
wolfSSL 15:117db924cf7c 236 /* Hardware Acceleration */
wolfSSL 15:117db924cf7c 237 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 238
wolfSSL 15:117db924cf7c 239 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 240
wolfSSL 15:117db924cf7c 241 /*****
wolfSSL 15:117db924cf7c 242 Intel AVX1/AVX2 Macro Control Structure
wolfSSL 15:117db924cf7c 243
wolfSSL 15:117db924cf7c 244 #if defined(HAVE_INteL_SPEEDUP)
wolfSSL 15:117db924cf7c 245 #define HAVE_INTEL_AVX1
wolfSSL 15:117db924cf7c 246 #define HAVE_INTEL_AVX2
wolfSSL 15:117db924cf7c 247 #endif
wolfSSL 15:117db924cf7c 248
wolfSSL 15:117db924cf7c 249 int InitSha512(wc_Sha512* sha512) {
wolfSSL 15:117db924cf7c 250 Save/Recover XMM, YMM
wolfSSL 15:117db924cf7c 251 ...
wolfSSL 15:117db924cf7c 252
wolfSSL 15:117db924cf7c 253 Check Intel AVX cpuid flags
wolfSSL 15:117db924cf7c 254 }
wolfSSL 15:117db924cf7c 255
wolfSSL 15:117db924cf7c 256 #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 257 Transform_Sha512_AVX1(); # Function prototype
wolfSSL 15:117db924cf7c 258 Transform_Sha512_AVX2(); #
wolfSSL 15:117db924cf7c 259 #endif
wolfSSL 15:117db924cf7c 260
wolfSSL 15:117db924cf7c 261 _Transform_Sha512() { # Native Transform Function body
wolfSSL 15:117db924cf7c 262
wolfSSL 15:117db924cf7c 263 }
wolfSSL 15:117db924cf7c 264
wolfSSL 15:117db924cf7c 265 int Sha512Update() {
wolfSSL 15:117db924cf7c 266 Save/Recover XMM, YMM
wolfSSL 15:117db924cf7c 267 ...
wolfSSL 15:117db924cf7c 268 }
wolfSSL 15:117db924cf7c 269
wolfSSL 15:117db924cf7c 270 int Sha512Final() {
wolfSSL 15:117db924cf7c 271 Save/Recover XMM, YMM
wolfSSL 15:117db924cf7c 272 ...
wolfSSL 15:117db924cf7c 273 }
wolfSSL 15:117db924cf7c 274
wolfSSL 15:117db924cf7c 275
wolfSSL 15:117db924cf7c 276 #if defined(HAVE_INTEL_AVX1)
wolfSSL 15:117db924cf7c 277
wolfSSL 15:117db924cf7c 278 XMM Instructions/INLINE asm Definitions
wolfSSL 15:117db924cf7c 279
wolfSSL 15:117db924cf7c 280 #endif
wolfSSL 15:117db924cf7c 281
wolfSSL 15:117db924cf7c 282 #if defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 283
wolfSSL 15:117db924cf7c 284 YMM Instructions/INLINE asm Definitions
wolfSSL 15:117db924cf7c 285
wolfSSL 15:117db924cf7c 286 #endif
wolfSSL 15:117db924cf7c 287
wolfSSL 15:117db924cf7c 288 #if defnied(HAVE_INTEL_AVX1)
wolfSSL 15:117db924cf7c 289
wolfSSL 15:117db924cf7c 290 int Transform_Sha512_AVX1() {
wolfSSL 15:117db924cf7c 291 Stitched Message Sched/Round
wolfSSL 15:117db924cf7c 292 }
wolfSSL 15:117db924cf7c 293
wolfSSL 15:117db924cf7c 294 #endif
wolfSSL 15:117db924cf7c 295
wolfSSL 15:117db924cf7c 296 #if defnied(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 297
wolfSSL 15:117db924cf7c 298 int Transform_Sha512_AVX2() {
wolfSSL 15:117db924cf7c 299 Stitched Message Sched/Round
wolfSSL 15:117db924cf7c 300 }
wolfSSL 15:117db924cf7c 301 #endif
wolfSSL 15:117db924cf7c 302
wolfSSL 15:117db924cf7c 303 */
wolfSSL 15:117db924cf7c 304
wolfSSL 15:117db924cf7c 305
wolfSSL 15:117db924cf7c 306 /* Each platform needs to query info type 1 from cpuid to see if aesni is
wolfSSL 15:117db924cf7c 307 * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
wolfSSL 15:117db924cf7c 308 */
wolfSSL 15:117db924cf7c 309
wolfSSL 16:8e0d178b1d1e 310 #ifdef __cplusplus
wolfSSL 16:8e0d178b1d1e 311 extern "C" {
wolfSSL 16:8e0d178b1d1e 312 #endif
wolfSSL 16:8e0d178b1d1e 313
wolfSSL 15:117db924cf7c 314 #if defined(HAVE_INTEL_AVX1)
wolfSSL 16:8e0d178b1d1e 315 extern int Transform_Sha512_AVX1(wc_Sha512 *sha512);
wolfSSL 16:8e0d178b1d1e 316 extern int Transform_Sha512_AVX1_Len(wc_Sha512 *sha512, word32 len);
wolfSSL 15:117db924cf7c 317 #endif
wolfSSL 15:117db924cf7c 318 #if defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 319 extern int Transform_Sha512_AVX2(wc_Sha512 *sha512);
wolfSSL 16:8e0d178b1d1e 320 extern int Transform_Sha512_AVX2_Len(wc_Sha512 *sha512, word32 len);
wolfSSL 15:117db924cf7c 321 #if defined(HAVE_INTEL_RORX)
wolfSSL 16:8e0d178b1d1e 322 extern int Transform_Sha512_AVX1_RORX(wc_Sha512 *sha512);
wolfSSL 16:8e0d178b1d1e 323 extern int Transform_Sha512_AVX1_RORX_Len(wc_Sha512 *sha512,
wolfSSL 15:117db924cf7c 324 word32 len);
wolfSSL 16:8e0d178b1d1e 325 extern int Transform_Sha512_AVX2_RORX(wc_Sha512 *sha512);
wolfSSL 16:8e0d178b1d1e 326 extern int Transform_Sha512_AVX2_RORX_Len(wc_Sha512 *sha512,
wolfSSL 15:117db924cf7c 327 word32 len);
wolfSSL 15:117db924cf7c 328 #endif
wolfSSL 15:117db924cf7c 329 #endif
wolfSSL 16:8e0d178b1d1e 330
wolfSSL 16:8e0d178b1d1e 331 #ifdef __cplusplus
wolfSSL 16:8e0d178b1d1e 332 } /* extern "C" */
wolfSSL 16:8e0d178b1d1e 333 #endif
wolfSSL 16:8e0d178b1d1e 334
wolfSSL 15:117db924cf7c 335 static int _Transform_Sha512(wc_Sha512 *sha512);
wolfSSL 15:117db924cf7c 336 static int (*Transform_Sha512_p)(wc_Sha512* sha512) = _Transform_Sha512;
wolfSSL 15:117db924cf7c 337 static int (*Transform_Sha512_Len_p)(wc_Sha512* sha512, word32 len) = NULL;
wolfSSL 15:117db924cf7c 338 static int transform_check = 0;
wolfSSL 15:117db924cf7c 339 static int intel_flags;
wolfSSL 15:117db924cf7c 340 #define Transform_Sha512(sha512) (*Transform_Sha512_p)(sha512)
wolfSSL 15:117db924cf7c 341 #define Transform_Sha512_Len(sha512, len) \
wolfSSL 16:8e0d178b1d1e 342 (*Transform_Sha512_Len_p)(sha512, len)
wolfSSL 15:117db924cf7c 343
wolfSSL 15:117db924cf7c 344 static void Sha512_SetTransform()
wolfSSL 15:117db924cf7c 345 {
wolfSSL 15:117db924cf7c 346 if (transform_check)
wolfSSL 15:117db924cf7c 347 return;
wolfSSL 15:117db924cf7c 348
wolfSSL 15:117db924cf7c 349 intel_flags = cpuid_get_flags();
wolfSSL 15:117db924cf7c 350
wolfSSL 15:117db924cf7c 351 #if defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 352 if (IS_INTEL_AVX2(intel_flags)) {
wolfSSL 15:117db924cf7c 353 #ifdef HAVE_INTEL_RORX
wolfSSL 15:117db924cf7c 354 if (IS_INTEL_BMI2(intel_flags)) {
wolfSSL 15:117db924cf7c 355 Transform_Sha512_p = Transform_Sha512_AVX2_RORX;
wolfSSL 15:117db924cf7c 356 Transform_Sha512_Len_p = Transform_Sha512_AVX2_RORX_Len;
wolfSSL 15:117db924cf7c 357 }
wolfSSL 15:117db924cf7c 358 else
wolfSSL 15:117db924cf7c 359 #endif
wolfSSL 15:117db924cf7c 360 if (1) {
wolfSSL 15:117db924cf7c 361 Transform_Sha512_p = Transform_Sha512_AVX2;
wolfSSL 15:117db924cf7c 362 Transform_Sha512_Len_p = Transform_Sha512_AVX2_Len;
wolfSSL 15:117db924cf7c 363 }
wolfSSL 15:117db924cf7c 364 #ifdef HAVE_INTEL_RORX
wolfSSL 15:117db924cf7c 365 else {
wolfSSL 15:117db924cf7c 366 Transform_Sha512_p = Transform_Sha512_AVX1_RORX;
wolfSSL 15:117db924cf7c 367 Transform_Sha512_Len_p = Transform_Sha512_AVX1_RORX_Len;
wolfSSL 15:117db924cf7c 368 }
wolfSSL 15:117db924cf7c 369 #endif
wolfSSL 15:117db924cf7c 370 }
wolfSSL 15:117db924cf7c 371 else
wolfSSL 15:117db924cf7c 372 #endif
wolfSSL 15:117db924cf7c 373 #if defined(HAVE_INTEL_AVX1)
wolfSSL 15:117db924cf7c 374 if (IS_INTEL_AVX1(intel_flags)) {
wolfSSL 15:117db924cf7c 375 Transform_Sha512_p = Transform_Sha512_AVX1;
wolfSSL 15:117db924cf7c 376 Transform_Sha512_Len_p = Transform_Sha512_AVX1_Len;
wolfSSL 15:117db924cf7c 377 }
wolfSSL 15:117db924cf7c 378 else
wolfSSL 15:117db924cf7c 379 #endif
wolfSSL 15:117db924cf7c 380 Transform_Sha512_p = _Transform_Sha512;
wolfSSL 15:117db924cf7c 381
wolfSSL 15:117db924cf7c 382 transform_check = 1;
wolfSSL 15:117db924cf7c 383 }
wolfSSL 15:117db924cf7c 384 #endif /* WOLFSSL_SHA512 */
wolfSSL 15:117db924cf7c 385
wolfSSL 15:117db924cf7c 386 #else
wolfSSL 15:117db924cf7c 387 #define Transform_Sha512(sha512) _Transform_Sha512(sha512)
wolfSSL 15:117db924cf7c 388
wolfSSL 16:8e0d178b1d1e 389 #endif
wolfSSL 16:8e0d178b1d1e 390
wolfSSL 16:8e0d178b1d1e 391 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 392
wolfSSL 16:8e0d178b1d1e 393 int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
wolfSSL 16:8e0d178b1d1e 394 {
wolfSSL 16:8e0d178b1d1e 395 int ret = 0;
wolfSSL 15:117db924cf7c 396
wolfSSL 16:8e0d178b1d1e 397 if (sha512 == NULL)
wolfSSL 16:8e0d178b1d1e 398 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 399
wolfSSL 16:8e0d178b1d1e 400 sha512->heap = heap;
wolfSSL 15:117db924cf7c 401
wolfSSL 16:8e0d178b1d1e 402 ret = InitSha512(sha512);
wolfSSL 16:8e0d178b1d1e 403 if (ret != 0)
wolfSSL 16:8e0d178b1d1e 404 return ret;
wolfSSL 15:117db924cf7c 405
wolfSSL 16:8e0d178b1d1e 406 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 407 Sha512_SetTransform();
wolfSSL 16:8e0d178b1d1e 408 #endif
wolfSSL 16:8e0d178b1d1e 409
wolfSSL 16:8e0d178b1d1e 410 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 16:8e0d178b1d1e 411 sha512->W = NULL;
wolfSSL 16:8e0d178b1d1e 412 #endif
wolfSSL 15:117db924cf7c 413
wolfSSL 16:8e0d178b1d1e 414 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
wolfSSL 16:8e0d178b1d1e 415 ret = wolfAsync_DevCtxInit(&sha512->asyncDev,
wolfSSL 16:8e0d178b1d1e 416 WOLFSSL_ASYNC_MARKER_SHA512, sha512->heap, devId);
wolfSSL 16:8e0d178b1d1e 417 #else
wolfSSL 16:8e0d178b1d1e 418 (void)devId;
wolfSSL 16:8e0d178b1d1e 419 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 420
wolfSSL 16:8e0d178b1d1e 421 return ret;
wolfSSL 16:8e0d178b1d1e 422 }
wolfSSL 15:117db924cf7c 423
wolfSSL 16:8e0d178b1d1e 424 #endif /* WOLFSSL_SHA512 */
wolfSSL 15:117db924cf7c 425
wolfSSL 15:117db924cf7c 426
wolfSSL 15:117db924cf7c 427 static const word64 K512[80] = {
wolfSSL 15:117db924cf7c 428 W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd),
wolfSSL 15:117db924cf7c 429 W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc),
wolfSSL 15:117db924cf7c 430 W64LIT(0x3956c25bf348b538), W64LIT(0x59f111f1b605d019),
wolfSSL 15:117db924cf7c 431 W64LIT(0x923f82a4af194f9b), W64LIT(0xab1c5ed5da6d8118),
wolfSSL 15:117db924cf7c 432 W64LIT(0xd807aa98a3030242), W64LIT(0x12835b0145706fbe),
wolfSSL 15:117db924cf7c 433 W64LIT(0x243185be4ee4b28c), W64LIT(0x550c7dc3d5ffb4e2),
wolfSSL 15:117db924cf7c 434 W64LIT(0x72be5d74f27b896f), W64LIT(0x80deb1fe3b1696b1),
wolfSSL 15:117db924cf7c 435 W64LIT(0x9bdc06a725c71235), W64LIT(0xc19bf174cf692694),
wolfSSL 15:117db924cf7c 436 W64LIT(0xe49b69c19ef14ad2), W64LIT(0xefbe4786384f25e3),
wolfSSL 15:117db924cf7c 437 W64LIT(0x0fc19dc68b8cd5b5), W64LIT(0x240ca1cc77ac9c65),
wolfSSL 15:117db924cf7c 438 W64LIT(0x2de92c6f592b0275), W64LIT(0x4a7484aa6ea6e483),
wolfSSL 15:117db924cf7c 439 W64LIT(0x5cb0a9dcbd41fbd4), W64LIT(0x76f988da831153b5),
wolfSSL 15:117db924cf7c 440 W64LIT(0x983e5152ee66dfab), W64LIT(0xa831c66d2db43210),
wolfSSL 15:117db924cf7c 441 W64LIT(0xb00327c898fb213f), W64LIT(0xbf597fc7beef0ee4),
wolfSSL 15:117db924cf7c 442 W64LIT(0xc6e00bf33da88fc2), W64LIT(0xd5a79147930aa725),
wolfSSL 15:117db924cf7c 443 W64LIT(0x06ca6351e003826f), W64LIT(0x142929670a0e6e70),
wolfSSL 15:117db924cf7c 444 W64LIT(0x27b70a8546d22ffc), W64LIT(0x2e1b21385c26c926),
wolfSSL 15:117db924cf7c 445 W64LIT(0x4d2c6dfc5ac42aed), W64LIT(0x53380d139d95b3df),
wolfSSL 15:117db924cf7c 446 W64LIT(0x650a73548baf63de), W64LIT(0x766a0abb3c77b2a8),
wolfSSL 15:117db924cf7c 447 W64LIT(0x81c2c92e47edaee6), W64LIT(0x92722c851482353b),
wolfSSL 15:117db924cf7c 448 W64LIT(0xa2bfe8a14cf10364), W64LIT(0xa81a664bbc423001),
wolfSSL 15:117db924cf7c 449 W64LIT(0xc24b8b70d0f89791), W64LIT(0xc76c51a30654be30),
wolfSSL 15:117db924cf7c 450 W64LIT(0xd192e819d6ef5218), W64LIT(0xd69906245565a910),
wolfSSL 15:117db924cf7c 451 W64LIT(0xf40e35855771202a), W64LIT(0x106aa07032bbd1b8),
wolfSSL 15:117db924cf7c 452 W64LIT(0x19a4c116b8d2d0c8), W64LIT(0x1e376c085141ab53),
wolfSSL 15:117db924cf7c 453 W64LIT(0x2748774cdf8eeb99), W64LIT(0x34b0bcb5e19b48a8),
wolfSSL 15:117db924cf7c 454 W64LIT(0x391c0cb3c5c95a63), W64LIT(0x4ed8aa4ae3418acb),
wolfSSL 15:117db924cf7c 455 W64LIT(0x5b9cca4f7763e373), W64LIT(0x682e6ff3d6b2b8a3),
wolfSSL 15:117db924cf7c 456 W64LIT(0x748f82ee5defb2fc), W64LIT(0x78a5636f43172f60),
wolfSSL 15:117db924cf7c 457 W64LIT(0x84c87814a1f0ab72), W64LIT(0x8cc702081a6439ec),
wolfSSL 15:117db924cf7c 458 W64LIT(0x90befffa23631e28), W64LIT(0xa4506cebde82bde9),
wolfSSL 15:117db924cf7c 459 W64LIT(0xbef9a3f7b2c67915), W64LIT(0xc67178f2e372532b),
wolfSSL 15:117db924cf7c 460 W64LIT(0xca273eceea26619c), W64LIT(0xd186b8c721c0c207),
wolfSSL 15:117db924cf7c 461 W64LIT(0xeada7dd6cde0eb1e), W64LIT(0xf57d4f7fee6ed178),
wolfSSL 15:117db924cf7c 462 W64LIT(0x06f067aa72176fba), W64LIT(0x0a637dc5a2c898a6),
wolfSSL 15:117db924cf7c 463 W64LIT(0x113f9804bef90dae), W64LIT(0x1b710b35131c471b),
wolfSSL 15:117db924cf7c 464 W64LIT(0x28db77f523047d84), W64LIT(0x32caab7b40c72493),
wolfSSL 15:117db924cf7c 465 W64LIT(0x3c9ebe0a15c9bebc), W64LIT(0x431d67c49c100d4c),
wolfSSL 15:117db924cf7c 466 W64LIT(0x4cc5d4becb3e42b6), W64LIT(0x597f299cfc657e2a),
wolfSSL 15:117db924cf7c 467 W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817)
wolfSSL 15:117db924cf7c 468 };
wolfSSL 15:117db924cf7c 469
wolfSSL 15:117db924cf7c 470 #define blk0(i) (W[i] = sha512->buffer[i])
wolfSSL 15:117db924cf7c 471
wolfSSL 15:117db924cf7c 472 #define blk2(i) (\
wolfSSL 15:117db924cf7c 473 W[ i & 15] += \
wolfSSL 15:117db924cf7c 474 s1(W[(i-2) & 15])+ \
wolfSSL 15:117db924cf7c 475 W[(i-7) & 15] + \
wolfSSL 15:117db924cf7c 476 s0(W[(i-15) & 15]) \
wolfSSL 15:117db924cf7c 477 )
wolfSSL 15:117db924cf7c 478
wolfSSL 15:117db924cf7c 479 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
wolfSSL 15:117db924cf7c 480 #define Maj(x,y,z) ((x & y) | (z & (x | y)))
wolfSSL 15:117db924cf7c 481
wolfSSL 15:117db924cf7c 482 #define a(i) T[(0-i) & 7]
wolfSSL 15:117db924cf7c 483 #define b(i) T[(1-i) & 7]
wolfSSL 15:117db924cf7c 484 #define c(i) T[(2-i) & 7]
wolfSSL 15:117db924cf7c 485 #define d(i) T[(3-i) & 7]
wolfSSL 15:117db924cf7c 486 #define e(i) T[(4-i) & 7]
wolfSSL 15:117db924cf7c 487 #define f(i) T[(5-i) & 7]
wolfSSL 15:117db924cf7c 488 #define g(i) T[(6-i) & 7]
wolfSSL 15:117db924cf7c 489 #define h(i) T[(7-i) & 7]
wolfSSL 15:117db924cf7c 490
wolfSSL 15:117db924cf7c 491 #define S0(x) (rotrFixed64(x,28) ^ rotrFixed64(x,34) ^ rotrFixed64(x,39))
wolfSSL 15:117db924cf7c 492 #define S1(x) (rotrFixed64(x,14) ^ rotrFixed64(x,18) ^ rotrFixed64(x,41))
wolfSSL 15:117db924cf7c 493 #define s0(x) (rotrFixed64(x,1) ^ rotrFixed64(x,8) ^ (x>>7))
wolfSSL 15:117db924cf7c 494 #define s1(x) (rotrFixed64(x,19) ^ rotrFixed64(x,61) ^ (x>>6))
wolfSSL 15:117db924cf7c 495
wolfSSL 15:117db924cf7c 496 #define R(i) \
wolfSSL 15:117db924cf7c 497 h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[i+j] + (j ? blk2(i) : blk0(i)); \
wolfSSL 15:117db924cf7c 498 d(i) += h(i); \
wolfSSL 15:117db924cf7c 499 h(i) += S0(a(i)) + Maj(a(i),b(i),c(i))
wolfSSL 15:117db924cf7c 500
wolfSSL 15:117db924cf7c 501 static int _Transform_Sha512(wc_Sha512* sha512)
wolfSSL 15:117db924cf7c 502 {
wolfSSL 15:117db924cf7c 503 const word64* K = K512;
wolfSSL 15:117db924cf7c 504 word32 j;
wolfSSL 15:117db924cf7c 505 word64 T[8];
wolfSSL 15:117db924cf7c 506
wolfSSL 15:117db924cf7c 507 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 508 word64* W = sha512->W;
wolfSSL 15:117db924cf7c 509 if (W == NULL) {
wolfSSL 15:117db924cf7c 510 W = (word64*) XMALLOC(sizeof(word64) * 16, NULL,
wolfSSL 15:117db924cf7c 511 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 512 if (W == NULL)
wolfSSL 15:117db924cf7c 513 return MEMORY_E;
wolfSSL 15:117db924cf7c 514 sha512->W = W;
wolfSSL 15:117db924cf7c 515 }
wolfSSL 15:117db924cf7c 516 #elif defined(WOLFSSL_SMALL_STACK)
wolfSSL 15:117db924cf7c 517 word64* W;
wolfSSL 15:117db924cf7c 518 W = (word64*) XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 519 if (W == NULL)
wolfSSL 15:117db924cf7c 520 return MEMORY_E;
wolfSSL 15:117db924cf7c 521 #else
wolfSSL 15:117db924cf7c 522 word64 W[16];
wolfSSL 15:117db924cf7c 523 #endif
wolfSSL 15:117db924cf7c 524
wolfSSL 15:117db924cf7c 525 /* Copy digest to working vars */
wolfSSL 15:117db924cf7c 526 XMEMCPY(T, sha512->digest, sizeof(T));
wolfSSL 15:117db924cf7c 527
wolfSSL 15:117db924cf7c 528 #ifdef USE_SLOW_SHA512
wolfSSL 15:117db924cf7c 529 /* over twice as small, but 50% slower */
wolfSSL 15:117db924cf7c 530 /* 80 operations, not unrolled */
wolfSSL 15:117db924cf7c 531 for (j = 0; j < 80; j += 16) {
wolfSSL 15:117db924cf7c 532 int m;
wolfSSL 15:117db924cf7c 533 for (m = 0; m < 16; m++) { /* braces needed here for macros {} */
wolfSSL 15:117db924cf7c 534 R(m);
wolfSSL 15:117db924cf7c 535 }
wolfSSL 15:117db924cf7c 536 }
wolfSSL 15:117db924cf7c 537 #else
wolfSSL 15:117db924cf7c 538 /* 80 operations, partially loop unrolled */
wolfSSL 15:117db924cf7c 539 for (j = 0; j < 80; j += 16) {
wolfSSL 15:117db924cf7c 540 R( 0); R( 1); R( 2); R( 3);
wolfSSL 15:117db924cf7c 541 R( 4); R( 5); R( 6); R( 7);
wolfSSL 15:117db924cf7c 542 R( 8); R( 9); R(10); R(11);
wolfSSL 15:117db924cf7c 543 R(12); R(13); R(14); R(15);
wolfSSL 15:117db924cf7c 544 }
wolfSSL 15:117db924cf7c 545 #endif /* USE_SLOW_SHA512 */
wolfSSL 15:117db924cf7c 546
wolfSSL 15:117db924cf7c 547 /* Add the working vars back into digest */
wolfSSL 15:117db924cf7c 548 sha512->digest[0] += a(0);
wolfSSL 15:117db924cf7c 549 sha512->digest[1] += b(0);
wolfSSL 15:117db924cf7c 550 sha512->digest[2] += c(0);
wolfSSL 15:117db924cf7c 551 sha512->digest[3] += d(0);
wolfSSL 15:117db924cf7c 552 sha512->digest[4] += e(0);
wolfSSL 15:117db924cf7c 553 sha512->digest[5] += f(0);
wolfSSL 15:117db924cf7c 554 sha512->digest[6] += g(0);
wolfSSL 15:117db924cf7c 555 sha512->digest[7] += h(0);
wolfSSL 15:117db924cf7c 556
wolfSSL 15:117db924cf7c 557 /* Wipe variables */
wolfSSL 15:117db924cf7c 558 ForceZero(W, sizeof(word64) * 16);
wolfSSL 15:117db924cf7c 559 ForceZero(T, sizeof(T));
wolfSSL 15:117db924cf7c 560
wolfSSL 15:117db924cf7c 561 #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE)
wolfSSL 15:117db924cf7c 562 XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 563 #endif
wolfSSL 15:117db924cf7c 564
wolfSSL 15:117db924cf7c 565 return 0;
wolfSSL 15:117db924cf7c 566 }
wolfSSL 15:117db924cf7c 567
wolfSSL 15:117db924cf7c 568
wolfSSL 15:117db924cf7c 569 static WC_INLINE void AddLength(wc_Sha512* sha512, word32 len)
wolfSSL 15:117db924cf7c 570 {
wolfSSL 15:117db924cf7c 571 word64 tmp = sha512->loLen;
wolfSSL 15:117db924cf7c 572 if ( (sha512->loLen += len) < tmp)
wolfSSL 15:117db924cf7c 573 sha512->hiLen++; /* carry low to high */
wolfSSL 15:117db924cf7c 574 }
wolfSSL 15:117db924cf7c 575
wolfSSL 15:117db924cf7c 576 static WC_INLINE int Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 577 {
wolfSSL 15:117db924cf7c 578 int ret = 0;
wolfSSL 15:117db924cf7c 579 /* do block size increments */
wolfSSL 15:117db924cf7c 580 byte* local = (byte*)sha512->buffer;
wolfSSL 15:117db924cf7c 581
wolfSSL 15:117db924cf7c 582 /* check that internal buffLen is valid */
wolfSSL 15:117db924cf7c 583 if (sha512->buffLen >= WC_SHA512_BLOCK_SIZE)
wolfSSL 15:117db924cf7c 584 return BUFFER_E;
wolfSSL 15:117db924cf7c 585
wolfSSL 16:8e0d178b1d1e 586 AddLength(sha512, len);
wolfSSL 16:8e0d178b1d1e 587
wolfSSL 15:117db924cf7c 588 if (sha512->buffLen > 0) {
wolfSSL 15:117db924cf7c 589 word32 add = min(len, WC_SHA512_BLOCK_SIZE - sha512->buffLen);
wolfSSL 15:117db924cf7c 590 if (add > 0) {
wolfSSL 15:117db924cf7c 591 XMEMCPY(&local[sha512->buffLen], data, add);
wolfSSL 15:117db924cf7c 592
wolfSSL 15:117db924cf7c 593 sha512->buffLen += add;
wolfSSL 15:117db924cf7c 594 data += add;
wolfSSL 15:117db924cf7c 595 len -= add;
wolfSSL 15:117db924cf7c 596 }
wolfSSL 15:117db924cf7c 597
wolfSSL 15:117db924cf7c 598 if (sha512->buffLen == WC_SHA512_BLOCK_SIZE) {
wolfSSL 15:117db924cf7c 599 #if defined(LITTLE_ENDIAN_ORDER)
wolfSSL 15:117db924cf7c 600 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 601 if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
wolfSSL 15:117db924cf7c 602 #endif
wolfSSL 15:117db924cf7c 603 {
wolfSSL 16:8e0d178b1d1e 604 #if !defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
wolfSSL 16:8e0d178b1d1e 605 defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 15:117db924cf7c 606 ByteReverseWords64(sha512->buffer, sha512->buffer,
wolfSSL 16:8e0d178b1d1e 607 WC_SHA512_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 608 #endif
wolfSSL 15:117db924cf7c 609 }
wolfSSL 15:117db924cf7c 610 #endif
wolfSSL 16:8e0d178b1d1e 611 #if !defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
wolfSSL 16:8e0d178b1d1e 612 defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 15:117db924cf7c 613 ret = Transform_Sha512(sha512);
wolfSSL 16:8e0d178b1d1e 614 #else
wolfSSL 16:8e0d178b1d1e 615 if(sha512->ctx.mode == ESP32_SHA_INIT) {
wolfSSL 16:8e0d178b1d1e 616 esp_sha_try_hw_lock(&sha512->ctx);
wolfSSL 16:8e0d178b1d1e 617 }
wolfSSL 16:8e0d178b1d1e 618 ret = esp_sha512_process(sha512);
wolfSSL 16:8e0d178b1d1e 619 if(ret == 0 && sha512->ctx.mode == ESP32_SHA_SW){
wolfSSL 16:8e0d178b1d1e 620 ret = Transform_Sha512(sha512);
wolfSSL 16:8e0d178b1d1e 621 }
wolfSSL 16:8e0d178b1d1e 622 #endif
wolfSSL 16:8e0d178b1d1e 623 if (ret == 0)
wolfSSL 15:117db924cf7c 624 sha512->buffLen = 0;
wolfSSL 15:117db924cf7c 625 else
wolfSSL 15:117db924cf7c 626 len = 0;
wolfSSL 15:117db924cf7c 627 }
wolfSSL 15:117db924cf7c 628 }
wolfSSL 15:117db924cf7c 629
wolfSSL 15:117db924cf7c 630 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 631 if (Transform_Sha512_Len_p != NULL) {
wolfSSL 15:117db924cf7c 632 word32 blocksLen = len & ~(WC_SHA512_BLOCK_SIZE-1);
wolfSSL 15:117db924cf7c 633
wolfSSL 15:117db924cf7c 634 if (blocksLen > 0) {
wolfSSL 15:117db924cf7c 635 sha512->data = data;
wolfSSL 15:117db924cf7c 636 /* Byte reversal performed in function if required. */
wolfSSL 15:117db924cf7c 637 Transform_Sha512_Len(sha512, blocksLen);
wolfSSL 15:117db924cf7c 638 data += blocksLen;
wolfSSL 15:117db924cf7c 639 len -= blocksLen;
wolfSSL 15:117db924cf7c 640 }
wolfSSL 15:117db924cf7c 641 }
wolfSSL 15:117db924cf7c 642 else
wolfSSL 15:117db924cf7c 643 #endif
wolfSSL 16:8e0d178b1d1e 644 #if !defined(LITTLE_ENDIAN_ORDER) || defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 645 {
wolfSSL 15:117db924cf7c 646 while (len >= WC_SHA512_BLOCK_SIZE) {
wolfSSL 15:117db924cf7c 647 XMEMCPY(local, data, WC_SHA512_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 648
wolfSSL 15:117db924cf7c 649 data += WC_SHA512_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 650 len -= WC_SHA512_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 651
wolfSSL 16:8e0d178b1d1e 652 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 653 if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
wolfSSL 16:8e0d178b1d1e 654 {
wolfSSL 16:8e0d178b1d1e 655 ByteReverseWords64(sha512->buffer, sha512->buffer,
wolfSSL 16:8e0d178b1d1e 656 WC_SHA512_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 657 }
wolfSSL 16:8e0d178b1d1e 658 #endif
wolfSSL 15:117db924cf7c 659 /* Byte reversal performed in function if required. */
wolfSSL 15:117db924cf7c 660 ret = Transform_Sha512(sha512);
wolfSSL 15:117db924cf7c 661 if (ret != 0)
wolfSSL 15:117db924cf7c 662 break;
wolfSSL 15:117db924cf7c 663 }
wolfSSL 15:117db924cf7c 664 }
wolfSSL 15:117db924cf7c 665 #else
wolfSSL 15:117db924cf7c 666 {
wolfSSL 15:117db924cf7c 667 while (len >= WC_SHA512_BLOCK_SIZE) {
wolfSSL 15:117db924cf7c 668 XMEMCPY(local, data, WC_SHA512_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 669
wolfSSL 15:117db924cf7c 670 data += WC_SHA512_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 671 len -= WC_SHA512_BLOCK_SIZE;
wolfSSL 16:8e0d178b1d1e 672 #if !defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
wolfSSL 16:8e0d178b1d1e 673 defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 15:117db924cf7c 674 ByteReverseWords64(sha512->buffer, sha512->buffer,
wolfSSL 16:8e0d178b1d1e 675 WC_SHA512_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 676 #endif
wolfSSL 16:8e0d178b1d1e 677 #if !defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
wolfSSL 16:8e0d178b1d1e 678 defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 15:117db924cf7c 679 ret = Transform_Sha512(sha512);
wolfSSL 16:8e0d178b1d1e 680 #else
wolfSSL 16:8e0d178b1d1e 681 if(sha512->ctx.mode == ESP32_SHA_INIT) {
wolfSSL 16:8e0d178b1d1e 682 esp_sha_try_hw_lock(&sha512->ctx);
wolfSSL 16:8e0d178b1d1e 683 }
wolfSSL 16:8e0d178b1d1e 684 ret = esp_sha512_process(sha512);
wolfSSL 16:8e0d178b1d1e 685 if(ret == 0 && sha512->ctx.mode == ESP32_SHA_SW){
wolfSSL 16:8e0d178b1d1e 686 ret = Transform_Sha512(sha512);
wolfSSL 16:8e0d178b1d1e 687 }
wolfSSL 16:8e0d178b1d1e 688 #endif
wolfSSL 15:117db924cf7c 689 if (ret != 0)
wolfSSL 15:117db924cf7c 690 break;
wolfSSL 15:117db924cf7c 691 }
wolfSSL 15:117db924cf7c 692 }
wolfSSL 15:117db924cf7c 693 #endif
wolfSSL 15:117db924cf7c 694
wolfSSL 15:117db924cf7c 695 if (len > 0) {
wolfSSL 15:117db924cf7c 696 XMEMCPY(local, data, len);
wolfSSL 15:117db924cf7c 697 sha512->buffLen = len;
wolfSSL 15:117db924cf7c 698 }
wolfSSL 15:117db924cf7c 699
wolfSSL 15:117db924cf7c 700 return ret;
wolfSSL 15:117db924cf7c 701 }
wolfSSL 15:117db924cf7c 702
wolfSSL 15:117db924cf7c 703 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 704
wolfSSL 15:117db924cf7c 705 int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 706 {
wolfSSL 15:117db924cf7c 707 if (sha512 == NULL || (data == NULL && len > 0)) {
wolfSSL 15:117db924cf7c 708 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 709 }
wolfSSL 15:117db924cf7c 710
wolfSSL 15:117db924cf7c 711 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
wolfSSL 15:117db924cf7c 712 if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
wolfSSL 15:117db924cf7c 713 #if defined(HAVE_INTEL_QA)
wolfSSL 15:117db924cf7c 714 return IntelQaSymSha512(&sha512->asyncDev, NULL, data, len);
wolfSSL 15:117db924cf7c 715 #endif
wolfSSL 15:117db924cf7c 716 }
wolfSSL 15:117db924cf7c 717 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 718
wolfSSL 15:117db924cf7c 719 return Sha512Update(sha512, data, len);
wolfSSL 15:117db924cf7c 720 }
wolfSSL 15:117db924cf7c 721
wolfSSL 15:117db924cf7c 722 #endif /* WOLFSSL_SHA512 */
wolfSSL 15:117db924cf7c 723
wolfSSL 15:117db924cf7c 724 #endif /* WOLFSSL_IMX6_CAAM */
wolfSSL 15:117db924cf7c 725
wolfSSL 15:117db924cf7c 726 static WC_INLINE int Sha512Final(wc_Sha512* sha512)
wolfSSL 15:117db924cf7c 727 {
wolfSSL 15:117db924cf7c 728 byte* local = (byte*)sha512->buffer;
wolfSSL 15:117db924cf7c 729 int ret;
wolfSSL 15:117db924cf7c 730
wolfSSL 15:117db924cf7c 731 if (sha512 == NULL) {
wolfSSL 15:117db924cf7c 732 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 733 }
wolfSSL 15:117db924cf7c 734
wolfSSL 15:117db924cf7c 735 local[sha512->buffLen++] = 0x80; /* add 1 */
wolfSSL 15:117db924cf7c 736
wolfSSL 15:117db924cf7c 737 /* pad with zeros */
wolfSSL 15:117db924cf7c 738 if (sha512->buffLen > WC_SHA512_PAD_SIZE) {
wolfSSL 15:117db924cf7c 739 XMEMSET(&local[sha512->buffLen], 0, WC_SHA512_BLOCK_SIZE - sha512->buffLen);
wolfSSL 15:117db924cf7c 740 sha512->buffLen += WC_SHA512_BLOCK_SIZE - sha512->buffLen;
wolfSSL 15:117db924cf7c 741 #if defined(LITTLE_ENDIAN_ORDER)
wolfSSL 15:117db924cf7c 742 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 743 if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
wolfSSL 15:117db924cf7c 744 #endif
wolfSSL 15:117db924cf7c 745 {
wolfSSL 16:8e0d178b1d1e 746
wolfSSL 16:8e0d178b1d1e 747 #if !defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
wolfSSL 16:8e0d178b1d1e 748 defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 15:117db924cf7c 749 ByteReverseWords64(sha512->buffer,sha512->buffer,
wolfSSL 16:8e0d178b1d1e 750 WC_SHA512_BLOCK_SIZE);
wolfSSL 16:8e0d178b1d1e 751 #endif
wolfSSL 15:117db924cf7c 752 }
wolfSSL 15:117db924cf7c 753 #endif /* LITTLE_ENDIAN_ORDER */
wolfSSL 16:8e0d178b1d1e 754 #if !defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
wolfSSL 16:8e0d178b1d1e 755 defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 15:117db924cf7c 756 ret = Transform_Sha512(sha512);
wolfSSL 16:8e0d178b1d1e 757 #else
wolfSSL 16:8e0d178b1d1e 758 if(sha512->ctx.mode == ESP32_SHA_INIT) {
wolfSSL 16:8e0d178b1d1e 759 esp_sha_try_hw_lock(&sha512->ctx);
wolfSSL 16:8e0d178b1d1e 760 }
wolfSSL 16:8e0d178b1d1e 761 ret = esp_sha512_process(sha512);
wolfSSL 16:8e0d178b1d1e 762 if(ret == 0 && sha512->ctx.mode == ESP32_SHA_SW){
wolfSSL 16:8e0d178b1d1e 763 ret = Transform_Sha512(sha512);
wolfSSL 16:8e0d178b1d1e 764 }
wolfSSL 16:8e0d178b1d1e 765 #endif
wolfSSL 15:117db924cf7c 766 if (ret != 0)
wolfSSL 15:117db924cf7c 767 return ret;
wolfSSL 15:117db924cf7c 768
wolfSSL 15:117db924cf7c 769 sha512->buffLen = 0;
wolfSSL 15:117db924cf7c 770 }
wolfSSL 15:117db924cf7c 771 XMEMSET(&local[sha512->buffLen], 0, WC_SHA512_PAD_SIZE - sha512->buffLen);
wolfSSL 15:117db924cf7c 772
wolfSSL 15:117db924cf7c 773 /* put lengths in bits */
wolfSSL 15:117db924cf7c 774 sha512->hiLen = (sha512->loLen >> (8 * sizeof(sha512->loLen) - 3)) +
wolfSSL 15:117db924cf7c 775 (sha512->hiLen << 3);
wolfSSL 15:117db924cf7c 776 sha512->loLen = sha512->loLen << 3;
wolfSSL 15:117db924cf7c 777
wolfSSL 15:117db924cf7c 778 /* store lengths */
wolfSSL 15:117db924cf7c 779 #if defined(LITTLE_ENDIAN_ORDER)
wolfSSL 15:117db924cf7c 780 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 781 if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
wolfSSL 15:117db924cf7c 782 #endif
wolfSSL 16:8e0d178b1d1e 783 #if !defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
wolfSSL 16:8e0d178b1d1e 784 defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 15:117db924cf7c 785 ByteReverseWords64(sha512->buffer, sha512->buffer, WC_SHA512_PAD_SIZE);
wolfSSL 16:8e0d178b1d1e 786 #endif
wolfSSL 15:117db924cf7c 787 #endif
wolfSSL 15:117db924cf7c 788 /* ! length ordering dependent on digest endian type ! */
wolfSSL 15:117db924cf7c 789
wolfSSL 16:8e0d178b1d1e 790 #if !defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
wolfSSL 16:8e0d178b1d1e 791 defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 15:117db924cf7c 792 sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
wolfSSL 15:117db924cf7c 793 sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
wolfSSL 16:8e0d178b1d1e 794 #endif
wolfSSL 16:8e0d178b1d1e 795
wolfSSL 15:117db924cf7c 796 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 15:117db924cf7c 797 if (IS_INTEL_AVX1(intel_flags) || IS_INTEL_AVX2(intel_flags))
wolfSSL 15:117db924cf7c 798 ByteReverseWords64(&(sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
wolfSSL 15:117db924cf7c 799 &(sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
wolfSSL 15:117db924cf7c 800 WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE);
wolfSSL 15:117db924cf7c 801 #endif
wolfSSL 16:8e0d178b1d1e 802 #if !defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
wolfSSL 16:8e0d178b1d1e 803 defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 15:117db924cf7c 804 ret = Transform_Sha512(sha512);
wolfSSL 16:8e0d178b1d1e 805 #else
wolfSSL 16:8e0d178b1d1e 806 if(sha512->ctx.mode == ESP32_SHA_INIT) {
wolfSSL 16:8e0d178b1d1e 807 esp_sha_try_hw_lock(&sha512->ctx);
wolfSSL 16:8e0d178b1d1e 808 }
wolfSSL 16:8e0d178b1d1e 809 ret = esp_sha512_digest_process(sha512, 1);
wolfSSL 16:8e0d178b1d1e 810 if(ret == 0 && sha512->ctx.mode == ESP32_SHA_SW) {
wolfSSL 16:8e0d178b1d1e 811 ret = Transform_Sha512(sha512);
wolfSSL 16:8e0d178b1d1e 812 }
wolfSSL 16:8e0d178b1d1e 813 #endif
wolfSSL 15:117db924cf7c 814 if (ret != 0)
wolfSSL 15:117db924cf7c 815 return ret;
wolfSSL 15:117db924cf7c 816
wolfSSL 15:117db924cf7c 817 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 818 ByteReverseWords64(sha512->digest, sha512->digest, WC_SHA512_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 819 #endif
wolfSSL 15:117db924cf7c 820
wolfSSL 15:117db924cf7c 821 return 0;
wolfSSL 15:117db924cf7c 822 }
wolfSSL 15:117db924cf7c 823
wolfSSL 15:117db924cf7c 824 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 825
wolfSSL 15:117db924cf7c 826 int wc_Sha512FinalRaw(wc_Sha512* sha512, byte* hash)
wolfSSL 15:117db924cf7c 827 {
wolfSSL 15:117db924cf7c 828 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 829 word64 digest[WC_SHA512_DIGEST_SIZE / sizeof(word64)];
wolfSSL 15:117db924cf7c 830 #endif
wolfSSL 15:117db924cf7c 831
wolfSSL 15:117db924cf7c 832 if (sha512 == NULL || hash == NULL) {
wolfSSL 15:117db924cf7c 833 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 834 }
wolfSSL 15:117db924cf7c 835
wolfSSL 15:117db924cf7c 836 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 837 ByteReverseWords64((word64*)digest, (word64*)sha512->digest,
wolfSSL 15:117db924cf7c 838 WC_SHA512_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 839 XMEMCPY(hash, digest, WC_SHA512_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 840 #else
wolfSSL 15:117db924cf7c 841 XMEMCPY(hash, sha512->digest, WC_SHA512_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 842 #endif
wolfSSL 15:117db924cf7c 843
wolfSSL 15:117db924cf7c 844 return 0;
wolfSSL 15:117db924cf7c 845 }
wolfSSL 15:117db924cf7c 846
wolfSSL 15:117db924cf7c 847 int wc_Sha512Final(wc_Sha512* sha512, byte* hash)
wolfSSL 15:117db924cf7c 848 {
wolfSSL 15:117db924cf7c 849 int ret;
wolfSSL 15:117db924cf7c 850
wolfSSL 15:117db924cf7c 851 if (sha512 == NULL || hash == NULL) {
wolfSSL 15:117db924cf7c 852 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 853 }
wolfSSL 15:117db924cf7c 854
wolfSSL 15:117db924cf7c 855 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
wolfSSL 15:117db924cf7c 856 if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
wolfSSL 15:117db924cf7c 857 #if defined(HAVE_INTEL_QA)
wolfSSL 15:117db924cf7c 858 return IntelQaSymSha512(&sha512->asyncDev, hash, NULL,
wolfSSL 15:117db924cf7c 859 WC_SHA512_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 860 #endif
wolfSSL 15:117db924cf7c 861 }
wolfSSL 15:117db924cf7c 862 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 863
wolfSSL 15:117db924cf7c 864 ret = Sha512Final(sha512);
wolfSSL 15:117db924cf7c 865 if (ret != 0)
wolfSSL 15:117db924cf7c 866 return ret;
wolfSSL 15:117db924cf7c 867
wolfSSL 15:117db924cf7c 868 XMEMCPY(hash, sha512->digest, WC_SHA512_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 869
wolfSSL 15:117db924cf7c 870 return InitSha512(sha512); /* reset state */
wolfSSL 15:117db924cf7c 871 }
wolfSSL 15:117db924cf7c 872
wolfSSL 15:117db924cf7c 873 int wc_InitSha512(wc_Sha512* sha512)
wolfSSL 15:117db924cf7c 874 {
wolfSSL 15:117db924cf7c 875 return wc_InitSha512_ex(sha512, NULL, INVALID_DEVID);
wolfSSL 15:117db924cf7c 876 }
wolfSSL 15:117db924cf7c 877
wolfSSL 15:117db924cf7c 878 void wc_Sha512Free(wc_Sha512* sha512)
wolfSSL 15:117db924cf7c 879 {
wolfSSL 15:117db924cf7c 880 if (sha512 == NULL)
wolfSSL 15:117db924cf7c 881 return;
wolfSSL 15:117db924cf7c 882
wolfSSL 15:117db924cf7c 883 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 884 if (sha512->W != NULL) {
wolfSSL 15:117db924cf7c 885 XFREE(sha512->W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 886 sha512->W = NULL;
wolfSSL 15:117db924cf7c 887 }
wolfSSL 15:117db924cf7c 888 #endif
wolfSSL 15:117db924cf7c 889
wolfSSL 15:117db924cf7c 890 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
wolfSSL 15:117db924cf7c 891 wolfAsync_DevCtxFree(&sha512->asyncDev, WOLFSSL_ASYNC_MARKER_SHA512);
wolfSSL 15:117db924cf7c 892 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 893 }
wolfSSL 15:117db924cf7c 894
wolfSSL 15:117db924cf7c 895 #endif /* WOLFSSL_SHA512 */
wolfSSL 15:117db924cf7c 896
wolfSSL 15:117db924cf7c 897 /* -------------------------------------------------------------------------- */
wolfSSL 15:117db924cf7c 898 /* SHA384 */
wolfSSL 15:117db924cf7c 899 /* -------------------------------------------------------------------------- */
wolfSSL 15:117db924cf7c 900 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 901
wolfSSL 15:117db924cf7c 902 #if defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH)
wolfSSL 15:117db924cf7c 903 /* functions defined in wolfcrypt/src/port/caam/caam_sha.c */
wolfSSL 16:8e0d178b1d1e 904
wolfSSL 15:117db924cf7c 905 #else
wolfSSL 15:117db924cf7c 906
wolfSSL 15:117db924cf7c 907 static int InitSha384(wc_Sha384* sha384)
wolfSSL 15:117db924cf7c 908 {
wolfSSL 15:117db924cf7c 909 if (sha384 == NULL) {
wolfSSL 15:117db924cf7c 910 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 911 }
wolfSSL 15:117db924cf7c 912
wolfSSL 15:117db924cf7c 913 sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8);
wolfSSL 15:117db924cf7c 914 sha384->digest[1] = W64LIT(0x629a292a367cd507);
wolfSSL 15:117db924cf7c 915 sha384->digest[2] = W64LIT(0x9159015a3070dd17);
wolfSSL 15:117db924cf7c 916 sha384->digest[3] = W64LIT(0x152fecd8f70e5939);
wolfSSL 15:117db924cf7c 917 sha384->digest[4] = W64LIT(0x67332667ffc00b31);
wolfSSL 15:117db924cf7c 918 sha384->digest[5] = W64LIT(0x8eb44a8768581511);
wolfSSL 15:117db924cf7c 919 sha384->digest[6] = W64LIT(0xdb0c2e0d64f98fa7);
wolfSSL 15:117db924cf7c 920 sha384->digest[7] = W64LIT(0x47b5481dbefa4fa4);
wolfSSL 15:117db924cf7c 921
wolfSSL 15:117db924cf7c 922 sha384->buffLen = 0;
wolfSSL 15:117db924cf7c 923 sha384->loLen = 0;
wolfSSL 15:117db924cf7c 924 sha384->hiLen = 0;
wolfSSL 15:117db924cf7c 925
wolfSSL 16:8e0d178b1d1e 926 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 927 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 928 sha384->ctx.sha_type = SHA2_384;
wolfSSL 16:8e0d178b1d1e 929 /* always start firstblock = 1 when using hw engine */
wolfSSL 16:8e0d178b1d1e 930 sha384->ctx.isfirstblock = 1;
wolfSSL 16:8e0d178b1d1e 931 if(sha384->ctx.mode == ESP32_SHA_HW) {
wolfSSL 16:8e0d178b1d1e 932 /* release hw */
wolfSSL 16:8e0d178b1d1e 933 esp_sha_hw_unlock();
wolfSSL 16:8e0d178b1d1e 934 }
wolfSSL 16:8e0d178b1d1e 935 /* always set mode as INIT
wolfSSL 16:8e0d178b1d1e 936 * whether using HW or SW is determined at first call of update()
wolfSSL 16:8e0d178b1d1e 937 */
wolfSSL 16:8e0d178b1d1e 938 sha384->ctx.mode = ESP32_SHA_INIT;
wolfSSL 16:8e0d178b1d1e 939
wolfSSL 16:8e0d178b1d1e 940 #endif
wolfSSL 16:8e0d178b1d1e 941 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 942 sha384->flags = 0;
wolfSSL 16:8e0d178b1d1e 943 #endif
wolfSSL 16:8e0d178b1d1e 944
wolfSSL 15:117db924cf7c 945 return 0;
wolfSSL 15:117db924cf7c 946 }
wolfSSL 15:117db924cf7c 947
wolfSSL 15:117db924cf7c 948 int wc_Sha384Update(wc_Sha384* sha384, const byte* data, word32 len)
wolfSSL 15:117db924cf7c 949 {
wolfSSL 15:117db924cf7c 950 if (sha384 == NULL || (data == NULL && len > 0)) {
wolfSSL 15:117db924cf7c 951 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 952 }
wolfSSL 15:117db924cf7c 953
wolfSSL 15:117db924cf7c 954 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
wolfSSL 15:117db924cf7c 955 if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
wolfSSL 15:117db924cf7c 956 #if defined(HAVE_INTEL_QA)
wolfSSL 15:117db924cf7c 957 return IntelQaSymSha384(&sha384->asyncDev, NULL, data, len);
wolfSSL 15:117db924cf7c 958 #endif
wolfSSL 15:117db924cf7c 959 }
wolfSSL 15:117db924cf7c 960 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 961
wolfSSL 15:117db924cf7c 962 return Sha512Update((wc_Sha512*)sha384, data, len);
wolfSSL 15:117db924cf7c 963 }
wolfSSL 15:117db924cf7c 964
wolfSSL 15:117db924cf7c 965
wolfSSL 15:117db924cf7c 966 int wc_Sha384FinalRaw(wc_Sha384* sha384, byte* hash)
wolfSSL 15:117db924cf7c 967 {
wolfSSL 15:117db924cf7c 968 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 969 word64 digest[WC_SHA384_DIGEST_SIZE / sizeof(word64)];
wolfSSL 15:117db924cf7c 970 #endif
wolfSSL 15:117db924cf7c 971
wolfSSL 15:117db924cf7c 972 if (sha384 == NULL || hash == NULL) {
wolfSSL 15:117db924cf7c 973 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 974 }
wolfSSL 15:117db924cf7c 975
wolfSSL 15:117db924cf7c 976 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 977 ByteReverseWords64((word64*)digest, (word64*)sha384->digest,
wolfSSL 15:117db924cf7c 978 WC_SHA384_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 979 XMEMCPY(hash, digest, WC_SHA384_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 980 #else
wolfSSL 15:117db924cf7c 981 XMEMCPY(hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 982 #endif
wolfSSL 15:117db924cf7c 983
wolfSSL 15:117db924cf7c 984 return 0;
wolfSSL 15:117db924cf7c 985 }
wolfSSL 15:117db924cf7c 986
wolfSSL 15:117db924cf7c 987 int wc_Sha384Final(wc_Sha384* sha384, byte* hash)
wolfSSL 15:117db924cf7c 988 {
wolfSSL 15:117db924cf7c 989 int ret;
wolfSSL 15:117db924cf7c 990
wolfSSL 15:117db924cf7c 991 if (sha384 == NULL || hash == NULL) {
wolfSSL 15:117db924cf7c 992 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 993 }
wolfSSL 15:117db924cf7c 994
wolfSSL 15:117db924cf7c 995 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
wolfSSL 15:117db924cf7c 996 if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
wolfSSL 15:117db924cf7c 997 #if defined(HAVE_INTEL_QA)
wolfSSL 15:117db924cf7c 998 return IntelQaSymSha384(&sha384->asyncDev, hash, NULL,
wolfSSL 15:117db924cf7c 999 WC_SHA384_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1000 #endif
wolfSSL 15:117db924cf7c 1001 }
wolfSSL 15:117db924cf7c 1002 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1003
wolfSSL 15:117db924cf7c 1004 ret = Sha512Final((wc_Sha512*)sha384);
wolfSSL 15:117db924cf7c 1005 if (ret != 0)
wolfSSL 15:117db924cf7c 1006 return ret;
wolfSSL 15:117db924cf7c 1007
wolfSSL 15:117db924cf7c 1008 XMEMCPY(hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
wolfSSL 15:117db924cf7c 1009
wolfSSL 15:117db924cf7c 1010 return InitSha384(sha384); /* reset state */
wolfSSL 15:117db924cf7c 1011 }
wolfSSL 15:117db924cf7c 1012
wolfSSL 15:117db924cf7c 1013 int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
wolfSSL 15:117db924cf7c 1014 {
wolfSSL 15:117db924cf7c 1015 int ret;
wolfSSL 15:117db924cf7c 1016
wolfSSL 15:117db924cf7c 1017 if (sha384 == NULL) {
wolfSSL 15:117db924cf7c 1018 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1019 }
wolfSSL 15:117db924cf7c 1020
wolfSSL 15:117db924cf7c 1021 sha384->heap = heap;
wolfSSL 15:117db924cf7c 1022 ret = InitSha384(sha384);
wolfSSL 15:117db924cf7c 1023 if (ret != 0)
wolfSSL 15:117db924cf7c 1024 return ret;
wolfSSL 15:117db924cf7c 1025
wolfSSL 16:8e0d178b1d1e 1026 #if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
wolfSSL 16:8e0d178b1d1e 1027 Sha512_SetTransform();
wolfSSL 16:8e0d178b1d1e 1028 #endif
wolfSSL 15:117db924cf7c 1029 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 1030 sha384->W = NULL;
wolfSSL 15:117db924cf7c 1031 #endif
wolfSSL 15:117db924cf7c 1032
wolfSSL 15:117db924cf7c 1033 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
wolfSSL 15:117db924cf7c 1034 ret = wolfAsync_DevCtxInit(&sha384->asyncDev, WOLFSSL_ASYNC_MARKER_SHA384,
wolfSSL 15:117db924cf7c 1035 sha384->heap, devId);
wolfSSL 15:117db924cf7c 1036 #else
wolfSSL 15:117db924cf7c 1037 (void)devId;
wolfSSL 15:117db924cf7c 1038 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1039
wolfSSL 15:117db924cf7c 1040 return ret;
wolfSSL 15:117db924cf7c 1041 }
wolfSSL 16:8e0d178b1d1e 1042
wolfSSL 15:117db924cf7c 1043 #endif /* WOLFSSL_IMX6_CAAM */
wolfSSL 15:117db924cf7c 1044
wolfSSL 15:117db924cf7c 1045 int wc_InitSha384(wc_Sha384* sha384)
wolfSSL 15:117db924cf7c 1046 {
wolfSSL 15:117db924cf7c 1047 return wc_InitSha384_ex(sha384, NULL, INVALID_DEVID);
wolfSSL 15:117db924cf7c 1048 }
wolfSSL 15:117db924cf7c 1049
wolfSSL 15:117db924cf7c 1050 void wc_Sha384Free(wc_Sha384* sha384)
wolfSSL 15:117db924cf7c 1051 {
wolfSSL 15:117db924cf7c 1052 if (sha384 == NULL)
wolfSSL 15:117db924cf7c 1053 return;
wolfSSL 15:117db924cf7c 1054
wolfSSL 15:117db924cf7c 1055 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 1056 if (sha384->W != NULL) {
wolfSSL 15:117db924cf7c 1057 XFREE(sha384->W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1058 sha384->W = NULL;
wolfSSL 15:117db924cf7c 1059 }
wolfSSL 15:117db924cf7c 1060 #endif
wolfSSL 15:117db924cf7c 1061
wolfSSL 15:117db924cf7c 1062 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
wolfSSL 15:117db924cf7c 1063 wolfAsync_DevCtxFree(&sha384->asyncDev, WOLFSSL_ASYNC_MARKER_SHA384);
wolfSSL 15:117db924cf7c 1064 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1065 }
wolfSSL 15:117db924cf7c 1066
wolfSSL 15:117db924cf7c 1067 #endif /* WOLFSSL_SHA384 */
wolfSSL 15:117db924cf7c 1068
wolfSSL 15:117db924cf7c 1069 #endif /* HAVE_FIPS */
wolfSSL 15:117db924cf7c 1070
wolfSSL 15:117db924cf7c 1071 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 1072
wolfSSL 15:117db924cf7c 1073 int wc_Sha512GetHash(wc_Sha512* sha512, byte* hash)
wolfSSL 15:117db924cf7c 1074 {
wolfSSL 15:117db924cf7c 1075 int ret;
wolfSSL 15:117db924cf7c 1076 wc_Sha512 tmpSha512;
wolfSSL 15:117db924cf7c 1077
wolfSSL 15:117db924cf7c 1078 if (sha512 == NULL || hash == NULL)
wolfSSL 15:117db924cf7c 1079 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1080
wolfSSL 16:8e0d178b1d1e 1081 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1082 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1083 if(sha512->ctx.mode == ESP32_SHA_INIT) {
wolfSSL 16:8e0d178b1d1e 1084 esp_sha_try_hw_lock(&sha512->ctx);
wolfSSL 16:8e0d178b1d1e 1085 }
wolfSSL 16:8e0d178b1d1e 1086 if(sha512->ctx.mode != ESP32_SHA_SW)
wolfSSL 16:8e0d178b1d1e 1087 esp_sha512_digest_process(sha512, 0);
wolfSSL 16:8e0d178b1d1e 1088 #endif
wolfSSL 16:8e0d178b1d1e 1089
wolfSSL 15:117db924cf7c 1090 ret = wc_Sha512Copy(sha512, &tmpSha512);
wolfSSL 15:117db924cf7c 1091 if (ret == 0) {
wolfSSL 15:117db924cf7c 1092 ret = wc_Sha512Final(&tmpSha512, hash);
wolfSSL 16:8e0d178b1d1e 1093 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1094 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1095 sha512->ctx.mode = ESP32_SHA_SW;;
wolfSSL 16:8e0d178b1d1e 1096 #endif
wolfSSL 15:117db924cf7c 1097 wc_Sha512Free(&tmpSha512);
wolfSSL 15:117db924cf7c 1098 }
wolfSSL 15:117db924cf7c 1099 return ret;
wolfSSL 15:117db924cf7c 1100 }
wolfSSL 15:117db924cf7c 1101
wolfSSL 15:117db924cf7c 1102 int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
wolfSSL 15:117db924cf7c 1103 {
wolfSSL 15:117db924cf7c 1104 int ret = 0;
wolfSSL 15:117db924cf7c 1105
wolfSSL 15:117db924cf7c 1106 if (src == NULL || dst == NULL)
wolfSSL 15:117db924cf7c 1107 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1108
wolfSSL 15:117db924cf7c 1109 XMEMCPY(dst, src, sizeof(wc_Sha512));
wolfSSL 15:117db924cf7c 1110 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 1111 dst->W = NULL;
wolfSSL 15:117db924cf7c 1112 #endif
wolfSSL 15:117db924cf7c 1113
wolfSSL 15:117db924cf7c 1114 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 1115 ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
wolfSSL 15:117db924cf7c 1116 #endif
wolfSSL 16:8e0d178b1d1e 1117 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1118 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1119 dst->ctx.mode = src->ctx.mode;
wolfSSL 16:8e0d178b1d1e 1120 dst->ctx.isfirstblock = src->ctx.isfirstblock;
wolfSSL 16:8e0d178b1d1e 1121 dst->ctx.sha_type = src->ctx.sha_type;
wolfSSL 16:8e0d178b1d1e 1122 #endif
wolfSSL 16:8e0d178b1d1e 1123 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 1124 dst->flags |= WC_HASH_FLAG_ISCOPY;
wolfSSL 16:8e0d178b1d1e 1125 #endif
wolfSSL 15:117db924cf7c 1126
wolfSSL 15:117db924cf7c 1127 return ret;
wolfSSL 15:117db924cf7c 1128 }
wolfSSL 15:117db924cf7c 1129
wolfSSL 16:8e0d178b1d1e 1130 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 1131 int wc_Sha512SetFlags(wc_Sha512* sha512, word32 flags)
wolfSSL 16:8e0d178b1d1e 1132 {
wolfSSL 16:8e0d178b1d1e 1133 if (sha512) {
wolfSSL 16:8e0d178b1d1e 1134 sha512->flags = flags;
wolfSSL 16:8e0d178b1d1e 1135 }
wolfSSL 16:8e0d178b1d1e 1136 return 0;
wolfSSL 16:8e0d178b1d1e 1137 }
wolfSSL 16:8e0d178b1d1e 1138 int wc_Sha512GetFlags(wc_Sha512* sha512, word32* flags)
wolfSSL 16:8e0d178b1d1e 1139 {
wolfSSL 16:8e0d178b1d1e 1140 if (sha512 && flags) {
wolfSSL 16:8e0d178b1d1e 1141 *flags = sha512->flags;
wolfSSL 16:8e0d178b1d1e 1142 }
wolfSSL 16:8e0d178b1d1e 1143 return 0;
wolfSSL 16:8e0d178b1d1e 1144 }
wolfSSL 16:8e0d178b1d1e 1145 #endif
wolfSSL 16:8e0d178b1d1e 1146
wolfSSL 15:117db924cf7c 1147 #endif /* WOLFSSL_SHA512 */
wolfSSL 15:117db924cf7c 1148
wolfSSL 15:117db924cf7c 1149 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 1150
wolfSSL 15:117db924cf7c 1151 int wc_Sha384GetHash(wc_Sha384* sha384, byte* hash)
wolfSSL 15:117db924cf7c 1152 {
wolfSSL 15:117db924cf7c 1153 int ret;
wolfSSL 15:117db924cf7c 1154 wc_Sha384 tmpSha384;
wolfSSL 15:117db924cf7c 1155
wolfSSL 15:117db924cf7c 1156 if (sha384 == NULL || hash == NULL)
wolfSSL 15:117db924cf7c 1157 return BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 1158 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1159 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1160 if(sha384->ctx.mode == ESP32_SHA_INIT) {
wolfSSL 16:8e0d178b1d1e 1161 esp_sha_try_hw_lock(&sha384->ctx);
wolfSSL 16:8e0d178b1d1e 1162 }
wolfSSL 16:8e0d178b1d1e 1163 if(sha384->ctx.mode != ESP32_SHA_SW) {
wolfSSL 16:8e0d178b1d1e 1164 esp_sha512_digest_process(sha384, 0);
wolfSSL 16:8e0d178b1d1e 1165 }
wolfSSL 16:8e0d178b1d1e 1166 #endif
wolfSSL 15:117db924cf7c 1167 ret = wc_Sha384Copy(sha384, &tmpSha384);
wolfSSL 15:117db924cf7c 1168 if (ret == 0) {
wolfSSL 15:117db924cf7c 1169 ret = wc_Sha384Final(&tmpSha384, hash);
wolfSSL 16:8e0d178b1d1e 1170 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1171 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1172 sha384->ctx.mode = ESP32_SHA_SW;
wolfSSL 16:8e0d178b1d1e 1173 #endif
wolfSSL 15:117db924cf7c 1174 wc_Sha384Free(&tmpSha384);
wolfSSL 15:117db924cf7c 1175 }
wolfSSL 15:117db924cf7c 1176 return ret;
wolfSSL 15:117db924cf7c 1177 }
wolfSSL 15:117db924cf7c 1178 int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
wolfSSL 15:117db924cf7c 1179 {
wolfSSL 15:117db924cf7c 1180 int ret = 0;
wolfSSL 15:117db924cf7c 1181
wolfSSL 15:117db924cf7c 1182 if (src == NULL || dst == NULL)
wolfSSL 15:117db924cf7c 1183 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1184
wolfSSL 15:117db924cf7c 1185 XMEMCPY(dst, src, sizeof(wc_Sha384));
wolfSSL 15:117db924cf7c 1186 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 1187 dst->W = NULL;
wolfSSL 15:117db924cf7c 1188 #endif
wolfSSL 15:117db924cf7c 1189
wolfSSL 15:117db924cf7c 1190 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 1191 ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
wolfSSL 15:117db924cf7c 1192 #endif
wolfSSL 16:8e0d178b1d1e 1193 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
wolfSSL 16:8e0d178b1d1e 1194 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
wolfSSL 16:8e0d178b1d1e 1195 dst->ctx.mode = src->ctx.mode;
wolfSSL 16:8e0d178b1d1e 1196 dst->ctx.isfirstblock = src->ctx.isfirstblock;
wolfSSL 16:8e0d178b1d1e 1197 dst->ctx.sha_type = src->ctx.sha_type;
wolfSSL 16:8e0d178b1d1e 1198 #endif
wolfSSL 16:8e0d178b1d1e 1199 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 1200 dst->flags |= WC_HASH_FLAG_ISCOPY;
wolfSSL 16:8e0d178b1d1e 1201 #endif
wolfSSL 15:117db924cf7c 1202
wolfSSL 15:117db924cf7c 1203 return ret;
wolfSSL 15:117db924cf7c 1204 }
wolfSSL 15:117db924cf7c 1205
wolfSSL 16:8e0d178b1d1e 1206 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 1207 int wc_Sha384SetFlags(wc_Sha384* sha384, word32 flags)
wolfSSL 16:8e0d178b1d1e 1208 {
wolfSSL 16:8e0d178b1d1e 1209 if (sha384) {
wolfSSL 16:8e0d178b1d1e 1210 sha384->flags = flags;
wolfSSL 16:8e0d178b1d1e 1211 }
wolfSSL 16:8e0d178b1d1e 1212 return 0;
wolfSSL 16:8e0d178b1d1e 1213 }
wolfSSL 16:8e0d178b1d1e 1214 int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags)
wolfSSL 16:8e0d178b1d1e 1215 {
wolfSSL 16:8e0d178b1d1e 1216 if (sha384 && flags) {
wolfSSL 16:8e0d178b1d1e 1217 *flags = sha384->flags;
wolfSSL 16:8e0d178b1d1e 1218 }
wolfSSL 16:8e0d178b1d1e 1219 return 0;
wolfSSL 16:8e0d178b1d1e 1220 }
wolfSSL 16:8e0d178b1d1e 1221 #endif
wolfSSL 16:8e0d178b1d1e 1222
wolfSSL 15:117db924cf7c 1223 #endif /* WOLFSSL_SHA384 */
wolfSSL 15:117db924cf7c 1224
wolfSSL 15:117db924cf7c 1225 #endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */
wolfSSL 15:117db924cf7c 1226