hash.h 7.0 KB

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