Coverage Report

Created: 2026-10-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl/wolfcrypt/src/sha.c
Line
Count
Source
1
/* sha.c
2
 *
3
 * Copyright (C) 2006-2026 wolfSSL Inc.
4
 *
5
 * This file is part of wolfSSL.
6
 *
7
 * wolfSSL is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * wolfSSL is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20
 */
21
22
/*
23
 * SHA-1 Build Options:
24
 *
25
 * Core:
26
 * NO_SHA:                   Disable SHA-1 support entirely        default: off
27
 * USE_SLOW_SHA:             Disable SHA-1 loop unrolling          default: off
28
 * WC_HASH_DATA_ALIGNMENT:   Required data alignment for hashing   default: off
29
 *
30
 * Hardware Acceleration (SHA-1-specific):
31
 * WC_ASYNC_ENABLE_SHA:      Enable async SHA-1 operations         default: off
32
 * WOLFSSL_PIC32MZ_HASH:     PIC32MZ hardware SHA                  default: off
33
 * WOLFSSL_PSA_NO_HASH:      Disable PSA hash                      default: off
34
 * WOLFSSL_TI_HASH:          TI hardware hash                      default: off
35
 * WOLFSSL_RENESAS_RX64_HASH: Renesas RX64 hardware hash           default: off
36
 * FREESCALE_LTC_SHA:        Freescale LTC SHA acceleration        default: off
37
 * FREESCALE_MMCAU_SHA:      Freescale MMCAU SHA acceleration      default: off
38
 * STM32_HASH:               STM32 hardware hash                   default: off
39
 * PSOC6_HASH_SHA1:          PSoC6 hardware SHA-1                  default: off
40
 */
41
42
#define WC_FIPS_LL_CRYPTO
43
#define _WC_BUILDING_SHA_C
44
45
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
46
47
#ifdef DEBUG_WOLFSSL_VERBOSE
48
    #if defined(WOLFSSL_ESPIDF)
49
        #include <esp_log.h>
50
    #endif
51
#endif
52
53
#if !defined(NO_SHA)
54
55
#if FIPS_VERSION3_GE(2,0,0)
56
    #if defined(USE_WINDOWS_API) && defined(_MSC_VER)
57
        #pragma code_seg(".fipsA$k")
58
        #pragma const_seg(".fipsB$k")
59
    #endif
60
#endif
61
62
#include <wolfssl/wolfcrypt/sha.h>
63
#include <wolfssl/wolfcrypt/hash.h>
64
65
#ifdef WOLF_CRYPTO_CB
66
    #include <wolfssl/wolfcrypt/cryptocb.h>
67
#endif
68
69
#ifdef WOLFSSL_IMXRT1170_CAAM
70
#include <wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h>
71
#endif
72
73
#if defined(WOLFSSL_PSOC6_CRYPTO)
74
    #include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h>
75
#endif
76
77
/* Assume no hash HW available until supporting HW found. */
78
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
79
80
#if defined(WOLFSSL_ESP32_CRYPT) && \
81
    !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
82
    /* define a single keyword for simplicity & readability
83
     *
84
     * by default the HW acceleration is on for ESP32-WROOM32
85
     * but individual components can be turned off.
86
     */
87
    #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
88
    #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
89
90
    /* Although we have hardware acceleration,
91
    ** we may need to fall back to software */
92
    #define USE_SHA_SOFTWARE_IMPL
93
94
#elif defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)
95
    /* The ESP32C3 is different; HW crypto here. Not yet implemented.
96
    ** We'll be using software for RISC-V at this time */
97
#else
98
    #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
99
#endif
100
101
#undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
102
#if defined(WOLFSSL_ESP32_CRYPT) && \
103
    !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
104
    /* define a single keyword for simplicity & readability
105
     *
106
     * by default the HW acceleration is on for ESP32-WROOM32
107
     * but individual components can be turned off.
108
     */
109
    #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW
110
    #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
111
112
    /* Although we have hardware acceleration,
113
    ** we may need to fall back to software */
114
    #define USE_SHA_SOFTWARE_IMPL
115
    static const char* TAG = "wc_sha";
116
#elif defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)
117
    /* The ESP32C3 is different; HW crypto here. Not yet implemented.
118
    ** We'll be using software for RISC-V at this time */
119
    static const char* TAG = "wc_sha-c3";
120
#else
121
    #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
122
#endif
123
124
#if defined(WOLFSSL_TI_HASH)
125
    /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
126
127
#else
128
129
#ifdef NO_INLINE
130
    #include <wolfssl/wolfcrypt/misc.h>
131
#else
132
    #define WOLFSSL_MISC_INCLUDED
133
    #include <wolfcrypt/src/misc.c>
134
#endif
135
136
#if FIPS_VERSION3_GE(6,0,0)
137
    const unsigned int wolfCrypt_FIPS_sha_ro_sanity[2] =
138
                                                     { 0x1a2b3c4d, 0x00000013 };
139
    int wolfCrypt_FIPS_SHA_sanity(void)
140
    {
141
        return 0;
142
    }
143
#endif
144
145
/* Hardware Acceleration */
146
#if defined(WOLFSSL_PIC32MZ_HASH)
147
    #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
148
149
#elif defined(STM32_HASH)
150
151
    /* Supports CubeMX HAL or Standard Peripheral Library */
152
    int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
153
    {
154
        if (sha == NULL) {
155
            return BAD_FUNC_ARG;
156
        }
157
158
        WC_SILABS_CLEAR_HASH_CTX(sha);
159
        (void)devId;
160
        (void)heap;
161
162
        wc_Stm32_Hash_Init(&sha->stmCtx);
163
164
        return 0;
165
    }
166
167
    int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
168
    {
169
        int ret;
170
171
        if (sha == NULL || (data == NULL && len > 0)) {
172
            return BAD_FUNC_ARG;
173
        }
174
175
        ret = wolfSSL_CryptHwMutexLock();
176
        if (ret == 0) {
177
            ret = wc_Stm32_Hash_Update(&sha->stmCtx, HASH_AlgoSelection_SHA1,
178
                data, len, WC_SHA_BLOCK_SIZE);
179
            wolfSSL_CryptHwMutexUnLock();
180
        }
181
        return ret;
182
    }
183
184
    int wc_ShaFinal(wc_Sha* sha, byte* hash)
185
    {
186
        int ret;
187
188
        if (sha == NULL || hash == NULL) {
189
            return BAD_FUNC_ARG;
190
        }
191
192
        ret = wolfSSL_CryptHwMutexLock();
193
        if (ret == 0) {
194
            ret = wc_Stm32_Hash_Final(&sha->stmCtx, HASH_AlgoSelection_SHA1,
195
                hash, WC_SHA_DIGEST_SIZE);
196
            wolfSSL_CryptHwMutexUnLock();
197
        }
198
199
        (void)wc_InitSha(sha);  /* reset state */
200
201
        return ret;
202
    }
203
204
205
#elif defined(FREESCALE_LTC_SHA)
206
207
    #include "fsl_ltc.h"
208
    int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
209
    {
210
        if (sha == NULL) {
211
            return BAD_FUNC_ARG;
212
        }
213
214
        WC_SILABS_CLEAR_HASH_CTX(sha);
215
        (void)devId;
216
        (void)heap;
217
218
        LTC_HASH_Init(LTC_BASE, &sha->ctx, kLTC_Sha1, NULL, 0);
219
        return 0;
220
    }
221
222
    int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
223
    {
224
        LTC_HASH_Update(&sha->ctx, data, len);
225
        return 0;
226
    }
227
228
    int wc_ShaFinal(wc_Sha* sha, byte* hash)
229
    {
230
        word32 hashlen = WC_SHA_DIGEST_SIZE;
231
        LTC_HASH_Finish(&sha->ctx, hash, &hashlen);
232
        return wc_InitSha(sha);  /* reset state */
233
    }
234
235
236
#elif defined(FREESCALE_MMCAU_SHA)
237
238
    #ifdef FREESCALE_MMCAU_CLASSIC_SHA
239
        #include "cau_api.h"
240
    #else
241
        #include "fsl_mmcau.h"
242
    #endif
243
244
    #define USE_SHA_SOFTWARE_IMPL /* Only for API's, actual transform is here */
245
246
    #define XTRANSFORM(S,B)       Transform((S),(B))
247
    #define XTRANSFORM_LEN(S,B,L) Transform_Len((S),(B),(L))
248
249
    #ifndef WC_HASH_DATA_ALIGNMENT
250
        /* these hardware API's require 4 byte (word32) alignment */
251
        #define WC_HASH_DATA_ALIGNMENT 4
252
    #endif
253
254
    static int InitSha(wc_Sha* sha)
255
    {
256
        int ret = 0;
257
        ret = wolfSSL_CryptHwMutexLock();
258
        if (ret != 0) {
259
            return ret;
260
        }
261
    #ifdef FREESCALE_MMCAU_CLASSIC_SHA
262
        cau_sha1_initialize_output(sha->digest);
263
    #else
264
        MMCAU_SHA1_InitializeOutput((word32*)sha->digest);
265
    #endif
266
        wolfSSL_CryptHwMutexUnLock();
267
268
        sha->buffLen = 0;
269
        sha->loLen   = 0;
270
        sha->hiLen   = 0;
271
272
        return ret;
273
    }
274
275
    static int Transform(wc_Sha* sha, const byte* data)
276
    {
277
        int ret = wolfSSL_CryptHwMutexLock();
278
        if (ret == 0) {
279
    #ifdef FREESCALE_MMCAU_CLASSIC_SHA
280
            cau_sha1_hash_n((byte*)data, 1, sha->digest);
281
    #else
282
            MMCAU_SHA1_HashN((byte*)data, 1, (word32*)sha->digest);
283
    #endif
284
            wolfSSL_CryptHwMutexUnLock();
285
        }
286
        return ret;
287
    }
288
289
    static int Transform_Len(wc_Sha* sha, const byte* data, word32 len)
290
    {
291
        int ret = wolfSSL_CryptHwMutexLock();
292
        if (ret == 0) {
293
        #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
294
            if ((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) {
295
                /* data pointer is NOT aligned,
296
                 * so copy and perform one block at a time */
297
                byte* local = (byte*)sha->buffer;
298
                while (len >= WC_SHA_BLOCK_SIZE) {
299
                    XMEMCPY(local, data, WC_SHA_BLOCK_SIZE);
300
                #ifdef FREESCALE_MMCAU_CLASSIC_SHA
301
                    cau_sha1_hash_n(local, 1, sha->digest);
302
                #else
303
                    MMCAU_SHA1_HashN(local, 1, sha->digest);
304
                #endif
305
                    data += WC_SHA_BLOCK_SIZE;
306
                    len  -= WC_SHA_BLOCK_SIZE;
307
                }
308
            }
309
            else
310
        #endif
311
            {
312
    #ifdef FREESCALE_MMCAU_CLASSIC_SHA
313
            cau_sha1_hash_n((byte*)data, len/WC_SHA_BLOCK_SIZE, sha->digest);
314
    #else
315
            MMCAU_SHA1_HashN((byte*)data, len/WC_SHA_BLOCK_SIZE,
316
                (word32*)sha->digest);
317
    #endif
318
            }
319
            wolfSSL_CryptHwMutexUnLock();
320
        }
321
        return ret;
322
    }
323
324
#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
325
    !defined(WOLFSSL_QNX_CAAM)
326
    /* wolfcrypt/src/port/caam/caam_sha.c */
327
328
#elif defined(MAX3266X_SHA)
329
    /* Already brought in by sha.h */
330
    /* #include <wolfssl/wolfcrypt/port/maxim/max3266x.h> */
331
332
#elif defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) || \
333
      defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)
334
335
    /* This function initializes SHA.
336
    ** This is automatically called by wc_ShaHash */
337
    static int InitSha(wc_Sha* sha)
338
    {
339
        int ret = 0;
340
341
        sha->digest[0] = 0x67452301L;
342
        sha->digest[1] = 0xEFCDAB89L;
343
        sha->digest[2] = 0x98BADCFEL;
344
        sha->digest[3] = 0x10325476L;
345
        sha->digest[4] = 0xC3D2E1F0L;
346
347
        sha->buffLen = 0;
348
        sha->loLen   = 0;
349
        sha->hiLen   = 0;
350
351
        /* HW needs to be carefully initialized, taking into account soft copy.
352
        ** If already in use; copy may revert to SW as needed. */
353
        ret = esp_sha_init(&(sha->ctx), WC_HASH_TYPE_SHA);
354
355
        return ret;
356
    }
357
358
#elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
359
    defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
360
    !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
361
362
    /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
363
364
#elif defined(WOLFSSL_RENESAS_RSIP) && \
365
     !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)
366
367
    /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */
368
369
#elif defined(WOLFSSL_IMXRT_DCP)
370
    #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
371
    /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */
372
373
#elif defined(WOLFSSL_NXP_HASHCRYPT_SHA)
374
    /* implemented in wolfcrypt/src/port/nxp/hashcrypt_port.c */
375
376
#elif defined(WOLFSSL_SILABS_SE_ACCEL)
377
378
    /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */
379
380
#elif defined(WOLFSSL_RENESAS_RX64_HASH)
381
382
/* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
383
384
#elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
385
386
    #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
387
    int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
388
    {
389
        if (sha == NULL) {
390
            return BAD_FUNC_ARG;
391
        }
392
        WC_SILABS_CLEAR_HASH_CTX(sha);
393
        (void)devId;
394
395
        return se050_hash_init(&sha->se050Ctx, heap);
396
    }
397
398
    int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
399
    {
400
        return se050_hash_update(&sha->se050Ctx, data, len);
401
402
    }
403
404
    int wc_ShaFinal(wc_Sha* sha, byte* hash)
405
    {
406
        int ret = 0;
407
        ret = se050_hash_final(&sha->se050Ctx, hash, WC_SHA_DIGEST_SIZE,
408
                               kAlgorithm_SSS_SHA1);
409
        return ret;
410
    }
411
    int wc_ShaFinalRaw(wc_Sha* sha, byte* hash)
412
    {
413
        int ret = 0;
414
        ret = se050_hash_final(&sha->se050Ctx, hash, WC_SHA_DIGEST_SIZE,
415
                               kAlgorithm_SSS_SHA1);
416
        return ret;
417
    }
418
419
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
420
/* implemented in wolfcrypt/src/port/psa/psa_hash.c */
421
#elif defined(PSOC6_HASH_SHA1)
422
    /* Implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */
423
#else
424
    /* Software implementation */
425
    #define USE_SHA_SOFTWARE_IMPL
426
427
    static int InitSha(wc_Sha* sha)
428
0
    {
429
0
        int ret = 0;
430
431
0
        sha->digest[0] = 0x67452301L;
432
0
        sha->digest[1] = 0xEFCDAB89L;
433
0
        sha->digest[2] = 0x98BADCFEL;
434
0
        sha->digest[3] = 0x10325476L;
435
0
        sha->digest[4] = 0xC3D2E1F0L;
436
437
0
        sha->buffLen = 0;
438
0
        XMEMSET(sha->buffer, 0, sizeof(sha->buffer));
439
0
        sha->loLen   = 0;
440
0
        sha->hiLen   = 0;
441
    #ifdef WOLFSSL_HASH_FLAGS
442
        sha->flags = 0;
443
    #endif
444
445
0
        return ret;
446
0
    }
447
#endif /* End Hardware Acceleration */
448
449
/* Software implementation */
450
#ifdef USE_SHA_SOFTWARE_IMPL
451
452
static WC_INLINE void AddLength(wc_Sha* sha, word32 len)
453
0
{
454
0
    word32 tmp = sha->loLen;
455
0
    if ((sha->loLen += len) < tmp)
456
0
        sha->hiLen++;                       /* carry low to high */
457
0
}
458
459
/* Check if custom wc_Sha transform is used */
460
#ifndef XTRANSFORM
461
0
    #define XTRANSFORM(S,B)   Transform((S),(B))
462
463
0
    #define blk0(i) (W[i] = *((const word32*)&data[(i)*sizeof(word32)]))
464
0
    #define blk1(i) (W[(i)&15] = \
465
0
        rotlFixed(W[((i)+13)&15]^W[((i)+8)&15]^W[((i)+2)&15]^W[(i)&15],1))
466
467
0
    #define f1(x,y,z) ((z)^((x) &((y)^(z))))
468
0
    #define f2(x,y,z) ((x)^(y)^(z))
469
0
    #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y))))
470
0
    #define f4(x,y,z) ((x)^(y)^(z))
471
472
    #if defined(WOLFSSL_NUCLEUS_1_2) || defined(NUCLEUS_PLUS_2_3)
473
        /* nucleus.h also defines R1-R4 */
474
        #undef R1
475
        #undef R2
476
        #undef R3
477
        #undef R4
478
    #endif
479
480
    /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
481
0
    #define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \
482
0
        rotlFixed((v),5); (w) = rotlFixed((w),30);
483
0
    #define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \
484
0
        rotlFixed((v),5); (w) = rotlFixed((w),30);
485
0
    #define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \
486
0
        rotlFixed((v),5); (w) = rotlFixed((w),30);
487
0
    #define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \
488
0
        rotlFixed((v),5); (w) = rotlFixed((w),30);
489
0
    #define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \
490
0
        rotlFixed((v),5); (w) = rotlFixed((w),30);
491
492
    static int Transform(wc_Sha* sha, const byte* data)
493
0
    {
494
0
        word32 W[WC_SHA_BLOCK_SIZE / sizeof(word32)];
495
496
        /* Copy context->state[] to working vars */
497
0
        word32 a = sha->digest[0];
498
0
        word32 b = sha->digest[1];
499
0
        word32 c = sha->digest[2];
500
0
        word32 d = sha->digest[3];
501
0
        word32 e = sha->digest[4];
502
503
    #ifdef USE_SLOW_SHA
504
        word32 t, i;
505
506
        for (i = 0; i < 16; i++) {
507
            R0(a, b, c, d, e, i);
508
            t = e; e = d; d = c; c = b; b = a; a = t;
509
        }
510
511
        for (; i < 20; i++) {
512
            R1(a, b, c, d, e, i);
513
            t = e; e = d; d = c; c = b; b = a; a = t;
514
        }
515
516
        for (; i < 40; i++) {
517
            R2(a, b, c, d, e, i);
518
            t = e; e = d; d = c; c = b; b = a; a = t;
519
        }
520
521
        for (; i < 60; i++) {
522
            R3(a, b, c, d, e, i);
523
            t = e; e = d; d = c; c = b; b = a; a = t;
524
        }
525
526
        for (; i < 80; i++) {
527
            R4(a, b, c, d, e, i);
528
            t = e; e = d; d = c; c = b; b = a; a = t;
529
        }
530
    #else
531
        /* nearly 1 K bigger in code size but 25% faster */
532
        /* 4 rounds of 20 operations each. Loop unrolled. */
533
0
        R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
534
0
        R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
535
0
        R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
536
0
        R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
537
538
0
        R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
539
540
0
        R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
541
0
        R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
542
0
        R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
543
0
        R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
544
0
        R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
545
546
0
        R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
547
0
        R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
548
0
        R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
549
0
        R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
550
0
        R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
551
552
0
        R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
553
0
        R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
554
0
        R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
555
0
        R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
556
0
        R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
557
0
    #endif
558
559
        /* Add the working vars back into digest state[] */
560
0
        sha->digest[0] += a;
561
0
        sha->digest[1] += b;
562
0
        sha->digest[2] += c;
563
0
        sha->digest[3] += d;
564
0
        sha->digest[4] += e;
565
566
0
        (void)data; /* Not used */
567
568
0
        return 0;
569
0
    }
570
#endif /* XTRANSFORM when USE_SHA_SOFTWARE_IMPL is enabled */
571
572
573
/*
574
** wolfCrypt InitSha external wrapper.
575
**
576
** we'll assume this is ALWAYS for a new, uninitialized sha
577
*/
578
int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
579
0
{
580
0
    int ret = 0;
581
0
    if (sha == NULL) {
582
0
        return BAD_FUNC_ARG;
583
0
    }
584
585
0
    WC_SILABS_CLEAR_HASH_CTX(sha);
586
0
    sha->heap = heap;
587
#ifdef WOLF_CRYPTO_CB
588
    sha->devId = devId;
589
    sha->devCtx = NULL;
590
#endif
591
#ifdef WOLFSSL_HASH_KEEP
592
    sha->msg  = NULL;
593
    sha->len  = 0;
594
    sha->used = 0;
595
#endif
596
597
#ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW
598
    if (sha->ctx.mode != ESP32_SHA_INIT) {
599
        /* it may be interesting to see old values during debugging */
600
        ESP_LOGV(TAG, "Set ctx mode from prior value: %d", sha->ctx.mode);
601
    }
602
    /* We know this is a fresh, uninitialized item, so set to INIT */
603
    sha->ctx.mode = ESP32_SHA_INIT;
604
#endif
605
606
0
    ret = InitSha(sha);
607
0
    if (ret != 0) {
608
0
        return ret;
609
0
    }
610
611
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
612
    ret = wolfAsync_DevCtxInit(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA,
613
                                                            sha->heap, devId);
614
#else
615
0
    (void)devId;
616
0
#endif /* WOLFSSL_ASYNC_CRYPT */
617
#ifdef WOLFSSL_IMXRT1170_CAAM
618
   ret = wc_CAAM_HashInit(&sha->hndl, &sha->ctx, WC_HASH_TYPE_SHA);
619
#endif
620
621
0
    return ret;
622
0
} /* wc_InitSha_ex */
623
624
/* do block size increments/updates */
625
int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
626
0
{
627
0
    int ret = 0;
628
0
    word32 blocksLen;
629
0
    byte* local;
630
631
0
    if (sha == NULL) {
632
0
        return BAD_FUNC_ARG;
633
0
    }
634
635
0
    if (data == NULL && len == 0) {
636
        /* valid, but do nothing */
637
0
        return 0;
638
0
    }
639
640
0
    if (data == NULL) {
641
0
        return BAD_FUNC_ARG;
642
0
    }
643
644
#ifdef WOLF_CRYPTO_CB
645
    if (sha->devId != INVALID_DEVID) {
646
        ret = wc_CryptoCb_ShaHash(sha, data, len, NULL);
647
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
648
            return ret;
649
        ret = 0; /* reset ret */
650
        /* fall-through when unavailable */
651
    }
652
#endif
653
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
654
    if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) {
655
    #if defined(HAVE_INTEL_QA)
656
        return IntelQaSymSha(&sha->asyncDev, NULL, data, len);
657
    #endif
658
    }
659
#endif /* WOLFSSL_ASYNC_CRYPT */
660
661
    /* check that internal buffLen is valid */
662
0
    if (sha->buffLen >= WC_SHA_BLOCK_SIZE) {
663
0
        return BUFFER_E;
664
0
    }
665
666
    /* add length for final */
667
0
    AddLength(sha, len);
668
669
0
    local = (byte*)sha->buffer;
670
671
    /* process any remainder from previous operation */
672
0
    if (sha->buffLen > 0) {
673
0
        blocksLen = min(len, WC_SHA_BLOCK_SIZE - sha->buffLen);
674
0
        XMEMCPY(&local[sha->buffLen], data, blocksLen);
675
676
0
        sha->buffLen += blocksLen;
677
0
        data         += blocksLen;
678
0
        len          -= blocksLen;
679
680
0
        if (sha->buffLen == WC_SHA_BLOCK_SIZE) {
681
        #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
682
            if (sha->ctx.mode == ESP32_SHA_INIT) {
683
                #if defined(WOLFSSL_DEBUG_MUTEX)
684
                {
685
                    ESP_LOGI(TAG, "wc_ShaUpdate try hardware");
686
                }
687
                #endif
688
                esp_sha_try_hw_lock(&sha->ctx);
689
            }
690
        #endif
691
692
0
        #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
693
            #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
694
                  defined(CONFIG_IDF_TARGET_ESP8684) || \
695
                  defined(CONFIG_IDF_TARGET_ESP32C3) || \
696
                  defined(CONFIG_IDF_TARGET_ESP32C6)    \
697
                ) && \
698
                 defined(WOLFSSL_ESP32_CRYPT) && \
699
                !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
700
                /* For Espressif RISC-V Targets, we *may* need to reverse bytes
701
                 * depending on if HW is active or not. */
702
                if (esp_sha_need_byte_reversal(&sha->ctx))
703
            #endif
704
0
            {
705
            #ifdef WOLFSSL_WIDE_BYTE
706
                /* CHAR_BIT != 8: pack the 16 big-endian schedule words
707
                 * octet-wise rather than reversing whole cells. */
708
                WordsFromBytesBE32(sha->buffer, (const byte*)sha->buffer,
709
                    WC_SHA_BLOCK_SIZE / 4);
710
            #else
711
0
                ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
712
0
            #endif
713
0
            }
714
0
        #endif
715
716
        #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
717
            if (sha->ctx.mode == ESP32_SHA_SW) {
718
                #if defined(WOLFSSL_DEBUG_MUTEX)
719
                {
720
                    ESP_LOGI(TAG, "wc_ShaUpdate process software");
721
                }
722
                #endif
723
                ret = XTRANSFORM(sha, (const byte*)local);
724
            }
725
            else {
726
                #if defined(WOLFSSL_DEBUG_MUTEX)
727
                {
728
                    ESP_LOGI(TAG, "wc_ShaUpdate process hardware");
729
                }
730
                #endif
731
                esp_sha_process(sha, (const byte*)local);
732
            }
733
        #elif defined (WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)
734
            ESP_LOGI(TAG, "wc_ShaUpdate not implemented for ESP32C3");
735
            ret = XTRANSFORM(sha, (const byte*)local);
736
        #else
737
0
            ret = XTRANSFORM(sha, (const byte*)local);
738
0
        #endif
739
0
            if (ret != 0) {
740
0
                return ret;
741
0
            }
742
743
0
            sha->buffLen = 0; /* Nothing left to do, so set to zero. */
744
0
        } /* (sha->buffLen == WC_SHA_BLOCK_SIZE) */
745
0
    } /* (sha->buffLen > 0)  Process any remainder from previous operation. */
746
747
    /* process blocks */
748
#ifdef XTRANSFORM_LEN
749
    /* get number of blocks */
750
    /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */
751
    /* len (masked by 0xFFFFFFC0) returns block aligned length */
752
    blocksLen = len & ~(WC_SHA_BLOCK_SIZE-1);
753
    if (blocksLen > 0) {
754
        /* Byte reversal performed in function if required. */
755
        XTRANSFORM_LEN(sha, data, blocksLen);
756
        data += blocksLen;
757
        len  -= blocksLen;
758
    }
759
#else
760
0
    while (len >= WC_SHA_BLOCK_SIZE) {
761
0
        word32* local32 = sha->buffer;
762
        /* optimization to avoid memcpy if data pointer is properly aligned */
763
        /* Little Endian requires byte swap, so can't use data directly */
764
    #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(LITTLE_ENDIAN_ORDER)
765
        if (((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) == 0) {
766
            local32 = (word32*)data;
767
        }
768
        else
769
    #endif
770
0
        {
771
0
            XMEMCPY(local32, data, WC_SHA_BLOCK_SIZE);
772
0
        }
773
774
0
        data += WC_SHA_BLOCK_SIZE;
775
0
        len  -= WC_SHA_BLOCK_SIZE;
776
777
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
778
        if (sha->ctx.mode == ESP32_SHA_INIT){
779
            esp_sha_try_hw_lock(&sha->ctx);
780
        }
781
    #endif
782
783
0
    #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
784
        #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
785
              defined(CONFIG_IDF_TARGET_ESP8684) || \
786
              defined(CONFIG_IDF_TARGET_ESP32C3) || \
787
              defined(CONFIG_IDF_TARGET_ESP32C6)    \
788
            ) && \
789
             defined(WOLFSSL_ESP32_CRYPT) && \
790
            !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
791
            /* For Espressif RISC-V Targets, we *may* need to reverse bytes
792
             * depending on if HW is active or not. */
793
            if (esp_sha_need_byte_reversal(&sha->ctx))
794
        #endif
795
0
        {
796
        #ifdef WOLFSSL_WIDE_BYTE
797
            WordsFromBytesBE32(local32, (const byte*)local32,
798
                WC_SHA_BLOCK_SIZE / 4);
799
        #else
800
0
            ByteReverseWords(local32, local32, WC_SHA_BLOCK_SIZE);
801
0
        #endif
802
0
        }
803
0
    #endif
804
805
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
806
        if (sha->ctx.mode == ESP32_SHA_SW){
807
            ret = XTRANSFORM(sha, (const byte*)local32);
808
        }
809
        else {
810
            esp_sha_process(sha, (const byte*)local32);
811
        }
812
    #else
813
0
        ret = XTRANSFORM(sha, (const byte*)local32);
814
0
    #endif
815
0
    }
816
0
#endif /* XTRANSFORM_LEN */
817
818
    /* save remainder */
819
0
    if (len > 0) {
820
0
        XMEMCPY(local, data, len);
821
0
        sha->buffLen = len;
822
0
    }
823
824
0
    return ret;
825
0
}
826
827
int wc_ShaFinalRaw(wc_Sha* sha, byte* hash)
828
0
{
829
0
#if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_WIDE_BYTE)
830
0
    word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)];
831
0
    XMEMSET(digest, 0, sizeof(digest));
832
0
#endif
833
834
0
    if (sha == NULL || hash == NULL) {
835
0
        return BAD_FUNC_ARG;
836
0
    }
837
838
#if defined(WOLFSSL_WIDE_BYTE)
839
    /* CHAR_BIT != 8: write the digest words as big-endian octets. */
840
    BytesFromWordsBE32(hash, sha->digest, WC_SHA_DIGEST_SIZE);
841
#elif defined(LITTLE_ENDIAN_ORDER)
842
    #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
843
          defined(CONFIG_IDF_TARGET_ESP8684) || \
844
          defined(CONFIG_IDF_TARGET_ESP32C3) || \
845
          defined(CONFIG_IDF_TARGET_ESP32C6)    \
846
        ) && \
847
         defined(WOLFSSL_ESP32_CRYPT) && \
848
        !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
849
        /* For Espressif RISC-V Targets, we *may* need to reverse bytes
850
         * depending on if HW is active or not. */
851
        if (esp_sha_need_byte_reversal(&sha->ctx))
852
    #endif
853
0
    {
854
0
        ByteReverseWords((word32*)digest, (word32*)sha->digest, WC_SHA_DIGEST_SIZE);
855
0
    }
856
0
    XMEMCPY(hash, (byte *)&digest[0], WC_SHA_DIGEST_SIZE);
857
#else
858
    XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE);
859
#endif
860
861
0
    return 0;
862
0
}
863
864
/*
865
** Finalizes hashing of data. Result is placed into hash.
866
** Resets state of sha struct.
867
*/
868
int wc_ShaFinal(wc_Sha* sha, byte* hash)
869
0
{
870
0
    int ret;
871
0
    byte* local;
872
873
0
    if (sha == NULL || hash == NULL) {
874
0
        return BAD_FUNC_ARG;
875
0
    }
876
877
0
    local = (byte*)sha->buffer;
878
879
#ifdef WOLF_CRYPTO_CB
880
    if (sha->devId != INVALID_DEVID) {
881
        ret = wc_CryptoCb_ShaHash(sha, NULL, 0, hash);
882
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
883
            return ret;
884
        /* fall-through when unavailable */
885
    }
886
#endif
887
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
888
    if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) {
889
    #if defined(HAVE_INTEL_QA)
890
        return IntelQaSymSha(&sha->asyncDev, hash, NULL, WC_SHA_DIGEST_SIZE);
891
    #endif
892
    }
893
#endif /* WOLFSSL_ASYNC_CRYPT */
894
895
    /* we'll add a 0x80 byte at the end,
896
    ** so make sure we have appropriate buffer length. */
897
0
    if (sha->buffLen > WC_SHA_BLOCK_SIZE - 1) {
898
        /* exit with error code if there's a bad buffer size in buffLen */
899
0
        return BAD_STATE_E;
900
0
    } /* buffLen check */
901
902
0
    local[sha->buffLen++] = 0x80;  /* add 1 */
903
904
    /* pad with zeros */
905
0
    if (sha->buffLen > WC_SHA_PAD_SIZE) {
906
0
        if (sha->buffLen < WC_SHA_BLOCK_SIZE) {
907
0
            XMEMSET(&local[sha->buffLen], 0, WC_SHA_BLOCK_SIZE - sha->buffLen);
908
0
        }
909
910
0
        sha->buffLen += WC_SHA_BLOCK_SIZE - sha->buffLen;
911
912
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
913
        /* For a fresh sha.ctx, try to use hardware acceleration */
914
        if (sha->ctx.mode == ESP32_SHA_INIT) {
915
            esp_sha_try_hw_lock(&sha->ctx);
916
        }
917
    #endif
918
919
0
    #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
920
        #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
921
              defined(CONFIG_IDF_TARGET_ESP8684) || \
922
              defined(CONFIG_IDF_TARGET_ESP32C3) || \
923
              defined(CONFIG_IDF_TARGET_ESP32C6)    \
924
            ) && \
925
             defined(WOLFSSL_ESP32_CRYPT) && \
926
            !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
927
            /* For Espressif RISC-V Targets, we *may* need to reverse bytes
928
             * depending on if HW is active or not. */
929
            if (esp_sha_need_byte_reversal(&sha->ctx))
930
        #endif
931
0
        {
932
        #ifdef WOLFSSL_WIDE_BYTE
933
            WordsFromBytesBE32(sha->buffer, (const byte*)sha->buffer,
934
                WC_SHA_BLOCK_SIZE / 4);
935
        #else
936
0
            ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
937
0
        #endif
938
0
        }
939
0
    #endif
940
941
    #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
942
        /* if HW was busy, we may need to fall back to SW. */
943
        if (sha->ctx.mode == ESP32_SHA_SW) {
944
            ret = XTRANSFORM(sha, (const byte*)local);
945
        }
946
        else {
947
            ret = esp_sha_process(sha, (const byte*)local);
948
        }
949
    #else
950
        /*
951
        ** The #if defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW) also falls
952
        ** though here to SW, as it's not yet implemented for HW.
953
        */
954
0
        ret = XTRANSFORM(sha, (const byte*)local);
955
0
    #endif
956
0
        if (ret != 0) {
957
0
            return ret;
958
0
        }
959
960
0
        sha->buffLen = 0;
961
0
    } /* (sha->buffLen > WC_SHA_PAD_SIZE) */
962
963
0
    XMEMSET(&local[sha->buffLen], 0, WC_SHA_PAD_SIZE - sha->buffLen);
964
965
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
966
    if (sha->ctx.mode == ESP32_SHA_INIT) {
967
        esp_sha_try_hw_lock(&sha->ctx);
968
    }
969
#endif
970
971
    /* WOLFSSL_WIDE_BYTE packs the whole final block (including the length
972
     * words) octet-wise below, so skip the in-place word reversal here. */
973
0
#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) && \
974
0
    !defined(WOLFSSL_WIDE_BYTE)
975
    #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
976
          defined(CONFIG_IDF_TARGET_ESP8684) || \
977
          defined(CONFIG_IDF_TARGET_ESP32C3) || \
978
          defined(CONFIG_IDF_TARGET_ESP32C6)    \
979
        ) && \
980
         defined(WOLFSSL_ESP32_CRYPT) && \
981
        !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
982
        /* For Espressif RISC-V Targets, we *may* need to reverse bytes
983
         * depending on if HW is active or not. */
984
        if (esp_sha_need_byte_reversal(&sha->ctx))
985
    #endif
986
0
    { /* reminder local also points to sha->buffer  */
987
0
        ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
988
0
    }
989
0
#endif
990
991
    /* store lengths */
992
    /* put lengths in bits */
993
0
    sha->hiLen = (sha->loLen >> (CHAR_BIT*sizeof(sha->loLen) - 3)) +
994
0
                                                            (sha->hiLen << 3);
995
0
    sha->loLen = sha->loLen << 3;
996
997
    /* ! length ordering dependent on digest endian type ! */
998
#ifdef WOLFSSL_WIDE_BYTE
999
    /* CHAR_BIT != 8: place the 64-bit length as 8 big-endian octets, then pack
1000
     * all 16 big-endian schedule words octet-wise (covers the length words). */
1001
    local[WC_SHA_PAD_SIZE + 0] = (byte)((sha->hiLen >> 24) & 0xFF);
1002
    local[WC_SHA_PAD_SIZE + 1] = (byte)((sha->hiLen >> 16) & 0xFF);
1003
    local[WC_SHA_PAD_SIZE + 2] = (byte)((sha->hiLen >>  8) & 0xFF);
1004
    local[WC_SHA_PAD_SIZE + 3] = (byte)((sha->hiLen      ) & 0xFF);
1005
    local[WC_SHA_PAD_SIZE + 4] = (byte)((sha->loLen >> 24) & 0xFF);
1006
    local[WC_SHA_PAD_SIZE + 5] = (byte)((sha->loLen >> 16) & 0xFF);
1007
    local[WC_SHA_PAD_SIZE + 6] = (byte)((sha->loLen >>  8) & 0xFF);
1008
    local[WC_SHA_PAD_SIZE + 7] = (byte)((sha->loLen      ) & 0xFF);
1009
    WordsFromBytesBE32(sha->buffer, (const byte*)sha->buffer,
1010
        WC_SHA_BLOCK_SIZE / 4);
1011
#else
1012
0
    XMEMCPY(&local[WC_SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
1013
0
    XMEMCPY(&local[WC_SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
1014
0
#endif
1015
1016
#if defined(FREESCALE_MMCAU_SHA)
1017
    /* Kinetis requires only these bytes reversed */
1018
    ByteReverseWords(&sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)],
1019
                     &sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)],
1020
                     2 * sizeof(word32));
1021
#endif
1022
1023
1024
#if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
1025
      defined(CONFIG_IDF_TARGET_ESP8684) || \
1026
      defined(CONFIG_IDF_TARGET_ESP32C3) || \
1027
      defined(CONFIG_IDF_TARGET_ESP32C6)    \
1028
    ) && \
1029
    defined(WOLFSSL_ESP32_CRYPT) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
1030
if (sha->ctx.mode == ESP32_SHA_HW) {
1031
    #if defined(WOLFSSL_SUPER_VERBOSE_DEBUG)
1032
    {
1033
        ESP_LOGV(TAG, "Start: Reverse PAD SIZE Endianness.");
1034
    }
1035
    #endif
1036
    ByteReverseWords(&sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)], /* out */
1037
                     &sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)], /* in  */
1038
                     2 * sizeof(word32) /* byte count to reverse */
1039
                    );
1040
    #if defined(WOLFSSL_SUPER_VERBOSE_DEBUG)
1041
    {
1042
        ESP_LOGV(TAG, "End: Reverse PAD SIZE Endianness.");
1043
    }
1044
    #endif
1045
} /* end if (sha->ctx.mode == ESP32_SHA_HW) */
1046
#endif
1047
1048
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
1049
    if (sha->ctx.mode == ESP32_SHA_SW) {
1050
        ret = XTRANSFORM(sha, (const byte*)local);
1051
    }
1052
    else {
1053
        ret = esp_sha_digest_process(sha, 1);
1054
    }
1055
/*
1056
** The #if defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW) also falls
1057
** though here to SW, as it's not yet implemented for HW.
1058
*/
1059
#else
1060
0
    ret = XTRANSFORM(sha, (const byte*)local);
1061
0
#endif
1062
1063
#if defined(WOLFSSL_WIDE_BYTE)
1064
    /* CHAR_BIT != 8: write the digest words as big-endian octets. */
1065
    BytesFromWordsBE32(hash, sha->digest, WC_SHA_DIGEST_SIZE);
1066
#else
1067
0
#ifdef LITTLE_ENDIAN_ORDER
1068
    #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \
1069
          defined(CONFIG_IDF_TARGET_ESP8684) || \
1070
          defined(CONFIG_IDF_TARGET_ESP32C3) || \
1071
          defined(CONFIG_IDF_TARGET_ESP32C6)    \
1072
        ) && \
1073
        defined(WOLFSSL_ESP32_CRYPT) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
1074
        /* For Espressif RISC-V Targets, we *may* need to reverse bytes
1075
         * depending on if HW is active or not. */
1076
        if (esp_sha_need_byte_reversal(&sha->ctx))
1077
    #endif
1078
0
    {
1079
0
        ByteReverseWords(sha->digest, sha->digest, WC_SHA_DIGEST_SIZE);
1080
0
    }
1081
0
#endif
1082
1083
0
    XMEMCPY(hash, (byte *)&sha->digest[0], WC_SHA_DIGEST_SIZE);
1084
0
#endif
1085
1086
    /* we'll always reset state upon exit and return the error code from above,
1087
     * which may cause fall back to SW if HW is busy. we do not return result
1088
     * of initSha here */
1089
0
    (void)InitSha(sha); /* reset state */
1090
0
    return ret;
1091
0
}
1092
1093
#if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
1094
/* Apply SHA1 transformation to the data                  */
1095
/* @param sha  a pointer to wc_Sha structure              */
1096
/* @param data data to be applied SHA1 transformation     */
1097
/* @return 0 on successful, otherwise non-zero on failure */
1098
int wc_ShaTransform(wc_Sha* sha, const unsigned char* data)
1099
{
1100
    /* sanity check */
1101
    if (sha == NULL || data == NULL) {
1102
        return BAD_FUNC_ARG;
1103
    }
1104
    return (Transform(sha, data));
1105
}
1106
#endif
1107
1108
#endif /* USE_SHA_SOFTWARE_IMPL */
1109
1110
/*
1111
** This function initializes SHA. This is automatically called by wc_ShaHash.
1112
*/
1113
int wc_InitSha(wc_Sha* sha)
1114
0
{
1115
0
    return wc_InitSha_ex(sha, NULL, INVALID_DEVID);
1116
0
}
1117
1118
1119
#if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
1120
1121
#ifndef MAX3266X_SHA
1122
1123
void wc_ShaFree(wc_Sha* sha)
1124
0
{
1125
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
1126
    int ret = 0;
1127
#endif
1128
1129
0
    if (sha == NULL)
1130
0
        return;
1131
1132
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
1133
    #ifndef WOLF_CRYPTO_CB_FIND
1134
    if (sha->devId != INVALID_DEVID)
1135
    #endif
1136
    {
1137
        ret = wc_CryptoCb_Free(sha->devId, WC_ALGO_TYPE_HASH,
1138
                         WC_HASH_TYPE_SHA, 0, (void*)sha);
1139
        /* If they want the standard free, they can call it themselves */
1140
        /* via their callback setting devId to INVALID_DEVID */
1141
        /* otherwise assume the callback handled it */
1142
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1143
            return;
1144
        /* fall-through when unavailable */
1145
    }
1146
1147
    /* silence compiler warning */
1148
    (void)ret;
1149
1150
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */
1151
1152
#if defined(WOLFSSL_ESP32) &&  !defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
1153
    esp_sha_release_unfinished_lock(&sha->ctx);
1154
#endif
1155
1156
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
1157
    wolfAsync_DevCtxFree(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA);
1158
#endif /* WOLFSSL_ASYNC_CRYPT */
1159
1160
#ifdef WOLFSSL_PIC32MZ_HASH
1161
    wc_ShaPic32Free(sha);
1162
#endif
1163
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
1164
    se050_hash_free(&sha->se050Ctx);
1165
#endif
1166
#if (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
1167
     defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
1168
    !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) || \
1169
    (defined(WOLFSSL_RENESAS_RSIP) && (WOLFSSL_RENESAS_RZFSP_VER >= 220)) ||\
1170
    defined(WOLFSSL_RENESAS_RX64_HASH)
1171
    XFREE(sha->msg, sha->heap, DYNAMIC_TYPE_TMP_BUFFER);
1172
    sha->msg = NULL;
1173
#endif
1174
#ifdef WOLFSSL_IMXRT_DCP
1175
    DCPShaFree(sha);
1176
#endif
1177
1178
#ifdef WOLFSSL_HASH_KEEP
1179
    if (sha->msg != NULL) {
1180
        ForceZero(sha->msg, sha->len);
1181
        XFREE(sha->msg, sha->heap, DYNAMIC_TYPE_TMP_BUFFER);
1182
        sha->msg = NULL;
1183
    }
1184
#endif
1185
1186
#if defined(PSOC6_HASH_SHA1)
1187
    wc_Psoc6_Sha_Free();
1188
#endif
1189
0
}
1190
1191
#endif /* !MAX3266X_SHA */
1192
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
1193
#endif /* !WOLFSSL_TI_HASH */
1194
1195
#if !defined(WOLFSSL_TI_HASH) && !defined(WOLFSSL_IMXRT_DCP)
1196
1197
#if ((!defined(WOLFSSL_RENESAS_TSIP_TLS) && \
1198
     !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) || \
1199
    defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) && \
1200
    (!defined(WOLFSSL_RENESAS_RSIP) || \
1201
      defined(NO_WOLFSSL_RENESAS_FSPSM_HASH))
1202
#if !defined(WOLFSSL_RENESAS_RX64_HASH)
1203
1204
#if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)
1205
1206
#ifndef MAX3266X_SHA
1207
1208
/* wc_ShaGetHash get hash value */
1209
int wc_ShaGetHash(wc_Sha* sha, byte* hash)
1210
0
{
1211
0
    int ret;
1212
0
    WC_DECLARE_VAR(tmpSha, wc_Sha, 1, 0);
1213
1214
0
    if (sha == NULL || hash == NULL) {
1215
0
        return BAD_FUNC_ARG;
1216
0
    }
1217
1218
0
    WC_CALLOC_VAR_EX(tmpSha, wc_Sha, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
1219
0
        return MEMORY_E);
1220
1221
0
    ret = wc_ShaCopy(sha, tmpSha);
1222
0
    if (ret == 0) {
1223
0
        ret = wc_ShaFinal(tmpSha, hash);
1224
0
    }
1225
1226
0
    WC_FREE_VAR_EX(tmpSha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1227
1228
0
    return ret;
1229
0
}
1230
1231
int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
1232
0
{
1233
0
    int ret = 0;
1234
1235
0
    if (src == NULL || dst == NULL)
1236
0
        return BAD_FUNC_ARG;
1237
1238
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_COPY)
1239
    #ifndef WOLF_CRYPTO_CB_FIND
1240
    if (src->devId != INVALID_DEVID)
1241
    #endif
1242
    {
1243
        /* Cast the source and destination to be void to keep the abstraction */
1244
        ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH,
1245
                               WC_HASH_TYPE_SHA, (void*)src, (void*)dst);
1246
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1247
            return ret;
1248
        /* fall-through when unavailable */
1249
    }
1250
    ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
1251
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
1252
1253
    /* Free dst resources before copy to prevent memory leaks (e.g., msg
1254
     * buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
1255
0
    wc_ShaFree(dst);
1256
0
    XMEMCPY(dst, src, sizeof(wc_Sha));
1257
1258
#if defined(WOLFSSL_SILABS_SE_TYPES) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
1259
    dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
1260
    dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
1261
#endif
1262
1263
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
1264
    ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
1265
#endif
1266
1267
#ifdef WOLFSSL_PIC32MZ_HASH
1268
    ret = wc_Pic32HashCopy(&src->cache, &dst->cache, (byte*)src->buffer,
1269
        (byte*)dst->buffer, dst->heap);
1270
#endif
1271
1272
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)
1273
    ret = se050_hash_copy(&src->se050Ctx, &dst->se050Ctx);
1274
#endif
1275
1276
#if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW)
1277
    esp_sha_ctx_copy(src, dst);
1278
#endif
1279
1280
1281
#if defined(PSOC6_HASH_SHA1)
1282
    wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA1, 0);
1283
#endif
1284
1285
#ifdef WOLFSSL_HASH_FLAGS
1286
    dst->flags |= WC_HASH_FLAG_ISCOPY;
1287
#endif
1288
1289
#if defined(WOLFSSL_HASH_KEEP)
1290
    if (src->msg != NULL) {
1291
        dst->msg = (byte*)XMALLOC(src->len, dst->heap,
1292
                                  DYNAMIC_TYPE_TMP_BUFFER);
1293
        if (dst->msg == NULL) {
1294
            return MEMORY_E;
1295
        }
1296
        XMEMCPY(dst->msg, src->msg, src->used);
1297
    }
1298
#endif
1299
1300
0
    return ret;
1301
0
}
1302
#endif /* WOLFSSL_RENESAS_RX64_HASH */
1303
#endif /* !MAX3266X_SHA */
1304
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
1305
#endif /* !defined(WOLFSSL_RENESAS_TSIP_TLS) && \
1306
          !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY) ||
1307
          defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) */
1308
#endif /* !defined(WOLFSSL_TI_HASH) && !defined(WOLFSSL_IMXRT_DCP) */
1309
1310
#ifdef WOLFSSL_HASH_FLAGS
1311
int wc_ShaSetFlags(wc_Sha* sha, word32 flags)
1312
{
1313
    if (sha) {
1314
        sha->flags = flags;
1315
    }
1316
    return 0;
1317
}
1318
int wc_ShaGetFlags(wc_Sha* sha, word32* flags)
1319
{
1320
    if (sha && flags) {
1321
        *flags = sha->flags;
1322
    }
1323
    return 0;
1324
}
1325
#endif
1326
1327
#endif /* !NO_SHA */