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

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

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

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