gnunet-android

GNUnet for Android
Log | Files | Refs | README

sha3.h (8138B)


      1 /* sha3.h
      2  *
      3  * Copyright (C) 2006-2025 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 #ifndef WOLF_CRYPT_SHA3_H
     24 #define WOLF_CRYPT_SHA3_H
     25 
     26 #include <wolfssl/wolfcrypt/types.h>
     27 
     28 #ifdef WOLFSSL_SHA3
     29 
     30 #ifdef HAVE_FIPS
     31     /* for fips @wc_fips */
     32     #include <wolfssl/wolfcrypt/fips.h>
     33 #endif
     34 
     35 #ifdef __cplusplus
     36     extern "C" {
     37 #endif
     38 
     39 #if FIPS_VERSION3_GE(6,0,0)
     40     extern const unsigned int wolfCrypt_FIPS_sha3_ro_sanity[2];
     41     WOLFSSL_LOCAL int wolfCrypt_FIPS_SHA3_sanity(void);
     42 #endif
     43 
     44 #ifdef WOLFSSL_ASYNC_CRYPT
     45     #include <wolfssl/wolfcrypt/async.h>
     46 #endif
     47 
     48 #ifdef STM32_HASH
     49     #include <wolfssl/wolfcrypt/port/st/stm32.h>
     50 #endif
     51 
     52 /* in bytes */
     53 enum {
     54     /* SHAKE-128 */
     55     WC_SHA3_128_COUNT        = 21,
     56 
     57     WC_SHA3_224              = WC_HASH_TYPE_SHA3_224,
     58     WC_SHA3_224_DIGEST_SIZE  = 28,
     59     WC_SHA3_224_COUNT        = 18,
     60 
     61     WC_SHA3_256              = WC_HASH_TYPE_SHA3_256,
     62     WC_SHA3_256_DIGEST_SIZE  = 32,
     63     WC_SHA3_256_COUNT        = 17,
     64 
     65     WC_SHA3_384              = WC_HASH_TYPE_SHA3_384,
     66     WC_SHA3_384_DIGEST_SIZE  = 48,
     67     WC_SHA3_384_COUNT        = 13,
     68 
     69     WC_SHA3_512              = WC_HASH_TYPE_SHA3_512,
     70     WC_SHA3_512_DIGEST_SIZE  = 64,
     71     WC_SHA3_512_COUNT        =  9,
     72 
     73     #ifdef WOLFSSL_SHAKE128
     74         WC_SHAKE128          = WC_HASH_TYPE_SHAKE128,
     75     #endif
     76     #ifdef WOLFSSL_SHAKE256
     77         WC_SHAKE256          = WC_HASH_TYPE_SHAKE256,
     78     #endif
     79 
     80 #if !defined(HAVE_SELFTEST) || \
     81     defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION >= 2)
     82     /* These values are used for HMAC, not SHA-3 directly.
     83      * They come from from FIPS PUB 202. */
     84     WC_SHA3_128_BLOCK_SIZE = 168,
     85     WC_SHA3_224_BLOCK_SIZE = 144,
     86     WC_SHA3_256_BLOCK_SIZE = 136,
     87     WC_SHA3_384_BLOCK_SIZE = 104,
     88     WC_SHA3_512_BLOCK_SIZE = 72,
     89 #endif
     90 
     91     WOLF_ENUM_DUMMY_LAST_ELEMENT(WC_SHA3)
     92 };
     93 
     94 #ifndef NO_OLD_WC_NAMES
     95     #define SHA3_224             WC_SHA3_224
     96     #define SHA3_224_DIGEST_SIZE WC_SHA3_224_DIGEST_SIZE
     97     #define SHA3_256             WC_SHA3_256
     98     #define SHA3_256_DIGEST_SIZE WC_SHA3_256_DIGEST_SIZE
     99     #define SHA3_384             WC_SHA3_384
    100     #define SHA3_384_DIGEST_SIZE WC_SHA3_384_DIGEST_SIZE
    101     #define SHA3_512             WC_SHA3_512
    102     #define SHA3_512_DIGEST_SIZE WC_SHA3_512_DIGEST_SIZE
    103     #define Sha3 wc_Sha3
    104     #ifdef WOLFSSL_SHAKE128
    105         #define SHAKE128             WC_SHAKE128
    106     #endif
    107     #ifdef WOLFSSL_SHAKE256
    108         #define SHAKE256             WC_SHAKE256
    109     #endif
    110 #endif
    111 
    112 
    113 
    114 #ifdef WOLFSSL_XILINX_CRYPT
    115     #include "wolfssl/wolfcrypt/port/xilinx/xil-sha3.h"
    116 #elif defined(WOLFSSL_AFALG_XILINX_SHA3)
    117     #include <wolfssl/wolfcrypt/port/af_alg/afalg_hash.h>
    118 #else
    119 
    120 /* Sha3 digest */
    121 struct wc_Sha3 {
    122     /* State data that is processed for each block. */
    123     word64 s[25];
    124     /* Unprocessed message data. */
    125     byte   t[200];
    126     /* Index into unprocessed data to place next message byte. */
    127     byte   i;
    128 
    129     void*  heap;
    130 
    131 #ifdef WOLF_CRYPTO_CB
    132     int    devId;
    133 #endif
    134 
    135 #ifdef WC_C_DYNAMIC_FALLBACK
    136     void (*sha3_block)(word64 *s);
    137     void (*sha3_block_n)(word64 *s, const byte* data, word32 n,
    138         word64 c);
    139 #endif
    140 
    141 #ifdef WOLFSSL_ASYNC_CRYPT
    142     WC_ASYNC_DEV asyncDev;
    143 #endif /* WOLFSSL_ASYNC_CRYPT */
    144 #ifdef WOLFSSL_HASH_FLAGS
    145     word32 flags; /* enum wc_HashFlags in hash.h */
    146 #endif
    147 #if defined(STM32_HASH_SHA3)
    148     STM32_HASH_Context stmCtx;
    149 #endif
    150 };
    151 
    152 #ifndef WC_SHA3_TYPE_DEFINED
    153     typedef struct wc_Sha3 wc_Sha3;
    154     #define WC_SHA3_TYPE_DEFINED
    155 #endif
    156 
    157 #endif
    158 
    159 #if defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256)
    160     #ifndef WC_SHAKE_TYPE_DEFINED
    161         typedef wc_Sha3 wc_Shake;
    162         #define WC_SHAKE_TYPE_DEFINED
    163     #endif
    164 #endif
    165 
    166 WOLFSSL_API int wc_InitSha3_224(wc_Sha3* sha3, void* heap, int devId);
    167 WOLFSSL_API int wc_Sha3_224_Update(wc_Sha3* sha3, const byte* data, word32 len);
    168 WOLFSSL_API int wc_Sha3_224_Final(wc_Sha3* sha3, byte* hash);
    169 WOLFSSL_API void wc_Sha3_224_Free(wc_Sha3* sha3);
    170 WOLFSSL_API int wc_Sha3_224_GetHash(wc_Sha3* sha3, byte* hash);
    171 WOLFSSL_API int wc_Sha3_224_Copy(wc_Sha3* src, wc_Sha3* dst);
    172 
    173 WOLFSSL_API int wc_InitSha3_256(wc_Sha3* sha3, void* heap, int devId);
    174 WOLFSSL_API int wc_Sha3_256_Update(wc_Sha3* sha3, const byte* data, word32 len);
    175 WOLFSSL_API int wc_Sha3_256_Final(wc_Sha3* sha3, byte* hash);
    176 WOLFSSL_API void wc_Sha3_256_Free(wc_Sha3* sha3);
    177 WOLFSSL_API int wc_Sha3_256_GetHash(wc_Sha3* sha3, byte* hash);
    178 WOLFSSL_API int wc_Sha3_256_Copy(wc_Sha3* src, wc_Sha3* dst);
    179 
    180 WOLFSSL_API int wc_InitSha3_384(wc_Sha3* sha3, void* heap, int devId);
    181 WOLFSSL_API int wc_Sha3_384_Update(wc_Sha3* sha3, const byte* data, word32 len);
    182 WOLFSSL_API int wc_Sha3_384_Final(wc_Sha3* sha3, byte* hash);
    183 WOLFSSL_API void wc_Sha3_384_Free(wc_Sha3* sha3);
    184 WOLFSSL_API int wc_Sha3_384_GetHash(wc_Sha3* sha3, byte* hash);
    185 WOLFSSL_API int wc_Sha3_384_Copy(wc_Sha3* src, wc_Sha3* dst);
    186 
    187 WOLFSSL_API int wc_InitSha3_512(wc_Sha3* sha3, void* heap, int devId);
    188 WOLFSSL_API int wc_Sha3_512_Update(wc_Sha3* sha3, const byte* data, word32 len);
    189 WOLFSSL_API int wc_Sha3_512_Final(wc_Sha3* sha3, byte* hash);
    190 WOLFSSL_API void wc_Sha3_512_Free(wc_Sha3* sha3);
    191 WOLFSSL_API int wc_Sha3_512_GetHash(wc_Sha3* sha3, byte* hash);
    192 WOLFSSL_API int wc_Sha3_512_Copy(wc_Sha3* src, wc_Sha3* dst);
    193 
    194 #ifdef WOLFSSL_SHAKE128
    195 WOLFSSL_API int wc_InitShake128(wc_Shake* shake, void* heap, int devId);
    196 WOLFSSL_API int wc_Shake128_Update(wc_Shake* shake, const byte* data, word32 len);
    197 WOLFSSL_API int wc_Shake128_Final(wc_Shake* shake, byte* hash, word32 hashLen);
    198 WOLFSSL_API int wc_Shake128_Absorb(wc_Shake* shake, const byte* data,
    199     word32 len);
    200 WOLFSSL_API int wc_Shake128_SqueezeBlocks(wc_Shake* shake, byte* out,
    201     word32 blockCnt);
    202 WOLFSSL_API void wc_Shake128_Free(wc_Shake* shake);
    203 WOLFSSL_API int wc_Shake128_Copy(wc_Shake* src, wc_Sha3* dst);
    204 #endif
    205 
    206 #ifdef WOLFSSL_SHAKE256
    207 WOLFSSL_API int wc_InitShake256(wc_Shake* shake, void* heap, int devId);
    208 WOLFSSL_API int wc_Shake256_Update(wc_Shake* shake, const byte* data, word32 len);
    209 WOLFSSL_API int wc_Shake256_Final(wc_Shake* shake, byte* hash, word32 hashLen);
    210 WOLFSSL_API int wc_Shake256_Absorb(wc_Shake* shake, const byte* data,
    211     word32 len);
    212 WOLFSSL_API int wc_Shake256_SqueezeBlocks(wc_Shake* shake, byte* out,
    213     word32 blockCnt);
    214 WOLFSSL_API void wc_Shake256_Free(wc_Shake* shake);
    215 WOLFSSL_API int wc_Shake256_Copy(wc_Shake* src, wc_Sha3* dst);
    216 #endif
    217 
    218 #ifdef WOLFSSL_HASH_FLAGS
    219     WOLFSSL_API int wc_Sha3_SetFlags(wc_Sha3* sha3, word32 flags);
    220     WOLFSSL_API int wc_Sha3_GetFlags(wc_Sha3* sha3, word32* flags);
    221 #endif
    222 
    223 WOLFSSL_LOCAL void BlockSha3(word64 *s);
    224 
    225 #ifdef WC_SHA3_NO_ASM
    226     /* asm speedups disabled */
    227     #if defined(USE_INTEL_SPEEDUP) && !defined(WC_MLKEM_NO_ASM)
    228         /* native ML-KEM uses this directly. */
    229         WOLFSSL_LOCAL void sha3_blocksx4_avx2(word64* s);
    230     #endif
    231 #elif defined(USE_INTEL_SPEEDUP)
    232     WOLFSSL_LOCAL void sha3_block_n_bmi2(word64* s, const byte* data, word32 n,
    233         word64 c);
    234     WOLFSSL_LOCAL void sha3_block_bmi2(word64* s);
    235     WOLFSSL_LOCAL void sha3_block_avx2(word64* s);
    236     WOLFSSL_LOCAL void sha3_blocksx4_avx2(word64* s);
    237 #elif defined(__aarch64__) && defined(WOLFSSL_ARMASM)
    238     #ifdef WOLFSSL_ARMASM_CRYPTO_SHA3
    239         WOLFSSL_LOCAL void BlockSha3_crypto(word64 *s);
    240     #endif
    241     WOLFSSL_LOCAL void BlockSha3_base(word64 *s);
    242 #endif
    243 
    244 #ifdef __cplusplus
    245     } /* extern "C" */
    246 #endif
    247 
    248 #endif /* WOLFSSL_SHA3 */
    249 #endif /* WOLF_CRYPT_SHA3_H */
    250