1 | /* hmac.h
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2017 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 |
|
---|
24 | #ifndef NO_HMAC
|
---|
25 |
|
---|
26 | #ifndef WOLF_CRYPT_HMAC_H
|
---|
27 | #define WOLF_CRYPT_HMAC_H
|
---|
28 |
|
---|
29 | #include <wolfssl/wolfcrypt/types.h>
|
---|
30 |
|
---|
31 | #ifndef NO_MD5
|
---|
32 | #include <wolfssl/wolfcrypt/md5.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #ifndef NO_SHA
|
---|
36 | #include <wolfssl/wolfcrypt/sha.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #if !defined(NO_SHA256) || defined(WOLFSSL_SHA224)
|
---|
40 | #include <wolfssl/wolfcrypt/sha256.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #ifdef WOLFSSL_SHA512
|
---|
44 | #include <wolfssl/wolfcrypt/sha512.h>
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | #ifdef HAVE_BLAKE2
|
---|
48 | #include <wolfssl/wolfcrypt/blake2.h>
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | #ifdef HAVE_FIPS
|
---|
52 | /* for fips */
|
---|
53 | #include <cyassl/ctaocrypt/hmac.h>
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 | #ifdef __cplusplus
|
---|
58 | extern "C" {
|
---|
59 | #endif
|
---|
60 | #ifndef HAVE_FIPS
|
---|
61 |
|
---|
62 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
63 | #include <wolfssl/wolfcrypt/async.h>
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | enum {
|
---|
67 | HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */
|
---|
68 |
|
---|
69 | IPAD = 0x36,
|
---|
70 | OPAD = 0x5C,
|
---|
71 |
|
---|
72 | /* If any hash is not enabled, add the ID here. */
|
---|
73 | #ifdef NO_MD5
|
---|
74 | WC_MD5 = 0,
|
---|
75 | #endif
|
---|
76 | #ifdef NO_SHA
|
---|
77 | WC_SHA = 1,
|
---|
78 | #endif
|
---|
79 | #ifdef NO_SHA256
|
---|
80 | WC_SHA256 = 2,
|
---|
81 | #endif
|
---|
82 | #ifndef WOLFSSL_SHA512
|
---|
83 | WC_SHA512 = 4,
|
---|
84 | #endif
|
---|
85 | #ifndef WOLFSSL_SHA384
|
---|
86 | WC_SHA384 = 5,
|
---|
87 | #endif
|
---|
88 | #ifndef HAVE_BLAKE2
|
---|
89 | BLAKE2B_ID = 7,
|
---|
90 | #endif
|
---|
91 | #ifndef WOLFSSL_SHA224
|
---|
92 | WC_SHA224 = 8,
|
---|
93 | #endif
|
---|
94 |
|
---|
95 |
|
---|
96 | /* Select the largest available hash for the buffer size. */
|
---|
97 | #if defined(WOLFSSL_SHA512)
|
---|
98 | MAX_DIGEST_SIZE = WC_SHA512_DIGEST_SIZE,
|
---|
99 | HMAC_BLOCK_SIZE = WC_SHA512_BLOCK_SIZE,
|
---|
100 | #elif defined(HAVE_BLAKE2)
|
---|
101 | MAX_DIGEST_SIZE = BLAKE2B_OUTBYTES,
|
---|
102 | HMAC_BLOCK_SIZE = BLAKE2B_BLOCKBYTES,
|
---|
103 | #elif defined(WOLFSSL_SHA384)
|
---|
104 | MAX_DIGEST_SIZE = WC_SHA384_DIGEST_SIZE,
|
---|
105 | HMAC_BLOCK_SIZE = WC_SHA384_BLOCK_SIZE
|
---|
106 | #elif !defined(NO_SHA256)
|
---|
107 | MAX_DIGEST_SIZE = WC_SHA256_DIGEST_SIZE,
|
---|
108 | HMAC_BLOCK_SIZE = WC_SHA256_BLOCK_SIZE
|
---|
109 | #elif defined(WOLFSSL_SHA224)
|
---|
110 | MAX_DIGEST_SIZE = WC_SHA224_DIGEST_SIZE,
|
---|
111 | HMAC_BLOCK_SIZE = WC_SHA224_BLOCK_SIZE
|
---|
112 | #elif !defined(NO_SHA)
|
---|
113 | MAX_DIGEST_SIZE = WC_SHA_DIGEST_SIZE,
|
---|
114 | HMAC_BLOCK_SIZE = WC_SHA_BLOCK_SIZE,
|
---|
115 | #elif !defined(NO_MD5)
|
---|
116 | MAX_DIGEST_SIZE = WC_MD5_DIGEST_SIZE,
|
---|
117 | HMAC_BLOCK_SIZE = WC_MD5_BLOCK_SIZE,
|
---|
118 | #else
|
---|
119 | #error "You have to have some kind of hash if you want to use HMAC."
|
---|
120 | #endif
|
---|
121 | };
|
---|
122 |
|
---|
123 |
|
---|
124 | /* hash union */
|
---|
125 | typedef union {
|
---|
126 | #ifndef NO_MD5
|
---|
127 | wc_Md5 md5;
|
---|
128 | #endif
|
---|
129 | #ifndef NO_SHA
|
---|
130 | wc_Sha sha;
|
---|
131 | #endif
|
---|
132 | #ifdef WOLFSSL_SHA224
|
---|
133 | wc_Sha224 sha224;
|
---|
134 | #endif
|
---|
135 | #ifndef NO_SHA256
|
---|
136 | wc_Sha256 sha256;
|
---|
137 | #endif
|
---|
138 | #ifdef WOLFSSL_SHA512
|
---|
139 | #ifdef WOLFSSL_SHA384
|
---|
140 | wc_Sha384 sha384;
|
---|
141 | #endif
|
---|
142 | wc_Sha512 sha512;
|
---|
143 | #endif
|
---|
144 | #ifdef HAVE_BLAKE2
|
---|
145 | Blake2b blake2b;
|
---|
146 | #endif
|
---|
147 | } Hash;
|
---|
148 |
|
---|
149 | /* Hmac digest */
|
---|
150 | typedef struct Hmac {
|
---|
151 | Hash hash;
|
---|
152 | word32 ipad[HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/
|
---|
153 | word32 opad[HMAC_BLOCK_SIZE / sizeof(word32)];
|
---|
154 | word32 innerHash[MAX_DIGEST_SIZE / sizeof(word32)];
|
---|
155 | void* heap; /* heap hint */
|
---|
156 | byte macType; /* md5 sha or sha256 */
|
---|
157 | byte innerHashKeyed; /* keyed flag */
|
---|
158 |
|
---|
159 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
160 | WC_ASYNC_DEV asyncDev;
|
---|
161 | word16 keyLen; /* hmac key length (key in ipad) */
|
---|
162 | #ifdef HAVE_CAVIUM
|
---|
163 | byte* data; /* buffered input data for one call */
|
---|
164 | word16 dataLen;
|
---|
165 | #endif /* HAVE_CAVIUM */
|
---|
166 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
167 | } Hmac;
|
---|
168 |
|
---|
169 | #endif /* HAVE_FIPS */
|
---|
170 |
|
---|
171 | /* does init */
|
---|
172 | WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
|
---|
173 | WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32);
|
---|
174 | WOLFSSL_API int wc_HmacFinal(Hmac*, byte*);
|
---|
175 | WOLFSSL_API int wc_HmacSizeByType(int type);
|
---|
176 |
|
---|
177 | WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId);
|
---|
178 | WOLFSSL_API void wc_HmacFree(Hmac*);
|
---|
179 |
|
---|
180 | WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
|
---|
181 |
|
---|
182 | #ifdef HAVE_HKDF
|
---|
183 |
|
---|
184 | WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
|
---|
185 | const byte* inKey, word32 inKeySz, byte* out);
|
---|
186 | WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
|
---|
187 | const byte* info, word32 infoSz,
|
---|
188 | byte* out, word32 outSz);
|
---|
189 |
|
---|
190 | WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
|
---|
191 | const byte* salt, word32 saltSz,
|
---|
192 | const byte* info, word32 infoSz,
|
---|
193 | byte* out, word32 outSz);
|
---|
194 |
|
---|
195 | #endif /* HAVE_HKDF */
|
---|
196 |
|
---|
197 | #ifdef __cplusplus
|
---|
198 | } /* extern "C" */
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | #endif /* WOLF_CRYPT_HMAC_H */
|
---|
202 |
|
---|
203 | #endif /* NO_HMAC */
|
---|
204 |
|
---|