A library for setting up Secure Socket Layer (SSL) connections and verifying remote hosts using certificates. Contains only the source files for mbed platform implementation of the library.
Dependents: HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL
cyassl/ctaocrypt/aes.h@6:cf58d49e1a86, 2015-03-23 (annotated)
- Committer:
- Mike Fiore
- Date:
- Mon Mar 23 16:51:07 2015 -0500
- Revision:
- 6:cf58d49e1a86
- Parent:
- 0:b86d15c6ba29
fix whitespace in sha512.c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vanger | 0:b86d15c6ba29 | 1 | /* aes.h |
Vanger | 0:b86d15c6ba29 | 2 | * |
Vanger | 0:b86d15c6ba29 | 3 | * Copyright (C) 2006-2014 wolfSSL Inc. |
Vanger | 0:b86d15c6ba29 | 4 | * |
Vanger | 0:b86d15c6ba29 | 5 | * This file is part of CyaSSL. |
Vanger | 0:b86d15c6ba29 | 6 | * |
Vanger | 0:b86d15c6ba29 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
Vanger | 0:b86d15c6ba29 | 8 | * it under the terms of the GNU General Public License as published by |
Vanger | 0:b86d15c6ba29 | 9 | * the Free Software Foundation; either version 2 of the License, or |
Vanger | 0:b86d15c6ba29 | 10 | * (at your option) any later version. |
Vanger | 0:b86d15c6ba29 | 11 | * |
Vanger | 0:b86d15c6ba29 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
Vanger | 0:b86d15c6ba29 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Vanger | 0:b86d15c6ba29 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Vanger | 0:b86d15c6ba29 | 15 | * GNU General Public License for more details. |
Vanger | 0:b86d15c6ba29 | 16 | * |
Vanger | 0:b86d15c6ba29 | 17 | * You should have received a copy of the GNU General Public License |
Vanger | 0:b86d15c6ba29 | 18 | * along with this program; if not, write to the Free Software |
Vanger | 0:b86d15c6ba29 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
Vanger | 0:b86d15c6ba29 | 20 | */ |
Vanger | 0:b86d15c6ba29 | 21 | |
Vanger | 0:b86d15c6ba29 | 22 | |
Vanger | 0:b86d15c6ba29 | 23 | #ifndef NO_AES |
Vanger | 0:b86d15c6ba29 | 24 | |
Vanger | 0:b86d15c6ba29 | 25 | #ifndef CTAO_CRYPT_AES_H |
Vanger | 0:b86d15c6ba29 | 26 | #define CTAO_CRYPT_AES_H |
Vanger | 0:b86d15c6ba29 | 27 | |
Vanger | 0:b86d15c6ba29 | 28 | |
Vanger | 0:b86d15c6ba29 | 29 | #include <cyassl/ctaocrypt/types.h> |
Vanger | 0:b86d15c6ba29 | 30 | |
Vanger | 0:b86d15c6ba29 | 31 | #ifdef HAVE_CAVIUM |
Vanger | 0:b86d15c6ba29 | 32 | #include <cyassl/ctaocrypt/logging.h> |
Vanger | 0:b86d15c6ba29 | 33 | #include "cavium_common.h" |
Vanger | 0:b86d15c6ba29 | 34 | #endif |
Vanger | 0:b86d15c6ba29 | 35 | |
Vanger | 0:b86d15c6ba29 | 36 | #ifdef CYASSL_AESNI |
Vanger | 0:b86d15c6ba29 | 37 | |
Vanger | 0:b86d15c6ba29 | 38 | #include <wmmintrin.h> |
Vanger | 0:b86d15c6ba29 | 39 | |
Vanger | 0:b86d15c6ba29 | 40 | #if !defined (ALIGN16) |
Vanger | 0:b86d15c6ba29 | 41 | #if defined (__GNUC__) |
Vanger | 0:b86d15c6ba29 | 42 | #define ALIGN16 __attribute__ ( (aligned (16))) |
Vanger | 0:b86d15c6ba29 | 43 | #elif defined(_MSC_VER) |
Vanger | 0:b86d15c6ba29 | 44 | /* disable align warning, we want alignment ! */ |
Vanger | 0:b86d15c6ba29 | 45 | #pragma warning(disable: 4324) |
Vanger | 0:b86d15c6ba29 | 46 | #define ALIGN16 __declspec (align (16)) |
Vanger | 0:b86d15c6ba29 | 47 | #else |
Vanger | 0:b86d15c6ba29 | 48 | #define ALIGN16 |
Vanger | 0:b86d15c6ba29 | 49 | #endif |
Vanger | 0:b86d15c6ba29 | 50 | #endif |
Vanger | 0:b86d15c6ba29 | 51 | |
Vanger | 0:b86d15c6ba29 | 52 | #endif /* CYASSL_AESNI */ |
Vanger | 0:b86d15c6ba29 | 53 | |
Vanger | 0:b86d15c6ba29 | 54 | #if !defined (ALIGN16) |
Vanger | 0:b86d15c6ba29 | 55 | #define ALIGN16 |
Vanger | 0:b86d15c6ba29 | 56 | #endif |
Vanger | 0:b86d15c6ba29 | 57 | |
Vanger | 0:b86d15c6ba29 | 58 | #ifdef __cplusplus |
Vanger | 0:b86d15c6ba29 | 59 | extern "C" { |
Vanger | 0:b86d15c6ba29 | 60 | #endif |
Vanger | 0:b86d15c6ba29 | 61 | |
Vanger | 0:b86d15c6ba29 | 62 | |
Vanger | 0:b86d15c6ba29 | 63 | #define CYASSL_AES_CAVIUM_MAGIC 0xBEEF0002 |
Vanger | 0:b86d15c6ba29 | 64 | |
Vanger | 0:b86d15c6ba29 | 65 | enum { |
Vanger | 0:b86d15c6ba29 | 66 | AES_ENC_TYPE = 1, /* cipher unique type */ |
Vanger | 0:b86d15c6ba29 | 67 | AES_ENCRYPTION = 0, |
Vanger | 0:b86d15c6ba29 | 68 | AES_DECRYPTION = 1, |
Vanger | 0:b86d15c6ba29 | 69 | AES_BLOCK_SIZE = 16 |
Vanger | 0:b86d15c6ba29 | 70 | }; |
Vanger | 0:b86d15c6ba29 | 71 | |
Vanger | 0:b86d15c6ba29 | 72 | |
Vanger | 0:b86d15c6ba29 | 73 | typedef struct Aes { |
Vanger | 0:b86d15c6ba29 | 74 | /* AESNI needs key first, rounds 2nd, not sure why yet */ |
Vanger | 0:b86d15c6ba29 | 75 | ALIGN16 word32 key[60]; |
Vanger | 0:b86d15c6ba29 | 76 | word32 rounds; |
Vanger | 0:b86d15c6ba29 | 77 | |
Vanger | 0:b86d15c6ba29 | 78 | ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ |
Vanger | 0:b86d15c6ba29 | 79 | ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)]; /* same */ |
Vanger | 0:b86d15c6ba29 | 80 | |
Vanger | 0:b86d15c6ba29 | 81 | #ifdef HAVE_AESGCM |
Vanger | 0:b86d15c6ba29 | 82 | ALIGN16 byte H[AES_BLOCK_SIZE]; |
Vanger | 0:b86d15c6ba29 | 83 | #ifdef GCM_TABLE |
Vanger | 0:b86d15c6ba29 | 84 | /* key-based fast multiplication table. */ |
Vanger | 0:b86d15c6ba29 | 85 | ALIGN16 byte M0[256][AES_BLOCK_SIZE]; |
Vanger | 0:b86d15c6ba29 | 86 | #endif /* GCM_TABLE */ |
Vanger | 0:b86d15c6ba29 | 87 | #endif /* HAVE_AESGCM */ |
Vanger | 0:b86d15c6ba29 | 88 | #ifdef CYASSL_AESNI |
Vanger | 0:b86d15c6ba29 | 89 | byte use_aesni; |
Vanger | 0:b86d15c6ba29 | 90 | #endif /* CYASSL_AESNI */ |
Vanger | 0:b86d15c6ba29 | 91 | #ifdef HAVE_CAVIUM |
Vanger | 0:b86d15c6ba29 | 92 | AesType type; /* aes key type */ |
Vanger | 0:b86d15c6ba29 | 93 | int devId; /* nitrox device id */ |
Vanger | 0:b86d15c6ba29 | 94 | word32 magic; /* using cavium magic */ |
Vanger | 0:b86d15c6ba29 | 95 | word64 contextHandle; /* nitrox context memory handle */ |
Vanger | 0:b86d15c6ba29 | 96 | #endif |
Vanger | 0:b86d15c6ba29 | 97 | #ifdef CYASSL_AES_COUNTER |
Vanger | 0:b86d15c6ba29 | 98 | word32 left; /* unsued bytes left from last call */ |
Vanger | 0:b86d15c6ba29 | 99 | #endif |
Vanger | 0:b86d15c6ba29 | 100 | #ifdef CYASSL_PIC32MZ_CRYPT |
Vanger | 0:b86d15c6ba29 | 101 | word32 key_ce[AES_BLOCK_SIZE*2/sizeof(word32)] ; |
Vanger | 0:b86d15c6ba29 | 102 | word32 iv_ce [AES_BLOCK_SIZE /sizeof(word32)] ; |
Vanger | 0:b86d15c6ba29 | 103 | int keylen ; |
Vanger | 0:b86d15c6ba29 | 104 | #endif |
Vanger | 0:b86d15c6ba29 | 105 | } Aes; |
Vanger | 0:b86d15c6ba29 | 106 | |
Vanger | 0:b86d15c6ba29 | 107 | |
Vanger | 0:b86d15c6ba29 | 108 | CYASSL_API int AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv, |
Vanger | 0:b86d15c6ba29 | 109 | int dir); |
Vanger | 0:b86d15c6ba29 | 110 | CYASSL_API int AesSetIV(Aes* aes, const byte* iv); |
Vanger | 0:b86d15c6ba29 | 111 | CYASSL_API int AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz); |
Vanger | 0:b86d15c6ba29 | 112 | CYASSL_API int AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz); |
Vanger | 0:b86d15c6ba29 | 113 | CYASSL_API int AesCbcDecryptWithKey(byte* out, const byte* in, word32 inSz, |
Vanger | 0:b86d15c6ba29 | 114 | const byte* key, word32 keySz, const byte* iv); |
Vanger | 0:b86d15c6ba29 | 115 | CYASSL_API void AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz); |
Vanger | 0:b86d15c6ba29 | 116 | CYASSL_API void AesEncryptDirect(Aes* aes, byte* out, const byte* in); |
Vanger | 0:b86d15c6ba29 | 117 | CYASSL_API void AesDecryptDirect(Aes* aes, byte* out, const byte* in); |
Vanger | 0:b86d15c6ba29 | 118 | CYASSL_API int AesSetKeyDirect(Aes* aes, const byte* key, word32 len, |
Vanger | 0:b86d15c6ba29 | 119 | const byte* iv, int dir); |
Vanger | 0:b86d15c6ba29 | 120 | #ifdef HAVE_AESGCM |
Vanger | 0:b86d15c6ba29 | 121 | CYASSL_API int AesGcmSetKey(Aes* aes, const byte* key, word32 len); |
Vanger | 0:b86d15c6ba29 | 122 | CYASSL_API int AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz, |
Vanger | 0:b86d15c6ba29 | 123 | const byte* iv, word32 ivSz, |
Vanger | 0:b86d15c6ba29 | 124 | byte* authTag, word32 authTagSz, |
Vanger | 0:b86d15c6ba29 | 125 | const byte* authIn, word32 authInSz); |
Vanger | 0:b86d15c6ba29 | 126 | CYASSL_API int AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz, |
Vanger | 0:b86d15c6ba29 | 127 | const byte* iv, word32 ivSz, |
Vanger | 0:b86d15c6ba29 | 128 | const byte* authTag, word32 authTagSz, |
Vanger | 0:b86d15c6ba29 | 129 | const byte* authIn, word32 authInSz); |
Vanger | 0:b86d15c6ba29 | 130 | |
Vanger | 0:b86d15c6ba29 | 131 | typedef struct Gmac { |
Vanger | 0:b86d15c6ba29 | 132 | Aes aes; |
Vanger | 0:b86d15c6ba29 | 133 | } Gmac; |
Vanger | 0:b86d15c6ba29 | 134 | CYASSL_API int GmacSetKey(Gmac* gmac, const byte* key, word32 len); |
Vanger | 0:b86d15c6ba29 | 135 | CYASSL_API int GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz, |
Vanger | 0:b86d15c6ba29 | 136 | const byte* authIn, word32 authInSz, |
Vanger | 0:b86d15c6ba29 | 137 | byte* authTag, word32 authTagSz); |
Vanger | 0:b86d15c6ba29 | 138 | #endif /* HAVE_AESGCM */ |
Vanger | 0:b86d15c6ba29 | 139 | #ifdef HAVE_AESCCM |
Vanger | 0:b86d15c6ba29 | 140 | CYASSL_API void AesCcmSetKey(Aes* aes, const byte* key, word32 keySz); |
Vanger | 0:b86d15c6ba29 | 141 | CYASSL_API void AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz, |
Vanger | 0:b86d15c6ba29 | 142 | const byte* nonce, word32 nonceSz, |
Vanger | 0:b86d15c6ba29 | 143 | byte* authTag, word32 authTagSz, |
Vanger | 0:b86d15c6ba29 | 144 | const byte* authIn, word32 authInSz); |
Vanger | 0:b86d15c6ba29 | 145 | CYASSL_API int AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz, |
Vanger | 0:b86d15c6ba29 | 146 | const byte* nonce, word32 nonceSz, |
Vanger | 0:b86d15c6ba29 | 147 | const byte* authTag, word32 authTagSz, |
Vanger | 0:b86d15c6ba29 | 148 | const byte* authIn, word32 authInSz); |
Vanger | 0:b86d15c6ba29 | 149 | #endif /* HAVE_AESCCM */ |
Vanger | 0:b86d15c6ba29 | 150 | |
Vanger | 0:b86d15c6ba29 | 151 | #ifdef HAVE_CAVIUM |
Vanger | 0:b86d15c6ba29 | 152 | CYASSL_API int AesInitCavium(Aes*, int); |
Vanger | 0:b86d15c6ba29 | 153 | CYASSL_API void AesFreeCavium(Aes*); |
Vanger | 0:b86d15c6ba29 | 154 | #endif |
Vanger | 0:b86d15c6ba29 | 155 | |
Vanger | 0:b86d15c6ba29 | 156 | |
Vanger | 0:b86d15c6ba29 | 157 | #ifdef HAVE_FIPS |
Vanger | 0:b86d15c6ba29 | 158 | /* fips wrapper calls, user can call direct */ |
Vanger | 0:b86d15c6ba29 | 159 | CYASSL_API int AesSetKey_fips(Aes* aes, const byte* key, word32 len, |
Vanger | 0:b86d15c6ba29 | 160 | const byte* iv, int dir); |
Vanger | 0:b86d15c6ba29 | 161 | CYASSL_API int AesSetIV_fips(Aes* aes, const byte* iv); |
Vanger | 0:b86d15c6ba29 | 162 | CYASSL_API int AesCbcEncrypt_fips(Aes* aes, byte* out, const byte* in, |
Vanger | 0:b86d15c6ba29 | 163 | word32 sz); |
Vanger | 0:b86d15c6ba29 | 164 | CYASSL_API int AesCbcDecrypt_fips(Aes* aes, byte* out, const byte* in, |
Vanger | 0:b86d15c6ba29 | 165 | word32 sz); |
Vanger | 0:b86d15c6ba29 | 166 | CYASSL_API int AesGcmSetKey_fips(Aes* aes, const byte* key, word32 len); |
Vanger | 0:b86d15c6ba29 | 167 | CYASSL_API int AesGcmEncrypt_fips(Aes* aes, byte* out, const byte* in, |
Vanger | 0:b86d15c6ba29 | 168 | word32 sz, const byte* iv, word32 ivSz, |
Vanger | 0:b86d15c6ba29 | 169 | byte* authTag, word32 authTagSz, |
Vanger | 0:b86d15c6ba29 | 170 | const byte* authIn, word32 authInSz); |
Vanger | 0:b86d15c6ba29 | 171 | CYASSL_API int AesGcmDecrypt_fips(Aes* aes, byte* out, const byte* in, |
Vanger | 0:b86d15c6ba29 | 172 | word32 sz, const byte* iv, word32 ivSz, |
Vanger | 0:b86d15c6ba29 | 173 | const byte* authTag, word32 authTagSz, |
Vanger | 0:b86d15c6ba29 | 174 | const byte* authIn, word32 authInSz); |
Vanger | 0:b86d15c6ba29 | 175 | #ifndef FIPS_NO_WRAPPERS |
Vanger | 0:b86d15c6ba29 | 176 | /* if not impl or fips.c impl wrapper force fips calls if fips build */ |
Vanger | 0:b86d15c6ba29 | 177 | #define AesSetKey AesSetKey_fips |
Vanger | 0:b86d15c6ba29 | 178 | #define AesSetIV AesSetIV_fips |
Vanger | 0:b86d15c6ba29 | 179 | #define AesCbcEncrypt AesCbcEncrypt_fips |
Vanger | 0:b86d15c6ba29 | 180 | #define AesCbcDecrypt AesCbcDecrypt_fips |
Vanger | 0:b86d15c6ba29 | 181 | #define AesGcmSetKey AesGcmSetKey_fips |
Vanger | 0:b86d15c6ba29 | 182 | #define AesGcmEncrypt AesGcmEncrypt_fips |
Vanger | 0:b86d15c6ba29 | 183 | #define AesGcmDecrypt AesGcmDecrypt_fips |
Vanger | 0:b86d15c6ba29 | 184 | #endif /* FIPS_NO_WRAPPERS */ |
Vanger | 0:b86d15c6ba29 | 185 | |
Vanger | 0:b86d15c6ba29 | 186 | #endif /* HAVE_FIPS */ |
Vanger | 0:b86d15c6ba29 | 187 | |
Vanger | 0:b86d15c6ba29 | 188 | |
Vanger | 0:b86d15c6ba29 | 189 | #ifdef __cplusplus |
Vanger | 0:b86d15c6ba29 | 190 | } /* extern "C" */ |
Vanger | 0:b86d15c6ba29 | 191 | #endif |
Vanger | 0:b86d15c6ba29 | 192 | |
Vanger | 0:b86d15c6ba29 | 193 | |
Vanger | 0:b86d15c6ba29 | 194 | #endif /* CTAO_CRYPT_AES_H */ |
Vanger | 0:b86d15c6ba29 | 195 | #endif /* NO_AES */ |
Vanger | 0:b86d15c6ba29 | 196 |