Coverage Report

Created: 2026-10-31 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl-openssl-api/wolfcrypt/src/sha256.c
Line
Count
Source
1
/* sha256.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
/* For more info on the algorithm, see https://tools.ietf.org/html/rfc6234
23
 *
24
 * For more information on NIST FIPS PUB 180-4, see
25
 * https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
26
 */
27
28
/*
29
30
DESCRIPTION
31
This library provides the interface to SHA-256 secure hash algorithms.
32
SHA-256 performs processing on message blocks to produce a final hash digest
33
output. It can be used to hash a message, M, having a length of L bits,
34
where 0 <= L < 2^64.
35
36
Note that in some cases, hardware acceleration may be enabled, depending
37
on the specific device platform.
38
39
*/
40
41
#define WC_FIPS_LL_CRYPTO
42
#define _WC_BUILDING_SHA256_C
43
44
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
45
46
/*
47
 * SHA256 Build Options:
48
 * USE_SLOW_SHA256:            Reduces code size by not partially unrolling
49
                                (~2KB smaller and ~25% slower) (default OFF)
50
 * WOLFSSL_SHA256_BY_SPEC:     Uses the Ch/Maj based on SHA256 specification
51
                                (default ON)
52
 * WOLFSSL_SHA256_ALT_CH_MAJ:  Alternate Ch/Maj that is easier for compilers to
53
                                optimize and recognize as SHA256 (default OFF)
54
 * SHA256_MANY_REGISTERS:      A SHA256 version that keeps all data in registers
55
                                and partial unrolled (default OFF)
56
 */
57
58
/* Default SHA256 to use Ch/Maj based on specification */
59
#if !defined(WOLFSSL_SHA256_BY_SPEC) && !defined(WOLFSSL_SHA256_ALT_CH_MAJ)
60
    #define WOLFSSL_SHA256_BY_SPEC
61
#endif
62
63
64
#if !defined(NO_SHA256)
65
66
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
67
    #if defined(USE_WINDOWS_API) && defined(_MSC_VER)
68
        #pragma code_seg(".fipsA$l")
69
        #pragma const_seg(".fipsB$l")
70
    #endif
71
#endif
72
73
#include <wolfssl/wolfcrypt/sha256.h>
74
#include <wolfssl/wolfcrypt/cpuid.h>
75
#include <wolfssl/wolfcrypt/hash.h>
76
77
#ifdef WOLF_CRYPTO_CB
78
    #include <wolfssl/wolfcrypt/cryptocb.h>
79
#endif
80
81
#ifdef WOLFSSL_IMXRT1170_CAAM
82
    #include <wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h>
83
#endif
84
85
86
/* determine if we are using Espressif SHA hardware acceleration */
87
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
88
#if defined(WOLFSSL_ESP32_CRYPT) && \
89
    !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
90
    /* define a single keyword for simplicity & readability
91
     *
92
     * by default the HW acceleration is on for ESP32-WROOM32
93
     * but individual components can be turned off.
94
     */
95
    #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
96
#else
97
    #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
98
#endif
99
100
/* WOLF_CRYPTO_CB_ONLY_SHA256 strips the software SHA-256 implementation and
101
 * routes every operation through the crypto callback. It is mutually exclusive
102
 * with any in-tree SHA-256 hardware/asm backend below: keep this list in sync
103
 * with the #elif chain at the start of the "Hardware Acceleration" section. */
104
#if defined(WOLF_CRYPTO_CB_ONLY_SHA256) && ( \
105
        defined(WOLFSSL_TI_HASH) || \
106
        defined(MAX3266X_SHA) || \
107
        defined(FREESCALE_LTC_SHA) || \
108
        defined(FREESCALE_MMCAU_SHA) || \
109
        defined(WOLFSSL_PIC32MZ_HASH) || \
110
        defined(STM32_HASH_SHA2) || \
111
        (defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH)) || \
112
        (defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)) || \
113
        defined(WOLFSSL_AFALG_HASH) || \
114
        defined(WOLFSSL_DEVCRYPTO_HASH) || \
115
        (defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_HASH)) || \
116
        defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) || \
117
        defined(WOLFSSL_RENESAS_TSIP_TLS) || \
118
        defined(WOLFSSL_RENESAS_SCEPROTECT) || \
119
        defined(WOLFSSL_RENESAS_RSIP) || \
120
        defined(PSOC6_HASH_SHA2) || \
121
        defined(WOLFSSL_IMXRT_DCP) || \
122
        defined(WOLFSSL_NXP_HASHCRYPT_SHA) || \
123
        defined(WOLFSSL_SILABS_SE_ACCEL) || \
124
        defined(WOLFSSL_KCAPI_HASH) || \
125
        (defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)) || \
126
        defined(WOLFSSL_RENESAS_RX64_HASH) || \
127
        defined(WOLFSSL_PPC32_ASM) || \
128
        defined(WOLFSSL_PPC64_ASM) || \
129
        defined(WOLFSSL_ARMASM) || \
130
        defined(WOLFSSL_RISCV_ASM) || \
131
        (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
132
            (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))))
133
    #error "WOLF_CRYPTO_CB_ONLY_SHA256 is incompatible with SHA-256 hardware" \
134
           " acceleration backends"
135
#endif
136
137
#ifdef WOLFSSL_ESPIDF
138
    /* Define the ESP_LOGx(TAG,  WOLFSSL_ESPIDF_BLANKLINE_MESSAGE value for output messages here.
139
    **
140
    ** Beware of possible conflict in test.c (that one now named TEST_TAG)
141
    */
142
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
143
       !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
144
        static const char* TAG = "wc_sha256";
145
    #endif
146
#endif
147
148
#if defined(WOLFSSL_TI_HASH)
149
    /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
150
#elif defined(MAX3266X_SHA)
151
    /* Already brought in by sha256.h */
152
    /* #include <wolfssl/wolfcrypt/port/maxim/max3266x.h> */
153
#else
154
155
#ifdef NO_INLINE
156
    #include <wolfssl/wolfcrypt/misc.h>
157
#else
158
    #define WOLFSSL_MISC_INCLUDED
159
    #include <wolfcrypt/src/misc.c>
160
#endif
161
162
#ifdef WOLFSSL_DEVCRYPTO_HASH
163
    #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
164
#endif
165
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
166
    #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
167
#endif
168
169
#if FIPS_VERSION3_GE(6,0,0)
170
    const unsigned int wolfCrypt_FIPS_sha256_ro_sanity[2] =
171
                                                     { 0x1a2b3c4d, 0x00000014 };
172
    int wolfCrypt_FIPS_SHA256_sanity(void)
173
    {
174
        return 0;
175
    }
176
#endif
177
178
#if defined(WC_C_DYNAMIC_FALLBACK) && \
179
        defined(WOLFSSL_AESNI) && !defined(USE_INTEL_SPEEDUP)
180
    /* AES-NI can be enabled with WC_C_DYNAMIC_FALLBACK, but without the rest of
181
     * USE_INTEL_SPEEDUP, in which case we need to disable the dynamic
182
     * fallback.
183
     */
184
    #undef WC_C_DYNAMIC_FALLBACK
185
#endif
186
187
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
188
    #if defined(__GNUC__) && ((__GNUC__ < 4) || \
189
                              (__GNUC__ == 4 && __GNUC_MINOR__ <= 8))
190
        #undef  NO_AVX2_SUPPORT
191
        #define NO_AVX2_SUPPORT
192
    #endif
193
    #if defined(__clang__) && ((__clang_major__ < 3) || \
194
                               (__clang_major__ == 3 && __clang_minor__ <= 5))
195
        #define NO_AVX2_SUPPORT
196
    #elif defined(__clang__) && defined(NO_AVX2_SUPPORT)
197
        #undef NO_AVX2_SUPPORT
198
    #endif
199
200
    #define HAVE_INTEL_AVX1
201
    #ifndef NO_AVX2_SUPPORT
202
        #define HAVE_INTEL_AVX2
203
    #endif
204
#else
205
    #undef HAVE_INTEL_AVX1
206
    #undef HAVE_INTEL_AVX2
207
#endif /* WOLFSSL_X86_64_BUILD && USE_INTEL_SPEEDUP */
208
209
#if defined(HAVE_INTEL_AVX2)
210
    #define HAVE_INTEL_RORX
211
#endif
212
213
#if defined(LITTLE_ENDIAN_ORDER)
214
    #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
215
          defined(CONFIG_IDF_TARGET_ESP8684) || \
216
          defined(CONFIG_IDF_TARGET_ESP32C3) || \
217
          defined(CONFIG_IDF_TARGET_ESP32C6)    \
218
        ) && \
219
        defined(WOLFSSL_ESP32_CRYPT) &&         \
220
        !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
221
        !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
222
        /* For Espressif RISC-V Targets, we *may* need to reverse bytes
223
         * depending on if HW is active or not. */
224
        #define SHA256_REV_BYTES(ctx) \
225
            (esp_sha_need_byte_reversal(ctx))
226
    #elif defined(FREESCALE_MMCAU_SHA)
227
        #define SHA256_REV_BYTES(ctx)       1 /* reverse needed on final */
228
    #endif
229
#endif
230
#ifndef SHA256_REV_BYTES
231
    #if defined(LITTLE_ENDIAN_ORDER) || defined(WOLFSSL_WIDE_BYTE)
232
756k
        #define SHA256_REV_BYTES(ctx)       1
233
    #else
234
        #define SHA256_REV_BYTES(ctx)       0
235
    #endif
236
#endif
237
#if defined(LITTLE_ENDIAN_ORDER) && \
238
        defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
239
        (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
240
241
    #if defined(WC_C_DYNAMIC_FALLBACK) && !defined(WC_NO_INTERNAL_FUNCTION_POINTERS)
242
        /* With the AVX backend, wc_Sha256.buffer is in big endian even though
243
         * the host is little endian.  For WC_C_DYNAMIC_FALLBACK, which requires
244
         * alternating between AVX and C, we activate
245
         * WC_NO_INTERNAL_FUNCTION_POINTERS, which arranges for just-in-time
246
         * byte swapping on each call to the C back end.  This keeps the buffers
247
         * big endian at all times.
248
         */
249
        #define WC_NO_INTERNAL_FUNCTION_POINTERS
250
    #endif
251
252
    #ifdef WC_NO_INTERNAL_FUNCTION_POINTERS
253
        /* With WC_NO_INTERNAL_FUNCTION_POINTERS every transform is dispatched
254
         * through inline_XTRANSFORM{,_LEN}(), whose C arm is
255
         * Transform_Sha256{,_Len}_C_from_raw() -- those byte-reverse the block
256
         * themselves, just in time.
257
         */
258
        #define WC_SHA256_RAW_BE_BUFFER
259
        #define SHA256_UPDATE_REV_BYTES(ctx) 0
260
    #else
261
        #define SHA256_UPDATE_REV_BYTES(ctx) \
262
            (!IS_INTEL_AVX1(intel_flags) && !IS_INTEL_AVX2(intel_flags) && \
263
             !IS_INTEL_SHA(intel_flags))
264
    #endif
265
#elif defined(FREESCALE_MMCAU_SHA)
266
    #define SHA256_UPDATE_REV_BYTES(ctx)    0 /* reverse not needed on update */
267
#elif defined(WOLFSSL_PPC32_ASM)
268
    #define SHA256_UPDATE_REV_BYTES(ctx)    0
269
#elif defined(WOLFSSL_PPC64_ASM)
270
    #define SHA256_UPDATE_REV_BYTES(ctx)    0
271
#elif defined(WOLFSSL_ARMASM)
272
    #define SHA256_UPDATE_REV_BYTES(ctx)    0
273
#elif defined(WOLFSSL_RISCV_ASM)
274
    #define SHA256_UPDATE_REV_BYTES(ctx)    0
275
#else
276
668k
    #define SHA256_UPDATE_REV_BYTES(ctx)    SHA256_REV_BYTES(ctx)
277
#endif
278
279
#if !defined(WOLFSSL_PIC32MZ_HASH) && !defined(STM32_HASH_SHA2) && \
280
    (!defined(WOLFSSL_IMX6_CAAM) || defined(NO_IMX6_CAAM_HASH) || \
281
     defined(WOLFSSL_QNX_CAAM)) && \
282
    !defined(WOLFSSL_AFALG_HASH) && !defined(WOLFSSL_DEVCRYPTO_HASH) && \
283
    (!defined(WOLFSSL_ESP32_CRYPT) || defined(NO_WOLFSSL_ESP32_CRYPT_HASH)) && \
284
    ((!defined(WOLFSSL_RENESAS_TSIP_TLS) && \
285
      !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) || \
286
     defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) && \
287
    !defined(PSOC6_HASH_SHA2) && !defined(WOLFSSL_IMXRT_DCP) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
288
    !defined(WOLFSSL_NXP_HASHCRYPT_SHA) && \
289
    !defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_SE050_HASH) && \
290
    ((!defined(WOLFSSL_RENESAS_SCEPROTECT) && \
291
      !defined(WOLFSSL_RENESAS_RSIP)) \
292
      || defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) && \
293
    (!defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)) && \
294
    !defined(WOLFSSL_RENESAS_RX64_HASH)
295
296
#if (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
297
     (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))) || \
298
    (defined(WOLFSSL_ARMASM) && defined(__aarch64__) && \
299
     !defined(WOLF_CRYPTO_CB_ONLY_SHA256))
300
static void Sha256_SetTransform(void);
301
#endif
302
303
static int InitSha256(wc_Sha256* sha256)
304
189k
{
305
189k
    XMEMSET(sha256->digest, 0, sizeof(sha256->digest));
306
189k
    sha256->digest[0] = 0x6A09E667L;
307
189k
    sha256->digest[1] = 0xBB67AE85L;
308
189k
    sha256->digest[2] = 0x3C6EF372L;
309
189k
    sha256->digest[3] = 0xA54FF53AL;
310
189k
    sha256->digest[4] = 0x510E527FL;
311
189k
    sha256->digest[5] = 0x9B05688CL;
312
189k
    sha256->digest[6] = 0x1F83D9ABL;
313
189k
    sha256->digest[7] = 0x5BE0CD19L;
314
315
189k
    sha256->buffLen = 0;
316
189k
    XMEMSET(sha256->buffer, 0, sizeof(sha256->buffer));
317
189k
    sha256->loLen   = 0;
318
189k
    sha256->hiLen   = 0;
319
189k
#ifdef WOLFSSL_HASH_FLAGS
320
189k
    sha256->flags = 0;
321
189k
#endif
322
#ifdef WOLFSSL_HASH_KEEP
323
    sha256->msg  = NULL;
324
    sha256->len  = 0;
325
    sha256->used = 0;
326
#endif
327
328
#if (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
329
     (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))) || \
330
    (defined(WOLFSSL_ARMASM) && defined(__aarch64__) && \
331
     !defined(WOLF_CRYPTO_CB_ONLY_SHA256))
332
    /* choose best Transform function under this runtime environment */
333
    Sha256_SetTransform();
334
#endif
335
336
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
337
    XMEMSET(&sha256->maxq_ctx, 0, sizeof(sha256->maxq_ctx));
338
#endif
339
340
#ifdef HAVE_ARIA
341
    sha256->hSession = NULL;
342
#endif
343
344
189k
    return 0;
345
189k
}
346
347
#if !defined(WOLFSSL_HASH_KEEP) && !defined(WOLF_CRYPTO_CB_ONLY_SHA256)
348
349
/* Reset a hash context to its freshly initialized state, reusing its existing
350
 * allocations.  Like the Final functions, Reset does not destroy sensitive
351
 * internal state; use the matching Free function for teardown at end of life.
352
 *
353
 * The in-place form is only for the software implementation: a CB_ONLY build
354
 * routes hashing through a device whose state InitSha256() cannot restart, so
355
 * it falls back to Free + Init_ex, which gives the callback its teardown and
356
 * re-setup hooks.  (SHA-224's in-place variant sits in the NEED_SOFT arm and
357
 * sha512.c's sit in the software-implementation arm, so both exclude CB_ONLY
358
 * structurally; SHA-256's region also serves the CB_ONLY build -- see
359
 * wc_InitSha224_ex()'s CB_ONLY arm -- hence the explicit conjunct.) */
360
0
int wc_Sha256Reset(wc_Sha256* sha256) {
361
0
    if (sha256 == NULL)
362
0
        return BAD_FUNC_ARG;
363
0
#ifdef WOLF_CRYPTO_CB
364
    /* A device may hang state off devCtx that InitSha256() cannot restart.
365
     * Free and re-init so the callback gets its teardown and setup. */
366
0
    #ifndef WOLF_CRYPTO_CB_FIND
367
0
    if (sha256->devId != INVALID_DEVID)
368
0
    #endif
369
0
    {
370
0
        void* heap = sha256->heap;
371
0
        int devId = sha256->devId;
372
0
        wc_Sha256Free(sha256);
373
0
        return wc_InitSha256_ex(sha256, heap, devId);
374
0
    }
375
0
#endif
376
0
    return InitSha256(sha256);
377
0
}
378
#define WC_SHA256RESET_DEFINED
379
#endif /* !WOLFSSL_HASH_KEEP && !WOLF_CRYPTO_CB_ONLY_SHA256 */
380
381
#endif
382
383
/* Hardware Acceleration */
384
/* Caller-owned transient scratch for the C transform: the message schedule
385
 * and the working variables, so one ForceZero when the public call returns
386
 * wipes all of it at once, instead of per-block wipes inside the transform.
387
 * Natural alignment suffices; only the C transform uses it. */
388
typedef struct Sha256TScratch {
389
#ifdef SHA256_MANY_REGISTERS
390
    /* The MANY_REGISTERS transform rolls a 16-word window. */
391
    word32 W[WC_SHA256_BLOCK_SIZE / sizeof(word32)];
392
#else
393
    word32 W[WC_SHA256_BLOCK_SIZE];
394
#endif
395
    word32 S[8];
396
} Sha256TScratch;
397
398
/* External W providers size by the header constant; keep them in lockstep. */
399
wc_static_assert(sizeof(Sha256TScratch) == WC_SHA256_TSCRATCH_SIZE);
400
401
/* Scratch provisioning for the callers that drive the C transform.  The
402
 * declared pointer is named sha256TScr.  BEGIN validates it (and registers it
403
 * with WOLFSSL_CHECK_MEM_ZERO); WIPE is the single ForceZero (and the
404
 * mem-zero check).  Under WOLFSSL_SMALL_STACK_CACHE the scratch is the
405
 * per-context cache allocated at init and wiped at Free, so BEGIN checks for
406
 * it and WIPE is a no-op, preserving that configuration's zero per-call
407
 * cost. */
408
#if defined(WOLFSSL_SMALL_STACK_CACHE) && !defined(WOLFSSL_NO_MALLOC)
409
    #define WC_SHA256_TSCRATCH_DECL(s) \
410
        Sha256TScratch* sha256TScr = (Sha256TScratch*)(s)->W
411
    #define WC_SHA256_TSCRATCH_BEGIN(s) \
412
        do { if (sha256TScr == NULL) return BAD_FUNC_ARG; } while (0)
413
    #define WC_SHA256_TSCRATCH_WIPE(s) WC_DO_NOTHING /* wiped at Free */
414
#else
415
    /* Stack in big-stack builds, heap under WOLFSSL_SMALL_STACK, per the
416
     * WC_DECLARE_VAR family (one allocation per public call -- the previous
417
     * code allocated and freed per block under WOLFSSL_SMALL_STACK). */
418
    #ifdef WOLFSSL_CHECK_MEM_ZERO
419
        /* Zero first: the registration call takes const void*, and a
420
         * pointer to an uninitialized object trips -Wmaybe-uninitialized
421
         * under -Werror; a deterministic start also suits the checker. */
422
        #define WC_SHA256_TSCRATCH_MZ_ADD()                       \
423
            do {                                                             \
424
                XMEMSET(sha256TScr, 0, sizeof(Sha256TScratch));                   \
425
                wc_MemZero_Add("Sha256 transform scratch", sha256TScr,      \
426
                               sizeof(Sha256TScratch));                       \
427
            } while (0)
428
        #define WC_SHA256_TSCRATCH_MZ_CHECK() \
429
            wc_MemZero_Check(sha256TScr, sizeof(Sha256TScratch))
430
    #else
431
605k
        #define WC_SHA256_TSCRATCH_MZ_ADD() WC_DO_NOTHING
432
605k
        #define WC_SHA256_TSCRATCH_MZ_CHECK() WC_DO_NOTHING
433
    #endif
434
    #define WC_SHA256_TSCRATCH_DECL(s) \
435
605k
        WC_DECLARE_VAR(sha256TScr, Sha256TScratch, 1, (s)->heap)
436
    #define WC_SHA256_TSCRATCH_BEGIN(s)                                      \
437
605k
        do {                                                                 \
438
605k
            WC_ALLOC_VAR_EX(sha256TScr, Sha256TScratch, 1, (s)->heap,        \
439
605k
                            DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);       \
440
605k
            WC_SHA256_TSCRATCH_MZ_ADD();                                     \
441
605k
        } while (0)
442
    #define WC_SHA256_TSCRATCH_WIPE(s)                                       \
443
605k
        do {                                                                 \
444
605k
            ForceZero(sha256TScr, sizeof(Sha256TScratch));                   \
445
605k
            WC_SHA256_TSCRATCH_MZ_CHECK();                                   \
446
605k
            WC_FREE_VAR_EX(sha256TScr, (s)->heap, DYNAMIC_TYPE_TMP_BUFFER);  \
447
605k
        } while (0)
448
#endif
449
450
#if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
451
    (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)) && \
452
    !defined(WOLF_CRYPTO_CB_ONLY_SHA256)
453
454
    /* in case intel instructions aren't available, plus we need the K[] global */
455
    #define NEED_SOFT_SHA256
456
457
    /*****
458
    Intel AVX1/AVX2 Macro Control Structure
459
460
    #define HAVE_INTEL_AVX1
461
    #define HAVE_INTEL_AVX2
462
463
    #define HAVE_INTEL_RORX
464
465
466
    int InitSha256(wc_Sha256* sha256) {
467
         Save/Recover XMM, YMM
468
         ...
469
    }
470
471
    #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
472
      Transform_Sha256(); Function prototype
473
    #else
474
      Transform_Sha256() {   }
475
      int Sha256Final() {
476
         Save/Recover XMM, YMM
477
         ...
478
      }
479
    #endif
480
481
    #if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
482
        #if defined(HAVE_INTEL_RORX
483
             #define RND with rorx instruction
484
        #else
485
            #define RND
486
        #endif
487
    #endif
488
489
    #if defined(HAVE_INTEL_AVX1)
490
491
       #define XMM Instructions/inline asm
492
493
       int Transform_Sha256() {
494
           Stitched Message Sched/Round
495
        }
496
497
    #elif defined(HAVE_INTEL_AVX2)
498
499
      #define YMM Instructions/inline asm
500
501
      int Transform_Sha256() {
502
          More granular Stitched Message Sched/Round
503
      }
504
505
    #endif
506
507
    */
508
509
    /* Each platform needs to query info type 1 from cpuid to see if aesni is
510
     * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
511
     */
512
513
    /* #if defined(HAVE_INTEL_AVX1/2) at the tail of sha256 */
514
    static int Transform_Sha256(wc_Sha256* sha256, const byte* data);
515
    static int Transform_Sha256_scr(wc_Sha256* sha256, const byte* data,
516
                                    Sha256TScratch* WC_RESTRICT scr);
517
518
#ifdef __cplusplus
519
    extern "C" {
520
#endif
521
522
        extern int Transform_Sha256_SSE2_Sha(wc_Sha256 *sha256,
523
                                             const byte* data);
524
        extern int Transform_Sha256_SSE2_Sha_Len(wc_Sha256* sha256,
525
                                                 const byte* data, word32 len);
526
    #if defined(HAVE_INTEL_AVX1)
527
        extern int Transform_Sha256_AVX1_Sha(wc_Sha256 *sha256,
528
                                             const byte* data);
529
        extern int Transform_Sha256_AVX1_Sha_Len(wc_Sha256* sha256,
530
                                                 const byte* data, word32 len);
531
        extern int Transform_Sha256_AVX1(wc_Sha256 *sha256, const byte* data);
532
        extern int Transform_Sha256_AVX1_Len(wc_Sha256* sha256,
533
                                             const byte* data, word32 len);
534
    #endif
535
    #if defined(HAVE_INTEL_AVX2)
536
        extern int Transform_Sha256_AVX2(wc_Sha256 *sha256, const byte* data);
537
        extern int Transform_Sha256_AVX2_Len(wc_Sha256* sha256,
538
                                             const byte* data, word32 len);
539
        #ifdef HAVE_INTEL_RORX
540
        extern int Transform_Sha256_AVX1_RORX(wc_Sha256 *sha256, const byte* data);
541
        extern int Transform_Sha256_AVX1_RORX_Len(wc_Sha256* sha256,
542
                                                  const byte* data, word32 len);
543
        extern int Transform_Sha256_AVX2_RORX(wc_Sha256 *sha256, const byte* data);
544
        extern int Transform_Sha256_AVX2_RORX_Len(wc_Sha256* sha256,
545
                                                  const byte* data, word32 len);
546
        #endif /* HAVE_INTEL_RORX */
547
    #endif /* HAVE_INTEL_AVX2 */
548
549
#ifdef __cplusplus
550
    }  /* extern "C" */
551
#endif
552
553
    static cpuid_flags_atomic_t intel_flags = WC_CPUID_ATOMIC_INITIALIZER;
554
555
#ifdef WC_NO_INTERNAL_FUNCTION_POINTERS
556
557
    enum sha_methods { SHA256_UNSET = 0, SHA256_AVX1_SHA, SHA256_AVX2,
558
                       SHA256_AVX1_RORX, SHA256_AVX1_NOSHA, SHA256_AVX2_RORX,
559
                       SHA256_SSE2, SHA256_C };
560
561
    /* note that all write access to this static variable must be idempotent,
562
     * as arranged by Sha256_SetTransform(), else it will be susceptible to
563
     * data races.
564
     */
565
    static enum sha_methods sha_method = SHA256_UNSET;
566
567
    static void Sha256_SetTransform(void)
568
    {
569
        if (sha_method != SHA256_UNSET)
570
            return;
571
572
        /* Note, with WC_C_DYNAMIC_FALLBACK, sha_method records CPU capability
573
         * only.  Whether vector registers are actually usable is determined
574
         * independently at each transform via SAVE_VECTOR_REGISTERS2(),
575
         * allowing a context to move freely between vectorized and C transforms
576
         * call by call.
577
         */
578
579
        cpuid_get_flags_atomic(&intel_flags);
580
581
        if (IS_INTEL_SHA(intel_flags)) {
582
        #ifdef HAVE_INTEL_AVX1
583
            if (IS_INTEL_AVX1(intel_flags)) {
584
                sha_method = SHA256_AVX1_SHA;
585
            }
586
            else
587
        #endif
588
            {
589
                sha_method = SHA256_SSE2;
590
            }
591
        }
592
        else
593
    #ifdef HAVE_INTEL_AVX2
594
        if (IS_INTEL_AVX2(intel_flags)) {
595
        #ifdef HAVE_INTEL_RORX
596
            if (IS_INTEL_BMI2(intel_flags)) {
597
                sha_method = SHA256_AVX2_RORX;
598
            }
599
            else
600
        #endif
601
            {
602
                sha_method = SHA256_AVX2;
603
            }
604
        }
605
        else
606
    #endif
607
    #ifdef HAVE_INTEL_AVX1
608
        if (IS_INTEL_AVX1(intel_flags)) {
609
        #ifdef HAVE_INTEL_RORX
610
            if (IS_INTEL_BMI2(intel_flags)) {
611
                sha_method = SHA256_AVX1_RORX;
612
            }
613
            else
614
        #endif
615
            {
616
                sha_method = SHA256_AVX1_NOSHA;
617
            }
618
        }
619
        else
620
    #endif
621
        {
622
            sha_method = SHA256_C;
623
        }
624
    }
625
626
    #ifdef WC_SHA256_RAW_BE_BUFFER
627
628
    static WC_INLINE int Transform_Sha256_C_from_raw_scr(wc_Sha256* S,
629
                                                         const byte* D,
630
                                                         Sha256TScratch* scr)
631
    {
632
        /* D may be unaligned caller data: stage it in the aligned context
633
         * buffer before word-wise byte reversal, and feed the transform
634
         * from there on both endiannesses. */
635
        if (D != (const byte*)S->buffer)
636
            XMEMCPY(S->buffer, D, WC_SHA256_BLOCK_SIZE);
637
    #ifdef LITTLE_ENDIAN_ORDER
638
        ByteReverseWords(S->buffer, S->buffer, WC_SHA256_BLOCK_SIZE);
639
    #endif
640
        return Transform_Sha256_scr(S, (const byte*)S->buffer, scr);
641
    }
642
643
    static WC_INLINE int Transform_Sha256_C_from_raw(wc_Sha256* S,
644
                                                     const byte* D)
645
    {
646
        int ret;
647
        WC_SHA256_TSCRATCH_DECL(S);
648
        WC_SHA256_TSCRATCH_BEGIN(S);
649
        ret = Transform_Sha256_C_from_raw_scr(S, D, sha256TScr);
650
        WC_SHA256_TSCRATCH_WIPE(S);
651
        return ret;
652
    }
653
654
    static WC_INLINE int Transform_Sha256_Len_C_from_raw(wc_Sha256* S,
655
                                                         const byte* D,
656
                                                         word32 L)
657
    {
658
        int ret = 0;
659
        WC_SHA256_TSCRATCH_DECL(S);
660
661
        WC_SHA256_TSCRATCH_BEGIN(S);
662
        while (L >= WC_SHA256_BLOCK_SIZE) {
663
            ret = Transform_Sha256_C_from_raw_scr(S, D, sha256TScr);
664
            if (ret != 0)
665
                break;
666
            D += WC_SHA256_BLOCK_SIZE;
667
            L -= WC_SHA256_BLOCK_SIZE;
668
        }
669
        WC_SHA256_TSCRATCH_WIPE(S);
670
671
        return ret;
672
    }
673
674
    #endif /* WC_SHA256_RAW_BE_BUFFER */
675
676
    static WC_INLINE int inline_XTRANSFORM(wc_Sha256* S, const byte* D) {
677
        int ret;
678
679
    #ifdef WC_C_DYNAMIC_FALLBACK
680
        if ((sha_method == SHA256_C) ||
681
            (SAVE_VECTOR_REGISTERS2() != 0))
682
        {
683
            return Transform_Sha256_C_from_raw(S, D);
684
        }
685
    #else
686
        if (sha_method == SHA256_C) {
687
            #ifdef WC_SHA256_RAW_BE_BUFFER
688
            /* not currently reachable */
689
            return Transform_Sha256_C_from_raw(S, D);
690
            #else
691
            return Transform_Sha256(S, D);
692
            #endif
693
        }
694
        SAVE_VECTOR_REGISTERS(return _svr_ret;);
695
    #endif
696
        switch (sha_method) {
697
        case SHA256_AVX2:
698
            ret = Transform_Sha256_AVX2(S, D);
699
            break;
700
        case SHA256_AVX2_RORX:
701
            ret = Transform_Sha256_AVX2_RORX(S, D);
702
            break;
703
        case SHA256_AVX1_SHA:
704
            ret = Transform_Sha256_AVX1_Sha(S, D);
705
            break;
706
        case SHA256_AVX1_NOSHA:
707
            ret = Transform_Sha256_AVX1(S, D);
708
            break;
709
        case SHA256_AVX1_RORX:
710
            ret = Transform_Sha256_AVX1_RORX(S, D);
711
            break;
712
        case SHA256_SSE2:
713
            ret = Transform_Sha256_SSE2_Sha(S, D);
714
            break;
715
        case SHA256_C:
716
        case SHA256_UNSET:
717
        default:
718
            /* not reachable -- the C path exits above, before vector register
719
             * save -- but must stay layout-correct. */
720
            #ifdef WC_SHA256_RAW_BE_BUFFER
721
            ret = Transform_Sha256_C_from_raw(S, D);
722
            #else
723
            ret = Transform_Sha256(S, D);
724
            #endif
725
            break;
726
        }
727
        RESTORE_VECTOR_REGISTERS();
728
        return ret;
729
    }
730
#define XTRANSFORM(...) inline_XTRANSFORM(__VA_ARGS__)
731
732
    static WC_INLINE int inline_XTRANSFORM_LEN(wc_Sha256* S, const byte* D, word32 L) {
733
        int ret;
734
    #ifdef WC_C_DYNAMIC_FALLBACK
735
        if ((sha_method == SHA256_C) ||
736
            (SAVE_VECTOR_REGISTERS2() != 0))
737
        {
738
            return Transform_Sha256_Len_C_from_raw(S, D, L);
739
        }
740
    #else
741
        SAVE_VECTOR_REGISTERS(return _svr_ret;);
742
    #endif
743
        switch (sha_method) {
744
        case SHA256_AVX2:
745
            ret = Transform_Sha256_AVX2_Len(S, D, L);
746
            break;
747
        case SHA256_AVX2_RORX:
748
            ret = Transform_Sha256_AVX2_RORX_Len(S, D, L);
749
            break;
750
        case SHA256_AVX1_SHA:
751
            ret = Transform_Sha256_AVX1_Sha_Len(S, D, L);
752
            break;
753
        case SHA256_AVX1_NOSHA:
754
            ret = Transform_Sha256_AVX1_Len(S, D, L);
755
            break;
756
        case SHA256_AVX1_RORX:
757
            ret = Transform_Sha256_AVX1_RORX_Len(S, D, L);
758
            break;
759
        case SHA256_SSE2:
760
            ret = Transform_Sha256_SSE2_Sha_Len(S, D, L);
761
            break;
762
        case SHA256_C:
763
        case SHA256_UNSET:
764
        default:
765
            #ifdef WC_SHA256_RAW_BE_BUFFER
766
            ret = Transform_Sha256_Len_C_from_raw(S, D, L);
767
            #else
768
            ret = 0;
769
            #endif
770
            break;
771
        }
772
        RESTORE_VECTOR_REGISTERS();
773
        return ret;
774
    }
775
#define XTRANSFORM_LEN(...) inline_XTRANSFORM_LEN(__VA_ARGS__)
776
777
#else /* !WC_NO_INTERNAL_FUNCTION_POINTERS */
778
779
    static int (*Transform_Sha256_p)(wc_Sha256* sha256, const byte* data);
780
                                                       /* = _Transform_Sha256 */
781
    static int (*Transform_Sha256_Len_p)(wc_Sha256* sha256, const byte* data,
782
                                         word32 len);
783
                                                                    /* = NULL */
784
    static int transform_check = 0;
785
    #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
786
    static int Transform_Sha256_is_vectorized = 0;
787
    #endif
788
789
    static WC_INLINE int inline_XTRANSFORM(wc_Sha256* S, const byte* D) {
790
        int ret;
791
    #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
792
        if (Transform_Sha256_is_vectorized)
793
            SAVE_VECTOR_REGISTERS(return _svr_ret;);
794
    #endif
795
        ret = (*Transform_Sha256_p)(S, D);
796
    #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
797
        if (Transform_Sha256_is_vectorized)
798
            RESTORE_VECTOR_REGISTERS();
799
    #endif
800
        return ret;
801
    }
802
#define XTRANSFORM(...) inline_XTRANSFORM(__VA_ARGS__)
803
804
    static WC_INLINE int inline_XTRANSFORM_LEN(wc_Sha256* S, const byte* D, word32 L) {
805
        int ret;
806
    #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
807
        if (Transform_Sha256_is_vectorized)
808
            SAVE_VECTOR_REGISTERS(return _svr_ret;);
809
    #endif
810
        ret = (*Transform_Sha256_Len_p)(S, D, L);
811
    #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
812
        if (Transform_Sha256_is_vectorized)
813
            RESTORE_VECTOR_REGISTERS();
814
    #endif
815
        return ret;
816
    }
817
#define XTRANSFORM_LEN(...) inline_XTRANSFORM_LEN(__VA_ARGS__)
818
819
    static void Sha256_SetTransform(void)
820
    {
821
822
        if (transform_check)
823
            return;
824
825
        cpuid_get_flags_atomic(&intel_flags);
826
827
        if (IS_INTEL_SHA(intel_flags)) {
828
        #ifdef HAVE_INTEL_AVX1
829
            if (IS_INTEL_AVX1(intel_flags)) {
830
                Transform_Sha256_p = Transform_Sha256_AVX1_Sha;
831
                Transform_Sha256_Len_p = Transform_Sha256_AVX1_Sha_Len;
832
            #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
833
                Transform_Sha256_is_vectorized = 1;
834
            #endif
835
            }
836
            else
837
        #endif
838
            {
839
                Transform_Sha256_p = Transform_Sha256_SSE2_Sha;
840
                Transform_Sha256_Len_p = Transform_Sha256_SSE2_Sha_Len;
841
            #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
842
                Transform_Sha256_is_vectorized = 1;
843
            #endif
844
            }
845
        }
846
        else
847
    #ifdef HAVE_INTEL_AVX2
848
        if (IS_INTEL_AVX2(intel_flags)) {
849
        #ifdef HAVE_INTEL_RORX
850
            if (IS_INTEL_BMI2(intel_flags)) {
851
                Transform_Sha256_p = Transform_Sha256_AVX2_RORX;
852
                Transform_Sha256_Len_p = Transform_Sha256_AVX2_RORX_Len;
853
            #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
854
                Transform_Sha256_is_vectorized = 1;
855
            #endif
856
            }
857
            else
858
        #endif
859
            {
860
                Transform_Sha256_p = Transform_Sha256_AVX2;
861
                Transform_Sha256_Len_p = Transform_Sha256_AVX2_Len;
862
            #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
863
                Transform_Sha256_is_vectorized = 1;
864
            #endif
865
            }
866
        }
867
        else
868
    #endif
869
    #ifdef HAVE_INTEL_AVX1
870
        if (IS_INTEL_AVX1(intel_flags)) {
871
        #ifdef HAVE_INTEL_RORX
872
            if (IS_INTEL_BMI2(intel_flags)) {
873
                Transform_Sha256_p = Transform_Sha256_AVX1_RORX;
874
                Transform_Sha256_Len_p = Transform_Sha256_AVX1_RORX_Len;
875
            #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
876
                Transform_Sha256_is_vectorized = 1;
877
            #endif
878
            }
879
            else
880
        #endif
881
            {
882
                Transform_Sha256_p = Transform_Sha256_AVX1;
883
                Transform_Sha256_Len_p = Transform_Sha256_AVX1_Len;
884
            #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
885
                Transform_Sha256_is_vectorized = 1;
886
            #endif
887
            }
888
        }
889
        else
890
    #endif
891
        {
892
            Transform_Sha256_p = Transform_Sha256;
893
            Transform_Sha256_Len_p = NULL;
894
        #ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
895
            Transform_Sha256_is_vectorized = 0;
896
        #endif
897
        }
898
899
        transform_check = 1;
900
    }
901
902
#endif /* !WC_NO_INTERNAL_FUNCTION_POINTERS */
903
904
#if !defined(WOLFSSL_KCAPI_HASH)
905
    int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
906
    {
907
        int ret = 0;
908
        if (sha256 == NULL)
909
            return BAD_FUNC_ARG;
910
911
        WC_SILABS_CLEAR_HASH_CTX(sha256);
912
        sha256->heap = heap;
913
    #ifdef WOLF_CRYPTO_CB
914
        sha256->devId = devId;
915
        sha256->devCtx = NULL;
916
    #endif
917
    #ifdef WOLFSSL_SMALL_STACK_CACHE
918
        sha256->W = (word32*)XMALLOC(sizeof(Sha256TScratch),
919
                                     sha256->heap, DYNAMIC_TYPE_DIGEST);
920
        if (sha256->W == NULL)
921
            return MEMORY_E;
922
    #endif
923
924
        ret = InitSha256(sha256);
925
        if (ret != 0)
926
            return ret;
927
928
    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
929
        ret = wolfAsync_DevCtxInit(&sha256->asyncDev,
930
                            WOLFSSL_ASYNC_MARKER_SHA256, sha256->heap, devId);
931
    #else
932
        (void)devId;
933
    #endif /* WOLFSSL_ASYNC_CRYPT */
934
935
        return ret;
936
    }
937
#endif /* !WOLFSSL_KCAPI_HASH */
938
939
#elif defined(FREESCALE_LTC_SHA)
940
    int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
941
    {
942
        (void)heap;
943
        (void)devId;
944
945
        LTC_HASH_Init(LTC_BASE, &sha256->ctx, kLTC_Sha256, NULL, 0);
946
947
        return 0;
948
    }
949
950
#elif defined(FREESCALE_MMCAU_SHA)
951
952
    #ifdef FREESCALE_MMCAU_CLASSIC_SHA
953
        #include "cau_api.h"
954
    #else
955
        #include "fsl_mmcau.h"
956
    #endif
957
958
    #define XTRANSFORM(S, D)         Transform_Sha256(S, D)
959
    #define XTRANSFORM_LEN(S, D, L)  Transform_Sha256_Len(S, D, L)
960
961
    #ifndef WC_HASH_DATA_ALIGNMENT
962
        /* these hardware API's require 4 byte (word32) alignment */
963
        #define WC_HASH_DATA_ALIGNMENT 4
964
    #endif
965
966
    int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
967
    {
968
        int ret = 0;
969
970
        (void)heap;
971
        (void)devId;
972
973
        ret = wolfSSL_CryptHwMutexLock();
974
        if (ret != 0) {
975
            return ret;
976
        }
977
978
    #ifdef FREESCALE_MMCAU_CLASSIC_SHA
979
        cau_sha256_initialize_output(sha256->digest);
980
    #else
981
        MMCAU_SHA256_InitializeOutput((uint32_t*)sha256->digest);
982
    #endif
983
        wolfSSL_CryptHwMutexUnLock();
984
985
        sha256->buffLen = 0;
986
        sha256->loLen   = 0;
987
        sha256->hiLen   = 0;
988
    #ifdef WOLFSSL_SMALL_STACK_CACHE
989
        sha256->W = NULL;
990
    #endif
991
992
        return ret;
993
    }
994
995
    static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
996
    {
997
        int ret = wolfSSL_CryptHwMutexLock();
998
        if (ret == 0) {
999
    #ifdef FREESCALE_MMCAU_CLASSIC_SHA
1000
            cau_sha256_hash_n((byte*)data, 1, sha256->digest);
1001
    #else
1002
            MMCAU_SHA256_HashN((byte*)data, 1, (uint32_t*)sha256->digest);
1003
    #endif
1004
            wolfSSL_CryptHwMutexUnLock();
1005
        }
1006
        return ret;
1007
    }
1008
1009
    static int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
1010
        word32 len)
1011
    {
1012
        int ret = wolfSSL_CryptHwMutexLock();
1013
        if (ret == 0) {
1014
        #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
1015
            if ((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) {
1016
                /* data pointer is NOT aligned,
1017
                 * so copy and perform one block at a time */
1018
                byte* local = (byte*)sha256->buffer;
1019
                while (len >= WC_SHA256_BLOCK_SIZE) {
1020
                    XMEMCPY(local, data, WC_SHA256_BLOCK_SIZE);
1021
                #ifdef FREESCALE_MMCAU_CLASSIC_SHA
1022
                    cau_sha256_hash_n(local, 1, sha256->digest);
1023
                #else
1024
                    MMCAU_SHA256_HashN(local, 1, (uint32_t*)sha256->digest);
1025
                #endif
1026
                    data += WC_SHA256_BLOCK_SIZE;
1027
                    len  -= WC_SHA256_BLOCK_SIZE;
1028
                }
1029
            }
1030
            else
1031
        #endif
1032
            {
1033
    #ifdef FREESCALE_MMCAU_CLASSIC_SHA
1034
            cau_sha256_hash_n((byte*)data, len/WC_SHA256_BLOCK_SIZE,
1035
                sha256->digest);
1036
    #else
1037
            MMCAU_SHA256_HashN((byte*)data, len/WC_SHA256_BLOCK_SIZE,
1038
                (uint32_t*)sha256->digest);
1039
    #endif
1040
            }
1041
            wolfSSL_CryptHwMutexUnLock();
1042
        }
1043
        return ret;
1044
    }
1045
1046
#elif defined(WOLFSSL_PIC32MZ_HASH)
1047
    #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
1048
1049
#elif defined(STM32_HASH_SHA2)
1050
1051
    /* Supports CubeMX HAL or Standard Peripheral Library */
1052
1053
    int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
1054
    {
1055
        if (sha256 == NULL)
1056
            return BAD_FUNC_ARG;
1057
1058
        WC_SILABS_CLEAR_HASH_CTX(sha256);
1059
        (void)devId;
1060
        (void)heap;
1061
1062
        XMEMSET(sha256, 0, sizeof(wc_Sha256));
1063
        wc_Stm32_Hash_Init(&sha256->stmCtx);
1064
        return 0;
1065
    }
1066
1067
    int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
1068
    {
1069
        int ret = 0;
1070
1071
        if (sha256 == NULL) {
1072
            return BAD_FUNC_ARG;
1073
        }
1074
        if (data == NULL && len == 0) {
1075
            /* valid, but do nothing */
1076
            return 0;
1077
        }
1078
        if (data == NULL) {
1079
            return BAD_FUNC_ARG;
1080
        }
1081
1082
        ret = wolfSSL_CryptHwMutexLock();
1083
        if (ret == 0) {
1084
            ret = wc_Stm32_Hash_Update(&sha256->stmCtx,
1085
                HASH_AlgoSelection_SHA256, data, len, WC_SHA256_BLOCK_SIZE);
1086
            wolfSSL_CryptHwMutexUnLock();
1087
        }
1088
        return ret;
1089
    }
1090
1091
    int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
1092
    {
1093
        int ret = 0;
1094
1095
        if (sha256 == NULL || hash == NULL) {
1096
            return BAD_FUNC_ARG;
1097
        }
1098
1099
        ret = wolfSSL_CryptHwMutexLock();
1100
        if (ret == 0) {
1101
            ret = wc_Stm32_Hash_Final(&sha256->stmCtx,
1102
                HASH_AlgoSelection_SHA256, hash, WC_SHA256_DIGEST_SIZE);
1103
            wolfSSL_CryptHwMutexUnLock();
1104
        }
1105
1106
        (void)wc_InitSha256(sha256); /* reset state */
1107
1108
        return ret;
1109
    }
1110
1111
#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
1112
    !defined(WOLFSSL_QNX_CAAM)
1113
    /* functions defined in wolfcrypt/src/port/caam/caam_sha256.c */
1114
1115
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
1116
1117
    int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
1118
    {
1119
        if (sha256 == NULL) {
1120
            return BAD_FUNC_ARG;
1121
        }
1122
        WC_SILABS_CLEAR_HASH_CTX(sha256);
1123
        (void)devId;
1124
1125
        return se050_hash_init(&sha256->se050Ctx, heap);
1126
    }
1127
1128
    int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
1129
    {
1130
        if (sha256 == NULL) {
1131
            return BAD_FUNC_ARG;
1132
        }
1133
        if (data == NULL && len == 0) {
1134
            /* valid, but do nothing */
1135
            return 0;
1136
        }
1137
        if (data == NULL) {
1138
            return BAD_FUNC_ARG;
1139
        }
1140
1141
        return se050_hash_update(&sha256->se050Ctx, data, len);
1142
    }
1143
1144
    int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
1145
    {
1146
        int ret = 0;
1147
        ret = se050_hash_final(&sha256->se050Ctx, hash, WC_SHA256_DIGEST_SIZE,
1148
                               kAlgorithm_SSS_SHA256);
1149
        return ret;
1150
    }
1151
1152
#elif defined(WOLFSSL_AFALG_HASH)
1153
    /* implemented in wolfcrypt/src/port/af_alg/afalg_hash.c */
1154
1155
#elif defined(WOLFSSL_DEVCRYPTO_HASH)
1156
    /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
1157
1158
#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_HASH)
1159
    #include "hal_data.h"
1160
1161
    #ifndef WOLFSSL_SCE_SHA256_HANDLE
1162
        #define WOLFSSL_SCE_SHA256_HANDLE g_sce_hash_0
1163
    #endif
1164
1165
    #define WC_SHA256_DIGEST_WORD_SIZE 16
1166
    #define XTRANSFORM(S, D) wc_Sha256SCE_XTRANSFORM(S, D)
1167
    static int wc_Sha256SCE_XTRANSFORM(wc_Sha256* sha256, const byte* data)
1168
    {
1169
        if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
1170
                CRYPTO_WORD_ENDIAN_LITTLE)
1171
        {
1172
            ByteReverseWords((word32*)data, (word32*)data,
1173
                    WC_SHA256_BLOCK_SIZE);
1174
            ByteReverseWords(sha256->digest, sha256->digest,
1175
                    WC_SHA256_DIGEST_SIZE);
1176
        }
1177
1178
        if (WOLFSSL_SCE_SHA256_HANDLE.p_api->hashUpdate(
1179
                    WOLFSSL_SCE_SHA256_HANDLE.p_ctrl, (word32*)data,
1180
                    WC_SHA256_DIGEST_WORD_SIZE, sha256->digest) != SSP_SUCCESS){
1181
            WOLFSSL_MSG("Unexpected hardware return value");
1182
            return WC_HW_E;
1183
        }
1184
1185
        if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
1186
                CRYPTO_WORD_ENDIAN_LITTLE)
1187
        {
1188
            ByteReverseWords((word32*)data, (word32*)data,
1189
                    WC_SHA256_BLOCK_SIZE);
1190
            ByteReverseWords(sha256->digest, sha256->digest,
1191
                    WC_SHA256_DIGEST_SIZE);
1192
        }
1193
1194
        return 0;
1195
    }
1196
1197
1198
    int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
1199
    {
1200
        int ret = 0;
1201
        if (sha256 == NULL)
1202
            return BAD_FUNC_ARG;
1203
1204
        WC_SILABS_CLEAR_HASH_CTX(sha256);
1205
        sha256->heap = heap;
1206
1207
        ret = InitSha256(sha256);
1208
        if (ret != 0)
1209
            return ret;
1210
1211
        (void)devId;
1212
1213
        return ret;
1214
    }
1215
1216
#elif defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
1217
1218
    /* HW may fail since there's only one, so we still need SW */
1219
    #define NEED_SOFT_SHA256
1220
1221
    /*
1222
    ** An Espressif-specific InitSha256()
1223
    **
1224
    ** soft SHA needs initialization digest, but HW does not.
1225
    */
1226
    static int InitSha256(wc_Sha256* sha256)
1227
    {
1228
        int ret = 0; /* zero = success */
1229
1230
        /* We may or may not need initial digest for HW.
1231
         * Always needed for SW-only. */
1232
        sha256->digest[0] = 0x6A09E667L;
1233
        sha256->digest[1] = 0xBB67AE85L;
1234
        sha256->digest[2] = 0x3C6EF372L;
1235
        sha256->digest[3] = 0xA54FF53AL;
1236
        sha256->digest[4] = 0x510E527FL;
1237
        sha256->digest[5] = 0x9B05688CL;
1238
        sha256->digest[6] = 0x1F83D9ABL;
1239
        sha256->digest[7] = 0x5BE0CD19L;
1240
1241
        sha256->buffLen = 0;
1242
        sha256->loLen   = 0;
1243
        sha256->hiLen   = 0;
1244
1245
#ifndef NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256
1246
        ret = esp_sha_init((WC_ESP32SHA*)&(sha256->ctx), WC_HASH_TYPE_SHA256);
1247
#endif
1248
        return ret;
1249
    }
1250
1251
    /*
1252
    ** An Espressif-specific wolfCrypt InitSha256 external wrapper.
1253
    **
1254
    ** we'll assume this is ALWAYS for a new, uninitialized sha256
1255
    */
1256
    int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
1257
    {
1258
        (void)devId;
1259
        if (sha256 == NULL) {
1260
            return BAD_FUNC_ARG;
1261
        }
1262
1263
        WC_SILABS_CLEAR_HASH_CTX(sha256);
1264
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
1265
       !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
1266
        /* We know this is a fresh, uninitialized item, so set to INIT */
1267
        if (sha256->ctx.mode != ESP32_SHA_INIT) {
1268
            ESP_LOGV(TAG, "Set ctx mode from prior value: "
1269
                               "%d", sha256->ctx.mode);
1270
        }
1271
        sha256->ctx.mode = ESP32_SHA_INIT;
1272
    #endif
1273
1274
        return InitSha256(sha256);
1275
    }
1276
1277
#elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
1278
       defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
1279
    !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
1280
1281
    /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
1282
1283
#elif (defined(WOLFSSL_RENESAS_SCEPROTECT) || defined(WOLFSSL_RENESAS_RSIP)) \
1284
     && !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
1285
1286
    /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
1287
1288
#elif defined(PSOC6_HASH_SHA2)
1289
    /* implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
1290
1291
#elif defined(WOLFSSL_IMXRT_DCP)
1292
    #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
1293
    /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */
1294
1295
#elif defined(WOLFSSL_NXP_HASHCRYPT_SHA)
1296
    /* implemented in wolfcrypt/src/port/nxp/hashcrypt_port.c */
1297
1298
#elif defined(WOLFSSL_SILABS_SE_ACCEL)
1299
    /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
1300
1301
#elif defined(WOLFSSL_KCAPI_HASH)
1302
    /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
1303
1304
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
1305
    /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
1306
1307
#elif defined(WOLFSSL_RENESAS_RX64_HASH)
1308
1309
    /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
1310
#elif (defined(WOLFSSL_PPC32_ASM) || defined(WOLFSSL_PPC64_ASM)) && \
1311
    !defined(WOLF_CRYPTO_CB_ONLY_SHA256)
1312
1313
extern void Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
1314
    word32 len);
1315
1316
#if defined(WOLFSSL_PPC64_ASM) && defined(WOLFSSL_PPC64_ASM_CRYPTO)
1317
/* POWER8+ has a vector SHA-256 sigma instruction (vshasigmaw).  When built
1318
 * in, select that implementation at run time if the CPU supports it.
1319
 *
1320
 * A run-time flag with direct calls is used rather than a function pointer:
1321
 * an indirect call would require an ELFv1 function descriptor, whereas direct
1322
 * calls work under both the ELFv1 and ELFv2 ABIs. */
1323
extern void Transform_Sha256_Len_crypto(wc_Sha256* sha256, const byte* data,
1324
    word32 len);
1325
1326
/* -1 = not yet determined, 0 = base, 1 = vector-crypto */
1327
/* Resolved dispatch decision (0 = base, 1 = vector-crypto), accessed with the
1328
 * wolfSSL atomic APIs so the one-time detection is free of data races.  The
1329
 * write is idempotent (all callers compute the same value from the atomic
1330
 * master flags), so a benign concurrent double-write is harmless. */
1331
static wolfSSL_Atomic_Uint sha256_use_crypto = WOLFSSL_ATOMIC_INITIALIZER(0);
1332
1333
/* Detect CPU support via the central cpuid module. */
1334
static void Sha256_SetTransform(void)
1335
{
1336
    WOLFSSL_ATOMIC_STORE(sha256_use_crypto,
1337
        (unsigned int)(IS_PPC64_VEC_CRYPTO(cpuid_get_flags()) != 0));
1338
}
1339
1340
static WC_INLINE int SHA256_TRANSFORM_LEN(wc_Sha256* sha256, const byte* data,
1341
    word32 len)
1342
{
1343
    if (WOLFSSL_ATOMIC_LOAD(sha256_use_crypto))
1344
        Transform_Sha256_Len_crypto(sha256, data, len);
1345
    else
1346
        Transform_Sha256_Len(sha256, data, len);
1347
    return 0;
1348
}
1349
#else
1350
#define Sha256_SetTransform()           WC_DO_NOTHING
1351
static WC_INLINE int SHA256_TRANSFORM_LEN(wc_Sha256* sha256, const byte* data,
1352
    word32 len)
1353
{
1354
    Transform_Sha256_Len(sha256, data, len);
1355
    return 0;
1356
}
1357
#endif
1358
1359
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
1360
{
1361
    int ret = 0;
1362
1363
    if (sha256 == NULL)
1364
        return BAD_FUNC_ARG;
1365
    WC_SILABS_CLEAR_HASH_CTX(sha256);
1366
    ret = InitSha256(sha256);
1367
    if (ret != 0)
1368
        return ret;
1369
1370
    Sha256_SetTransform();
1371
1372
    sha256->heap = heap;
1373
#ifdef WOLF_CRYPTO_CB
1374
    sha256->devId = devId;
1375
    sha256->devCtx = NULL;
1376
#else
1377
    (void)devId;
1378
#endif
1379
1380
#ifdef WOLFSSL_SMALL_STACK_CACHE
1381
    sha256->W = NULL;
1382
#endif
1383
1384
    return ret;
1385
}
1386
1387
static int Transform_Sha256(wc_Sha256* sha256, const byte* data)
1388
{
1389
    SHA256_TRANSFORM_LEN(sha256, data, WC_SHA256_BLOCK_SIZE);
1390
    return 0;
1391
}
1392
1393
#define XTRANSFORM Transform_Sha256
1394
#define XTRANSFORM_LEN(s, d, l)         SHA256_TRANSFORM_LEN(s, d, l)
1395
1396
#elif defined(WOLFSSL_ARMASM) && defined(__aarch64__) && \
1397
      !defined(WOLF_CRYPTO_CB_ONLY_SHA256)
1398
1399
/* This arm of the chain provides Sha256_SetTransform() - marks it available to
1400
 * the SHA-224 initializer, which shares the SHA-256 transform.  Earlier arms
1401
 * (hardware hash ports) win the chain and provide no such selection. */
1402
#define WOLFSSL_ARMASM_SHA256_TRANSFORM
1403
1404
static int transform_check = 0;
1405
static cpuid_flags_atomic_t cpuid_flags = WC_CPUID_ATOMIC_INITIALIZER;
1406
1407
static int Transform_Sha256(wc_Sha256* sha256, const byte* data);
1408
static int Transform_Sha256_scr(wc_Sha256* sha256, const byte* data,
1409
                                Sha256TScratch* WC_RESTRICT scr);
1410
static int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
1411
     word32 len);
1412
1413
/* Initialize to the software fallback so the pointer is never NULL if it is
1414
 * read before Sha256_SetTransform() has published the selected variant. */
1415
static int (*Transform_Sha256_Len_p)(wc_Sha256* sha256, const byte* data,
1416
     word32 len) = Transform_Sha256_Len;
1417
1418
static WC_INLINE int Transform_Sha256_aarch64(wc_Sha256* sha256,
1419
     const byte* data)
1420
{
1421
    return (*Transform_Sha256_Len_p)(sha256, data, WC_SHA256_BLOCK_SIZE);
1422
}
1423
1424
static WC_INLINE int Transform_Sha256_Len_aarch64(wc_Sha256* sha256,
1425
    const byte* data, word32 len)
1426
{
1427
    return (*Transform_Sha256_Len_p)(sha256, data, len);
1428
}
1429
1430
/* Both transforms below run on v0-v31 and save d8-d15
1431
 * (port/arm/armv8-sha256-asm.S), so a kernel module must bracket them. */
1432
#if defined(__aarch64__) && defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS)
1433
    #define WC_SHA256_ARM64_SVR_BEGIN()                                     \
1434
        do { int _svr_ret = SAVE_VECTOR_REGISTERS2();                       \
1435
             if (_svr_ret != 0) return _svr_ret; } while (0)
1436
    #define WC_SHA256_ARM64_SVR_END()  RESTORE_VECTOR_REGISTERS()
1437
#else
1438
    #define WC_SHA256_ARM64_SVR_BEGIN() WC_DO_NOTHING
1439
    #define WC_SHA256_ARM64_SVR_END()   WC_DO_NOTHING
1440
#endif
1441
1442
#if !defined(WOLFSSL_ARMASM_NO_NEON)
1443
#if !defined(WOLFSSL_ARMASM_NO_HW_CRYPTO)
1444
static int Transform_Sha256_Len_crypto_aarch64(wc_Sha256* sha256,
1445
    const byte* data, word32 len)
1446
{
1447
    WC_SHA256_ARM64_SVR_BEGIN();
1448
    Transform_Sha256_Len_crypto(sha256, data, len);
1449
    WC_SHA256_ARM64_SVR_END();
1450
    return 0;
1451
}
1452
#endif
1453
1454
static int Transform_Sha256_Len_neon_aarch64(wc_Sha256* sha256,
1455
    const byte* data, word32 len)
1456
{
1457
    WC_SHA256_ARM64_SVR_BEGIN();
1458
    Transform_Sha256_Len_neon(sha256, data, len);
1459
    WC_SHA256_ARM64_SVR_END();
1460
    return 0;
1461
}
1462
#endif
1463
1464
static int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
1465
    word32 len)
1466
{
1467
    int ret = 0;
1468
    word32 buffer[WC_SHA256_BLOCK_SIZE / sizeof(word32)];
1469
    WC_SHA256_TSCRATCH_DECL(sha256);
1470
1471
    WC_SHA256_TSCRATCH_BEGIN(sha256);
1472
#ifdef WOLFSSL_CHECK_MEM_ZERO
1473
    wc_MemZero_Add("Transform_Sha256_Len buffer", buffer, sizeof(buffer));
1474
#endif
1475
    while (len >= WC_SHA256_BLOCK_SIZE) {
1476
        XMEMCPY(buffer, data, WC_SHA256_BLOCK_SIZE);
1477
    #ifdef LITTLE_ENDIAN_ORDER
1478
        ByteReverseWords(buffer, buffer, WC_SHA256_BLOCK_SIZE);
1479
    #endif
1480
        ret = Transform_Sha256_scr(sha256, (const byte*)buffer, sha256TScr);
1481
        if (ret != 0)
1482
            break;
1483
        data += WC_SHA256_BLOCK_SIZE;
1484
        len  -= WC_SHA256_BLOCK_SIZE;
1485
    }
1486
    WC_SHA256_TSCRATCH_WIPE(sha256);
1487
    ForceZero(buffer, sizeof(buffer));
1488
#ifdef WOLFSSL_CHECK_MEM_ZERO
1489
    wc_MemZero_Check(buffer, sizeof(buffer));
1490
#endif
1491
1492
    return ret;
1493
}
1494
1495
static void Sha256_SetTransform(void)
1496
{
1497
    if (transform_check)
1498
        return;
1499
1500
    cpuid_get_flags_atomic(&cpuid_flags);
1501
1502
#if !defined(WOLFSSL_ARMASM_NO_NEON)
1503
#if !defined(WOLFSSL_ARMASM_NO_HW_CRYPTO)
1504
    if (IS_AARCH64_SHA256(cpuid_flags)) {
1505
        Transform_Sha256_Len_p = Transform_Sha256_Len_crypto_aarch64;
1506
    }
1507
    else
1508
#endif
1509
    if (IS_AARCH64_ASIMD(cpuid_flags)) {
1510
        Transform_Sha256_Len_p = Transform_Sha256_Len_neon_aarch64;
1511
    }
1512
    else
1513
#endif
1514
    {
1515
        Transform_Sha256_Len_p = Transform_Sha256_Len;
1516
    }
1517
1518
    transform_check = 1;
1519
}
1520
1521
#define XTRANSFORM      Transform_Sha256_aarch64
1522
#define XTRANSFORM_LEN  Transform_Sha256_Len_aarch64
1523
1524
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
1525
{
1526
    int ret = 0;
1527
1528
    if (sha256 == NULL)
1529
        return BAD_FUNC_ARG;
1530
    WC_SILABS_CLEAR_HASH_CTX(sha256);
1531
    ret = InitSha256(sha256);
1532
    if (ret != 0)
1533
        return ret;
1534
1535
    sha256->heap = heap;
1536
#ifdef WOLF_CRYPTO_CB
1537
    sha256->devId = devId;
1538
    sha256->devCtx = NULL;
1539
#else
1540
    (void)devId;
1541
#endif
1542
1543
#ifdef WOLFSSL_SMALL_STACK_CACHE
1544
    sha256->W = (word32*)XMALLOC(sizeof(Sha256TScratch),
1545
                                 sha256->heap, DYNAMIC_TYPE_DIGEST);
1546
    if (sha256->W == NULL)
1547
        return MEMORY_E;
1548
#endif
1549
1550
    return ret;
1551
}
1552
1553
#define NEED_SOFT_SHA256
1554
1555
#elif defined(WOLFSSL_ARMASM) && !defined(WOLF_CRYPTO_CB_ONLY_SHA256)
1556
1557
/* As in the AArch64 arm above: this arm provides Sha256_SetTransform(). */
1558
#define WOLFSSL_ARMASM_SHA256_TRANSFORM
1559
1560
/* On 32-bit Arm a NEON build compiles in the base and NEON and (unless
1561
 * disabled) the Armv8 crypto-extension block transforms, so the best supported
1562
 * one is chosen at run time - mirroring the AArch64 path.  Thumb-2, no-NEON,
1563
 * no-crypto (NO_HW_CRYPTO) and crypto-only (NO_NEON_IMPL) builds compile a
1564
 * single variant and call it directly, as does a build with no run-time
1565
 * detection to dispatch on (HAVE_CPUID_ARM32). */
1566
#if !defined(WOLFSSL_ARMASM_THUMB2) && !defined(WOLFSSL_ARMASM_NO_NEON) && \
1567
    !defined(WOLFSSL_ARMASM_NO_HW_CRYPTO) && \
1568
    !(defined(WOLFSSL_ARMASM_NO_NEON_IMPL) && \
1569
      defined(WOLFSSL_ARMASM_NO_BASE_IMPL)) && defined(HAVE_CPUID_ARM32)
1570
    #define SHA256_ARM32_DISPATCH
1571
#endif
1572
1573
/* The crypto and NEON transforms run on the q registers, so a kernel module
1574
 * must hold the vector registers around them; the base transform needs none. */
1575
#if !defined(WOLFSSL_ARMASM_THUMB2) && !defined(WOLFSSL_ARMASM_NO_NEON)
1576
#ifdef WOLFSSL_USE_SAVE_VECTOR_REGISTERS
1577
    #define WC_SHA256_ARM32_SVR_BEGIN()                                     \
1578
        do { int _svr_ret = SAVE_VECTOR_REGISTERS2();                       \
1579
             if (_svr_ret != 0) return _svr_ret; } while (0)
1580
    #define WC_SHA256_ARM32_SVR_END()  RESTORE_VECTOR_REGISTERS()
1581
#else
1582
    #define WC_SHA256_ARM32_SVR_BEGIN() WC_DO_NOTHING
1583
    #define WC_SHA256_ARM32_SVR_END()   WC_DO_NOTHING
1584
#endif
1585
#ifndef WOLFSSL_ARMASM_NO_HW_CRYPTO
1586
static WC_INLINE int Transform_Sha256_Len_crypto_arm32(wc_Sha256* sha256,
1587
    const byte* data, word32 len)
1588
{
1589
    WC_SHA256_ARM32_SVR_BEGIN();
1590
    Transform_Sha256_Len_crypto(sha256, data, len);
1591
    WC_SHA256_ARM32_SVR_END();
1592
    return 0;
1593
}
1594
#endif
1595
/* Guarded exactly as armv8-32-sha256-asm_c.c guards the assembly it calls, so
1596
 * a wrapper exists only where its implementation does. */
1597
#if !defined(WOLFSSL_ARMASM_NO_NEON_IMPL) || defined(WOLFSSL_ARMASM_NO_HW_CRYPTO)
1598
static WC_INLINE int Transform_Sha256_Len_neon_arm32(wc_Sha256* sha256,
1599
    const byte* data, word32 len)
1600
{
1601
    WC_SHA256_ARM32_SVR_BEGIN();
1602
    Transform_Sha256_Len_neon(sha256, data, len);
1603
    WC_SHA256_ARM32_SVR_END();
1604
    return 0;
1605
}
1606
#endif
1607
#endif
1608
#if !defined(WOLFSSL_ARMASM_NO_BASE_IMPL) || defined(WOLFSSL_ARMASM_NO_NEON)
1609
static WC_INLINE int Transform_Sha256_Len_base_arm32(wc_Sha256* sha256,
1610
    const byte* data, word32 len)
1611
{
1612
    Transform_Sha256_Len_base(sha256, data, len);
1613
    return 0;
1614
}
1615
#endif
1616
1617
#ifdef SHA256_ARM32_DISPATCH
1618
1619
static int sha256_transform_check = 0;
1620
static cpuid_flags_atomic_t sha256_cpuid_flags = WC_CPUID_ATOMIC_INITIALIZER;
1621
1622
/* Initialize to the transform that needs the least of the CPU - the base one
1623
 * requires no extension at all - so the pointer is safe to use even if read
1624
 * before Sha256_SetTransform() runs. */
1625
#ifndef WOLFSSL_ARMASM_NO_BASE_IMPL
1626
    #define SHA256_ARM32_TRANSFORM_INIT     Transform_Sha256_Len_base_arm32
1627
#else
1628
    #define SHA256_ARM32_TRANSFORM_INIT     Transform_Sha256_Len_neon_arm32
1629
#endif
1630
1631
static int (*Transform_Sha256_Len_p)(wc_Sha256* sha256, const byte* data,
1632
    word32 len) = SHA256_ARM32_TRANSFORM_INIT;
1633
1634
/* Select the crypto-extension transform when the CPU implements FEAT_SHA256,
1635
 * otherwise the best fallback this build kept: NEON when the CPU implements
1636
 * Advanced SIMD, else the base transform.  Either fallback can be dropped
1637
 * (WOLFSSL_ARMASM_NO_NEON_IMPL / WOLFSSL_ARMASM_NO_BASE_IMPL) - dropping both
1638
 * is what turns dispatch off above. */
1639
static void Sha256_SetTransform(void)
1640
{
1641
    if (sha256_transform_check)
1642
        return;
1643
1644
    cpuid_get_flags_atomic(&sha256_cpuid_flags);
1645
1646
    if (IS_ARM32_SHA256(sha256_cpuid_flags)) {
1647
        Transform_Sha256_Len_p = Transform_Sha256_Len_crypto_arm32;
1648
    }
1649
#if !defined(WOLFSSL_ARMASM_NO_NEON_IMPL) && \
1650
    !defined(WOLFSSL_ARMASM_NO_BASE_IMPL)
1651
    else if (IS_ARM32_ASIMD(sha256_cpuid_flags)) {
1652
        Transform_Sha256_Len_p = Transform_Sha256_Len_neon_arm32;
1653
    }
1654
    else {
1655
        Transform_Sha256_Len_p = Transform_Sha256_Len_base_arm32;
1656
    }
1657
#elif !defined(WOLFSSL_ARMASM_NO_NEON_IMPL)
1658
    /* Base dropped - a NEON build always implements Advanced SIMD. */
1659
    else {
1660
        Transform_Sha256_Len_p = Transform_Sha256_Len_neon_arm32;
1661
    }
1662
#else
1663
    /* NEON implementation dropped - the base transform needs no extension. */
1664
    else {
1665
        Transform_Sha256_Len_p = Transform_Sha256_Len_base_arm32;
1666
    }
1667
#endif
1668
1669
    sha256_transform_check = 1;
1670
}
1671
1672
#else
1673
#define Sha256_SetTransform()   WC_DO_NOTHING
1674
#endif /* SHA256_ARM32_DISPATCH */
1675
1676
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
1677
{
1678
    int ret = 0;
1679
1680
    if (sha256 == NULL)
1681
        return BAD_FUNC_ARG;
1682
    WC_SILABS_CLEAR_HASH_CTX(sha256);
1683
    ret = InitSha256(sha256);
1684
    if (ret != 0)
1685
        return ret;
1686
1687
    Sha256_SetTransform();
1688
1689
    sha256->heap = heap;
1690
#ifdef WOLF_CRYPTO_CB
1691
    sha256->devId = devId;
1692
    sha256->devCtx = NULL;
1693
#else
1694
    (void)devId;
1695
#endif
1696
1697
    #ifdef WOLFSSL_SMALL_STACK_CACHE
1698
    sha256->W = NULL;
1699
    #endif
1700
1701
    return ret;
1702
}
1703
1704
/* Call the transform selected at run time, or - when only one variant is
1705
 * compiled in - the single one this build has.  The base transform is the
1706
 * choice for Thumb-2 and no-NEON builds, NEON when the crypto extension is off,
1707
 * and otherwise the crypto-extension transform the build was configured for. */
1708
static WC_INLINE int Transform_Sha256(wc_Sha256* sha256, const byte* data)
1709
{
1710
#ifdef SHA256_ARM32_DISPATCH
1711
    return (*Transform_Sha256_Len_p)(sha256, data, WC_SHA256_BLOCK_SIZE);
1712
#elif defined(WOLFSSL_ARMASM_THUMB2) || defined(WOLFSSL_ARMASM_NO_NEON)
1713
    return Transform_Sha256_Len_base_arm32(sha256, data, WC_SHA256_BLOCK_SIZE);
1714
#elif defined(WOLFSSL_ARMASM_NO_HW_CRYPTO)
1715
    return Transform_Sha256_Len_neon_arm32(sha256, data, WC_SHA256_BLOCK_SIZE);
1716
#else
1717
    return Transform_Sha256_Len_crypto_arm32(sha256, data,
1718
        WC_SHA256_BLOCK_SIZE);
1719
#endif
1720
}
1721
1722
/* Multi-block form of Transform_Sha256() - see there for the selection. */
1723
static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
1724
    word32 len)
1725
{
1726
#ifdef SHA256_ARM32_DISPATCH
1727
    return (*Transform_Sha256_Len_p)(sha256, data, len);
1728
#elif defined(WOLFSSL_ARMASM_THUMB2) || defined(WOLFSSL_ARMASM_NO_NEON)
1729
    return Transform_Sha256_Len_base_arm32(sha256, data, len);
1730
#elif defined(WOLFSSL_ARMASM_NO_HW_CRYPTO)
1731
    return Transform_Sha256_Len_neon_arm32(sha256, data, len);
1732
#else
1733
    return Transform_Sha256_Len_crypto_arm32(sha256, data, len);
1734
#endif
1735
}
1736
1737
#define XTRANSFORM      Transform_Sha256
1738
#define XTRANSFORM_LEN  Transform_Sha256_Len
1739
1740
#elif defined(WOLFSSL_RISCV_ASM) && !defined(WOLF_CRYPTO_CB_ONLY_SHA256)
1741
1742
int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
1743
{
1744
    int ret = 0;
1745
1746
    if (sha256 == NULL)
1747
        return BAD_FUNC_ARG;
1748
    WC_SILABS_CLEAR_HASH_CTX(sha256);
1749
    ret = InitSha256(sha256);
1750
    if (ret != 0)
1751
        return ret;
1752
1753
    sha256->heap = heap;
1754
#ifdef WOLF_CRYPTO_CB
1755
    sha256->devId = devId;
1756
    sha256->devCtx = NULL;
1757
#else
1758
    (void)devId;
1759
#endif
1760
1761
#ifdef WOLFSSL_SMALL_STACK_CACHE
1762
    sha256->W = NULL;
1763
#endif
1764
1765
    return ret;
1766
}
1767
1768
static WC_INLINE int Transform_Sha256(wc_Sha256* sha256, const byte* data)
1769
{
1770
#if defined(WOLFSSL_RISCV_VECTOR_CRYPTO_ASM)
1771
    Transform_Sha256_Len_riscv_vector(sha256, data, WC_SHA256_BLOCK_SIZE);
1772
#elif defined(WOLFSSL_RISCV_SCALAR_CRYPTO_ASM)
1773
    Transform_Sha256_Len_riscv_crypto(sha256, data, WC_SHA256_BLOCK_SIZE);
1774
#else
1775
    Transform_Sha256_Len_riscv(sha256, data, WC_SHA256_BLOCK_SIZE);
1776
#endif
1777
    return 0;
1778
}
1779
1780
static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
1781
    word32 len)
1782
{
1783
#if defined(WOLFSSL_RISCV_VECTOR_CRYPTO_ASM)
1784
    Transform_Sha256_Len_riscv_vector(sha256, data, len);
1785
#elif defined(WOLFSSL_RISCV_SCALAR_CRYPTO_ASM)
1786
    Transform_Sha256_Len_riscv_crypto(sha256, data, len);
1787
#else
1788
    Transform_Sha256_Len_riscv(sha256, data, len);
1789
#endif
1790
    return 0;
1791
}
1792
#define XTRANSFORM      Transform_Sha256
1793
#define XTRANSFORM_LEN  Transform_Sha256_Len
1794
1795
#elif defined(WOLF_CRYPTO_CB_ONLY_SHA256)
1796
    /* Software SHA-256 stripped; every op dispatches via cryptocb. */
1797
    int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
1798
    {
1799
        int ret;
1800
        if (sha256 == NULL)
1801
            return BAD_FUNC_ARG;
1802
        WC_SILABS_CLEAR_HASH_CTX(sha256);
1803
        ret = InitSha256(sha256);
1804
        if (ret != 0)
1805
            return ret;
1806
        sha256->heap   = heap;
1807
        sha256->devId  = devId;
1808
        sha256->devCtx = NULL;
1809
        return ret;
1810
    }
1811
#else
1812
    #define NEED_SOFT_SHA256
1813
1814
    int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
1815
102k
    {
1816
102k
        int ret = 0;
1817
102k
        if (sha256 == NULL)
1818
0
            return BAD_FUNC_ARG;
1819
102k
        WC_SILABS_CLEAR_HASH_CTX(sha256);
1820
102k
        ret = InitSha256(sha256);
1821
102k
        if (ret != 0)
1822
0
            return ret;
1823
1824
102k
        sha256->heap = heap;
1825
102k
    #ifdef WOLF_CRYPTO_CB
1826
102k
        sha256->devId = devId;
1827
102k
        sha256->devCtx = NULL;
1828
102k
    #endif
1829
    #ifdef WOLFSSL_SMALL_STACK_CACHE
1830
        sha256->W = (word32*)XMALLOC(sizeof(Sha256TScratch),
1831
                                     sha256->heap, DYNAMIC_TYPE_DIGEST);
1832
        if (sha256->W == NULL)
1833
            return MEMORY_E;
1834
    #endif
1835
1836
    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
1837
        ret = wolfAsync_DevCtxInit(&sha256->asyncDev,
1838
                            WOLFSSL_ASYNC_MARKER_SHA256, sha256->heap, devId);
1839
    #else
1840
102k
        (void)devId;
1841
102k
    #endif /* WOLFSSL_ASYNC_CRYPT */
1842
    #ifdef WOLFSSL_IMXRT1170_CAAM
1843
         ret = wc_CAAM_HashInit(&sha256->hndl, &sha256->ctx, WC_HASH_TYPE_SHA256);
1844
    #endif
1845
1846
102k
        return ret;
1847
102k
    }
1848
#endif /* End Hardware Acceleration */
1849
1850
#ifdef NEED_SOFT_SHA256
1851
1852
    static const FLASH_QUALIFIER ALIGN32 word32 K[64] = {
1853
        0x428A2F98L, 0x71374491L, 0xB5C0FBCFL, 0xE9B5DBA5L, 0x3956C25BL,
1854
        0x59F111F1L, 0x923F82A4L, 0xAB1C5ED5L, 0xD807AA98L, 0x12835B01L,
1855
        0x243185BEL, 0x550C7DC3L, 0x72BE5D74L, 0x80DEB1FEL, 0x9BDC06A7L,
1856
        0xC19BF174L, 0xE49B69C1L, 0xEFBE4786L, 0x0FC19DC6L, 0x240CA1CCL,
1857
        0x2DE92C6FL, 0x4A7484AAL, 0x5CB0A9DCL, 0x76F988DAL, 0x983E5152L,
1858
        0xA831C66DL, 0xB00327C8L, 0xBF597FC7L, 0xC6E00BF3L, 0xD5A79147L,
1859
        0x06CA6351L, 0x14292967L, 0x27B70A85L, 0x2E1B2138L, 0x4D2C6DFCL,
1860
        0x53380D13L, 0x650A7354L, 0x766A0ABBL, 0x81C2C92EL, 0x92722C85L,
1861
        0xA2BFE8A1L, 0xA81A664BL, 0xC24B8B70L, 0xC76C51A3L, 0xD192E819L,
1862
        0xD6990624L, 0xF40E3585L, 0x106AA070L, 0x19A4C116L, 0x1E376C08L,
1863
        0x2748774CL, 0x34B0BCB5L, 0x391C0CB3L, 0x4ED8AA4AL, 0x5B9CCA4FL,
1864
        0x682E6FF3L, 0x748F82EEL, 0x78A5636FL, 0x84C87814L, 0x8CC70208L,
1865
        0x90BEFFFAL, 0xA4506CEBL, 0xBEF9A3F7L, 0xC67178F2L
1866
    };
1867
1868
/* Both versions of Ch and Maj are logically the same, but with the second set
1869
    the compilers can recognize them better for optimization */
1870
#ifdef WOLFSSL_SHA256_BY_SPEC
1871
    /* SHA256 math based on specification */
1872
42.7M
    #define Ch(x,y,z)       ((z) ^ ((x) & ((y) ^ (z))))
1873
42.7M
    #define Maj(x,y,z)      ((((x) | (y)) & (z)) | ((x) & (y)))
1874
#else
1875
    /* SHA256 math reworked for easier compiler optimization */
1876
    #define Ch(x,y,z)       ((((y) ^ (z)) & (x)) ^ (z))
1877
    #define Maj(x,y,z)      ((((x) ^ (y)) & ((y) ^ (z))) ^ (y))
1878
#endif
1879
64.1M
    #define R(x, n)         (((x) & 0xFFFFFFFFU) >> (n))
1880
1881
384M
    #define S(x, n)         rotrFixed(x, n)
1882
42.7M
    #define Sigma0(x)       (S(x, 2)  ^ S(x, 13) ^ S(x, 22))
1883
42.7M
    #define Sigma1(x)       (S(x, 6)  ^ S(x, 11) ^ S(x, 25))
1884
32.0M
    #define Gamma0(x)       (S(x, 7)  ^ S(x, 18) ^ R(x, 3))
1885
32.0M
    #define Gamma1(x)       (S(x, 17) ^ S(x, 19) ^ R(x, 10))
1886
1887
    #define a(i) S[(0-(i)) & 7]
1888
    #define b(i) S[(1-(i)) & 7]
1889
    #define c(i) S[(2-(i)) & 7]
1890
42.7M
    #define d(i) S[(3-(i)) & 7]
1891
    #define e(i) S[(4-(i)) & 7]
1892
    #define f(i) S[(5-(i)) & 7]
1893
    #define g(i) S[(6-(i)) & 7]
1894
85.5M
    #define h(i) S[(7-(i)) & 7]
1895
1896
    #ifndef XTRANSFORM
1897
668k
         #define XTRANSFORM(S, D)         Transform_Sha256(S, D)
1898
    #endif
1899
1900
#ifndef SHA256_MANY_REGISTERS
1901
    #define RND(j) \
1902
42.7M
         t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+(j)] + W[i+(j)]; \
1903
42.7M
         t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
1904
42.7M
         d(j) += t0; \
1905
42.7M
         h(j)  = t0 + t1
1906
1907
    static int Transform_Sha256_scr(wc_Sha256* sha256, const byte* data,
1908
                                    Sha256TScratch* WC_RESTRICT scr)
1909
668k
    {
1910
668k
        word32 t0, t1;
1911
668k
        int i;
1912
668k
        word32* WC_RESTRICT W = scr->W;
1913
668k
        word32* WC_RESTRICT S = scr->S;
1914
1915
        /* Copy context->state[] to working vars */
1916
6.01M
        for (i = 0; i < 8; i++)
1917
5.34M
            S[i] = sha256->digest[i];
1918
1919
11.3M
        for (i = 0; i < 16; i++) {
1920
#ifdef WOLFSSL_WIDE_BYTE
1921
            W[i] = *((const word32*)&data[i*(int)sizeof(word32)]);
1922
#else
1923
10.6M
            W[i] = readUnalignedWord32(&data[i*(int)sizeof(word32)]);
1924
10.6M
#endif
1925
10.6M
        }
1926
1927
32.7M
        for (i = 16; i < WC_SHA256_BLOCK_SIZE; i++)
1928
32.0M
            W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16];
1929
1930
    #ifdef USE_SLOW_SHA256
1931
        /* not unrolled - ~2k smaller and ~25% slower */
1932
        for (i = 0; i < WC_SHA256_BLOCK_SIZE; i += 8) {
1933
            int j;
1934
            for (j = 0; j < 8; j++) { /* braces needed here for macros {} */
1935
                RND(j);
1936
            }
1937
        }
1938
    #else
1939
        /* partially loop unrolled */
1940
6.01M
        for (i = 0; i < WC_SHA256_BLOCK_SIZE; i += 8) {
1941
5.34M
            RND(0); RND(1); RND(2); RND(3);
1942
5.34M
            RND(4); RND(5); RND(6); RND(7);
1943
5.34M
        }
1944
668k
    #endif /* USE_SLOW_SHA256 */
1945
1946
        /* Add the working vars back into digest state[] */
1947
6.01M
        for (i = 0; i < 8; i++) {
1948
5.34M
            sha256->digest[i] += S[i];
1949
5.34M
        }
1950
1951
        /* W and S are the caller's Sha256TScratch, wiped with one ForceZero
1952
         * when the public call returns. */
1953
668k
        return 0;
1954
668k
    }
1955
#else
1956
    /* SHA256 version that keeps all data in registers */
1957
    #define SCHED1(j) (W[j] = *((word32*)&data[j*sizeof(word32)]))
1958
    #define SCHED(j) (               \
1959
                   W[ j     & 15] += \
1960
            Gamma1(W[(j-2)  & 15])+  \
1961
                   W[(j-7)  & 15] +  \
1962
            Gamma0(W[(j-15) & 15])   \
1963
        )
1964
1965
    #define RND1(j) \
1966
         t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + SCHED1(j); \
1967
         t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
1968
         d(j) += t0; \
1969
         h(j)  = t0 + t1
1970
    #define RNDN(j) \
1971
         t0 = h(j) + Sigma1(e(j)) + Ch(e(j), f(j), g(j)) + K[i+j] + SCHED(j); \
1972
         t1 = Sigma0(a(j)) + Maj(a(j), b(j), c(j)); \
1973
         d(j) += t0; \
1974
         h(j)  = t0 + t1
1975
1976
    static int Transform_Sha256_scr(wc_Sha256* sha256, const byte* data,
1977
                                    Sha256TScratch* WC_RESTRICT scr)
1978
    {
1979
        word32 t0, t1;
1980
        int i;
1981
    #ifdef USE_SLOW_SHA256
1982
        int j;
1983
    #endif
1984
        word32* WC_RESTRICT W = scr->W;
1985
        word32* WC_RESTRICT S = scr->S;
1986
1987
        /* Copy digest to working vars */
1988
        S[0] = sha256->digest[0];
1989
        S[1] = sha256->digest[1];
1990
        S[2] = sha256->digest[2];
1991
        S[3] = sha256->digest[3];
1992
        S[4] = sha256->digest[4];
1993
        S[5] = sha256->digest[5];
1994
        S[6] = sha256->digest[6];
1995
        S[7] = sha256->digest[7];
1996
1997
        i = 0;
1998
    #ifdef USE_SLOW_SHA256
1999
        for (j = 0; j < 16; j++) {
2000
            RND1(j);
2001
        }
2002
        for (i = 16; i < 64; i += 16) {
2003
            for (j = 0; j < 16; j++) {
2004
                RNDN(j);
2005
            }
2006
        }
2007
    #else
2008
        RND1( 0); RND1( 1); RND1( 2); RND1( 3);
2009
        RND1( 4); RND1( 5); RND1( 6); RND1( 7);
2010
        RND1( 8); RND1( 9); RND1(10); RND1(11);
2011
        RND1(12); RND1(13); RND1(14); RND1(15);
2012
        /* 64 operations, partially loop unrolled */
2013
        for (i = 16; i < 64; i += 16) {
2014
            RNDN( 0); RNDN( 1); RNDN( 2); RNDN( 3);
2015
            RNDN( 4); RNDN( 5); RNDN( 6); RNDN( 7);
2016
            RNDN( 8); RNDN( 9); RNDN(10); RNDN(11);
2017
            RNDN(12); RNDN(13); RNDN(14); RNDN(15);
2018
        }
2019
    #endif
2020
2021
        /* Add the working vars back into digest */
2022
        sha256->digest[0] += S[0];
2023
        sha256->digest[1] += S[1];
2024
        sha256->digest[2] += S[2];
2025
        sha256->digest[3] += S[3];
2026
        sha256->digest[4] += S[4];
2027
        sha256->digest[5] += S[5];
2028
        sha256->digest[6] += S[6];
2029
        sha256->digest[7] += S[7];
2030
2031
        /* W and S are the caller's Sha256TScratch, wiped with one ForceZero
2032
         * when the public call returns. */
2033
        return 0;
2034
    }
2035
#endif /* SHA256_MANY_REGISTERS */
2036
2037
    /* Compatibility entry with per-call scratch, for the dispatch arms,
2038
     * function-pointer tables, and single-block callers, all of whose
2039
     * signatures stay unchanged.  Bulk callers hoist one scratch across
2040
     * their block loop and call Transform_Sha256_scr() directly, so the
2041
     * wipe runs once per public call rather than once per block. */
2042
    static WC_MAYBE_UNUSED int Transform_Sha256(wc_Sha256* sha256,
2043
                                                const byte* data)
2044
605k
    {
2045
605k
        int ret;
2046
605k
        WC_SHA256_TSCRATCH_DECL(sha256);
2047
605k
        WC_SHA256_TSCRATCH_BEGIN(sha256);
2048
605k
        ret = Transform_Sha256_scr(sha256, data, sha256TScr);
2049
605k
        WC_SHA256_TSCRATCH_WIPE(sha256);
2050
605k
        return ret;
2051
605k
    }
2052
#endif
2053
/* End wc_ software implementation */
2054
2055
#ifdef XTRANSFORM
2056
2057
    static WC_INLINE void AddLength(wc_Sha256* sha256, word32 len)
2058
403k
    {
2059
403k
        word32 tmp = sha256->loLen;
2060
403k
        if ((sha256->loLen += len) < tmp) {
2061
0
            sha256->hiLen++;                       /* carry low to high */
2062
0
        }
2063
403k
    }
2064
2065
    /* do block size increments/updates */
2066
    static WC_INLINE int Sha256Update(wc_Sha256* sha256, const byte* data,
2067
        word32 len)
2068
403k
    {
2069
403k
        int ret = 0;
2070
403k
        word32 blocksLen;
2071
403k
        byte* local;
2072
2073
        /* check that internal buffLen is valid */
2074
403k
        if (sha256->buffLen >= WC_SHA256_BLOCK_SIZE) {
2075
0
            return BUFFER_E;
2076
0
        }
2077
2078
        /* add length for final */
2079
403k
        AddLength(sha256, len);
2080
2081
403k
        local = (byte*)sha256->buffer;
2082
2083
        /* process any remainder from previous operation */
2084
403k
        if (sha256->buffLen > 0) {
2085
227k
            blocksLen = min(len, WC_SHA256_BLOCK_SIZE - sha256->buffLen);
2086
227k
            XMEMCPY(&local[sha256->buffLen], data, blocksLen);
2087
2088
227k
            sha256->buffLen += blocksLen;
2089
227k
            data            += blocksLen;
2090
227k
            len             -= blocksLen;
2091
2092
227k
            if (sha256->buffLen == WC_SHA256_BLOCK_SIZE) {
2093
            #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2094
               !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
2095
                if (sha256->ctx.mode == ESP32_SHA_INIT) {
2096
                    ESP_LOGV(TAG, "Sha256Update try hardware");
2097
                    esp_sha_try_hw_lock(&sha256->ctx);
2098
                }
2099
            #endif
2100
2101
52.8k
            if (SHA256_UPDATE_REV_BYTES(&sha256->ctx)) {
2102
            #ifdef WOLFSSL_WIDE_BYTE
2103
                /* CHAR_BIT != 8: pack 16 big-endian schedule words octet-wise */
2104
                WordsFromBytesBE32(sha256->buffer, (const byte*)sha256->buffer,
2105
                    WC_SHA256_BLOCK_SIZE / 4);
2106
            #else
2107
52.8k
                ByteReverseWords(sha256->buffer, sha256->buffer,
2108
52.8k
                    WC_SHA256_BLOCK_SIZE);
2109
52.8k
            #endif
2110
52.8k
            }
2111
2112
            #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2113
               !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
2114
                if (sha256->ctx.mode == ESP32_SHA_SW) {
2115
                    #if defined(WOLFSSL_DEBUG_MUTEX)
2116
                    {
2117
                        ESP_LOGI(TAG, "Sha256Update process software");
2118
                    }
2119
                    #endif
2120
                    #ifdef WOLFSSL_HW_METRICS
2121
                    {
2122
                        /* Track of # SW during transforms during active HW */
2123
                        esp_sw_sha256_count_add();
2124
                    }
2125
                    #endif /* WOLFSSL_HW_METRICS */
2126
                    ret = XTRANSFORM(sha256, (const byte*)local);
2127
                }
2128
                else {
2129
                    #if defined(WOLFSSL_DEBUG_MUTEX)
2130
                    {
2131
                        ESP_LOGI(TAG, "Sha256Update process hardware");
2132
                    }
2133
                    #endif
2134
                    esp_sha256_process(sha256, (const byte*)local);
2135
                }
2136
            #else
2137
                /* Always SW */
2138
52.8k
                ret = XTRANSFORM(sha256, (const byte*)local);
2139
52.8k
            #endif
2140
52.8k
                if (ret == 0)
2141
52.8k
                    sha256->buffLen = 0;
2142
13
                else
2143
13
                    len = 0; /* error */
2144
52.8k
            }
2145
227k
        }
2146
2147
        /* process blocks */
2148
    #ifdef XTRANSFORM_LEN
2149
        #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2150
                          (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
2151
2152
        #ifdef WC_NO_INTERNAL_FUNCTION_POINTERS
2153
        if (sha_method != SHA256_C)
2154
        #else
2155
        if (Transform_Sha256_Len_p != NULL)
2156
        #endif
2157
2158
        #endif
2159
        {
2160
            if (len >= WC_SHA256_BLOCK_SIZE) {
2161
                /* get number of blocks */
2162
                /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */
2163
                /* len (masked by 0xFFFFFFC0) returns block aligned length */
2164
                blocksLen = len & ~((word32)WC_SHA256_BLOCK_SIZE-1);
2165
                /* Byte reversal and alignment handled in function if required
2166
                 */
2167
                ret = XTRANSFORM_LEN(sha256, data, blocksLen);
2168
                if (ret == 0) {
2169
                    data += blocksLen;
2170
                    len  -= blocksLen;
2171
                }
2172
            }
2173
        }
2174
        #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2175
                          (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
2176
        else
2177
        #endif
2178
    #endif /* XTRANSFORM_LEN */
2179
403k
    #if !defined(XTRANSFORM_LEN) || \
2180
403k
        (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2181
403k
         (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
2182
403k
        {
2183
929k
            while (len >= WC_SHA256_BLOCK_SIZE) {
2184
525k
                word32* local32 = sha256->buffer;
2185
                /* optimization to avoid memcpy if data pointer is properly aligned */
2186
                /* Intel transform function requires use of sha256->buffer */
2187
                /* Little Endian requires byte swap, so can't use data directly */
2188
            #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(LITTLE_ENDIAN_ORDER) && \
2189
                !(defined(WOLFSSL_X86_64_BUILD) && \
2190
                         defined(USE_INTEL_SPEEDUP) && \
2191
                         (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
2192
                if (((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) == 0) {
2193
                    local32 = (word32*)data;
2194
                }
2195
                else
2196
            #endif
2197
525k
                {
2198
525k
                    XMEMCPY(local32, data, WC_SHA256_BLOCK_SIZE);
2199
525k
                }
2200
2201
525k
                data += WC_SHA256_BLOCK_SIZE;
2202
525k
                len  -= WC_SHA256_BLOCK_SIZE;
2203
            #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2204
               !defined( NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
2205
                if (sha256->ctx.mode == ESP32_SHA_INIT){
2206
                    ESP_LOGV(TAG, "Sha256Update try hardware loop");
2207
                    esp_sha_try_hw_lock(&sha256->ctx);
2208
                }
2209
            #endif
2210
2211
525k
            if (SHA256_UPDATE_REV_BYTES(&sha256->ctx)) {
2212
            #ifdef WOLFSSL_WIDE_BYTE
2213
                WordsFromBytesBE32(local32, (const byte*)local32,
2214
                    WC_SHA256_BLOCK_SIZE / 4);
2215
            #else
2216
525k
                ByteReverseWords(local32, local32, WC_SHA256_BLOCK_SIZE);
2217
525k
            #endif
2218
525k
            }
2219
2220
            #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2221
               !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
2222
                if (sha256->ctx.mode == ESP32_SHA_SW) {
2223
                    ESP_LOGV(TAG, "Sha256Update process software loop");
2224
                    ret = XTRANSFORM(sha256, (const byte*)local32);
2225
                }
2226
                else {
2227
                    ESP_LOGV(TAG, "Sha256Update process hardware");
2228
                    esp_sha256_process(sha256, (const byte*)local32);
2229
                }
2230
            #else
2231
525k
                ret = XTRANSFORM(sha256, (const byte*)local32);
2232
525k
            #endif
2233
2234
525k
                if (ret != 0)
2235
39
                    break;
2236
525k
            }
2237
403k
        }
2238
403k
    #endif
2239
2240
        /* save remainder */
2241
403k
        if (ret == 0 && len > 0) {
2242
151k
            XMEMCPY(local, data, len);
2243
151k
            sha256->buffLen = len;
2244
151k
        }
2245
2246
403k
        return ret;
2247
403k
    }
2248
2249
#if defined(WOLFSSL_KCAPI_HASH)
2250
    /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
2251
2252
#else
2253
    int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
2254
393k
    {
2255
393k
        if (sha256 == NULL) {
2256
0
            return BAD_FUNC_ARG;
2257
0
        }
2258
393k
        if (len == 0) {
2259
            /* valid, but do nothing */
2260
2.83k
            return 0;
2261
2.83k
        }
2262
390k
        if (data == NULL) {
2263
0
            return BAD_FUNC_ARG;
2264
0
        }
2265
2266
390k
    #ifdef WOLF_CRYPTO_CB
2267
390k
        #ifndef WOLF_CRYPTO_CB_FIND
2268
390k
        if (sha256->devId != INVALID_DEVID)
2269
145
        #endif
2270
145
        {
2271
145
            int ret = wc_CryptoCb_Sha256Hash(sha256, data, len, NULL);
2272
145
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2273
0
                return ret;
2274
            /* fall-through when unavailable */
2275
145
        }
2276
390k
    #endif
2277
    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
2278
        if (sha256->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA256) {
2279
        #if defined(HAVE_INTEL_QA)
2280
            return IntelQaSymSha256(&sha256->asyncDev, NULL, data, len);
2281
        #endif
2282
        }
2283
    #endif /* WOLFSSL_ASYNC_CRYPT */
2284
2285
390k
        return Sha256Update(sha256, data, len);
2286
390k
    }
2287
#endif
2288
2289
    static WC_INLINE int Sha256Final(wc_Sha256* sha256)
2290
88.7k
    {
2291
88.7k
        int ret;
2292
88.7k
        byte* local;
2293
2294
        /* we'll add a 0x80 byte at the end,
2295
        ** so make sure we have appropriate buffer length. */
2296
88.7k
        if (sha256->buffLen > WC_SHA256_BLOCK_SIZE - 1) {
2297
            /* exit with error code if there's a bad buffer size in buffLen */
2298
0
            return BAD_STATE_E;
2299
0
        } /* buffLen check */
2300
2301
88.7k
        local = (byte*)sha256->buffer;
2302
88.7k
        local[sha256->buffLen++] = 0x80; /* add 1 */
2303
2304
        /* pad with zeros */
2305
88.7k
        if (sha256->buffLen > WC_SHA256_PAD_SIZE) {
2306
830
            if (sha256->buffLen < WC_SHA256_BLOCK_SIZE) {
2307
737
                XMEMSET(&local[sha256->buffLen], 0,
2308
737
                    WC_SHA256_BLOCK_SIZE - sha256->buffLen);
2309
737
            }
2310
2311
        #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2312
           !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
2313
            if (sha256->ctx.mode == ESP32_SHA_INIT) {
2314
                esp_sha_try_hw_lock(&sha256->ctx);
2315
            }
2316
        #endif
2317
2318
830
        if (SHA256_UPDATE_REV_BYTES(&sha256->ctx)) {
2319
        #ifdef WOLFSSL_WIDE_BYTE
2320
            WordsFromBytesBE32(sha256->buffer, (const byte*)sha256->buffer,
2321
                WC_SHA256_BLOCK_SIZE / 4);
2322
        #else
2323
830
            ByteReverseWords(sha256->buffer, sha256->buffer,
2324
830
                WC_SHA256_BLOCK_SIZE);
2325
830
        #endif
2326
830
        }
2327
2328
        #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2329
           !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
2330
            if (sha256->ctx.mode == ESP32_SHA_INIT) {
2331
                esp_sha_try_hw_lock(&sha256->ctx);
2332
            }
2333
            if (sha256->ctx.mode == ESP32_SHA_SW) {
2334
                ret = XTRANSFORM(sha256, (const byte*)local);
2335
            }
2336
            else {
2337
                ret = esp_sha256_process(sha256, (const byte*)local);
2338
            }
2339
        #else
2340
830
            ret = XTRANSFORM(sha256, (const byte*)local);
2341
830
        #endif
2342
830
            if (ret != 0)
2343
0
                return ret;
2344
2345
830
            sha256->buffLen = 0;
2346
830
        }
2347
88.7k
        XMEMSET(&local[sha256->buffLen], 0,
2348
88.7k
            WC_SHA256_PAD_SIZE - sha256->buffLen);
2349
2350
        /* put 64 bit length in separate 32 bit parts */
2351
88.7k
        sha256->hiLen = (sha256->loLen >>
2352
88.7k
                            (CHAR_BIT * sizeof(sha256->loLen) - 3)) +
2353
88.7k
                                                         (sha256->hiLen << 3);
2354
88.7k
        sha256->loLen = sha256->loLen << 3;
2355
2356
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2357
       !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
2358
        if (sha256->ctx.mode == ESP32_SHA_INIT) {
2359
            esp_sha_try_hw_lock(&sha256->ctx);
2360
        }
2361
    #endif
2362
2363
        /* store lengths */
2364
#ifdef WOLFSSL_WIDE_BYTE
2365
        /* CHAR_BIT != 8: 'local' indexes octet cells, so place the 64-bit
2366
         * bit-length as 8 big-endian octets (W[14]=hiLen, W[15]=loLen). */
2367
        local[WC_SHA256_PAD_SIZE + 0] = (byte)((sha256->hiLen >> 24) & 0xFF);
2368
        local[WC_SHA256_PAD_SIZE + 1] = (byte)((sha256->hiLen >> 16) & 0xFF);
2369
        local[WC_SHA256_PAD_SIZE + 2] = (byte)((sha256->hiLen >>  8) & 0xFF);
2370
        local[WC_SHA256_PAD_SIZE + 3] = (byte)((sha256->hiLen      ) & 0xFF);
2371
        local[WC_SHA256_PAD_SIZE + 4] = (byte)((sha256->loLen >> 24) & 0xFF);
2372
        local[WC_SHA256_PAD_SIZE + 5] = (byte)((sha256->loLen >> 16) & 0xFF);
2373
        local[WC_SHA256_PAD_SIZE + 6] = (byte)((sha256->loLen >>  8) & 0xFF);
2374
        local[WC_SHA256_PAD_SIZE + 7] = (byte)((sha256->loLen      ) & 0xFF);
2375
#endif
2376
88.7k
        if (SHA256_UPDATE_REV_BYTES(&sha256->ctx)) {
2377
        #ifdef WOLFSSL_WIDE_BYTE
2378
            /* pack all 16 big-endian schedule words octet-wise (incl. length) */
2379
            WordsFromBytesBE32(sha256->buffer, (const byte*)sha256->buffer,
2380
                WC_SHA256_BLOCK_SIZE / 4);
2381
        #else
2382
88.7k
            ByteReverseWords(sha256->buffer, sha256->buffer,
2383
88.7k
                WC_SHA256_PAD_SIZE);
2384
88.7k
        #endif
2385
88.7k
        }
2386
88.7k
#ifndef WOLFSSL_WIDE_BYTE
2387
        /* ! 64-bit length ordering dependent on digest endian type ! */
2388
88.7k
        XMEMCPY(&local[WC_SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32));
2389
88.7k
        XMEMCPY(&local[WC_SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen,
2390
88.7k
                sizeof(word32));
2391
88.7k
#endif
2392
2393
    /* Only the ESP32-C3 with HW enabled may need pad size byte order reversal
2394
     * depending on HW or SW mode */
2395
    #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
2396
          defined(CONFIG_IDF_TARGET_ESP8684) || \
2397
          defined(CONFIG_IDF_TARGET_ESP32C3) || \
2398
          defined(CONFIG_IDF_TARGET_ESP32C6)    \
2399
        ) && \
2400
        defined(WOLFSSL_ESP32_CRYPT) &&         \
2401
       !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
2402
       !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
2403
        /* For Espressif RISC-V Targets, we *may* need to reverse bytes
2404
         * depending on if HW is active or not. */
2405
        if (sha256->ctx.mode == ESP32_SHA_HW) {
2406
        #if defined(WOLFSSL_SUPER_VERBOSE_DEBUG)
2407
            ESP_LOGV(TAG, "Start: Reverse PAD SIZE Endianness.");
2408
        #endif
2409
            ByteReverseWords(
2410
                &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)], /* out */
2411
                &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)], /* in  */
2412
                2 * sizeof(word32) /* byte count to reverse */
2413
            );
2414
        #if defined(WOLFSSL_SUPER_VERBOSE_DEBUG)
2415
            ESP_LOGV(TAG, "End: Reverse PAD SIZE Endianness.");
2416
        #endif
2417
        } /* end if (sha256->ctx.mode == ESP32_SHA_HW) */
2418
    #endif
2419
2420
    #if defined(FREESCALE_MMCAU_SHA) || \
2421
        (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2422
                         (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)))
2423
        /* Kinetis requires only these bytes reversed */
2424
        #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2425
                          (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
2426
        #ifdef WC_SHA256_RAW_BE_BUFFER
2427
        /* raw-buffer convention -- the length words must be big-endian in the
2428
         * stream regardless of which transform consumes the final block. */
2429
        #else
2430
        if (IS_INTEL_AVX1(intel_flags) || IS_INTEL_AVX2(intel_flags) ||
2431
            IS_INTEL_SHA(intel_flags))
2432
        #endif
2433
        #endif
2434
        {
2435
            ByteReverseWords(
2436
                &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
2437
                &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
2438
                2 * sizeof(word32));
2439
        }
2440
    #endif
2441
    #if (defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)) && \
2442
        !defined(FREESCALE_MMCAU_SHA)
2443
        ByteReverseWords( &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
2444
            &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
2445
            2 * sizeof(word32));
2446
    #endif
2447
    #if defined(WOLFSSL_PPC64_ASM) && defined(LITTLE_ENDIAN_ORDER)
2448
        /* The PPC64 assembly loads the message with byte-reversed loads on
2449
         * little-endian, treating the whole block as a big-endian byte stream.
2450
         * The 64-bit length above is stored in native (little-endian) word
2451
         * order, so reverse it here to keep the block a consistent big-endian
2452
         * stream. */
2453
        ByteReverseWords( &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
2454
            &sha256->buffer[WC_SHA256_PAD_SIZE / sizeof(word32)],
2455
            2 * sizeof(word32));
2456
    #endif
2457
2458
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2459
       !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
2460
        if (sha256->ctx.mode == ESP32_SHA_INIT) {
2461
            esp_sha_try_hw_lock(&sha256->ctx);
2462
        }
2463
        /* depending on architecture and ctx.mode value
2464
         * we may or may not need default digest */
2465
        if (sha256->ctx.mode == ESP32_SHA_SW) {
2466
            ret = XTRANSFORM(sha256, (const byte*)local);
2467
        }
2468
        else {
2469
            ret = esp_sha256_digest_process(sha256, 1);
2470
        }
2471
    #else
2472
88.7k
        ret = XTRANSFORM(sha256, (const byte*)local);
2473
88.7k
    #endif
2474
2475
88.7k
        return ret;
2476
88.7k
    }
2477
2478
#if !defined(WOLFSSL_KCAPI_HASH)
2479
2480
#ifndef WOLF_CRYPTO_CB_ONLY_SHA256
2481
    int wc_Sha256FinalRaw(wc_Sha256* sha256, byte* hash)
2482
79
    {
2483
79
    #if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_WIDE_BYTE)
2484
79
        word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
2485
79
        XMEMSET(digest, 0, sizeof(digest));
2486
79
    #endif
2487
2488
79
        if (sha256 == NULL || hash == NULL) {
2489
0
            return BAD_FUNC_ARG;
2490
0
        }
2491
    #if defined(WOLFSSL_CHECK_MEM_ZERO) && defined(LITTLE_ENDIAN_ORDER) && \
2492
        !defined(WOLFSSL_WIDE_BYTE)
2493
        wc_MemZero_Add("wc_Sha256FinalRaw digest", digest, sizeof(digest));
2494
    #endif
2495
2496
    #if defined(WOLFSSL_WIDE_BYTE)
2497
        /* CHAR_BIT != 8: store digest words as big-endian octets. */
2498
        BytesFromWordsBE32(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
2499
    #elif defined(LITTLE_ENDIAN_ORDER)
2500
79
        if (SHA256_REV_BYTES(&sha256->ctx)) {
2501
79
            ByteReverseWords((word32*)digest, (word32*)sha256->digest,
2502
79
                              WC_SHA256_DIGEST_SIZE);
2503
79
        }
2504
79
        XMEMCPY(hash, digest, WC_SHA256_DIGEST_SIZE);
2505
79
        ForceZero(digest, sizeof(digest));
2506
    #ifdef WOLFSSL_CHECK_MEM_ZERO
2507
        wc_MemZero_Check(digest, sizeof(digest));
2508
    #endif
2509
    #else
2510
        XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
2511
    #endif
2512
2513
79
        return 0;
2514
79
    }
2515
#endif /* !WOLF_CRYPTO_CB_ONLY_SHA256 */
2516
2517
    int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
2518
86.3k
    {
2519
86.3k
        int ret;
2520
2521
86.3k
        if (sha256 == NULL || hash == NULL) {
2522
0
            return BAD_FUNC_ARG;
2523
0
        }
2524
2525
86.3k
    #ifdef WOLF_CRYPTO_CB
2526
86.3k
        #ifndef WOLF_CRYPTO_CB_FIND
2527
86.3k
        if (sha256->devId != INVALID_DEVID)
2528
45
        #endif
2529
45
        {
2530
45
            ret = wc_CryptoCb_Sha256Hash(sha256, NULL, 0, hash);
2531
45
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2532
0
                return ret;
2533
            /* fall-through when unavailable */
2534
45
        }
2535
86.3k
    #endif
2536
2537
    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
2538
        if (sha256->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA256) {
2539
        #if defined(HAVE_INTEL_QA)
2540
            return IntelQaSymSha256(&sha256->asyncDev, hash, NULL,
2541
                                            WC_SHA256_DIGEST_SIZE);
2542
        #endif
2543
        }
2544
    #endif /* WOLFSSL_ASYNC_CRYPT */
2545
2546
86.3k
        ret = Sha256Final(sha256);
2547
86.3k
        if (ret != 0) {
2548
11
            return ret;
2549
11
        }
2550
2551
    #if defined(WOLFSSL_WIDE_BYTE)
2552
        /* CHAR_BIT != 8: store digest words as big-endian octets. */
2553
        BytesFromWordsBE32(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
2554
    #else
2555
86.3k
    #if defined(LITTLE_ENDIAN_ORDER)
2556
86.3k
        if (SHA256_REV_BYTES(&sha256->ctx)) {
2557
86.3k
            ByteReverseWords(sha256->digest, sha256->digest,
2558
86.3k
                WC_SHA256_DIGEST_SIZE);
2559
86.3k
        }
2560
86.3k
    #endif
2561
86.3k
        XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
2562
86.3k
    #endif
2563
2564
86.3k
        return InitSha256(sha256);  /* reset state */
2565
86.3k
    }
2566
2567
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
2568
/* Apply SHA256 transformation to the data                */
2569
/* @param sha  a pointer to wc_Sha256 structure           */
2570
/* @param data data to be applied SHA256 transformation   */
2571
/* @return 0 on successful, otherwise non-zero on failure */
2572
    int wc_Sha256Transform(wc_Sha256* sha256, const unsigned char* data)
2573
0
    {
2574
0
        if (sha256 == NULL || data == NULL) {
2575
0
            return BAD_FUNC_ARG;
2576
0
        }
2577
2578
    #if defined(WOLFSSL_ARMASM) || defined(WOLFSSL_RISCV_ASM)
2579
        #ifdef __aarch64__
2580
        if (Transform_Sha256_Len_p == Transform_Sha256_Len) {
2581
            return Transform_Sha256(sha256, data);
2582
        }
2583
        else
2584
        #endif
2585
        {
2586
            byte buffer[WC_SHA256_BLOCK_SIZE];
2587
            int tret;
2588
        #ifdef WOLFSSL_CHECK_MEM_ZERO
2589
            wc_MemZero_Add("wc_Sha256Transform buffer", buffer,
2590
                sizeof(buffer));
2591
        #endif
2592
            ByteReverseWords((word32*)buffer, (word32*)data,
2593
                WC_SHA256_BLOCK_SIZE);
2594
        #ifdef __aarch64__
2595
            tret = Transform_Sha256_aarch64(sha256, buffer);
2596
        #else
2597
            tret = Transform_Sha256(sha256, buffer);
2598
        #endif
2599
            ForceZero(buffer, sizeof(buffer));
2600
        #ifdef WOLFSSL_CHECK_MEM_ZERO
2601
            wc_MemZero_Check(buffer, sizeof(buffer));
2602
        #endif
2603
            return tret;
2604
        }
2605
    #else
2606
0
        return Transform_Sha256(sha256, data);
2607
0
    #endif
2608
0
    }
2609
#endif /* OPENSSL_EXTRA || HAVE_CURL */
2610
2611
#if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_FULL_HASH)
2612
    /* One block will be used from data.
2613
     * hash must be big enough to hold all of digest output.
2614
     */
2615
    int wc_Sha256HashBlock(wc_Sha256* sha256, const unsigned char* data,
2616
        unsigned char* hash)
2617
    {
2618
        int ret;
2619
2620
        if ((sha256 == NULL) || (data == NULL)) {
2621
            return BAD_FUNC_ARG;
2622
        }
2623
2624
        if (SHA256_UPDATE_REV_BYTES(&sha256->ctx)) {
2625
        #ifdef WOLFSSL_WIDE_BYTE
2626
            /* CHAR_BIT != 8: pack 16 big-endian schedule words octet-wise */
2627
            WordsFromBytesBE32(sha256->buffer, data,
2628
                WC_SHA256_BLOCK_SIZE / 4);
2629
        #else
2630
            /* Reverse in place. Stage first only when data is the caller's
2631
             * own buffer, which may be unaligned - LMS hands us
2632
             * sha256->buffer itself, and copying that onto itself is UB. */
2633
            if (data != (const unsigned char*)sha256->buffer) {
2634
                XMEMCPY(sha256->buffer, data, WC_SHA256_BLOCK_SIZE);
2635
            }
2636
            ByteReverseWords(sha256->buffer, sha256->buffer,
2637
                WC_SHA256_BLOCK_SIZE);
2638
        #endif
2639
            data = (const unsigned char*)sha256->buffer;
2640
        }
2641
        ret = XTRANSFORM(sha256, data);
2642
2643
        if ((ret == 0) && (hash != NULL)) {
2644
            if (!SHA256_REV_BYTES(&sha256->ctx)) {
2645
                XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
2646
            }
2647
        #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
2648
            else if (!IS_INTEL_MOVBE(cpuid_get_flags())) {
2649
                /* No MOVBE: reverse into aligned buffer, hash may be
2650
                 * unaligned. */
2651
                word32 buf[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
2652
                ByteReverseWords(buf, sha256->digest, WC_SHA256_DIGEST_SIZE);
2653
                XMEMCPY(hash, buf, WC_SHA256_DIGEST_SIZE);
2654
            }
2655
        #endif
2656
            else {
2657
        #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
2658
                __asm__ __volatile__ (
2659
                    "mov    0x00(%[d]), %%esi\n\t"
2660
                    "movbe  %%esi, 0x00(%[h])\n\t"
2661
                    "mov    0x04(%[d]), %%esi\n\t"
2662
                    "movbe  %%esi, 0x04(%[h])\n\t"
2663
                    "mov    0x08(%[d]), %%esi\n\t"
2664
                    "movbe  %%esi, 0x08(%[h])\n\t"
2665
                    "mov    0x0c(%[d]), %%esi\n\t"
2666
                    "movbe  %%esi, 0x0c(%[h])\n\t"
2667
                    "mov    0x10(%[d]), %%esi\n\t"
2668
                    "movbe  %%esi, 0x10(%[h])\n\t"
2669
                    "mov    0x14(%[d]), %%esi\n\t"
2670
                    "movbe  %%esi, 0x14(%[h])\n\t"
2671
                    "mov    0x18(%[d]), %%esi\n\t"
2672
                    "movbe  %%esi, 0x18(%[h])\n\t"
2673
                    "mov    0x1c(%[d]), %%esi\n\t"
2674
                    "movbe  %%esi, 0x1c(%[h])\n\t"
2675
                    :
2676
                    : [d] "r" (sha256->digest), [h] "r" (hash)
2677
                    : "memory", "esi"
2678
                );
2679
        #else
2680
                word32* hash32 = (word32*)hash;
2681
                word32* digest = (word32*)sha256->digest;
2682
            #if WOLFSSL_GENERAL_ALIGNMENT < 4
2683
                ALIGN16 word32 buf[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
2684
2685
                if (((size_t)digest & 0x3) != 0) {
2686
                    if (((size_t)hash32 & 0x3) != 0) {
2687
                        XMEMCPY(buf, digest, WC_SHA256_DIGEST_SIZE);
2688
                        hash32 = buf;
2689
                        digest = buf;
2690
                    }
2691
                    else {
2692
                        XMEMCPY(hash, digest, WC_SHA256_DIGEST_SIZE);
2693
                        digest = hash32;
2694
                    }
2695
                }
2696
                else if (((size_t)hash32 & 0x3) != 0) {
2697
                    hash32 = digest;
2698
                }
2699
            #endif
2700
                ByteReverseWords(hash32, digest, (word32)(sizeof(word32) * 8));
2701
            #if WOLFSSL_GENERAL_ALIGNMENT < 4
2702
                if (hash != (byte*)hash32) {
2703
                    XMEMCPY(hash, hash32, WC_SHA256_DIGEST_SIZE);
2704
                }
2705
            #endif
2706
        #endif /* WOLFSSL_X86_64_BUILD && USE_INTEL_SPEEDUP */
2707
            }
2708
            sha256->digest[0] = 0x6A09E667L;
2709
            sha256->digest[1] = 0xBB67AE85L;
2710
            sha256->digest[2] = 0x3C6EF372L;
2711
            sha256->digest[3] = 0xA54FF53AL;
2712
            sha256->digest[4] = 0x510E527FL;
2713
            sha256->digest[5] = 0x9B05688CL;
2714
            sha256->digest[6] = 0x1F83D9ABL;
2715
            sha256->digest[7] = 0x5BE0CD19L;
2716
        }
2717
2718
        return ret;
2719
    }
2720
#endif /* WOLFSSL_HAVE_LMS && !WOLFSSL_LMS_FULL_HASH */
2721
#endif /* !WOLFSSL_KCAPI_HASH */
2722
2723
#endif /* XTRANSFORM */
2724
2725
#ifdef WOLF_CRYPTO_CB_ONLY_SHA256
2726
2727
    int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
2728
    {
2729
        if (sha256 == NULL) {
2730
            return BAD_FUNC_ARG;
2731
        }
2732
        if (data == NULL && len == 0) {
2733
            /* valid, but do nothing */
2734
            return 0;
2735
        }
2736
        if (data == NULL) {
2737
            return BAD_FUNC_ARG;
2738
        }
2739
2740
        #ifndef WOLF_CRYPTO_CB_FIND
2741
        if (sha256->devId != INVALID_DEVID)
2742
        #endif
2743
        {
2744
            int ret = wc_CryptoCb_Sha256Hash(sha256, data, len, NULL);
2745
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2746
                return ret;
2747
        }
2748
2749
        return NO_VALID_DEVID;
2750
    }
2751
2752
    int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
2753
    {
2754
        int ret;
2755
2756
        if (sha256 == NULL || hash == NULL) {
2757
            return BAD_FUNC_ARG;
2758
        }
2759
2760
        #ifndef WOLF_CRYPTO_CB_FIND
2761
        if (sha256->devId != INVALID_DEVID)
2762
        #endif
2763
        {
2764
            ret = wc_CryptoCb_Sha256Hash(sha256, NULL, 0, hash);
2765
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2766
                return ret;
2767
        }
2768
2769
        return NO_VALID_DEVID;
2770
    }
2771
2772
#endif /* WOLF_CRYPTO_CB_ONLY_SHA256 */
2773
2774
2775
#ifdef WOLFSSL_SHA224
2776
2777
#ifdef STM32_HASH_SHA2
2778
2779
    /* Supports CubeMX HAL or Standard Peripheral Library */
2780
2781
    int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
2782
    {
2783
        if (sha224 == NULL)
2784
            return BAD_FUNC_ARG;
2785
        WC_SILABS_CLEAR_HASH_CTX(sha224);
2786
        (void)devId;
2787
        (void)heap;
2788
2789
        XMEMSET(sha224, 0, sizeof(wc_Sha224));
2790
        wc_Stm32_Hash_Init(&sha224->stmCtx);
2791
        return 0;
2792
    }
2793
2794
    int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
2795
    {
2796
        int ret = 0;
2797
2798
        if (sha224 == NULL || (data == NULL && len > 0)) {
2799
            return BAD_FUNC_ARG;
2800
        }
2801
2802
        ret = wolfSSL_CryptHwMutexLock();
2803
        if (ret == 0) {
2804
            ret = wc_Stm32_Hash_Update(&sha224->stmCtx,
2805
                HASH_AlgoSelection_SHA224, data, len, WC_SHA224_BLOCK_SIZE);
2806
            wolfSSL_CryptHwMutexUnLock();
2807
        }
2808
        return ret;
2809
    }
2810
2811
    int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
2812
    {
2813
        int ret = 0;
2814
2815
        if (sha224 == NULL || hash == NULL) {
2816
            return BAD_FUNC_ARG;
2817
        }
2818
2819
        ret = wolfSSL_CryptHwMutexLock();
2820
        if (ret == 0) {
2821
            ret = wc_Stm32_Hash_Final(&sha224->stmCtx,
2822
                HASH_AlgoSelection_SHA224, hash, WC_SHA224_DIGEST_SIZE);
2823
            wolfSSL_CryptHwMutexUnLock();
2824
        }
2825
2826
        (void)wc_InitSha224(sha224); /* reset state */
2827
2828
        return ret;
2829
    }
2830
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
2831
2832
    int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
2833
    {
2834
        if (sha224 == NULL) {
2835
            return BAD_FUNC_ARG;
2836
        }
2837
        WC_SILABS_CLEAR_HASH_CTX(sha224);
2838
        (void)devId;
2839
2840
        return se050_hash_init(&sha224->se050Ctx, heap);
2841
    }
2842
2843
    int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
2844
    {
2845
        return se050_hash_update(&sha224->se050Ctx, data, len);
2846
    }
2847
2848
    int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
2849
    {
2850
        int ret = 0;
2851
        ret = se050_hash_final(&sha224->se050Ctx, hash, WC_SHA224_DIGEST_SIZE,
2852
                               kAlgorithm_SSS_SHA224);
2853
        (void)wc_InitSha224(sha224);
2854
        return ret;
2855
    }
2856
2857
#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
2858
    !defined(WOLFSSL_QNX_CAAM)
2859
    /* functions defined in wolfcrypt/src/port/caam/caam_sha256.c */
2860
2861
#elif defined(WOLFSSL_AFALG_HASH)
2862
    #error SHA224 currently not supported with AF_ALG enabled
2863
2864
#elif defined(WOLFSSL_DEVCRYPTO_HASH)
2865
    /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
2866
2867
#elif defined(WOLFSSL_SILABS_SE_ACCEL)
2868
    /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
2869
2870
#elif defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_NO_KCAPI_SHA224)
2871
    /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
2872
2873
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
2874
    /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
2875
2876
#elif defined(MAX3266X_SHA)
2877
    /* implemented in wolfcrypt/src/port/maxim/max3266x.c */
2878
2879
#elif defined(WOLFSSL_RENESAS_RX64_HASH)
2880
2881
/* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
2882
2883
#elif defined(WOLFSSL_RENESAS_RSIP) && \
2884
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
2885
2886
    /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
2887
#elif defined(PSOC6_HASH_SHA2)
2888
    /* Implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
2889
2890
#elif defined(WOLF_CRYPTO_CB_ONLY_SHA256)
2891
    int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
2892
    {
2893
        int ret;
2894
        if (sha224 == NULL)
2895
            return BAD_FUNC_ARG;
2896
        WC_SILABS_CLEAR_HASH_CTX(sha224);
2897
        ret = InitSha256((wc_Sha256*)sha224);
2898
        if (ret != 0)
2899
            return ret;
2900
        sha224->digest[0] = 0xc1059ed8;
2901
        sha224->digest[1] = 0x367cd507;
2902
        sha224->digest[2] = 0x3070dd17;
2903
        sha224->digest[3] = 0xf70e5939;
2904
        sha224->digest[4] = 0xffc00b31;
2905
        sha224->digest[5] = 0x68581511;
2906
        sha224->digest[6] = 0x64f98fa7;
2907
        sha224->digest[7] = 0xbefa4fa4;
2908
        sha224->heap   = heap;
2909
        sha224->devId  = devId;
2910
        sha224->devCtx = NULL;
2911
    #ifdef WOLFSSL_SMALL_STACK_CACHE
2912
        sha224->W = NULL;
2913
    #endif
2914
    #ifdef WOLFSSL_ASYNC_CRYPT
2915
        XMEMSET(&sha224->asyncDev, 0, sizeof(sha224->asyncDev));
2916
    #endif
2917
        return ret;
2918
    }
2919
2920
#else
2921
2922
    #define NEED_SOFT_SHA224
2923
2924
2925
    static int InitSha224(wc_Sha224* sha224)
2926
2.61k
    {
2927
2.61k
        int ret = 0;
2928
2929
#ifdef WOLFSSL_SMALL_STACK_CACHE
2930
    if (sha224->W == NULL) {
2931
        sha224->W = (word32*)XMALLOC(sizeof(Sha256TScratch),
2932
                                     sha224->heap, DYNAMIC_TYPE_DIGEST);
2933
        if (sha224->W == NULL)
2934
            return MEMORY_E;
2935
    }
2936
#endif
2937
2938
2.61k
        sha224->digest[0] = 0xc1059ed8;
2939
2.61k
        sha224->digest[1] = 0x367cd507;
2940
2.61k
        sha224->digest[2] = 0x3070dd17;
2941
2.61k
        sha224->digest[3] = 0xf70e5939;
2942
2.61k
        sha224->digest[4] = 0xffc00b31;
2943
2.61k
        sha224->digest[5] = 0x68581511;
2944
2.61k
        sha224->digest[6] = 0x64f98fa7;
2945
2.61k
        sha224->digest[7] = 0xbefa4fa4;
2946
2947
2.61k
        sha224->buffLen = 0;
2948
2.61k
        XMEMSET(sha224->buffer, 0, sizeof(sha224->buffer));
2949
2.61k
        sha224->loLen   = 0;
2950
2.61k
        sha224->hiLen   = 0;
2951
2952
    #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP) && \
2953
                          (defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2))
2954
        /* choose best Transform function under this runtime environment */
2955
        Sha256_SetTransform();
2956
    #elif defined(WOLFSSL_ARMASM_SHA256_TRANSFORM)
2957
        /* SHA-224 shares the SHA-256 transform, on AArch32 as well as AArch64;
2958
         * a no-op in builds that compile a single variant.  Keyed off the
2959
         * marker so the call cannot outlive its definition when a hardware
2960
         * hash port wins the implementation chain. */
2961
        Sha256_SetTransform();
2962
    #endif
2963
    #if defined(WOLFSSL_PPC64_ASM) && defined(WOLFSSL_PPC64_ASM_CRYPTO)
2964
        /* SHA-224 shares the SHA-256 transform; select the base/vector-crypto
2965
         * implementation at run time (sets sha256_use_crypto). */
2966
        Sha256_SetTransform();
2967
    #endif
2968
2.61k
    #ifdef WOLFSSL_HASH_FLAGS
2969
2.61k
        sha224->flags = 0;
2970
2.61k
    #endif
2971
    #ifdef WOLFSSL_HASH_KEEP
2972
        sha224->msg  = NULL;
2973
        sha224->len  = 0;
2974
        sha224->used = 0;
2975
    #endif
2976
2977
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
2978
       (!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
2979
        !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
2980
        /* not to be confused with SHAS512_224 */
2981
        ret = esp_sha_init(&(sha224->ctx), WC_HASH_TYPE_SHA224);
2982
    #endif
2983
2984
2.61k
        return ret;
2985
2.61k
    }
2986
2987
#if !defined(WOLFSSL_HASH_KEEP)
2988
0
int wc_Sha224Reset(wc_Sha224* sha224) {
2989
0
    if (sha224 == NULL)
2990
0
        return BAD_FUNC_ARG;
2991
0
#ifdef WOLF_CRYPTO_CB
2992
    /* A device may hang state off devCtx that InitSha224() cannot restart.
2993
     * Free and re-init so the callback gets its teardown and setup. */
2994
0
    #ifndef WOLF_CRYPTO_CB_FIND
2995
0
    if (sha224->devId != INVALID_DEVID)
2996
0
    #endif
2997
0
    {
2998
0
        void* heap = sha224->heap;
2999
0
        int devId = sha224->devId;
3000
0
        wc_Sha224Free(sha224);
3001
0
        return wc_InitSha224_ex(sha224, heap, devId);
3002
0
    }
3003
0
#endif
3004
0
    return InitSha224(sha224);
3005
0
}
3006
#define WC_SHA224RESET_DEFINED
3007
#endif /* !WOLFSSL_HASH_KEEP */
3008
3009
#endif
3010
3011
#ifdef NEED_SOFT_SHA224
3012
    int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
3013
935
    {
3014
935
        int ret = 0;
3015
3016
935
        if (sha224 == NULL)
3017
0
            return BAD_FUNC_ARG;
3018
3019
935
        WC_SILABS_CLEAR_HASH_CTX(sha224);
3020
935
        sha224->heap = heap;
3021
    #ifdef WOLFSSL_SMALL_STACK_CACHE
3022
        sha224->W = NULL;
3023
    #endif
3024
935
    #ifdef WOLF_CRYPTO_CB
3025
935
        sha224->devId = devId;
3026
935
        sha224->devCtx = NULL;
3027
935
    #endif
3028
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
3029
        #if defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224)
3030
        /* We know this is a fresh, uninitialized item, so set to INIT */
3031
        if (sha224->ctx.mode != ESP32_SHA_SW) {
3032
            ESP_LOGV(TAG, "Set sha224 ctx mode init to ESP32_SHA_SW. "
3033
                          "Prior value: %d", sha224->ctx.mode);
3034
        }
3035
        /* no sha224 HW support is available, set to SW */
3036
            sha224->ctx.mode = ESP32_SHA_SW;
3037
        #else
3038
            /* We know this is a fresh, uninitialized item, so set to INIT */
3039
            sha224->ctx.mode = ESP32_SHA_INIT;
3040
        #endif
3041
    #endif
3042
3043
935
        ret = InitSha224(sha224);
3044
935
        if (ret != 0) {
3045
0
            return ret;
3046
0
        }
3047
3048
    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
3049
        ret = wolfAsync_DevCtxInit(&sha224->asyncDev,
3050
                            WOLFSSL_ASYNC_MARKER_SHA224, sha224->heap, devId);
3051
    #else
3052
935
        (void)devId;
3053
935
    #endif /* WOLFSSL_ASYNC_CRYPT */
3054
#ifdef WOLFSSL_IMXRT1170_CAAM
3055
     ret = wc_CAAM_HashInit(&sha224->hndl, &sha224->ctx, WC_HASH_TYPE_SHA224);
3056
#endif
3057
3058
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
3059
       (!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
3060
        !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
3061
        if (sha224->ctx.mode != ESP32_SHA_INIT) {
3062
            ESP_LOGV("SHA224", "Set ctx mode from prior value: "
3063
                               "%d", sha224->ctx.mode);
3064
        }
3065
        /* We know this is a fresh, uninitialized item, so set to INIT */
3066
        sha224->ctx.mode = ESP32_SHA_INIT;
3067
    #endif
3068
3069
935
        return ret;
3070
935
    }
3071
3072
    int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
3073
3.27k
    {
3074
3.27k
        int ret;
3075
3076
3.27k
        if (sha224 == NULL) {
3077
0
            return BAD_FUNC_ARG;
3078
0
        }
3079
3.27k
        if (len == 0) {
3080
            /* valid, but do nothing */
3081
7
            return 0;
3082
7
        }
3083
3.26k
        if (data == NULL) {
3084
0
            return BAD_FUNC_ARG;
3085
0
        }
3086
3.26k
    #ifdef WOLF_CRYPTO_CB
3087
3.26k
        #ifndef WOLF_CRYPTO_CB_FIND
3088
3.26k
        if (sha224->devId != INVALID_DEVID)
3089
131
        #endif
3090
131
        {
3091
131
            ret = wc_CryptoCb_Sha224Hash(sha224, data, len, NULL);
3092
131
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3093
0
                return ret;
3094
            /* fall-through when unavailable */
3095
131
        }
3096
3.26k
    #endif
3097
    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
3098
        if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) {
3099
        #if defined(HAVE_INTEL_QA)
3100
            return IntelQaSymSha224(&sha224->asyncDev, NULL, data, len);
3101
        #endif
3102
        }
3103
    #endif /* WOLFSSL_ASYNC_CRYPT */
3104
3105
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
3106
       (defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
3107
        defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
3108
        sha224->ctx.mode = ESP32_SHA_SW; /* no SHA224 HW, so always SW */
3109
    #endif
3110
3111
3.26k
        ret = Sha256Update((wc_Sha256*)sha224, data, len);
3112
3113
3.26k
        return ret;
3114
3.26k
    }
3115
3116
    int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
3117
1.68k
    {
3118
1.68k
        int ret;
3119
3120
1.68k
        if (sha224 == NULL || hash == NULL) {
3121
0
            return BAD_FUNC_ARG;
3122
0
        }
3123
1.68k
    #ifdef WOLF_CRYPTO_CB
3124
1.68k
        #ifndef WOLF_CRYPTO_CB_FIND
3125
1.68k
        if (sha224->devId != INVALID_DEVID)
3126
66
        #endif
3127
66
        {
3128
66
            ret = wc_CryptoCb_Sha224Hash(sha224, NULL, 0, hash);
3129
66
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3130
0
                return ret;
3131
            /* fall-through when unavailable */
3132
66
        }
3133
1.68k
    #endif
3134
    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
3135
        if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) {
3136
        #if defined(HAVE_INTEL_QA)
3137
            return IntelQaSymSha224(&sha224->asyncDev, hash, NULL,
3138
                                            WC_SHA224_DIGEST_SIZE);
3139
        #endif
3140
        }
3141
    #endif /* WOLFSSL_ASYNC_CRYPT */
3142
3143
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) &&      \
3144
       ( !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
3145
         !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224) )
3146
3147
        /* nothing enabled here for RISC-V C2/C3/C6 success */
3148
    #endif
3149
3150
1.68k
        ret = Sha256Final((wc_Sha256*)sha224);
3151
1.68k
        if (ret != 0)
3152
0
            return ret;
3153
3154
    #if defined(WOLFSSL_WIDE_BYTE)
3155
        /* CHAR_BIT != 8: store digest words as big-endian octets. */
3156
        BytesFromWordsBE32(hash, sha224->digest, WC_SHA224_DIGEST_SIZE);
3157
    #else
3158
1.68k
    #if defined(LITTLE_ENDIAN_ORDER)
3159
1.68k
        if (SHA256_REV_BYTES(&sha224->ctx)) {
3160
1.68k
            ByteReverseWords(sha224->digest,
3161
1.68k
                             sha224->digest,
3162
1.68k
                             WC_SHA224_DIGEST_SIZE);
3163
1.68k
        }
3164
1.68k
    #endif
3165
1.68k
        XMEMCPY(hash, sha224->digest, WC_SHA224_DIGEST_SIZE);
3166
1.68k
    #endif
3167
3168
1.68k
        return InitSha224(sha224);  /* reset state */
3169
1.68k
    }
3170
#endif /* end of SHA224 software implementation */
3171
3172
#ifdef WOLF_CRYPTO_CB_ONLY_SHA256
3173
3174
    int wc_Sha224Update(wc_Sha224* sha224, const byte* data, word32 len)
3175
    {
3176
        if (sha224 == NULL)
3177
            return BAD_FUNC_ARG;
3178
        if (data == NULL && len == 0)
3179
            return 0;
3180
        if (data == NULL)
3181
            return BAD_FUNC_ARG;
3182
3183
        #ifndef WOLF_CRYPTO_CB_FIND
3184
        if (sha224->devId != INVALID_DEVID)
3185
        #endif
3186
        {
3187
            int ret = wc_CryptoCb_Sha224Hash(sha224, data, len, NULL);
3188
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3189
                return ret;
3190
        }
3191
3192
        return NO_VALID_DEVID;
3193
    }
3194
3195
    int wc_Sha224Final(wc_Sha224* sha224, byte* hash)
3196
    {
3197
        int ret;
3198
3199
        if (sha224 == NULL || hash == NULL)
3200
            return BAD_FUNC_ARG;
3201
3202
        #ifndef WOLF_CRYPTO_CB_FIND
3203
        if (sha224->devId != INVALID_DEVID)
3204
        #endif
3205
        {
3206
            ret = wc_CryptoCb_Sha224Hash(sha224, NULL, 0, hash);
3207
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3208
                return ret;
3209
        }
3210
3211
        return NO_VALID_DEVID;
3212
    }
3213
3214
#endif /* WOLF_CRYPTO_CB_ONLY_SHA256 */
3215
3216
    int wc_InitSha224(wc_Sha224* sha224)
3217
66
    {
3218
66
        int devId = INVALID_DEVID;
3219
3220
66
    #ifdef WOLF_CRYPTO_CB
3221
66
        devId = wc_CryptoCb_DefaultDevID();
3222
66
    #endif
3223
66
        return wc_InitSha224_ex(sha224, NULL, devId);
3224
66
    }
3225
3226
#if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
3227
    /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
3228
3229
    void wc_Sha224Free(wc_Sha224* sha224)
3230
1.09k
    {
3231
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
3232
        int ret = 0;
3233
#endif
3234
3235
1.09k
        if (sha224 == NULL)
3236
0
            return;
3237
3238
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
3239
    #ifndef WOLF_CRYPTO_CB_FIND
3240
        if (sha224->devId != INVALID_DEVID)
3241
    #endif
3242
        {
3243
            ret = wc_CryptoCb_Free(sha224->devId, WC_ALGO_TYPE_HASH,
3244
                             WC_HASH_TYPE_SHA224, 0, (void*)sha224);
3245
            /* If they want the standard free, they can call it themselves */
3246
            /* via their callback setting devId to INVALID_DEVID */
3247
            /* otherwise assume the callback handled it */
3248
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3249
                return;
3250
            /* fall-through when unavailable */
3251
        }
3252
3253
        /* silence compiler warning */
3254
        (void)ret;
3255
3256
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */
3257
3258
#ifdef WOLFSSL_SMALL_STACK_CACHE
3259
        if (sha224->W != NULL) {
3260
            ForceZero(sha224->W, sizeof(Sha256TScratch));
3261
            XFREE(sha224->W, sha224->heap, DYNAMIC_TYPE_DIGEST);
3262
            sha224->W = NULL;
3263
        }
3264
#endif
3265
3266
    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
3267
        wolfAsync_DevCtxFree(&sha224->asyncDev, WOLFSSL_ASYNC_MARKER_SHA224);
3268
    #endif /* WOLFSSL_ASYNC_CRYPT */
3269
3270
    #ifdef WOLFSSL_PIC32MZ_HASH
3271
        wc_Sha256Pic32Free(sha224);
3272
    #endif
3273
    #if defined(WOLFSSL_KCAPI_HASH)
3274
        KcapiHashFree(&sha224->kcapi);
3275
    #endif
3276
    #if defined(WOLFSSL_RENESAS_RX64_HASH)
3277
        if (sha224->msg != NULL) {
3278
            ForceZero(sha224->msg, sha224->len);
3279
            XFREE(sha224->msg, sha224->heap, DYNAMIC_TYPE_TMP_BUFFER);
3280
            sha224->msg = NULL;
3281
        }
3282
    #endif
3283
    #if defined(PSOC6_HASH_SHA2)
3284
        wc_Psoc6_Sha_Free();
3285
    #endif
3286
1.09k
        ForceZero(sha224, sizeof(*sha224));
3287
1.09k
    }
3288
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)  */
3289
#endif /*  WOLFSSL_SHA224 */
3290
3291
3292
int wc_InitSha256(wc_Sha256* sha256)
3293
124
{
3294
124
    int devId = INVALID_DEVID;
3295
3296
124
#ifdef WOLF_CRYPTO_CB
3297
124
    devId = wc_CryptoCb_DefaultDevID();
3298
124
#endif
3299
124
    return wc_InitSha256_ex(sha256, NULL, devId);
3300
124
}
3301
3302
#if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
3303
    /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
3304
3305
void wc_Sha256Free(wc_Sha256* sha256)
3306
110k
{
3307
3308
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
3309
    int ret = 0;
3310
#endif
3311
3312
110k
    if (sha256 == NULL)
3313
0
        return;
3314
3315
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
3316
    #ifndef WOLF_CRYPTO_CB_FIND
3317
    if (sha256->devId != INVALID_DEVID)
3318
    #endif
3319
    {
3320
        ret = wc_CryptoCb_Free(sha256->devId, WC_ALGO_TYPE_HASH,
3321
                         WC_HASH_TYPE_SHA256, 0, (void*)sha256);
3322
        /* If they want the standard free, they can call it themselves */
3323
        /* via their callback setting devId to INVALID_DEVID */
3324
        /* otherwise assume the callback handled it */
3325
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3326
            return;
3327
        /* fall-through when unavailable */
3328
    }
3329
3330
    /* silence compiler warning */
3331
    (void)ret;
3332
3333
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */
3334
3335
#if defined(WOLFSSL_ESP32) && \
3336
    !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
3337
    !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
3338
    esp_sha_release_unfinished_lock(&sha256->ctx);
3339
#endif
3340
3341
#ifdef WOLFSSL_SMALL_STACK_CACHE
3342
    if (sha256->W != NULL) {
3343
        ForceZero(sha256->W, sizeof(Sha256TScratch));
3344
        XFREE(sha256->W, sha256->heap, DYNAMIC_TYPE_DIGEST);
3345
        sha256->W = NULL;
3346
    }
3347
#endif
3348
3349
3350
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
3351
    wolfAsync_DevCtxFree(&sha256->asyncDev, WOLFSSL_ASYNC_MARKER_SHA256);
3352
#endif /* WOLFSSL_ASYNC_CRYPT */
3353
#ifdef WOLFSSL_PIC32MZ_HASH
3354
    wc_Sha256Pic32Free(sha256);
3355
#endif
3356
#if defined(WOLFSSL_AFALG_HASH)
3357
    if (sha256->alFd > 0) {
3358
        close(sha256->alFd);
3359
        sha256->alFd = -1; /* avoid possible double close on socket */
3360
    }
3361
    if (sha256->rdFd > 0) {
3362
        close(sha256->rdFd);
3363
        sha256->rdFd = -1; /* avoid possible double close on socket */
3364
    }
3365
#endif /* WOLFSSL_AFALG_HASH */
3366
#ifdef WOLFSSL_DEVCRYPTO_HASH
3367
    wc_DevCryptoFree(&sha256->ctx);
3368
#endif /* WOLFSSL_DEVCRYPTO */
3369
#if (defined(WOLFSSL_AFALG_HASH) && defined(WOLFSSL_AFALG_HASH_KEEP)) || \
3370
    (defined(WOLFSSL_DEVCRYPTO_HASH) && defined(WOLFSSL_DEVCRYPTO_HASH_KEEP)) || \
3371
    ((defined(WOLFSSL_RENESAS_TSIP_TLS) || \
3372
      defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
3373
    !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) || \
3374
    ((defined(WOLFSSL_RENESAS_SCEPROTECT) || \
3375
    (defined(WOLFSSL_RENESAS_RSIP) && (WOLFSSL_RENESAS_RZFSP_VER >= 220))) && \
3376
    !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) || \
3377
    defined(WOLFSSL_RENESAS_RX64_HASH) || \
3378
    defined(WOLFSSL_HASH_KEEP)
3379
3380
    if (sha256->msg != NULL) {
3381
        ForceZero(sha256->msg, sha256->len);
3382
        XFREE(sha256->msg, sha256->heap, DYNAMIC_TYPE_TMP_BUFFER);
3383
        sha256->msg = NULL;
3384
    }
3385
#endif
3386
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
3387
    se050_hash_free(&sha256->se050Ctx);
3388
#endif
3389
#if defined(WOLFSSL_KCAPI_HASH)
3390
    KcapiHashFree(&sha256->kcapi);
3391
#endif
3392
#ifdef WOLFSSL_IMXRT_DCP
3393
    DCPSha256Free(sha256);
3394
#endif
3395
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
3396
    wc_MAXQ10XX_Sha256Free(sha256);
3397
#endif
3398
3399
#ifdef HAVE_ARIA
3400
    if (sha256->hSession != NULL) {
3401
        MC_CloseSession(sha256->hSession);
3402
        sha256->hSession = NULL;
3403
    }
3404
#endif
3405
3406
/* Espressif embedded hardware acceleration specific: */
3407
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
3408
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) && \
3409
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
3410
    if (sha256->ctx.lockDepth > 0) {
3411
        /* probably due to unclean shutdown, error, or other problem.
3412
         *
3413
         * if you find yourself here, code needs to be cleaned up to
3414
         * properly release hardware. this init is only for handling
3415
         * the unexpected. by the time free is called, the hardware
3416
         * should have already been released (lockDepth = 0)
3417
         */
3418
        (void)InitSha256(sha256); /* unlock mutex, set mode to ESP32_SHA_INIT */
3419
        ESP_LOGV(TAG, "Alert: hardware unlock needed in wc_Sha256Free.");
3420
    }
3421
    else {
3422
        ESP_LOGV(TAG, "Hardware unlock not needed in wc_Sha256Free.");
3423
    }
3424
#endif
3425
3426
#if defined(PSOC6_HASH_SHA2)
3427
    wc_Psoc6_Sha_Free();
3428
#endif
3429
3430
110k
    ForceZero(sha256, sizeof(*sha256));
3431
110k
} /* wc_Sha256Free */
3432
3433
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
3434
#ifdef WOLFSSL_HASH_KEEP
3435
/* Some hardware have issues with update, this function stores the data to be
3436
 * hashed into an array. Once ready, the Final operation is called on all of the
3437
 * data to be hashed at once.
3438
 * returns 0 on success
3439
 */
3440
int wc_Sha256_Grow(wc_Sha256* sha256, const byte* in, int inSz)
3441
{
3442
    return _wc_Hash_Grow(&(sha256->msg), &(sha256->used), &(sha256->len), in,
3443
                        inSz, sha256->heap);
3444
}
3445
#ifdef WOLFSSL_SHA224
3446
int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
3447
{
3448
    return _wc_Hash_Grow(&(sha224->msg), &(sha224->used), &(sha224->len), in,
3449
                        inSz, sha224->heap);
3450
}
3451
#endif /* WOLFSSL_SHA224 */
3452
#endif /* WOLFSSL_HASH_KEEP */
3453
3454
#endif /* !WOLFSSL_TI_HASH */
3455
3456
/* Fallback implementations of the Reset functions, for all configurations other
3457
 * than plain software.  Continues with the established heap and devId.
3458
 */
3459
3460
#ifndef WC_SHA256RESET_DEFINED
3461
3462
int wc_Sha256Reset(wc_Sha256* sha256) {
3463
    void *heap;
3464
    int devId;
3465
3466
    if (sha256 == NULL)
3467
        return BAD_FUNC_ARG;
3468
3469
    heap = sha256->heap;
3470
#ifdef WOLF_CRYPTO_CB
3471
    devId = sha256->devId;
3472
#else
3473
    devId = INVALID_DEVID;
3474
#endif
3475
3476
    wc_Sha256Free(sha256);
3477
    return wc_InitSha256_ex(sha256, heap, devId);
3478
}
3479
#define WC_SHA256RESET_DEFINED
3480
#endif
3481
3482
#if defined(WOLFSSL_SHA224) && !defined(WC_SHA224RESET_DEFINED)
3483
int wc_Sha224Reset(wc_Sha224* sha224) {
3484
    void *heap;
3485
    int devId;
3486
3487
    if (sha224 == NULL)
3488
        return BAD_FUNC_ARG;
3489
3490
    heap = sha224->heap;
3491
#ifdef WOLF_CRYPTO_CB
3492
    devId = sha224->devId;
3493
#else
3494
    devId = INVALID_DEVID;
3495
#endif
3496
3497
    wc_Sha224Free(sha224);
3498
    return wc_InitSha224_ex(sha224, heap, devId);
3499
}
3500
#define WC_SHA224RESET_DEFINED
3501
#endif /* WOLFSSL_SHA224 && !WC_SHA224RESET_DEFINED */
3502
3503
#ifndef WOLFSSL_TI_HASH
3504
#if !defined(WOLFSSL_RENESAS_RX64_HASH) && \
3505
    (!defined(WOLFSSL_RENESAS_RSIP) || \
3506
      defined(NO_WOLFSSL_RENESAS_FSPSM_HASH))
3507
#ifdef WOLFSSL_SHA224
3508
3509
#if defined(WOLFSSL_KCAPI_HASH) && !defined(WOLFSSL_NO_KCAPI_SHA224)
3510
    /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
3511
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
3512
    /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
3513
3514
#elif defined(MAX3266X_SHA)
3515
    /* implemented in wolfcrypt/src/port/maxim/max3266x.c */
3516
3517
#else
3518
3519
    int wc_Sha224GetHash(wc_Sha224* sha224, byte* hash)
3520
0
    {
3521
0
        int ret;
3522
0
        WC_DECLARE_VAR(tmpSha224, wc_Sha224, 1, 0);
3523
3524
0
        if (sha224 == NULL || hash == NULL) {
3525
0
            return BAD_FUNC_ARG;
3526
0
        }
3527
3528
0
        WC_CALLOC_VAR_EX(tmpSha224, wc_Sha224, 1, NULL,
3529
0
            DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
3530
    #ifdef WOLFSSL_CHECK_MEM_ZERO
3531
        wc_MemZero_Add("wc_Sha224GetHash tmpSha224", tmpSha224,
3532
            sizeof(*tmpSha224));
3533
    #endif
3534
3535
0
        ret = wc_Sha224Copy(sha224, tmpSha224);
3536
0
        if (ret == 0) {
3537
0
            ret = wc_Sha224Final(tmpSha224, hash);
3538
0
            wc_Sha224Free(tmpSha224);
3539
0
        }
3540
3541
0
        ForceZero(tmpSha224, sizeof(*tmpSha224));
3542
    #if defined(WOLFSSL_CHECK_MEM_ZERO) && !defined(WOLFSSL_SMALL_STACK)
3543
        wc_MemZero_Check(tmpSha224, sizeof(*tmpSha224));
3544
    #endif
3545
3546
0
        WC_FREE_VAR_EX(tmpSha224, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3547
0
        return ret;
3548
0
    }
3549
3550
    int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst)
3551
46
    {
3552
46
        int ret = 0; /* assume success unless proven otherwise */
3553
3554
46
        if (src == NULL || dst == NULL) {
3555
0
            return BAD_FUNC_ARG;
3556
0
        }
3557
3558
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_COPY)
3559
    #ifndef WOLF_CRYPTO_CB_FIND
3560
        if (src->devId != INVALID_DEVID)
3561
    #endif
3562
        {
3563
            /* Cast the source and destination to be void to keep the abstraction */
3564
            ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH,
3565
                                   WC_HASH_TYPE_SHA224, (void*)src, (void*)dst);
3566
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3567
                return ret;
3568
            /* fall-through when unavailable */
3569
        }
3570
        ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
3571
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
3572
3573
        /* Free dst resources before copy to prevent memory leaks (e.g., msg
3574
         * buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
3575
46
        wc_Sha224Free(dst);
3576
46
        XMEMCPY(dst, src, sizeof(wc_Sha224));
3577
3578
    #ifdef WOLFSSL_SMALL_STACK_CACHE
3579
        dst->W = (word32*)XMALLOC(WC_SHA256_TSCRATCH_SIZE,
3580
                                  dst->heap, DYNAMIC_TYPE_DIGEST);
3581
        if (dst->W == NULL) {
3582
            ForceZero(dst, sizeof(wc_Sha224));
3583
            return MEMORY_E;
3584
        }
3585
    #endif
3586
3587
    #if defined(WOLFSSL_SILABS_SE_TYPES) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
3588
        dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
3589
        dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
3590
    #endif
3591
3592
    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
3593
        ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
3594
    #endif
3595
3596
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
3597
       (!defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256) || \
3598
        !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA224))
3599
        /* regardless of any other settings, there's no SHA-224 HW on ESP32 */
3600
        #ifndef CONFIG_IDF_TARGET_ESP32
3601
            ret = esp_sha224_ctx_copy(src, dst);
3602
        #endif
3603
    #endif
3604
3605
46
    #ifdef WOLFSSL_HASH_FLAGS
3606
46
        dst->flags |= WC_HASH_FLAG_ISCOPY;
3607
46
    #endif
3608
3609
    #if defined(WOLFSSL_HASH_KEEP)
3610
        if (src->msg != NULL) {
3611
            dst->msg = (byte*)XMALLOC(src->len, dst->heap,
3612
                                      DYNAMIC_TYPE_TMP_BUFFER);
3613
            if (dst->msg == NULL)
3614
                return MEMORY_E;
3615
            XMEMCPY(dst->msg, src->msg, src->len);
3616
        }
3617
    #endif
3618
3619
    #if defined(PSOC6_HASH_SHA2)
3620
        wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA224, 0);
3621
    #endif
3622
46
        return ret;
3623
46
    }
3624
3625
#endif /* WOLFSSL_KCAPI_HASH && !WOLFSSL_NO_KCAPI_SHA224 */
3626
3627
#ifdef WOLFSSL_HASH_FLAGS
3628
    int wc_Sha224SetFlags(wc_Sha224* sha224, word32 flags)
3629
0
    {
3630
0
        if (sha224) {
3631
0
            sha224->flags = flags;
3632
0
        }
3633
0
        return 0;
3634
0
    }
3635
    int wc_Sha224GetFlags(wc_Sha224* sha224, word32* flags)
3636
0
    {
3637
0
        if (sha224 && flags) {
3638
0
            *flags = sha224->flags;
3639
0
        }
3640
0
        return 0;
3641
0
    }
3642
#endif
3643
3644
#endif /* WOLFSSL_SHA224 */
3645
#endif /* WOLFSSL_RENESAS_RX64_HASH */
3646
3647
#ifdef WOLFSSL_AFALG_HASH
3648
    /* implemented in wolfcrypt/src/port/af_alg/afalg_hash.c */
3649
3650
#elif defined(WOLFSSL_DEVCRYPTO_HASH)
3651
    /* implemented in wolfcrypt/src/port/devcrypto/devcrypt_hash.c */
3652
3653
#elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
3654
       defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
3655
    !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
3656
3657
    /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
3658
3659
#elif (defined(WOLFSSL_RENESAS_SCEPROTECT) || defined(WOLFSSL_RENESAS_RSIP))\
3660
     && !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
3661
3662
    /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
3663
#elif defined(WOLFSSL_IMXRT_DCP)
3664
    /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */
3665
#elif defined(WOLFSSL_KCAPI_HASH)
3666
    /* implemented in wolfcrypt/src/port/kcapi/kcapi_hash.c */
3667
3668
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
3669
    /* implemented in wolfcrypt/src/port/psa/psa_hash.c */
3670
#elif defined(WOLFSSL_RENESAS_RX64_HASH)
3671
    /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
3672
#elif defined(MAX3266X_SHA)
3673
    /* Implemented in wolfcrypt/src/port/maxim/max3266x.c */
3674
#else
3675
3676
int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
3677
3.68k
{
3678
3.68k
    int ret;
3679
3.68k
    WC_DECLARE_VAR(tmpSha256, wc_Sha256, 1, 0);
3680
3681
3.68k
    if (sha256 == NULL || hash == NULL) {
3682
0
        return BAD_FUNC_ARG;
3683
0
    }
3684
3685
3.68k
    WC_CALLOC_VAR_EX(tmpSha256, wc_Sha256, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
3686
3.68k
        return MEMORY_E);
3687
#ifdef WOLFSSL_CHECK_MEM_ZERO
3688
    wc_MemZero_Add("wc_Sha256GetHash tmpSha256", tmpSha256,
3689
        sizeof(*tmpSha256));
3690
#endif
3691
3692
3.68k
    ret = wc_Sha256Copy(sha256, tmpSha256);
3693
3.68k
    if (ret == 0) {
3694
3.68k
        ret = wc_Sha256Final(tmpSha256, hash);
3695
3.68k
        wc_Sha256Free(tmpSha256);
3696
3.68k
    }
3697
3698
3699
3.68k
    ForceZero(tmpSha256, sizeof(*tmpSha256));
3700
#if defined(WOLFSSL_CHECK_MEM_ZERO) && !defined(WOLFSSL_SMALL_STACK)
3701
    wc_MemZero_Check(tmpSha256, sizeof(*tmpSha256));
3702
#endif
3703
3704
3705
3.68k
    WC_FREE_VAR_EX(tmpSha256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3706
3707
3.68k
    return ret;
3708
3.68k
}
3709
int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
3710
3.72k
{
3711
3.72k
    int ret = 0;
3712
3713
3.72k
    if (src == NULL || dst == NULL) {
3714
0
        return BAD_FUNC_ARG;
3715
0
    }
3716
3717
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_COPY)
3718
    #ifndef WOLF_CRYPTO_CB_FIND
3719
    if (src->devId != INVALID_DEVID)
3720
    #endif
3721
    {
3722
        /* Cast the source and destination to be void to keep the abstraction */
3723
        ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH,
3724
                               WC_HASH_TYPE_SHA256, (void*)src, (void*)dst);
3725
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3726
            return ret;
3727
        /* fall-through when unavailable */
3728
    }
3729
    ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
3730
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
3731
3732
    /* Free dst resources before copy to prevent memory leaks (e.g., msg
3733
     * buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
3734
3.72k
    wc_Sha256Free(dst);
3735
3.72k
    XMEMCPY(dst, src, sizeof(wc_Sha256));
3736
3737
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
3738
    wc_MAXQ10XX_Sha256Copy(src);
3739
#endif
3740
3741
3742
#ifdef WOLFSSL_SMALL_STACK_CACHE
3743
    dst->W = (word32*)XMALLOC(WC_SHA256_TSCRATCH_SIZE,
3744
                              dst->heap, DYNAMIC_TYPE_DIGEST);
3745
    if (dst->W == NULL) {
3746
        ForceZero(dst, sizeof(wc_Sha256));
3747
        return MEMORY_E;
3748
    }
3749
#endif
3750
3751
#if defined(WOLFSSL_SILABS_SE_TYPES) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
3752
    dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
3753
    dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
3754
#endif
3755
3756
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
3757
    ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
3758
#endif
3759
3760
#ifdef WOLFSSL_PIC32MZ_HASH
3761
    ret = wc_Pic32HashCopy(&src->cache, &dst->cache, (byte*)src->buffer,
3762
        (byte*)dst->buffer, dst->heap);
3763
#endif
3764
3765
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) && \
3766
   !defined(NO_WOLFSSL_ESP32_CRYPT_HASH_SHA256)
3767
    esp_sha256_ctx_copy(src, dst);
3768
#endif
3769
3770
#ifdef HAVE_ARIA
3771
    dst->hSession = NULL;
3772
    if((src->hSession != NULL) && (MC_CopySession(src->hSession, &(dst->hSession)) != MC_OK)) {
3773
        return MEMORY_E;
3774
    }
3775
#endif
3776
3777
#if defined(WOLFSSL_TI_AM64X_R5) && !defined(WOLFSSL_TI_AM64X_NO_SHA)
3778
    dst->devCtx = NULL;
3779
#endif
3780
3781
3.72k
#ifdef WOLFSSL_HASH_FLAGS
3782
3.72k
    dst->flags |= WC_HASH_FLAG_ISCOPY;
3783
3.72k
#endif
3784
3785
    /* The XMEMCPY(dst, src, sizeof(wc_Sha256)) above copies src->msg as a
3786
     * raw pointer value, aliasing dst->msg to the same heap block. For any
3787
     * backend that buffers the whole message in sha256->msg (see the
3788
     * matching condition in wc_Sha256Free() above), that block later gets
3789
     * freed once via src's own Final()/Free() call and a second time via
3790
     * dst's -- a double free that silently corrupts the heap a little more
3791
     * on every wc_Sha256Copy()+wc_Sha256Free(dst) pair, instead of crashing
3792
     * outright. Give dst its own independent copy so the two objects don't
3793
     * share ownership of the same allocation. */
3794
#if defined(WOLFSSL_HASH_KEEP) || \
3795
    (defined(WOLFSSL_AFALG_HASH) && defined(WOLFSSL_AFALG_HASH_KEEP)) || \
3796
    (defined(WOLFSSL_DEVCRYPTO_HASH) && defined(WOLFSSL_DEVCRYPTO_HASH_KEEP)) || \
3797
    ((defined(WOLFSSL_RENESAS_TSIP_TLS) || \
3798
      defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
3799
    !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) || \
3800
    ((defined(WOLFSSL_RENESAS_SCEPROTECT) || \
3801
    (defined(WOLFSSL_RENESAS_RSIP) && (WOLFSSL_RENESAS_RZFSP_VER >= 220))) && \
3802
    !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) || \
3803
    defined(WOLFSSL_RENESAS_RX64_HASH)
3804
    if (src->msg != NULL) {
3805
        dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
3806
        if (dst->msg == NULL)
3807
            return MEMORY_E;
3808
        XMEMCPY(dst->msg, src->msg, src->len);
3809
    }
3810
#endif
3811
3812
#if defined(PSOC6_HASH_SHA2)
3813
    if ((ret == 0) && (wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA256, 0) != 0))
3814
        ret = WC_HW_E;
3815
#endif
3816
3817
3.72k
    return ret;
3818
3.72k
}
3819
#endif
3820
3821
#ifdef WOLFSSL_HASH_FLAGS
3822
int wc_Sha256SetFlags(wc_Sha256* sha256, word32 flags)
3823
59.5k
{
3824
59.5k
    if (sha256) {
3825
59.5k
        sha256->flags = flags;
3826
59.5k
    }
3827
59.5k
    return 0;
3828
59.5k
}
3829
int wc_Sha256GetFlags(wc_Sha256* sha256, word32* flags)
3830
0
{
3831
0
    if (sha256 && flags) {
3832
0
        *flags = sha256->flags;
3833
0
    }
3834
0
    return 0;
3835
0
}
3836
#endif
3837
#endif /* !WOLFSSL_TI_HASH */
3838
3839
#endif /* NO_SHA256 */