MultiTech / CyaSSL

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-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-2014 wolfSSL Inc.
00004  *
00005  * This file is part of CyaSSL.
00006  *
00007  * CyaSSL 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  * CyaSSL 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-1301, USA
00020  */
00021 
00022 
00023 #ifndef NO_AES
00024 
00025 #ifndef CTAO_CRYPT_AES_H
00026 #define CTAO_CRYPT_AES_H
00027 
00028 
00029 #include <cyassl/ctaocrypt/types.h>
00030 
00031 #ifdef HAVE_CAVIUM
00032     #include <cyassl/ctaocrypt/logging.h>
00033     #include "cavium_common.h"
00034 #endif
00035 
00036 #ifdef CYASSL_AESNI
00037 
00038 #include <wmmintrin.h>
00039 
00040 #if !defined (ALIGN16)
00041     #if defined (__GNUC__)
00042         #define ALIGN16 __attribute__ ( (aligned (16)))
00043     #elif defined(_MSC_VER)
00044         /* disable align warning, we want alignment ! */
00045         #pragma warning(disable: 4324)
00046         #define ALIGN16 __declspec (align (16))
00047     #else
00048         #define ALIGN16
00049     #endif
00050 #endif
00051 
00052 #endif /* CYASSL_AESNI */
00053 
00054 #if !defined (ALIGN16)
00055     #define ALIGN16
00056 #endif
00057 
00058 #ifdef __cplusplus
00059     extern "C" {
00060 #endif
00061 
00062 
00063 #define CYASSL_AES_CAVIUM_MAGIC 0xBEEF0002
00064 
00065 enum {
00066     AES_ENC_TYPE   = 1,   /* cipher unique type */
00067     AES_ENCRYPTION = 0,
00068     AES_DECRYPTION = 1,
00069     AES_BLOCK_SIZE = 16
00070 };
00071 
00072 
00073 typedef struct Aes {
00074     /* AESNI needs key first, rounds 2nd, not sure why yet */
00075     ALIGN16 word32 key[60];
00076     word32  rounds;
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 CYASSL_AESNI
00089     byte use_aesni;
00090 #endif /* CYASSL_AESNI */
00091 #ifdef HAVE_CAVIUM
00092     AesType type;            /* aes key type */
00093     int     devId;           /* nitrox device id */
00094     word32  magic;           /* using cavium magic */
00095     word64  contextHandle;   /* nitrox context memory handle */
00096 #endif
00097 #ifdef CYASSL_AES_COUNTER
00098     word32  left;            /* unsued bytes left from last call */
00099 #endif
00100 #ifdef CYASSL_PIC32MZ_CRYPT
00101     word32 key_ce[AES_BLOCK_SIZE*2/sizeof(word32)] ;
00102     word32 iv_ce [AES_BLOCK_SIZE  /sizeof(word32)] ;
00103     int    keylen ;
00104 #endif
00105 } Aes;
00106 
00107 
00108 CYASSL_API int  AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
00109                           int dir);
00110 CYASSL_API int  AesSetIV(Aes* aes, const byte* iv);
00111 CYASSL_API int  AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz);
00112 CYASSL_API int  AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz);
00113 CYASSL_API int  AesCbcDecryptWithKey(byte* out, const byte* in, word32 inSz,
00114                                  const byte* key, word32 keySz, const byte* iv);
00115 CYASSL_API void AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz);
00116 CYASSL_API void AesEncryptDirect(Aes* aes, byte* out, const byte* in);
00117 CYASSL_API void AesDecryptDirect(Aes* aes, byte* out, const byte* in);
00118 CYASSL_API int  AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
00119                                 const byte* iv, int dir);
00120 #ifdef HAVE_AESGCM
00121 CYASSL_API int  AesGcmSetKey(Aes* aes, const byte* key, word32 len);
00122 CYASSL_API int  AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
00123                               const byte* iv, word32 ivSz,
00124                               byte* authTag, word32 authTagSz,
00125                               const byte* authIn, word32 authInSz);
00126 CYASSL_API int  AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
00127                               const byte* iv, word32 ivSz,
00128                               const byte* authTag, word32 authTagSz,
00129                               const byte* authIn, word32 authInSz);
00130 
00131 typedef struct Gmac {
00132     Aes aes;
00133 } Gmac;
00134 CYASSL_API int GmacSetKey(Gmac* gmac, const byte* key, word32 len);
00135 CYASSL_API int GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
00136                               const byte* authIn, word32 authInSz,
00137                               byte* authTag, word32 authTagSz);
00138 #endif /* HAVE_AESGCM */
00139 #ifdef HAVE_AESCCM
00140 CYASSL_API void AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
00141 CYASSL_API void AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
00142                               const byte* nonce, word32 nonceSz,
00143                               byte* authTag, word32 authTagSz,
00144                               const byte* authIn, word32 authInSz);
00145 CYASSL_API int  AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
00146                               const byte* nonce, word32 nonceSz,
00147                               const byte* authTag, word32 authTagSz,
00148                               const byte* authIn, word32 authInSz);
00149 #endif /* HAVE_AESCCM */
00150 
00151 #ifdef HAVE_CAVIUM
00152     CYASSL_API int  AesInitCavium(Aes*, int);
00153     CYASSL_API void AesFreeCavium(Aes*);
00154 #endif
00155 
00156 
00157 #ifdef HAVE_FIPS
00158     /* fips wrapper calls, user can call direct */
00159     CYASSL_API int  AesSetKey_fips(Aes* aes, const byte* key, word32 len,
00160                                    const byte* iv, int dir);
00161     CYASSL_API int  AesSetIV_fips(Aes* aes, const byte* iv);
00162     CYASSL_API int  AesCbcEncrypt_fips(Aes* aes, byte* out, const byte* in,
00163                                        word32 sz);
00164     CYASSL_API int  AesCbcDecrypt_fips(Aes* aes, byte* out, const byte* in,
00165                                        word32 sz);
00166     CYASSL_API int  AesGcmSetKey_fips(Aes* aes, const byte* key, word32 len);
00167     CYASSL_API int  AesGcmEncrypt_fips(Aes* aes, byte* out, const byte* in,
00168                               word32 sz, const byte* iv, word32 ivSz,
00169                               byte* authTag, word32 authTagSz,
00170                               const byte* authIn, word32 authInSz);
00171     CYASSL_API int  AesGcmDecrypt_fips(Aes* aes, byte* out, const byte* in,
00172                               word32 sz, const byte* iv, word32 ivSz,
00173                               const byte* authTag, word32 authTagSz,
00174                               const byte* authIn, word32 authInSz);
00175     #ifndef FIPS_NO_WRAPPERS
00176         /* if not impl or fips.c impl wrapper force fips calls if fips build */
00177         #define AesSetKey     AesSetKey_fips
00178         #define AesSetIV      AesSetIV_fips
00179         #define AesCbcEncrypt AesCbcEncrypt_fips
00180         #define AesCbcDecrypt AesCbcDecrypt_fips
00181         #define AesGcmSetKey  AesGcmSetKey_fips
00182         #define AesGcmEncrypt AesGcmEncrypt_fips
00183         #define AesGcmDecrypt AesGcmDecrypt_fips
00184     #endif /* FIPS_NO_WRAPPERS */
00185 
00186 #endif /* HAVE_FIPS */
00187 
00188 
00189 #ifdef __cplusplus
00190     } /* extern "C" */
00191 #endif
00192 
00193 
00194 #endif /* CTAO_CRYPT_AES_H */
00195 #endif /* NO_AES */
00196