sha3.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* sha3.h
  2. *
  3. * Copyright (C) 2006-2023 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. #ifndef WOLF_CRYPT_SHA3_H
  22. #define WOLF_CRYPT_SHA3_H
  23. #include <wolfssl/wolfcrypt/types.h>
  24. #ifdef WOLFSSL_SHA3
  25. #ifdef HAVE_FIPS
  26. /* for fips @wc_fips */
  27. #include <wolfssl/wolfcrypt/fips.h>
  28. #endif
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #ifdef WOLFSSL_ASYNC_CRYPT
  33. #include <wolfssl/wolfcrypt/async.h>
  34. #endif
  35. /* in bytes */
  36. enum {
  37. /* SHAKE-128 */
  38. WC_SHA3_128_COUNT = 21,
  39. WC_SHA3_224 = WC_HASH_TYPE_SHA3_224,
  40. WC_SHA3_224_DIGEST_SIZE = 28,
  41. WC_SHA3_224_COUNT = 18,
  42. WC_SHA3_256 = WC_HASH_TYPE_SHA3_256,
  43. WC_SHA3_256_DIGEST_SIZE = 32,
  44. WC_SHA3_256_COUNT = 17,
  45. WC_SHA3_384 = WC_HASH_TYPE_SHA3_384,
  46. WC_SHA3_384_DIGEST_SIZE = 48,
  47. WC_SHA3_384_COUNT = 13,
  48. WC_SHA3_512 = WC_HASH_TYPE_SHA3_512,
  49. WC_SHA3_512_DIGEST_SIZE = 64,
  50. WC_SHA3_512_COUNT = 9,
  51. #ifdef WOLFSSL_SHAKE128
  52. WC_SHAKE128 = WC_HASH_TYPE_SHAKE128,
  53. #endif
  54. #ifdef WOLFSSL_SHAKE256
  55. WC_SHAKE256 = WC_HASH_TYPE_SHAKE256,
  56. #endif
  57. #if !defined(HAVE_SELFTEST) || \
  58. defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION >= 2)
  59. /* These values are used for HMAC, not SHA-3 directly.
  60. * They come from from FIPS PUB 202. */
  61. WC_SHA3_128_BLOCK_SIZE = 168,
  62. WC_SHA3_224_BLOCK_SIZE = 144,
  63. WC_SHA3_256_BLOCK_SIZE = 136,
  64. WC_SHA3_384_BLOCK_SIZE = 104,
  65. WC_SHA3_512_BLOCK_SIZE = 72,
  66. #endif
  67. WOLF_ENUM_DUMMY_LAST_ELEMENT(WC_SHA3)
  68. };
  69. #ifndef NO_OLD_WC_NAMES
  70. #define SHA3_224 WC_SHA3_224
  71. #define SHA3_224_DIGEST_SIZE WC_SHA3_224_DIGEST_SIZE
  72. #define SHA3_256 WC_SHA3_256
  73. #define SHA3_256_DIGEST_SIZE WC_SHA3_256_DIGEST_SIZE
  74. #define SHA3_384 WC_SHA3_384
  75. #define SHA3_384_DIGEST_SIZE WC_SHA3_384_DIGEST_SIZE
  76. #define SHA3_512 WC_SHA3_512
  77. #define SHA3_512_DIGEST_SIZE WC_SHA3_512_DIGEST_SIZE
  78. #define Sha3 wc_Sha3
  79. #ifdef WOLFSSL_SHAKE128
  80. #define SHAKE128 WC_SHAKE128
  81. #endif
  82. #ifdef WOLFSSL_SHAKE256
  83. #define SHAKE256 WC_SHAKE256
  84. #endif
  85. #endif
  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. /* Sha3 digest */
  92. struct wc_Sha3 {
  93. /* State data that is processed for each block. */
  94. word64 s[25];
  95. /* Unprocessed message data. */
  96. byte t[200];
  97. /* Index into unprocessed data to place next message byte. */
  98. byte i;
  99. void* heap;
  100. #ifdef WOLFSSL_ASYNC_CRYPT
  101. WC_ASYNC_DEV asyncDev;
  102. #endif /* WOLFSSL_ASYNC_CRYPT */
  103. #ifdef WOLFSSL_HASH_FLAGS
  104. word32 flags; /* enum wc_HashFlags in hash.h */
  105. #endif
  106. };
  107. #ifndef WC_SHA3_TYPE_DEFINED
  108. typedef struct wc_Sha3 wc_Sha3;
  109. #define WC_SHA3_TYPE_DEFINED
  110. #endif
  111. #endif
  112. #if defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256)
  113. typedef wc_Sha3 wc_Shake;
  114. #endif
  115. WOLFSSL_API int wc_InitSha3_224(wc_Sha3* sha3, void* heap, int devId);
  116. WOLFSSL_API int wc_Sha3_224_Update(wc_Sha3* sha3, const byte* data, word32 len);
  117. WOLFSSL_API int wc_Sha3_224_Final(wc_Sha3* sha3, byte* hash);
  118. WOLFSSL_API void wc_Sha3_224_Free(wc_Sha3* sha3);
  119. WOLFSSL_API int wc_Sha3_224_GetHash(wc_Sha3* sha3, byte* hash);
  120. WOLFSSL_API int wc_Sha3_224_Copy(wc_Sha3* src, wc_Sha3* dst);
  121. WOLFSSL_API int wc_InitSha3_256(wc_Sha3* sha3, void* heap, int devId);
  122. WOLFSSL_API int wc_Sha3_256_Update(wc_Sha3* sha3, const byte* data, word32 len);
  123. WOLFSSL_API int wc_Sha3_256_Final(wc_Sha3* sha3, byte* hash);
  124. WOLFSSL_API void wc_Sha3_256_Free(wc_Sha3* sha3);
  125. WOLFSSL_API int wc_Sha3_256_GetHash(wc_Sha3* sha3, byte* hash);
  126. WOLFSSL_API int wc_Sha3_256_Copy(wc_Sha3* src, wc_Sha3* dst);
  127. WOLFSSL_API int wc_InitSha3_384(wc_Sha3* sha3, void* heap, int devId);
  128. WOLFSSL_API int wc_Sha3_384_Update(wc_Sha3* sha3, const byte* data, word32 len);
  129. WOLFSSL_API int wc_Sha3_384_Final(wc_Sha3* sha3, byte* hash);
  130. WOLFSSL_API void wc_Sha3_384_Free(wc_Sha3* sha3);
  131. WOLFSSL_API int wc_Sha3_384_GetHash(wc_Sha3* sha3, byte* hash);
  132. WOLFSSL_API int wc_Sha3_384_Copy(wc_Sha3* src, wc_Sha3* dst);
  133. WOLFSSL_API int wc_InitSha3_512(wc_Sha3* sha3, void* heap, int devId);
  134. WOLFSSL_API int wc_Sha3_512_Update(wc_Sha3* sha3, const byte* data, word32 len);
  135. WOLFSSL_API int wc_Sha3_512_Final(wc_Sha3* sha3, byte* hash);
  136. WOLFSSL_API void wc_Sha3_512_Free(wc_Sha3* sha3);
  137. WOLFSSL_API int wc_Sha3_512_GetHash(wc_Sha3* sha3, byte* hash);
  138. WOLFSSL_API int wc_Sha3_512_Copy(wc_Sha3* src, wc_Sha3* dst);
  139. #ifdef WOLFSSL_SHAKE128
  140. WOLFSSL_API int wc_InitShake128(wc_Shake* shake, void* heap, int devId);
  141. WOLFSSL_API int wc_Shake128_Update(wc_Shake* shake, const byte* data, word32 len);
  142. WOLFSSL_API int wc_Shake128_Final(wc_Shake* shake, byte* hash, word32 hashLen);
  143. WOLFSSL_API int wc_Shake128_Absorb(wc_Shake* shake, const byte* data,
  144. word32 len);
  145. WOLFSSL_API int wc_Shake128_SqueezeBlocks(wc_Shake* shake, byte* out,
  146. word32 blockCnt);
  147. WOLFSSL_API void wc_Shake128_Free(wc_Shake* shake);
  148. WOLFSSL_API int wc_Shake128_Copy(wc_Shake* src, wc_Sha3* dst);
  149. #endif
  150. #ifdef WOLFSSL_SHAKE256
  151. WOLFSSL_API int wc_InitShake256(wc_Shake* shake, void* heap, int devId);
  152. WOLFSSL_API int wc_Shake256_Update(wc_Shake* shake, const byte* data, word32 len);
  153. WOLFSSL_API int wc_Shake256_Final(wc_Shake* shake, byte* hash, word32 hashLen);
  154. WOLFSSL_API int wc_Shake256_Absorb(wc_Shake* shake, const byte* data,
  155. word32 len);
  156. WOLFSSL_API int wc_Shake256_SqueezeBlocks(wc_Shake* shake, byte* out,
  157. word32 blockCnt);
  158. WOLFSSL_API void wc_Shake256_Free(wc_Shake* shake);
  159. WOLFSSL_API int wc_Shake256_Copy(wc_Shake* src, wc_Sha3* dst);
  160. #endif
  161. #ifdef WOLFSSL_HASH_FLAGS
  162. WOLFSSL_API int wc_Sha3_SetFlags(wc_Sha3* sha3, word32 flags);
  163. WOLFSSL_API int wc_Sha3_GetFlags(wc_Sha3* sha3, word32* flags);
  164. #endif
  165. #ifdef USE_INTEL_SPEEDUP
  166. WOLFSSL_LOCAL void sha3_block_n_bmi2(word64* s, const byte* data, word32 n,
  167. word64 c);
  168. WOLFSSL_LOCAL void sha3_block_bmi2(word64* s);
  169. WOLFSSL_LOCAL void sha3_block_avx2(word64* s);
  170. WOLFSSL_LOCAL void BlockSha3(word64 *s);
  171. #endif
  172. #if defined(WOLFSSL_ARMASM) && defined(WOLFSSL_ARMASM_CRYPTO_SHA3)
  173. WOLFSSL_LOCAL void BlockSha3(word64 *s);
  174. #endif
  175. #ifdef __cplusplus
  176. } /* extern "C" */
  177. #endif
  178. #endif /* WOLFSSL_SHA3 */
  179. #endif /* WOLF_CRYPT_SHA3_H */