hmac.h
Go to the documentation of this file.
1 /* hmac.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 NO_HMAC
27 
28 #ifndef WOLF_CRYPT_HMAC_H
29 #define WOLF_CRYPT_HMAC_H
30 
31 #include <wolfssl/wolfcrypt/hash.h>
32 
33 #if defined(HAVE_FIPS) && \
34  (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
35 /* for fips @wc_fips */
36  #include <cyassl/ctaocrypt/hmac.h>
37  #define WC_HMAC_BLOCK_SIZE HMAC_BLOCK_SIZE
38 #endif
39 
40 
41 #if defined(HAVE_FIPS) && \
42  defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
43  #include <wolfssl/wolfcrypt/fips.h>
44 #endif
45 
46 #ifdef __cplusplus
47  extern "C" {
48 #endif
49 
50 /* avoid redefinition of structs */
51 #if !defined(HAVE_FIPS) || \
52  (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
53 
54 #ifdef WOLFSSL_ASYNC_CRYPT
55  #include <wolfssl/wolfcrypt/async.h>
56 #endif
57 
58 #ifndef NO_OLD_WC_NAMES
59  #define HMAC_BLOCK_SIZE WC_HMAC_BLOCK_SIZE
60 #endif
61 
62 #define WC_HMAC_INNER_HASH_KEYED_SW 1
63 #define WC_HMAC_INNER_HASH_KEYED_DEV 2
64 
65 enum {
66  HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */
67 
68  IPAD = 0x36,
69  OPAD = 0x5C,
70 
71 /* If any hash is not enabled, add the ID here. */
72 #ifdef NO_MD5
73  WC_MD5 = WC_HASH_TYPE_MD5,
74 #endif
75 #ifdef NO_SHA
76  WC_SHA = WC_HASH_TYPE_SHA,
77 #endif
78 #ifdef NO_SHA256
79  WC_SHA256 = WC_HASH_TYPE_SHA256,
80 #endif
81 #ifndef WOLFSSL_SHA512
82  WC_SHA512 = WC_HASH_TYPE_SHA512,
83 #endif
84 #ifndef WOLFSSL_SHA384
85  WC_SHA384 = WC_HASH_TYPE_SHA384,
86 #endif
87 #ifndef WOLFSSL_SHA224
88  WC_SHA224 = WC_HASH_TYPE_SHA224,
89 #endif
90 #ifndef WOLFSSL_SHA3
91  WC_SHA3_224 = WC_HASH_TYPE_SHA3_224,
92  WC_SHA3_256 = WC_HASH_TYPE_SHA3_256,
93  WC_SHA3_384 = WC_HASH_TYPE_SHA3_384,
94  WC_SHA3_512 = WC_HASH_TYPE_SHA3_512,
95 #endif
96 #ifdef HAVE_PKCS11
97  HMAC_MAX_ID_LEN = 32,
98 #endif
99 };
100 
101 /* Select the largest available hash for the buffer size. */
102 #define WC_HMAC_BLOCK_SIZE WC_MAX_BLOCK_SIZE
103 
104 #if !defined(WOLFSSL_SHA3) && !defined(WOLFSSL_SHA512) && \
105  !defined(WOLFSSL_SHA384) && defined(NO_SHA256) && \
106  defined(WOLFSSL_SHA224) && defined(NO_SHA) && defined(NO_MD5)
107  #error "You have to have some kind of hash if you want to use HMAC."
108 #endif
109 
110 
111 /* hash union */
112 typedef union {
113 #ifndef NO_MD5
114  wc_Md5 md5;
115 #endif
116 #ifndef NO_SHA
117  wc_Sha sha;
118 #endif
119 #ifdef WOLFSSL_SHA224
120  wc_Sha224 sha224;
121 #endif
122 #ifndef NO_SHA256
123  wc_Sha256 sha256;
124 #endif
125 #ifdef WOLFSSL_SHA384
126  wc_Sha384 sha384;
127 #endif
128 #ifdef WOLFSSL_SHA512
129  wc_Sha512 sha512;
130 #endif
131 #ifdef WOLFSSL_SHA3
132  wc_Sha3 sha3;
133 #endif
134 } Hash;
135 
136 /* Hmac digest */
137 struct Hmac {
138  Hash hash;
139  word32 ipad[WC_HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/
140  word32 opad[WC_HMAC_BLOCK_SIZE / sizeof(word32)];
141  word32 innerHash[WC_MAX_DIGEST_SIZE / sizeof(word32)];
142  void* heap; /* heap hint */
143  byte macType; /* md5 sha or sha256 */
144  byte innerHashKeyed; /* keyed flag */
145 #ifdef WOLFSSL_ASYNC_CRYPT
146  WC_ASYNC_DEV asyncDev;
147 #endif /* WOLFSSL_ASYNC_CRYPT */
148 #ifdef WOLF_CRYPTO_CB
149  int devId;
150  void* devCtx;
151  const byte* keyRaw;
152 #endif
153 #ifdef HAVE_PKCS11
154  byte id[HMAC_MAX_ID_LEN];
155  int idLen;
156 #endif
157 #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
158  word16 keyLen; /* hmac key length (key in ipad) */
159 #endif
160 };
161 
162 #ifndef WC_HMAC_TYPE_DEFINED
163  typedef struct Hmac Hmac;
164  #define WC_HMAC_TYPE_DEFINED
165 #endif
166 
167 
168 #endif /* HAVE_FIPS */
169 
170 /* does init */
171 WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
172 WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32);
173 WOLFSSL_API int wc_HmacFinal(Hmac*, byte*);
174 WOLFSSL_API int wc_HmacSizeByType(int type);
175 
176 WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId);
177 WOLFSSL_API int wc_HmacInit_Id(Hmac* hmac, byte* id, int len, void* heap,
178  int devId);
179 WOLFSSL_API void wc_HmacFree(Hmac*);
180 
181 WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
182 
183 WOLFSSL_LOCAL int _InitHmac(Hmac* hmac, int type, void* heap);
184 
185 #ifdef HAVE_HKDF
186 
187 WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
188  const byte* inKey, word32 inKeySz, byte* out);
189 WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
190  const byte* info, word32 infoSz,
191  byte* out, word32 outSz);
192 
193 WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
194  const byte* salt, word32 saltSz,
195  const byte* info, word32 infoSz,
196  byte* out, word32 outSz);
197 
198 #endif /* HAVE_HKDF */
199 
200 #ifdef __cplusplus
201  } /* extern "C" */
202 #endif
203 
204 #endif /* WOLF_CRYPT_HMAC_H */
205 
206 #endif /* NO_HMAC */
207 
WOLFSSL_API int wolfSSL_GetHmacMaxSize(void)
This function returns the largest HMAC digest size available based on the configured cipher suites...
Definition: hmac.c:1147
Definition: sha256.h:132
Definition: md5.h:79
Definition: ti-hash.h:38
WOLFSSL_API int wc_HmacSetKey(Hmac *, int type, const byte *key, word32 keySz)
This function initializes an Hmac object, setting its encryption type, key and HMAC length...
Definition: hmac.c:279
WOLFSSL_API int wc_HKDF(int type, const byte *inKey, word32 inKeySz, const byte *salt, word32 saltSz, const byte *info, word32 infoSz, byte *out, word32 outSz)
This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert i...
Definition: hmac.c:1268
WOLFSSL_API int wc_HmacFinal(Hmac *, byte *)
This function computes the final hash of an Hmac object&#39;s message.
Definition: hmac.c:757
Definition: sha512.h:116
Definition: hmac.h:137
WOLFSSL_API int wc_HmacUpdate(Hmac *, const byte *, word32)
This function updates the message to authenticate using HMAC. It should be called after the Hmac obje...
Definition: hmac.c:654
Definition: hmac.h:112
Definition: wolfcaam_sha.h:68
Definition: afalg_hash.h:31