wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 7:481bce714567 1 /* aes.h
wolfSSL 7:481bce714567 2 *
wolfSSL 7:481bce714567 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 7:481bce714567 4 *
wolfSSL 7:481bce714567 5 * This file is part of wolfSSL.
wolfSSL 7:481bce714567 6 *
wolfSSL 7:481bce714567 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 7:481bce714567 8 * it under the terms of the GNU General Public License as published by
wolfSSL 7:481bce714567 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 7:481bce714567 10 * (at your option) any later version.
wolfSSL 7:481bce714567 11 *
wolfSSL 7:481bce714567 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 7:481bce714567 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 7:481bce714567 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 7:481bce714567 15 * GNU General Public License for more details.
wolfSSL 7:481bce714567 16 *
wolfSSL 7:481bce714567 17 * You should have received a copy of the GNU General Public License
wolfSSL 7:481bce714567 18 * along with this program; if not, write to the Free Software
wolfSSL 7:481bce714567 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 7:481bce714567 20 */
wolfSSL 7:481bce714567 21
wolfSSL 7:481bce714567 22
wolfSSL 7:481bce714567 23 #ifndef WOLF_CRYPT_AES_H
wolfSSL 7:481bce714567 24 #define WOLF_CRYPT_AES_H
wolfSSL 7:481bce714567 25
wolfSSL 7:481bce714567 26 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 7:481bce714567 27
wolfSSL 7:481bce714567 28 #ifndef NO_AES
wolfSSL 7:481bce714567 29
wolfSSL 7:481bce714567 30 /* included for fips @wc_fips */
wolfSSL 7:481bce714567 31 #ifdef HAVE_FIPS
wolfSSL 7:481bce714567 32 #include <cyassl/ctaocrypt/aes.h>
wolfSSL 7:481bce714567 33 #if defined(CYASSL_AES_COUNTER) && !defined(WOLFSSL_AES_COUNTER)
wolfSSL 7:481bce714567 34 #define WOLFSSL_AES_COUNTER
wolfSSL 7:481bce714567 35 #endif
wolfSSL 7:481bce714567 36 #if !defined(WOLFSSL_AES_DIRECT) && defined(CYASSL_AES_DIRECT)
wolfSSL 7:481bce714567 37 #define WOLFSSL_AES_DIRECT
wolfSSL 7:481bce714567 38 #endif
wolfSSL 7:481bce714567 39 #endif
wolfSSL 7:481bce714567 40
wolfSSL 7:481bce714567 41 #ifndef HAVE_FIPS /* to avoid redefinition of macros */
wolfSSL 7:481bce714567 42
wolfSSL 7:481bce714567 43 #ifdef WOLFSSL_AESNI
wolfSSL 7:481bce714567 44
wolfSSL 7:481bce714567 45 #include <wmmintrin.h>
wolfSSL 7:481bce714567 46 #include <emmintrin.h>
wolfSSL 7:481bce714567 47 #include <smmintrin.h>
wolfSSL 7:481bce714567 48
wolfSSL 7:481bce714567 49 #endif /* WOLFSSL_AESNI */
wolfSSL 7:481bce714567 50
wolfSSL 7:481bce714567 51 #endif /* HAVE_FIPS */
wolfSSL 7:481bce714567 52
wolfSSL 7:481bce714567 53 #ifdef __cplusplus
wolfSSL 7:481bce714567 54 extern "C" {
wolfSSL 7:481bce714567 55 #endif
wolfSSL 7:481bce714567 56
wolfSSL 7:481bce714567 57 #ifndef HAVE_FIPS /* to avoid redefinition of structures */
wolfSSL 7:481bce714567 58
wolfSSL 7:481bce714567 59 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 7:481bce714567 60 #include <wolfssl/wolfcrypt/async.h>
wolfSSL 7:481bce714567 61 #endif
wolfSSL 7:481bce714567 62
wolfSSL 7:481bce714567 63 enum {
wolfSSL 7:481bce714567 64 AES_ENC_TYPE = 1, /* cipher unique type */
wolfSSL 7:481bce714567 65 AES_ENCRYPTION = 0,
wolfSSL 7:481bce714567 66 AES_DECRYPTION = 1,
wolfSSL 7:481bce714567 67 KEYWRAP_BLOCK_SIZE = 8,
wolfSSL 7:481bce714567 68 AES_BLOCK_SIZE = 16
wolfSSL 7:481bce714567 69 };
wolfSSL 7:481bce714567 70
wolfSSL 7:481bce714567 71
wolfSSL 7:481bce714567 72 typedef struct Aes {
wolfSSL 7:481bce714567 73 /* AESNI needs key first, rounds 2nd, not sure why yet */
wolfSSL 7:481bce714567 74 ALIGN16 word32 key[60];
wolfSSL 7:481bce714567 75 word32 rounds;
wolfSSL 7:481bce714567 76
wolfSSL 7:481bce714567 77 ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
wolfSSL 7:481bce714567 78 ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)]; /* same */
wolfSSL 7:481bce714567 79
wolfSSL 7:481bce714567 80 #ifdef HAVE_AESGCM
wolfSSL 7:481bce714567 81 ALIGN16 byte H[AES_BLOCK_SIZE];
wolfSSL 7:481bce714567 82 #ifdef GCM_TABLE
wolfSSL 7:481bce714567 83 /* key-based fast multiplication table. */
wolfSSL 7:481bce714567 84 ALIGN16 byte M0[256][AES_BLOCK_SIZE];
wolfSSL 7:481bce714567 85 #endif /* GCM_TABLE */
wolfSSL 7:481bce714567 86 #endif /* HAVE_AESGCM */
wolfSSL 7:481bce714567 87 #ifdef WOLFSSL_AESNI
wolfSSL 7:481bce714567 88 byte use_aesni;
wolfSSL 7:481bce714567 89 #endif /* WOLFSSL_AESNI */
wolfSSL 7:481bce714567 90 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 7:481bce714567 91 AsyncCryptDev asyncDev;
wolfSSL 7:481bce714567 92 #ifdef HAVE_CAVIUM
wolfSSL 7:481bce714567 93 AesType type; /* aes key type */
wolfSSL 7:481bce714567 94 #endif
wolfSSL 7:481bce714567 95 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 7:481bce714567 96 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 7:481bce714567 97 word32 left; /* unused bytes left from last call */
wolfSSL 7:481bce714567 98 #endif
wolfSSL 7:481bce714567 99 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 7:481bce714567 100 word32 key_ce[AES_BLOCK_SIZE*2/sizeof(word32)] ;
wolfSSL 7:481bce714567 101 word32 iv_ce [AES_BLOCK_SIZE /sizeof(word32)] ;
wolfSSL 7:481bce714567 102 int keylen ;
wolfSSL 7:481bce714567 103 #endif
wolfSSL 7:481bce714567 104 #ifdef WOLFSSL_TI_CRYPT
wolfSSL 7:481bce714567 105 int keylen ;
wolfSSL 7:481bce714567 106 #endif
wolfSSL 7:481bce714567 107 void* heap; /* memory hint to use */
wolfSSL 7:481bce714567 108 } Aes;
wolfSSL 7:481bce714567 109
wolfSSL 7:481bce714567 110
wolfSSL 7:481bce714567 111 #ifdef HAVE_AESGCM
wolfSSL 7:481bce714567 112 typedef struct Gmac {
wolfSSL 7:481bce714567 113 Aes aes;
wolfSSL 7:481bce714567 114 } Gmac;
wolfSSL 7:481bce714567 115 #endif /* HAVE_AESGCM */
wolfSSL 7:481bce714567 116 #endif /* HAVE_FIPS */
wolfSSL 7:481bce714567 117
wolfSSL 7:481bce714567 118 WOLFSSL_LOCAL int wc_InitAes_h(Aes* aes, void* h);
wolfSSL 7:481bce714567 119 WOLFSSL_API int wc_AesSetKey(Aes* aes, const byte* key, word32 len,
wolfSSL 7:481bce714567 120 const byte* iv, int dir);
wolfSSL 7:481bce714567 121 WOLFSSL_API int wc_AesSetIV(Aes* aes, const byte* iv);
wolfSSL 7:481bce714567 122 WOLFSSL_API int wc_AesCbcEncrypt(Aes* aes, byte* out,
wolfSSL 7:481bce714567 123 const byte* in, word32 sz);
wolfSSL 7:481bce714567 124 WOLFSSL_API int wc_AesCbcDecrypt(Aes* aes, byte* out,
wolfSSL 7:481bce714567 125 const byte* in, word32 sz);
wolfSSL 7:481bce714567 126
wolfSSL 7:481bce714567 127 #ifdef HAVE_AES_ECB
wolfSSL 7:481bce714567 128 WOLFSSL_API int wc_AesEcbEncrypt(Aes* aes, byte* out,
wolfSSL 7:481bce714567 129 const byte* in, word32 sz);
wolfSSL 7:481bce714567 130 WOLFSSL_API int wc_AesEcbDecrypt(Aes* aes, byte* out,
wolfSSL 7:481bce714567 131 const byte* in, word32 sz);
wolfSSL 7:481bce714567 132 #endif
wolfSSL 7:481bce714567 133
wolfSSL 7:481bce714567 134 /* AES-CTR */
wolfSSL 7:481bce714567 135 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 7:481bce714567 136 WOLFSSL_API void wc_AesCtrEncrypt(Aes* aes, byte* out,
wolfSSL 7:481bce714567 137 const byte* in, word32 sz);
wolfSSL 7:481bce714567 138 #endif
wolfSSL 7:481bce714567 139 /* AES-DIRECT */
wolfSSL 7:481bce714567 140 #if defined(WOLFSSL_AES_DIRECT)
wolfSSL 7:481bce714567 141 WOLFSSL_API void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in);
wolfSSL 7:481bce714567 142 WOLFSSL_API void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in);
wolfSSL 7:481bce714567 143 WOLFSSL_API int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
wolfSSL 7:481bce714567 144 const byte* iv, int dir);
wolfSSL 7:481bce714567 145 #endif
wolfSSL 7:481bce714567 146 #ifdef HAVE_AESGCM
wolfSSL 7:481bce714567 147 WOLFSSL_API int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len);
wolfSSL 7:481bce714567 148 WOLFSSL_API int wc_AesGcmEncrypt(Aes* aes, byte* out,
wolfSSL 7:481bce714567 149 const byte* in, word32 sz,
wolfSSL 7:481bce714567 150 const byte* iv, word32 ivSz,
wolfSSL 7:481bce714567 151 byte* authTag, word32 authTagSz,
wolfSSL 7:481bce714567 152 const byte* authIn, word32 authInSz);
wolfSSL 7:481bce714567 153 WOLFSSL_API int wc_AesGcmDecrypt(Aes* aes, byte* out,
wolfSSL 7:481bce714567 154 const byte* in, word32 sz,
wolfSSL 7:481bce714567 155 const byte* iv, word32 ivSz,
wolfSSL 7:481bce714567 156 const byte* authTag, word32 authTagSz,
wolfSSL 7:481bce714567 157 const byte* authIn, word32 authInSz);
wolfSSL 7:481bce714567 158
wolfSSL 7:481bce714567 159 WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len);
wolfSSL 7:481bce714567 160 WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
wolfSSL 7:481bce714567 161 const byte* authIn, word32 authInSz,
wolfSSL 7:481bce714567 162 byte* authTag, word32 authTagSz);
wolfSSL 7:481bce714567 163 #endif /* HAVE_AESGCM */
wolfSSL 7:481bce714567 164 #ifdef HAVE_AESCCM
wolfSSL 7:481bce714567 165 WOLFSSL_API int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
wolfSSL 7:481bce714567 166 WOLFSSL_API int wc_AesCcmEncrypt(Aes* aes, byte* out,
wolfSSL 7:481bce714567 167 const byte* in, word32 inSz,
wolfSSL 7:481bce714567 168 const byte* nonce, word32 nonceSz,
wolfSSL 7:481bce714567 169 byte* authTag, word32 authTagSz,
wolfSSL 7:481bce714567 170 const byte* authIn, word32 authInSz);
wolfSSL 7:481bce714567 171 WOLFSSL_API int wc_AesCcmDecrypt(Aes* aes, byte* out,
wolfSSL 7:481bce714567 172 const byte* in, word32 inSz,
wolfSSL 7:481bce714567 173 const byte* nonce, word32 nonceSz,
wolfSSL 7:481bce714567 174 const byte* authTag, word32 authTagSz,
wolfSSL 7:481bce714567 175 const byte* authIn, word32 authInSz);
wolfSSL 7:481bce714567 176 #endif /* HAVE_AESCCM */
wolfSSL 7:481bce714567 177 #ifdef HAVE_AES_KEYWRAP
wolfSSL 7:481bce714567 178 WOLFSSL_API int wc_AesKeyWrap(const byte* key, word32 keySz,
wolfSSL 7:481bce714567 179 const byte* in, word32 inSz,
wolfSSL 7:481bce714567 180 byte* out, word32 outSz,
wolfSSL 7:481bce714567 181 const byte* iv);
wolfSSL 7:481bce714567 182 WOLFSSL_API int wc_AesKeyUnWrap(const byte* key, word32 keySz,
wolfSSL 7:481bce714567 183 const byte* in, word32 inSz,
wolfSSL 7:481bce714567 184 byte* out, word32 outSz,
wolfSSL 7:481bce714567 185 const byte* iv);
wolfSSL 7:481bce714567 186 #endif /* HAVE_AES_KEYWRAP */
wolfSSL 7:481bce714567 187
wolfSSL 7:481bce714567 188 WOLFSSL_API int wc_AesGetKeySize(Aes* aes, word32* keySize);
wolfSSL 7:481bce714567 189
wolfSSL 7:481bce714567 190 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 7:481bce714567 191 WOLFSSL_API int wc_AesAsyncInit(Aes*, int);
wolfSSL 7:481bce714567 192 WOLFSSL_API void wc_AesAsyncFree(Aes*);
wolfSSL 7:481bce714567 193 #endif
wolfSSL 7:481bce714567 194
wolfSSL 7:481bce714567 195 #ifdef __cplusplus
wolfSSL 7:481bce714567 196 } /* extern "C" */
wolfSSL 7:481bce714567 197 #endif
wolfSSL 7:481bce714567 198
wolfSSL 7:481bce714567 199
wolfSSL 7:481bce714567 200 #endif /* NO_AES */
wolfSSL 7:481bce714567 201 #endif /* WOLF_CRYPT_AES_H */
wolfSSL 7:481bce714567 202