Coverage Report

Created: 2026-10-31 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl-normal-math/wolfcrypt/src/sha512.c
Line
Count
Source
1
/* sha512.c
2
 *
3
 * Copyright (C) 2006-2026 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 3 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
 * SHA-512/384 Build Options:
24
 *
25
 * Core:
26
 * WOLFSSL_SHA512:           Enable SHA-512 support                default: off
27
 * WOLFSSL_SHA384:           Enable SHA-384 support                default: off
28
 * WOLFSSL_NOSHA512_224:     Disable SHA-512/224 variant           default: off
29
 * WOLFSSL_NOSHA512_256:     Disable SHA-512/256 variant           default: off
30
 *
31
 * Performance:
32
 * USE_SLOW_SHA512:          Disable SHA-512 loop unrolling        default: off
33
 * USE_SLOW_SHA2:            Disable SHA-2 loop unrolling          default: off
34
 * WOLFSSL_HASH_FLAGS:       Enable hash flags for state tracking  default: off
35
 * WOLFSSL_HASH_KEEP:        Keep hash input data for reuse        default: off
36
 * WOLFSSL_SMALL_STACK_CACHE: Cache hash state on small stack      default: off
37
 * WC_NO_INTERNAL_FUNCTION_POINTERS: Disable internal func ptrs   default: off
38
 *
39
 * Hardware Acceleration (SHA-512-specific):
40
 * WC_ASYNC_ENABLE_SHA512:   Enable async SHA-512 operations       default: off
41
 * WC_ASYNC_ENABLE_SHA384:   Enable async SHA-384 operations       default: off
42
 * WOLFSSL_KCAPI_HASH:       Linux kernel crypto API for hashing  default: off
43
 * WOLFSSL_SE050_HASH:       SE050 hardware hashing               default: off
44
 * WOLFSSL_SILABS_SHA384:    Silicon Labs SHA-384 acceleration    default: off
45
 * WOLFSSL_SILABS_SHA512:    Silicon Labs SHA-512 acceleration    default: off
46
 * NO_IMX6_CAAM_HASH:        Disable i.MX6 CAAM hash             default: off
47
 * NO_WOLFSSL_ESP32_CRYPT_HASH: Disable ESP32 hash acceleration   default: off
48
 * WOLFSSL_ARMASM_CRYPTO_SHA512: ARM crypto SHA-512 instructions  default: off
49
 * STM32_HASH_SHA384:        STM32 hardware SHA-384               default: off
50
 * STM32_HASH_SHA512:        STM32 hardware SHA-512               default: off
51
 * WOLFSSL_SHA512_HASHTYPE:  SHA-512 hash type for hw dispatch    default: off
52
 * MAX3266X_SHA:             MAX3266X hardware SHA                 default: off
53
 * PSOC6_HASH_SHA2:          PSoC6 hardware SHA-2                 default: off
54
 * WOLFSSL_RENESAS_RSIP:     Renesas RSIP SHA acceleration        default: off
55
 */
56
57
#define WC_FIPS_LL_CRYPTO
58
#define _WC_BUILDING_SHA512_C
59
60
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
61
62
#if (defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)) && \
63
    defined(WOLF_CRYPTO_CB_ONLY_SHA512) && defined(WOLFSSL_RISCV_ASM)
64
    #error "WOLF_CRYPTO_CB_ONLY_SHA512 is incompatible with SHA-512 hardware" \
65
           " acceleration backends"
66
#endif
67
68
#if (defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384))
69
70
/* determine if we are using Espressif SHA hardware acceleration */
71
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
72
#if defined(WOLFSSL_ESP32_CRYPT) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
73
    #include "sdkconfig.h"
74
    /* Define a single keyword for simplicity & readability.
75
     *
76
     * By default the HW acceleration is on for ESP32 Chipsets,
77
     * but individual components can be turned off. See user_settings.h
78
     */
79
    #define TAG "wc_sha_512"
80
    #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
81
#else
82
    #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
83
#endif
84
85
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
86
    #if defined(USE_WINDOWS_API) && defined(_MSC_VER)
87
        #pragma code_seg(".fipsA$m")
88
        #pragma const_seg(".fipsB$m")
89
    #endif
90
#endif
91
92
#include <wolfssl/wolfcrypt/sha512.h>
93
#include <wolfssl/wolfcrypt/cpuid.h>
94
#include <wolfssl/wolfcrypt/hash.h>
95
96
#ifdef WOLF_CRYPTO_CB
97
    #include <wolfssl/wolfcrypt/cryptocb.h>
98
#endif
99
100
#ifdef WOLFSSL_IMXRT1170_CAAM
101
    #include <wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h>
102
#endif
103
104
/* deprecated USE_SLOW_SHA2 (replaced with USE_SLOW_SHA512) */
105
#if defined(USE_SLOW_SHA2) && !defined(USE_SLOW_SHA512)
106
    #define USE_SLOW_SHA512
107
#endif
108
109
#ifdef NO_INLINE
110
    #include <wolfssl/wolfcrypt/misc.h>
111
#else
112
    #define WOLFSSL_MISC_INCLUDED
113
    #include <wolfcrypt/src/misc.c>
114
#endif
115
116
#if FIPS_VERSION3_GE(6,0,0)
117
    const unsigned int wolfCrypt_FIPS_sha512_ro_sanity[2] =
118
                                                     { 0x1a2b3c4d, 0x00000015 };
119
    int wolfCrypt_FIPS_SHA512_sanity(void)
120
    {
121
        return 0;
122
    }
123
#endif
124
125
126
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
127
    #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
128
#endif
129
130
#if defined(MAX3266X_SHA)
131
    /* Already brought in by sha512.h */
132
    /* #include <wolfssl/wolfcrypt/port/maxim/max3266x.h> */
133
#endif
134
135
#if defined(WOLFSSL_PSOC6_CRYPTO)
136
    #include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h>
137
#endif
138
139
#if defined(WC_C_DYNAMIC_FALLBACK) && \
140
        defined(WOLFSSL_AESNI) && !defined(USE_INTEL_SPEEDUP)
141
    /* AES-NI can be enabled with WC_C_DYNAMIC_FALLBACK, but without the rest of
142
     * USE_INTEL_SPEEDUP, in which case we need to disable the dynamic
143
     * fallback.
144
     */
145
    #undef WC_C_DYNAMIC_FALLBACK
146
#endif
147
148
/* Caller-owned transient scratch for the C transform: the 16-entry message
149
 * schedule window, so one ForceZero when the public call returns wipes it,
150
 * instead of a per-block wipe inside the transform.  The working variables T
151
 * stay a local of the transform: through a pointer they cannot be kept in
152
 * registers across the 80 rounds (measured ~9% on the C transform), so they
153
 * keep their small per-block wipe.  Natural alignment suffices; only the C
154
 * transform uses this. */
155
typedef struct Sha512TScratch {
156
    word64 W[16];
157
} Sha512TScratch;
158
159
/* External W providers size by the header constant; keep them in lockstep. */
160
wc_static_assert(sizeof(Sha512TScratch) == WC_SHA512_TSCRATCH_SIZE);
161
162
/* Scratch provisioning for the callers that drive the C transform.  The
163
 * declared pointer is named sha512TScr.  Under WOLFSSL_SMALL_STACK_CACHE the
164
 * scratch is the front of the per-context cache allocated at init (the
165
 * wc_Sha512Transform() backup block sits after it) and is wiped at Free, so
166
 * WIPE is a no-op there, preserving that configuration's zero per-call
167
 * cost. */
168
#if defined(WOLFSSL_SMALL_STACK_CACHE)
169
    #define WC_SHA512_TSCRATCH_DECL(s) \
170
        Sha512TScratch* sha512TScr = (Sha512TScratch*)(s)->W
171
    #define WC_SHA512_TSCRATCH_BEGIN(s) \
172
        do { if (sha512TScr == NULL) return BAD_FUNC_ARG; } while (0)
173
    #define WC_SHA512_TSCRATCH_WIPE(s) WC_DO_NOTHING /* wiped at Free */
174
#else
175
    /* Stack in big-stack builds, heap under WOLFSSL_SMALL_STACK, per the
176
     * WC_DECLARE_VAR family (one allocation per public call -- the previous
177
     * code allocated and freed per block under WOLFSSL_SMALL_STACK). */
178
    #ifdef WOLFSSL_CHECK_MEM_ZERO
179
        /* Zero first: the registration call takes const void*, and a
180
         * pointer to an uninitialized object trips -Wmaybe-uninitialized
181
         * under -Werror; a deterministic start also suits the checker. */
182
        #define WC_SHA512_TSCRATCH_MZ_ADD()                       \
183
            do {                                                             \
184
                XMEMSET(sha512TScr, 0, sizeof(Sha512TScratch));                   \
185
                wc_MemZero_Add("Sha512 transform scratch", sha512TScr,      \
186
                               sizeof(Sha512TScratch));                       \
187
            } while (0)
188
        #define WC_SHA512_TSCRATCH_MZ_CHECK() \
189
            wc_MemZero_Check(sha512TScr, sizeof(Sha512TScratch))
190
    #else
191
7.31M
        #define WC_SHA512_TSCRATCH_MZ_ADD() WC_DO_NOTHING
192
7.31M
        #define WC_SHA512_TSCRATCH_MZ_CHECK() WC_DO_NOTHING
193
    #endif
194
    #define WC_SHA512_TSCRATCH_DECL(s) \
195
7.31M
        WC_DECLARE_VAR(sha512TScr, Sha512TScratch, 1, (s)->heap)
196
    #define WC_SHA512_TSCRATCH_BEGIN(s)                                      \
197
7.31M
        do {                                                                 \
198
7.31M
            WC_ALLOC_VAR_EX(sha512TScr, Sha512TScratch, 1, (s)->heap,        \
199
7.31M
                            DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);       \
200
7.31M
            WC_SHA512_TSCRATCH_MZ_ADD();                                     \
201
7.31M
        } while (0)
202
    #define WC_SHA512_TSCRATCH_WIPE(s)                                       \
203
7.31M
        do {                                                                 \
204
7.31M
            ForceZero(sha512TScr, sizeof(Sha512TScratch));                   \
205
7.31M
            WC_SHA512_TSCRATCH_MZ_CHECK();                                   \
206
7.31M
            WC_FREE_VAR_EX(sha512TScr, (s)->heap, DYNAMIC_TYPE_TMP_BUFFER);  \
207
7.31M
        } while (0)
208
#endif
209
210
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
211
    #if defined(__GNUC__) && ((__GNUC__ < 4) || \
212
                              (__GNUC__ == 4 && __GNUC_MINOR__ <= 8))
213
        #undef  NO_AVX2_SUPPORT
214
        #define NO_AVX2_SUPPORT
215
    #endif
216
    #if defined(__clang__) && ((__clang_major__ < 3) || \
217
                               (__clang_major__ == 3 && __clang_minor__ <= 5))
218
        #define NO_AVX2_SUPPORT
219
    #elif defined(__clang__) && defined(NO_AVX2_SUPPORT)
220
        #undef NO_AVX2_SUPPORT
221
    #endif
222
223
    #define HAVE_INTEL_AVX1
224
    #ifndef NO_AVX2_SUPPORT
225
        #define HAVE_INTEL_AVX2
226
    #endif
227
#endif
228
229
#if defined(HAVE_INTEL_AVX1)
230
    /* #define DEBUG_XMM  */
231
#endif
232
233
#if defined(HAVE_INTEL_AVX2)
234
    #define HAVE_INTEL_RORX
235
    /* #define DEBUG_YMM  */
236
#endif
237
238
#ifdef WOLF_CRYPTO_CB_ONLY_SHA512
239
/* WOLF_CRYPTO_CB_ONLY_SHA512 strips the software SHA-512 implementation and
240
 * routes every operation (SHA-512, SHA-384, SHA-512/224, SHA-512/256) through
241
 * the crypto callback. It is mutually exclusive with any in-tree SHA-512
242
 * hardware/asm backend: keep this list in sync with the backend dispatch
243
 * chains in sha512.c. The RISC-V asm guard lives before the outer file guard;
244
 * these guards live before the dispatch chain so they are evaluated before a
245
 * hardware backend wins the #elif chain (in which case the
246
 * WOLF_CRYPTO_CB_ONLY_SHA512 branch itself is never compiled). */
247
#if (defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
248
        !defined(WOLFSSL_QNX_CAAM)) || \
249
    defined(WOLFSSL_SILABS_SHA512) || \
250
    defined(WOLFSSL_KCAPI_HASH) || \
251
    (defined(WOLFSSL_RENESAS_RSIP) && \
252
        !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) || \
253
    defined(MAX3266X_SHA) || \
254
    (defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)) || \
255
    defined(STM32_HASH_SHA512) || \
256
    defined(PSOC6_HASH_SHA2) || \
257
    defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) || \
258
    defined(WOLFSSL_ARMASM) || \
259
    defined(WOLFSSL_RISCV_ASM) || \
260
    (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
261
        (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
262
    #error "WOLF_CRYPTO_CB_ONLY_SHA512 is incompatible with SHA-512 hardware" \
263
           " acceleration backends"
264
#endif
265
#if defined(HAVE_FIPS)
266
    #error "WOLF_CRYPTO_CB_ONLY_SHA512 is incompatible with FIPS builds"
267
#endif
268
/* WOLFSSL_HASH_KEEP accumulates all Update data into sha->msg and passes it
269
 * all to hardware in Final. That pattern is driven by port-specific backends
270
 * (e.g. CAAM) which are already excluded above; the crypto-callback Update
271
 * path dispatches each chunk directly to the callback instead, so the two
272
 * mechanisms are incompatible. */
273
#ifdef WOLFSSL_HASH_KEEP
274
    #error "WOLF_CRYPTO_CB_ONLY_SHA512 is incompatible with WOLFSSL_HASH_KEEP"
275
#endif
276
#endif /* WOLF_CRYPTO_CB_ONLY_SHA512 */
277
278
#if defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
279
    !defined(WOLFSSL_QNX_CAAM)
280
    /* functions defined in wolfcrypt/src/port/caam/caam_sha.c */
281
282
#elif defined(WOLFSSL_SILABS_SHA512)
283
    /* functions defined in wolfcrypt/src/port/silabs/silabs_hash.c */
284
285
#elif defined(WOLFSSL_KCAPI_HASH)
286
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
287
288
#elif defined(WOLFSSL_RENESAS_RSIP) && \
289
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
290
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
291
292
#elif defined(MAX3266X_SHA)
293
    /* Functions defined in wolfcrypt/src/port/maxim/max3266x.c */
294
295
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
296
    int wc_InitSha512(wc_Sha512* sha512)
297
    {
298
        int ret;
299
        if (sha512 == NULL)
300
            return BAD_FUNC_ARG;
301
        ret = se050_hash_init(&sha512->se050Ctx, NULL);
302
#if defined(WOLFSSL_SHA512_HASHTYPE)
303
        if (ret == 0) {
304
            sha512->hashType = WC_HASH_TYPE_SHA512;
305
        }
306
#endif
307
        return ret;
308
    }
309
    int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
310
    {
311
        int ret;
312
        if (sha512 == NULL) {
313
            return BAD_FUNC_ARG;
314
        }
315
        (void)devId;
316
        ret = se050_hash_init(&sha512->se050Ctx, heap);
317
#if defined(WOLFSSL_SHA512_HASHTYPE)
318
        if (ret == 0) {
319
            sha512->hashType = WC_HASH_TYPE_SHA512;
320
        }
321
#endif
322
        return ret;
323
    }
324
    int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
325
    {
326
        if (sha512 == NULL) {
327
            return BAD_FUNC_ARG;
328
        }
329
        if (data == NULL && len == 0) {
330
            /* valid, but do nothing */
331
            return 0;
332
        }
333
        if (data == NULL) {
334
            return BAD_FUNC_ARG;
335
        }
336
337
        return se050_hash_update(&sha512->se050Ctx, data, len);
338
    }
339
    int wc_Sha512Final(wc_Sha512* sha512, byte* hash)
340
    {
341
        int ret = 0;
342
        int devId = INVALID_DEVID;
343
        if (sha512 == NULL) {
344
            return BAD_FUNC_ARG;
345
        }
346
    #ifdef WOLF_CRYPTO_CB
347
        devId = sha512->devId;
348
    #endif
349
        ret = se050_hash_final(&sha512->se050Ctx, hash, WC_SHA512_DIGEST_SIZE,
350
                               kAlgorithm_SSS_SHA512);
351
        return ret;
352
    }
353
    int wc_Sha512FinalRaw(wc_Sha512* sha512, byte* hash)
354
    {
355
        int ret = 0;
356
        int devId = INVALID_DEVID;
357
        if (sha512 == NULL) {
358
            return BAD_FUNC_ARG;
359
        }
360
    #ifdef WOLF_CRYPTO_CB
361
        devId = sha512->devId;
362
    #endif
363
        ret = se050_hash_final(&sha512->se050Ctx, hash, WC_SHA512_DIGEST_SIZE,
364
                               kAlgorithm_SSS_SHA512);
365
        return ret;
366
    }
367
    void wc_Sha512Free(wc_Sha512* sha512)
368
    {
369
        se050_hash_free(&sha512->se050Ctx);
370
    }
371
#elif defined(STM32_HASH_SHA512)
372
373
    /* Supports CubeMX HAL or Standard Peripheral Library */
374
375
    int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
376
    {
377
        if (sha512 == NULL)
378
            return BAD_FUNC_ARG;
379
380
        (void)devId;
381
        (void)heap;
382
383
        XMEMSET(sha512, 0, sizeof(wc_Sha512));
384
        wc_Stm32_Hash_Init(&sha512->stmCtx);
385
#if defined(WOLFSSL_SHA512_HASHTYPE)
386
        sha512->hashType = WC_HASH_TYPE_SHA512;
387
#endif
388
        return 0;
389
    }
390
391
    int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
392
    {
393
        int ret = 0;
394
395
        if (sha512 == NULL) {
396
            return BAD_FUNC_ARG;
397
        }
398
        if (data == NULL && len == 0) {
399
            /* valid, but do nothing */
400
            return 0;
401
        }
402
        if (data == NULL) {
403
            return BAD_FUNC_ARG;
404
        }
405
406
        ret = wolfSSL_CryptHwMutexLock();
407
        if (ret == 0) {
408
            ret = wc_Stm32_Hash_Update(&sha512->stmCtx,
409
                HASH_ALGOSELECTION_SHA512, data, len, WC_SHA512_BLOCK_SIZE);
410
            wolfSSL_CryptHwMutexUnLock();
411
        }
412
        return ret;
413
    }
414
415
    int wc_Sha512Final(wc_Sha512* sha512, byte* hash)
416
    {
417
        int ret = 0;
418
419
        if (sha512 == NULL || hash == NULL) {
420
            return BAD_FUNC_ARG;
421
        }
422
423
        ret = wolfSSL_CryptHwMutexLock();
424
        if (ret == 0) {
425
            ret = wc_Stm32_Hash_Final(&sha512->stmCtx,
426
                HASH_ALGOSELECTION_SHA512, hash, WC_SHA512_DIGEST_SIZE);
427
            wolfSSL_CryptHwMutexUnLock();
428
        }
429
430
        (void)wc_InitSha512(sha512); /* reset state */
431
432
        return ret;
433
    }
434
#elif defined(PSOC6_HASH_SHA2)
435
    /* Functions defined in wolfcrypt/src/port/cypress/psoc6_crypto.c */
436
437
#elif defined(WOLF_CRYPTO_CB_ONLY_SHA512)
438
439
static int Sha512_CbReset(wc_Sha512* sha512, const word64* initDigest,
440
    int hashType)
441
{
442
    int i;
443
444
    if (sha512 == NULL)
445
        return BAD_FUNC_ARG;
446
447
    for (i = 0; i < 8; i++)
448
        sha512->digest[i] = initDigest[i];
449
450
    sha512->buffLen = 0;
451
    XMEMSET(sha512->buffer, 0, sizeof(sha512->buffer));
452
    sha512->loLen = 0;
453
    sha512->hiLen = 0;
454
#ifdef WOLFSSL_HASH_FLAGS
455
    sha512->flags = 0;
456
#endif
457
#if defined(WOLFSSL_SHA512_HASHTYPE)
458
    sha512->hashType = hashType;
459
#else
460
    (void)hashType;
461
#endif
462
    return 0;
463
}
464
465
static int Sha512_CbInit(wc_Sha512* sha512, const word64* initDigest,
466
    void* heap, int devId, int hashType)
467
{
468
    int ret;
469
470
    /* Zero the whole struct first so fields not touched by the callback path
471
     * (e.g. asyncDev, W, devCtx) never expose uninitialized stack data to a
472
     * callback; the admin fields below are then set explicitly. */
473
    if (sha512 != NULL)
474
        XMEMSET(sha512, 0, sizeof(*sha512));
475
476
    ret = Sha512_CbReset(sha512, initDigest, hashType);
477
    if (ret != 0)
478
        return ret;
479
480
    sha512->heap = heap;
481
    sha512->devId = devId;
482
    sha512->devCtx = NULL;
483
484
    return 0;
485
}
486
487
#ifdef WOLFSSL_SHA512
488
489
static const word64 sha512Init[8] = {
490
    W64LIT(0x6a09e667f3bcc908), W64LIT(0xbb67ae8584caa73b),
491
    W64LIT(0x3c6ef372fe94f82b), W64LIT(0xa54ff53a5f1d36f1),
492
    W64LIT(0x510e527fade682d1), W64LIT(0x9b05688c2b3e6c1f),
493
    W64LIT(0x1f83d9abfb41bd6b), W64LIT(0x5be0cd19137e2179)
494
};
495
496
static int Sha512_CbFinal(wc_Sha512* sha512, byte* hash, size_t digestSz)
497
{
498
    if (sha512 == NULL || hash == NULL)
499
        return BAD_FUNC_ARG;
500
501
    #ifndef WOLF_CRYPTO_CB_FIND
502
    if (sha512->devId != INVALID_DEVID)
503
    #endif
504
    {
505
        int ret = wc_CryptoCb_Sha512Hash(sha512, NULL, 0, hash, digestSz);
506
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
507
            return ret;
508
    }
509
    return NO_VALID_DEVID;
510
}
511
512
int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
513
{
514
    return Sha512_CbInit(sha512, sha512Init, heap, devId,
515
                         WC_HASH_TYPE_SHA512);
516
}
517
518
int wc_InitSha512(wc_Sha512* sha512)
519
{
520
    int devId = INVALID_DEVID;
521
522
#ifdef WOLF_CRYPTO_CB
523
    devId = wc_CryptoCb_DefaultDevID();
524
#endif
525
    return wc_InitSha512_ex(sha512, NULL, devId);
526
}
527
528
int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
529
{
530
    if (sha512 == NULL)
531
        return BAD_FUNC_ARG;
532
    if (data == NULL && len == 0)
533
        return 0;
534
    if (data == NULL)
535
        return BAD_FUNC_ARG;
536
537
    #ifndef WOLF_CRYPTO_CB_FIND
538
    if (sha512->devId != INVALID_DEVID)
539
    #endif
540
    {
541
        int ret = wc_CryptoCb_Sha512Hash(sha512, data, len, NULL, 0);
542
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
543
            return ret;
544
    }
545
    return NO_VALID_DEVID;
546
}
547
548
int wc_Sha512Final(wc_Sha512* sha512, byte* hash)
549
{
550
    return Sha512_CbFinal(sha512, hash, WC_SHA512_DIGEST_SIZE);
551
}
552
553
void wc_Sha512Free(wc_Sha512* sha512)
554
{
555
#ifdef WOLF_CRYPTO_CB_FREE
556
    int ret = 0;
557
#endif
558
559
    if (sha512 == NULL)
560
        return;
561
562
#ifdef WOLF_CRYPTO_CB_FREE
563
    #ifndef WOLF_CRYPTO_CB_FIND
564
    if (sha512->devId != INVALID_DEVID)
565
    #endif
566
    {
567
        ret = wc_CryptoCb_Free(sha512->devId, WC_ALGO_TYPE_HASH,
568
                         WC_HASH_TYPE_SHA512, 0, (void*)sha512);
569
        /* If they want the standard free, they can call it themselves */
570
        /* via their callback setting devId to INVALID_DEVID */
571
        /* otherwise assume the callback handled it */
572
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
573
            return;
574
        /* fall-through when unavailable */
575
    }
576
577
    /* silence compiler warning */
578
    (void)ret;
579
#endif /* WOLF_CRYPTO_CB_FREE */
580
581
    ForceZero(sha512, sizeof(*sha512));
582
}
583
584
int wc_Sha512GetHash(wc_Sha512* sha512, byte* hash)
585
{
586
    int ret;
587
    WC_DECLARE_VAR(tmpSha512, wc_Sha512, 1, 0);
588
589
    if (sha512 == NULL || hash == NULL)
590
        return BAD_FUNC_ARG;
591
592
    WC_CALLOC_VAR_EX(tmpSha512, wc_Sha512, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
593
        return MEMORY_E);
594
595
    ret = wc_Sha512Copy(sha512, tmpSha512);
596
    if (ret == 0) {
597
        ret = wc_Sha512Final(tmpSha512, hash);
598
        wc_Sha512Free(tmpSha512);
599
    }
600
601
    WC_FREE_VAR_EX(tmpSha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
602
603
    return ret;
604
}
605
606
int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
607
{
608
    int ret = 0;
609
610
    if (src == NULL || dst == NULL)
611
        return BAD_FUNC_ARG;
612
613
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_COPY)
614
    #ifndef WOLF_CRYPTO_CB_FIND
615
    if (src->devId != INVALID_DEVID)
616
    #endif
617
    {
618
        ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH,
619
                               WC_HASH_TYPE_SHA512, (void*)src, (void*)dst);
620
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
621
            return ret;
622
        /* fall-through when the callback is unavailable */
623
    }
624
    ret = 0; /* discard CRYPTOCB_UNAVAILABLE before the plain struct copy */
625
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
626
627
    wc_Sha512Free(dst);
628
    XMEMCPY(dst, src, sizeof(wc_Sha512));
629
630
#ifdef WOLFSSL_HASH_FLAGS
631
    dst->flags |= WC_HASH_FLAG_ISCOPY;
632
#endif
633
634
    return ret;
635
}
636
637
#ifdef WOLFSSL_HASH_FLAGS
638
int wc_Sha512SetFlags(wc_Sha512* sha512, word32 flags)
639
{
640
    if (sha512)
641
        sha512->flags = flags;
642
    return 0;
643
}
644
int wc_Sha512GetFlags(wc_Sha512* sha512, word32* flags)
645
{
646
    if (sha512 && flags)
647
        *flags = sha512->flags;
648
    return 0;
649
}
650
#endif /* WOLFSSL_HASH_FLAGS */
651
652
#if !defined(WOLFSSL_NOSHA512_224) && !defined(HAVE_SELFTEST)
653
654
static const word64 sha512_224Init[8] = {
655
    W64LIT(0x8c3d37c819544da2), W64LIT(0x73e1996689dcd4d6),
656
    W64LIT(0x1dfab7ae32ff9c82), W64LIT(0x679dd514582f9fcf),
657
    W64LIT(0x0f6d2b697bd44da8), W64LIT(0x77e36f7304c48942),
658
    W64LIT(0x3f9d85a86a1d36c8), W64LIT(0x1112e6ad91d692a1)
659
};
660
661
int wc_InitSha512_224_ex(wc_Sha512* sha512, void* heap, int devId)
662
{
663
    return Sha512_CbInit(sha512, sha512_224Init, heap, devId,
664
                         WC_HASH_TYPE_SHA512_224);
665
}
666
667
int wc_InitSha512_224(wc_Sha512* sha512)
668
{
669
    int devId = INVALID_DEVID;
670
671
#ifdef WOLF_CRYPTO_CB
672
    devId = wc_CryptoCb_DefaultDevID();
673
#endif
674
    return wc_InitSha512_224_ex(sha512, NULL, devId);
675
}
676
677
int wc_Sha512_224Update(wc_Sha512* sha512, const byte* data, word32 len)
678
{
679
    return wc_Sha512Update(sha512, data, len);
680
}
681
682
int wc_Sha512_224Final(wc_Sha512* sha512, byte* hash)
683
{
684
    return Sha512_CbFinal(sha512, hash, WC_SHA512_224_DIGEST_SIZE);
685
}
686
687
void wc_Sha512_224Free(wc_Sha512* sha512)
688
{
689
    wc_Sha512Free(sha512);
690
}
691
692
int wc_Sha512_224Copy(wc_Sha512* src, wc_Sha512* dst)
693
{
694
    return wc_Sha512Copy(src, dst);
695
}
696
697
int wc_Sha512_224GetHash(wc_Sha512* sha512, byte* hash)
698
{
699
    int ret;
700
    WC_DECLARE_VAR(tmpSha512, wc_Sha512, 1, 0);
701
702
    if (sha512 == NULL || hash == NULL)
703
        return BAD_FUNC_ARG;
704
705
    WC_CALLOC_VAR_EX(tmpSha512, wc_Sha512, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
706
        return MEMORY_E);
707
708
    ret = wc_Sha512_224Copy(sha512, tmpSha512);
709
    if (ret == 0) {
710
        ret = wc_Sha512_224Final(tmpSha512, hash);
711
        wc_Sha512_224Free(tmpSha512);
712
    }
713
714
    WC_FREE_VAR_EX(tmpSha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
715
716
    return ret;
717
}
718
719
#ifdef WOLFSSL_HASH_FLAGS
720
int wc_Sha512_224SetFlags(wc_Sha512* sha512, word32 flags)
721
{
722
    return wc_Sha512SetFlags(sha512, flags);
723
}
724
int wc_Sha512_224GetFlags(wc_Sha512* sha512, word32* flags)
725
{
726
    return wc_Sha512GetFlags(sha512, flags);
727
}
728
#endif /* WOLFSSL_HASH_FLAGS */
729
730
#endif /* !WOLFSSL_NOSHA512_224 && !HAVE_SELFTEST */
731
732
#if !defined(WOLFSSL_NOSHA512_256) && !defined(HAVE_SELFTEST)
733
734
static const word64 sha512_256Init[8] = {
735
    W64LIT(0x22312194fc2bf72c), W64LIT(0x9f555fa3c84c64c2),
736
    W64LIT(0x2393b86b6f53b151), W64LIT(0x963877195940eabd),
737
    W64LIT(0x96283ee2a88effe3), W64LIT(0xbe5e1e2553863992),
738
    W64LIT(0x2b0199fc2c85b8aa), W64LIT(0x0eb72ddc81c52ca2)
739
};
740
741
int wc_InitSha512_256_ex(wc_Sha512* sha512, void* heap, int devId)
742
{
743
    return Sha512_CbInit(sha512, sha512_256Init, heap, devId,
744
                         WC_HASH_TYPE_SHA512_256);
745
}
746
747
int wc_InitSha512_256(wc_Sha512* sha512)
748
{
749
    int devId = INVALID_DEVID;
750
751
#ifdef WOLF_CRYPTO_CB
752
    devId = wc_CryptoCb_DefaultDevID();
753
#endif
754
    return wc_InitSha512_256_ex(sha512, NULL, devId);
755
}
756
757
int wc_Sha512_256Update(wc_Sha512* sha512, const byte* data, word32 len)
758
{
759
    return wc_Sha512Update(sha512, data, len);
760
}
761
762
int wc_Sha512_256Final(wc_Sha512* sha512, byte* hash)
763
{
764
    return Sha512_CbFinal(sha512, hash, WC_SHA512_256_DIGEST_SIZE);
765
}
766
767
void wc_Sha512_256Free(wc_Sha512* sha512)
768
{
769
    wc_Sha512Free(sha512);
770
}
771
772
int wc_Sha512_256Copy(wc_Sha512* src, wc_Sha512* dst)
773
{
774
    return wc_Sha512Copy(src, dst);
775
}
776
777
int wc_Sha512_256GetHash(wc_Sha512* sha512, byte* hash)
778
{
779
    int ret;
780
    WC_DECLARE_VAR(tmpSha512, wc_Sha512, 1, 0);
781
782
    if (sha512 == NULL || hash == NULL)
783
        return BAD_FUNC_ARG;
784
785
    WC_CALLOC_VAR_EX(tmpSha512, wc_Sha512, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
786
        return MEMORY_E);
787
788
    ret = wc_Sha512_256Copy(sha512, tmpSha512);
789
    if (ret == 0) {
790
        ret = wc_Sha512_256Final(tmpSha512, hash);
791
        wc_Sha512_256Free(tmpSha512);
792
    }
793
794
    WC_FREE_VAR_EX(tmpSha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
795
796
    return ret;
797
}
798
799
#ifdef WOLFSSL_HASH_FLAGS
800
int wc_Sha512_256SetFlags(wc_Sha512* sha512, word32 flags)
801
{
802
    return wc_Sha512SetFlags(sha512, flags);
803
}
804
int wc_Sha512_256GetFlags(wc_Sha512* sha512, word32* flags)
805
{
806
    return wc_Sha512GetFlags(sha512, flags);
807
}
808
#endif /* WOLFSSL_HASH_FLAGS */
809
810
#endif /* !WOLFSSL_NOSHA512_256 && !HAVE_SELFTEST */
811
812
#endif /* WOLFSSL_SHA512 */
813
814
#ifdef WOLFSSL_SHA384
815
816
static const word64 sha384Init[8] = {
817
    W64LIT(0xcbbb9d5dc1059ed8), W64LIT(0x629a292a367cd507),
818
    W64LIT(0x9159015a3070dd17), W64LIT(0x152fecd8f70e5939),
819
    W64LIT(0x67332667ffc00b31), W64LIT(0x8eb44a8768581511),
820
    W64LIT(0xdb0c2e0d64f98fa7), W64LIT(0x47b5481dbefa4fa4)
821
};
822
823
int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
824
{
825
    return Sha512_CbInit(sha384, sha384Init, heap, devId,
826
                         WC_HASH_TYPE_SHA384);
827
}
828
829
int wc_InitSha384(wc_Sha384* sha384)
830
{
831
    int devId = INVALID_DEVID;
832
833
#ifdef WOLF_CRYPTO_CB
834
    devId = wc_CryptoCb_DefaultDevID();
835
#endif
836
    return wc_InitSha384_ex(sha384, NULL, devId);
837
}
838
839
int wc_Sha384Update(wc_Sha384* sha384, const byte* data, word32 len)
840
{
841
    if (sha384 == NULL)
842
        return BAD_FUNC_ARG;
843
    if (data == NULL && len == 0)
844
        return 0;
845
    if (data == NULL)
846
        return BAD_FUNC_ARG;
847
848
    #ifndef WOLF_CRYPTO_CB_FIND
849
    if (sha384->devId != INVALID_DEVID)
850
    #endif
851
    {
852
        int ret = wc_CryptoCb_Sha384Hash(sha384, data, len, NULL);
853
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
854
            return ret;
855
    }
856
    return NO_VALID_DEVID;
857
}
858
859
int wc_Sha384Final(wc_Sha384* sha384, byte* hash)
860
{
861
    if (sha384 == NULL || hash == NULL)
862
        return BAD_FUNC_ARG;
863
864
    #ifndef WOLF_CRYPTO_CB_FIND
865
    if (sha384->devId != INVALID_DEVID)
866
    #endif
867
    {
868
        int ret = wc_CryptoCb_Sha384Hash(sha384, NULL, 0, hash);
869
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
870
            return ret;
871
    }
872
    return NO_VALID_DEVID;
873
}
874
875
void wc_Sha384Free(wc_Sha384* sha384)
876
{
877
#ifdef WOLF_CRYPTO_CB_FREE
878
    int ret = 0;
879
#endif
880
881
    if (sha384 == NULL)
882
        return;
883
884
#ifdef WOLF_CRYPTO_CB_FREE
885
    #ifndef WOLF_CRYPTO_CB_FIND
886
    if (sha384->devId != INVALID_DEVID)
887
    #endif
888
    {
889
        ret = wc_CryptoCb_Free(sha384->devId, WC_ALGO_TYPE_HASH,
890
                         WC_HASH_TYPE_SHA384, 0, (void*)sha384);
891
        /* If they want the standard free, they can call it themselves */
892
        /* via their callback setting devId to INVALID_DEVID */
893
        /* otherwise assume the callback handled it */
894
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
895
            return;
896
        /* fall-through when unavailable */
897
    }
898
899
    /* silence compiler warning */
900
    (void)ret;
901
#endif /* WOLF_CRYPTO_CB_FREE */
902
903
    ForceZero(sha384, sizeof(*sha384));
904
}
905
906
int wc_Sha384GetHash(wc_Sha384* sha384, byte* hash)
907
{
908
    int ret;
909
    WC_DECLARE_VAR(tmpSha384, wc_Sha384, 1, 0);
910
911
    if (sha384 == NULL || hash == NULL)
912
        return BAD_FUNC_ARG;
913
914
    WC_CALLOC_VAR_EX(tmpSha384, wc_Sha384, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
915
        return MEMORY_E);
916
917
    ret = wc_Sha384Copy(sha384, tmpSha384);
918
    if (ret == 0) {
919
        ret = wc_Sha384Final(tmpSha384, hash);
920
        wc_Sha384Free(tmpSha384);
921
    }
922
923
    WC_FREE_VAR_EX(tmpSha384, NULL, DYNAMIC_TYPE_TMP_BUFFER);
924
925
    return ret;
926
}
927
928
int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
929
{
930
    int ret = 0;
931
932
    if (src == NULL || dst == NULL)
933
        return BAD_FUNC_ARG;
934
935
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_COPY)
936
    #ifndef WOLF_CRYPTO_CB_FIND
937
    if (src->devId != INVALID_DEVID)
938
    #endif
939
    {
940
        ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH,
941
                               WC_HASH_TYPE_SHA384, (void*)src, (void*)dst);
942
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
943
            return ret;
944
        /* fall-through when the callback is unavailable */
945
    }
946
    ret = 0; /* discard CRYPTOCB_UNAVAILABLE before the plain struct copy */
947
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
948
949
    wc_Sha384Free(dst);
950
    XMEMCPY(dst, src, sizeof(wc_Sha384));
951
952
#ifdef WOLFSSL_HASH_FLAGS
953
    dst->flags |= WC_HASH_FLAG_ISCOPY;
954
#endif
955
956
    return ret;
957
}
958
959
#ifdef WOLFSSL_HASH_FLAGS
960
int wc_Sha384SetFlags(wc_Sha384* sha384, word32 flags)
961
{
962
    if (sha384)
963
        sha384->flags = flags;
964
    return 0;
965
}
966
int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags)
967
{
968
    if (sha384 && flags)
969
        *flags = sha384->flags;
970
    return 0;
971
}
972
#endif /* WOLFSSL_HASH_FLAGS */
973
974
#endif /* WOLFSSL_SHA384 */
975
#else
976
977
#ifdef WOLFSSL_SHA512
978
979
#if (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
980
     (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))) || \
981
    defined(WOLFSSL_ARMASM)
982
static void Sha512_SetTransform(void);
983
#endif
984
985
static int InitSha512(wc_Sha512* sha512)
986
4.52M
{
987
4.52M
    if (sha512 == NULL)
988
0
        return BAD_FUNC_ARG;
989
990
4.52M
    sha512->digest[0] = W64LIT(0x6a09e667f3bcc908);
991
4.52M
    sha512->digest[1] = W64LIT(0xbb67ae8584caa73b);
992
4.52M
    sha512->digest[2] = W64LIT(0x3c6ef372fe94f82b);
993
4.52M
    sha512->digest[3] = W64LIT(0xa54ff53a5f1d36f1);
994
4.52M
    sha512->digest[4] = W64LIT(0x510e527fade682d1);
995
4.52M
    sha512->digest[5] = W64LIT(0x9b05688c2b3e6c1f);
996
4.52M
    sha512->digest[6] = W64LIT(0x1f83d9abfb41bd6b);
997
4.52M
    sha512->digest[7] = W64LIT(0x5be0cd19137e2179);
998
999
4.52M
    sha512->buffLen = 0;
1000
4.52M
    XMEMSET(sha512->buffer, 0, sizeof(sha512->buffer));
1001
4.52M
    sha512->loLen   = 0;
1002
4.52M
    sha512->hiLen   = 0;
1003
1004
#if (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
1005
     (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))) || \
1006
    defined(WOLFSSL_ARMASM)
1007
    Sha512_SetTransform();
1008
#endif
1009
1010
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
1011
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
1012
1013
    /* HW needs to be carefully initialized, taking into account soft copy.
1014
    ** If already in use; copy may revert to SW as needed. */
1015
    esp_sha_init(&(sha512->ctx), WC_HASH_TYPE_SHA512);
1016
#endif
1017
1018
4.52M
#ifdef WOLFSSL_HASH_FLAGS
1019
4.52M
    sha512->flags = 0;
1020
4.52M
#endif
1021
#if defined(WOLFSSL_SHA512_HASHTYPE)
1022
    sha512->hashType = WC_HASH_TYPE_SHA512;
1023
#endif /* WOLFSSL_SHA512_HASHTYPE */
1024
1025
4.52M
    return 0;
1026
4.52M
}
1027
1028
#if !defined(WOLFSSL_HASH_KEEP)
1029
1030
/* Reset a hash context to its freshly initialized state, reusing its existing
1031
 * allocations.  Like the Final functions, Reset does not destroy sensitive
1032
 * internal state; use the matching Free function for teardown at end of life.
1033
 */
1034
0
int wc_Sha512Reset(wc_Sha512* sha512) {
1035
0
    if (sha512 == NULL)
1036
0
        return BAD_FUNC_ARG;
1037
0
#ifdef WOLF_CRYPTO_CB
1038
    /* A device may hang state off devCtx that InitSha512() cannot restart.
1039
     * Free and re-init so the callback gets its teardown and setup. */
1040
0
    #ifndef WOLF_CRYPTO_CB_FIND
1041
0
    if (sha512->devId != INVALID_DEVID)
1042
0
    #endif
1043
0
    {
1044
0
        void* heap = sha512->heap;
1045
0
        int devId = sha512->devId;
1046
0
        wc_Sha512Free(sha512);
1047
0
        return wc_InitSha512_ex(sha512, heap, devId);
1048
0
    }
1049
0
#endif
1050
0
    return InitSha512(sha512);
1051
0
}
1052
#define WC_SHA512RESET_DEFINED
1053
#endif /* !WOLFSSL_HASH_KEEP */
1054
1055
#if !defined(WOLFSSL_NOSHA512_224) && \
1056
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
1057
1058
/**
1059
 * Initialize given wc_Sha512 structure with value specific to sha512/224.
1060
 * Note that sha512/224 has different initial hash value from sha512.
1061
 * The initial hash value consists of eight 64bit words. They are given
1062
 * in FIPS180-4.
1063
 */
1064
static int InitSha512_224(wc_Sha512* sha512)
1065
1.17k
{
1066
1.17k
    if (sha512 == NULL)
1067
0
        return BAD_FUNC_ARG;
1068
1069
1.17k
    sha512->digest[0] = W64LIT(0x8c3d37c819544da2);
1070
1.17k
    sha512->digest[1] = W64LIT(0x73e1996689dcd4d6);
1071
1.17k
    sha512->digest[2] = W64LIT(0x1dfab7ae32ff9c82);
1072
1.17k
    sha512->digest[3] = W64LIT(0x679dd514582f9fcf);
1073
1.17k
    sha512->digest[4] = W64LIT(0x0f6d2b697bd44da8);
1074
1.17k
    sha512->digest[5] = W64LIT(0x77e36f7304c48942);
1075
1.17k
    sha512->digest[6] = W64LIT(0x3f9d85a86a1d36c8);
1076
1.17k
    sha512->digest[7] = W64LIT(0x1112e6ad91d692a1);
1077
1078
1.17k
    sha512->buffLen = 0;
1079
1.17k
    XMEMSET(sha512->buffer, 0, sizeof(sha512->buffer));
1080
1.17k
    sha512->loLen   = 0;
1081
1.17k
    sha512->hiLen   = 0;
1082
1083
#if (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
1084
     (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))) || \
1085
    defined(WOLFSSL_ARMASM)
1086
    Sha512_SetTransform();
1087
#endif
1088
1089
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
1090
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
1091
    /* HW needs to be carefully initialized, taking into account soft copy.
1092
    ** If already in use; copy may revert to SW as needed.
1093
    **
1094
    ** Note for original ESP32, there's no HW for SHA512/224
1095
    */
1096
    esp_sha_init(&(sha512->ctx), WC_HASH_TYPE_SHA512_224);
1097
#endif
1098
1099
1.17k
#ifdef WOLFSSL_HASH_FLAGS
1100
1.17k
    sha512->flags = 0;
1101
1.17k
#endif
1102
#if defined(WOLFSSL_SHA512_HASHTYPE)
1103
    sha512->hashType = WC_HASH_TYPE_SHA512_224;
1104
#endif /* WOLFSSL_SHA512_HASHTYPE */
1105
1.17k
    return 0;
1106
1.17k
}
1107
1108
#if !defined(WOLFSSL_HASH_KEEP)
1109
0
int wc_Sha512_224Reset(wc_Sha512* sha512) {
1110
0
    if (sha512 == NULL)
1111
0
        return BAD_FUNC_ARG;
1112
0
#ifdef WOLF_CRYPTO_CB
1113
    /* A device may hang state off devCtx that InitSha512_224() cannot restart.
1114
     * Free and re-init so the callback gets its teardown and setup. */
1115
0
    #ifndef WOLF_CRYPTO_CB_FIND
1116
0
    if (sha512->devId != INVALID_DEVID)
1117
0
    #endif
1118
0
    {
1119
0
        void* heap = sha512->heap;
1120
0
        int devId = sha512->devId;
1121
0
        wc_Sha512_224Free(sha512);
1122
0
        return wc_InitSha512_224_ex(sha512, heap, devId);
1123
0
    }
1124
0
#endif
1125
0
    return InitSha512_224(sha512);
1126
0
}
1127
#define WC_SHA512_224RESET_DEFINED
1128
#endif /* !WOLFSSL_HASH_KEEP */
1129
#endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
1130
1131
#if !defined(WOLFSSL_NOSHA512_256) && \
1132
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
1133
/**
1134
 * Initialize given wc_Sha512 structure with value specific to sha512/256.
1135
 * Note that sha512/256 has different initial hash value from sha512.
1136
 * The initial hash value consists of eight 64bit words. They are given
1137
 * in FIPS180-4.
1138
 */
1139
static int InitSha512_256(wc_Sha512* sha512)
1140
368
{
1141
368
    if (sha512 == NULL)
1142
0
        return BAD_FUNC_ARG;
1143
1144
368
    sha512->digest[0] = W64LIT(0x22312194fc2bf72c);
1145
368
    sha512->digest[1] = W64LIT(0x9f555fa3c84c64c2);
1146
368
    sha512->digest[2] = W64LIT(0x2393b86b6f53b151);
1147
368
    sha512->digest[3] = W64LIT(0x963877195940eabd);
1148
368
    sha512->digest[4] = W64LIT(0x96283ee2a88effe3);
1149
368
    sha512->digest[5] = W64LIT(0xbe5e1e2553863992);
1150
368
    sha512->digest[6] = W64LIT(0x2b0199fc2c85b8aa);
1151
368
    sha512->digest[7] = W64LIT(0x0eb72ddc81c52ca2);
1152
1153
368
    sha512->buffLen = 0;
1154
368
    XMEMSET(sha512->buffer, 0, sizeof(sha512->buffer));
1155
368
    sha512->loLen   = 0;
1156
368
    sha512->hiLen   = 0;
1157
1158
#if (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
1159
     (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))) || \
1160
    defined(WOLFSSL_ARMASM)
1161
    Sha512_SetTransform();
1162
#endif
1163
1164
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
1165
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
1166
    /* HW needs to be carefully initialized, taking into account soft copy.
1167
    ** If already in use; copy may revert to SW as needed.
1168
    **
1169
    ** Note for original ESP32, there's no HW for SHA512/2256.
1170
    */
1171
    esp_sha_init(&(sha512->ctx), WC_HASH_TYPE_SHA512_256);
1172
#endif
1173
1174
368
#ifdef WOLFSSL_HASH_FLAGS
1175
368
    sha512->flags = 0;
1176
368
#endif
1177
#if defined(WOLFSSL_SHA512_HASHTYPE)
1178
    sha512->hashType = WC_HASH_TYPE_SHA512_256;
1179
#endif /* WOLFSSL_SHA512_HASHTYPE */
1180
368
    return 0;
1181
368
}
1182
1183
#if !defined(WOLFSSL_HASH_KEEP)
1184
0
int wc_Sha512_256Reset(wc_Sha512* sha512) {
1185
0
    if (sha512 == NULL)
1186
0
        return BAD_FUNC_ARG;
1187
0
#ifdef WOLF_CRYPTO_CB
1188
    /* A device may hang state off devCtx that InitSha512_256() cannot restart.
1189
     * Free and re-init so the callback gets its teardown and setup. */
1190
0
    #ifndef WOLF_CRYPTO_CB_FIND
1191
0
    if (sha512->devId != INVALID_DEVID)
1192
0
    #endif
1193
0
    {
1194
0
        void* heap = sha512->heap;
1195
0
        int devId = sha512->devId;
1196
0
        wc_Sha512_256Free(sha512);
1197
0
        return wc_InitSha512_256_ex(sha512, heap, devId);
1198
0
    }
1199
0
#endif
1200
0
    return InitSha512_256(sha512);
1201
0
}
1202
#define WC_SHA512_256RESET_DEFINED
1203
#endif /* !WOLFSSL_HASH_KEEP */
1204
#endif /* !WOLFSSL_NOSHA512_256 && !FIPS... */
1205
1206
#endif /* WOLFSSL_SHA512 */
1207
1208
/* Hardware Acceleration */
1209
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
1210
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
1211
1212
    /*****
1213
    Intel AVX1/AVX2 Macro Control Structure
1214
1215
    #if defined(HAVE_INTEL_SPEEDUP)
1216
        #define HAVE_INTEL_AVX1
1217
        #define HAVE_INTEL_AVX2
1218
    #endif
1219
1220
    int InitSha512(wc_Sha512* sha512) {
1221
         Save/Recover XMM, YMM
1222
         ...
1223
1224
         Check Intel AVX cpuid flags
1225
    }
1226
1227
    #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
1228
      Transform_Sha512_AVX1(); # Function prototype
1229
      Transform_Sha512_AVX2(); #
1230
    #endif
1231
1232
      _Transform_Sha512() {     # Native Transform Function body
1233
1234
      }
1235
1236
      int Sha512Update() {
1237
         Save/Recover XMM, YMM
1238
         ...
1239
      }
1240
1241
      int Sha512Final() {
1242
         Save/Recover XMM, YMM
1243
         ...
1244
      }
1245
1246
1247
    #if defined(HAVE_INTEL_AVX1)
1248
1249
       XMM Instructions/INLINE asm Definitions
1250
1251
    #endif
1252
1253
    #if defined(HAVE_INTEL_AVX2)
1254
1255
       YMM Instructions/INLINE asm Definitions
1256
1257
    #endif
1258
1259
    #if defined(HAVE_INTEL_AVX1)
1260
1261
      int Transform_Sha512_AVX1() {
1262
          Stitched Message Sched/Round
1263
      }
1264
1265
    #endif
1266
1267
    #if defined(HAVE_INTEL_AVX2)
1268
1269
      int Transform_Sha512_AVX2() {
1270
          Stitched Message Sched/Round
1271
      }
1272
    #endif
1273
1274
    */
1275
1276
1277
    /* Each platform needs to query info type 1 from cpuid to see if aesni is
1278
     * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
1279
     */
1280
1281
#ifdef __cplusplus
1282
    extern "C" {
1283
#endif
1284
1285
    #if defined(HAVE_INTEL_AVX1)
1286
        extern int Transform_Sha512_AVX1(wc_Sha512 *sha512);
1287
        extern int Transform_Sha512_AVX1_Len(wc_Sha512 *sha512, word32 len);
1288
    #endif
1289
    #if defined(HAVE_INTEL_AVX2)
1290
        extern int Transform_Sha512_AVX2(wc_Sha512 *sha512);
1291
        extern int Transform_Sha512_AVX2_Len(wc_Sha512 *sha512, word32 len);
1292
        #if defined(HAVE_INTEL_RORX)
1293
            extern int Transform_Sha512_AVX1_RORX(wc_Sha512 *sha512);
1294
            extern int Transform_Sha512_AVX1_RORX_Len(wc_Sha512 *sha512,
1295
                                                      word32 len);
1296
            extern int Transform_Sha512_AVX2_RORX(wc_Sha512 *sha512);
1297
            extern int Transform_Sha512_AVX2_RORX_Len(wc_Sha512 *sha512,
1298
                                                      word32 len);
1299
        #endif
1300
    #endif
1301
1302
#ifdef __cplusplus
1303
    }  /* extern "C" */
1304
#endif
1305
1306
    static cpuid_flags_atomic_t intel_flags = WC_CPUID_ATOMIC_INITIALIZER;
1307
1308
#if defined(WC_C_DYNAMIC_FALLBACK) && !defined(WC_NO_INTERNAL_FUNCTION_POINTERS)
1309
    #define WC_NO_INTERNAL_FUNCTION_POINTERS
1310
#endif
1311
1312
    static int _Transform_Sha512(wc_Sha512 *sha512);
1313
    static int _Transform_Sha512_scr(wc_Sha512 *sha512,
1314
                                     Sha512TScratch* WC_RESTRICT scr);
1315
1316
#ifdef WC_NO_INTERNAL_FUNCTION_POINTERS
1317
1318
    enum sha_methods { SHA512_UNSET = 0, SHA512_AVX1, SHA512_AVX2,
1319
                       SHA512_AVX1_RORX, SHA512_AVX2_RORX, SHA512_C };
1320
1321
    /* note that all write access to this static variable must be idempotent,
1322
     * as arranged by Sha512_SetTransform(), else it will be susceptible to
1323
     * data races.
1324
     */
1325
    static enum sha_methods sha_method = SHA512_UNSET;
1326
1327
    #ifdef WC_C_DYNAMIC_FALLBACK
1328
        /* With the AVX backend, wc_Sha512.buffer is in big endian even though
1329
         * the host is little endian.  For WC_C_DYNAMIC_FALLBACK, which requires
1330
         * alternating between AVX and C, we activate WC_SHA512_RAW_BE_BUFFER,
1331
         * which brings in the below shims for just-in-time byte swapping on
1332
         * each call to the C back end.  This keeps the buffers big endian at
1333
         * all times.
1334
         *
1335
         * Callers test WC_SHA512_RAW_BE_BUFFER rather than
1336
         * WC_C_DYNAMIC_FALLBACK directly: the latter is a global setting whose
1337
         * only prerequisite is WC_HAVE_VECTOR_SPEEDUPS, so it can be set in
1338
         * configurations that never compile these adapters (WOLFSSL_SP_ASM
1339
         * only, 32-bit x86 --enable-intelasm, ...), and suppressing the
1340
         * caller-side byte-reversal there would leave nothing to compensate.
1341
         */
1342
        #define WC_SHA512_RAW_BE_BUFFER
1343
    #endif
1344
1345
    #ifdef WC_SHA512_RAW_BE_BUFFER
1346
1347
    static WC_INLINE int Transform_Sha512_C_from_raw_scr(wc_Sha512 *sha512,
1348
        const byte* data, Sha512TScratch* WC_RESTRICT scr)
1349
    {
1350
        /* data may be unaligned caller input: stage it in the aligned
1351
         * context buffer before word-wise byte reversal. */
1352
        if (data != (const byte*)sha512->buffer)
1353
            XMEMCPY(sha512->buffer, data, WC_SHA512_BLOCK_SIZE);
1354
    #ifdef LITTLE_ENDIAN_ORDER
1355
        ByteReverseWords64(sha512->buffer, sha512->buffer,
1356
                           WC_SHA512_BLOCK_SIZE);
1357
    #endif
1358
        return _Transform_Sha512_scr(sha512, scr);
1359
    }
1360
1361
    static WC_INLINE int Transform_Sha512_C_from_raw(wc_Sha512 *sha512)
1362
    {
1363
        int ret;
1364
        WC_SHA512_TSCRATCH_DECL(sha512);
1365
        WC_SHA512_TSCRATCH_BEGIN(sha512);
1366
        ret = Transform_Sha512_C_from_raw_scr(sha512,
1367
                  (const byte*)sha512->buffer, sha512TScr);
1368
        WC_SHA512_TSCRATCH_WIPE(sha512);
1369
        return ret;
1370
    }
1371
1372
    static WC_INLINE int Transform_Sha512_Len_C_from_raw(wc_Sha512 *sha512,
1373
                                                         word32 len)
1374
    {
1375
        const byte* data = sha512->data;
1376
        int ret = 0;
1377
        WC_SHA512_TSCRATCH_DECL(sha512);
1378
1379
        WC_SHA512_TSCRATCH_BEGIN(sha512);
1380
        while (len >= WC_SHA512_BLOCK_SIZE) {
1381
            ret = Transform_Sha512_C_from_raw_scr(sha512, data, sha512TScr);
1382
            if (ret != 0)
1383
                break;
1384
            data += WC_SHA512_BLOCK_SIZE;
1385
            len  -= WC_SHA512_BLOCK_SIZE;
1386
        }
1387
        WC_SHA512_TSCRATCH_WIPE(sha512);
1388
1389
        return ret;
1390
    }
1391
    #endif /* WC_SHA512_RAW_BE_BUFFER */
1392
1393
    static void Sha512_SetTransform(void)
1394
    {
1395
        if (sha_method != SHA512_UNSET)
1396
            return;
1397
1398
        /* Note that, with WC_C_DYNAMIC_FALLBACK, sha_method records CPU
1399
         * capability only.  Whether vector registers are actually usable is
1400
         * determined independently at each transform via
1401
         * SAVE_VECTOR_REGISTERS2(), allowing a context to move freely between
1402
         * vectorized and C transforms call by call.
1403
         */
1404
1405
        cpuid_get_flags_atomic(&intel_flags);
1406
1407
    #if defined(HAVE_INTEL_AVX2)
1408
        if (IS_INTEL_AVX2(intel_flags)) {
1409
        #ifdef HAVE_INTEL_RORX
1410
            if (IS_INTEL_BMI2(intel_flags)) {
1411
                sha_method = SHA512_AVX2_RORX;
1412
            }
1413
            else
1414
        #endif
1415
            {
1416
                sha_method = SHA512_AVX2;
1417
            }
1418
        }
1419
        else
1420
    #endif
1421
    #if defined(HAVE_INTEL_AVX1)
1422
        if (IS_INTEL_AVX1(intel_flags)) {
1423
        #ifdef HAVE_INTEL_RORX
1424
            if (IS_INTEL_BMI2(intel_flags)) {
1425
                sha_method = SHA512_AVX1_RORX;
1426
            }
1427
            else
1428
        #endif
1429
            {
1430
                sha_method = SHA512_AVX1;
1431
            }
1432
        }
1433
        else
1434
    #endif
1435
        {
1436
            sha_method = SHA512_C;
1437
        }
1438
    }
1439
1440
    static WC_INLINE int Transform_Sha512(wc_Sha512 *sha512) {
1441
        int ret;
1442
    #ifdef WC_C_DYNAMIC_FALLBACK
1443
        if ((sha_method == SHA512_C) ||
1444
            (SAVE_VECTOR_REGISTERS2() != 0))
1445
        {
1446
            return Transform_Sha512_C_from_raw(sha512);
1447
        }
1448
    #else
1449
        if (sha_method == SHA512_C) {
1450
            #ifdef WC_SHA512_RAW_BE_BUFFER
1451
            /* not currently reachable */
1452
            return Transform_Sha512_C_from_raw(sha512);
1453
            #else
1454
            return _Transform_Sha512(sha512);
1455
            #endif
1456
        }
1457
        SAVE_VECTOR_REGISTERS(return _svr_ret;);
1458
    #endif
1459
        switch (sha_method) {
1460
        case SHA512_AVX2:
1461
            ret = Transform_Sha512_AVX2(sha512);
1462
            break;
1463
        case SHA512_AVX2_RORX:
1464
            ret = Transform_Sha512_AVX2_RORX(sha512);
1465
            break;
1466
        case SHA512_AVX1:
1467
            ret = Transform_Sha512_AVX1(sha512);
1468
            break;
1469
        case SHA512_AVX1_RORX:
1470
            ret = Transform_Sha512_AVX1_RORX(sha512);
1471
            break;
1472
        case SHA512_C:
1473
        case SHA512_UNSET:
1474
        default:
1475
            #ifdef WC_SHA512_RAW_BE_BUFFER
1476
            /* not reachable -- the C path exits above, before vector register
1477
             * save -- but must stay layout-correct. */
1478
            ret = Transform_Sha512_C_from_raw(sha512);
1479
            #else
1480
            ret = _Transform_Sha512(sha512);
1481
            #endif
1482
            break;
1483
        }
1484
        RESTORE_VECTOR_REGISTERS();
1485
        return ret;
1486
    }
1487
1488
    static WC_INLINE int Transform_Sha512_Len(wc_Sha512 *sha512, word32 len) {
1489
        int ret;
1490
    #ifdef WC_C_DYNAMIC_FALLBACK
1491
        if ((sha_method == SHA512_C) ||
1492
            (SAVE_VECTOR_REGISTERS2() != 0))
1493
        {
1494
            return Transform_Sha512_Len_C_from_raw(sha512, len);
1495
        }
1496
    #else
1497
        SAVE_VECTOR_REGISTERS(return _svr_ret;);
1498
    #endif
1499
        switch (sha_method) {
1500
        case SHA512_AVX2:
1501
            ret = Transform_Sha512_AVX2_Len(sha512, len);
1502
            break;
1503
        case SHA512_AVX2_RORX:
1504
            ret = Transform_Sha512_AVX2_RORX_Len(sha512, len);
1505
            break;
1506
        case SHA512_AVX1:
1507
            ret = Transform_Sha512_AVX1_Len(sha512, len);
1508
            break;
1509
        case SHA512_AVX1_RORX:
1510
            ret = Transform_Sha512_AVX1_RORX_Len(sha512, len);
1511
            break;
1512
        case SHA512_C:
1513
        case SHA512_UNSET:
1514
        default:
1515
            #ifdef WC_SHA512_RAW_BE_BUFFER
1516
            /* not reachable -- the C path exits above, before vector register
1517
             * save -- but must stay correct. */
1518
            ret = Transform_Sha512_Len_C_from_raw(sha512, len);
1519
            #else
1520
            ret = 0;
1521
            #endif
1522
            break;
1523
        }
1524
        RESTORE_VECTOR_REGISTERS();
1525
        return ret;
1526
    }
1527
1528
#else /* !WC_NO_INTERNAL_FUNCTION_POINTERS */
1529
1530
    static int (*Transform_Sha512_p)(wc_Sha512* sha512) = _Transform_Sha512;
1531
    static int (*Transform_Sha512_Len_p)(wc_Sha512* sha512, word32 len) = NULL;
1532
    static int transform_check = 0;
1533
    #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1534
    static int Transform_Sha512_is_vectorized = 0;
1535
    #endif
1536
1537
    static WC_INLINE int Transform_Sha512(wc_Sha512 *sha512) {
1538
        int ret;
1539
    #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1540
        if (Transform_Sha512_is_vectorized)
1541
            SAVE_VECTOR_REGISTERS(return _svr_ret;);
1542
    #endif
1543
        ret = (*Transform_Sha512_p)(sha512);
1544
    #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1545
        if (Transform_Sha512_is_vectorized)
1546
            RESTORE_VECTOR_REGISTERS();
1547
    #endif
1548
        return ret;
1549
    }
1550
    static WC_INLINE int Transform_Sha512_Len(wc_Sha512 *sha512, word32 len) {
1551
        int ret;
1552
    #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1553
        if (Transform_Sha512_is_vectorized)
1554
            SAVE_VECTOR_REGISTERS(return _svr_ret;);
1555
    #endif
1556
        ret = (*Transform_Sha512_Len_p)(sha512, len);
1557
    #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1558
        if (Transform_Sha512_is_vectorized)
1559
            RESTORE_VECTOR_REGISTERS();
1560
    #endif
1561
        return ret;
1562
    }
1563
1564
    static void Sha512_SetTransform(void)
1565
    {
1566
        if (transform_check)
1567
            return;
1568
1569
        cpuid_get_flags_atomic(&intel_flags);
1570
1571
    #if defined(HAVE_INTEL_AVX2)
1572
        if (IS_INTEL_AVX2(intel_flags)) {
1573
        #ifdef HAVE_INTEL_RORX
1574
            if (IS_INTEL_BMI2(intel_flags)) {
1575
                Transform_Sha512_p = Transform_Sha512_AVX2_RORX;
1576
                Transform_Sha512_Len_p = Transform_Sha512_AVX2_RORX_Len;
1577
            #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1578
                Transform_Sha512_is_vectorized = 1;
1579
            #endif
1580
            }
1581
            else
1582
        #endif
1583
            {
1584
                Transform_Sha512_p = Transform_Sha512_AVX2;
1585
                Transform_Sha512_Len_p = Transform_Sha512_AVX2_Len;
1586
            #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1587
                Transform_Sha512_is_vectorized = 1;
1588
            #endif
1589
            }
1590
        }
1591
        else
1592
    #endif
1593
    #if defined(HAVE_INTEL_AVX1)
1594
        if (IS_INTEL_AVX1(intel_flags)) {
1595
        #ifdef HAVE_INTEL_RORX
1596
            if (IS_INTEL_BMI2(intel_flags)) {
1597
                Transform_Sha512_p = Transform_Sha512_AVX1_RORX;
1598
                Transform_Sha512_Len_p = Transform_Sha512_AVX1_RORX_Len;
1599
            #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1600
                Transform_Sha512_is_vectorized = 1;
1601
            #endif
1602
            }
1603
            else
1604
        #endif
1605
            {
1606
                Transform_Sha512_p = Transform_Sha512_AVX1;
1607
                Transform_Sha512_Len_p = Transform_Sha512_AVX1_Len;
1608
            #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1609
                Transform_Sha512_is_vectorized = 1;
1610
            #endif
1611
            }
1612
        }
1613
        else
1614
    #endif
1615
        {
1616
            Transform_Sha512_p = _Transform_Sha512;
1617
            Transform_Sha512_Len_p = NULL;
1618
        #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1619
            Transform_Sha512_is_vectorized = 0;
1620
        #endif
1621
        }
1622
1623
        transform_check = 1;
1624
    }
1625
1626
#endif /* !WC_NO_INTERNAL_FUNCTION_POINTERS */
1627
1628
#elif defined(WOLFSSL_ARMASM)
1629
1630
#ifdef __aarch64__
1631
1632
/* AArch64: choose the SHA-512 crypto extension, NEON or the software
1633
 * implementation at runtime based on CPU features, so a core that lacks the
1634
 * crypto extension and/or NEON still has a working SHA-512. */
1635
#define NEED_SOFT_SHA512
1636
1637
static int transform_check = 0;
1638
static cpuid_flags_atomic_t cpuid_flags = WC_CPUID_ATOMIC_INITIALIZER;
1639
1640
static int _Transform_Sha512(wc_Sha512* sha512);
1641
static int _Transform_Sha512_scr(wc_Sha512* sha512,
1642
                                 Sha512TScratch* WC_RESTRICT scr);
1643
static int Transform_Sha512_C(wc_Sha512* sha512, const byte* data);
1644
static int Transform_Sha512_Len_C(wc_Sha512* sha512, const byte* data,
1645
    word32 len);
1646
1647
/* Initialize to the software fallback so the pointers are never NULL if they
1648
 * are read before Sha512_SetTransform() has published the selected variant. */
1649
static int (*Transform_Sha512_p)(wc_Sha512* sha512, const byte* data)
1650
    = Transform_Sha512_C;
1651
static int (*Transform_Sha512_Len_p)(wc_Sha512* sha512, const byte* data,
1652
    word32 len) = Transform_Sha512_Len_C;
1653
1654
/* Software fallback adapters in the asm (sha512, data[, len]) form. The asm
1655
 * transforms consume raw big-endian input and byte-reverse internally, so the
1656
 * software path mirrors that by reversing the block before _Transform_Sha512()
1657
 * (which reads host-endian words from sha512->buffer). */
1658
static int Transform_Sha512_C(wc_Sha512* sha512, const byte* data)
1659
{
1660
    if (data != (const byte*)sha512->buffer)
1661
        XMEMCPY(sha512->buffer, data, WC_SHA512_BLOCK_SIZE);
1662
#ifdef LITTLE_ENDIAN_ORDER
1663
    ByteReverseWords64(sha512->buffer, sha512->buffer, WC_SHA512_BLOCK_SIZE);
1664
#endif
1665
    return _Transform_Sha512(sha512);
1666
}
1667
static int Transform_Sha512_Len_C(wc_Sha512* sha512, const byte* data,
1668
    word32 len)
1669
{
1670
    int ret = 0;
1671
1672
    while (len >= WC_SHA512_BLOCK_SIZE) {
1673
        ret = Transform_Sha512_C(sha512, data);
1674
        if (ret != 0)
1675
            break;
1676
        data += WC_SHA512_BLOCK_SIZE;
1677
        len  -= WC_SHA512_BLOCK_SIZE;
1678
    }
1679
1680
    return ret;
1681
}
1682
1683
/* The SHA-512 crypto instructions operate on SIMD registers, so the assembly
1684
 * only defines these when NEON is available - see armv8-sha512-asm.S and the
1685
 * prototype guard in sha512.h. */
1686
/* Both transforms below run on v0-v31 and save d8-d15
1687
 * (port/arm/armv8-sha512-asm.S), so a kernel module must bracket them. */
1688
#if defined(__aarch64__) && defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS)
1689
    #define WC_SHA512_ARM64_SVR_BEGIN()                                     \
1690
        do { int _svr_ret = SAVE_VECTOR_REGISTERS2();                       \
1691
             if (_svr_ret != 0) return _svr_ret; } while (0)
1692
    #define WC_SHA512_ARM64_SVR_END()  RESTORE_VECTOR_REGISTERS()
1693
#else
1694
    #define WC_SHA512_ARM64_SVR_BEGIN() WC_DO_NOTHING
1695
    #define WC_SHA512_ARM64_SVR_END()   WC_DO_NOTHING
1696
#endif
1697
1698
#if defined(WOLFSSL_ARMASM_CRYPTO_SHA512) && !defined(WOLFSSL_ARMASM_NO_NEON)
1699
static int Transform_Sha512_crypto_aarch64(wc_Sha512* sha512, const byte* data)
1700
{
1701
    WC_SHA512_ARM64_SVR_BEGIN();
1702
    Transform_Sha512_Len_crypto(sha512, data, WC_SHA512_BLOCK_SIZE);
1703
    WC_SHA512_ARM64_SVR_END();
1704
    return 0;
1705
}
1706
static int Transform_Sha512_Len_crypto_aarch64(wc_Sha512* sha512,
1707
    const byte* data, word32 len)
1708
{
1709
    WC_SHA512_ARM64_SVR_BEGIN();
1710
    Transform_Sha512_Len_crypto(sha512, data, len);
1711
    WC_SHA512_ARM64_SVR_END();
1712
    return 0;
1713
}
1714
#endif
1715
#ifndef WOLFSSL_ARMASM_NO_NEON
1716
static int Transform_Sha512_neon_aarch64(wc_Sha512* sha512, const byte* data)
1717
{
1718
    WC_SHA512_ARM64_SVR_BEGIN();
1719
    Transform_Sha512_Len_neon(sha512, data, WC_SHA512_BLOCK_SIZE);
1720
    WC_SHA512_ARM64_SVR_END();
1721
    return 0;
1722
}
1723
static int Transform_Sha512_Len_neon_aarch64(wc_Sha512* sha512,
1724
    const byte* data, word32 len)
1725
{
1726
    WC_SHA512_ARM64_SVR_BEGIN();
1727
    Transform_Sha512_Len_neon(sha512, data, len);
1728
    WC_SHA512_ARM64_SVR_END();
1729
    return 0;
1730
}
1731
#endif
1732
1733
static WC_INLINE int Transform_Sha512(wc_Sha512 *sha512, const byte* data)
1734
{
1735
    return (*Transform_Sha512_p)(sha512, data);
1736
}
1737
static WC_INLINE int Transform_Sha512_Len(wc_Sha512 *sha512, const byte* data,
1738
    word32 len)
1739
{
1740
    return (*Transform_Sha512_Len_p)(sha512, data, len);
1741
}
1742
1743
static void Sha512_SetTransform(void)
1744
{
1745
    if (transform_check)
1746
        return;
1747
1748
    cpuid_get_flags_atomic(&cpuid_flags);
1749
1750
#if defined(WOLFSSL_ARMASM_CRYPTO_SHA512) && !defined(WOLFSSL_ARMASM_NO_NEON)
1751
    if (IS_AARCH64_SHA512(cpuid_flags)) {
1752
        Transform_Sha512_p     = Transform_Sha512_crypto_aarch64;
1753
        Transform_Sha512_Len_p = Transform_Sha512_Len_crypto_aarch64;
1754
    }
1755
    else
1756
#endif
1757
#ifndef WOLFSSL_ARMASM_NO_NEON
1758
    if (IS_AARCH64_ASIMD(cpuid_flags)) {
1759
        Transform_Sha512_p     = Transform_Sha512_neon_aarch64;
1760
        Transform_Sha512_Len_p = Transform_Sha512_Len_neon_aarch64;
1761
    }
1762
    else
1763
#endif
1764
    {
1765
        Transform_Sha512_p     = Transform_Sha512_C;
1766
        Transform_Sha512_Len_p = Transform_Sha512_Len_C;
1767
    }
1768
1769
    transform_check = 1;
1770
}
1771
1772
#else /* !__aarch64__ : 32-bit Arm (Thumb2 / ARMv7) */
1773
1774
static int transform_check = 0;
1775
1776
#if !defined(WOLFSSL_ARMASM_THUMB2) && !defined(WOLFSSL_ARMASM_SHA512_NO_NEON)
1777
/* The 32-bit Arm NEON transform runs on the d registers, so a kernel module
1778
 * must hold the vector registers around it. */
1779
#ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1780
    #define WC_SHA512_ARM32_SVR_BEGIN()                                     \
1781
        do { int _svr_ret = SAVE_VECTOR_REGISTERS2();                       \
1782
             if (_svr_ret != 0) return _svr_ret; } while (0)
1783
    #define WC_SHA512_ARM32_SVR_END()  RESTORE_VECTOR_REGISTERS()
1784
#else
1785
    #define WC_SHA512_ARM32_SVR_BEGIN() WC_DO_NOTHING
1786
    #define WC_SHA512_ARM32_SVR_END()   WC_DO_NOTHING
1787
#endif
1788
static int Transform_Sha512_neon(wc_Sha512* sha512, const byte* data)
1789
{
1790
    WC_SHA512_ARM32_SVR_BEGIN();
1791
    Transform_Sha512_Len_neon(sha512, data, WC_SHA512_BLOCK_SIZE);
1792
    WC_SHA512_ARM32_SVR_END();
1793
    return 0;
1794
}
1795
static int Transform_Sha512_Len_neon_arm32(wc_Sha512* sha512,
1796
    const byte* data, word32 len)
1797
{
1798
    WC_SHA512_ARM32_SVR_BEGIN();
1799
    Transform_Sha512_Len_neon(sha512, data, len);
1800
    WC_SHA512_ARM32_SVR_END();
1801
    return 0;
1802
}
1803
#endif
1804
#if defined(WOLFSSL_ARMASM_THUMB2) || defined(WOLFSSL_ARMASM_SHA512_NO_NEON)
1805
static int Transform_Sha512_base(wc_Sha512* sha512, const byte* data)
1806
{
1807
    Transform_Sha512_Len_base(sha512, data, WC_SHA512_BLOCK_SIZE);
1808
    return 0;
1809
}
1810
static int Transform_Sha512_Len_base_arm32(wc_Sha512* sha512,
1811
    const byte* data, word32 len)
1812
{
1813
    Transform_Sha512_Len_base(sha512, data, len);
1814
    return 0;
1815
}
1816
#endif
1817
1818
static int (*Transform_Sha512_p)(wc_Sha512* sha512, const byte* data) = NULL;
1819
static int (*Transform_Sha512_Len_p)(wc_Sha512* sha512, const byte* data,
1820
    word32 len) = NULL;
1821
1822
static WC_INLINE int Transform_Sha512(wc_Sha512 *sha512, const byte* data)
1823
{
1824
    return (*Transform_Sha512_p)(sha512, data);
1825
}
1826
static WC_INLINE int Transform_Sha512_Len(wc_Sha512 *sha512, const byte* data,
1827
    word32 len)
1828
{
1829
    return (*Transform_Sha512_Len_p)(sha512, data, len);
1830
}
1831
1832
static void Sha512_SetTransform(void)
1833
{
1834
    if (transform_check)
1835
        return;
1836
1837
#if !defined(WOLFSSL_ARMASM_THUMB2) && !defined(WOLFSSL_ARMASM_SHA512_NO_NEON)
1838
    {
1839
        Transform_Sha512_p = Transform_Sha512_neon;
1840
        Transform_Sha512_Len_p = Transform_Sha512_Len_neon_arm32;
1841
    }
1842
#else
1843
    {
1844
        Transform_Sha512_p = Transform_Sha512_base;
1845
        Transform_Sha512_Len_p = Transform_Sha512_Len_base_arm32;
1846
    }
1847
#endif
1848
1849
    transform_check = 1;
1850
}
1851
1852
#endif /* __aarch64__ */
1853
1854
#elif defined(WOLFSSL_PPC64_ASM) || defined(WOLFSSL_PPC32_ASM)
1855
1856
/* Scalar (base instruction) SHA-512 transform for big-endian PowerPC (32- and
1857
 * 64-bit).  The asm loads the message words directly, so no byte reversal is
1858
 * needed and the (sha512, data, len) form is used just like the ARM assembly. */
1859
extern void Transform_Sha512_Len(wc_Sha512* sha512, const byte* data,
1860
    word32 len);
1861
1862
#if defined(WOLFSSL_PPC64_ASM) && defined(WOLFSSL_PPC64_ASM_CRYPTO)
1863
/* POWER8+ has a vector SHA-512 sigma instruction (vshasigmad).  When built
1864
 * in, select that implementation at run time if the CPU supports it.
1865
 *
1866
 * A run-time flag with direct calls is used rather than a function pointer:
1867
 * an indirect call would require an ELFv1 function descriptor, whereas direct
1868
 * calls work under both the ELFv1 and ELFv2 ABIs. */
1869
extern void Transform_Sha512_Len_crypto(wc_Sha512* sha512, const byte* data,
1870
    word32 len);
1871
1872
/* Resolved dispatch decision, accessed with the wolfSSL atomic APIs so the
1873
 * lazy one-time detection is free of data races.  WC_CPUID_INITIALIZER means
1874
 * "not yet determined"; the write is idempotent (all callers compute the same
1875
 * value from the atomic master flags), so a benign concurrent double-write is
1876
 * harmless. */
1877
static wolfSSL_Atomic_Uint sha512_use_crypto =
1878
    WOLFSSL_ATOMIC_INITIALIZER(WC_CPUID_INITIALIZER);
1879
1880
/* Detect CPU support via the central cpuid module on first use. */
1881
static WC_INLINE void SHA512_TRANSFORM_LEN(wc_Sha512* sha512, const byte* data,
1882
    word32 len)
1883
{
1884
    unsigned int use_crypto = WOLFSSL_ATOMIC_LOAD(sha512_use_crypto);
1885
1886
    if (use_crypto == WC_CPUID_INITIALIZER) {
1887
        use_crypto = (unsigned int)(IS_PPC64_VEC_CRYPTO(cpuid_get_flags()) != 0);
1888
        WOLFSSL_ATOMIC_STORE(sha512_use_crypto, use_crypto);
1889
    }
1890
1891
    if (use_crypto)
1892
        Transform_Sha512_Len_crypto(sha512, data, len);
1893
    else
1894
        Transform_Sha512_Len(sha512, data, len);
1895
}
1896
/* SHA512_TRANSFORM_LEN is a function here, not a macro, so signal that a
1897
 * dispatcher is provided - otherwise the generic fallback below sees
1898
 * !defined(SHA512_TRANSFORM_LEN) and shadows it with a base-only macro. */
1899
#define SHA512_HAVE_TRANSFORM_LEN
1900
#else
1901
#define SHA512_TRANSFORM_LEN(s, d, l)   Transform_Sha512_Len((s), (d), (l))
1902
#define SHA512_HAVE_TRANSFORM_LEN
1903
#endif
1904
1905
static WC_INLINE int Transform_Sha512(wc_Sha512* sha512, const byte* data)
1906
{
1907
    SHA512_TRANSFORM_LEN(sha512, data, WC_SHA512_BLOCK_SIZE);
1908
    return 0;
1909
}
1910
1911
#define Sha512_SetTransform()   WC_DO_NOTHING
1912
1913
#elif defined(WOLFSSL_RISCV_ASM)
1914
1915
static WC_INLINE int Transform_Sha512(wc_Sha512* sha512, const byte* data)
1916
{
1917
#if defined(WOLFSSL_RISCV_VECTOR_CRYPTO_ASM)
1918
    Transform_Sha512_Len_riscv_vector(sha512, data, WC_SHA512_BLOCK_SIZE);
1919
#elif defined(WOLFSSL_RISCV_SCALAR_CRYPTO_ASM)
1920
    Transform_Sha512_Len_riscv_crypto(sha512, data, WC_SHA512_BLOCK_SIZE);
1921
#else
1922
    Transform_Sha512_Len_riscv(sha512, data, WC_SHA512_BLOCK_SIZE);
1923
#endif
1924
    return 0;
1925
}
1926
static WC_INLINE int Transform_Sha512_Len(wc_Sha512* sha512, const byte* data,
1927
    word32 len)
1928
{
1929
#if defined(WOLFSSL_RISCV_VECTOR_CRYPTO_ASM)
1930
    Transform_Sha512_Len_riscv_vector(sha512, data, len);
1931
#elif defined(WOLFSSL_RISCV_SCALAR_CRYPTO_ASM)
1932
    Transform_Sha512_Len_riscv_crypto(sha512, data, len);
1933
#else
1934
    Transform_Sha512_Len_riscv(sha512, data, len);
1935
#endif
1936
    return 0;
1937
}
1938
1939
#else
1940
3.17M
    #define Transform_Sha512(sha512) _Transform_Sha512(sha512)
1941
1942
#endif
1943
1944
/* For platforms that share the (sha512, data, len) block-loop call below but
1945
 * don't provide their own dispatcher (e.g. ARM), call the length transform
1946
 * directly. */
1947
#if (defined(WOLFSSL_ARMASM) || defined(WOLFSSL_PPC64_ASM) || \
1948
     defined(WOLFSSL_PPC32_ASM)) && \
1949
    !defined(SHA512_TRANSFORM_LEN) && !defined(SHA512_HAVE_TRANSFORM_LEN)
1950
#define SHA512_TRANSFORM_LEN(s, d, l)   Transform_Sha512_Len((s), (d), (l))
1951
#endif
1952
1953
#ifdef WOLFSSL_SHA512
1954
1955
static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId,
1956
                             int (*initfp)(wc_Sha512*))
1957
2.28M
{
1958
2.28M
    int ret = 0;
1959
1960
2.28M
    if (sha512 == NULL) {
1961
0
        return BAD_FUNC_ARG;
1962
0
    }
1963
1964
2.28M
    XMEMSET(sha512, 0, sizeof(*sha512));
1965
1966
2.28M
    sha512->heap = heap;
1967
#ifdef WOLFSSL_SMALL_STACK_CACHE
1968
    /* This allocation combines the customary W buffer used by
1969
     * _Transform_Sha512() with additional buffer space used by
1970
     * wc_Sha512Transform().
1971
     */
1972
    sha512->W = (word64 *)XMALLOC(sizeof(Sha512TScratch) + WC_SHA512_BLOCK_SIZE,
1973
                                  sha512->heap, DYNAMIC_TYPE_DIGEST);
1974
    if (sha512->W == NULL)
1975
        return MEMORY_E;
1976
#endif
1977
2.28M
#ifdef WOLF_CRYPTO_CB
1978
2.28M
    sha512->devId = devId;
1979
2.28M
    sha512->devCtx = NULL;
1980
2.28M
#endif
1981
1982
#ifdef WOLFSSL_HASH_KEEP
1983
    sha512->msg  = NULL;
1984
    sha512->len  = 0;
1985
    sha512->used = 0;
1986
#endif
1987
1988
    /* call the initialization function pointed to by initfp */
1989
2.28M
    ret = initfp(sha512);
1990
1991
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
1992
    if (ret == 0) {
1993
        ret = wolfAsync_DevCtxInit(&sha512->asyncDev,
1994
                        WOLFSSL_ASYNC_MARKER_SHA512, sha512->heap, devId);
1995
    }
1996
#else
1997
2.28M
    (void)devId;
1998
2.28M
#endif /* WOLFSSL_ASYNC_CRYPT */
1999
#ifdef WOLFSSL_IMXRT1170_CAAM
2000
    if (ret == 0)
2001
        ret = wc_CAAM_HashInit(&sha512->hndl, &sha512->ctx, WC_HASH_TYPE_SHA512);
2002
#endif
2003
2004
#ifdef WOLFSSL_SMALL_STACK_CACHE
2005
    if (ret != 0) {
2006
        XFREE(sha512->W, sha512->heap, DYNAMIC_TYPE_DIGEST);
2007
        sha512->W = NULL;
2008
    }
2009
#endif
2010
2011
2.28M
    return ret;
2012
2.28M
} /* InitSha512_Family */
2013
2014
int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
2015
2.28M
{
2016
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2017
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2018
    if (sha512->ctx.mode != ESP32_SHA_INIT) {
2019
        ESP_LOGV(TAG, "Set ctx mode from prior value: "
2020
                      "%d", sha512->ctx.mode);
2021
    }
2022
    /* We know this is a fresh, uninitialized item, so set to INIT */
2023
    sha512->ctx.mode = ESP32_SHA_INIT;
2024
#endif
2025
2026
2027
2.28M
    return InitSha512_Family(sha512, heap, devId, InitSha512);
2028
2.28M
}
2029
2030
#if !defined(WOLFSSL_NOSHA512_224) && \
2031
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
2032
int wc_InitSha512_224_ex(wc_Sha512* sha512, void* heap, int devId)
2033
585
{
2034
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2035
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2036
    /* No SHA512/224 HW support is available, set to SW. */
2037
    sha512->ctx.mode = ESP32_SHA_SW; /* no SHA224 HW, so always SW */
2038
#endif
2039
585
    return InitSha512_Family(sha512, heap, devId, InitSha512_224);
2040
585
}
2041
#endif /* !WOLFSSL_NOSHA512_224 ... */
2042
2043
#if !defined(WOLFSSL_NOSHA512_256) && \
2044
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
2045
int wc_InitSha512_256_ex(wc_Sha512* sha512, void* heap, int devId)
2046
184
{
2047
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2048
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2049
    /* No SHA512/256 HW support is available on ESP32, set to SW. */
2050
    sha512->ctx.mode = ESP32_SHA_SW;
2051
#endif
2052
184
    return InitSha512_Family(sha512, heap, devId, InitSha512_256);
2053
184
}
2054
#endif /* !WOLFSSL_NOSHA512_256 ... */
2055
2056
#endif /* WOLFSSL_SHA512 */
2057
2058
#if (!defined(WOLFSSL_ARMASM) && !defined(WOLFSSL_PPC64_ASM) && \
2059
     !defined(WOLFSSL_PPC32_ASM) && !defined(WOLFSSL_RISCV_ASM)) || \
2060
    defined(NEED_SOFT_SHA512)
2061
2062
static const word64 K512[80] = {
2063
    W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd),
2064
    W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc),
2065
    W64LIT(0x3956c25bf348b538), W64LIT(0x59f111f1b605d019),
2066
    W64LIT(0x923f82a4af194f9b), W64LIT(0xab1c5ed5da6d8118),
2067
    W64LIT(0xd807aa98a3030242), W64LIT(0x12835b0145706fbe),
2068
    W64LIT(0x243185be4ee4b28c), W64LIT(0x550c7dc3d5ffb4e2),
2069
    W64LIT(0x72be5d74f27b896f), W64LIT(0x80deb1fe3b1696b1),
2070
    W64LIT(0x9bdc06a725c71235), W64LIT(0xc19bf174cf692694),
2071
    W64LIT(0xe49b69c19ef14ad2), W64LIT(0xefbe4786384f25e3),
2072
    W64LIT(0x0fc19dc68b8cd5b5), W64LIT(0x240ca1cc77ac9c65),
2073
    W64LIT(0x2de92c6f592b0275), W64LIT(0x4a7484aa6ea6e483),
2074
    W64LIT(0x5cb0a9dcbd41fbd4), W64LIT(0x76f988da831153b5),
2075
    W64LIT(0x983e5152ee66dfab), W64LIT(0xa831c66d2db43210),
2076
    W64LIT(0xb00327c898fb213f), W64LIT(0xbf597fc7beef0ee4),
2077
    W64LIT(0xc6e00bf33da88fc2), W64LIT(0xd5a79147930aa725),
2078
    W64LIT(0x06ca6351e003826f), W64LIT(0x142929670a0e6e70),
2079
    W64LIT(0x27b70a8546d22ffc), W64LIT(0x2e1b21385c26c926),
2080
    W64LIT(0x4d2c6dfc5ac42aed), W64LIT(0x53380d139d95b3df),
2081
    W64LIT(0x650a73548baf63de), W64LIT(0x766a0abb3c77b2a8),
2082
    W64LIT(0x81c2c92e47edaee6), W64LIT(0x92722c851482353b),
2083
    W64LIT(0xa2bfe8a14cf10364), W64LIT(0xa81a664bbc423001),
2084
    W64LIT(0xc24b8b70d0f89791), W64LIT(0xc76c51a30654be30),
2085
    W64LIT(0xd192e819d6ef5218), W64LIT(0xd69906245565a910),
2086
    W64LIT(0xf40e35855771202a), W64LIT(0x106aa07032bbd1b8),
2087
    W64LIT(0x19a4c116b8d2d0c8), W64LIT(0x1e376c085141ab53),
2088
    W64LIT(0x2748774cdf8eeb99), W64LIT(0x34b0bcb5e19b48a8),
2089
    W64LIT(0x391c0cb3c5c95a63), W64LIT(0x4ed8aa4ae3418acb),
2090
    W64LIT(0x5b9cca4f7763e373), W64LIT(0x682e6ff3d6b2b8a3),
2091
    W64LIT(0x748f82ee5defb2fc), W64LIT(0x78a5636f43172f60),
2092
    W64LIT(0x84c87814a1f0ab72), W64LIT(0x8cc702081a6439ec),
2093
    W64LIT(0x90befffa23631e28), W64LIT(0xa4506cebde82bde9),
2094
    W64LIT(0xbef9a3f7b2c67915), W64LIT(0xc67178f2e372532b),
2095
    W64LIT(0xca273eceea26619c), W64LIT(0xd186b8c721c0c207),
2096
    W64LIT(0xeada7dd6cde0eb1e), W64LIT(0xf57d4f7fee6ed178),
2097
    W64LIT(0x06f067aa72176fba), W64LIT(0x0a637dc5a2c898a6),
2098
    W64LIT(0x113f9804bef90dae), W64LIT(0x1b710b35131c471b),
2099
    W64LIT(0x28db77f523047d84), W64LIT(0x32caab7b40c72493),
2100
    W64LIT(0x3c9ebe0a15c9bebc), W64LIT(0x431d67c49c100d4c),
2101
    W64LIT(0x4cc5d4becb3e42b6), W64LIT(0x597f299cfc657e2a),
2102
    W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817)
2103
};
2104
2105
57.5M
#define blk0(i) (W[i] = sha512->buffer[i])
2106
2107
230M
#define blk2(i) (\
2108
230M
               W[ (i)     & 15] += \
2109
230M
            s1(W[((i)-2)  & 15])+ \
2110
230M
               W[((i)-7)  & 15] + \
2111
230M
            s0(W[((i)-15) & 15])  \
2112
230M
        )
2113
2114
287M
#define Ch(x,y,z)  ((z) ^ ((x) & ((y) ^ (z))))
2115
287M
#define Maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
2116
2117
3.59M
#define a(i) T[(0-(i)) & 7]
2118
3.59M
#define b(i) T[(1-(i)) & 7]
2119
3.59M
#define c(i) T[(2-(i)) & 7]
2120
291M
#define d(i) T[(3-(i)) & 7]
2121
3.59M
#define e(i) T[(4-(i)) & 7]
2122
3.59M
#define f(i) T[(5-(i)) & 7]
2123
3.59M
#define g(i) T[(6-(i)) & 7]
2124
867M
#define h(i) T[(7-(i)) & 7]
2125
2126
287M
#define S0(x) (rotrFixed64(x,28) ^ rotrFixed64(x,34) ^ rotrFixed64(x,39))
2127
287M
#define S1(x) (rotrFixed64(x,14) ^ rotrFixed64(x,18) ^ rotrFixed64(x,41))
2128
230M
#define s0(x) (rotrFixed64(x,1)  ^ rotrFixed64(x,8)  ^ ((x)>>7))
2129
230M
#define s1(x) (rotrFixed64(x,19) ^ rotrFixed64(x,61) ^ ((x)>>6))
2130
2131
#define R(i) \
2132
287M
    h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[(i)+j] + (j ? blk2(i) : blk0(i)); \
2133
287M
    d(i) += h(i); \
2134
287M
    h(i) += S0(a(i)) + Maj(a(i),b(i),c(i))
2135
2136
static int _Transform_Sha512_scr(wc_Sha512* sha512,
2137
                                 Sha512TScratch* WC_RESTRICT scr)
2138
3.59M
{
2139
3.59M
    const word64* K = K512;
2140
3.59M
    word32 j;
2141
3.59M
    word64 T[8];
2142
3.59M
    word64* WC_RESTRICT W = scr->W;
2143
2144
    /* Copy digest to working vars */
2145
3.59M
    XMEMCPY(T, sha512->digest, sizeof(T));
2146
2147
#ifdef USE_SLOW_SHA512
2148
    /* over twice as small, but 50% slower */
2149
    /* 80 operations, not unrolled */
2150
    for (j = 0; j < 80; j += 16) {
2151
        int m;
2152
        for (m = 0; m < 16; m++) { /* braces needed here for macros {} */
2153
            R(m);
2154
        }
2155
    }
2156
#else
2157
    /* 80 operations, partially loop unrolled */
2158
21.5M
    for (j = 0; j < 80; j += 16) {
2159
17.9M
        R( 0); R( 1); R( 2); R( 3);
2160
17.9M
        R( 4); R( 5); R( 6); R( 7);
2161
17.9M
        R( 8); R( 9); R(10); R(11);
2162
17.9M
        R(12); R(13); R(14); R(15);
2163
17.9M
    }
2164
3.59M
#endif /* USE_SLOW_SHA512 */
2165
2166
    /* Add the working vars back into digest */
2167
3.59M
    sha512->digest[0] += a(0);
2168
3.59M
    sha512->digest[1] += b(0);
2169
3.59M
    sha512->digest[2] += c(0);
2170
3.59M
    sha512->digest[3] += d(0);
2171
3.59M
    sha512->digest[4] += e(0);
2172
3.59M
    sha512->digest[5] += f(0);
2173
3.59M
    sha512->digest[6] += g(0);
2174
3.59M
    sha512->digest[7] += h(0);
2175
2176
    /* W is the caller's Sha512TScratch, wiped with one ForceZero when the
2177
     * public call returns.  T is wiped here: keeping it register-resident
2178
     * requires it to be a local, and its wipe is one cache line. */
2179
3.59M
    ForceZero(T, sizeof(T));
2180
3.59M
    return 0;
2181
3.59M
}
2182
2183
/* Compatibility entry with per-call scratch, for the dispatch arms,
2184
 * function-pointer tables, and single-block callers, all of whose signatures
2185
 * stay unchanged.  Bulk callers hoist one scratch across their block loop and
2186
 * call _Transform_Sha512_scr() directly, so the wipe runs once per public
2187
 * call rather than once per block. */
2188
static WC_MAYBE_UNUSED int _Transform_Sha512(wc_Sha512* sha512)
2189
2.98M
{
2190
2.98M
    int ret;
2191
2.98M
    WC_SHA512_TSCRATCH_DECL(sha512);
2192
2.98M
    WC_SHA512_TSCRATCH_BEGIN(sha512);
2193
2.98M
    ret = _Transform_Sha512_scr(sha512, sha512TScr);
2194
2.98M
    WC_SHA512_TSCRATCH_WIPE(sha512);
2195
2.98M
    return ret;
2196
2.98M
}
2197
#endif
2198
2199
2200
static WC_INLINE void AddLength(wc_Sha512* sha512, word32 len)
2201
4.60M
{
2202
4.60M
    word64 tmp = sha512->loLen;
2203
4.60M
    if ( (sha512->loLen += len) < tmp)
2204
0
        sha512->hiLen++;                       /* carry low to high */
2205
4.60M
}
2206
2207
static WC_INLINE int Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
2208
4.48M
{
2209
4.48M
    int ret = 0;
2210
    /* do block size increments */
2211
4.48M
    byte* local = (byte*)sha512->buffer;
2212
2213
    /* check that internal buffLen is valid */
2214
4.48M
    if (sha512->buffLen >= WC_SHA512_BLOCK_SIZE)
2215
0
        return BUFFER_E;
2216
2217
4.48M
    if (len == 0)
2218
153k
        return 0;
2219
2220
4.32M
    AddLength(sha512, len);
2221
2222
4.32M
    if (sha512->buffLen > 0) {
2223
2.13M
        word32 add = min(len, WC_SHA512_BLOCK_SIZE - sha512->buffLen);
2224
2.13M
        if (add > 0) {
2225
2.13M
            XMEMCPY(&local[sha512->buffLen], data, add);
2226
2227
2.13M
            sha512->buffLen += add;
2228
2.13M
            data            += add;
2229
2.13M
            len             -= add;
2230
2.13M
        }
2231
2232
2.13M
        if (sha512->buffLen == WC_SHA512_BLOCK_SIZE) {
2233
59.2k
    #if defined(LITTLE_ENDIAN_ORDER) && \
2234
59.2k
            !defined(WC_SHA512_RAW_BE_BUFFER) && \
2235
59.2k
            (!defined(WOLFSSL_ESP32_CRYPT) || \
2236
59.2k
             defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
2237
59.2k
             defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)) && \
2238
59.2k
            !defined(WOLFSSL_ARMASM) && !defined(WOLFSSL_PPC64_ASM) && \
2239
59.2k
            !defined(WOLFSSL_RISCV_ASM)
2240
        #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2241
                (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
2242
            if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
2243
        #endif
2244
59.2k
            {
2245
        #ifdef WOLFSSL_WIDE_BYTE
2246
                WordsFromBytesBE64(sha512->buffer,
2247
                    (const byte*)sha512->buffer, WC_SHA512_BLOCK_SIZE / 8);
2248
        #else
2249
59.2k
                ByteReverseWords64(sha512->buffer, sha512->buffer,
2250
59.2k
                                                         WC_SHA512_BLOCK_SIZE);
2251
59.2k
        #endif
2252
59.2k
            }
2253
59.2k
    #endif
2254
    #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_PPC64_ASM) || \
2255
        defined(WOLFSSL_PPC32_ASM) || defined(WOLFSSL_RISCV_ASM)
2256
            ret = Transform_Sha512(sha512, (const byte*)sha512->buffer);
2257
    #elif !defined(WOLFSSL_ESP32_CRYPT) || \
2258
           defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
2259
           defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2260
59.2k
            ret = Transform_Sha512(sha512);
2261
    #else
2262
            if (sha512->ctx.mode == ESP32_SHA_INIT) {
2263
                esp_sha_try_hw_lock(&sha512->ctx);
2264
            }
2265
            if (sha512->ctx.mode == ESP32_SHA_SW) {
2266
                ByteReverseWords64(sha512->buffer, sha512->buffer,
2267
                                                         WC_SHA512_BLOCK_SIZE);
2268
                ret = Transform_Sha512(sha512);
2269
            }
2270
            else {
2271
                ret = esp_sha512_process(sha512);
2272
            }
2273
    #endif
2274
59.2k
            if (ret == 0)
2275
59.2k
                sha512->buffLen = 0;
2276
20
            else
2277
20
                len = 0;
2278
59.2k
        }
2279
2.13M
    }
2280
2281
#if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_PPC64_ASM) || \
2282
    defined(WOLFSSL_PPC32_ASM) || defined(WOLFSSL_RISCV_ASM)
2283
    if (len >= WC_SHA512_BLOCK_SIZE) {
2284
        word32 blocksLen = len & ~((word32)WC_SHA512_BLOCK_SIZE-1);
2285
2286
#if defined(WOLFSSL_PPC64_ASM) || defined(WOLFSSL_PPC32_ASM)
2287
        SHA512_TRANSFORM_LEN(sha512, data, blocksLen);
2288
#else
2289
        ret = Transform_Sha512_Len(sha512, data, blocksLen);
2290
        if (ret != 0)
2291
            return ret;
2292
#endif
2293
        data += blocksLen;
2294
        len  -= blocksLen;
2295
    }
2296
#else
2297
#if (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2298
     (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
2299
2300
    #ifdef WC_NO_INTERNAL_FUNCTION_POINTERS
2301
    if (sha_method != SHA512_C)
2302
    #else
2303
    if (Transform_Sha512_Len_p != NULL)
2304
    #endif
2305
2306
    {
2307
        word32 blocksLen = len & ~((word32)WC_SHA512_BLOCK_SIZE-1);
2308
2309
        if (blocksLen > 0) {
2310
            sha512->data = data;
2311
            /* Byte reversal performed in function if required. */
2312
            ret = Transform_Sha512_Len(sha512, blocksLen);
2313
            if (ret == 0) {
2314
                data += blocksLen;
2315
                len  -= blocksLen;
2316
            }
2317
        }
2318
    }
2319
    else
2320
#endif
2321
#if !defined(LITTLE_ENDIAN_ORDER) || (defined(WOLFSSL_X86_64_BUILD) && \
2322
        defined(USE_INTEL_SPEEDUP) && (defined(HAVE_INTEL_AVX1) || \
2323
        defined(HAVE_INTEL_AVX2)))
2324
    {
2325
        WC_SHA512_TSCRATCH_DECL(sha512);
2326
2327
        WC_SHA512_TSCRATCH_BEGIN(sha512);
2328
        while (len >= WC_SHA512_BLOCK_SIZE) {
2329
            XMEMCPY(local, data, WC_SHA512_BLOCK_SIZE);
2330
2331
            data += WC_SHA512_BLOCK_SIZE;
2332
            len  -= WC_SHA512_BLOCK_SIZE;
2333
2334
        #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2335
            (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)) && \
2336
            !defined(WC_SHA512_RAW_BE_BUFFER)
2337
            if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
2338
            {
2339
                ByteReverseWords64(sha512->buffer, sha512->buffer,
2340
                                                          WC_SHA512_BLOCK_SIZE);
2341
            }
2342
        #endif
2343
            /* Byte reversal performed in function if required: under
2344
             * WC_SHA512_RAW_BE_BUFFER the in-loop reversal above is compiled
2345
             * out and the from_raw adapter owns it. */
2346
        #ifdef WC_SHA512_RAW_BE_BUFFER
2347
            ret = Transform_Sha512_C_from_raw_scr(sha512,
2348
                      (const byte*)sha512->buffer, sha512TScr);
2349
        #else
2350
            ret = _Transform_Sha512_scr(sha512, sha512TScr);
2351
        #endif
2352
            if (ret != 0)
2353
                break;
2354
        }
2355
        WC_SHA512_TSCRATCH_WIPE(sha512);
2356
    }
2357
#else
2358
4.32M
    {
2359
4.32M
    #if !defined(WOLFSSL_ESP32_CRYPT) || \
2360
4.32M
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
2361
4.32M
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2362
4.32M
        WC_SHA512_TSCRATCH_DECL(sha512);
2363
2364
4.32M
        WC_SHA512_TSCRATCH_BEGIN(sha512);
2365
4.32M
    #endif
2366
4.74M
        while (len >= WC_SHA512_BLOCK_SIZE) {
2367
413k
            XMEMCPY(local, data, WC_SHA512_BLOCK_SIZE);
2368
2369
413k
            data += WC_SHA512_BLOCK_SIZE;
2370
413k
            len  -= WC_SHA512_BLOCK_SIZE;
2371
413k
    #if !defined(WOLFSSL_ESP32_CRYPT) || \
2372
413k
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
2373
413k
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2374
        #ifdef WOLFSSL_WIDE_BYTE
2375
            WordsFromBytesBE64(sha512->buffer, (const byte*)sha512->buffer,
2376
                WC_SHA512_BLOCK_SIZE / 8);
2377
        #else
2378
413k
            ByteReverseWords64(sha512->buffer, sha512->buffer,
2379
413k
                                                       WC_SHA512_BLOCK_SIZE);
2380
413k
        #endif
2381
413k
    #endif
2382
413k
    #if !defined(WOLFSSL_ESP32_CRYPT) || \
2383
413k
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
2384
413k
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2385
413k
            ret = _Transform_Sha512_scr(sha512, sha512TScr);
2386
    #else
2387
            if(sha512->ctx.mode == ESP32_SHA_INIT) {
2388
                esp_sha_try_hw_lock(&sha512->ctx);
2389
            }
2390
            if (sha512->ctx.mode == ESP32_SHA_SW) {
2391
                ByteReverseWords64(sha512->buffer, sha512->buffer,
2392
                                                          WC_SHA512_BLOCK_SIZE);
2393
                ret = Transform_Sha512(sha512);
2394
            }
2395
            else {
2396
                ret = esp_sha512_process(sha512);
2397
            }
2398
    #endif
2399
413k
            if (ret != 0)
2400
0
                break;
2401
413k
        } /* while (len >= WC_SHA512_BLOCK_SIZE) */
2402
4.32M
    #if !defined(WOLFSSL_ESP32_CRYPT) || \
2403
4.32M
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
2404
4.32M
         defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2405
4.32M
        WC_SHA512_TSCRATCH_WIPE(sha512);
2406
4.32M
    #endif
2407
4.32M
    }
2408
0
#endif
2409
0
#endif
2410
2411
4.32M
    if (ret == 0 && len > 0) {
2412
2.21M
        XMEMCPY(local, data, len);
2413
2.21M
        sha512->buffLen = len;
2414
2.21M
    }
2415
2416
4.32M
    return ret;
2417
4.32M
}
2418
2419
#ifdef WOLFSSL_SHA512
2420
2421
int wc_Sha512Update(wc_Sha512* sha512, const byte* data, word32 len)
2422
4.27M
{
2423
4.27M
    if (sha512 == NULL) {
2424
0
        return BAD_FUNC_ARG;
2425
0
    }
2426
4.27M
    if (data == NULL && len == 0) {
2427
        /* valid, but do nothing */
2428
36
        return 0;
2429
36
    }
2430
4.27M
    if (data == NULL) {
2431
0
        return BAD_FUNC_ARG;
2432
0
    }
2433
2434
4.27M
#ifdef WOLF_CRYPTO_CB
2435
4.27M
    #ifndef WOLF_CRYPTO_CB_FIND
2436
4.27M
    if (sha512->devId != INVALID_DEVID)
2437
578
    #endif
2438
578
    {
2439
578
        int ret = wc_CryptoCb_Sha512Hash(sha512, data, len, NULL, 0);
2440
578
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2441
0
            return ret;
2442
        /* fall-through when unavailable */
2443
578
    }
2444
4.27M
#endif
2445
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
2446
    if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
2447
    #if defined(HAVE_INTEL_QA)
2448
        return IntelQaSymSha512(&sha512->asyncDev, NULL, data, len);
2449
    #endif
2450
    }
2451
#endif /* WOLFSSL_ASYNC_CRYPT */
2452
2453
4.27M
    return Sha512Update(sha512, data, len);
2454
4.27M
}
2455
2456
#endif /* WOLFSSL_SHA512 */
2457
2458
#endif /* WOLFSSL_IMX6_CAAM || WOLFSSL_SILABS_SHA512 */
2459
2460
#ifndef WOLF_CRYPTO_CB_ONLY_SHA512
2461
2462
#if defined(WOLFSSL_KCAPI_HASH)
2463
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
2464
#elif defined(WOLFSSL_RENESAS_RSIP) && \
2465
   !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
2466
    /* functions defined in wolfcrypt/src/port/renesas/renesas_fspsm_sha.c */
2467
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
2468
2469
#elif defined(MAX3266X_SHA)
2470
    /* Functions defined in wolfcrypt/src/port/maxim/max3266x.c */
2471
#elif defined(STM32_HASH_SHA512)
2472
#elif defined(PSOC6_HASH_SHA2)
2473
#else
2474
2475
static WC_INLINE int Sha512Final(wc_Sha512* sha512)
2476
2.25M
{
2477
2.25M
    int ret = 0;
2478
2.25M
    byte* local;
2479
2480
2.25M
    if (sha512 == NULL) {
2481
0
        return BAD_FUNC_ARG;
2482
0
    }
2483
2484
2.25M
    local = (byte*)sha512->buffer;
2485
2486
    /* we'll add a 0x80 byte at the end,
2487
    ** so make sure we have appropriate buffer length. */
2488
2.25M
    if (sha512->buffLen > WC_SHA512_BLOCK_SIZE - 1) {
2489
0
        return BAD_STATE_E;
2490
0
    } /* buffLen check */
2491
2492
2.25M
    local[sha512->buffLen++] = 0x80;  /* add 1 */
2493
2494
    /* pad with zeros */
2495
2.25M
    if (sha512->buffLen > WC_SHA512_PAD_SIZE) {
2496
854k
        if (sha512->buffLen < WC_SHA512_BLOCK_SIZE ) {
2497
854k
            XMEMSET(&local[sha512->buffLen], 0,
2498
854k
                WC_SHA512_BLOCK_SIZE - sha512->buffLen);
2499
854k
        }
2500
2501
854k
        sha512->buffLen += WC_SHA512_BLOCK_SIZE - sha512->buffLen;
2502
854k
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WC_SHA512_RAW_BE_BUFFER)
2503
    #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2504
        (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
2505
        if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
2506
    #endif
2507
854k
        {
2508
2509
854k
        #if (!defined(WOLFSSL_ESP32_CRYPT) || \
2510
854k
              defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
2511
854k
              defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)) && \
2512
854k
             !defined(WOLFSSL_ARMASM) && !defined(WOLFSSL_PPC64_ASM) && \
2513
854k
             !defined(WOLFSSL_RISCV_ASM)
2514
            #ifdef WOLFSSL_WIDE_BYTE
2515
            WordsFromBytesBE64(sha512->buffer, (const byte*)sha512->buffer,
2516
                WC_SHA512_BLOCK_SIZE / 8);
2517
            #else
2518
854k
            ByteReverseWords64(sha512->buffer,sha512->buffer,
2519
854k
                                                         WC_SHA512_BLOCK_SIZE);
2520
854k
            #endif
2521
854k
        #endif
2522
854k
        }
2523
2524
854k
#endif /* LITTLE_ENDIAN_ORDER */
2525
#if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_PPC64_ASM) || \
2526
    defined(WOLFSSL_PPC32_ASM) || defined(WOLFSSL_RISCV_ASM)
2527
        ret = Transform_Sha512(sha512, (const byte*)sha512->buffer);
2528
        if (ret != 0)
2529
            return ret;
2530
#else
2531
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2532
       !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2533
        if (sha512->ctx.mode == ESP32_SHA_INIT) {
2534
            esp_sha_try_hw_lock(&sha512->ctx);
2535
        }
2536
        if (sha512->ctx.mode == ESP32_SHA_SW) {
2537
            ByteReverseWords64(sha512->buffer,sha512->buffer,
2538
                                                         WC_SHA512_BLOCK_SIZE);
2539
            ret = Transform_Sha512(sha512);
2540
        }
2541
        else {
2542
            ret = esp_sha512_process(sha512);
2543
        }
2544
    #else
2545
854k
        ret = Transform_Sha512(sha512);
2546
854k
    #endif
2547
854k
        if (ret != 0) {
2548
81
            return ret;
2549
81
        }
2550
854k
#endif
2551
2552
854k
        sha512->buffLen = 0;
2553
854k
    } /* (sha512->buffLen > WC_SHA512_PAD_SIZE) pad with zeros */
2554
2555
2.25M
    XMEMSET(&local[sha512->buffLen], 0, WC_SHA512_PAD_SIZE - sha512->buffLen);
2556
2557
    /* put lengths in bits */
2558
2.25M
    sha512->hiLen = (sha512->loLen >>
2559
2.25M
                        (CHAR_BIT * sizeof(sha512->loLen) - 3)) +
2560
2.25M
                                                         (sha512->hiLen << 3);
2561
2.25M
    sha512->loLen = sha512->loLen << 3;
2562
2563
    /* store lengths */
2564
#ifdef WOLFSSL_WIDE_BYTE
2565
    /* CHAR_BIT != 8: 'local' indexes octet cells, not the word64 layout (the
2566
     * buffer[BLOCK/sizeof(word64) - 2/-1] indices assume 16 words per block,
2567
     * which is wrong when sizeof(word64) != 8).  Place the 128-bit bit-length
2568
     * as 16 big-endian octets at the pad offset, then load all 16 big-endian
2569
     * schedule words octet-wise. */
2570
    local[WC_SHA512_PAD_SIZE +  0] = (byte)((sha512->hiLen >> 56) & 0xFF);
2571
    local[WC_SHA512_PAD_SIZE +  1] = (byte)((sha512->hiLen >> 48) & 0xFF);
2572
    local[WC_SHA512_PAD_SIZE +  2] = (byte)((sha512->hiLen >> 40) & 0xFF);
2573
    local[WC_SHA512_PAD_SIZE +  3] = (byte)((sha512->hiLen >> 32) & 0xFF);
2574
    local[WC_SHA512_PAD_SIZE +  4] = (byte)((sha512->hiLen >> 24) & 0xFF);
2575
    local[WC_SHA512_PAD_SIZE +  5] = (byte)((sha512->hiLen >> 16) & 0xFF);
2576
    local[WC_SHA512_PAD_SIZE +  6] = (byte)((sha512->hiLen >>  8) & 0xFF);
2577
    local[WC_SHA512_PAD_SIZE +  7] = (byte)((sha512->hiLen      ) & 0xFF);
2578
    local[WC_SHA512_PAD_SIZE +  8] = (byte)((sha512->loLen >> 56) & 0xFF);
2579
    local[WC_SHA512_PAD_SIZE +  9] = (byte)((sha512->loLen >> 48) & 0xFF);
2580
    local[WC_SHA512_PAD_SIZE + 10] = (byte)((sha512->loLen >> 40) & 0xFF);
2581
    local[WC_SHA512_PAD_SIZE + 11] = (byte)((sha512->loLen >> 32) & 0xFF);
2582
    local[WC_SHA512_PAD_SIZE + 12] = (byte)((sha512->loLen >> 24) & 0xFF);
2583
    local[WC_SHA512_PAD_SIZE + 13] = (byte)((sha512->loLen >> 16) & 0xFF);
2584
    local[WC_SHA512_PAD_SIZE + 14] = (byte)((sha512->loLen >>  8) & 0xFF);
2585
    local[WC_SHA512_PAD_SIZE + 15] = (byte)((sha512->loLen      ) & 0xFF);
2586
    WordsFromBytesBE64(sha512->buffer, (const byte*)sha512->buffer,
2587
        WC_SHA512_BLOCK_SIZE / 8);
2588
#else
2589
2.25M
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WC_SHA512_RAW_BE_BUFFER)
2590
    #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2591
        (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
2592
        if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
2593
    #endif
2594
2.25M
    #if (!defined(WOLFSSL_ESP32_CRYPT) || \
2595
2.25M
          defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
2596
2.25M
          defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)) && \
2597
2.25M
         !defined(WOLFSSL_ARMASM) && !defined(WOLFSSL_PPC64_ASM) && \
2598
2.25M
         !defined(WOLFSSL_RISCV_ASM)
2599
2.25M
            ByteReverseWords64(sha512->buffer, sha512->buffer, WC_SHA512_PAD_SIZE);
2600
2.25M
    #endif
2601
2.25M
#endif
2602
    /* ! length ordering dependent on digest endian type ! */
2603
2604
2.25M
#if !defined(WOLFSSL_ESP32_CRYPT) || \
2605
2.25M
     defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
2606
2.25M
     defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2607
2.25M
    sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
2608
2.25M
    sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
2609
2.25M
#endif
2610
2.25M
#endif /* WOLFSSL_WIDE_BYTE */
2611
2612
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2613
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
2614
    #ifdef WC_SHA512_RAW_BE_BUFFER
2615
    /* raw-buffer convention -- the length words must be big-endian in the
2616
     * stream regardless of which transform consumes the final block. */
2617
    #else
2618
    if (IS_INTEL_AVX1(intel_flags) || IS_INTEL_AVX2(intel_flags))
2619
    #endif
2620
    {
2621
        ByteReverseWords64(&(sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
2622
                           &(sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
2623
                           WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE);
2624
    }
2625
#elif defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
2626
    #define SHA512_PAD_LEN_64  (WC_SHA512_PAD_SIZE / sizeof(word64))
2627
    {
2628
        ByteReverseWords64(&(sha512->buffer[SHA512_PAD_LEN_64]),
2629
                           &(sha512->buffer[SHA512_PAD_LEN_64]),
2630
                           WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE);
2631
    }
2632
#elif defined(WOLFSSL_PPC64_ASM) && defined(LITTLE_ENDIAN_ORDER)
2633
    /* The PPC64 assembly loads the message with byte-reversed loads on
2634
     * little-endian, treating the whole block as a big-endian byte stream.  The
2635
     * 128-bit length just stored is in native (little-endian) word order, so
2636
     * reverse it here to keep the block a consistent big-endian stream. */
2637
    {
2638
        ByteReverseWords64(
2639
            &(sha512->buffer[WC_SHA512_PAD_SIZE / sizeof(word64)]),
2640
            &(sha512->buffer[WC_SHA512_PAD_SIZE / sizeof(word64)]),
2641
            WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE);
2642
    }
2643
#endif
2644
2645
#if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_PPC64_ASM) || \
2646
    defined(WOLFSSL_PPC32_ASM) || defined(WOLFSSL_RISCV_ASM)
2647
    ret = Transform_Sha512(sha512, (const byte*)sha512->buffer);
2648
    if (ret != 0)
2649
        return ret;
2650
#else
2651
2.25M
#if !defined(WOLFSSL_ESP32_CRYPT) || \
2652
2.25M
      defined(NO_WOLFSSL_ESP32_CRYPT_HASH) || \
2653
2.25M
      defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2654
2.25M
    ret = Transform_Sha512(sha512);
2655
#else
2656
    if(sha512->ctx.mode == ESP32_SHA_INIT) {
2657
        /* typically for tiny block: first = last */
2658
        esp_sha_try_hw_lock(&sha512->ctx);
2659
    }
2660
    if (sha512->ctx.mode == ESP32_SHA_SW) {
2661
        ByteReverseWords64(sha512->buffer,
2662
                           sha512->buffer,
2663
                           WC_SHA512_BLOCK_SIZE);
2664
        sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
2665
        sha512->buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
2666
        ret = Transform_Sha512(sha512);
2667
    }
2668
    else {
2669
        ret = esp_sha512_digest_process(sha512, 1);
2670
    }
2671
#endif
2672
2673
2.25M
    if (ret != 0)
2674
616
        return ret;
2675
2.25M
#endif
2676
2677
    /* CHAR_BIT != 8: leave digest in host word order; the *Final functions
2678
     * emit big-endian octets via BytesFromWordsBE64 (no in-place reverse). */
2679
2.25M
    #if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_WIDE_BYTE)
2680
2.25M
        ByteReverseWords64(sha512->digest, sha512->digest,
2681
2.25M
            WC_SHA512_DIGEST_SIZE);
2682
2.25M
    #endif
2683
2684
2685
2.25M
    return 0;
2686
2.25M
}
2687
2688
#endif /* WOLFSSL_KCAPI_HASH */
2689
2690
#ifdef WOLFSSL_SHA512
2691
2692
#if defined(WOLFSSL_KCAPI_HASH)
2693
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
2694
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
2695
2696
#elif defined(WOLFSSL_RENESAS_RSIP) && \
2697
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
2698
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
2699
2700
#elif defined(MAX3266X_SHA)
2701
    /* Functions defined in wolfcrypt/src/port/maxim/max3266x.c */
2702
#elif defined(STM32_HASH_SHA512)
2703
#elif defined(PSOC6_HASH_SHA2)
2704
#elif defined(WOLFSSL_SILABS_SHA512)
2705
#else
2706
2707
static int Sha512FinalRaw(wc_Sha512* sha512, byte* hash, word32 digestSz)
2708
0
{
2709
0
    if (sha512 == NULL || hash == NULL) {
2710
0
        return BAD_FUNC_ARG;
2711
0
    }
2712
2713
#if defined(WOLFSSL_WIDE_BYTE)
2714
    BytesFromWordsBE64(hash, sha512->digest, digestSz);
2715
#elif defined(LITTLE_ENDIAN_ORDER)
2716
0
    if ((digestSz & 0x7) == 0)
2717
0
        ByteReverseWords64((word64 *)hash, sha512->digest, digestSz);
2718
0
    else {
2719
0
        ByteReverseWords64(sha512->digest, sha512->digest,
2720
0
                           WC_SHA512_DIGEST_SIZE);
2721
0
        XMEMCPY(hash, sha512->digest, digestSz);
2722
0
    }
2723
#else
2724
    XMEMCPY(hash, sha512->digest, digestSz);
2725
#endif
2726
2727
0
    return 0;
2728
0
}
2729
2730
int wc_Sha512FinalRaw(wc_Sha512* sha512, byte* hash)
2731
0
{
2732
0
    return Sha512FinalRaw(sha512, hash, WC_SHA512_DIGEST_SIZE);
2733
0
}
2734
2735
static int Sha512_Family_Final(wc_Sha512* sha512, byte* hash, size_t digestSz,
2736
                               int (*initfp)(wc_Sha512*))
2737
2.10M
{
2738
2.10M
    int ret;
2739
2740
2.10M
    if (sha512 == NULL || hash == NULL) {
2741
0
        return BAD_FUNC_ARG;
2742
0
    }
2743
2744
2.10M
#ifdef WOLF_CRYPTO_CB
2745
2.10M
    #ifndef WOLF_CRYPTO_CB_FIND
2746
2.10M
    if (sha512->devId != INVALID_DEVID)
2747
264
    #endif
2748
264
    {
2749
264
        ret = wc_CryptoCb_Sha512Hash(sha512, NULL, 0, hash, digestSz);
2750
264
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
2751
0
            return ret;
2752
0
        }
2753
        /* fall-through when unavailable */
2754
264
    }
2755
2.10M
#endif
2756
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
2757
    if (sha512->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA512) {
2758
    #if defined(HAVE_INTEL_QA)
2759
        return IntelQaSymSha512(&sha512->asyncDev, hash, NULL, digestSz);
2760
    #endif
2761
    }
2762
#endif /* WOLFSSL_ASYNC_CRYPT */
2763
2764
2.10M
    ret = Sha512Final(sha512);
2765
2.10M
    if (ret != 0)
2766
697
        return ret;
2767
2768
#ifdef WOLFSSL_WIDE_BYTE
2769
    BytesFromWordsBE64(hash, sha512->digest, (word32)digestSz);
2770
#else
2771
2.10M
    XMEMCPY(hash, sha512->digest, digestSz);
2772
2.10M
#endif
2773
2774
    /* initialize Sha512 structure for the next use */
2775
2.10M
    return initfp(sha512);
2776
2.10M
}
2777
2778
#ifndef STM32_HASH_SHA512
2779
int wc_Sha512Final(wc_Sha512* sha512, byte* hash)
2780
2.23M
{
2781
2.23M
    return Sha512_Family_Final(sha512, hash, WC_SHA512_DIGEST_SIZE, InitSha512);
2782
2.23M
}
2783
#endif
2784
2785
#endif /* WOLFSSL_KCAPI_HASH */
2786
2787
#if defined(MAX3266X_SHA)
2788
    /* Functions defined in wolfcrypt/src/port/maxim/max3266x.c */
2789
2790
#else
2791
#if !defined(WOLFSSL_SE050) || !defined(WOLFSSL_SE050_HASH)
2792
int wc_InitSha512(wc_Sha512* sha512)
2793
136k
{
2794
136k
    int devId = INVALID_DEVID;
2795
2796
136k
#ifdef WOLF_CRYPTO_CB
2797
136k
    devId = wc_CryptoCb_DefaultDevID();
2798
136k
#endif
2799
136k
    return wc_InitSha512_ex(sha512, NULL, devId);
2800
136k
}
2801
2802
void wc_Sha512Free(wc_Sha512* sha512)
2803
2.28M
{
2804
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
2805
    int ret = 0;
2806
#endif
2807
2808
2.28M
    if (sha512 == NULL)
2809
0
        return;
2810
2811
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
2812
    #ifndef WOLF_CRYPTO_CB_FIND
2813
    if (sha512->devId != INVALID_DEVID)
2814
    #endif
2815
    {
2816
        ret = wc_CryptoCb_Free(sha512->devId, WC_ALGO_TYPE_HASH,
2817
                         WC_HASH_TYPE_SHA512, 0, (void*)sha512);
2818
        /* If they want the standard free, they can call it themselves */
2819
        /* via their callback setting devId to INVALID_DEVID */
2820
        /* otherwise assume the callback handled it */
2821
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2822
            return;
2823
        /* fall-through when unavailable */
2824
    }
2825
2826
    /* silence compiler warning */
2827
    (void)ret;
2828
2829
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */
2830
2831
#if defined(WOLFSSL_ESP32) && \
2832
    !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)  && \
2833
    !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
2834
    esp_sha_release_unfinished_lock(&sha512->ctx);
2835
#endif
2836
2837
#ifdef WOLFSSL_SMALL_STACK_CACHE
2838
    if (sha512->W != NULL) {
2839
        ForceZero(sha512->W, sizeof(Sha512TScratch) + WC_SHA512_BLOCK_SIZE);
2840
        XFREE(sha512->W, sha512->heap, DYNAMIC_TYPE_DIGEST);
2841
        sha512->W = NULL;
2842
    }
2843
#endif
2844
2845
#if defined(WOLFSSL_KCAPI_HASH)
2846
    KcapiHashFree(&sha512->kcapi);
2847
#endif
2848
2849
#if defined(WOLFSSL_HASH_KEEP) ||\
2850
   (defined(WOLFSSL_RENESAS_RSIP) && (WOLFSSL_RENESAS_RZFSP_VER >= 220))
2851
    if (sha512->msg != NULL) {
2852
        ForceZero(sha512->msg, sha512->len);
2853
        XFREE(sha512->msg, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
2854
        sha512->msg = NULL;
2855
    }
2856
#endif
2857
2858
2859
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
2860
    wolfAsync_DevCtxFree(&sha512->asyncDev, WOLFSSL_ASYNC_MARKER_SHA512);
2861
#endif /* WOLFSSL_ASYNC_CRYPT */
2862
2863
#if defined(PSOC6_HASH_SHA2)
2864
    wc_Psoc6_Sha_Free();
2865
#endif
2866
2867
2.28M
    ForceZero(sha512, sizeof(*sha512));
2868
2.28M
}
2869
#endif
2870
2871
#if (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) \
2872
    && !defined(WOLFSSL_KCAPI_HASH)
2873
/* Apply SHA512 transformation to the data                */
2874
/* @param sha  a pointer to wc_Sha512 structure           */
2875
/* @param data data to be applied SHA512 transformation   */
2876
/* @return 0 on successful, otherwise non-zero on failure */
2877
int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data)
2878
{
2879
    int ret;
2880
    /* back up buffer */
2881
    WC_DECLARE_VAR(buffer, word64, WC_SHA512_BLOCK_SIZE  / sizeof(word64),
2882
        0);
2883
2884
    /* sanity check */
2885
    if (sha == NULL || data == NULL) {
2886
        return BAD_FUNC_ARG;
2887
    }
2888
2889
2890
#if defined(WOLFSSL_SMALL_STACK_CACHE)
2891
    if (sha->W == NULL)
2892
        return BAD_FUNC_ARG;
2893
    /* Skip over the initial `W' buffer at the start (used by
2894
     * _Transform_Sha512()).
2895
     */
2896
    buffer = (word64*)((byte*)sha->W + sizeof(Sha512TScratch));
2897
#elif defined(WOLFSSL_SMALL_STACK)
2898
    buffer = (word64*)XMALLOC(WC_SHA512_BLOCK_SIZE, sha->heap,
2899
        DYNAMIC_TYPE_TMP_BUFFER);
2900
    if (buffer == NULL)
2901
        return MEMORY_E;
2902
#endif
2903
#if defined(WOLFSSL_CHECK_MEM_ZERO) && !defined(WOLFSSL_SMALL_STACK_CACHE)
2904
    /* Zero first: the registration call takes const void*, and a pointer to
2905
     * an uninitialized object trips -Wmaybe-uninitialized under -Werror. */
2906
    XMEMSET(buffer, 0, WC_SHA512_BLOCK_SIZE);
2907
    wc_MemZero_Add("wc_Sha512Transform buffer", buffer, WC_SHA512_BLOCK_SIZE);
2908
#endif
2909
2910
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WC_SHA512_RAW_BE_BUFFER)
2911
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2912
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
2913
    if (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags))
2914
#endif
2915
    {
2916
        ByteReverseWords64((word64*)data, (word64*)data,
2917
                                                WC_SHA512_BLOCK_SIZE);
2918
    }
2919
#endif /* LITTLE_ENDIAN_ORDER && !WC_SHA512_RAW_BE_BUFFER */
2920
2921
#if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
2922
    ByteReverseWords64(buffer, (word64*)data, WC_SHA512_BLOCK_SIZE);
2923
    ret = Transform_Sha512(sha, (const byte*)buffer);
2924
#elif defined(WOLFSSL_PPC64_ASM) || defined(WOLFSSL_PPC32_ASM)
2925
    /* PPC assembly uses the (sha, data) form and reads the block directly
2926
     * (big-endian native - any little-endian reversal was done above). */
2927
    (void)buffer;
2928
    ret = Transform_Sha512(sha, data);
2929
#else
2930
    XMEMCPY(buffer, sha->buffer, WC_SHA512_BLOCK_SIZE);
2931
    XMEMCPY(sha->buffer, data, WC_SHA512_BLOCK_SIZE);
2932
2933
    ret = Transform_Sha512(sha);
2934
2935
    XMEMCPY(sha->buffer, buffer, WC_SHA512_BLOCK_SIZE);
2936
#endif
2937
    ForceZero(buffer, WC_SHA512_BLOCK_SIZE);
2938
#if defined(WOLFSSL_CHECK_MEM_ZERO) && !defined(WOLFSSL_SMALL_STACK)
2939
    wc_MemZero_Check(buffer, WC_SHA512_BLOCK_SIZE);
2940
#endif
2941
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE)
2942
    XFREE(buffer, sha->heap, DYNAMIC_TYPE_TMP_BUFFER);
2943
#endif
2944
    return ret;
2945
}
2946
#endif /* OPENSSL_EXTRA */
2947
#endif /* WOLFSSL_SHA512 */
2948
#endif /* !WOLFSSL_SE050 || !WOLFSSL_SE050_HASH */
2949
2950
2951
/* -------------------------------------------------------------------------- */
2952
/* SHA384 */
2953
/* -------------------------------------------------------------------------- */
2954
#ifdef WOLFSSL_SHA384
2955
2956
#if defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
2957
    !defined(WOLFSSL_QNX_CAAM)
2958
    /* functions defined in wolfcrypt/src/port/caam/caam_sha.c */
2959
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
2960
    int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
2961
    {
2962
        if (sha384 == NULL) {
2963
            return BAD_FUNC_ARG;
2964
        }
2965
        (void)devId;
2966
        return se050_hash_init(&sha384->se050Ctx, heap);
2967
    }
2968
    int wc_Sha384Update(wc_Sha384* sha384, const byte* data, word32 len)
2969
    {
2970
        if (sha384 == NULL) {
2971
            return BAD_FUNC_ARG;
2972
        }
2973
        if (data == NULL && len == 0) {
2974
            /* valid, but do nothing */
2975
            return 0;
2976
        }
2977
        if (data == NULL) {
2978
            return BAD_FUNC_ARG;
2979
        }
2980
2981
        return se050_hash_update(&sha384->se050Ctx, data, len);
2982
2983
    }
2984
    int wc_Sha384Final(wc_Sha384* sha384, byte* hash)
2985
    {
2986
        int ret = 0;
2987
        ret = se050_hash_final(&sha384->se050Ctx, hash, WC_SHA384_DIGEST_SIZE,
2988
                               kAlgorithm_SSS_SHA384);
2989
        return ret;
2990
    }
2991
2992
#elif defined(WOLFSSL_SILABS_SHA384)
2993
    /* functions defined in wolfcrypt/src/port/silabs/silabs_hash.c */
2994
2995
#elif defined(WOLFSSL_KCAPI_HASH)
2996
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
2997
2998
#elif defined(WOLFSSL_RENESAS_RSIP) && \
2999
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
3000
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
3001
3002
#elif defined(MAX3266X_SHA)
3003
    /* Functions defined in wolfcrypt/src/port/maxim/max3266x.c */
3004
#elif defined(STM32_HASH_SHA384)
3005
3006
    int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
3007
    {
3008
        if (sha384 == NULL)
3009
            return BAD_FUNC_ARG;
3010
3011
        (void)devId;
3012
        (void)heap;
3013
3014
        XMEMSET(sha384, 0, sizeof(wc_Sha384));
3015
        wc_Stm32_Hash_Init(&sha384->stmCtx);
3016
        return 0;
3017
    }
3018
3019
    int wc_Sha384Update(wc_Sha384* sha384, const byte* data, word32 len)
3020
    {
3021
        int ret = 0;
3022
3023
        if (sha384 == NULL) {
3024
            return BAD_FUNC_ARG;
3025
        }
3026
        if (data == NULL && len == 0) {
3027
            /* valid, but do nothing */
3028
            return 0;
3029
        }
3030
        if (data == NULL) {
3031
            return BAD_FUNC_ARG;
3032
        }
3033
3034
        ret = wolfSSL_CryptHwMutexLock();
3035
        if (ret == 0) {
3036
            ret = wc_Stm32_Hash_Update(&sha384->stmCtx,
3037
                HASH_ALGOSELECTION_SHA384, data, len, WC_SHA384_BLOCK_SIZE);
3038
            wolfSSL_CryptHwMutexUnLock();
3039
        }
3040
        return ret;
3041
    }
3042
3043
    int wc_Sha384Final(wc_Sha384* sha384, byte* hash)
3044
    {
3045
        int ret = 0;
3046
3047
        if (sha384 == NULL || hash == NULL) {
3048
            return BAD_FUNC_ARG;
3049
        }
3050
3051
        ret = wolfSSL_CryptHwMutexLock();
3052
        if (ret == 0) {
3053
            ret = wc_Stm32_Hash_Final(&sha384->stmCtx,
3054
                HASH_ALGOSELECTION_SHA384, hash, WC_SHA384_DIGEST_SIZE);
3055
            wolfSSL_CryptHwMutexUnLock();
3056
        }
3057
3058
        (void)wc_InitSha384(sha384); /* reset state */
3059
3060
        return ret;
3061
    }
3062
3063
#elif defined(PSOC6_HASH_SHA2)
3064
    /* implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
3065
3066
#else
3067
3068
static int InitSha384(wc_Sha384* sha384)
3069
86.7k
{
3070
86.7k
    if (sha384 == NULL) {
3071
0
        return BAD_FUNC_ARG;
3072
0
    }
3073
3074
#ifdef WOLFSSL_SMALL_STACK_CACHE
3075
    if (sha384->W == NULL) {
3076
        /* This allocation combines the customary W buffer used by
3077
         * _Transform_Sha512() with additional buffer space used by
3078
         * wc_Sha512Transform().
3079
         */
3080
        sha384->W = (word64 *)XMALLOC(sizeof(Sha512TScratch) + WC_SHA512_BLOCK_SIZE,
3081
                                      sha384->heap, DYNAMIC_TYPE_DIGEST);
3082
        if (sha384->W == NULL)
3083
            return MEMORY_E;
3084
    }
3085
#endif
3086
3087
86.7k
    sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8);
3088
86.7k
    sha384->digest[1] = W64LIT(0x629a292a367cd507);
3089
86.7k
    sha384->digest[2] = W64LIT(0x9159015a3070dd17);
3090
86.7k
    sha384->digest[3] = W64LIT(0x152fecd8f70e5939);
3091
86.7k
    sha384->digest[4] = W64LIT(0x67332667ffc00b31);
3092
86.7k
    sha384->digest[5] = W64LIT(0x8eb44a8768581511);
3093
86.7k
    sha384->digest[6] = W64LIT(0xdb0c2e0d64f98fa7);
3094
86.7k
    sha384->digest[7] = W64LIT(0x47b5481dbefa4fa4);
3095
3096
86.7k
    sha384->buffLen = 0;
3097
86.7k
    XMEMSET(sha384->buffer, 0, sizeof(sha384->buffer));
3098
86.7k
    sha384->loLen   = 0;
3099
86.7k
    sha384->hiLen   = 0;
3100
3101
#if (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
3102
     (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))) || \
3103
    defined(WOLFSSL_ARMASM)
3104
    Sha512_SetTransform();
3105
#endif
3106
3107
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)  && \
3108
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
3109
    /* HW needs to be carefully initialized, taking into account soft copy.
3110
    ** If already in use; copy may revert to SW as needed. */
3111
    esp_sha_init(&(sha384->ctx), WC_HASH_TYPE_SHA384);
3112
#endif
3113
3114
86.7k
#ifdef WOLFSSL_HASH_FLAGS
3115
86.7k
    sha384->flags = 0;
3116
86.7k
#endif
3117
3118
#ifdef HAVE_ARIA
3119
    sha384->hSession = NULL;
3120
#endif
3121
3122
#ifdef WOLFSSL_HASH_KEEP
3123
    sha384->msg  = NULL;
3124
    sha384->len  = 0;
3125
    sha384->used = 0;
3126
#endif
3127
3128
86.7k
    return 0;
3129
86.7k
}
3130
3131
#if !defined(WOLFSSL_HASH_KEEP)
3132
0
int wc_Sha384Reset(wc_Sha384* sha384) {
3133
0
    if (sha384 == NULL)
3134
0
        return BAD_FUNC_ARG;
3135
0
#ifdef WOLF_CRYPTO_CB
3136
    /* A device may hang state off devCtx that InitSha384() cannot restart.
3137
     * Free and re-init so the callback gets its teardown and setup. */
3138
0
    #ifndef WOLF_CRYPTO_CB_FIND
3139
0
    if (sha384->devId != INVALID_DEVID)
3140
0
    #endif
3141
0
    {
3142
0
        void* heap = sha384->heap;
3143
0
        int devId = sha384->devId;
3144
0
        wc_Sha384Free(sha384);
3145
0
        return wc_InitSha384_ex(sha384, heap, devId);
3146
0
    }
3147
0
#endif
3148
0
    return InitSha384(sha384);
3149
0
}
3150
#define WC_SHA384RESET_DEFINED
3151
#endif /* !WOLFSSL_HASH_KEEP */
3152
3153
int wc_Sha384Update(wc_Sha384* sha384, const byte* data, word32 len)
3154
205k
{
3155
3156
205k
    if (sha384 == NULL) {
3157
0
        return BAD_FUNC_ARG;
3158
0
    }
3159
205k
    if (data == NULL && len == 0) {
3160
        /* valid, but do nothing */
3161
98
        return 0;
3162
98
    }
3163
205k
    if (data == NULL) {
3164
0
        return BAD_FUNC_ARG;
3165
0
    }
3166
3167
205k
#ifdef WOLF_CRYPTO_CB
3168
205k
    #ifndef WOLF_CRYPTO_CB_FIND
3169
205k
    if (sha384->devId != INVALID_DEVID)
3170
139
    #endif
3171
139
    {
3172
139
        int ret = wc_CryptoCb_Sha384Hash(sha384, data, len, NULL);
3173
139
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3174
0
            return ret;
3175
        /* fall-through when unavailable */
3176
139
    }
3177
205k
#endif
3178
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
3179
    if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
3180
    #if defined(HAVE_INTEL_QA)
3181
        return IntelQaSymSha384(&sha384->asyncDev, NULL, data, len);
3182
    #endif
3183
    }
3184
#endif /* WOLFSSL_ASYNC_CRYPT */
3185
3186
205k
    return Sha512Update((wc_Sha512*)sha384, data, len);
3187
205k
}
3188
3189
3190
int wc_Sha384FinalRaw(wc_Sha384* sha384, byte* hash)
3191
105
{
3192
105
    if (sha384 == NULL || hash == NULL) {
3193
0
        return BAD_FUNC_ARG;
3194
0
    }
3195
3196
#if defined(WOLFSSL_WIDE_BYTE)
3197
    BytesFromWordsBE64(hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
3198
#elif defined(LITTLE_ENDIAN_ORDER)
3199
105
    ByteReverseWords64((word64 *)hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
3200
#else
3201
    XMEMCPY(hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
3202
#endif
3203
3204
105
    return 0;
3205
105
}
3206
3207
int wc_Sha384Final(wc_Sha384* sha384, byte* hash)
3208
15.1k
{
3209
15.1k
    int ret;
3210
3211
15.1k
    if (sha384 == NULL || hash == NULL) {
3212
0
        return BAD_FUNC_ARG;
3213
0
    }
3214
3215
15.1k
#ifdef WOLF_CRYPTO_CB
3216
15.1k
    #ifndef WOLF_CRYPTO_CB_FIND
3217
15.1k
    if (sha384->devId != INVALID_DEVID)
3218
52
    #endif
3219
52
    {
3220
52
        ret = wc_CryptoCb_Sha384Hash(sha384, NULL, 0, hash);
3221
52
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3222
0
            return ret;
3223
        /* fall-through when unavailable */
3224
52
    }
3225
15.1k
#endif
3226
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
3227
    if (sha384->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA384) {
3228
    #if defined(HAVE_INTEL_QA)
3229
        return IntelQaSymSha384(&sha384->asyncDev, hash, NULL,
3230
                                            WC_SHA384_DIGEST_SIZE);
3231
    #endif
3232
    }
3233
#endif /* WOLFSSL_ASYNC_CRYPT */
3234
3235
15.1k
    ret = Sha512Final((wc_Sha512*)sha384);
3236
15.1k
    if (ret != 0)
3237
0
        return ret;
3238
3239
#ifdef WOLFSSL_WIDE_BYTE
3240
    BytesFromWordsBE64(hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
3241
#else
3242
15.1k
    XMEMCPY(hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
3243
15.1k
#endif
3244
3245
15.1k
    return InitSha384(sha384);  /* reset state */
3246
15.1k
}
3247
3248
int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
3249
69.0k
{
3250
69.0k
    int ret;
3251
3252
69.0k
    if (sha384 == NULL) {
3253
0
        return BAD_FUNC_ARG;
3254
0
    }
3255
3256
69.0k
    sha384->heap = heap;
3257
#ifdef WOLFSSL_SMALL_STACK_CACHE
3258
    sha384->W = NULL;
3259
#endif
3260
69.0k
#ifdef WOLF_CRYPTO_CB
3261
69.0k
    sha384->devId = devId;
3262
69.0k
    sha384->devCtx = NULL;
3263
69.0k
#endif
3264
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)  && \
3265
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
3266
    if (sha384->ctx.mode != ESP32_SHA_INIT) {
3267
        ESP_LOGV(TAG, "Set ctx mode from prior value: "
3268
                           "%d", sha384->ctx.mode);
3269
    }
3270
    /* We know this is a fresh, uninitialized item, so set to INIT */
3271
    sha384->ctx.mode = ESP32_SHA_INIT;
3272
#endif
3273
3274
3275
69.0k
    ret = InitSha384(sha384);
3276
69.0k
    if (ret != 0) {
3277
0
        return ret;
3278
0
    }
3279
3280
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
3281
    ret = wolfAsync_DevCtxInit(&sha384->asyncDev, WOLFSSL_ASYNC_MARKER_SHA384,
3282
                                                           sha384->heap, devId);
3283
#else
3284
69.0k
    (void)devId;
3285
69.0k
#endif /* WOLFSSL_ASYNC_CRYPT */
3286
#ifdef WOLFSSL_IMXRT1170_CAAM
3287
     ret = wc_CAAM_HashInit(&sha384->hndl, &sha384->ctx, WC_HASH_TYPE_SHA384);
3288
#endif
3289
69.0k
    return ret;
3290
69.0k
}
3291
3292
#endif /* WOLFSSL_IMX6_CAAM || WOLFSSL_SILABS_SHA384 || WOLFSSL_KCAPI_HASH */
3293
3294
#if defined(MAX3266X_SHA)
3295
    /* Functions defined in wolfcrypt/src/port/maxim/max3266x.c */
3296
3297
#else
3298
int wc_InitSha384(wc_Sha384* sha384)
3299
76
{
3300
76
    int devId = INVALID_DEVID;
3301
3302
76
#ifdef WOLF_CRYPTO_CB
3303
76
    devId = wc_CryptoCb_DefaultDevID();
3304
76
#endif
3305
76
    return wc_InitSha384_ex(sha384, NULL, devId);
3306
76
}
3307
3308
void wc_Sha384Free(wc_Sha384* sha384)
3309
70.3k
{
3310
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
3311
    int ret = 0;
3312
#endif
3313
3314
70.3k
    if (sha384 == NULL)
3315
0
        return;
3316
3317
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
3318
    #ifndef WOLF_CRYPTO_CB_FIND
3319
    if (sha384->devId != INVALID_DEVID)
3320
    #endif
3321
    {
3322
        ret = wc_CryptoCb_Free(sha384->devId, WC_ALGO_TYPE_HASH,
3323
                         WC_HASH_TYPE_SHA384, 0, (void*)sha384);
3324
        /* If they want the standard free, they can call it themselves */
3325
        /* via their callback setting devId to INVALID_DEVID */
3326
        /* otherwise assume the callback handled it */
3327
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3328
            return;
3329
        /* fall-through when unavailable */
3330
    }
3331
3332
    /* silence compiler warning */
3333
    (void)ret;
3334
3335
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */
3336
3337
#if defined(WOLFSSL_ESP32) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)  && \
3338
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
3339
    esp_sha_release_unfinished_lock(&sha384->ctx);
3340
#endif
3341
3342
#ifdef WOLFSSL_SMALL_STACK_CACHE
3343
    if (sha384->W != NULL) {
3344
        ForceZero(sha384->W, sizeof(Sha512TScratch) + WC_SHA512_BLOCK_SIZE);
3345
        XFREE(sha384->W, sha384->heap, DYNAMIC_TYPE_DIGEST);
3346
        sha384->W = NULL;
3347
    }
3348
#endif
3349
3350
#if defined(WOLFSSL_KCAPI_HASH)
3351
    KcapiHashFree(&sha384->kcapi);
3352
#endif
3353
3354
#if defined(WOLFSSL_HASH_KEEP) || \
3355
   (defined(WOLFSSL_RENESAS_RSIP) && (WOLFSSL_RENESAS_RZFSP_VER >= 220))
3356
    if (sha384->msg != NULL) {
3357
        ForceZero(sha384->msg, sha384->len);
3358
        XFREE(sha384->msg, sha384->heap, DYNAMIC_TYPE_TMP_BUFFER);
3359
        sha384->msg = NULL;
3360
    }
3361
#endif
3362
3363
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
3364
    se050_hash_free(&sha384->se050Ctx);
3365
#endif
3366
3367
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
3368
    wolfAsync_DevCtxFree(&sha384->asyncDev, WOLFSSL_ASYNC_MARKER_SHA384);
3369
#endif /* WOLFSSL_ASYNC_CRYPT */
3370
3371
#ifdef HAVE_ARIA
3372
    if (sha384->hSession != NULL) {
3373
        MC_CloseSession(sha384->hSession);
3374
        sha384->hSession = NULL;
3375
    }
3376
#endif
3377
3378
3379
70.3k
    ForceZero(sha384, sizeof(*sha384));
3380
70.3k
}
3381
3382
#endif
3383
#endif /* WOLFSSL_SHA384 */
3384
3385
#ifdef WOLFSSL_SHA512
3386
3387
#if defined(WOLFSSL_KCAPI_HASH)
3388
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
3389
3390
#elif defined(WOLFSSL_RENESAS_RSIP) && \
3391
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
3392
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
3393
3394
#elif defined(MAX3266X_SHA)
3395
    /* Functions defined in wolfcrypt/src/port/maxim/max3266x.c */
3396
3397
#else
3398
3399
static int Sha512_Family_GetHash(wc_Sha512* sha512, byte* hash,
3400
                                 int (*finalfp)(wc_Sha512*, byte*))
3401
177
{
3402
177
    int ret;
3403
177
    WC_DECLARE_VAR(tmpSha512, wc_Sha512, 1, 0);
3404
3405
177
    if (sha512 == NULL || hash == NULL) {
3406
0
        return BAD_FUNC_ARG;
3407
0
    }
3408
3409
177
    WC_CALLOC_VAR_EX(tmpSha512, wc_Sha512, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
3410
177
        return MEMORY_E);
3411
#ifdef WOLFSSL_CHECK_MEM_ZERO
3412
    wc_MemZero_Add("Sha512_Family_GetHash tmpSha512", tmpSha512,
3413
        sizeof(*tmpSha512));
3414
#endif
3415
3416
    /* copy this sha512 into tmpSha */
3417
177
    ret = wc_Sha512Copy(sha512, tmpSha512);
3418
177
    if (ret == 0) {
3419
177
        ret = finalfp(tmpSha512, hash);
3420
177
        wc_Sha512Free(tmpSha512);
3421
177
    }
3422
3423
177
    ForceZero(tmpSha512, sizeof(*tmpSha512));
3424
#if defined(WOLFSSL_CHECK_MEM_ZERO) && !defined(WOLFSSL_SMALL_STACK)
3425
    wc_MemZero_Check(tmpSha512, sizeof(*tmpSha512));
3426
#endif
3427
3428
177
    WC_FREE_VAR_EX(tmpSha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3429
3430
177
    return ret;
3431
177
}
3432
3433
int wc_Sha512GetHash(wc_Sha512* sha512, byte* hash)
3434
177
{
3435
177
    return Sha512_Family_GetHash(sha512, hash, wc_Sha512Final);
3436
177
}
3437
3438
int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
3439
237
{
3440
237
    int ret = 0;
3441
3442
237
    if (src == NULL || dst == NULL) {
3443
0
        return BAD_FUNC_ARG;
3444
0
    }
3445
3446
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_COPY)
3447
    #ifndef WOLF_CRYPTO_CB_FIND
3448
    if (src->devId != INVALID_DEVID)
3449
    #endif
3450
    {
3451
        /* Cast the source and destination to be void to keep the abstraction */
3452
        ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH,
3453
                               WC_HASH_TYPE_SHA512, (void*)src, (void*)dst);
3454
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3455
            return ret;
3456
        /* fall-through when unavailable */
3457
    }
3458
    ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
3459
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
3460
3461
    /* Free dst resources before copy to prevent memory leaks (e.g., msg
3462
     * buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
3463
237
    wc_Sha512Free(dst);
3464
237
    XMEMCPY(dst, src, sizeof(wc_Sha512));
3465
#ifdef WOLFSSL_SMALL_STACK_CACHE
3466
    /* This allocation combines the customary W buffer used by
3467
     * _Transform_Sha512() with additional buffer space used by
3468
     * wc_Sha512Transform().
3469
     */
3470
    dst->W = (word64 *)XMALLOC(sizeof(Sha512TScratch) + WC_SHA512_BLOCK_SIZE,
3471
                               dst->heap, DYNAMIC_TYPE_DIGEST);
3472
    if (dst->W == NULL) {
3473
        ForceZero(dst, sizeof(wc_Sha512));
3474
        return MEMORY_E;
3475
    }
3476
#endif
3477
3478
#if defined(WOLFSSL_SILABS_SE_TYPES) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \
3479
    defined(WOLFSSL_SILABS_SHA512)
3480
    dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
3481
    dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
3482
#endif
3483
3484
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
3485
    ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
3486
#endif
3487
3488
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
3489
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA512)
3490
    #if defined(CONFIG_IDF_TARGET_ESP32)
3491
    if (ret == 0) {
3492
        ret = esp_sha512_ctx_copy(src, dst);
3493
    }
3494
    #elif defined(CONFIG_IDF_TARGET_ESP32C2) || \
3495
          defined(CONFIG_IDF_TARGET_ESP8684) || \
3496
          defined(CONFIG_IDF_TARGET_ESP32C3) || \
3497
          defined(CONFIG_IDF_TARGET_ESP32C6)
3498
        ESP_LOGV(TAG, "No SHA-512 HW on the ESP32-C3");
3499
3500
    #elif defined(CONFIG_IDF_TARGET_ESP32S2) || \
3501
          defined(CONFIG_IDF_TARGET_ESP32S3)
3502
        if (ret == 0) {
3503
            ret = esp_sha512_ctx_copy(src, dst);
3504
        }
3505
    #else
3506
        ESP_LOGW(TAG, "No SHA384 HW or not yet implemented for %s",
3507
                       CONFIG_IDF_TARGET);
3508
    #endif
3509
3510
#endif /* WOLFSSL_USE_ESP32_CRYPT_HASH_HW */
3511
3512
#if defined(WOLFSSL_TI_AM64X_R5) && !defined(WOLFSSL_TI_AM64X_NO_SHA)
3513
    dst->devCtx = NULL;
3514
#endif
3515
3516
237
#ifdef WOLFSSL_HASH_FLAGS
3517
237
     dst->flags |= WC_HASH_FLAG_ISCOPY;
3518
237
#endif
3519
3520
#if defined(WOLFSSL_HASH_KEEP)
3521
    if (src->msg != NULL) {
3522
        dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
3523
        if (dst->msg == NULL)
3524
            return MEMORY_E;
3525
        XMEMCPY(dst->msg, src->msg, src->len);
3526
    }
3527
#endif
3528
3529
3530
#if defined(PSOC6_HASH_SHA2)
3531
    wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA512, 0);
3532
#endif
3533
3534
237
    return ret;
3535
237
}
3536
3537
#endif /* WOLFSSL_KCAPI_HASH */
3538
3539
#ifdef WOLFSSL_HASH_FLAGS
3540
int wc_Sha512SetFlags(wc_Sha512* sha512, word32 flags)
3541
59.5k
{
3542
59.5k
    if (sha512) {
3543
59.5k
        sha512->flags = flags;
3544
59.5k
    }
3545
59.5k
    return 0;
3546
59.5k
}
3547
int wc_Sha512GetFlags(wc_Sha512* sha512, word32* flags)
3548
0
{
3549
0
    if (sha512 && flags) {
3550
0
        *flags = sha512->flags;
3551
0
    }
3552
0
    return 0;
3553
0
}
3554
#endif /* WOLFSSL_HASH_FLAGS */
3555
3556
#if !defined(WOLFSSL_NOSHA512_224) && \
3557
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
3558
3559
#if defined(STM32_HASH_SHA512_224)
3560
3561
int wc_InitSha512_224_ex(wc_Sha512* sha512, void* heap, int devId)
3562
{
3563
    if (sha512 == NULL)
3564
        return BAD_FUNC_ARG;
3565
3566
    (void)devId;
3567
    (void)heap;
3568
3569
    XMEMSET(sha512, 0, sizeof(wc_Sha512));
3570
    wc_Stm32_Hash_Init(&sha512->stmCtx);
3571
#if defined(WOLFSSL_SHA512_HASHTYPE)
3572
    sha512->hashType = WC_HASH_TYPE_SHA512_224;
3573
#endif
3574
    return 0;
3575
}
3576
3577
int wc_Sha512_224Update(wc_Sha512* sha512, const byte* data, word32 len)
3578
{
3579
    int ret = 0;
3580
3581
    if (sha512 == NULL) {
3582
        return BAD_FUNC_ARG;
3583
    }
3584
    if (data == NULL && len == 0) {
3585
        /* valid, but do nothing */
3586
        return 0;
3587
    }
3588
    if (data == NULL) {
3589
        return BAD_FUNC_ARG;
3590
    }
3591
3592
    ret = wolfSSL_CryptHwMutexLock();
3593
    if (ret == 0) {
3594
        ret = wc_Stm32_Hash_Update(&sha512->stmCtx,
3595
            HASH_ALGOSELECTION_SHA512_224, data, len, WC_SHA512_224_BLOCK_SIZE);
3596
        wolfSSL_CryptHwMutexUnLock();
3597
    }
3598
    return ret;
3599
}
3600
3601
int wc_Sha512_224Final(wc_Sha512* sha512, byte* hash)
3602
{
3603
    int ret = 0;
3604
3605
    if (sha512 == NULL || hash == NULL) {
3606
        return BAD_FUNC_ARG;
3607
    }
3608
3609
    ret = wolfSSL_CryptHwMutexLock();
3610
    if (ret == 0) {
3611
        ret = wc_Stm32_Hash_Final(&sha512->stmCtx,
3612
            HASH_ALGOSELECTION_SHA512_224, hash, WC_SHA512_224_DIGEST_SIZE);
3613
        wolfSSL_CryptHwMutexUnLock();
3614
    }
3615
3616
    (void)wc_InitSha512_224(sha512); /* reset state */
3617
3618
    return ret;
3619
}
3620
#elif defined(PSOC6_HASH_SHA2)
3621
    /* functions defined in wolfcrypt/src/port/cypress/psoc6_crypto.c */
3622
#endif
3623
int wc_InitSha512_224(wc_Sha512* sha)
3624
0
{
3625
0
    return wc_InitSha512_224_ex(sha, NULL, INVALID_DEVID);
3626
0
}
3627
#if !defined(STM32_HASH_SHA512_224) && !defined(PSOC6_HASH_SHA2)
3628
int wc_Sha512_224Update(wc_Sha512* sha, const byte* data, word32 len)
3629
585
{
3630
585
    return wc_Sha512Update(sha, data, len);
3631
585
}
3632
#endif
3633
#if defined(WOLFSSL_KCAPI_HASH)
3634
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
3635
#elif defined(WOLFSSL_RENESAS_RSIP) && \
3636
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
3637
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
3638
3639
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
3640
#elif defined(STM32_HASH_SHA512_224)
3641
#elif defined(PSOC6_HASH_SHA2)
3642
    /* functions defined in wolfcrypt/src/port/cypress/psoc6_crypto.c */
3643
3644
#else
3645
int wc_Sha512_224FinalRaw(wc_Sha512* sha, byte* hash)
3646
0
{
3647
0
    return Sha512FinalRaw(sha, hash, WC_SHA512_224_DIGEST_SIZE);
3648
0
}
3649
3650
int wc_Sha512_224Final(wc_Sha512* sha512, byte* hash)
3651
585
{
3652
585
    return Sha512_Family_Final(sha512, hash, WC_SHA512_224_DIGEST_SIZE,
3653
585
                               InitSha512_224);
3654
585
}
3655
#endif /* else none of the above: WOLFSSL_KCAPI_HASH, WOLFSSL_SE050 */
3656
3657
void wc_Sha512_224Free(wc_Sha512* sha)
3658
585
{
3659
585
    wc_Sha512Free(sha);
3660
585
}
3661
3662
#if defined(WOLFSSL_KCAPI_HASH)
3663
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
3664
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
3665
3666
#elif defined(WOLFSSL_RENESAS_RSIP) && \
3667
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
3668
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
3669
3670
#else
3671
int wc_Sha512_224GetHash(wc_Sha512* sha512, byte* hash)
3672
0
{
3673
0
    return Sha512_Family_GetHash(sha512, hash, wc_Sha512_224Final);
3674
0
}
3675
3676
int wc_Sha512_224Copy(wc_Sha512* src, wc_Sha512* dst)
3677
0
{
3678
0
    return wc_Sha512Copy(src, dst);
3679
0
}
3680
#endif /* else none of the above: WOLFSSL_KCAPI_HASH, WOLFSSL_SE050 */
3681
3682
#ifdef WOLFSSL_HASH_FLAGS
3683
int wc_Sha512_224SetFlags(wc_Sha512* sha, word32 flags)
3684
0
{
3685
0
    return wc_Sha512SetFlags(sha, flags);
3686
0
}
3687
int wc_Sha512_224GetFlags(wc_Sha512* sha, word32* flags)
3688
0
{
3689
0
    return wc_Sha512GetFlags(sha, flags);
3690
0
}
3691
#endif /* WOLFSSL_HASH_FLAGS */
3692
3693
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
3694
int wc_Sha512_224Transform(wc_Sha512* sha, const unsigned char* data)
3695
{
3696
    return wc_Sha512Transform(sha, data);
3697
}
3698
#endif /* OPENSSL_EXTRA */
3699
3700
3701
#endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
3702
3703
#if !defined(WOLFSSL_NOSHA512_256) && \
3704
   (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
3705
#if defined(STM32_HASH_SHA512_256)
3706
3707
    int wc_InitSha512_256_ex(wc_Sha512* sha512, void* heap, int devId)
3708
    {
3709
        if (sha512 == NULL)
3710
            return BAD_FUNC_ARG;
3711
3712
        (void)devId;
3713
        (void)heap;
3714
3715
        XMEMSET(sha512, 0, sizeof(wc_Sha512));
3716
        wc_Stm32_Hash_Init(&sha512->stmCtx);
3717
#if defined(WOLFSSL_SHA512_HASHTYPE)
3718
        sha512->hashType = WC_HASH_TYPE_SHA512_256;
3719
#endif
3720
        return 0;
3721
    }
3722
3723
    int wc_Sha512_256Update(wc_Sha512* sha512, const byte* data, word32 len)
3724
    {
3725
        int ret = 0;
3726
3727
        if (sha512 == NULL) {
3728
            return BAD_FUNC_ARG;
3729
        }
3730
        if (data == NULL && len == 0) {
3731
            /* valid, but do nothing */
3732
            return 0;
3733
        }
3734
        if (data == NULL) {
3735
            return BAD_FUNC_ARG;
3736
        }
3737
3738
        ret = wolfSSL_CryptHwMutexLock();
3739
        if (ret == 0) {
3740
            ret = wc_Stm32_Hash_Update(&sha512->stmCtx,
3741
                HASH_ALGOSELECTION_SHA512_256, data, len, WC_SHA512_256_BLOCK_SIZE);
3742
            wolfSSL_CryptHwMutexUnLock();
3743
        }
3744
        return ret;
3745
    }
3746
3747
    int wc_Sha512_256Final(wc_Sha512* sha512, byte* hash)
3748
    {
3749
        int ret = 0;
3750
3751
        if (sha512 == NULL || hash == NULL) {
3752
            return BAD_FUNC_ARG;
3753
        }
3754
3755
        ret = wolfSSL_CryptHwMutexLock();
3756
        if (ret == 0) {
3757
            ret = wc_Stm32_Hash_Final(&sha512->stmCtx,
3758
                HASH_ALGOSELECTION_SHA512_256, hash, WC_SHA512_256_DIGEST_SIZE);
3759
            wolfSSL_CryptHwMutexUnLock();
3760
        }
3761
3762
        (void)wc_InitSha512_256(sha512); /* reset state */
3763
3764
        return ret;
3765
    }
3766
#elif defined(PSOC6_HASH_SHA2)
3767
    /* functions defined in wolfcrypt/src/port/cypress/psoc6_crypto.c */
3768
#endif
3769
int wc_InitSha512_256(wc_Sha512* sha)
3770
0
{
3771
0
    return wc_InitSha512_256_ex(sha, NULL, INVALID_DEVID);
3772
0
}
3773
#if !defined(STM32_HASH_SHA512_256) && !defined(PSOC6_HASH_SHA2)
3774
int wc_Sha512_256Update(wc_Sha512* sha, const byte* data, word32 len)
3775
184
{
3776
184
    return wc_Sha512Update(sha, data, len);
3777
184
}
3778
#endif
3779
#if defined(WOLFSSL_KCAPI_HASH)
3780
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
3781
#elif defined(WOLFSSL_RENESAS_RSIP) && \
3782
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
3783
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
3784
3785
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
3786
#elif defined(STM32_HASH_SHA512_256)
3787
#elif defined(PSOC6_HASH_SHA2)
3788
    /* functions defined in wolfcrypt/src/port/cypress/psoc6_crypto.c */
3789
#else
3790
int wc_Sha512_256FinalRaw(wc_Sha512* sha, byte* hash)
3791
0
{
3792
0
    return Sha512FinalRaw(sha, hash, WC_SHA512_256_DIGEST_SIZE);
3793
0
}
3794
3795
int wc_Sha512_256Final(wc_Sha512* sha512, byte* hash)
3796
184
{
3797
184
    return Sha512_Family_Final(sha512, hash, WC_SHA512_256_DIGEST_SIZE,
3798
184
                               InitSha512_256);
3799
184
}
3800
#endif
3801
3802
void wc_Sha512_256Free(wc_Sha512* sha)
3803
184
{
3804
184
    wc_Sha512Free(sha);
3805
184
}
3806
3807
#if defined(WOLFSSL_KCAPI_HASH)
3808
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
3809
#elif defined(WOLFSSL_RENESAS_RSIP) && \
3810
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
3811
    /* functions defined in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
3812
3813
#else
3814
int wc_Sha512_256GetHash(wc_Sha512* sha512, byte* hash)
3815
0
{
3816
0
    return Sha512_Family_GetHash(sha512, hash, wc_Sha512_256Final);
3817
0
}
3818
int wc_Sha512_256Copy(wc_Sha512* src, wc_Sha512* dst)
3819
0
{
3820
0
    return wc_Sha512Copy(src, dst);
3821
0
}
3822
#endif
3823
3824
#ifdef WOLFSSL_HASH_FLAGS
3825
int wc_Sha512_256SetFlags(wc_Sha512* sha, word32 flags)
3826
0
{
3827
0
    return wc_Sha512SetFlags(sha, flags);
3828
0
}
3829
int wc_Sha512_256GetFlags(wc_Sha512* sha, word32* flags)
3830
0
{
3831
0
    return wc_Sha512GetFlags(sha, flags);
3832
0
}
3833
#endif /* WOLFSSL_HASH_FLAGS */
3834
3835
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
3836
int wc_Sha512_256Transform(wc_Sha512* sha, const unsigned char* data)
3837
{
3838
    return wc_Sha512Transform(sha, data);
3839
}
3840
#endif /* OPENSSL_EXTRA */
3841
3842
3843
#endif /* !WOLFSSL_NOSHA512_256 && !FIPS ... */
3844
3845
#endif /* WOLFSSL_SHA512 */
3846
3847
#ifdef WOLFSSL_SHA384
3848
3849
#if defined(WOLFSSL_KCAPI_HASH)
3850
    /* functions defined in wolfcrypt/src/port/kcapi/kcapi_hash.c */
3851
#elif defined(WOLFSSL_RENESAS_RSIP) && \
3852
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
3853
    /* functions defined in wolfcrypt/src/port/renesas/renesas_fspsm_sha.c */
3854
#elif defined(MAX3266X_SHA)
3855
    /* Functions defined in wolfcrypt/src/port/maxim/max3266x.c */
3856
3857
#else
3858
3859
int wc_Sha384GetHash(wc_Sha384* sha384, byte* hash)
3860
582
{
3861
582
    int ret;
3862
582
    WC_DECLARE_VAR(tmpSha384, wc_Sha384, 1, 0);
3863
3864
582
    if (sha384 == NULL || hash == NULL) {
3865
0
        return BAD_FUNC_ARG;
3866
0
    }
3867
3868
582
    WC_CALLOC_VAR_EX(tmpSha384, wc_Sha384, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
3869
582
        return MEMORY_E);
3870
#ifdef WOLFSSL_CHECK_MEM_ZERO
3871
    wc_MemZero_Add("wc_Sha384GetHash tmpSha384", tmpSha384,
3872
        sizeof(*tmpSha384));
3873
#endif
3874
3875
    /* copy this sha384 into tmpSha */
3876
582
    ret = wc_Sha384Copy(sha384, tmpSha384);
3877
582
    if (ret == 0) {
3878
582
        ret = wc_Sha384Final(tmpSha384, hash);
3879
582
        wc_Sha384Free(tmpSha384);
3880
582
    }
3881
3882
582
    ForceZero(tmpSha384, sizeof(*tmpSha384));
3883
#if defined(WOLFSSL_CHECK_MEM_ZERO) && !defined(WOLFSSL_SMALL_STACK)
3884
    wc_MemZero_Check(tmpSha384, sizeof(*tmpSha384));
3885
#endif
3886
3887
582
    WC_FREE_VAR_EX(tmpSha384, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3888
3889
582
    return ret;
3890
582
}
3891
3892
int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
3893
616
{
3894
616
    int ret = 0;
3895
3896
616
    if (src == NULL || dst == NULL) {
3897
0
        return BAD_FUNC_ARG;
3898
0
    }
3899
3900
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_COPY)
3901
    #ifndef WOLF_CRYPTO_CB_FIND
3902
    if (src->devId != INVALID_DEVID)
3903
    #endif
3904
    {
3905
        /* Cast the source and destination to be void to keep the abstraction */
3906
        ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH,
3907
                               WC_HASH_TYPE_SHA384, (void*)src, (void*)dst);
3908
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3909
            return ret;
3910
        /* fall-through when unavailable */
3911
    }
3912
    ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
3913
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
3914
3915
    /* Free dst resources before copy to prevent memory leaks (e.g., msg
3916
     * buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
3917
616
    wc_Sha384Free(dst);
3918
616
    XMEMCPY(dst, src, sizeof(wc_Sha384));
3919
3920
#ifdef WOLFSSL_SMALL_STACK_CACHE
3921
    /* This allocation combines the customary W buffer used by
3922
     * _Transform_Sha512() with additional buffer space used by
3923
     * wc_Sha512Transform().
3924
     */
3925
    dst->W = (word64 *)XMALLOC(sizeof(Sha512TScratch) + WC_SHA384_BLOCK_SIZE,
3926
                               dst->heap, DYNAMIC_TYPE_DIGEST);
3927
    if (dst->W == NULL) {
3928
        ForceZero(dst, sizeof(wc_Sha384));
3929
        return MEMORY_E;
3930
    }
3931
#endif
3932
3933
#if defined(WOLFSSL_SILABS_SE_TYPES) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \
3934
    defined(WOLFSSL_SILABS_SHA384)
3935
    dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
3936
    dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
3937
#endif
3938
3939
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
3940
    ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
3941
#endif
3942
3943
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
3944
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA384)
3945
    #if defined(CONFIG_IDF_TARGET_ESP32)
3946
        esp_sha384_ctx_copy(src, dst);
3947
    #elif defined(CONFIG_IDF_TARGET_ESP32C2) || \
3948
          defined(CONFIG_IDF_TARGET_ESP8684) || \
3949
          defined(CONFIG_IDF_TARGET_ESP32C3) || \
3950
          defined(CONFIG_IDF_TARGET_ESP32C6)
3951
        ESP_LOGV(TAG, "No SHA-384 HW on the ESP32-C3");
3952
    #elif defined(CONFIG_IDF_TARGET_ESP32S2) || \
3953
          defined(CONFIG_IDF_TARGET_ESP32S3)
3954
        esp_sha384_ctx_copy(src, dst);
3955
    #else
3956
        ESP_LOGW(TAG, "No SHA384 HW or not yet implemented for %s",
3957
                       CONFIG_IDF_TARGET);
3958
    #endif
3959
#endif
3960
3961
#ifdef HAVE_ARIA
3962
    dst->hSession = NULL;
3963
    if((src->hSession != NULL) && (MC_CopySession(src->hSession, &(dst->hSession)) != MC_OK)) {
3964
        return MEMORY_E;
3965
    }
3966
#endif
3967
3968
616
#ifdef WOLFSSL_HASH_FLAGS
3969
616
     dst->flags |= WC_HASH_FLAG_ISCOPY;
3970
616
#endif
3971
3972
#if defined(WOLFSSL_HASH_KEEP)
3973
    if (src->msg != NULL) {
3974
        dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
3975
        if (dst->msg == NULL)
3976
            return MEMORY_E;
3977
        XMEMCPY(dst->msg, src->msg, src->len);
3978
    }
3979
#endif
3980
3981
3982
#if defined(PSOC6_HASH_SHA2)
3983
    wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA384, 0);
3984
#endif
3985
3986
616
    return ret;
3987
616
}
3988
3989
#endif /* WOLFSSL_KCAPI_HASH */
3990
3991
#ifdef WOLFSSL_HASH_FLAGS
3992
int wc_Sha384SetFlags(wc_Sha384* sha384, word32 flags)
3993
59.5k
{
3994
59.5k
    if (sha384) {
3995
59.5k
        sha384->flags = flags;
3996
59.5k
    }
3997
59.5k
    return 0;
3998
59.5k
}
3999
int wc_Sha384GetFlags(wc_Sha384* sha384, word32* flags)
4000
0
{
4001
0
    if (sha384 && flags) {
4002
0
        *flags = sha384->flags;
4003
0
    }
4004
0
    return 0;
4005
0
}
4006
#endif
4007
4008
#endif /* WOLFSSL_SHA384 */
4009
4010
#ifdef WOLFSSL_HASH_KEEP
4011
/* Some hardware have issues with update, this function stores the data to be
4012
 * hashed into an array. Once ready, the Final operation is called on all of the
4013
 * data to be hashed at once.
4014
 * returns 0 on success
4015
 */
4016
int wc_Sha512_Grow(wc_Sha512* sha512, const byte* in, int inSz)
4017
{
4018
    return _wc_Hash_Grow(&(sha512->msg), &(sha512->used), &(sha512->len), in,
4019
                        inSz, sha512->heap);
4020
}
4021
#ifdef WOLFSSL_SHA384
4022
int wc_Sha384_Grow(wc_Sha384* sha384, const byte* in, int inSz)
4023
{
4024
    return _wc_Hash_Grow(&(sha384->msg), &(sha384->used), &(sha384->len), in,
4025
                        inSz, sha384->heap);
4026
}
4027
#endif /* WOLFSSL_SHA384 */
4028
#endif /* WOLFSSL_HASH_KEEP */
4029
4030
#endif /* !WOLF_CRYPTO_CB_ONLY_SHA512 */
4031
4032
/* Fallback implementations of the Reset functions, for all configurations other
4033
 * than plain software.  Continues with the established heap and devId.
4034
 */
4035
4036
#ifdef WOLF_CRYPTO_CB
4037
    #define WC_SHA512_RESET_DEVID(sha) ((sha)->devId)
4038
#else
4039
    #define WC_SHA512_RESET_DEVID(sha) (INVALID_DEVID)
4040
#endif
4041
#define WC_SHA512_RESET_FALLBACK_IMPLEMENT(flavor)              \
4042
    int wc_##flavor##Reset(wc_##flavor *sha) {                  \
4043
        void *heap;                                             \
4044
        int devId;                                              \
4045
                                                                \
4046
        if (sha == NULL)                                        \
4047
            return BAD_FUNC_ARG;                                \
4048
                                                                \
4049
        heap = sha->heap;                                       \
4050
        devId = WC_SHA512_RESET_DEVID(sha);                     \
4051
                                                                \
4052
        wc_##flavor##Free(sha);                                 \
4053
        return wc_Init##flavor##_ex(sha, heap, devId);          \
4054
    }
4055
4056
#if defined(WOLFSSL_SHA512) && !defined(WC_SHA512RESET_DEFINED)
4057
    WC_SHA512_RESET_FALLBACK_IMPLEMENT(Sha512)
4058
#endif
4059
4060
#if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
4061
    !defined(WC_SHA512_224RESET_DEFINED) && \
4062
    (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
4063
    WC_SHA512_RESET_FALLBACK_IMPLEMENT(Sha512_224)
4064
#endif
4065
4066
#if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
4067
    !defined(WC_SHA512_256RESET_DEFINED) && \
4068
    (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
4069
    WC_SHA512_RESET_FALLBACK_IMPLEMENT(Sha512_256)
4070
#endif
4071
4072
#if defined(WOLFSSL_SHA384) && !defined(WC_SHA384RESET_DEFINED)
4073
    WC_SHA512_RESET_FALLBACK_IMPLEMENT(Sha384)
4074
#endif
4075
4076
#endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */