Renesas / SecureDweet
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 #ifdef HAVE_CAVIUM
00043     #include <wolfssl/wolfcrypt/logging.h>
00044     #include "cavium_common.h"
00045 #endif
00046 
00047 #ifdef WOLFSSL_AESNI
00048 
00049 #include <wmmintrin.h>
00050 #include <emmintrin.h>
00051 #include <smmintrin.h>
00052 
00053 #if !defined (ALIGN16)
00054     #if defined (__GNUC__)
00055         #define ALIGN16 __attribute__ ( (aligned (16)))
00056     #elif defined(_MSC_VER)
00057         /* disable align warning, we want alignment ! */
00058         #pragma warning(disable: 4324)
00059         #define ALIGN16 __declspec (align (16))
00060     #else
00061         #define ALIGN16
00062     #endif
00063 #endif
00064 
00065 #endif /* WOLFSSL_AESNI */
00066 
00067 #if !defined (ALIGN16)
00068     #define ALIGN16
00069 #endif
00070 #endif /* HAVE_FIPS */
00071 
00072 #ifdef __cplusplus
00073     extern "C" {
00074 #endif
00075 
00076 #ifndef HAVE_FIPS /* to avoid redefinition of structures */
00077 #define WOLFSSL_AES_CAVIUM_MAGIC 0xBEEF0002
00078 
00079 enum {
00080     AES_ENC_TYPE   = 1,   /* cipher unique type */
00081     AES_ENCRYPTION = 0,
00082     AES_DECRYPTION = 1,
00083     AES_BLOCK_SIZE = 16
00084 };
00085 
00086 
00087 typedef struct Aes {
00088     /* AESNI needs key first, rounds 2nd, not sure why yet */
00089     ALIGN16 word32 key[60];
00090     word32  rounds;
00091 
00092     ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)];      /* for CBC mode */
00093     ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)];      /* same         */
00094 
00095 #ifdef HAVE_AESGCM
00096     ALIGN16 byte H[AES_BLOCK_SIZE];
00097 #ifdef GCM_TABLE
00098     /* key-based fast multiplication table. */
00099     ALIGN16 byte M0[256][AES_BLOCK_SIZE];
00100 #endif /* GCM_TABLE */
00101 #endif /* HAVE_AESGCM */
00102 #ifdef WOLFSSL_AESNI
00103     byte use_aesni;
00104 #endif /* WOLFSSL_AESNI */
00105 #ifdef HAVE_CAVIUM
00106     AesType type;            /* aes key type */
00107     int     devId;           /* nitrox device id */
00108     word32  magic;           /* using cavium magic */
00109     word64  contextHandle;   /* nitrox context memory handle */
00110 #endif
00111 #ifdef WOLFSSL_AES_COUNTER
00112     word32  left;            /* unused bytes left from last call */
00113 #endif
00114 #ifdef WOLFSSL_PIC32MZ_CRYPT
00115     word32 key_ce[AES_BLOCK_SIZE*2/sizeof(word32)] ;
00116     word32 iv_ce [AES_BLOCK_SIZE  /sizeof(word32)] ;
00117     int    keylen ;
00118 #endif
00119 #ifdef WOLFSSL_TI_CRYPT
00120     int    keylen ;
00121 #endif
00122 } Aes;
00123 
00124 
00125 #ifdef HAVE_AESGCM
00126 typedef struct Gmac {
00127     Aes aes;
00128 } Gmac;
00129 #endif /* HAVE_AESGCM */
00130 #endif /* HAVE_FIPS */
00131 
00132 WOLFSSL_API int  wc_AesSetKey(Aes* aes, const byte* key, word32 len,
00133                               const byte* iv, int dir);
00134 WOLFSSL_API int  wc_AesSetIV(Aes* aes, const byte* iv);
00135 WOLFSSL_API int  wc_AesCbcEncrypt(Aes* aes, byte* out,
00136                                   const byte* in, word32 sz);
00137 WOLFSSL_API int  wc_AesCbcDecrypt(Aes* aes, byte* out,
00138                                   const byte* in, word32 sz);
00139 
00140 /* AES-CTR */
00141 #ifdef WOLFSSL_AES_COUNTER
00142  WOLFSSL_API void wc_AesCtrEncrypt(Aes* aes, byte* out,
00143                                    const byte* in, word32 sz);
00144 #endif
00145 /* AES-DIRECT */
00146 #if defined(WOLFSSL_AES_DIRECT)
00147  WOLFSSL_API void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in);
00148  WOLFSSL_API void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in);
00149  WOLFSSL_API int  wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
00150                                 const byte* iv, int dir);
00151 #endif
00152 #ifdef HAVE_AESGCM
00153  WOLFSSL_API int  wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len);
00154  WOLFSSL_API int  wc_AesGcmEncrypt(Aes* aes, byte* out,
00155                                    const byte* in, word32 sz,
00156                                    const byte* iv, word32 ivSz,
00157                                    byte* authTag, word32 authTagSz,
00158                                    const byte* authIn, word32 authInSz);
00159  WOLFSSL_API int  wc_AesGcmDecrypt(Aes* aes, byte* out,
00160                                    const byte* in, word32 sz,
00161                                    const byte* iv, word32 ivSz,
00162                                    const byte* authTag, word32 authTagSz,
00163                                    const byte* authIn, word32 authInSz);
00164 
00165  WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len);
00166  WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
00167                                const byte* authIn, word32 authInSz,
00168                                byte* authTag, word32 authTagSz);
00169 #endif /* HAVE_AESGCM */
00170 #ifdef HAVE_AESCCM
00171  WOLFSSL_API void wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
00172  WOLFSSL_API int  wc_AesCcmEncrypt(Aes* aes, byte* out,
00173                                    const byte* in, word32 inSz,
00174                                    const byte* nonce, word32 nonceSz,
00175                                    byte* authTag, word32 authTagSz,
00176                                    const byte* authIn, word32 authInSz);
00177  WOLFSSL_API int  wc_AesCcmDecrypt(Aes* aes, byte* out,
00178                                    const byte* in, word32 inSz,
00179                                    const byte* nonce, word32 nonceSz,
00180                                    const byte* authTag, word32 authTagSz,
00181                                    const byte* authIn, word32 authInSz);
00182 #endif /* HAVE_AESCCM */
00183 
00184 #ifdef HAVE_CAVIUM
00185      WOLFSSL_API int  wc_AesInitCavium(Aes*, int);
00186      WOLFSSL_API void wc_AesFreeCavium(Aes*);
00187 #endif
00188 
00189 #ifdef __cplusplus
00190     } /* extern "C" */
00191 #endif
00192 
00193 
00194 #endif /* NO_AES */
00195 #endif /* WOLF_CRYPT_AES_H */
00196 
00197