yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* hash.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 | \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 | #ifdef HAVE_BLAKE2 |
| 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 | #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S) |
| 56 | #include <wolfssl/wolfcrypt/blake2.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 | }; |
| 81 | |
| 82 | enum wc_HashFlags { |
| 83 | WC_HASH_FLAG_NONE = 0x00000000, |
| 84 | WC_HASH_FLAG_WILLCOPY = 0x00000001, /* flag to indicate hash will be copied */ |
| 85 | WC_HASH_FLAG_ISCOPY = 0x00000002, /* hash is copy */ |
| 86 | #ifdef WOLFSSL_SHA3 |
| 87 | WC_HASH_SHA3_KECCAK256 =0x00010000, /* Older KECCAK256 */ |
| 88 | #endif |
| 89 | }; |
| 90 | |
| 91 | #ifndef NO_HASH_WRAPPER |
| 92 | typedef union { |
| 93 | #ifndef NO_MD5 |
| 94 | wc_Md5 md5; |
| 95 | #endif |
| 96 | #ifndef NO_SHA |
| 97 | wc_Sha sha; |
| 98 | #endif |
| 99 | #ifdef WOLFSSL_SHA224 |
| 100 | wc_Sha224 sha224; |
| 101 | #endif |
| 102 | #ifndef NO_SHA256 |
| 103 | wc_Sha256 sha256; |
| 104 | #endif |
| 105 | #ifdef WOLFSSL_SHA384 |
| 106 | wc_Sha384 sha384; |
| 107 | #endif |
| 108 | #ifdef WOLFSSL_SHA512 |
| 109 | wc_Sha512 sha512; |
| 110 | #endif |
| 111 | #ifdef WOLFSSL_SHA3 |
| 112 | wc_Sha3 sha3; |
| 113 | #endif |
| 114 | } wc_HashAlg; |
| 115 | #endif /* !NO_HASH_WRAPPER */ |
| 116 | |
| 117 | /* Find largest possible digest size |
| 118 | Note if this gets up to the size of 80 or over check smallstack build */ |
| 119 | #if defined(WOLFSSL_SHA3) |
| 120 | #define WC_MAX_DIGEST_SIZE WC_SHA3_512_DIGEST_SIZE |
| 121 | #define WC_MAX_BLOCK_SIZE WC_SHA3_224_BLOCK_SIZE /* 224 is the largest block size */ |
| 122 | #elif defined(WOLFSSL_SHA512) |
| 123 | #define WC_MAX_DIGEST_SIZE WC_SHA512_DIGEST_SIZE |
| 124 | #define WC_MAX_BLOCK_SIZE WC_SHA512_BLOCK_SIZE |
| 125 | #elif defined(HAVE_BLAKE2) |
| 126 | #define WC_MAX_DIGEST_SIZE BLAKE2B_OUTBYTES |
| 127 | #define WC_MAX_BLOCK_SIZE BLAKE2B_BLOCKBYTES |
| 128 | #elif defined(WOLFSSL_SHA384) |
| 129 | #define WC_MAX_DIGEST_SIZE WC_SHA384_DIGEST_SIZE |
| 130 | #define WC_MAX_BLOCK_SIZE WC_SHA384_BLOCK_SIZE |
| 131 | #elif !defined(NO_SHA256) |
| 132 | #define WC_MAX_DIGEST_SIZE WC_SHA256_DIGEST_SIZE |
| 133 | #define WC_MAX_BLOCK_SIZE WC_SHA256_BLOCK_SIZE |
| 134 | #elif defined(WOLFSSL_SHA224) |
| 135 | #define WC_MAX_DIGEST_SIZE WC_SHA224_DIGEST_SIZE |
| 136 | #define WC_MAX_BLOCK_SIZE WC_SHA224_BLOCK_SIZE |
| 137 | #elif !defined(NO_SHA) |
| 138 | #define WC_MAX_DIGEST_SIZE WC_SHA_DIGEST_SIZE |
| 139 | #define WC_MAX_BLOCK_SIZE WC_SHA_BLOCK_SIZE |
| 140 | #elif !defined(NO_MD5) |
| 141 | #define WC_MAX_DIGEST_SIZE WC_MD5_DIGEST_SIZE |
| 142 | #define WC_MAX_BLOCK_SIZE WC_MD5_BLOCK_SIZE |
| 143 | #else |
| 144 | #define WC_MAX_DIGEST_SIZE 64 /* default to max size of 64 */ |
| 145 | #define WC_MAX_BLOCK_SIZE 128 |
| 146 | #endif |
| 147 | |
| 148 | #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) |
| 149 | WOLFSSL_API int wc_HashGetOID(enum wc_HashType hash_type); |
| 150 | WOLFSSL_API enum wc_HashType wc_OidGetHash(int oid); |
| 151 | #endif |
| 152 | |
| 153 | WOLFSSL_API enum wc_HashType wc_HashTypeConvert(int hashType); |
| 154 | |
| 155 | #ifndef NO_HASH_WRAPPER |
| 156 | |
| 157 | WOLFSSL_API int wc_HashGetDigestSize(enum wc_HashType hash_type); |
| 158 | WOLFSSL_API int wc_HashGetBlockSize(enum wc_HashType hash_type); |
| 159 | WOLFSSL_API int wc_Hash(enum wc_HashType hash_type, |
| 160 | const byte* data, word32 data_len, |
| 161 | byte* hash, word32 hash_len); |
| 162 | |
| 163 | /* generic hash operation wrappers */ |
| 164 | WOLFSSL_API int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, |
| 165 | void* heap, int devId); |
| 166 | WOLFSSL_API int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type); |
| 167 | WOLFSSL_API int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, |
| 168 | const byte* data, word32 dataSz); |
| 169 | WOLFSSL_API int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, |
| 170 | byte* out); |
| 171 | WOLFSSL_API int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type); |
| 172 | |
| 173 | #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB) |
| 174 | WOLFSSL_API int wc_HashSetFlags(wc_HashAlg* hash, enum wc_HashType type, |
| 175 | word32 flags); |
| 176 | WOLFSSL_API int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type, |
| 177 | word32* flags); |
| 178 | #endif |
| 179 | |
| 180 | #ifndef NO_MD5 |
| 181 | #include <wolfssl/wolfcrypt/md5.h> |
| 182 | WOLFSSL_API int wc_Md5Hash(const byte* data, word32 len, byte* hash); |
| 183 | #endif |
| 184 | |
| 185 | #ifndef NO_SHA |
| 186 | #include <wolfssl/wolfcrypt/sha.h> |
| 187 | WOLFSSL_API int wc_ShaHash(const byte*, word32, byte*); |
| 188 | #endif |
| 189 | |
| 190 | #ifdef WOLFSSL_SHA224 |
| 191 | #include <wolfssl/wolfcrypt/sha256.h> |
| 192 | WOLFSSL_API int wc_Sha224Hash(const byte*, word32, byte*); |
| 193 | #endif /* defined(WOLFSSL_SHA224) */ |
| 194 | |
| 195 | #ifndef NO_SHA256 |
| 196 | #include <wolfssl/wolfcrypt/sha256.h> |
| 197 | WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*); |
| 198 | #endif |
| 199 | |
| 200 | #ifdef WOLFSSL_SHA384 |
| 201 | #include <wolfssl/wolfcrypt/sha512.h> |
| 202 | WOLFSSL_API int wc_Sha384Hash(const byte*, word32, byte*); |
| 203 | #endif /* defined(WOLFSSL_SHA384) */ |
| 204 | |
| 205 | #ifdef WOLFSSL_SHA512 |
| 206 | #include <wolfssl/wolfcrypt/sha512.h> |
| 207 | WOLFSSL_API int wc_Sha512Hash(const byte*, word32, byte*); |
| 208 | #endif /* WOLFSSL_SHA512 */ |
| 209 | |
| 210 | #ifdef WOLFSSL_SHA3 |
| 211 | #include <wolfssl/wolfcrypt/sha3.h> |
| 212 | WOLFSSL_API int wc_Sha3_224Hash(const byte*, word32, byte*); |
| 213 | WOLFSSL_API int wc_Sha3_256Hash(const byte*, word32, byte*); |
| 214 | WOLFSSL_API int wc_Sha3_384Hash(const byte*, word32, byte*); |
| 215 | WOLFSSL_API int wc_Sha3_512Hash(const byte*, word32, byte*); |
| 216 | #ifdef WOLFSSL_SHAKE256 |
| 217 | WOLFSSL_API int wc_Shake256Hash(const byte*, word32, byte*, word32); |
| 218 | #endif |
| 219 | #endif /* WOLFSSL_SHA3 */ |
| 220 | |
| 221 | #endif /* !NO_HASH_WRAPPER */ |
| 222 | |
| 223 | enum max_prf { |
| 224 | #ifdef HAVE_FFDHE_8192 |
| 225 | MAX_PRF_HALF = 516, /* Maximum half secret len */ |
| 226 | #elif defined(HAVE_FFDHE_6144) |
| 227 | MAX_PRF_HALF = 388, /* Maximum half secret len */ |
| 228 | #else |
| 229 | MAX_PRF_HALF = 260, /* Maximum half secret len */ |
| 230 | #endif |
| 231 | MAX_PRF_LABSEED = 128, /* Maximum label + seed len */ |
| 232 | MAX_PRF_DIG = 224 /* Maximum digest len */ |
| 233 | }; |
| 234 | |
| 235 | #ifdef WOLFSSL_HAVE_PRF |
| 236 | WOLFSSL_API int wc_PRF(byte* result, word32 resLen, const byte* secret, |
| 237 | word32 secLen, const byte* seed, word32 seedLen, int hash, |
| 238 | void* heap, int devId); |
| 239 | WOLFSSL_API int wc_PRF_TLSv1(byte* digest, word32 digLen, const byte* secret, |
| 240 | word32 secLen, const byte* label, word32 labLen, |
| 241 | const byte* seed, word32 seedLen, void* heap, int devId); |
| 242 | WOLFSSL_API int wc_PRF_TLS(byte* digest, word32 digLen, const byte* secret, |
| 243 | word32 secLen, const byte* label, word32 labLen, |
| 244 | const byte* seed, word32 seedLen, int useAtLeastSha256, |
| 245 | int hash_type, void* heap, int devId); |
| 246 | #endif /* WOLFSSL_HAVE_PRF */ |
| 247 | |
| 248 | #ifdef __cplusplus |
| 249 | } /* extern "C" */ |
| 250 | #endif |
| 251 | |
| 252 | #endif /* WOLF_CRYPT_HASH_H */ |