source: azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfssl/wolfcrypt/aes.h@ 464

Last change on this file since 464 was 464, checked in by coas-nagasima, 3 years ago

WolfSSLとAzure IoT SDKを更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 15.7 KB
Line 
1/* aes.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
22/*!
23 \file wolfssl/wolfcrypt/aes.h
24*/
25/*
26
27DESCRIPTION
28This library provides the interfaces to the Advanced Encryption Standard (AES)
29for encrypting and decrypting data. AES is the standard known for a symmetric
30block cipher mechanism that uses n-bit binary string parameter key with 128-bits,
31192-bits, and 256-bits of key sizes.
32
33*/
34#ifndef WOLF_CRYPT_AES_H
35#define WOLF_CRYPT_AES_H
36
37#include <wolfssl/wolfcrypt/types.h>
38
39#ifndef NO_AES
40
41#if defined(HAVE_FIPS) && \
42 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
43 #include <wolfssl/wolfcrypt/fips.h>
44#endif /* HAVE_FIPS_VERSION >= 2 */
45
46/* included for fips @wc_fips */
47#if defined(HAVE_FIPS) && \
48 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
49#include <cyassl/ctaocrypt/aes.h>
50#if defined(CYASSL_AES_COUNTER) && !defined(WOLFSSL_AES_COUNTER)
51 #define WOLFSSL_AES_COUNTER
52#endif
53#if !defined(WOLFSSL_AES_DIRECT) && defined(CYASSL_AES_DIRECT)
54 #define WOLFSSL_AES_DIRECT
55#endif
56#endif
57
58#ifndef WC_NO_RNG
59 #include <wolfssl/wolfcrypt/random.h>
60#endif
61#ifdef STM32_CRYPTO
62 #include <wolfssl/wolfcrypt/port/st/stm32.h>
63#endif
64
65#ifdef WOLFSSL_IMXRT_DCP
66 #include "fsl_dcp.h"
67#endif
68
69#ifdef WOLFSSL_XILINX_CRYPT
70#include "xsecure_aes.h"
71#endif
72
73#if defined(WOLFSSL_AFALG) || defined(WOLFSSL_AFALG_XILINX_AES)
74/* included for struct msghdr */
75#include <wolfssl/wolfcrypt/port/af_alg/wc_afalg.h>
76#endif
77
78#if defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC)
79#include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
80#endif
81
82#ifdef WOLFSSL_SILABS_SE_ACCEL
83 #include <wolfssl/wolfcrypt/port/silabs/silabs_aes.h>
84#endif
85
86
87#if defined(HAVE_AESGCM) && !defined(WC_NO_RNG)
88 #include <wolfssl/wolfcrypt/random.h>
89#endif
90
91#if defined(WOLFSSL_CRYPTOCELL)
92 #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
93#endif
94
95#if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
96 defined(WOLFSSL_RENESAS_TSIP_TLS_AES_CRYPT)
97 #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
98#endif
99
100#ifdef __cplusplus
101 extern "C" {
102#endif
103
104#ifndef WOLFSSL_AES_KEY_SIZE_ENUM
105#define WOLFSSL_AES_KEY_SIZE_ENUM
106/* these are required for FIPS and non-FIPS */
107enum {
108 AES_128_KEY_SIZE = 16, /* for 128 bit */
109 AES_192_KEY_SIZE = 24, /* for 192 bit */
110 AES_256_KEY_SIZE = 32, /* for 256 bit */
111
112 AES_IV_SIZE = 16, /* always block size */
113};
114#endif
115
116/* avoid redefinition of structs */
117#if !defined(HAVE_FIPS) || \
118 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
119
120#ifdef WOLFSSL_ASYNC_CRYPT
121 #include <wolfssl/wolfcrypt/async.h>
122#endif
123
124enum {
125 AES_ENC_TYPE = WC_CIPHER_AES, /* cipher unique type */
126 AES_ENCRYPTION = 0,
127 AES_DECRYPTION = 1,
128
129 AES_BLOCK_SIZE = 16,
130
131 KEYWRAP_BLOCK_SIZE = 8,
132
133 GCM_NONCE_MAX_SZ = 16, /* wolfCrypt's maximum nonce size allowed. */
134 GCM_NONCE_MID_SZ = 12, /* The usual default nonce size for AES-GCM. */
135 GCM_NONCE_MIN_SZ = 8, /* wolfCrypt's minimum nonce size allowed. */
136 CCM_NONCE_MIN_SZ = 7,
137 CCM_NONCE_MAX_SZ = 13,
138 CTR_SZ = 4,
139 AES_IV_FIXED_SZ = 4,
140#ifdef WOLFSSL_AES_CFB
141 AES_CFB_MODE = 1,
142#endif
143#ifdef WOLFSSL_AES_OFB
144 AES_OFB_MODE = 2,
145#endif
146#ifdef WOLFSSL_AES_XTS
147 AES_XTS_MODE = 3,
148#endif
149
150#ifdef HAVE_PKCS11
151 AES_MAX_ID_LEN = 32,
152 AES_MAX_LABEL_LEN = 32,
153#endif
154};
155
156
157struct Aes {
158 /* AESNI needs key first, rounds 2nd, not sure why yet */
159 ALIGN16 word32 key[60];
160 word32 rounds;
161 int keylen;
162
163 ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
164 ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)]; /* same */
165
166#if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
167 word32 invokeCtr[2];
168 word32 nonceSz;
169#endif
170#ifdef HAVE_AESGCM
171 ALIGN16 byte H[AES_BLOCK_SIZE];
172#ifdef OPENSSL_EXTRA
173 word32 aadH[4]; /* additional authenticated data GHASH */
174 word32 aadLen; /* additional authenticated data len */
175#endif
176
177#ifdef GCM_TABLE
178 /* key-based fast multiplication table. */
179 ALIGN16 byte M0[256][AES_BLOCK_SIZE];
180#elif defined(GCM_TABLE_4BIT)
181 #if defined(BIG_ENDIAN_ORDER) || defined(WC_16BIT_CPU)
182 ALIGN16 byte M0[16][AES_BLOCK_SIZE];
183 #else
184 ALIGN16 byte M0[32][AES_BLOCK_SIZE];
185 #endif
186#endif /* GCM_TABLE */
187#ifdef HAVE_CAVIUM_OCTEON_SYNC
188 word32 y0;
189#endif
190#endif /* HAVE_AESGCM */
191#ifdef WOLFSSL_AESNI
192 byte use_aesni;
193#endif /* WOLFSSL_AESNI */
194#ifdef WOLF_CRYPTO_CB
195 int devId;
196 void* devCtx;
197#endif
198#ifdef HAVE_PKCS11
199 byte id[AES_MAX_ID_LEN];
200 int idLen;
201 char label[AES_MAX_LABEL_LEN];
202 int labelLen;
203#endif
204#ifdef WOLFSSL_ASYNC_CRYPT
205 WC_ASYNC_DEV asyncDev;
206#endif /* WOLFSSL_ASYNC_CRYPT */
207#if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB) || \
208 defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_XTS)
209 word32 left; /* unused bytes left from last call */
210#endif
211#ifdef WOLFSSL_XILINX_CRYPT
212 XSecure_Aes xilAes;
213 XCsuDma dma;
214 word32 key_init[8];
215 word32 kup;
216#endif
217#if defined(WOLFSSL_AFALG) || defined(WOLFSSL_AFALG_XILINX_AES)
218 int alFd; /* server socket to bind to */
219 int rdFd; /* socket to read from */
220 struct msghdr msg;
221 int dir; /* flag for encrpyt or decrypt */
222#ifdef WOLFSSL_AFALG_XILINX_AES
223 word32 msgBuf[CMSG_SPACE(4) + CMSG_SPACE(sizeof(struct af_alg_iv) +
224 GCM_NONCE_MID_SZ)];
225#endif
226#endif
227#if defined(WOLF_CRYPTO_CB) || (defined(WOLFSSL_DEVCRYPTO) && \
228 (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))) || \
229 (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES))
230 word32 devKey[AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE/sizeof(word32)]; /* raw key */
231#ifdef HAVE_CAVIUM_OCTEON_SYNC
232 int keySet;
233#endif
234#endif
235#if defined(WOLFSSL_DEVCRYPTO) && \
236 (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
237 WC_CRYPTODEV ctx;
238#endif
239#if defined(WOLFSSL_CRYPTOCELL)
240 aes_context_t ctx;
241#endif
242#if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
243 defined(WOLFSSL_RENESAS_TSIP_TLS_AES_CRYPT)
244 TSIP_AES_CTX ctx;
245#endif
246#if defined(WOLFSSL_IMXRT_DCP)
247 dcp_handle_t handle;
248#endif
249#if defined(WOLFSSL_SILABS_SE_ACCEL)
250 silabs_aes_t ctx;
251#endif
252 void* heap; /* memory hint to use */
253};
254
255#ifndef WC_AES_TYPE_DEFINED
256 typedef struct Aes Aes;
257 #define WC_AES_TYPE_DEFINED
258#endif
259
260#ifdef WOLFSSL_AES_XTS
261typedef struct XtsAes {
262 Aes aes;
263 Aes tweak;
264} XtsAes;
265#endif
266
267#ifdef HAVE_AESGCM
268typedef struct Gmac {
269 Aes aes;
270} Gmac;
271#endif /* HAVE_AESGCM */
272#endif /* HAVE_FIPS */
273
274
275/* Authenticate cipher function prototypes */
276typedef int (*wc_AesAuthEncryptFunc)(Aes* aes, byte* out,
277 const byte* in, word32 sz,
278 const byte* iv, word32 ivSz,
279 byte* authTag, word32 authTagSz,
280 const byte* authIn, word32 authInSz);
281typedef int (*wc_AesAuthDecryptFunc)(Aes* aes, byte* out,
282 const byte* in, word32 sz,
283 const byte* iv, word32 ivSz,
284 const byte* authTag, word32 authTagSz,
285 const byte* authIn, word32 authInSz);
286
287/* AES-CBC */
288WOLFSSL_API int wc_AesSetKey(Aes* aes, const byte* key, word32 len,
289 const byte* iv, int dir);
290WOLFSSL_API int wc_AesSetIV(Aes* aes, const byte* iv);
291
292#ifdef HAVE_AES_CBC
293WOLFSSL_API int wc_AesCbcEncrypt(Aes* aes, byte* out,
294 const byte* in, word32 sz);
295WOLFSSL_API int wc_AesCbcDecrypt(Aes* aes, byte* out,
296 const byte* in, word32 sz);
297#endif
298
299#ifdef WOLFSSL_AES_CFB
300WOLFSSL_API int wc_AesCfbEncrypt(Aes* aes, byte* out,
301 const byte* in, word32 sz);
302WOLFSSL_API int wc_AesCfb1Encrypt(Aes* aes, byte* out,
303 const byte* in, word32 sz);
304WOLFSSL_API int wc_AesCfb8Encrypt(Aes* aes, byte* out,
305 const byte* in, word32 sz);
306#ifdef HAVE_AES_DECRYPT
307WOLFSSL_API int wc_AesCfbDecrypt(Aes* aes, byte* out,
308 const byte* in, word32 sz);
309WOLFSSL_API int wc_AesCfb1Decrypt(Aes* aes, byte* out,
310 const byte* in, word32 sz);
311WOLFSSL_API int wc_AesCfb8Decrypt(Aes* aes, byte* out,
312 const byte* in, word32 sz);
313#endif /* HAVE_AES_DECRYPT */
314#endif /* WOLFSSL_AES_CFB */
315
316#ifdef WOLFSSL_AES_OFB
317WOLFSSL_API int wc_AesOfbEncrypt(Aes* aes, byte* out,
318 const byte* in, word32 sz);
319#ifdef HAVE_AES_DECRYPT
320WOLFSSL_API int wc_AesOfbDecrypt(Aes* aes, byte* out,
321 const byte* in, word32 sz);
322#endif /* HAVE_AES_DECRYPT */
323#endif /* WOLFSSL_AES_OFB */
324
325#ifdef HAVE_AES_ECB
326WOLFSSL_API int wc_AesEcbEncrypt(Aes* aes, byte* out,
327 const byte* in, word32 sz);
328WOLFSSL_API int wc_AesEcbDecrypt(Aes* aes, byte* out,
329 const byte* in, word32 sz);
330#endif
331
332/* AES-CTR */
333#ifdef WOLFSSL_AES_COUNTER
334 WOLFSSL_API int wc_AesCtrEncrypt(Aes* aes, byte* out,
335 const byte* in, word32 sz);
336#endif
337/* AES-DIRECT */
338#if defined(WOLFSSL_AES_DIRECT)
339 WOLFSSL_API void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in);
340 WOLFSSL_API void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in);
341 WOLFSSL_API int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
342 const byte* iv, int dir);
343#endif
344
345#ifdef HAVE_AESGCM
346#ifdef WOLFSSL_XILINX_CRYPT
347 WOLFSSL_API int wc_AesGcmSetKey_ex(Aes* aes, const byte* key, word32 len,
348 word32 kup);
349#elif defined(WOLFSSL_AFALG_XILINX_AES)
350 WOLFSSL_LOCAL int wc_AesGcmSetKey_ex(Aes* aes, const byte* key, word32 len,
351 word32 kup);
352#endif
353 WOLFSSL_API int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len);
354 WOLFSSL_API int wc_AesGcmEncrypt(Aes* aes, byte* out,
355 const byte* in, word32 sz,
356 const byte* iv, word32 ivSz,
357 byte* authTag, word32 authTagSz,
358 const byte* authIn, word32 authInSz);
359 WOLFSSL_API int wc_AesGcmDecrypt(Aes* aes, byte* out,
360 const byte* in, word32 sz,
361 const byte* iv, word32 ivSz,
362 const byte* authTag, word32 authTagSz,
363 const byte* authIn, word32 authInSz);
364
365#ifndef WC_NO_RNG
366 WOLFSSL_API int wc_AesGcmSetExtIV(Aes* aes, const byte* iv, word32 ivSz);
367 WOLFSSL_API int wc_AesGcmSetIV(Aes* aes, word32 ivSz,
368 const byte* ivFixed, word32 ivFixedSz,
369 WC_RNG* rng);
370 WOLFSSL_API int wc_AesGcmEncrypt_ex(Aes* aes, byte* out,
371 const byte* in, word32 sz,
372 byte* ivOut, word32 ivOutSz,
373 byte* authTag, word32 authTagSz,
374 const byte* authIn, word32 authInSz);
375#endif /* WC_NO_RNG */
376
377 WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len);
378 WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
379 const byte* authIn, word32 authInSz,
380 byte* authTag, word32 authTagSz);
381#ifndef WC_NO_RNG
382 WOLFSSL_API int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
383 const byte* authIn, word32 authInSz,
384 byte* authTag, word32 authTagSz, WC_RNG* rng);
385 WOLFSSL_API int wc_GmacVerify(const byte* key, word32 keySz,
386 const byte* iv, word32 ivSz,
387 const byte* authIn, word32 authInSz,
388 const byte* authTag, word32 authTagSz);
389#endif /* WC_NO_RNG */
390 WOLFSSL_LOCAL void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
391 word32 cSz, byte* s, word32 sSz);
392#endif /* HAVE_AESGCM */
393#ifdef HAVE_AESCCM
394 WOLFSSL_LOCAL int wc_AesCcmCheckTagSize(int sz);
395 WOLFSSL_API int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
396 WOLFSSL_API int wc_AesCcmEncrypt(Aes* aes, byte* out,
397 const byte* in, word32 inSz,
398 const byte* nonce, word32 nonceSz,
399 byte* authTag, word32 authTagSz,
400 const byte* authIn, word32 authInSz);
401 WOLFSSL_API int wc_AesCcmDecrypt(Aes* aes, byte* out,
402 const byte* in, word32 inSz,
403 const byte* nonce, word32 nonceSz,
404 const byte* authTag, word32 authTagSz,
405 const byte* authIn, word32 authInSz);
406 WOLFSSL_API int wc_AesCcmSetNonce(Aes* aes,
407 const byte* nonce, word32 nonceSz);
408 WOLFSSL_API int wc_AesCcmEncrypt_ex(Aes* aes, byte* out,
409 const byte* in, word32 sz,
410 byte* ivOut, word32 ivOutSz,
411 byte* authTag, word32 authTagSz,
412 const byte* authIn, word32 authInSz);
413#endif /* HAVE_AESCCM */
414#ifdef HAVE_AES_KEYWRAP
415 WOLFSSL_API int wc_AesKeyWrap(const byte* key, word32 keySz,
416 const byte* in, word32 inSz,
417 byte* out, word32 outSz,
418 const byte* iv);
419 WOLFSSL_API int wc_AesKeyUnWrap(const byte* key, word32 keySz,
420 const byte* in, word32 inSz,
421 byte* out, word32 outSz,
422 const byte* iv);
423#endif /* HAVE_AES_KEYWRAP */
424
425#ifdef WOLFSSL_AES_XTS
426
427WOLFSSL_API int wc_AesXtsSetKey(XtsAes* aes, const byte* key,
428 word32 len, int dir, void* heap, int devId);
429
430WOLFSSL_API int wc_AesXtsEncryptSector(XtsAes* aes, byte* out,
431 const byte* in, word32 sz, word64 sector);
432
433WOLFSSL_API int wc_AesXtsDecryptSector(XtsAes* aes, byte* out,
434 const byte* in, word32 sz, word64 sector);
435
436WOLFSSL_API int wc_AesXtsEncrypt(XtsAes* aes, byte* out,
437 const byte* in, word32 sz, const byte* i, word32 iSz);
438
439WOLFSSL_API int wc_AesXtsDecrypt(XtsAes* aes, byte* out,
440 const byte* in, word32 sz, const byte* i, word32 iSz);
441
442WOLFSSL_API int wc_AesXtsFree(XtsAes* aes);
443#endif
444
445WOLFSSL_API int wc_AesGetKeySize(Aes* aes, word32* keySize);
446
447WOLFSSL_API int wc_AesInit(Aes* aes, void* heap, int devId);
448#ifdef HAVE_PKCS11
449WOLFSSL_API int wc_AesInit_Id(Aes* aes, unsigned char* id, int len, void* heap,
450 int devId);
451WOLFSSL_API int wc_AesInit_Label(Aes* aes, const char* label, void* heap,
452 int devId);
453#endif
454WOLFSSL_API void wc_AesFree(Aes* aes);
455
456#ifdef __cplusplus
457 } /* extern "C" */
458#endif
459
460
461#endif /* NO_AES */
462#endif /* WOLF_CRYPT_AES_H */
Note: See TracBrowser for help on using the repository browser.