hash.h (9458B)
1 /* hash.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 \file wolfssl/wolfcrypt/hash.h 24 */ 25 26 #ifndef WOLF_CRYPT_HASH_H 27 #define WOLF_CRYPT_HASH_H 28 29 #include <wolfssl/wolfcrypt/types.h> 30 31 #ifndef NO_MD5 32 #include <wolfssl/wolfcrypt/md5.h> 33 #endif 34 #ifndef NO_SHA 35 #include <wolfssl/wolfcrypt/sha.h> 36 #endif 37 #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256) 38 #include <wolfssl/wolfcrypt/sha256.h> 39 #endif 40 #if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512) 41 #include <wolfssl/wolfcrypt/sha512.h> 42 #endif 43 #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S) 44 #include <wolfssl/wolfcrypt/blake2.h> 45 #endif 46 #ifdef WOLFSSL_SHA3 47 #include <wolfssl/wolfcrypt/sha3.h> 48 #endif 49 #ifndef NO_MD4 50 #include <wolfssl/wolfcrypt/md4.h> 51 #endif 52 #ifdef WOLFSSL_MD2 53 #include <wolfssl/wolfcrypt/md2.h> 54 #endif 55 #ifdef WOLFSSL_SM3 56 #include <wolfssl/wolfcrypt/sm3.h> 57 #endif 58 59 60 #ifdef __cplusplus 61 extern "C" { 62 #endif 63 64 #if !defined(HAVE_FIPS) && !defined(NO_OLD_WC_NAMES) 65 #define MAX_DIGEST_SIZE WC_MAX_DIGEST_SIZE 66 #endif 67 68 69 /* Supported Message Authentication Codes from page 43 */ 70 enum wc_MACAlgorithm { 71 no_mac, 72 md5_mac, 73 sha_mac, 74 sha224_mac, 75 sha256_mac, /* needs to match external KDF_MacAlgorithm */ 76 sha384_mac, 77 sha512_mac, 78 rmd_mac, 79 blake2b_mac, 80 sm3_mac 81 }; 82 83 /* hash union */ 84 typedef union { 85 #ifndef NO_MD5 86 wc_Md5 md5; 87 #endif 88 #ifndef NO_SHA 89 wc_Sha sha; 90 #endif 91 #ifdef WOLFSSL_SHA224 92 wc_Sha224 sha224; 93 #endif 94 #ifndef NO_SHA256 95 wc_Sha256 sha256; 96 #endif 97 #ifdef WOLFSSL_SHA384 98 wc_Sha384 sha384; 99 #endif 100 #ifdef WOLFSSL_SHA512 101 wc_Sha512 sha512; 102 #endif 103 #ifdef WOLFSSL_SHA3 104 wc_Sha3 sha3; 105 #endif 106 #ifdef WOLFSSL_SM3 107 wc_Sm3 sm3; 108 #endif 109 WOLF_AGG_DUMMY_MEMBER; 110 } wc_Hashes; 111 112 #ifndef NO_HASH_WRAPPER 113 typedef struct { 114 wc_Hashes alg; 115 enum wc_HashType type; /* sanity check */ 116 #ifndef WC_NO_CONSTRUCTORS 117 void *heap; 118 #endif 119 } wc_HashAlg; 120 #endif /* !NO_HASH_WRAPPER */ 121 122 123 /* Find largest possible digest size 124 Note if this gets up to the size of 80 or over check smallstack build */ 125 #undef WC_MAX_DIGEST_SIZE 126 #undef WC_MAX_BLOCK_SIZE 127 #if defined(WOLFSSL_SHA3) 128 /* note: SHA3-224 has the largest block size */ 129 #define WC_MAX_DIGEST_SIZE WC_SHA3_512_DIGEST_SIZE 130 #define WC_MAX_BLOCK_SIZE WC_SHA3_224_BLOCK_SIZE 131 #elif defined(WOLFSSL_SHA512) 132 #define WC_MAX_DIGEST_SIZE WC_SHA512_DIGEST_SIZE 133 #define WC_MAX_BLOCK_SIZE WC_SHA512_BLOCK_SIZE 134 #elif defined(HAVE_BLAKE2) 135 #define WC_MAX_DIGEST_SIZE BLAKE2B_OUTBYTES 136 #define WC_MAX_BLOCK_SIZE BLAKE2B_BLOCKBYTES 137 #elif defined(WOLFSSL_SHA384) 138 #define WC_MAX_DIGEST_SIZE WC_SHA384_DIGEST_SIZE 139 #define WC_MAX_BLOCK_SIZE WC_SHA384_BLOCK_SIZE 140 #elif !defined(NO_SHA256) 141 #define WC_MAX_DIGEST_SIZE WC_SHA256_DIGEST_SIZE 142 #define WC_MAX_BLOCK_SIZE WC_SHA256_BLOCK_SIZE 143 #elif defined(WOLFSSL_SM3) 144 #define WC_MAX_DIGEST_SIZE WC_SM3_DIGEST_SIZE 145 #define WC_MAX_BLOCK_SIZE WC_SM3_BLOCK_SIZE 146 #elif defined(WOLFSSL_SHA224) 147 #define WC_MAX_DIGEST_SIZE WC_SHA224_DIGEST_SIZE 148 #define WC_MAX_BLOCK_SIZE WC_SHA224_BLOCK_SIZE 149 #elif !defined(NO_SHA) 150 #define WC_MAX_DIGEST_SIZE WC_SHA_DIGEST_SIZE 151 #define WC_MAX_BLOCK_SIZE WC_SHA_BLOCK_SIZE 152 #elif !defined(NO_MD5) 153 #define WC_MAX_DIGEST_SIZE WC_MD5_DIGEST_SIZE 154 #define WC_MAX_BLOCK_SIZE WC_MD5_BLOCK_SIZE 155 #else 156 #define WC_MAX_DIGEST_SIZE 64 /* default to max size of 64 */ 157 #define WC_MAX_BLOCK_SIZE 128 158 #endif 159 160 #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) 161 WOLFSSL_API int wc_HashGetOID(enum wc_HashType hash_type); 162 WOLFSSL_API enum wc_HashType wc_OidGetHash(int oid); 163 #endif 164 165 WOLFSSL_API enum wc_HashType wc_HashTypeConvert(int hashType); 166 167 #ifndef NO_HASH_WRAPPER 168 169 WOLFSSL_API int wc_HashGetDigestSize(enum wc_HashType hash_type); 170 WOLFSSL_API int wc_HashGetBlockSize(enum wc_HashType hash_type); 171 WOLFSSL_API int wc_Hash(enum wc_HashType hash_type, 172 const byte* data, word32 data_len, 173 byte* hash, word32 hash_len); 174 WOLFSSL_API int wc_Hash_ex(enum wc_HashType hash_type, 175 const byte* data, word32 data_len, 176 byte* hash, word32 hash_len, void* heap, int devId); 177 178 /* generic hash operation wrappers */ 179 WOLFSSL_API int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, 180 void* heap, int devId); 181 WOLFSSL_API int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type); 182 WOLFSSL_API int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, 183 const byte* data, word32 dataSz); 184 WOLFSSL_API int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, 185 byte* out); 186 WOLFSSL_API int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type); 187 #ifndef WC_NO_CONSTRUCTORS 188 WOLFSSL_API wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, 189 int devId, int *result_code); 190 WOLFSSL_API int wc_HashDelete(wc_HashAlg *hash, wc_HashAlg **hash_p); 191 #endif 192 193 #ifdef WOLFSSL_HASH_FLAGS 194 WOLFSSL_API int wc_HashSetFlags(wc_HashAlg* hash, enum wc_HashType type, 195 word32 flags); 196 WOLFSSL_API int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type, 197 word32* flags); 198 #endif 199 200 #ifndef NO_MD5 201 #include <wolfssl/wolfcrypt/md5.h> 202 WOLFSSL_API int wc_Md5Hash(const byte* data, word32 len, byte* hash); 203 WOLFSSL_API int wc_Md5Hash_ex(const byte* data, word32 len, byte* hash, 204 void* heap, int devId); 205 #endif 206 207 #ifndef NO_SHA 208 #include <wolfssl/wolfcrypt/sha.h> 209 WOLFSSL_API int wc_ShaHash(const byte* data, word32 len, byte* hash); 210 WOLFSSL_API int wc_ShaHash_ex(const byte* data, word32 len, byte* hash, 211 void* heap, int devId); 212 #endif 213 214 #ifdef WOLFSSL_SHA224 215 #include <wolfssl/wolfcrypt/sha256.h> 216 WOLFSSL_API int wc_Sha224Hash(const byte* data, word32 len, byte* hash); 217 WOLFSSL_API int wc_Sha224Hash_ex(const byte* data, word32 len, byte* hash, 218 void* heap, int devId); 219 #endif /* defined(WOLFSSL_SHA224) */ 220 221 #ifndef NO_SHA256 222 #include <wolfssl/wolfcrypt/sha256.h> 223 WOLFSSL_API int wc_Sha256Hash(const byte* data, word32 len, byte* hash); 224 WOLFSSL_API int wc_Sha256Hash_ex(const byte* data, word32 len, byte* hash, 225 void* heap, int devId); 226 #endif 227 228 #ifdef WOLFSSL_SHA384 229 #include <wolfssl/wolfcrypt/sha512.h> 230 WOLFSSL_API int wc_Sha384Hash(const byte* data, word32 len, byte* hash); 231 WOLFSSL_API int wc_Sha384Hash_ex(const byte* data, word32 len, byte* hash, 232 void* heap, int devId); 233 #endif /* defined(WOLFSSL_SHA384) */ 234 235 #ifdef WOLFSSL_SHA512 236 #include <wolfssl/wolfcrypt/sha512.h> 237 WOLFSSL_API int wc_Sha512Hash(const byte* data, word32 len, byte* hash); 238 WOLFSSL_API int wc_Sha512_224Hash(const byte* data, word32 len, byte* hash); 239 WOLFSSL_API int wc_Sha512_256Hash(const byte* data, word32 len, byte* hash); 240 WOLFSSL_API int wc_Sha512Hash_ex(const byte* data, word32 len, byte* hash, 241 void* heap, int devId); 242 WOLFSSL_API int wc_Sha512_224Hash_ex(const byte* data, word32 len, byte* hash, 243 void* heap, int devId); 244 WOLFSSL_API int wc_Sha512_256Hash_ex(const byte* data, word32 len, byte* hash, 245 void* heap, int devId); 246 #endif /* WOLFSSL_SHA512 */ 247 248 #ifdef WOLFSSL_SHA3 249 #include <wolfssl/wolfcrypt/sha3.h> 250 WOLFSSL_API int wc_Sha3_224Hash(const byte* data, word32 len, byte* hash); 251 WOLFSSL_API int wc_Sha3_256Hash(const byte* data, word32 len, byte* hash); 252 WOLFSSL_API int wc_Sha3_384Hash(const byte* data, word32 len, byte* hash); 253 WOLFSSL_API int wc_Sha3_512Hash(const byte* data, word32 len, byte* hash); 254 WOLFSSL_API int wc_Sha3_224Hash_ex(const byte* data, word32 len, byte* hash, 255 void* heap, int devId); 256 WOLFSSL_API int wc_Sha3_256Hash_ex(const byte* data, word32 len, byte* hash, 257 void* heap, int devId); 258 WOLFSSL_API int wc_Sha3_384Hash_ex(const byte* data, word32 len, byte* hash, 259 void* heap, int devId); 260 WOLFSSL_API int wc_Sha3_512Hash_ex(const byte* data, word32 len, byte* hash, 261 void* heap, int devId); 262 #ifdef WOLFSSL_SHAKE128 263 WOLFSSL_API int wc_Shake128Hash(const byte* data, word32 len, byte* hash, 264 word32 hashLen); 265 WOLFSSL_API int wc_Shake128Hash_ex(const byte* data, word32 len, byte* hash, 266 word32 hashLen, void* heap, int devId); 267 #endif 268 #ifdef WOLFSSL_SHAKE256 269 WOLFSSL_API int wc_Shake256Hash(const byte* data, word32 len, byte* hash, 270 word32 hashLen); 271 WOLFSSL_API int wc_Shake256Hash_ex(const byte* data, word32 len, byte* hash, 272 word32 hashLen, void* heap, int devId); 273 #endif 274 #endif /* WOLFSSL_SHA3 */ 275 276 #ifdef WOLFSSL_SM3 277 WOLFSSL_API int wc_Sm3Hash(const byte* data, word32 len, byte* hash); 278 WOLFSSL_API int wc_Sm3Hash_ex(const byte* data, word32 len, byte* hash, 279 void* heap, int devId); 280 #endif 281 282 #endif /* !NO_HASH_WRAPPER */ 283 284 #if defined(WOLFSSL_HASH_KEEP) 285 WOLFSSL_LOCAL int _wc_Hash_Grow(byte** msg, word32* used, word32* len, 286 const byte* in, int inSz, void* heap); 287 #endif 288 289 #ifdef __cplusplus 290 } /* extern "C" */ 291 #endif 292 293 #endif /* WOLF_CRYPT_HASH_H */