27 #ifndef WOLF_CRYPT_AES_H 28 #define WOLF_CRYPT_AES_H 34 #if defined(HAVE_FIPS) && \ 35 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) 36 #include <wolfssl/wolfcrypt/fips.h> 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 46 #if !defined(WOLFSSL_AES_DIRECT) && defined(CYASSL_AES_DIRECT) 47 #define WOLFSSL_AES_DIRECT 55 #include <wolfssl/wolfcrypt/port/st/stm32.h> 60 #include <wmmintrin.h> 61 #include <emmintrin.h> 62 #include <smmintrin.h> 67 #ifdef WOLFSSL_XILINX_CRYPT 68 #include "xsecure_aes.h" 71 #if defined(WOLFSSL_AFALG) || defined(WOLFSSL_AFALG_XILINX_AES) 73 #include <wolfssl/wolfcrypt/port/af_alg/wc_afalg.h> 76 #if defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC) 77 #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h> 80 #if defined(HAVE_AESGCM) && !defined(WC_NO_RNG) 84 #if defined(WOLFSSL_CRYPTOCELL) 85 #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h> 88 #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \ 89 defined(WOLFSSL_RENESAS_TSIP_TLS_AES_CRYPT) 90 #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h> 97 #ifndef WOLFSSL_AES_KEY_SIZE_ENUM 98 #define WOLFSSL_AES_KEY_SIZE_ENUM 101 AES_128_KEY_SIZE = 16,
102 AES_192_KEY_SIZE = 24,
103 AES_256_KEY_SIZE = 32,
110 #if !defined(HAVE_FIPS) || \ 111 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) 113 #ifdef WOLFSSL_ASYNC_CRYPT 114 #include <wolfssl/wolfcrypt/async.h> 118 AES_ENC_TYPE = WC_CIPHER_AES,
124 KEYWRAP_BLOCK_SIZE = 8,
126 GCM_NONCE_MAX_SZ = 16,
127 GCM_NONCE_MID_SZ = 12,
128 GCM_NONCE_MIN_SZ = 8,
129 CCM_NONCE_MIN_SZ = 7,
130 CCM_NONCE_MAX_SZ = 13,
133 #ifdef WOLFSSL_AES_CFB 136 #ifdef WOLFSSL_AES_OFB 139 #ifdef WOLFSSL_AES_XTS 151 ALIGN16 word32 key[60];
155 ALIGN16 word32 reg[AES_BLOCK_SIZE /
sizeof(word32)];
156 ALIGN16 word32 tmp[AES_BLOCK_SIZE /
sizeof(word32)];
158 #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM) 163 ALIGN16 byte H[AES_BLOCK_SIZE];
171 ALIGN16 byte M0[256][AES_BLOCK_SIZE];
173 #ifdef HAVE_CAVIUM_OCTEON_SYNC 180 #ifdef WOLF_CRYPTO_CB 185 byte
id[AES_MAX_ID_LEN];
188 #ifdef WOLFSSL_ASYNC_CRYPT 189 WC_ASYNC_DEV asyncDev;
191 #if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB) || \ 192 defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_XTS) 195 #ifdef WOLFSSL_XILINX_CRYPT 201 #if defined(WOLFSSL_AFALG) || defined(WOLFSSL_AFALG_XILINX_AES) 206 #ifdef WOLFSSL_AFALG_XILINX_AES 207 word32 msgBuf[CMSG_SPACE(4) + CMSG_SPACE(
sizeof(
struct af_alg_iv) +
211 #if defined(WOLF_CRYPTO_CB) || (defined(WOLFSSL_DEVCRYPTO) && \ 212 (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))) || \ 213 (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)) 214 word32 devKey[AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE/
sizeof(word32)];
215 #ifdef HAVE_CAVIUM_OCTEON_SYNC 219 #if defined(WOLFSSL_DEVCRYPTO) && \ 220 (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC)) 223 #if defined(WOLFSSL_CRYPTOCELL) 226 #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \ 227 defined(WOLFSSL_RENESAS_TSIP_TLS_AES_CRYPT) 233 #ifndef WC_AES_TYPE_DEFINED 235 #define WC_AES_TYPE_DEFINED 238 #ifdef WOLFSSL_AES_XTS 254 typedef int (*wc_AesAuthEncryptFunc)(
Aes* aes, byte* out,
255 const byte* in, word32 sz,
256 const byte* iv, word32 ivSz,
257 byte* authTag, word32 authTagSz,
258 const byte* authIn, word32 authInSz);
259 typedef int (*wc_AesAuthDecryptFunc)(
Aes* aes, byte* out,
260 const byte* in, word32 sz,
261 const byte* iv, word32 ivSz,
262 const byte* authTag, word32 authTagSz,
263 const byte* authIn, word32 authInSz);
267 const byte* iv,
int dir);
272 const byte* in, word32 sz);
274 const byte* in, word32 sz);
277 #ifdef WOLFSSL_AES_CFB 278 WOLFSSL_API
int wc_AesCfbEncrypt(
Aes* aes, byte* out,
279 const byte* in, word32 sz);
280 WOLFSSL_API
int wc_AesCfb1Encrypt(
Aes* aes, byte* out,
281 const byte* in, word32 sz);
282 WOLFSSL_API
int wc_AesCfb8Encrypt(
Aes* aes, byte* out,
283 const byte* in, word32 sz);
284 #ifdef HAVE_AES_DECRYPT 285 WOLFSSL_API
int wc_AesCfbDecrypt(
Aes* aes, byte* out,
286 const byte* in, word32 sz);
287 WOLFSSL_API
int wc_AesCfb1Decrypt(
Aes* aes, byte* out,
288 const byte* in, word32 sz);
289 WOLFSSL_API
int wc_AesCfb8Decrypt(
Aes* aes, byte* out,
290 const byte* in, word32 sz);
294 #ifdef WOLFSSL_AES_OFB 295 WOLFSSL_API
int wc_AesOfbEncrypt(
Aes* aes, byte* out,
296 const byte* in, word32 sz);
297 #ifdef HAVE_AES_DECRYPT 298 WOLFSSL_API
int wc_AesOfbDecrypt(
Aes* aes, byte* out,
299 const byte* in, word32 sz);
304 WOLFSSL_API
int wc_AesEcbEncrypt(
Aes* aes, byte* out,
305 const byte* in, word32 sz);
306 WOLFSSL_API
int wc_AesEcbDecrypt(
Aes* aes, byte* out,
307 const byte* in, word32 sz);
311 #ifdef WOLFSSL_AES_COUNTER 313 const byte* in, word32 sz);
316 #if defined(WOLFSSL_AES_DIRECT) 320 const byte* iv,
int dir);
324 #ifdef WOLFSSL_XILINX_CRYPT 325 WOLFSSL_API
int wc_AesGcmSetKey_ex(
Aes* aes,
const byte* key, word32 len,
327 #elif defined(WOLFSSL_AFALG_XILINX_AES) 328 WOLFSSL_LOCAL
int wc_AesGcmSetKey_ex(
Aes* aes,
const byte* key, word32 len,
333 const byte* in, word32 sz,
334 const byte* iv, word32 ivSz,
335 byte* authTag, word32 authTagSz,
336 const byte* authIn, word32 authInSz);
338 const byte* in, word32 sz,
339 const byte* iv, word32 ivSz,
340 const byte* authTag, word32 authTagSz,
341 const byte* authIn, word32 authInSz);
344 WOLFSSL_API
int wc_AesGcmSetExtIV(
Aes* aes,
const byte* iv, word32 ivSz);
345 WOLFSSL_API
int wc_AesGcmSetIV(
Aes* aes, word32 ivSz,
346 const byte* ivFixed, word32 ivFixedSz,
348 WOLFSSL_API
int wc_AesGcmEncrypt_ex(
Aes* aes, byte* out,
349 const byte* in, word32 sz,
350 byte* ivOut, word32 ivOutSz,
351 byte* authTag, word32 authTagSz,
352 const byte* authIn, word32 authInSz);
357 const byte* authIn, word32 authInSz,
358 byte* authTag, word32 authTagSz);
360 WOLFSSL_API
int wc_Gmac(
const byte* key, word32 keySz, byte* iv, word32 ivSz,
361 const byte* authIn, word32 authInSz,
362 byte* authTag, word32 authTagSz,
WC_RNG* rng);
363 WOLFSSL_API
int wc_GmacVerify(
const byte* key, word32 keySz,
364 const byte* iv, word32 ivSz,
365 const byte* authIn, word32 authInSz,
366 const byte* authTag, word32 authTagSz);
368 WOLFSSL_LOCAL
void GHASH(
Aes* aes,
const byte* a, word32 aSz,
const byte* c,
369 word32 cSz, byte* s, word32 sSz);
374 const byte* in, word32 inSz,
375 const byte* nonce, word32 nonceSz,
376 byte* authTag, word32 authTagSz,
377 const byte* authIn, word32 authInSz);
379 const byte* in, word32 inSz,
380 const byte* nonce, word32 nonceSz,
381 const byte* authTag, word32 authTagSz,
382 const byte* authIn, word32 authInSz);
383 WOLFSSL_API
int wc_AesCcmSetNonce(
Aes* aes,
384 const byte* nonce, word32 nonceSz);
385 WOLFSSL_API
int wc_AesCcmEncrypt_ex(
Aes* aes, byte* out,
386 const byte* in, word32 sz,
387 byte* ivOut, word32 ivOutSz,
388 byte* authTag, word32 authTagSz,
389 const byte* authIn, word32 authInSz);
391 #ifdef HAVE_AES_KEYWRAP 392 WOLFSSL_API
int wc_AesKeyWrap(
const byte* key, word32 keySz,
393 const byte* in, word32 inSz,
394 byte* out, word32 outSz,
396 WOLFSSL_API
int wc_AesKeyUnWrap(
const byte* key, word32 keySz,
397 const byte* in, word32 inSz,
398 byte* out, word32 outSz,
402 #ifdef WOLFSSL_AES_XTS 405 word32 len,
int dir,
void* heap,
int devId);
408 const byte* in, word32 sz, word64 sector);
411 const byte* in, word32 sz, word64 sector);
414 const byte* in, word32 sz,
const byte* i, word32 iSz);
417 const byte* in, word32 sz,
const byte* i, word32 iSz);
422 WOLFSSL_API
int wc_AesGetKeySize(
Aes* aes, word32* keySize);
426 WOLFSSL_API
int wc_AesInit_Id(
Aes* aes,
unsigned char*
id,
int len,
void* heap,
429 WOLFSSL_API
void wc_AesFree(
Aes* aes);
Definition: renesas-tsip-crypt.h:131
WOLFSSL_API int wc_AesCbcDecrypt(Aes *aes, byte *out, const byte *in, word32 sz)
Decrypts a cipher from the input buffer in, and places the resulting plain text in the output buffer ...
Definition: aes.c:3008
WOLFSSL_API void wc_AesEncryptDirect(Aes *aes, byte *out, const byte *in)
This function is a one-block encrypt of the input block, in, into the output block, out. It uses the key and iv (initialization vector) of the provided AES structure, which should be initialized with wc_AesSetKey before calling this function. It is only enabled if the configure option WOLFSSL_AES_DIRECT is enabled. Warning: In nearly all use cases ECB mode is considered to be less secure. Please avoid using ECB API’s directly whenever possible.
Definition: aes.c:2860
WOLFSSL_API int wc_AesCcmEncrypt(Aes *aes, byte *out, const byte *in, word32 inSz, const byte *nonce, word32 nonceSz, byte *authTag, word32 authTagSz, const byte *authIn, word32 authInSz)
This function encrypts the input message, in, into the output buffer, out, using CCM (Counter with CB...
Definition: aes.c:6985
Definition: wc_devcrypto.h:35
WOLFSSL_API int wc_AesXtsEncrypt(XtsAes *aes, byte *out, const byte *in, word32 sz, const byte *i, word32 iSz)
AES with XTS mode. (XTS) XEX encryption with Tweak and cipher text Stealing.
Definition: aes.c:8465
WOLFSSL_API int wc_AesXtsDecrypt(XtsAes *aes, byte *out, const byte *in, word32 sz, const byte *i, word32 iSz)
Same process as encryption but Aes key is AES_DECRYPTION type.
Definition: aes.c:8573
WOLFSSL_API int wc_AesXtsEncryptSector(XtsAes *aes, byte *out, const byte *in, word32 sz, word64 sector)
Same process as wc_AesXtsEncrypt but uses a word64 type as the tweak value instead of a byte array...
Definition: aes.c:8368
WOLFSSL_API int wc_GmacSetKey(Gmac *gmac, const byte *key, word32 len)
This function initializes and sets the key for a GMAC object to be used for Galois Message Authentica...
Definition: aes.c:6943
WOLFSSL_API int wc_AesCbcEncrypt(Aes *aes, byte *out, const byte *in, word32 sz)
Encrypts a plaintext message from the input buffer in, and places the resulting cipher text in the ou...
Definition: aes.c:2951
Definition: cryptoCell.h:44
WOLFSSL_API int wc_AesInit(Aes *, void *, int)
Initialize Aes structure. Sets heap hint to be used and ID for use with async hardware.
Definition: aes.c:7447
WOLFSSL_API int wc_AesXtsFree(XtsAes *aes)
This is to free up any resources used by the XtsAes structure.
Definition: aes.c:8345
WOLFSSL_API int wc_AesXtsSetKey(XtsAes *aes, const byte *key, word32 len, int dir, void *heap, int devId)
This is to help with setting keys to correct encrypt or decrypt type. It is up to user to call wc_Aes...
Definition: aes.c:8304
WOLFSSL_API int wc_AesCcmSetKey(Aes *aes, const byte *key, word32 keySz)
This function sets the key for an AES object using CCM (Counter with CBC-MAC). It takes a pointer to ...
Definition: aes.c:6965
WOLFSSL_API int wc_AesCcmDecrypt(Aes *aes, byte *out, const byte *in, word32 inSz, const byte *nonce, word32 nonceSz, const byte *authTag, word32 authTagSz, const byte *authIn, word32 authInSz)
This function decrypts the input cipher text, in, into the output buffer, out, using CCM (Counter wit...
Definition: aes.c:7013
WOLFSSL_API int wc_AesSetIV(Aes *aes, const byte *iv)
This function sets the initialization vector for a particular AES object. The AES object should be in...
Definition: aes.c:2840
WOLFSSL_API int wc_GmacUpdate(Gmac *gmac, const byte *iv, word32 ivSz, const byte *authIn, word32 authInSz, byte *authTag, word32 authTagSz)
This function generates the Gmac hash of the authIn input and stores the result in the authTag buffer...
Definition: aes.c:6952
WOLFSSL_API int wc_AesGcmSetKey(Aes *aes, const byte *key, word32 len)
This function is used to set the key for AES GCM (Galois/Counter Mode). It initializes an AES object ...
Definition: aes.c:4118
WOLFSSL_API int wc_AesGcmDecrypt(Aes *aes, byte *out, const byte *in, word32 sz, const byte *iv, word32 ivSz, const byte *authTag, word32 authTagSz, const byte *authIn, word32 authInSz)
This function decrypts the input cipher text, held in the buffer in, and stores the resulting message...
Definition: aes.c:6319
WOLFSSL_API int wc_AesSetKey(Aes *aes, const byte *key, word32 len, const byte *iv, int dir)
This function initializes an AES structure by setting the key and then setting the initialization vec...
Definition: aes.c:2178
WOLFSSL_API void wc_AesDecryptDirect(Aes *aes, byte *out, const byte *in)
This function is a one-block decrypt of the input block, in, into the output block, out. It uses the key and iv (initialization vector) of the provided AES structure, which should be initialized with wc_AesSetKey before calling this function. It is only enabled if the configure option WOLFSSL_AES_DIRECT is enabled, and there is support for direct AES encryption on the system in question. Warning: In nearly all use cases ECB mode is considered to be less secure. Please avoid using ECB API’s directly whenever possible.
Definition: aes.c:2873
WOLFSSL_API int wc_AesGcmEncrypt(Aes *aes, byte *out, const byte *in, word32 sz, const byte *iv, word32 ivSz, byte *authTag, word32 authTagSz, const byte *authIn, word32 authInSz)
This function encrypts the input message, held in the buffer in, and stores the resulting cipher text...
Definition: aes.c:5885
WOLFSSL_API int wc_AesCtrEncrypt(Aes *aes, byte *out, const byte *in, word32 sz)
Encrypts/Decrypts a message from the input buffer in, and places the resulting cipher text in the out...
Definition: aes.c:3859
WOLFSSL_API int wc_AesSetKeyDirect(Aes *aes, const byte *key, word32 len, const byte *iv, int dir)
This function is used to set the AES keys for CTR mode with AES. It initializes an AES object with th...
Definition: aes.c:2208
WOLFSSL_API int wc_AesXtsDecryptSector(XtsAes *aes, byte *out, const byte *in, word32 sz, word64 sector)
Same process as wc_AesXtsDecrypt but uses a word64 type as the tweak value instead of a byte array...
Definition: aes.c:8396