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

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

Committer:
wolfSSL
Date:
Thu Apr 28 00:57:21 2016 +0000
Revision:
4:1b0d80432c79
wolfSSL 3.9.0

Who changed what in which revision?

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