Coverage Report

Created: 2025-04-11 06:45

/src/wolfssl/wolfcrypt/src/sha512.c
Line
Count
Source (jump to first uncovered line)
1
/* sha512.c
2
 *
3
 * Copyright (C) 2006-2023 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)) && \
30
    (!defined(WOLFSSL_ARMASM) && !defined(WOLFSSL_ARMASM_NO_NEON)) && \
31
    !defined(WOLFSSL_PSOC6_CRYPTO)
32
33
/* determine if we are using Espressif SHA hardware acceleration */
34
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
35
#if defined(WOLFSSL_ESP32_CRYPT) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
36
    #include "sdkconfig.h"
37
    /* Define a single keyword for simplicity & readability.
38
     *
39
     * By default the HW acceleration is on for ESP32 Chipsets,
40
     * but individual components can be turned off. See user_settings.h
41
     */
42
    #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
43
    static const char* TAG = "wc_sha_512";
44
#else
45
    #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
46
#endif
47
48
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
49
    /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
50
    #define FIPS_NO_WRAPPERS
51
52
    #ifdef USE_WINDOWS_API
53
        #pragma code_seg(".fipsA$k")
54
        #pragma const_seg(".fipsB$k")
55
    #endif
56
#endif
57
58
#include <wolfssl/wolfcrypt/sha512.h>
59
#include <wolfssl/wolfcrypt/error-crypt.h>
60
#include <wolfssl/wolfcrypt/cpuid.h>
61
#include <wolfssl/wolfcrypt/hash.h>
62
63
#ifdef WOLF_CRYPTO_CB
64
    #include <wolfssl/wolfcrypt/cryptocb.h>
65
#endif
66
67
#ifdef WOLFSSL_IMXRT1170_CAAM
68
    #include <wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h>
69
#endif
70
71
/* deprecated USE_SLOW_SHA2 (replaced with USE_SLOW_SHA512) */
72
#if defined(USE_SLOW_SHA2) && !defined(USE_SLOW_SHA512)
73
    #define USE_SLOW_SHA512
74
#endif
75
76
#include <wolfssl/wolfcrypt/logging.h>
77
78
#ifdef NO_INLINE
79
    #include <wolfssl/wolfcrypt/misc.h>
80
#else
81
    #define WOLFSSL_MISC_INCLUDED
82
    #include <wolfcrypt/src/misc.c>
83
#endif
84
85
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
86
    #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
87
#endif
88
89
90
#if defined(USE_INTEL_SPEEDUP)
91
    #if defined(__GNUC__) && ((__GNUC__ < 4) || \
92
                              (__GNUC__ == 4 && __GNUC_MINOR__ <= 8))
93
        #undef  NO_AVX2_SUPPORT
94
        #define NO_AVX2_SUPPORT
95
    #endif
96
    #if defined(__clang__) && ((__clang_major__ < 3) || \
97
                               (__clang_major__ == 3 && __clang_minor__ <= 5))
98
        #define NO_AVX2_SUPPORT
99
    #elif defined(__clang__) && defined(NO_AVX2_SUPPORT)
100
        #undef NO_AVX2_SUPPORT
101
    #endif
102
103
    #define HAVE_INTEL_AVX1
104
    #ifndef NO_AVX2_SUPPORT
105
        #define HAVE_INTEL_AVX2
106
    #endif
107
#endif
108
109
#if defined(HAVE_INTEL_AVX1)
110
    /* #define DEBUG_XMM  */
111
#endif
112
113
#if defined(HAVE_INTEL_AVX2)
114
    #define HAVE_INTEL_RORX
115
    /* #define DEBUG_YMM  */
116
#endif
117
118
#if defined(HAVE_BYTEREVERSE64) && \
119
        !defined(HAVE_INTEL_AVX1) && !defined(HAVE_INTEL_AVX2)
120
    #define ByteReverseWords64(out, in, size) ByteReverseWords64_1(out, size)
121
    #define ByteReverseWords64_1(buf, size) \
122
        { unsigned int i ;\
123
            for(i=0; i< size/sizeof(word64); i++){\
124
                __asm__ volatile("bswapq %0":"+r"(buf[i])::) ;\
125
            }\
126
        }
127
#endif
128
129
#if defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
130
    !defined(WOLFSSL_QNX_CAAM)
131
    /* functions defined in wolfcrypt/src/port/caam/caam_sha.c */
132
133
#elif defined(WOLFSSL_SILABS_SHA384)
134
    /* functions defined in wolfcrypt/src/port/silabs/silabs_hash.c */
135
136
#elif defined(WOLFSSL_KCAPI_HASH)
137
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
138
139
#elif defined(WOLFSSL_RENESAS_RSIP) && \
140
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
141
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
142
143
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
144
    int wc_InitSha512(wc_Sha512* sha512)
145
    {
146
        if (sha512 == NULL)
147
            return BAD_FUNC_ARG;
148
        return se050_hash_init(&sha512->se050Ctx, NULL);
149
    }
150
    int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
151
    {
152
        if (sha512 == NULL) {
153
            return BAD_FUNC_ARG;
154
        }
155
        (void)devId;
156
        return se050_hash_init(&sha512->se050Ctx, heap);
157
    }
158
    int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
159
    {
160
        return se050_hash_update(&sha512->se050Ctx, data, len);
161
    }
162
    int wc_Sha512Final(wc_Sha512* sha512, byte* hash)
163
    {
164
        int ret = 0;
165
        int devId = INVALID_DEVID;
166
        if (sha512 == NULL) {
167
            return BAD_FUNC_ARG;
168
        }
169
    #ifdef WOLF_CRYPTO_CB
170
        devId = sha512->devId;
171
    #endif
172
        ret = se050_hash_final(&sha512->se050Ctx, hash, WC_SHA512_DIGEST_SIZE,
173
                               kAlgorithm_SSS_SHA512);
174
        return ret;
175
    }
176
    int wc_Sha512FinalRaw(wc_Sha512* sha512, byte* hash)
177
    {
178
        int ret = 0;
179
        int devId = INVALID_DEVID;
180
        if (sha512 == NULL) {
181
            return BAD_FUNC_ARG;
182
        }
183
    #ifdef WOLF_CRYPTO_CB
184
        devId = sha512->devId;
185
    #endif
186
        ret = se050_hash_final(&sha512->se050Ctx, hash, WC_SHA512_DIGEST_SIZE,
187
                               kAlgorithm_SSS_SHA512);
188
        return ret;
189
    }
190
    void wc_Sha512Free(wc_Sha512* sha512)
191
    {
192
        se050_hash_free(&sha512->se050Ctx);
193
    }
194
195
#else
196
197
#ifdef WOLFSSL_SHA512
198
199
static int InitSha512(wc_Sha512* sha512)
200
427
{
201
427
    if (sha512 == NULL)
202
0
        return BAD_FUNC_ARG;
203
204
427
    sha512->digest[0] = W64LIT(0x6a09e667f3bcc908);
205
427
    sha512->digest[1] = W64LIT(0xbb67ae8584caa73b);
206
427
    sha512->digest[2] = W64LIT(0x3c6ef372fe94f82b);
207
427
    sha512->digest[3] = W64LIT(0xa54ff53a5f1d36f1);
208
427
    sha512->digest[4] = W64LIT(0x510e527fade682d1);
209
427
    sha512->digest[5] = W64LIT(0x9b05688c2b3e6c1f);
210
427
    sha512->digest[6] = W64LIT(0x1f83d9abfb41bd6b);
211
427
    sha512->digest[7] = W64LIT(0x5be0cd19137e2179);
212
213
427
    sha512->buffLen = 0;
214
427
    sha512->loLen   = 0;
215
427
    sha512->hiLen   = 0;
216
217
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
218
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
219
220
    /* HW needs to be carefully initialized, taking into account soft copy.
221
    ** If already in use; copy may revert to SW as needed. */
222
    esp_sha_init(&(sha512->ctx), WC_HASH_TYPE_SHA512);
223
#endif
224
225
427
#ifdef WOLFSSL_HASH_FLAGS
226
427
    sha512->flags = 0;
227
427
#endif
228
427
    return 0;
229
427
}
230
231
#if !defined(WOLFSSL_NOSHA512_224) && \
232
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
233
234
/**
235
 * Initialize given wc_Sha512 structure with value specific to sha512/224.
236
 * Note that sha512/224 has different initial hash value from sha512.
237
 * The initial hash value consists of eight 64bit words. They are given
238
 * in FIPS180-4.
239
 */
240
static int InitSha512_224(wc_Sha512* sha512)
241
0
{
242
0
    if (sha512 == NULL)
243
0
        return BAD_FUNC_ARG;
244
245
0
    sha512->digest[0] = W64LIT(0x8c3d37c819544da2);
246
0
    sha512->digest[1] = W64LIT(0x73e1996689dcd4d6);
247
0
    sha512->digest[2] = W64LIT(0x1dfab7ae32ff9c82);
248
0
    sha512->digest[3] = W64LIT(0x679dd514582f9fcf);
249
0
    sha512->digest[4] = W64LIT(0x0f6d2b697bd44da8);
250
0
    sha512->digest[5] = W64LIT(0x77e36f7304c48942);
251
0
    sha512->digest[6] = W64LIT(0x3f9d85a86a1d36c8);
252
0
    sha512->digest[7] = W64LIT(0x1112e6ad91d692a1);
253
254
0
    sha512->buffLen = 0;
255
0
    sha512->loLen   = 0;
256
0
    sha512->hiLen   = 0;
257
258
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
259
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
260
    /* HW needs to be carefully initialized, taking into account soft copy.
261
    ** If already in use; copy may revert to SW as needed.
262
    **
263
    ** Note for original ESP32, there's no HW for SHA512/224
264
    */
265
    esp_sha_init(&(sha512->ctx), WC_HASH_TYPE_SHA512_224);
266
#endif
267
268
0
#ifdef WOLFSSL_HASH_FLAGS
269
0
    sha512->flags = 0;
270
0
#endif
271
0
    return 0;
272
0
}
273
#endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
274
275
#if !defined(WOLFSSL_NOSHA512_256) && \
276
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
277
/**
278
 * Initialize given wc_Sha512 structure with value specific to sha512/256.
279
 * Note that sha512/256 has different initial hash value from sha512.
280
 * The initial hash value consists of eight 64bit words. They are given
281
 * in FIPS180-4.
282
 */
283
static int InitSha512_256(wc_Sha512* sha512)
284
0
{
285
0
    if (sha512 == NULL)
286
0
        return BAD_FUNC_ARG;
287
288
0
    sha512->digest[0] = W64LIT(0x22312194fc2bf72c);
289
0
    sha512->digest[1] = W64LIT(0x9f555fa3c84c64c2);
290
0
    sha512->digest[2] = W64LIT(0x2393b86b6f53b151);
291
0
    sha512->digest[3] = W64LIT(0x963877195940eabd);
292
0
    sha512->digest[4] = W64LIT(0x96283ee2a88effe3);
293
0
    sha512->digest[5] = W64LIT(0xbe5e1e2553863992);
294
0
    sha512->digest[6] = W64LIT(0x2b0199fc2c85b8aa);
295
0
    sha512->digest[7] = W64LIT(0x0eb72ddc81c52ca2);
296
297
0
    sha512->buffLen = 0;
298
0
    sha512->loLen   = 0;
299
0
    sha512->hiLen   = 0;
300
301
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
302
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
303
    /* HW needs to be carefully initialized, taking into account soft copy.
304
    ** If already in use; copy may revert to SW as needed.
305
    **
306
    ** Note for original ESP32, there's no HW for SHA512/2256.
307
    */
308
    esp_sha_init(&(sha512->ctx), WC_HASH_TYPE_SHA512_256);
309
#endif
310
311
0
#ifdef WOLFSSL_HASH_FLAGS
312
0
    sha512->flags = 0;
313
0
#endif
314
0
    return 0;
315
0
}
316
#endif /* !WOLFSSL_NOSHA512_256 && !FIPS... */
317
318
#endif /* WOLFSSL_SHA512 */
319
320
/* Hardware Acceleration */
321
#if defined(USE_INTEL_SPEEDUP) && \
322
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
323
324
#ifdef WOLFSSL_SHA512
325
326
    /*****
327
    Intel AVX1/AVX2 Macro Control Structure
328
329
    #if defined(HAVE_INTEL_SPEEDUP)
330
        #define HAVE_INTEL_AVX1
331
        #define HAVE_INTEL_AVX2
332
    #endif
333
334
    int InitSha512(wc_Sha512* sha512) {
335
         Save/Recover XMM, YMM
336
         ...
337
338
         Check Intel AVX cpuid flags
339
    }
340
341
    #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
342
      Transform_Sha512_AVX1(); # Function prototype
343
      Transform_Sha512_AVX2(); #
344
    #endif
345
346
      _Transform_Sha512() {     # Native Transform Function body
347
348
      }
349
350
      int Sha512Update() {
351
         Save/Recover XMM, YMM
352
         ...
353
      }
354
355
      int Sha512Final() {
356
         Save/Recover XMM, YMM
357
         ...
358
      }
359
360
361
    #if defined(HAVE_INTEL_AVX1)
362
363
       XMM Instructions/INLINE asm Definitions
364
365
    #endif
366
367
    #if defined(HAVE_INTEL_AVX2)
368
369
       YMM Instructions/INLINE asm Definitions
370
371
    #endif
372
373
    #if defined(HAVE_INTEL_AVX1)
374
375
      int Transform_Sha512_AVX1() {
376
          Stitched Message Sched/Round
377
      }
378
379
    #endif
380
381
    #if defined(HAVE_INTEL_AVX2)
382
383
      int Transform_Sha512_AVX2() {
384
          Stitched Message Sched/Round
385
      }
386
    #endif
387
388
    */
389
390
391
    /* Each platform needs to query info type 1 from cpuid to see if aesni is
392
     * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
393
     */
394
395
#ifdef __cplusplus
396
    extern "C" {
397
#endif
398
399
    #if defined(HAVE_INTEL_AVX1)
400
        extern int Transform_Sha512_AVX1(wc_Sha512 *sha512);
401
        extern int Transform_Sha512_AVX1_Len(wc_Sha512 *sha512, word32 len);
402
    #endif
403
    #if defined(HAVE_INTEL_AVX2)
404
        extern int Transform_Sha512_AVX2(wc_Sha512 *sha512);
405
        extern int Transform_Sha512_AVX2_Len(wc_Sha512 *sha512, word32 len);
406
        #if defined(HAVE_INTEL_RORX)
407
            extern int Transform_Sha512_AVX1_RORX(wc_Sha512 *sha512);
408
            extern int Transform_Sha512_AVX1_RORX_Len(wc_Sha512 *sha512,
409
                                                      word32 len);
410
            extern int Transform_Sha512_AVX2_RORX(wc_Sha512 *sha512);
411
            extern int Transform_Sha512_AVX2_RORX_Len(wc_Sha512 *sha512,
412
                                                      word32 len);
413
        #endif
414
    #endif
415
416
#ifdef __cplusplus
417
    }  /* extern "C" */
418
#endif
419
420
    static int _Transform_Sha512(wc_Sha512 *sha512);
421
    static int (*Transform_Sha512_p)(wc_Sha512* sha512) = _Transform_Sha512;
422
    static int (*Transform_Sha512_Len_p)(wc_Sha512* sha512, word32 len) = NULL;
423
    static int transform_check = 0;
424
    static word32 intel_flags;
425
    static int Transform_Sha512_is_vectorized = 0;
426
427
    static WC_INLINE int Transform_Sha512(wc_Sha512 *sha512) {
428
        int ret;
429
        ret = (*Transform_Sha512_p)(sha512);
430
        return ret;
431
    }
432
    static WC_INLINE int Transform_Sha512_Len(wc_Sha512 *sha512, word32 len) {
433
        int ret;
434
        ret = (*Transform_Sha512_Len_p)(sha512, len);
435
        return ret;
436
    }
437
438
    static void Sha512_SetTransform(void)
439
    {
440
        if (transform_check)
441
            return;
442
443
        intel_flags = cpuid_get_flags();
444
445
    #if defined(HAVE_INTEL_AVX2)
446
        if (IS_INTEL_AVX2(intel_flags)) {
447
        #ifdef HAVE_INTEL_RORX
448
            if (IS_INTEL_BMI2(intel_flags)) {
449
                Transform_Sha512_p = Transform_Sha512_AVX2_RORX;
450
                Transform_Sha512_Len_p = Transform_Sha512_AVX2_RORX_Len;
451
                Transform_Sha512_is_vectorized = 1;
452
            }
453
            else
454
        #endif
455
            if (1) {
456
                Transform_Sha512_p = Transform_Sha512_AVX2;
457
                Transform_Sha512_Len_p = Transform_Sha512_AVX2_Len;
458
                Transform_Sha512_is_vectorized = 1;
459
            }
460
        #ifdef HAVE_INTEL_RORX
461
            else {
462
                Transform_Sha512_p = Transform_Sha512_AVX1_RORX;
463
                Transform_Sha512_Len_p = Transform_Sha512_AVX1_RORX_Len;
464
                Transform_Sha512_is_vectorized = 1;
465
            }
466
        #endif
467
        }
468
        else
469
    #endif
470
    #if defined(HAVE_INTEL_AVX1)
471
        if (IS_INTEL_AVX1(intel_flags)) {
472
            Transform_Sha512_p = Transform_Sha512_AVX1;
473
            Transform_Sha512_Len_p = Transform_Sha512_AVX1_Len;
474
            Transform_Sha512_is_vectorized = 1;
475
        }
476
        else
477
    #endif
478
        {
479
            Transform_Sha512_p = _Transform_Sha512;
480
            Transform_Sha512_is_vectorized = 1;
481
        }
482
483
        transform_check = 1;
484
    }
485
#endif /* WOLFSSL_SHA512 */
486
487
#else
488
37.9k
    #define Transform_Sha512(sha512) _Transform_Sha512(sha512)
489
490
#endif
491
492
#ifdef WOLFSSL_SHA512
493
494
static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId,
495
                             int (*initfp)(wc_Sha512*))
496
179
{
497
179
   int ret = 0;
498
499
179
    if (sha512 == NULL) {
500
0
        return BAD_FUNC_ARG;
501
0
    }
502
503
504
179
    sha512->heap = heap;
505
#ifdef WOLFSSL_SMALL_STACK_CACHE
506
    sha512->W = NULL;
507
#endif
508
179
#ifdef WOLF_CRYPTO_CB
509
179
    sha512->devId = devId;
510
179
    sha512->devCtx = NULL;
511
179
#endif
512
513
    /* call the initialization function pointed to by initfp */
514
179
    ret = initfp(sha512);
515
179
    if (ret != 0)
516
0
        return ret;
517
518
#if defined(USE_INTEL_SPEEDUP) && \
519
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
520
    Sha512_SetTransform();
521
#endif
522
#ifdef WOLFSSL_HASH_KEEP
523
    sha512->msg  = NULL;
524
    sha512->len  = 0;
525
    sha512->used = 0;
526
#endif
527
528
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
529
    ret = wolfAsync_DevCtxInit(&sha512->asyncDev,
530
                        WOLFSSL_ASYNC_MARKER_SHA512, sha512->heap, devId);
531
#else
532
179
    (void)devId;
533
179
#endif /* WOLFSSL_ASYNC_CRYPT */
534
#ifdef WOLFSSL_IMXRT1170_CAAM
535
     ret = wc_CAAM_HashInit(&sha512->hndl, &sha512->ctx, WC_HASH_TYPE_SHA512);
536
#endif
537
179
    return ret;
538
179
} /* InitSha512_Family */
539
540
int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
541
179
{
542
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
543
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
544
    if (sha512->ctx.mode != ESP32_SHA_INIT) {
545
        ESP_LOGV(TAG, "Set ctx mode from prior value: "
546
                      "%d", sha512->ctx.mode);
547
    }
548
    /* We know this is a fresh, uninitialized item, so set to INIT */
549
    sha512->ctx.mode = ESP32_SHA_INIT;
550
#endif
551
552
179
    return InitSha512_Family(sha512, heap, devId, InitSha512);
553
179
}
554
555
#if !defined(WOLFSSL_NOSHA512_224) && \
556
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
557
int wc_InitSha512_224_ex(wc_Sha512* sha512, void* heap, int devId)
558
0
{
559
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
560
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
561
    /* No SHA512/224 HW support is available, set to SW. */
562
    sha512->ctx.mode = ESP32_SHA_SW; /* no SHA224 HW, so always SW */
563
#endif
564
0
    return InitSha512_Family(sha512, heap, devId, InitSha512_224);
565
0
}
566
#endif /* !WOLFSSL_NOSHA512_224 ... */
567
568
#if !defined(WOLFSSL_NOSHA512_256) && \
569
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
570
int wc_InitSha512_256_ex(wc_Sha512* sha512, void* heap, int devId)
571
0
{
572
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
573
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
574
    /* No SHA512/256 HW support is available on ESP32, set to SW. */
575
    sha512->ctx.mode = ESP32_SHA_SW;
576
#endif
577
0
    return InitSha512_Family(sha512, heap, devId, InitSha512_256);
578
0
}
579
#endif /* !WOLFSSL_NOSHA512_256 ... */
580
581
#endif /* WOLFSSL_SHA512 */
582
583
584
static const word64 K512[80] = {
585
    W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd),
586
    W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc),
587
    W64LIT(0x3956c25bf348b538), W64LIT(0x59f111f1b605d019),
588
    W64LIT(0x923f82a4af194f9b), W64LIT(0xab1c5ed5da6d8118),
589
    W64LIT(0xd807aa98a3030242), W64LIT(0x12835b0145706fbe),
590
    W64LIT(0x243185be4ee4b28c), W64LIT(0x550c7dc3d5ffb4e2),
591
    W64LIT(0x72be5d74f27b896f), W64LIT(0x80deb1fe3b1696b1),
592
    W64LIT(0x9bdc06a725c71235), W64LIT(0xc19bf174cf692694),
593
    W64LIT(0xe49b69c19ef14ad2), W64LIT(0xefbe4786384f25e3),
594
    W64LIT(0x0fc19dc68b8cd5b5), W64LIT(0x240ca1cc77ac9c65),
595
    W64LIT(0x2de92c6f592b0275), W64LIT(0x4a7484aa6ea6e483),
596
    W64LIT(0x5cb0a9dcbd41fbd4), W64LIT(0x76f988da831153b5),
597
    W64LIT(0x983e5152ee66dfab), W64LIT(0xa831c66d2db43210),
598
    W64LIT(0xb00327c898fb213f), W64LIT(0xbf597fc7beef0ee4),
599
    W64LIT(0xc6e00bf33da88fc2), W64LIT(0xd5a79147930aa725),
600
    W64LIT(0x06ca6351e003826f), W64LIT(0x142929670a0e6e70),
601
    W64LIT(0x27b70a8546d22ffc), W64LIT(0x2e1b21385c26c926),
602
    W64LIT(0x4d2c6dfc5ac42aed), W64LIT(0x53380d139d95b3df),
603
    W64LIT(0x650a73548baf63de), W64LIT(0x766a0abb3c77b2a8),
604
    W64LIT(0x81c2c92e47edaee6), W64LIT(0x92722c851482353b),
605
    W64LIT(0xa2bfe8a14cf10364), W64LIT(0xa81a664bbc423001),
606
    W64LIT(0xc24b8b70d0f89791), W64LIT(0xc76c51a30654be30),
607
    W64LIT(0xd192e819d6ef5218), W64LIT(0xd69906245565a910),
608
    W64LIT(0xf40e35855771202a), W64LIT(0x106aa07032bbd1b8),
609
    W64LIT(0x19a4c116b8d2d0c8), W64LIT(0x1e376c085141ab53),
610
    W64LIT(0x2748774cdf8eeb99), W64LIT(0x34b0bcb5e19b48a8),
611
    W64LIT(0x391c0cb3c5c95a63), W64LIT(0x4ed8aa4ae3418acb),
612
    W64LIT(0x5b9cca4f7763e373), W64LIT(0x682e6ff3d6b2b8a3),
613
    W64LIT(0x748f82ee5defb2fc), W64LIT(0x78a5636f43172f60),
614
    W64LIT(0x84c87814a1f0ab72), W64LIT(0x8cc702081a6439ec),
615
    W64LIT(0x90befffa23631e28), W64LIT(0xa4506cebde82bde9),
616
    W64LIT(0xbef9a3f7b2c67915), W64LIT(0xc67178f2e372532b),
617
    W64LIT(0xca273eceea26619c), W64LIT(0xd186b8c721c0c207),
618
    W64LIT(0xeada7dd6cde0eb1e), W64LIT(0xf57d4f7fee6ed178),
619
    W64LIT(0x06f067aa72176fba), W64LIT(0x0a637dc5a2c898a6),
620
    W64LIT(0x113f9804bef90dae), W64LIT(0x1b710b35131c471b),
621
    W64LIT(0x28db77f523047d84), W64LIT(0x32caab7b40c72493),
622
    W64LIT(0x3c9ebe0a15c9bebc), W64LIT(0x431d67c49c100d4c),
623
    W64LIT(0x4cc5d4becb3e42b6), W64LIT(0x597f299cfc657e2a),
624
    W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817)
625
};
626
627
606k
#define blk0(i) (W[i] = sha512->buffer[i])
628
629
2.42M
#define blk2(i) (\
630
2.42M
               W[ (i)     & 15] += \
631
2.42M
            s1(W[((i)-2)  & 15])+ \
632
2.42M
               W[((i)-7)  & 15] + \
633
2.42M
            s0(W[((i)-15) & 15])  \
634
2.42M
        )
635
636
3.03M
#define Ch(x,y,z)  ((z) ^ ((x) & ((y) ^ (z))))
637
3.03M
#define Maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
638
639
37.9k
#define a(i) T[(0-(i)) & 7]
640
37.9k
#define b(i) T[(1-(i)) & 7]
641
37.9k
#define c(i) T[(2-(i)) & 7]
642
3.07M
#define d(i) T[(3-(i)) & 7]
643
37.9k
#define e(i) T[(4-(i)) & 7]
644
37.9k
#define f(i) T[(5-(i)) & 7]
645
37.9k
#define g(i) T[(6-(i)) & 7]
646
9.14M
#define h(i) T[(7-(i)) & 7]
647
648
3.03M
#define S0(x) (rotrFixed64(x,28) ^ rotrFixed64(x,34) ^ rotrFixed64(x,39))
649
3.03M
#define S1(x) (rotrFixed64(x,14) ^ rotrFixed64(x,18) ^ rotrFixed64(x,41))
650
2.42M
#define s0(x) (rotrFixed64(x,1)  ^ rotrFixed64(x,8)  ^ ((x)>>7))
651
2.42M
#define s1(x) (rotrFixed64(x,19) ^ rotrFixed64(x,61) ^ ((x)>>6))
652
653
#define R(i) \
654
3.03M
    h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[(i)+j] + (j ? blk2(i) : blk0(i)); \
655
3.03M
    d(i) += h(i); \
656
3.03M
    h(i) += S0(a(i)) + Maj(a(i),b(i),c(i))
657
658
static int _Transform_Sha512(wc_Sha512* sha512)
659
37.9k
{
660
37.9k
    const word64* K = K512;
661
37.9k
    word32 j;
662
37.9k
    word64 T[8];
663
664
#ifdef WOLFSSL_SMALL_STACK_CACHE
665
    word64* W = sha512->W;
666
    if (W == NULL) {
667
        W = (word64*)XMALLOC(sizeof(word64) * 16, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
668
        if (W == NULL)
669
            return MEMORY_E;
670
        sha512->W = W;
671
    }
672
#elif defined(WOLFSSL_SMALL_STACK)
673
37.9k
    word64* W;
674
37.9k
    W = (word64*) XMALLOC(sizeof(word64) * 16, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
675
37.9k
    if (W == NULL)
676
55
        return MEMORY_E;
677
#else
678
    word64 W[16];
679
#endif
680
681
    /* Copy digest to working vars */
682
37.9k
    XMEMCPY(T, sha512->digest, sizeof(T));
683
684
#ifdef USE_SLOW_SHA512
685
    /* over twice as small, but 50% slower */
686
    /* 80 operations, not unrolled */
687
    for (j = 0; j < 80; j += 16) {
688
        int m;
689
        for (m = 0; m < 16; m++) { /* braces needed here for macros {} */
690
            R(m);
691
        }
692
    }
693
#else
694
    /* 80 operations, partially loop unrolled */
695
227k
    for (j = 0; j < 80; j += 16) {
696
189k
        R( 0); R( 1); R( 2); R( 3);
697
189k
        R( 4); R( 5); R( 6); R( 7);
698
189k
        R( 8); R( 9); R(10); R(11);
699
189k
        R(12); R(13); R(14); R(15);
700
189k
    }
701
37.9k
#endif /* USE_SLOW_SHA512 */
702
703
    /* Add the working vars back into digest */
704
37.9k
    sha512->digest[0] += a(0);
705
37.9k
    sha512->digest[1] += b(0);
706
37.9k
    sha512->digest[2] += c(0);
707
37.9k
    sha512->digest[3] += d(0);
708
37.9k
    sha512->digest[4] += e(0);
709
37.9k
    sha512->digest[5] += f(0);
710
37.9k
    sha512->digest[6] += g(0);
711
37.9k
    sha512->digest[7] += h(0);
712
713
    /* Wipe variables */
714
37.9k
    ForceZero(W, sizeof(word64) * 16);
715
37.9k
    ForceZero(T, sizeof(T));
716
717
37.9k
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE)
718
37.9k
    XFREE(W, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
719
37.9k
#endif
720
721
37.9k
    return 0;
722
37.9k
}
723
724
725
static WC_INLINE void AddLength(wc_Sha512* sha512, word32 len)
726
1.91k
{
727
1.91k
    word64 tmp = sha512->loLen;
728
1.91k
    if ( (sha512->loLen += len) < tmp)
729
0
        sha512->hiLen++;                       /* carry low to high */
730
1.91k
}
731
732
static WC_INLINE int Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
733
17.6k
{
734
17.6k
    int ret = 0;
735
    /* do block size increments */
736
17.6k
    byte* local = (byte*)sha512->buffer;
737
738
    /* check that internal buffLen is valid */
739
17.6k
    if (sha512->buffLen >= WC_SHA512_BLOCK_SIZE)
740
0
        return BUFFER_E;
741
742
17.6k
    if (len == 0)
743
15.7k
        return 0;
744
745
1.91k
    AddLength(sha512, len);
746
747
1.91k
    if (sha512->buffLen > 0) {
748
967
        word32 add = min(len, WC_SHA512_BLOCK_SIZE - sha512->buffLen);
749
967
        if (add > 0) {
750
967
            XMEMCPY(&local[sha512->buffLen], data, add);
751
752
967
            sha512->buffLen += add;
753
967
            data            += add;
754
967
            len             -= add;
755
967
        }
756
757
967
        if (sha512->buffLen == WC_SHA512_BLOCK_SIZE) {
758
377
    #if defined(LITTLE_ENDIAN_ORDER)
759
        #if defined(USE_INTEL_SPEEDUP) && \
760
            (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
761
            if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
762
        #endif
763
377
            {
764
377
        #if !defined(WOLFSSL_ESP32_CRYPT) || \
765
377
             defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
766
377
             defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
767
377
                ByteReverseWords64(sha512->buffer, sha512->buffer,
768
377
                                                         WC_SHA512_BLOCK_SIZE);
769
377
        #endif
770
377
            }
771
377
    #endif
772
377
    #if !defined(WOLFSSL_ESP32_CRYPT) || \
773
377
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
774
377
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
775
377
            ret = Transform_Sha512(sha512);
776
    #else
777
            if(sha512->ctx.mode == ESP32_SHA_INIT) {
778
                esp_sha_try_hw_lock(&sha512->ctx);
779
            }
780
            ret = esp_sha512_process(sha512);
781
            if(ret == 0 && sha512->ctx.mode == ESP32_SHA_SW){
782
                ByteReverseWords64(sha512->buffer, sha512->buffer,
783
                                                         WC_SHA512_BLOCK_SIZE);
784
                ret = Transform_Sha512(sha512);
785
            }
786
    #endif
787
377
            if (ret == 0)
788
377
                sha512->buffLen = 0;
789
0
            else
790
0
                len = 0;
791
377
        }
792
967
    }
793
794
#if defined(USE_INTEL_SPEEDUP) && \
795
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
796
    if (Transform_Sha512_Len_p != NULL) {
797
        word32 blocksLen = len & ~((word32)WC_SHA512_BLOCK_SIZE-1);
798
799
        if (blocksLen > 0) {
800
            sha512->data = data;
801
            /* Byte reversal performed in function if required. */
802
            Transform_Sha512_Len(sha512, blocksLen);
803
            data += blocksLen;
804
            len  -= blocksLen;
805
        }
806
    }
807
    else
808
#endif
809
#if !defined(LITTLE_ENDIAN_ORDER) || (defined(USE_INTEL_SPEEDUP) && \
810
        (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
811
    {
812
        while (len >= WC_SHA512_BLOCK_SIZE) {
813
            XMEMCPY(local, data, WC_SHA512_BLOCK_SIZE);
814
815
            data += WC_SHA512_BLOCK_SIZE;
816
            len  -= WC_SHA512_BLOCK_SIZE;
817
818
        #if defined(USE_INTEL_SPEEDUP) && \
819
            (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
820
            if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
821
            {
822
                ByteReverseWords64(sha512->buffer, sha512->buffer,
823
                                                          WC_SHA512_BLOCK_SIZE);
824
            }
825
        #endif
826
            /* Byte reversal performed in function if required. */
827
            ret = Transform_Sha512(sha512);
828
            if (ret != 0)
829
                break;
830
        }
831
    }
832
#else
833
1.91k
    {
834
38.8k
        while (len >= WC_SHA512_BLOCK_SIZE) {
835
36.9k
            XMEMCPY(local, data, WC_SHA512_BLOCK_SIZE);
836
837
36.9k
            data += WC_SHA512_BLOCK_SIZE;
838
36.9k
            len  -= WC_SHA512_BLOCK_SIZE;
839
36.9k
    #if !defined(WOLFSSL_ESP32_CRYPT) || \
840
36.9k
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
841
36.9k
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
842
36.9k
            ByteReverseWords64(sha512->buffer, sha512->buffer,
843
36.9k
                                                       WC_SHA512_BLOCK_SIZE);
844
36.9k
    #endif
845
36.9k
    #if !defined(WOLFSSL_ESP32_CRYPT) || \
846
36.9k
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
847
36.9k
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
848
36.9k
            ret = Transform_Sha512(sha512);
849
    #else
850
            if(sha512->ctx.mode == ESP32_SHA_INIT) {
851
                esp_sha_try_hw_lock(&sha512->ctx);
852
            }
853
            if (sha512->ctx.mode == ESP32_SHA_SW) {
854
                ByteReverseWords64(sha512->buffer, sha512->buffer,
855
                                                          WC_SHA512_BLOCK_SIZE);
856
                ret = Transform_Sha512(sha512);
857
            }
858
            else {
859
                ret = esp_sha512_process(sha512);
860
            }
861
    #endif
862
36.9k
            if (ret != 0)
863
42
                break;
864
36.9k
        } /* while (len >= WC_SHA512_BLOCK_SIZE) */
865
1.91k
    }
866
1.91k
#endif
867
868
1.91k
    if (ret == 0 && len > 0) {
869
920
        XMEMCPY(local, data, len);
870
920
        sha512->buffLen = len;
871
920
    }
872
873
1.91k
    return ret;
874
17.6k
}
875
876
#ifdef WOLFSSL_SHA512
877
878
int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
879
7.13k
{
880
7.13k
    if (sha512 == NULL || (data == NULL && len > 0)) {
881
0
        return BAD_FUNC_ARG;
882
0
    }
883
884
7.13k
#ifdef WOLF_CRYPTO_CB
885
7.13k
    #ifndef WOLF_CRYPTO_CB_FIND
886
7.13k
    if (sha512->devId != INVALID_DEVID)
887
0
    #endif
888
0
    {
889
0
        int ret = wc_CryptoCb_Sha512Hash(sha512, data, len, NULL);
890
0
        if (ret != CRYPTOCB_UNAVAILABLE)
891
0
            return ret;
892
        /* fall-through when unavailable */
893
0
    }
894
7.13k
#endif
895
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
896
    if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
897
    #if defined(HAVE_INTEL_QA)
898
        return IntelQaSymSha512(&sha512->asyncDev, NULL, data, len);
899
    #endif
900
    }
901
#endif /* WOLFSSL_ASYNC_CRYPT */
902
903
7.13k
    return Sha512Update(sha512, data, len);
904
7.13k
}
905
906
#endif /* WOLFSSL_SHA512 */
907
908
#endif /* WOLFSSL_IMX6_CAAM || WOLFSSL_SILABS_SHA384 */
909
910
911
#if defined(WOLFSSL_KCAPI_HASH)
912
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
913
#elif defined(WOLFSSL_RENESAS_RSIP) && \
914
   !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
915
    /* functions defined in wolfcrypt/src/port/renesas/renesas_fspsm_sha.c */
916
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
917
918
#else
919
920
static WC_INLINE int Sha512Final(wc_Sha512* sha512)
921
606
{
922
606
    int ret;
923
606
    byte* local;
924
925
606
    if (sha512 == NULL) {
926
0
        return BAD_FUNC_ARG;
927
0
    }
928
929
606
    local = (byte*)sha512->buffer;
930
931
    /* we'll add a 0x80 byte at the end,
932
    ** so make sure we have appropriate buffer length. */
933
606
    if (sha512->buffLen > WC_SHA512_BLOCK_SIZE - 1) {
934
0
        return BAD_STATE_E;
935
0
    } /* buffLen check */
936
937
606
    local[sha512->buffLen++] = 0x80;  /* add 1 */
938
939
    /* pad with zeros */
940
606
    if (sha512->buffLen > WC_SHA512_PAD_SIZE) {
941
45
        XMEMSET(&local[sha512->buffLen], 0, WC_SHA512_BLOCK_SIZE - sha512->buffLen);
942
45
        sha512->buffLen += WC_SHA512_BLOCK_SIZE - sha512->buffLen;
943
45
#if defined(LITTLE_ENDIAN_ORDER)
944
    #if defined(USE_INTEL_SPEEDUP) && \
945
        (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
946
        if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
947
    #endif
948
45
        {
949
950
45
       #if !defined(WOLFSSL_ESP32_CRYPT) || \
951
45
            defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
952
45
            defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
953
45
            ByteReverseWords64(sha512->buffer,sha512->buffer,
954
45
                                                         WC_SHA512_BLOCK_SIZE);
955
45
       #endif
956
45
        }
957
958
45
#endif /* LITTLE_ENDIAN_ORDER */
959
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
960
       !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
961
        if (sha512->ctx.mode == ESP32_SHA_INIT) {
962
            esp_sha_try_hw_lock(&sha512->ctx);
963
        }
964
        if (sha512->ctx.mode == ESP32_SHA_SW) {
965
            ByteReverseWords64(sha512->buffer,sha512->buffer,
966
                                                         WC_SHA512_BLOCK_SIZE);
967
            ret = Transform_Sha512(sha512);
968
        }
969
        else {
970
            ret = esp_sha512_process(sha512);
971
        }
972
    #else
973
45
        ret = Transform_Sha512(sha512);
974
45
    #endif
975
45
        if (ret != 0)
976
1
            return ret;
977
978
44
        sha512->buffLen = 0;
979
44
    } /* (sha512->buffLen > WC_SHA512_PAD_SIZE) pad with zeros */
980
981
605
    XMEMSET(&local[sha512->buffLen], 0, WC_SHA512_PAD_SIZE - sha512->buffLen);
982
983
    /* put lengths in bits */
984
605
    sha512->hiLen = (sha512->loLen >> (8 * sizeof(sha512->loLen) - 3)) +
985
605
                                                         (sha512->hiLen << 3);
986
605
    sha512->loLen = sha512->loLen << 3;
987
988
    /* store lengths */
989
605
#if defined(LITTLE_ENDIAN_ORDER)
990
    #if defined(USE_INTEL_SPEEDUP) && \
991
        (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
992
        if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
993
    #endif
994
605
    #if !defined(WOLFSSL_ESP32_CRYPT) || \
995
605
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
996
605
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
997
605
            ByteReverseWords64(sha512->buffer, sha512->buffer, WC_SHA512_PAD_SIZE);
998
605
    #endif
999
605
#endif
1000
    /* ! length ordering dependent on digest endian type ! */
1001
1002
605
#if !defined(WOLFSSL_ESP32_CRYPT) || \
1003
605
     defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
1004
605
     defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
1005
605
    sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
1006
605
    sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
1007
605
#endif
1008
1009
#if defined(USE_INTEL_SPEEDUP) && \
1010
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
1011
    if (IS_INTEL_AVX1(intel_flags) || IS_INTEL_AVX2(intel_flags))
1012
        ByteReverseWords64(&(sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
1013
                           &(sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
1014
                           WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE);
1015
#endif
1016
1017
605
#if !defined(WOLFSSL_ESP32_CRYPT) || \
1018
605
    defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
1019
605
    defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
1020
605
    ret = Transform_Sha512(sha512);
1021
#else
1022
    if(sha512->ctx.mode == ESP32_SHA_INIT) {
1023
        /* typically for tiny block: first = last */
1024
        esp_sha_try_hw_lock(&sha512->ctx);
1025
    }
1026
    if (sha512->ctx.mode == ESP32_SHA_SW) {
1027
        ByteReverseWords64(sha512->buffer,
1028
                           sha512->buffer,
1029
                           WC_SHA512_BLOCK_SIZE);
1030
        sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
1031
        sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
1032
        ret = Transform_Sha512(sha512);
1033
    }
1034
    else {
1035
        ret = esp_sha512_digest_process(sha512, 1);
1036
    }
1037
#endif
1038
1039
605
    if (ret != 0)
1040
12
        return ret;
1041
1042
593
    #ifdef LITTLE_ENDIAN_ORDER
1043
593
        ByteReverseWords64(sha512->digest, sha512->digest, WC_SHA512_DIGEST_SIZE);
1044
593
    #endif
1045
1046
1047
593
    return 0;
1048
605
}
1049
1050
#endif /* WOLFSSL_KCAPI_HASH */
1051
1052
#ifdef WOLFSSL_SHA512
1053
1054
#if defined(WOLFSSL_KCAPI_HASH)
1055
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
1056
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
1057
1058
#elif defined(WOLFSSL_RENESAS_RSIP) && \
1059
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
1060
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
1061
1062
#else
1063
1064
static int Sha512FinalRaw(wc_Sha512* sha512, byte* hash, size_t digestSz)
1065
0
{
1066
0
#ifdef LITTLE_ENDIAN_ORDER
1067
0
    word64 digest[WC_SHA512_DIGEST_SIZE / sizeof(word64)];
1068
0
#endif
1069
1070
0
    if (sha512 == NULL || hash == NULL) {
1071
0
        return BAD_FUNC_ARG;
1072
0
    }
1073
1074
0
#ifdef LITTLE_ENDIAN_ORDER
1075
0
    ByteReverseWords64((word64*)digest, (word64*)sha512->digest,
1076
0
                                                         WC_SHA512_DIGEST_SIZE);
1077
0
    XMEMCPY(hash, digest, digestSz);
1078
#else
1079
    XMEMCPY(hash, sha512->digest, digestSz);
1080
#endif
1081
1082
0
    return 0;
1083
0
}
1084
1085
int wc_Sha512FinalRaw(wc_Sha512* sha512, byte* hash)
1086
0
{
1087
0
    return Sha512FinalRaw(sha512, hash, WC_SHA512_DIGEST_SIZE);
1088
0
}
1089
1090
static int Sha512_Family_Final(wc_Sha512* sha512, byte* hash, size_t digestSz,
1091
                               int (*initfp)(wc_Sha512*))
1092
254
{
1093
254
    int ret;
1094
1095
254
    if (sha512 == NULL || hash == NULL) {
1096
0
        return BAD_FUNC_ARG;
1097
0
    }
1098
1099
254
#ifdef WOLF_CRYPTO_CB
1100
254
    #ifndef WOLF_CRYPTO_CB_FIND
1101
254
    if (sha512->devId != INVALID_DEVID)
1102
0
    #endif
1103
0
    {
1104
0
        byte localHash[WC_SHA512_DIGEST_SIZE];
1105
0
        ret = wc_CryptoCb_Sha512Hash(sha512, NULL, 0, localHash);
1106
0
        if (ret != CRYPTOCB_UNAVAILABLE) {
1107
0
            XMEMCPY(hash, localHash, digestSz);
1108
0
            return ret;
1109
0
        }
1110
        /* fall-through when unavailable */
1111
0
    }
1112
254
#endif
1113
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
1114
    if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
1115
    #if defined(HAVE_INTEL_QA)
1116
        return IntelQaSymSha512(&sha512->asyncDev, hash, NULL, digestSz);
1117
    #endif
1118
    }
1119
#endif /* WOLFSSL_ASYNC_CRYPT */
1120
1121
254
    ret = Sha512Final(sha512);
1122
254
    if (ret != 0)
1123
6
        return ret;
1124
1125
248
    XMEMCPY(hash, sha512->digest, digestSz);
1126
1127
    /* initialize Sha512 structure for the next use */
1128
248
    return initfp(sha512);
1129
254
}
1130
1131
int wc_Sha512Final(wc_Sha512* sha512, byte* hash)
1132
254
{
1133
254
    return Sha512_Family_Final(sha512, hash, WC_SHA512_DIGEST_SIZE, InitSha512);
1134
254
}
1135
1136
#endif /* WOLFSSL_KCAPI_HASH */
1137
1138
#if !defined(WOLFSSL_SE050) || !defined(WOLFSSL_SE050_HASH)
1139
int wc_InitSha512(wc_Sha512* sha512)
1140
105
{
1141
105
    int devId = INVALID_DEVID;
1142
1143
105
#ifdef WOLF_CRYPTO_CB
1144
105
    devId = wc_CryptoCb_DefaultDevID();
1145
105
#endif
1146
105
    return wc_InitSha512_ex(sha512, NULL, devId);
1147
105
}
1148
1149
void wc_Sha512Free(wc_Sha512* sha512)
1150
179
{
1151
179
    if (sha512 == NULL)
1152
0
        return;
1153
1154
#if defined(WOLFSSL_ESP32) && \
1155
    !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)  && \
1156
    !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
1157
    esp_sha_release_unfinished_lock(&sha512->ctx);
1158
#endif
1159
1160
#ifdef WOLFSSL_SMALL_STACK_CACHE
1161
    if (sha512->W != NULL) {
1162
        ForceZero(sha512->W, sizeof(word64) * 16);
1163
        XFREE(sha512->W, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
1164
        sha512->W = NULL;
1165
    }
1166
#endif
1167
1168
#if defined(WOLFSSL_KCAPI_HASH)
1169
    KcapiHashFree(&sha512->kcapi);
1170
#endif
1171
1172
#if defined(WOLFSSL_HASH_KEEP)
1173
    if (sha512->msg != NULL) {
1174
        ForceZero(sha512->msg, sha512->len);
1175
        XFREE(sha512->msg, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
1176
        sha512->msg = NULL;
1177
    }
1178
#endif
1179
1180
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
1181
    wolfAsync_DevCtxFree(&sha512->asyncDev, WOLFSSL_ASYNC_MARKER_SHA512);
1182
#endif /* WOLFSSL_ASYNC_CRYPT */
1183
1184
179
    ForceZero(sha512, sizeof(*sha512));
1185
179
}
1186
#if (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) \
1187
    && !defined(WOLFSSL_KCAPI_HASH)
1188
/* Apply SHA512 transformation to the data                */
1189
/* @param sha  a pointer to wc_Sha512 structure           */
1190
/* @param data data to be applied SHA512 transformation   */
1191
/* @return 0 on successful, otherwise non-zero on failure */
1192
int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data)
1193
{
1194
    int ret;
1195
    /* back up buffer */
1196
#ifdef WOLFSSL_SMALL_STACK
1197
    word64 *buffer;
1198
#else
1199
    word64  buffer[WC_SHA512_BLOCK_SIZE  / sizeof(word64)];
1200
#endif
1201
1202
    /* sanity check */
1203
    if (sha == NULL || data == NULL) {
1204
        return BAD_FUNC_ARG;
1205
    }
1206
1207
#ifdef WOLFSSL_SMALL_STACK
1208
    buffer = (word64*)XMALLOC(WC_SHA512_BLOCK_SIZE, sha->heap,
1209
        DYNAMIC_TYPE_TMP_BUFFER);
1210
    if (buffer == NULL)
1211
        return MEMORY_E;
1212
#endif
1213
1214
#if defined(USE_INTEL_SPEEDUP) && \
1215
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
1216
    Sha512_SetTransform();
1217
#endif
1218
1219
#if defined(LITTLE_ENDIAN_ORDER)
1220
#if defined(USE_INTEL_SPEEDUP) && \
1221
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
1222
    if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
1223
#endif
1224
    {
1225
        ByteReverseWords64((word64*)data, (word64*)data,
1226
                                                WC_SHA512_BLOCK_SIZE);
1227
    }
1228
#endif /* LITTLE_ENDIAN_ORDER */
1229
1230
    XMEMCPY(buffer, sha->buffer, WC_SHA512_BLOCK_SIZE);
1231
    XMEMCPY(sha->buffer, data, WC_SHA512_BLOCK_SIZE);
1232
1233
    ret = Transform_Sha512(sha);
1234
1235
    XMEMCPY(sha->buffer, buffer, WC_SHA512_BLOCK_SIZE);
1236
#ifdef WOLFSSL_SMALL_STACK
1237
    ForceZero(buffer, WC_SHA512_BLOCK_SIZE);
1238
    XFREE(buffer, sha->heap, DYNAMIC_TYPE_TMP_BUFFER);
1239
#endif
1240
    return ret;
1241
}
1242
#endif /* OPENSSL_EXTRA */
1243
#endif /* WOLFSSL_SHA512 */
1244
#endif /* !WOLFSSL_SE050 || !WOLFSSL_SE050_HASH */
1245
1246
1247
/* -------------------------------------------------------------------------- */
1248
/* SHA384 */
1249
/* -------------------------------------------------------------------------- */
1250
#ifdef WOLFSSL_SHA384
1251
1252
#if defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
1253
    !defined(WOLFSSL_QNX_CAAM)
1254
    /* functions defined in wolfcrypt/src/port/caam/caam_sha.c */
1255
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
1256
    int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
1257
    {
1258
        if (sha384 == NULL) {
1259
            return BAD_FUNC_ARG;
1260
        }
1261
        (void)devId;
1262
        return se050_hash_init(&sha384->se050Ctx, heap);
1263
    }
1264
    int wc_Sha384Update(wc_Sha384* sha384, const byte* data, word32 len)
1265
    {
1266
        return se050_hash_update(&sha384->se050Ctx, data, len);
1267
1268
    }
1269
    int wc_Sha384Final(wc_Sha384* sha384, byte* hash)
1270
    {
1271
        int ret = 0;
1272
        ret = se050_hash_final(&sha384->se050Ctx, hash, WC_SHA384_DIGEST_SIZE,
1273
                               kAlgorithm_SSS_SHA384);
1274
        return ret;
1275
    }
1276
    int wc_Sha384FinalRaw(wc_Sha384* sha384, byte* hash)
1277
    {
1278
        int ret = 0;
1279
        ret = se050_hash_final(&sha384->se050Ctx, hash, WC_SHA384_DIGEST_SIZE,
1280
                               kAlgorithm_SSS_SHA384);
1281
        return ret;
1282
    }
1283
1284
#elif defined(WOLFSSL_SILABS_SHA512)
1285
    /* functions defined in wolfcrypt/src/port/silabs/silabs_hash.c */
1286
1287
#elif defined(WOLFSSL_KCAPI_HASH)
1288
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
1289
1290
#elif defined(WOLFSSL_RENESAS_RSIP) && \
1291
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
1292
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
1293
1294
#else
1295
1296
static int InitSha384(wc_Sha384* sha384)
1297
582
{
1298
582
    if (sha384 == NULL) {
1299
0
        return BAD_FUNC_ARG;
1300
0
    }
1301
1302
582
    sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8);
1303
582
    sha384->digest[1] = W64LIT(0x629a292a367cd507);
1304
582
    sha384->digest[2] = W64LIT(0x9159015a3070dd17);
1305
582
    sha384->digest[3] = W64LIT(0x152fecd8f70e5939);
1306
582
    sha384->digest[4] = W64LIT(0x67332667ffc00b31);
1307
582
    sha384->digest[5] = W64LIT(0x8eb44a8768581511);
1308
582
    sha384->digest[6] = W64LIT(0xdb0c2e0d64f98fa7);
1309
582
    sha384->digest[7] = W64LIT(0x47b5481dbefa4fa4);
1310
1311
582
    sha384->buffLen = 0;
1312
582
    sha384->loLen   = 0;
1313
582
    sha384->hiLen   = 0;
1314
1315
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)  && \
1316
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
1317
    /* HW needs to be carefully initialized, taking into account soft copy.
1318
    ** If already in use; copy may revert to SW as needed. */
1319
    esp_sha_init(&(sha384->ctx), WC_HASH_TYPE_SHA384);
1320
#endif
1321
1322
582
#ifdef WOLFSSL_HASH_FLAGS
1323
582
    sha384->flags = 0;
1324
582
#endif
1325
1326
#ifdef HAVE_ARIA
1327
    sha384->hSession = NULL;
1328
#endif
1329
1330
#ifdef WOLFSSL_HASH_KEEP
1331
    sha384->msg  = NULL;
1332
    sha384->len  = 0;
1333
    sha384->used = 0;
1334
#endif
1335
1336
582
    return 0;
1337
582
}
1338
1339
int wc_Sha384Update(wc_Sha384* sha384, const byte* data, word32 len)
1340
10.4k
{
1341
10.4k
    if (sha384 == NULL || (data == NULL && len > 0)) {
1342
0
        return BAD_FUNC_ARG;
1343
0
    }
1344
1345
10.4k
#ifdef WOLF_CRYPTO_CB
1346
10.4k
    #ifndef WOLF_CRYPTO_CB_FIND
1347
10.4k
    if (sha384->devId != INVALID_DEVID)
1348
0
    #endif
1349
0
    {
1350
0
        int ret = wc_CryptoCb_Sha384Hash(sha384, data, len, NULL);
1351
0
        if (ret != CRYPTOCB_UNAVAILABLE)
1352
0
            return ret;
1353
        /* fall-through when unavailable */
1354
0
    }
1355
10.4k
#endif
1356
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
1357
    if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
1358
    #if defined(HAVE_INTEL_QA)
1359
        return IntelQaSymSha384(&sha384->asyncDev, NULL, data, len);
1360
    #endif
1361
    }
1362
#endif /* WOLFSSL_ASYNC_CRYPT */
1363
1364
10.4k
    return Sha512Update((wc_Sha512*)sha384, data, len);
1365
10.4k
}
1366
1367
1368
int wc_Sha384FinalRaw(wc_Sha384* sha384, byte* hash)
1369
0
{
1370
0
#ifdef LITTLE_ENDIAN_ORDER
1371
0
    word64 digest[WC_SHA384_DIGEST_SIZE / sizeof(word64)];
1372
0
#endif
1373
1374
0
    if (sha384 == NULL || hash == NULL) {
1375
0
        return BAD_FUNC_ARG;
1376
0
    }
1377
1378
0
#ifdef LITTLE_ENDIAN_ORDER
1379
0
    ByteReverseWords64((word64*)digest, (word64*)sha384->digest,
1380
0
                                                         WC_SHA384_DIGEST_SIZE);
1381
0
    XMEMCPY(hash, digest, WC_SHA384_DIGEST_SIZE);
1382
#else
1383
    XMEMCPY(hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
1384
#endif
1385
1386
0
    return 0;
1387
0
}
1388
1389
int wc_Sha384Final(wc_Sha384* sha384, byte* hash)
1390
352
{
1391
352
    int ret;
1392
1393
352
    if (sha384 == NULL || hash == NULL) {
1394
0
        return BAD_FUNC_ARG;
1395
0
    }
1396
1397
352
#ifdef WOLF_CRYPTO_CB
1398
352
    #ifndef WOLF_CRYPTO_CB_FIND
1399
352
    if (sha384->devId != INVALID_DEVID)
1400
0
    #endif
1401
0
    {
1402
0
        ret = wc_CryptoCb_Sha384Hash(sha384, NULL, 0, hash);
1403
0
        if (ret != CRYPTOCB_UNAVAILABLE)
1404
0
            return ret;
1405
        /* fall-through when unavailable */
1406
0
    }
1407
352
#endif
1408
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
1409
    if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
1410
    #if defined(HAVE_INTEL_QA)
1411
        return IntelQaSymSha384(&sha384->asyncDev, hash, NULL,
1412
                                            WC_SHA384_DIGEST_SIZE);
1413
    #endif
1414
    }
1415
#endif /* WOLFSSL_ASYNC_CRYPT */
1416
1417
352
    ret = Sha512Final((wc_Sha512*)sha384);
1418
352
    if (ret != 0)
1419
7
        return ret;
1420
1421
345
    XMEMCPY(hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
1422
1423
345
    return InitSha384(sha384);  /* reset state */
1424
352
}
1425
1426
int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
1427
237
{
1428
237
    int ret;
1429
1430
237
    if (sha384 == NULL) {
1431
0
        return BAD_FUNC_ARG;
1432
0
    }
1433
1434
237
    sha384->heap = heap;
1435
#ifdef WOLFSSL_SMALL_STACK_CACHE
1436
    sha384->W = NULL;
1437
#endif
1438
237
#ifdef WOLF_CRYPTO_CB
1439
237
    sha384->devId = devId;
1440
237
    sha384->devCtx = NULL;
1441
237
#endif
1442
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)  && \
1443
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
1444
    if (sha384->ctx.mode != ESP32_SHA_INIT) {
1445
        ESP_LOGV(TAG, "Set ctx mode from prior value: "
1446
                           "%d", sha384->ctx.mode);
1447
    }
1448
    /* We know this is a fresh, uninitialized item, so set to INIT */
1449
    sha384->ctx.mode = ESP32_SHA_INIT;
1450
#endif
1451
1452
237
    ret = InitSha384(sha384);
1453
237
    if (ret != 0) {
1454
0
        return ret;
1455
0
    }
1456
1457
#if defined(USE_INTEL_SPEEDUP) && \
1458
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
1459
    Sha512_SetTransform();
1460
#endif
1461
1462
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
1463
    ret = wolfAsync_DevCtxInit(&sha384->asyncDev, WOLFSSL_ASYNC_MARKER_SHA384,
1464
                                                           sha384->heap, devId);
1465
#else
1466
237
    (void)devId;
1467
237
#endif /* WOLFSSL_ASYNC_CRYPT */
1468
#ifdef WOLFSSL_IMXRT1170_CAAM
1469
     ret = wc_CAAM_HashInit(&sha384->hndl, &sha384->ctx, WC_HASH_TYPE_SHA384);
1470
#endif
1471
237
    return ret;
1472
237
}
1473
1474
#endif /* WOLFSSL_IMX6_CAAM || WOLFSSL_SILABS_SHA512 || WOLFSSL_KCAPI_HASH */
1475
1476
int wc_InitSha384(wc_Sha384* sha384)
1477
120
{
1478
120
    int devId = INVALID_DEVID;
1479
1480
120
#ifdef WOLF_CRYPTO_CB
1481
120
    devId = wc_CryptoCb_DefaultDevID();
1482
120
#endif
1483
120
    return wc_InitSha384_ex(sha384, NULL, devId);
1484
120
}
1485
1486
void wc_Sha384Free(wc_Sha384* sha384)
1487
237
{
1488
237
    if (sha384 == NULL)
1489
0
        return;
1490
1491
#if defined(WOLFSSL_ESP32) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)  && \
1492
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
1493
    esp_sha_release_unfinished_lock(&sha384->ctx);
1494
#endif
1495
1496
#ifdef WOLFSSL_SMALL_STACK_CACHE
1497
    if (sha384->W != NULL) {
1498
        ForceZero(sha384->W, sizeof(word64) * 16);
1499
        XFREE(sha384->W, sha384->heap, DYNAMIC_TYPE_TMP_BUFFER);
1500
        sha384->W = NULL;
1501
    }
1502
#endif
1503
1504
#if defined(WOLFSSL_KCAPI_HASH)
1505
    KcapiHashFree(&sha384->kcapi);
1506
#endif
1507
1508
#if defined(WOLFSSL_HASH_KEEP)
1509
    if (sha384->msg != NULL) {
1510
        ForceZero(sha384->msg, sha384->len);
1511
        XFREE(sha384->msg, sha384->heap, DYNAMIC_TYPE_TMP_BUFFER);
1512
        sha384->msg = NULL;
1513
    }
1514
#endif
1515
1516
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
1517
    se050_hash_free(&sha384->se050Ctx);
1518
#endif
1519
1520
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
1521
    wolfAsync_DevCtxFree(&sha384->asyncDev, WOLFSSL_ASYNC_MARKER_SHA384);
1522
#endif /* WOLFSSL_ASYNC_CRYPT */
1523
1524
#ifdef HAVE_ARIA
1525
    if (sha384->hSession != NULL) {
1526
        MC_CloseSession(sha384->hSession);
1527
        sha384->hSession = NULL;
1528
    }
1529
#endif
1530
1531
237
    ForceZero(sha384, sizeof(*sha384));
1532
237
}
1533
1534
#endif /* WOLFSSL_SHA384 */
1535
1536
#ifdef WOLFSSL_SHA512
1537
1538
#if defined(WOLFSSL_KCAPI_HASH)
1539
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
1540
1541
#elif defined(WOLFSSL_RENESAS_RSIP) && \
1542
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
1543
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
1544
1545
#else
1546
1547
static int Sha512_Family_GetHash(wc_Sha512* sha512, byte* hash,
1548
                                 int (*finalfp)(wc_Sha512*, byte*))
1549
0
{
1550
0
    int ret;
1551
0
#ifdef WOLFSSL_SMALL_STACK
1552
0
    wc_Sha512* tmpSha512;
1553
#else
1554
    wc_Sha512  tmpSha512[1];
1555
#endif
1556
1557
0
    if (sha512 == NULL || hash == NULL) {
1558
0
        return BAD_FUNC_ARG;
1559
0
    }
1560
1561
0
#ifdef WOLFSSL_SMALL_STACK
1562
0
    tmpSha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL,
1563
0
        DYNAMIC_TYPE_TMP_BUFFER);
1564
0
    if (tmpSha512 == NULL) {
1565
0
        return MEMORY_E;
1566
0
    }
1567
0
#endif
1568
1569
    /* copy this sha512 into tmpSha */
1570
0
    ret = wc_Sha512Copy(sha512, tmpSha512);
1571
0
    if (ret == 0) {
1572
0
        ret = finalfp(tmpSha512, hash);
1573
0
        wc_Sha512Free(tmpSha512);
1574
0
    }
1575
1576
0
#ifdef WOLFSSL_SMALL_STACK
1577
0
    XFREE(tmpSha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1578
0
#endif
1579
1580
0
    return ret;
1581
0
}
1582
1583
int wc_Sha512GetHash(wc_Sha512* sha512, byte* hash)
1584
0
{
1585
0
    return Sha512_Family_GetHash(sha512, hash, wc_Sha512Final);
1586
0
}
1587
1588
int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
1589
8
{
1590
8
    int ret = 0;
1591
1592
8
    if (src == NULL || dst == NULL) {
1593
0
        return BAD_FUNC_ARG;
1594
0
    }
1595
1596
8
    XMEMCPY(dst, src, sizeof(wc_Sha512));
1597
#ifdef WOLFSSL_SMALL_STACK_CACHE
1598
    dst->W = NULL;
1599
#endif
1600
1601
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \
1602
    defined(WOLFSSL_SILABS_SHA512)
1603
    dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
1604
    dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
1605
#endif
1606
1607
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
1608
    ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
1609
#endif
1610
1611
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
1612
    #if defined(CONFIG_IDF_TARGET_ESP32)
1613
    if (ret == 0) {
1614
        ret = esp_sha512_ctx_copy(src, dst);
1615
    }
1616
    #elif defined(CONFIG_IDF_TARGET_ESP32C3) || \
1617
          defined(CONFIG_IDF_TARGET_ESP32C6)
1618
        ESP_LOGV(TAG, "No SHA-512 HW on the ESP32-C3");
1619
    #elif defined(CONFIG_IDF_TARGET_ESP32S2) || \
1620
          defined(CONFIG_IDF_TARGET_ESP32S3)
1621
    if (ret == 0) {
1622
        ret = esp_sha512_ctx_copy(src, dst);
1623
    }
1624
    #else
1625
        ESP_LOGW(TAG, "No SHA384 HW or not yet implemented for %s",
1626
                       CONFIG_IDF_TARGET);
1627
    #endif
1628
1629
#endif
1630
1631
8
#ifdef WOLFSSL_HASH_FLAGS
1632
8
     dst->flags |= WC_HASH_FLAG_ISCOPY;
1633
8
#endif
1634
1635
#if defined(WOLFSSL_HASH_KEEP)
1636
    if (src->msg != NULL) {
1637
        dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
1638
        if (dst->msg == NULL)
1639
            return MEMORY_E;
1640
        XMEMCPY(dst->msg, src->msg, src->len);
1641
    }
1642
#endif
1643
1644
8
    return ret;
1645
8
}
1646
1647
#endif /* WOLFSSL_KCAPI_HASH */
1648
1649
#ifdef WOLFSSL_HASH_FLAGS
1650
int wc_Sha512SetFlags(wc_Sha512* sha512, word32 flags)
1651
0
{
1652
0
    if (sha512) {
1653
0
        sha512->flags = flags;
1654
0
    }
1655
0
    return 0;
1656
0
}
1657
int wc_Sha512GetFlags(wc_Sha512* sha512, word32* flags)
1658
0
{
1659
0
    if (sha512 && flags) {
1660
0
        *flags = sha512->flags;
1661
0
    }
1662
0
    return 0;
1663
0
}
1664
#endif /* WOLFSSL_HASH_FLAGS */
1665
1666
#if !defined(WOLFSSL_NOSHA512_224) && \
1667
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
1668
1669
1670
int wc_InitSha512_224(wc_Sha512* sha)
1671
0
{
1672
0
    return wc_InitSha512_224_ex(sha, NULL, INVALID_DEVID);
1673
0
}
1674
1675
int wc_Sha512_224Update(wc_Sha512* sha, const byte* data, word32 len)
1676
0
{
1677
0
    return wc_Sha512Update(sha, data, len);
1678
0
}
1679
1680
#if defined(WOLFSSL_KCAPI_HASH)
1681
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
1682
#elif defined(WOLFSSL_RENESAS_RSIP) && \
1683
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
1684
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
1685
1686
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
1687
1688
#else
1689
int wc_Sha512_224FinalRaw(wc_Sha512* sha, byte* hash)
1690
0
{
1691
0
    return Sha512FinalRaw(sha, hash, WC_SHA512_224_DIGEST_SIZE);
1692
0
}
1693
1694
int wc_Sha512_224Final(wc_Sha512* sha512, byte* hash)
1695
0
{
1696
0
    return Sha512_Family_Final(sha512, hash, WC_SHA512_224_DIGEST_SIZE,
1697
0
                               InitSha512_224);
1698
0
}
1699
#endif /* else none of the above: WOLFSSL_KCAPI_HASH, WOLFSSL_SE050 */
1700
1701
void wc_Sha512_224Free(wc_Sha512* sha)
1702
0
{
1703
0
    wc_Sha512Free(sha);
1704
0
}
1705
1706
#if defined(WOLFSSL_KCAPI_HASH)
1707
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
1708
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
1709
1710
#elif defined(WOLFSSL_RENESAS_RSIP) && \
1711
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
1712
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
1713
1714
#else
1715
int wc_Sha512_224GetHash(wc_Sha512* sha512, byte* hash)
1716
0
{
1717
0
    return Sha512_Family_GetHash(sha512, hash, wc_Sha512_224Final);
1718
0
}
1719
1720
int wc_Sha512_224Copy(wc_Sha512* src, wc_Sha512* dst)
1721
0
{
1722
0
    return wc_Sha512Copy(src, dst);
1723
0
}
1724
#endif /* else none of the above: WOLFSSL_KCAPI_HASH, WOLFSSL_SE050 */
1725
1726
#ifdef WOLFSSL_HASH_FLAGS
1727
int wc_Sha512_224SetFlags(wc_Sha512* sha, word32 flags)
1728
0
{
1729
0
    return wc_Sha512SetFlags(sha, flags);
1730
0
}
1731
int wc_Sha512_224GetFlags(wc_Sha512* sha, word32* flags)
1732
0
{
1733
0
    return wc_Sha512GetFlags(sha, flags);
1734
0
}
1735
#endif /* WOLFSSL_HASH_FLAGS */
1736
1737
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
1738
int wc_Sha512_224Transform(wc_Sha512* sha, const unsigned char* data)
1739
{
1740
    return wc_Sha512Transform(sha, data);
1741
}
1742
#endif /* OPENSSL_EXTRA */
1743
1744
1745
#endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
1746
1747
#if !defined(WOLFSSL_NOSHA512_256) && \
1748
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
1749
1750
int wc_InitSha512_256(wc_Sha512* sha)
1751
0
{
1752
0
    return wc_InitSha512_256_ex(sha, NULL, INVALID_DEVID);
1753
0
}
1754
1755
int wc_Sha512_256Update(wc_Sha512* sha, const byte* data, word32 len)
1756
0
{
1757
0
    return wc_Sha512Update(sha, data, len);
1758
0
}
1759
#if defined(WOLFSSL_KCAPI_HASH)
1760
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
1761
#elif defined(WOLFSSL_RENESAS_RSIP) && \
1762
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
1763
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
1764
1765
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
1766
1767
#else
1768
int wc_Sha512_256FinalRaw(wc_Sha512* sha, byte* hash)
1769
0
{
1770
0
    return Sha512FinalRaw(sha, hash, WC_SHA512_256_DIGEST_SIZE);
1771
0
}
1772
1773
int wc_Sha512_256Final(wc_Sha512* sha512, byte* hash)
1774
0
{
1775
0
    return Sha512_Family_Final(sha512, hash, WC_SHA512_256_DIGEST_SIZE,
1776
0
                               InitSha512_256);
1777
0
}
1778
#endif
1779
1780
void wc_Sha512_256Free(wc_Sha512* sha)
1781
0
{
1782
0
    wc_Sha512Free(sha);
1783
0
}
1784
1785
#if defined(WOLFSSL_KCAPI_HASH)
1786
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
1787
#elif defined(WOLFSSL_RENESAS_RSIP) && \
1788
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
1789
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
1790
1791
#else
1792
int wc_Sha512_256GetHash(wc_Sha512* sha512, byte* hash)
1793
0
{
1794
0
    return Sha512_Family_GetHash(sha512, hash, wc_Sha512_256Final);
1795
0
}
1796
int wc_Sha512_256Copy(wc_Sha512* src, wc_Sha512* dst)
1797
0
{
1798
0
    return wc_Sha512Copy(src, dst);
1799
0
}
1800
#endif
1801
1802
#ifdef WOLFSSL_HASH_FLAGS
1803
int wc_Sha512_256SetFlags(wc_Sha512* sha, word32 flags)
1804
0
{
1805
0
    return wc_Sha512SetFlags(sha, flags);
1806
0
}
1807
int wc_Sha512_256GetFlags(wc_Sha512* sha, word32* flags)
1808
0
{
1809
0
    return wc_Sha512GetFlags(sha, flags);
1810
0
}
1811
#endif /* WOLFSSL_HASH_FLAGS */
1812
1813
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
1814
int wc_Sha512_256Transform(wc_Sha512* sha, const unsigned char* data)
1815
{
1816
    return wc_Sha512Transform(sha, data);
1817
}
1818
#endif /* OPENSSL_EXTRA */
1819
1820
1821
#endif /* !WOLFSSL_NOSHA512_256 && !FIPS ... */
1822
1823
#endif /* WOLFSSL_SHA512 */
1824
1825
#ifdef WOLFSSL_SHA384
1826
1827
#if defined(WOLFSSL_KCAPI_HASH)
1828
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
1829
#elif defined(WOLFSSL_RENESAS_RSIP) && \
1830
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
1831
    /* functions defined in wolfcrypt/src/port/renesas/renesas_fspsm_sha.c */
1832
#else
1833
1834
int wc_Sha384GetHash(wc_Sha384* sha384, byte* hash)
1835
0
{
1836
0
    int ret;
1837
0
#ifdef WOLFSSL_SMALL_STACK
1838
0
    wc_Sha384* tmpSha384;
1839
#else
1840
    wc_Sha384  tmpSha384[1];
1841
#endif
1842
1843
0
    if (sha384 == NULL || hash == NULL) {
1844
0
        return BAD_FUNC_ARG;
1845
0
    }
1846
1847
0
#ifdef WOLFSSL_SMALL_STACK
1848
0
    tmpSha384 = (wc_Sha384*)XMALLOC(sizeof(wc_Sha384), NULL,
1849
0
        DYNAMIC_TYPE_TMP_BUFFER);
1850
0
    if (tmpSha384 == NULL) {
1851
0
        return MEMORY_E;
1852
0
    }
1853
0
#endif
1854
1855
    /* copy this sha384 into tmpSha */
1856
0
    ret = wc_Sha384Copy(sha384, tmpSha384);
1857
0
    if (ret == 0) {
1858
0
        ret = wc_Sha384Final(tmpSha384, hash);
1859
0
        wc_Sha384Free(tmpSha384);
1860
0
    }
1861
1862
0
#ifdef WOLFSSL_SMALL_STACK
1863
0
    XFREE(tmpSha384, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1864
0
#endif
1865
1866
0
    return ret;
1867
0
}
1868
1869
int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
1870
9
{
1871
9
    int ret = 0;
1872
1873
9
    if (src == NULL || dst == NULL) {
1874
0
        return BAD_FUNC_ARG;
1875
0
    }
1876
1877
9
    XMEMCPY(dst, src, sizeof(wc_Sha384));
1878
1879
#ifdef WOLFSSL_SMALL_STACK_CACHE
1880
    dst->W = NULL;
1881
#endif
1882
1883
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \
1884
    defined(WOLFSSL_SILABS_SHA384)
1885
    dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
1886
    dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
1887
#endif
1888
1889
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
1890
    ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
1891
#endif
1892
1893
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
1894
    #if defined(CONFIG_IDF_TARGET_ESP32)
1895
        esp_sha384_ctx_copy(src, dst);
1896
    #elif defined(CONFIG_IDF_TARGET_ESP32C3) || \
1897
          defined(CONFIG_IDF_TARGET_ESP32C6)
1898
        ESP_LOGV(TAG, "No SHA-384 HW on the ESP32-C3");
1899
    #elif defined(CONFIG_IDF_TARGET_ESP32S2) || \
1900
          defined(CONFIG_IDF_TARGET_ESP32S3)
1901
        esp_sha384_ctx_copy(src, dst);
1902
    #else
1903
        ESP_LOGW(TAG, "No SHA384 HW or not yet implemented for %s",
1904
                       CONFIG_IDF_TARGET);
1905
    #endif
1906
#endif
1907
1908
#ifdef HAVE_ARIA
1909
    dst->hSession = NULL;
1910
    if((src->hSession != NULL) && (MC_CopySession(src->hSession, &(dst->hSession)) != MC_OK)) {
1911
        return MEMORY_E;
1912
    }
1913
#endif
1914
1915
9
#ifdef WOLFSSL_HASH_FLAGS
1916
9
     dst->flags |= WC_HASH_FLAG_ISCOPY;
1917
9
#endif
1918
1919
#if defined(WOLFSSL_HASH_KEEP)
1920
    if (src->msg != NULL) {
1921
        dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
1922
        if (dst->msg == NULL)
1923
            return MEMORY_E;
1924
        XMEMCPY(dst->msg, src->msg, src->len);
1925
    }
1926
#endif
1927
1928
9
    return ret;
1929
9
}
1930
1931
#endif /* WOLFSSL_KCAPI_HASH */
1932
1933
#ifdef WOLFSSL_HASH_FLAGS
1934
int wc_Sha384SetFlags(wc_Sha384* sha384, word32 flags)
1935
0
{
1936
0
    if (sha384) {
1937
0
        sha384->flags = flags;
1938
0
    }
1939
0
    return 0;
1940
0
}
1941
int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags)
1942
0
{
1943
0
    if (sha384 && flags) {
1944
0
        *flags = sha384->flags;
1945
0
    }
1946
0
    return 0;
1947
0
}
1948
#endif
1949
1950
#endif /* WOLFSSL_SHA384 */
1951
1952
#ifdef WOLFSSL_HASH_KEEP
1953
/* Some hardware have issues with update, this function stores the data to be
1954
 * hashed into an array. Once ready, the Final operation is called on all of the
1955
 * data to be hashed at once.
1956
 * returns 0 on success
1957
 */
1958
int wc_Sha512_Grow(wc_Sha512* sha512, const byte* in, int inSz)
1959
{
1960
    return _wc_Hash_Grow(&(sha512->msg), &(sha512->used), &(sha512->len), in,
1961
                        inSz, sha512->heap);
1962
}
1963
#ifdef WOLFSSL_SHA384
1964
int wc_Sha384_Grow(wc_Sha384* sha384, const byte* in, int inSz)
1965
{
1966
    return _wc_Hash_Grow(&(sha384->msg), &(sha384->used), &(sha384->len), in,
1967
                        inSz, sha384->heap);
1968
}
1969
#endif /* WOLFSSL_SHA384 */
1970
#endif /* WOLFSSL_HASH_KEEP */
1971
#endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */