wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 30 06:16:19 2017 +0000
Revision:
13:80fb167dafdf
wolfSSL 3.11.1: TLS1.3 Beta

Who changed what in which revision?

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