3 * Copyright (C) 2006-2015 wolfSSL Inc.
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
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.
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.
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
22 #ifndef WOLF_CRYPT_AES_H
23 #define WOLF_CRYPT_AES_H
25 #include <wolfssl/wolfcrypt/types.h>
29 /* included for fips @wc_fips */
31 #include <cyassl/ctaocrypt/aes.h>
32 #if defined(CYASSL_AES_COUNTER) && !defined(WOLFSSL_AES_COUNTER)
33 #define WOLFSSL_AES_COUNTER
35 #if !defined(WOLFSSL_AES_DIRECT) && defined(CYASSL_AES_DIRECT)
36 #define WOLFSSL_AES_DIRECT
40 #ifndef HAVE_FIPS /* to avoid redefinition of macros */
42 #include <wolfssl/ctaocrypt/logging.h>
43 #include "cavium_common.h"
48 #include <wmmintrin.h>
50 #if !defined (ALIGN16)
51 #if defined (__GNUC__)
52 #define ALIGN16 __attribute__ ( (aligned (16)))
53 #elif defined(_MSC_VER)
54 /* disable align warning, we want alignment ! */
55 #pragma warning(disable: 4324)
56 #define ALIGN16 __declspec (align (16))
62 #endif /* WOLFSSL_AESNI */
64 #if !defined (ALIGN16)
67 #endif /* HAVE_FIPS */
73 #ifndef HAVE_FIPS /* to avoid redefinition of structures */
74 #define WOLFSSL_AES_CAVIUM_MAGIC 0xBEEF0002
77 AES_ENC_TYPE = 1, /* cipher unique type */
85 /* AESNI needs key first, rounds 2nd, not sure why yet */
86 ALIGN16 word32 key[60];
89 ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
90 ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)]; /* same */
93 ALIGN16 byte H[AES_BLOCK_SIZE];
95 /* key-based fast multiplication table. */
96 ALIGN16 byte M0[256][AES_BLOCK_SIZE];
97 #endif /* GCM_TABLE */
98 #endif /* HAVE_AESGCM */
101 #endif /* WOLFSSL_AESNI */
103 AesType type; /* aes key type */
104 int devId; /* nitrox device id */
105 word32 magic; /* using cavium magic */
106 word64 contextHandle; /* nitrox context memory handle */
108 #ifdef WOLFSSL_AES_COUNTER
109 word32 left; /* unsued bytes left from last call */
111 #ifdef WOLFSSL_PIC32MZ_CRYPT
112 word32 key_ce[AES_BLOCK_SIZE*2/sizeof(word32)] ;
113 word32 iv_ce [AES_BLOCK_SIZE /sizeof(word32)] ;
116 #ifdef WOLFSSL_TI_CRYPT
123 typedef struct Gmac {
126 #endif /* HAVE_AESGCM */
127 #endif /* HAVE_FIPS */
129 WOLFSSL_API int wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
131 WOLFSSL_API int wc_AesSetIV(Aes* aes, const byte* iv);
132 WOLFSSL_API int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz);
133 WOLFSSL_API int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz);
134 WOLFSSL_API int wc_AesCbcDecryptWithKey(byte* out, const byte* in, word32 inSz,
135 const byte* key, word32 keySz, const byte* iv);
138 #ifdef WOLFSSL_AES_COUNTER
139 WOLFSSL_API void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz);
142 #if defined(WOLFSSL_AES_DIRECT)
143 WOLFSSL_API void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in);
144 WOLFSSL_API void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in);
145 WOLFSSL_API int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
146 const byte* iv, int dir);
149 WOLFSSL_API int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len);
150 WOLFSSL_API int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
151 const byte* iv, word32 ivSz,
152 byte* authTag, word32 authTagSz,
153 const byte* authIn, word32 authInSz);
154 WOLFSSL_API int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
155 const byte* iv, word32 ivSz,
156 const byte* authTag, word32 authTagSz,
157 const byte* authIn, word32 authInSz);
159 WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len);
160 WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
161 const byte* authIn, word32 authInSz,
162 byte* authTag, word32 authTagSz);
163 #endif /* HAVE_AESGCM */
165 WOLFSSL_API void wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
166 WOLFSSL_API void wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
167 const byte* nonce, word32 nonceSz,
168 byte* authTag, word32 authTagSz,
169 const byte* authIn, word32 authInSz);
170 WOLFSSL_API int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
171 const byte* nonce, word32 nonceSz,
172 const byte* authTag, word32 authTagSz,
173 const byte* authIn, word32 authInSz);
174 #endif /* HAVE_AESCCM */
177 WOLFSSL_API int wc_AesInitCavium(Aes*, int);
178 WOLFSSL_API void wc_AesFreeCavium(Aes*);
187 #endif /* WOLF_CRYPT_AES_H */