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 30 01:44:10 2017 +0000
Revision:
11:cee25a834751
wolfSSL 3.11.0

Who changed what in which revision?

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