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