wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

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