wolf SSL / wolfSSL-TLS13-Beta

Fork of wolfSSL by wolf SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers aes.h Source File

aes.h

00001 /* aes.h
00002  *
00003  * Copyright (C) 2006-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 #ifndef WOLF_CRYPT_AES_H
00024 #define WOLF_CRYPT_AES_H
00025 
00026 #include <wolfssl/wolfcrypt/types.h>
00027 
00028 #ifndef NO_AES
00029 
00030 /* included for fips @wc_fips */
00031 #ifdef HAVE_FIPS
00032 #include <cyassl/ctaocrypt/aes.h>
00033 #if defined(CYASSL_AES_COUNTER) && !defined(WOLFSSL_AES_COUNTER)
00034     #define WOLFSSL_AES_COUNTER
00035 #endif
00036 #if !defined(WOLFSSL_AES_DIRECT) && defined(CYASSL_AES_DIRECT)
00037     #define WOLFSSL_AES_DIRECT
00038 #endif
00039 #endif
00040 
00041 #ifndef HAVE_FIPS /* to avoid redefinition of macros */
00042 
00043 #ifdef WOLFSSL_AESNI
00044 
00045 #include <wmmintrin.h>
00046 #include <emmintrin.h>
00047 #include <smmintrin.h>
00048 
00049 #endif /* WOLFSSL_AESNI */
00050 
00051 #endif /* HAVE_FIPS */
00052 
00053 #ifdef __cplusplus
00054     extern "C" {
00055 #endif
00056 
00057 #ifndef HAVE_FIPS /* to avoid redefinition of structures */
00058 
00059 #ifdef WOLFSSL_ASYNC_CRYPT
00060     #include <wolfssl/wolfcrypt/async.h>
00061 #endif
00062 
00063 enum {
00064     AES_ENC_TYPE   = 1,   /* cipher unique type */
00065     AES_ENCRYPTION = 0,
00066     AES_DECRYPTION = 1,
00067     KEYWRAP_BLOCK_SIZE = 8,
00068     AES_BLOCK_SIZE = 16
00069 };
00070 
00071 
00072 typedef struct Aes {
00073     /* AESNI needs key first, rounds 2nd, not sure why yet */
00074     ALIGN16 word32 key[60];
00075     word32  rounds;
00076     int     keylen;
00077 
00078     ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)];      /* for CBC mode */
00079     ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)];      /* same         */
00080 
00081 #ifdef HAVE_AESGCM
00082     ALIGN16 byte H[AES_BLOCK_SIZE];
00083 #ifdef GCM_TABLE
00084     /* key-based fast multiplication table. */
00085     ALIGN16 byte M0[256][AES_BLOCK_SIZE];
00086 #endif /* GCM_TABLE */
00087 #endif /* HAVE_AESGCM */
00088 #ifdef WOLFSSL_AESNI
00089     byte use_aesni;
00090 #endif /* WOLFSSL_AESNI */
00091 #ifdef WOLFSSL_ASYNC_CRYPT
00092     const byte* asyncKey;
00093     const byte* asyncIv;
00094     WC_ASYNC_DEV asyncDev;
00095 #endif /* WOLFSSL_ASYNC_CRYPT */
00096 #ifdef WOLFSSL_AES_COUNTER
00097     word32  left;            /* unused bytes left from last call */
00098 #endif
00099 #ifdef WOLFSSL_PIC32MZ_CRYPT
00100     word32 key_ce[AES_BLOCK_SIZE*2/sizeof(word32)] ;
00101     word32 iv_ce [AES_BLOCK_SIZE  /sizeof(word32)] ;
00102 #endif
00103     void*  heap; /* memory hint to use */
00104 } Aes;
00105 
00106 
00107 #ifdef HAVE_AESGCM
00108 typedef struct Gmac {
00109     Aes aes;
00110 } Gmac;
00111 #endif /* HAVE_AESGCM */
00112 #endif /* HAVE_FIPS */
00113 
00114 
00115 /* Authenticate cipher function prototypes */
00116 typedef int (*wc_AesAuthEncryptFunc)(Aes* aes, byte* out,
00117                                    const byte* in, word32 sz,
00118                                    const byte* iv, word32 ivSz,
00119                                    byte* authTag, word32 authTagSz,
00120                                    const byte* authIn, word32 authInSz);
00121 typedef int (*wc_AesAuthDecryptFunc)(Aes* aes, byte* out,
00122                                    const byte* in, word32 sz,
00123                                    const byte* iv, word32 ivSz,
00124                                    const byte* authTag, word32 authTagSz,
00125                                    const byte* authIn, word32 authInSz);
00126 
00127 /* AES-CBC */
00128 WOLFSSL_API int  wc_AesSetKey(Aes* aes, const byte* key, word32 len,
00129                               const byte* iv, int dir);
00130 WOLFSSL_API int  wc_AesSetIV(Aes* aes, const byte* iv);
00131 WOLFSSL_API int  wc_AesCbcEncrypt(Aes* aes, byte* out,
00132                                   const byte* in, word32 sz);
00133 WOLFSSL_API int  wc_AesCbcDecrypt(Aes* aes, byte* out,
00134                                   const byte* in, word32 sz);
00135 
00136 #ifdef HAVE_AES_ECB
00137 WOLFSSL_API int wc_AesEcbEncrypt(Aes* aes, byte* out,
00138                                   const byte* in, word32 sz);
00139 WOLFSSL_API int wc_AesEcbDecrypt(Aes* aes, byte* out,
00140                                   const byte* in, word32 sz);
00141 #endif
00142 
00143 /* AES-CTR */
00144 #ifdef WOLFSSL_AES_COUNTER
00145  WOLFSSL_API void wc_AesCtrEncrypt(Aes* aes, byte* out,
00146                                    const byte* in, word32 sz);
00147 #endif
00148 /* AES-DIRECT */
00149 #if defined(WOLFSSL_AES_DIRECT)
00150  WOLFSSL_API void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in);
00151  WOLFSSL_API void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in);
00152  WOLFSSL_API int  wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
00153                                 const byte* iv, int dir);
00154 #endif
00155 #ifdef HAVE_AESGCM
00156  WOLFSSL_API int  wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len);
00157  WOLFSSL_API int  wc_AesGcmEncrypt(Aes* aes, byte* out,
00158                                    const byte* in, word32 sz,
00159                                    const byte* iv, word32 ivSz,
00160                                    byte* authTag, word32 authTagSz,
00161                                    const byte* authIn, word32 authInSz);
00162  WOLFSSL_API int  wc_AesGcmDecrypt(Aes* aes, byte* out,
00163                                    const byte* in, word32 sz,
00164                                    const byte* iv, word32 ivSz,
00165                                    const byte* authTag, word32 authTagSz,
00166                                    const byte* authIn, word32 authInSz);
00167 
00168  WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len);
00169  WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
00170                                const byte* authIn, word32 authInSz,
00171                                byte* authTag, word32 authTagSz);
00172 #endif /* HAVE_AESGCM */
00173 #ifdef HAVE_AESCCM
00174  WOLFSSL_API int  wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
00175  WOLFSSL_API int  wc_AesCcmEncrypt(Aes* aes, byte* out,
00176                                    const byte* in, word32 inSz,
00177                                    const byte* nonce, word32 nonceSz,
00178                                    byte* authTag, word32 authTagSz,
00179                                    const byte* authIn, word32 authInSz);
00180  WOLFSSL_API int  wc_AesCcmDecrypt(Aes* aes, byte* out,
00181                                    const byte* in, word32 inSz,
00182                                    const byte* nonce, word32 nonceSz,
00183                                    const byte* authTag, word32 authTagSz,
00184                                    const byte* authIn, word32 authInSz);
00185 #endif /* HAVE_AESCCM */
00186 #ifdef HAVE_AES_KEYWRAP
00187  WOLFSSL_API int  wc_AesKeyWrap(const byte* key, word32 keySz,
00188                                 const byte* in, word32 inSz,
00189                                 byte* out, word32 outSz,
00190                                 const byte* iv);
00191  WOLFSSL_API int  wc_AesKeyUnWrap(const byte* key, word32 keySz,
00192                                 const byte* in, word32 inSz,
00193                                 byte* out, word32 outSz,
00194                                 const byte* iv);
00195 #endif /* HAVE_AES_KEYWRAP */
00196 
00197 WOLFSSL_API int wc_AesGetKeySize(Aes* aes, word32* keySize);
00198 
00199 WOLFSSL_API int  wc_AesInit(Aes*, void*, int);
00200 WOLFSSL_API void wc_AesFree(Aes*);
00201 
00202 #ifdef __cplusplus
00203     } /* extern "C" */
00204 #endif
00205 
00206 
00207 #endif /* NO_AES */
00208 #endif /* WOLF_CRYPT_AES_H */
00209