hash.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* hash.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. /*!
  22. \file wolfssl/wolfcrypt/hash.h
  23. */
  24. #ifndef WOLF_CRYPT_HASH_H
  25. #define WOLF_CRYPT_HASH_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifndef NO_MD5
  28. #include <wolfssl/wolfcrypt/md5.h>
  29. #endif
  30. #ifndef NO_SHA
  31. #include <wolfssl/wolfcrypt/sha.h>
  32. #endif
  33. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  34. #include <wolfssl/wolfcrypt/sha256.h>
  35. #endif
  36. #if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  37. #include <wolfssl/wolfcrypt/sha512.h>
  38. #endif
  39. #ifdef HAVE_BLAKE2
  40. #include <wolfssl/wolfcrypt/blake2.h>
  41. #endif
  42. #ifdef WOLFSSL_SHA3
  43. #include <wolfssl/wolfcrypt/sha3.h>
  44. #endif
  45. #ifndef NO_MD4
  46. #include <wolfssl/wolfcrypt/md4.h>
  47. #endif
  48. #ifdef WOLFSSL_MD2
  49. #include <wolfssl/wolfcrypt/md2.h>
  50. #endif
  51. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  52. #include <wolfssl/wolfcrypt/blake2.h>
  53. #endif
  54. #ifdef WOLFSSL_SM3
  55. #include <wolfssl/wolfcrypt/sm3.h>
  56. #endif
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. #if !defined(HAVE_FIPS) && !defined(NO_OLD_WC_NAMES)
  61. #define MAX_DIGEST_SIZE WC_MAX_DIGEST_SIZE
  62. #endif
  63. /* Supported Message Authentication Codes from page 43 */
  64. enum wc_MACAlgorithm {
  65. no_mac,
  66. md5_mac,
  67. sha_mac,
  68. sha224_mac,
  69. sha256_mac, /* needs to match external KDF_MacAlgorithm */
  70. sha384_mac,
  71. sha512_mac,
  72. rmd_mac,
  73. blake2b_mac,
  74. sm3_mac,
  75. };
  76. enum wc_HashFlags {
  77. WC_HASH_FLAG_NONE = 0x00000000,
  78. WC_HASH_FLAG_WILLCOPY = 0x00000001, /* flag to indicate hash will be copied */
  79. WC_HASH_FLAG_ISCOPY = 0x00000002, /* hash is copy */
  80. #ifdef WOLFSSL_SHA3
  81. WC_HASH_SHA3_KECCAK256 =0x00010000, /* Older KECCAK256 */
  82. #endif
  83. WOLF_ENUM_DUMMY_LAST_ELEMENT(WC_HASH)
  84. };
  85. #ifndef NO_HASH_WRAPPER
  86. typedef union {
  87. #ifndef NO_MD5
  88. wc_Md5 md5;
  89. #endif
  90. #ifndef NO_SHA
  91. wc_Sha sha;
  92. #endif
  93. #ifdef WOLFSSL_SHA224
  94. wc_Sha224 sha224;
  95. #endif
  96. #ifndef NO_SHA256
  97. wc_Sha256 sha256;
  98. #endif
  99. #ifdef WOLFSSL_SHA384
  100. wc_Sha384 sha384;
  101. #endif
  102. #ifdef WOLFSSL_SHA512
  103. wc_Sha512 sha512;
  104. #endif
  105. #ifdef WOLFSSL_SHA3
  106. wc_Sha3 sha3;
  107. #endif
  108. #ifdef WOLFSSL_SM3
  109. wc_Sm3 sm3;
  110. #endif
  111. } wc_HashAlg;
  112. #endif /* !NO_HASH_WRAPPER */
  113. /* Find largest possible digest size
  114. Note if this gets up to the size of 80 or over check smallstack build */
  115. #if defined(WOLFSSL_SHA3)
  116. #define WC_MAX_DIGEST_SIZE WC_SHA3_512_DIGEST_SIZE
  117. #define WC_MAX_BLOCK_SIZE WC_SHA3_224_BLOCK_SIZE /* 224 is the largest block size */
  118. #elif defined(WOLFSSL_SHA512)
  119. #define WC_MAX_DIGEST_SIZE WC_SHA512_DIGEST_SIZE
  120. #define WC_MAX_BLOCK_SIZE WC_SHA512_BLOCK_SIZE
  121. #elif defined(HAVE_BLAKE2)
  122. #define WC_MAX_DIGEST_SIZE BLAKE2B_OUTBYTES
  123. #define WC_MAX_BLOCK_SIZE BLAKE2B_BLOCKBYTES
  124. #elif defined(WOLFSSL_SHA384)
  125. #define WC_MAX_DIGEST_SIZE WC_SHA384_DIGEST_SIZE
  126. #define WC_MAX_BLOCK_SIZE WC_SHA384_BLOCK_SIZE
  127. #elif !defined(NO_SHA256)
  128. #define WC_MAX_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  129. #define WC_MAX_BLOCK_SIZE WC_SHA256_BLOCK_SIZE
  130. #elif defined(WOLFSSL_SM3)
  131. #define WC_MAX_DIGEST_SIZE WC_SM3_DIGEST_SIZE
  132. #define WC_MAX_BLOCK_SIZE WC_SM3_BLOCK_SIZE
  133. #elif defined(WOLFSSL_SHA224)
  134. #define WC_MAX_DIGEST_SIZE WC_SHA224_DIGEST_SIZE
  135. #define WC_MAX_BLOCK_SIZE WC_SHA224_BLOCK_SIZE
  136. #elif !defined(NO_SHA)
  137. #define WC_MAX_DIGEST_SIZE WC_SHA_DIGEST_SIZE
  138. #define WC_MAX_BLOCK_SIZE WC_SHA_BLOCK_SIZE
  139. #elif !defined(NO_MD5)
  140. #define WC_MAX_DIGEST_SIZE WC_MD5_DIGEST_SIZE
  141. #define WC_MAX_BLOCK_SIZE WC_MD5_BLOCK_SIZE
  142. #else
  143. #define WC_MAX_DIGEST_SIZE 64 /* default to max size of 64 */
  144. #define WC_MAX_BLOCK_SIZE 128
  145. #endif
  146. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  147. WOLFSSL_API int wc_HashGetOID(enum wc_HashType hash_type);
  148. WOLFSSL_API enum wc_HashType wc_OidGetHash(int oid);
  149. #endif
  150. WOLFSSL_API enum wc_HashType wc_HashTypeConvert(int hashType);
  151. #ifndef NO_HASH_WRAPPER
  152. WOLFSSL_API int wc_HashGetDigestSize(enum wc_HashType hash_type);
  153. WOLFSSL_API int wc_HashGetBlockSize(enum wc_HashType hash_type);
  154. WOLFSSL_API int wc_Hash(enum wc_HashType hash_type,
  155. const byte* data, word32 data_len,
  156. byte* hash, word32 hash_len);
  157. /* generic hash operation wrappers */
  158. WOLFSSL_API int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type,
  159. void* heap, int devId);
  160. WOLFSSL_API int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type);
  161. WOLFSSL_API int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type,
  162. const byte* data, word32 dataSz);
  163. WOLFSSL_API int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type,
  164. byte* out);
  165. WOLFSSL_API int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type);
  166. #ifdef WOLFSSL_HASH_FLAGS
  167. WOLFSSL_API int wc_HashSetFlags(wc_HashAlg* hash, enum wc_HashType type,
  168. word32 flags);
  169. WOLFSSL_API int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type,
  170. word32* flags);
  171. #endif
  172. #ifndef NO_MD5
  173. #include <wolfssl/wolfcrypt/md5.h>
  174. WOLFSSL_API int wc_Md5Hash(const byte* data, word32 len, byte* hash);
  175. #endif
  176. #ifndef NO_SHA
  177. #include <wolfssl/wolfcrypt/sha.h>
  178. WOLFSSL_API int wc_ShaHash(const byte* data, word32 len, byte* hash);
  179. #endif
  180. #ifdef WOLFSSL_SHA224
  181. #include <wolfssl/wolfcrypt/sha256.h>
  182. WOLFSSL_API int wc_Sha224Hash(const byte* data, word32 len, byte* hash);
  183. #endif /* defined(WOLFSSL_SHA224) */
  184. #ifndef NO_SHA256
  185. #include <wolfssl/wolfcrypt/sha256.h>
  186. WOLFSSL_API int wc_Sha256Hash(const byte* data, word32 len, byte* hash);
  187. #endif
  188. #ifdef WOLFSSL_SHA384
  189. #include <wolfssl/wolfcrypt/sha512.h>
  190. WOLFSSL_API int wc_Sha384Hash(const byte* data, word32 len, byte* hash);
  191. #endif /* defined(WOLFSSL_SHA384) */
  192. #ifdef WOLFSSL_SHA512
  193. #include <wolfssl/wolfcrypt/sha512.h>
  194. WOLFSSL_API int wc_Sha512Hash(const byte* data, word32 len, byte* hash);
  195. WOLFSSL_API int wc_Sha512_224Hash(const byte* data, word32 len, byte* hash);
  196. WOLFSSL_API int wc_Sha512_256Hash(const byte* data, word32 len, byte* hash);
  197. #endif /* WOLFSSL_SHA512 */
  198. #ifdef WOLFSSL_SHA3
  199. #include <wolfssl/wolfcrypt/sha3.h>
  200. WOLFSSL_API int wc_Sha3_224Hash(const byte* data, word32 len, byte* hash);
  201. WOLFSSL_API int wc_Sha3_256Hash(const byte* data, word32 len, byte* hash);
  202. WOLFSSL_API int wc_Sha3_384Hash(const byte* data, word32 len, byte* hash);
  203. WOLFSSL_API int wc_Sha3_512Hash(const byte* data, word32 len, byte* hash);
  204. #ifdef WOLFSSL_SHAKE128
  205. WOLFSSL_API int wc_Shake128Hash(const byte* data, word32 len, byte* hash,
  206. word32 hashLen);
  207. #endif
  208. #ifdef WOLFSSL_SHAKE256
  209. WOLFSSL_API int wc_Shake256Hash(const byte* data, word32 len, byte* hash,
  210. word32 hashLen);
  211. #endif
  212. #endif /* WOLFSSL_SHA3 */
  213. #ifdef WOLFSSL_SM3
  214. WOLFSSL_API int wc_Sm3Hash(const byte* data, word32 len, byte* hash);
  215. #endif
  216. #endif /* !NO_HASH_WRAPPER */
  217. #if defined(WOLFSSL_HASH_KEEP)
  218. WOLFSSL_LOCAL int _wc_Hash_Grow(byte** msg, word32* used, word32* len,
  219. const byte* in, int inSz, void* heap);
  220. #endif
  221. #ifdef __cplusplus
  222. } /* extern "C" */
  223. #endif
  224. #endif /* WOLF_CRYPT_HASH_H */