Coverage Report

Created: 2026-08-15 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl-sp-math-all-8bit/wolfcrypt/src/sha3.c
Line
Count
Source
1
/* sha3.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-3 Build Options:
24
 *
25
 * Core:
26
 * WOLFSSL_SHA3:             Enable SHA-3 support                  default: off
27
 * WOLFSSL_SHA3_SMALL:       Use smaller SHA-3 implementation      default: off
28
 * WOLFSSL_SHAKE128:         Enable SHAKE128 XOF                   default: off
29
 * WOLFSSL_SHAKE256:         Enable SHAKE256 XOF                   default: off
30
 * SHA3_BY_SPEC:             Use specification Keccak-f order      default: off
31
 * WC_SHA3_NO_ASM:           Disable SHA-3 assembly optimizations  default: off
32
 * WC_SHA3_FAULT_HARDEN:     Harden SHA-3 against fault attacks    default: off
33
 * WC_SHA3_SPLIT64:          Run the Keccak permutation on 32-bit halves of each
34
 *                           64-bit lane so a compiler that lowers 64-bit bitwise
35
 *                           ops to out-of-line helper calls (e.g. cl2000 on TI
36
 *                           C28x) emits native 32-bit ops instead.  Auto-enabled
37
 *                           for little-endian WC_16BIT_CPU; the default
38
 *                           permutation is otherwise unchanged.    default: off
39
 *
40
 * Hardware Acceleration (SHA-3-specific):
41
 * WC_ASYNC_ENABLE_SHA3:     Enable async SHA-3 operations         default: off
42
 * WOLFSSL_ARMASM_CRYPTO_SHA3: ARM crypto SHA-3 instructions       default: off
43
 * STM32_HASH_SHA3:          STM32 hardware SHA-3                  default: off
44
 * PSOC6_HASH_SHA3:          PSoC6 hardware SHA-3                  default: off
45
 */
46
47
#define WC_FIPS_LL_CRYPTO
48
#define _WC_BUILDING_SHA3_C
49
50
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
51
52
#ifdef WC_SHA3_NO_ASM
53
    #undef USE_INTEL_SPEEDUP
54
    #undef WOLFSSL_ARMASM
55
    #undef WOLFSSL_RISCV_ASM
56
#endif
57
#ifdef WOLFSSL_X86_BUILD
58
    #undef USE_INTEL_SPEEDUP
59
#endif
60
61
#if defined(WOLFSSL_PSOC6_CRYPTO)
62
    #include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h>
63
#endif
64
65
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
66
   !defined(WOLFSSL_AFALG_XILINX_SHA3)
67
68
#if FIPS_VERSION3_GE(2,0,0)
69
    #ifdef USE_WINDOWS_API
70
        #pragma code_seg(".fipsA$n")
71
        #pragma const_seg(".fipsB$n")
72
    #endif
73
#endif
74
75
#include <wolfssl/wolfcrypt/sha3.h>
76
#include <wolfssl/wolfcrypt/hash.h>
77
78
#ifdef WOLF_CRYPTO_CB
79
    #include <wolfssl/wolfcrypt/cryptocb.h>
80
#endif
81
#ifdef NO_INLINE
82
    #include <wolfssl/wolfcrypt/misc.h>
83
#else
84
    #define WOLFSSL_MISC_INCLUDED
85
    #include <wolfcrypt/src/misc.c>
86
#endif
87
88
/* Gates the non-WOLFSSL_SHA3_SMALL software Keccak primitives
89
 * (hash_keccak_r, BlockSha3, InitSha3, Sha3Update, Sha3Final and the
90
 * Load64* helpers). Compiled when:
91
 *  - No HW SHA-3 backend is selected (the original baseline), OR
92
 *  - STM32 HW SHA-3 is selected and SHAKE is enabled - SHAKE on STM32MP13
93
 *    runs in software because the HASH peripheral's SHAKE support is
94
 *    fixed-length and does not match wolfSSL's variable-length / iterative
95
 *    SqueezeBlocks API. SHA-3 still uses the HASH peripheral.
96
 *
97
 * Note: the WOLFSSL_SHA3_SMALL branch earlier in this file defines its
98
 * own hash_keccak_r and BlockSha3 unconditionally inside its #ifdef
99
 * block, so this macro only controls the non-SMALL implementation. */
100
#if (!defined(STM32_HASH_SHA3) && !defined(PSOC6_HASH_SHA3)) || \
101
    (defined(STM32_HASH_SHA3) && \
102
     (defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256)))
103
    #define WC_SHA3_SW_KECCAK
104
#endif
105
106
#if FIPS_VERSION3_GE(6,0,0)
107
    const unsigned int wolfCrypt_FIPS_sha3_ro_sanity[2] =
108
                                                     { 0x1a2b3c4d, 0x00000016 };
109
    int wolfCrypt_FIPS_SHA3_sanity(void)
110
    {
111
        return 0;
112
    }
113
#endif
114
115
116
#if defined(USE_INTEL_SPEEDUP) || (defined(__aarch64__) && \
117
        defined(WOLFSSL_ARMASM))
118
    #include <wolfssl/wolfcrypt/cpuid.h>
119
120
    static cpuid_flags_t cpuid_flags = WC_CPUID_INITIALIZER;
121
#ifdef WC_C_DYNAMIC_FALLBACK
122
    #define SHA3_BLOCK (sha3->sha3_block)
123
    #define SHA3_BLOCK_N (sha3->sha3_block_n)
124
#else
125
    void (*sha3_block)(word64 *s) = NULL;
126
    void (*sha3_block_n)(word64 *s, const byte* data, word32 n,
127
        word64 c) = NULL;
128
    #define SHA3_BLOCK sha3_block
129
    #define SHA3_BLOCK_N sha3_block_n
130
#endif
131
#endif
132
133
#ifdef USE_INTEL_SPEEDUP
134
    /* Block-function selection when USE_INTEL_SPEEDUP: AVX2 on Intel, else
135
     * BMI2, else the C block.  Measured single-instance Keccak-f[1600]
136
     * (Ethereum "Optimizing Keccak"; OpenSSL keccak1600-x86_64.pl): AVX2 is
137
     * ~13-17% faster than BMI2 on Intel Haswell..Skylake, tied on Ice Lake,
138
     * but ~2x SLOWER on AMD Zen, so AVX2 is Intel-only.  (Single-stream
139
     * AVX-512 is vpermt2q-bound and slower than BMI2 everywhere measured, so
140
     * it is not built - see scripts sha3_avx512.rb.)
141
     * Overrides: WOLFSSL_SHA3_AVX2 forces AVX2 on any vendor with it;
142
     *            WOLFSSL_SHA3_NO_AVX2 never uses AVX2. */
143
    /* SHA3_USE_AVX2() is defined in sha3.h - shared with ML-DSA. */
144
145
    /* True when the selected block function uses vector registers and so
146
     * needs the caller to save/restore them.  BMI2 and the C block use only
147
     * general registers. */
148
#ifdef WOLFSSL_SHA3_NO_AVX2
149
    #define SHA3_BLOCK_VREGS(f) 0
150
#else
151
    #define SHA3_BLOCK_VREGS(f) ((f) == sha3_block_avx2)
152
#endif
153
#endif
154
155
#if !defined(WOLFSSL_ARMASM) && !defined(WOLFSSL_RISCV_ASM) && \
156
    !defined(WOLFSSL_PPC64_ASM) && !defined(WOLFSSL_PPC32_ASM)
157
158
#ifdef WOLFSSL_SHA3_SMALL
159
/* Rotate a 64-bit value left.
160
 *
161
 * a  Number to rotate left.
162
 * r  Number od bits to rotate left.
163
 * returns the rotated number.
164
 */
165
#define ROTL64(a, n)    (((a)<<(n))|((a)>>(64-(n))))
166
167
/* An array of values to XOR for block operation. */
168
static const word64 hash_keccak_r[24] =
169
{
170
    0x0000000000000001UL, 0x0000000000008082UL,
171
    0x800000000000808aUL, 0x8000000080008000UL,
172
    0x000000000000808bUL, 0x0000000080000001UL,
173
    0x8000000080008081UL, 0x8000000000008009UL,
174
    0x000000000000008aUL, 0x0000000000000088UL,
175
    0x0000000080008009UL, 0x000000008000000aUL,
176
    0x000000008000808bUL, 0x800000000000008bUL,
177
    0x8000000000008089UL, 0x8000000000008003UL,
178
    0x8000000000008002UL, 0x8000000000000080UL,
179
    0x000000000000800aUL, 0x800000008000000aUL,
180
    0x8000000080008081UL, 0x8000000000008080UL,
181
    0x0000000080000001UL, 0x8000000080008008UL
182
};
183
184
/* Indices used in swap and rotate operation. */
185
#define K_I_0   10
186
#define K_I_1    7
187
#define K_I_2   11
188
#define K_I_3   17
189
#define K_I_4   18
190
#define K_I_5    3
191
#define K_I_6    5
192
#define K_I_7   16
193
#define K_I_8    8
194
#define K_I_9   21
195
#define K_I_10  24
196
#define K_I_11   4
197
#define K_I_12  15
198
#define K_I_13  23
199
#define K_I_14  19
200
#define K_I_15  13
201
#define K_I_16  12
202
#define K_I_17   2
203
#define K_I_18  20
204
#define K_I_19  14
205
#define K_I_20  22
206
#define K_I_21   9
207
#define K_I_22   6
208
#define K_I_23   1
209
210
/* Number of bits to rotate in swap and rotate operation. */
211
#define K_R_0    1
212
#define K_R_1    3
213
#define K_R_2    6
214
#define K_R_3   10
215
#define K_R_4   15
216
#define K_R_5   21
217
#define K_R_6   28
218
#define K_R_7   36
219
#define K_R_8   45
220
#define K_R_9   55
221
#define K_R_10   2
222
#define K_R_11  14
223
#define K_R_12  27
224
#define K_R_13  41
225
#define K_R_14  56
226
#define K_R_15   8
227
#define K_R_16  25
228
#define K_R_17  43
229
#define K_R_18  62
230
#define K_R_19  18
231
#define K_R_20  39
232
#define K_R_21  61
233
#define K_R_22  20
234
#define K_R_23  44
235
236
/* Swap and rotate left operation.
237
 *
238
 * s   The state.
239
 * t1  Temporary value.
240
 * t2  Second temporary value.
241
 * i   The index of the loop.
242
 */
243
#define SWAP_ROTL(s, t1, t2, i)                                         \
244
do {                                                                    \
245
    t2 = s[K_I_##i]; s[K_I_##i] = ROTL64(t1, K_R_##i);                  \
246
}                                                                       \
247
while (0)
248
249
/* Mix the XOR of the column's values into each number by column.
250
 *
251
 * s  The state.
252
 * b  Temporary array of XORed column values.
253
 * x  The index of the column.
254
 * t  Temporary variable.
255
 */
256
#define COL_MIX(s, b, x, t)                                             \
257
do {                                                                    \
258
    for (x = 0; x < 5; x++)                                             \
259
        b[x] = s[x + 0] ^ s[x + 5] ^ s[x + 10] ^ s[x + 15] ^ s[x + 20]; \
260
    for (x = 0; x < 5; x++) {                                           \
261
        t = b[(x + 4) % 5] ^ ROTL64(b[(x + 1) % 5], 1);                 \
262
        s[x +  0] ^= t;                                                 \
263
        s[x +  5] ^= t;                                                 \
264
        s[x + 10] ^= t;                                                 \
265
        s[x + 15] ^= t;                                                 \
266
        s[x + 20] ^= t;                                                 \
267
    }                                                                   \
268
}                                                                       \
269
while (0)
270
271
#ifdef SHA3_BY_SPEC
272
/* Mix the row values.
273
 * BMI1 has ANDN instruction ((~a) & b) - Haswell and above.
274
 *
275
 * s   The state.
276
 * b   Temporary array of XORed row values.
277
 * y   The index of the row to work on.
278
 * x   The index of the column.
279
 * t0  Temporary variable.
280
 * t1  Temporary variable.
281
 */
282
#define ROW_MIX(s, b, y, x, t0, t1)                                     \
283
do {                                                                    \
284
    for (y = 0; y < 5; y++) {                                           \
285
        for (x = 0; x < 5; x++)                                         \
286
            b[x] = s[y * 5 + x];                                        \
287
        for (x = 0; x < 5; x++)                                         \
288
            s[y * 5 + x] = b[x] ^ (~b[(x + 1) % 5] & b[(x + 2) % 5]);   \
289
    }                                                                   \
290
}                                                                       \
291
while (0)
292
#else
293
/* Mix the row values.
294
 * a ^ (~b & c) == a ^ (c & (b ^ c)) == (a ^ b) ^ (b | c)
295
 *
296
 * s   The state.
297
 * b   Temporary array of XORed row values.
298
 * y   The index of the row to work on.
299
 * x   The index of the column.
300
 * t0  Temporary variable.
301
 * t1  Temporary variable.
302
 */
303
#define ROW_MIX(s, b, y, x, t12, t34)                                   \
304
do {                                                                    \
305
    for (y = 0; y < 5; y++) {                                           \
306
        for (x = 0; x < 5; x++)                                         \
307
            b[x] = s[y * 5 + x];                                        \
308
        t12 = (b[1] ^ b[2]); t34 = (b[3] ^ b[4]);                       \
309
        s[y * 5 + 0] = b[0] ^ (b[2] &  t12);                            \
310
        s[y * 5 + 1] =  t12 ^ (b[2] | b[3]);                            \
311
        s[y * 5 + 2] = b[2] ^ (b[4] &  t34);                            \
312
        s[y * 5 + 3] =  t34 ^ (b[4] | b[0]);                            \
313
        s[y * 5 + 4] = b[4] ^ (b[1] & (b[0] ^ b[1]));                   \
314
    }                                                                   \
315
}                                                                       \
316
while (0)
317
#endif /* SHA3_BY_SPEC */
318
319
/* The block operation performed on the state.
320
 *
321
 * s  The state.
322
 */
323
void BlockSha3(word64* s)
324
{
325
    byte i, x, y;
326
    word64 t0, t1;
327
    word64 b[5];
328
329
    for (i = 0; i < 24; i++)
330
    {
331
        COL_MIX(s, b, x, t0);
332
333
        t0 = s[1];
334
        SWAP_ROTL(s, t0, t1,  0);
335
        SWAP_ROTL(s, t1, t0,  1);
336
        SWAP_ROTL(s, t0, t1,  2);
337
        SWAP_ROTL(s, t1, t0,  3);
338
        SWAP_ROTL(s, t0, t1,  4);
339
        SWAP_ROTL(s, t1, t0,  5);
340
        SWAP_ROTL(s, t0, t1,  6);
341
        SWAP_ROTL(s, t1, t0,  7);
342
        SWAP_ROTL(s, t0, t1,  8);
343
        SWAP_ROTL(s, t1, t0,  9);
344
        SWAP_ROTL(s, t0, t1, 10);
345
        SWAP_ROTL(s, t1, t0, 11);
346
        SWAP_ROTL(s, t0, t1, 12);
347
        SWAP_ROTL(s, t1, t0, 13);
348
        SWAP_ROTL(s, t0, t1, 14);
349
        SWAP_ROTL(s, t1, t0, 15);
350
        SWAP_ROTL(s, t0, t1, 16);
351
        SWAP_ROTL(s, t1, t0, 17);
352
        SWAP_ROTL(s, t0, t1, 18);
353
        SWAP_ROTL(s, t1, t0, 19);
354
        SWAP_ROTL(s, t0, t1, 20);
355
        SWAP_ROTL(s, t1, t0, 21);
356
        SWAP_ROTL(s, t0, t1, 22);
357
        SWAP_ROTL(s, t1, t0, 23);
358
359
        ROW_MIX(s, b, y, x, t0, t1);
360
361
        s[0] ^= hash_keccak_r[i];
362
    }
363
}
364
#else
365
/* Rotate a 64-bit value left.
366
 *
367
 * a  Number to rotate left.
368
 * r  Number od bits to rotate left.
369
 * returns the rotated number.
370
 */
371
157M
#define ROTL64(a, n)    (((a)<<(n))|((a)>>(64-(n))))
372
373
#ifdef WC_SHA3_SW_KECCAK
374
/* An array of values to XOR for block operation. */
375
static const word64 hash_keccak_r[24] =
376
{
377
    W64LIT(0x0000000000000001), W64LIT(0x0000000000008082),
378
    W64LIT(0x800000000000808a), W64LIT(0x8000000080008000),
379
    W64LIT(0x000000000000808b), W64LIT(0x0000000080000001),
380
    W64LIT(0x8000000080008081), W64LIT(0x8000000000008009),
381
    W64LIT(0x000000000000008a), W64LIT(0x0000000000000088),
382
    W64LIT(0x0000000080008009), W64LIT(0x000000008000000a),
383
    W64LIT(0x000000008000808b), W64LIT(0x800000000000008b),
384
    W64LIT(0x8000000000008089), W64LIT(0x8000000000008003),
385
    W64LIT(0x8000000000008002), W64LIT(0x8000000000000080),
386
    W64LIT(0x000000000000800a), W64LIT(0x800000008000000a),
387
    W64LIT(0x8000000080008081), W64LIT(0x8000000000008080),
388
    W64LIT(0x0000000080000001), W64LIT(0x8000000080008008)
389
};
390
#endif
391
392
/* Indices used in swap and rotate operation. */
393
#define KI_0     6
394
#define KI_1    12
395
#define KI_2    18
396
#define KI_3    24
397
#define KI_4     3
398
#define KI_5     9
399
#define KI_6    10
400
#define KI_7    16
401
#define KI_8    22
402
#define KI_9     1
403
#define KI_10    7
404
#define KI_11   13
405
#define KI_12   19
406
#define KI_13   20
407
#define KI_14    4
408
#define KI_15    5
409
#define KI_16   11
410
#define KI_17   17
411
#define KI_18   23
412
#define KI_19    2
413
#define KI_20    8
414
#define KI_21   14
415
#define KI_22   15
416
#define KI_23   21
417
418
/* Number of bits to rotate in swap and rotate operation. */
419
#define KR_0    44
420
#define KR_1    43
421
#define KR_2    21
422
#define KR_3    14
423
#define KR_4    28
424
#define KR_5    20
425
#define KR_6     3
426
#define KR_7    45
427
#define KR_8    61
428
#define KR_9     1
429
#define KR_10    6
430
#define KR_11   25
431
#define KR_12    8
432
#define KR_13   18
433
#define KR_14   27
434
#define KR_15   36
435
#define KR_16   10
436
#define KR_17   15
437
#define KR_18   56
438
#define KR_19   62
439
#define KR_20   55
440
#define KR_21   39
441
#define KR_22   41
442
#define KR_23    2
443
444
/* Mix the XOR of the column's values into each number by column.
445
 *
446
 * s  The state.
447
 * b  Temporary array of XORed column values.
448
 * x  The index of the column.
449
 * t  Temporary variable.
450
 */
451
5.42M
#define COL_MIX(s, b, x, t)                                                         \
452
5.42M
do {                                                                                \
453
5.42M
    (b)[0] = (s)[0] ^ (s)[5] ^ (s)[10] ^ (s)[15] ^ (s)[20];                         \
454
5.42M
    (b)[1] = (s)[1] ^ (s)[6] ^ (s)[11] ^ (s)[16] ^ (s)[21];                         \
455
5.42M
    (b)[2] = (s)[2] ^ (s)[7] ^ (s)[12] ^ (s)[17] ^ (s)[22];                         \
456
5.42M
    (b)[3] = (s)[3] ^ (s)[8] ^ (s)[13] ^ (s)[18] ^ (s)[23];                         \
457
5.42M
    (b)[4] = (s)[4] ^ (s)[9] ^ (s)[14] ^ (s)[19] ^ (s)[24];                         \
458
5.42M
    (t) = (b)[(0 + 4) % 5] ^ ROTL64((b)[(0 + 1) % 5], 1);                           \
459
5.42M
    (s)[ 0] ^= (t); (s)[ 5] ^= (t); (s)[10] ^= (t); (s)[15] ^= (t); (s)[20] ^= (t); \
460
5.42M
    (t) = (b)[(1 + 4) % 5] ^ ROTL64((b)[(1 + 1) % 5], 1);                           \
461
5.42M
    (s)[ 1] ^= (t); (s)[ 6] ^= (t); (s)[11] ^= (t); (s)[16] ^= (t); (s)[21] ^= (t); \
462
5.42M
    (t) = (b)[(2 + 4) % 5] ^ ROTL64((b)[(2 + 1) % 5], 1);                           \
463
5.42M
    (s)[ 2] ^= (t); (s)[ 7] ^= (t); (s)[12] ^= (t); (s)[17] ^= (t); (s)[22] ^= (t); \
464
5.42M
    (t) = (b)[(3 + 4) % 5] ^ ROTL64((b)[(3 + 1) % 5], 1);                           \
465
5.42M
    (s)[ 3] ^= (t); (s)[ 8] ^= (t); (s)[13] ^= (t); (s)[18] ^= (t); (s)[23] ^= (t); \
466
5.42M
    (t) = (b)[(4 + 4) % 5] ^ ROTL64((b)[(4 + 1) % 5], 1);                           \
467
5.42M
    (s)[ 4] ^= (t); (s)[ 9] ^= (t); (s)[14] ^= (t); (s)[19] ^= (t); (s)[24] ^= (t); \
468
5.42M
}                                                                                   \
469
5.42M
while (0)
470
471
130M
#define S(s1, i) ROTL64((s1)[KI_##i], KR_##i)
472
473
#ifdef SHA3_BY_SPEC
474
/* Mix the row values.
475
 * BMI1 has ANDN instruction ((~a) & b) - Haswell and above.
476
 *
477
 * s2  The new state.
478
 * s1  The current state.
479
 * b   Temporary array of XORed row values.
480
 * t0  Temporary variable. (Unused)
481
 * t1  Temporary variable. (Unused)
482
 */
483
#define ROW_MIX(s2, s1, b, t0, t1)                    \
484
do {                                                  \
485
    (b)[0] = (s1)[0];                                 \
486
    (b)[1] = S((s1), 0);                              \
487
    (b)[2] = S((s1), 1);                              \
488
    (b)[3] = S((s1), 2);                              \
489
    (b)[4] = S((s1), 3);                              \
490
    (s2)[0] = (b)[0] ^ (~(b)[1] & (b)[2]);            \
491
    (s2)[1] = (b)[1] ^ (~(b)[2] & (b)[3]);            \
492
    (s2)[2] = (b)[2] ^ (~(b)[3] & (b)[4]);            \
493
    (s2)[3] = (b)[3] ^ (~(b)[4] & (b)[0]);            \
494
    (s2)[4] = (b)[4] ^ (~(b)[0] & (b)[1]);            \
495
    (b)[0] = S((s1), 4);                              \
496
    (b)[1] = S((s1), 5);                              \
497
    (b)[2] = S((s1), 6);                              \
498
    (b)[3] = S((s1), 7);                              \
499
    (b)[4] = S((s1), 8);                              \
500
    (s2)[5] = (b)[0] ^ (~(b)[1] & (b)[2]);            \
501
    (s2)[6] = (b)[1] ^ (~(b)[2] & (b)[3]);            \
502
    (s2)[7] = (b)[2] ^ (~(b)[3] & (b)[4]);            \
503
    (s2)[8] = (b)[3] ^ (~(b)[4] & (b)[0]);            \
504
    (s2)[9] = (b)[4] ^ (~(b)[0] & (b)[1]);            \
505
    (b)[0] = S((s1), 9);                              \
506
    (b)[1] = S((s1), 10);                             \
507
    (b)[2] = S((s1), 11);                             \
508
    (b)[3] = S((s1), 12);                             \
509
    (b)[4] = S((s1), 13);                             \
510
    (s2)[10] = (b)[0] ^ (~(b)[1] & (b)[2]);           \
511
    (s2)[11] = (b)[1] ^ (~(b)[2] & (b)[3]);           \
512
    (s2)[12] = (b)[2] ^ (~(b)[3] & (b)[4]);           \
513
    (s2)[13] = (b)[3] ^ (~(b)[4] & (b)[0]);           \
514
    (s2)[14] = (b)[4] ^ (~(b)[0] & (b)[1]);           \
515
    (b)[0] = S((s1), 14);                             \
516
    (b)[1] = S((s1), 15);                             \
517
    (b)[2] = S((s1), 16);                             \
518
    (b)[3] = S((s1), 17);                             \
519
    (b)[4] = S((s1), 18);                             \
520
    (s2)[15] = (b)[0] ^ (~(b)[1] & (b)[2]);           \
521
    (s2)[16] = (b)[1] ^ (~(b)[2] & (b)[3]);           \
522
    (s2)[17] = (b)[2] ^ (~(b)[3] & (b)[4]);           \
523
    (s2)[18] = (b)[3] ^ (~(b)[4] & (b)[0]);           \
524
    (s2)[19] = (b)[4] ^ (~(b)[0] & (b)[1]);           \
525
    (b)[0] = S((s1), 19);                             \
526
    (b)[1] = S((s1), 20);                             \
527
    (b)[2] = S((s1), 21);                             \
528
    (b)[3] = S((s1), 22);                             \
529
    (b)[4] = S((s1), 23);                             \
530
    (s2)[20] = (b)[0] ^ (~(b)[1] & (b)[2]);           \
531
    (s2)[21] = (b)[1] ^ (~(b)[2] & (b)[3]);           \
532
    (s2)[22] = (b)[2] ^ (~(b)[3] & (b)[4]);           \
533
    (s2)[23] = (b)[3] ^ (~(b)[4] & (b)[0]);           \
534
    (s2)[24] = (b)[4] ^ (~(b)[0] & (b)[1]);           \
535
}                                                     \
536
while (0)
537
#else
538
/* Mix the row values.
539
 * a ^ (~b & c) == a ^ (c & (b ^ c)) == (a ^ b) ^ (b | c)
540
 *
541
 * s2  The new state.
542
 * s1  The current state.
543
 * b   Temporary array of XORed row values.
544
 * t12 Temporary variable.
545
 * t34 Temporary variable.
546
 */
547
5.42M
#define ROW_MIX(s2, s1, b, t12, t34)                      \
548
5.42M
do {                                                      \
549
5.42M
    (b)[0] = (s1)[0];                                     \
550
5.42M
    (b)[1] = S((s1), 0);                                  \
551
5.42M
    (b)[2] = S((s1), 1);                                  \
552
5.42M
    (b)[3] = S((s1), 2);                                  \
553
5.42M
    (b)[4] = S((s1), 3);                                  \
554
5.42M
    (t12) = ((b)[1] ^ (b)[2]); (t34) = ((b)[3] ^ (b)[4]); \
555
5.42M
    (s2)[0] = (b)[0] ^ ((b)[2] &  (t12));                 \
556
5.42M
    (s2)[1] =  (t12) ^ ((b)[2] | (b)[3]);                 \
557
5.42M
    (s2)[2] = (b)[2] ^ ((b)[4] &  (t34));                 \
558
5.42M
    (s2)[3] =  (t34) ^ ((b)[4] | (b)[0]);                 \
559
5.42M
    (s2)[4] = (b)[4] ^ ((b)[1] & ((b)[0] ^ (b)[1]));      \
560
5.42M
    (b)[0] = S((s1), 4);                                  \
561
5.42M
    (b)[1] = S((s1), 5);                                  \
562
5.42M
    (b)[2] = S((s1), 6);                                  \
563
5.42M
    (b)[3] = S((s1), 7);                                  \
564
5.42M
    (b)[4] = S((s1), 8);                                  \
565
5.42M
    (t12) = ((b)[1] ^ (b)[2]); (t34) = ((b)[3] ^ (b)[4]); \
566
5.42M
    (s2)[5] = (b)[0] ^ ((b)[2] &  (t12));                 \
567
5.42M
    (s2)[6] =  (t12) ^ ((b)[2] | (b)[3]);                 \
568
5.42M
    (s2)[7] = (b)[2] ^ ((b)[4] &  (t34));                 \
569
5.42M
    (s2)[8] =  (t34) ^ ((b)[4] | (b)[0]);                 \
570
5.42M
    (s2)[9] = (b)[4] ^ ((b)[1] & ((b)[0] ^ (b)[1]));      \
571
5.42M
    (b)[0] = S((s1), 9);                                  \
572
5.42M
    (b)[1] = S((s1), 10);                                 \
573
5.42M
    (b)[2] = S((s1), 11);                                 \
574
5.42M
    (b)[3] = S((s1), 12);                                 \
575
5.42M
    (b)[4] = S((s1), 13);                                 \
576
5.42M
    (t12) = ((b)[1] ^ (b)[2]); (t34) = ((b)[3] ^ (b)[4]); \
577
5.42M
    (s2)[10] = (b)[0] ^ ((b)[2] &  (t12));                \
578
5.42M
    (s2)[11] =  (t12) ^ ((b)[2] | (b)[3]);                \
579
5.42M
    (s2)[12] = (b)[2] ^ ((b)[4] &  (t34));                \
580
5.42M
    (s2)[13] =  (t34) ^ ((b)[4] | (b)[0]);                \
581
5.42M
    (s2)[14] = (b)[4] ^ ((b)[1] & ((b)[0] ^ (b)[1]));     \
582
5.42M
    (b)[0] = S((s1), 14);                                 \
583
5.42M
    (b)[1] = S((s1), 15);                                 \
584
5.42M
    (b)[2] = S((s1), 16);                                 \
585
5.42M
    (b)[3] = S((s1), 17);                                 \
586
5.42M
    (b)[4] = S((s1), 18);                                 \
587
5.42M
    (t12) = ((b)[1] ^ (b)[2]); (t34) = ((b)[3] ^ (b)[4]); \
588
5.42M
    (s2)[15] = (b)[0] ^ ((b)[2] &  (t12));                \
589
5.42M
    (s2)[16] =  (t12) ^ ((b)[2] | (b)[3]);                \
590
5.42M
    (s2)[17] = (b)[2] ^ ((b)[4] &  (t34));                \
591
5.42M
    (s2)[18] =  (t34) ^ ((b)[4] | (b)[0]);                \
592
5.42M
    (s2)[19] = (b)[4] ^ ((b)[1] & ((b)[0] ^ (b)[1]));     \
593
5.42M
    (b)[0] = S((s1), 19);                                 \
594
5.42M
    (b)[1] = S((s1), 20);                                 \
595
5.42M
    (b)[2] = S((s1), 21);                                 \
596
5.42M
    (b)[3] = S((s1), 22);                                 \
597
5.42M
    (b)[4] = S((s1), 23);                                 \
598
5.42M
    (t12) = ((b)[1] ^ (b)[2]); (t34) = ((b)[3] ^ (b)[4]); \
599
5.42M
    (s2)[20] = (b)[0] ^ ((b)[2] &  (t12));                \
600
5.42M
    (s2)[21] =  (t12) ^ ((b)[2] | (b)[3]);                \
601
5.42M
    (s2)[22] = (b)[2] ^ ((b)[4] &  (t34));                \
602
5.42M
    (s2)[23] =  (t34) ^ ((b)[4] | (b)[0]);                \
603
5.42M
    (s2)[24] = (b)[4] ^ ((b)[1] & ((b)[0] ^ (b)[1]));     \
604
5.42M
}                                                         \
605
5.42M
while (0)
606
#endif /* SHA3_BY_SPEC */
607
608
#ifdef WC_SHA3_SW_KECCAK
609
/* The block operation performed on the state.
610
 *
611
 * s  The state.
612
 */
613
614
/* WC_16BIT_CPU (e.g. TI C28x) lowers every 64-bit ^, | and & to an out-of-line
615
 * runtime-helper call (cl2000: __c28xabi_xorll / _orll / _andll), which
616
 * dominates the Keccak permutation.  Auto-select a BlockSha3 that runs on
617
 * 32-bit halves so the compiler emits native 32-bit ops; external state stays
618
 * word64 s[25].  Auto-enabled only for WOLFSSL_WIDE_BYTE (the hardware-validated
619
 * targets); other little-endian 16-bit ports keep the long-tested generic
620
 * permutation but can opt in by defining WC_SHA3_SPLIT64.  Little-endian word
621
 * layout assumed (lo half first). */
622
#if !defined(WC_SHA3_SPLIT64) && defined(WOLFSSL_WIDE_BYTE) && \
623
    !defined(BIG_ENDIAN_ORDER)
624
    #define WC_SHA3_SPLIT64
625
#endif
626
627
#ifdef WC_SHA3_SPLIT64
628
629
/* Rotate the 64-bit value (sl=low, sh=high) left by compile-time constant r in
630
 * 1..63, r != 32, into (dl, dh).  r is always a Keccak rho offset (never 0 or
631
 * 32; r==32 would need a plain half-swap), so that case never occurs.  The & 31
632
 * keeps the shift count in range in the dead (compile-time-eliminated) branch
633
 * so there is no undefined shift. */
634
#define WC_SHA3_RL(dl, dh, sl, sh, r)                                        \
635
    do {                                                                     \
636
        word32 _l = (sl), _h = (sh);                                         \
637
        if ((r) < 32) {                                                      \
638
            (dl) = (word32)((_l << ((r) & 31)) | (_h >> ((32 - (r)) & 31))); \
639
            (dh) = (word32)((_h << ((r) & 31)) | (_l >> ((32 - (r)) & 31))); \
640
        }                                                                    \
641
        else {                                                               \
642
            (dl) = (word32)((_h << (((r) - 32) & 31)) |                      \
643
                            (_l >> ((64 - (r)) & 31)));                      \
644
            (dh) = (word32)((_l << (((r) - 32) & 31)) |                      \
645
                            (_h >> ((64 - (r)) & 31)));                      \
646
        }                                                                    \
647
    } while (0)
648
649
/* Chi over the rotated row held in bl[0..4]/bh[0..4], writing five output lanes
650
 * at (DL,DH)[k..k+4].  a ^ (~b & c) == (a ^ b) ^ (b | c) per half. */
651
#define WC_SHA3_CHI(DL, DH, k)                                  \
652
    do {                                                        \
653
        word32 al = bl[1] ^ bl[2], ah = bh[1] ^ bh[2];          \
654
        word32 cl = bl[3] ^ bl[4], ch = bh[3] ^ bh[4];          \
655
        (DL)[(k)+0] = bl[0] ^ (bl[2] &  al);                    \
656
        (DH)[(k)+0] = bh[0] ^ (bh[2] &  ah);                    \
657
        (DL)[(k)+1] =  al   ^ (bl[2] | bl[3]);                  \
658
        (DH)[(k)+1] =  ah   ^ (bh[2] | bh[3]);                  \
659
        (DL)[(k)+2] = bl[2] ^ (bl[4] &  cl);                    \
660
        (DH)[(k)+2] = bh[2] ^ (bh[4] &  ch);                    \
661
        (DL)[(k)+3] =  cl   ^ (bl[4] | bl[0]);                  \
662
        (DH)[(k)+3] =  ch   ^ (bh[4] | bh[0]);                  \
663
        (DL)[(k)+4] = bl[4] ^ (bl[1] & (bl[0] ^ bl[1]));        \
664
        (DH)[(k)+4] = bh[4] ^ (bh[1] & (bh[0] ^ bh[1]));        \
665
    } while (0)
666
667
/* Theta: mix the column parities into split state L (low) / H (high). */
668
#define WC_SHA3_THETA(L, H)                                                   \
669
    do {                                                                      \
670
        int c;                                                                \
671
        for (c = 0; c < 5; c++) {                                             \
672
            bl[c] = (L)[c]^(L)[c+5]^(L)[c+10]^(L)[c+15]^(L)[c+20];            \
673
            bh[c] = (H)[c]^(H)[c+5]^(H)[c+10]^(H)[c+15]^(H)[c+20];            \
674
        }                                                                     \
675
        for (c = 0; c < 5; c++) {                                             \
676
            int d = (c + 1) % 5, e = (c + 4) % 5;                             \
677
            word32 xl = bl[e] ^ (word32)((bl[d] << 1) | (bh[d] >> 31));       \
678
            word32 xh = bh[e] ^ (word32)((bh[d] << 1) | (bl[d] >> 31));       \
679
            (L)[c]   ^= xl; (H)[c]   ^= xh; (L)[c+5]  ^= xl; (H)[c+5]  ^= xh; \
680
            (L)[c+10]^= xl; (H)[c+10]^= xh; (L)[c+15] ^= xl; (H)[c+15] ^= xh; \
681
            (L)[c+20]^= xl; (H)[c+20]^= xh;                                   \
682
        }                                                                     \
683
    } while (0)
684
685
/* Rho + pi + chi: rotate/permute split state SL/SH into DL/DH. */
686
#define WC_SHA3_ROWMIX(DL, DH, SL, SH)                            \
687
    do {                                                          \
688
        bl[0] = (SL)[0]; bh[0] = (SH)[0];                         \
689
        WC_SHA3_RL(bl[1],bh[1], (SL)[KI_0], (SH)[KI_0],  KR_0);   \
690
        WC_SHA3_RL(bl[2],bh[2], (SL)[KI_1], (SH)[KI_1],  KR_1);   \
691
        WC_SHA3_RL(bl[3],bh[3], (SL)[KI_2], (SH)[KI_2],  KR_2);   \
692
        WC_SHA3_RL(bl[4],bh[4], (SL)[KI_3], (SH)[KI_3],  KR_3);   \
693
        WC_SHA3_CHI(DL, DH, 0);                                   \
694
        WC_SHA3_RL(bl[0],bh[0], (SL)[KI_4], (SH)[KI_4],  KR_4);   \
695
        WC_SHA3_RL(bl[1],bh[1], (SL)[KI_5], (SH)[KI_5],  KR_5);   \
696
        WC_SHA3_RL(bl[2],bh[2], (SL)[KI_6], (SH)[KI_6],  KR_6);   \
697
        WC_SHA3_RL(bl[3],bh[3], (SL)[KI_7], (SH)[KI_7],  KR_7);   \
698
        WC_SHA3_RL(bl[4],bh[4], (SL)[KI_8], (SH)[KI_8],  KR_8);   \
699
        WC_SHA3_CHI(DL, DH, 5);                                   \
700
        WC_SHA3_RL(bl[0],bh[0], (SL)[KI_9], (SH)[KI_9],  KR_9);   \
701
        WC_SHA3_RL(bl[1],bh[1], (SL)[KI_10],(SH)[KI_10], KR_10);  \
702
        WC_SHA3_RL(bl[2],bh[2], (SL)[KI_11],(SH)[KI_11], KR_11);  \
703
        WC_SHA3_RL(bl[3],bh[3], (SL)[KI_12],(SH)[KI_12], KR_12);  \
704
        WC_SHA3_RL(bl[4],bh[4], (SL)[KI_13],(SH)[KI_13], KR_13);  \
705
        WC_SHA3_CHI(DL, DH, 10);                                  \
706
        WC_SHA3_RL(bl[0],bh[0], (SL)[KI_14],(SH)[KI_14], KR_14);  \
707
        WC_SHA3_RL(bl[1],bh[1], (SL)[KI_15],(SH)[KI_15], KR_15);  \
708
        WC_SHA3_RL(bl[2],bh[2], (SL)[KI_16],(SH)[KI_16], KR_16);  \
709
        WC_SHA3_RL(bl[3],bh[3], (SL)[KI_17],(SH)[KI_17], KR_17);  \
710
        WC_SHA3_RL(bl[4],bh[4], (SL)[KI_18],(SH)[KI_18], KR_18);  \
711
        WC_SHA3_CHI(DL, DH, 15);                                  \
712
        WC_SHA3_RL(bl[0],bh[0], (SL)[KI_19],(SH)[KI_19], KR_19);  \
713
        WC_SHA3_RL(bl[1],bh[1], (SL)[KI_20],(SH)[KI_20], KR_20);  \
714
        WC_SHA3_RL(bl[2],bh[2], (SL)[KI_21],(SH)[KI_21], KR_21);  \
715
        WC_SHA3_RL(bl[3],bh[3], (SL)[KI_22],(SH)[KI_22], KR_22);  \
716
        WC_SHA3_RL(bl[4],bh[4], (SL)[KI_23],(SH)[KI_23], KR_23);  \
717
        WC_SHA3_CHI(DL, DH, 20);                                  \
718
    } while (0)
719
720
void BlockSha3(word64* s)
721
{
722
    /* Process the 25 little-endian lanes as 32-bit halves to avoid 64-bit
723
     * helper calls.  XMEMCPY in/out (aliasing s through word32* is strict-
724
     * aliasing UB); st[2k] is lane k's low half, st[2k+1] the high half.
725
     * Round constants are split with shifts for the same reason. */
726
    word32 st[50];
727
    word32 sl[25], sh[25], nl[25], nh[25], bl[5], bh[5];
728
    word32 i, k;
729
    word64 rc;
730
731
    XMEMCPY(st, s, sizeof(st));
732
    for (k = 0; k < 25; k++) {
733
        sl[k] = st[2 * k];
734
        sh[k] = st[2 * k + 1];
735
    }
736
    for (i = 0; i < 24; i += 2) {
737
        WC_SHA3_THETA(sl, sh);
738
        WC_SHA3_ROWMIX(nl, nh, sl, sh);
739
        rc = hash_keccak_r[i];
740
        nl[0] ^= (word32)rc;          nh[0] ^= (word32)(rc >> 32);
741
        WC_SHA3_THETA(nl, nh);
742
        WC_SHA3_ROWMIX(sl, sh, nl, nh);
743
        rc = hash_keccak_r[i + 1];
744
        sl[0] ^= (word32)rc;          sh[0] ^= (word32)(rc >> 32);
745
    }
746
    for (k = 0; k < 25; k++) {
747
        st[2 * k]     = sl[k];
748
        st[2 * k + 1] = sh[k];
749
    }
750
    XMEMCPY(s, st, sizeof(st));
751
}
752
753
#undef WC_SHA3_RL
754
#undef WC_SHA3_CHI
755
#undef WC_SHA3_THETA
756
#undef WC_SHA3_ROWMIX
757
758
#else /* !WC_SHA3_SPLIT64 */
759
760
void BlockSha3(word64* s)
761
225k
{
762
225k
    word64 n[25];
763
225k
    word64 b[5];
764
225k
    word64 t0;
765
225k
#ifndef SHA3_BY_SPEC
766
225k
    word64 t1;
767
225k
#endif
768
225k
    word32 i;
769
770
2.93M
    for (i = 0; i < 24; i += 2)
771
2.71M
    {
772
2.71M
        COL_MIX(s, b, x, t0);
773
2.71M
        ROW_MIX(n, s, b, t0, t1);
774
2.71M
        n[0] ^= hash_keccak_r[i];
775
776
2.71M
        COL_MIX(n, b, x, t0);
777
2.71M
        ROW_MIX(s, n, b, t0, t1);
778
2.71M
        s[0] ^= hash_keccak_r[i+1];
779
2.71M
    }
780
225k
}
781
782
#endif /* WC_SHA3_SPLIT64 */
783
#endif /* WC_SHA3_SW_KECCAK */
784
#endif /* !WOLFSSL_SHA3_SMALL */
785
#endif /* !WOLFSSL_ARMASM && !WOLFSSL_RISCV_ASM && !WOLFSSL_PPC64_ASM &&
786
        * !WOLFSSL_PPC32_ASM */
787
788
#if defined(WOLFSSL_PPC64_ASM)
789
#if defined(WOLFSSL_PPC64_ASM_POWER8)
790
/* PowerPC64 provides two Keccak-f[1600] implementations: the scalar
791
 * BlockSha3_base and a POWER8 (PowerISA 2.07) VSX BlockSha3_power8 (which uses
792
 * vrld/mtvsrd).  Select the POWER8 one at run time when the CPU is POWER8 or
793
 * later.
794
 *
795
 * A run-time flag with direct calls is used rather than a function pointer: an
796
 * indirect call would require an ELFv1 function descriptor, whereas direct
797
 * calls work under both the ELFv1 and ELFv2 ABIs. */
798
#include <wolfssl/wolfcrypt/cpuid.h>
799
800
/* -1 = not yet determined, 0 = base, 1 = POWER8 */
801
static int sha3_use_power8 = -1;
802
803
void BlockSha3(word64* s)
804
{
805
    if (sha3_use_power8 < 0) {
806
        word32 f = cpuid_get_flags();
807
        /* The VSX permutation is only worthwhile where the scalar issue width
808
         * does not already win.  POWER9 (PowerISA 3.0 but not 3.1) has enough
809
         * scalar throughput that BlockSha3_base is faster, so use the VSX path
810
         * only on POWER8 and on POWER10 (3.1) or later. */
811
        sha3_use_power8 = IS_PPC64_ARCH_2_07(f) &&
812
            (!IS_PPC64_ARCH_3_00(f) || IS_PPC64_ARCH_3_1(f));
813
    }
814
815
    if (sha3_use_power8)
816
        BlockSha3_power8(s);
817
    else
818
        BlockSha3_base(s);
819
}
820
#else
821
/* Only the scalar implementation is built; call it directly (no run-time
822
 * dispatch, no function pointer). */
823
void BlockSha3(word64* s)
824
{
825
    BlockSha3_base(s);
826
}
827
#endif
828
#endif
829
/* Scalar PowerPC32 assembly provides BlockSha3 directly (see
830
 * wolfcrypt/src/port/ppc32/ppc32-sha3-asm.S), so nothing is needed here. */
831
832
#ifdef WC_SHA3_SW_KECCAK
833
#if defined(BIG_ENDIAN_ORDER) || defined(WOLFSSL_WIDE_BYTE)
834
static WC_INLINE word64 Load64Unaligned(const unsigned char *a)
835
{
836
    return ((word64)a[0] <<  0) |
837
           ((word64)a[1] <<  8) |
838
           ((word64)a[2] << 16) |
839
           ((word64)a[3] << 24) |
840
           ((word64)a[4] << 32) |
841
           ((word64)a[5] << 40) |
842
           ((word64)a[6] << 48) |
843
           ((word64)a[7] << 56);
844
}
845
846
/* Convert the array of bytes, in little-endian order, to a 64-bit integer.
847
 *
848
 * a  Array of bytes.
849
 * returns a 64-bit integer.
850
 */
851
static word64 Load64BitLittleEndian(const byte* a)
852
{
853
    word64 n = 0;
854
    int i;
855
856
    for (i = 0; i < 8; i++)
857
        n |= (word64)a[i] << (8 * i);
858
859
    return n;
860
}
861
#elif defined(WC_SHA3_FAULT_HARDEN)
862
static WC_INLINE word64 Load64Unaligned(const unsigned char *a) {
863
    return readUnalignedWord64(a);
864
}
865
866
/* Convert the array of bytes, in little-endian order, to a 64-bit integer.
867
 *
868
 * a  Array of bytes.
869
 * returns a 64-bit integer.
870
 */
871
static word64 Load64BitLittleEndian(const byte* a)
872
{
873
    return Load64Unaligned(a);
874
}
875
#endif
876
877
/* Initialize the state for a SHA3-224 hash operation.
878
 *
879
 * sha3   wc_Sha3 object holding state.
880
 * returns 0 on success.
881
 */
882
883
static int InitSha3(wc_Sha3* sha3)
884
109k
{
885
109k
    int i;
886
887
2.85M
    for (i = 0; i < 25; i++)
888
2.74M
        sha3->s[i] = 0;
889
109k
    XMEMSET(sha3->t, 0, sizeof(sha3->t));
890
109k
    sha3->i = 0;
891
109k
#ifdef WOLFSSL_HASH_FLAGS
892
109k
    sha3->flags = 0;
893
109k
#endif
894
109k
#ifdef WOLF_CRYPTO_CB
895
    /* Cached hash variant is tied to sponge state; clear it whenever the
896
     * state is reset so reuse for a different SHA3 variant dispatches
897
     * correctly through the crypto callback. */
898
109k
    sha3->hashType = WC_HASH_TYPE_NONE;
899
109k
#endif
900
901
#ifdef USE_INTEL_SPEEDUP
902
    {
903
        int cpuid_flags_were_updated = cpuid_get_flags_ex(&cpuid_flags);
904
#ifdef WC_C_DYNAMIC_FALLBACK
905
        (void)cpuid_flags_were_updated;
906
        if (! CAN_SAVE_VECTOR_REGISTERS()) {
907
            SHA3_BLOCK = BlockSha3;
908
            SHA3_BLOCK_N = NULL;
909
        }
910
        else
911
#else
912
        if ((! cpuid_flags_were_updated) && (SHA3_BLOCK != NULL)) {
913
        }
914
        else
915
#endif
916
        /* See the selection comment above: AVX2 on Intel, otherwise BMI2. */
917
        if (SHA3_USE_AVX2(cpuid_flags)) {
918
            SHA3_BLOCK = sha3_block_avx2;
919
            SHA3_BLOCK_N = sha3_block_n_avx2;
920
        }
921
        else if (IS_INTEL_BMI1(cpuid_flags) && IS_INTEL_BMI2(cpuid_flags)) {
922
            SHA3_BLOCK = sha3_block_bmi2;
923
            SHA3_BLOCK_N = sha3_block_n_bmi2;
924
        }
925
        else {
926
            SHA3_BLOCK = BlockSha3;
927
            SHA3_BLOCK_N = NULL;
928
        }
929
    }
930
#define SHA3_FUNC_PTR
931
#endif /* USE_INTEL_SPEEDUP */
932
#if defined(__aarch64__) && defined(WOLFSSL_ARMASM)
933
    {
934
        int cpuid_flags_were_updated = cpuid_get_flags_ex(&cpuid_flags);
935
        if ((! cpuid_flags_were_updated) && (SHA3_BLOCK != NULL)) {
936
        }
937
        else
938
    #ifdef WOLFSSL_ARMASM_CRYPTO_SHA3
939
        if (IS_AARCH64_SHA3(cpuid_flags)) {
940
            SHA3_BLOCK = BlockSha3_crypto;
941
            SHA3_BLOCK_N = NULL;
942
        }
943
        else
944
    #endif
945
        {
946
            SHA3_BLOCK = BlockSha3_base;
947
            SHA3_BLOCK_N = NULL;
948
        }
949
    }
950
#define SHA3_FUNC_PTR
951
#endif
952
953
109k
    return 0;
954
109k
}
955
956
#if defined(__aarch64__) && defined(WOLFSSL_ARMASM)
957
void BlockSha3(word64* s)
958
{
959
    (*SHA3_BLOCK)(s);
960
}
961
#endif
962
963
/* Update the SHA-3 hash state with message data.
964
 *
965
 * sha3  wc_Sha3 object holding state.
966
 * data  Message data to be hashed.
967
 * len   Length of the message data.
968
 * p     Number of 64-bit numbers in a block of data to process.
969
 * returns 0 on success.
970
 */
971
static int Sha3Update(wc_Sha3* sha3, const byte* data, word32 len, word32 p)
972
225k
{
973
225k
    word32 i;
974
225k
    word32 blocks;
975
225k
    int ret = 0;
976
#ifdef WC_SHA3_FAULT_HARDEN
977
    word32 check = 0;
978
    word32 total_check = 0;
979
#endif
980
#ifdef USE_INTEL_SPEEDUP
981
#ifdef WC_C_DYNAMIC_FALLBACK
982
    void (*sha3_block)(word64 *s) = SHA3_BLOCK;
983
    void (*sha3_block_n)(word64 *s, const byte* data, word32 n,
984
        word64 c) = SHA3_BLOCK_N;
985
#endif
986
#endif /* USE_INTEL_SPEEDUP */
987
988
225k
    if ((p < WC_SHA3_512_COUNT) || (p > WC_SHA3_128_COUNT))
989
0
        return BAD_STATE_E;
990
991
#ifdef USE_INTEL_SPEEDUP
992
    if (SHA3_BLOCK_VREGS(sha3_block)) {
993
        ret = SAVE_VECTOR_REGISTERS2();
994
        if (ret != 0) {
995
#ifdef WC_C_DYNAMIC_FALLBACK
996
            sha3_block = BlockSha3;
997
            sha3_block_n = NULL;
998
            ret = 0;
999
#else
1000
            return ret;
1001
#endif
1002
        }
1003
    }
1004
#endif /* USE_INTEL_SPEEDUP */
1005
1006
225k
    if (sha3->i > 0) {
1007
134k
        byte *t;
1008
134k
        word32 l;
1009
134k
        if (p * 8 < sha3->i) {
1010
0
            ret = BAD_STATE_E;
1011
0
            goto out;
1012
0
        }
1013
134k
        l = (p * 8 - sha3->i);
1014
134k
        if (l > len) {
1015
133k
            l = len;
1016
133k
        }
1017
1018
134k
        t = &sha3->t[sha3->i];
1019
700k
        for (i = 0; i < l; i++) {
1020
565k
            t[i] = data[i];
1021
    #ifdef WC_SHA3_FAULT_HARDEN
1022
            check++;
1023
    #endif
1024
565k
        }
1025
    #ifdef WC_SHA3_FAULT_HARDEN
1026
        if (check != l) {
1027
            ret = BAD_COND_E;
1028
            goto out;
1029
        }
1030
        total_check += l;
1031
    #endif
1032
134k
        data += i;
1033
134k
        len -= i;
1034
134k
        sha3->i += i;
1035
1036
134k
        if (sha3->i == p * 8) {
1037
1.75k
    #if !defined(BIG_ENDIAN_ORDER) && !defined(WC_SHA3_FAULT_HARDEN) && \
1038
1.75k
        !defined(WOLFSSL_WIDE_BYTE)
1039
1.75k
            xorbuf(sha3->s, sha3->t, (word32)(p * 8));
1040
    #else
1041
            for (i = 0; i < p; i++) {
1042
                sha3->s[i] ^= Load64BitLittleEndian(sha3->t + 8 * i);
1043
            #ifdef WC_SHA3_FAULT_HARDEN
1044
                check++;
1045
            #endif
1046
            }
1047
        #ifdef WC_SHA3_FAULT_HARDEN
1048
            if (check != p + l) {
1049
                ret = BAD_COND_E;
1050
                goto out;
1051
            }
1052
            total_check += p;
1053
        #endif
1054
    #endif
1055
        #ifdef SHA3_FUNC_PTR
1056
            (*sha3_block)(sha3->s);
1057
        #else
1058
1.75k
            BlockSha3(sha3->s);
1059
1.75k
        #endif
1060
1.75k
            sha3->i = 0;
1061
1.75k
        }
1062
134k
    }
1063
225k
    blocks = len / (p * 8U);
1064
    #ifdef SHA3_FUNC_PTR
1065
    if ((sha3_block_n != NULL) && (blocks > 0)) {
1066
        (*sha3_block_n)(sha3->s, data, blocks, p * 8U);
1067
        len -= blocks * (p * 8U);
1068
        data += blocks * (p * 8U);
1069
        blocks = 0;
1070
    }
1071
    #endif
1072
#ifdef WC_SHA3_FAULT_HARDEN
1073
    total_check += blocks * p;
1074
#endif
1075
276k
    for (; blocks > 0; blocks--) {
1076
51.6k
#if !defined(BIG_ENDIAN_ORDER) && !defined(WC_SHA3_FAULT_HARDEN) && \
1077
51.6k
    !defined(WOLFSSL_WIDE_BYTE)
1078
51.6k
        xorbuf(sha3->s, data, (word32)(p * 8));
1079
#else
1080
        for (i = 0; i < p; i++) {
1081
            sha3->s[i] ^= Load64Unaligned(data + 8 * i);
1082
        #ifdef WC_SHA3_FAULT_HARDEN
1083
            check++;
1084
        #endif
1085
        }
1086
    #ifdef WC_SHA3_FAULT_HARDEN
1087
        if (check != total_check - ((blocks - 1) * p)) {
1088
            ret = BAD_COND_E;
1089
            goto out;
1090
        }
1091
    #endif
1092
#endif
1093
    #ifdef SHA3_FUNC_PTR
1094
        (*sha3_block)(sha3->s);
1095
    #else
1096
51.6k
        BlockSha3(sha3->s);
1097
51.6k
    #endif
1098
51.6k
        len -= p * 8U;
1099
51.6k
        data += p * 8U;
1100
51.6k
    }
1101
#ifdef WC_SHA3_FAULT_HARDEN
1102
    if (check != total_check) {
1103
        ret = BAD_COND_E;
1104
        goto out;
1105
    }
1106
#endif
1107
1108
225k
out:
1109
1110
#ifdef USE_INTEL_SPEEDUP
1111
    if (SHA3_BLOCK_VREGS(sha3_block)) {
1112
        RESTORE_VECTOR_REGISTERS();
1113
    }
1114
#endif
1115
1116
225k
    if (ret == 0) {
1117
225k
        if (len > 0) {
1118
91.0k
            XMEMCPY(sha3->t, data, len);
1119
91.0k
        }
1120
225k
        sha3->i += len;
1121
225k
    }
1122
1123
225k
    return ret;
1124
225k
}
1125
1126
/* Calculate the SHA-3 hash based on all the message data seen.
1127
 *
1128
 * sha3  wc_Sha3 object holding state.
1129
 * hash  Buffer to hold the hash result.
1130
 * p     Number of 64-bit numbers in a block of data to process.
1131
 * len   Number of bytes in output.
1132
 * returns 0 on success.
1133
 */
1134
#ifdef WOLFSSL_WIDE_BYTE
1135
/* Squeeze len output bytes from the Keccak state, extracting each octet from
1136
 * the 64-bit lanes (little-endian within a lane).  Used where a C 'byte' is
1137
 * wider than 8 bits (CHAR_BIT != 8) so the state cannot be copied as an octet
1138
 * stream. */
1139
static void Sha3SqueezeBytes(byte* out, const word64* s, word32 len)
1140
{
1141
    word32 k;
1142
    for (k = 0; k < len; k++) {
1143
        out[k] = (byte)((s[k >> 3] >> (8 * (k & 7))) & 0xFF);
1144
    }
1145
}
1146
#endif
1147
1148
static int Sha3Final(wc_Sha3* sha3, byte padChar, byte* hash, word32 p, word32 l)
1149
83.8k
{
1150
83.8k
    word32 rate = p * 8U;
1151
83.8k
    word32 j;
1152
#if defined(BIG_ENDIAN_ORDER) || defined(WC_SHA3_FAULT_HARDEN) || \
1153
    defined(WOLFSSL_WIDE_BYTE)
1154
    word32 i;
1155
#endif
1156
#ifdef WC_SHA3_FAULT_HARDEN
1157
    word32 check = 0;
1158
#endif
1159
#if defined(WC_C_DYNAMIC_FALLBACK) && defined(USE_INTEL_SPEEDUP)
1160
    void (*sha3_block)(word64 *s) = SHA3_BLOCK;
1161
#endif
1162
1163
83.8k
    if ((p < WC_SHA3_512_COUNT) || (p > WC_SHA3_128_COUNT))
1164
0
        return BAD_STATE_E;
1165
83.8k
    if (sha3->i >= rate)
1166
0
        return BAD_STATE_E;
1167
1168
83.8k
#if !defined(BIG_ENDIAN_ORDER) && !defined(WC_SHA3_FAULT_HARDEN) && \
1169
83.8k
    !defined(WOLFSSL_WIDE_BYTE)
1170
83.8k
    xorbuf(sha3->s, sha3->t, sha3->i);
1171
83.8k
#ifdef WOLFSSL_HASH_FLAGS
1172
83.8k
    if ((p == WC_SHA3_256_COUNT) && (sha3->flags & WC_HASH_SHA3_KECCAK256)) {
1173
0
        padChar = 0x01;
1174
0
    }
1175
83.8k
#endif
1176
83.8k
    ((byte*)sha3->s)[sha3->i ] ^= padChar;
1177
83.8k
    ((byte*)sha3->s)[rate - 1] ^= 0x80;
1178
#else
1179
    sha3->t[rate - 1]  = 0x00;
1180
#ifdef WOLFSSL_HASH_FLAGS
1181
    if ((p == WC_SHA3_256_COUNT) && (sha3->flags & WC_HASH_SHA3_KECCAK256)) {
1182
        padChar = 0x01;
1183
    }
1184
#endif
1185
    sha3->t[sha3->i ]  = padChar;
1186
    sha3->t[rate - 1] |= 0x80;
1187
    if (rate - 1 > sha3->i + 1) {
1188
        XMEMSET(sha3->t + sha3->i + 1, 0, rate - 1U - (sha3->i + 1U));
1189
    }
1190
    for (i = 0; i < p; i++) {
1191
        sha3->s[i] ^= Load64BitLittleEndian(sha3->t + 8 * i);
1192
    #ifdef WC_SHA3_FAULT_HARDEN
1193
        check++;
1194
    #endif
1195
    }
1196
#ifdef WC_SHA3_FAULT_HARDEN
1197
    if (check != p) {
1198
        return BAD_COND_E;
1199
    }
1200
#endif
1201
#endif
1202
1203
#ifdef USE_INTEL_SPEEDUP
1204
    if (SHA3_BLOCK_VREGS(sha3_block)) {
1205
        int ret = SAVE_VECTOR_REGISTERS2();
1206
        if (ret != 0) {
1207
#ifdef WC_C_DYNAMIC_FALLBACK
1208
            sha3_block = BlockSha3;
1209
#else
1210
            return ret;
1211
#endif
1212
        }
1213
    }
1214
#endif
1215
1216
83.8k
    for (j = 0; l - j >= rate; j += rate) {
1217
    #ifdef SHA3_FUNC_PTR
1218
        (*sha3_block)(sha3->s);
1219
    #else
1220
26
        BlockSha3(sha3->s);
1221
26
    #endif
1222
    #if defined(BIG_ENDIAN_ORDER)
1223
        ByteReverseWords64((word64*)(hash + j), sha3->s, rate);
1224
    #elif defined(WOLFSSL_WIDE_BYTE)
1225
        Sha3SqueezeBytes(hash + j, sha3->s, rate);
1226
    #else
1227
26
        XMEMCPY(hash + j, sha3->s, rate);
1228
26
    #endif
1229
26
    }
1230
83.8k
    if (j != l) {
1231
    #ifdef SHA3_FUNC_PTR
1232
        (*sha3_block)(sha3->s);
1233
    #else
1234
45.5k
        BlockSha3(sha3->s);
1235
45.5k
    #endif
1236
    #if defined(BIG_ENDIAN_ORDER)
1237
        ByteReverseWords64(sha3->s, sha3->s, rate);
1238
        XMEMCPY(hash + j, sha3->s, l - j);
1239
    #elif defined(WOLFSSL_WIDE_BYTE)
1240
        Sha3SqueezeBytes(hash + j, sha3->s, l - j);
1241
    #else
1242
45.5k
        XMEMCPY(hash + j, sha3->s, l - j);
1243
45.5k
    #endif
1244
45.5k
    }
1245
#ifdef USE_INTEL_SPEEDUP
1246
    if (SHA3_BLOCK_VREGS(sha3_block)) {
1247
        RESTORE_VECTOR_REGISTERS();
1248
    }
1249
#endif
1250
1251
83.8k
    return 0;
1252
83.8k
}
1253
#endif /* WC_SHA3_SW_KECCAK */
1254
#if defined(STM32_HASH_SHA3)
1255
1256
/* Supports CubeMX HAL or Standard Peripheral Library */
1257
1258
static int wc_InitSha3(wc_Sha3* sha3, void* heap, int devId)
1259
{
1260
    if (sha3 == NULL)
1261
        return BAD_FUNC_ARG;
1262
1263
    (void)devId;
1264
    (void)heap;
1265
1266
    XMEMSET(sha3, 0, sizeof(wc_Sha3));
1267
    wc_Stm32_Hash_Init(&sha3->stmCtx);
1268
    return 0;
1269
}
1270
1271
static int Stm32GetAlgo(word32 p)
1272
{
1273
    switch(p) {
1274
        case WC_SHA3_224_COUNT:
1275
            return HASH_ALGOSELECTION_SHA3_224;
1276
        case WC_SHA3_256_COUNT:
1277
            return HASH_ALGOSELECTION_SHA3_256;
1278
        case WC_SHA3_384_COUNT:
1279
            return HASH_ALGOSELECTION_SHA3_384;
1280
        case WC_SHA3_512_COUNT:
1281
            return HASH_ALGOSELECTION_SHA3_512;
1282
    }
1283
    /* Should never get here */
1284
    return WC_SHA3_224_COUNT;
1285
}
1286
1287
static int wc_Sha3Update(wc_Sha3* sha3, const byte* data, word32 len, word32 p)
1288
{
1289
    int ret = 0;
1290
1291
    if (sha3 == NULL) {
1292
        return BAD_FUNC_ARG;
1293
    }
1294
    if (data == NULL && len == 0) {
1295
        /* valid, but do nothing */
1296
        return 0;
1297
    }
1298
    if (data == NULL) {
1299
        return BAD_FUNC_ARG;
1300
    }
1301
1302
    ret = wolfSSL_CryptHwMutexLock();
1303
    if (ret == 0) {
1304
        ret = wc_Stm32_Hash_Update(&sha3->stmCtx, Stm32GetAlgo(p), data, len,
1305
            p * 8);
1306
        wolfSSL_CryptHwMutexUnLock();
1307
    }
1308
    return ret;
1309
}
1310
1311
static int wc_Sha3Final(wc_Sha3* sha3, byte* hash, word32 p, word32 len)
1312
{
1313
    int ret = 0;
1314
1315
    if (sha3 == NULL || hash == NULL) {
1316
        return BAD_FUNC_ARG;
1317
    }
1318
1319
    ret = wolfSSL_CryptHwMutexLock();
1320
    if (ret == 0) {
1321
        ret = wc_Stm32_Hash_Final(&sha3->stmCtx, Stm32GetAlgo(p), hash, len);
1322
        wolfSSL_CryptHwMutexUnLock();
1323
    }
1324
1325
    (void)wc_InitSha3(sha3, NULL, 0); /* reset state */
1326
1327
    return ret;
1328
}
1329
#elif defined(PSOC6_HASH_SHA3)
1330
1331
static int wc_InitSha3(wc_Sha3* sha3, void* heap, int devId)
1332
{
1333
    int ret;
1334
    if (sha3 == NULL) {
1335
        return BAD_FUNC_ARG;
1336
    }
1337
    (void)devId;
1338
    (void)heap;
1339
1340
    /* Lock the mutex to perform crypto operations */
1341
    ret = wolfSSL_CryptHwMutexLock();
1342
    if (ret == 0) {
1343
        /* Initialize hash state for SHA-3 operation */
1344
        ret = wc_Psoc6_Sha3_Init(sha3);
1345
        /* Release the lock */
1346
        wolfSSL_CryptHwMutexUnLock();
1347
    }
1348
1349
    return ret;
1350
}
1351
1352
static int wc_Sha3Update(wc_Sha3* sha3, const byte* data, word32 len, word32 p)
1353
{
1354
    int ret;
1355
1356
    if (sha3 == NULL || (data == NULL && len > 0)) {
1357
        return BAD_FUNC_ARG;
1358
    }
1359
1360
    if (data == NULL) {
1361
        /* len is 0 here: valid, but do nothing */
1362
        return 0;
1363
    }
1364
1365
    /* Lock the mutex to perform crypto operations */
1366
    ret = wolfSSL_CryptHwMutexLock();
1367
    if (ret == 0) {
1368
        /* Perform SHA3 on the input data and update the hash state */
1369
        ret = wc_Psoc6_Sha3_Update(sha3, data, len, p);
1370
        /* Release the lock */
1371
        wolfSSL_CryptHwMutexUnLock();
1372
    }
1373
1374
    return ret;
1375
}
1376
1377
static int wc_Sha3Final(wc_Sha3* sha3, byte* hash, word32 p, word32 len)
1378
{
1379
    int ret;
1380
1381
    if (sha3 == NULL || hash == NULL) {
1382
        return BAD_FUNC_ARG;
1383
    }
1384
1385
    /* Lock the mutex to perform crypto operations */
1386
    ret = wolfSSL_CryptHwMutexLock();
1387
    if (ret == 0) {
1388
        /* Finalize SHA3 operations and produce digest */
1389
        ret = wc_Psoc6_Sha3_Final(sha3, 0x06, hash, p, len);
1390
        if (ret == 0) {
1391
            /* Initialize hash state for SHA-3 operation */
1392
            ret = wc_Psoc6_Sha3_Init(sha3);
1393
        }
1394
        /* Release the lock */
1395
        wolfSSL_CryptHwMutexUnLock();
1396
    }
1397
1398
    return ret;
1399
}
1400
1401
#else
1402
1403
/* Initialize the state for a SHA-3 hash operation.
1404
 *
1405
 * sha3   wc_Sha3 object holding state.
1406
 * heap   Heap reference for dynamic memory allocation. (Used in async ops.)
1407
 * devId  Device identifier for asynchronous operation.
1408
 * returns 0 on success.
1409
 */
1410
static int wc_InitSha3(wc_Sha3* sha3, void* heap, int devId)
1411
61.8k
{
1412
61.8k
    int ret = 0;
1413
1414
61.8k
    if (sha3 == NULL)
1415
0
        return BAD_FUNC_ARG;
1416
1417
61.8k
    sha3->heap = heap;
1418
61.8k
    ret = InitSha3(sha3);
1419
61.8k
    if (ret != 0)
1420
0
        return ret;
1421
1422
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
1423
    ret = wolfAsync_DevCtxInit(&sha3->asyncDev,
1424
                        WOLFSSL_ASYNC_MARKER_SHA3, sha3->heap, devId);
1425
#endif
1426
61.8k
#if defined(WOLF_CRYPTO_CB)
1427
61.8k
    sha3->devId = devId;
1428
61.8k
    sha3->devCtx = NULL;
1429
    /* Set to none to determine the hash type later */
1430
    /* in the update/final functions based on the p value */
1431
61.8k
    sha3->hashType = WC_HASH_TYPE_NONE;
1432
61.8k
#endif
1433
61.8k
    (void)devId;
1434
1435
61.8k
    return ret;
1436
61.8k
}
1437
1438
#if !(defined(WOLFSSL_NOSHA3_224) && defined(WOLFSSL_NOSHA3_256) && \
1439
      defined(WOLFSSL_NOSHA3_384) && defined(WOLFSSL_NOSHA3_512))
1440
/* Update the SHA-3 hash state with message data.
1441
 *
1442
 * sha3  wc_Sha3 object holding state.
1443
 * data  Message data to be hashed.
1444
 * len   Length of the message data.
1445
 * p     Number of 64-bit numbers in a block of data to process.
1446
 * returns 0 on success.
1447
 */
1448
static int wc_Sha3Update(wc_Sha3* sha3, const byte* data, word32 len, word32 p)
1449
13.4k
{
1450
13.4k
    int ret;
1451
1452
13.4k
    if (sha3 == NULL) {
1453
0
        return BAD_FUNC_ARG;
1454
0
    }
1455
1456
13.4k
    if (data == NULL && len == 0) {
1457
        /* valid, but do nothing */
1458
55
        return 0;
1459
55
    }
1460
1461
13.4k
    if (data == NULL) {
1462
0
        return BAD_FUNC_ARG;
1463
0
    }
1464
1465
13.4k
#ifdef WOLF_CRYPTO_CB
1466
13.4k
    #ifndef WOLF_CRYPTO_CB_FIND
1467
13.4k
    if (sha3->devId != INVALID_DEVID)
1468
0
    #endif
1469
0
    {
1470
        /* If the hash type is not set, determine it based on the p value */
1471
        /* We can skip the switch statement if the hash type set already */
1472
0
        if (sha3->hashType == WC_HASH_TYPE_NONE) {
1473
0
            switch (p) {
1474
0
                case WC_SHA3_224_COUNT:
1475
0
                    sha3->hashType = WC_HASH_TYPE_SHA3_224; break;
1476
0
                case WC_SHA3_256_COUNT:
1477
0
                    sha3->hashType = WC_HASH_TYPE_SHA3_256; break;
1478
0
                case WC_SHA3_384_COUNT:
1479
0
                    sha3->hashType = WC_HASH_TYPE_SHA3_384; break;
1480
0
                case WC_SHA3_512_COUNT:
1481
0
                    sha3->hashType = WC_HASH_TYPE_SHA3_512; break;
1482
0
                default: return BAD_FUNC_ARG;
1483
0
            }
1484
0
        }
1485
0
        ret = wc_CryptoCb_Sha3Hash(sha3, sha3->hashType, data, len, NULL);
1486
0
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1487
0
            return ret;
1488
        /* fall-through when unavailable */
1489
0
    }
1490
13.4k
#endif
1491
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
1492
    if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) {
1493
    #if defined(HAVE_INTEL_QA) && defined(QAT_V2)
1494
        /* QAT only supports SHA3_256 */
1495
        if (p == WC_SHA3_256_COUNT) {
1496
            ret = IntelQaSymSha3(&sha3->asyncDev, NULL, data, len);
1497
            if (ret != WC_NO_ERR_TRACE(NOT_COMPILED_IN))
1498
                return ret;
1499
            /* fall-through when unavailable */
1500
        }
1501
    #endif
1502
    }
1503
#endif /* WOLFSSL_ASYNC_CRYPT */
1504
1505
13.4k
    ret = Sha3Update(sha3, data, len, p);
1506
1507
13.4k
    return ret;
1508
13.4k
}
1509
1510
/* Calculate the SHA-3 hash based on all the message data seen.
1511
 *
1512
 * sha3  wc_Sha3 object holding state.
1513
 * hash  Buffer to hold the hash result.
1514
 * p     Number of 64-bit numbers in a block of data to process.
1515
 * len   Number of bytes in output.
1516
 * returns 0 on success.
1517
 */
1518
static int wc_Sha3Final(wc_Sha3* sha3, byte* hash, word32 p, word32 len)
1519
9.22k
{
1520
9.22k
    int ret;
1521
1522
9.22k
    if (sha3 == NULL || hash == NULL) {
1523
0
        return BAD_FUNC_ARG;
1524
0
    }
1525
1526
9.22k
#ifdef WOLF_CRYPTO_CB
1527
9.22k
    #ifndef WOLF_CRYPTO_CB_FIND
1528
9.22k
    if (sha3->devId != INVALID_DEVID)
1529
0
    #endif
1530
0
    {
1531
        /* If the hash type is not set, determine it based on the p value */
1532
        /* We can skip the switch statement if the hash type is set already */
1533
0
        if (sha3->hashType == WC_HASH_TYPE_NONE) {
1534
0
            switch (p) {
1535
0
                case WC_SHA3_224_COUNT:
1536
0
                    sha3->hashType = WC_HASH_TYPE_SHA3_224; break;
1537
0
                case WC_SHA3_256_COUNT:
1538
0
                    sha3->hashType = WC_HASH_TYPE_SHA3_256; break;
1539
0
                case WC_SHA3_384_COUNT:
1540
0
                    sha3->hashType = WC_HASH_TYPE_SHA3_384; break;
1541
0
                case WC_SHA3_512_COUNT:
1542
0
                    sha3->hashType = WC_HASH_TYPE_SHA3_512; break;
1543
0
                default: return BAD_FUNC_ARG;
1544
0
            }
1545
0
        }
1546
0
        ret = wc_CryptoCb_Sha3Hash(sha3, sha3->hashType, NULL, 0, hash);
1547
0
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1548
0
            return ret;
1549
        /* fall-through when unavailable */
1550
0
    }
1551
9.22k
#endif
1552
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
1553
    if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) {
1554
    #if defined(HAVE_INTEL_QA) && defined(QAT_V2)
1555
        /* QAT only supports SHA3_256 */
1556
        /* QAT SHA-3 only supported on v2 (8970 or later cards) */
1557
        if (len == WC_SHA3_256_DIGEST_SIZE) {
1558
            ret = IntelQaSymSha3(&sha3->asyncDev, hash, NULL, len);
1559
            if (ret != WC_NO_ERR_TRACE(NOT_COMPILED_IN))
1560
                return ret;
1561
            /* fall-through when unavailable */
1562
        }
1563
    #endif
1564
    }
1565
#endif /* WOLFSSL_ASYNC_CRYPT */
1566
1567
9.22k
    ret = Sha3Final(sha3, 0x06, hash, p, (word32)len);
1568
9.22k
    if (ret != 0)
1569
0
        return ret;
1570
1571
9.22k
    return InitSha3(sha3);  /* reset state */
1572
9.22k
}
1573
#endif
1574
#endif
1575
1576
/* Dispose of any dynamically allocated data from the SHA3-384 operation.
1577
 * (Required for async ops.)
1578
 *
1579
 * sha3  wc_Sha3 object holding state.
1580
 * returns 0 on success.
1581
 */
1582
static void wc_Sha3Free(wc_Sha3* sha3)
1583
15.9k
{
1584
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
1585
    int ret = 0;
1586
#endif
1587
1588
15.9k
    (void)sha3;
1589
1590
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
1591
    if (sha3 == NULL)
1592
        return;
1593
1594
    #ifndef WOLF_CRYPTO_CB_FIND
1595
    if (sha3->devId != INVALID_DEVID)
1596
    #endif
1597
    {
1598
        ret = wc_CryptoCb_Free(sha3->devId, WC_ALGO_TYPE_HASH,
1599
                         sha3->hashType, 0, (void*)sha3);
1600
        /* If they want the standard free, they can call it themselves */
1601
        /* via their callback setting devId to INVALID_DEVID */
1602
        /* otherwise assume the callback handled it */
1603
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1604
            return;
1605
        /* fall-through when unavailable */
1606
    }
1607
1608
    /* silence compiler warning */
1609
    (void)ret;
1610
1611
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */
1612
1613
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
1614
    if (sha3 == NULL)
1615
        return;
1616
1617
    wolfAsync_DevCtxFree(&sha3->asyncDev, WOLFSSL_ASYNC_MARKER_SHA3);
1618
#endif /* WOLFSSL_ASYNC_CRYPT */
1619
1620
#if defined(PSOC6_HASH_SHA3)
1621
    wc_Psoc6_Sha_Free();
1622
#endif
1623
15.9k
}
1624
1625
/* Copy the state of the SHA3 operation.
1626
 *
1627
 * src  wc_Sha3 object holding state top copy.
1628
 * dst  wc_Sha3 object to copy into.
1629
 * returns 0 on success.
1630
 */
1631
static int wc_Sha3Copy(wc_Sha3* src, wc_Sha3* dst)
1632
0
{
1633
0
    int ret = 0;
1634
1635
0
    if (src == NULL || dst == NULL)
1636
0
        return BAD_FUNC_ARG;
1637
1638
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_COPY)
1639
    #ifndef WOLF_CRYPTO_CB_FIND
1640
    if (src->devId != INVALID_DEVID)
1641
    #endif
1642
    {
1643
        /* Cast the source and destination to be void to keep the abstraction */
1644
        ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH,
1645
                               src->hashType, (void*)src, (void*)dst);
1646
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1647
            return ret;
1648
        /* fall-through when unavailable */
1649
    }
1650
    ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
1651
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
1652
1653
    /* Free dst resources before copy to prevent memory leaks (e.g.,
1654
     * hardware contexts). XMEMCPY overwrites dst. */
1655
0
    wc_Sha3Free(dst);
1656
0
    XMEMCPY(dst, src, sizeof(wc_Sha3));
1657
1658
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
1659
    ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
1660
#endif
1661
1662
#if defined(PSOC6_HASH_SHA3)
1663
    /* Re-initialize internal pointers in hash_state that point inside sha_buffers */
1664
    dst->hash_state.hash = (uint8_t*)((cy_stc_crypto_v2_sha3_buffers_t *)&dst->sha_buffers)->hash;
1665
#endif
1666
1667
0
#ifdef WOLFSSL_HASH_FLAGS
1668
0
     dst->flags |= WC_HASH_FLAG_ISCOPY;
1669
0
#endif
1670
1671
0
    return ret;
1672
0
}
1673
1674
#if !(defined(WOLFSSL_NOSHA3_224) && defined(WOLFSSL_NOSHA3_256) && \
1675
      defined(WOLFSSL_NOSHA3_384) && defined(WOLFSSL_NOSHA3_512))
1676
/* Calculate the SHA3-224 hash based on all the message data so far.
1677
 * More message data can be added, after this operation, using the current
1678
 * state.
1679
 *
1680
 * sha3  wc_Sha3 object holding state.
1681
 * hash  Buffer to hold the hash result. Must be at least 28 bytes.
1682
 * p     Number of 64-bit numbers in a block of data to process.
1683
 * len   Number of bytes in output.
1684
 * returns 0 on success.
1685
 */
1686
static int wc_Sha3GetHash(wc_Sha3* sha3, byte* hash, word32 p, word32 len)
1687
0
{
1688
0
    int ret;
1689
0
    WC_DECLARE_VAR(tmpSha3, wc_Sha3, 1, sha3 ? sha3->heap : NULL);
1690
1691
0
    if (sha3 == NULL || hash == NULL)
1692
0
        return BAD_FUNC_ARG;
1693
1694
0
    WC_ALLOC_VAR_EX(tmpSha3, wc_Sha3, 1, sha3->heap, DYNAMIC_TYPE_TMP_BUFFER,
1695
0
                    return MEMORY_E);
1696
1697
0
    XMEMSET(tmpSha3, 0, sizeof(*tmpSha3));
1698
0
    ret = wc_Sha3Copy(sha3, tmpSha3);
1699
0
    if (ret == 0) {
1700
0
        ret = wc_Sha3Final(tmpSha3, hash, p, len);
1701
0
    }
1702
1703
0
    WC_FREE_VAR_EX(tmpSha3, sha3->heap, DYNAMIC_TYPE_TMP_BUFFER);
1704
0
    return ret;
1705
0
}
1706
#endif
1707
1708
#ifndef WOLFSSL_NOSHA3_224
1709
/* Initialize the state for a SHA3-224 hash operation.
1710
 *
1711
 * sha3   wc_Sha3 object holding state.
1712
 * heap   Heap reference for dynamic memory allocation. (Used in async ops.)
1713
 * devId  Device identifier for asynchronous operation.
1714
 * returns 0 on success.
1715
 */
1716
int wc_InitSha3_224(wc_Sha3* sha3, void* heap, int devId)
1717
409
{
1718
409
    return wc_InitSha3(sha3, heap, devId);
1719
409
}
1720
1721
/* Update the SHA3-224 hash state with message data.
1722
 *
1723
 * sha3  wc_Sha3 object holding state.
1724
 * data  Message data to be hashed.
1725
 * len   Length of the message data.
1726
 * returns 0 on success.
1727
 */
1728
int wc_Sha3_224_Update(wc_Sha3* sha3, const byte* data, word32 len)
1729
409
{
1730
409
    return wc_Sha3Update(sha3, data, len, WC_SHA3_224_COUNT);
1731
409
}
1732
1733
/* Calculate the SHA3-224 hash based on all the message data seen.
1734
 * The state is initialized ready for a new message to hash.
1735
 *
1736
 * sha3  wc_Sha3 object holding state.
1737
 * hash  Buffer to hold the hash result. Must be at least 28 bytes.
1738
 * returns 0 on success.
1739
 */
1740
int wc_Sha3_224_Final(wc_Sha3* sha3, byte* hash)
1741
409
{
1742
409
    return wc_Sha3Final(sha3, hash, WC_SHA3_224_COUNT, WC_SHA3_224_DIGEST_SIZE);
1743
409
}
1744
1745
/* Dispose of any dynamically allocated data from the SHA3-224 operation.
1746
 * (Required for async ops.)
1747
 *
1748
 * sha3  wc_Sha3 object holding state.
1749
 * returns 0 on success.
1750
 */
1751
void wc_Sha3_224_Free(wc_Sha3* sha3)
1752
409
{
1753
409
    wc_Sha3Free(sha3);
1754
409
}
1755
1756
/* Calculate the SHA3-224 hash based on all the message data so far.
1757
 * More message data can be added, after this operation, using the current
1758
 * state.
1759
 *
1760
 * sha3  wc_Sha3 object holding state.
1761
 * hash  Buffer to hold the hash result. Must be at least 28 bytes.
1762
 * returns 0 on success.
1763
 */
1764
int wc_Sha3_224_GetHash(wc_Sha3* sha3, byte* hash)
1765
0
{
1766
0
    return wc_Sha3GetHash(sha3, hash, WC_SHA3_224_COUNT, WC_SHA3_224_DIGEST_SIZE);
1767
0
}
1768
1769
/* Copy the state of the SHA3-224 operation.
1770
 *
1771
 * src  wc_Sha3 object holding state top copy.
1772
 * dst  wc_Sha3 object to copy into.
1773
 * returns 0 on success.
1774
 */
1775
int wc_Sha3_224_Copy(wc_Sha3* src, wc_Sha3* dst)
1776
0
{
1777
0
    return wc_Sha3Copy(src, dst);
1778
0
}
1779
#endif
1780
1781
#ifndef WOLFSSL_NOSHA3_256
1782
/* Initialize the state for a SHA3-256 hash operation.
1783
 *
1784
 * sha3   wc_Sha3 object holding state.
1785
 * heap   Heap reference for dynamic memory allocation. (Used in async ops.)
1786
 * devId  Device identifier for asynchronous operation.
1787
 * returns 0 on success.
1788
 */
1789
int wc_InitSha3_256(wc_Sha3* sha3, void* heap, int devId)
1790
4.66k
{
1791
4.66k
    return wc_InitSha3(sha3, heap, devId);
1792
4.66k
}
1793
1794
/* Update the SHA3-256 hash state with message data.
1795
 *
1796
 * sha3  wc_Sha3 object holding state.
1797
 * data  Message data to be hashed.
1798
 * len   Length of the message data.
1799
 * returns 0 on success.
1800
 */
1801
int wc_Sha3_256_Update(wc_Sha3* sha3, const byte* data, word32 len)
1802
4.60k
{
1803
4.60k
    return wc_Sha3Update(sha3, data, len, WC_SHA3_256_COUNT);
1804
4.60k
}
1805
1806
/* Calculate the SHA3-256 hash based on all the message data seen.
1807
 * The state is initialized ready for a new message to hash.
1808
 *
1809
 * sha3  wc_Sha3 object holding state.
1810
 * hash  Buffer to hold the hash result. Must be at least 32 bytes.
1811
 * returns 0 on success.
1812
 */
1813
int wc_Sha3_256_Final(wc_Sha3* sha3, byte* hash)
1814
4.60k
{
1815
4.60k
    return wc_Sha3Final(sha3, hash, WC_SHA3_256_COUNT, WC_SHA3_256_DIGEST_SIZE);
1816
4.60k
}
1817
1818
/* Dispose of any dynamically allocated data from the SHA3-256 operation.
1819
 * (Required for async ops.)
1820
 *
1821
 * sha3  wc_Sha3 object holding state.
1822
 * returns 0 on success.
1823
 */
1824
void wc_Sha3_256_Free(wc_Sha3* sha3)
1825
4.66k
{
1826
4.66k
    wc_Sha3Free(sha3);
1827
4.66k
}
1828
1829
/* Calculate the SHA3-256 hash based on all the message data so far.
1830
 * More message data can be added, after this operation, using the current
1831
 * state.
1832
 *
1833
 * sha3  wc_Sha3 object holding state.
1834
 * hash  Buffer to hold the hash result. Must be at least 32 bytes.
1835
 * returns 0 on success.
1836
 */
1837
int wc_Sha3_256_GetHash(wc_Sha3* sha3, byte* hash)
1838
0
{
1839
0
    return wc_Sha3GetHash(sha3, hash, WC_SHA3_256_COUNT, WC_SHA3_256_DIGEST_SIZE);
1840
0
}
1841
1842
/* Copy the state of the SHA3-256 operation.
1843
 *
1844
 * src  wc_Sha3 object holding state top copy.
1845
 * dst  wc_Sha3 object to copy into.
1846
 * returns 0 on success.
1847
 */
1848
int wc_Sha3_256_Copy(wc_Sha3* src, wc_Sha3* dst)
1849
0
{
1850
0
    return wc_Sha3Copy(src, dst);
1851
0
}
1852
#endif
1853
1854
#ifndef WOLFSSL_NOSHA3_384
1855
/* Initialize the state for a SHA3-384 hash operation.
1856
 *
1857
 * sha3   wc_Sha3 object holding state.
1858
 * heap   Heap reference for dynamic memory allocation. (Used in async ops.)
1859
 * devId  Device identifier for asynchronous operation.
1860
 * returns 0 on success.
1861
 */
1862
int wc_InitSha3_384(wc_Sha3* sha3, void* heap, int devId)
1863
104
{
1864
104
    return wc_InitSha3(sha3, heap, devId);
1865
104
}
1866
1867
/* Update the SHA3-384 hash state with message data.
1868
 *
1869
 * sha3  wc_Sha3 object holding state.
1870
 * data  Message data to be hashed.
1871
 * len   Length of the message data.
1872
 * returns 0 on success.
1873
 */
1874
int wc_Sha3_384_Update(wc_Sha3* sha3, const byte* data, word32 len)
1875
104
{
1876
104
    return wc_Sha3Update(sha3, data, len, WC_SHA3_384_COUNT);
1877
104
}
1878
1879
/* Calculate the SHA3-384 hash based on all the message data seen.
1880
 * The state is initialized ready for a new message to hash.
1881
 *
1882
 * sha3  wc_Sha3 object holding state.
1883
 * hash  Buffer to hold the hash result. Must be at least 48 bytes.
1884
 * returns 0 on success.
1885
 */
1886
int wc_Sha3_384_Final(wc_Sha3* sha3, byte* hash)
1887
104
{
1888
104
    return wc_Sha3Final(sha3, hash, WC_SHA3_384_COUNT, WC_SHA3_384_DIGEST_SIZE);
1889
104
}
1890
1891
/* Dispose of any dynamically allocated data from the SHA3-384 operation.
1892
 * (Required for async ops.)
1893
 *
1894
 * sha3  wc_Sha3 object holding state.
1895
 * returns 0 on success.
1896
 */
1897
void wc_Sha3_384_Free(wc_Sha3* sha3)
1898
104
{
1899
104
    wc_Sha3Free(sha3);
1900
104
}
1901
1902
/* Calculate the SHA3-384 hash based on all the message data so far.
1903
 * More message data can be added, after this operation, using the current
1904
 * state.
1905
 *
1906
 * sha3  wc_Sha3 object holding state.
1907
 * hash  Buffer to hold the hash result. Must be at least 48 bytes.
1908
 * returns 0 on success.
1909
 */
1910
int wc_Sha3_384_GetHash(wc_Sha3* sha3, byte* hash)
1911
0
{
1912
0
    return wc_Sha3GetHash(sha3, hash, WC_SHA3_384_COUNT, WC_SHA3_384_DIGEST_SIZE);
1913
0
}
1914
1915
/* Copy the state of the SHA3-384 operation.
1916
 *
1917
 * src  wc_Sha3 object holding state top copy.
1918
 * dst  wc_Sha3 object to copy into.
1919
 * returns 0 on success.
1920
 */
1921
int wc_Sha3_384_Copy(wc_Sha3* src, wc_Sha3* dst)
1922
0
{
1923
0
    return wc_Sha3Copy(src, dst);
1924
0
}
1925
#endif
1926
1927
#ifndef WOLFSSL_NOSHA3_512
1928
/* Initialize the state for a SHA3-512 hash operation.
1929
 *
1930
 * sha3   wc_Sha3 object holding state.
1931
 * heap   Heap reference for dynamic memory allocation. (Used in async ops.)
1932
 * devId  Device identifier for asynchronous operation.
1933
 * returns 0 on success.
1934
 */
1935
int wc_InitSha3_512(wc_Sha3* sha3, void* heap, int devId)
1936
121
{
1937
121
    return wc_InitSha3(sha3, heap, devId);
1938
121
}
1939
1940
/* Update the SHA3-512 hash state with message data.
1941
 *
1942
 * sha3  wc_Sha3 object holding state.
1943
 * data  Message data to be hashed.
1944
 * len   Length of the message data.
1945
 * returns 0 on success.
1946
 */
1947
int wc_Sha3_512_Update(wc_Sha3* sha3, const byte* data, word32 len)
1948
9.10k
{
1949
9.10k
    return wc_Sha3Update(sha3, data, len, WC_SHA3_512_COUNT);
1950
9.10k
}
1951
1952
/* Calculate the SHA3-512 hash based on all the message data seen.
1953
 * The state is initialized ready for a new message to hash.
1954
 *
1955
 * sha3  wc_Sha3 object holding state.
1956
 * hash  Buffer to hold the hash result. Must be at least 64 bytes.
1957
 * returns 0 on success.
1958
 */
1959
int wc_Sha3_512_Final(wc_Sha3* sha3, byte* hash)
1960
4.61k
{
1961
4.61k
    return wc_Sha3Final(sha3, hash, WC_SHA3_512_COUNT, WC_SHA3_512_DIGEST_SIZE);
1962
4.61k
}
1963
1964
/* Dispose of any dynamically allocated data from the SHA3-512 operation.
1965
 * (Required for async ops.)
1966
 *
1967
 * sha3  wc_Sha3 object holding state.
1968
 * returns 0 on success.
1969
 */
1970
void wc_Sha3_512_Free(wc_Sha3* sha3)
1971
121
{
1972
121
    wc_Sha3Free(sha3);
1973
121
}
1974
1975
/* Calculate the SHA3-512 hash based on all the message data so far.
1976
 * More message data can be added, after this operation, using the current
1977
 * state.
1978
 *
1979
 * sha3  wc_Sha3 object holding state.
1980
 * hash  Buffer to hold the hash result. Must be at least 64 bytes.
1981
 * returns 0 on success.
1982
 */
1983
int wc_Sha3_512_GetHash(wc_Sha3* sha3, byte* hash)
1984
0
{
1985
0
    return wc_Sha3GetHash(sha3, hash, WC_SHA3_512_COUNT, WC_SHA3_512_DIGEST_SIZE);
1986
0
}
1987
1988
/* Copy the state of the SHA3-512 operation.
1989
 *
1990
 * src  wc_Sha3 object holding state top copy.
1991
 * dst  wc_Sha3 object to copy into.
1992
 * returns 0 on success.
1993
 */
1994
int wc_Sha3_512_Copy(wc_Sha3* src, wc_Sha3* dst)
1995
0
{
1996
0
    return wc_Sha3Copy(src, dst);
1997
0
}
1998
#endif
1999
2000
#ifdef WOLFSSL_HASH_FLAGS
2001
int wc_Sha3_SetFlags(wc_Sha3* sha3, word32 flags)
2002
0
{
2003
0
    if (sha3) {
2004
0
        sha3->flags = flags;
2005
0
    }
2006
0
    return 0;
2007
0
}
2008
int wc_Sha3_GetFlags(wc_Sha3* sha3, word32* flags)
2009
0
{
2010
0
    if (sha3 && flags) {
2011
0
        *flags = sha3->flags;
2012
0
    }
2013
0
    return 0;
2014
0
}
2015
#endif
2016
2017
#ifdef WOLFSSL_SHAKE128
2018
/* Initialize the state for a Shake128 hash operation.
2019
 *
2020
 * shake  wc_Shake object holding state.
2021
 * heap   Heap reference for dynamic memory allocation. (Used in async ops.)
2022
 * devId  Device identifier for asynchronous operation.
2023
 * returns 0 on success.
2024
 */
2025
int wc_InitShake128(wc_Shake* shake, void* heap, int devId)
2026
41.6k
{
2027
41.6k
    return wc_InitSha3(shake, heap, devId);
2028
41.6k
}
2029
2030
#if defined(PSOC6_HASH_SHA3)
2031
2032
int wc_Shake128_Update(wc_Shake* shake, const byte* data, word32 len)
2033
{
2034
    int ret;
2035
    if (shake == NULL || (data == NULL && len > 0)) {
2036
         return BAD_FUNC_ARG;
2037
    }
2038
2039
    if (data == NULL) {
2040
        /* len is 0 here: valid, but do nothing */
2041
        return 0;
2042
    }
2043
2044
    /* Lock the mutex to perform crypto operations */
2045
    ret = wolfSSL_CryptHwMutexLock();
2046
    if (ret == 0) {
2047
        /* Perform SHA3 on the input data and update the hash state */
2048
        ret = wc_Psoc6_Sha3_Update(shake, data, len, WC_SHA3_128_COUNT);
2049
        /* Release the lock */
2050
        wolfSSL_CryptHwMutexUnLock();
2051
    }
2052
2053
    return ret;
2054
}
2055
2056
int wc_Shake128_Final(wc_Shake* shake, byte* hash, word32 hashLen)
2057
{
2058
    int ret;
2059
2060
    if (shake == NULL || hash == NULL) {
2061
        return BAD_FUNC_ARG;
2062
    }
2063
2064
    /* Lock the mutex to perform crypto operations */
2065
    ret = wolfSSL_CryptHwMutexLock();
2066
    if (ret == 0) {
2067
        /* Finalize SHA3 operations and produce digest */
2068
        ret = wc_Psoc6_Sha3_Final(shake, 0x1f, hash, WC_SHA3_128_COUNT, hashLen);
2069
        if (ret == 0) {
2070
            /* Initialize hash state for SHA-3 operation */
2071
            ret = wc_Psoc6_Sha3_Init(shake);
2072
        }
2073
        /* Release the lock */
2074
        wolfSSL_CryptHwMutexUnLock();
2075
    }
2076
2077
    return ret;
2078
2079
}
2080
2081
int wc_Shake128_Absorb(wc_Shake* shake, const byte* data, word32 len)
2082
{
2083
    int ret;
2084
2085
    if ((shake == NULL) || (data == NULL && len != 0)) {
2086
        return BAD_FUNC_ARG;
2087
    }
2088
2089
    /* Lock the mutex to perform crypto operations */
2090
    ret = wolfSSL_CryptHwMutexLock();
2091
    if (ret == 0) {
2092
        /* Perform SHA3 on the input data and update the hash state */
2093
        ret = wc_Psoc6_Sha3_Update(shake, data, len, WC_SHA3_128_COUNT);
2094
        if (ret == 0) {
2095
            /* Finalize SHA3 operations and produce digest */
2096
            ret = wc_Psoc6_Sha3_Final(shake, 0x1f, NULL, WC_SHA3_128_COUNT, 0);
2097
        }
2098
        /* Release the lock */
2099
        wolfSSL_CryptHwMutexUnLock();
2100
    }
2101
2102
    return ret;
2103
}
2104
2105
2106
int wc_Shake128_SqueezeBlocks(wc_Shake* shake, byte* out, word32 blockCnt)
2107
{
2108
    int ret;
2109
    if ((shake == NULL) || (out == NULL && blockCnt != 0)) {
2110
        return BAD_FUNC_ARG;
2111
    }
2112
2113
    /* Lock the mutex to perform crypto operations */
2114
    ret = wolfSSL_CryptHwMutexLock();
2115
    if (ret == 0) {
2116
        /* Squeeze output blocks from current hash state */
2117
        ret = wc_Psoc6_Shake_SqueezeBlocks(shake, out, blockCnt);
2118
        /* Release the lock */
2119
        wolfSSL_CryptHwMutexUnLock();
2120
    }
2121
2122
    return ret;
2123
}
2124
#else
2125
/* Update the SHAKE128 hash state with message data.
2126
 *
2127
 * shake  wc_Shake object holding state.
2128
 * data  Message data to be hashed.
2129
 * len   Length of the message data.
2130
 * returns 0 on success.
2131
 */
2132
int wc_Shake128_Update(wc_Shake* shake, const byte* data, word32 len)
2133
181
{
2134
181
    if (shake == NULL) {
2135
0
        return BAD_FUNC_ARG;
2136
0
    }
2137
2138
181
    if (data == NULL && len == 0) {
2139
        /* valid, but do nothing */
2140
3
        return 0;
2141
3
    }
2142
2143
178
    if (data == NULL) {
2144
0
        return BAD_FUNC_ARG;
2145
0
    }
2146
2147
178
#ifdef WOLF_CRYPTO_CB
2148
178
    #ifndef WOLF_CRYPTO_CB_FIND
2149
178
    if (shake->devId != INVALID_DEVID)
2150
0
    #endif
2151
0
    {
2152
0
        int ret = wc_CryptoCb_Shake(shake, WC_HASH_TYPE_SHAKE128, data, len,
2153
0
            NULL, 0);
2154
0
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2155
0
            return ret;
2156
        /* fall-through when unavailable */
2157
0
    }
2158
178
#endif
2159
2160
178
    return Sha3Update(shake, data, len, WC_SHA3_128_COUNT);
2161
178
}
2162
2163
/* Calculate the SHAKE128 hash based on all the message data seen.
2164
 * The state is initialized ready for a new message to hash.
2165
 *
2166
 * shake  wc_Shake object holding state.
2167
 * hash  Buffer to hold the hash result. Must be at least 64 bytes.
2168
 * returns 0 on success.
2169
 */
2170
int wc_Shake128_Final(wc_Shake* shake, byte* hash, word32 hashLen)
2171
181
{
2172
181
    int ret;
2173
2174
181
    if (shake == NULL || hash == NULL) {
2175
0
        return BAD_FUNC_ARG;
2176
0
    }
2177
2178
181
#ifdef WOLF_CRYPTO_CB
2179
181
    #ifndef WOLF_CRYPTO_CB_FIND
2180
181
    if (shake->devId != INVALID_DEVID)
2181
0
    #endif
2182
0
    {
2183
0
        ret = wc_CryptoCb_Shake(shake, WC_HASH_TYPE_SHAKE128, NULL, 0, hash,
2184
0
            hashLen);
2185
0
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2186
0
            return ret;
2187
        /* fall-through when unavailable */
2188
0
    }
2189
181
#endif
2190
2191
181
    ret = Sha3Final(shake, 0x1f, hash, WC_SHA3_128_COUNT, hashLen);
2192
181
    if (ret != 0)
2193
0
        return ret;
2194
2195
181
    return InitSha3(shake);  /* reset state */
2196
181
}
2197
2198
/* Absorb the data for squeezing.
2199
 *
2200
 * Update and final with data but no output and no reset
2201
 *
2202
 * shake  wc_Shake object holding state.
2203
 * data  Data to absorb.
2204
 * len  Length of d to absorb in bytes.
2205
 * returns 0 on success.
2206
 */
2207
int wc_Shake128_Absorb(wc_Shake* shake, const byte* data, word32 len)
2208
41.4k
{
2209
41.4k
    int ret;
2210
2211
41.4k
    if ((shake == NULL) || (data == NULL && len != 0)) {
2212
0
        return BAD_FUNC_ARG;
2213
0
    }
2214
2215
41.4k
    ret = Sha3Update(shake, data, len, WC_SHA3_128_COUNT);
2216
41.4k
    if (ret == 0) {
2217
41.4k
        byte hash[1];
2218
41.4k
        ret = Sha3Final(shake, 0x1f, hash, WC_SHA3_128_COUNT, 0);
2219
41.4k
    }
2220
    /* No partial data. */
2221
41.4k
    shake->i = 0;
2222
2223
41.4k
    return ret;
2224
41.4k
}
2225
2226
#ifdef WC_C_DYNAMIC_FALLBACK
2227
    #undef SHA3_BLOCK
2228
    #undef SHA3_BLOCK_N
2229
    #define SHA3_BLOCK (shake->sha3_block)
2230
    #define SHA3_BLOCK_N (shake->sha3_block_n)
2231
#endif
2232
2233
/* Squeeze the state to produce pseudo-random output.
2234
 *
2235
 * shake  wc_Shake object holding state.
2236
 * out  Output buffer.
2237
 * blockCnt  Number of blocks to write.
2238
 * returns 0 on success.
2239
 */
2240
int wc_Shake128_SqueezeBlocks(wc_Shake* shake, byte* out, word32 blockCnt)
2241
41.7k
{
2242
#if defined(WC_C_DYNAMIC_FALLBACK) && defined(USE_INTEL_SPEEDUP)
2243
    void (*sha3_block)(word64 *s);
2244
#endif
2245
2246
41.7k
    if ((shake == NULL) || (out == NULL && blockCnt != 0)) {
2247
0
        return BAD_FUNC_ARG;
2248
0
    }
2249
2250
#ifdef USE_INTEL_SPEEDUP
2251
#ifdef WC_C_DYNAMIC_FALLBACK
2252
    sha3_block = SHA3_BLOCK;
2253
#endif
2254
2255
    if (SHA3_BLOCK_VREGS(sha3_block)) {
2256
        int ret = SAVE_VECTOR_REGISTERS2();
2257
        if (ret != 0) {
2258
#ifdef WC_C_DYNAMIC_FALLBACK
2259
            sha3_block = BlockSha3;
2260
#else
2261
            return ret;
2262
#endif
2263
        }
2264
    }
2265
#endif /* USE_INTEL_SPEEDUP */
2266
2267
166k
    for (; (blockCnt > 0); blockCnt--) {
2268
    #ifdef SHA3_FUNC_PTR
2269
        (*sha3_block)(shake->s);
2270
    #else
2271
124k
        BlockSha3(shake->s);
2272
124k
    #endif
2273
    #if defined(BIG_ENDIAN_ORDER)
2274
        ByteReverseWords64((word64*)out, shake->s, WC_SHA3_128_COUNT * 8);
2275
    #elif defined(WOLFSSL_WIDE_BYTE)
2276
        Sha3SqueezeBytes(out, shake->s, WC_SHA3_128_COUNT * 8);
2277
    #else
2278
124k
        XMEMCPY(out, shake->s, WC_SHA3_128_COUNT * 8);
2279
124k
    #endif
2280
124k
        out += WC_SHA3_128_COUNT * 8;
2281
124k
    }
2282
2283
#ifdef USE_INTEL_SPEEDUP
2284
    if (SHA3_BLOCK_VREGS(sha3_block))
2285
        RESTORE_VECTOR_REGISTERS();
2286
#endif
2287
2288
41.7k
    return 0;
2289
41.7k
}
2290
#endif
2291
2292
2293
/* Dispose of any dynamically allocated data from the SHAKE128 operation.
2294
 * (Required for async ops.)
2295
 *
2296
 * shake  wc_Shake object holding state.
2297
 * returns 0 on success.
2298
 */
2299
void wc_Shake128_Free(wc_Shake* shake)
2300
181
{
2301
181
    wc_Sha3Free(shake);
2302
181
}
2303
2304
/* Copy the state of the SHA3-512 operation.
2305
 *
2306
 * src  wc_Shake object holding state top copy.
2307
 * dst  wc_Shake object to copy into.
2308
 * returns 0 on success.
2309
 */
2310
int wc_Shake128_Copy(wc_Shake* src, wc_Shake* dst)
2311
0
{
2312
0
    return wc_Sha3Copy(src, dst);
2313
0
}
2314
#endif
2315
2316
#ifdef WOLFSSL_SHAKE256
2317
/* Initialize the state for a Shake256 hash operation.
2318
 *
2319
 * shake  wc_Shake object holding state.
2320
 * heap   Heap reference for dynamic memory allocation. (Used in async ops.)
2321
 * devId  Device identifier for asynchronous operation.
2322
 * returns 0 on success.
2323
 */
2324
int wc_InitShake256(wc_Shake* shake, void* heap, int devId)
2325
14.9k
{
2326
14.9k
    return wc_InitSha3(shake, heap, devId);
2327
14.9k
}
2328
2329
2330
#ifdef PSOC6_HASH_SHA3
2331
2332
int wc_Shake256_Update(wc_Shake* shake, const byte* data, word32 len)
2333
{
2334
    int ret;
2335
    if (shake == NULL || (data == NULL && len > 0)) {
2336
         return BAD_FUNC_ARG;
2337
    }
2338
2339
    if (data == NULL) {
2340
        /* len is 0 here: valid, but do nothing */
2341
        return 0;
2342
    }
2343
2344
    /* Lock the mutex to perform crypto operations */
2345
    ret = wolfSSL_CryptHwMutexLock();
2346
    if (ret == 0) {
2347
        /* Perform SHA3 on the input data and update the hash state */
2348
        ret = wc_Psoc6_Sha3_Update(shake, data, len, WC_SHA3_256_COUNT);
2349
        /* Release the lock */
2350
        wolfSSL_CryptHwMutexUnLock();
2351
    }
2352
2353
    return ret;
2354
}
2355
2356
int wc_Shake256_Final(wc_Shake* shake, byte* hash, word32 hashLen)
2357
{
2358
    int ret;
2359
    if (shake == NULL || hash == NULL) {
2360
        return BAD_FUNC_ARG;
2361
    }
2362
2363
    /* Lock the mutex to perform crypto operations */
2364
    ret = wolfSSL_CryptHwMutexLock();
2365
    if (ret == 0) {
2366
        /* Finalize SHA3 operations and produce digest */
2367
        ret = wc_Psoc6_Sha3_Final(shake, 0x1f, hash, WC_SHA3_256_COUNT, hashLen);
2368
        if (ret == 0) {
2369
            /* Initialize hash state for SHA-3 operation */
2370
            ret = wc_Psoc6_Sha3_Init(shake);
2371
        }
2372
        /* Release the lock */
2373
        wolfSSL_CryptHwMutexUnLock();
2374
    }
2375
2376
    return ret;
2377
}
2378
2379
int wc_Shake256_Absorb(wc_Shake* shake, const byte* data, word32 len)
2380
{
2381
    int ret;
2382
2383
    if ((shake == NULL) || (data == NULL && len != 0)) {
2384
        return BAD_FUNC_ARG;
2385
    }
2386
2387
    /* Lock the mutex to perform crypto operations */
2388
    ret = wolfSSL_CryptHwMutexLock();
2389
    if (ret == 0) {
2390
        /* Perform SHA3 on the input data and update the hash state */
2391
        ret = wc_Psoc6_Sha3_Update(shake, data, len, WC_SHA3_256_COUNT);
2392
        if (ret == 0) {
2393
            /* Finalize SHA3 operations and produce digest */
2394
            ret = wc_Psoc6_Sha3_Final(shake, 0x1f, NULL, WC_SHA3_256_COUNT, 0);
2395
        }
2396
        /* Release the lock */
2397
        wolfSSL_CryptHwMutexUnLock();
2398
    }
2399
2400
    return ret;
2401
}
2402
2403
int wc_Shake256_SqueezeBlocks(wc_Shake* shake, byte* out, word32 blockCnt)
2404
{
2405
    int ret;
2406
    if ((shake == NULL) || (out == NULL && blockCnt != 0)) {
2407
        return BAD_FUNC_ARG;
2408
    }
2409
2410
    /* Lock the mutex to perform crypto operations */
2411
    ret = wolfSSL_CryptHwMutexLock();
2412
    if (ret == 0) {
2413
        /* Squeeze output blocks from current hash state */
2414
        ret = wc_Psoc6_Shake_SqueezeBlocks(shake, out, blockCnt);
2415
        /* Release the lock */
2416
        wolfSSL_CryptHwMutexUnLock();
2417
    }
2418
2419
    return ret;
2420
}
2421
2422
#else
2423
/* Update the SHAKE256 hash state with message data.
2424
 *
2425
 * shake  wc_Shake object holding state.
2426
 * data  Message data to be hashed.
2427
 * len   Length of the message data.
2428
 * returns 0 on success.
2429
 */
2430
int wc_Shake256_Update(wc_Shake* shake, const byte* data, word32 len)
2431
167k
{
2432
167k
    if (shake == NULL) {
2433
0
        return BAD_FUNC_ARG;
2434
0
    }
2435
2436
167k
    if (data == NULL && len == 0) {
2437
        /* valid, but do nothing */
2438
15
        return 0;
2439
15
    }
2440
2441
167k
    if (data == NULL) {
2442
0
        return BAD_FUNC_ARG;
2443
0
    }
2444
2445
167k
#ifdef WOLF_CRYPTO_CB
2446
167k
    #ifndef WOLF_CRYPTO_CB_FIND
2447
167k
    if (shake->devId != INVALID_DEVID)
2448
25.4k
    #endif
2449
25.4k
    {
2450
25.4k
        int ret = wc_CryptoCb_Shake(shake, WC_HASH_TYPE_SHAKE256, data, len,
2451
25.4k
            NULL, 0);
2452
25.4k
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2453
0
            return ret;
2454
        /* fall-through when unavailable */
2455
25.4k
    }
2456
167k
#endif
2457
2458
167k
    return Sha3Update(shake, data, len, WC_SHA3_256_COUNT);
2459
167k
}
2460
2461
/* Calculate the SHAKE256 hash based on all the message data seen.
2462
 * The state is initialized ready for a new message to hash.
2463
 *
2464
 * shake  wc_Shake object holding state.
2465
 * hash  Buffer to hold the hash result. Must be at least 64 bytes.
2466
 * hashLen Size of hash in bytes.
2467
 * returns 0 on success.
2468
 */
2469
int wc_Shake256_Final(wc_Shake* shake, byte* hash, word32 hashLen)
2470
36.1k
{
2471
36.1k
    int ret;
2472
2473
36.1k
    if (shake == NULL || hash == NULL) {
2474
0
        return BAD_FUNC_ARG;
2475
0
    }
2476
2477
36.1k
#ifdef WOLF_CRYPTO_CB
2478
36.1k
    #ifndef WOLF_CRYPTO_CB_FIND
2479
36.1k
    if (shake->devId != INVALID_DEVID)
2480
25.4k
    #endif
2481
25.4k
    {
2482
25.4k
        ret = wc_CryptoCb_Shake(shake, WC_HASH_TYPE_SHAKE256, NULL, 0, hash,
2483
25.4k
            hashLen);
2484
25.4k
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
2485
0
            return ret;
2486
        /* fall-through when unavailable */
2487
25.4k
    }
2488
36.1k
#endif
2489
2490
36.1k
    ret = Sha3Final(shake, 0x1f, hash, WC_SHA3_256_COUNT, hashLen);
2491
36.1k
    if (ret != 0)
2492
0
        return ret;
2493
2494
36.1k
    return InitSha3(shake);  /* reset state */
2495
36.1k
}
2496
2497
/* Absorb the data for squeezing.
2498
 *
2499
 * Update and final with data but no output and no reset
2500
 *
2501
 * shake  wc_Shake object holding state.
2502
 * data  Data to absorb.
2503
 * len  Length of d to absorb in bytes.
2504
 * returns 0 on success.
2505
 */
2506
int wc_Shake256_Absorb(wc_Shake* shake, const byte* data, word32 len)
2507
0
{
2508
0
    int ret;
2509
2510
0
    if ((shake == NULL) || (data == NULL && len != 0)) {
2511
0
        return BAD_FUNC_ARG;
2512
0
    }
2513
2514
0
    ret = Sha3Update(shake, data, len, WC_SHA3_256_COUNT);
2515
0
    if (ret == 0) {
2516
0
        byte hash[1];
2517
0
        ret = Sha3Final(shake, 0x1f, hash, WC_SHA3_256_COUNT, 0);
2518
0
    }
2519
    /* No partial data. */
2520
0
    shake->i = 0;
2521
2522
0
    return ret;
2523
0
}
2524
2525
/* Squeeze the state to produce pseudo-random output.
2526
 *
2527
 * shake  wc_Shake object holding state.
2528
 * out  Output buffer.
2529
 * blockCnt  Number of blocks to write.
2530
 * returns 0 on success.
2531
 */
2532
int wc_Shake256_SqueezeBlocks(wc_Shake* shake, byte* out, word32 blockCnt)
2533
0
{
2534
#if defined(WC_C_DYNAMIC_FALLBACK) && defined(USE_INTEL_SPEEDUP)
2535
    void (*sha3_block)(word64 *s);
2536
#endif
2537
2538
0
    if ((shake == NULL) || (out == NULL && blockCnt != 0)) {
2539
0
        return BAD_FUNC_ARG;
2540
0
    }
2541
2542
#ifdef USE_INTEL_SPEEDUP
2543
#ifdef WC_C_DYNAMIC_FALLBACK
2544
    sha3_block = SHA3_BLOCK;
2545
#endif
2546
2547
    if (SHA3_BLOCK_VREGS(sha3_block)) {
2548
        int ret = SAVE_VECTOR_REGISTERS2();
2549
        if (ret != 0) {
2550
#ifdef WC_C_DYNAMIC_FALLBACK
2551
            sha3_block = BlockSha3;
2552
#else
2553
            return ret;
2554
#endif
2555
        }
2556
    }
2557
#endif /* USE_INTEL_SPEEDUP */
2558
2559
0
    for (; (blockCnt > 0); blockCnt--) {
2560
    #ifdef SHA3_FUNC_PTR
2561
        (*sha3_block)(shake->s);
2562
    #else
2563
0
        BlockSha3(shake->s);
2564
0
    #endif
2565
    #if defined(BIG_ENDIAN_ORDER)
2566
        ByteReverseWords64((word64*)out, shake->s, WC_SHA3_256_COUNT * 8);
2567
    #elif defined(WOLFSSL_WIDE_BYTE)
2568
        Sha3SqueezeBytes(out, shake->s, WC_SHA3_256_COUNT * 8);
2569
    #else
2570
0
        XMEMCPY(out, shake->s, WC_SHA3_256_COUNT * 8);
2571
0
    #endif
2572
0
        out += WC_SHA3_256_COUNT * 8;
2573
0
    }
2574
2575
#ifdef USE_INTEL_SPEEDUP
2576
    if (SHA3_BLOCK_VREGS(sha3_block))
2577
        RESTORE_VECTOR_REGISTERS();
2578
#endif
2579
2580
0
    return 0;
2581
0
}
2582
#endif
2583
2584
/* Dispose of any dynamically allocated data from the SHAKE256 operation.
2585
 * (Required for async ops.)
2586
 *
2587
 * shake  wc_Shake object holding state.
2588
 * returns 0 on success.
2589
 */
2590
void wc_Shake256_Free(wc_Shake* shake)
2591
10.5k
{
2592
10.5k
    wc_Sha3Free(shake);
2593
10.5k
}
2594
2595
/* Copy the state of the SHA3-512 operation.
2596
 *
2597
 * src  wc_Shake object holding state top copy.
2598
 * dst  wc_Shake object to copy into.
2599
 * returns 0 on success.
2600
 */
2601
int wc_Shake256_Copy(wc_Shake* src, wc_Shake* dst)
2602
0
{
2603
0
    return wc_Sha3Copy(src, dst);
2604
0
}
2605
#endif
2606
2607
#if (defined(WOLFSSL_KMAC) || defined(WOLFSSL_CSHAKE)) && \
2608
    defined(WC_SHA3_SW_KECCAK)
2609
/* cSHAKE and KMAC - NIST SP 800-185.
2610
 *
2611
 * cSHAKE is a customizable SHAKE; KMAC is cSHAKE keyed with the function name
2612
 * "KMAC". Both feed length-prefixed strings into the SHAKE (KECCAK) sponge and
2613
 * (when customized) finalize with the cSHAKE domain-separation pad byte 0x04
2614
 * rather than SHAKE's 0x1f. The heavy lifting - absorbing message bytes and
2615
 * squeezing output - reuses the software Sha3Update()/Sha3Final() helpers
2616
 * above. The KMAC-specific code is compiled only when WOLFSSL_KMAC is set;
2617
 * cSHAKE is also available on its own via WOLFSSL_CSHAKE. */
2618
2619
/* left_encode(value) per NIST SP 800-185, section 2.3.1.
2620
 *
2621
 * A length byte giving the number of value bytes, followed by that many bytes
2622
 * of the value in big-endian (most significant first) order.
2623
 *
2624
 * @param [out] out    Buffer to write encoding to. Must hold at least 9 bytes.
2625
 * @param [in]  value  Value to encode. 0 encodes as the bytes 0x01 0x00.
2626
 *
2627
 * @return  Number of bytes written to out - between 2 and 9.
2628
 */
2629
static word32 KmacLeftEncode(byte* out, word64 value)
2630
{
2631
    word32 n = 1;
2632
    word64 v = value;
2633
2634
    /* Build up the number of significant bytes (min 1) by halving: test the
2635
     * top 32 bits, then each smaller half, shifting away counted bytes. */
2636
    if ((v >> 32) != 0) { n += 4; v >>= 32; }
2637
    if ((v >> 16) != 0) { n += 2; v >>= 16; }
2638
    if ((v >>  8) != 0) { n += 1;           }
2639
2640
    /* Length byte then the n value bytes big-endian.  Enter the switch at
2641
     * case n and fall through, storing least-significant byte first into
2642
     * out[n]..out[1]. */
2643
    out[0] = (byte)n;
2644
    switch (n) {
2645
        case 8: out[8] = (byte)value; value >>= 8; FALL_THROUGH;
2646
        case 7: out[7] = (byte)value; value >>= 8; FALL_THROUGH;
2647
        case 6: out[6] = (byte)value; value >>= 8; FALL_THROUGH;
2648
        case 5: out[5] = (byte)value; value >>= 8; FALL_THROUGH;
2649
        case 4: out[4] = (byte)value; value >>= 8; FALL_THROUGH;
2650
        case 3: out[3] = (byte)value; value >>= 8; FALL_THROUGH;
2651
        case 2: out[2] = (byte)value; value >>= 8; FALL_THROUGH;
2652
        default: out[1] = (byte)value;
2653
    }
2654
2655
    return n + 1;
2656
}
2657
2658
#ifdef WOLFSSL_KMAC
2659
/* right_encode(value) per NIST SP 800-185, section 2.3.1. Only used by KMAC
2660
 * (cSHAKE does not bind an output length).
2661
 *
2662
 * The value in big-endian (most significant first) order, followed by a length
2663
 * byte giving the number of value bytes.
2664
 *
2665
 * @param [out] out    Buffer to write encoding to. Must hold at least 9 bytes.
2666
 * @param [in]  value  Value to encode. 0 encodes as the bytes 0x00 0x01.
2667
 *
2668
 * @return  Number of bytes written to out - between 2 and 9.
2669
 */
2670
static word32 KmacRightEncode(byte* out, word64 value)
2671
{
2672
    word32 n = 1;
2673
    word64 v = value;
2674
2675
    /* Build up the number of significant bytes (min 1) by halving: test the
2676
     * top 32 bits, then each smaller half, shifting away counted bytes. */
2677
    if ((v >> 32) != 0) { n += 4; v >>= 32; }
2678
    if ((v >> 16) != 0) { n += 2; v >>= 16; }
2679
    if ((v >>  8) != 0) { n += 1;           }
2680
2681
    /* The n value bytes big-endian then the length byte.  Enter the switch at
2682
     * case n and fall through, storing least-significant byte first into
2683
     * out[n-1]..out[0]. */
2684
    switch (n) {
2685
        case 8: out[7] = (byte)value; value >>= 8; FALL_THROUGH;
2686
        case 7: out[6] = (byte)value; value >>= 8; FALL_THROUGH;
2687
        case 6: out[5] = (byte)value; value >>= 8; FALL_THROUGH;
2688
        case 5: out[4] = (byte)value; value >>= 8; FALL_THROUGH;
2689
        case 4: out[3] = (byte)value; value >>= 8; FALL_THROUGH;
2690
        case 3: out[2] = (byte)value; value >>= 8; FALL_THROUGH;
2691
        case 2: out[1] = (byte)value; value >>= 8; FALL_THROUGH;
2692
        default: out[0] = (byte)value;
2693
    }
2694
    out[n] = (byte)n;
2695
2696
    return n + 1;
2697
}
2698
#endif /* WOLFSSL_KMAC */
2699
2700
/* Zero-pad the current bytepad() block, per NIST SP 800-185, section 2.3.3.
2701
 *
2702
 * Fills the tail of the current block with zeros so the number of bytes fed
2703
 * into the bytepad() block becomes a multiple of the KECCAK rate, then flushes
2704
 * the completed block.  The block offset is the sponge's own shake->i.
2705
 *
2706
 * @param [in,out] shake  SHAKE (KECCAK) object holding the sponge state.
2707
 * @param [in]     count  KECCAK 64-bit words per block - rate / 8.
2708
 * @param [in]     rate   KECCAK rate in bytes - the block size.
2709
 *
2710
 * @return  0 on success.
2711
 * @return  Negative error code from the sponge update on failure.
2712
 */
2713
static int CshakeBytePad(wc_Sha3* shake, word32 count, word32 rate)
2714
{
2715
    int    ret = 0;
2716
    word32 pad = (rate - shake->i) % rate;
2717
2718
    if (pad > 0) {
2719
        /* Zero the rest of the block in place and flush it - a zero-length
2720
         * update with i == rate triggers the XOR-in and permutation. */
2721
        XMEMSET(shake->t + shake->i, 0, pad);
2722
        shake->i = rate;
2723
        ret = Sha3Update(shake, shake->t, 0, count);
2724
    }
2725
    return ret;
2726
}
2727
2728
/* Absorb the leading customization block shared by cSHAKE and KMAC:
2729
 *   bytepad(encode_string(name) || encode_string(custom), rate)
2730
 * (NIST SP 800-185, sections 3.2 and 3.3).
2731
 *
2732
 * Only ever called right after Init, so the sponge is fresh (shake->i is 0
2733
 * and shake->t is all zero). When the whole bytepad content fits in one block
2734
 * (the common case) it is copied straight into the block buffer and flushed
2735
 * once; otherwise the parts that may cross a block boundary go through
2736
 * Sha3Update.
2737
 *
2738
 * @param [in,out] shake      SHAKE (KECCAK) object holding the sponge state.
2739
 * @param [in]     count      KECCAK 64-bit words per block - rate / 8.
2740
 * @param [in]     name       Function-name string, NULL when nameLen is 0.
2741
 * @param [in]     nameLen    Length of name in bytes.
2742
 * @param [in]     custom     Customization string, NULL when customLen is 0.
2743
 * @param [in]     customLen  Length of custom in bytes.
2744
 *
2745
 * @return  0 on success.
2746
 * @return  Negative error code from the sponge update on failure.
2747
 */
2748
static int CshakeAbsorbBlock(wc_Sha3* shake, word32 count, const byte* name,
2749
    word32 nameLen, const byte* custom, word32 customLen)
2750
{
2751
    word32 rate = count * 8U;
2752
    byte   enc[9];
2753
    word32 e;
2754
    word32 h;
2755
    word32 avail;
2756
    int    ret = 0;
2757
2758
    /* left_encode(rate) || left_encode(nameLen * 8) straight into the block
2759
     * buffer - fits at the start of a fresh block. */
2760
    h  = KmacLeftEncode(shake->t, (word64)rate);
2761
    h += KmacLeftEncode(shake->t + h, (word64)nameLen * 8);
2762
    e  = KmacLeftEncode(enc, (word64)customLen * 8);
2763
    avail = rate - h;
2764
2765
    /* Common case: the whole bytepad content fits in this one block, so copy
2766
     * name || left_encode(customLen*8) || custom straight in and let the pad
2767
     * flush it - no per-piece Sha3Update.  Conditions are ordered to avoid
2768
     * word32 overflow when name/custom are large. */
2769
    if ((nameLen < avail) && (e < avail - nameLen) &&
2770
            (customLen < avail - nameLen - e)) {
2771
        if (nameLen > 0) {
2772
            XMEMCPY(shake->t + h, name, nameLen);
2773
            h += nameLen;
2774
        }
2775
        XMEMCPY(shake->t + h, enc, e);
2776
        h += e;
2777
        if (customLen > 0) {
2778
            XMEMCPY(shake->t + h, custom, customLen);
2779
            h += customLen;
2780
        }
2781
        shake->i = h;
2782
    }
2783
    else {
2784
        /* name and/or custom cross a block boundary - absorb them. */
2785
        shake->i = h;
2786
        if (nameLen > 0) {
2787
            ret = Sha3Update(shake, name, nameLen, count);
2788
        }
2789
        if (ret == 0) {
2790
            ret = Sha3Update(shake, enc, e, count);
2791
        }
2792
        if ((ret == 0) && (customLen > 0)) {
2793
            ret = Sha3Update(shake, custom, customLen, count);
2794
        }
2795
    }
2796
2797
    /* bytepad zero-fill - shake->i already tracks the block offset. */
2798
    if (ret == 0) {
2799
        ret = CshakeBytePad(shake, count, rate);
2800
    }
2801
    return ret;
2802
}
2803
2804
#ifdef WOLFSSL_KMAC
2805
/* Initialize a KMAC operation for the given KECCAK block count.
2806
 *
2807
 * count is WC_SHA3_128_COUNT for KMAC128 or WC_SHA3_256_COUNT for KMAC256.
2808
 * Absorbs the two leading cSHAKE/KMAC bytepad blocks, leaving the sponge ready
2809
 * for message data (NIST SP 800-185, sections 3.2 and 4.3):
2810
 *   bytepad(encode_string("KMAC") || encode_string(custom), rate)
2811
 *   bytepad(encode_string(key), rate)
2812
 *
2813
 * @param [out] kmac       KMAC object to initialize.
2814
 * @param [in]  count      KECCAK 64-bit words per block - rate / 8.
2815
 * @param [in]  key        Key bytes.
2816
 * @param [in]  keyLen     Length of key in bytes.
2817
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
2818
 * @param [in]  customLen  Length of custom in bytes.
2819
 * @param [in]  heap       Dynamic memory hint.
2820
 * @param [in]  devId      Device identifier.
2821
 *
2822
 * @return  0 on success.
2823
 * @return  BAD_FUNC_ARG when a NULL pointer has a non-zero length.
2824
 * @return  Negative error code from the sponge update on failure.
2825
 */
2826
static int KmacInit(wc_Kmac* kmac, word32 count, const byte* key, word32 keyLen,
2827
    const byte* custom, word32 customLen, void* heap, int devId)
2828
{
2829
    /* The KMAC function name string "KMAC". */
2830
    static const byte kmacName[4] = { 0x4b, 0x4d, 0x41, 0x43 };
2831
    word32 rate;
2832
    int    ret;
2833
2834
    if ((kmac == NULL) || ((key == NULL) && (keyLen != 0)) ||
2835
            ((custom == NULL) && (customLen != 0))) {
2836
        ret = BAD_FUNC_ARG;
2837
    }
2838
#ifdef HAVE_FIPS
2839
    else if (keyLen < KMAC_FIPS_MIN_KEY) {
2840
        ret = KMAC_MIN_KEYLEN_E;
2841
    }
2842
#endif
2843
    else {
2844
        kmac->count = count;
2845
        rate = count * 8U;
2846
        ret = wc_InitSha3(&kmac->shake, heap, devId);
2847
2848
        /* bytepad(encode_string("KMAC") || encode_string(custom), rate) */
2849
        if (ret == 0) {
2850
            ret = CshakeAbsorbBlock(&kmac->shake, count, kmacName,
2851
                (word32)sizeof(kmacName), custom, customLen);
2852
        }
2853
2854
        /* bytepad(encode_string(key), rate).  The block above flushed, so the
2855
         * sponge is at a block boundary (shake->i == 0) - write the length
2856
         * encodings straight into the block buffer, as in CshakeAbsorbBlock. */
2857
        if (ret == 0) {
2858
            word32 h;
2859
2860
            h  = KmacLeftEncode(kmac->shake.t, (word64)rate);
2861
            h += KmacLeftEncode(kmac->shake.t + h, (word64)keyLen * 8);
2862
            kmac->shake.i = h;
2863
2864
            if (keyLen > 0) {
2865
                /* Copy a key that fits into the block straight in and flush
2866
                 * once; a longer key crosses a boundary so is absorbed. */
2867
                if (keyLen < rate - h) {
2868
                    XMEMCPY(kmac->shake.t + h, key, keyLen);
2869
                    kmac->shake.i += keyLen;
2870
                }
2871
                else {
2872
                    ret = Sha3Update(&kmac->shake, key, keyLen, count);
2873
                }
2874
            }
2875
            if (ret == 0) {
2876
                ret = CshakeBytePad(&kmac->shake, count, rate);
2877
            }
2878
        }
2879
    }
2880
2881
    return ret;
2882
}
2883
2884
/* Absorb message data into a KMAC operation.
2885
 *
2886
 * @param [in,out] kmac   KMAC object holding the sponge state.
2887
 * @param [in]     in     Message bytes, or NULL when inLen is 0.
2888
 * @param [in]     inLen  Length of in in bytes.
2889
 *
2890
 * @return  0 on success.
2891
 * @return  BAD_FUNC_ARG on a NULL message with a non-zero length.
2892
 * @return  Negative error code from the sponge update on failure.
2893
 */
2894
static int KmacUpdate(wc_Kmac* kmac, const byte* in, word32 inLen)
2895
{
2896
    int ret;
2897
2898
    if ((kmac == NULL) || ((in == NULL) && (inLen != 0))) {
2899
        ret = BAD_FUNC_ARG;
2900
    }
2901
    else {
2902
        ret = Sha3Update(&kmac->shake, in, inLen, kmac->count);
2903
    }
2904
    return ret;
2905
}
2906
2907
/* Finalize a KMAC operation, producing outLen bytes of output.
2908
 *
2909
 * For fixed-length KMAC (xof == 0) the requested length is encoded into the
2910
 * message (right_encode(outLen * 8)) before the cSHAKE pad, so changing outLen
2911
 * changes the whole result - as required by SP 800-185. For the XOF variant
2912
 * (xof != 0) right_encode(0) is used and any number of output bytes may be
2913
 * produced without changing the leading bytes.
2914
 *
2915
 * @param [in,out] kmac    KMAC object holding the sponge state.
2916
 * @param [out]    out     Buffer to hold output.
2917
 * @param [in]     outLen  Number of output bytes to produce.
2918
 * @param [in]     xof     Non-zero to finalize as an XOF - encode length 0.
2919
 *
2920
 * @return  0 on success.
2921
 * @return  BAD_FUNC_ARG when kmac or out is NULL.
2922
 * @return  Negative error code from the sponge on failure.
2923
 */
2924
static int KmacFinal(wc_Kmac* kmac, byte* out, word32 outLen, int xof)
2925
{
2926
    word32 rate;
2927
    int    ret = 0;
2928
2929
    if ((kmac == NULL) || (out == NULL)) {
2930
        ret = BAD_FUNC_ARG;
2931
    }
2932
#ifdef HAVE_FIPS
2933
    else if ((xof == 0) && (outLen < KMAC_FIPS_MIN_OUTPUT)) {
2934
        ret = BAD_LENGTH_E;
2935
    }
2936
#endif
2937
    else if ((kmac->count < WC_SHA3_512_COUNT) ||
2938
             (kmac->count > WC_SHA3_128_COUNT) ||
2939
             (kmac->shake.i >= kmac->count * 8U)) {
2940
        ret = BAD_STATE_E;
2941
    }
2942
    else {
2943
        /* right_encode(outLen * 8), or right_encode(0) for the XOF. */
2944
        word64 v = xof ? (word64)0 : (word64)outLen * 8;
2945
        rate = kmac->count * 8U;
2946
2947
        /* The encoding is at most 9 bytes; when that many fit in the current
2948
         * block, write it straight into the block buffer, otherwise use a
2949
         * temporary and Sha3Update (which handles crossing the boundary). */
2950
        if (kmac->shake.i + 9 < rate) {
2951
            word32 l = KmacRightEncode(kmac->shake.t + kmac->shake.i, v);
2952
            kmac->shake.i += l;
2953
        }
2954
        else {
2955
            byte   enc[9];
2956
            word32 encLen = KmacRightEncode(enc, v);
2957
            ret = Sha3Update(&kmac->shake, enc, encLen, kmac->count);
2958
        }
2959
        if (ret == 0) {
2960
            /* cSHAKE domain separation pad (0x04), then squeeze outLen. */
2961
            ret = Sha3Final(&kmac->shake, 0x04, out, kmac->count, outLen);
2962
        }
2963
    }
2964
    return ret;
2965
}
2966
2967
/* Copy the state of a KMAC operation so it can be finalized more than once
2968
 * (for example over a common prefix).
2969
 *
2970
 * dst must be an initialized wc_Kmac: the copy releases any resources it
2971
 * already holds before overwriting it (as with wc_Sha3Copy/wc_Shake_Copy).
2972
 *
2973
 * @param [in]  src  KMAC object to copy from.
2974
 * @param [out] dst  Initialized KMAC object to copy into.
2975
 *
2976
 * @return  0 on success.
2977
 * @return  BAD_FUNC_ARG when src or dst is NULL.
2978
 * @return  Negative error code from the sponge copy on failure.
2979
 */
2980
static int KmacCopy(wc_Kmac* src, wc_Kmac* dst)
2981
{
2982
    int ret;
2983
2984
    if ((src == NULL) || (dst == NULL)) {
2985
        ret = BAD_FUNC_ARG;
2986
    }
2987
    else {
2988
        ret = wc_Sha3Copy(&src->shake, &dst->shake);
2989
        if (ret == 0) {
2990
            dst->count = src->count;
2991
        }
2992
    }
2993
    return ret;
2994
}
2995
#endif /* WOLFSSL_KMAC */
2996
2997
#if defined(WOLFSSL_CSHAKE128) || defined(WOLFSSL_CSHAKE256)
2998
/* Initialize a cSHAKE operation for the given KECCAK block count.
2999
 *
3000
 * count is WC_SHA3_128_COUNT for cSHAKE128 or WC_SHA3_256_COUNT for cSHAKE256.
3001
 * When both the function-name and customization strings are empty, cSHAKE is
3002
 * defined to reduce to plain SHAKE (NIST SP 800-185, section 3.3), so no
3003
 * customization block is absorbed and the SHAKE pad (0x1f) is used.
3004
 *
3005
 * @param [out] cshake     cSHAKE object to initialize.
3006
 * @param [in]  count      KECCAK 64-bit words per block - rate / 8.
3007
 * @param [in]  name       Function-name string, or NULL when nameLen is 0.
3008
 * @param [in]  nameLen    Length of name in bytes.
3009
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3010
 * @param [in]  customLen  Length of custom in bytes.
3011
 * @param [in]  heap       Dynamic memory hint.
3012
 * @param [in]  devId      Device identifier.
3013
 *
3014
 * @return  0 on success.
3015
 * @return  BAD_FUNC_ARG when a NULL pointer has a non-zero length.
3016
 * @return  Negative error code from the sponge update on failure.
3017
 */
3018
static int CshakeInit(wc_Cshake* cshake, word32 count, const byte* name,
3019
    word32 nameLen, const byte* custom, word32 customLen, void* heap, int devId)
3020
{
3021
    int ret;
3022
3023
    if ((cshake == NULL) || ((name == NULL) && (nameLen != 0)) ||
3024
            ((custom == NULL) && (customLen != 0))) {
3025
        ret = BAD_FUNC_ARG;
3026
    }
3027
    else {
3028
        cshake->count = count;
3029
        ret = wc_InitSha3(&cshake->shake, heap, devId);
3030
        if (ret == 0) {
3031
            if ((nameLen == 0) && (customLen == 0)) {
3032
                /* No customization: cSHAKE reduces to SHAKE. */
3033
                cshake->pad = 0x1f;
3034
            }
3035
            else {
3036
                cshake->pad = 0x04;
3037
                ret = CshakeAbsorbBlock(&cshake->shake, count, name, nameLen,
3038
                    custom, customLen);
3039
            }
3040
        }
3041
    }
3042
    return ret;
3043
}
3044
3045
/* Absorb message data into a cSHAKE operation.
3046
 *
3047
 * @param [in,out] cshake  cSHAKE object holding the sponge state.
3048
 * @param [in]     in      Message bytes, or NULL when inLen is 0.
3049
 * @param [in]     inLen   Length of in in bytes.
3050
 *
3051
 * @return  0 on success.
3052
 * @return  BAD_FUNC_ARG on a NULL message with a non-zero length.
3053
 * @return  Negative error code from the sponge update on failure.
3054
 */
3055
static int CshakeUpdate(wc_Cshake* cshake, const byte* in, word32 inLen)
3056
{
3057
    int ret;
3058
3059
    if ((cshake == NULL) || ((in == NULL) && (inLen != 0))) {
3060
        ret = BAD_FUNC_ARG;
3061
    }
3062
    else {
3063
        ret = Sha3Update(&cshake->shake, in, inLen, cshake->count);
3064
    }
3065
    return ret;
3066
}
3067
3068
/* Finalize a cSHAKE operation, squeezing outLen bytes. cSHAKE is an XOF, so
3069
 * the output length is not bound into the result and a longer squeeze extends
3070
 * a shorter one.
3071
 *
3072
 * @param [in,out] cshake  cSHAKE object holding the sponge state.
3073
 * @param [out]    out     Buffer to hold output.
3074
 * @param [in]     outLen  Number of output bytes to produce.
3075
 *
3076
 * @return  0 on success.
3077
 * @return  BAD_FUNC_ARG when cshake or out is NULL.
3078
 * @return  Negative error code from the sponge on failure.
3079
 */
3080
static int CshakeFinal(wc_Cshake* cshake, byte* out, word32 outLen)
3081
{
3082
    int ret;
3083
3084
    if ((cshake == NULL) || (out == NULL)) {
3085
        ret = BAD_FUNC_ARG;
3086
    }
3087
    else {
3088
        ret = Sha3Final(&cshake->shake, cshake->pad, out, cshake->count,
3089
            outLen);
3090
    }
3091
    return ret;
3092
}
3093
3094
/* Copy the state of a cSHAKE operation so it can be finalized more than once
3095
 * (for example over a common message prefix).
3096
 *
3097
 * dst must be an initialized wc_Cshake: the copy releases any resources it
3098
 * already holds before overwriting it (as with wc_Sha3Copy/wc_Shake_Copy).
3099
 *
3100
 * @param [in]  src  cSHAKE object to copy from.
3101
 * @param [out] dst  Initialized cSHAKE object to copy into.
3102
 *
3103
 * @return  0 on success.
3104
 * @return  BAD_FUNC_ARG when src or dst is NULL.
3105
 * @return  Negative error code from the sponge copy on failure.
3106
 */
3107
static int CshakeCopy(wc_Cshake* src, wc_Cshake* dst)
3108
{
3109
    int ret;
3110
3111
    if ((src == NULL) || (dst == NULL)) {
3112
        ret = BAD_FUNC_ARG;
3113
    }
3114
    else {
3115
        ret = wc_Sha3Copy(&src->shake, &dst->shake);
3116
        if (ret == 0) {
3117
            dst->count = src->count;
3118
            dst->pad   = src->pad;
3119
        }
3120
    }
3121
    return ret;
3122
}
3123
#endif /* WOLFSSL_CSHAKE128 || WOLFSSL_CSHAKE256 */
3124
3125
#ifdef WOLFSSL_KMAC128
3126
/* Initialize a KMAC128 operation with a key and optional customization string.
3127
 *
3128
 * @param [out] kmac       wc_Kmac object to initialize.
3129
 * @param [in]  key        Key bytes.
3130
 * @param [in]  keyLen     Length of the key in bytes.
3131
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3132
 * @param [in]  customLen  Length of the customization string in bytes.
3133
 * @param [in]  heap       Dynamic memory hint.
3134
 * @param [in]  devId      Device identifier.
3135
 *
3136
 * @return  0 on success.
3137
 * @return  BAD_FUNC_ARG when a required pointer is NULL.
3138
 */
3139
int wc_InitKmac128(wc_Kmac* kmac, const byte* key, word32 keyLen,
3140
    const byte* custom, word32 customLen, void* heap, int devId)
3141
{
3142
    return KmacInit(kmac, WC_SHA3_128_COUNT, key, keyLen, custom, customLen,
3143
        heap, devId);
3144
}
3145
3146
/* Absorb message data into a KMAC128 operation.
3147
 *
3148
 * @param [in,out] kmac   wc_Kmac object holding state.
3149
 * @param [in]     in     Message bytes, or NULL when inLen is 0.
3150
 * @param [in]     inLen  Length of in in bytes.
3151
 *
3152
 * @return  0 on success.
3153
 * @return  BAD_FUNC_ARG on a NULL message with a non-zero length.
3154
 */
3155
int wc_Kmac128_Update(wc_Kmac* kmac, const byte* in, word32 inLen)
3156
{
3157
    return KmacUpdate(kmac, in, inLen);
3158
}
3159
3160
/* Finalize a KMAC128 operation, writing outLen bytes to out.
3161
 *
3162
 * The output length is bound into the result (NIST SP 800-185 KMAC).
3163
 *
3164
 * @param [in,out] kmac    wc_Kmac object holding state.
3165
 * @param [out]    out     Buffer to hold the output.
3166
 * @param [in]     outLen  Number of output bytes to produce.
3167
 *
3168
 * @return  0 on success.
3169
 * @return  BAD_FUNC_ARG when a parameter is NULL.
3170
 */
3171
int wc_Kmac128_Final(wc_Kmac* kmac, byte* out, word32 outLen)
3172
{
3173
    return KmacFinal(kmac, out, outLen, 0);
3174
}
3175
3176
/* Finalize a KMAC128 operation as an XOF - KMACXOF128.
3177
 *
3178
 * The output length is not bound into the result, so any amount of output may
3179
 * be requested.
3180
 *
3181
 * @param [in,out] kmac    wc_Kmac object holding state.
3182
 * @param [out]    out     Buffer to hold the output.
3183
 * @param [in]     outLen  Number of output bytes to produce.
3184
 *
3185
 * @return  0 on success.
3186
 * @return  BAD_FUNC_ARG when a parameter is NULL.
3187
 */
3188
int wc_Kmac128_FinalXof(wc_Kmac* kmac, byte* out, word32 outLen)
3189
{
3190
    return KmacFinal(kmac, out, outLen, 1);
3191
}
3192
3193
/* Copy the state of a KMAC128 operation, allowing it to be finalized more
3194
 * than once (for example over a common message prefix).
3195
 *
3196
 * @param [in]  src  wc_Kmac object to copy from.
3197
 * @param [out] dst  wc_Kmac object to copy into.
3198
 *
3199
 * @return  0 on success.
3200
 * @return  BAD_FUNC_ARG when src or dst is NULL.
3201
 */
3202
int wc_Kmac128_Copy(wc_Kmac* src, wc_Kmac* dst)
3203
{
3204
    return KmacCopy(src, dst);
3205
}
3206
3207
/* Dispose of any dynamically allocated data from a KMAC128 operation.
3208
 *
3209
 * The sponge state is key-derived, so it is zeroized on free, as with the
3210
 * other keyed MACs, HMAC and CMAC.
3211
 *
3212
 * @param [in,out] kmac  wc_Kmac object to free. May be NULL.
3213
 */
3214
void wc_Kmac128_Free(wc_Kmac* kmac)
3215
{
3216
    if (kmac != NULL) {
3217
        wc_Sha3Free(&kmac->shake);
3218
        ForceZero(kmac, sizeof(*kmac));
3219
    }
3220
}
3221
3222
/* One-shot KMAC128 over a single message.
3223
 *
3224
 * @param [in]  key        Key bytes.
3225
 * @param [in]  keyLen     Length of the key in bytes.
3226
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3227
 * @param [in]  customLen  Length of the customization string in bytes.
3228
 * @param [in]  in         Message bytes, or NULL when inLen is 0.
3229
 * @param [in]  inLen      Length of the message in bytes.
3230
 * @param [out] out        Buffer to hold the output.
3231
 * @param [in]  outLen     Number of output bytes to produce.
3232
 *
3233
 * @return  0 on success.
3234
 * @return  Negative error code on failure.
3235
 */
3236
int wc_Kmac128Hash(const byte* key, word32 keyLen, const byte* custom,
3237
    word32 customLen, const byte* in, word32 inLen, byte* out, word32 outLen)
3238
{
3239
    int ret = 0;
3240
    /* Heap-allocate the state on small-stack builds (it is ~400 bytes). */
3241
    WC_DECLARE_VAR(kmac, wc_Kmac, 1, NULL);
3242
3243
    WC_ALLOC_VAR_EX(kmac, wc_Kmac, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
3244
        ret = MEMORY_E);
3245
3246
    if (ret == 0) {
3247
        ret = wc_InitKmac128(kmac, key, keyLen, custom, customLen, NULL,
3248
            INVALID_DEVID);
3249
    }
3250
    if (ret == 0) {
3251
        ret = wc_Kmac128_Update(kmac, in, inLen);
3252
    }
3253
    if (ret == 0) {
3254
        ret = wc_Kmac128_Final(kmac, out, outLen);
3255
    }
3256
    /* wc_Kmac128_Free tolerates a NULL pointer (allocation failure). */
3257
    wc_Kmac128_Free(kmac);
3258
    WC_FREE_VAR_EX(kmac, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3259
3260
    return ret;
3261
}
3262
3263
/* One-shot KMACXOF128 over a single message.
3264
 *
3265
 * As wc_Kmac128Hash(), but the output length is not bound into the result
3266
 * (KMACXOF128), so any amount of output may be requested.
3267
 *
3268
 * @param [in]  key        Key bytes.
3269
 * @param [in]  keyLen     Length of the key in bytes.
3270
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3271
 * @param [in]  customLen  Length of the customization string in bytes.
3272
 * @param [in]  in         Message bytes, or NULL when inLen is 0.
3273
 * @param [in]  inLen      Length of the message in bytes.
3274
 * @param [out] out        Buffer to hold the output.
3275
 * @param [in]  outLen     Number of output bytes to produce.
3276
 *
3277
 * @return  0 on success.
3278
 * @return  Negative error code on failure.
3279
 */
3280
int wc_Kmac128HashXof(const byte* key, word32 keyLen, const byte* custom,
3281
    word32 customLen, const byte* in, word32 inLen, byte* out, word32 outLen)
3282
{
3283
    int ret = 0;
3284
    /* Heap-allocate the state on small-stack builds (it is ~400 bytes). */
3285
    WC_DECLARE_VAR(kmac, wc_Kmac, 1, NULL);
3286
3287
    WC_ALLOC_VAR_EX(kmac, wc_Kmac, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
3288
        ret = MEMORY_E);
3289
3290
    if (ret == 0) {
3291
        ret = wc_InitKmac128(kmac, key, keyLen, custom, customLen, NULL,
3292
            INVALID_DEVID);
3293
    }
3294
    if (ret == 0) {
3295
        ret = wc_Kmac128_Update(kmac, in, inLen);
3296
    }
3297
    if (ret == 0) {
3298
        ret = wc_Kmac128_FinalXof(kmac, out, outLen);
3299
    }
3300
    /* wc_Kmac128_Free tolerates a NULL pointer (allocation failure). */
3301
    wc_Kmac128_Free(kmac);
3302
    WC_FREE_VAR_EX(kmac, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3303
3304
    return ret;
3305
}
3306
#endif /* WOLFSSL_KMAC128 */
3307
3308
#ifdef WOLFSSL_KMAC256
3309
/* Initialize a KMAC256 operation with a key and optional customization string.
3310
 *
3311
 * @param [out] kmac       wc_Kmac object to initialize.
3312
 * @param [in]  key        Key bytes.
3313
 * @param [in]  keyLen     Length of the key in bytes.
3314
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3315
 * @param [in]  customLen  Length of the customization string in bytes.
3316
 * @param [in]  heap       Dynamic memory hint.
3317
 * @param [in]  devId      Device identifier.
3318
 *
3319
 * @return  0 on success.
3320
 * @return  BAD_FUNC_ARG when a required pointer is NULL.
3321
 */
3322
int wc_InitKmac256(wc_Kmac* kmac, const byte* key, word32 keyLen,
3323
    const byte* custom, word32 customLen, void* heap, int devId)
3324
{
3325
    return KmacInit(kmac, WC_SHA3_256_COUNT, key, keyLen, custom, customLen,
3326
        heap, devId);
3327
}
3328
3329
/* Absorb message data into a KMAC256 operation.
3330
 *
3331
 * @param [in,out] kmac   wc_Kmac object holding state.
3332
 * @param [in]     in     Message bytes, or NULL when inLen is 0.
3333
 * @param [in]     inLen  Length of in in bytes.
3334
 *
3335
 * @return  0 on success.
3336
 * @return  BAD_FUNC_ARG on a NULL message with a non-zero length.
3337
 */
3338
int wc_Kmac256_Update(wc_Kmac* kmac, const byte* in, word32 inLen)
3339
{
3340
    return KmacUpdate(kmac, in, inLen);
3341
}
3342
3343
/* Finalize a KMAC256 operation, writing outLen bytes to out.
3344
 *
3345
 * The output length is bound into the result (NIST SP 800-185 KMAC).
3346
 *
3347
 * @param [in,out] kmac    wc_Kmac object holding state.
3348
 * @param [out]    out     Buffer to hold the output.
3349
 * @param [in]     outLen  Number of output bytes to produce.
3350
 *
3351
 * @return  0 on success.
3352
 * @return  BAD_FUNC_ARG when a parameter is NULL.
3353
 */
3354
int wc_Kmac256_Final(wc_Kmac* kmac, byte* out, word32 outLen)
3355
{
3356
    return KmacFinal(kmac, out, outLen, 0);
3357
}
3358
3359
/* Finalize a KMAC256 operation as an XOF - KMACXOF256.
3360
 *
3361
 * The output length is not bound into the result, so any amount of output may
3362
 * be requested.
3363
 *
3364
 * @param [in,out] kmac    wc_Kmac object holding state.
3365
 * @param [out]    out     Buffer to hold the output.
3366
 * @param [in]     outLen  Number of output bytes to produce.
3367
 *
3368
 * @return  0 on success.
3369
 * @return  BAD_FUNC_ARG when a parameter is NULL.
3370
 */
3371
int wc_Kmac256_FinalXof(wc_Kmac* kmac, byte* out, word32 outLen)
3372
{
3373
    return KmacFinal(kmac, out, outLen, 1);
3374
}
3375
3376
/* Copy the state of a KMAC256 operation, allowing it to be finalized more
3377
 * than once (for example over a common message prefix).
3378
 *
3379
 * @param [in]  src  wc_Kmac object to copy from.
3380
 * @param [out] dst  wc_Kmac object to copy into.
3381
 *
3382
 * @return  0 on success.
3383
 * @return  BAD_FUNC_ARG when src or dst is NULL.
3384
 */
3385
int wc_Kmac256_Copy(wc_Kmac* src, wc_Kmac* dst)
3386
{
3387
    return KmacCopy(src, dst);
3388
}
3389
3390
/* Dispose of any dynamically allocated data from a KMAC256 operation.
3391
 *
3392
 * The sponge state is key-derived, so it is zeroized on free, as with the
3393
 * other keyed MACs, HMAC and CMAC.
3394
 *
3395
 * @param [in,out] kmac  wc_Kmac object to free. May be NULL.
3396
 */
3397
void wc_Kmac256_Free(wc_Kmac* kmac)
3398
{
3399
    if (kmac != NULL) {
3400
        wc_Sha3Free(&kmac->shake);
3401
        ForceZero(kmac, sizeof(*kmac));
3402
    }
3403
}
3404
3405
/* One-shot KMAC256 over a single message.
3406
 *
3407
 * @param [in]  key        Key bytes.
3408
 * @param [in]  keyLen     Length of the key in bytes.
3409
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3410
 * @param [in]  customLen  Length of the customization string in bytes.
3411
 * @param [in]  in         Message bytes, or NULL when inLen is 0.
3412
 * @param [in]  inLen      Length of the message in bytes.
3413
 * @param [out] out        Buffer to hold the output.
3414
 * @param [in]  outLen     Number of output bytes to produce.
3415
 *
3416
 * @return  0 on success.
3417
 * @return  Negative error code on failure.
3418
 */
3419
int wc_Kmac256Hash(const byte* key, word32 keyLen, const byte* custom,
3420
    word32 customLen, const byte* in, word32 inLen, byte* out, word32 outLen)
3421
{
3422
    int ret = 0;
3423
    /* Heap-allocate the state on small-stack builds (it is ~400 bytes). */
3424
    WC_DECLARE_VAR(kmac, wc_Kmac, 1, NULL);
3425
3426
    WC_ALLOC_VAR_EX(kmac, wc_Kmac, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
3427
        ret = MEMORY_E);
3428
3429
    if (ret == 0) {
3430
        ret = wc_InitKmac256(kmac, key, keyLen, custom, customLen, NULL,
3431
            INVALID_DEVID);
3432
    }
3433
    if (ret == 0) {
3434
        ret = wc_Kmac256_Update(kmac, in, inLen);
3435
    }
3436
    if (ret == 0) {
3437
        ret = wc_Kmac256_Final(kmac, out, outLen);
3438
    }
3439
    /* wc_Kmac256_Free tolerates a NULL pointer (allocation failure). */
3440
    wc_Kmac256_Free(kmac);
3441
    WC_FREE_VAR_EX(kmac, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3442
3443
    return ret;
3444
}
3445
3446
/* One-shot KMACXOF256 over a single message.
3447
 *
3448
 * As wc_Kmac256Hash(), but the output length is not bound into the result
3449
 * (KMACXOF256), so any amount of output may be requested.
3450
 *
3451
 * @param [in]  key        Key bytes.
3452
 * @param [in]  keyLen     Length of the key in bytes.
3453
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3454
 * @param [in]  customLen  Length of the customization string in bytes.
3455
 * @param [in]  in         Message bytes, or NULL when inLen is 0.
3456
 * @param [in]  inLen      Length of the message in bytes.
3457
 * @param [out] out        Buffer to hold the output.
3458
 * @param [in]  outLen     Number of output bytes to produce.
3459
 *
3460
 * @return  0 on success.
3461
 * @return  Negative error code on failure.
3462
 */
3463
int wc_Kmac256HashXof(const byte* key, word32 keyLen, const byte* custom,
3464
    word32 customLen, const byte* in, word32 inLen, byte* out, word32 outLen)
3465
{
3466
    int ret = 0;
3467
    /* Heap-allocate the state on small-stack builds (it is ~400 bytes). */
3468
    WC_DECLARE_VAR(kmac, wc_Kmac, 1, NULL);
3469
3470
    WC_ALLOC_VAR_EX(kmac, wc_Kmac, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
3471
        ret = MEMORY_E);
3472
3473
    if (ret == 0) {
3474
        ret = wc_InitKmac256(kmac, key, keyLen, custom, customLen, NULL,
3475
            INVALID_DEVID);
3476
    }
3477
    if (ret == 0) {
3478
        ret = wc_Kmac256_Update(kmac, in, inLen);
3479
    }
3480
    if (ret == 0) {
3481
        ret = wc_Kmac256_FinalXof(kmac, out, outLen);
3482
    }
3483
    /* wc_Kmac256_Free tolerates a NULL pointer (allocation failure). */
3484
    wc_Kmac256_Free(kmac);
3485
    WC_FREE_VAR_EX(kmac, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3486
3487
    return ret;
3488
}
3489
#endif /* WOLFSSL_KMAC256 */
3490
3491
#ifdef WOLFSSL_CSHAKE128
3492
/* Initialize a cSHAKE128 operation with a function-name and customization
3493
 * string (NIST SP 800-185). Enabled together with KMAC (WOLFSSL_KMAC).
3494
 *
3495
 * @param [out] cshake     wc_Cshake object to initialize.
3496
 * @param [in]  name       Function-name string, or NULL when nameLen is 0.
3497
 *                         Reserved for NIST-defined functions; use an empty
3498
 *                         string for application customization via custom.
3499
 * @param [in]  nameLen    Length of name in bytes.
3500
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3501
 * @param [in]  customLen  Length of the customization string in bytes.
3502
 * @param [in]  heap       Dynamic memory hint.
3503
 * @param [in]  devId      Device identifier.
3504
 *
3505
 * @return  0 on success.
3506
 * @return  BAD_FUNC_ARG when a required pointer is NULL.
3507
 */
3508
int wc_InitCshake128(wc_Cshake* cshake, const byte* name, word32 nameLen,
3509
    const byte* custom, word32 customLen, void* heap, int devId)
3510
{
3511
    return CshakeInit(cshake, WC_SHA3_128_COUNT, name, nameLen, custom,
3512
        customLen, heap, devId);
3513
}
3514
3515
/* Absorb message data into a cSHAKE128 operation.
3516
 *
3517
 * @param [in,out] cshake  wc_Cshake object holding state.
3518
 * @param [in]     in      Message bytes, or NULL when inLen is 0.
3519
 * @param [in]     inLen   Length of in in bytes.
3520
 *
3521
 * @return  0 on success.
3522
 * @return  BAD_FUNC_ARG on a NULL message with a non-zero length.
3523
 */
3524
int wc_Cshake128_Update(wc_Cshake* cshake, const byte* in, word32 inLen)
3525
{
3526
    return CshakeUpdate(cshake, in, inLen);
3527
}
3528
3529
/* Finalize a cSHAKE128 operation, writing outLen bytes to out.
3530
 *
3531
 * @param [in,out] cshake  wc_Cshake object holding state.
3532
 * @param [out]    out     Buffer to hold the output.
3533
 * @param [in]     outLen  Number of output bytes to produce.
3534
 *
3535
 * @return  0 on success.
3536
 * @return  BAD_FUNC_ARG when a parameter is NULL.
3537
 */
3538
int wc_Cshake128_Final(wc_Cshake* cshake, byte* out, word32 outLen)
3539
{
3540
    return CshakeFinal(cshake, out, outLen);
3541
}
3542
3543
/* Copy the state of a cSHAKE128 operation, allowing it to be finalized more
3544
 * than once (for example over a common message prefix). dst must already be
3545
 * an initialized wc_Cshake.
3546
 *
3547
 * @param [in]  src  wc_Cshake object to copy from.
3548
 * @param [out] dst  wc_Cshake object to copy into.
3549
 *
3550
 * @return  0 on success.
3551
 * @return  BAD_FUNC_ARG when src or dst is NULL.
3552
 */
3553
int wc_Cshake128_Copy(wc_Cshake* src, wc_Cshake* dst)
3554
{
3555
    return CshakeCopy(src, dst);
3556
}
3557
3558
/* Dispose of any dynamically allocated data from a cSHAKE128 operation.
3559
 *
3560
 * @param [in,out] cshake  wc_Cshake object to free. May be NULL.
3561
 */
3562
void wc_Cshake128_Free(wc_Cshake* cshake)
3563
{
3564
    if (cshake != NULL) {
3565
        wc_Sha3Free(&cshake->shake);
3566
    }
3567
}
3568
3569
/* One-shot cSHAKE128 over a single message.
3570
 *
3571
 * @param [in]  name       Function-name string, or NULL when nameLen is 0.
3572
 * @param [in]  nameLen    Length of name in bytes.
3573
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3574
 * @param [in]  customLen  Length of the customization string in bytes.
3575
 * @param [in]  in         Message bytes, or NULL when inLen is 0.
3576
 * @param [in]  inLen      Length of the message in bytes.
3577
 * @param [out] out        Buffer to hold the output.
3578
 * @param [in]  outLen     Number of output bytes to produce.
3579
 *
3580
 * @return  0 on success.
3581
 * @return  Negative error code on failure.
3582
 */
3583
int wc_Cshake128(const byte* name, word32 nameLen, const byte* custom,
3584
    word32 customLen, const byte* in, word32 inLen, byte* out, word32 outLen)
3585
{
3586
    int ret = 0;
3587
    /* Heap-allocate the state on small-stack builds (it is ~400 bytes). */
3588
    WC_DECLARE_VAR(cshake, wc_Cshake, 1, NULL);
3589
3590
    WC_ALLOC_VAR_EX(cshake, wc_Cshake, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
3591
        ret = MEMORY_E);
3592
3593
    if (ret == 0) {
3594
        ret = wc_InitCshake128(cshake, name, nameLen, custom, customLen, NULL,
3595
            INVALID_DEVID);
3596
    }
3597
    if (ret == 0) {
3598
        ret = wc_Cshake128_Update(cshake, in, inLen);
3599
    }
3600
    if (ret == 0) {
3601
        ret = wc_Cshake128_Final(cshake, out, outLen);
3602
    }
3603
    /* wc_Cshake128_Free tolerates a NULL pointer (allocation failure). */
3604
    wc_Cshake128_Free(cshake);
3605
    WC_FREE_VAR_EX(cshake, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3606
3607
    return ret;
3608
}
3609
#endif /* WOLFSSL_CSHAKE128 */
3610
3611
#ifdef WOLFSSL_CSHAKE256
3612
/* Initialize a cSHAKE256 operation with a function-name and customization
3613
 * string. See wc_InitCshake128() for parameter details.
3614
 *
3615
 * @param [out] cshake     wc_Cshake object to initialize.
3616
 * @param [in]  name       Function-name string, or NULL when nameLen is 0.
3617
 * @param [in]  nameLen    Length of name in bytes.
3618
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3619
 * @param [in]  customLen  Length of the customization string in bytes.
3620
 * @param [in]  heap       Dynamic memory hint.
3621
 * @param [in]  devId      Device identifier.
3622
 *
3623
 * @return  0 on success.
3624
 * @return  BAD_FUNC_ARG when a required pointer is NULL.
3625
 */
3626
int wc_InitCshake256(wc_Cshake* cshake, const byte* name, word32 nameLen,
3627
    const byte* custom, word32 customLen, void* heap, int devId)
3628
{
3629
    return CshakeInit(cshake, WC_SHA3_256_COUNT, name, nameLen, custom,
3630
        customLen, heap, devId);
3631
}
3632
3633
/* Absorb message data into a cSHAKE256 operation.
3634
 *
3635
 * @param [in,out] cshake  wc_Cshake object holding state.
3636
 * @param [in]     in      Message bytes, or NULL when inLen is 0.
3637
 * @param [in]     inLen   Length of in in bytes.
3638
 *
3639
 * @return  0 on success.
3640
 * @return  BAD_FUNC_ARG on a NULL message with a non-zero length.
3641
 */
3642
int wc_Cshake256_Update(wc_Cshake* cshake, const byte* in, word32 inLen)
3643
{
3644
    return CshakeUpdate(cshake, in, inLen);
3645
}
3646
3647
/* Finalize a cSHAKE256 operation, writing outLen bytes to out.
3648
 *
3649
 * @param [in,out] cshake  wc_Cshake object holding state.
3650
 * @param [out]    out     Buffer to hold the output.
3651
 * @param [in]     outLen  Number of output bytes to produce.
3652
 *
3653
 * @return  0 on success.
3654
 * @return  BAD_FUNC_ARG when a parameter is NULL.
3655
 */
3656
int wc_Cshake256_Final(wc_Cshake* cshake, byte* out, word32 outLen)
3657
{
3658
    return CshakeFinal(cshake, out, outLen);
3659
}
3660
3661
/* Copy the state of a cSHAKE256 operation, allowing it to be finalized more
3662
 * than once (for example over a common message prefix). dst must already be
3663
 * an initialized wc_Cshake.
3664
 *
3665
 * @param [in]  src  wc_Cshake object to copy from.
3666
 * @param [out] dst  wc_Cshake object to copy into.
3667
 *
3668
 * @return  0 on success.
3669
 * @return  BAD_FUNC_ARG when src or dst is NULL.
3670
 */
3671
int wc_Cshake256_Copy(wc_Cshake* src, wc_Cshake* dst)
3672
{
3673
    return CshakeCopy(src, dst);
3674
}
3675
3676
/* Dispose of any dynamically allocated data from a cSHAKE256 operation.
3677
 *
3678
 * @param [in,out] cshake  wc_Cshake object to free. May be NULL.
3679
 */
3680
void wc_Cshake256_Free(wc_Cshake* cshake)
3681
{
3682
    if (cshake != NULL) {
3683
        wc_Sha3Free(&cshake->shake);
3684
    }
3685
}
3686
3687
/* One-shot cSHAKE256 over a single message. See wc_Cshake128() for details.
3688
 *
3689
 * @param [in]  name       Function-name string, or NULL when nameLen is 0.
3690
 * @param [in]  nameLen    Length of name in bytes.
3691
 * @param [in]  custom     Customization string, or NULL when customLen is 0.
3692
 * @param [in]  customLen  Length of the customization string in bytes.
3693
 * @param [in]  in         Message bytes, or NULL when inLen is 0.
3694
 * @param [in]  inLen      Length of the message in bytes.
3695
 * @param [out] out        Buffer to hold the output.
3696
 * @param [in]  outLen     Number of output bytes to produce.
3697
 *
3698
 * @return  0 on success.
3699
 * @return  Negative error code on failure.
3700
 */
3701
int wc_Cshake256(const byte* name, word32 nameLen, const byte* custom,
3702
    word32 customLen, const byte* in, word32 inLen, byte* out, word32 outLen)
3703
{
3704
    int ret = 0;
3705
    /* Heap-allocate the state on small-stack builds (it is ~400 bytes). */
3706
    WC_DECLARE_VAR(cshake, wc_Cshake, 1, NULL);
3707
3708
    WC_ALLOC_VAR_EX(cshake, wc_Cshake, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
3709
        ret = MEMORY_E);
3710
3711
    if (ret == 0) {
3712
        ret = wc_InitCshake256(cshake, name, nameLen, custom, customLen, NULL,
3713
            INVALID_DEVID);
3714
    }
3715
    if (ret == 0) {
3716
        ret = wc_Cshake256_Update(cshake, in, inLen);
3717
    }
3718
    if (ret == 0) {
3719
        ret = wc_Cshake256_Final(cshake, out, outLen);
3720
    }
3721
    /* wc_Cshake256_Free tolerates a NULL pointer (allocation failure). */
3722
    wc_Cshake256_Free(cshake);
3723
    WC_FREE_VAR_EX(cshake, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3724
3725
    return ret;
3726
}
3727
#endif /* WOLFSSL_CSHAKE256 */
3728
3729
#endif /* (WOLFSSL_KMAC || WOLFSSL_CSHAKE) && WC_SHA3_SW_KECCAK */
3730
3731
#endif /* WOLFSSL_SHA3 */