1 | /* hmac.h
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2015 wolfSSL Inc.
|
---|
4 | *
|
---|
5 | * This file is part of wolfSSL. (formerly known as CyaSSL)
|
---|
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-1301, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | #ifndef NO_HMAC
|
---|
24 |
|
---|
25 | #ifndef WOLF_CRYPT_HMAC_H
|
---|
26 | #define WOLF_CRYPT_HMAC_H
|
---|
27 |
|
---|
28 | #include <wolfssl/wolfcrypt/types.h>
|
---|
29 |
|
---|
30 | #ifndef NO_MD5
|
---|
31 | #include <wolfssl/wolfcrypt/md5.h>
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #ifndef NO_SHA
|
---|
35 | #include <wolfssl/wolfcrypt/sha.h>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #ifndef NO_SHA256
|
---|
39 | #include <wolfssl/wolfcrypt/sha256.h>
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #ifdef WOLFSSL_SHA512
|
---|
43 | #include <wolfssl/wolfcrypt/sha512.h>
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #ifdef HAVE_BLAKE2
|
---|
47 | #include <wolfssl/wolfcrypt/blake2.h>
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | #ifdef HAVE_FIPS
|
---|
51 | /* for fips */
|
---|
52 | #include <cyassl/ctaocrypt/hmac.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #ifdef HAVE_CAVIUM
|
---|
56 | #include <wolfssl/wolfcrypt/logging.h>
|
---|
57 | #include "cavium_common.h"
|
---|
58 | #endif
|
---|
59 |
|
---|
60 |
|
---|
61 | #ifdef __cplusplus
|
---|
62 | extern "C" {
|
---|
63 | #endif
|
---|
64 | #ifndef HAVE_FIPS
|
---|
65 | #define WOLFSSL_HMAC_CAVIUM_MAGIC 0xBEEF0005
|
---|
66 |
|
---|
67 | enum {
|
---|
68 | HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */
|
---|
69 |
|
---|
70 | IPAD = 0x36,
|
---|
71 | OPAD = 0x5C,
|
---|
72 |
|
---|
73 | /* If any hash is not enabled, add the ID here. */
|
---|
74 | #ifdef NO_MD5
|
---|
75 | MD5 = 0,
|
---|
76 | #endif
|
---|
77 | #ifdef NO_SHA
|
---|
78 | SHA = 1,
|
---|
79 | #endif
|
---|
80 | #ifdef NO_SHA256
|
---|
81 | SHA256 = 2,
|
---|
82 | #endif
|
---|
83 | #ifndef WOLFSSL_SHA512
|
---|
84 | SHA512 = 4,
|
---|
85 | #endif
|
---|
86 | #ifndef WOLFSSL_SHA384
|
---|
87 | SHA384 = 5,
|
---|
88 | #endif
|
---|
89 | #ifndef HAVE_BLAKE2
|
---|
90 | BLAKE2B_ID = 7,
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | /* Select the largest available hash for the buffer size. */
|
---|
94 | #if defined(WOLFSSL_SHA512)
|
---|
95 | MAX_DIGEST_SIZE = SHA512_DIGEST_SIZE,
|
---|
96 | HMAC_BLOCK_SIZE = SHA512_BLOCK_SIZE
|
---|
97 | #elif defined(HAVE_BLAKE2)
|
---|
98 | MAX_DIGEST_SIZE = BLAKE2B_OUTBYTES,
|
---|
99 | HMAC_BLOCK_SIZE = BLAKE2B_BLOCKBYTES,
|
---|
100 | #elif defined(WOLFSSL_SHA384)
|
---|
101 | MAX_DIGEST_SIZE = SHA384_DIGEST_SIZE,
|
---|
102 | HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE
|
---|
103 | #elif !defined(NO_SHA256)
|
---|
104 | MAX_DIGEST_SIZE = SHA256_DIGEST_SIZE,
|
---|
105 | HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE
|
---|
106 | #elif !defined(NO_SHA)
|
---|
107 | MAX_DIGEST_SIZE = SHA_DIGEST_SIZE,
|
---|
108 | HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE
|
---|
109 | #elif !defined(NO_MD5)
|
---|
110 | MAX_DIGEST_SIZE = MD5_DIGEST_SIZE,
|
---|
111 | HMAC_BLOCK_SIZE = MD5_BLOCK_SIZE
|
---|
112 | #else
|
---|
113 | #error "You have to have some kind of hash if you want to use HMAC."
|
---|
114 | #endif
|
---|
115 | };
|
---|
116 |
|
---|
117 |
|
---|
118 | /* hash union */
|
---|
119 | typedef union {
|
---|
120 | #ifndef NO_MD5
|
---|
121 | Md5 md5;
|
---|
122 | #endif
|
---|
123 | #ifndef NO_SHA
|
---|
124 | Sha sha;
|
---|
125 | #endif
|
---|
126 | #ifndef NO_SHA256
|
---|
127 | Sha256 sha256;
|
---|
128 | #endif
|
---|
129 | #ifdef WOLFSSL_SHA384
|
---|
130 | Sha384 sha384;
|
---|
131 | #endif
|
---|
132 | #ifdef WOLFSSL_SHA512
|
---|
133 | Sha512 sha512;
|
---|
134 | #endif
|
---|
135 | #ifdef HAVE_BLAKE2
|
---|
136 | Blake2b blake2b;
|
---|
137 | #endif
|
---|
138 | } Hash;
|
---|
139 |
|
---|
140 | /* Hmac digest */
|
---|
141 | typedef struct Hmac {
|
---|
142 | Hash hash;
|
---|
143 | word32 ipad[HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/
|
---|
144 | word32 opad[HMAC_BLOCK_SIZE / sizeof(word32)];
|
---|
145 | word32 innerHash[MAX_DIGEST_SIZE / sizeof(word32)];
|
---|
146 | byte macType; /* md5 sha or sha256 */
|
---|
147 | byte innerHashKeyed; /* keyed flag */
|
---|
148 | #ifdef HAVE_CAVIUM
|
---|
149 | word16 keyLen; /* hmac key length */
|
---|
150 | word16 dataLen;
|
---|
151 | HashType type; /* hmac key type */
|
---|
152 | int devId; /* nitrox device id */
|
---|
153 | word32 magic; /* using cavium magic */
|
---|
154 | word64 contextHandle; /* nitrox context memory handle */
|
---|
155 | byte* data; /* buffered input data for one call */
|
---|
156 | #endif
|
---|
157 | } Hmac;
|
---|
158 |
|
---|
159 | #endif /* HAVE_FIPS */
|
---|
160 |
|
---|
161 | /* does init */
|
---|
162 | WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
|
---|
163 | WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32);
|
---|
164 | WOLFSSL_API int wc_HmacFinal(Hmac*, byte*);
|
---|
165 |
|
---|
166 | #ifdef HAVE_CAVIUM
|
---|
167 | WOLFSSL_API int wc_HmacInitCavium(Hmac*, int);
|
---|
168 | WOLFSSL_API void wc_HmacFreeCavium(Hmac*);
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
|
---|
172 |
|
---|
173 |
|
---|
174 | #ifdef HAVE_HKDF
|
---|
175 |
|
---|
176 | WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
|
---|
177 | const byte* salt, word32 saltSz,
|
---|
178 | const byte* info, word32 infoSz,
|
---|
179 | byte* out, word32 outSz);
|
---|
180 |
|
---|
181 | #endif /* HAVE_HKDF */
|
---|
182 |
|
---|
183 | #ifdef __cplusplus
|
---|
184 | } /* extern "C" */
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | #endif /* WOLF_CRYPT_HMAC_H */
|
---|
188 |
|
---|
189 | #endif /* NO_HMAC */
|
---|
190 |
|
---|