yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* sha3.h |
| 2 | * |
| 3 | * Copyright (C) 2006-2021 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 2 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 | #ifdef WOLFSSL_ASYNC_CRYPT |
| 40 | #include <wolfssl/wolfcrypt/async.h> |
| 41 | #endif |
| 42 | |
| 43 | /* in bytes */ |
| 44 | enum { |
| 45 | WC_SHA3_224 = WC_HASH_TYPE_SHA3_224, |
| 46 | WC_SHA3_224_DIGEST_SIZE = 28, |
| 47 | WC_SHA3_224_COUNT = 18, |
| 48 | |
| 49 | WC_SHA3_256 = WC_HASH_TYPE_SHA3_256, |
| 50 | WC_SHA3_256_DIGEST_SIZE = 32, |
| 51 | WC_SHA3_256_COUNT = 17, |
| 52 | |
| 53 | WC_SHA3_384 = WC_HASH_TYPE_SHA3_384, |
| 54 | WC_SHA3_384_DIGEST_SIZE = 48, |
| 55 | WC_SHA3_384_COUNT = 13, |
| 56 | |
| 57 | WC_SHA3_512 = WC_HASH_TYPE_SHA3_512, |
| 58 | WC_SHA3_512_DIGEST_SIZE = 64, |
| 59 | WC_SHA3_512_COUNT = 9, |
| 60 | |
| 61 | #if !defined(HAVE_SELFTEST) || \ |
| 62 | defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION >= 2) |
| 63 | /* These values are used for HMAC, not SHA-3 directly. |
| 64 | * They come from from FIPS PUB 202. */ |
| 65 | WC_SHA3_224_BLOCK_SIZE = 144, |
| 66 | WC_SHA3_256_BLOCK_SIZE = 136, |
| 67 | WC_SHA3_384_BLOCK_SIZE = 104, |
| 68 | WC_SHA3_512_BLOCK_SIZE = 72, |
| 69 | #endif |
| 70 | }; |
| 71 | |
| 72 | #ifndef NO_OLD_WC_NAMES |
| 73 | #define SHA3_224 WC_SHA3_224 |
| 74 | #define SHA3_224_DIGEST_SIZE WC_SHA3_224_DIGEST_SIZE |
| 75 | #define SHA3_256 WC_SHA3_256 |
| 76 | #define SHA3_256_DIGEST_SIZE WC_SHA3_256_DIGEST_SIZE |
| 77 | #define SHA3_384 WC_SHA3_384 |
| 78 | #define SHA3_384_DIGEST_SIZE WC_SHA3_384_DIGEST_SIZE |
| 79 | #define SHA3_512 WC_SHA3_512 |
| 80 | #define SHA3_512_DIGEST_SIZE WC_SHA3_512_DIGEST_SIZE |
| 81 | #define Sha3 wc_Sha3 |
| 82 | #endif |
| 83 | |
| 84 | |
| 85 | |
| 86 | #ifdef WOLFSSL_XILINX_CRYPT |
| 87 | #include "wolfssl/wolfcrypt/port/xilinx/xil-sha3.h" |
| 88 | #elif defined(WOLFSSL_AFALG_XILINX_SHA3) |
| 89 | #include <wolfssl/wolfcrypt/port/af_alg/afalg_hash.h> |
| 90 | #else |
| 91 | |
| 92 | /* Sha3 digest */ |
| 93 | struct wc_Sha3 { |
| 94 | /* State data that is processed for each block. */ |
| 95 | word64 s[25]; |
| 96 | /* Unprocessed message data. */ |
| 97 | byte t[200]; |
| 98 | /* Index into unprocessed data to place next message byte. */ |
| 99 | byte i; |
| 100 | |
| 101 | void* heap; |
| 102 | |
| 103 | #ifdef WOLFSSL_ASYNC_CRYPT |
| 104 | WC_ASYNC_DEV asyncDev; |
| 105 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
| 106 | #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB) |
| 107 | word32 flags; /* enum wc_HashFlags in hash.h */ |
| 108 | #endif |
| 109 | }; |
| 110 | |
| 111 | #ifndef WC_SHA3_TYPE_DEFINED |
| 112 | typedef struct wc_Sha3 wc_Sha3; |
| 113 | #define WC_SHA3_TYPE_DEFINED |
| 114 | #endif |
| 115 | |
| 116 | #endif |
| 117 | |
| 118 | typedef wc_Sha3 wc_Shake; |
| 119 | |
| 120 | |
| 121 | WOLFSSL_API int wc_InitSha3_224(wc_Sha3*, void*, int); |
| 122 | WOLFSSL_API int wc_Sha3_224_Update(wc_Sha3*, const byte*, word32); |
| 123 | WOLFSSL_API int wc_Sha3_224_Final(wc_Sha3*, byte*); |
| 124 | WOLFSSL_API void wc_Sha3_224_Free(wc_Sha3*); |
| 125 | WOLFSSL_API int wc_Sha3_224_GetHash(wc_Sha3*, byte*); |
| 126 | WOLFSSL_API int wc_Sha3_224_Copy(wc_Sha3* src, wc_Sha3* dst); |
| 127 | |
| 128 | WOLFSSL_API int wc_InitSha3_256(wc_Sha3*, void*, int); |
| 129 | WOLFSSL_API int wc_Sha3_256_Update(wc_Sha3*, const byte*, word32); |
| 130 | WOLFSSL_API int wc_Sha3_256_Final(wc_Sha3*, byte*); |
| 131 | WOLFSSL_API void wc_Sha3_256_Free(wc_Sha3*); |
| 132 | WOLFSSL_API int wc_Sha3_256_GetHash(wc_Sha3*, byte*); |
| 133 | WOLFSSL_API int wc_Sha3_256_Copy(wc_Sha3* src, wc_Sha3* dst); |
| 134 | |
| 135 | WOLFSSL_API int wc_InitSha3_384(wc_Sha3*, void*, int); |
| 136 | WOLFSSL_API int wc_Sha3_384_Update(wc_Sha3*, const byte*, word32); |
| 137 | WOLFSSL_API int wc_Sha3_384_Final(wc_Sha3*, byte*); |
| 138 | WOLFSSL_API void wc_Sha3_384_Free(wc_Sha3*); |
| 139 | WOLFSSL_API int wc_Sha3_384_GetHash(wc_Sha3*, byte*); |
| 140 | WOLFSSL_API int wc_Sha3_384_Copy(wc_Sha3* src, wc_Sha3* dst); |
| 141 | |
| 142 | WOLFSSL_API int wc_InitSha3_512(wc_Sha3*, void*, int); |
| 143 | WOLFSSL_API int wc_Sha3_512_Update(wc_Sha3*, const byte*, word32); |
| 144 | WOLFSSL_API int wc_Sha3_512_Final(wc_Sha3*, byte*); |
| 145 | WOLFSSL_API void wc_Sha3_512_Free(wc_Sha3*); |
| 146 | WOLFSSL_API int wc_Sha3_512_GetHash(wc_Sha3*, byte*); |
| 147 | WOLFSSL_API int wc_Sha3_512_Copy(wc_Sha3* src, wc_Sha3* dst); |
| 148 | |
| 149 | WOLFSSL_API int wc_InitShake256(wc_Shake*, void*, int); |
| 150 | WOLFSSL_API int wc_Shake256_Update(wc_Shake*, const byte*, word32); |
| 151 | WOLFSSL_API int wc_Shake256_Final(wc_Shake*, byte*, word32); |
| 152 | WOLFSSL_API void wc_Shake256_Free(wc_Shake*); |
| 153 | WOLFSSL_API int wc_Shake256_Copy(wc_Shake* src, wc_Sha3* dst); |
| 154 | |
| 155 | #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB) |
| 156 | WOLFSSL_API int wc_Sha3_SetFlags(wc_Sha3* sha3, word32 flags); |
| 157 | WOLFSSL_API int wc_Sha3_GetFlags(wc_Sha3* sha3, word32* flags); |
| 158 | #endif |
| 159 | |
| 160 | #ifdef __cplusplus |
| 161 | } /* extern "C" */ |
| 162 | #endif |
| 163 | |
| 164 | #endif /* WOLFSSL_SHA3 */ |
| 165 | #endif /* WOLF_CRYPT_SHA3_H */ |
| 166 | |