]> begriffs open source - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/wolfssl/wolfcrypt/aes.h
Update WolfSSL library to the latest version.
[freertos] / FreeRTOS-Plus / Source / WolfSSL / wolfssl / wolfcrypt / aes.h
1 /* aes.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 #ifndef WOLF_CRYPT_AES_H
23 #define WOLF_CRYPT_AES_H
24
25 #include <wolfssl/wolfcrypt/types.h>
26
27 #ifndef NO_AES
28
29 /* included for fips @wc_fips */
30 #ifdef HAVE_FIPS
31 #include <cyassl/ctaocrypt/aes.h>
32 #if defined(CYASSL_AES_COUNTER) && !defined(WOLFSSL_AES_COUNTER)
33     #define WOLFSSL_AES_COUNTER
34 #endif
35 #if !defined(WOLFSSL_AES_DIRECT) && defined(CYASSL_AES_DIRECT)
36     #define WOLFSSL_AES_DIRECT
37 #endif
38 #endif
39
40 #ifndef HAVE_FIPS /* to avoid redefinition of macros */
41 #ifdef HAVE_CAVIUM
42     #include <wolfssl/ctaocrypt/logging.h>
43     #include "cavium_common.h"
44 #endif
45
46 #ifdef WOLFSSL_AESNI
47
48 #include <wmmintrin.h>
49
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))
57     #else
58         #define ALIGN16
59     #endif
60 #endif
61
62 #endif /* WOLFSSL_AESNI */
63
64 #if !defined (ALIGN16)
65     #define ALIGN16
66 #endif
67 #endif /* HAVE_FIPS */
68
69 #ifdef __cplusplus
70     extern "C" {
71 #endif
72
73 #ifndef HAVE_FIPS /* to avoid redefinition of structures */
74 #define WOLFSSL_AES_CAVIUM_MAGIC 0xBEEF0002
75
76 enum {
77     AES_ENC_TYPE   = 1,   /* cipher unique type */
78     AES_ENCRYPTION = 0,
79     AES_DECRYPTION = 1,
80     AES_BLOCK_SIZE = 16
81 };
82
83
84 typedef struct Aes {
85     /* AESNI needs key first, rounds 2nd, not sure why yet */
86     ALIGN16 word32 key[60];
87     word32  rounds;
88
89     ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)];      /* for CBC mode */
90     ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)];      /* same         */
91
92 #ifdef HAVE_AESGCM
93     ALIGN16 byte H[AES_BLOCK_SIZE];
94 #ifdef GCM_TABLE
95     /* key-based fast multiplication table. */
96     ALIGN16 byte M0[256][AES_BLOCK_SIZE];
97 #endif /* GCM_TABLE */
98 #endif /* HAVE_AESGCM */
99 #ifdef WOLFSSL_AESNI
100     byte use_aesni;
101 #endif /* WOLFSSL_AESNI */
102 #ifdef HAVE_CAVIUM
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 */
107 #endif
108 #ifdef WOLFSSL_AES_COUNTER
109     word32  left;            /* unsued bytes left from last call */
110 #endif
111 #ifdef WOLFSSL_PIC32MZ_CRYPT
112     word32 key_ce[AES_BLOCK_SIZE*2/sizeof(word32)] ;
113     word32 iv_ce [AES_BLOCK_SIZE  /sizeof(word32)] ;
114     int    keylen ;
115 #endif
116 #ifdef WOLFSSL_TI_CRYPT
117     int    keylen ;
118 #endif
119 } Aes;
120
121
122 #ifdef HAVE_AESGCM
123 typedef struct Gmac {
124     Aes aes;
125 } Gmac;
126 #endif /* HAVE_AESGCM */
127 #endif /* HAVE_FIPS */
128
129  WOLFSSL_API int  wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
130                           int dir);
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);
136
137 /* AES-CTR */
138 #ifdef WOLFSSL_AES_COUNTER
139  WOLFSSL_API void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz);
140 #endif
141 /* AES-DIRECT */
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);
147 #endif
148 #ifdef HAVE_AESGCM
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);
158
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 */
164 #ifdef HAVE_AESCCM
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 */
175
176 #ifdef HAVE_CAVIUM
177      WOLFSSL_API int  wc_AesInitCavium(Aes*, int);
178      WOLFSSL_API void wc_AesFreeCavium(Aes*);
179 #endif
180
181 #ifdef __cplusplus
182     } /* extern "C" */
183 #endif
184
185
186 #endif /* NO_AES */
187 #endif /* WOLF_CRYPT_AES_H */
188