source: azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/sha512.c@ 464

Last change on this file since 464 was 464, checked in by coas-nagasima, 3 years ago

WolfSSLとAzure IoT SDKを更新

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