source: asp3_tinet_ecnl_arm/trunk/wolfssl-3.12.2/wolfssl/wolfcrypt/aes.h@ 352

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

arm向けASP3版ECNLを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 13.4 KB
Line 
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#ifndef WOLF_CRYPT_AES_H
24#define WOLF_CRYPT_AES_H
25
26#include <wolfssl/wolfcrypt/types.h>
27
28#ifndef NO_AES
29
30/* included for fips @wc_fips */
31#ifdef HAVE_FIPS
32#include <cyassl/ctaocrypt/aes.h>
33#if defined(CYASSL_AES_COUNTER) && !defined(WOLFSSL_AES_COUNTER)
34 #define WOLFSSL_AES_COUNTER
35#endif
36#if !defined(WOLFSSL_AES_DIRECT) && defined(CYASSL_AES_DIRECT)
37 #define WOLFSSL_AES_DIRECT
38#endif
39#endif
40
41#ifndef HAVE_FIPS /* to avoid redefinition of macros */
42
43#ifdef WOLFSSL_AESNI
44
45#include <wmmintrin.h>
46#include <emmintrin.h>
47#include <smmintrin.h>
48
49#endif /* WOLFSSL_AESNI */
50
51#ifdef WOLFSSL_XILINX_CRYPT
52#include "xsecure_aes.h"
53#endif
54
55#endif /* HAVE_FIPS */
56
57#ifdef __cplusplus
58 extern "C" {
59#endif
60
61#ifndef HAVE_FIPS /* to avoid redefinition of structures */
62
63#ifdef WOLFSSL_ASYNC_CRYPT
64 #include <wolfssl/wolfcrypt/async.h>
65#endif
66
67enum {
68 AES_ENC_TYPE = 1, /* cipher unique type */
69 AES_ENCRYPTION = 0,
70 AES_DECRYPTION = 1,
71 KEYWRAP_BLOCK_SIZE = 8,
72 AES_BLOCK_SIZE = 16
73};
74
75
76typedef struct Aes {
77 /* AESNI needs key first, rounds 2nd, not sure why yet */
78 ALIGN16 word32 key[60];
79 word32 rounds;
80 int keylen;
81
82 ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
83 ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)]; /* same */
84
85#ifdef HAVE_AESGCM
86 ALIGN16 byte H[AES_BLOCK_SIZE];
87#ifdef GCM_TABLE
88 /* key-based fast multiplication table. */
89 ALIGN16 byte M0[256][AES_BLOCK_SIZE];
90#endif /* GCM_TABLE */
91#endif /* HAVE_AESGCM */
92#ifdef WOLFSSL_AESNI
93 byte use_aesni;
94#endif /* WOLFSSL_AESNI */
95#ifdef WOLFSSL_ASYNC_CRYPT
96 word32 asyncKey[AES_MAX_KEY_SIZE/8/sizeof(word32)]; /* raw key */
97 word32 asyncIv[AES_BLOCK_SIZE/sizeof(word32)]; /* raw IV */
98 WC_ASYNC_DEV asyncDev;
99#endif /* WOLFSSL_ASYNC_CRYPT */
100#ifdef WOLFSSL_AES_COUNTER
101 word32 left; /* unused bytes left from last call */
102#endif
103#ifdef WOLFSSL_XILINX_CRYPT
104 XSecure_Aes xilAes;
105 XCsuDma dma;
106 word32 key_init[8];
107 word32 kup;
108#endif
109 void* heap; /* memory hint to use */
110} Aes;
111
112#ifdef WOLFSSL_AES_XTS
113typedef struct XtsAes {
114 Aes aes;
115 Aes tweak;
116} XtsAes;
117#endif
118
119#ifdef HAVE_AESGCM
120typedef struct Gmac {
121 Aes aes;
122} Gmac;
123#endif /* HAVE_AESGCM */
124#endif /* HAVE_FIPS */
125
126
127/* Authenticate cipher function prototypes */
128typedef int (*wc_AesAuthEncryptFunc)(Aes* aes, byte* out,
129 const byte* in, word32 sz,
130 const byte* iv, word32 ivSz,
131 byte* authTag, word32 authTagSz,
132 const byte* authIn, word32 authInSz);
133typedef int (*wc_AesAuthDecryptFunc)(Aes* aes, byte* out,
134 const byte* in, word32 sz,
135 const byte* iv, word32 ivSz,
136 const byte* authTag, word32 authTagSz,
137 const byte* authIn, word32 authInSz);
138
139/* AES-CBC */
140WOLFSSL_API int wc_AesSetKey(Aes* aes, const byte* key, word32 len,
141 const byte* iv, int dir);
142WOLFSSL_API int wc_AesSetIV(Aes* aes, const byte* iv);
143WOLFSSL_API int wc_AesCbcEncrypt(Aes* aes, byte* out,
144 const byte* in, word32 sz);
145WOLFSSL_API int wc_AesCbcDecrypt(Aes* aes, byte* out,
146 const byte* in, word32 sz);
147
148#ifdef HAVE_AES_ECB
149WOLFSSL_API int wc_AesEcbEncrypt(Aes* aes, byte* out,
150 const byte* in, word32 sz);
151WOLFSSL_API int wc_AesEcbDecrypt(Aes* aes, byte* out,
152 const byte* in, word32 sz);
153#endif
154
155/* AES-CTR */
156#ifdef WOLFSSL_AES_COUNTER
157 WOLFSSL_API int wc_AesCtrEncrypt(Aes* aes, byte* out,
158 const byte* in, word32 sz);
159#endif
160/* AES-DIRECT */
161#if defined(WOLFSSL_AES_DIRECT)
162 WOLFSSL_API void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in);
163 WOLFSSL_API void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in);
164 WOLFSSL_API int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
165 const byte* iv, int dir);
166#endif
167#ifdef HAVE_AESGCM
168#ifdef WOLFSSL_XILINX_CRYPT
169 WOLFSSL_API int wc_AesGcmSetKey_ex(Aes* aes, const byte* key, word32 len,
170 word32 kup);
171#endif
172 WOLFSSL_API int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len);
173 WOLFSSL_API int wc_AesGcmEncrypt(Aes* aes, byte* out,
174 const byte* in, word32 sz,
175 const byte* iv, word32 ivSz,
176 byte* authTag, word32 authTagSz,
177 const byte* authIn, word32 authInSz);
178 WOLFSSL_API int wc_AesGcmDecrypt(Aes* aes, byte* out,
179 const byte* in, word32 sz,
180 const byte* iv, word32 ivSz,
181 const byte* authTag, word32 authTagSz,
182 const byte* authIn, word32 authInSz);
183
184 WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len);
185 WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
186 const byte* authIn, word32 authInSz,
187 byte* authTag, word32 authTagSz);
188 WOLFSSL_LOCAL void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
189 word32 cSz, byte* s, word32 sSz);
190#endif /* HAVE_AESGCM */
191#ifdef HAVE_AESCCM
192 WOLFSSL_API int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
193 WOLFSSL_API int wc_AesCcmEncrypt(Aes* aes, byte* out,
194 const byte* in, word32 inSz,
195 const byte* nonce, word32 nonceSz,
196 byte* authTag, word32 authTagSz,
197 const byte* authIn, word32 authInSz);
198 WOLFSSL_API int wc_AesCcmDecrypt(Aes* aes, byte* out,
199 const byte* in, word32 inSz,
200 const byte* nonce, word32 nonceSz,
201 const byte* authTag, word32 authTagSz,
202 const byte* authIn, word32 authInSz);
203#endif /* HAVE_AESCCM */
204#ifdef HAVE_AES_KEYWRAP
205 WOLFSSL_API int wc_AesKeyWrap(const byte* key, word32 keySz,
206 const byte* in, word32 inSz,
207 byte* out, word32 outSz,
208 const byte* iv);
209 WOLFSSL_API int wc_AesKeyUnWrap(const byte* key, word32 keySz,
210 const byte* in, word32 inSz,
211 byte* out, word32 outSz,
212 const byte* iv);
213#endif /* HAVE_AES_KEYWRAP */
214
215#ifdef WOLFSSL_AES_XTS
216/*!
217 \ingroup AES
218
219 \brief This is to help with setting keys to correct encrypt or decrypt type.
220
221 \note Is up to user to call wc_AesXtsFree on aes key when done.
222
223 \return 0 Success
224
225 \param aes AES keys for encrypt/decrypt process
226 \param key buffer holding aes key | tweak key
227 \param len length of key buffer in bytes. Should be twice that of key size.
228 i.e. 32 for a 16 byte key.
229 \param dir direction, either AES_ENCRYPTION or AES_DECRYPTION
230 \param heap heap hint to use for memory. Can be NULL
231 \param devId id to use with async crypto. Can be 0
232
233 _Example_
234 \code
235 XtsAes aes;
236
237 if(wc_AesXtsSetKey(&aes, key, sizeof(key), AES_ENCRYPTION, NULL, 0) != 0)
238 {
239 // Handle error
240 }
241 wc_AesXtsFree(&aes);
242 \endcode
243
244 \sa wc_AesXtsEncrypt
245 \sa wc_AesXtsDecrypt
246 \sa wc_AesXtsFree
247*/
248WOLFSSL_API int wc_AesXtsSetKey(XtsAes* aes, const byte* key,
249 word32 len, int dir, void* heap, int devId);
250
251
252/*!
253 \ingroup AES
254
255 \brief Same process as wc_AesXtsEncrypt but uses a word64 type as the tweak
256 value instead of a byte array. This just converts the word64 to a
257 byte array and calls wc_AesXtsEncrypt.
258
259 \return 0 Success
260
261 \param aes AES keys to use for block encrypt/decrypt
262 \param out output buffer to hold cipher text
263 \param in input plain text buffer to encrypt
264 \param sz size of both out and in buffers
265 \param sector value to use for tweak
266
267 _Example_
268 \code
269 XtsAes aes;
270 unsigned char plain[SIZE];
271 unsigned char cipher[SIZE];
272 word64 s = VALUE;
273
274 //set up keys with AES_ENCRYPTION as dir
275
276 if(wc_AesXtsEncryptSector(&aes, cipher, plain, SIZE, s) != 0)
277 {
278 // Handle error
279 }
280 wc_AesXtsFree(&aes);
281 \endcode
282
283 \sa wc_AesXtsEncrypt
284 \sa wc_AesXtsDecrypt
285 \sa wc_AesXtsSetKey
286 \sa wc_AesXtsFree
287*/
288WOLFSSL_API int wc_AesXtsEncryptSector(XtsAes* aes, byte* out,
289 const byte* in, word32 sz, word64 sector);
290
291
292/*!
293 \ingroup AES
294
295 \brief Same process as wc_AesXtsDecrypt but uses a word64 type as the tweak
296 value instead of a byte array. This just converts the word64 to a
297 byte array.
298
299 \return 0 Success
300
301 \param aes AES keys to use for block encrypt/decrypt
302 \param out output buffer to hold plain text
303 \param in input cipher text buffer to decrypt
304 \param sz size of both out and in buffers
305 \param sector value to use for tweak
306
307 _Example_
308 \code
309 XtsAes aes;
310 unsigned char plain[SIZE];
311 unsigned char cipher[SIZE];
312 word64 s = VALUE;
313
314 //set up aes key with AES_DECRYPTION as dir and tweak with AES_ENCRYPTION
315
316 if(wc_AesXtsDecryptSector(&aes, plain, cipher, SIZE, s) != 0)
317 {
318 // Handle error
319 }
320 wc_AesXtsFree(&aes);
321 \endcode
322
323 \sa wc_AesXtsEncrypt
324 \sa wc_AesXtsDecrypt
325 \sa wc_AesXtsSetKey
326 \sa wc_AesXtsFree
327*/
328WOLFSSL_API int wc_AesXtsDecryptSector(XtsAes* aes, byte* out,
329 const byte* in, word32 sz, word64 sector);
330
331
332/*!
333 \ingroup AES
334
335 \brief AES with XTS mode. (XTS) XEX encryption with Tweak and cipher text
336 Stealing.
337
338 \return 0 Success
339
340 \param aes AES keys to use for block encrypt/decrypt
341 \param out output buffer to hold cipher text
342 \param in input plain text buffer to encrypt
343 \param sz size of both out and in buffers
344 \param i value to use for tweak
345 \param iSz size of i buffer, should always be AES_BLOCK_SIZE but having
346 this input adds a sanity check on how the user calls the
347 function.
348
349 _Example_
350 \code
351 XtsAes aes;
352 unsigned char plain[SIZE];
353 unsigned char cipher[SIZE];
354 unsigned char i[AES_BLOCK_SIZE];
355
356 //set up key with AES_ENCRYPTION as dir
357
358 if(wc_AesXtsEncrypt(&aes, cipher, plain, SIZE, i, sizeof(i)) != 0)
359 {
360 // Handle error
361 }
362 wc_AesXtsFree(&aes);
363 \endcode
364
365 \sa wc_AesXtsDecrypt
366 \sa wc_AesXtsSetKey
367 \sa wc_AesXtsFree
368*/
369WOLFSSL_API int wc_AesXtsEncrypt(XtsAes* aes, byte* out,
370 const byte* in, word32 sz, const byte* i, word32 iSz);
371
372
373/*!
374 \ingroup AES
375
376 \brief Same process as encryption but Aes key is AES_DECRYPTION type.
377
378 \return 0 Success
379
380 \param aes AES keys to use for block encrypt/decrypt
381 \param out output buffer to hold plain text
382 \param in input cipher text buffer to decrypt
383 \param sz size of both out and in buffers
384 \param i value to use for tweak
385 \param iSz size of i buffer, should always be AES_BLOCK_SIZE but having
386 this input adds a sanity check on how the user calls the
387 function.
388 _Example_
389 \code
390 XtsAes aes;
391 unsigned char plain[SIZE];
392 unsigned char cipher[SIZE];
393 unsigned char i[AES_BLOCK_SIZE];
394
395 //set up key with AES_DECRYPTION as dir and tweak with AES_ENCRYPTION
396
397 if(wc_AesXtsDecrypt(&aes, plain, cipher, SIZE, i, sizeof(i)) != 0)
398 {
399 // Handle error
400 }
401 wc_AesXtsFree(&aes);
402 \endcode
403
404 \sa wc_AesXtsEncrypt
405 \sa wc_AesXtsSetKey
406 \sa wc_AesXtsFree
407*/
408WOLFSSL_API int wc_AesXtsDecrypt(XtsAes* aes, byte* out,
409 const byte* in, word32 sz, const byte* i, word32 iSz);
410
411
412/*!
413 \ingroup AES
414
415 \brief This is to free up any resources used by the XtsAes structure
416
417 \return 0 Success
418
419 \param aes AES keys to free
420
421 _Example_
422 \code
423 XtsAes aes;
424
425 if(wc_AesXtsSetKey(&aes, key, sizeof(key), AES_ENCRYPTION, NULL, 0) != 0)
426 {
427 // Handle error
428 }
429 wc_AesXtsFree(&aes);
430 \endcode
431
432 \sa wc_AesXtsEncrypt
433 \sa wc_AesXtsDecrypt
434 \sa wc_AesXtsSetKey
435*/
436WOLFSSL_API int wc_AesXtsFree(XtsAes* aes);
437#endif
438
439WOLFSSL_API int wc_AesGetKeySize(Aes* aes, word32* keySize);
440
441WOLFSSL_API int wc_AesInit(Aes*, void*, int);
442WOLFSSL_API void wc_AesFree(Aes*);
443
444#ifdef __cplusplus
445 } /* extern "C" */
446#endif
447
448
449#endif /* NO_AES */
450#endif /* WOLF_CRYPT_AES_H */
Note: See TracBrowser for help on using the repository browser.