hash.h
Go to the documentation of this file.
1 /* hash.h
2  *
3  * Copyright (C) 2006-2020 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 
26 #ifndef WOLF_CRYPT_HASH_H
27 #define WOLF_CRYPT_HASH_H
28 
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)
39 #endif
40 #if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
42 #endif
43 #ifdef HAVE_BLAKE2
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)
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 
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 
116 /* Find largest possible digest size
117  Note if this gets up to the size of 80 or over check smallstack build */
118 #if defined(WOLFSSL_SHA3)
119  #define WC_MAX_DIGEST_SIZE WC_SHA3_512_DIGEST_SIZE
120  #define WC_MAX_BLOCK_SIZE WC_SHA3_224_BLOCK_SIZE /* 224 is the largest block size */
121 #elif defined(WOLFSSL_SHA512)
122  #define WC_MAX_DIGEST_SIZE WC_SHA512_DIGEST_SIZE
123  #define WC_MAX_BLOCK_SIZE WC_SHA512_BLOCK_SIZE
124 #elif defined(HAVE_BLAKE2)
125  #define WC_MAX_DIGEST_SIZE BLAKE2B_OUTBYTES
126  #define WC_MAX_BLOCK_SIZE BLAKE2B_BLOCKBYTES
127 #elif defined(WOLFSSL_SHA384)
128  #define WC_MAX_DIGEST_SIZE WC_SHA384_DIGEST_SIZE
129  #define WC_MAX_BLOCK_SIZE WC_SHA384_BLOCK_SIZE
130 #elif !defined(NO_SHA256)
131  #define WC_MAX_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
132  #define WC_MAX_BLOCK_SIZE WC_SHA256_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 
147 #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
148 WOLFSSL_API int wc_HashGetOID(enum wc_HashType hash_type);
149 WOLFSSL_API enum wc_HashType wc_OidGetHash(int oid);
150 #endif
151 
152 WOLFSSL_API enum wc_HashType wc_HashTypeConvert(int hashType);
153 
154 WOLFSSL_API int wc_HashGetDigestSize(enum wc_HashType hash_type);
155 WOLFSSL_API int wc_HashGetBlockSize(enum wc_HashType hash_type);
156 WOLFSSL_API int wc_Hash(enum wc_HashType hash_type,
157  const byte* data, word32 data_len,
158  byte* hash, word32 hash_len);
159 
160 /* generic hash operation wrappers */
161 WOLFSSL_API int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type,
162  void* heap, int devId);
163 WOLFSSL_API int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type);
164 WOLFSSL_API int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type,
165  const byte* data, word32 dataSz);
166 WOLFSSL_API int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type,
167  byte* out);
168 WOLFSSL_API int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type);
169 
170 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
171  WOLFSSL_API int wc_HashSetFlags(wc_HashAlg* hash, enum wc_HashType type,
172  word32 flags);
173  WOLFSSL_API int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type,
174  word32* flags);
175 #endif
176 
177 #ifndef NO_MD5
178 #include <wolfssl/wolfcrypt/md5.h>
179 WOLFSSL_API int wc_Md5Hash(const byte* data, word32 len, byte* hash);
180 #endif
181 
182 #ifndef NO_SHA
183 #include <wolfssl/wolfcrypt/sha.h>
184 WOLFSSL_API int wc_ShaHash(const byte*, word32, byte*);
185 #endif
186 
187 #ifdef WOLFSSL_SHA224
189 WOLFSSL_API int wc_Sha224Hash(const byte*, word32, byte*);
190 #endif /* defined(WOLFSSL_SHA224) */
191 
192 #ifndef NO_SHA256
194 WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*);
195 #endif
196 
197 #ifdef WOLFSSL_SHA384
199 WOLFSSL_API int wc_Sha384Hash(const byte*, word32, byte*);
200 #endif /* defined(WOLFSSL_SHA384) */
201 
202 #ifdef WOLFSSL_SHA512
204 WOLFSSL_API int wc_Sha512Hash(const byte*, word32, byte*);
205 #endif /* WOLFSSL_SHA512 */
206 
207 #ifdef WOLFSSL_SHA3
208 #include <wolfssl/wolfcrypt/sha3.h>
209 WOLFSSL_API int wc_Sha3_224Hash(const byte*, word32, byte*);
210 WOLFSSL_API int wc_Sha3_256Hash(const byte*, word32, byte*);
211 WOLFSSL_API int wc_Sha3_384Hash(const byte*, word32, byte*);
212 WOLFSSL_API int wc_Sha3_512Hash(const byte*, word32, byte*);
213 #ifdef WOLFSSL_SHAKE256
214 WOLFSSL_API int wc_Shake256Hash(const byte*, word32, byte*, word32);
215 #endif
216 #endif /* WOLFSSL_SHA3 */
217 
218 enum max_prf {
219 #ifdef HAVE_FFDHE_8192
220  MAX_PRF_HALF = 516, /* Maximum half secret len */
221 #elif defined(HAVE_FFDHE_6144)
222  MAX_PRF_HALF = 388, /* Maximum half secret len */
223 #else
224  MAX_PRF_HALF = 260, /* Maximum half secret len */
225 #endif
226  MAX_PRF_LABSEED = 128, /* Maximum label + seed len */
227  MAX_PRF_DIG = 224 /* Maximum digest len */
228 };
229 
230 #ifdef WOLFSSL_HAVE_PRF
231 WOLFSSL_API int wc_PRF(byte* result, word32 resLen, const byte* secret,
232  word32 secLen, const byte* seed, word32 seedLen, int hash,
233  void* heap, int devId);
234 WOLFSSL_API int wc_PRF_TLSv1(byte* digest, word32 digLen, const byte* secret,
235  word32 secLen, const byte* label, word32 labLen,
236  const byte* seed, word32 seedLen, void* heap, int devId);
237 WOLFSSL_API int wc_PRF_TLS(byte* digest, word32 digLen, const byte* secret,
238  word32 secLen, const byte* label, word32 labLen,
239  const byte* seed, word32 seedLen, int useAtLeastSha256,
240  int hash_type, void* heap, int devId);
241 #endif /* WOLFSSL_HAVE_PRF */
242 
243 #ifdef __cplusplus
244  } /* extern "C" */
245 #endif
246 
247 #endif /* WOLF_CRYPT_HASH_H */
Definition: sha256.h:132
Definition: md5.h:79
Definition: ti-hash.h:38
WOLFSSL_API int wc_Sha384Hash(const byte *, word32, byte *)
Convenience function, handles all the hashing and places the result into hash.
Definition: hash.c:1176
WOLFSSL_API int wc_Sha512Hash(const byte *, word32, byte *)
Convenience function, handles all the hashing and places the result into hash.
Definition: hash.c:1138
WOLFSSL_API int wc_ShaHash(const byte *, word32, byte *)
Convenience function, handles all the hashing and places the result into hash.
Definition: hash.c:1021
WOLFSSL_API int wc_Md5Hash(const byte *data, word32 len, byte *hash)
Convenience function, handles all the hashing and places the result into hash.
Definition: hash.c:984
WOLFSSL_API int wc_HashGetDigestSize(enum wc_HashType hash_type)
This function returns the size of the digest (output) for a hash_type. The returns size is used to ma...
Definition: hash.c:272
Definition: sha512.h:116
WOLFSSL_API int wc_Sha224Hash(const byte *, word32, byte *)
Convenience function, handles all the hashing and places the result into hash.
Definition: hash.c:1058
WOLFSSL_API int wc_Sha256Hash(const byte *, word32, byte *)
Convenience function, handles all the hashing and places the result into hash.
Definition: hash.c:1096
WOLFSSL_API int wc_Hash(enum wc_HashType hash_type, const byte *data, word32 data_len, byte *hash, word32 hash_len)
This function performs a hash on the provided data buffer and returns it in the hash buffer provided...
Definition: hash.c:449
Definition: wolfcaam_sha.h:68
Definition: afalg_hash.h:31
Definition: hash.h:92
WOLFSSL_API int wc_HashGetOID(enum wc_HashType hash_type)
This function will return the OID for the wc_HashType provided.
Definition: hash.c:134