wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

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.c
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 #ifdef HAVE_CONFIG_H
wolfSSL 11:cee25a834751 24 #include <config.h>
wolfSSL 11:cee25a834751 25 #endif
wolfSSL 11:cee25a834751 26
wolfSSL 11:cee25a834751 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 11:cee25a834751 28
wolfSSL 11:cee25a834751 29 #ifndef NO_AES
wolfSSL 11:cee25a834751 30
wolfSSL 11:cee25a834751 31 #include <wolfssl/wolfcrypt/aes.h>
wolfSSL 11:cee25a834751 32
wolfSSL 11:cee25a834751 33 /* fips wrapper calls, user can call direct */
wolfSSL 11:cee25a834751 34 #ifdef HAVE_FIPS
wolfSSL 11:cee25a834751 35 int wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
wolfSSL 11:cee25a834751 36 int dir)
wolfSSL 11:cee25a834751 37 {
wolfSSL 11:cee25a834751 38 return AesSetKey_fips(aes, key, len, iv, dir);
wolfSSL 11:cee25a834751 39 }
wolfSSL 11:cee25a834751 40 int wc_AesSetIV(Aes* aes, const byte* iv)
wolfSSL 11:cee25a834751 41 {
wolfSSL 11:cee25a834751 42 return AesSetIV_fips(aes, iv);
wolfSSL 11:cee25a834751 43 }
wolfSSL 11:cee25a834751 44 #ifdef HAVE_AES_CBC
wolfSSL 11:cee25a834751 45 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 46 {
wolfSSL 11:cee25a834751 47 return AesCbcEncrypt_fips(aes, out, in, sz);
wolfSSL 11:cee25a834751 48 }
wolfSSL 11:cee25a834751 49 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 50 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 51 {
wolfSSL 11:cee25a834751 52 return AesCbcDecrypt_fips(aes, out, in, sz);
wolfSSL 11:cee25a834751 53 }
wolfSSL 11:cee25a834751 54 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 55 #endif /* HAVE_AES_CBC */
wolfSSL 11:cee25a834751 56
wolfSSL 11:cee25a834751 57 /* AES-CTR */
wolfSSL 11:cee25a834751 58 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 11:cee25a834751 59 void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 60 {
wolfSSL 11:cee25a834751 61 AesCtrEncrypt(aes, out, in, sz);
wolfSSL 11:cee25a834751 62 }
wolfSSL 11:cee25a834751 63 #endif
wolfSSL 11:cee25a834751 64
wolfSSL 11:cee25a834751 65 /* AES-DIRECT */
wolfSSL 11:cee25a834751 66 #if defined(WOLFSSL_AES_DIRECT)
wolfSSL 11:cee25a834751 67 void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 11:cee25a834751 68 {
wolfSSL 11:cee25a834751 69 AesEncryptDirect(aes, out, in);
wolfSSL 11:cee25a834751 70 }
wolfSSL 11:cee25a834751 71
wolfSSL 11:cee25a834751 72 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 73 void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 11:cee25a834751 74 {
wolfSSL 11:cee25a834751 75 AesDecryptDirect(aes, out, in);
wolfSSL 11:cee25a834751 76 }
wolfSSL 11:cee25a834751 77 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 78
wolfSSL 11:cee25a834751 79 int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
wolfSSL 11:cee25a834751 80 const byte* iv, int dir)
wolfSSL 11:cee25a834751 81 {
wolfSSL 11:cee25a834751 82 return AesSetKeyDirect(aes, key, len, iv, dir);
wolfSSL 11:cee25a834751 83 }
wolfSSL 11:cee25a834751 84 #endif /* WOLFSSL_AES_DIRECT */
wolfSSL 11:cee25a834751 85
wolfSSL 11:cee25a834751 86 /* AES-GCM */
wolfSSL 11:cee25a834751 87 #ifdef HAVE_AESGCM
wolfSSL 11:cee25a834751 88 int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
wolfSSL 11:cee25a834751 89 {
wolfSSL 11:cee25a834751 90 return AesGcmSetKey_fips(aes, key, len);
wolfSSL 11:cee25a834751 91 }
wolfSSL 11:cee25a834751 92 int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
wolfSSL 11:cee25a834751 93 const byte* iv, word32 ivSz,
wolfSSL 11:cee25a834751 94 byte* authTag, word32 authTagSz,
wolfSSL 11:cee25a834751 95 const byte* authIn, word32 authInSz)
wolfSSL 11:cee25a834751 96 {
wolfSSL 11:cee25a834751 97 return AesGcmEncrypt_fips(aes, out, in, sz, iv, ivSz, authTag,
wolfSSL 11:cee25a834751 98 authTagSz, authIn, authInSz);
wolfSSL 11:cee25a834751 99 }
wolfSSL 11:cee25a834751 100
wolfSSL 11:cee25a834751 101 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 102 int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
wolfSSL 11:cee25a834751 103 const byte* iv, word32 ivSz,
wolfSSL 11:cee25a834751 104 const byte* authTag, word32 authTagSz,
wolfSSL 11:cee25a834751 105 const byte* authIn, word32 authInSz)
wolfSSL 11:cee25a834751 106 {
wolfSSL 11:cee25a834751 107 return AesGcmDecrypt_fips(aes, out, in, sz, iv, ivSz, authTag,
wolfSSL 11:cee25a834751 108 authTagSz, authIn, authInSz);
wolfSSL 11:cee25a834751 109 }
wolfSSL 11:cee25a834751 110 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 111
wolfSSL 11:cee25a834751 112 int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
wolfSSL 11:cee25a834751 113 {
wolfSSL 11:cee25a834751 114 return GmacSetKey(gmac, key, len);
wolfSSL 11:cee25a834751 115 }
wolfSSL 11:cee25a834751 116 int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
wolfSSL 11:cee25a834751 117 const byte* authIn, word32 authInSz,
wolfSSL 11:cee25a834751 118 byte* authTag, word32 authTagSz)
wolfSSL 11:cee25a834751 119 {
wolfSSL 11:cee25a834751 120 return GmacUpdate(gmac, iv, ivSz, authIn, authInSz,
wolfSSL 11:cee25a834751 121 authTag, authTagSz);
wolfSSL 11:cee25a834751 122 }
wolfSSL 11:cee25a834751 123 #endif /* HAVE_AESGCM */
wolfSSL 11:cee25a834751 124
wolfSSL 11:cee25a834751 125 /* AES-CCM */
wolfSSL 11:cee25a834751 126 #ifdef HAVE_AESCCM
wolfSSL 11:cee25a834751 127 void wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
wolfSSL 11:cee25a834751 128 {
wolfSSL 11:cee25a834751 129 AesCcmSetKey(aes, key, keySz);
wolfSSL 11:cee25a834751 130 }
wolfSSL 11:cee25a834751 131 int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
wolfSSL 11:cee25a834751 132 const byte* nonce, word32 nonceSz,
wolfSSL 11:cee25a834751 133 byte* authTag, word32 authTagSz,
wolfSSL 11:cee25a834751 134 const byte* authIn, word32 authInSz)
wolfSSL 11:cee25a834751 135 {
wolfSSL 11:cee25a834751 136 /* sanity check on arguments */
wolfSSL 11:cee25a834751 137 if (aes == NULL || out == NULL || in == NULL || nonce == NULL
wolfSSL 11:cee25a834751 138 || authTag == NULL || nonceSz < 7 || nonceSz > 13)
wolfSSL 11:cee25a834751 139 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 140
wolfSSL 11:cee25a834751 141 AesCcmEncrypt(aes, out, in, inSz, nonce, nonceSz, authTag,
wolfSSL 11:cee25a834751 142 authTagSz, authIn, authInSz);
wolfSSL 11:cee25a834751 143 return 0;
wolfSSL 11:cee25a834751 144 }
wolfSSL 11:cee25a834751 145
wolfSSL 11:cee25a834751 146 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 147 int wc_AesCcmDecrypt(Aes* aes, byte* out,
wolfSSL 11:cee25a834751 148 const byte* in, word32 inSz,
wolfSSL 11:cee25a834751 149 const byte* nonce, word32 nonceSz,
wolfSSL 11:cee25a834751 150 const byte* authTag, word32 authTagSz,
wolfSSL 11:cee25a834751 151 const byte* authIn, word32 authInSz)
wolfSSL 11:cee25a834751 152 {
wolfSSL 11:cee25a834751 153 return AesCcmDecrypt(aes, out, in, inSz, nonce, nonceSz,
wolfSSL 11:cee25a834751 154 authTag, authTagSz, authIn, authInSz);
wolfSSL 11:cee25a834751 155 }
wolfSSL 11:cee25a834751 156 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 157 #endif /* HAVE_AESCCM */
wolfSSL 11:cee25a834751 158
wolfSSL 11:cee25a834751 159 int wc_AesInit(Aes* aes, void* h, int i)
wolfSSL 11:cee25a834751 160 {
wolfSSL 11:cee25a834751 161 (void)aes;
wolfSSL 11:cee25a834751 162 (void)h;
wolfSSL 11:cee25a834751 163 (void)i;
wolfSSL 11:cee25a834751 164 /* FIPS doesn't support:
wolfSSL 11:cee25a834751 165 return AesInit(aes, h, i); */
wolfSSL 11:cee25a834751 166 return 0;
wolfSSL 11:cee25a834751 167 }
wolfSSL 11:cee25a834751 168 void wc_AesFree(Aes* aes)
wolfSSL 11:cee25a834751 169 {
wolfSSL 11:cee25a834751 170 (void)aes;
wolfSSL 11:cee25a834751 171 /* FIPS doesn't support:
wolfSSL 11:cee25a834751 172 AesFree(aes); */
wolfSSL 11:cee25a834751 173 }
wolfSSL 11:cee25a834751 174
wolfSSL 11:cee25a834751 175 #else /* HAVE_FIPS */
wolfSSL 11:cee25a834751 176
wolfSSL 11:cee25a834751 177
wolfSSL 11:cee25a834751 178 #if defined(WOLFSSL_TI_CRYPT)
wolfSSL 11:cee25a834751 179 #include <wolfcrypt/src/port/ti/ti-aes.c>
wolfSSL 11:cee25a834751 180 #else
wolfSSL 11:cee25a834751 181
wolfSSL 11:cee25a834751 182 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 11:cee25a834751 183 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 11:cee25a834751 184
wolfSSL 11:cee25a834751 185 #ifdef NO_INLINE
wolfSSL 11:cee25a834751 186 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 11:cee25a834751 187 #else
wolfSSL 11:cee25a834751 188 #define WOLFSSL_MISC_INCLUDED
wolfSSL 11:cee25a834751 189 #include <wolfcrypt/src/misc.c>
wolfSSL 11:cee25a834751 190 #endif
wolfSSL 11:cee25a834751 191
wolfSSL 11:cee25a834751 192 #ifdef DEBUG_AESNI
wolfSSL 11:cee25a834751 193 #include <stdio.h>
wolfSSL 11:cee25a834751 194 #endif
wolfSSL 11:cee25a834751 195
wolfSSL 11:cee25a834751 196 #ifdef _MSC_VER
wolfSSL 11:cee25a834751 197 /* 4127 warning constant while(1) */
wolfSSL 11:cee25a834751 198 #pragma warning(disable: 4127)
wolfSSL 11:cee25a834751 199 #endif
wolfSSL 11:cee25a834751 200
wolfSSL 11:cee25a834751 201
wolfSSL 11:cee25a834751 202 /* Define AES implementation includes and functions */
wolfSSL 11:cee25a834751 203 #if defined(STM32F2_CRYPTO) || defined(STM32F4_CRYPTO)
wolfSSL 11:cee25a834751 204 /* STM32F2/F4 hardware AES support for CBC, CTR modes */
wolfSSL 11:cee25a834751 205
wolfSSL 11:cee25a834751 206 #if defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
wolfSSL 11:cee25a834751 207 static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 11:cee25a834751 208 {
wolfSSL 11:cee25a834751 209 int ret = 0;
wolfSSL 11:cee25a834751 210 #ifdef WOLFSSL_STM32_CUBEMX
wolfSSL 11:cee25a834751 211 CRYP_HandleTypeDef hcryp;
wolfSSL 11:cee25a834751 212 XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef));
wolfSSL 11:cee25a834751 213
wolfSSL 11:cee25a834751 214 /* load key into correct registers */
wolfSSL 11:cee25a834751 215 switch(aes->rounds) {
wolfSSL 11:cee25a834751 216 case 10: /* 128-bit key */
wolfSSL 11:cee25a834751 217 hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
wolfSSL 11:cee25a834751 218 break;
wolfSSL 11:cee25a834751 219 case 12: /* 192-bit key */
wolfSSL 11:cee25a834751 220 hcryp.Init.KeySize = CRYP_KEYSIZE_192B;
wolfSSL 11:cee25a834751 221 break;
wolfSSL 11:cee25a834751 222 case 14: /* 256-bit key */
wolfSSL 11:cee25a834751 223 hcryp.Init.KeySize = CRYP_KEYSIZE_256B;
wolfSSL 11:cee25a834751 224 break;
wolfSSL 11:cee25a834751 225 default:
wolfSSL 11:cee25a834751 226 break;
wolfSSL 11:cee25a834751 227 }
wolfSSL 11:cee25a834751 228 hcryp.Instance = CRYP;
wolfSSL 11:cee25a834751 229 hcryp.Init.DataType = CRYP_DATATYPE_8B;
wolfSSL 11:cee25a834751 230 hcryp.Init.pKey = (uint8_t*)aes->key;
wolfSSL 11:cee25a834751 231
wolfSSL 11:cee25a834751 232 HAL_CRYP_Init(&hcryp);
wolfSSL 11:cee25a834751 233
wolfSSL 11:cee25a834751 234 if (HAL_CRYP_AESECB_Encrypt(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
wolfSSL 11:cee25a834751 235 outBlock, STM32_HAL_TIMEOUT) != HAL_OK) {
wolfSSL 11:cee25a834751 236 ret = WC_TIMEOUT_E;
wolfSSL 11:cee25a834751 237 }
wolfSSL 11:cee25a834751 238
wolfSSL 11:cee25a834751 239 HAL_CRYP_DeInit(&hcryp);
wolfSSL 11:cee25a834751 240 #else
wolfSSL 11:cee25a834751 241 word32 *enc_key;
wolfSSL 11:cee25a834751 242 CRYP_InitTypeDef AES_CRYP_InitStructure;
wolfSSL 11:cee25a834751 243 CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
wolfSSL 11:cee25a834751 244
wolfSSL 11:cee25a834751 245 enc_key = aes->key;
wolfSSL 11:cee25a834751 246
wolfSSL 11:cee25a834751 247 /* crypto structure initialization */
wolfSSL 11:cee25a834751 248 CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
wolfSSL 11:cee25a834751 249 CRYP_StructInit(&AES_CRYP_InitStructure);
wolfSSL 11:cee25a834751 250
wolfSSL 11:cee25a834751 251 /* reset registers to their default values */
wolfSSL 11:cee25a834751 252 CRYP_DeInit();
wolfSSL 11:cee25a834751 253
wolfSSL 11:cee25a834751 254 /* load key into correct registers */
wolfSSL 11:cee25a834751 255 switch(aes->rounds)
wolfSSL 11:cee25a834751 256 {
wolfSSL 11:cee25a834751 257 case 10: /* 128-bit key */
wolfSSL 11:cee25a834751 258 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
wolfSSL 11:cee25a834751 259 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[0];
wolfSSL 11:cee25a834751 260 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[1];
wolfSSL 11:cee25a834751 261 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[2];
wolfSSL 11:cee25a834751 262 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[3];
wolfSSL 11:cee25a834751 263 break;
wolfSSL 11:cee25a834751 264
wolfSSL 11:cee25a834751 265 case 12: /* 192-bit key */
wolfSSL 11:cee25a834751 266 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
wolfSSL 11:cee25a834751 267 AES_CRYP_KeyInitStructure.CRYP_Key1Left = enc_key[0];
wolfSSL 11:cee25a834751 268 AES_CRYP_KeyInitStructure.CRYP_Key1Right = enc_key[1];
wolfSSL 11:cee25a834751 269 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[2];
wolfSSL 11:cee25a834751 270 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[3];
wolfSSL 11:cee25a834751 271 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[4];
wolfSSL 11:cee25a834751 272 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[5];
wolfSSL 11:cee25a834751 273 break;
wolfSSL 11:cee25a834751 274
wolfSSL 11:cee25a834751 275 case 14: /* 256-bit key */
wolfSSL 11:cee25a834751 276 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
wolfSSL 11:cee25a834751 277 AES_CRYP_KeyInitStructure.CRYP_Key0Left = enc_key[0];
wolfSSL 11:cee25a834751 278 AES_CRYP_KeyInitStructure.CRYP_Key0Right = enc_key[1];
wolfSSL 11:cee25a834751 279 AES_CRYP_KeyInitStructure.CRYP_Key1Left = enc_key[2];
wolfSSL 11:cee25a834751 280 AES_CRYP_KeyInitStructure.CRYP_Key1Right = enc_key[3];
wolfSSL 11:cee25a834751 281 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[4];
wolfSSL 11:cee25a834751 282 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[5];
wolfSSL 11:cee25a834751 283 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[6];
wolfSSL 11:cee25a834751 284 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[7];
wolfSSL 11:cee25a834751 285 break;
wolfSSL 11:cee25a834751 286
wolfSSL 11:cee25a834751 287 default:
wolfSSL 11:cee25a834751 288 break;
wolfSSL 11:cee25a834751 289 }
wolfSSL 11:cee25a834751 290 CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
wolfSSL 11:cee25a834751 291
wolfSSL 11:cee25a834751 292 /* set direction, mode, and datatype */
wolfSSL 11:cee25a834751 293 AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
wolfSSL 11:cee25a834751 294 AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_ECB;
wolfSSL 11:cee25a834751 295 AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 11:cee25a834751 296 CRYP_Init(&AES_CRYP_InitStructure);
wolfSSL 11:cee25a834751 297
wolfSSL 11:cee25a834751 298 /* enable crypto processor */
wolfSSL 11:cee25a834751 299 CRYP_Cmd(ENABLE);
wolfSSL 11:cee25a834751 300
wolfSSL 11:cee25a834751 301 /* flush IN/OUT FIFOs */
wolfSSL 11:cee25a834751 302 CRYP_FIFOFlush();
wolfSSL 11:cee25a834751 303
wolfSSL 11:cee25a834751 304 CRYP_DataIn(*(uint32_t*)&inBlock[0]);
wolfSSL 11:cee25a834751 305 CRYP_DataIn(*(uint32_t*)&inBlock[4]);
wolfSSL 11:cee25a834751 306 CRYP_DataIn(*(uint32_t*)&inBlock[8]);
wolfSSL 11:cee25a834751 307 CRYP_DataIn(*(uint32_t*)&inBlock[12]);
wolfSSL 11:cee25a834751 308
wolfSSL 11:cee25a834751 309 /* wait until the complete message has been processed */
wolfSSL 11:cee25a834751 310 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 11:cee25a834751 311
wolfSSL 11:cee25a834751 312 *(uint32_t*)&outBlock[0] = CRYP_DataOut();
wolfSSL 11:cee25a834751 313 *(uint32_t*)&outBlock[4] = CRYP_DataOut();
wolfSSL 11:cee25a834751 314 *(uint32_t*)&outBlock[8] = CRYP_DataOut();
wolfSSL 11:cee25a834751 315 *(uint32_t*)&outBlock[12] = CRYP_DataOut();
wolfSSL 11:cee25a834751 316
wolfSSL 11:cee25a834751 317 /* disable crypto processor */
wolfSSL 11:cee25a834751 318 CRYP_Cmd(DISABLE);
wolfSSL 11:cee25a834751 319 #endif /* WOLFSSL_STM32_CUBEMX */
wolfSSL 11:cee25a834751 320 return ret;
wolfSSL 11:cee25a834751 321 }
wolfSSL 11:cee25a834751 322 #endif /* WOLFSSL_AES_DIRECT || HAVE_AESGCM || HAVE_AESCCM */
wolfSSL 11:cee25a834751 323
wolfSSL 11:cee25a834751 324 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 325 #if defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESCCM)
wolfSSL 11:cee25a834751 326 static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 11:cee25a834751 327 {
wolfSSL 11:cee25a834751 328 int ret = 0;
wolfSSL 11:cee25a834751 329 #ifdef WOLFSSL_STM32_CUBEMX
wolfSSL 11:cee25a834751 330 CRYP_HandleTypeDef hcryp;
wolfSSL 11:cee25a834751 331 XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef));
wolfSSL 11:cee25a834751 332 /* load key into correct registers */
wolfSSL 11:cee25a834751 333 switch(aes->rounds) {
wolfSSL 11:cee25a834751 334 case 10: /* 128-bit key */
wolfSSL 11:cee25a834751 335 hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
wolfSSL 11:cee25a834751 336 break;
wolfSSL 11:cee25a834751 337 case 12: /* 192-bit key */
wolfSSL 11:cee25a834751 338 hcryp.Init.KeySize = CRYP_KEYSIZE_192B;
wolfSSL 11:cee25a834751 339 break;
wolfSSL 11:cee25a834751 340 case 14: /* 256-bit key */
wolfSSL 11:cee25a834751 341 hcryp.Init.KeySize = CRYP_KEYSIZE_256B;
wolfSSL 11:cee25a834751 342 break;
wolfSSL 11:cee25a834751 343 default:
wolfSSL 11:cee25a834751 344 break;
wolfSSL 11:cee25a834751 345 }
wolfSSL 11:cee25a834751 346 hcryp.Instance = CRYP;
wolfSSL 11:cee25a834751 347 hcryp.Init.DataType = CRYP_DATATYPE_8B;
wolfSSL 11:cee25a834751 348 hcryp.Init.pKey = (uint8_t*)aes->key;
wolfSSL 11:cee25a834751 349
wolfSSL 11:cee25a834751 350 HAL_CRYP_Init(&hcryp);
wolfSSL 11:cee25a834751 351
wolfSSL 11:cee25a834751 352 if (HAL_CRYP_AESECB_Decrypt(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
wolfSSL 11:cee25a834751 353 outBlock, STM32_HAL_TIMEOUT) != HAL_OK) {
wolfSSL 11:cee25a834751 354 ret = WC_TIMEOUT_E;
wolfSSL 11:cee25a834751 355 }
wolfSSL 11:cee25a834751 356
wolfSSL 11:cee25a834751 357 HAL_CRYP_DeInit(&hcryp);
wolfSSL 11:cee25a834751 358 #else
wolfSSL 11:cee25a834751 359 #error AES Decrypt not implemented for STM32 StdPeri lib
wolfSSL 11:cee25a834751 360 #endif /* WOLFSSL_STM32_CUBEMX */
wolfSSL 11:cee25a834751 361 return ret;
wolfSSL 11:cee25a834751 362 }
wolfSSL 11:cee25a834751 363 #endif /* WOLFSSL_AES_DIRECT || HAVE_AESCCM */
wolfSSL 11:cee25a834751 364 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 365
wolfSSL 11:cee25a834751 366 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 11:cee25a834751 367 /* Freescale Coldfire SEC support for CBC mode.
wolfSSL 11:cee25a834751 368 * NOTE: no support for AES-CTR/GCM/CCM/Direct */
wolfSSL 11:cee25a834751 369 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 11:cee25a834751 370 #include "sec.h"
wolfSSL 11:cee25a834751 371 #include "mcf5475_sec.h"
wolfSSL 11:cee25a834751 372 #include "mcf5475_siu.h"
wolfSSL 11:cee25a834751 373 #elif defined(FREESCALE_LTC)
wolfSSL 11:cee25a834751 374 #include "fsl_ltc.h"
wolfSSL 11:cee25a834751 375 #if defined(FREESCALE_LTC_AES_GCM)
wolfSSL 11:cee25a834751 376 #undef NEED_AES_TABLES
wolfSSL 11:cee25a834751 377 #undef GCM_TABLE
wolfSSL 11:cee25a834751 378 #else
wolfSSL 11:cee25a834751 379 /* if LTC doesn't have GCM, use software with LTC AES ECB mode */
wolfSSL 11:cee25a834751 380 static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 11:cee25a834751 381 {
wolfSSL 11:cee25a834751 382 wc_AesEncryptDirect(aes, outBlock, inBlock);
wolfSSL 11:cee25a834751 383 return 0;
wolfSSL 11:cee25a834751 384 }
wolfSSL 11:cee25a834751 385 static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 11:cee25a834751 386 {
wolfSSL 11:cee25a834751 387 wc_AesDecryptDirect(aes, outBlock, inBlock);
wolfSSL 11:cee25a834751 388 return 0;
wolfSSL 11:cee25a834751 389 }
wolfSSL 11:cee25a834751 390 #endif
wolfSSL 11:cee25a834751 391 #elif defined(FREESCALE_MMCAU)
wolfSSL 11:cee25a834751 392 /* Freescale mmCAU hardware AES support for Direct, CBC, CCM, GCM modes
wolfSSL 11:cee25a834751 393 * through the CAU/mmCAU library. Documentation located in
wolfSSL 11:cee25a834751 394 * ColdFire/ColdFire+ CAU and Kinetis mmCAU Software Library User
wolfSSL 11:cee25a834751 395 * Guide (See note in README). */
wolfSSL 11:cee25a834751 396 #include "fsl_mmcau.h"
wolfSSL 11:cee25a834751 397
wolfSSL 11:cee25a834751 398 static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 11:cee25a834751 399 {
wolfSSL 11:cee25a834751 400 int ret = wolfSSL_CryptHwMutexLock();
wolfSSL 11:cee25a834751 401 if(ret == 0) {
wolfSSL 11:cee25a834751 402 MMCAU_AES_EncryptEcb(inBlock, (byte*)aes->key, aes->rounds, outBlock);
wolfSSL 11:cee25a834751 403 wolfSSL_CryptHwMutexUnLock();
wolfSSL 11:cee25a834751 404 }
wolfSSL 11:cee25a834751 405 return ret;
wolfSSL 11:cee25a834751 406 }
wolfSSL 11:cee25a834751 407 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 408 static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 11:cee25a834751 409 {
wolfSSL 11:cee25a834751 410 int ret = wolfSSL_CryptHwMutexLock();
wolfSSL 11:cee25a834751 411 if(ret == 0) {
wolfSSL 11:cee25a834751 412 MMCAU_AES_DecryptEcb(inBlock, (byte*)aes->key, aes->rounds, outBlock);
wolfSSL 11:cee25a834751 413 wolfSSL_CryptHwMutexUnLock();
wolfSSL 11:cee25a834751 414 }
wolfSSL 11:cee25a834751 415 return ret;
wolfSSL 11:cee25a834751 416 }
wolfSSL 11:cee25a834751 417 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 418
wolfSSL 11:cee25a834751 419 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 11:cee25a834751 420 /* NOTE: no support for AES-CCM/Direct */
wolfSSL 11:cee25a834751 421 #define DEBUG_WOLFSSL
wolfSSL 11:cee25a834751 422 #include "wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h"
wolfSSL 11:cee25a834751 423
wolfSSL 11:cee25a834751 424 #elif defined(WOLFSSL_NRF51_AES)
wolfSSL 11:cee25a834751 425 /* Use built-in AES hardware - AES 128 ECB Encrypt Only */
wolfSSL 11:cee25a834751 426 #include "wolfssl/wolfcrypt/port/nrf51.h"
wolfSSL 11:cee25a834751 427
wolfSSL 11:cee25a834751 428 static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 11:cee25a834751 429 {
wolfSSL 11:cee25a834751 430 return nrf51_aes_encrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
wolfSSL 11:cee25a834751 431 }
wolfSSL 11:cee25a834751 432
wolfSSL 11:cee25a834751 433 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 434 #error nRF51 AES Hardware does not support decrypt
wolfSSL 11:cee25a834751 435 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 436
wolfSSL 11:cee25a834751 437
wolfSSL 11:cee25a834751 438 #elif defined(WOLFSSL_AESNI)
wolfSSL 11:cee25a834751 439
wolfSSL 11:cee25a834751 440 #define NEED_AES_TABLES
wolfSSL 11:cee25a834751 441
wolfSSL 11:cee25a834751 442 /* Each platform needs to query info type 1 from cpuid to see if aesni is
wolfSSL 11:cee25a834751 443 * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
wolfSSL 11:cee25a834751 444 */
wolfSSL 11:cee25a834751 445
wolfSSL 11:cee25a834751 446 #ifndef AESNI_ALIGN
wolfSSL 11:cee25a834751 447 #define AESNI_ALIGN 16
wolfSSL 11:cee25a834751 448 #endif
wolfSSL 11:cee25a834751 449
wolfSSL 11:cee25a834751 450 #ifndef _MSC_VER
wolfSSL 11:cee25a834751 451 #define cpuid(reg, func)\
wolfSSL 11:cee25a834751 452 __asm__ __volatile__ ("cpuid":\
wolfSSL 11:cee25a834751 453 "=a" (reg[0]), "=b" (reg[1]), "=c" (reg[2]), "=d" (reg[3]) :\
wolfSSL 11:cee25a834751 454 "a" (func));
wolfSSL 11:cee25a834751 455
wolfSSL 11:cee25a834751 456 #define XASM_LINK(f) asm(f)
wolfSSL 11:cee25a834751 457 #else
wolfSSL 11:cee25a834751 458
wolfSSL 11:cee25a834751 459 #include <intrin.h>
wolfSSL 11:cee25a834751 460 #define cpuid(a,b) __cpuid((int*)a,b)
wolfSSL 11:cee25a834751 461
wolfSSL 11:cee25a834751 462 #define XASM_LINK(f)
wolfSSL 11:cee25a834751 463 #endif /* _MSC_VER */
wolfSSL 11:cee25a834751 464
wolfSSL 11:cee25a834751 465
wolfSSL 11:cee25a834751 466 static int Check_CPU_support_AES(void)
wolfSSL 11:cee25a834751 467 {
wolfSSL 11:cee25a834751 468 unsigned int reg[4]; /* put a,b,c,d into 0,1,2,3 */
wolfSSL 11:cee25a834751 469 cpuid(reg, 1); /* query info 1 */
wolfSSL 11:cee25a834751 470
wolfSSL 11:cee25a834751 471 if (reg[2] & 0x2000000)
wolfSSL 11:cee25a834751 472 return 1;
wolfSSL 11:cee25a834751 473
wolfSSL 11:cee25a834751 474 return 0;
wolfSSL 11:cee25a834751 475 }
wolfSSL 11:cee25a834751 476
wolfSSL 11:cee25a834751 477 static int checkAESNI = 0;
wolfSSL 11:cee25a834751 478 static int haveAESNI = 0;
wolfSSL 11:cee25a834751 479
wolfSSL 11:cee25a834751 480
wolfSSL 11:cee25a834751 481 /* tell C compiler these are asm functions in case any mix up of ABI underscore
wolfSSL 11:cee25a834751 482 prefix between clang/gcc/llvm etc */
wolfSSL 11:cee25a834751 483 #ifdef HAVE_AES_CBC
wolfSSL 11:cee25a834751 484 void AES_CBC_encrypt(const unsigned char* in, unsigned char* out,
wolfSSL 11:cee25a834751 485 unsigned char* ivec, unsigned long length,
wolfSSL 11:cee25a834751 486 const unsigned char* KS, int nr)
wolfSSL 11:cee25a834751 487 XASM_LINK("AES_CBC_encrypt");
wolfSSL 11:cee25a834751 488
wolfSSL 11:cee25a834751 489 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 490 #if defined(WOLFSSL_AESNI_BY4)
wolfSSL 11:cee25a834751 491 void AES_CBC_decrypt_by4(const unsigned char* in, unsigned char* out,
wolfSSL 11:cee25a834751 492 unsigned char* ivec, unsigned long length,
wolfSSL 11:cee25a834751 493 const unsigned char* KS, int nr)
wolfSSL 11:cee25a834751 494 XASM_LINK("AES_CBC_decrypt_by4");
wolfSSL 11:cee25a834751 495 #elif defined(WOLFSSL_AESNI_BY6)
wolfSSL 11:cee25a834751 496 void AES_CBC_decrypt_by6(const unsigned char* in, unsigned char* out,
wolfSSL 11:cee25a834751 497 unsigned char* ivec, unsigned long length,
wolfSSL 11:cee25a834751 498 const unsigned char* KS, int nr)
wolfSSL 11:cee25a834751 499 XASM_LINK("AES_CBC_decrypt_by6");
wolfSSL 11:cee25a834751 500 #else /* WOLFSSL_AESNI_BYx */
wolfSSL 11:cee25a834751 501 void AES_CBC_decrypt_by8(const unsigned char* in, unsigned char* out,
wolfSSL 11:cee25a834751 502 unsigned char* ivec, unsigned long length,
wolfSSL 11:cee25a834751 503 const unsigned char* KS, int nr)
wolfSSL 11:cee25a834751 504 XASM_LINK("AES_CBC_decrypt_by8");
wolfSSL 11:cee25a834751 505 #endif /* WOLFSSL_AESNI_BYx */
wolfSSL 11:cee25a834751 506 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 507 #endif /* HAVE_AES_CBC */
wolfSSL 11:cee25a834751 508
wolfSSL 11:cee25a834751 509 void AES_ECB_encrypt(const unsigned char* in, unsigned char* out,
wolfSSL 11:cee25a834751 510 unsigned long length, const unsigned char* KS, int nr)
wolfSSL 11:cee25a834751 511 XASM_LINK("AES_ECB_encrypt");
wolfSSL 11:cee25a834751 512
wolfSSL 11:cee25a834751 513 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 514 void AES_ECB_decrypt(const unsigned char* in, unsigned char* out,
wolfSSL 11:cee25a834751 515 unsigned long length, const unsigned char* KS, int nr)
wolfSSL 11:cee25a834751 516 XASM_LINK("AES_ECB_decrypt");
wolfSSL 11:cee25a834751 517 #endif
wolfSSL 11:cee25a834751 518
wolfSSL 11:cee25a834751 519 void AES_128_Key_Expansion(const unsigned char* userkey,
wolfSSL 11:cee25a834751 520 unsigned char* key_schedule)
wolfSSL 11:cee25a834751 521 XASM_LINK("AES_128_Key_Expansion");
wolfSSL 11:cee25a834751 522
wolfSSL 11:cee25a834751 523 void AES_192_Key_Expansion(const unsigned char* userkey,
wolfSSL 11:cee25a834751 524 unsigned char* key_schedule)
wolfSSL 11:cee25a834751 525 XASM_LINK("AES_192_Key_Expansion");
wolfSSL 11:cee25a834751 526
wolfSSL 11:cee25a834751 527 void AES_256_Key_Expansion(const unsigned char* userkey,
wolfSSL 11:cee25a834751 528 unsigned char* key_schedule)
wolfSSL 11:cee25a834751 529 XASM_LINK("AES_256_Key_Expansion");
wolfSSL 11:cee25a834751 530
wolfSSL 11:cee25a834751 531
wolfSSL 11:cee25a834751 532 static int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
wolfSSL 11:cee25a834751 533 Aes* aes)
wolfSSL 11:cee25a834751 534 {
wolfSSL 11:cee25a834751 535 int ret;
wolfSSL 11:cee25a834751 536
wolfSSL 11:cee25a834751 537 if (!userKey || !aes)
wolfSSL 11:cee25a834751 538 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 539
wolfSSL 11:cee25a834751 540 switch (bits) {
wolfSSL 11:cee25a834751 541 case 128:
wolfSSL 11:cee25a834751 542 AES_128_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 10;
wolfSSL 11:cee25a834751 543 return 0;
wolfSSL 11:cee25a834751 544 case 192:
wolfSSL 11:cee25a834751 545 AES_192_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 12;
wolfSSL 11:cee25a834751 546 return 0;
wolfSSL 11:cee25a834751 547 case 256:
wolfSSL 11:cee25a834751 548 AES_256_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 14;
wolfSSL 11:cee25a834751 549 return 0;
wolfSSL 11:cee25a834751 550 default:
wolfSSL 11:cee25a834751 551 ret = BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 552 }
wolfSSL 11:cee25a834751 553
wolfSSL 11:cee25a834751 554 return ret;
wolfSSL 11:cee25a834751 555 }
wolfSSL 11:cee25a834751 556
wolfSSL 11:cee25a834751 557 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 558 static int AES_set_decrypt_key(const unsigned char* userKey,
wolfSSL 11:cee25a834751 559 const int bits, Aes* aes)
wolfSSL 11:cee25a834751 560 {
wolfSSL 11:cee25a834751 561 int nr;
wolfSSL 11:cee25a834751 562 Aes temp_key;
wolfSSL 11:cee25a834751 563 __m128i *Key_Schedule = (__m128i*)aes->key;
wolfSSL 11:cee25a834751 564 __m128i *Temp_Key_Schedule = (__m128i*)temp_key.key;
wolfSSL 11:cee25a834751 565
wolfSSL 11:cee25a834751 566 if (!userKey || !aes)
wolfSSL 11:cee25a834751 567 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 568
wolfSSL 11:cee25a834751 569 if (AES_set_encrypt_key(userKey,bits,&temp_key) == BAD_FUNC_ARG)
wolfSSL 11:cee25a834751 570 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 571
wolfSSL 11:cee25a834751 572 nr = temp_key.rounds;
wolfSSL 11:cee25a834751 573 aes->rounds = nr;
wolfSSL 11:cee25a834751 574
wolfSSL 11:cee25a834751 575 Key_Schedule[nr] = Temp_Key_Schedule[0];
wolfSSL 11:cee25a834751 576 Key_Schedule[nr-1] = _mm_aesimc_si128(Temp_Key_Schedule[1]);
wolfSSL 11:cee25a834751 577 Key_Schedule[nr-2] = _mm_aesimc_si128(Temp_Key_Schedule[2]);
wolfSSL 11:cee25a834751 578 Key_Schedule[nr-3] = _mm_aesimc_si128(Temp_Key_Schedule[3]);
wolfSSL 11:cee25a834751 579 Key_Schedule[nr-4] = _mm_aesimc_si128(Temp_Key_Schedule[4]);
wolfSSL 11:cee25a834751 580 Key_Schedule[nr-5] = _mm_aesimc_si128(Temp_Key_Schedule[5]);
wolfSSL 11:cee25a834751 581 Key_Schedule[nr-6] = _mm_aesimc_si128(Temp_Key_Schedule[6]);
wolfSSL 11:cee25a834751 582 Key_Schedule[nr-7] = _mm_aesimc_si128(Temp_Key_Schedule[7]);
wolfSSL 11:cee25a834751 583 Key_Schedule[nr-8] = _mm_aesimc_si128(Temp_Key_Schedule[8]);
wolfSSL 11:cee25a834751 584 Key_Schedule[nr-9] = _mm_aesimc_si128(Temp_Key_Schedule[9]);
wolfSSL 11:cee25a834751 585
wolfSSL 11:cee25a834751 586 if (nr>10) {
wolfSSL 11:cee25a834751 587 Key_Schedule[nr-10] = _mm_aesimc_si128(Temp_Key_Schedule[10]);
wolfSSL 11:cee25a834751 588 Key_Schedule[nr-11] = _mm_aesimc_si128(Temp_Key_Schedule[11]);
wolfSSL 11:cee25a834751 589 }
wolfSSL 11:cee25a834751 590
wolfSSL 11:cee25a834751 591 if (nr>12) {
wolfSSL 11:cee25a834751 592 Key_Schedule[nr-12] = _mm_aesimc_si128(Temp_Key_Schedule[12]);
wolfSSL 11:cee25a834751 593 Key_Schedule[nr-13] = _mm_aesimc_si128(Temp_Key_Schedule[13]);
wolfSSL 11:cee25a834751 594 }
wolfSSL 11:cee25a834751 595
wolfSSL 11:cee25a834751 596 Key_Schedule[0] = Temp_Key_Schedule[nr];
wolfSSL 11:cee25a834751 597
wolfSSL 11:cee25a834751 598 return 0;
wolfSSL 11:cee25a834751 599 }
wolfSSL 11:cee25a834751 600 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 601
wolfSSL 11:cee25a834751 602 #else
wolfSSL 11:cee25a834751 603
wolfSSL 11:cee25a834751 604 /* using wolfCrypt software AES implementation */
wolfSSL 11:cee25a834751 605 #define NEED_AES_TABLES
wolfSSL 11:cee25a834751 606 #endif
wolfSSL 11:cee25a834751 607
wolfSSL 11:cee25a834751 608
wolfSSL 11:cee25a834751 609
wolfSSL 11:cee25a834751 610 #ifdef NEED_AES_TABLES
wolfSSL 11:cee25a834751 611
wolfSSL 11:cee25a834751 612 static const word32 rcon[] = {
wolfSSL 11:cee25a834751 613 0x01000000, 0x02000000, 0x04000000, 0x08000000,
wolfSSL 11:cee25a834751 614 0x10000000, 0x20000000, 0x40000000, 0x80000000,
wolfSSL 11:cee25a834751 615 0x1B000000, 0x36000000,
wolfSSL 11:cee25a834751 616 /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
wolfSSL 11:cee25a834751 617 };
wolfSSL 11:cee25a834751 618
wolfSSL 11:cee25a834751 619 static const word32 Te[4][256] = {
wolfSSL 11:cee25a834751 620 {
wolfSSL 11:cee25a834751 621 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
wolfSSL 11:cee25a834751 622 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
wolfSSL 11:cee25a834751 623 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
wolfSSL 11:cee25a834751 624 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU,
wolfSSL 11:cee25a834751 625 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U,
wolfSSL 11:cee25a834751 626 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU,
wolfSSL 11:cee25a834751 627 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU,
wolfSSL 11:cee25a834751 628 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU,
wolfSSL 11:cee25a834751 629 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU,
wolfSSL 11:cee25a834751 630 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU,
wolfSSL 11:cee25a834751 631 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U,
wolfSSL 11:cee25a834751 632 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU,
wolfSSL 11:cee25a834751 633 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU,
wolfSSL 11:cee25a834751 634 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U,
wolfSSL 11:cee25a834751 635 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU,
wolfSSL 11:cee25a834751 636 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU,
wolfSSL 11:cee25a834751 637 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU,
wolfSSL 11:cee25a834751 638 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU,
wolfSSL 11:cee25a834751 639 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU,
wolfSSL 11:cee25a834751 640 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U,
wolfSSL 11:cee25a834751 641 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU,
wolfSSL 11:cee25a834751 642 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU,
wolfSSL 11:cee25a834751 643 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU,
wolfSSL 11:cee25a834751 644 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU,
wolfSSL 11:cee25a834751 645 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U,
wolfSSL 11:cee25a834751 646 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U,
wolfSSL 11:cee25a834751 647 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U,
wolfSSL 11:cee25a834751 648 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U,
wolfSSL 11:cee25a834751 649 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU,
wolfSSL 11:cee25a834751 650 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U,
wolfSSL 11:cee25a834751 651 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U,
wolfSSL 11:cee25a834751 652 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU,
wolfSSL 11:cee25a834751 653 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU,
wolfSSL 11:cee25a834751 654 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U,
wolfSSL 11:cee25a834751 655 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U,
wolfSSL 11:cee25a834751 656 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U,
wolfSSL 11:cee25a834751 657 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU,
wolfSSL 11:cee25a834751 658 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U,
wolfSSL 11:cee25a834751 659 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU,
wolfSSL 11:cee25a834751 660 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U,
wolfSSL 11:cee25a834751 661 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU,
wolfSSL 11:cee25a834751 662 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U,
wolfSSL 11:cee25a834751 663 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U,
wolfSSL 11:cee25a834751 664 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU,
wolfSSL 11:cee25a834751 665 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U,
wolfSSL 11:cee25a834751 666 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U,
wolfSSL 11:cee25a834751 667 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U,
wolfSSL 11:cee25a834751 668 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U,
wolfSSL 11:cee25a834751 669 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U,
wolfSSL 11:cee25a834751 670 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U,
wolfSSL 11:cee25a834751 671 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U,
wolfSSL 11:cee25a834751 672 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U,
wolfSSL 11:cee25a834751 673 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU,
wolfSSL 11:cee25a834751 674 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U,
wolfSSL 11:cee25a834751 675 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U,
wolfSSL 11:cee25a834751 676 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U,
wolfSSL 11:cee25a834751 677 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U,
wolfSSL 11:cee25a834751 678 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U,
wolfSSL 11:cee25a834751 679 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U,
wolfSSL 11:cee25a834751 680 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU,
wolfSSL 11:cee25a834751 681 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U,
wolfSSL 11:cee25a834751 682 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U,
wolfSSL 11:cee25a834751 683 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U,
wolfSSL 11:cee25a834751 684 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU,
wolfSSL 11:cee25a834751 685 },
wolfSSL 11:cee25a834751 686 {
wolfSSL 11:cee25a834751 687 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU,
wolfSSL 11:cee25a834751 688 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U,
wolfSSL 11:cee25a834751 689 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU,
wolfSSL 11:cee25a834751 690 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U,
wolfSSL 11:cee25a834751 691 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU,
wolfSSL 11:cee25a834751 692 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U,
wolfSSL 11:cee25a834751 693 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU,
wolfSSL 11:cee25a834751 694 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U,
wolfSSL 11:cee25a834751 695 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U,
wolfSSL 11:cee25a834751 696 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU,
wolfSSL 11:cee25a834751 697 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U,
wolfSSL 11:cee25a834751 698 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U,
wolfSSL 11:cee25a834751 699 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U,
wolfSSL 11:cee25a834751 700 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU,
wolfSSL 11:cee25a834751 701 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U,
wolfSSL 11:cee25a834751 702 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U,
wolfSSL 11:cee25a834751 703 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU,
wolfSSL 11:cee25a834751 704 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U,
wolfSSL 11:cee25a834751 705 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U,
wolfSSL 11:cee25a834751 706 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U,
wolfSSL 11:cee25a834751 707 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU,
wolfSSL 11:cee25a834751 708 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU,
wolfSSL 11:cee25a834751 709 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U,
wolfSSL 11:cee25a834751 710 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU,
wolfSSL 11:cee25a834751 711 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU,
wolfSSL 11:cee25a834751 712 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U,
wolfSSL 11:cee25a834751 713 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU,
wolfSSL 11:cee25a834751 714 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U,
wolfSSL 11:cee25a834751 715 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU,
wolfSSL 11:cee25a834751 716 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U,
wolfSSL 11:cee25a834751 717 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U,
wolfSSL 11:cee25a834751 718 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U,
wolfSSL 11:cee25a834751 719 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU,
wolfSSL 11:cee25a834751 720 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U,
wolfSSL 11:cee25a834751 721 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU,
wolfSSL 11:cee25a834751 722 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U,
wolfSSL 11:cee25a834751 723 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU,
wolfSSL 11:cee25a834751 724 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U,
wolfSSL 11:cee25a834751 725 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U,
wolfSSL 11:cee25a834751 726 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU,
wolfSSL 11:cee25a834751 727 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU,
wolfSSL 11:cee25a834751 728 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU,
wolfSSL 11:cee25a834751 729 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U,
wolfSSL 11:cee25a834751 730 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U,
wolfSSL 11:cee25a834751 731 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU,
wolfSSL 11:cee25a834751 732 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U,
wolfSSL 11:cee25a834751 733 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU,
wolfSSL 11:cee25a834751 734 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U,
wolfSSL 11:cee25a834751 735 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU,
wolfSSL 11:cee25a834751 736 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U,
wolfSSL 11:cee25a834751 737 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU,
wolfSSL 11:cee25a834751 738 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU,
wolfSSL 11:cee25a834751 739 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U,
wolfSSL 11:cee25a834751 740 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU,
wolfSSL 11:cee25a834751 741 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U,
wolfSSL 11:cee25a834751 742 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU,
wolfSSL 11:cee25a834751 743 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U,
wolfSSL 11:cee25a834751 744 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U,
wolfSSL 11:cee25a834751 745 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U,
wolfSSL 11:cee25a834751 746 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU,
wolfSSL 11:cee25a834751 747 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU,
wolfSSL 11:cee25a834751 748 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U,
wolfSSL 11:cee25a834751 749 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU,
wolfSSL 11:cee25a834751 750 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U,
wolfSSL 11:cee25a834751 751 },
wolfSSL 11:cee25a834751 752 {
wolfSSL 11:cee25a834751 753 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU,
wolfSSL 11:cee25a834751 754 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U,
wolfSSL 11:cee25a834751 755 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU,
wolfSSL 11:cee25a834751 756 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U,
wolfSSL 11:cee25a834751 757 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU,
wolfSSL 11:cee25a834751 758 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U,
wolfSSL 11:cee25a834751 759 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU,
wolfSSL 11:cee25a834751 760 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U,
wolfSSL 11:cee25a834751 761 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U,
wolfSSL 11:cee25a834751 762 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU,
wolfSSL 11:cee25a834751 763 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U,
wolfSSL 11:cee25a834751 764 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U,
wolfSSL 11:cee25a834751 765 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U,
wolfSSL 11:cee25a834751 766 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU,
wolfSSL 11:cee25a834751 767 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U,
wolfSSL 11:cee25a834751 768 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U,
wolfSSL 11:cee25a834751 769 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU,
wolfSSL 11:cee25a834751 770 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U,
wolfSSL 11:cee25a834751 771 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U,
wolfSSL 11:cee25a834751 772 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U,
wolfSSL 11:cee25a834751 773 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU,
wolfSSL 11:cee25a834751 774 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU,
wolfSSL 11:cee25a834751 775 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U,
wolfSSL 11:cee25a834751 776 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU,
wolfSSL 11:cee25a834751 777 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU,
wolfSSL 11:cee25a834751 778 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U,
wolfSSL 11:cee25a834751 779 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU,
wolfSSL 11:cee25a834751 780 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U,
wolfSSL 11:cee25a834751 781 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU,
wolfSSL 11:cee25a834751 782 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U,
wolfSSL 11:cee25a834751 783 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U,
wolfSSL 11:cee25a834751 784 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U,
wolfSSL 11:cee25a834751 785 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU,
wolfSSL 11:cee25a834751 786 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U,
wolfSSL 11:cee25a834751 787 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU,
wolfSSL 11:cee25a834751 788 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U,
wolfSSL 11:cee25a834751 789 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU,
wolfSSL 11:cee25a834751 790 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U,
wolfSSL 11:cee25a834751 791 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U,
wolfSSL 11:cee25a834751 792 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU,
wolfSSL 11:cee25a834751 793 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU,
wolfSSL 11:cee25a834751 794 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU,
wolfSSL 11:cee25a834751 795 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U,
wolfSSL 11:cee25a834751 796 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U,
wolfSSL 11:cee25a834751 797 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU,
wolfSSL 11:cee25a834751 798 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U,
wolfSSL 11:cee25a834751 799 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU,
wolfSSL 11:cee25a834751 800 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U,
wolfSSL 11:cee25a834751 801 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU,
wolfSSL 11:cee25a834751 802 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U,
wolfSSL 11:cee25a834751 803 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU,
wolfSSL 11:cee25a834751 804 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU,
wolfSSL 11:cee25a834751 805 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U,
wolfSSL 11:cee25a834751 806 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU,
wolfSSL 11:cee25a834751 807 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U,
wolfSSL 11:cee25a834751 808 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU,
wolfSSL 11:cee25a834751 809 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U,
wolfSSL 11:cee25a834751 810 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U,
wolfSSL 11:cee25a834751 811 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U,
wolfSSL 11:cee25a834751 812 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU,
wolfSSL 11:cee25a834751 813 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU,
wolfSSL 11:cee25a834751 814 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U,
wolfSSL 11:cee25a834751 815 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU,
wolfSSL 11:cee25a834751 816 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U,
wolfSSL 11:cee25a834751 817 },
wolfSSL 11:cee25a834751 818 {
wolfSSL 11:cee25a834751 819 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U,
wolfSSL 11:cee25a834751 820 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U,
wolfSSL 11:cee25a834751 821 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U,
wolfSSL 11:cee25a834751 822 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU,
wolfSSL 11:cee25a834751 823 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU,
wolfSSL 11:cee25a834751 824 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU,
wolfSSL 11:cee25a834751 825 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U,
wolfSSL 11:cee25a834751 826 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU,
wolfSSL 11:cee25a834751 827 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU,
wolfSSL 11:cee25a834751 828 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U,
wolfSSL 11:cee25a834751 829 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U,
wolfSSL 11:cee25a834751 830 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU,
wolfSSL 11:cee25a834751 831 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU,
wolfSSL 11:cee25a834751 832 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU,
wolfSSL 11:cee25a834751 833 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU,
wolfSSL 11:cee25a834751 834 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU,
wolfSSL 11:cee25a834751 835 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U,
wolfSSL 11:cee25a834751 836 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU,
wolfSSL 11:cee25a834751 837 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU,
wolfSSL 11:cee25a834751 838 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U,
wolfSSL 11:cee25a834751 839 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U,
wolfSSL 11:cee25a834751 840 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U,
wolfSSL 11:cee25a834751 841 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U,
wolfSSL 11:cee25a834751 842 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U,
wolfSSL 11:cee25a834751 843 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU,
wolfSSL 11:cee25a834751 844 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U,
wolfSSL 11:cee25a834751 845 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU,
wolfSSL 11:cee25a834751 846 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU,
wolfSSL 11:cee25a834751 847 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U,
wolfSSL 11:cee25a834751 848 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U,
wolfSSL 11:cee25a834751 849 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U,
wolfSSL 11:cee25a834751 850 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU,
wolfSSL 11:cee25a834751 851 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U,
wolfSSL 11:cee25a834751 852 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU,
wolfSSL 11:cee25a834751 853 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU,
wolfSSL 11:cee25a834751 854 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U,
wolfSSL 11:cee25a834751 855 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U,
wolfSSL 11:cee25a834751 856 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU,
wolfSSL 11:cee25a834751 857 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U,
wolfSSL 11:cee25a834751 858 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU,
wolfSSL 11:cee25a834751 859 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U,
wolfSSL 11:cee25a834751 860 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U,
wolfSSL 11:cee25a834751 861 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U,
wolfSSL 11:cee25a834751 862 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U,
wolfSSL 11:cee25a834751 863 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU,
wolfSSL 11:cee25a834751 864 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U,
wolfSSL 11:cee25a834751 865 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU,
wolfSSL 11:cee25a834751 866 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U,
wolfSSL 11:cee25a834751 867 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU,
wolfSSL 11:cee25a834751 868 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U,
wolfSSL 11:cee25a834751 869 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU,
wolfSSL 11:cee25a834751 870 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU,
wolfSSL 11:cee25a834751 871 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU,
wolfSSL 11:cee25a834751 872 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU,
wolfSSL 11:cee25a834751 873 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U,
wolfSSL 11:cee25a834751 874 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U,
wolfSSL 11:cee25a834751 875 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U,
wolfSSL 11:cee25a834751 876 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U,
wolfSSL 11:cee25a834751 877 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U,
wolfSSL 11:cee25a834751 878 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U,
wolfSSL 11:cee25a834751 879 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU,
wolfSSL 11:cee25a834751 880 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U,
wolfSSL 11:cee25a834751 881 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU,
wolfSSL 11:cee25a834751 882 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU,
wolfSSL 11:cee25a834751 883 }
wolfSSL 11:cee25a834751 884 };
wolfSSL 11:cee25a834751 885
wolfSSL 11:cee25a834751 886 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 887 static const word32 Td[4][256] = {
wolfSSL 11:cee25a834751 888 {
wolfSSL 11:cee25a834751 889 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
wolfSSL 11:cee25a834751 890 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U,
wolfSSL 11:cee25a834751 891 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U,
wolfSSL 11:cee25a834751 892 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU,
wolfSSL 11:cee25a834751 893 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U,
wolfSSL 11:cee25a834751 894 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U,
wolfSSL 11:cee25a834751 895 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU,
wolfSSL 11:cee25a834751 896 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U,
wolfSSL 11:cee25a834751 897 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU,
wolfSSL 11:cee25a834751 898 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U,
wolfSSL 11:cee25a834751 899 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U,
wolfSSL 11:cee25a834751 900 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U,
wolfSSL 11:cee25a834751 901 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U,
wolfSSL 11:cee25a834751 902 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU,
wolfSSL 11:cee25a834751 903 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U,
wolfSSL 11:cee25a834751 904 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU,
wolfSSL 11:cee25a834751 905 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U,
wolfSSL 11:cee25a834751 906 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU,
wolfSSL 11:cee25a834751 907 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U,
wolfSSL 11:cee25a834751 908 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U,
wolfSSL 11:cee25a834751 909 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U,
wolfSSL 11:cee25a834751 910 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU,
wolfSSL 11:cee25a834751 911 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U,
wolfSSL 11:cee25a834751 912 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU,
wolfSSL 11:cee25a834751 913 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U,
wolfSSL 11:cee25a834751 914 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU,
wolfSSL 11:cee25a834751 915 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U,
wolfSSL 11:cee25a834751 916 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU,
wolfSSL 11:cee25a834751 917 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU,
wolfSSL 11:cee25a834751 918 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U,
wolfSSL 11:cee25a834751 919 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU,
wolfSSL 11:cee25a834751 920 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U,
wolfSSL 11:cee25a834751 921 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU,
wolfSSL 11:cee25a834751 922 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U,
wolfSSL 11:cee25a834751 923 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U,
wolfSSL 11:cee25a834751 924 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U,
wolfSSL 11:cee25a834751 925 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU,
wolfSSL 11:cee25a834751 926 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U,
wolfSSL 11:cee25a834751 927 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U,
wolfSSL 11:cee25a834751 928 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU,
wolfSSL 11:cee25a834751 929 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U,
wolfSSL 11:cee25a834751 930 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U,
wolfSSL 11:cee25a834751 931 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U,
wolfSSL 11:cee25a834751 932 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U,
wolfSSL 11:cee25a834751 933 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U,
wolfSSL 11:cee25a834751 934 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU,
wolfSSL 11:cee25a834751 935 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U,
wolfSSL 11:cee25a834751 936 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U,
wolfSSL 11:cee25a834751 937 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U,
wolfSSL 11:cee25a834751 938 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U,
wolfSSL 11:cee25a834751 939 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U,
wolfSSL 11:cee25a834751 940 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU,
wolfSSL 11:cee25a834751 941 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU,
wolfSSL 11:cee25a834751 942 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU,
wolfSSL 11:cee25a834751 943 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU,
wolfSSL 11:cee25a834751 944 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U,
wolfSSL 11:cee25a834751 945 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U,
wolfSSL 11:cee25a834751 946 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU,
wolfSSL 11:cee25a834751 947 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU,
wolfSSL 11:cee25a834751 948 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U,
wolfSSL 11:cee25a834751 949 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU,
wolfSSL 11:cee25a834751 950 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U,
wolfSSL 11:cee25a834751 951 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U,
wolfSSL 11:cee25a834751 952 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U,
wolfSSL 11:cee25a834751 953 },
wolfSSL 11:cee25a834751 954 {
wolfSSL 11:cee25a834751 955 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU,
wolfSSL 11:cee25a834751 956 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U,
wolfSSL 11:cee25a834751 957 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU,
wolfSSL 11:cee25a834751 958 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U,
wolfSSL 11:cee25a834751 959 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U,
wolfSSL 11:cee25a834751 960 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U,
wolfSSL 11:cee25a834751 961 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U,
wolfSSL 11:cee25a834751 962 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U,
wolfSSL 11:cee25a834751 963 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U,
wolfSSL 11:cee25a834751 964 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU,
wolfSSL 11:cee25a834751 965 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU,
wolfSSL 11:cee25a834751 966 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU,
wolfSSL 11:cee25a834751 967 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U,
wolfSSL 11:cee25a834751 968 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU,
wolfSSL 11:cee25a834751 969 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U,
wolfSSL 11:cee25a834751 970 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U,
wolfSSL 11:cee25a834751 971 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U,
wolfSSL 11:cee25a834751 972 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU,
wolfSSL 11:cee25a834751 973 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU,
wolfSSL 11:cee25a834751 974 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U,
wolfSSL 11:cee25a834751 975 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU,
wolfSSL 11:cee25a834751 976 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U,
wolfSSL 11:cee25a834751 977 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU,
wolfSSL 11:cee25a834751 978 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU,
wolfSSL 11:cee25a834751 979 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U,
wolfSSL 11:cee25a834751 980 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U,
wolfSSL 11:cee25a834751 981 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U,
wolfSSL 11:cee25a834751 982 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU,
wolfSSL 11:cee25a834751 983 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U,
wolfSSL 11:cee25a834751 984 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU,
wolfSSL 11:cee25a834751 985 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U,
wolfSSL 11:cee25a834751 986 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U,
wolfSSL 11:cee25a834751 987 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U,
wolfSSL 11:cee25a834751 988 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU,
wolfSSL 11:cee25a834751 989 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U,
wolfSSL 11:cee25a834751 990 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U,
wolfSSL 11:cee25a834751 991 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U,
wolfSSL 11:cee25a834751 992 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U,
wolfSSL 11:cee25a834751 993 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U,
wolfSSL 11:cee25a834751 994 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U,
wolfSSL 11:cee25a834751 995 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU,
wolfSSL 11:cee25a834751 996 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU,
wolfSSL 11:cee25a834751 997 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U,
wolfSSL 11:cee25a834751 998 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU,
wolfSSL 11:cee25a834751 999 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U,
wolfSSL 11:cee25a834751 1000 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU,
wolfSSL 11:cee25a834751 1001 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU,
wolfSSL 11:cee25a834751 1002 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U,
wolfSSL 11:cee25a834751 1003 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU,
wolfSSL 11:cee25a834751 1004 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U,
wolfSSL 11:cee25a834751 1005 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U,
wolfSSL 11:cee25a834751 1006 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U,
wolfSSL 11:cee25a834751 1007 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U,
wolfSSL 11:cee25a834751 1008 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U,
wolfSSL 11:cee25a834751 1009 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U,
wolfSSL 11:cee25a834751 1010 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U,
wolfSSL 11:cee25a834751 1011 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU,
wolfSSL 11:cee25a834751 1012 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U,
wolfSSL 11:cee25a834751 1013 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U,
wolfSSL 11:cee25a834751 1014 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU,
wolfSSL 11:cee25a834751 1015 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U,
wolfSSL 11:cee25a834751 1016 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U,
wolfSSL 11:cee25a834751 1017 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U,
wolfSSL 11:cee25a834751 1018 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U,
wolfSSL 11:cee25a834751 1019 },
wolfSSL 11:cee25a834751 1020 {
wolfSSL 11:cee25a834751 1021 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U,
wolfSSL 11:cee25a834751 1022 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U,
wolfSSL 11:cee25a834751 1023 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U,
wolfSSL 11:cee25a834751 1024 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U,
wolfSSL 11:cee25a834751 1025 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU,
wolfSSL 11:cee25a834751 1026 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U,
wolfSSL 11:cee25a834751 1027 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U,
wolfSSL 11:cee25a834751 1028 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U,
wolfSSL 11:cee25a834751 1029 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U,
wolfSSL 11:cee25a834751 1030 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU,
wolfSSL 11:cee25a834751 1031 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U,
wolfSSL 11:cee25a834751 1032 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U,
wolfSSL 11:cee25a834751 1033 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU,
wolfSSL 11:cee25a834751 1034 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U,
wolfSSL 11:cee25a834751 1035 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U,
wolfSSL 11:cee25a834751 1036 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U,
wolfSSL 11:cee25a834751 1037 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U,
wolfSSL 11:cee25a834751 1038 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U,
wolfSSL 11:cee25a834751 1039 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U,
wolfSSL 11:cee25a834751 1040 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU,
wolfSSL 11:cee25a834751 1041
wolfSSL 11:cee25a834751 1042 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U,
wolfSSL 11:cee25a834751 1043 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U,
wolfSSL 11:cee25a834751 1044 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U,
wolfSSL 11:cee25a834751 1045 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U,
wolfSSL 11:cee25a834751 1046 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U,
wolfSSL 11:cee25a834751 1047 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU,
wolfSSL 11:cee25a834751 1048 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU,
wolfSSL 11:cee25a834751 1049 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U,
wolfSSL 11:cee25a834751 1050 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU,
wolfSSL 11:cee25a834751 1051 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U,
wolfSSL 11:cee25a834751 1052 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU,
wolfSSL 11:cee25a834751 1053 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU,
wolfSSL 11:cee25a834751 1054 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU,
wolfSSL 11:cee25a834751 1055 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU,
wolfSSL 11:cee25a834751 1056 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U,
wolfSSL 11:cee25a834751 1057 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U,
wolfSSL 11:cee25a834751 1058 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U,
wolfSSL 11:cee25a834751 1059 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U,
wolfSSL 11:cee25a834751 1060 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U,
wolfSSL 11:cee25a834751 1061 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U,
wolfSSL 11:cee25a834751 1062 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U,
wolfSSL 11:cee25a834751 1063 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU,
wolfSSL 11:cee25a834751 1064 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU,
wolfSSL 11:cee25a834751 1065 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U,
wolfSSL 11:cee25a834751 1066 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U,
wolfSSL 11:cee25a834751 1067 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU,
wolfSSL 11:cee25a834751 1068 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU,
wolfSSL 11:cee25a834751 1069 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U,
wolfSSL 11:cee25a834751 1070 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U,
wolfSSL 11:cee25a834751 1071 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U,
wolfSSL 11:cee25a834751 1072 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U,
wolfSSL 11:cee25a834751 1073 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U,
wolfSSL 11:cee25a834751 1074 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U,
wolfSSL 11:cee25a834751 1075 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U,
wolfSSL 11:cee25a834751 1076 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU,
wolfSSL 11:cee25a834751 1077 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U,
wolfSSL 11:cee25a834751 1078 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U,
wolfSSL 11:cee25a834751 1079 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U,
wolfSSL 11:cee25a834751 1080 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U,
wolfSSL 11:cee25a834751 1081 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U,
wolfSSL 11:cee25a834751 1082 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U,
wolfSSL 11:cee25a834751 1083 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU,
wolfSSL 11:cee25a834751 1084 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U,
wolfSSL 11:cee25a834751 1085 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U,
wolfSSL 11:cee25a834751 1086 },
wolfSSL 11:cee25a834751 1087 {
wolfSSL 11:cee25a834751 1088 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU,
wolfSSL 11:cee25a834751 1089 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU,
wolfSSL 11:cee25a834751 1090 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U,
wolfSSL 11:cee25a834751 1091 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U,
wolfSSL 11:cee25a834751 1092 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU,
wolfSSL 11:cee25a834751 1093 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU,
wolfSSL 11:cee25a834751 1094 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U,
wolfSSL 11:cee25a834751 1095 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU,
wolfSSL 11:cee25a834751 1096 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U,
wolfSSL 11:cee25a834751 1097 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU,
wolfSSL 11:cee25a834751 1098 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U,
wolfSSL 11:cee25a834751 1099 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U,
wolfSSL 11:cee25a834751 1100 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U,
wolfSSL 11:cee25a834751 1101 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U,
wolfSSL 11:cee25a834751 1102 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U,
wolfSSL 11:cee25a834751 1103 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU,
wolfSSL 11:cee25a834751 1104 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU,
wolfSSL 11:cee25a834751 1105 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U,
wolfSSL 11:cee25a834751 1106 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U,
wolfSSL 11:cee25a834751 1107 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU,
wolfSSL 11:cee25a834751 1108 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU,
wolfSSL 11:cee25a834751 1109 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U,
wolfSSL 11:cee25a834751 1110 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U,
wolfSSL 11:cee25a834751 1111 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U,
wolfSSL 11:cee25a834751 1112 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U,
wolfSSL 11:cee25a834751 1113 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU,
wolfSSL 11:cee25a834751 1114 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U,
wolfSSL 11:cee25a834751 1115 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U,
wolfSSL 11:cee25a834751 1116 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU,
wolfSSL 11:cee25a834751 1117 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU,
wolfSSL 11:cee25a834751 1118 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U,
wolfSSL 11:cee25a834751 1119 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U,
wolfSSL 11:cee25a834751 1120 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U,
wolfSSL 11:cee25a834751 1121 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU,
wolfSSL 11:cee25a834751 1122 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U,
wolfSSL 11:cee25a834751 1123 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U,
wolfSSL 11:cee25a834751 1124 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U,
wolfSSL 11:cee25a834751 1125 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U,
wolfSSL 11:cee25a834751 1126 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U,
wolfSSL 11:cee25a834751 1127 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U,
wolfSSL 11:cee25a834751 1128 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U,
wolfSSL 11:cee25a834751 1129 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU,
wolfSSL 11:cee25a834751 1130 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U,
wolfSSL 11:cee25a834751 1131 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U,
wolfSSL 11:cee25a834751 1132 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU,
wolfSSL 11:cee25a834751 1133 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU,
wolfSSL 11:cee25a834751 1134 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U,
wolfSSL 11:cee25a834751 1135 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU,
wolfSSL 11:cee25a834751 1136 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U,
wolfSSL 11:cee25a834751 1137 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U,
wolfSSL 11:cee25a834751 1138 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U,
wolfSSL 11:cee25a834751 1139 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U,
wolfSSL 11:cee25a834751 1140 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U,
wolfSSL 11:cee25a834751 1141 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U,
wolfSSL 11:cee25a834751 1142 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU,
wolfSSL 11:cee25a834751 1143 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU,
wolfSSL 11:cee25a834751 1144 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU,
wolfSSL 11:cee25a834751 1145 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU,
wolfSSL 11:cee25a834751 1146 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U,
wolfSSL 11:cee25a834751 1147 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U,
wolfSSL 11:cee25a834751 1148 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U,
wolfSSL 11:cee25a834751 1149 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU,
wolfSSL 11:cee25a834751 1150 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U,
wolfSSL 11:cee25a834751 1151 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U,
wolfSSL 11:cee25a834751 1152 }
wolfSSL 11:cee25a834751 1153 };
wolfSSL 11:cee25a834751 1154
wolfSSL 11:cee25a834751 1155
wolfSSL 11:cee25a834751 1156 static const byte Td4[256] =
wolfSSL 11:cee25a834751 1157 {
wolfSSL 11:cee25a834751 1158 0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U,
wolfSSL 11:cee25a834751 1159 0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU,
wolfSSL 11:cee25a834751 1160 0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U,
wolfSSL 11:cee25a834751 1161 0x34U, 0x8eU, 0x43U, 0x44U, 0xc4U, 0xdeU, 0xe9U, 0xcbU,
wolfSSL 11:cee25a834751 1162 0x54U, 0x7bU, 0x94U, 0x32U, 0xa6U, 0xc2U, 0x23U, 0x3dU,
wolfSSL 11:cee25a834751 1163 0xeeU, 0x4cU, 0x95U, 0x0bU, 0x42U, 0xfaU, 0xc3U, 0x4eU,
wolfSSL 11:cee25a834751 1164 0x08U, 0x2eU, 0xa1U, 0x66U, 0x28U, 0xd9U, 0x24U, 0xb2U,
wolfSSL 11:cee25a834751 1165 0x76U, 0x5bU, 0xa2U, 0x49U, 0x6dU, 0x8bU, 0xd1U, 0x25U,
wolfSSL 11:cee25a834751 1166 0x72U, 0xf8U, 0xf6U, 0x64U, 0x86U, 0x68U, 0x98U, 0x16U,
wolfSSL 11:cee25a834751 1167 0xd4U, 0xa4U, 0x5cU, 0xccU, 0x5dU, 0x65U, 0xb6U, 0x92U,
wolfSSL 11:cee25a834751 1168 0x6cU, 0x70U, 0x48U, 0x50U, 0xfdU, 0xedU, 0xb9U, 0xdaU,
wolfSSL 11:cee25a834751 1169 0x5eU, 0x15U, 0x46U, 0x57U, 0xa7U, 0x8dU, 0x9dU, 0x84U,
wolfSSL 11:cee25a834751 1170 0x90U, 0xd8U, 0xabU, 0x00U, 0x8cU, 0xbcU, 0xd3U, 0x0aU,
wolfSSL 11:cee25a834751 1171 0xf7U, 0xe4U, 0x58U, 0x05U, 0xb8U, 0xb3U, 0x45U, 0x06U,
wolfSSL 11:cee25a834751 1172 0xd0U, 0x2cU, 0x1eU, 0x8fU, 0xcaU, 0x3fU, 0x0fU, 0x02U,
wolfSSL 11:cee25a834751 1173 0xc1U, 0xafU, 0xbdU, 0x03U, 0x01U, 0x13U, 0x8aU, 0x6bU,
wolfSSL 11:cee25a834751 1174 0x3aU, 0x91U, 0x11U, 0x41U, 0x4fU, 0x67U, 0xdcU, 0xeaU,
wolfSSL 11:cee25a834751 1175 0x97U, 0xf2U, 0xcfU, 0xceU, 0xf0U, 0xb4U, 0xe6U, 0x73U,
wolfSSL 11:cee25a834751 1176 0x96U, 0xacU, 0x74U, 0x22U, 0xe7U, 0xadU, 0x35U, 0x85U,
wolfSSL 11:cee25a834751 1177 0xe2U, 0xf9U, 0x37U, 0xe8U, 0x1cU, 0x75U, 0xdfU, 0x6eU,
wolfSSL 11:cee25a834751 1178 0x47U, 0xf1U, 0x1aU, 0x71U, 0x1dU, 0x29U, 0xc5U, 0x89U,
wolfSSL 11:cee25a834751 1179 0x6fU, 0xb7U, 0x62U, 0x0eU, 0xaaU, 0x18U, 0xbeU, 0x1bU,
wolfSSL 11:cee25a834751 1180 0xfcU, 0x56U, 0x3eU, 0x4bU, 0xc6U, 0xd2U, 0x79U, 0x20U,
wolfSSL 11:cee25a834751 1181 0x9aU, 0xdbU, 0xc0U, 0xfeU, 0x78U, 0xcdU, 0x5aU, 0xf4U,
wolfSSL 11:cee25a834751 1182 0x1fU, 0xddU, 0xa8U, 0x33U, 0x88U, 0x07U, 0xc7U, 0x31U,
wolfSSL 11:cee25a834751 1183 0xb1U, 0x12U, 0x10U, 0x59U, 0x27U, 0x80U, 0xecU, 0x5fU,
wolfSSL 11:cee25a834751 1184 0x60U, 0x51U, 0x7fU, 0xa9U, 0x19U, 0xb5U, 0x4aU, 0x0dU,
wolfSSL 11:cee25a834751 1185 0x2dU, 0xe5U, 0x7aU, 0x9fU, 0x93U, 0xc9U, 0x9cU, 0xefU,
wolfSSL 11:cee25a834751 1186 0xa0U, 0xe0U, 0x3bU, 0x4dU, 0xaeU, 0x2aU, 0xf5U, 0xb0U,
wolfSSL 11:cee25a834751 1187 0xc8U, 0xebU, 0xbbU, 0x3cU, 0x83U, 0x53U, 0x99U, 0x61U,
wolfSSL 11:cee25a834751 1188 0x17U, 0x2bU, 0x04U, 0x7eU, 0xbaU, 0x77U, 0xd6U, 0x26U,
wolfSSL 11:cee25a834751 1189 0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU,
wolfSSL 11:cee25a834751 1190 };
wolfSSL 11:cee25a834751 1191 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 1192
wolfSSL 11:cee25a834751 1193 #define GETBYTE(x, y) (word32)((byte)((x) >> (8 * (y))))
wolfSSL 11:cee25a834751 1194
wolfSSL 11:cee25a834751 1195
wolfSSL 11:cee25a834751 1196
wolfSSL 11:cee25a834751 1197 #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESGCM)
wolfSSL 11:cee25a834751 1198
wolfSSL 11:cee25a834751 1199 #ifndef WC_CACHE_LINE_SZ
wolfSSL 11:cee25a834751 1200 #if defined(__x86_64__) || defined(_M_X64) || \
wolfSSL 11:cee25a834751 1201 (defined(__ILP32__) && (__ILP32__ >= 1))
wolfSSL 11:cee25a834751 1202 #define WC_CACHE_LINE_SZ 64
wolfSSL 11:cee25a834751 1203 #else
wolfSSL 11:cee25a834751 1204 /* default cache line size */
wolfSSL 11:cee25a834751 1205 #define WC_CACHE_LINE_SZ 32
wolfSSL 11:cee25a834751 1206 #endif
wolfSSL 11:cee25a834751 1207 #endif
wolfSSL 11:cee25a834751 1208
wolfSSL 11:cee25a834751 1209
wolfSSL 11:cee25a834751 1210 /* load 4 Te Tables into cache by cache line stride */
wolfSSL 11:cee25a834751 1211 static INLINE word32 PreFetchTe(void)
wolfSSL 11:cee25a834751 1212 {
wolfSSL 11:cee25a834751 1213 word32 x = 0;
wolfSSL 11:cee25a834751 1214 int i,j;
wolfSSL 11:cee25a834751 1215
wolfSSL 11:cee25a834751 1216 for (i = 0; i < 4; i++) {
wolfSSL 11:cee25a834751 1217 /* 256 elements, each one is 4 bytes */
wolfSSL 11:cee25a834751 1218 for (j = 0; j < 256; j += WC_CACHE_LINE_SZ/4) {
wolfSSL 11:cee25a834751 1219 x &= Te[i][j];
wolfSSL 11:cee25a834751 1220 }
wolfSSL 11:cee25a834751 1221 }
wolfSSL 11:cee25a834751 1222 return x;
wolfSSL 11:cee25a834751 1223 }
wolfSSL 11:cee25a834751 1224
wolfSSL 11:cee25a834751 1225
wolfSSL 11:cee25a834751 1226 static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 11:cee25a834751 1227 {
wolfSSL 11:cee25a834751 1228 word32 s0, s1, s2, s3;
wolfSSL 11:cee25a834751 1229 word32 t0, t1, t2, t3;
wolfSSL 11:cee25a834751 1230 word32 r = aes->rounds >> 1;
wolfSSL 11:cee25a834751 1231 const word32* rk = aes->key;
wolfSSL 11:cee25a834751 1232
wolfSSL 11:cee25a834751 1233 if (r > 7 || r == 0) {
wolfSSL 11:cee25a834751 1234 WOLFSSL_MSG("AesEncrypt encountered improper key, set it up");
wolfSSL 11:cee25a834751 1235 return; /* stop instead of segfaulting, set up your keys! */
wolfSSL 11:cee25a834751 1236 }
wolfSSL 11:cee25a834751 1237
wolfSSL 11:cee25a834751 1238 #ifdef WOLFSSL_AESNI
wolfSSL 11:cee25a834751 1239 if (haveAESNI && aes->use_aesni) {
wolfSSL 11:cee25a834751 1240 #ifdef DEBUG_AESNI
wolfSSL 11:cee25a834751 1241 printf("about to aes encrypt\n");
wolfSSL 11:cee25a834751 1242 printf("in = %p\n", inBlock);
wolfSSL 11:cee25a834751 1243 printf("out = %p\n", outBlock);
wolfSSL 11:cee25a834751 1244 printf("aes->key = %p\n", aes->key);
wolfSSL 11:cee25a834751 1245 printf("aes->rounds = %d\n", aes->rounds);
wolfSSL 11:cee25a834751 1246 printf("sz = %d\n", AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 1247 #endif
wolfSSL 11:cee25a834751 1248
wolfSSL 11:cee25a834751 1249 /* check alignment, decrypt doesn't need alignment */
wolfSSL 11:cee25a834751 1250 if ((wolfssl_word)inBlock % AESNI_ALIGN) {
wolfSSL 11:cee25a834751 1251 #ifndef NO_WOLFSSL_ALLOC_ALIGN
wolfSSL 11:cee25a834751 1252 byte* tmp = (byte*)XMALLOC(AES_BLOCK_SIZE, aes->heap,
wolfSSL 11:cee25a834751 1253 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 1254 byte* tmp_align;
wolfSSL 11:cee25a834751 1255 if (tmp == NULL) return;
wolfSSL 11:cee25a834751 1256
wolfSSL 11:cee25a834751 1257 tmp_align = tmp + (AESNI_ALIGN - ((size_t)tmp % AESNI_ALIGN));
wolfSSL 11:cee25a834751 1258
wolfSSL 11:cee25a834751 1259 XMEMCPY(tmp_align, inBlock, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 1260 AES_ECB_encrypt(tmp_align, tmp_align, AES_BLOCK_SIZE, (byte*)aes->key,
wolfSSL 11:cee25a834751 1261 aes->rounds);
wolfSSL 11:cee25a834751 1262 XMEMCPY(outBlock, tmp_align, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 1263 XFREE(tmp, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 1264 return;
wolfSSL 11:cee25a834751 1265 #else
wolfSSL 11:cee25a834751 1266 WOLFSSL_MSG("AES-ECB encrypt with bad alignment");
wolfSSL 11:cee25a834751 1267 return;
wolfSSL 11:cee25a834751 1268 #endif
wolfSSL 11:cee25a834751 1269 }
wolfSSL 11:cee25a834751 1270
wolfSSL 11:cee25a834751 1271 AES_ECB_encrypt(inBlock, outBlock, AES_BLOCK_SIZE, (byte*)aes->key,
wolfSSL 11:cee25a834751 1272 aes->rounds);
wolfSSL 11:cee25a834751 1273
wolfSSL 11:cee25a834751 1274 return;
wolfSSL 11:cee25a834751 1275 }
wolfSSL 11:cee25a834751 1276 else {
wolfSSL 11:cee25a834751 1277 #ifdef DEBUG_AESNI
wolfSSL 11:cee25a834751 1278 printf("Skipping AES-NI\n");
wolfSSL 11:cee25a834751 1279 #endif
wolfSSL 11:cee25a834751 1280 }
wolfSSL 11:cee25a834751 1281 #endif
wolfSSL 11:cee25a834751 1282
wolfSSL 11:cee25a834751 1283 /*
wolfSSL 11:cee25a834751 1284 * map byte array block to cipher state
wolfSSL 11:cee25a834751 1285 * and add initial round key:
wolfSSL 11:cee25a834751 1286 */
wolfSSL 11:cee25a834751 1287 XMEMCPY(&s0, inBlock, sizeof(s0));
wolfSSL 11:cee25a834751 1288 XMEMCPY(&s1, inBlock + sizeof(s0), sizeof(s1));
wolfSSL 11:cee25a834751 1289 XMEMCPY(&s2, inBlock + 2 * sizeof(s0), sizeof(s2));
wolfSSL 11:cee25a834751 1290 XMEMCPY(&s3, inBlock + 3 * sizeof(s0), sizeof(s3));
wolfSSL 11:cee25a834751 1291
wolfSSL 11:cee25a834751 1292 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 1293 s0 = ByteReverseWord32(s0);
wolfSSL 11:cee25a834751 1294 s1 = ByteReverseWord32(s1);
wolfSSL 11:cee25a834751 1295 s2 = ByteReverseWord32(s2);
wolfSSL 11:cee25a834751 1296 s3 = ByteReverseWord32(s3);
wolfSSL 11:cee25a834751 1297 #endif
wolfSSL 11:cee25a834751 1298
wolfSSL 11:cee25a834751 1299 s0 ^= rk[0];
wolfSSL 11:cee25a834751 1300 s1 ^= rk[1];
wolfSSL 11:cee25a834751 1301 s2 ^= rk[2];
wolfSSL 11:cee25a834751 1302 s3 ^= rk[3];
wolfSSL 11:cee25a834751 1303
wolfSSL 11:cee25a834751 1304 s0 |= PreFetchTe();
wolfSSL 11:cee25a834751 1305
wolfSSL 11:cee25a834751 1306 /*
wolfSSL 11:cee25a834751 1307 * Nr - 1 full rounds:
wolfSSL 11:cee25a834751 1308 */
wolfSSL 11:cee25a834751 1309
wolfSSL 11:cee25a834751 1310 for (;;) {
wolfSSL 11:cee25a834751 1311 t0 =
wolfSSL 11:cee25a834751 1312 Te[0][GETBYTE(s0, 3)] ^
wolfSSL 11:cee25a834751 1313 Te[1][GETBYTE(s1, 2)] ^
wolfSSL 11:cee25a834751 1314 Te[2][GETBYTE(s2, 1)] ^
wolfSSL 11:cee25a834751 1315 Te[3][GETBYTE(s3, 0)] ^
wolfSSL 11:cee25a834751 1316 rk[4];
wolfSSL 11:cee25a834751 1317 t1 =
wolfSSL 11:cee25a834751 1318 Te[0][GETBYTE(s1, 3)] ^
wolfSSL 11:cee25a834751 1319 Te[1][GETBYTE(s2, 2)] ^
wolfSSL 11:cee25a834751 1320 Te[2][GETBYTE(s3, 1)] ^
wolfSSL 11:cee25a834751 1321 Te[3][GETBYTE(s0, 0)] ^
wolfSSL 11:cee25a834751 1322 rk[5];
wolfSSL 11:cee25a834751 1323 t2 =
wolfSSL 11:cee25a834751 1324 Te[0][GETBYTE(s2, 3)] ^
wolfSSL 11:cee25a834751 1325 Te[1][GETBYTE(s3, 2)] ^
wolfSSL 11:cee25a834751 1326 Te[2][GETBYTE(s0, 1)] ^
wolfSSL 11:cee25a834751 1327 Te[3][GETBYTE(s1, 0)] ^
wolfSSL 11:cee25a834751 1328 rk[6];
wolfSSL 11:cee25a834751 1329 t3 =
wolfSSL 11:cee25a834751 1330 Te[0][GETBYTE(s3, 3)] ^
wolfSSL 11:cee25a834751 1331 Te[1][GETBYTE(s0, 2)] ^
wolfSSL 11:cee25a834751 1332 Te[2][GETBYTE(s1, 1)] ^
wolfSSL 11:cee25a834751 1333 Te[3][GETBYTE(s2, 0)] ^
wolfSSL 11:cee25a834751 1334 rk[7];
wolfSSL 11:cee25a834751 1335
wolfSSL 11:cee25a834751 1336 rk += 8;
wolfSSL 11:cee25a834751 1337 if (--r == 0) {
wolfSSL 11:cee25a834751 1338 break;
wolfSSL 11:cee25a834751 1339 }
wolfSSL 11:cee25a834751 1340
wolfSSL 11:cee25a834751 1341 s0 =
wolfSSL 11:cee25a834751 1342 Te[0][GETBYTE(t0, 3)] ^
wolfSSL 11:cee25a834751 1343 Te[1][GETBYTE(t1, 2)] ^
wolfSSL 11:cee25a834751 1344 Te[2][GETBYTE(t2, 1)] ^
wolfSSL 11:cee25a834751 1345 Te[3][GETBYTE(t3, 0)] ^
wolfSSL 11:cee25a834751 1346 rk[0];
wolfSSL 11:cee25a834751 1347 s1 =
wolfSSL 11:cee25a834751 1348 Te[0][GETBYTE(t1, 3)] ^
wolfSSL 11:cee25a834751 1349 Te[1][GETBYTE(t2, 2)] ^
wolfSSL 11:cee25a834751 1350 Te[2][GETBYTE(t3, 1)] ^
wolfSSL 11:cee25a834751 1351 Te[3][GETBYTE(t0, 0)] ^
wolfSSL 11:cee25a834751 1352 rk[1];
wolfSSL 11:cee25a834751 1353 s2 =
wolfSSL 11:cee25a834751 1354 Te[0][GETBYTE(t2, 3)] ^
wolfSSL 11:cee25a834751 1355 Te[1][GETBYTE(t3, 2)] ^
wolfSSL 11:cee25a834751 1356 Te[2][GETBYTE(t0, 1)] ^
wolfSSL 11:cee25a834751 1357 Te[3][GETBYTE(t1, 0)] ^
wolfSSL 11:cee25a834751 1358 rk[2];
wolfSSL 11:cee25a834751 1359 s3 =
wolfSSL 11:cee25a834751 1360 Te[0][GETBYTE(t3, 3)] ^
wolfSSL 11:cee25a834751 1361 Te[1][GETBYTE(t0, 2)] ^
wolfSSL 11:cee25a834751 1362 Te[2][GETBYTE(t1, 1)] ^
wolfSSL 11:cee25a834751 1363 Te[3][GETBYTE(t2, 0)] ^
wolfSSL 11:cee25a834751 1364 rk[3];
wolfSSL 11:cee25a834751 1365 }
wolfSSL 11:cee25a834751 1366
wolfSSL 11:cee25a834751 1367 /*
wolfSSL 11:cee25a834751 1368 * apply last round and
wolfSSL 11:cee25a834751 1369 * map cipher state to byte array block:
wolfSSL 11:cee25a834751 1370 */
wolfSSL 11:cee25a834751 1371
wolfSSL 11:cee25a834751 1372 s0 =
wolfSSL 11:cee25a834751 1373 (Te[2][GETBYTE(t0, 3)] & 0xff000000) ^
wolfSSL 11:cee25a834751 1374 (Te[3][GETBYTE(t1, 2)] & 0x00ff0000) ^
wolfSSL 11:cee25a834751 1375 (Te[0][GETBYTE(t2, 1)] & 0x0000ff00) ^
wolfSSL 11:cee25a834751 1376 (Te[1][GETBYTE(t3, 0)] & 0x000000ff) ^
wolfSSL 11:cee25a834751 1377 rk[0];
wolfSSL 11:cee25a834751 1378 s1 =
wolfSSL 11:cee25a834751 1379 (Te[2][GETBYTE(t1, 3)] & 0xff000000) ^
wolfSSL 11:cee25a834751 1380 (Te[3][GETBYTE(t2, 2)] & 0x00ff0000) ^
wolfSSL 11:cee25a834751 1381 (Te[0][GETBYTE(t3, 1)] & 0x0000ff00) ^
wolfSSL 11:cee25a834751 1382 (Te[1][GETBYTE(t0, 0)] & 0x000000ff) ^
wolfSSL 11:cee25a834751 1383 rk[1];
wolfSSL 11:cee25a834751 1384 s2 =
wolfSSL 11:cee25a834751 1385 (Te[2][GETBYTE(t2, 3)] & 0xff000000) ^
wolfSSL 11:cee25a834751 1386 (Te[3][GETBYTE(t3, 2)] & 0x00ff0000) ^
wolfSSL 11:cee25a834751 1387 (Te[0][GETBYTE(t0, 1)] & 0x0000ff00) ^
wolfSSL 11:cee25a834751 1388 (Te[1][GETBYTE(t1, 0)] & 0x000000ff) ^
wolfSSL 11:cee25a834751 1389 rk[2];
wolfSSL 11:cee25a834751 1390 s3 =
wolfSSL 11:cee25a834751 1391 (Te[2][GETBYTE(t3, 3)] & 0xff000000) ^
wolfSSL 11:cee25a834751 1392 (Te[3][GETBYTE(t0, 2)] & 0x00ff0000) ^
wolfSSL 11:cee25a834751 1393 (Te[0][GETBYTE(t1, 1)] & 0x0000ff00) ^
wolfSSL 11:cee25a834751 1394 (Te[1][GETBYTE(t2, 0)] & 0x000000ff) ^
wolfSSL 11:cee25a834751 1395 rk[3];
wolfSSL 11:cee25a834751 1396
wolfSSL 11:cee25a834751 1397 /* write out */
wolfSSL 11:cee25a834751 1398 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 1399 s0 = ByteReverseWord32(s0);
wolfSSL 11:cee25a834751 1400 s1 = ByteReverseWord32(s1);
wolfSSL 11:cee25a834751 1401 s2 = ByteReverseWord32(s2);
wolfSSL 11:cee25a834751 1402 s3 = ByteReverseWord32(s3);
wolfSSL 11:cee25a834751 1403 #endif
wolfSSL 11:cee25a834751 1404
wolfSSL 11:cee25a834751 1405 XMEMCPY(outBlock, &s0, sizeof(s0));
wolfSSL 11:cee25a834751 1406 XMEMCPY(outBlock + sizeof(s0), &s1, sizeof(s1));
wolfSSL 11:cee25a834751 1407 XMEMCPY(outBlock + 2 * sizeof(s0), &s2, sizeof(s2));
wolfSSL 11:cee25a834751 1408 XMEMCPY(outBlock + 3 * sizeof(s0), &s3, sizeof(s3));
wolfSSL 11:cee25a834751 1409
wolfSSL 11:cee25a834751 1410 }
wolfSSL 11:cee25a834751 1411 #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT || HAVE_AESGCM */
wolfSSL 11:cee25a834751 1412
wolfSSL 11:cee25a834751 1413
wolfSSL 11:cee25a834751 1414 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 1415 #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
wolfSSL 11:cee25a834751 1416
wolfSSL 11:cee25a834751 1417 /* load 4 Td Tables into cache by cache line stride */
wolfSSL 11:cee25a834751 1418 static INLINE word32 PreFetchTd(void)
wolfSSL 11:cee25a834751 1419 {
wolfSSL 11:cee25a834751 1420 word32 x = 0;
wolfSSL 11:cee25a834751 1421 int i,j;
wolfSSL 11:cee25a834751 1422
wolfSSL 11:cee25a834751 1423 for (i = 0; i < 4; i++) {
wolfSSL 11:cee25a834751 1424 /* 256 elements, each one is 4 bytes */
wolfSSL 11:cee25a834751 1425 for (j = 0; j < 256; j += WC_CACHE_LINE_SZ/4) {
wolfSSL 11:cee25a834751 1426 x &= Td[i][j];
wolfSSL 11:cee25a834751 1427 }
wolfSSL 11:cee25a834751 1428 }
wolfSSL 11:cee25a834751 1429 return x;
wolfSSL 11:cee25a834751 1430 }
wolfSSL 11:cee25a834751 1431
wolfSSL 11:cee25a834751 1432 /* load Td Table4 into cache by cache line stride */
wolfSSL 11:cee25a834751 1433 static INLINE word32 PreFetchTd4(void)
wolfSSL 11:cee25a834751 1434 {
wolfSSL 11:cee25a834751 1435 word32 x = 0;
wolfSSL 11:cee25a834751 1436 int i;
wolfSSL 11:cee25a834751 1437
wolfSSL 11:cee25a834751 1438 for (i = 0; i < 256; i += WC_CACHE_LINE_SZ) {
wolfSSL 11:cee25a834751 1439 x &= (word32)Td4[i];
wolfSSL 11:cee25a834751 1440 }
wolfSSL 11:cee25a834751 1441 return x;
wolfSSL 11:cee25a834751 1442 }
wolfSSL 11:cee25a834751 1443
wolfSSL 11:cee25a834751 1444 static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 11:cee25a834751 1445 {
wolfSSL 11:cee25a834751 1446 word32 s0, s1, s2, s3;
wolfSSL 11:cee25a834751 1447 word32 t0, t1, t2, t3;
wolfSSL 11:cee25a834751 1448 word32 r = aes->rounds >> 1;
wolfSSL 11:cee25a834751 1449
wolfSSL 11:cee25a834751 1450 const word32* rk = aes->key;
wolfSSL 11:cee25a834751 1451 if (r > 7 || r == 0) {
wolfSSL 11:cee25a834751 1452 WOLFSSL_MSG("AesDecrypt encountered improper key, set it up");
wolfSSL 11:cee25a834751 1453 return; /* stop instead of segfaulting, set up your keys! */
wolfSSL 11:cee25a834751 1454 }
wolfSSL 11:cee25a834751 1455 #ifdef WOLFSSL_AESNI
wolfSSL 11:cee25a834751 1456 if (haveAESNI && aes->use_aesni) {
wolfSSL 11:cee25a834751 1457 #ifdef DEBUG_AESNI
wolfSSL 11:cee25a834751 1458 printf("about to aes decrypt\n");
wolfSSL 11:cee25a834751 1459 printf("in = %p\n", inBlock);
wolfSSL 11:cee25a834751 1460 printf("out = %p\n", outBlock);
wolfSSL 11:cee25a834751 1461 printf("aes->key = %p\n", aes->key);
wolfSSL 11:cee25a834751 1462 printf("aes->rounds = %d\n", aes->rounds);
wolfSSL 11:cee25a834751 1463 printf("sz = %d\n", AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 1464 #endif
wolfSSL 11:cee25a834751 1465
wolfSSL 11:cee25a834751 1466 /* if input and output same will overwrite input iv */
wolfSSL 11:cee25a834751 1467 XMEMCPY(aes->tmp, inBlock, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 1468 AES_ECB_decrypt(inBlock, outBlock, AES_BLOCK_SIZE, (byte*)aes->key,
wolfSSL 11:cee25a834751 1469 aes->rounds);
wolfSSL 11:cee25a834751 1470 return;
wolfSSL 11:cee25a834751 1471 }
wolfSSL 11:cee25a834751 1472 else {
wolfSSL 11:cee25a834751 1473 #ifdef DEBUG_AESNI
wolfSSL 11:cee25a834751 1474 printf("Skipping AES-NI\n");
wolfSSL 11:cee25a834751 1475 #endif
wolfSSL 11:cee25a834751 1476 }
wolfSSL 11:cee25a834751 1477 #endif /* WOLFSSL_AESNI */
wolfSSL 11:cee25a834751 1478
wolfSSL 11:cee25a834751 1479 /*
wolfSSL 11:cee25a834751 1480 * map byte array block to cipher state
wolfSSL 11:cee25a834751 1481 * and add initial round key:
wolfSSL 11:cee25a834751 1482 */
wolfSSL 11:cee25a834751 1483 XMEMCPY(&s0, inBlock, sizeof(s0));
wolfSSL 11:cee25a834751 1484 XMEMCPY(&s1, inBlock + sizeof(s0), sizeof(s1));
wolfSSL 11:cee25a834751 1485 XMEMCPY(&s2, inBlock + 2 * sizeof(s0), sizeof(s2));
wolfSSL 11:cee25a834751 1486 XMEMCPY(&s3, inBlock + 3 * sizeof(s0), sizeof(s3));
wolfSSL 11:cee25a834751 1487
wolfSSL 11:cee25a834751 1488 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 1489 s0 = ByteReverseWord32(s0);
wolfSSL 11:cee25a834751 1490 s1 = ByteReverseWord32(s1);
wolfSSL 11:cee25a834751 1491 s2 = ByteReverseWord32(s2);
wolfSSL 11:cee25a834751 1492 s3 = ByteReverseWord32(s3);
wolfSSL 11:cee25a834751 1493 #endif
wolfSSL 11:cee25a834751 1494
wolfSSL 11:cee25a834751 1495 s0 ^= rk[0];
wolfSSL 11:cee25a834751 1496 s1 ^= rk[1];
wolfSSL 11:cee25a834751 1497 s2 ^= rk[2];
wolfSSL 11:cee25a834751 1498 s3 ^= rk[3];
wolfSSL 11:cee25a834751 1499
wolfSSL 11:cee25a834751 1500 s0 |= PreFetchTd();
wolfSSL 11:cee25a834751 1501
wolfSSL 11:cee25a834751 1502 /*
wolfSSL 11:cee25a834751 1503 * Nr - 1 full rounds:
wolfSSL 11:cee25a834751 1504 */
wolfSSL 11:cee25a834751 1505
wolfSSL 11:cee25a834751 1506 for (;;) {
wolfSSL 11:cee25a834751 1507 t0 =
wolfSSL 11:cee25a834751 1508 Td[0][GETBYTE(s0, 3)] ^
wolfSSL 11:cee25a834751 1509 Td[1][GETBYTE(s3, 2)] ^
wolfSSL 11:cee25a834751 1510 Td[2][GETBYTE(s2, 1)] ^
wolfSSL 11:cee25a834751 1511 Td[3][GETBYTE(s1, 0)] ^
wolfSSL 11:cee25a834751 1512 rk[4];
wolfSSL 11:cee25a834751 1513 t1 =
wolfSSL 11:cee25a834751 1514 Td[0][GETBYTE(s1, 3)] ^
wolfSSL 11:cee25a834751 1515 Td[1][GETBYTE(s0, 2)] ^
wolfSSL 11:cee25a834751 1516 Td[2][GETBYTE(s3, 1)] ^
wolfSSL 11:cee25a834751 1517 Td[3][GETBYTE(s2, 0)] ^
wolfSSL 11:cee25a834751 1518 rk[5];
wolfSSL 11:cee25a834751 1519 t2 =
wolfSSL 11:cee25a834751 1520 Td[0][GETBYTE(s2, 3)] ^
wolfSSL 11:cee25a834751 1521 Td[1][GETBYTE(s1, 2)] ^
wolfSSL 11:cee25a834751 1522 Td[2][GETBYTE(s0, 1)] ^
wolfSSL 11:cee25a834751 1523 Td[3][GETBYTE(s3, 0)] ^
wolfSSL 11:cee25a834751 1524 rk[6];
wolfSSL 11:cee25a834751 1525 t3 =
wolfSSL 11:cee25a834751 1526 Td[0][GETBYTE(s3, 3)] ^
wolfSSL 11:cee25a834751 1527 Td[1][GETBYTE(s2, 2)] ^
wolfSSL 11:cee25a834751 1528 Td[2][GETBYTE(s1, 1)] ^
wolfSSL 11:cee25a834751 1529 Td[3][GETBYTE(s0, 0)] ^
wolfSSL 11:cee25a834751 1530 rk[7];
wolfSSL 11:cee25a834751 1531
wolfSSL 11:cee25a834751 1532 rk += 8;
wolfSSL 11:cee25a834751 1533 if (--r == 0) {
wolfSSL 11:cee25a834751 1534 break;
wolfSSL 11:cee25a834751 1535 }
wolfSSL 11:cee25a834751 1536
wolfSSL 11:cee25a834751 1537 s0 =
wolfSSL 11:cee25a834751 1538 Td[0][GETBYTE(t0, 3)] ^
wolfSSL 11:cee25a834751 1539 Td[1][GETBYTE(t3, 2)] ^
wolfSSL 11:cee25a834751 1540 Td[2][GETBYTE(t2, 1)] ^
wolfSSL 11:cee25a834751 1541 Td[3][GETBYTE(t1, 0)] ^
wolfSSL 11:cee25a834751 1542 rk[0];
wolfSSL 11:cee25a834751 1543 s1 =
wolfSSL 11:cee25a834751 1544 Td[0][GETBYTE(t1, 3)] ^
wolfSSL 11:cee25a834751 1545 Td[1][GETBYTE(t0, 2)] ^
wolfSSL 11:cee25a834751 1546 Td[2][GETBYTE(t3, 1)] ^
wolfSSL 11:cee25a834751 1547 Td[3][GETBYTE(t2, 0)] ^
wolfSSL 11:cee25a834751 1548 rk[1];
wolfSSL 11:cee25a834751 1549 s2 =
wolfSSL 11:cee25a834751 1550 Td[0][GETBYTE(t2, 3)] ^
wolfSSL 11:cee25a834751 1551 Td[1][GETBYTE(t1, 2)] ^
wolfSSL 11:cee25a834751 1552 Td[2][GETBYTE(t0, 1)] ^
wolfSSL 11:cee25a834751 1553 Td[3][GETBYTE(t3, 0)] ^
wolfSSL 11:cee25a834751 1554 rk[2];
wolfSSL 11:cee25a834751 1555 s3 =
wolfSSL 11:cee25a834751 1556 Td[0][GETBYTE(t3, 3)] ^
wolfSSL 11:cee25a834751 1557 Td[1][GETBYTE(t2, 2)] ^
wolfSSL 11:cee25a834751 1558 Td[2][GETBYTE(t1, 1)] ^
wolfSSL 11:cee25a834751 1559 Td[3][GETBYTE(t0, 0)] ^
wolfSSL 11:cee25a834751 1560 rk[3];
wolfSSL 11:cee25a834751 1561 }
wolfSSL 11:cee25a834751 1562 /*
wolfSSL 11:cee25a834751 1563 * apply last round and
wolfSSL 11:cee25a834751 1564 * map cipher state to byte array block:
wolfSSL 11:cee25a834751 1565 */
wolfSSL 11:cee25a834751 1566
wolfSSL 11:cee25a834751 1567 t0 |= PreFetchTd4();
wolfSSL 11:cee25a834751 1568
wolfSSL 11:cee25a834751 1569 s0 =
wolfSSL 11:cee25a834751 1570 ((word32)Td4[GETBYTE(t0, 3)] << 24) ^
wolfSSL 11:cee25a834751 1571 ((word32)Td4[GETBYTE(t3, 2)] << 16) ^
wolfSSL 11:cee25a834751 1572 ((word32)Td4[GETBYTE(t2, 1)] << 8) ^
wolfSSL 11:cee25a834751 1573 ((word32)Td4[GETBYTE(t1, 0)]) ^
wolfSSL 11:cee25a834751 1574 rk[0];
wolfSSL 11:cee25a834751 1575 s1 =
wolfSSL 11:cee25a834751 1576 ((word32)Td4[GETBYTE(t1, 3)] << 24) ^
wolfSSL 11:cee25a834751 1577 ((word32)Td4[GETBYTE(t0, 2)] << 16) ^
wolfSSL 11:cee25a834751 1578 ((word32)Td4[GETBYTE(t3, 1)] << 8) ^
wolfSSL 11:cee25a834751 1579 ((word32)Td4[GETBYTE(t2, 0)]) ^
wolfSSL 11:cee25a834751 1580 rk[1];
wolfSSL 11:cee25a834751 1581 s2 =
wolfSSL 11:cee25a834751 1582 ((word32)Td4[GETBYTE(t2, 3)] << 24) ^
wolfSSL 11:cee25a834751 1583 ((word32)Td4[GETBYTE(t1, 2)] << 16) ^
wolfSSL 11:cee25a834751 1584 ((word32)Td4[GETBYTE(t0, 1)] << 8) ^
wolfSSL 11:cee25a834751 1585 ((word32)Td4[GETBYTE(t3, 0)]) ^
wolfSSL 11:cee25a834751 1586 rk[2];
wolfSSL 11:cee25a834751 1587 s3 =
wolfSSL 11:cee25a834751 1588 ((word32)Td4[GETBYTE(t3, 3)] << 24) ^
wolfSSL 11:cee25a834751 1589 ((word32)Td4[GETBYTE(t2, 2)] << 16) ^
wolfSSL 11:cee25a834751 1590 ((word32)Td4[GETBYTE(t1, 1)] << 8) ^
wolfSSL 11:cee25a834751 1591 ((word32)Td4[GETBYTE(t0, 0)]) ^
wolfSSL 11:cee25a834751 1592 rk[3];
wolfSSL 11:cee25a834751 1593
wolfSSL 11:cee25a834751 1594 /* write out */
wolfSSL 11:cee25a834751 1595 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 1596 s0 = ByteReverseWord32(s0);
wolfSSL 11:cee25a834751 1597 s1 = ByteReverseWord32(s1);
wolfSSL 11:cee25a834751 1598 s2 = ByteReverseWord32(s2);
wolfSSL 11:cee25a834751 1599 s3 = ByteReverseWord32(s3);
wolfSSL 11:cee25a834751 1600 #endif
wolfSSL 11:cee25a834751 1601
wolfSSL 11:cee25a834751 1602 XMEMCPY(outBlock, &s0, sizeof(s0));
wolfSSL 11:cee25a834751 1603 XMEMCPY(outBlock + sizeof(s0), &s1, sizeof(s1));
wolfSSL 11:cee25a834751 1604 XMEMCPY(outBlock + 2 * sizeof(s0), &s2, sizeof(s2));
wolfSSL 11:cee25a834751 1605 XMEMCPY(outBlock + 3 * sizeof(s0), &s3, sizeof(s3));
wolfSSL 11:cee25a834751 1606 }
wolfSSL 11:cee25a834751 1607 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 1608 #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
wolfSSL 11:cee25a834751 1609 #endif /* NEED_AES_TABLES */
wolfSSL 11:cee25a834751 1610
wolfSSL 11:cee25a834751 1611
wolfSSL 11:cee25a834751 1612
wolfSSL 11:cee25a834751 1613 /* wc_AesSetKey */
wolfSSL 11:cee25a834751 1614 #if defined(STM32F2_CRYPTO) || defined(STM32F4_CRYPTO)
wolfSSL 11:cee25a834751 1615
wolfSSL 11:cee25a834751 1616 int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 1617 const byte* iv, int dir)
wolfSSL 11:cee25a834751 1618 {
wolfSSL 11:cee25a834751 1619 word32 *rk = aes->key;
wolfSSL 11:cee25a834751 1620
wolfSSL 11:cee25a834751 1621 (void)dir;
wolfSSL 11:cee25a834751 1622
wolfSSL 11:cee25a834751 1623 if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
wolfSSL 11:cee25a834751 1624 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1625
wolfSSL 11:cee25a834751 1626 aes->keylen = keylen;
wolfSSL 11:cee25a834751 1627 aes->rounds = keylen/4 + 6;
wolfSSL 11:cee25a834751 1628 XMEMCPY(rk, userKey, keylen);
wolfSSL 11:cee25a834751 1629 #ifndef WOLFSSL_STM32_CUBEMX
wolfSSL 11:cee25a834751 1630 ByteReverseWords(rk, rk, keylen);
wolfSSL 11:cee25a834751 1631 #endif
wolfSSL 11:cee25a834751 1632
wolfSSL 11:cee25a834751 1633 return wc_AesSetIV(aes, iv);
wolfSSL 11:cee25a834751 1634 }
wolfSSL 11:cee25a834751 1635 #if defined(WOLFSSL_AES_DIRECT)
wolfSSL 11:cee25a834751 1636 int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 1637 const byte* iv, int dir)
wolfSSL 11:cee25a834751 1638 {
wolfSSL 11:cee25a834751 1639 return wc_AesSetKey(aes, userKey, keylen, iv, dir);
wolfSSL 11:cee25a834751 1640 }
wolfSSL 11:cee25a834751 1641 #endif
wolfSSL 11:cee25a834751 1642
wolfSSL 11:cee25a834751 1643 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 11:cee25a834751 1644 #if defined (HAVE_THREADX)
wolfSSL 11:cee25a834751 1645 #include "memory_pools.h"
wolfSSL 11:cee25a834751 1646 extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */
wolfSSL 11:cee25a834751 1647 #endif
wolfSSL 11:cee25a834751 1648
wolfSSL 11:cee25a834751 1649 #define AES_BUFFER_SIZE (AES_BLOCK_SIZE * 64)
wolfSSL 11:cee25a834751 1650 static unsigned char *AESBuffIn = NULL;
wolfSSL 11:cee25a834751 1651 static unsigned char *AESBuffOut = NULL;
wolfSSL 11:cee25a834751 1652 static byte *secReg;
wolfSSL 11:cee25a834751 1653 static byte *secKey;
wolfSSL 11:cee25a834751 1654 static volatile SECdescriptorType *secDesc;
wolfSSL 11:cee25a834751 1655
wolfSSL 11:cee25a834751 1656 static wolfSSL_Mutex Mutex_AesSEC;
wolfSSL 11:cee25a834751 1657
wolfSSL 11:cee25a834751 1658 #define SEC_DESC_AES_CBC_ENCRYPT 0x60300010
wolfSSL 11:cee25a834751 1659 #define SEC_DESC_AES_CBC_DECRYPT 0x60200010
wolfSSL 11:cee25a834751 1660
wolfSSL 11:cee25a834751 1661 extern volatile unsigned char __MBAR[];
wolfSSL 11:cee25a834751 1662
wolfSSL 11:cee25a834751 1663 int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 1664 const byte* iv, int dir)
wolfSSL 11:cee25a834751 1665 {
wolfSSL 11:cee25a834751 1666 if (AESBuffIn == NULL) {
wolfSSL 11:cee25a834751 1667 #if defined (HAVE_THREADX)
wolfSSL 11:cee25a834751 1668 int s1, s2, s3, s4, s5;
wolfSSL 11:cee25a834751 1669 s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc,
wolfSSL 11:cee25a834751 1670 sizeof(SECdescriptorType), TX_NO_WAIT);
wolfSSL 11:cee25a834751 1671 s1 = tx_byte_allocate(&mp_ncached, (void *)&AESBuffIn,
wolfSSL 11:cee25a834751 1672 AES_BUFFER_SIZE, TX_NO_WAIT);
wolfSSL 11:cee25a834751 1673 s2 = tx_byte_allocate(&mp_ncached, (void *)&AESBuffOut,
wolfSSL 11:cee25a834751 1674 AES_BUFFER_SIZE, TX_NO_WAIT);
wolfSSL 11:cee25a834751 1675 s3 = tx_byte_allocate(&mp_ncached, (void *)&secKey,
wolfSSL 11:cee25a834751 1676 AES_BLOCK_SIZE*2, TX_NO_WAIT);
wolfSSL 11:cee25a834751 1677 s4 = tx_byte_allocate(&mp_ncached, (void *)&secReg,
wolfSSL 11:cee25a834751 1678 AES_BLOCK_SIZE, TX_NO_WAIT);
wolfSSL 11:cee25a834751 1679
wolfSSL 11:cee25a834751 1680 if (s1 || s2 || s3 || s4 || s5)
wolfSSL 11:cee25a834751 1681 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1682 #else
wolfSSL 11:cee25a834751 1683 #warning "Allocate non-Cache buffers"
wolfSSL 11:cee25a834751 1684 #endif
wolfSSL 11:cee25a834751 1685
wolfSSL 11:cee25a834751 1686 wc_InitMutex(&Mutex_AesSEC);
wolfSSL 11:cee25a834751 1687 }
wolfSSL 11:cee25a834751 1688
wolfSSL 11:cee25a834751 1689 if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
wolfSSL 11:cee25a834751 1690 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1691
wolfSSL 11:cee25a834751 1692 if (aes == NULL)
wolfSSL 11:cee25a834751 1693 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1694
wolfSSL 11:cee25a834751 1695 aes->keylen = keylen;
wolfSSL 11:cee25a834751 1696 aes->rounds = keylen/4 + 6;
wolfSSL 11:cee25a834751 1697 XMEMCPY(aes->key, userKey, keylen);
wolfSSL 11:cee25a834751 1698
wolfSSL 11:cee25a834751 1699 if (iv)
wolfSSL 11:cee25a834751 1700 XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 1701
wolfSSL 11:cee25a834751 1702 return 0;
wolfSSL 11:cee25a834751 1703 }
wolfSSL 11:cee25a834751 1704 #elif defined(FREESCALE_LTC)
wolfSSL 11:cee25a834751 1705 int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
wolfSSL 11:cee25a834751 1706 int dir)
wolfSSL 11:cee25a834751 1707 {
wolfSSL 11:cee25a834751 1708 if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
wolfSSL 11:cee25a834751 1709 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1710
wolfSSL 11:cee25a834751 1711 aes->rounds = keylen/4 + 6;
wolfSSL 11:cee25a834751 1712 XMEMCPY(aes->key, userKey, keylen);
wolfSSL 11:cee25a834751 1713
wolfSSL 11:cee25a834751 1714 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 11:cee25a834751 1715 aes->left = 0;
wolfSSL 11:cee25a834751 1716 #endif /* WOLFSSL_AES_COUNTER */
wolfSSL 11:cee25a834751 1717
wolfSSL 11:cee25a834751 1718 return wc_AesSetIV(aes, iv);
wolfSSL 11:cee25a834751 1719 }
wolfSSL 11:cee25a834751 1720
wolfSSL 11:cee25a834751 1721 int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 1722 const byte* iv, int dir)
wolfSSL 11:cee25a834751 1723 {
wolfSSL 11:cee25a834751 1724 return wc_AesSetKey(aes, userKey, keylen, iv, dir);
wolfSSL 11:cee25a834751 1725 }
wolfSSL 11:cee25a834751 1726 #elif defined(FREESCALE_MMCAU)
wolfSSL 11:cee25a834751 1727 int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 1728 const byte* iv, int dir)
wolfSSL 11:cee25a834751 1729 {
wolfSSL 11:cee25a834751 1730 int ret;
wolfSSL 11:cee25a834751 1731 byte *rk = (byte*)aes->key;
wolfSSL 11:cee25a834751 1732
wolfSSL 11:cee25a834751 1733 (void)dir;
wolfSSL 11:cee25a834751 1734
wolfSSL 11:cee25a834751 1735 if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
wolfSSL 11:cee25a834751 1736 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1737
wolfSSL 11:cee25a834751 1738 if (rk == NULL)
wolfSSL 11:cee25a834751 1739 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1740
wolfSSL 11:cee25a834751 1741 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 11:cee25a834751 1742 aes->left = 0;
wolfSSL 11:cee25a834751 1743 #endif /* WOLFSSL_AES_COUNTER */
wolfSSL 11:cee25a834751 1744
wolfSSL 11:cee25a834751 1745 aes->keylen = keylen;
wolfSSL 11:cee25a834751 1746 aes->rounds = keylen/4 + 6;
wolfSSL 11:cee25a834751 1747
wolfSSL 11:cee25a834751 1748 ret = wolfSSL_CryptHwMutexLock();
wolfSSL 11:cee25a834751 1749 if(ret == 0) {
wolfSSL 11:cee25a834751 1750 MMCAU_AES_SetKey(userKey, keylen, rk);
wolfSSL 11:cee25a834751 1751 wolfSSL_CryptHwMutexUnLock();
wolfSSL 11:cee25a834751 1752
wolfSSL 11:cee25a834751 1753 ret = wc_AesSetIV(aes, iv);
wolfSSL 11:cee25a834751 1754 }
wolfSSL 11:cee25a834751 1755
wolfSSL 11:cee25a834751 1756 return ret;
wolfSSL 11:cee25a834751 1757 }
wolfSSL 11:cee25a834751 1758
wolfSSL 11:cee25a834751 1759 int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 1760 const byte* iv, int dir)
wolfSSL 11:cee25a834751 1761 {
wolfSSL 11:cee25a834751 1762 return wc_AesSetKey(aes, userKey, keylen, iv, dir);
wolfSSL 11:cee25a834751 1763 }
wolfSSL 11:cee25a834751 1764
wolfSSL 11:cee25a834751 1765 #elif defined(WOLFSSL_NRF51_AES)
wolfSSL 11:cee25a834751 1766 int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 1767 const byte* iv, int dir)
wolfSSL 11:cee25a834751 1768 {
wolfSSL 11:cee25a834751 1769 int ret;
wolfSSL 11:cee25a834751 1770
wolfSSL 11:cee25a834751 1771 (void)dir;
wolfSSL 11:cee25a834751 1772 (void)iv;
wolfSSL 11:cee25a834751 1773
wolfSSL 11:cee25a834751 1774 if (keylen != 16)
wolfSSL 11:cee25a834751 1775 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1776
wolfSSL 11:cee25a834751 1777 aes->keylen = keylen;
wolfSSL 11:cee25a834751 1778 aes->rounds = keylen/4 + 6;
wolfSSL 11:cee25a834751 1779 ret = nrf51_aes_set_key(userKey);
wolfSSL 11:cee25a834751 1780
wolfSSL 11:cee25a834751 1781 return ret;
wolfSSL 11:cee25a834751 1782 }
wolfSSL 11:cee25a834751 1783
wolfSSL 11:cee25a834751 1784 int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 1785 const byte* iv, int dir)
wolfSSL 11:cee25a834751 1786 {
wolfSSL 11:cee25a834751 1787 return wc_AesSetKey(aes, userKey, keylen, iv, dir);
wolfSSL 11:cee25a834751 1788 }
wolfSSL 11:cee25a834751 1789
wolfSSL 11:cee25a834751 1790 #else
wolfSSL 11:cee25a834751 1791 static int wc_AesSetKeyLocal(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 1792 const byte* iv, int dir)
wolfSSL 11:cee25a834751 1793 {
wolfSSL 11:cee25a834751 1794 word32 temp, *rk = aes->key;
wolfSSL 11:cee25a834751 1795 unsigned int i = 0;
wolfSSL 11:cee25a834751 1796
wolfSSL 11:cee25a834751 1797 #ifdef WOLFSSL_AESNI
wolfSSL 11:cee25a834751 1798 aes->use_aesni = 0;
wolfSSL 11:cee25a834751 1799 #endif /* WOLFSSL_AESNI */
wolfSSL 11:cee25a834751 1800 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 11:cee25a834751 1801 aes->left = 0;
wolfSSL 11:cee25a834751 1802 #endif /* WOLFSSL_AES_COUNTER */
wolfSSL 11:cee25a834751 1803
wolfSSL 11:cee25a834751 1804 aes->keylen = keylen;
wolfSSL 11:cee25a834751 1805 aes->rounds = keylen/4 + 6;
wolfSSL 11:cee25a834751 1806
wolfSSL 11:cee25a834751 1807 XMEMCPY(rk, userKey, keylen);
wolfSSL 11:cee25a834751 1808 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 1809 ByteReverseWords(rk, rk, keylen);
wolfSSL 11:cee25a834751 1810 #endif
wolfSSL 11:cee25a834751 1811
wolfSSL 11:cee25a834751 1812 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 11:cee25a834751 1813 {
wolfSSL 11:cee25a834751 1814 word32 *akey1 = aes->key_ce;
wolfSSL 11:cee25a834751 1815 word32 *areg = aes->iv_ce;
wolfSSL 11:cee25a834751 1816 XMEMCPY(akey1, userKey, keylen);
wolfSSL 11:cee25a834751 1817 if (iv)
wolfSSL 11:cee25a834751 1818 XMEMCPY(areg, iv, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 1819 else
wolfSSL 11:cee25a834751 1820 XMEMSET(areg, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 1821 }
wolfSSL 11:cee25a834751 1822 #endif
wolfSSL 11:cee25a834751 1823
wolfSSL 11:cee25a834751 1824 switch(keylen)
wolfSSL 11:cee25a834751 1825 {
wolfSSL 11:cee25a834751 1826 #if defined(AES_MAX_KEY_SIZE) && AES_MAX_KEY_SIZE >= 128
wolfSSL 11:cee25a834751 1827 case 16:
wolfSSL 11:cee25a834751 1828 while (1)
wolfSSL 11:cee25a834751 1829 {
wolfSSL 11:cee25a834751 1830 temp = rk[3];
wolfSSL 11:cee25a834751 1831 rk[4] = rk[0] ^
wolfSSL 11:cee25a834751 1832 (Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
wolfSSL 11:cee25a834751 1833 (Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
wolfSSL 11:cee25a834751 1834 (Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
wolfSSL 11:cee25a834751 1835 (Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
wolfSSL 11:cee25a834751 1836 rcon[i];
wolfSSL 11:cee25a834751 1837 rk[5] = rk[1] ^ rk[4];
wolfSSL 11:cee25a834751 1838 rk[6] = rk[2] ^ rk[5];
wolfSSL 11:cee25a834751 1839 rk[7] = rk[3] ^ rk[6];
wolfSSL 11:cee25a834751 1840 if (++i == 10)
wolfSSL 11:cee25a834751 1841 break;
wolfSSL 11:cee25a834751 1842 rk += 4;
wolfSSL 11:cee25a834751 1843 }
wolfSSL 11:cee25a834751 1844 break;
wolfSSL 11:cee25a834751 1845 #endif /* 128 */
wolfSSL 11:cee25a834751 1846
wolfSSL 11:cee25a834751 1847 #if defined(AES_MAX_KEY_SIZE) && AES_MAX_KEY_SIZE >= 192
wolfSSL 11:cee25a834751 1848 case 24:
wolfSSL 11:cee25a834751 1849 /* for (;;) here triggers a bug in VC60 SP4 w/ Pro Pack */
wolfSSL 11:cee25a834751 1850 while (1)
wolfSSL 11:cee25a834751 1851 {
wolfSSL 11:cee25a834751 1852 temp = rk[ 5];
wolfSSL 11:cee25a834751 1853 rk[ 6] = rk[ 0] ^
wolfSSL 11:cee25a834751 1854 (Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
wolfSSL 11:cee25a834751 1855 (Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
wolfSSL 11:cee25a834751 1856 (Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
wolfSSL 11:cee25a834751 1857 (Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
wolfSSL 11:cee25a834751 1858 rcon[i];
wolfSSL 11:cee25a834751 1859 rk[ 7] = rk[ 1] ^ rk[ 6];
wolfSSL 11:cee25a834751 1860 rk[ 8] = rk[ 2] ^ rk[ 7];
wolfSSL 11:cee25a834751 1861 rk[ 9] = rk[ 3] ^ rk[ 8];
wolfSSL 11:cee25a834751 1862 if (++i == 8)
wolfSSL 11:cee25a834751 1863 break;
wolfSSL 11:cee25a834751 1864 rk[10] = rk[ 4] ^ rk[ 9];
wolfSSL 11:cee25a834751 1865 rk[11] = rk[ 5] ^ rk[10];
wolfSSL 11:cee25a834751 1866 rk += 6;
wolfSSL 11:cee25a834751 1867 }
wolfSSL 11:cee25a834751 1868 break;
wolfSSL 11:cee25a834751 1869 #endif /* 192 */
wolfSSL 11:cee25a834751 1870
wolfSSL 11:cee25a834751 1871 #if defined(AES_MAX_KEY_SIZE) && AES_MAX_KEY_SIZE >= 256
wolfSSL 11:cee25a834751 1872 case 32:
wolfSSL 11:cee25a834751 1873 while (1)
wolfSSL 11:cee25a834751 1874 {
wolfSSL 11:cee25a834751 1875 temp = rk[ 7];
wolfSSL 11:cee25a834751 1876 rk[ 8] = rk[ 0] ^
wolfSSL 11:cee25a834751 1877 (Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
wolfSSL 11:cee25a834751 1878 (Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
wolfSSL 11:cee25a834751 1879 (Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
wolfSSL 11:cee25a834751 1880 (Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
wolfSSL 11:cee25a834751 1881 rcon[i];
wolfSSL 11:cee25a834751 1882 rk[ 9] = rk[ 1] ^ rk[ 8];
wolfSSL 11:cee25a834751 1883 rk[10] = rk[ 2] ^ rk[ 9];
wolfSSL 11:cee25a834751 1884 rk[11] = rk[ 3] ^ rk[10];
wolfSSL 11:cee25a834751 1885 if (++i == 7)
wolfSSL 11:cee25a834751 1886 break;
wolfSSL 11:cee25a834751 1887 temp = rk[11];
wolfSSL 11:cee25a834751 1888 rk[12] = rk[ 4] ^
wolfSSL 11:cee25a834751 1889 (Te[2][GETBYTE(temp, 3)] & 0xff000000) ^
wolfSSL 11:cee25a834751 1890 (Te[3][GETBYTE(temp, 2)] & 0x00ff0000) ^
wolfSSL 11:cee25a834751 1891 (Te[0][GETBYTE(temp, 1)] & 0x0000ff00) ^
wolfSSL 11:cee25a834751 1892 (Te[1][GETBYTE(temp, 0)] & 0x000000ff);
wolfSSL 11:cee25a834751 1893 rk[13] = rk[ 5] ^ rk[12];
wolfSSL 11:cee25a834751 1894 rk[14] = rk[ 6] ^ rk[13];
wolfSSL 11:cee25a834751 1895 rk[15] = rk[ 7] ^ rk[14];
wolfSSL 11:cee25a834751 1896
wolfSSL 11:cee25a834751 1897 rk += 8;
wolfSSL 11:cee25a834751 1898 }
wolfSSL 11:cee25a834751 1899 break;
wolfSSL 11:cee25a834751 1900 #endif /* 256 */
wolfSSL 11:cee25a834751 1901
wolfSSL 11:cee25a834751 1902 default:
wolfSSL 11:cee25a834751 1903 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1904 }
wolfSSL 11:cee25a834751 1905
wolfSSL 11:cee25a834751 1906 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 1907 if (dir == AES_DECRYPTION)
wolfSSL 11:cee25a834751 1908 {
wolfSSL 11:cee25a834751 1909 unsigned int j;
wolfSSL 11:cee25a834751 1910 rk = aes->key;
wolfSSL 11:cee25a834751 1911
wolfSSL 11:cee25a834751 1912 /* invert the order of the round keys: */
wolfSSL 11:cee25a834751 1913 for (i = 0, j = 4* aes->rounds; i < j; i += 4, j -= 4) {
wolfSSL 11:cee25a834751 1914 temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
wolfSSL 11:cee25a834751 1915 temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
wolfSSL 11:cee25a834751 1916 temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
wolfSSL 11:cee25a834751 1917 temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
wolfSSL 11:cee25a834751 1918 }
wolfSSL 11:cee25a834751 1919 /* apply the inverse MixColumn transform to all round keys but the
wolfSSL 11:cee25a834751 1920 first and the last: */
wolfSSL 11:cee25a834751 1921 for (i = 1; i < aes->rounds; i++) {
wolfSSL 11:cee25a834751 1922 rk += 4;
wolfSSL 11:cee25a834751 1923 rk[0] =
wolfSSL 11:cee25a834751 1924 Td[0][Te[1][GETBYTE(rk[0], 3)] & 0xff] ^
wolfSSL 11:cee25a834751 1925 Td[1][Te[1][GETBYTE(rk[0], 2)] & 0xff] ^
wolfSSL 11:cee25a834751 1926 Td[2][Te[1][GETBYTE(rk[0], 1)] & 0xff] ^
wolfSSL 11:cee25a834751 1927 Td[3][Te[1][GETBYTE(rk[0], 0)] & 0xff];
wolfSSL 11:cee25a834751 1928 rk[1] =
wolfSSL 11:cee25a834751 1929 Td[0][Te[1][GETBYTE(rk[1], 3)] & 0xff] ^
wolfSSL 11:cee25a834751 1930 Td[1][Te[1][GETBYTE(rk[1], 2)] & 0xff] ^
wolfSSL 11:cee25a834751 1931 Td[2][Te[1][GETBYTE(rk[1], 1)] & 0xff] ^
wolfSSL 11:cee25a834751 1932 Td[3][Te[1][GETBYTE(rk[1], 0)] & 0xff];
wolfSSL 11:cee25a834751 1933 rk[2] =
wolfSSL 11:cee25a834751 1934 Td[0][Te[1][GETBYTE(rk[2], 3)] & 0xff] ^
wolfSSL 11:cee25a834751 1935 Td[1][Te[1][GETBYTE(rk[2], 2)] & 0xff] ^
wolfSSL 11:cee25a834751 1936 Td[2][Te[1][GETBYTE(rk[2], 1)] & 0xff] ^
wolfSSL 11:cee25a834751 1937 Td[3][Te[1][GETBYTE(rk[2], 0)] & 0xff];
wolfSSL 11:cee25a834751 1938 rk[3] =
wolfSSL 11:cee25a834751 1939 Td[0][Te[1][GETBYTE(rk[3], 3)] & 0xff] ^
wolfSSL 11:cee25a834751 1940 Td[1][Te[1][GETBYTE(rk[3], 2)] & 0xff] ^
wolfSSL 11:cee25a834751 1941 Td[2][Te[1][GETBYTE(rk[3], 1)] & 0xff] ^
wolfSSL 11:cee25a834751 1942 Td[3][Te[1][GETBYTE(rk[3], 0)] & 0xff];
wolfSSL 11:cee25a834751 1943 }
wolfSSL 11:cee25a834751 1944 }
wolfSSL 11:cee25a834751 1945 #else
wolfSSL 11:cee25a834751 1946 (void)dir;
wolfSSL 11:cee25a834751 1947 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 1948
wolfSSL 11:cee25a834751 1949 return wc_AesSetIV(aes, iv);
wolfSSL 11:cee25a834751 1950 }
wolfSSL 11:cee25a834751 1951
wolfSSL 11:cee25a834751 1952 int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 1953 const byte* iv, int dir)
wolfSSL 11:cee25a834751 1954 {
wolfSSL 11:cee25a834751 1955 #if defined(AES_MAX_KEY_SIZE)
wolfSSL 11:cee25a834751 1956 const word32 max_key_len = (AES_MAX_KEY_SIZE / 8);
wolfSSL 11:cee25a834751 1957 #endif
wolfSSL 11:cee25a834751 1958
wolfSSL 11:cee25a834751 1959 if (aes == NULL ||
wolfSSL 11:cee25a834751 1960 !((keylen == 16) || (keylen == 24) || (keylen == 32))) {
wolfSSL 11:cee25a834751 1961 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1962 }
wolfSSL 11:cee25a834751 1963
wolfSSL 11:cee25a834751 1964 #if defined(AES_MAX_KEY_SIZE)
wolfSSL 11:cee25a834751 1965 /* Check key length */
wolfSSL 11:cee25a834751 1966 if (keylen > max_key_len) {
wolfSSL 11:cee25a834751 1967 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 1968 }
wolfSSL 11:cee25a834751 1969 #endif
wolfSSL 11:cee25a834751 1970 aes->keylen = keylen;
wolfSSL 11:cee25a834751 1971 aes->rounds = keylen/4 + 6;
wolfSSL 11:cee25a834751 1972
wolfSSL 11:cee25a834751 1973 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
wolfSSL 11:cee25a834751 1974 if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES) {
wolfSSL 11:cee25a834751 1975 aes->asyncKey = userKey;
wolfSSL 11:cee25a834751 1976 aes->asyncIv = iv;
wolfSSL 11:cee25a834751 1977 }
wolfSSL 11:cee25a834751 1978 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 1979
wolfSSL 11:cee25a834751 1980 #ifdef WOLFSSL_AESNI
wolfSSL 11:cee25a834751 1981 if (checkAESNI == 0) {
wolfSSL 11:cee25a834751 1982 haveAESNI = Check_CPU_support_AES();
wolfSSL 11:cee25a834751 1983 checkAESNI = 1;
wolfSSL 11:cee25a834751 1984 }
wolfSSL 11:cee25a834751 1985 if (haveAESNI) {
wolfSSL 11:cee25a834751 1986 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 11:cee25a834751 1987 aes->left = 0;
wolfSSL 11:cee25a834751 1988 #endif /* WOLFSSL_AES_COUNTER */
wolfSSL 11:cee25a834751 1989 aes->use_aesni = 1;
wolfSSL 11:cee25a834751 1990 if (iv)
wolfSSL 11:cee25a834751 1991 XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 1992 if (dir == AES_ENCRYPTION)
wolfSSL 11:cee25a834751 1993 return AES_set_encrypt_key(userKey, keylen * 8, aes);
wolfSSL 11:cee25a834751 1994 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 1995 else
wolfSSL 11:cee25a834751 1996 return AES_set_decrypt_key(userKey, keylen * 8, aes);
wolfSSL 11:cee25a834751 1997 #endif
wolfSSL 11:cee25a834751 1998 }
wolfSSL 11:cee25a834751 1999 #endif /* WOLFSSL_AESNI */
wolfSSL 11:cee25a834751 2000
wolfSSL 11:cee25a834751 2001 return wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir);
wolfSSL 11:cee25a834751 2002 }
wolfSSL 11:cee25a834751 2003
wolfSSL 11:cee25a834751 2004 #if defined(WOLFSSL_AES_DIRECT) || defined(WOLFSSL_AES_COUNTER)
wolfSSL 11:cee25a834751 2005 /* AES-CTR and AES-DIRECT need to use this for key setup, no aesni yet */
wolfSSL 11:cee25a834751 2006 int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 11:cee25a834751 2007 const byte* iv, int dir)
wolfSSL 11:cee25a834751 2008 {
wolfSSL 11:cee25a834751 2009 return wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir);
wolfSSL 11:cee25a834751 2010 }
wolfSSL 11:cee25a834751 2011 #endif /* WOLFSSL_AES_DIRECT || WOLFSSL_AES_COUNTER */
wolfSSL 11:cee25a834751 2012 #endif /* wc_AesSetKey block */
wolfSSL 11:cee25a834751 2013
wolfSSL 11:cee25a834751 2014
wolfSSL 11:cee25a834751 2015 /* wc_AesSetIV is shared between software and hardware */
wolfSSL 11:cee25a834751 2016 int wc_AesSetIV(Aes* aes, const byte* iv)
wolfSSL 11:cee25a834751 2017 {
wolfSSL 11:cee25a834751 2018 if (aes == NULL)
wolfSSL 11:cee25a834751 2019 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 2020
wolfSSL 11:cee25a834751 2021 if (iv)
wolfSSL 11:cee25a834751 2022 XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2023 else
wolfSSL 11:cee25a834751 2024 XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2025
wolfSSL 11:cee25a834751 2026 return 0;
wolfSSL 11:cee25a834751 2027 }
wolfSSL 11:cee25a834751 2028
wolfSSL 11:cee25a834751 2029 /* AES-DIRECT */
wolfSSL 11:cee25a834751 2030 #if defined(WOLFSSL_AES_DIRECT)
wolfSSL 11:cee25a834751 2031 #if defined(HAVE_COLDFIRE_SEC)
wolfSSL 11:cee25a834751 2032 #error "Coldfire SEC doesn't yet support AES direct"
wolfSSL 11:cee25a834751 2033
wolfSSL 11:cee25a834751 2034 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 11:cee25a834751 2035 #error "PIC32MZ doesn't yet support AES direct"
wolfSSL 11:cee25a834751 2036
wolfSSL 11:cee25a834751 2037 #elif defined(FREESCALE_LTC)
wolfSSL 11:cee25a834751 2038 /* Allow direct access to one block encrypt */
wolfSSL 11:cee25a834751 2039 void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 11:cee25a834751 2040 {
wolfSSL 11:cee25a834751 2041 byte *key;
wolfSSL 11:cee25a834751 2042 uint32_t keySize;
wolfSSL 11:cee25a834751 2043
wolfSSL 11:cee25a834751 2044 key = (byte*)aes->key;
wolfSSL 11:cee25a834751 2045 wc_AesGetKeySize(aes, &keySize);
wolfSSL 11:cee25a834751 2046
wolfSSL 11:cee25a834751 2047 LTC_AES_EncryptEcb(LTC_BASE, in, out, AES_BLOCK_SIZE,
wolfSSL 11:cee25a834751 2048 key, keySize);
wolfSSL 11:cee25a834751 2049 }
wolfSSL 11:cee25a834751 2050
wolfSSL 11:cee25a834751 2051 /* Allow direct access to one block decrypt */
wolfSSL 11:cee25a834751 2052 void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 11:cee25a834751 2053 {
wolfSSL 11:cee25a834751 2054 byte *key;
wolfSSL 11:cee25a834751 2055 uint32_t keySize;
wolfSSL 11:cee25a834751 2056
wolfSSL 11:cee25a834751 2057 key = (byte*)aes->key;
wolfSSL 11:cee25a834751 2058 wc_AesGetKeySize(aes, &keySize);
wolfSSL 11:cee25a834751 2059
wolfSSL 11:cee25a834751 2060 LTC_AES_DecryptEcb(LTC_BASE, in, out, AES_BLOCK_SIZE,
wolfSSL 11:cee25a834751 2061 key, keySize, kLTC_EncryptKey);
wolfSSL 11:cee25a834751 2062 }
wolfSSL 11:cee25a834751 2063
wolfSSL 11:cee25a834751 2064 #else
wolfSSL 11:cee25a834751 2065 /* Allow direct access to one block encrypt */
wolfSSL 11:cee25a834751 2066 void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 11:cee25a834751 2067 {
wolfSSL 11:cee25a834751 2068 wc_AesEncrypt(aes, in, out);
wolfSSL 11:cee25a834751 2069 }
wolfSSL 11:cee25a834751 2070 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 2071 /* Allow direct access to one block decrypt */
wolfSSL 11:cee25a834751 2072 void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 11:cee25a834751 2073 {
wolfSSL 11:cee25a834751 2074 wc_AesDecrypt(aes, in, out);
wolfSSL 11:cee25a834751 2075 }
wolfSSL 11:cee25a834751 2076 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 2077 #endif /* AES direct block */
wolfSSL 11:cee25a834751 2078 #endif /* WOLFSSL_AES_DIRECT */
wolfSSL 11:cee25a834751 2079
wolfSSL 11:cee25a834751 2080
wolfSSL 11:cee25a834751 2081 /* AES-CBC */
wolfSSL 11:cee25a834751 2082 #ifdef HAVE_AES_CBC
wolfSSL 11:cee25a834751 2083 #if defined(STM32F2_CRYPTO) || defined(STM32F4_CRYPTO)
wolfSSL 11:cee25a834751 2084
wolfSSL 11:cee25a834751 2085 #ifdef WOLFSSL_STM32_CUBEMX
wolfSSL 11:cee25a834751 2086 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2087 {
wolfSSL 11:cee25a834751 2088 int ret = 0;
wolfSSL 11:cee25a834751 2089 CRYP_HandleTypeDef hcryp;
wolfSSL 11:cee25a834751 2090 XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef));
wolfSSL 11:cee25a834751 2091 /* load key into correct registers */
wolfSSL 11:cee25a834751 2092 switch(aes->rounds) {
wolfSSL 11:cee25a834751 2093 case 10: /* 128-bit key */
wolfSSL 11:cee25a834751 2094 hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
wolfSSL 11:cee25a834751 2095 break;
wolfSSL 11:cee25a834751 2096 case 12: /* 192-bit key */
wolfSSL 11:cee25a834751 2097 hcryp.Init.KeySize = CRYP_KEYSIZE_192B;
wolfSSL 11:cee25a834751 2098 break;
wolfSSL 11:cee25a834751 2099 case 14: /* 256-bit key */
wolfSSL 11:cee25a834751 2100 hcryp.Init.KeySize = CRYP_KEYSIZE_256B;
wolfSSL 11:cee25a834751 2101 break;
wolfSSL 11:cee25a834751 2102 default:
wolfSSL 11:cee25a834751 2103 break;
wolfSSL 11:cee25a834751 2104 }
wolfSSL 11:cee25a834751 2105 hcryp.Instance = CRYP;
wolfSSL 11:cee25a834751 2106 hcryp.Init.DataType = CRYP_DATATYPE_8B;
wolfSSL 11:cee25a834751 2107 hcryp.Init.pKey = (uint8_t*)aes->key;
wolfSSL 11:cee25a834751 2108 hcryp.Init.pInitVect = (uint8_t*)aes->reg;
wolfSSL 11:cee25a834751 2109
wolfSSL 11:cee25a834751 2110 HAL_CRYP_Init(&hcryp);
wolfSSL 11:cee25a834751 2111
wolfSSL 11:cee25a834751 2112 while (sz > 0) {
wolfSSL 11:cee25a834751 2113 if (HAL_CRYP_AESCBC_Encrypt(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
wolfSSL 11:cee25a834751 2114 out, STM32_HAL_TIMEOUT) != HAL_OK) {
wolfSSL 11:cee25a834751 2115 ret = WC_TIMEOUT_E;
wolfSSL 11:cee25a834751 2116 break;
wolfSSL 11:cee25a834751 2117 }
wolfSSL 11:cee25a834751 2118
wolfSSL 11:cee25a834751 2119 /* store iv for next call */
wolfSSL 11:cee25a834751 2120 XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2121
wolfSSL 11:cee25a834751 2122 sz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2123 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2124 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2125 }
wolfSSL 11:cee25a834751 2126
wolfSSL 11:cee25a834751 2127 HAL_CRYP_DeInit(&hcryp);
wolfSSL 11:cee25a834751 2128
wolfSSL 11:cee25a834751 2129 return ret;
wolfSSL 11:cee25a834751 2130 }
wolfSSL 11:cee25a834751 2131 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 2132 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2133 {
wolfSSL 11:cee25a834751 2134 int ret = 0;
wolfSSL 11:cee25a834751 2135 CRYP_HandleTypeDef hcryp;
wolfSSL 11:cee25a834751 2136 XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef));
wolfSSL 11:cee25a834751 2137 /* load key into correct registers */
wolfSSL 11:cee25a834751 2138 switch(aes->rounds) {
wolfSSL 11:cee25a834751 2139 case 10: /* 128-bit key */
wolfSSL 11:cee25a834751 2140 hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
wolfSSL 11:cee25a834751 2141 break;
wolfSSL 11:cee25a834751 2142 case 12: /* 192-bit key */
wolfSSL 11:cee25a834751 2143 hcryp.Init.KeySize = CRYP_KEYSIZE_192B;
wolfSSL 11:cee25a834751 2144 break;
wolfSSL 11:cee25a834751 2145 case 14: /* 256-bit key */
wolfSSL 11:cee25a834751 2146 hcryp.Init.KeySize = CRYP_KEYSIZE_256B;
wolfSSL 11:cee25a834751 2147 break;
wolfSSL 11:cee25a834751 2148 default:
wolfSSL 11:cee25a834751 2149 break;
wolfSSL 11:cee25a834751 2150 }
wolfSSL 11:cee25a834751 2151 hcryp.Instance = CRYP;
wolfSSL 11:cee25a834751 2152 hcryp.Init.DataType = CRYP_DATATYPE_8B;
wolfSSL 11:cee25a834751 2153 hcryp.Init.pKey = (uint8_t*)aes->key;
wolfSSL 11:cee25a834751 2154 hcryp.Init.pInitVect = (uint8_t*)aes->reg;
wolfSSL 11:cee25a834751 2155
wolfSSL 11:cee25a834751 2156 HAL_CRYP_Init(&hcryp);
wolfSSL 11:cee25a834751 2157
wolfSSL 11:cee25a834751 2158 while (sz > 0) {
wolfSSL 11:cee25a834751 2159 if (HAL_CRYP_AESCBC_Decrypt(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
wolfSSL 11:cee25a834751 2160 out, STM32_HAL_TIMEOUT) != HAL_OK) {
wolfSSL 11:cee25a834751 2161 ret = WC_TIMEOUT_E;
wolfSSL 11:cee25a834751 2162 }
wolfSSL 11:cee25a834751 2163
wolfSSL 11:cee25a834751 2164 /* store iv for next call */
wolfSSL 11:cee25a834751 2165 XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2166
wolfSSL 11:cee25a834751 2167 sz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2168 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2169 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2170 }
wolfSSL 11:cee25a834751 2171
wolfSSL 11:cee25a834751 2172 HAL_CRYP_DeInit(&hcryp);
wolfSSL 11:cee25a834751 2173
wolfSSL 11:cee25a834751 2174 return ret;
wolfSSL 11:cee25a834751 2175 }
wolfSSL 11:cee25a834751 2176 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 2177 #else
wolfSSL 11:cee25a834751 2178 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2179 {
wolfSSL 11:cee25a834751 2180 word32 *enc_key, *iv;
wolfSSL 11:cee25a834751 2181 CRYP_InitTypeDef AES_CRYP_InitStructure;
wolfSSL 11:cee25a834751 2182 CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
wolfSSL 11:cee25a834751 2183 CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
wolfSSL 11:cee25a834751 2184
wolfSSL 11:cee25a834751 2185 enc_key = aes->key;
wolfSSL 11:cee25a834751 2186 iv = aes->reg;
wolfSSL 11:cee25a834751 2187
wolfSSL 11:cee25a834751 2188 /* crypto structure initialization */
wolfSSL 11:cee25a834751 2189 CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
wolfSSL 11:cee25a834751 2190 CRYP_StructInit(&AES_CRYP_InitStructure);
wolfSSL 11:cee25a834751 2191 CRYP_IVStructInit(&AES_CRYP_IVInitStructure);
wolfSSL 11:cee25a834751 2192
wolfSSL 11:cee25a834751 2193 /* reset registers to their default values */
wolfSSL 11:cee25a834751 2194 CRYP_DeInit();
wolfSSL 11:cee25a834751 2195
wolfSSL 11:cee25a834751 2196 /* load key into correct registers */
wolfSSL 11:cee25a834751 2197 switch(aes->rounds)
wolfSSL 11:cee25a834751 2198 {
wolfSSL 11:cee25a834751 2199 case 10: /* 128-bit key */
wolfSSL 11:cee25a834751 2200 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
wolfSSL 11:cee25a834751 2201 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[0];
wolfSSL 11:cee25a834751 2202 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[1];
wolfSSL 11:cee25a834751 2203 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[2];
wolfSSL 11:cee25a834751 2204 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[3];
wolfSSL 11:cee25a834751 2205 break;
wolfSSL 11:cee25a834751 2206
wolfSSL 11:cee25a834751 2207 case 12: /* 192-bit key */
wolfSSL 11:cee25a834751 2208 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
wolfSSL 11:cee25a834751 2209 AES_CRYP_KeyInitStructure.CRYP_Key1Left = enc_key[0];
wolfSSL 11:cee25a834751 2210 AES_CRYP_KeyInitStructure.CRYP_Key1Right = enc_key[1];
wolfSSL 11:cee25a834751 2211 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[2];
wolfSSL 11:cee25a834751 2212 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[3];
wolfSSL 11:cee25a834751 2213 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[4];
wolfSSL 11:cee25a834751 2214 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[5];
wolfSSL 11:cee25a834751 2215 break;
wolfSSL 11:cee25a834751 2216
wolfSSL 11:cee25a834751 2217 case 14: /* 256-bit key */
wolfSSL 11:cee25a834751 2218 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
wolfSSL 11:cee25a834751 2219 AES_CRYP_KeyInitStructure.CRYP_Key0Left = enc_key[0];
wolfSSL 11:cee25a834751 2220 AES_CRYP_KeyInitStructure.CRYP_Key0Right = enc_key[1];
wolfSSL 11:cee25a834751 2221 AES_CRYP_KeyInitStructure.CRYP_Key1Left = enc_key[2];
wolfSSL 11:cee25a834751 2222 AES_CRYP_KeyInitStructure.CRYP_Key1Right = enc_key[3];
wolfSSL 11:cee25a834751 2223 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[4];
wolfSSL 11:cee25a834751 2224 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[5];
wolfSSL 11:cee25a834751 2225 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[6];
wolfSSL 11:cee25a834751 2226 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[7];
wolfSSL 11:cee25a834751 2227 break;
wolfSSL 11:cee25a834751 2228
wolfSSL 11:cee25a834751 2229 default:
wolfSSL 11:cee25a834751 2230 break;
wolfSSL 11:cee25a834751 2231 }
wolfSSL 11:cee25a834751 2232 CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
wolfSSL 11:cee25a834751 2233
wolfSSL 11:cee25a834751 2234 /* set iv */
wolfSSL 11:cee25a834751 2235 ByteReverseWords(iv, iv, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2236 AES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
wolfSSL 11:cee25a834751 2237 AES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
wolfSSL 11:cee25a834751 2238 AES_CRYP_IVInitStructure.CRYP_IV1Left = iv[2];
wolfSSL 11:cee25a834751 2239 AES_CRYP_IVInitStructure.CRYP_IV1Right = iv[3];
wolfSSL 11:cee25a834751 2240 CRYP_IVInit(&AES_CRYP_IVInitStructure);
wolfSSL 11:cee25a834751 2241
wolfSSL 11:cee25a834751 2242 /* set direction, mode, and datatype */
wolfSSL 11:cee25a834751 2243 AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
wolfSSL 11:cee25a834751 2244 AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;
wolfSSL 11:cee25a834751 2245 AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 11:cee25a834751 2246 CRYP_Init(&AES_CRYP_InitStructure);
wolfSSL 11:cee25a834751 2247
wolfSSL 11:cee25a834751 2248 /* enable crypto processor */
wolfSSL 11:cee25a834751 2249 CRYP_Cmd(ENABLE);
wolfSSL 11:cee25a834751 2250
wolfSSL 11:cee25a834751 2251 while (sz > 0)
wolfSSL 11:cee25a834751 2252 {
wolfSSL 11:cee25a834751 2253 /* flush IN/OUT FIFOs */
wolfSSL 11:cee25a834751 2254 CRYP_FIFOFlush();
wolfSSL 11:cee25a834751 2255
wolfSSL 11:cee25a834751 2256 CRYP_DataIn(*(uint32_t*)&in[0]);
wolfSSL 11:cee25a834751 2257 CRYP_DataIn(*(uint32_t*)&in[4]);
wolfSSL 11:cee25a834751 2258 CRYP_DataIn(*(uint32_t*)&in[8]);
wolfSSL 11:cee25a834751 2259 CRYP_DataIn(*(uint32_t*)&in[12]);
wolfSSL 11:cee25a834751 2260
wolfSSL 11:cee25a834751 2261 /* wait until the complete message has been processed */
wolfSSL 11:cee25a834751 2262 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 11:cee25a834751 2263
wolfSSL 11:cee25a834751 2264 *(uint32_t*)&out[0] = CRYP_DataOut();
wolfSSL 11:cee25a834751 2265 *(uint32_t*)&out[4] = CRYP_DataOut();
wolfSSL 11:cee25a834751 2266 *(uint32_t*)&out[8] = CRYP_DataOut();
wolfSSL 11:cee25a834751 2267 *(uint32_t*)&out[12] = CRYP_DataOut();
wolfSSL 11:cee25a834751 2268
wolfSSL 11:cee25a834751 2269 /* store iv for next call */
wolfSSL 11:cee25a834751 2270 XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2271
wolfSSL 11:cee25a834751 2272 sz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2273 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2274 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2275 }
wolfSSL 11:cee25a834751 2276
wolfSSL 11:cee25a834751 2277 /* disable crypto processor */
wolfSSL 11:cee25a834751 2278 CRYP_Cmd(DISABLE);
wolfSSL 11:cee25a834751 2279
wolfSSL 11:cee25a834751 2280 return 0;
wolfSSL 11:cee25a834751 2281 }
wolfSSL 11:cee25a834751 2282
wolfSSL 11:cee25a834751 2283 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 2284 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2285 {
wolfSSL 11:cee25a834751 2286 word32 *dec_key, *iv;
wolfSSL 11:cee25a834751 2287 CRYP_InitTypeDef AES_CRYP_InitStructure;
wolfSSL 11:cee25a834751 2288 CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
wolfSSL 11:cee25a834751 2289 CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
wolfSSL 11:cee25a834751 2290
wolfSSL 11:cee25a834751 2291 dec_key = aes->key;
wolfSSL 11:cee25a834751 2292 iv = aes->reg;
wolfSSL 11:cee25a834751 2293
wolfSSL 11:cee25a834751 2294 /* crypto structure initialization */
wolfSSL 11:cee25a834751 2295 CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
wolfSSL 11:cee25a834751 2296 CRYP_StructInit(&AES_CRYP_InitStructure);
wolfSSL 11:cee25a834751 2297 CRYP_IVStructInit(&AES_CRYP_IVInitStructure);
wolfSSL 11:cee25a834751 2298
wolfSSL 11:cee25a834751 2299 /* if input and output same will overwrite input iv */
wolfSSL 11:cee25a834751 2300 XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2301
wolfSSL 11:cee25a834751 2302 /* reset registers to their default values */
wolfSSL 11:cee25a834751 2303 CRYP_DeInit();
wolfSSL 11:cee25a834751 2304
wolfSSL 11:cee25a834751 2305 /* load key into correct registers */
wolfSSL 11:cee25a834751 2306 switch(aes->rounds)
wolfSSL 11:cee25a834751 2307 {
wolfSSL 11:cee25a834751 2308 case 10: /* 128-bit key */
wolfSSL 11:cee25a834751 2309 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
wolfSSL 11:cee25a834751 2310 AES_CRYP_KeyInitStructure.CRYP_Key2Left = dec_key[0];
wolfSSL 11:cee25a834751 2311 AES_CRYP_KeyInitStructure.CRYP_Key2Right = dec_key[1];
wolfSSL 11:cee25a834751 2312 AES_CRYP_KeyInitStructure.CRYP_Key3Left = dec_key[2];
wolfSSL 11:cee25a834751 2313 AES_CRYP_KeyInitStructure.CRYP_Key3Right = dec_key[3];
wolfSSL 11:cee25a834751 2314 break;
wolfSSL 11:cee25a834751 2315
wolfSSL 11:cee25a834751 2316 case 12: /* 192-bit key */
wolfSSL 11:cee25a834751 2317 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
wolfSSL 11:cee25a834751 2318 AES_CRYP_KeyInitStructure.CRYP_Key1Left = dec_key[0];
wolfSSL 11:cee25a834751 2319 AES_CRYP_KeyInitStructure.CRYP_Key1Right = dec_key[1];
wolfSSL 11:cee25a834751 2320 AES_CRYP_KeyInitStructure.CRYP_Key2Left = dec_key[2];
wolfSSL 11:cee25a834751 2321 AES_CRYP_KeyInitStructure.CRYP_Key2Right = dec_key[3];
wolfSSL 11:cee25a834751 2322 AES_CRYP_KeyInitStructure.CRYP_Key3Left = dec_key[4];
wolfSSL 11:cee25a834751 2323 AES_CRYP_KeyInitStructure.CRYP_Key3Right = dec_key[5];
wolfSSL 11:cee25a834751 2324 break;
wolfSSL 11:cee25a834751 2325
wolfSSL 11:cee25a834751 2326 case 14: /* 256-bit key */
wolfSSL 11:cee25a834751 2327 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
wolfSSL 11:cee25a834751 2328 AES_CRYP_KeyInitStructure.CRYP_Key0Left = dec_key[0];
wolfSSL 11:cee25a834751 2329 AES_CRYP_KeyInitStructure.CRYP_Key0Right = dec_key[1];
wolfSSL 11:cee25a834751 2330 AES_CRYP_KeyInitStructure.CRYP_Key1Left = dec_key[2];
wolfSSL 11:cee25a834751 2331 AES_CRYP_KeyInitStructure.CRYP_Key1Right = dec_key[3];
wolfSSL 11:cee25a834751 2332 AES_CRYP_KeyInitStructure.CRYP_Key2Left = dec_key[4];
wolfSSL 11:cee25a834751 2333 AES_CRYP_KeyInitStructure.CRYP_Key2Right = dec_key[5];
wolfSSL 11:cee25a834751 2334 AES_CRYP_KeyInitStructure.CRYP_Key3Left = dec_key[6];
wolfSSL 11:cee25a834751 2335 AES_CRYP_KeyInitStructure.CRYP_Key3Right = dec_key[7];
wolfSSL 11:cee25a834751 2336 break;
wolfSSL 11:cee25a834751 2337
wolfSSL 11:cee25a834751 2338 default:
wolfSSL 11:cee25a834751 2339 break;
wolfSSL 11:cee25a834751 2340 }
wolfSSL 11:cee25a834751 2341
wolfSSL 11:cee25a834751 2342 /* set direction, mode, and datatype for key preparation */
wolfSSL 11:cee25a834751 2343 AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
wolfSSL 11:cee25a834751 2344 AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
wolfSSL 11:cee25a834751 2345 AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_32b;
wolfSSL 11:cee25a834751 2346 CRYP_Init(&AES_CRYP_InitStructure);
wolfSSL 11:cee25a834751 2347 CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
wolfSSL 11:cee25a834751 2348
wolfSSL 11:cee25a834751 2349 /* enable crypto processor */
wolfSSL 11:cee25a834751 2350 CRYP_Cmd(ENABLE);
wolfSSL 11:cee25a834751 2351
wolfSSL 11:cee25a834751 2352 /* wait until key has been prepared */
wolfSSL 11:cee25a834751 2353 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 11:cee25a834751 2354
wolfSSL 11:cee25a834751 2355 /* set direction, mode, and datatype for decryption */
wolfSSL 11:cee25a834751 2356 AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
wolfSSL 11:cee25a834751 2357 AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;
wolfSSL 11:cee25a834751 2358 AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 11:cee25a834751 2359 CRYP_Init(&AES_CRYP_InitStructure);
wolfSSL 11:cee25a834751 2360
wolfSSL 11:cee25a834751 2361 /* set iv */
wolfSSL 11:cee25a834751 2362 ByteReverseWords(iv, iv, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2363
wolfSSL 11:cee25a834751 2364 AES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
wolfSSL 11:cee25a834751 2365 AES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
wolfSSL 11:cee25a834751 2366 AES_CRYP_IVInitStructure.CRYP_IV1Left = iv[2];
wolfSSL 11:cee25a834751 2367 AES_CRYP_IVInitStructure.CRYP_IV1Right = iv[3];
wolfSSL 11:cee25a834751 2368 CRYP_IVInit(&AES_CRYP_IVInitStructure);
wolfSSL 11:cee25a834751 2369
wolfSSL 11:cee25a834751 2370 /* enable crypto processor */
wolfSSL 11:cee25a834751 2371 CRYP_Cmd(ENABLE);
wolfSSL 11:cee25a834751 2372
wolfSSL 11:cee25a834751 2373 while (sz > 0)
wolfSSL 11:cee25a834751 2374 {
wolfSSL 11:cee25a834751 2375 /* flush IN/OUT FIFOs */
wolfSSL 11:cee25a834751 2376 CRYP_FIFOFlush();
wolfSSL 11:cee25a834751 2377
wolfSSL 11:cee25a834751 2378 CRYP_DataIn(*(uint32_t*)&in[0]);
wolfSSL 11:cee25a834751 2379 CRYP_DataIn(*(uint32_t*)&in[4]);
wolfSSL 11:cee25a834751 2380 CRYP_DataIn(*(uint32_t*)&in[8]);
wolfSSL 11:cee25a834751 2381 CRYP_DataIn(*(uint32_t*)&in[12]);
wolfSSL 11:cee25a834751 2382
wolfSSL 11:cee25a834751 2383 /* wait until the complete message has been processed */
wolfSSL 11:cee25a834751 2384 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 11:cee25a834751 2385
wolfSSL 11:cee25a834751 2386 *(uint32_t*)&out[0] = CRYP_DataOut();
wolfSSL 11:cee25a834751 2387 *(uint32_t*)&out[4] = CRYP_DataOut();
wolfSSL 11:cee25a834751 2388 *(uint32_t*)&out[8] = CRYP_DataOut();
wolfSSL 11:cee25a834751 2389 *(uint32_t*)&out[12] = CRYP_DataOut();
wolfSSL 11:cee25a834751 2390
wolfSSL 11:cee25a834751 2391 /* store iv for next call */
wolfSSL 11:cee25a834751 2392 XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2393
wolfSSL 11:cee25a834751 2394 sz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2395 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2396 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2397 }
wolfSSL 11:cee25a834751 2398
wolfSSL 11:cee25a834751 2399 /* disable crypto processor */
wolfSSL 11:cee25a834751 2400 CRYP_Cmd(DISABLE);
wolfSSL 11:cee25a834751 2401
wolfSSL 11:cee25a834751 2402 return 0;
wolfSSL 11:cee25a834751 2403 }
wolfSSL 11:cee25a834751 2404 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 2405 #endif /* WOLFSSL_STM32_CUBEMX */
wolfSSL 11:cee25a834751 2406
wolfSSL 11:cee25a834751 2407 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 11:cee25a834751 2408 static int wc_AesCbcCrypt(Aes* aes, byte* po, const byte* pi, word32 sz,
wolfSSL 11:cee25a834751 2409 word32 descHeader)
wolfSSL 11:cee25a834751 2410 {
wolfSSL 11:cee25a834751 2411 #ifdef DEBUG_WOLFSSL
wolfSSL 11:cee25a834751 2412 int i; int stat1, stat2; int ret;
wolfSSL 11:cee25a834751 2413 #endif
wolfSSL 11:cee25a834751 2414
wolfSSL 11:cee25a834751 2415 int size;
wolfSSL 11:cee25a834751 2416 volatile int v;
wolfSSL 11:cee25a834751 2417
wolfSSL 11:cee25a834751 2418 if ((pi == NULL) || (po == NULL))
wolfSSL 11:cee25a834751 2419 return BAD_FUNC_ARG; /*wrong pointer*/
wolfSSL 11:cee25a834751 2420
wolfSSL 11:cee25a834751 2421 wc_LockMutex(&Mutex_AesSEC);
wolfSSL 11:cee25a834751 2422
wolfSSL 11:cee25a834751 2423 /* Set descriptor for SEC */
wolfSSL 11:cee25a834751 2424 secDesc->length1 = 0x0;
wolfSSL 11:cee25a834751 2425 secDesc->pointer1 = NULL;
wolfSSL 11:cee25a834751 2426
wolfSSL 11:cee25a834751 2427 secDesc->length2 = AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2428 secDesc->pointer2 = (byte *)secReg; /* Initial Vector */
wolfSSL 11:cee25a834751 2429
wolfSSL 11:cee25a834751 2430 switch(aes->rounds) {
wolfSSL 11:cee25a834751 2431 case 10: secDesc->length3 = 16; break;
wolfSSL 11:cee25a834751 2432 case 12: secDesc->length3 = 24; break;
wolfSSL 11:cee25a834751 2433 case 14: secDesc->length3 = 32; break;
wolfSSL 11:cee25a834751 2434 }
wolfSSL 11:cee25a834751 2435 XMEMCPY(secKey, aes->key, secDesc->length3);
wolfSSL 11:cee25a834751 2436
wolfSSL 11:cee25a834751 2437 secDesc->pointer3 = (byte *)secKey;
wolfSSL 11:cee25a834751 2438 secDesc->pointer4 = AESBuffIn;
wolfSSL 11:cee25a834751 2439 secDesc->pointer5 = AESBuffOut;
wolfSSL 11:cee25a834751 2440 secDesc->length6 = 0x0;
wolfSSL 11:cee25a834751 2441 secDesc->pointer6 = NULL;
wolfSSL 11:cee25a834751 2442 secDesc->length7 = 0x0;
wolfSSL 11:cee25a834751 2443 secDesc->pointer7 = NULL;
wolfSSL 11:cee25a834751 2444 secDesc->nextDescriptorPtr = NULL;
wolfSSL 11:cee25a834751 2445
wolfSSL 11:cee25a834751 2446 while (sz) {
wolfSSL 11:cee25a834751 2447 secDesc->header = descHeader;
wolfSSL 11:cee25a834751 2448 XMEMCPY(secReg, aes->reg, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2449 if ((sz % AES_BUFFER_SIZE) == sz) {
wolfSSL 11:cee25a834751 2450 size = sz;
wolfSSL 11:cee25a834751 2451 sz = 0;
wolfSSL 11:cee25a834751 2452 } else {
wolfSSL 11:cee25a834751 2453 size = AES_BUFFER_SIZE;
wolfSSL 11:cee25a834751 2454 sz -= AES_BUFFER_SIZE;
wolfSSL 11:cee25a834751 2455 }
wolfSSL 11:cee25a834751 2456 secDesc->length4 = size;
wolfSSL 11:cee25a834751 2457 secDesc->length5 = size;
wolfSSL 11:cee25a834751 2458
wolfSSL 11:cee25a834751 2459 XMEMCPY(AESBuffIn, pi, size);
wolfSSL 11:cee25a834751 2460 if(descHeader == SEC_DESC_AES_CBC_DECRYPT) {
wolfSSL 11:cee25a834751 2461 XMEMCPY((void*)aes->tmp, (void*)&(pi[size-AES_BLOCK_SIZE]),
wolfSSL 11:cee25a834751 2462 AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2463 }
wolfSSL 11:cee25a834751 2464
wolfSSL 11:cee25a834751 2465 /* Point SEC to the location of the descriptor */
wolfSSL 11:cee25a834751 2466 MCF_SEC_FR0 = (uint32)secDesc;
wolfSSL 11:cee25a834751 2467 /* Initialize SEC and wait for encryption to complete */
wolfSSL 11:cee25a834751 2468 MCF_SEC_CCCR0 = 0x0000001a;
wolfSSL 11:cee25a834751 2469 /* poll SISR to determine when channel is complete */
wolfSSL 11:cee25a834751 2470 v=0;
wolfSSL 11:cee25a834751 2471
wolfSSL 11:cee25a834751 2472 while ((secDesc->header>> 24) != 0xff) v++;
wolfSSL 11:cee25a834751 2473
wolfSSL 11:cee25a834751 2474 #ifdef DEBUG_WOLFSSL
wolfSSL 11:cee25a834751 2475 ret = MCF_SEC_SISRH;
wolfSSL 11:cee25a834751 2476 stat1 = MCF_SEC_AESSR;
wolfSSL 11:cee25a834751 2477 stat2 = MCF_SEC_AESISR;
wolfSSL 11:cee25a834751 2478 if (ret & 0xe0000000) {
wolfSSL 11:cee25a834751 2479 db_printf("Aes_Cbc(i=%d):ISRH=%08x, AESSR=%08x, "
wolfSSL 11:cee25a834751 2480 "AESISR=%08x\n", i, ret, stat1, stat2);
wolfSSL 11:cee25a834751 2481 }
wolfSSL 11:cee25a834751 2482 #endif
wolfSSL 11:cee25a834751 2483
wolfSSL 11:cee25a834751 2484 XMEMCPY(po, AESBuffOut, size);
wolfSSL 11:cee25a834751 2485
wolfSSL 11:cee25a834751 2486 if (descHeader == SEC_DESC_AES_CBC_ENCRYPT) {
wolfSSL 11:cee25a834751 2487 XMEMCPY((void*)aes->reg, (void*)&(po[size-AES_BLOCK_SIZE]),
wolfSSL 11:cee25a834751 2488 AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2489 } else {
wolfSSL 11:cee25a834751 2490 XMEMCPY((void*)aes->reg, (void*)aes->tmp, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2491 }
wolfSSL 11:cee25a834751 2492
wolfSSL 11:cee25a834751 2493 pi += size;
wolfSSL 11:cee25a834751 2494 po += size;
wolfSSL 11:cee25a834751 2495 }
wolfSSL 11:cee25a834751 2496
wolfSSL 11:cee25a834751 2497 wc_UnLockMutex(&Mutex_AesSEC);
wolfSSL 11:cee25a834751 2498 return 0;
wolfSSL 11:cee25a834751 2499 }
wolfSSL 11:cee25a834751 2500
wolfSSL 11:cee25a834751 2501 int wc_AesCbcEncrypt(Aes* aes, byte* po, const byte* pi, word32 sz)
wolfSSL 11:cee25a834751 2502 {
wolfSSL 11:cee25a834751 2503 return (wc_AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_ENCRYPT));
wolfSSL 11:cee25a834751 2504 }
wolfSSL 11:cee25a834751 2505
wolfSSL 11:cee25a834751 2506 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 2507 int wc_AesCbcDecrypt(Aes* aes, byte* po, const byte* pi, word32 sz)
wolfSSL 11:cee25a834751 2508 {
wolfSSL 11:cee25a834751 2509 return (wc_AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_DECRYPT));
wolfSSL 11:cee25a834751 2510 }
wolfSSL 11:cee25a834751 2511 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 2512
wolfSSL 11:cee25a834751 2513 #elif defined(FREESCALE_LTC)
wolfSSL 11:cee25a834751 2514 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2515 {
wolfSSL 11:cee25a834751 2516 uint32_t keySize;
wolfSSL 11:cee25a834751 2517 status_t status;
wolfSSL 11:cee25a834751 2518 byte *iv, *enc_key;
wolfSSL 11:cee25a834751 2519
wolfSSL 11:cee25a834751 2520 iv = (byte*)aes->reg;
wolfSSL 11:cee25a834751 2521 enc_key = (byte*)aes->key;
wolfSSL 11:cee25a834751 2522
wolfSSL 11:cee25a834751 2523 status = wc_AesGetKeySize(aes, &keySize);
wolfSSL 11:cee25a834751 2524 if (status != 0) {
wolfSSL 11:cee25a834751 2525 return status;
wolfSSL 11:cee25a834751 2526 }
wolfSSL 11:cee25a834751 2527
wolfSSL 11:cee25a834751 2528 status = LTC_AES_EncryptCbc(LTC_BASE, in, out, sz,
wolfSSL 11:cee25a834751 2529 iv, enc_key, keySize);
wolfSSL 11:cee25a834751 2530 return (status == kStatus_Success) ? 0 : -1;
wolfSSL 11:cee25a834751 2531 }
wolfSSL 11:cee25a834751 2532
wolfSSL 11:cee25a834751 2533 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 2534 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2535 {
wolfSSL 11:cee25a834751 2536 uint32_t keySize;
wolfSSL 11:cee25a834751 2537 status_t status;
wolfSSL 11:cee25a834751 2538 byte* iv, *dec_key;
wolfSSL 11:cee25a834751 2539
wolfSSL 11:cee25a834751 2540 iv = (byte*)aes->reg;
wolfSSL 11:cee25a834751 2541 dec_key = (byte*)aes->key;
wolfSSL 11:cee25a834751 2542
wolfSSL 11:cee25a834751 2543 status = wc_AesGetKeySize(aes, &keySize);
wolfSSL 11:cee25a834751 2544 if (status != 0) {
wolfSSL 11:cee25a834751 2545 return status;
wolfSSL 11:cee25a834751 2546 }
wolfSSL 11:cee25a834751 2547
wolfSSL 11:cee25a834751 2548 status = LTC_AES_DecryptCbc(LTC_BASE, in, out, sz,
wolfSSL 11:cee25a834751 2549 iv, dec_key, keySize, kLTC_EncryptKey);
wolfSSL 11:cee25a834751 2550 return (status == kStatus_Success) ? 0 : -1;
wolfSSL 11:cee25a834751 2551 }
wolfSSL 11:cee25a834751 2552 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 2553
wolfSSL 11:cee25a834751 2554 #elif defined(FREESCALE_MMCAU)
wolfSSL 11:cee25a834751 2555 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2556 {
wolfSSL 11:cee25a834751 2557 int i;
wolfSSL 11:cee25a834751 2558 int offset = 0;
wolfSSL 11:cee25a834751 2559 int len = sz;
wolfSSL 11:cee25a834751 2560
wolfSSL 11:cee25a834751 2561 byte *iv;
wolfSSL 11:cee25a834751 2562 byte temp_block[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 2563
wolfSSL 11:cee25a834751 2564 iv = (byte*)aes->reg;
wolfSSL 11:cee25a834751 2565
wolfSSL 11:cee25a834751 2566 while (len > 0)
wolfSSL 11:cee25a834751 2567 {
wolfSSL 11:cee25a834751 2568 XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2569
wolfSSL 11:cee25a834751 2570 /* XOR block with IV for CBC */
wolfSSL 11:cee25a834751 2571 for (i = 0; i < AES_BLOCK_SIZE; i++)
wolfSSL 11:cee25a834751 2572 temp_block[i] ^= iv[i];
wolfSSL 11:cee25a834751 2573
wolfSSL 11:cee25a834751 2574 wc_AesEncrypt(aes, temp_block, out + offset);
wolfSSL 11:cee25a834751 2575
wolfSSL 11:cee25a834751 2576 len -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2577 offset += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2578
wolfSSL 11:cee25a834751 2579 /* store IV for next block */
wolfSSL 11:cee25a834751 2580 XMEMCPY(iv, out + offset - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2581 }
wolfSSL 11:cee25a834751 2582
wolfSSL 11:cee25a834751 2583 return 0;
wolfSSL 11:cee25a834751 2584 }
wolfSSL 11:cee25a834751 2585 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 2586 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2587 {
wolfSSL 11:cee25a834751 2588 int i;
wolfSSL 11:cee25a834751 2589 int offset = 0;
wolfSSL 11:cee25a834751 2590 int len = sz;
wolfSSL 11:cee25a834751 2591
wolfSSL 11:cee25a834751 2592 byte* iv;
wolfSSL 11:cee25a834751 2593 byte temp_block[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 2594
wolfSSL 11:cee25a834751 2595 iv = (byte*)aes->reg;
wolfSSL 11:cee25a834751 2596
wolfSSL 11:cee25a834751 2597
wolfSSL 11:cee25a834751 2598 while (len > 0)
wolfSSL 11:cee25a834751 2599 {
wolfSSL 11:cee25a834751 2600 XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2601
wolfSSL 11:cee25a834751 2602 wc_AesDecrypt(aes, in + offset, out + offset);
wolfSSL 11:cee25a834751 2603
wolfSSL 11:cee25a834751 2604 /* XOR block with IV for CBC */
wolfSSL 11:cee25a834751 2605 for (i = 0; i < AES_BLOCK_SIZE; i++)
wolfSSL 11:cee25a834751 2606 (out + offset)[i] ^= iv[i];
wolfSSL 11:cee25a834751 2607
wolfSSL 11:cee25a834751 2608 /* store IV for next block */
wolfSSL 11:cee25a834751 2609 XMEMCPY(iv, temp_block, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2610
wolfSSL 11:cee25a834751 2611 len -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2612 offset += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2613 }
wolfSSL 11:cee25a834751 2614
wolfSSL 11:cee25a834751 2615 return 0;
wolfSSL 11:cee25a834751 2616 }
wolfSSL 11:cee25a834751 2617 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 2618
wolfSSL 11:cee25a834751 2619 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 11:cee25a834751 2620 /* core hardware crypt engine driver */
wolfSSL 11:cee25a834751 2621 static void wc_AesCrypt(Aes *aes, byte* out, const byte* in, word32 sz,
wolfSSL 11:cee25a834751 2622 int dir, int algo, int cryptoalgo)
wolfSSL 11:cee25a834751 2623 {
wolfSSL 11:cee25a834751 2624 securityAssociation *sa_p;
wolfSSL 11:cee25a834751 2625 bufferDescriptor *bd_p;
wolfSSL 11:cee25a834751 2626
wolfSSL 11:cee25a834751 2627 volatile securityAssociation sa __attribute__((aligned (8)));
wolfSSL 11:cee25a834751 2628 volatile bufferDescriptor bd __attribute__((aligned (8)));
wolfSSL 11:cee25a834751 2629 volatile int k;
wolfSSL 11:cee25a834751 2630
wolfSSL 11:cee25a834751 2631 /* get uncached address */
wolfSSL 11:cee25a834751 2632 sa_p = KVA0_TO_KVA1(&sa);
wolfSSL 11:cee25a834751 2633 bd_p = KVA0_TO_KVA1(&bd);
wolfSSL 11:cee25a834751 2634
wolfSSL 11:cee25a834751 2635 /* Sync cache and physical memory */
wolfSSL 11:cee25a834751 2636 if(PIC32MZ_IF_RAM(in)) {
wolfSSL 11:cee25a834751 2637 XMEMCPY((void *)KVA0_TO_KVA1(in), (void *)in, sz);
wolfSSL 11:cee25a834751 2638 }
wolfSSL 11:cee25a834751 2639 XMEMSET((void *)KVA0_TO_KVA1(out), 0, sz);
wolfSSL 11:cee25a834751 2640 /* Set up the Security Association */
wolfSSL 11:cee25a834751 2641 XMEMSET((byte *)KVA0_TO_KVA1(&sa), 0, sizeof(sa));
wolfSSL 11:cee25a834751 2642 sa_p->SA_CTRL.ALGO = algo; /* AES */
wolfSSL 11:cee25a834751 2643 sa_p->SA_CTRL.LNC = 1;
wolfSSL 11:cee25a834751 2644 sa_p->SA_CTRL.LOADIV = 1;
wolfSSL 11:cee25a834751 2645 sa_p->SA_CTRL.FB = 1;
wolfSSL 11:cee25a834751 2646 sa_p->SA_CTRL.ENCTYPE = dir; /* Encryption/Decryption */
wolfSSL 11:cee25a834751 2647 sa_p->SA_CTRL.CRYPTOALGO = cryptoalgo;
wolfSSL 11:cee25a834751 2648
wolfSSL 11:cee25a834751 2649 if(cryptoalgo == PIC32_CRYPTOALGO_AES_GCM){
wolfSSL 11:cee25a834751 2650 switch(aes->keylen) {
wolfSSL 11:cee25a834751 2651 case 32:
wolfSSL 11:cee25a834751 2652 sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_256;
wolfSSL 11:cee25a834751 2653 break;
wolfSSL 11:cee25a834751 2654 case 24:
wolfSSL 11:cee25a834751 2655 sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_192;
wolfSSL 11:cee25a834751 2656 break;
wolfSSL 11:cee25a834751 2657 case 16:
wolfSSL 11:cee25a834751 2658 sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_128;
wolfSSL 11:cee25a834751 2659 break;
wolfSSL 11:cee25a834751 2660 }
wolfSSL 11:cee25a834751 2661 } else
wolfSSL 11:cee25a834751 2662 sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_128;
wolfSSL 11:cee25a834751 2663
wolfSSL 11:cee25a834751 2664 ByteReverseWords(
wolfSSL 11:cee25a834751 2665 (word32 *)KVA0_TO_KVA1(sa.SA_ENCKEY + 8 - aes->keylen/sizeof(word32)),
wolfSSL 11:cee25a834751 2666 (word32 *)aes->key_ce, aes->keylen);
wolfSSL 11:cee25a834751 2667 ByteReverseWords(
wolfSSL 11:cee25a834751 2668 (word32*)KVA0_TO_KVA1(sa.SA_ENCIV), (word32 *)aes->iv_ce, 16);
wolfSSL 11:cee25a834751 2669
wolfSSL 11:cee25a834751 2670 XMEMSET((byte *)KVA0_TO_KVA1(&bd), 0, sizeof(bd));
wolfSSL 11:cee25a834751 2671 /* Set up the Buffer Descriptor */
wolfSSL 11:cee25a834751 2672 bd_p->BD_CTRL.BUFLEN = sz;
wolfSSL 11:cee25a834751 2673 if(cryptoalgo == PIC32_CRYPTOALGO_AES_GCM) {
wolfSSL 11:cee25a834751 2674 if(sz % 0x10)
wolfSSL 11:cee25a834751 2675 bd_p->BD_CTRL.BUFLEN = (sz/0x10 + 1) * 0x10;
wolfSSL 11:cee25a834751 2676 }
wolfSSL 11:cee25a834751 2677 bd_p->BD_CTRL.LIFM = 1;
wolfSSL 11:cee25a834751 2678 bd_p->BD_CTRL.SA_FETCH_EN = 1;
wolfSSL 11:cee25a834751 2679 bd_p->BD_CTRL.LAST_BD = 1;
wolfSSL 11:cee25a834751 2680 bd_p->BD_CTRL.DESC_EN = 1;
wolfSSL 11:cee25a834751 2681
wolfSSL 11:cee25a834751 2682 bd_p->SA_ADDR = (unsigned int)KVA_TO_PA(&sa);
wolfSSL 11:cee25a834751 2683 bd_p->SRCADDR = (unsigned int)KVA_TO_PA(in);
wolfSSL 11:cee25a834751 2684 bd_p->DSTADDR = (unsigned int)KVA_TO_PA(out);
wolfSSL 11:cee25a834751 2685 bd_p->MSGLEN = sz;
wolfSSL 11:cee25a834751 2686
wolfSSL 11:cee25a834751 2687 CECON = 1 << 6;
wolfSSL 11:cee25a834751 2688 while (CECON);
wolfSSL 11:cee25a834751 2689
wolfSSL 11:cee25a834751 2690 /* Run the engine */
wolfSSL 11:cee25a834751 2691 CEBDPADDR = (unsigned int)KVA_TO_PA(&bd);
wolfSSL 11:cee25a834751 2692 CEINTEN = 0x07;
wolfSSL 11:cee25a834751 2693 CECON = 0x27;
wolfSSL 11:cee25a834751 2694
wolfSSL 11:cee25a834751 2695 WAIT_ENGINE;
wolfSSL 11:cee25a834751 2696
wolfSSL 11:cee25a834751 2697 if((cryptoalgo == PIC32_CRYPTOALGO_CBC) ||
wolfSSL 11:cee25a834751 2698 (cryptoalgo == PIC32_CRYPTOALGO_TCBC)||
wolfSSL 11:cee25a834751 2699 (cryptoalgo == PIC32_CRYPTOALGO_RCBC)) {
wolfSSL 11:cee25a834751 2700 /* set iv for the next call */
wolfSSL 11:cee25a834751 2701 if(dir == PIC32_ENCRYPTION) {
wolfSSL 11:cee25a834751 2702 XMEMCPY((void *)aes->iv_ce,
wolfSSL 11:cee25a834751 2703 (void*)KVA0_TO_KVA1(out + sz - AES_BLOCK_SIZE),
wolfSSL 11:cee25a834751 2704 AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2705 } else {
wolfSSL 11:cee25a834751 2706 ByteReverseWords((word32*)aes->iv_ce,
wolfSSL 11:cee25a834751 2707 (word32 *)KVA0_TO_KVA1(in + sz - AES_BLOCK_SIZE),
wolfSSL 11:cee25a834751 2708 AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2709 }
wolfSSL 11:cee25a834751 2710 }
wolfSSL 11:cee25a834751 2711 XMEMCPY((byte *)out, (byte *)KVA0_TO_KVA1(out), sz);
wolfSSL 11:cee25a834751 2712 ByteReverseWords((word32*)out, (word32 *)out, sz);
wolfSSL 11:cee25a834751 2713 }
wolfSSL 11:cee25a834751 2714
wolfSSL 11:cee25a834751 2715 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2716 {
wolfSSL 11:cee25a834751 2717 wc_AesCrypt(aes, out, in, sz, PIC32_ENCRYPTION, PIC32_ALGO_AES,
wolfSSL 11:cee25a834751 2718 PIC32_CRYPTOALGO_RCBC );
wolfSSL 11:cee25a834751 2719 return 0;
wolfSSL 11:cee25a834751 2720 }
wolfSSL 11:cee25a834751 2721 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 2722 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2723 {
wolfSSL 11:cee25a834751 2724 wc_AesCrypt(aes, out, in, sz, PIC32_DECRYPTION, PIC32_ALGO_AES,
wolfSSL 11:cee25a834751 2725 PIC32_CRYPTOALGO_RCBC);
wolfSSL 11:cee25a834751 2726 return 0;
wolfSSL 11:cee25a834751 2727 }
wolfSSL 11:cee25a834751 2728 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 2729
wolfSSL 11:cee25a834751 2730 #else
wolfSSL 11:cee25a834751 2731 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2732 {
wolfSSL 11:cee25a834751 2733 word32 blocks = sz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2734
wolfSSL 11:cee25a834751 2735 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
wolfSSL 11:cee25a834751 2736 /* if async and byte count above threshold */
wolfSSL 11:cee25a834751 2737 if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
wolfSSL 11:cee25a834751 2738 sz >= WC_ASYNC_THRESH_AES_CBC) {
wolfSSL 11:cee25a834751 2739 #if defined(HAVE_CAVIUM)
wolfSSL 11:cee25a834751 2740 return NitroxAesCbcEncrypt(aes, out, in, sz);
wolfSSL 11:cee25a834751 2741 #elif defined(HAVE_INTEL_QA)
wolfSSL 11:cee25a834751 2742 return IntelQaSymAesCbcEncrypt(&aes->asyncDev, out, in, sz,
wolfSSL 11:cee25a834751 2743 aes->asyncKey, aes->keylen, aes->asyncIv, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2744 #else /* WOLFSSL_ASYNC_CRYPT_TEST */
wolfSSL 11:cee25a834751 2745 WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
wolfSSL 11:cee25a834751 2746 if (testDev->type == ASYNC_TEST_NONE) {
wolfSSL 11:cee25a834751 2747 testDev->type = ASYNC_TEST_AES_CBC_ENCRYPT;
wolfSSL 11:cee25a834751 2748 testDev->aes.aes = aes;
wolfSSL 11:cee25a834751 2749 testDev->aes.out = out;
wolfSSL 11:cee25a834751 2750 testDev->aes.in = in;
wolfSSL 11:cee25a834751 2751 testDev->aes.sz = sz;
wolfSSL 11:cee25a834751 2752 return WC_PENDING_E;
wolfSSL 11:cee25a834751 2753 }
wolfSSL 11:cee25a834751 2754 #endif
wolfSSL 11:cee25a834751 2755 }
wolfSSL 11:cee25a834751 2756 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 2757
wolfSSL 11:cee25a834751 2758 #ifdef WOLFSSL_AESNI
wolfSSL 11:cee25a834751 2759 if (haveAESNI) {
wolfSSL 11:cee25a834751 2760 #ifdef DEBUG_AESNI
wolfSSL 11:cee25a834751 2761 printf("about to aes cbc encrypt\n");
wolfSSL 11:cee25a834751 2762 printf("in = %p\n", in);
wolfSSL 11:cee25a834751 2763 printf("out = %p\n", out);
wolfSSL 11:cee25a834751 2764 printf("aes->key = %p\n", aes->key);
wolfSSL 11:cee25a834751 2765 printf("aes->reg = %p\n", aes->reg);
wolfSSL 11:cee25a834751 2766 printf("aes->rounds = %d\n", aes->rounds);
wolfSSL 11:cee25a834751 2767 printf("sz = %d\n", sz);
wolfSSL 11:cee25a834751 2768 #endif
wolfSSL 11:cee25a834751 2769
wolfSSL 11:cee25a834751 2770 /* check alignment, decrypt doesn't need alignment */
wolfSSL 11:cee25a834751 2771 if ((wolfssl_word)in % AESNI_ALIGN) {
wolfSSL 11:cee25a834751 2772 #ifndef NO_WOLFSSL_ALLOC_ALIGN
wolfSSL 11:cee25a834751 2773 byte* tmp = (byte*)XMALLOC(sz + AESNI_ALIGN, aes->heap,
wolfSSL 11:cee25a834751 2774 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 2775 byte* tmp_align;
wolfSSL 11:cee25a834751 2776 if (tmp == NULL) return MEMORY_E;
wolfSSL 11:cee25a834751 2777
wolfSSL 11:cee25a834751 2778 tmp_align = tmp + (AESNI_ALIGN - ((size_t)tmp % AESNI_ALIGN));
wolfSSL 11:cee25a834751 2779 XMEMCPY(tmp_align, in, sz);
wolfSSL 11:cee25a834751 2780 AES_CBC_encrypt(tmp_align, tmp_align, (byte*)aes->reg, sz, (byte*)aes->key,
wolfSSL 11:cee25a834751 2781 aes->rounds);
wolfSSL 11:cee25a834751 2782 /* store iv for next call */
wolfSSL 11:cee25a834751 2783 XMEMCPY(aes->reg, tmp_align + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2784
wolfSSL 11:cee25a834751 2785 XMEMCPY(out, tmp_align, sz);
wolfSSL 11:cee25a834751 2786 XFREE(tmp, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 2787 return 0;
wolfSSL 11:cee25a834751 2788 #else
wolfSSL 11:cee25a834751 2789 WOLFSSL_MSG("AES-CBC encrypt with bad alignment");
wolfSSL 11:cee25a834751 2790 return BAD_ALIGN_E;
wolfSSL 11:cee25a834751 2791 #endif
wolfSSL 11:cee25a834751 2792 }
wolfSSL 11:cee25a834751 2793
wolfSSL 11:cee25a834751 2794 AES_CBC_encrypt(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
wolfSSL 11:cee25a834751 2795 aes->rounds);
wolfSSL 11:cee25a834751 2796 /* store iv for next call */
wolfSSL 11:cee25a834751 2797 XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2798
wolfSSL 11:cee25a834751 2799 return 0;
wolfSSL 11:cee25a834751 2800 }
wolfSSL 11:cee25a834751 2801 #endif
wolfSSL 11:cee25a834751 2802
wolfSSL 11:cee25a834751 2803 while (blocks--) {
wolfSSL 11:cee25a834751 2804 xorbuf((byte*)aes->reg, in, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2805 wc_AesEncrypt(aes, (byte*)aes->reg, (byte*)aes->reg);
wolfSSL 11:cee25a834751 2806 XMEMCPY(out, aes->reg, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2807
wolfSSL 11:cee25a834751 2808 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2809 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2810 }
wolfSSL 11:cee25a834751 2811
wolfSSL 11:cee25a834751 2812 return 0;
wolfSSL 11:cee25a834751 2813 }
wolfSSL 11:cee25a834751 2814
wolfSSL 11:cee25a834751 2815 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 2816 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2817 {
wolfSSL 11:cee25a834751 2818 word32 blocks;
wolfSSL 11:cee25a834751 2819
wolfSSL 11:cee25a834751 2820 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
wolfSSL 11:cee25a834751 2821 /* if async and byte count above threshold */
wolfSSL 11:cee25a834751 2822 if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
wolfSSL 11:cee25a834751 2823 sz >= WC_ASYNC_THRESH_AES_CBC) {
wolfSSL 11:cee25a834751 2824 #if defined(HAVE_CAVIUM)
wolfSSL 11:cee25a834751 2825 return NitroxAesCbcDecrypt(aes, out, in, sz);
wolfSSL 11:cee25a834751 2826 #elif defined(HAVE_INTEL_QA)
wolfSSL 11:cee25a834751 2827 return IntelQaSymAesCbcDecrypt(&aes->asyncDev, out, in, sz,
wolfSSL 11:cee25a834751 2828 aes->asyncKey, aes->keylen, aes->asyncIv, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2829 #else /* WOLFSSL_ASYNC_CRYPT_TEST */
wolfSSL 11:cee25a834751 2830 WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
wolfSSL 11:cee25a834751 2831 if (testDev->type == ASYNC_TEST_NONE) {
wolfSSL 11:cee25a834751 2832 testDev->type = ASYNC_TEST_AES_CBC_DECRYPT;
wolfSSL 11:cee25a834751 2833 testDev->aes.aes = aes;
wolfSSL 11:cee25a834751 2834 testDev->aes.out = out;
wolfSSL 11:cee25a834751 2835 testDev->aes.in = in;
wolfSSL 11:cee25a834751 2836 testDev->aes.sz = sz;
wolfSSL 11:cee25a834751 2837 return WC_PENDING_E;
wolfSSL 11:cee25a834751 2838 }
wolfSSL 11:cee25a834751 2839 #endif
wolfSSL 11:cee25a834751 2840 }
wolfSSL 11:cee25a834751 2841 #endif
wolfSSL 11:cee25a834751 2842
wolfSSL 11:cee25a834751 2843 #ifdef WOLFSSL_AESNI
wolfSSL 11:cee25a834751 2844 if (haveAESNI) {
wolfSSL 11:cee25a834751 2845 #ifdef DEBUG_AESNI
wolfSSL 11:cee25a834751 2846 printf("about to aes cbc decrypt\n");
wolfSSL 11:cee25a834751 2847 printf("in = %p\n", in);
wolfSSL 11:cee25a834751 2848 printf("out = %p\n", out);
wolfSSL 11:cee25a834751 2849 printf("aes->key = %p\n", aes->key);
wolfSSL 11:cee25a834751 2850 printf("aes->reg = %p\n", aes->reg);
wolfSSL 11:cee25a834751 2851 printf("aes->rounds = %d\n", aes->rounds);
wolfSSL 11:cee25a834751 2852 printf("sz = %d\n", sz);
wolfSSL 11:cee25a834751 2853 #endif
wolfSSL 11:cee25a834751 2854
wolfSSL 11:cee25a834751 2855 /* if input and output same will overwrite input iv */
wolfSSL 11:cee25a834751 2856 XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2857 #if defined(WOLFSSL_AESNI_BY4)
wolfSSL 11:cee25a834751 2858 AES_CBC_decrypt_by4(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
wolfSSL 11:cee25a834751 2859 aes->rounds);
wolfSSL 11:cee25a834751 2860 #elif defined(WOLFSSL_AESNI_BY6)
wolfSSL 11:cee25a834751 2861 AES_CBC_decrypt_by6(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
wolfSSL 11:cee25a834751 2862 aes->rounds);
wolfSSL 11:cee25a834751 2863 #else /* WOLFSSL_AESNI_BYx */
wolfSSL 11:cee25a834751 2864 AES_CBC_decrypt_by8(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
wolfSSL 11:cee25a834751 2865 aes->rounds);
wolfSSL 11:cee25a834751 2866 #endif /* WOLFSSL_AESNI_BYx */
wolfSSL 11:cee25a834751 2867 /* store iv for next call */
wolfSSL 11:cee25a834751 2868 XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2869 return 0;
wolfSSL 11:cee25a834751 2870 }
wolfSSL 11:cee25a834751 2871 #endif
wolfSSL 11:cee25a834751 2872
wolfSSL 11:cee25a834751 2873 blocks = sz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2874 while (blocks--) {
wolfSSL 11:cee25a834751 2875 XMEMCPY(aes->tmp, in, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2876 wc_AesDecrypt(aes, (byte*)aes->tmp, out);
wolfSSL 11:cee25a834751 2877 xorbuf(out, (byte*)aes->reg, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2878 XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 2879
wolfSSL 11:cee25a834751 2880 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2881 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2882 }
wolfSSL 11:cee25a834751 2883
wolfSSL 11:cee25a834751 2884 return 0;
wolfSSL 11:cee25a834751 2885 }
wolfSSL 11:cee25a834751 2886 #endif
wolfSSL 11:cee25a834751 2887
wolfSSL 11:cee25a834751 2888 #endif /* AES-CBC block */
wolfSSL 11:cee25a834751 2889 #endif /* HAVE_AES_CBC */
wolfSSL 11:cee25a834751 2890
wolfSSL 11:cee25a834751 2891 #ifdef HAVE_AES_ECB
wolfSSL 11:cee25a834751 2892 int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2893 {
wolfSSL 11:cee25a834751 2894 if ((in == NULL) || (out == NULL) || (aes == NULL))
wolfSSL 11:cee25a834751 2895 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 2896 while (sz>0) {
wolfSSL 11:cee25a834751 2897 wc_AesEncryptDirect(aes, out, in);
wolfSSL 11:cee25a834751 2898 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2899 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2900 sz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2901 }
wolfSSL 11:cee25a834751 2902 return 0;
wolfSSL 11:cee25a834751 2903 }
wolfSSL 11:cee25a834751 2904 int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2905 {
wolfSSL 11:cee25a834751 2906 if ((in == NULL) || (out == NULL) || (aes == NULL))
wolfSSL 11:cee25a834751 2907 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 2908 while (sz>0) {
wolfSSL 11:cee25a834751 2909 wc_AesDecryptDirect(aes, out, in);
wolfSSL 11:cee25a834751 2910 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2911 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2912 sz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 2913 }
wolfSSL 11:cee25a834751 2914 return 0;
wolfSSL 11:cee25a834751 2915 }
wolfSSL 11:cee25a834751 2916 #endif
wolfSSL 11:cee25a834751 2917
wolfSSL 11:cee25a834751 2918 /* AES-CTR */
wolfSSL 11:cee25a834751 2919 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 11:cee25a834751 2920
wolfSSL 11:cee25a834751 2921 #if defined(STM32F2_CRYPTO) || defined(STM32F4_CRYPTO)
wolfSSL 11:cee25a834751 2922 #ifdef WOLFSSL_STM32_CUBEMX
wolfSSL 11:cee25a834751 2923 void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2924 {
wolfSSL 11:cee25a834751 2925 CRYP_HandleTypeDef hcryp;
wolfSSL 11:cee25a834751 2926 XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef));
wolfSSL 11:cee25a834751 2927 /* load key into correct registers */
wolfSSL 11:cee25a834751 2928 switch(aes->rounds) {
wolfSSL 11:cee25a834751 2929 case 10: /* 128-bit key */
wolfSSL 11:cee25a834751 2930 hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
wolfSSL 11:cee25a834751 2931 break;
wolfSSL 11:cee25a834751 2932 case 12: /* 192-bit key */
wolfSSL 11:cee25a834751 2933 hcryp.Init.KeySize = CRYP_KEYSIZE_192B;
wolfSSL 11:cee25a834751 2934 break;
wolfSSL 11:cee25a834751 2935 case 14: /* 256-bit key */
wolfSSL 11:cee25a834751 2936 hcryp.Init.KeySize = CRYP_KEYSIZE_256B;
wolfSSL 11:cee25a834751 2937 break;
wolfSSL 11:cee25a834751 2938 default:
wolfSSL 11:cee25a834751 2939 break;
wolfSSL 11:cee25a834751 2940 }
wolfSSL 11:cee25a834751 2941 hcryp.Instance = CRYP;
wolfSSL 11:cee25a834751 2942 hcryp.Init.DataType = CRYP_DATATYPE_8B;
wolfSSL 11:cee25a834751 2943 hcryp.Init.pKey = aes->key;
wolfSSL 11:cee25a834751 2944 hcryp.Init.pInitVect = aes->reg;
wolfSSL 11:cee25a834751 2945
wolfSSL 11:cee25a834751 2946 HAL_CRYP_Init(&hcryp);
wolfSSL 11:cee25a834751 2947
wolfSSL 11:cee25a834751 2948 HAL_CRYP_AESCTR_Encrypt(&hcryp, in, AES_BLOCK_SIZE, out,
wolfSSL 11:cee25a834751 2949 STM32_HAL_TIMEOUT);
wolfSSL 11:cee25a834751 2950
wolfSSL 11:cee25a834751 2951 HAL_CRYP_DeInit(&hcryp);
wolfSSL 11:cee25a834751 2952 }
wolfSSL 11:cee25a834751 2953 #else
wolfSSL 11:cee25a834751 2954 void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 2955 {
wolfSSL 11:cee25a834751 2956 word32 *enc_key, *iv;
wolfSSL 11:cee25a834751 2957 CRYP_InitTypeDef AES_CRYP_InitStructure;
wolfSSL 11:cee25a834751 2958 CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
wolfSSL 11:cee25a834751 2959 CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
wolfSSL 11:cee25a834751 2960
wolfSSL 11:cee25a834751 2961 enc_key = aes->key;
wolfSSL 11:cee25a834751 2962 iv = aes->reg;
wolfSSL 11:cee25a834751 2963
wolfSSL 11:cee25a834751 2964 /* crypto structure initialization */
wolfSSL 11:cee25a834751 2965 CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
wolfSSL 11:cee25a834751 2966 CRYP_StructInit(&AES_CRYP_InitStructure);
wolfSSL 11:cee25a834751 2967 CRYP_IVStructInit(&AES_CRYP_IVInitStructure);
wolfSSL 11:cee25a834751 2968
wolfSSL 11:cee25a834751 2969 /* reset registers to their default values */
wolfSSL 11:cee25a834751 2970 CRYP_DeInit();
wolfSSL 11:cee25a834751 2971
wolfSSL 11:cee25a834751 2972 /* load key into correct registers */
wolfSSL 11:cee25a834751 2973 switch(aes->rounds)
wolfSSL 11:cee25a834751 2974 {
wolfSSL 11:cee25a834751 2975 case 10: /* 128-bit key */
wolfSSL 11:cee25a834751 2976 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
wolfSSL 11:cee25a834751 2977 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[0];
wolfSSL 11:cee25a834751 2978 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[1];
wolfSSL 11:cee25a834751 2979 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[2];
wolfSSL 11:cee25a834751 2980 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[3];
wolfSSL 11:cee25a834751 2981 break;
wolfSSL 11:cee25a834751 2982
wolfSSL 11:cee25a834751 2983 case 12: /* 192-bit key */
wolfSSL 11:cee25a834751 2984 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
wolfSSL 11:cee25a834751 2985 AES_CRYP_KeyInitStructure.CRYP_Key1Left = enc_key[0];
wolfSSL 11:cee25a834751 2986 AES_CRYP_KeyInitStructure.CRYP_Key1Right = enc_key[1];
wolfSSL 11:cee25a834751 2987 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[2];
wolfSSL 11:cee25a834751 2988 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[3];
wolfSSL 11:cee25a834751 2989 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[4];
wolfSSL 11:cee25a834751 2990 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[5];
wolfSSL 11:cee25a834751 2991 break;
wolfSSL 11:cee25a834751 2992
wolfSSL 11:cee25a834751 2993 case 14: /* 256-bit key */
wolfSSL 11:cee25a834751 2994 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
wolfSSL 11:cee25a834751 2995 AES_CRYP_KeyInitStructure.CRYP_Key0Left = enc_key[0];
wolfSSL 11:cee25a834751 2996 AES_CRYP_KeyInitStructure.CRYP_Key0Right = enc_key[1];
wolfSSL 11:cee25a834751 2997 AES_CRYP_KeyInitStructure.CRYP_Key1Left = enc_key[2];
wolfSSL 11:cee25a834751 2998 AES_CRYP_KeyInitStructure.CRYP_Key1Right = enc_key[3];
wolfSSL 11:cee25a834751 2999 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[4];
wolfSSL 11:cee25a834751 3000 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[5];
wolfSSL 11:cee25a834751 3001 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[6];
wolfSSL 11:cee25a834751 3002 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[7];
wolfSSL 11:cee25a834751 3003 break;
wolfSSL 11:cee25a834751 3004
wolfSSL 11:cee25a834751 3005 default:
wolfSSL 11:cee25a834751 3006 break;
wolfSSL 11:cee25a834751 3007 }
wolfSSL 11:cee25a834751 3008 CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
wolfSSL 11:cee25a834751 3009
wolfSSL 11:cee25a834751 3010 /* set iv */
wolfSSL 11:cee25a834751 3011 ByteReverseWords(iv, iv, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3012 AES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
wolfSSL 11:cee25a834751 3013 AES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
wolfSSL 11:cee25a834751 3014 AES_CRYP_IVInitStructure.CRYP_IV1Left = iv[2];
wolfSSL 11:cee25a834751 3015 AES_CRYP_IVInitStructure.CRYP_IV1Right = iv[3];
wolfSSL 11:cee25a834751 3016 CRYP_IVInit(&AES_CRYP_IVInitStructure);
wolfSSL 11:cee25a834751 3017
wolfSSL 11:cee25a834751 3018 /* set direction, mode, and datatype */
wolfSSL 11:cee25a834751 3019 AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
wolfSSL 11:cee25a834751 3020 AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CTR;
wolfSSL 11:cee25a834751 3021 AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 11:cee25a834751 3022 CRYP_Init(&AES_CRYP_InitStructure);
wolfSSL 11:cee25a834751 3023
wolfSSL 11:cee25a834751 3024 /* enable crypto processor */
wolfSSL 11:cee25a834751 3025 CRYP_Cmd(ENABLE);
wolfSSL 11:cee25a834751 3026
wolfSSL 11:cee25a834751 3027 while (sz > 0)
wolfSSL 11:cee25a834751 3028 {
wolfSSL 11:cee25a834751 3029 /* flush IN/OUT FIFOs */
wolfSSL 11:cee25a834751 3030 CRYP_FIFOFlush();
wolfSSL 11:cee25a834751 3031
wolfSSL 11:cee25a834751 3032 CRYP_DataIn(*(uint32_t*)&in[0]);
wolfSSL 11:cee25a834751 3033 CRYP_DataIn(*(uint32_t*)&in[4]);
wolfSSL 11:cee25a834751 3034 CRYP_DataIn(*(uint32_t*)&in[8]);
wolfSSL 11:cee25a834751 3035 CRYP_DataIn(*(uint32_t*)&in[12]);
wolfSSL 11:cee25a834751 3036
wolfSSL 11:cee25a834751 3037 /* wait until the complete message has been processed */
wolfSSL 11:cee25a834751 3038 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 11:cee25a834751 3039
wolfSSL 11:cee25a834751 3040 *(uint32_t*)&out[0] = CRYP_DataOut();
wolfSSL 11:cee25a834751 3041 *(uint32_t*)&out[4] = CRYP_DataOut();
wolfSSL 11:cee25a834751 3042 *(uint32_t*)&out[8] = CRYP_DataOut();
wolfSSL 11:cee25a834751 3043 *(uint32_t*)&out[12] = CRYP_DataOut();
wolfSSL 11:cee25a834751 3044
wolfSSL 11:cee25a834751 3045 /* store iv for next call */
wolfSSL 11:cee25a834751 3046 XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3047
wolfSSL 11:cee25a834751 3048 sz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3049 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3050 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3051 }
wolfSSL 11:cee25a834751 3052
wolfSSL 11:cee25a834751 3053 /* disable crypto processor */
wolfSSL 11:cee25a834751 3054 CRYP_Cmd(DISABLE);
wolfSSL 11:cee25a834751 3055 }
wolfSSL 11:cee25a834751 3056 #endif /* WOLFSSL_STM32_CUBEMX */
wolfSSL 11:cee25a834751 3057
wolfSSL 11:cee25a834751 3058 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 11:cee25a834751 3059 void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 3060 {
wolfSSL 11:cee25a834751 3061 int i;
wolfSSL 11:cee25a834751 3062 char out_block[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 3063 int odd;
wolfSSL 11:cee25a834751 3064 int even;
wolfSSL 11:cee25a834751 3065 char *tmp; /* (char *)aes->tmp, for short */
wolfSSL 11:cee25a834751 3066
wolfSSL 11:cee25a834751 3067 tmp = (char *)aes->tmp;
wolfSSL 11:cee25a834751 3068 if(aes->left) {
wolfSSL 11:cee25a834751 3069 if((aes->left + sz) >= AES_BLOCK_SIZE){
wolfSSL 11:cee25a834751 3070 odd = AES_BLOCK_SIZE - aes->left;
wolfSSL 11:cee25a834751 3071 } else {
wolfSSL 11:cee25a834751 3072 odd = sz;
wolfSSL 11:cee25a834751 3073 }
wolfSSL 11:cee25a834751 3074 XMEMCPY(tmp+aes->left, in, odd);
wolfSSL 11:cee25a834751 3075 if((odd+aes->left) == AES_BLOCK_SIZE){
wolfSSL 11:cee25a834751 3076 wc_AesCrypt(aes, out_block, tmp, AES_BLOCK_SIZE,
wolfSSL 11:cee25a834751 3077 PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCTR);
wolfSSL 11:cee25a834751 3078 XMEMCPY(out, out_block+aes->left, odd);
wolfSSL 11:cee25a834751 3079 aes->left = 0;
wolfSSL 11:cee25a834751 3080 XMEMSET(tmp, 0x0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3081 /* Increment IV */
wolfSSL 11:cee25a834751 3082 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
wolfSSL 11:cee25a834751 3083 if (++((byte *)aes->iv_ce)[i])
wolfSSL 11:cee25a834751 3084 break;
wolfSSL 11:cee25a834751 3085 }
wolfSSL 11:cee25a834751 3086 }
wolfSSL 11:cee25a834751 3087 in += odd;
wolfSSL 11:cee25a834751 3088 out+= odd;
wolfSSL 11:cee25a834751 3089 sz -= odd;
wolfSSL 11:cee25a834751 3090 }
wolfSSL 11:cee25a834751 3091 odd = sz % AES_BLOCK_SIZE; /* if there is tail fragment */
wolfSSL 11:cee25a834751 3092 if(sz / AES_BLOCK_SIZE) {
wolfSSL 11:cee25a834751 3093 even = (sz/AES_BLOCK_SIZE)*AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3094 wc_AesCrypt(aes, out, in, even, PIC32_ENCRYPTION, PIC32_ALGO_AES,
wolfSSL 11:cee25a834751 3095 PIC32_CRYPTOALGO_RCTR);
wolfSSL 11:cee25a834751 3096 out += even;
wolfSSL 11:cee25a834751 3097 in += even;
wolfSSL 11:cee25a834751 3098 do { /* Increment IV */
wolfSSL 11:cee25a834751 3099 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
wolfSSL 11:cee25a834751 3100 if (++((byte *)aes->iv_ce)[i])
wolfSSL 11:cee25a834751 3101 break;
wolfSSL 11:cee25a834751 3102 }
wolfSSL 11:cee25a834751 3103 even -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3104 } while((int)even > 0);
wolfSSL 11:cee25a834751 3105 }
wolfSSL 11:cee25a834751 3106 if(odd) {
wolfSSL 11:cee25a834751 3107 XMEMSET(tmp+aes->left, 0x0, AES_BLOCK_SIZE - aes->left);
wolfSSL 11:cee25a834751 3108 XMEMCPY(tmp+aes->left, in, odd);
wolfSSL 11:cee25a834751 3109 wc_AesCrypt(aes, out_block, tmp, AES_BLOCK_SIZE,
wolfSSL 11:cee25a834751 3110 PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCTR);
wolfSSL 11:cee25a834751 3111 XMEMCPY(out, out_block+aes->left,odd);
wolfSSL 11:cee25a834751 3112 aes->left += odd;
wolfSSL 11:cee25a834751 3113 }
wolfSSL 11:cee25a834751 3114 }
wolfSSL 11:cee25a834751 3115
wolfSSL 11:cee25a834751 3116 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 11:cee25a834751 3117 #error "Coldfire SEC doesn't currently support AES-CTR mode"
wolfSSL 11:cee25a834751 3118
wolfSSL 11:cee25a834751 3119 #elif defined(FREESCALE_LTC)
wolfSSL 11:cee25a834751 3120 void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 3121 {
wolfSSL 11:cee25a834751 3122 uint32_t keySize;
wolfSSL 11:cee25a834751 3123 byte *iv, *enc_key;
wolfSSL 11:cee25a834751 3124 byte* tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
wolfSSL 11:cee25a834751 3125
wolfSSL 11:cee25a834751 3126 /* consume any unused bytes left in aes->tmp */
wolfSSL 11:cee25a834751 3127 while (aes->left && sz) {
wolfSSL 11:cee25a834751 3128 *(out++) = *(in++) ^ *(tmp++);
wolfSSL 11:cee25a834751 3129 aes->left--;
wolfSSL 11:cee25a834751 3130 sz--;
wolfSSL 11:cee25a834751 3131 }
wolfSSL 11:cee25a834751 3132
wolfSSL 11:cee25a834751 3133 if (sz) {
wolfSSL 11:cee25a834751 3134 iv = (byte*)aes->reg;
wolfSSL 11:cee25a834751 3135 enc_key = (byte*)aes->key;
wolfSSL 11:cee25a834751 3136
wolfSSL 11:cee25a834751 3137 wc_AesGetKeySize(aes, &keySize);
wolfSSL 11:cee25a834751 3138
wolfSSL 11:cee25a834751 3139 LTC_AES_CryptCtr(LTC_BASE, in, out, sz,
wolfSSL 11:cee25a834751 3140 iv, enc_key, keySize, (byte*)aes->tmp,
wolfSSL 11:cee25a834751 3141 (uint32_t*)&(aes->left));
wolfSSL 11:cee25a834751 3142 }
wolfSSL 11:cee25a834751 3143 }
wolfSSL 11:cee25a834751 3144
wolfSSL 11:cee25a834751 3145 #else
wolfSSL 11:cee25a834751 3146 /* Increment AES counter */
wolfSSL 11:cee25a834751 3147 static INLINE void IncrementAesCounter(byte* inOutCtr)
wolfSSL 11:cee25a834751 3148 {
wolfSSL 11:cee25a834751 3149 int i;
wolfSSL 11:cee25a834751 3150
wolfSSL 11:cee25a834751 3151 /* in network byte order so start at end and work back */
wolfSSL 11:cee25a834751 3152 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
wolfSSL 11:cee25a834751 3153 if (++inOutCtr[i]) /* we're done unless we overflow */
wolfSSL 11:cee25a834751 3154 return;
wolfSSL 11:cee25a834751 3155 }
wolfSSL 11:cee25a834751 3156 }
wolfSSL 11:cee25a834751 3157
wolfSSL 11:cee25a834751 3158 void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 11:cee25a834751 3159 {
wolfSSL 11:cee25a834751 3160 byte* tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
wolfSSL 11:cee25a834751 3161
wolfSSL 11:cee25a834751 3162 /* consume any unused bytes left in aes->tmp */
wolfSSL 11:cee25a834751 3163 while (aes->left && sz) {
wolfSSL 11:cee25a834751 3164 *(out++) = *(in++) ^ *(tmp++);
wolfSSL 11:cee25a834751 3165 aes->left--;
wolfSSL 11:cee25a834751 3166 sz--;
wolfSSL 11:cee25a834751 3167 }
wolfSSL 11:cee25a834751 3168
wolfSSL 11:cee25a834751 3169 /* do as many block size ops as possible */
wolfSSL 11:cee25a834751 3170 while (sz >= AES_BLOCK_SIZE) {
wolfSSL 11:cee25a834751 3171 wc_AesEncrypt(aes, (byte*)aes->reg, out);
wolfSSL 11:cee25a834751 3172 IncrementAesCounter((byte*)aes->reg);
wolfSSL 11:cee25a834751 3173 xorbuf(out, in, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3174
wolfSSL 11:cee25a834751 3175 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3176 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3177 sz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3178 aes->left = 0;
wolfSSL 11:cee25a834751 3179 }
wolfSSL 11:cee25a834751 3180
wolfSSL 11:cee25a834751 3181 /* handle non block size remaining and store unused byte count in left */
wolfSSL 11:cee25a834751 3182 if (sz) {
wolfSSL 11:cee25a834751 3183 wc_AesEncrypt(aes, (byte*)aes->reg, (byte*)aes->tmp);
wolfSSL 11:cee25a834751 3184 IncrementAesCounter((byte*)aes->reg);
wolfSSL 11:cee25a834751 3185
wolfSSL 11:cee25a834751 3186 aes->left = AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3187 tmp = (byte*)aes->tmp;
wolfSSL 11:cee25a834751 3188
wolfSSL 11:cee25a834751 3189 while (sz--) {
wolfSSL 11:cee25a834751 3190 *(out++) = *(in++) ^ *(tmp++);
wolfSSL 11:cee25a834751 3191 aes->left--;
wolfSSL 11:cee25a834751 3192 }
wolfSSL 11:cee25a834751 3193 }
wolfSSL 11:cee25a834751 3194 }
wolfSSL 11:cee25a834751 3195
wolfSSL 11:cee25a834751 3196 #endif /* AES-CTR block */
wolfSSL 11:cee25a834751 3197
wolfSSL 11:cee25a834751 3198 #endif /* WOLFSSL_AES_COUNTER */
wolfSSL 11:cee25a834751 3199
wolfSSL 11:cee25a834751 3200
wolfSSL 11:cee25a834751 3201 #ifdef HAVE_AESGCM
wolfSSL 11:cee25a834751 3202
wolfSSL 11:cee25a834751 3203 /*
wolfSSL 11:cee25a834751 3204 * The IV for AES GCM, stored in struct Aes's member reg, is comprised of
wolfSSL 11:cee25a834751 3205 * three parts in order:
wolfSSL 11:cee25a834751 3206 * 1. The implicit IV. This is generated from the PRF using the shared
wolfSSL 11:cee25a834751 3207 * secrets between endpoints. It is 4 bytes long.
wolfSSL 11:cee25a834751 3208 * 2. The explicit IV. This is set by the user of the AES. It needs to be
wolfSSL 11:cee25a834751 3209 * unique for each call to encrypt. The explicit IV is shared with the
wolfSSL 11:cee25a834751 3210 * other end of the transaction in the clear.
wolfSSL 11:cee25a834751 3211 * 3. The counter. Each block of data is encrypted with its own sequence
wolfSSL 11:cee25a834751 3212 * number counter.
wolfSSL 11:cee25a834751 3213 */
wolfSSL 11:cee25a834751 3214
wolfSSL 11:cee25a834751 3215 #if defined(HAVE_COLDFIRE_SEC)
wolfSSL 11:cee25a834751 3216 #error "Coldfire SEC doesn't currently support AES-GCM mode"
wolfSSL 11:cee25a834751 3217
wolfSSL 11:cee25a834751 3218 #elif defined(WOLFSSL_NRF51_AES)
wolfSSL 11:cee25a834751 3219 #error "nRF51 doesn't currently support AES-GCM mode"
wolfSSL 11:cee25a834751 3220
wolfSSL 11:cee25a834751 3221 #endif
wolfSSL 11:cee25a834751 3222
wolfSSL 11:cee25a834751 3223 enum {
wolfSSL 11:cee25a834751 3224 NONCE_SZ = 12,
wolfSSL 11:cee25a834751 3225 CTR_SZ = 4
wolfSSL 11:cee25a834751 3226 };
wolfSSL 11:cee25a834751 3227
wolfSSL 11:cee25a834751 3228 #if !defined(FREESCALE_LTC_AES_GCM)
wolfSSL 11:cee25a834751 3229 static INLINE void IncrementGcmCounter(byte* inOutCtr)
wolfSSL 11:cee25a834751 3230 {
wolfSSL 11:cee25a834751 3231 int i;
wolfSSL 11:cee25a834751 3232
wolfSSL 11:cee25a834751 3233 /* in network byte order so start at end and work back */
wolfSSL 11:cee25a834751 3234 for (i = AES_BLOCK_SIZE - 1; i >= AES_BLOCK_SIZE - CTR_SZ; i--) {
wolfSSL 11:cee25a834751 3235 if (++inOutCtr[i]) /* we're done unless we overflow */
wolfSSL 11:cee25a834751 3236 return;
wolfSSL 11:cee25a834751 3237 }
wolfSSL 11:cee25a834751 3238 }
wolfSSL 11:cee25a834751 3239 #endif /* !FREESCALE_LTC_AES_GCM */
wolfSSL 11:cee25a834751 3240
wolfSSL 11:cee25a834751 3241 #if defined(GCM_SMALL) || defined(GCM_TABLE)
wolfSSL 11:cee25a834751 3242
wolfSSL 11:cee25a834751 3243 static INLINE void FlattenSzInBits(byte* buf, word32 sz)
wolfSSL 11:cee25a834751 3244 {
wolfSSL 11:cee25a834751 3245 /* Multiply the sz by 8 */
wolfSSL 11:cee25a834751 3246 word32 szHi = (sz >> (8*sizeof(sz) - 3));
wolfSSL 11:cee25a834751 3247 sz <<= 3;
wolfSSL 11:cee25a834751 3248
wolfSSL 11:cee25a834751 3249 /* copy over the words of the sz into the destination buffer */
wolfSSL 11:cee25a834751 3250 buf[0] = (szHi >> 24) & 0xff;
wolfSSL 11:cee25a834751 3251 buf[1] = (szHi >> 16) & 0xff;
wolfSSL 11:cee25a834751 3252 buf[2] = (szHi >> 8) & 0xff;
wolfSSL 11:cee25a834751 3253 buf[3] = szHi & 0xff;
wolfSSL 11:cee25a834751 3254 buf[4] = (sz >> 24) & 0xff;
wolfSSL 11:cee25a834751 3255 buf[5] = (sz >> 16) & 0xff;
wolfSSL 11:cee25a834751 3256 buf[6] = (sz >> 8) & 0xff;
wolfSSL 11:cee25a834751 3257 buf[7] = sz & 0xff;
wolfSSL 11:cee25a834751 3258 }
wolfSSL 11:cee25a834751 3259
wolfSSL 11:cee25a834751 3260
wolfSSL 11:cee25a834751 3261 static INLINE void RIGHTSHIFTX(byte* x)
wolfSSL 11:cee25a834751 3262 {
wolfSSL 11:cee25a834751 3263 int i;
wolfSSL 11:cee25a834751 3264 int carryOut = 0;
wolfSSL 11:cee25a834751 3265 int carryIn = 0;
wolfSSL 11:cee25a834751 3266 int borrow = x[15] & 0x01;
wolfSSL 11:cee25a834751 3267
wolfSSL 11:cee25a834751 3268 for (i = 0; i < AES_BLOCK_SIZE; i++) {
wolfSSL 11:cee25a834751 3269 carryOut = x[i] & 0x01;
wolfSSL 11:cee25a834751 3270 x[i] = (x[i] >> 1) | (carryIn ? 0x80 : 0);
wolfSSL 11:cee25a834751 3271 carryIn = carryOut;
wolfSSL 11:cee25a834751 3272 }
wolfSSL 11:cee25a834751 3273 if (borrow) x[0] ^= 0xE1;
wolfSSL 11:cee25a834751 3274 }
wolfSSL 11:cee25a834751 3275
wolfSSL 11:cee25a834751 3276 #endif /* defined(GCM_SMALL) || defined(GCM_TABLE) */
wolfSSL 11:cee25a834751 3277
wolfSSL 11:cee25a834751 3278
wolfSSL 11:cee25a834751 3279 #ifdef GCM_TABLE
wolfSSL 11:cee25a834751 3280
wolfSSL 11:cee25a834751 3281 static void GenerateM0(Aes* aes)
wolfSSL 11:cee25a834751 3282 {
wolfSSL 11:cee25a834751 3283 int i, j;
wolfSSL 11:cee25a834751 3284 byte (*m)[AES_BLOCK_SIZE] = aes->M0;
wolfSSL 11:cee25a834751 3285
wolfSSL 11:cee25a834751 3286 XMEMCPY(m[128], aes->H, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3287
wolfSSL 11:cee25a834751 3288 for (i = 64; i > 0; i /= 2) {
wolfSSL 11:cee25a834751 3289 XMEMCPY(m[i], m[i*2], AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3290 RIGHTSHIFTX(m[i]);
wolfSSL 11:cee25a834751 3291 }
wolfSSL 11:cee25a834751 3292
wolfSSL 11:cee25a834751 3293 for (i = 2; i < 256; i *= 2) {
wolfSSL 11:cee25a834751 3294 for (j = 1; j < i; j++) {
wolfSSL 11:cee25a834751 3295 XMEMCPY(m[i+j], m[i], AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3296 xorbuf(m[i+j], m[j], AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3297 }
wolfSSL 11:cee25a834751 3298 }
wolfSSL 11:cee25a834751 3299
wolfSSL 11:cee25a834751 3300 XMEMSET(m[0], 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3301 }
wolfSSL 11:cee25a834751 3302
wolfSSL 11:cee25a834751 3303 #endif /* GCM_TABLE */
wolfSSL 11:cee25a834751 3304
wolfSSL 11:cee25a834751 3305
wolfSSL 11:cee25a834751 3306 int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
wolfSSL 11:cee25a834751 3307 {
wolfSSL 11:cee25a834751 3308 int ret;
wolfSSL 11:cee25a834751 3309 byte iv[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 3310
wolfSSL 11:cee25a834751 3311 if (!((len == 16) || (len == 24) || (len == 32)))
wolfSSL 11:cee25a834751 3312 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 3313
wolfSSL 11:cee25a834751 3314 XMEMSET(iv, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3315 ret = wc_AesSetKey(aes, key, len, iv, AES_ENCRYPTION);
wolfSSL 11:cee25a834751 3316
wolfSSL 11:cee25a834751 3317 #ifdef WOLFSSL_AESNI
wolfSSL 11:cee25a834751 3318 /* AES-NI code generates its own H value. */
wolfSSL 11:cee25a834751 3319 if (haveAESNI)
wolfSSL 11:cee25a834751 3320 return ret;
wolfSSL 11:cee25a834751 3321 #endif /* WOLFSSL_AESNI */
wolfSSL 11:cee25a834751 3322
wolfSSL 11:cee25a834751 3323 #if !defined(FREESCALE_LTC_AES_GCM)
wolfSSL 11:cee25a834751 3324 if (ret == 0) {
wolfSSL 11:cee25a834751 3325 wc_AesEncrypt(aes, iv, aes->H);
wolfSSL 11:cee25a834751 3326 #ifdef GCM_TABLE
wolfSSL 11:cee25a834751 3327 GenerateM0(aes);
wolfSSL 11:cee25a834751 3328 #endif /* GCM_TABLE */
wolfSSL 11:cee25a834751 3329 }
wolfSSL 11:cee25a834751 3330 #endif /* FREESCALE_LTC_AES_GCM */
wolfSSL 11:cee25a834751 3331
wolfSSL 11:cee25a834751 3332 return ret;
wolfSSL 11:cee25a834751 3333 }
wolfSSL 11:cee25a834751 3334
wolfSSL 11:cee25a834751 3335
wolfSSL 11:cee25a834751 3336 #ifdef WOLFSSL_AESNI
wolfSSL 11:cee25a834751 3337
wolfSSL 11:cee25a834751 3338 void gfmul(__m128i a, __m128i b, __m128i* out) XASM_LINK("gfmul");
wolfSSL 11:cee25a834751 3339
wolfSSL 11:cee25a834751 3340
wolfSSL 11:cee25a834751 3341 /* See Intel® Carry-Less Multiplication Instruction
wolfSSL 11:cee25a834751 3342 * and its Usage for Computing the GCM Mode White Paper
wolfSSL 11:cee25a834751 3343 * by Shay Gueron, Intel Mobility Group, Israel Development Center;
wolfSSL 11:cee25a834751 3344 * and Michael E. Kounavis, Intel Labs, Circuits and Systems Research */
wolfSSL 11:cee25a834751 3345
wolfSSL 11:cee25a834751 3346
wolfSSL 11:cee25a834751 3347 /* Figure 9. AES-GCM – Encrypt With Single Block Ghash at a Time */
wolfSSL 11:cee25a834751 3348
wolfSSL 11:cee25a834751 3349 static void AES_GCM_encrypt(const unsigned char *in,
wolfSSL 11:cee25a834751 3350 unsigned char *out,
wolfSSL 11:cee25a834751 3351 const unsigned char* addt,
wolfSSL 11:cee25a834751 3352 const unsigned char* ivec,
wolfSSL 11:cee25a834751 3353 unsigned char *tag,
wolfSSL 11:cee25a834751 3354 int nbytes, int abytes, int ibytes,
wolfSSL 11:cee25a834751 3355 const unsigned char* key, int nr)
wolfSSL 11:cee25a834751 3356 {
wolfSSL 11:cee25a834751 3357 int i, j ,k;
wolfSSL 11:cee25a834751 3358 __m128i tmp1, tmp2, tmp3, tmp4;
wolfSSL 11:cee25a834751 3359 __m128i H, Y, T;
wolfSSL 11:cee25a834751 3360 __m128i *KEY = (__m128i*)key;
wolfSSL 11:cee25a834751 3361 __m128i ctr1, ctr2, ctr3, ctr4;
wolfSSL 11:cee25a834751 3362 __m128i last_block = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3363 __m128i ONE = _mm_set_epi32(0, 1, 0, 0);
wolfSSL 11:cee25a834751 3364 __m128i FOUR = _mm_set_epi32(0, 4, 0, 0);
wolfSSL 11:cee25a834751 3365 __m128i BSWAP_EPI64 = _mm_set_epi8(8,9,10,11,12,13,14,15,0,1,2,3,4,5,6,7);
wolfSSL 11:cee25a834751 3366 __m128i BSWAP_MASK = _mm_set_epi8(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
wolfSSL 11:cee25a834751 3367 __m128i X = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3368
wolfSSL 11:cee25a834751 3369 if(ibytes == 96/8) {
wolfSSL 11:cee25a834751 3370 Y = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3371 for(j=0; j < ibytes%16; j++)
wolfSSL 11:cee25a834751 3372 ((unsigned char*)&Y)[j] = ivec[j];
wolfSSL 11:cee25a834751 3373 Y = _mm_insert_epi32(Y, 0x1000000, 3);
wolfSSL 11:cee25a834751 3374 /* (Compute E[ZERO, KS] and E[Y0, KS] together */
wolfSSL 11:cee25a834751 3375 tmp1 = _mm_xor_si128(X, KEY[0]);
wolfSSL 11:cee25a834751 3376 tmp2 = _mm_xor_si128(Y, KEY[0]);
wolfSSL 11:cee25a834751 3377 for(j=1; j < nr-1; j+=2) {
wolfSSL 11:cee25a834751 3378 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3379 tmp2 = _mm_aesenc_si128(tmp2, KEY[j]);
wolfSSL 11:cee25a834751 3380 tmp1 = _mm_aesenc_si128(tmp1, KEY[j+1]);
wolfSSL 11:cee25a834751 3381 tmp2 = _mm_aesenc_si128(tmp2, KEY[j+1]);
wolfSSL 11:cee25a834751 3382 }
wolfSSL 11:cee25a834751 3383 tmp1 = _mm_aesenc_si128(tmp1, KEY[nr-1]);
wolfSSL 11:cee25a834751 3384 tmp2 = _mm_aesenc_si128(tmp2, KEY[nr-1]);
wolfSSL 11:cee25a834751 3385 H = _mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3386 T = _mm_aesenclast_si128(tmp2, KEY[nr]);
wolfSSL 11:cee25a834751 3387 H = _mm_shuffle_epi8(H, BSWAP_MASK);
wolfSSL 11:cee25a834751 3388 }
wolfSSL 11:cee25a834751 3389 else {
wolfSSL 11:cee25a834751 3390 tmp1 = _mm_xor_si128(X, KEY[0]);
wolfSSL 11:cee25a834751 3391 for(j=1; j <nr; j++)
wolfSSL 11:cee25a834751 3392 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3393 H = _mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3394 H = _mm_shuffle_epi8(H, BSWAP_MASK);
wolfSSL 11:cee25a834751 3395 Y = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3396 for(i=0; i < ibytes/16; i++) {
wolfSSL 11:cee25a834751 3397 tmp1 = _mm_loadu_si128(&((__m128i*)ivec)[i]);
wolfSSL 11:cee25a834751 3398 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3399 Y = _mm_xor_si128(Y, tmp1);
wolfSSL 11:cee25a834751 3400 gfmul(Y, H, &Y);
wolfSSL 11:cee25a834751 3401 }
wolfSSL 11:cee25a834751 3402 if(ibytes%16) {
wolfSSL 11:cee25a834751 3403 for(j=0; j < ibytes%16; j++)
wolfSSL 11:cee25a834751 3404 ((unsigned char*)&last_block)[j] = ivec[i*16+j];
wolfSSL 11:cee25a834751 3405 tmp1 = last_block;
wolfSSL 11:cee25a834751 3406 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3407 Y = _mm_xor_si128(Y, tmp1);
wolfSSL 11:cee25a834751 3408 gfmul(Y, H, &Y);
wolfSSL 11:cee25a834751 3409 }
wolfSSL 11:cee25a834751 3410 tmp1 = _mm_insert_epi64(tmp1, ibytes*8, 0);
wolfSSL 11:cee25a834751 3411 tmp1 = _mm_insert_epi64(tmp1, 0, 1);
wolfSSL 11:cee25a834751 3412 Y = _mm_xor_si128(Y, tmp1);
wolfSSL 11:cee25a834751 3413 gfmul(Y, H, &Y);
wolfSSL 11:cee25a834751 3414 Y = _mm_shuffle_epi8(Y, BSWAP_MASK); /* Compute E(K, Y0) */
wolfSSL 11:cee25a834751 3415 tmp1 = _mm_xor_si128(Y, KEY[0]);
wolfSSL 11:cee25a834751 3416 for(j=1; j < nr; j++)
wolfSSL 11:cee25a834751 3417 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3418 T = _mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3419 }
wolfSSL 11:cee25a834751 3420
wolfSSL 11:cee25a834751 3421 for(i=0; i<abytes/16; i++){
wolfSSL 11:cee25a834751 3422 tmp1 = _mm_loadu_si128(&((__m128i*)addt)[i]);
wolfSSL 11:cee25a834751 3423 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3424 X = _mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3425 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3426 }
wolfSSL 11:cee25a834751 3427 if(abytes%16){
wolfSSL 11:cee25a834751 3428 last_block = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3429 for(j=0; j<abytes%16; j++)
wolfSSL 11:cee25a834751 3430 ((unsigned char*)&last_block)[j] = addt[i*16+j];
wolfSSL 11:cee25a834751 3431 tmp1 = last_block;
wolfSSL 11:cee25a834751 3432 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3433 X = _mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3434 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3435 }
wolfSSL 11:cee25a834751 3436
wolfSSL 11:cee25a834751 3437 ctr1 = _mm_shuffle_epi8(Y, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3438 ctr1 = _mm_add_epi32(ctr1, ONE);
wolfSSL 11:cee25a834751 3439 ctr2 = _mm_add_epi32(ctr1, ONE);
wolfSSL 11:cee25a834751 3440 ctr3 = _mm_add_epi32(ctr2, ONE);
wolfSSL 11:cee25a834751 3441 ctr4 = _mm_add_epi32(ctr3, ONE);
wolfSSL 11:cee25a834751 3442
wolfSSL 11:cee25a834751 3443 for(i=0; i < nbytes/16/4; i++){
wolfSSL 11:cee25a834751 3444 tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3445 tmp2 = _mm_shuffle_epi8(ctr2, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3446 tmp3 = _mm_shuffle_epi8(ctr3, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3447 tmp4 = _mm_shuffle_epi8(ctr4, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3448 ctr1 = _mm_add_epi32(ctr1, FOUR);
wolfSSL 11:cee25a834751 3449 ctr2 = _mm_add_epi32(ctr2, FOUR);
wolfSSL 11:cee25a834751 3450 ctr3 = _mm_add_epi32(ctr3, FOUR);
wolfSSL 11:cee25a834751 3451 ctr4 = _mm_add_epi32(ctr4, FOUR);
wolfSSL 11:cee25a834751 3452 tmp1 =_mm_xor_si128(tmp1, KEY[0]);
wolfSSL 11:cee25a834751 3453 tmp2 =_mm_xor_si128(tmp2, KEY[0]);
wolfSSL 11:cee25a834751 3454 tmp3 =_mm_xor_si128(tmp3, KEY[0]);
wolfSSL 11:cee25a834751 3455 tmp4 =_mm_xor_si128(tmp4, KEY[0]);
wolfSSL 11:cee25a834751 3456 for(j=1; j < nr-1; j+=2){
wolfSSL 11:cee25a834751 3457 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3458 tmp2 = _mm_aesenc_si128(tmp2, KEY[j]);
wolfSSL 11:cee25a834751 3459 tmp3 = _mm_aesenc_si128(tmp3, KEY[j]);
wolfSSL 11:cee25a834751 3460 tmp4 = _mm_aesenc_si128(tmp4, KEY[j]);
wolfSSL 11:cee25a834751 3461 tmp1 = _mm_aesenc_si128(tmp1, KEY[j+1]);
wolfSSL 11:cee25a834751 3462 tmp2 = _mm_aesenc_si128(tmp2, KEY[j+1]);
wolfSSL 11:cee25a834751 3463 tmp3 = _mm_aesenc_si128(tmp3, KEY[j+1]);
wolfSSL 11:cee25a834751 3464 tmp4 = _mm_aesenc_si128(tmp4, KEY[j+1]);
wolfSSL 11:cee25a834751 3465 }
wolfSSL 11:cee25a834751 3466 tmp1 = _mm_aesenc_si128(tmp1, KEY[nr-1]);
wolfSSL 11:cee25a834751 3467 tmp2 = _mm_aesenc_si128(tmp2, KEY[nr-1]);
wolfSSL 11:cee25a834751 3468 tmp3 = _mm_aesenc_si128(tmp3, KEY[nr-1]);
wolfSSL 11:cee25a834751 3469 tmp4 = _mm_aesenc_si128(tmp4, KEY[nr-1]);
wolfSSL 11:cee25a834751 3470 tmp1 =_mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3471 tmp2 =_mm_aesenclast_si128(tmp2, KEY[nr]);
wolfSSL 11:cee25a834751 3472 tmp3 =_mm_aesenclast_si128(tmp3, KEY[nr]);
wolfSSL 11:cee25a834751 3473 tmp4 =_mm_aesenclast_si128(tmp4, KEY[nr]);
wolfSSL 11:cee25a834751 3474 tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[i*4+0]));
wolfSSL 11:cee25a834751 3475 tmp2 = _mm_xor_si128(tmp2, _mm_loadu_si128(&((__m128i*)in)[i*4+1]));
wolfSSL 11:cee25a834751 3476 tmp3 = _mm_xor_si128(tmp3, _mm_loadu_si128(&((__m128i*)in)[i*4+2]));
wolfSSL 11:cee25a834751 3477 tmp4 = _mm_xor_si128(tmp4, _mm_loadu_si128(&((__m128i*)in)[i*4+3]));
wolfSSL 11:cee25a834751 3478 _mm_storeu_si128(&((__m128i*)out)[i*4+0], tmp1);
wolfSSL 11:cee25a834751 3479 _mm_storeu_si128(&((__m128i*)out)[i*4+1], tmp2);
wolfSSL 11:cee25a834751 3480 _mm_storeu_si128(&((__m128i*)out)[i*4+2], tmp3);
wolfSSL 11:cee25a834751 3481 _mm_storeu_si128(&((__m128i*)out)[i*4+3], tmp4);
wolfSSL 11:cee25a834751 3482 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3483 tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_MASK);
wolfSSL 11:cee25a834751 3484 tmp3 = _mm_shuffle_epi8(tmp3, BSWAP_MASK);
wolfSSL 11:cee25a834751 3485 tmp4 = _mm_shuffle_epi8(tmp4, BSWAP_MASK);
wolfSSL 11:cee25a834751 3486 X = _mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3487 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3488 X = _mm_xor_si128(X, tmp2);
wolfSSL 11:cee25a834751 3489 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3490 X = _mm_xor_si128(X, tmp3);
wolfSSL 11:cee25a834751 3491 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3492 X = _mm_xor_si128(X, tmp4);
wolfSSL 11:cee25a834751 3493 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3494 }
wolfSSL 11:cee25a834751 3495 for(k = i*4; k < nbytes/16; k++){
wolfSSL 11:cee25a834751 3496 tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3497 ctr1 = _mm_add_epi32(ctr1, ONE);
wolfSSL 11:cee25a834751 3498 tmp1 = _mm_xor_si128(tmp1, KEY[0]);
wolfSSL 11:cee25a834751 3499 for(j=1; j<nr-1; j+=2){
wolfSSL 11:cee25a834751 3500 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3501 tmp1 = _mm_aesenc_si128(tmp1, KEY[j+1]);
wolfSSL 11:cee25a834751 3502 }
wolfSSL 11:cee25a834751 3503 tmp1 = _mm_aesenc_si128(tmp1, KEY[nr-1]);
wolfSSL 11:cee25a834751 3504 tmp1 = _mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3505 tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[k]));
wolfSSL 11:cee25a834751 3506 _mm_storeu_si128(&((__m128i*)out)[k], tmp1);
wolfSSL 11:cee25a834751 3507 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3508 X =_mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3509 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3510 }
wolfSSL 11:cee25a834751 3511 /* If one partial block remains */
wolfSSL 11:cee25a834751 3512 if(nbytes%16){
wolfSSL 11:cee25a834751 3513 tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3514 tmp1 = _mm_xor_si128(tmp1, KEY[0]);
wolfSSL 11:cee25a834751 3515 for(j=1; j<nr-1; j+=2){
wolfSSL 11:cee25a834751 3516 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3517 tmp1 = _mm_aesenc_si128(tmp1, KEY[j+1]);
wolfSSL 11:cee25a834751 3518 }
wolfSSL 11:cee25a834751 3519 tmp1 = _mm_aesenc_si128(tmp1, KEY[nr-1]);
wolfSSL 11:cee25a834751 3520 tmp1 = _mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3521 for(j=0; j < nbytes%16; j++)
wolfSSL 11:cee25a834751 3522 ((unsigned char*)&last_block)[j]= in[k*16+j];
wolfSSL 11:cee25a834751 3523 tmp1 = _mm_xor_si128(tmp1, last_block);
wolfSSL 11:cee25a834751 3524 last_block = tmp1;
wolfSSL 11:cee25a834751 3525 for(j=0; j < nbytes%16; j++)
wolfSSL 11:cee25a834751 3526 out[k*16+j]=((unsigned char*)&last_block)[j];
wolfSSL 11:cee25a834751 3527 for(; j<16; j++)
wolfSSL 11:cee25a834751 3528 ((unsigned char*)&last_block)[j]=0;
wolfSSL 11:cee25a834751 3529 tmp1 = last_block;
wolfSSL 11:cee25a834751 3530 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3531 X =_mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3532 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3533 }
wolfSSL 11:cee25a834751 3534 tmp1 = _mm_insert_epi64(tmp1, nbytes*8, 0);
wolfSSL 11:cee25a834751 3535 tmp1 = _mm_insert_epi64(tmp1, abytes*8, 1);
wolfSSL 11:cee25a834751 3536 X = _mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3537 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3538 X = _mm_shuffle_epi8(X, BSWAP_MASK);
wolfSSL 11:cee25a834751 3539 T = _mm_xor_si128(X, T);
wolfSSL 11:cee25a834751 3540 _mm_storeu_si128((__m128i*)tag, T);
wolfSSL 11:cee25a834751 3541 }
wolfSSL 11:cee25a834751 3542
wolfSSL 11:cee25a834751 3543
wolfSSL 11:cee25a834751 3544 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 3545 /* Figure 10. AES-GCM – Decrypt With Single Block Ghash at a Time */
wolfSSL 11:cee25a834751 3546
wolfSSL 11:cee25a834751 3547 static int AES_GCM_decrypt(const unsigned char *in,
wolfSSL 11:cee25a834751 3548 unsigned char *out,
wolfSSL 11:cee25a834751 3549 const unsigned char* addt,
wolfSSL 11:cee25a834751 3550 const unsigned char* ivec,
wolfSSL 11:cee25a834751 3551 const unsigned char *tag, int nbytes, int abytes,
wolfSSL 11:cee25a834751 3552 int ibytes, const unsigned char* key, int nr)
wolfSSL 11:cee25a834751 3553 {
wolfSSL 11:cee25a834751 3554 int i, j ,k;
wolfSSL 11:cee25a834751 3555 __m128i tmp1, tmp2, tmp3, tmp4;
wolfSSL 11:cee25a834751 3556 __m128i H, Y, T;
wolfSSL 11:cee25a834751 3557 __m128i *KEY = (__m128i*)key;
wolfSSL 11:cee25a834751 3558 __m128i ctr1, ctr2, ctr3, ctr4;
wolfSSL 11:cee25a834751 3559 __m128i last_block = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3560 __m128i ONE = _mm_set_epi32(0, 1, 0, 0);
wolfSSL 11:cee25a834751 3561 __m128i FOUR = _mm_set_epi32(0, 4, 0, 0);
wolfSSL 11:cee25a834751 3562 __m128i BSWAP_EPI64 = _mm_set_epi8(8,9,10,11,12,13,14,15,0,1,2,3,4,5,6,7);
wolfSSL 11:cee25a834751 3563 __m128i BSWAP_MASK = _mm_set_epi8(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
wolfSSL 11:cee25a834751 3564 __m128i X = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3565
wolfSSL 11:cee25a834751 3566 if (ibytes == 96/8) {
wolfSSL 11:cee25a834751 3567 Y = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3568 for(j=0; j < ibytes%16; j++)
wolfSSL 11:cee25a834751 3569 ((unsigned char*)&Y)[j] = ivec[j];
wolfSSL 11:cee25a834751 3570 Y = _mm_insert_epi32(Y, 0x1000000, 3);
wolfSSL 11:cee25a834751 3571 /* (Compute E[ZERO, KS] and E[Y0, KS] together */
wolfSSL 11:cee25a834751 3572 tmp1 = _mm_xor_si128(X, KEY[0]);
wolfSSL 11:cee25a834751 3573 tmp2 = _mm_xor_si128(Y, KEY[0]);
wolfSSL 11:cee25a834751 3574 for (j = 1; j < nr - 1; j += 2) {
wolfSSL 11:cee25a834751 3575 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3576 tmp2 = _mm_aesenc_si128(tmp2, KEY[j]);
wolfSSL 11:cee25a834751 3577 tmp1 = _mm_aesenc_si128(tmp1, KEY[j+1]);
wolfSSL 11:cee25a834751 3578 tmp2 = _mm_aesenc_si128(tmp2, KEY[j+1]);
wolfSSL 11:cee25a834751 3579 }
wolfSSL 11:cee25a834751 3580 tmp1 = _mm_aesenc_si128(tmp1, KEY[nr-1]);
wolfSSL 11:cee25a834751 3581 tmp2 = _mm_aesenc_si128(tmp2, KEY[nr-1]);
wolfSSL 11:cee25a834751 3582 H = _mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3583 T = _mm_aesenclast_si128(tmp2, KEY[nr]);
wolfSSL 11:cee25a834751 3584 H = _mm_shuffle_epi8(H, BSWAP_MASK);
wolfSSL 11:cee25a834751 3585 }
wolfSSL 11:cee25a834751 3586 else {
wolfSSL 11:cee25a834751 3587 tmp1 = _mm_xor_si128(X, KEY[0]);
wolfSSL 11:cee25a834751 3588 for (j = 1; j < nr; j++)
wolfSSL 11:cee25a834751 3589 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3590 H = _mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3591 H = _mm_shuffle_epi8(H, BSWAP_MASK);
wolfSSL 11:cee25a834751 3592 Y = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3593
wolfSSL 11:cee25a834751 3594 for (i = 0; i < ibytes / 16; i++) {
wolfSSL 11:cee25a834751 3595 tmp1 = _mm_loadu_si128(&((__m128i*)ivec)[i]);
wolfSSL 11:cee25a834751 3596 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3597 Y = _mm_xor_si128(Y, tmp1);
wolfSSL 11:cee25a834751 3598 gfmul(Y, H, &Y);
wolfSSL 11:cee25a834751 3599 }
wolfSSL 11:cee25a834751 3600
wolfSSL 11:cee25a834751 3601 if (ibytes % 16) {
wolfSSL 11:cee25a834751 3602 for(j = 0; j < ibytes % 16; j++)
wolfSSL 11:cee25a834751 3603 ((unsigned char*)&last_block)[j] = ivec[i*16+j];
wolfSSL 11:cee25a834751 3604 tmp1 = last_block;
wolfSSL 11:cee25a834751 3605 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3606 Y = _mm_xor_si128(Y, tmp1);
wolfSSL 11:cee25a834751 3607 gfmul(Y, H, &Y);
wolfSSL 11:cee25a834751 3608 }
wolfSSL 11:cee25a834751 3609
wolfSSL 11:cee25a834751 3610 tmp1 = _mm_insert_epi64(tmp1, ibytes*8, 0);
wolfSSL 11:cee25a834751 3611 tmp1 = _mm_insert_epi64(tmp1, 0, 1);
wolfSSL 11:cee25a834751 3612 Y = _mm_xor_si128(Y, tmp1);
wolfSSL 11:cee25a834751 3613 gfmul(Y, H, &Y);
wolfSSL 11:cee25a834751 3614 Y = _mm_shuffle_epi8(Y, BSWAP_MASK);
wolfSSL 11:cee25a834751 3615 /* Compute E(K, Y0) */
wolfSSL 11:cee25a834751 3616 tmp1 = _mm_xor_si128(Y, KEY[0]);
wolfSSL 11:cee25a834751 3617 for(j=1; j < nr; j++)
wolfSSL 11:cee25a834751 3618 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3619 T = _mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3620 }
wolfSSL 11:cee25a834751 3621
wolfSSL 11:cee25a834751 3622 for (i = 0; i < abytes / 16; i++) {
wolfSSL 11:cee25a834751 3623 tmp1 = _mm_loadu_si128(&((__m128i*)addt)[i]);
wolfSSL 11:cee25a834751 3624 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3625 X = _mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3626 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3627 }
wolfSSL 11:cee25a834751 3628
wolfSSL 11:cee25a834751 3629 if (abytes % 16) {
wolfSSL 11:cee25a834751 3630 last_block = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3631 for (j = 0;j < abytes % 16; j++)
wolfSSL 11:cee25a834751 3632 ((unsigned char*)&last_block)[j] = addt[i*16+j];
wolfSSL 11:cee25a834751 3633 tmp1 = last_block;
wolfSSL 11:cee25a834751 3634 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3635 X =_mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3636 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3637 }
wolfSSL 11:cee25a834751 3638
wolfSSL 11:cee25a834751 3639 for (i = 0; i < nbytes / 16; i++) {
wolfSSL 11:cee25a834751 3640 tmp1 = _mm_loadu_si128(&((__m128i*)in)[i]);
wolfSSL 11:cee25a834751 3641 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3642 X = _mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3643 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3644 }
wolfSSL 11:cee25a834751 3645
wolfSSL 11:cee25a834751 3646 if (nbytes % 16) {
wolfSSL 11:cee25a834751 3647 last_block = _mm_setzero_si128();
wolfSSL 11:cee25a834751 3648 for(j = 0; j < nbytes % 16; j++)
wolfSSL 11:cee25a834751 3649 ((unsigned char*)&last_block)[j] = in[i*16+j];
wolfSSL 11:cee25a834751 3650 tmp1 = last_block;
wolfSSL 11:cee25a834751 3651 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3652 X = _mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3653 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3654 }
wolfSSL 11:cee25a834751 3655
wolfSSL 11:cee25a834751 3656 tmp1 = _mm_insert_epi64(tmp1, nbytes * 8, 0);
wolfSSL 11:cee25a834751 3657 tmp1 = _mm_insert_epi64(tmp1, abytes * 8, 1);
wolfSSL 11:cee25a834751 3658 X = _mm_xor_si128(X, tmp1);
wolfSSL 11:cee25a834751 3659 gfmul(X, H, &X);
wolfSSL 11:cee25a834751 3660 X = _mm_shuffle_epi8(X, BSWAP_MASK);
wolfSSL 11:cee25a834751 3661 T = _mm_xor_si128(X, T);
wolfSSL 11:cee25a834751 3662
wolfSSL 11:cee25a834751 3663 if (0xffff !=
wolfSSL 11:cee25a834751 3664 _mm_movemask_epi8(_mm_cmpeq_epi8(T, _mm_loadu_si128((__m128i*)tag))))
wolfSSL 11:cee25a834751 3665 return 0; /* in case the authentication failed */
wolfSSL 11:cee25a834751 3666
wolfSSL 11:cee25a834751 3667 ctr1 = _mm_shuffle_epi8(Y, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3668 ctr1 = _mm_add_epi32(ctr1, ONE);
wolfSSL 11:cee25a834751 3669 ctr2 = _mm_add_epi32(ctr1, ONE);
wolfSSL 11:cee25a834751 3670 ctr3 = _mm_add_epi32(ctr2, ONE);
wolfSSL 11:cee25a834751 3671 ctr4 = _mm_add_epi32(ctr3, ONE);
wolfSSL 11:cee25a834751 3672
wolfSSL 11:cee25a834751 3673 for (i=0; i < nbytes/16/4; i++) {
wolfSSL 11:cee25a834751 3674 tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3675 tmp2 = _mm_shuffle_epi8(ctr2, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3676 tmp3 = _mm_shuffle_epi8(ctr3, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3677 tmp4 = _mm_shuffle_epi8(ctr4, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3678
wolfSSL 11:cee25a834751 3679 ctr1 = _mm_add_epi32(ctr1, FOUR);
wolfSSL 11:cee25a834751 3680 ctr2 = _mm_add_epi32(ctr2, FOUR);
wolfSSL 11:cee25a834751 3681 ctr3 = _mm_add_epi32(ctr3, FOUR);
wolfSSL 11:cee25a834751 3682 ctr4 = _mm_add_epi32(ctr4, FOUR);
wolfSSL 11:cee25a834751 3683
wolfSSL 11:cee25a834751 3684 tmp1 =_mm_xor_si128(tmp1, KEY[0]);
wolfSSL 11:cee25a834751 3685 tmp2 =_mm_xor_si128(tmp2, KEY[0]);
wolfSSL 11:cee25a834751 3686 tmp3 =_mm_xor_si128(tmp3, KEY[0]);
wolfSSL 11:cee25a834751 3687 tmp4 =_mm_xor_si128(tmp4, KEY[0]);
wolfSSL 11:cee25a834751 3688
wolfSSL 11:cee25a834751 3689 for (j = 1; j < nr - 1; j += 2) {
wolfSSL 11:cee25a834751 3690 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3691 tmp2 = _mm_aesenc_si128(tmp2, KEY[j]);
wolfSSL 11:cee25a834751 3692 tmp3 = _mm_aesenc_si128(tmp3, KEY[j]);
wolfSSL 11:cee25a834751 3693 tmp4 = _mm_aesenc_si128(tmp4, KEY[j]);
wolfSSL 11:cee25a834751 3694
wolfSSL 11:cee25a834751 3695 tmp1 = _mm_aesenc_si128(tmp1, KEY[j+1]);
wolfSSL 11:cee25a834751 3696 tmp2 = _mm_aesenc_si128(tmp2, KEY[j+1]);
wolfSSL 11:cee25a834751 3697 tmp3 = _mm_aesenc_si128(tmp3, KEY[j+1]);
wolfSSL 11:cee25a834751 3698 tmp4 = _mm_aesenc_si128(tmp4, KEY[j+1]);
wolfSSL 11:cee25a834751 3699 }
wolfSSL 11:cee25a834751 3700
wolfSSL 11:cee25a834751 3701 tmp1 = _mm_aesenc_si128(tmp1, KEY[nr-1]);
wolfSSL 11:cee25a834751 3702 tmp2 = _mm_aesenc_si128(tmp2, KEY[nr-1]);
wolfSSL 11:cee25a834751 3703 tmp3 = _mm_aesenc_si128(tmp3, KEY[nr-1]);
wolfSSL 11:cee25a834751 3704 tmp4 = _mm_aesenc_si128(tmp4, KEY[nr-1]);
wolfSSL 11:cee25a834751 3705
wolfSSL 11:cee25a834751 3706 tmp1 =_mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3707 tmp2 =_mm_aesenclast_si128(tmp2, KEY[nr]);
wolfSSL 11:cee25a834751 3708 tmp3 =_mm_aesenclast_si128(tmp3, KEY[nr]);
wolfSSL 11:cee25a834751 3709 tmp4 =_mm_aesenclast_si128(tmp4, KEY[nr]);
wolfSSL 11:cee25a834751 3710
wolfSSL 11:cee25a834751 3711 tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[i*4+0]));
wolfSSL 11:cee25a834751 3712 tmp2 = _mm_xor_si128(tmp2, _mm_loadu_si128(&((__m128i*)in)[i*4+1]));
wolfSSL 11:cee25a834751 3713 tmp3 = _mm_xor_si128(tmp3, _mm_loadu_si128(&((__m128i*)in)[i*4+2]));
wolfSSL 11:cee25a834751 3714 tmp4 = _mm_xor_si128(tmp4, _mm_loadu_si128(&((__m128i*)in)[i*4+3]));
wolfSSL 11:cee25a834751 3715
wolfSSL 11:cee25a834751 3716 _mm_storeu_si128(&((__m128i*)out)[i*4+0], tmp1);
wolfSSL 11:cee25a834751 3717 _mm_storeu_si128(&((__m128i*)out)[i*4+1], tmp2);
wolfSSL 11:cee25a834751 3718 _mm_storeu_si128(&((__m128i*)out)[i*4+2], tmp3);
wolfSSL 11:cee25a834751 3719 _mm_storeu_si128(&((__m128i*)out)[i*4+3], tmp4);
wolfSSL 11:cee25a834751 3720
wolfSSL 11:cee25a834751 3721 tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
wolfSSL 11:cee25a834751 3722 tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_MASK);
wolfSSL 11:cee25a834751 3723 tmp3 = _mm_shuffle_epi8(tmp3, BSWAP_MASK);
wolfSSL 11:cee25a834751 3724 tmp4 = _mm_shuffle_epi8(tmp4, BSWAP_MASK);
wolfSSL 11:cee25a834751 3725 }
wolfSSL 11:cee25a834751 3726
wolfSSL 11:cee25a834751 3727 /* Acknowledge the dead store and continue */
wolfSSL 11:cee25a834751 3728 (void) tmp1;
wolfSSL 11:cee25a834751 3729 (void) tmp2;
wolfSSL 11:cee25a834751 3730 (void) tmp3;
wolfSSL 11:cee25a834751 3731 (void) tmp4;
wolfSSL 11:cee25a834751 3732
wolfSSL 11:cee25a834751 3733 for (k = i*4; k < nbytes/16; k++) {
wolfSSL 11:cee25a834751 3734 tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3735 ctr1 = _mm_add_epi32(ctr1, ONE);
wolfSSL 11:cee25a834751 3736 tmp1 = _mm_xor_si128(tmp1, KEY[0]);
wolfSSL 11:cee25a834751 3737 for (j = 1; j < nr-1; j += 2) {
wolfSSL 11:cee25a834751 3738 tmp1 = _mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3739 tmp1 = _mm_aesenc_si128(tmp1, KEY[j+1]);
wolfSSL 11:cee25a834751 3740 }
wolfSSL 11:cee25a834751 3741 tmp1 = _mm_aesenc_si128(tmp1, KEY[nr-1]);
wolfSSL 11:cee25a834751 3742 tmp1 = _mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3743 tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[k]));
wolfSSL 11:cee25a834751 3744 _mm_storeu_si128(&((__m128i*)out)[k], tmp1);
wolfSSL 11:cee25a834751 3745 }
wolfSSL 11:cee25a834751 3746
wolfSSL 11:cee25a834751 3747 /* If one partial block remains */
wolfSSL 11:cee25a834751 3748 if (nbytes % 16) {
wolfSSL 11:cee25a834751 3749 tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
wolfSSL 11:cee25a834751 3750 tmp1 = _mm_xor_si128(tmp1, KEY[0]);
wolfSSL 11:cee25a834751 3751 for (j = 1; j < nr-1; j += 2) {
wolfSSL 11:cee25a834751 3752 tmp1 =_mm_aesenc_si128(tmp1, KEY[j]);
wolfSSL 11:cee25a834751 3753 tmp1 =_mm_aesenc_si128(tmp1, KEY[j+1]);
wolfSSL 11:cee25a834751 3754 }
wolfSSL 11:cee25a834751 3755 tmp1 = _mm_aesenc_si128(tmp1, KEY[nr-1]);
wolfSSL 11:cee25a834751 3756 tmp1 = _mm_aesenclast_si128(tmp1, KEY[nr]);
wolfSSL 11:cee25a834751 3757 for(j=0; j < nbytes%16; j++)
wolfSSL 11:cee25a834751 3758 ((unsigned char*)&last_block)[j]= in[k*16+j];
wolfSSL 11:cee25a834751 3759 tmp1 = _mm_xor_si128(tmp1, last_block);
wolfSSL 11:cee25a834751 3760 last_block = tmp1;
wolfSSL 11:cee25a834751 3761 for (j = 0; j < nbytes % 16; j++)
wolfSSL 11:cee25a834751 3762 out[k*16+j]=((unsigned char*)&last_block)[j];
wolfSSL 11:cee25a834751 3763 }
wolfSSL 11:cee25a834751 3764
wolfSSL 11:cee25a834751 3765 return 1; /* when successful returns 1 */
wolfSSL 11:cee25a834751 3766 }
wolfSSL 11:cee25a834751 3767 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 3768 #endif /* WOLFSSL_AESNI */
wolfSSL 11:cee25a834751 3769
wolfSSL 11:cee25a834751 3770
wolfSSL 11:cee25a834751 3771 #if defined(GCM_SMALL)
wolfSSL 11:cee25a834751 3772 static void GMULT(byte* X, byte* Y)
wolfSSL 11:cee25a834751 3773 {
wolfSSL 11:cee25a834751 3774 byte Z[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 3775 byte V[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 3776 int i, j;
wolfSSL 11:cee25a834751 3777
wolfSSL 11:cee25a834751 3778 XMEMSET(Z, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3779 XMEMCPY(V, X, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3780 for (i = 0; i < AES_BLOCK_SIZE; i++)
wolfSSL 11:cee25a834751 3781 {
wolfSSL 11:cee25a834751 3782 byte y = Y[i];
wolfSSL 11:cee25a834751 3783 for (j = 0; j < 8; j++)
wolfSSL 11:cee25a834751 3784 {
wolfSSL 11:cee25a834751 3785 if (y & 0x80) {
wolfSSL 11:cee25a834751 3786 xorbuf(Z, V, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3787 }
wolfSSL 11:cee25a834751 3788
wolfSSL 11:cee25a834751 3789 RIGHTSHIFTX(V);
wolfSSL 11:cee25a834751 3790 y = y << 1;
wolfSSL 11:cee25a834751 3791 }
wolfSSL 11:cee25a834751 3792 }
wolfSSL 11:cee25a834751 3793 XMEMCPY(X, Z, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3794 }
wolfSSL 11:cee25a834751 3795
wolfSSL 11:cee25a834751 3796
wolfSSL 11:cee25a834751 3797 static void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
wolfSSL 11:cee25a834751 3798 word32 cSz, byte* s, word32 sSz)
wolfSSL 11:cee25a834751 3799 {
wolfSSL 11:cee25a834751 3800 byte x[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 3801 byte scratch[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 3802 word32 blocks, partial;
wolfSSL 11:cee25a834751 3803 byte* h = aes->H;
wolfSSL 11:cee25a834751 3804
wolfSSL 11:cee25a834751 3805 XMEMSET(x, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3806
wolfSSL 11:cee25a834751 3807 /* Hash in A, the Additional Authentication Data */
wolfSSL 11:cee25a834751 3808 if (aSz != 0 && a != NULL) {
wolfSSL 11:cee25a834751 3809 blocks = aSz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3810 partial = aSz % AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3811 while (blocks--) {
wolfSSL 11:cee25a834751 3812 xorbuf(x, a, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3813 GMULT(x, h);
wolfSSL 11:cee25a834751 3814 a += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3815 }
wolfSSL 11:cee25a834751 3816 if (partial != 0) {
wolfSSL 11:cee25a834751 3817 XMEMSET(scratch, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3818 XMEMCPY(scratch, a, partial);
wolfSSL 11:cee25a834751 3819 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3820 GMULT(x, h);
wolfSSL 11:cee25a834751 3821 }
wolfSSL 11:cee25a834751 3822 }
wolfSSL 11:cee25a834751 3823
wolfSSL 11:cee25a834751 3824 /* Hash in C, the Ciphertext */
wolfSSL 11:cee25a834751 3825 if (cSz != 0 && c != NULL) {
wolfSSL 11:cee25a834751 3826 blocks = cSz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3827 partial = cSz % AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3828 while (blocks--) {
wolfSSL 11:cee25a834751 3829 xorbuf(x, c, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3830 GMULT(x, h);
wolfSSL 11:cee25a834751 3831 c += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3832 }
wolfSSL 11:cee25a834751 3833 if (partial != 0) {
wolfSSL 11:cee25a834751 3834 XMEMSET(scratch, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3835 XMEMCPY(scratch, c, partial);
wolfSSL 11:cee25a834751 3836 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3837 GMULT(x, h);
wolfSSL 11:cee25a834751 3838 }
wolfSSL 11:cee25a834751 3839 }
wolfSSL 11:cee25a834751 3840
wolfSSL 11:cee25a834751 3841 /* Hash in the lengths of A and C in bits */
wolfSSL 11:cee25a834751 3842 FlattenSzInBits(&scratch[0], aSz);
wolfSSL 11:cee25a834751 3843 FlattenSzInBits(&scratch[8], cSz);
wolfSSL 11:cee25a834751 3844 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3845 GMULT(x, h);
wolfSSL 11:cee25a834751 3846
wolfSSL 11:cee25a834751 3847 /* Copy the result into s. */
wolfSSL 11:cee25a834751 3848 XMEMCPY(s, x, sSz);
wolfSSL 11:cee25a834751 3849 }
wolfSSL 11:cee25a834751 3850
wolfSSL 11:cee25a834751 3851 /* end GCM_SMALL */
wolfSSL 11:cee25a834751 3852 #elif defined(GCM_TABLE)
wolfSSL 11:cee25a834751 3853
wolfSSL 11:cee25a834751 3854 static const byte R[256][2] = {
wolfSSL 11:cee25a834751 3855 {0x00, 0x00}, {0x01, 0xc2}, {0x03, 0x84}, {0x02, 0x46},
wolfSSL 11:cee25a834751 3856 {0x07, 0x08}, {0x06, 0xca}, {0x04, 0x8c}, {0x05, 0x4e},
wolfSSL 11:cee25a834751 3857 {0x0e, 0x10}, {0x0f, 0xd2}, {0x0d, 0x94}, {0x0c, 0x56},
wolfSSL 11:cee25a834751 3858 {0x09, 0x18}, {0x08, 0xda}, {0x0a, 0x9c}, {0x0b, 0x5e},
wolfSSL 11:cee25a834751 3859 {0x1c, 0x20}, {0x1d, 0xe2}, {0x1f, 0xa4}, {0x1e, 0x66},
wolfSSL 11:cee25a834751 3860 {0x1b, 0x28}, {0x1a, 0xea}, {0x18, 0xac}, {0x19, 0x6e},
wolfSSL 11:cee25a834751 3861 {0x12, 0x30}, {0x13, 0xf2}, {0x11, 0xb4}, {0x10, 0x76},
wolfSSL 11:cee25a834751 3862 {0x15, 0x38}, {0x14, 0xfa}, {0x16, 0xbc}, {0x17, 0x7e},
wolfSSL 11:cee25a834751 3863 {0x38, 0x40}, {0x39, 0x82}, {0x3b, 0xc4}, {0x3a, 0x06},
wolfSSL 11:cee25a834751 3864 {0x3f, 0x48}, {0x3e, 0x8a}, {0x3c, 0xcc}, {0x3d, 0x0e},
wolfSSL 11:cee25a834751 3865 {0x36, 0x50}, {0x37, 0x92}, {0x35, 0xd4}, {0x34, 0x16},
wolfSSL 11:cee25a834751 3866 {0x31, 0x58}, {0x30, 0x9a}, {0x32, 0xdc}, {0x33, 0x1e},
wolfSSL 11:cee25a834751 3867 {0x24, 0x60}, {0x25, 0xa2}, {0x27, 0xe4}, {0x26, 0x26},
wolfSSL 11:cee25a834751 3868 {0x23, 0x68}, {0x22, 0xaa}, {0x20, 0xec}, {0x21, 0x2e},
wolfSSL 11:cee25a834751 3869 {0x2a, 0x70}, {0x2b, 0xb2}, {0x29, 0xf4}, {0x28, 0x36},
wolfSSL 11:cee25a834751 3870 {0x2d, 0x78}, {0x2c, 0xba}, {0x2e, 0xfc}, {0x2f, 0x3e},
wolfSSL 11:cee25a834751 3871 {0x70, 0x80}, {0x71, 0x42}, {0x73, 0x04}, {0x72, 0xc6},
wolfSSL 11:cee25a834751 3872 {0x77, 0x88}, {0x76, 0x4a}, {0x74, 0x0c}, {0x75, 0xce},
wolfSSL 11:cee25a834751 3873 {0x7e, 0x90}, {0x7f, 0x52}, {0x7d, 0x14}, {0x7c, 0xd6},
wolfSSL 11:cee25a834751 3874 {0x79, 0x98}, {0x78, 0x5a}, {0x7a, 0x1c}, {0x7b, 0xde},
wolfSSL 11:cee25a834751 3875 {0x6c, 0xa0}, {0x6d, 0x62}, {0x6f, 0x24}, {0x6e, 0xe6},
wolfSSL 11:cee25a834751 3876 {0x6b, 0xa8}, {0x6a, 0x6a}, {0x68, 0x2c}, {0x69, 0xee},
wolfSSL 11:cee25a834751 3877 {0x62, 0xb0}, {0x63, 0x72}, {0x61, 0x34}, {0x60, 0xf6},
wolfSSL 11:cee25a834751 3878 {0x65, 0xb8}, {0x64, 0x7a}, {0x66, 0x3c}, {0x67, 0xfe},
wolfSSL 11:cee25a834751 3879 {0x48, 0xc0}, {0x49, 0x02}, {0x4b, 0x44}, {0x4a, 0x86},
wolfSSL 11:cee25a834751 3880 {0x4f, 0xc8}, {0x4e, 0x0a}, {0x4c, 0x4c}, {0x4d, 0x8e},
wolfSSL 11:cee25a834751 3881 {0x46, 0xd0}, {0x47, 0x12}, {0x45, 0x54}, {0x44, 0x96},
wolfSSL 11:cee25a834751 3882 {0x41, 0xd8}, {0x40, 0x1a}, {0x42, 0x5c}, {0x43, 0x9e},
wolfSSL 11:cee25a834751 3883 {0x54, 0xe0}, {0x55, 0x22}, {0x57, 0x64}, {0x56, 0xa6},
wolfSSL 11:cee25a834751 3884 {0x53, 0xe8}, {0x52, 0x2a}, {0x50, 0x6c}, {0x51, 0xae},
wolfSSL 11:cee25a834751 3885 {0x5a, 0xf0}, {0x5b, 0x32}, {0x59, 0x74}, {0x58, 0xb6},
wolfSSL 11:cee25a834751 3886 {0x5d, 0xf8}, {0x5c, 0x3a}, {0x5e, 0x7c}, {0x5f, 0xbe},
wolfSSL 11:cee25a834751 3887 {0xe1, 0x00}, {0xe0, 0xc2}, {0xe2, 0x84}, {0xe3, 0x46},
wolfSSL 11:cee25a834751 3888 {0xe6, 0x08}, {0xe7, 0xca}, {0xe5, 0x8c}, {0xe4, 0x4e},
wolfSSL 11:cee25a834751 3889 {0xef, 0x10}, {0xee, 0xd2}, {0xec, 0x94}, {0xed, 0x56},
wolfSSL 11:cee25a834751 3890 {0xe8, 0x18}, {0xe9, 0xda}, {0xeb, 0x9c}, {0xea, 0x5e},
wolfSSL 11:cee25a834751 3891 {0xfd, 0x20}, {0xfc, 0xe2}, {0xfe, 0xa4}, {0xff, 0x66},
wolfSSL 11:cee25a834751 3892 {0xfa, 0x28}, {0xfb, 0xea}, {0xf9, 0xac}, {0xf8, 0x6e},
wolfSSL 11:cee25a834751 3893 {0xf3, 0x30}, {0xf2, 0xf2}, {0xf0, 0xb4}, {0xf1, 0x76},
wolfSSL 11:cee25a834751 3894 {0xf4, 0x38}, {0xf5, 0xfa}, {0xf7, 0xbc}, {0xf6, 0x7e},
wolfSSL 11:cee25a834751 3895 {0xd9, 0x40}, {0xd8, 0x82}, {0xda, 0xc4}, {0xdb, 0x06},
wolfSSL 11:cee25a834751 3896 {0xde, 0x48}, {0xdf, 0x8a}, {0xdd, 0xcc}, {0xdc, 0x0e},
wolfSSL 11:cee25a834751 3897 {0xd7, 0x50}, {0xd6, 0x92}, {0xd4, 0xd4}, {0xd5, 0x16},
wolfSSL 11:cee25a834751 3898 {0xd0, 0x58}, {0xd1, 0x9a}, {0xd3, 0xdc}, {0xd2, 0x1e},
wolfSSL 11:cee25a834751 3899 {0xc5, 0x60}, {0xc4, 0xa2}, {0xc6, 0xe4}, {0xc7, 0x26},
wolfSSL 11:cee25a834751 3900 {0xc2, 0x68}, {0xc3, 0xaa}, {0xc1, 0xec}, {0xc0, 0x2e},
wolfSSL 11:cee25a834751 3901 {0xcb, 0x70}, {0xca, 0xb2}, {0xc8, 0xf4}, {0xc9, 0x36},
wolfSSL 11:cee25a834751 3902 {0xcc, 0x78}, {0xcd, 0xba}, {0xcf, 0xfc}, {0xce, 0x3e},
wolfSSL 11:cee25a834751 3903 {0x91, 0x80}, {0x90, 0x42}, {0x92, 0x04}, {0x93, 0xc6},
wolfSSL 11:cee25a834751 3904 {0x96, 0x88}, {0x97, 0x4a}, {0x95, 0x0c}, {0x94, 0xce},
wolfSSL 11:cee25a834751 3905 {0x9f, 0x90}, {0x9e, 0x52}, {0x9c, 0x14}, {0x9d, 0xd6},
wolfSSL 11:cee25a834751 3906 {0x98, 0x98}, {0x99, 0x5a}, {0x9b, 0x1c}, {0x9a, 0xde},
wolfSSL 11:cee25a834751 3907 {0x8d, 0xa0}, {0x8c, 0x62}, {0x8e, 0x24}, {0x8f, 0xe6},
wolfSSL 11:cee25a834751 3908 {0x8a, 0xa8}, {0x8b, 0x6a}, {0x89, 0x2c}, {0x88, 0xee},
wolfSSL 11:cee25a834751 3909 {0x83, 0xb0}, {0x82, 0x72}, {0x80, 0x34}, {0x81, 0xf6},
wolfSSL 11:cee25a834751 3910 {0x84, 0xb8}, {0x85, 0x7a}, {0x87, 0x3c}, {0x86, 0xfe},
wolfSSL 11:cee25a834751 3911 {0xa9, 0xc0}, {0xa8, 0x02}, {0xaa, 0x44}, {0xab, 0x86},
wolfSSL 11:cee25a834751 3912 {0xae, 0xc8}, {0xaf, 0x0a}, {0xad, 0x4c}, {0xac, 0x8e},
wolfSSL 11:cee25a834751 3913 {0xa7, 0xd0}, {0xa6, 0x12}, {0xa4, 0x54}, {0xa5, 0x96},
wolfSSL 11:cee25a834751 3914 {0xa0, 0xd8}, {0xa1, 0x1a}, {0xa3, 0x5c}, {0xa2, 0x9e},
wolfSSL 11:cee25a834751 3915 {0xb5, 0xe0}, {0xb4, 0x22}, {0xb6, 0x64}, {0xb7, 0xa6},
wolfSSL 11:cee25a834751 3916 {0xb2, 0xe8}, {0xb3, 0x2a}, {0xb1, 0x6c}, {0xb0, 0xae},
wolfSSL 11:cee25a834751 3917 {0xbb, 0xf0}, {0xba, 0x32}, {0xb8, 0x74}, {0xb9, 0xb6},
wolfSSL 11:cee25a834751 3918 {0xbc, 0xf8}, {0xbd, 0x3a}, {0xbf, 0x7c}, {0xbe, 0xbe} };
wolfSSL 11:cee25a834751 3919
wolfSSL 11:cee25a834751 3920
wolfSSL 11:cee25a834751 3921 static void GMULT(byte *x, byte m[256][AES_BLOCK_SIZE])
wolfSSL 11:cee25a834751 3922 {
wolfSSL 11:cee25a834751 3923 int i, j;
wolfSSL 11:cee25a834751 3924 byte Z[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 3925 byte a;
wolfSSL 11:cee25a834751 3926
wolfSSL 11:cee25a834751 3927 XMEMSET(Z, 0, sizeof(Z));
wolfSSL 11:cee25a834751 3928
wolfSSL 11:cee25a834751 3929 for (i = 15; i > 0; i--) {
wolfSSL 11:cee25a834751 3930 xorbuf(Z, m[x[i]], AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3931 a = Z[15];
wolfSSL 11:cee25a834751 3932
wolfSSL 11:cee25a834751 3933 for (j = 15; j > 0; j--) {
wolfSSL 11:cee25a834751 3934 Z[j] = Z[j-1];
wolfSSL 11:cee25a834751 3935 }
wolfSSL 11:cee25a834751 3936
wolfSSL 11:cee25a834751 3937 Z[0] = R[a][0];
wolfSSL 11:cee25a834751 3938 Z[1] ^= R[a][1];
wolfSSL 11:cee25a834751 3939 }
wolfSSL 11:cee25a834751 3940 xorbuf(Z, m[x[0]], AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3941
wolfSSL 11:cee25a834751 3942 XMEMCPY(x, Z, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3943 }
wolfSSL 11:cee25a834751 3944
wolfSSL 11:cee25a834751 3945
wolfSSL 11:cee25a834751 3946 static void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
wolfSSL 11:cee25a834751 3947 word32 cSz, byte* s, word32 sSz)
wolfSSL 11:cee25a834751 3948 {
wolfSSL 11:cee25a834751 3949 byte x[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 3950 byte scratch[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 3951 word32 blocks, partial;
wolfSSL 11:cee25a834751 3952
wolfSSL 11:cee25a834751 3953 XMEMSET(x, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3954
wolfSSL 11:cee25a834751 3955 /* Hash in A, the Additional Authentication Data */
wolfSSL 11:cee25a834751 3956 if (aSz != 0 && a != NULL) {
wolfSSL 11:cee25a834751 3957 blocks = aSz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3958 partial = aSz % AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3959 while (blocks--) {
wolfSSL 11:cee25a834751 3960 xorbuf(x, a, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3961 GMULT(x, aes->M0);
wolfSSL 11:cee25a834751 3962 a += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3963 }
wolfSSL 11:cee25a834751 3964 if (partial != 0) {
wolfSSL 11:cee25a834751 3965 XMEMSET(scratch, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3966 XMEMCPY(scratch, a, partial);
wolfSSL 11:cee25a834751 3967 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3968 GMULT(x, aes->M0);
wolfSSL 11:cee25a834751 3969 }
wolfSSL 11:cee25a834751 3970 }
wolfSSL 11:cee25a834751 3971
wolfSSL 11:cee25a834751 3972 /* Hash in C, the Ciphertext */
wolfSSL 11:cee25a834751 3973 if (cSz != 0 && c != NULL) {
wolfSSL 11:cee25a834751 3974 blocks = cSz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3975 partial = cSz % AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3976 while (blocks--) {
wolfSSL 11:cee25a834751 3977 xorbuf(x, c, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3978 GMULT(x, aes->M0);
wolfSSL 11:cee25a834751 3979 c += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 3980 }
wolfSSL 11:cee25a834751 3981 if (partial != 0) {
wolfSSL 11:cee25a834751 3982 XMEMSET(scratch, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3983 XMEMCPY(scratch, c, partial);
wolfSSL 11:cee25a834751 3984 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3985 GMULT(x, aes->M0);
wolfSSL 11:cee25a834751 3986 }
wolfSSL 11:cee25a834751 3987 }
wolfSSL 11:cee25a834751 3988
wolfSSL 11:cee25a834751 3989 /* Hash in the lengths of A and C in bits */
wolfSSL 11:cee25a834751 3990 FlattenSzInBits(&scratch[0], aSz);
wolfSSL 11:cee25a834751 3991 FlattenSzInBits(&scratch[8], cSz);
wolfSSL 11:cee25a834751 3992 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 3993 GMULT(x, aes->M0);
wolfSSL 11:cee25a834751 3994
wolfSSL 11:cee25a834751 3995 /* Copy the result into s. */
wolfSSL 11:cee25a834751 3996 XMEMCPY(s, x, sSz);
wolfSSL 11:cee25a834751 3997 }
wolfSSL 11:cee25a834751 3998
wolfSSL 11:cee25a834751 3999 /* end GCM_TABLE */
wolfSSL 11:cee25a834751 4000 #elif defined(WORD64_AVAILABLE) && !defined(GCM_WORD32)
wolfSSL 11:cee25a834751 4001
wolfSSL 11:cee25a834751 4002 #if !defined(FREESCALE_LTC_AES_GCM)
wolfSSL 11:cee25a834751 4003 static void GMULT(word64* X, word64* Y)
wolfSSL 11:cee25a834751 4004 {
wolfSSL 11:cee25a834751 4005 word64 Z[2] = {0,0};
wolfSSL 11:cee25a834751 4006 word64 V[2];
wolfSSL 11:cee25a834751 4007 int i, j;
wolfSSL 11:cee25a834751 4008 V[0] = X[0]; V[1] = X[1];
wolfSSL 11:cee25a834751 4009
wolfSSL 11:cee25a834751 4010 for (i = 0; i < 2; i++)
wolfSSL 11:cee25a834751 4011 {
wolfSSL 11:cee25a834751 4012 word64 y = Y[i];
wolfSSL 11:cee25a834751 4013 for (j = 0; j < 64; j++)
wolfSSL 11:cee25a834751 4014 {
wolfSSL 11:cee25a834751 4015 if (y & 0x8000000000000000ULL) {
wolfSSL 11:cee25a834751 4016 Z[0] ^= V[0];
wolfSSL 11:cee25a834751 4017 Z[1] ^= V[1];
wolfSSL 11:cee25a834751 4018 }
wolfSSL 11:cee25a834751 4019
wolfSSL 11:cee25a834751 4020 if (V[1] & 0x0000000000000001) {
wolfSSL 11:cee25a834751 4021 V[1] >>= 1;
wolfSSL 11:cee25a834751 4022 V[1] |= ((V[0] & 0x0000000000000001) ?
wolfSSL 11:cee25a834751 4023 0x8000000000000000ULL : 0);
wolfSSL 11:cee25a834751 4024 V[0] >>= 1;
wolfSSL 11:cee25a834751 4025 V[0] ^= 0xE100000000000000ULL;
wolfSSL 11:cee25a834751 4026 }
wolfSSL 11:cee25a834751 4027 else {
wolfSSL 11:cee25a834751 4028 V[1] >>= 1;
wolfSSL 11:cee25a834751 4029 V[1] |= ((V[0] & 0x0000000000000001) ?
wolfSSL 11:cee25a834751 4030 0x8000000000000000ULL : 0);
wolfSSL 11:cee25a834751 4031 V[0] >>= 1;
wolfSSL 11:cee25a834751 4032 }
wolfSSL 11:cee25a834751 4033 y <<= 1;
wolfSSL 11:cee25a834751 4034 }
wolfSSL 11:cee25a834751 4035 }
wolfSSL 11:cee25a834751 4036 X[0] = Z[0];
wolfSSL 11:cee25a834751 4037 X[1] = Z[1];
wolfSSL 11:cee25a834751 4038 }
wolfSSL 11:cee25a834751 4039
wolfSSL 11:cee25a834751 4040
wolfSSL 11:cee25a834751 4041 static void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
wolfSSL 11:cee25a834751 4042 word32 cSz, byte* s, word32 sSz)
wolfSSL 11:cee25a834751 4043 {
wolfSSL 11:cee25a834751 4044 word64 x[2] = {0,0};
wolfSSL 11:cee25a834751 4045 word32 blocks, partial;
wolfSSL 11:cee25a834751 4046 word64 bigH[2];
wolfSSL 11:cee25a834751 4047
wolfSSL 11:cee25a834751 4048 XMEMCPY(bigH, aes->H, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4049 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4050 ByteReverseWords64(bigH, bigH, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4051 #endif
wolfSSL 11:cee25a834751 4052
wolfSSL 11:cee25a834751 4053 /* Hash in A, the Additional Authentication Data */
wolfSSL 11:cee25a834751 4054 if (aSz != 0 && a != NULL) {
wolfSSL 11:cee25a834751 4055 word64 bigA[2];
wolfSSL 11:cee25a834751 4056 blocks = aSz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4057 partial = aSz % AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4058 while (blocks--) {
wolfSSL 11:cee25a834751 4059 XMEMCPY(bigA, a, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4060 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4061 ByteReverseWords64(bigA, bigA, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4062 #endif
wolfSSL 11:cee25a834751 4063 x[0] ^= bigA[0];
wolfSSL 11:cee25a834751 4064 x[1] ^= bigA[1];
wolfSSL 11:cee25a834751 4065 GMULT(x, bigH);
wolfSSL 11:cee25a834751 4066 a += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4067 }
wolfSSL 11:cee25a834751 4068 if (partial != 0) {
wolfSSL 11:cee25a834751 4069 XMEMSET(bigA, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4070 XMEMCPY(bigA, a, partial);
wolfSSL 11:cee25a834751 4071 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4072 ByteReverseWords64(bigA, bigA, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4073 #endif
wolfSSL 11:cee25a834751 4074 x[0] ^= bigA[0];
wolfSSL 11:cee25a834751 4075 x[1] ^= bigA[1];
wolfSSL 11:cee25a834751 4076 GMULT(x, bigH);
wolfSSL 11:cee25a834751 4077 }
wolfSSL 11:cee25a834751 4078 }
wolfSSL 11:cee25a834751 4079
wolfSSL 11:cee25a834751 4080 /* Hash in C, the Ciphertext */
wolfSSL 11:cee25a834751 4081 if (cSz != 0 && c != NULL) {
wolfSSL 11:cee25a834751 4082 word64 bigC[2];
wolfSSL 11:cee25a834751 4083 blocks = cSz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4084 partial = cSz % AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4085 while (blocks--) {
wolfSSL 11:cee25a834751 4086 XMEMCPY(bigC, c, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4087 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4088 ByteReverseWords64(bigC, bigC, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4089 #endif
wolfSSL 11:cee25a834751 4090 x[0] ^= bigC[0];
wolfSSL 11:cee25a834751 4091 x[1] ^= bigC[1];
wolfSSL 11:cee25a834751 4092 GMULT(x, bigH);
wolfSSL 11:cee25a834751 4093 c += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4094 }
wolfSSL 11:cee25a834751 4095 if (partial != 0) {
wolfSSL 11:cee25a834751 4096 XMEMSET(bigC, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4097 XMEMCPY(bigC, c, partial);
wolfSSL 11:cee25a834751 4098 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4099 ByteReverseWords64(bigC, bigC, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4100 #endif
wolfSSL 11:cee25a834751 4101 x[0] ^= bigC[0];
wolfSSL 11:cee25a834751 4102 x[1] ^= bigC[1];
wolfSSL 11:cee25a834751 4103 GMULT(x, bigH);
wolfSSL 11:cee25a834751 4104 }
wolfSSL 11:cee25a834751 4105 }
wolfSSL 11:cee25a834751 4106
wolfSSL 11:cee25a834751 4107 /* Hash in the lengths in bits of A and C */
wolfSSL 11:cee25a834751 4108 {
wolfSSL 11:cee25a834751 4109 word64 len[2];
wolfSSL 11:cee25a834751 4110 len[0] = aSz; len[1] = cSz;
wolfSSL 11:cee25a834751 4111
wolfSSL 11:cee25a834751 4112 /* Lengths are in bytes. Convert to bits. */
wolfSSL 11:cee25a834751 4113 len[0] *= 8;
wolfSSL 11:cee25a834751 4114 len[1] *= 8;
wolfSSL 11:cee25a834751 4115
wolfSSL 11:cee25a834751 4116 x[0] ^= len[0];
wolfSSL 11:cee25a834751 4117 x[1] ^= len[1];
wolfSSL 11:cee25a834751 4118 GMULT(x, bigH);
wolfSSL 11:cee25a834751 4119 }
wolfSSL 11:cee25a834751 4120 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4121 ByteReverseWords64(x, x, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4122 #endif
wolfSSL 11:cee25a834751 4123 XMEMCPY(s, x, sSz);
wolfSSL 11:cee25a834751 4124 }
wolfSSL 11:cee25a834751 4125 #endif /* !FREESCALE_LTC_AES_GCM */
wolfSSL 11:cee25a834751 4126
wolfSSL 11:cee25a834751 4127 /* end defined(WORD64_AVAILABLE) && !defined(GCM_WORD32) */
wolfSSL 11:cee25a834751 4128 #else /* GCM_WORD32 */
wolfSSL 11:cee25a834751 4129
wolfSSL 11:cee25a834751 4130 static void GMULT(word32* X, word32* Y)
wolfSSL 11:cee25a834751 4131 {
wolfSSL 11:cee25a834751 4132 word32 Z[4] = {0,0,0,0};
wolfSSL 11:cee25a834751 4133 word32 V[4];
wolfSSL 11:cee25a834751 4134 int i, j;
wolfSSL 11:cee25a834751 4135
wolfSSL 11:cee25a834751 4136 V[0] = X[0]; V[1] = X[1]; V[2] = X[2]; V[3] = X[3];
wolfSSL 11:cee25a834751 4137
wolfSSL 11:cee25a834751 4138 for (i = 0; i < 4; i++)
wolfSSL 11:cee25a834751 4139 {
wolfSSL 11:cee25a834751 4140 word32 y = Y[i];
wolfSSL 11:cee25a834751 4141 for (j = 0; j < 32; j++)
wolfSSL 11:cee25a834751 4142 {
wolfSSL 11:cee25a834751 4143 if (y & 0x80000000) {
wolfSSL 11:cee25a834751 4144 Z[0] ^= V[0];
wolfSSL 11:cee25a834751 4145 Z[1] ^= V[1];
wolfSSL 11:cee25a834751 4146 Z[2] ^= V[2];
wolfSSL 11:cee25a834751 4147 Z[3] ^= V[3];
wolfSSL 11:cee25a834751 4148 }
wolfSSL 11:cee25a834751 4149
wolfSSL 11:cee25a834751 4150 if (V[3] & 0x00000001) {
wolfSSL 11:cee25a834751 4151 V[3] >>= 1;
wolfSSL 11:cee25a834751 4152 V[3] |= ((V[2] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 11:cee25a834751 4153 V[2] >>= 1;
wolfSSL 11:cee25a834751 4154 V[2] |= ((V[1] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 11:cee25a834751 4155 V[1] >>= 1;
wolfSSL 11:cee25a834751 4156 V[1] |= ((V[0] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 11:cee25a834751 4157 V[0] >>= 1;
wolfSSL 11:cee25a834751 4158 V[0] ^= 0xE1000000;
wolfSSL 11:cee25a834751 4159 } else {
wolfSSL 11:cee25a834751 4160 V[3] >>= 1;
wolfSSL 11:cee25a834751 4161 V[3] |= ((V[2] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 11:cee25a834751 4162 V[2] >>= 1;
wolfSSL 11:cee25a834751 4163 V[2] |= ((V[1] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 11:cee25a834751 4164 V[1] >>= 1;
wolfSSL 11:cee25a834751 4165 V[1] |= ((V[0] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 11:cee25a834751 4166 V[0] >>= 1;
wolfSSL 11:cee25a834751 4167 }
wolfSSL 11:cee25a834751 4168 y <<= 1;
wolfSSL 11:cee25a834751 4169 }
wolfSSL 11:cee25a834751 4170 }
wolfSSL 11:cee25a834751 4171 X[0] = Z[0];
wolfSSL 11:cee25a834751 4172 X[1] = Z[1];
wolfSSL 11:cee25a834751 4173 X[2] = Z[2];
wolfSSL 11:cee25a834751 4174 X[3] = Z[3];
wolfSSL 11:cee25a834751 4175 }
wolfSSL 11:cee25a834751 4176
wolfSSL 11:cee25a834751 4177
wolfSSL 11:cee25a834751 4178 static void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
wolfSSL 11:cee25a834751 4179 word32 cSz, byte* s, word32 sSz)
wolfSSL 11:cee25a834751 4180 {
wolfSSL 11:cee25a834751 4181 word32 x[4] = {0,0,0,0};
wolfSSL 11:cee25a834751 4182 word32 blocks, partial;
wolfSSL 11:cee25a834751 4183 word32 bigH[4];
wolfSSL 11:cee25a834751 4184
wolfSSL 11:cee25a834751 4185 XMEMCPY(bigH, aes->H, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4186 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4187 ByteReverseWords(bigH, bigH, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4188 #endif
wolfSSL 11:cee25a834751 4189
wolfSSL 11:cee25a834751 4190 /* Hash in A, the Additional Authentication Data */
wolfSSL 11:cee25a834751 4191 if (aSz != 0 && a != NULL) {
wolfSSL 11:cee25a834751 4192 word32 bigA[4];
wolfSSL 11:cee25a834751 4193 blocks = aSz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4194 partial = aSz % AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4195 while (blocks--) {
wolfSSL 11:cee25a834751 4196 XMEMCPY(bigA, a, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4197 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4198 ByteReverseWords(bigA, bigA, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4199 #endif
wolfSSL 11:cee25a834751 4200 x[0] ^= bigA[0];
wolfSSL 11:cee25a834751 4201 x[1] ^= bigA[1];
wolfSSL 11:cee25a834751 4202 x[2] ^= bigA[2];
wolfSSL 11:cee25a834751 4203 x[3] ^= bigA[3];
wolfSSL 11:cee25a834751 4204 GMULT(x, bigH);
wolfSSL 11:cee25a834751 4205 a += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4206 }
wolfSSL 11:cee25a834751 4207 if (partial != 0) {
wolfSSL 11:cee25a834751 4208 XMEMSET(bigA, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4209 XMEMCPY(bigA, a, partial);
wolfSSL 11:cee25a834751 4210 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4211 ByteReverseWords(bigA, bigA, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4212 #endif
wolfSSL 11:cee25a834751 4213 x[0] ^= bigA[0];
wolfSSL 11:cee25a834751 4214 x[1] ^= bigA[1];
wolfSSL 11:cee25a834751 4215 x[2] ^= bigA[2];
wolfSSL 11:cee25a834751 4216 x[3] ^= bigA[3];
wolfSSL 11:cee25a834751 4217 GMULT(x, bigH);
wolfSSL 11:cee25a834751 4218 }
wolfSSL 11:cee25a834751 4219 }
wolfSSL 11:cee25a834751 4220
wolfSSL 11:cee25a834751 4221 /* Hash in C, the Ciphertext */
wolfSSL 11:cee25a834751 4222 if (cSz != 0 && c != NULL) {
wolfSSL 11:cee25a834751 4223 word32 bigC[4];
wolfSSL 11:cee25a834751 4224 blocks = cSz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4225 partial = cSz % AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4226 while (blocks--) {
wolfSSL 11:cee25a834751 4227 XMEMCPY(bigC, c, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4228 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4229 ByteReverseWords(bigC, bigC, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4230 #endif
wolfSSL 11:cee25a834751 4231 x[0] ^= bigC[0];
wolfSSL 11:cee25a834751 4232 x[1] ^= bigC[1];
wolfSSL 11:cee25a834751 4233 x[2] ^= bigC[2];
wolfSSL 11:cee25a834751 4234 x[3] ^= bigC[3];
wolfSSL 11:cee25a834751 4235 GMULT(x, bigH);
wolfSSL 11:cee25a834751 4236 c += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4237 }
wolfSSL 11:cee25a834751 4238 if (partial != 0) {
wolfSSL 11:cee25a834751 4239 XMEMSET(bigC, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4240 XMEMCPY(bigC, c, partial);
wolfSSL 11:cee25a834751 4241 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4242 ByteReverseWords(bigC, bigC, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4243 #endif
wolfSSL 11:cee25a834751 4244 x[0] ^= bigC[0];
wolfSSL 11:cee25a834751 4245 x[1] ^= bigC[1];
wolfSSL 11:cee25a834751 4246 x[2] ^= bigC[2];
wolfSSL 11:cee25a834751 4247 x[3] ^= bigC[3];
wolfSSL 11:cee25a834751 4248 GMULT(x, bigH);
wolfSSL 11:cee25a834751 4249 }
wolfSSL 11:cee25a834751 4250 }
wolfSSL 11:cee25a834751 4251
wolfSSL 11:cee25a834751 4252 /* Hash in the lengths in bits of A and C */
wolfSSL 11:cee25a834751 4253 {
wolfSSL 11:cee25a834751 4254 word32 len[4];
wolfSSL 11:cee25a834751 4255
wolfSSL 11:cee25a834751 4256 /* Lengths are in bytes. Convert to bits. */
wolfSSL 11:cee25a834751 4257 len[0] = (aSz >> (8*sizeof(aSz) - 3));
wolfSSL 11:cee25a834751 4258 len[1] = aSz << 3;
wolfSSL 11:cee25a834751 4259 len[2] = (cSz >> (8*sizeof(cSz) - 3));
wolfSSL 11:cee25a834751 4260 len[3] = cSz << 3;
wolfSSL 11:cee25a834751 4261
wolfSSL 11:cee25a834751 4262 x[0] ^= len[0];
wolfSSL 11:cee25a834751 4263 x[1] ^= len[1];
wolfSSL 11:cee25a834751 4264 x[2] ^= len[2];
wolfSSL 11:cee25a834751 4265 x[3] ^= len[3];
wolfSSL 11:cee25a834751 4266 GMULT(x, bigH);
wolfSSL 11:cee25a834751 4267 }
wolfSSL 11:cee25a834751 4268 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 11:cee25a834751 4269 ByteReverseWords(x, x, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4270 #endif
wolfSSL 11:cee25a834751 4271 XMEMCPY(s, x, sSz);
wolfSSL 11:cee25a834751 4272 }
wolfSSL 11:cee25a834751 4273
wolfSSL 11:cee25a834751 4274 #endif /* end GCM_WORD32 */
wolfSSL 11:cee25a834751 4275
wolfSSL 11:cee25a834751 4276
wolfSSL 11:cee25a834751 4277 int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
wolfSSL 11:cee25a834751 4278 const byte* iv, word32 ivSz,
wolfSSL 11:cee25a834751 4279 byte* authTag, word32 authTagSz,
wolfSSL 11:cee25a834751 4280 const byte* authIn, word32 authInSz)
wolfSSL 11:cee25a834751 4281 {
wolfSSL 11:cee25a834751 4282 #if defined(FREESCALE_LTC_AES_GCM)
wolfSSL 11:cee25a834751 4283 byte *key;
wolfSSL 11:cee25a834751 4284 uint32_t keySize;
wolfSSL 11:cee25a834751 4285 status_t status;
wolfSSL 11:cee25a834751 4286
wolfSSL 11:cee25a834751 4287 if (authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ) {
wolfSSL 11:cee25a834751 4288 WOLFSSL_MSG("GcmEncrypt authTagSz too small error");
wolfSSL 11:cee25a834751 4289 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4290 }
wolfSSL 11:cee25a834751 4291
wolfSSL 11:cee25a834751 4292 key = (byte*)aes->key;
wolfSSL 11:cee25a834751 4293
wolfSSL 11:cee25a834751 4294 status = wc_AesGetKeySize(aes, &keySize);
wolfSSL 11:cee25a834751 4295 if (status != 0) {
wolfSSL 11:cee25a834751 4296 return status;
wolfSSL 11:cee25a834751 4297 }
wolfSSL 11:cee25a834751 4298
wolfSSL 11:cee25a834751 4299 status = LTC_AES_EncryptTagGcm(LTC_BASE, in, out, sz,
wolfSSL 11:cee25a834751 4300 iv, ivSz, authIn, authInSz, key, keySize, authTag, authTagSz);
wolfSSL 11:cee25a834751 4301
wolfSSL 11:cee25a834751 4302 return (status == kStatus_Success) ? 0 : AES_GCM_AUTH_E;
wolfSSL 11:cee25a834751 4303
wolfSSL 11:cee25a834751 4304 #else /* FREESCALE_LTC_AES_GCM */
wolfSSL 11:cee25a834751 4305
wolfSSL 11:cee25a834751 4306 word32 blocks = sz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4307 word32 partial = sz % AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4308 const byte* p = in;
wolfSSL 11:cee25a834751 4309 byte* c = out;
wolfSSL 11:cee25a834751 4310 byte counter[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4311 byte initialCounter[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4312 byte *ctr;
wolfSSL 11:cee25a834751 4313 byte scratch[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4314
wolfSSL 11:cee25a834751 4315 /* Sanity check for XMEMCPY in GHASH function and local xorbuf call */
wolfSSL 11:cee25a834751 4316 if (authTagSz > AES_BLOCK_SIZE)
wolfSSL 11:cee25a834751 4317 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4318
wolfSSL 11:cee25a834751 4319 if (authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ) {
wolfSSL 11:cee25a834751 4320 WOLFSSL_MSG("GcmEncrypt authTagSz too small error");
wolfSSL 11:cee25a834751 4321 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4322 }
wolfSSL 11:cee25a834751 4323
wolfSSL 11:cee25a834751 4324 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
wolfSSL 11:cee25a834751 4325 /* if async and byte count above threshold */
wolfSSL 11:cee25a834751 4326 if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
wolfSSL 11:cee25a834751 4327 sz >= WC_ASYNC_THRESH_AES_GCM) {
wolfSSL 11:cee25a834751 4328 #if defined(HAVE_CAVIUM)
wolfSSL 11:cee25a834751 4329 /* Not yet supported, contact wolfSSL if interested in using */
wolfSSL 11:cee25a834751 4330 #elif defined(HAVE_INTEL_QA)
wolfSSL 11:cee25a834751 4331 return IntelQaSymAesGcmEncrypt(&aes->asyncDev, out, in, sz,
wolfSSL 11:cee25a834751 4332 aes->asyncKey, aes->keylen, iv, ivSz,
wolfSSL 11:cee25a834751 4333 authTag, authTagSz, authIn, authInSz);
wolfSSL 11:cee25a834751 4334 #else /* WOLFSSL_ASYNC_CRYPT_TEST */
wolfSSL 11:cee25a834751 4335 WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
wolfSSL 11:cee25a834751 4336 if (testDev->type == ASYNC_TEST_NONE) {
wolfSSL 11:cee25a834751 4337 testDev->type = ASYNC_TEST_AES_GCM_ENCRYPT;
wolfSSL 11:cee25a834751 4338 testDev->aes.aes = aes;
wolfSSL 11:cee25a834751 4339 testDev->aes.out = out;
wolfSSL 11:cee25a834751 4340 testDev->aes.in = in;
wolfSSL 11:cee25a834751 4341 testDev->aes.sz = sz;
wolfSSL 11:cee25a834751 4342 testDev->aes.iv = iv;
wolfSSL 11:cee25a834751 4343 testDev->aes.ivSz = ivSz;
wolfSSL 11:cee25a834751 4344 testDev->aes.authTag = authTag;
wolfSSL 11:cee25a834751 4345 testDev->aes.authTagSz = authTagSz;
wolfSSL 11:cee25a834751 4346 testDev->aes.authIn = authIn;
wolfSSL 11:cee25a834751 4347 testDev->aes.authInSz = authInSz;
wolfSSL 11:cee25a834751 4348 }
wolfSSL 11:cee25a834751 4349 #endif
wolfSSL 11:cee25a834751 4350 }
wolfSSL 11:cee25a834751 4351 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 4352
wolfSSL 11:cee25a834751 4353 #ifdef WOLFSSL_AESNI
wolfSSL 11:cee25a834751 4354 if (haveAESNI) {
wolfSSL 11:cee25a834751 4355 AES_GCM_encrypt(in, out, authIn, iv, authTag,
wolfSSL 11:cee25a834751 4356 sz, authInSz, ivSz, (const byte*)aes->key, aes->rounds);
wolfSSL 11:cee25a834751 4357 return 0;
wolfSSL 11:cee25a834751 4358 }
wolfSSL 11:cee25a834751 4359 #endif
wolfSSL 11:cee25a834751 4360
wolfSSL 11:cee25a834751 4361 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 11:cee25a834751 4362 ctr = (char *)aes->iv_ce;
wolfSSL 11:cee25a834751 4363 #else
wolfSSL 11:cee25a834751 4364 ctr = counter;
wolfSSL 11:cee25a834751 4365 #endif
wolfSSL 11:cee25a834751 4366
wolfSSL 11:cee25a834751 4367 XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4368 if (ivSz == NONCE_SZ) {
wolfSSL 11:cee25a834751 4369 XMEMCPY(initialCounter, iv, ivSz);
wolfSSL 11:cee25a834751 4370 initialCounter[AES_BLOCK_SIZE - 1] = 1;
wolfSSL 11:cee25a834751 4371 }
wolfSSL 11:cee25a834751 4372 else {
wolfSSL 11:cee25a834751 4373 GHASH(aes, NULL, 0, iv, ivSz, initialCounter, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4374 }
wolfSSL 11:cee25a834751 4375 XMEMCPY(ctr, initialCounter, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4376
wolfSSL 11:cee25a834751 4377 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 11:cee25a834751 4378 if(blocks)
wolfSSL 11:cee25a834751 4379 wc_AesCrypt(aes, out, in, blocks*AES_BLOCK_SIZE,
wolfSSL 11:cee25a834751 4380 PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_AES_GCM );
wolfSSL 11:cee25a834751 4381 #endif
wolfSSL 11:cee25a834751 4382 while (blocks--) {
wolfSSL 11:cee25a834751 4383 IncrementGcmCounter(ctr);
wolfSSL 11:cee25a834751 4384 #ifndef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 11:cee25a834751 4385 wc_AesEncrypt(aes, ctr, scratch);
wolfSSL 11:cee25a834751 4386 xorbuf(scratch, p, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4387 XMEMCPY(c, scratch, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4388 #endif
wolfSSL 11:cee25a834751 4389 p += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4390 c += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4391 }
wolfSSL 11:cee25a834751 4392
wolfSSL 11:cee25a834751 4393 if (partial != 0) {
wolfSSL 11:cee25a834751 4394 IncrementGcmCounter(ctr);
wolfSSL 11:cee25a834751 4395 wc_AesEncrypt(aes, ctr, scratch);
wolfSSL 11:cee25a834751 4396 xorbuf(scratch, p, partial);
wolfSSL 11:cee25a834751 4397 XMEMCPY(c, scratch, partial);
wolfSSL 11:cee25a834751 4398
wolfSSL 11:cee25a834751 4399 }
wolfSSL 11:cee25a834751 4400
wolfSSL 11:cee25a834751 4401 GHASH(aes, authIn, authInSz, out, sz, authTag, authTagSz);
wolfSSL 11:cee25a834751 4402 wc_AesEncrypt(aes, initialCounter, scratch);
wolfSSL 11:cee25a834751 4403 xorbuf(authTag, scratch, authTagSz);
wolfSSL 11:cee25a834751 4404
wolfSSL 11:cee25a834751 4405 return 0;
wolfSSL 11:cee25a834751 4406 #endif /* FREESCALE_LTC_AES_GCM */
wolfSSL 11:cee25a834751 4407 }
wolfSSL 11:cee25a834751 4408
wolfSSL 11:cee25a834751 4409
wolfSSL 11:cee25a834751 4410 #if defined(HAVE_AES_DECRYPT) || defined(HAVE_AESGCM_DECRYPT)
wolfSSL 11:cee25a834751 4411 int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
wolfSSL 11:cee25a834751 4412 const byte* iv, word32 ivSz,
wolfSSL 11:cee25a834751 4413 const byte* authTag, word32 authTagSz,
wolfSSL 11:cee25a834751 4414 const byte* authIn, word32 authInSz)
wolfSSL 11:cee25a834751 4415 {
wolfSSL 11:cee25a834751 4416 #if defined(FREESCALE_LTC_AES_GCM)
wolfSSL 11:cee25a834751 4417 byte *key;
wolfSSL 11:cee25a834751 4418 uint32_t keySize;
wolfSSL 11:cee25a834751 4419 status_t status;
wolfSSL 11:cee25a834751 4420
wolfSSL 11:cee25a834751 4421 key = (byte*)aes->key;
wolfSSL 11:cee25a834751 4422
wolfSSL 11:cee25a834751 4423 status = wc_AesGetKeySize(aes, &keySize);
wolfSSL 11:cee25a834751 4424 if (status != 0) {
wolfSSL 11:cee25a834751 4425 return status;
wolfSSL 11:cee25a834751 4426 }
wolfSSL 11:cee25a834751 4427
wolfSSL 11:cee25a834751 4428 status = LTC_AES_DecryptTagGcm(LTC_BASE, in, out, sz,
wolfSSL 11:cee25a834751 4429 iv, ivSz, authIn, authInSz, key, keySize, authTag, authTagSz);
wolfSSL 11:cee25a834751 4430
wolfSSL 11:cee25a834751 4431 return (status == kStatus_Success) ? 0 : AES_GCM_AUTH_E;
wolfSSL 11:cee25a834751 4432
wolfSSL 11:cee25a834751 4433 #else /* FREESCALE_LTC_AES_GCM */
wolfSSL 11:cee25a834751 4434
wolfSSL 11:cee25a834751 4435 word32 blocks = sz / AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4436 word32 partial = sz % AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4437 const byte* c = in;
wolfSSL 11:cee25a834751 4438 byte* p = out;
wolfSSL 11:cee25a834751 4439 byte counter[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4440 byte initialCounter[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4441 byte *ctr;
wolfSSL 11:cee25a834751 4442 byte scratch[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4443
wolfSSL 11:cee25a834751 4444 /* argument checks */
wolfSSL 11:cee25a834751 4445 if (aes == NULL || out == NULL || in == NULL || sz == 0 || iv == NULL ||
wolfSSL 11:cee25a834751 4446 authTag == NULL || authIn == NULL || authTagSz > AES_BLOCK_SIZE) {
wolfSSL 11:cee25a834751 4447 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4448 }
wolfSSL 11:cee25a834751 4449
wolfSSL 11:cee25a834751 4450 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
wolfSSL 11:cee25a834751 4451 /* if async and byte count above threshold */
wolfSSL 11:cee25a834751 4452 if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
wolfSSL 11:cee25a834751 4453 sz >= WC_ASYNC_THRESH_AES_GCM) {
wolfSSL 11:cee25a834751 4454 #if defined(HAVE_CAVIUM)
wolfSSL 11:cee25a834751 4455 /* Not yet supported, contact wolfSSL if interested in using */
wolfSSL 11:cee25a834751 4456 #elif defined(HAVE_INTEL_QA)
wolfSSL 11:cee25a834751 4457 return IntelQaSymAesGcmDecrypt(&aes->asyncDev, out, in, sz,
wolfSSL 11:cee25a834751 4458 aes->asyncKey, aes->keylen, iv, ivSz,
wolfSSL 11:cee25a834751 4459 authTag, authTagSz, authIn, authInSz);
wolfSSL 11:cee25a834751 4460 #else /* WOLFSSL_ASYNC_CRYPT_TEST */
wolfSSL 11:cee25a834751 4461 WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
wolfSSL 11:cee25a834751 4462 if (testDev->type == ASYNC_TEST_NONE) {
wolfSSL 11:cee25a834751 4463 testDev->type = ASYNC_TEST_AES_GCM_DECRYPT;
wolfSSL 11:cee25a834751 4464 testDev->aes.aes = aes;
wolfSSL 11:cee25a834751 4465 testDev->aes.out = out;
wolfSSL 11:cee25a834751 4466 testDev->aes.in = in;
wolfSSL 11:cee25a834751 4467 testDev->aes.sz = sz;
wolfSSL 11:cee25a834751 4468 testDev->aes.iv = iv;
wolfSSL 11:cee25a834751 4469 testDev->aes.ivSz = ivSz;
wolfSSL 11:cee25a834751 4470 testDev->aes.authTag = (byte*)authTag;
wolfSSL 11:cee25a834751 4471 testDev->aes.authTagSz = authTagSz;
wolfSSL 11:cee25a834751 4472 testDev->aes.authIn = authIn;
wolfSSL 11:cee25a834751 4473 testDev->aes.authInSz = authInSz;
wolfSSL 11:cee25a834751 4474 return WC_PENDING_E;
wolfSSL 11:cee25a834751 4475 }
wolfSSL 11:cee25a834751 4476 #endif
wolfSSL 11:cee25a834751 4477 }
wolfSSL 11:cee25a834751 4478 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 4479
wolfSSL 11:cee25a834751 4480 #ifdef WOLFSSL_AESNI
wolfSSL 11:cee25a834751 4481 if (haveAESNI) {
wolfSSL 11:cee25a834751 4482 if (AES_GCM_decrypt(in, out, authIn, iv, authTag,
wolfSSL 11:cee25a834751 4483 sz, authInSz, ivSz, (byte*)aes->key, aes->rounds) == 0)
wolfSSL 11:cee25a834751 4484 return AES_GCM_AUTH_E;
wolfSSL 11:cee25a834751 4485 return 0;
wolfSSL 11:cee25a834751 4486 }
wolfSSL 11:cee25a834751 4487 #endif
wolfSSL 11:cee25a834751 4488
wolfSSL 11:cee25a834751 4489 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 11:cee25a834751 4490 ctr = (char *)aes->iv_ce;
wolfSSL 11:cee25a834751 4491 #else
wolfSSL 11:cee25a834751 4492 ctr = counter;
wolfSSL 11:cee25a834751 4493 #endif
wolfSSL 11:cee25a834751 4494
wolfSSL 11:cee25a834751 4495 XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4496 if (ivSz == NONCE_SZ) {
wolfSSL 11:cee25a834751 4497 XMEMCPY(initialCounter, iv, ivSz);
wolfSSL 11:cee25a834751 4498 initialCounter[AES_BLOCK_SIZE - 1] = 1;
wolfSSL 11:cee25a834751 4499 }
wolfSSL 11:cee25a834751 4500 else {
wolfSSL 11:cee25a834751 4501 GHASH(aes, NULL, 0, iv, ivSz, initialCounter, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4502 }
wolfSSL 11:cee25a834751 4503 XMEMCPY(ctr, initialCounter, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4504
wolfSSL 11:cee25a834751 4505 /* Calculate the authTag again using the received auth data and the
wolfSSL 11:cee25a834751 4506 * cipher text. */
wolfSSL 11:cee25a834751 4507 {
wolfSSL 11:cee25a834751 4508 byte Tprime[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4509 byte EKY0[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4510
wolfSSL 11:cee25a834751 4511 GHASH(aes, authIn, authInSz, in, sz, Tprime, sizeof(Tprime));
wolfSSL 11:cee25a834751 4512 wc_AesEncrypt(aes, ctr, EKY0);
wolfSSL 11:cee25a834751 4513 xorbuf(Tprime, EKY0, sizeof(Tprime));
wolfSSL 11:cee25a834751 4514
wolfSSL 11:cee25a834751 4515 if (ConstantCompare(authTag, Tprime, authTagSz) != 0) {
wolfSSL 11:cee25a834751 4516 return AES_GCM_AUTH_E;
wolfSSL 11:cee25a834751 4517 }
wolfSSL 11:cee25a834751 4518 }
wolfSSL 11:cee25a834751 4519
wolfSSL 11:cee25a834751 4520 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 11:cee25a834751 4521 if(blocks)
wolfSSL 11:cee25a834751 4522 wc_AesCrypt(aes, out, in, blocks*AES_BLOCK_SIZE,
wolfSSL 11:cee25a834751 4523 PIC32_DECRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_AES_GCM );
wolfSSL 11:cee25a834751 4524 #endif
wolfSSL 11:cee25a834751 4525
wolfSSL 11:cee25a834751 4526 while (blocks--) {
wolfSSL 11:cee25a834751 4527 IncrementGcmCounter(ctr);
wolfSSL 11:cee25a834751 4528 #ifndef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 11:cee25a834751 4529 wc_AesEncrypt(aes, ctr, scratch);
wolfSSL 11:cee25a834751 4530 xorbuf(scratch, c, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4531 XMEMCPY(p, scratch, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4532 #endif
wolfSSL 11:cee25a834751 4533 p += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4534 c += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4535 }
wolfSSL 11:cee25a834751 4536 if (partial != 0) {
wolfSSL 11:cee25a834751 4537 IncrementGcmCounter(ctr);
wolfSSL 11:cee25a834751 4538 wc_AesEncrypt(aes, ctr, scratch);
wolfSSL 11:cee25a834751 4539 xorbuf(scratch, c, partial);
wolfSSL 11:cee25a834751 4540 XMEMCPY(p, scratch, partial);
wolfSSL 11:cee25a834751 4541 }
wolfSSL 11:cee25a834751 4542 return 0;
wolfSSL 11:cee25a834751 4543 #endif /* FREESCALE_LTC_AES_GCM */
wolfSSL 11:cee25a834751 4544 }
wolfSSL 11:cee25a834751 4545
wolfSSL 11:cee25a834751 4546 #endif /* HAVE_AES_DECRYPT || HAVE_AESGCM_DECRYPT */
wolfSSL 11:cee25a834751 4547
wolfSSL 11:cee25a834751 4548 WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
wolfSSL 11:cee25a834751 4549 {
wolfSSL 11:cee25a834751 4550 return wc_AesGcmSetKey(&gmac->aes, key, len);
wolfSSL 11:cee25a834751 4551 }
wolfSSL 11:cee25a834751 4552
wolfSSL 11:cee25a834751 4553
wolfSSL 11:cee25a834751 4554 WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
wolfSSL 11:cee25a834751 4555 const byte* authIn, word32 authInSz,
wolfSSL 11:cee25a834751 4556 byte* authTag, word32 authTagSz)
wolfSSL 11:cee25a834751 4557 {
wolfSSL 11:cee25a834751 4558 return wc_AesGcmEncrypt(&gmac->aes, NULL, NULL, 0, iv, ivSz,
wolfSSL 11:cee25a834751 4559 authTag, authTagSz, authIn, authInSz);
wolfSSL 11:cee25a834751 4560 }
wolfSSL 11:cee25a834751 4561
wolfSSL 11:cee25a834751 4562 #endif /* HAVE_AESGCM */
wolfSSL 11:cee25a834751 4563
wolfSSL 11:cee25a834751 4564
wolfSSL 11:cee25a834751 4565 #ifdef HAVE_AESCCM
wolfSSL 11:cee25a834751 4566
wolfSSL 11:cee25a834751 4567 #if defined(HAVE_COLDFIRE_SEC)
wolfSSL 11:cee25a834751 4568 #error "Coldfire SEC doesn't currently support AES-CCM mode"
wolfSSL 11:cee25a834751 4569
wolfSSL 11:cee25a834751 4570 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 11:cee25a834751 4571 #error "PIC32MZ doesn't currently support AES-CCM mode"
wolfSSL 11:cee25a834751 4572
wolfSSL 11:cee25a834751 4573 #endif
wolfSSL 11:cee25a834751 4574
wolfSSL 11:cee25a834751 4575 int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
wolfSSL 11:cee25a834751 4576 {
wolfSSL 11:cee25a834751 4577 byte nonce[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4578
wolfSSL 11:cee25a834751 4579 if (!((keySz == 16) || (keySz == 24) || (keySz == 32)))
wolfSSL 11:cee25a834751 4580 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4581
wolfSSL 11:cee25a834751 4582 XMEMSET(nonce, 0, sizeof(nonce));
wolfSSL 11:cee25a834751 4583 return wc_AesSetKey(aes, key, keySz, nonce, AES_ENCRYPTION);
wolfSSL 11:cee25a834751 4584 }
wolfSSL 11:cee25a834751 4585
wolfSSL 11:cee25a834751 4586
wolfSSL 11:cee25a834751 4587 #ifndef FREESCALE_LTC
wolfSSL 11:cee25a834751 4588 static void roll_x(Aes* aes, const byte* in, word32 inSz, byte* out)
wolfSSL 11:cee25a834751 4589 {
wolfSSL 11:cee25a834751 4590 /* process the bulk of the data */
wolfSSL 11:cee25a834751 4591 while (inSz >= AES_BLOCK_SIZE) {
wolfSSL 11:cee25a834751 4592 xorbuf(out, in, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4593 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4594 inSz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4595
wolfSSL 11:cee25a834751 4596 wc_AesEncrypt(aes, out, out);
wolfSSL 11:cee25a834751 4597 }
wolfSSL 11:cee25a834751 4598
wolfSSL 11:cee25a834751 4599 /* process remainder of the data */
wolfSSL 11:cee25a834751 4600 if (inSz > 0) {
wolfSSL 11:cee25a834751 4601 xorbuf(out, in, inSz);
wolfSSL 11:cee25a834751 4602 wc_AesEncrypt(aes, out, out);
wolfSSL 11:cee25a834751 4603 }
wolfSSL 11:cee25a834751 4604 }
wolfSSL 11:cee25a834751 4605
wolfSSL 11:cee25a834751 4606
wolfSSL 11:cee25a834751 4607 static void roll_auth(Aes* aes, const byte* in, word32 inSz, byte* out)
wolfSSL 11:cee25a834751 4608 {
wolfSSL 11:cee25a834751 4609 word32 authLenSz;
wolfSSL 11:cee25a834751 4610 word32 remainder;
wolfSSL 11:cee25a834751 4611
wolfSSL 11:cee25a834751 4612 /* encode the length in */
wolfSSL 11:cee25a834751 4613 if (inSz <= 0xFEFF) {
wolfSSL 11:cee25a834751 4614 authLenSz = 2;
wolfSSL 11:cee25a834751 4615 out[0] ^= ((inSz & 0xFF00) >> 8);
wolfSSL 11:cee25a834751 4616 out[1] ^= (inSz & 0x00FF);
wolfSSL 11:cee25a834751 4617 }
wolfSSL 11:cee25a834751 4618 else if (inSz <= 0xFFFFFFFF) {
wolfSSL 11:cee25a834751 4619 authLenSz = 6;
wolfSSL 11:cee25a834751 4620 out[0] ^= 0xFF; out[1] ^= 0xFE;
wolfSSL 11:cee25a834751 4621 out[2] ^= ((inSz & 0xFF000000) >> 24);
wolfSSL 11:cee25a834751 4622 out[3] ^= ((inSz & 0x00FF0000) >> 16);
wolfSSL 11:cee25a834751 4623 out[4] ^= ((inSz & 0x0000FF00) >> 8);
wolfSSL 11:cee25a834751 4624 out[5] ^= (inSz & 0x000000FF);
wolfSSL 11:cee25a834751 4625 }
wolfSSL 11:cee25a834751 4626 /* Note, the protocol handles auth data up to 2^64, but we are
wolfSSL 11:cee25a834751 4627 * using 32-bit sizes right now, so the bigger data isn't handled
wolfSSL 11:cee25a834751 4628 * else if (inSz <= 0xFFFFFFFFFFFFFFFF) {} */
wolfSSL 11:cee25a834751 4629 else
wolfSSL 11:cee25a834751 4630 return;
wolfSSL 11:cee25a834751 4631
wolfSSL 11:cee25a834751 4632 /* start fill out the rest of the first block */
wolfSSL 11:cee25a834751 4633 remainder = AES_BLOCK_SIZE - authLenSz;
wolfSSL 11:cee25a834751 4634 if (inSz >= remainder) {
wolfSSL 11:cee25a834751 4635 /* plenty of bulk data to fill the remainder of this block */
wolfSSL 11:cee25a834751 4636 xorbuf(out + authLenSz, in, remainder);
wolfSSL 11:cee25a834751 4637 inSz -= remainder;
wolfSSL 11:cee25a834751 4638 in += remainder;
wolfSSL 11:cee25a834751 4639 }
wolfSSL 11:cee25a834751 4640 else {
wolfSSL 11:cee25a834751 4641 /* not enough bulk data, copy what is available, and pad zero */
wolfSSL 11:cee25a834751 4642 xorbuf(out + authLenSz, in, inSz);
wolfSSL 11:cee25a834751 4643 inSz = 0;
wolfSSL 11:cee25a834751 4644 }
wolfSSL 11:cee25a834751 4645 wc_AesEncrypt(aes, out, out);
wolfSSL 11:cee25a834751 4646
wolfSSL 11:cee25a834751 4647 if (inSz > 0)
wolfSSL 11:cee25a834751 4648 roll_x(aes, in, inSz, out);
wolfSSL 11:cee25a834751 4649 }
wolfSSL 11:cee25a834751 4650
wolfSSL 11:cee25a834751 4651
wolfSSL 11:cee25a834751 4652 static INLINE void AesCcmCtrInc(byte* B, word32 lenSz)
wolfSSL 11:cee25a834751 4653 {
wolfSSL 11:cee25a834751 4654 word32 i;
wolfSSL 11:cee25a834751 4655
wolfSSL 11:cee25a834751 4656 for (i = 0; i < lenSz; i++) {
wolfSSL 11:cee25a834751 4657 if (++B[AES_BLOCK_SIZE - 1 - i] != 0) return;
wolfSSL 11:cee25a834751 4658 }
wolfSSL 11:cee25a834751 4659 }
wolfSSL 11:cee25a834751 4660 #endif /* !FREESCALE_LTC */
wolfSSL 11:cee25a834751 4661
wolfSSL 11:cee25a834751 4662 /* return 0 on success */
wolfSSL 11:cee25a834751 4663 int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
wolfSSL 11:cee25a834751 4664 const byte* nonce, word32 nonceSz,
wolfSSL 11:cee25a834751 4665 byte* authTag, word32 authTagSz,
wolfSSL 11:cee25a834751 4666 const byte* authIn, word32 authInSz)
wolfSSL 11:cee25a834751 4667 {
wolfSSL 11:cee25a834751 4668 #ifdef FREESCALE_LTC
wolfSSL 11:cee25a834751 4669 byte *key;
wolfSSL 11:cee25a834751 4670 uint32_t keySize;
wolfSSL 11:cee25a834751 4671 status_t status;
wolfSSL 11:cee25a834751 4672
wolfSSL 11:cee25a834751 4673 key = (byte*)aes->key;
wolfSSL 11:cee25a834751 4674
wolfSSL 11:cee25a834751 4675 status = wc_AesGetKeySize(aes, &keySize);
wolfSSL 11:cee25a834751 4676 if (status != 0) {
wolfSSL 11:cee25a834751 4677 return status;
wolfSSL 11:cee25a834751 4678 }
wolfSSL 11:cee25a834751 4679
wolfSSL 11:cee25a834751 4680 status = LTC_AES_EncryptTagCcm(LTC_BASE, in, out, inSz,
wolfSSL 11:cee25a834751 4681 nonce, nonceSz, authIn, authInSz, key, keySize, authTag, authTagSz);
wolfSSL 11:cee25a834751 4682
wolfSSL 11:cee25a834751 4683 return (kStatus_Success == status) ? 0 : BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4684 #else
wolfSSL 11:cee25a834751 4685 byte A[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4686 byte B[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4687 byte lenSz;
wolfSSL 11:cee25a834751 4688 word32 i;
wolfSSL 11:cee25a834751 4689 byte mask = 0xFF;
wolfSSL 11:cee25a834751 4690 word32 wordSz = (word32)sizeof(word32);
wolfSSL 11:cee25a834751 4691
wolfSSL 11:cee25a834751 4692 /* sanity check on arguments */
wolfSSL 11:cee25a834751 4693 if (aes == NULL || out == NULL || in == NULL || nonce == NULL
wolfSSL 11:cee25a834751 4694 || authTag == NULL || nonceSz < 7 || nonceSz > 13)
wolfSSL 11:cee25a834751 4695 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4696
wolfSSL 11:cee25a834751 4697 XMEMCPY(B+1, nonce, nonceSz);
wolfSSL 11:cee25a834751 4698 lenSz = AES_BLOCK_SIZE - 1 - (byte)nonceSz;
wolfSSL 11:cee25a834751 4699 B[0] = (authInSz > 0 ? 64 : 0)
wolfSSL 11:cee25a834751 4700 + (8 * (((byte)authTagSz - 2) / 2))
wolfSSL 11:cee25a834751 4701 + (lenSz - 1);
wolfSSL 11:cee25a834751 4702 for (i = 0; i < lenSz; i++) {
wolfSSL 11:cee25a834751 4703 if (mask && i >= wordSz)
wolfSSL 11:cee25a834751 4704 mask = 0x00;
wolfSSL 11:cee25a834751 4705 B[AES_BLOCK_SIZE - 1 - i] = (inSz >> ((8 * i) & mask)) & mask;
wolfSSL 11:cee25a834751 4706 }
wolfSSL 11:cee25a834751 4707
wolfSSL 11:cee25a834751 4708 wc_AesEncrypt(aes, B, A);
wolfSSL 11:cee25a834751 4709
wolfSSL 11:cee25a834751 4710 if (authInSz > 0)
wolfSSL 11:cee25a834751 4711 roll_auth(aes, authIn, authInSz, A);
wolfSSL 11:cee25a834751 4712 if (inSz > 0)
wolfSSL 11:cee25a834751 4713 roll_x(aes, in, inSz, A);
wolfSSL 11:cee25a834751 4714 XMEMCPY(authTag, A, authTagSz);
wolfSSL 11:cee25a834751 4715
wolfSSL 11:cee25a834751 4716 B[0] = lenSz - 1;
wolfSSL 11:cee25a834751 4717 for (i = 0; i < lenSz; i++)
wolfSSL 11:cee25a834751 4718 B[AES_BLOCK_SIZE - 1 - i] = 0;
wolfSSL 11:cee25a834751 4719 wc_AesEncrypt(aes, B, A);
wolfSSL 11:cee25a834751 4720 xorbuf(authTag, A, authTagSz);
wolfSSL 11:cee25a834751 4721
wolfSSL 11:cee25a834751 4722 B[15] = 1;
wolfSSL 11:cee25a834751 4723 while (inSz >= AES_BLOCK_SIZE) {
wolfSSL 11:cee25a834751 4724 wc_AesEncrypt(aes, B, A);
wolfSSL 11:cee25a834751 4725 xorbuf(A, in, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4726 XMEMCPY(out, A, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4727
wolfSSL 11:cee25a834751 4728 AesCcmCtrInc(B, lenSz);
wolfSSL 11:cee25a834751 4729 inSz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4730 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4731 out += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4732 }
wolfSSL 11:cee25a834751 4733 if (inSz > 0) {
wolfSSL 11:cee25a834751 4734 wc_AesEncrypt(aes, B, A);
wolfSSL 11:cee25a834751 4735 xorbuf(A, in, inSz);
wolfSSL 11:cee25a834751 4736 XMEMCPY(out, A, inSz);
wolfSSL 11:cee25a834751 4737 }
wolfSSL 11:cee25a834751 4738
wolfSSL 11:cee25a834751 4739 ForceZero(A, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4740 ForceZero(B, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4741
wolfSSL 11:cee25a834751 4742 return 0;
wolfSSL 11:cee25a834751 4743 #endif /* FREESCALE_LTC */
wolfSSL 11:cee25a834751 4744 }
wolfSSL 11:cee25a834751 4745
wolfSSL 11:cee25a834751 4746 #ifdef HAVE_AES_DECRYPT
wolfSSL 11:cee25a834751 4747 int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
wolfSSL 11:cee25a834751 4748 const byte* nonce, word32 nonceSz,
wolfSSL 11:cee25a834751 4749 const byte* authTag, word32 authTagSz,
wolfSSL 11:cee25a834751 4750 const byte* authIn, word32 authInSz)
wolfSSL 11:cee25a834751 4751 {
wolfSSL 11:cee25a834751 4752 #ifdef FREESCALE_LTC
wolfSSL 11:cee25a834751 4753 byte *key;
wolfSSL 11:cee25a834751 4754 uint32_t keySize;
wolfSSL 11:cee25a834751 4755 status_t status;
wolfSSL 11:cee25a834751 4756
wolfSSL 11:cee25a834751 4757 key = (byte*)aes->key;
wolfSSL 11:cee25a834751 4758
wolfSSL 11:cee25a834751 4759 status = wc_AesGetKeySize(aes, &keySize);
wolfSSL 11:cee25a834751 4760 if (status != 0) {
wolfSSL 11:cee25a834751 4761 return status;
wolfSSL 11:cee25a834751 4762 }
wolfSSL 11:cee25a834751 4763
wolfSSL 11:cee25a834751 4764 status = LTC_AES_DecryptTagCcm(LTC_BASE, in, out, inSz,
wolfSSL 11:cee25a834751 4765 nonce, nonceSz, authIn, authInSz, key, keySize, authTag, authTagSz);
wolfSSL 11:cee25a834751 4766
wolfSSL 11:cee25a834751 4767 if (status == kStatus_Success) {
wolfSSL 11:cee25a834751 4768 return 0;
wolfSSL 11:cee25a834751 4769 }
wolfSSL 11:cee25a834751 4770 else {
wolfSSL 11:cee25a834751 4771 XMEMSET(out, 0, inSz);
wolfSSL 11:cee25a834751 4772 return AES_CCM_AUTH_E;
wolfSSL 11:cee25a834751 4773 }
wolfSSL 11:cee25a834751 4774 #else /* FREESCALE_LTC */
wolfSSL 11:cee25a834751 4775
wolfSSL 11:cee25a834751 4776 byte A[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4777 byte B[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4778 byte* o;
wolfSSL 11:cee25a834751 4779 byte lenSz;
wolfSSL 11:cee25a834751 4780 word32 i, oSz;
wolfSSL 11:cee25a834751 4781 int result = 0;
wolfSSL 11:cee25a834751 4782 byte mask = 0xFF;
wolfSSL 11:cee25a834751 4783 word32 wordSz = (word32)sizeof(word32);
wolfSSL 11:cee25a834751 4784
wolfSSL 11:cee25a834751 4785 /* sanity check on arguments */
wolfSSL 11:cee25a834751 4786 if (aes == NULL || out == NULL || in == NULL || nonce == NULL
wolfSSL 11:cee25a834751 4787 || authTag == NULL || nonceSz < 7 || nonceSz > 13)
wolfSSL 11:cee25a834751 4788 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4789
wolfSSL 11:cee25a834751 4790 o = out;
wolfSSL 11:cee25a834751 4791 oSz = inSz;
wolfSSL 11:cee25a834751 4792 XMEMCPY(B+1, nonce, nonceSz);
wolfSSL 11:cee25a834751 4793 lenSz = AES_BLOCK_SIZE - 1 - (byte)nonceSz;
wolfSSL 11:cee25a834751 4794
wolfSSL 11:cee25a834751 4795 B[0] = lenSz - 1;
wolfSSL 11:cee25a834751 4796 for (i = 0; i < lenSz; i++)
wolfSSL 11:cee25a834751 4797 B[AES_BLOCK_SIZE - 1 - i] = 0;
wolfSSL 11:cee25a834751 4798 B[15] = 1;
wolfSSL 11:cee25a834751 4799
wolfSSL 11:cee25a834751 4800 while (oSz >= AES_BLOCK_SIZE) {
wolfSSL 11:cee25a834751 4801 wc_AesEncrypt(aes, B, A);
wolfSSL 11:cee25a834751 4802 xorbuf(A, in, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4803 XMEMCPY(o, A, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4804
wolfSSL 11:cee25a834751 4805 AesCcmCtrInc(B, lenSz);
wolfSSL 11:cee25a834751 4806 oSz -= AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4807 in += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4808 o += AES_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4809 }
wolfSSL 11:cee25a834751 4810 if (inSz > 0) {
wolfSSL 11:cee25a834751 4811 wc_AesEncrypt(aes, B, A);
wolfSSL 11:cee25a834751 4812 xorbuf(A, in, oSz);
wolfSSL 11:cee25a834751 4813 XMEMCPY(o, A, oSz);
wolfSSL 11:cee25a834751 4814 }
wolfSSL 11:cee25a834751 4815
wolfSSL 11:cee25a834751 4816 for (i = 0; i < lenSz; i++)
wolfSSL 11:cee25a834751 4817 B[AES_BLOCK_SIZE - 1 - i] = 0;
wolfSSL 11:cee25a834751 4818 wc_AesEncrypt(aes, B, A);
wolfSSL 11:cee25a834751 4819
wolfSSL 11:cee25a834751 4820 o = out;
wolfSSL 11:cee25a834751 4821 oSz = inSz;
wolfSSL 11:cee25a834751 4822
wolfSSL 11:cee25a834751 4823 B[0] = (authInSz > 0 ? 64 : 0)
wolfSSL 11:cee25a834751 4824 + (8 * (((byte)authTagSz - 2) / 2))
wolfSSL 11:cee25a834751 4825 + (lenSz - 1);
wolfSSL 11:cee25a834751 4826 for (i = 0; i < lenSz; i++) {
wolfSSL 11:cee25a834751 4827 if (mask && i >= wordSz)
wolfSSL 11:cee25a834751 4828 mask = 0x00;
wolfSSL 11:cee25a834751 4829 B[AES_BLOCK_SIZE - 1 - i] = (inSz >> ((8 * i) & mask)) & mask;
wolfSSL 11:cee25a834751 4830 }
wolfSSL 11:cee25a834751 4831
wolfSSL 11:cee25a834751 4832 wc_AesEncrypt(aes, B, A);
wolfSSL 11:cee25a834751 4833
wolfSSL 11:cee25a834751 4834 if (authInSz > 0)
wolfSSL 11:cee25a834751 4835 roll_auth(aes, authIn, authInSz, A);
wolfSSL 11:cee25a834751 4836 if (inSz > 0)
wolfSSL 11:cee25a834751 4837 roll_x(aes, o, oSz, A);
wolfSSL 11:cee25a834751 4838
wolfSSL 11:cee25a834751 4839 B[0] = lenSz - 1;
wolfSSL 11:cee25a834751 4840 for (i = 0; i < lenSz; i++)
wolfSSL 11:cee25a834751 4841 B[AES_BLOCK_SIZE - 1 - i] = 0;
wolfSSL 11:cee25a834751 4842 wc_AesEncrypt(aes, B, B);
wolfSSL 11:cee25a834751 4843 xorbuf(A, B, authTagSz);
wolfSSL 11:cee25a834751 4844
wolfSSL 11:cee25a834751 4845 if (ConstantCompare(A, authTag, authTagSz) != 0) {
wolfSSL 11:cee25a834751 4846 /* If the authTag check fails, don't keep the decrypted data.
wolfSSL 11:cee25a834751 4847 * Unfortunately, you need the decrypted data to calculate the
wolfSSL 11:cee25a834751 4848 * check value. */
wolfSSL 11:cee25a834751 4849 XMEMSET(out, 0, inSz);
wolfSSL 11:cee25a834751 4850 result = AES_CCM_AUTH_E;
wolfSSL 11:cee25a834751 4851 }
wolfSSL 11:cee25a834751 4852
wolfSSL 11:cee25a834751 4853 ForceZero(A, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4854 ForceZero(B, AES_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4855 o = NULL;
wolfSSL 11:cee25a834751 4856
wolfSSL 11:cee25a834751 4857 return result;
wolfSSL 11:cee25a834751 4858 #endif /* FREESCALE_LTC */
wolfSSL 11:cee25a834751 4859 }
wolfSSL 11:cee25a834751 4860 #endif /* HAVE_AES_DECRYPT */
wolfSSL 11:cee25a834751 4861 #endif /* HAVE_AESCCM */
wolfSSL 11:cee25a834751 4862
wolfSSL 11:cee25a834751 4863
wolfSSL 11:cee25a834751 4864 #ifdef HAVE_AES_KEYWRAP
wolfSSL 11:cee25a834751 4865
wolfSSL 11:cee25a834751 4866 /* Initialize key wrap counter with value */
wolfSSL 11:cee25a834751 4867 static INLINE void InitKeyWrapCounter(byte* inOutCtr, word32 value)
wolfSSL 11:cee25a834751 4868 {
wolfSSL 11:cee25a834751 4869 int i;
wolfSSL 11:cee25a834751 4870 word32 bytes;
wolfSSL 11:cee25a834751 4871
wolfSSL 11:cee25a834751 4872 bytes = sizeof(word32);
wolfSSL 11:cee25a834751 4873 for (i = 0; i < (int)sizeof(word32); i++) {
wolfSSL 11:cee25a834751 4874 inOutCtr[i+sizeof(word32)] = (value >> ((bytes - 1) * 8)) & 0xFF;
wolfSSL 11:cee25a834751 4875 bytes--;
wolfSSL 11:cee25a834751 4876 }
wolfSSL 11:cee25a834751 4877 }
wolfSSL 11:cee25a834751 4878
wolfSSL 11:cee25a834751 4879 /* Increment key wrap counter */
wolfSSL 11:cee25a834751 4880 static INLINE void IncrementKeyWrapCounter(byte* inOutCtr)
wolfSSL 11:cee25a834751 4881 {
wolfSSL 11:cee25a834751 4882 int i;
wolfSSL 11:cee25a834751 4883
wolfSSL 11:cee25a834751 4884 /* in network byte order so start at end and work back */
wolfSSL 11:cee25a834751 4885 for (i = KEYWRAP_BLOCK_SIZE - 1; i >= 0; i--) {
wolfSSL 11:cee25a834751 4886 if (++inOutCtr[i]) /* we're done unless we overflow */
wolfSSL 11:cee25a834751 4887 return;
wolfSSL 11:cee25a834751 4888 }
wolfSSL 11:cee25a834751 4889 }
wolfSSL 11:cee25a834751 4890
wolfSSL 11:cee25a834751 4891 /* Decrement key wrap counter */
wolfSSL 11:cee25a834751 4892 static INLINE void DecrementKeyWrapCounter(byte* inOutCtr)
wolfSSL 11:cee25a834751 4893 {
wolfSSL 11:cee25a834751 4894 int i;
wolfSSL 11:cee25a834751 4895
wolfSSL 11:cee25a834751 4896 for (i = KEYWRAP_BLOCK_SIZE - 1; i >= 0; i--) {
wolfSSL 11:cee25a834751 4897 if (--inOutCtr[i] != 0xFF) /* we're done unless we underflow */
wolfSSL 11:cee25a834751 4898 return;
wolfSSL 11:cee25a834751 4899 }
wolfSSL 11:cee25a834751 4900 }
wolfSSL 11:cee25a834751 4901
wolfSSL 11:cee25a834751 4902 /* perform AES key wrap (RFC3394), return out sz on success, negative on err */
wolfSSL 11:cee25a834751 4903 int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
wolfSSL 11:cee25a834751 4904 byte* out, word32 outSz, const byte* iv)
wolfSSL 11:cee25a834751 4905 {
wolfSSL 11:cee25a834751 4906 Aes aes;
wolfSSL 11:cee25a834751 4907 byte* r;
wolfSSL 11:cee25a834751 4908 word32 i;
wolfSSL 11:cee25a834751 4909 int ret, j;
wolfSSL 11:cee25a834751 4910
wolfSSL 11:cee25a834751 4911 byte t[KEYWRAP_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4912 byte tmp[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4913
wolfSSL 11:cee25a834751 4914 /* n must be at least 2, output size is n + 8 bytes */
wolfSSL 11:cee25a834751 4915 if (key == NULL || in == NULL || inSz < 2 ||
wolfSSL 11:cee25a834751 4916 out == NULL || outSz < (inSz + KEYWRAP_BLOCK_SIZE))
wolfSSL 11:cee25a834751 4917 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4918
wolfSSL 11:cee25a834751 4919 /* input must be multiple of 64-bits */
wolfSSL 11:cee25a834751 4920 if (inSz % KEYWRAP_BLOCK_SIZE != 0)
wolfSSL 11:cee25a834751 4921 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4922
wolfSSL 11:cee25a834751 4923 /* user IV is optional */
wolfSSL 11:cee25a834751 4924 if (iv == NULL) {
wolfSSL 11:cee25a834751 4925 XMEMSET(tmp, 0xA6, KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4926 } else {
wolfSSL 11:cee25a834751 4927 XMEMCPY(tmp, iv, KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4928 }
wolfSSL 11:cee25a834751 4929
wolfSSL 11:cee25a834751 4930 r = out + 8;
wolfSSL 11:cee25a834751 4931 XMEMCPY(r, in, inSz);
wolfSSL 11:cee25a834751 4932 XMEMSET(t, 0, sizeof(t));
wolfSSL 11:cee25a834751 4933
wolfSSL 11:cee25a834751 4934 ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_ENCRYPTION);
wolfSSL 11:cee25a834751 4935 if (ret != 0)
wolfSSL 11:cee25a834751 4936 return ret;
wolfSSL 11:cee25a834751 4937
wolfSSL 11:cee25a834751 4938 for (j = 0; j <= 5; j++) {
wolfSSL 11:cee25a834751 4939 for (i = 1; i <= inSz / KEYWRAP_BLOCK_SIZE; i++) {
wolfSSL 11:cee25a834751 4940
wolfSSL 11:cee25a834751 4941 /* load R[i] */
wolfSSL 11:cee25a834751 4942 XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4943
wolfSSL 11:cee25a834751 4944 wc_AesEncryptDirect(&aes, tmp, tmp);
wolfSSL 11:cee25a834751 4945
wolfSSL 11:cee25a834751 4946 /* calculate new A */
wolfSSL 11:cee25a834751 4947 IncrementKeyWrapCounter(t);
wolfSSL 11:cee25a834751 4948 xorbuf(tmp, t, KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4949
wolfSSL 11:cee25a834751 4950 /* save R[i] */
wolfSSL 11:cee25a834751 4951 XMEMCPY(r, tmp + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4952 r += KEYWRAP_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4953 }
wolfSSL 11:cee25a834751 4954 r = out + KEYWRAP_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4955 }
wolfSSL 11:cee25a834751 4956
wolfSSL 11:cee25a834751 4957 /* C[0] = A */
wolfSSL 11:cee25a834751 4958 XMEMCPY(out, tmp, KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4959
wolfSSL 11:cee25a834751 4960 return inSz + KEYWRAP_BLOCK_SIZE;
wolfSSL 11:cee25a834751 4961 }
wolfSSL 11:cee25a834751 4962
wolfSSL 11:cee25a834751 4963 int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
wolfSSL 11:cee25a834751 4964 byte* out, word32 outSz, const byte* iv)
wolfSSL 11:cee25a834751 4965 {
wolfSSL 11:cee25a834751 4966 (void)iv;
wolfSSL 11:cee25a834751 4967
wolfSSL 11:cee25a834751 4968 Aes aes;
wolfSSL 11:cee25a834751 4969 byte* r;
wolfSSL 11:cee25a834751 4970 word32 i, n;
wolfSSL 11:cee25a834751 4971 int ret, j;
wolfSSL 11:cee25a834751 4972
wolfSSL 11:cee25a834751 4973 byte t[KEYWRAP_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4974 byte tmp[AES_BLOCK_SIZE];
wolfSSL 11:cee25a834751 4975
wolfSSL 11:cee25a834751 4976 const byte* expIv;
wolfSSL 11:cee25a834751 4977 const byte defaultIV[] = {
wolfSSL 11:cee25a834751 4978 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6
wolfSSL 11:cee25a834751 4979 };
wolfSSL 11:cee25a834751 4980
wolfSSL 11:cee25a834751 4981 if (key == NULL || in == NULL || inSz < 3 ||
wolfSSL 11:cee25a834751 4982 out == NULL || outSz < (inSz - KEYWRAP_BLOCK_SIZE))
wolfSSL 11:cee25a834751 4983 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4984
wolfSSL 11:cee25a834751 4985 /* input must be multiple of 64-bits */
wolfSSL 11:cee25a834751 4986 if (inSz % KEYWRAP_BLOCK_SIZE != 0)
wolfSSL 11:cee25a834751 4987 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 4988
wolfSSL 11:cee25a834751 4989 /* user IV optional */
wolfSSL 11:cee25a834751 4990 if (iv != NULL) {
wolfSSL 11:cee25a834751 4991 expIv = iv;
wolfSSL 11:cee25a834751 4992 } else {
wolfSSL 11:cee25a834751 4993 expIv = defaultIV;
wolfSSL 11:cee25a834751 4994 }
wolfSSL 11:cee25a834751 4995
wolfSSL 11:cee25a834751 4996 /* A = C[0], R[i] = C[i] */
wolfSSL 11:cee25a834751 4997 XMEMCPY(tmp, in, KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4998 XMEMCPY(out, in + KEYWRAP_BLOCK_SIZE, inSz - KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 4999 XMEMSET(t, 0, sizeof(t));
wolfSSL 11:cee25a834751 5000
wolfSSL 11:cee25a834751 5001 ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_DECRYPTION);
wolfSSL 11:cee25a834751 5002 if (ret != 0)
wolfSSL 11:cee25a834751 5003 return ret;
wolfSSL 11:cee25a834751 5004
wolfSSL 11:cee25a834751 5005 /* initialize counter to 6n */
wolfSSL 11:cee25a834751 5006 n = (inSz - 1) / KEYWRAP_BLOCK_SIZE;
wolfSSL 11:cee25a834751 5007 InitKeyWrapCounter(t, 6 * n);
wolfSSL 11:cee25a834751 5008
wolfSSL 11:cee25a834751 5009 for (j = 5; j >= 0; j--) {
wolfSSL 11:cee25a834751 5010 for (i = n; i >= 1; i--) {
wolfSSL 11:cee25a834751 5011
wolfSSL 11:cee25a834751 5012 /* calculate A */
wolfSSL 11:cee25a834751 5013 xorbuf(tmp, t, KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 5014 DecrementKeyWrapCounter(t);
wolfSSL 11:cee25a834751 5015
wolfSSL 11:cee25a834751 5016 /* load R[i], starting at end of R */
wolfSSL 11:cee25a834751 5017 r = out + ((i - 1) * KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 5018 XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 5019 wc_AesDecryptDirect(&aes, tmp, tmp);
wolfSSL 11:cee25a834751 5020
wolfSSL 11:cee25a834751 5021 /* save R[i] */
wolfSSL 11:cee25a834751 5022 XMEMCPY(r, tmp + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
wolfSSL 11:cee25a834751 5023 }
wolfSSL 11:cee25a834751 5024 }
wolfSSL 11:cee25a834751 5025
wolfSSL 11:cee25a834751 5026 /* verify IV */
wolfSSL 11:cee25a834751 5027 if (XMEMCMP(tmp, expIv, KEYWRAP_BLOCK_SIZE) != 0)
wolfSSL 11:cee25a834751 5028 return BAD_KEYWRAP_IV_E;
wolfSSL 11:cee25a834751 5029
wolfSSL 11:cee25a834751 5030 return inSz - KEYWRAP_BLOCK_SIZE;
wolfSSL 11:cee25a834751 5031 }
wolfSSL 11:cee25a834751 5032
wolfSSL 11:cee25a834751 5033 #endif /* HAVE_AES_KEYWRAP */
wolfSSL 11:cee25a834751 5034
wolfSSL 11:cee25a834751 5035
wolfSSL 11:cee25a834751 5036 /* Initialize Aes for use with async hardware */
wolfSSL 11:cee25a834751 5037 int wc_AesInit(Aes* aes, void* heap, int devId)
wolfSSL 11:cee25a834751 5038 {
wolfSSL 11:cee25a834751 5039 int ret = 0;
wolfSSL 11:cee25a834751 5040
wolfSSL 11:cee25a834751 5041 if (aes == NULL)
wolfSSL 11:cee25a834751 5042 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 5043
wolfSSL 11:cee25a834751 5044 aes->heap = heap;
wolfSSL 11:cee25a834751 5045
wolfSSL 11:cee25a834751 5046 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
wolfSSL 11:cee25a834751 5047 ret = wolfAsync_DevCtxInit(&aes->asyncDev, WOLFSSL_ASYNC_MARKER_AES,
wolfSSL 11:cee25a834751 5048 aes->heap, devId);
wolfSSL 11:cee25a834751 5049 #else
wolfSSL 11:cee25a834751 5050 (void)devId;
wolfSSL 11:cee25a834751 5051 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 5052
wolfSSL 11:cee25a834751 5053 return ret;
wolfSSL 11:cee25a834751 5054 }
wolfSSL 11:cee25a834751 5055
wolfSSL 11:cee25a834751 5056 /* Free Aes from use with async hardware */
wolfSSL 11:cee25a834751 5057 void wc_AesFree(Aes* aes)
wolfSSL 11:cee25a834751 5058 {
wolfSSL 11:cee25a834751 5059 if (aes == NULL)
wolfSSL 11:cee25a834751 5060 return;
wolfSSL 11:cee25a834751 5061
wolfSSL 11:cee25a834751 5062 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
wolfSSL 11:cee25a834751 5063 wolfAsync_DevCtxFree(&aes->asyncDev, WOLFSSL_ASYNC_MARKER_AES);
wolfSSL 11:cee25a834751 5064 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 5065 }
wolfSSL 11:cee25a834751 5066
wolfSSL 11:cee25a834751 5067
wolfSSL 11:cee25a834751 5068 int wc_AesGetKeySize(Aes* aes, word32* keySize)
wolfSSL 11:cee25a834751 5069 {
wolfSSL 11:cee25a834751 5070 int ret = 0;
wolfSSL 11:cee25a834751 5071
wolfSSL 11:cee25a834751 5072 if (aes == NULL || keySize == NULL) {
wolfSSL 11:cee25a834751 5073 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 5074 }
wolfSSL 11:cee25a834751 5075
wolfSSL 11:cee25a834751 5076 switch (aes->rounds) {
wolfSSL 11:cee25a834751 5077 case 10:
wolfSSL 11:cee25a834751 5078 *keySize = 16;
wolfSSL 11:cee25a834751 5079 break;
wolfSSL 11:cee25a834751 5080 case 12:
wolfSSL 11:cee25a834751 5081 *keySize = 24;
wolfSSL 11:cee25a834751 5082 break;
wolfSSL 11:cee25a834751 5083 case 14:
wolfSSL 11:cee25a834751 5084 *keySize = 32;
wolfSSL 11:cee25a834751 5085 break;
wolfSSL 11:cee25a834751 5086 default:
wolfSSL 11:cee25a834751 5087 *keySize = 0;
wolfSSL 11:cee25a834751 5088 ret = BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 5089 }
wolfSSL 11:cee25a834751 5090
wolfSSL 11:cee25a834751 5091 return ret;
wolfSSL 11:cee25a834751 5092 }
wolfSSL 11:cee25a834751 5093
wolfSSL 11:cee25a834751 5094 #endif /* !WOLFSSL_TI_CRYPT */
wolfSSL 11:cee25a834751 5095
wolfSSL 11:cee25a834751 5096 #endif /* HAVE_FIPS */
wolfSSL 11:cee25a834751 5097
wolfSSL 11:cee25a834751 5098 #endif /* NO_AES */
wolfSSL 11:cee25a834751 5099