1 | /* aes.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 | \file wolfssl/wolfcrypt/aes.h
|
---|
24 | */
|
---|
25 |
|
---|
26 |
|
---|
27 | #ifndef WOLF_CRYPT_AES_H
|
---|
28 | #define WOLF_CRYPT_AES_H
|
---|
29 |
|
---|
30 | #include <wolfssl/wolfcrypt/types.h>
|
---|
31 |
|
---|
32 | #ifndef NO_AES
|
---|
33 |
|
---|
34 | #if defined(HAVE_FIPS) && \
|
---|
35 | defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
|
---|
36 | #include <wolfssl/wolfcrypt/fips.h>
|
---|
37 | #endif /* HAVE_FIPS_VERSION >= 2 */
|
---|
38 |
|
---|
39 | /* included for fips @wc_fips */
|
---|
40 | #if defined(HAVE_FIPS) && \
|
---|
41 | (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
|
---|
42 | #include <cyassl/ctaocrypt/aes.h>
|
---|
43 | #if defined(CYASSL_AES_COUNTER) && !defined(WOLFSSL_AES_COUNTER)
|
---|
44 | #define WOLFSSL_AES_COUNTER
|
---|
45 | #endif
|
---|
46 | #if !defined(WOLFSSL_AES_DIRECT) && defined(CYASSL_AES_DIRECT)
|
---|
47 | #define WOLFSSL_AES_DIRECT
|
---|
48 | #endif
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | #ifndef WC_NO_RNG
|
---|
52 | #include <wolfssl/wolfcrypt/random.h>
|
---|
53 | #endif
|
---|
54 | #ifdef STM32_CRYPTO
|
---|
55 | #include <wolfssl/wolfcrypt/port/st/stm32.h>
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #ifdef WOLFSSL_AESNI
|
---|
59 |
|
---|
60 | #include <wmmintrin.h>
|
---|
61 | #include <emmintrin.h>
|
---|
62 | #include <smmintrin.h>
|
---|
63 |
|
---|
64 | #endif /* WOLFSSL_AESNI */
|
---|
65 |
|
---|
66 |
|
---|
67 | #ifdef WOLFSSL_XILINX_CRYPT
|
---|
68 | #include "xsecure_aes.h"
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | #ifdef WOLFSSL_AFALG
|
---|
72 | /* included for struct msghdr */
|
---|
73 | #include <sys/socket.h>
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | #if defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC)
|
---|
77 | #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | #if defined(HAVE_AESGCM) && !defined(WC_NO_RNG)
|
---|
81 | #include <wolfssl/wolfcrypt/random.h>
|
---|
82 | #endif
|
---|
83 |
|
---|
84 |
|
---|
85 | #ifdef __cplusplus
|
---|
86 | extern "C" {
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | /* these are required for FIPS and non-FIPS */
|
---|
90 | enum {
|
---|
91 | AES_128_KEY_SIZE = 16, /* for 128 bit */
|
---|
92 | AES_192_KEY_SIZE = 24, /* for 192 bit */
|
---|
93 | AES_256_KEY_SIZE = 32, /* for 256 bit */
|
---|
94 |
|
---|
95 | AES_IV_SIZE = 16, /* always block size */
|
---|
96 | };
|
---|
97 |
|
---|
98 |
|
---|
99 | /* avoid redefinition of structs */
|
---|
100 | #if !defined(HAVE_FIPS) || \
|
---|
101 | (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
|
---|
102 |
|
---|
103 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
104 | #include <wolfssl/wolfcrypt/async.h>
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | enum {
|
---|
108 | AES_ENC_TYPE = WC_CIPHER_AES, /* cipher unique type */
|
---|
109 | AES_ENCRYPTION = 0,
|
---|
110 | AES_DECRYPTION = 1,
|
---|
111 |
|
---|
112 | AES_BLOCK_SIZE = 16,
|
---|
113 |
|
---|
114 | KEYWRAP_BLOCK_SIZE = 8,
|
---|
115 |
|
---|
116 | GCM_NONCE_MAX_SZ = 16, /* wolfCrypt's maximum nonce size allowed. */
|
---|
117 | GCM_NONCE_MID_SZ = 12, /* The usual default nonce size for AES-GCM. */
|
---|
118 | GCM_NONCE_MIN_SZ = 8, /* wolfCrypt's minimum nonce size allowed. */
|
---|
119 | CCM_NONCE_MIN_SZ = 7,
|
---|
120 | CCM_NONCE_MAX_SZ = 13,
|
---|
121 | CTR_SZ = 4,
|
---|
122 | AES_IV_FIXED_SZ = 4,
|
---|
123 |
|
---|
124 | #ifdef HAVE_PKCS11
|
---|
125 | AES_MAX_ID_LEN = 32,
|
---|
126 | #endif
|
---|
127 | };
|
---|
128 |
|
---|
129 |
|
---|
130 | typedef struct Aes {
|
---|
131 | /* AESNI needs key first, rounds 2nd, not sure why yet */
|
---|
132 | ALIGN16 word32 key[60];
|
---|
133 | word32 rounds;
|
---|
134 | int keylen;
|
---|
135 |
|
---|
136 | ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
|
---|
137 | ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)]; /* same */
|
---|
138 |
|
---|
139 | #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
|
---|
140 | word32 invokeCtr[2];
|
---|
141 | word32 nonceSz;
|
---|
142 | #endif
|
---|
143 | #ifdef HAVE_AESGCM
|
---|
144 | ALIGN16 byte H[AES_BLOCK_SIZE];
|
---|
145 | #ifdef GCM_TABLE
|
---|
146 | /* key-based fast multiplication table. */
|
---|
147 | ALIGN16 byte M0[256][AES_BLOCK_SIZE];
|
---|
148 | #endif /* GCM_TABLE */
|
---|
149 | #endif /* HAVE_AESGCM */
|
---|
150 | #ifdef WOLFSSL_AESNI
|
---|
151 | byte use_aesni;
|
---|
152 | #endif /* WOLFSSL_AESNI */
|
---|
153 | #ifdef WOLF_CRYPTO_DEV
|
---|
154 | int devId;
|
---|
155 | #endif
|
---|
156 | #ifdef HAVE_PKCS11
|
---|
157 | byte id[AES_MAX_ID_LEN];
|
---|
158 | int idLen;
|
---|
159 | #endif
|
---|
160 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
161 | word32 asyncKey[AES_MAX_KEY_SIZE/8/sizeof(word32)]; /* raw key */
|
---|
162 | word32 asyncIv[AES_BLOCK_SIZE/sizeof(word32)]; /* raw IV */
|
---|
163 | WC_ASYNC_DEV asyncDev;
|
---|
164 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
165 | #if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB)
|
---|
166 | word32 left; /* unused bytes left from last call */
|
---|
167 | #endif
|
---|
168 | #ifdef WOLFSSL_XILINX_CRYPT
|
---|
169 | XSecure_Aes xilAes;
|
---|
170 | XCsuDma dma;
|
---|
171 | word32 key_init[8];
|
---|
172 | word32 kup;
|
---|
173 | #endif
|
---|
174 | #ifdef WOLFSSL_AFALG
|
---|
175 | int alFd; /* server socket to bind to */
|
---|
176 | int rdFd; /* socket to read from */
|
---|
177 | struct msghdr msg;
|
---|
178 | int dir; /* flag for encrpyt or decrypt */
|
---|
179 | #endif
|
---|
180 | #if defined(WOLFSSL_DEVCRYPTO) && \
|
---|
181 | (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
|
---|
182 | word32 devKey[AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE/sizeof(word32)]; /* raw key */
|
---|
183 | WC_CRYPTODEV ctx;
|
---|
184 | #endif
|
---|
185 | void* heap; /* memory hint to use */
|
---|
186 | } Aes;
|
---|
187 |
|
---|
188 | #ifdef WOLFSSL_AES_XTS
|
---|
189 | typedef struct XtsAes {
|
---|
190 | Aes aes;
|
---|
191 | Aes tweak;
|
---|
192 | } XtsAes;
|
---|
193 | #endif
|
---|
194 |
|
---|
195 | #ifdef HAVE_AESGCM
|
---|
196 | typedef struct Gmac {
|
---|
197 | Aes aes;
|
---|
198 | } Gmac;
|
---|
199 | #endif /* HAVE_AESGCM */
|
---|
200 | #endif /* HAVE_FIPS */
|
---|
201 |
|
---|
202 |
|
---|
203 | /* Authenticate cipher function prototypes */
|
---|
204 | typedef int (*wc_AesAuthEncryptFunc)(Aes* aes, byte* out,
|
---|
205 | const byte* in, word32 sz,
|
---|
206 | const byte* iv, word32 ivSz,
|
---|
207 | byte* authTag, word32 authTagSz,
|
---|
208 | const byte* authIn, word32 authInSz);
|
---|
209 | typedef int (*wc_AesAuthDecryptFunc)(Aes* aes, byte* out,
|
---|
210 | const byte* in, word32 sz,
|
---|
211 | const byte* iv, word32 ivSz,
|
---|
212 | const byte* authTag, word32 authTagSz,
|
---|
213 | const byte* authIn, word32 authInSz);
|
---|
214 |
|
---|
215 | /* AES-CBC */
|
---|
216 | WOLFSSL_API int wc_AesSetKey(Aes* aes, const byte* key, word32 len,
|
---|
217 | const byte* iv, int dir);
|
---|
218 | WOLFSSL_API int wc_AesSetIV(Aes* aes, const byte* iv);
|
---|
219 |
|
---|
220 | #ifdef HAVE_AES_CBC
|
---|
221 | WOLFSSL_API int wc_AesCbcEncrypt(Aes* aes, byte* out,
|
---|
222 | const byte* in, word32 sz);
|
---|
223 | WOLFSSL_API int wc_AesCbcDecrypt(Aes* aes, byte* out,
|
---|
224 | const byte* in, word32 sz);
|
---|
225 | #endif
|
---|
226 |
|
---|
227 | #ifdef WOLFSSL_AES_CFB
|
---|
228 | WOLFSSL_API int wc_AesCfbEncrypt(Aes* aes, byte* out,
|
---|
229 | const byte* in, word32 sz);
|
---|
230 | #ifdef HAVE_AES_DECRYPT
|
---|
231 | WOLFSSL_API int wc_AesCfbDecrypt(Aes* aes, byte* out,
|
---|
232 | const byte* in, word32 sz);
|
---|
233 | #endif /* HAVE_AES_DECRYPT */
|
---|
234 | #endif /* WOLFSSL_AES_CFB */
|
---|
235 |
|
---|
236 | #ifdef HAVE_AES_ECB
|
---|
237 | WOLFSSL_API int wc_AesEcbEncrypt(Aes* aes, byte* out,
|
---|
238 | const byte* in, word32 sz);
|
---|
239 | WOLFSSL_API int wc_AesEcbDecrypt(Aes* aes, byte* out,
|
---|
240 | const byte* in, word32 sz);
|
---|
241 | #endif
|
---|
242 |
|
---|
243 | /* AES-CTR */
|
---|
244 | #ifdef WOLFSSL_AES_COUNTER
|
---|
245 | WOLFSSL_API int wc_AesCtrEncrypt(Aes* aes, byte* out,
|
---|
246 | const byte* in, word32 sz);
|
---|
247 | #endif
|
---|
248 | /* AES-DIRECT */
|
---|
249 | #if defined(WOLFSSL_AES_DIRECT)
|
---|
250 | WOLFSSL_API void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in);
|
---|
251 | WOLFSSL_API void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in);
|
---|
252 | WOLFSSL_API int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
|
---|
253 | const byte* iv, int dir);
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | #ifdef HAVE_AESGCM
|
---|
257 | #ifdef WOLFSSL_XILINX_CRYPT
|
---|
258 | WOLFSSL_API int wc_AesGcmSetKey_ex(Aes* aes, const byte* key, word32 len,
|
---|
259 | word32 kup);
|
---|
260 | #endif
|
---|
261 | WOLFSSL_API int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len);
|
---|
262 | WOLFSSL_API int wc_AesGcmEncrypt(Aes* aes, byte* out,
|
---|
263 | const byte* in, word32 sz,
|
---|
264 | const byte* iv, word32 ivSz,
|
---|
265 | byte* authTag, word32 authTagSz,
|
---|
266 | const byte* authIn, word32 authInSz);
|
---|
267 | WOLFSSL_API int wc_AesGcmDecrypt(Aes* aes, byte* out,
|
---|
268 | const byte* in, word32 sz,
|
---|
269 | const byte* iv, word32 ivSz,
|
---|
270 | const byte* authTag, word32 authTagSz,
|
---|
271 | const byte* authIn, word32 authInSz);
|
---|
272 |
|
---|
273 | #ifndef WC_NO_RNG
|
---|
274 | WOLFSSL_API int wc_AesGcmSetExtIV(Aes* aes, const byte* iv, word32 ivSz);
|
---|
275 | WOLFSSL_API int wc_AesGcmSetIV(Aes* aes, word32 ivSz,
|
---|
276 | const byte* ivFixed, word32 ivFixedSz,
|
---|
277 | WC_RNG* rng);
|
---|
278 | WOLFSSL_API int wc_AesGcmEncrypt_ex(Aes* aes, byte* out,
|
---|
279 | const byte* in, word32 sz,
|
---|
280 | byte* ivOut, word32 ivOutSz,
|
---|
281 | byte* authTag, word32 authTagSz,
|
---|
282 | const byte* authIn, word32 authInSz);
|
---|
283 | #endif /* WC_NO_RNG */
|
---|
284 |
|
---|
285 | WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len);
|
---|
286 | WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
|
---|
287 | const byte* authIn, word32 authInSz,
|
---|
288 | byte* authTag, word32 authTagSz);
|
---|
289 | #ifndef WC_NO_RNG
|
---|
290 | WOLFSSL_API int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
|
---|
291 | const byte* authIn, word32 authInSz,
|
---|
292 | byte* authTag, word32 authTagSz, WC_RNG* rng);
|
---|
293 | WOLFSSL_API int wc_GmacVerify(const byte* key, word32 keySz,
|
---|
294 | const byte* iv, word32 ivSz,
|
---|
295 | const byte* authIn, word32 authInSz,
|
---|
296 | const byte* authTag, word32 authTagSz);
|
---|
297 | #endif /* WC_NO_RNG */
|
---|
298 | WOLFSSL_LOCAL void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
|
---|
299 | word32 cSz, byte* s, word32 sSz);
|
---|
300 | #endif /* HAVE_AESGCM */
|
---|
301 | #ifdef HAVE_AESCCM
|
---|
302 | WOLFSSL_API int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
|
---|
303 | WOLFSSL_API int wc_AesCcmEncrypt(Aes* aes, byte* out,
|
---|
304 | const byte* in, word32 inSz,
|
---|
305 | const byte* nonce, word32 nonceSz,
|
---|
306 | byte* authTag, word32 authTagSz,
|
---|
307 | const byte* authIn, word32 authInSz);
|
---|
308 | WOLFSSL_API int wc_AesCcmDecrypt(Aes* aes, byte* out,
|
---|
309 | const byte* in, word32 inSz,
|
---|
310 | const byte* nonce, word32 nonceSz,
|
---|
311 | const byte* authTag, word32 authTagSz,
|
---|
312 | const byte* authIn, word32 authInSz);
|
---|
313 | WOLFSSL_API int wc_AesCcmSetNonce(Aes* aes,
|
---|
314 | const byte* nonce, word32 nonceSz);
|
---|
315 | WOLFSSL_API int wc_AesCcmEncrypt_ex(Aes* aes, byte* out,
|
---|
316 | const byte* in, word32 sz,
|
---|
317 | byte* ivOut, word32 ivOutSz,
|
---|
318 | byte* authTag, word32 authTagSz,
|
---|
319 | const byte* authIn, word32 authInSz);
|
---|
320 | #endif /* HAVE_AESCCM */
|
---|
321 | #ifdef HAVE_AES_KEYWRAP
|
---|
322 | WOLFSSL_API int wc_AesKeyWrap(const byte* key, word32 keySz,
|
---|
323 | const byte* in, word32 inSz,
|
---|
324 | byte* out, word32 outSz,
|
---|
325 | const byte* iv);
|
---|
326 | WOLFSSL_API int wc_AesKeyUnWrap(const byte* key, word32 keySz,
|
---|
327 | const byte* in, word32 inSz,
|
---|
328 | byte* out, word32 outSz,
|
---|
329 | const byte* iv);
|
---|
330 | #endif /* HAVE_AES_KEYWRAP */
|
---|
331 |
|
---|
332 | #ifdef WOLFSSL_AES_XTS
|
---|
333 |
|
---|
334 | WOLFSSL_API int wc_AesXtsSetKey(XtsAes* aes, const byte* key,
|
---|
335 | word32 len, int dir, void* heap, int devId);
|
---|
336 |
|
---|
337 | WOLFSSL_API int wc_AesXtsEncryptSector(XtsAes* aes, byte* out,
|
---|
338 | const byte* in, word32 sz, word64 sector);
|
---|
339 |
|
---|
340 | WOLFSSL_API int wc_AesXtsDecryptSector(XtsAes* aes, byte* out,
|
---|
341 | const byte* in, word32 sz, word64 sector);
|
---|
342 |
|
---|
343 | WOLFSSL_API int wc_AesXtsEncrypt(XtsAes* aes, byte* out,
|
---|
344 | const byte* in, word32 sz, const byte* i, word32 iSz);
|
---|
345 |
|
---|
346 | WOLFSSL_API int wc_AesXtsDecrypt(XtsAes* aes, byte* out,
|
---|
347 | const byte* in, word32 sz, const byte* i, word32 iSz);
|
---|
348 |
|
---|
349 | WOLFSSL_API int wc_AesXtsFree(XtsAes* aes);
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | WOLFSSL_API int wc_AesGetKeySize(Aes* aes, word32* keySize);
|
---|
353 |
|
---|
354 | WOLFSSL_API int wc_AesInit(Aes* aes, void* heap, int devId);
|
---|
355 | #ifdef HAVE_PKCS11
|
---|
356 | WOLFSSL_API int wc_AesInit_Id(Aes* aes, unsigned char* id, int len, void* heap,
|
---|
357 | int devId);
|
---|
358 | #endif
|
---|
359 | WOLFSSL_API void wc_AesFree(Aes* aes);
|
---|
360 |
|
---|
361 | #ifdef __cplusplus
|
---|
362 | } /* extern "C" */
|
---|
363 | #endif
|
---|
364 |
|
---|
365 |
|
---|
366 | #endif /* NO_AES */
|
---|
367 | #endif /* WOLF_CRYPT_AES_H */
|
---|