wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Fri Jun 26 00:39:20 2015 +0000
Revision:
0:d92f9d21154c
wolfSSL 3.6.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:d92f9d21154c 1 /* aes.c
wolfSSL 0:d92f9d21154c 2 *
wolfSSL 0:d92f9d21154c 3 * Copyright (C) 2006-2015 wolfSSL Inc.
wolfSSL 0:d92f9d21154c 4 *
wolfSSL 0:d92f9d21154c 5 * This file is part of wolfSSL. (formerly known as CyaSSL)
wolfSSL 0:d92f9d21154c 6 *
wolfSSL 0:d92f9d21154c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 0:d92f9d21154c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:d92f9d21154c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:d92f9d21154c 10 * (at your option) any later version.
wolfSSL 0:d92f9d21154c 11 *
wolfSSL 0:d92f9d21154c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 0:d92f9d21154c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:d92f9d21154c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:d92f9d21154c 15 * GNU General Public License for more details.
wolfSSL 0:d92f9d21154c 16 *
wolfSSL 0:d92f9d21154c 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:d92f9d21154c 18 * along with this program; if not, write to the Free Software
wolfSSL 0:d92f9d21154c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:d92f9d21154c 20 */
wolfSSL 0:d92f9d21154c 21
wolfSSL 0:d92f9d21154c 22 #ifdef HAVE_CONFIG_H
wolfSSL 0:d92f9d21154c 23 #include <config.h>
wolfSSL 0:d92f9d21154c 24 #endif
wolfSSL 0:d92f9d21154c 25
wolfSSL 0:d92f9d21154c 26 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 0:d92f9d21154c 27
wolfSSL 0:d92f9d21154c 28 #ifndef NO_AES
wolfSSL 0:d92f9d21154c 29
wolfSSL 0:d92f9d21154c 30 #include <wolfssl/wolfcrypt/aes.h>
wolfSSL 0:d92f9d21154c 31
wolfSSL 0:d92f9d21154c 32 #ifdef HAVE_FIPS
wolfSSL 0:d92f9d21154c 33 int wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
wolfSSL 0:d92f9d21154c 34 int dir)
wolfSSL 0:d92f9d21154c 35 {
wolfSSL 0:d92f9d21154c 36 return AesSetKey_fips(aes, key, len, iv, dir);
wolfSSL 0:d92f9d21154c 37 }
wolfSSL 0:d92f9d21154c 38
wolfSSL 0:d92f9d21154c 39
wolfSSL 0:d92f9d21154c 40 int wc_AesSetIV(Aes* aes, const byte* iv)
wolfSSL 0:d92f9d21154c 41 {
wolfSSL 0:d92f9d21154c 42 return AesSetIV_fips(aes, iv);
wolfSSL 0:d92f9d21154c 43 }
wolfSSL 0:d92f9d21154c 44
wolfSSL 0:d92f9d21154c 45
wolfSSL 0:d92f9d21154c 46 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 47 {
wolfSSL 0:d92f9d21154c 48 return AesCbcEncrypt_fips(aes, out, in, sz);
wolfSSL 0:d92f9d21154c 49 }
wolfSSL 0:d92f9d21154c 50
wolfSSL 0:d92f9d21154c 51
wolfSSL 0:d92f9d21154c 52 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 53 {
wolfSSL 0:d92f9d21154c 54 return AesCbcDecrypt_fips(aes, out, in, sz);
wolfSSL 0:d92f9d21154c 55 }
wolfSSL 0:d92f9d21154c 56
wolfSSL 0:d92f9d21154c 57
wolfSSL 0:d92f9d21154c 58 int wc_AesCbcDecryptWithKey(byte* out, const byte* in, word32 inSz,
wolfSSL 0:d92f9d21154c 59 const byte* key, word32 keySz, const byte* iv)
wolfSSL 0:d92f9d21154c 60 {
wolfSSL 0:d92f9d21154c 61 return AesCbcDecryptWithKey(out, in, inSz, key, keySz, iv);
wolfSSL 0:d92f9d21154c 62 }
wolfSSL 0:d92f9d21154c 63
wolfSSL 0:d92f9d21154c 64
wolfSSL 0:d92f9d21154c 65 /* AES-CTR */
wolfSSL 0:d92f9d21154c 66 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 0:d92f9d21154c 67 void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 68 {
wolfSSL 0:d92f9d21154c 69 AesCtrEncrypt(aes, out, in, sz);
wolfSSL 0:d92f9d21154c 70 }
wolfSSL 0:d92f9d21154c 71 #endif
wolfSSL 0:d92f9d21154c 72
wolfSSL 0:d92f9d21154c 73 /* AES-DIRECT */
wolfSSL 0:d92f9d21154c 74 #if defined(WOLFSSL_AES_DIRECT)
wolfSSL 0:d92f9d21154c 75 void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 0:d92f9d21154c 76 {
wolfSSL 0:d92f9d21154c 77 AesEncryptDirect(aes, out, in);
wolfSSL 0:d92f9d21154c 78 }
wolfSSL 0:d92f9d21154c 79
wolfSSL 0:d92f9d21154c 80
wolfSSL 0:d92f9d21154c 81 void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 0:d92f9d21154c 82 {
wolfSSL 0:d92f9d21154c 83 AesDecryptDirect(aes, out, in);
wolfSSL 0:d92f9d21154c 84 }
wolfSSL 0:d92f9d21154c 85
wolfSSL 0:d92f9d21154c 86
wolfSSL 0:d92f9d21154c 87 int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
wolfSSL 0:d92f9d21154c 88 const byte* iv, int dir)
wolfSSL 0:d92f9d21154c 89 {
wolfSSL 0:d92f9d21154c 90 return AesSetKeyDirect(aes, key, len, iv, dir);
wolfSSL 0:d92f9d21154c 91 }
wolfSSL 0:d92f9d21154c 92 #endif
wolfSSL 0:d92f9d21154c 93
wolfSSL 0:d92f9d21154c 94
wolfSSL 0:d92f9d21154c 95 #ifdef HAVE_AESGCM
wolfSSL 0:d92f9d21154c 96 int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
wolfSSL 0:d92f9d21154c 97 {
wolfSSL 0:d92f9d21154c 98 return AesGcmSetKey_fips(aes, key, len);
wolfSSL 0:d92f9d21154c 99 }
wolfSSL 0:d92f9d21154c 100
wolfSSL 0:d92f9d21154c 101
wolfSSL 0:d92f9d21154c 102 int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
wolfSSL 0:d92f9d21154c 103 const byte* iv, word32 ivSz,
wolfSSL 0:d92f9d21154c 104 byte* authTag, word32 authTagSz,
wolfSSL 0:d92f9d21154c 105 const byte* authIn, word32 authInSz)
wolfSSL 0:d92f9d21154c 106 {
wolfSSL 0:d92f9d21154c 107 return AesGcmEncrypt_fips(aes, out, in, sz, iv, ivSz, authTag, authTagSz,
wolfSSL 0:d92f9d21154c 108 authIn, authInSz);
wolfSSL 0:d92f9d21154c 109 }
wolfSSL 0:d92f9d21154c 110
wolfSSL 0:d92f9d21154c 111
wolfSSL 0:d92f9d21154c 112 int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
wolfSSL 0:d92f9d21154c 113 const byte* iv, word32 ivSz,
wolfSSL 0:d92f9d21154c 114 const byte* authTag, word32 authTagSz,
wolfSSL 0:d92f9d21154c 115 const byte* authIn, word32 authInSz)
wolfSSL 0:d92f9d21154c 116 {
wolfSSL 0:d92f9d21154c 117 return AesGcmDecrypt_fips(aes, out, in, sz, iv, ivSz, authTag, authTagSz,
wolfSSL 0:d92f9d21154c 118 authIn, authInSz);
wolfSSL 0:d92f9d21154c 119 }
wolfSSL 0:d92f9d21154c 120
wolfSSL 0:d92f9d21154c 121
wolfSSL 0:d92f9d21154c 122 int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
wolfSSL 0:d92f9d21154c 123 {
wolfSSL 0:d92f9d21154c 124 return GmacSetKey(gmac, key, len);
wolfSSL 0:d92f9d21154c 125 }
wolfSSL 0:d92f9d21154c 126
wolfSSL 0:d92f9d21154c 127
wolfSSL 0:d92f9d21154c 128 int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
wolfSSL 0:d92f9d21154c 129 const byte* authIn, word32 authInSz,
wolfSSL 0:d92f9d21154c 130 byte* authTag, word32 authTagSz)
wolfSSL 0:d92f9d21154c 131 {
wolfSSL 0:d92f9d21154c 132 return GmacUpdate(gmac, iv, ivSz, authIn, authInSz,
wolfSSL 0:d92f9d21154c 133 authTag, authTagSz);
wolfSSL 0:d92f9d21154c 134 }
wolfSSL 0:d92f9d21154c 135
wolfSSL 0:d92f9d21154c 136 #endif /* HAVE_AESGCM */
wolfSSL 0:d92f9d21154c 137 #ifdef HAVE_AESCCM
wolfSSL 0:d92f9d21154c 138 void wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
wolfSSL 0:d92f9d21154c 139 {
wolfSSL 0:d92f9d21154c 140 AesCcmSetKey(aes, key, keySz);
wolfSSL 0:d92f9d21154c 141 }
wolfSSL 0:d92f9d21154c 142
wolfSSL 0:d92f9d21154c 143
wolfSSL 0:d92f9d21154c 144 void wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
wolfSSL 0:d92f9d21154c 145 const byte* nonce, word32 nonceSz,
wolfSSL 0:d92f9d21154c 146 byte* authTag, word32 authTagSz,
wolfSSL 0:d92f9d21154c 147 const byte* authIn, word32 authInSz)
wolfSSL 0:d92f9d21154c 148 {
wolfSSL 0:d92f9d21154c 149 AesCcmEncrypt(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz,
wolfSSL 0:d92f9d21154c 150 authIn, authInSz);
wolfSSL 0:d92f9d21154c 151 }
wolfSSL 0:d92f9d21154c 152
wolfSSL 0:d92f9d21154c 153
wolfSSL 0:d92f9d21154c 154 int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
wolfSSL 0:d92f9d21154c 155 const byte* nonce, word32 nonceSz,
wolfSSL 0:d92f9d21154c 156 const byte* authTag, word32 authTagSz,
wolfSSL 0:d92f9d21154c 157 const byte* authIn, word32 authInSz)
wolfSSL 0:d92f9d21154c 158 {
wolfSSL 0:d92f9d21154c 159 return AesCcmDecrypt(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz,
wolfSSL 0:d92f9d21154c 160 authIn, authInSz);
wolfSSL 0:d92f9d21154c 161 }
wolfSSL 0:d92f9d21154c 162 #endif /* HAVE_AESCCM */
wolfSSL 0:d92f9d21154c 163
wolfSSL 0:d92f9d21154c 164 #ifdef HAVE_CAVIUM
wolfSSL 0:d92f9d21154c 165 int wc_AesInitCavium(Aes* aes, int i)
wolfSSL 0:d92f9d21154c 166 {
wolfSSL 0:d92f9d21154c 167 return AesInitCavium(aes, i);
wolfSSL 0:d92f9d21154c 168 }
wolfSSL 0:d92f9d21154c 169
wolfSSL 0:d92f9d21154c 170
wolfSSL 0:d92f9d21154c 171 void wc_AesFreeCavium(Aes* aes)
wolfSSL 0:d92f9d21154c 172 {
wolfSSL 0:d92f9d21154c 173 AesFreeCavium(aes);
wolfSSL 0:d92f9d21154c 174 }
wolfSSL 0:d92f9d21154c 175 #endif
wolfSSL 0:d92f9d21154c 176 #else /* HAVE_FIPS */
wolfSSL 0:d92f9d21154c 177
wolfSSL 0:d92f9d21154c 178 #ifdef WOLFSSL_TI_CRYPT
wolfSSL 0:d92f9d21154c 179 #include <wolfcrypt/src/port/ti/ti-aes.c>
wolfSSL 0:d92f9d21154c 180 #else
wolfSSL 0:d92f9d21154c 181
wolfSSL 0:d92f9d21154c 182 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 0:d92f9d21154c 183 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 0:d92f9d21154c 184 #ifdef NO_INLINE
wolfSSL 0:d92f9d21154c 185 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 0:d92f9d21154c 186 #else
wolfSSL 0:d92f9d21154c 187 #include <wolfcrypt/src/misc.c>
wolfSSL 0:d92f9d21154c 188 #endif
wolfSSL 0:d92f9d21154c 189 #ifdef DEBUG_AESNI
wolfSSL 0:d92f9d21154c 190 #include <stdio.h>
wolfSSL 0:d92f9d21154c 191 #endif
wolfSSL 0:d92f9d21154c 192
wolfSSL 0:d92f9d21154c 193
wolfSSL 0:d92f9d21154c 194 #ifdef _MSC_VER
wolfSSL 0:d92f9d21154c 195 /* 4127 warning constant while(1) */
wolfSSL 0:d92f9d21154c 196 #pragma warning(disable: 4127)
wolfSSL 0:d92f9d21154c 197 #endif
wolfSSL 0:d92f9d21154c 198
wolfSSL 0:d92f9d21154c 199
wolfSSL 0:d92f9d21154c 200 #if defined(STM32F2_CRYPTO)
wolfSSL 0:d92f9d21154c 201 /* STM32F2 hardware AES support for CBC, CTR modes through the STM32F2
wolfSSL 0:d92f9d21154c 202 * Standard Peripheral Library. Documentation located in STM32F2xx
wolfSSL 0:d92f9d21154c 203 * Standard Peripheral Library document (See note in README).
wolfSSL 0:d92f9d21154c 204 * NOTE: no support for AES-GCM/CCM/Direct */
wolfSSL 0:d92f9d21154c 205 #include "stm32f2xx.h"
wolfSSL 0:d92f9d21154c 206 #include "stm32f2xx_cryp.h"
wolfSSL 0:d92f9d21154c 207 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 0:d92f9d21154c 208 /* Freescale Coldfire SEC support for CBC mode.
wolfSSL 0:d92f9d21154c 209 * NOTE: no support for AES-CTR/GCM/CCM/Direct */
wolfSSL 0:d92f9d21154c 210 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 0:d92f9d21154c 211 #include "sec.h"
wolfSSL 0:d92f9d21154c 212 #include "mcf5475_sec.h"
wolfSSL 0:d92f9d21154c 213 #include "mcf5475_siu.h"
wolfSSL 0:d92f9d21154c 214 #elif defined(FREESCALE_MMCAU)
wolfSSL 0:d92f9d21154c 215 /* Freescale mmCAU hardware AES support for Direct, CBC, CCM, GCM modes
wolfSSL 0:d92f9d21154c 216 * through the CAU/mmCAU library. Documentation located in
wolfSSL 0:d92f9d21154c 217 * ColdFire/ColdFire+ CAU and Kinetis mmCAU Software Library User
wolfSSL 0:d92f9d21154c 218 * Guide (See note in README).
wolfSSL 0:d92f9d21154c 219 * NOTE: no support for AES-CTR */
wolfSSL 0:d92f9d21154c 220 #include "cau_api.h"
wolfSSL 0:d92f9d21154c 221 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 0:d92f9d21154c 222 /* NOTE: no support for AES-CCM/Direct */
wolfSSL 0:d92f9d21154c 223 #define DEBUG_WOLFSSL
wolfSSL 0:d92f9d21154c 224 #include "wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h"
wolfSSL 0:d92f9d21154c 225 #elif defined(HAVE_CAVIUM)
wolfSSL 0:d92f9d21154c 226 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 0:d92f9d21154c 227 #include "cavium_common.h"
wolfSSL 0:d92f9d21154c 228
wolfSSL 0:d92f9d21154c 229 /* still leave SW crypto available */
wolfSSL 0:d92f9d21154c 230 #define NEED_AES_TABLES
wolfSSL 0:d92f9d21154c 231
wolfSSL 0:d92f9d21154c 232 static int wc_AesCaviumSetKey(Aes* aes, const byte* key, word32 length,
wolfSSL 0:d92f9d21154c 233 const byte* iv);
wolfSSL 0:d92f9d21154c 234 static int wc_AesCaviumCbcEncrypt(Aes* aes, byte* out, const byte* in,
wolfSSL 0:d92f9d21154c 235 word32 length);
wolfSSL 0:d92f9d21154c 236 static int wc_AesCaviumCbcDecrypt(Aes* aes, byte* out, const byte* in,
wolfSSL 0:d92f9d21154c 237 word32 length);
wolfSSL 0:d92f9d21154c 238 #else
wolfSSL 0:d92f9d21154c 239 /* using CTaoCrypt software AES implementation */
wolfSSL 0:d92f9d21154c 240 #define NEED_AES_TABLES
wolfSSL 0:d92f9d21154c 241 #endif /* STM32F2_CRYPTO */
wolfSSL 0:d92f9d21154c 242
wolfSSL 0:d92f9d21154c 243
wolfSSL 0:d92f9d21154c 244 #ifdef NEED_AES_TABLES
wolfSSL 0:d92f9d21154c 245
wolfSSL 0:d92f9d21154c 246 static const word32 rcon[] = {
wolfSSL 0:d92f9d21154c 247 0x01000000, 0x02000000, 0x04000000, 0x08000000,
wolfSSL 0:d92f9d21154c 248 0x10000000, 0x20000000, 0x40000000, 0x80000000,
wolfSSL 0:d92f9d21154c 249 0x1B000000, 0x36000000,
wolfSSL 0:d92f9d21154c 250 /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
wolfSSL 0:d92f9d21154c 251 };
wolfSSL 0:d92f9d21154c 252
wolfSSL 0:d92f9d21154c 253 static const word32 Te[5][256] = {
wolfSSL 0:d92f9d21154c 254 {
wolfSSL 0:d92f9d21154c 255 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
wolfSSL 0:d92f9d21154c 256 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
wolfSSL 0:d92f9d21154c 257 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
wolfSSL 0:d92f9d21154c 258 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU,
wolfSSL 0:d92f9d21154c 259 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U,
wolfSSL 0:d92f9d21154c 260 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU,
wolfSSL 0:d92f9d21154c 261 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU,
wolfSSL 0:d92f9d21154c 262 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU,
wolfSSL 0:d92f9d21154c 263 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU,
wolfSSL 0:d92f9d21154c 264 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU,
wolfSSL 0:d92f9d21154c 265 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U,
wolfSSL 0:d92f9d21154c 266 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU,
wolfSSL 0:d92f9d21154c 267 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU,
wolfSSL 0:d92f9d21154c 268 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U,
wolfSSL 0:d92f9d21154c 269 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU,
wolfSSL 0:d92f9d21154c 270 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU,
wolfSSL 0:d92f9d21154c 271 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU,
wolfSSL 0:d92f9d21154c 272 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU,
wolfSSL 0:d92f9d21154c 273 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU,
wolfSSL 0:d92f9d21154c 274 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U,
wolfSSL 0:d92f9d21154c 275 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU,
wolfSSL 0:d92f9d21154c 276 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU,
wolfSSL 0:d92f9d21154c 277 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU,
wolfSSL 0:d92f9d21154c 278 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU,
wolfSSL 0:d92f9d21154c 279 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U,
wolfSSL 0:d92f9d21154c 280 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U,
wolfSSL 0:d92f9d21154c 281 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U,
wolfSSL 0:d92f9d21154c 282 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U,
wolfSSL 0:d92f9d21154c 283 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU,
wolfSSL 0:d92f9d21154c 284 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U,
wolfSSL 0:d92f9d21154c 285 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U,
wolfSSL 0:d92f9d21154c 286 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU,
wolfSSL 0:d92f9d21154c 287 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU,
wolfSSL 0:d92f9d21154c 288 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U,
wolfSSL 0:d92f9d21154c 289 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U,
wolfSSL 0:d92f9d21154c 290 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U,
wolfSSL 0:d92f9d21154c 291 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU,
wolfSSL 0:d92f9d21154c 292 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U,
wolfSSL 0:d92f9d21154c 293 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU,
wolfSSL 0:d92f9d21154c 294 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U,
wolfSSL 0:d92f9d21154c 295 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU,
wolfSSL 0:d92f9d21154c 296 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U,
wolfSSL 0:d92f9d21154c 297 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U,
wolfSSL 0:d92f9d21154c 298 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU,
wolfSSL 0:d92f9d21154c 299 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U,
wolfSSL 0:d92f9d21154c 300 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U,
wolfSSL 0:d92f9d21154c 301 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U,
wolfSSL 0:d92f9d21154c 302 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U,
wolfSSL 0:d92f9d21154c 303 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U,
wolfSSL 0:d92f9d21154c 304 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U,
wolfSSL 0:d92f9d21154c 305 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U,
wolfSSL 0:d92f9d21154c 306 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U,
wolfSSL 0:d92f9d21154c 307 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU,
wolfSSL 0:d92f9d21154c 308 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U,
wolfSSL 0:d92f9d21154c 309 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U,
wolfSSL 0:d92f9d21154c 310 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U,
wolfSSL 0:d92f9d21154c 311 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U,
wolfSSL 0:d92f9d21154c 312 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U,
wolfSSL 0:d92f9d21154c 313 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U,
wolfSSL 0:d92f9d21154c 314 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU,
wolfSSL 0:d92f9d21154c 315 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U,
wolfSSL 0:d92f9d21154c 316 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U,
wolfSSL 0:d92f9d21154c 317 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U,
wolfSSL 0:d92f9d21154c 318 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU,
wolfSSL 0:d92f9d21154c 319 },
wolfSSL 0:d92f9d21154c 320 {
wolfSSL 0:d92f9d21154c 321 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU,
wolfSSL 0:d92f9d21154c 322 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U,
wolfSSL 0:d92f9d21154c 323 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU,
wolfSSL 0:d92f9d21154c 324 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U,
wolfSSL 0:d92f9d21154c 325 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU,
wolfSSL 0:d92f9d21154c 326 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U,
wolfSSL 0:d92f9d21154c 327 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU,
wolfSSL 0:d92f9d21154c 328 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U,
wolfSSL 0:d92f9d21154c 329 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U,
wolfSSL 0:d92f9d21154c 330 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU,
wolfSSL 0:d92f9d21154c 331 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U,
wolfSSL 0:d92f9d21154c 332 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U,
wolfSSL 0:d92f9d21154c 333 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U,
wolfSSL 0:d92f9d21154c 334 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU,
wolfSSL 0:d92f9d21154c 335 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U,
wolfSSL 0:d92f9d21154c 336 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U,
wolfSSL 0:d92f9d21154c 337 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU,
wolfSSL 0:d92f9d21154c 338 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U,
wolfSSL 0:d92f9d21154c 339 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U,
wolfSSL 0:d92f9d21154c 340 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U,
wolfSSL 0:d92f9d21154c 341 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU,
wolfSSL 0:d92f9d21154c 342 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU,
wolfSSL 0:d92f9d21154c 343 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U,
wolfSSL 0:d92f9d21154c 344 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU,
wolfSSL 0:d92f9d21154c 345 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU,
wolfSSL 0:d92f9d21154c 346 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U,
wolfSSL 0:d92f9d21154c 347 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU,
wolfSSL 0:d92f9d21154c 348 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U,
wolfSSL 0:d92f9d21154c 349 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU,
wolfSSL 0:d92f9d21154c 350 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U,
wolfSSL 0:d92f9d21154c 351 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U,
wolfSSL 0:d92f9d21154c 352 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U,
wolfSSL 0:d92f9d21154c 353 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU,
wolfSSL 0:d92f9d21154c 354 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U,
wolfSSL 0:d92f9d21154c 355 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU,
wolfSSL 0:d92f9d21154c 356 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U,
wolfSSL 0:d92f9d21154c 357 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU,
wolfSSL 0:d92f9d21154c 358 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U,
wolfSSL 0:d92f9d21154c 359 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U,
wolfSSL 0:d92f9d21154c 360 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU,
wolfSSL 0:d92f9d21154c 361 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU,
wolfSSL 0:d92f9d21154c 362 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU,
wolfSSL 0:d92f9d21154c 363 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U,
wolfSSL 0:d92f9d21154c 364 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U,
wolfSSL 0:d92f9d21154c 365 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU,
wolfSSL 0:d92f9d21154c 366 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U,
wolfSSL 0:d92f9d21154c 367 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU,
wolfSSL 0:d92f9d21154c 368 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U,
wolfSSL 0:d92f9d21154c 369 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU,
wolfSSL 0:d92f9d21154c 370 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U,
wolfSSL 0:d92f9d21154c 371 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU,
wolfSSL 0:d92f9d21154c 372 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU,
wolfSSL 0:d92f9d21154c 373 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U,
wolfSSL 0:d92f9d21154c 374 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU,
wolfSSL 0:d92f9d21154c 375 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U,
wolfSSL 0:d92f9d21154c 376 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU,
wolfSSL 0:d92f9d21154c 377 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U,
wolfSSL 0:d92f9d21154c 378 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U,
wolfSSL 0:d92f9d21154c 379 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U,
wolfSSL 0:d92f9d21154c 380 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU,
wolfSSL 0:d92f9d21154c 381 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU,
wolfSSL 0:d92f9d21154c 382 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U,
wolfSSL 0:d92f9d21154c 383 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU,
wolfSSL 0:d92f9d21154c 384 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U,
wolfSSL 0:d92f9d21154c 385 },
wolfSSL 0:d92f9d21154c 386 {
wolfSSL 0:d92f9d21154c 387 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU,
wolfSSL 0:d92f9d21154c 388 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U,
wolfSSL 0:d92f9d21154c 389 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU,
wolfSSL 0:d92f9d21154c 390 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U,
wolfSSL 0:d92f9d21154c 391 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU,
wolfSSL 0:d92f9d21154c 392 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U,
wolfSSL 0:d92f9d21154c 393 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU,
wolfSSL 0:d92f9d21154c 394 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U,
wolfSSL 0:d92f9d21154c 395 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U,
wolfSSL 0:d92f9d21154c 396 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU,
wolfSSL 0:d92f9d21154c 397 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U,
wolfSSL 0:d92f9d21154c 398 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U,
wolfSSL 0:d92f9d21154c 399 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U,
wolfSSL 0:d92f9d21154c 400 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU,
wolfSSL 0:d92f9d21154c 401 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U,
wolfSSL 0:d92f9d21154c 402 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U,
wolfSSL 0:d92f9d21154c 403 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU,
wolfSSL 0:d92f9d21154c 404 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U,
wolfSSL 0:d92f9d21154c 405 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U,
wolfSSL 0:d92f9d21154c 406 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U,
wolfSSL 0:d92f9d21154c 407 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU,
wolfSSL 0:d92f9d21154c 408 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU,
wolfSSL 0:d92f9d21154c 409 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U,
wolfSSL 0:d92f9d21154c 410 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU,
wolfSSL 0:d92f9d21154c 411 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU,
wolfSSL 0:d92f9d21154c 412 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U,
wolfSSL 0:d92f9d21154c 413 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU,
wolfSSL 0:d92f9d21154c 414 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U,
wolfSSL 0:d92f9d21154c 415 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU,
wolfSSL 0:d92f9d21154c 416 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U,
wolfSSL 0:d92f9d21154c 417 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U,
wolfSSL 0:d92f9d21154c 418 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U,
wolfSSL 0:d92f9d21154c 419 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU,
wolfSSL 0:d92f9d21154c 420 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U,
wolfSSL 0:d92f9d21154c 421 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU,
wolfSSL 0:d92f9d21154c 422 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U,
wolfSSL 0:d92f9d21154c 423 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU,
wolfSSL 0:d92f9d21154c 424 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U,
wolfSSL 0:d92f9d21154c 425 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U,
wolfSSL 0:d92f9d21154c 426 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU,
wolfSSL 0:d92f9d21154c 427 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU,
wolfSSL 0:d92f9d21154c 428 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU,
wolfSSL 0:d92f9d21154c 429 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U,
wolfSSL 0:d92f9d21154c 430 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U,
wolfSSL 0:d92f9d21154c 431 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU,
wolfSSL 0:d92f9d21154c 432 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U,
wolfSSL 0:d92f9d21154c 433 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU,
wolfSSL 0:d92f9d21154c 434 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U,
wolfSSL 0:d92f9d21154c 435 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU,
wolfSSL 0:d92f9d21154c 436 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U,
wolfSSL 0:d92f9d21154c 437 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU,
wolfSSL 0:d92f9d21154c 438 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU,
wolfSSL 0:d92f9d21154c 439 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U,
wolfSSL 0:d92f9d21154c 440 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU,
wolfSSL 0:d92f9d21154c 441 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U,
wolfSSL 0:d92f9d21154c 442 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU,
wolfSSL 0:d92f9d21154c 443 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U,
wolfSSL 0:d92f9d21154c 444 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U,
wolfSSL 0:d92f9d21154c 445 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U,
wolfSSL 0:d92f9d21154c 446 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU,
wolfSSL 0:d92f9d21154c 447 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU,
wolfSSL 0:d92f9d21154c 448 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U,
wolfSSL 0:d92f9d21154c 449 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU,
wolfSSL 0:d92f9d21154c 450 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U,
wolfSSL 0:d92f9d21154c 451 },
wolfSSL 0:d92f9d21154c 452 {
wolfSSL 0:d92f9d21154c 453 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U,
wolfSSL 0:d92f9d21154c 454 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U,
wolfSSL 0:d92f9d21154c 455 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U,
wolfSSL 0:d92f9d21154c 456 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU,
wolfSSL 0:d92f9d21154c 457 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU,
wolfSSL 0:d92f9d21154c 458 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU,
wolfSSL 0:d92f9d21154c 459 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U,
wolfSSL 0:d92f9d21154c 460 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU,
wolfSSL 0:d92f9d21154c 461 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU,
wolfSSL 0:d92f9d21154c 462 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U,
wolfSSL 0:d92f9d21154c 463 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U,
wolfSSL 0:d92f9d21154c 464 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU,
wolfSSL 0:d92f9d21154c 465 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU,
wolfSSL 0:d92f9d21154c 466 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU,
wolfSSL 0:d92f9d21154c 467 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU,
wolfSSL 0:d92f9d21154c 468 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU,
wolfSSL 0:d92f9d21154c 469 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U,
wolfSSL 0:d92f9d21154c 470 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU,
wolfSSL 0:d92f9d21154c 471 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU,
wolfSSL 0:d92f9d21154c 472 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U,
wolfSSL 0:d92f9d21154c 473 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U,
wolfSSL 0:d92f9d21154c 474 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U,
wolfSSL 0:d92f9d21154c 475 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U,
wolfSSL 0:d92f9d21154c 476 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U,
wolfSSL 0:d92f9d21154c 477 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU,
wolfSSL 0:d92f9d21154c 478 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U,
wolfSSL 0:d92f9d21154c 479 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU,
wolfSSL 0:d92f9d21154c 480 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU,
wolfSSL 0:d92f9d21154c 481 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U,
wolfSSL 0:d92f9d21154c 482 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U,
wolfSSL 0:d92f9d21154c 483 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U,
wolfSSL 0:d92f9d21154c 484 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU,
wolfSSL 0:d92f9d21154c 485 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U,
wolfSSL 0:d92f9d21154c 486 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU,
wolfSSL 0:d92f9d21154c 487 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU,
wolfSSL 0:d92f9d21154c 488 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U,
wolfSSL 0:d92f9d21154c 489 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U,
wolfSSL 0:d92f9d21154c 490 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU,
wolfSSL 0:d92f9d21154c 491 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U,
wolfSSL 0:d92f9d21154c 492 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU,
wolfSSL 0:d92f9d21154c 493 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U,
wolfSSL 0:d92f9d21154c 494 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U,
wolfSSL 0:d92f9d21154c 495 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U,
wolfSSL 0:d92f9d21154c 496 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U,
wolfSSL 0:d92f9d21154c 497 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU,
wolfSSL 0:d92f9d21154c 498 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U,
wolfSSL 0:d92f9d21154c 499 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU,
wolfSSL 0:d92f9d21154c 500 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U,
wolfSSL 0:d92f9d21154c 501 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU,
wolfSSL 0:d92f9d21154c 502 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U,
wolfSSL 0:d92f9d21154c 503 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU,
wolfSSL 0:d92f9d21154c 504 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU,
wolfSSL 0:d92f9d21154c 505 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU,
wolfSSL 0:d92f9d21154c 506 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU,
wolfSSL 0:d92f9d21154c 507 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U,
wolfSSL 0:d92f9d21154c 508 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U,
wolfSSL 0:d92f9d21154c 509 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U,
wolfSSL 0:d92f9d21154c 510 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U,
wolfSSL 0:d92f9d21154c 511 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U,
wolfSSL 0:d92f9d21154c 512 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U,
wolfSSL 0:d92f9d21154c 513 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU,
wolfSSL 0:d92f9d21154c 514 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U,
wolfSSL 0:d92f9d21154c 515 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU,
wolfSSL 0:d92f9d21154c 516 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU,
wolfSSL 0:d92f9d21154c 517 },
wolfSSL 0:d92f9d21154c 518 {
wolfSSL 0:d92f9d21154c 519 0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU,
wolfSSL 0:d92f9d21154c 520 0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U,
wolfSSL 0:d92f9d21154c 521 0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU,
wolfSSL 0:d92f9d21154c 522 0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U,
wolfSSL 0:d92f9d21154c 523 0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU,
wolfSSL 0:d92f9d21154c 524 0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U,
wolfSSL 0:d92f9d21154c 525 0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU,
wolfSSL 0:d92f9d21154c 526 0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U,
wolfSSL 0:d92f9d21154c 527 0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U,
wolfSSL 0:d92f9d21154c 528 0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU,
wolfSSL 0:d92f9d21154c 529 0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U,
wolfSSL 0:d92f9d21154c 530 0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U,
wolfSSL 0:d92f9d21154c 531 0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U,
wolfSSL 0:d92f9d21154c 532 0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU,
wolfSSL 0:d92f9d21154c 533 0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U,
wolfSSL 0:d92f9d21154c 534 0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U,
wolfSSL 0:d92f9d21154c 535 0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU,
wolfSSL 0:d92f9d21154c 536 0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U,
wolfSSL 0:d92f9d21154c 537 0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U,
wolfSSL 0:d92f9d21154c 538 0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U,
wolfSSL 0:d92f9d21154c 539 0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU,
wolfSSL 0:d92f9d21154c 540 0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU,
wolfSSL 0:d92f9d21154c 541 0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U,
wolfSSL 0:d92f9d21154c 542 0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU,
wolfSSL 0:d92f9d21154c 543 0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU,
wolfSSL 0:d92f9d21154c 544 0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U,
wolfSSL 0:d92f9d21154c 545 0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU,
wolfSSL 0:d92f9d21154c 546 0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U,
wolfSSL 0:d92f9d21154c 547 0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU,
wolfSSL 0:d92f9d21154c 548 0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U,
wolfSSL 0:d92f9d21154c 549 0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U,
wolfSSL 0:d92f9d21154c 550 0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U,
wolfSSL 0:d92f9d21154c 551 0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU,
wolfSSL 0:d92f9d21154c 552 0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U,
wolfSSL 0:d92f9d21154c 553 0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU,
wolfSSL 0:d92f9d21154c 554 0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U,
wolfSSL 0:d92f9d21154c 555 0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU,
wolfSSL 0:d92f9d21154c 556 0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U,
wolfSSL 0:d92f9d21154c 557 0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U,
wolfSSL 0:d92f9d21154c 558 0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU,
wolfSSL 0:d92f9d21154c 559 0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU,
wolfSSL 0:d92f9d21154c 560 0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU,
wolfSSL 0:d92f9d21154c 561 0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U,
wolfSSL 0:d92f9d21154c 562 0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U,
wolfSSL 0:d92f9d21154c 563 0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU,
wolfSSL 0:d92f9d21154c 564 0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U,
wolfSSL 0:d92f9d21154c 565 0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU,
wolfSSL 0:d92f9d21154c 566 0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U,
wolfSSL 0:d92f9d21154c 567 0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU,
wolfSSL 0:d92f9d21154c 568 0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U,
wolfSSL 0:d92f9d21154c 569 0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU,
wolfSSL 0:d92f9d21154c 570 0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU,
wolfSSL 0:d92f9d21154c 571 0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U,
wolfSSL 0:d92f9d21154c 572 0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU,
wolfSSL 0:d92f9d21154c 573 0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U,
wolfSSL 0:d92f9d21154c 574 0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU,
wolfSSL 0:d92f9d21154c 575 0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U,
wolfSSL 0:d92f9d21154c 576 0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U,
wolfSSL 0:d92f9d21154c 577 0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U,
wolfSSL 0:d92f9d21154c 578 0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU,
wolfSSL 0:d92f9d21154c 579 0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU,
wolfSSL 0:d92f9d21154c 580 0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U,
wolfSSL 0:d92f9d21154c 581 0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU,
wolfSSL 0:d92f9d21154c 582 0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U,
wolfSSL 0:d92f9d21154c 583 }
wolfSSL 0:d92f9d21154c 584 };
wolfSSL 0:d92f9d21154c 585
wolfSSL 0:d92f9d21154c 586 static const word32 Td[5][256] = {
wolfSSL 0:d92f9d21154c 587 {
wolfSSL 0:d92f9d21154c 588 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
wolfSSL 0:d92f9d21154c 589 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U,
wolfSSL 0:d92f9d21154c 590 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U,
wolfSSL 0:d92f9d21154c 591 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU,
wolfSSL 0:d92f9d21154c 592 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U,
wolfSSL 0:d92f9d21154c 593 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U,
wolfSSL 0:d92f9d21154c 594 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU,
wolfSSL 0:d92f9d21154c 595 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U,
wolfSSL 0:d92f9d21154c 596 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU,
wolfSSL 0:d92f9d21154c 597 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U,
wolfSSL 0:d92f9d21154c 598 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U,
wolfSSL 0:d92f9d21154c 599 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U,
wolfSSL 0:d92f9d21154c 600 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U,
wolfSSL 0:d92f9d21154c 601 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU,
wolfSSL 0:d92f9d21154c 602 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U,
wolfSSL 0:d92f9d21154c 603 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU,
wolfSSL 0:d92f9d21154c 604 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U,
wolfSSL 0:d92f9d21154c 605 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU,
wolfSSL 0:d92f9d21154c 606 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U,
wolfSSL 0:d92f9d21154c 607 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U,
wolfSSL 0:d92f9d21154c 608 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U,
wolfSSL 0:d92f9d21154c 609 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU,
wolfSSL 0:d92f9d21154c 610 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U,
wolfSSL 0:d92f9d21154c 611 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU,
wolfSSL 0:d92f9d21154c 612 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U,
wolfSSL 0:d92f9d21154c 613 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU,
wolfSSL 0:d92f9d21154c 614 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U,
wolfSSL 0:d92f9d21154c 615 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU,
wolfSSL 0:d92f9d21154c 616 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU,
wolfSSL 0:d92f9d21154c 617 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U,
wolfSSL 0:d92f9d21154c 618 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU,
wolfSSL 0:d92f9d21154c 619 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U,
wolfSSL 0:d92f9d21154c 620 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU,
wolfSSL 0:d92f9d21154c 621 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U,
wolfSSL 0:d92f9d21154c 622 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U,
wolfSSL 0:d92f9d21154c 623 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U,
wolfSSL 0:d92f9d21154c 624 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU,
wolfSSL 0:d92f9d21154c 625 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U,
wolfSSL 0:d92f9d21154c 626 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U,
wolfSSL 0:d92f9d21154c 627 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU,
wolfSSL 0:d92f9d21154c 628 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U,
wolfSSL 0:d92f9d21154c 629 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U,
wolfSSL 0:d92f9d21154c 630 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U,
wolfSSL 0:d92f9d21154c 631 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U,
wolfSSL 0:d92f9d21154c 632 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U,
wolfSSL 0:d92f9d21154c 633 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU,
wolfSSL 0:d92f9d21154c 634 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U,
wolfSSL 0:d92f9d21154c 635 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U,
wolfSSL 0:d92f9d21154c 636 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U,
wolfSSL 0:d92f9d21154c 637 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U,
wolfSSL 0:d92f9d21154c 638 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U,
wolfSSL 0:d92f9d21154c 639 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU,
wolfSSL 0:d92f9d21154c 640 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU,
wolfSSL 0:d92f9d21154c 641 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU,
wolfSSL 0:d92f9d21154c 642 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU,
wolfSSL 0:d92f9d21154c 643 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U,
wolfSSL 0:d92f9d21154c 644 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U,
wolfSSL 0:d92f9d21154c 645 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU,
wolfSSL 0:d92f9d21154c 646 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU,
wolfSSL 0:d92f9d21154c 647 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U,
wolfSSL 0:d92f9d21154c 648 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU,
wolfSSL 0:d92f9d21154c 649 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U,
wolfSSL 0:d92f9d21154c 650 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U,
wolfSSL 0:d92f9d21154c 651 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U,
wolfSSL 0:d92f9d21154c 652 },
wolfSSL 0:d92f9d21154c 653 {
wolfSSL 0:d92f9d21154c 654 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU,
wolfSSL 0:d92f9d21154c 655 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U,
wolfSSL 0:d92f9d21154c 656 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU,
wolfSSL 0:d92f9d21154c 657 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U,
wolfSSL 0:d92f9d21154c 658 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U,
wolfSSL 0:d92f9d21154c 659 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U,
wolfSSL 0:d92f9d21154c 660 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U,
wolfSSL 0:d92f9d21154c 661 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U,
wolfSSL 0:d92f9d21154c 662 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U,
wolfSSL 0:d92f9d21154c 663 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU,
wolfSSL 0:d92f9d21154c 664 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU,
wolfSSL 0:d92f9d21154c 665 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU,
wolfSSL 0:d92f9d21154c 666 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U,
wolfSSL 0:d92f9d21154c 667 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU,
wolfSSL 0:d92f9d21154c 668 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U,
wolfSSL 0:d92f9d21154c 669 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U,
wolfSSL 0:d92f9d21154c 670 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U,
wolfSSL 0:d92f9d21154c 671 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU,
wolfSSL 0:d92f9d21154c 672 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU,
wolfSSL 0:d92f9d21154c 673 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U,
wolfSSL 0:d92f9d21154c 674 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU,
wolfSSL 0:d92f9d21154c 675 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U,
wolfSSL 0:d92f9d21154c 676 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU,
wolfSSL 0:d92f9d21154c 677 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU,
wolfSSL 0:d92f9d21154c 678 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U,
wolfSSL 0:d92f9d21154c 679 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U,
wolfSSL 0:d92f9d21154c 680 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U,
wolfSSL 0:d92f9d21154c 681 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU,
wolfSSL 0:d92f9d21154c 682 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U,
wolfSSL 0:d92f9d21154c 683 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU,
wolfSSL 0:d92f9d21154c 684 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U,
wolfSSL 0:d92f9d21154c 685 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U,
wolfSSL 0:d92f9d21154c 686 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U,
wolfSSL 0:d92f9d21154c 687 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU,
wolfSSL 0:d92f9d21154c 688 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U,
wolfSSL 0:d92f9d21154c 689 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U,
wolfSSL 0:d92f9d21154c 690 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U,
wolfSSL 0:d92f9d21154c 691 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U,
wolfSSL 0:d92f9d21154c 692 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U,
wolfSSL 0:d92f9d21154c 693 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U,
wolfSSL 0:d92f9d21154c 694 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU,
wolfSSL 0:d92f9d21154c 695 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU,
wolfSSL 0:d92f9d21154c 696 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U,
wolfSSL 0:d92f9d21154c 697 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU,
wolfSSL 0:d92f9d21154c 698 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U,
wolfSSL 0:d92f9d21154c 699 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU,
wolfSSL 0:d92f9d21154c 700 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU,
wolfSSL 0:d92f9d21154c 701 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U,
wolfSSL 0:d92f9d21154c 702 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU,
wolfSSL 0:d92f9d21154c 703 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U,
wolfSSL 0:d92f9d21154c 704 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U,
wolfSSL 0:d92f9d21154c 705 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U,
wolfSSL 0:d92f9d21154c 706 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U,
wolfSSL 0:d92f9d21154c 707 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U,
wolfSSL 0:d92f9d21154c 708 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U,
wolfSSL 0:d92f9d21154c 709 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U,
wolfSSL 0:d92f9d21154c 710 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU,
wolfSSL 0:d92f9d21154c 711 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U,
wolfSSL 0:d92f9d21154c 712 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U,
wolfSSL 0:d92f9d21154c 713 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU,
wolfSSL 0:d92f9d21154c 714 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U,
wolfSSL 0:d92f9d21154c 715 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U,
wolfSSL 0:d92f9d21154c 716 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U,
wolfSSL 0:d92f9d21154c 717 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U,
wolfSSL 0:d92f9d21154c 718 },
wolfSSL 0:d92f9d21154c 719 {
wolfSSL 0:d92f9d21154c 720 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U,
wolfSSL 0:d92f9d21154c 721 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U,
wolfSSL 0:d92f9d21154c 722 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U,
wolfSSL 0:d92f9d21154c 723 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U,
wolfSSL 0:d92f9d21154c 724 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU,
wolfSSL 0:d92f9d21154c 725 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U,
wolfSSL 0:d92f9d21154c 726 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U,
wolfSSL 0:d92f9d21154c 727 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U,
wolfSSL 0:d92f9d21154c 728 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U,
wolfSSL 0:d92f9d21154c 729 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU,
wolfSSL 0:d92f9d21154c 730 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U,
wolfSSL 0:d92f9d21154c 731 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U,
wolfSSL 0:d92f9d21154c 732 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU,
wolfSSL 0:d92f9d21154c 733 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U,
wolfSSL 0:d92f9d21154c 734 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U,
wolfSSL 0:d92f9d21154c 735 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U,
wolfSSL 0:d92f9d21154c 736 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U,
wolfSSL 0:d92f9d21154c 737 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U,
wolfSSL 0:d92f9d21154c 738 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U,
wolfSSL 0:d92f9d21154c 739 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU,
wolfSSL 0:d92f9d21154c 740
wolfSSL 0:d92f9d21154c 741 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U,
wolfSSL 0:d92f9d21154c 742 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U,
wolfSSL 0:d92f9d21154c 743 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U,
wolfSSL 0:d92f9d21154c 744 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U,
wolfSSL 0:d92f9d21154c 745 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U,
wolfSSL 0:d92f9d21154c 746 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU,
wolfSSL 0:d92f9d21154c 747 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU,
wolfSSL 0:d92f9d21154c 748 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U,
wolfSSL 0:d92f9d21154c 749 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU,
wolfSSL 0:d92f9d21154c 750 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U,
wolfSSL 0:d92f9d21154c 751 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU,
wolfSSL 0:d92f9d21154c 752 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU,
wolfSSL 0:d92f9d21154c 753 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU,
wolfSSL 0:d92f9d21154c 754 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU,
wolfSSL 0:d92f9d21154c 755 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U,
wolfSSL 0:d92f9d21154c 756 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U,
wolfSSL 0:d92f9d21154c 757 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U,
wolfSSL 0:d92f9d21154c 758 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U,
wolfSSL 0:d92f9d21154c 759 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U,
wolfSSL 0:d92f9d21154c 760 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U,
wolfSSL 0:d92f9d21154c 761 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U,
wolfSSL 0:d92f9d21154c 762 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU,
wolfSSL 0:d92f9d21154c 763 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU,
wolfSSL 0:d92f9d21154c 764 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U,
wolfSSL 0:d92f9d21154c 765 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U,
wolfSSL 0:d92f9d21154c 766 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU,
wolfSSL 0:d92f9d21154c 767 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU,
wolfSSL 0:d92f9d21154c 768 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U,
wolfSSL 0:d92f9d21154c 769 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U,
wolfSSL 0:d92f9d21154c 770 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U,
wolfSSL 0:d92f9d21154c 771 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U,
wolfSSL 0:d92f9d21154c 772 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U,
wolfSSL 0:d92f9d21154c 773 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U,
wolfSSL 0:d92f9d21154c 774 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U,
wolfSSL 0:d92f9d21154c 775 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU,
wolfSSL 0:d92f9d21154c 776 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U,
wolfSSL 0:d92f9d21154c 777 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U,
wolfSSL 0:d92f9d21154c 778 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U,
wolfSSL 0:d92f9d21154c 779 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U,
wolfSSL 0:d92f9d21154c 780 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U,
wolfSSL 0:d92f9d21154c 781 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U,
wolfSSL 0:d92f9d21154c 782 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU,
wolfSSL 0:d92f9d21154c 783 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U,
wolfSSL 0:d92f9d21154c 784 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U,
wolfSSL 0:d92f9d21154c 785 },
wolfSSL 0:d92f9d21154c 786 {
wolfSSL 0:d92f9d21154c 787 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU,
wolfSSL 0:d92f9d21154c 788 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU,
wolfSSL 0:d92f9d21154c 789 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U,
wolfSSL 0:d92f9d21154c 790 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U,
wolfSSL 0:d92f9d21154c 791 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU,
wolfSSL 0:d92f9d21154c 792 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU,
wolfSSL 0:d92f9d21154c 793 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U,
wolfSSL 0:d92f9d21154c 794 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU,
wolfSSL 0:d92f9d21154c 795 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U,
wolfSSL 0:d92f9d21154c 796 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU,
wolfSSL 0:d92f9d21154c 797 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U,
wolfSSL 0:d92f9d21154c 798 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U,
wolfSSL 0:d92f9d21154c 799 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U,
wolfSSL 0:d92f9d21154c 800 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U,
wolfSSL 0:d92f9d21154c 801 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U,
wolfSSL 0:d92f9d21154c 802 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU,
wolfSSL 0:d92f9d21154c 803 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU,
wolfSSL 0:d92f9d21154c 804 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U,
wolfSSL 0:d92f9d21154c 805 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U,
wolfSSL 0:d92f9d21154c 806 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU,
wolfSSL 0:d92f9d21154c 807 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU,
wolfSSL 0:d92f9d21154c 808 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U,
wolfSSL 0:d92f9d21154c 809 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U,
wolfSSL 0:d92f9d21154c 810 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U,
wolfSSL 0:d92f9d21154c 811 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U,
wolfSSL 0:d92f9d21154c 812 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU,
wolfSSL 0:d92f9d21154c 813 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U,
wolfSSL 0:d92f9d21154c 814 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U,
wolfSSL 0:d92f9d21154c 815 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU,
wolfSSL 0:d92f9d21154c 816 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU,
wolfSSL 0:d92f9d21154c 817 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U,
wolfSSL 0:d92f9d21154c 818 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U,
wolfSSL 0:d92f9d21154c 819 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U,
wolfSSL 0:d92f9d21154c 820 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU,
wolfSSL 0:d92f9d21154c 821 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U,
wolfSSL 0:d92f9d21154c 822 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U,
wolfSSL 0:d92f9d21154c 823 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U,
wolfSSL 0:d92f9d21154c 824 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U,
wolfSSL 0:d92f9d21154c 825 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U,
wolfSSL 0:d92f9d21154c 826 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U,
wolfSSL 0:d92f9d21154c 827 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U,
wolfSSL 0:d92f9d21154c 828 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU,
wolfSSL 0:d92f9d21154c 829 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U,
wolfSSL 0:d92f9d21154c 830 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U,
wolfSSL 0:d92f9d21154c 831 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU,
wolfSSL 0:d92f9d21154c 832 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU,
wolfSSL 0:d92f9d21154c 833 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U,
wolfSSL 0:d92f9d21154c 834 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU,
wolfSSL 0:d92f9d21154c 835 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U,
wolfSSL 0:d92f9d21154c 836 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U,
wolfSSL 0:d92f9d21154c 837 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U,
wolfSSL 0:d92f9d21154c 838 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U,
wolfSSL 0:d92f9d21154c 839 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U,
wolfSSL 0:d92f9d21154c 840 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U,
wolfSSL 0:d92f9d21154c 841 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU,
wolfSSL 0:d92f9d21154c 842 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU,
wolfSSL 0:d92f9d21154c 843 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU,
wolfSSL 0:d92f9d21154c 844 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU,
wolfSSL 0:d92f9d21154c 845 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U,
wolfSSL 0:d92f9d21154c 846 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U,
wolfSSL 0:d92f9d21154c 847 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U,
wolfSSL 0:d92f9d21154c 848 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU,
wolfSSL 0:d92f9d21154c 849 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U,
wolfSSL 0:d92f9d21154c 850 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U,
wolfSSL 0:d92f9d21154c 851 },
wolfSSL 0:d92f9d21154c 852 {
wolfSSL 0:d92f9d21154c 853 0x52525252U, 0x09090909U, 0x6a6a6a6aU, 0xd5d5d5d5U,
wolfSSL 0:d92f9d21154c 854 0x30303030U, 0x36363636U, 0xa5a5a5a5U, 0x38383838U,
wolfSSL 0:d92f9d21154c 855 0xbfbfbfbfU, 0x40404040U, 0xa3a3a3a3U, 0x9e9e9e9eU,
wolfSSL 0:d92f9d21154c 856 0x81818181U, 0xf3f3f3f3U, 0xd7d7d7d7U, 0xfbfbfbfbU,
wolfSSL 0:d92f9d21154c 857 0x7c7c7c7cU, 0xe3e3e3e3U, 0x39393939U, 0x82828282U,
wolfSSL 0:d92f9d21154c 858 0x9b9b9b9bU, 0x2f2f2f2fU, 0xffffffffU, 0x87878787U,
wolfSSL 0:d92f9d21154c 859 0x34343434U, 0x8e8e8e8eU, 0x43434343U, 0x44444444U,
wolfSSL 0:d92f9d21154c 860 0xc4c4c4c4U, 0xdedededeU, 0xe9e9e9e9U, 0xcbcbcbcbU,
wolfSSL 0:d92f9d21154c 861 0x54545454U, 0x7b7b7b7bU, 0x94949494U, 0x32323232U,
wolfSSL 0:d92f9d21154c 862 0xa6a6a6a6U, 0xc2c2c2c2U, 0x23232323U, 0x3d3d3d3dU,
wolfSSL 0:d92f9d21154c 863 0xeeeeeeeeU, 0x4c4c4c4cU, 0x95959595U, 0x0b0b0b0bU,
wolfSSL 0:d92f9d21154c 864 0x42424242U, 0xfafafafaU, 0xc3c3c3c3U, 0x4e4e4e4eU,
wolfSSL 0:d92f9d21154c 865 0x08080808U, 0x2e2e2e2eU, 0xa1a1a1a1U, 0x66666666U,
wolfSSL 0:d92f9d21154c 866 0x28282828U, 0xd9d9d9d9U, 0x24242424U, 0xb2b2b2b2U,
wolfSSL 0:d92f9d21154c 867 0x76767676U, 0x5b5b5b5bU, 0xa2a2a2a2U, 0x49494949U,
wolfSSL 0:d92f9d21154c 868 0x6d6d6d6dU, 0x8b8b8b8bU, 0xd1d1d1d1U, 0x25252525U,
wolfSSL 0:d92f9d21154c 869 0x72727272U, 0xf8f8f8f8U, 0xf6f6f6f6U, 0x64646464U,
wolfSSL 0:d92f9d21154c 870 0x86868686U, 0x68686868U, 0x98989898U, 0x16161616U,
wolfSSL 0:d92f9d21154c 871 0xd4d4d4d4U, 0xa4a4a4a4U, 0x5c5c5c5cU, 0xccccccccU,
wolfSSL 0:d92f9d21154c 872 0x5d5d5d5dU, 0x65656565U, 0xb6b6b6b6U, 0x92929292U,
wolfSSL 0:d92f9d21154c 873 0x6c6c6c6cU, 0x70707070U, 0x48484848U, 0x50505050U,
wolfSSL 0:d92f9d21154c 874 0xfdfdfdfdU, 0xededededU, 0xb9b9b9b9U, 0xdadadadaU,
wolfSSL 0:d92f9d21154c 875 0x5e5e5e5eU, 0x15151515U, 0x46464646U, 0x57575757U,
wolfSSL 0:d92f9d21154c 876 0xa7a7a7a7U, 0x8d8d8d8dU, 0x9d9d9d9dU, 0x84848484U,
wolfSSL 0:d92f9d21154c 877 0x90909090U, 0xd8d8d8d8U, 0xababababU, 0x00000000U,
wolfSSL 0:d92f9d21154c 878 0x8c8c8c8cU, 0xbcbcbcbcU, 0xd3d3d3d3U, 0x0a0a0a0aU,
wolfSSL 0:d92f9d21154c 879 0xf7f7f7f7U, 0xe4e4e4e4U, 0x58585858U, 0x05050505U,
wolfSSL 0:d92f9d21154c 880 0xb8b8b8b8U, 0xb3b3b3b3U, 0x45454545U, 0x06060606U,
wolfSSL 0:d92f9d21154c 881 0xd0d0d0d0U, 0x2c2c2c2cU, 0x1e1e1e1eU, 0x8f8f8f8fU,
wolfSSL 0:d92f9d21154c 882 0xcacacacaU, 0x3f3f3f3fU, 0x0f0f0f0fU, 0x02020202U,
wolfSSL 0:d92f9d21154c 883 0xc1c1c1c1U, 0xafafafafU, 0xbdbdbdbdU, 0x03030303U,
wolfSSL 0:d92f9d21154c 884 0x01010101U, 0x13131313U, 0x8a8a8a8aU, 0x6b6b6b6bU,
wolfSSL 0:d92f9d21154c 885 0x3a3a3a3aU, 0x91919191U, 0x11111111U, 0x41414141U,
wolfSSL 0:d92f9d21154c 886 0x4f4f4f4fU, 0x67676767U, 0xdcdcdcdcU, 0xeaeaeaeaU,
wolfSSL 0:d92f9d21154c 887 0x97979797U, 0xf2f2f2f2U, 0xcfcfcfcfU, 0xcecececeU,
wolfSSL 0:d92f9d21154c 888 0xf0f0f0f0U, 0xb4b4b4b4U, 0xe6e6e6e6U, 0x73737373U,
wolfSSL 0:d92f9d21154c 889 0x96969696U, 0xacacacacU, 0x74747474U, 0x22222222U,
wolfSSL 0:d92f9d21154c 890 0xe7e7e7e7U, 0xadadadadU, 0x35353535U, 0x85858585U,
wolfSSL 0:d92f9d21154c 891 0xe2e2e2e2U, 0xf9f9f9f9U, 0x37373737U, 0xe8e8e8e8U,
wolfSSL 0:d92f9d21154c 892 0x1c1c1c1cU, 0x75757575U, 0xdfdfdfdfU, 0x6e6e6e6eU,
wolfSSL 0:d92f9d21154c 893 0x47474747U, 0xf1f1f1f1U, 0x1a1a1a1aU, 0x71717171U,
wolfSSL 0:d92f9d21154c 894 0x1d1d1d1dU, 0x29292929U, 0xc5c5c5c5U, 0x89898989U,
wolfSSL 0:d92f9d21154c 895 0x6f6f6f6fU, 0xb7b7b7b7U, 0x62626262U, 0x0e0e0e0eU,
wolfSSL 0:d92f9d21154c 896 0xaaaaaaaaU, 0x18181818U, 0xbebebebeU, 0x1b1b1b1bU,
wolfSSL 0:d92f9d21154c 897 0xfcfcfcfcU, 0x56565656U, 0x3e3e3e3eU, 0x4b4b4b4bU,
wolfSSL 0:d92f9d21154c 898 0xc6c6c6c6U, 0xd2d2d2d2U, 0x79797979U, 0x20202020U,
wolfSSL 0:d92f9d21154c 899 0x9a9a9a9aU, 0xdbdbdbdbU, 0xc0c0c0c0U, 0xfefefefeU,
wolfSSL 0:d92f9d21154c 900 0x78787878U, 0xcdcdcdcdU, 0x5a5a5a5aU, 0xf4f4f4f4U,
wolfSSL 0:d92f9d21154c 901 0x1f1f1f1fU, 0xddddddddU, 0xa8a8a8a8U, 0x33333333U,
wolfSSL 0:d92f9d21154c 902 0x88888888U, 0x07070707U, 0xc7c7c7c7U, 0x31313131U,
wolfSSL 0:d92f9d21154c 903 0xb1b1b1b1U, 0x12121212U, 0x10101010U, 0x59595959U,
wolfSSL 0:d92f9d21154c 904 0x27272727U, 0x80808080U, 0xececececU, 0x5f5f5f5fU,
wolfSSL 0:d92f9d21154c 905 0x60606060U, 0x51515151U, 0x7f7f7f7fU, 0xa9a9a9a9U,
wolfSSL 0:d92f9d21154c 906 0x19191919U, 0xb5b5b5b5U, 0x4a4a4a4aU, 0x0d0d0d0dU,
wolfSSL 0:d92f9d21154c 907 0x2d2d2d2dU, 0xe5e5e5e5U, 0x7a7a7a7aU, 0x9f9f9f9fU,
wolfSSL 0:d92f9d21154c 908 0x93939393U, 0xc9c9c9c9U, 0x9c9c9c9cU, 0xefefefefU,
wolfSSL 0:d92f9d21154c 909 0xa0a0a0a0U, 0xe0e0e0e0U, 0x3b3b3b3bU, 0x4d4d4d4dU,
wolfSSL 0:d92f9d21154c 910 0xaeaeaeaeU, 0x2a2a2a2aU, 0xf5f5f5f5U, 0xb0b0b0b0U,
wolfSSL 0:d92f9d21154c 911 0xc8c8c8c8U, 0xebebebebU, 0xbbbbbbbbU, 0x3c3c3c3cU,
wolfSSL 0:d92f9d21154c 912 0x83838383U, 0x53535353U, 0x99999999U, 0x61616161U,
wolfSSL 0:d92f9d21154c 913 0x17171717U, 0x2b2b2b2bU, 0x04040404U, 0x7e7e7e7eU,
wolfSSL 0:d92f9d21154c 914 0xbabababaU, 0x77777777U, 0xd6d6d6d6U, 0x26262626U,
wolfSSL 0:d92f9d21154c 915 0xe1e1e1e1U, 0x69696969U, 0x14141414U, 0x63636363U,
wolfSSL 0:d92f9d21154c 916 0x55555555U, 0x21212121U, 0x0c0c0c0cU, 0x7d7d7d7dU,
wolfSSL 0:d92f9d21154c 917 }
wolfSSL 0:d92f9d21154c 918 };
wolfSSL 0:d92f9d21154c 919
wolfSSL 0:d92f9d21154c 920 #define GETBYTE(x, y) (word32)((byte)((x) >> (8 * (y))))
wolfSSL 0:d92f9d21154c 921
wolfSSL 0:d92f9d21154c 922 #ifdef WOLFSSL_AESNI
wolfSSL 0:d92f9d21154c 923
wolfSSL 0:d92f9d21154c 924 /* Each platform needs to query info type 1 from cpuid to see if aesni is
wolfSSL 0:d92f9d21154c 925 * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
wolfSSL 0:d92f9d21154c 926 */
wolfSSL 0:d92f9d21154c 927
wolfSSL 0:d92f9d21154c 928 #ifndef _MSC_VER
wolfSSL 0:d92f9d21154c 929
wolfSSL 0:d92f9d21154c 930 #define cpuid(reg, func)\
wolfSSL 0:d92f9d21154c 931 __asm__ __volatile__ ("cpuid":\
wolfSSL 0:d92f9d21154c 932 "=a" (reg[0]), "=b" (reg[1]), "=c" (reg[2]), "=d" (reg[3]) :\
wolfSSL 0:d92f9d21154c 933 "a" (func));
wolfSSL 0:d92f9d21154c 934
wolfSSL 0:d92f9d21154c 935 #define XASM_LINK(f) asm(f)
wolfSSL 0:d92f9d21154c 936 #else
wolfSSL 0:d92f9d21154c 937
wolfSSL 0:d92f9d21154c 938 #include <intrin.h>
wolfSSL 0:d92f9d21154c 939 #define cpuid(a,b) __cpuid((int*)a,b)
wolfSSL 0:d92f9d21154c 940
wolfSSL 0:d92f9d21154c 941 #define XASM_LINK(f)
wolfSSL 0:d92f9d21154c 942
wolfSSL 0:d92f9d21154c 943 #endif /* _MSC_VER */
wolfSSL 0:d92f9d21154c 944
wolfSSL 0:d92f9d21154c 945
wolfSSL 0:d92f9d21154c 946 static int Check_CPU_support_AES(void)
wolfSSL 0:d92f9d21154c 947 {
wolfSSL 0:d92f9d21154c 948 unsigned int reg[4]; /* put a,b,c,d into 0,1,2,3 */
wolfSSL 0:d92f9d21154c 949 cpuid(reg, 1); /* query info 1 */
wolfSSL 0:d92f9d21154c 950
wolfSSL 0:d92f9d21154c 951 if (reg[2] & 0x2000000)
wolfSSL 0:d92f9d21154c 952 return 1;
wolfSSL 0:d92f9d21154c 953
wolfSSL 0:d92f9d21154c 954 return 0;
wolfSSL 0:d92f9d21154c 955 }
wolfSSL 0:d92f9d21154c 956
wolfSSL 0:d92f9d21154c 957 static int checkAESNI = 0;
wolfSSL 0:d92f9d21154c 958 static int haveAESNI = 0;
wolfSSL 0:d92f9d21154c 959
wolfSSL 0:d92f9d21154c 960
wolfSSL 0:d92f9d21154c 961 /* tell C compiler these are asm functions in case any mix up of ABI underscore
wolfSSL 0:d92f9d21154c 962 prefix between clang/gcc/llvm etc */
wolfSSL 0:d92f9d21154c 963 void AES_CBC_encrypt(const unsigned char* in, unsigned char* out,
wolfSSL 0:d92f9d21154c 964 unsigned char* ivec, unsigned long length,
wolfSSL 0:d92f9d21154c 965 const unsigned char* KS, int nr)
wolfSSL 0:d92f9d21154c 966 XASM_LINK("AES_CBC_encrypt");
wolfSSL 0:d92f9d21154c 967
wolfSSL 0:d92f9d21154c 968
wolfSSL 0:d92f9d21154c 969 void AES_CBC_decrypt(const unsigned char* in, unsigned char* out,
wolfSSL 0:d92f9d21154c 970 unsigned char* ivec, unsigned long length,
wolfSSL 0:d92f9d21154c 971 const unsigned char* KS, int nr)
wolfSSL 0:d92f9d21154c 972 XASM_LINK("AES_CBC_decrypt");
wolfSSL 0:d92f9d21154c 973
wolfSSL 0:d92f9d21154c 974 void AES_ECB_encrypt(const unsigned char* in, unsigned char* out,
wolfSSL 0:d92f9d21154c 975 unsigned long length, const unsigned char* KS, int nr)
wolfSSL 0:d92f9d21154c 976 XASM_LINK("AES_ECB_encrypt");
wolfSSL 0:d92f9d21154c 977
wolfSSL 0:d92f9d21154c 978
wolfSSL 0:d92f9d21154c 979 void AES_ECB_decrypt(const unsigned char* in, unsigned char* out,
wolfSSL 0:d92f9d21154c 980 unsigned long length, const unsigned char* KS, int nr)
wolfSSL 0:d92f9d21154c 981 XASM_LINK("AES_ECB_decrypt");
wolfSSL 0:d92f9d21154c 982
wolfSSL 0:d92f9d21154c 983 void AES_128_Key_Expansion(const unsigned char* userkey,
wolfSSL 0:d92f9d21154c 984 unsigned char* key_schedule)
wolfSSL 0:d92f9d21154c 985 XASM_LINK("AES_128_Key_Expansion");
wolfSSL 0:d92f9d21154c 986
wolfSSL 0:d92f9d21154c 987 void AES_192_Key_Expansion(const unsigned char* userkey,
wolfSSL 0:d92f9d21154c 988 unsigned char* key_schedule)
wolfSSL 0:d92f9d21154c 989 XASM_LINK("AES_192_Key_Expansion");
wolfSSL 0:d92f9d21154c 990
wolfSSL 0:d92f9d21154c 991 void AES_256_Key_Expansion(const unsigned char* userkey,
wolfSSL 0:d92f9d21154c 992 unsigned char* key_schedule)
wolfSSL 0:d92f9d21154c 993 XASM_LINK("AES_256_Key_Expansion");
wolfSSL 0:d92f9d21154c 994
wolfSSL 0:d92f9d21154c 995
wolfSSL 0:d92f9d21154c 996 static int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
wolfSSL 0:d92f9d21154c 997 Aes* aes)
wolfSSL 0:d92f9d21154c 998 {
wolfSSL 0:d92f9d21154c 999 if (!userKey || !aes)
wolfSSL 0:d92f9d21154c 1000 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1001
wolfSSL 0:d92f9d21154c 1002 if (bits == 128) {
wolfSSL 0:d92f9d21154c 1003 AES_128_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 10;
wolfSSL 0:d92f9d21154c 1004 return 0;
wolfSSL 0:d92f9d21154c 1005 }
wolfSSL 0:d92f9d21154c 1006 else if (bits == 192) {
wolfSSL 0:d92f9d21154c 1007 AES_192_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 12;
wolfSSL 0:d92f9d21154c 1008 return 0;
wolfSSL 0:d92f9d21154c 1009 }
wolfSSL 0:d92f9d21154c 1010 else if (bits == 256) {
wolfSSL 0:d92f9d21154c 1011 AES_256_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 14;
wolfSSL 0:d92f9d21154c 1012 return 0;
wolfSSL 0:d92f9d21154c 1013 }
wolfSSL 0:d92f9d21154c 1014 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1015 }
wolfSSL 0:d92f9d21154c 1016
wolfSSL 0:d92f9d21154c 1017
wolfSSL 0:d92f9d21154c 1018 static int AES_set_decrypt_key(const unsigned char* userKey, const int bits,
wolfSSL 0:d92f9d21154c 1019 Aes* aes)
wolfSSL 0:d92f9d21154c 1020 {
wolfSSL 0:d92f9d21154c 1021 int nr;
wolfSSL 0:d92f9d21154c 1022 Aes temp_key;
wolfSSL 0:d92f9d21154c 1023 __m128i *Key_Schedule = (__m128i*)aes->key;
wolfSSL 0:d92f9d21154c 1024 __m128i *Temp_Key_Schedule = (__m128i*)temp_key.key;
wolfSSL 0:d92f9d21154c 1025
wolfSSL 0:d92f9d21154c 1026 if (!userKey || !aes)
wolfSSL 0:d92f9d21154c 1027 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1028
wolfSSL 0:d92f9d21154c 1029 if (AES_set_encrypt_key(userKey,bits,&temp_key) == BAD_FUNC_ARG)
wolfSSL 0:d92f9d21154c 1030 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1031
wolfSSL 0:d92f9d21154c 1032 nr = temp_key.rounds;
wolfSSL 0:d92f9d21154c 1033 aes->rounds = nr;
wolfSSL 0:d92f9d21154c 1034
wolfSSL 0:d92f9d21154c 1035 Key_Schedule[nr] = Temp_Key_Schedule[0];
wolfSSL 0:d92f9d21154c 1036 Key_Schedule[nr-1] = _mm_aesimc_si128(Temp_Key_Schedule[1]);
wolfSSL 0:d92f9d21154c 1037 Key_Schedule[nr-2] = _mm_aesimc_si128(Temp_Key_Schedule[2]);
wolfSSL 0:d92f9d21154c 1038 Key_Schedule[nr-3] = _mm_aesimc_si128(Temp_Key_Schedule[3]);
wolfSSL 0:d92f9d21154c 1039 Key_Schedule[nr-4] = _mm_aesimc_si128(Temp_Key_Schedule[4]);
wolfSSL 0:d92f9d21154c 1040 Key_Schedule[nr-5] = _mm_aesimc_si128(Temp_Key_Schedule[5]);
wolfSSL 0:d92f9d21154c 1041 Key_Schedule[nr-6] = _mm_aesimc_si128(Temp_Key_Schedule[6]);
wolfSSL 0:d92f9d21154c 1042 Key_Schedule[nr-7] = _mm_aesimc_si128(Temp_Key_Schedule[7]);
wolfSSL 0:d92f9d21154c 1043 Key_Schedule[nr-8] = _mm_aesimc_si128(Temp_Key_Schedule[8]);
wolfSSL 0:d92f9d21154c 1044 Key_Schedule[nr-9] = _mm_aesimc_si128(Temp_Key_Schedule[9]);
wolfSSL 0:d92f9d21154c 1045
wolfSSL 0:d92f9d21154c 1046 if(nr>10) {
wolfSSL 0:d92f9d21154c 1047 Key_Schedule[nr-10] = _mm_aesimc_si128(Temp_Key_Schedule[10]);
wolfSSL 0:d92f9d21154c 1048 Key_Schedule[nr-11] = _mm_aesimc_si128(Temp_Key_Schedule[11]);
wolfSSL 0:d92f9d21154c 1049 }
wolfSSL 0:d92f9d21154c 1050
wolfSSL 0:d92f9d21154c 1051 if(nr>12) {
wolfSSL 0:d92f9d21154c 1052 Key_Schedule[nr-12] = _mm_aesimc_si128(Temp_Key_Schedule[12]);
wolfSSL 0:d92f9d21154c 1053 Key_Schedule[nr-13] = _mm_aesimc_si128(Temp_Key_Schedule[13]);
wolfSSL 0:d92f9d21154c 1054 }
wolfSSL 0:d92f9d21154c 1055
wolfSSL 0:d92f9d21154c 1056 Key_Schedule[0] = Temp_Key_Schedule[nr];
wolfSSL 0:d92f9d21154c 1057
wolfSSL 0:d92f9d21154c 1058 return 0;
wolfSSL 0:d92f9d21154c 1059 }
wolfSSL 0:d92f9d21154c 1060
wolfSSL 0:d92f9d21154c 1061
wolfSSL 0:d92f9d21154c 1062
wolfSSL 0:d92f9d21154c 1063 #endif /* WOLFSSL_AESNI */
wolfSSL 0:d92f9d21154c 1064
wolfSSL 0:d92f9d21154c 1065
wolfSSL 0:d92f9d21154c 1066 static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 0:d92f9d21154c 1067 {
wolfSSL 0:d92f9d21154c 1068 word32 s0, s1, s2, s3;
wolfSSL 0:d92f9d21154c 1069 word32 t0, t1, t2, t3;
wolfSSL 0:d92f9d21154c 1070 word32 r = aes->rounds >> 1;
wolfSSL 0:d92f9d21154c 1071
wolfSSL 0:d92f9d21154c 1072 const word32* rk = aes->key;
wolfSSL 0:d92f9d21154c 1073 if (r > 7 || r == 0) {
wolfSSL 0:d92f9d21154c 1074 WOLFSSL_MSG("AesEncrypt encountered improper key, set it up");
wolfSSL 0:d92f9d21154c 1075 return; /* stop instead of segfaulting, set up your keys! */
wolfSSL 0:d92f9d21154c 1076 }
wolfSSL 0:d92f9d21154c 1077 #ifdef WOLFSSL_AESNI
wolfSSL 0:d92f9d21154c 1078 if (haveAESNI && aes->use_aesni) {
wolfSSL 0:d92f9d21154c 1079 #ifdef DEBUG_AESNI
wolfSSL 0:d92f9d21154c 1080 printf("about to aes encrypt\n");
wolfSSL 0:d92f9d21154c 1081 printf("in = %p\n", inBlock);
wolfSSL 0:d92f9d21154c 1082 printf("out = %p\n", outBlock);
wolfSSL 0:d92f9d21154c 1083 printf("aes->key = %p\n", aes->key);
wolfSSL 0:d92f9d21154c 1084 printf("aes->rounds = %d\n", aes->rounds);
wolfSSL 0:d92f9d21154c 1085 printf("sz = %d\n", AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1086 #endif
wolfSSL 0:d92f9d21154c 1087
wolfSSL 0:d92f9d21154c 1088 /* check alignment, decrypt doesn't need alignment */
wolfSSL 0:d92f9d21154c 1089 if ((wolfssl_word)inBlock % 16) {
wolfSSL 0:d92f9d21154c 1090 #ifndef NO_WOLFSSL_ALLOC_ALIGN
wolfSSL 0:d92f9d21154c 1091 byte* tmp = (byte*)XMALLOC(AES_BLOCK_SIZE, NULL,
wolfSSL 0:d92f9d21154c 1092 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:d92f9d21154c 1093 if (tmp == NULL) return;
wolfSSL 0:d92f9d21154c 1094
wolfSSL 0:d92f9d21154c 1095 XMEMCPY(tmp, inBlock, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1096 AES_ECB_encrypt(tmp, tmp, AES_BLOCK_SIZE, (byte*)aes->key,
wolfSSL 0:d92f9d21154c 1097 aes->rounds);
wolfSSL 0:d92f9d21154c 1098 XMEMCPY(outBlock, tmp, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1099 XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:d92f9d21154c 1100 return;
wolfSSL 0:d92f9d21154c 1101 #else
wolfSSL 0:d92f9d21154c 1102 WOLFSSL_MSG("AES-ECB encrypt with bad alignment");
wolfSSL 0:d92f9d21154c 1103 return;
wolfSSL 0:d92f9d21154c 1104 #endif
wolfSSL 0:d92f9d21154c 1105 }
wolfSSL 0:d92f9d21154c 1106
wolfSSL 0:d92f9d21154c 1107 AES_ECB_encrypt(inBlock, outBlock, AES_BLOCK_SIZE, (byte*)aes->key,
wolfSSL 0:d92f9d21154c 1108 aes->rounds);
wolfSSL 0:d92f9d21154c 1109
wolfSSL 0:d92f9d21154c 1110 return;
wolfSSL 0:d92f9d21154c 1111 }
wolfSSL 0:d92f9d21154c 1112 else {
wolfSSL 0:d92f9d21154c 1113 #ifdef DEBUG_AESNI
wolfSSL 0:d92f9d21154c 1114 printf("Skipping AES-NI\n");
wolfSSL 0:d92f9d21154c 1115 #endif
wolfSSL 0:d92f9d21154c 1116 }
wolfSSL 0:d92f9d21154c 1117 #endif
wolfSSL 0:d92f9d21154c 1118
wolfSSL 0:d92f9d21154c 1119 /*
wolfSSL 0:d92f9d21154c 1120 * map byte array block to cipher state
wolfSSL 0:d92f9d21154c 1121 * and add initial round key:
wolfSSL 0:d92f9d21154c 1122 */
wolfSSL 0:d92f9d21154c 1123 XMEMCPY(&s0, inBlock, sizeof(s0));
wolfSSL 0:d92f9d21154c 1124 XMEMCPY(&s1, inBlock + sizeof(s0), sizeof(s1));
wolfSSL 0:d92f9d21154c 1125 XMEMCPY(&s2, inBlock + 2 * sizeof(s0), sizeof(s2));
wolfSSL 0:d92f9d21154c 1126 XMEMCPY(&s3, inBlock + 3 * sizeof(s0), sizeof(s3));
wolfSSL 0:d92f9d21154c 1127
wolfSSL 0:d92f9d21154c 1128 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 1129 s0 = ByteReverseWord32(s0);
wolfSSL 0:d92f9d21154c 1130 s1 = ByteReverseWord32(s1);
wolfSSL 0:d92f9d21154c 1131 s2 = ByteReverseWord32(s2);
wolfSSL 0:d92f9d21154c 1132 s3 = ByteReverseWord32(s3);
wolfSSL 0:d92f9d21154c 1133 #endif
wolfSSL 0:d92f9d21154c 1134
wolfSSL 0:d92f9d21154c 1135 s0 ^= rk[0];
wolfSSL 0:d92f9d21154c 1136 s1 ^= rk[1];
wolfSSL 0:d92f9d21154c 1137 s2 ^= rk[2];
wolfSSL 0:d92f9d21154c 1138 s3 ^= rk[3];
wolfSSL 0:d92f9d21154c 1139
wolfSSL 0:d92f9d21154c 1140 /*
wolfSSL 0:d92f9d21154c 1141 * Nr - 1 full rounds:
wolfSSL 0:d92f9d21154c 1142 */
wolfSSL 0:d92f9d21154c 1143
wolfSSL 0:d92f9d21154c 1144 for (;;) {
wolfSSL 0:d92f9d21154c 1145 t0 =
wolfSSL 0:d92f9d21154c 1146 Te[0][GETBYTE(s0, 3)] ^
wolfSSL 0:d92f9d21154c 1147 Te[1][GETBYTE(s1, 2)] ^
wolfSSL 0:d92f9d21154c 1148 Te[2][GETBYTE(s2, 1)] ^
wolfSSL 0:d92f9d21154c 1149 Te[3][GETBYTE(s3, 0)] ^
wolfSSL 0:d92f9d21154c 1150 rk[4];
wolfSSL 0:d92f9d21154c 1151 t1 =
wolfSSL 0:d92f9d21154c 1152 Te[0][GETBYTE(s1, 3)] ^
wolfSSL 0:d92f9d21154c 1153 Te[1][GETBYTE(s2, 2)] ^
wolfSSL 0:d92f9d21154c 1154 Te[2][GETBYTE(s3, 1)] ^
wolfSSL 0:d92f9d21154c 1155 Te[3][GETBYTE(s0, 0)] ^
wolfSSL 0:d92f9d21154c 1156 rk[5];
wolfSSL 0:d92f9d21154c 1157 t2 =
wolfSSL 0:d92f9d21154c 1158 Te[0][GETBYTE(s2, 3)] ^
wolfSSL 0:d92f9d21154c 1159 Te[1][GETBYTE(s3, 2)] ^
wolfSSL 0:d92f9d21154c 1160 Te[2][GETBYTE(s0, 1)] ^
wolfSSL 0:d92f9d21154c 1161 Te[3][GETBYTE(s1, 0)] ^
wolfSSL 0:d92f9d21154c 1162 rk[6];
wolfSSL 0:d92f9d21154c 1163 t3 =
wolfSSL 0:d92f9d21154c 1164 Te[0][GETBYTE(s3, 3)] ^
wolfSSL 0:d92f9d21154c 1165 Te[1][GETBYTE(s0, 2)] ^
wolfSSL 0:d92f9d21154c 1166 Te[2][GETBYTE(s1, 1)] ^
wolfSSL 0:d92f9d21154c 1167 Te[3][GETBYTE(s2, 0)] ^
wolfSSL 0:d92f9d21154c 1168 rk[7];
wolfSSL 0:d92f9d21154c 1169
wolfSSL 0:d92f9d21154c 1170 rk += 8;
wolfSSL 0:d92f9d21154c 1171 if (--r == 0) {
wolfSSL 0:d92f9d21154c 1172 break;
wolfSSL 0:d92f9d21154c 1173 }
wolfSSL 0:d92f9d21154c 1174
wolfSSL 0:d92f9d21154c 1175 s0 =
wolfSSL 0:d92f9d21154c 1176 Te[0][GETBYTE(t0, 3)] ^
wolfSSL 0:d92f9d21154c 1177 Te[1][GETBYTE(t1, 2)] ^
wolfSSL 0:d92f9d21154c 1178 Te[2][GETBYTE(t2, 1)] ^
wolfSSL 0:d92f9d21154c 1179 Te[3][GETBYTE(t3, 0)] ^
wolfSSL 0:d92f9d21154c 1180 rk[0];
wolfSSL 0:d92f9d21154c 1181 s1 =
wolfSSL 0:d92f9d21154c 1182 Te[0][GETBYTE(t1, 3)] ^
wolfSSL 0:d92f9d21154c 1183 Te[1][GETBYTE(t2, 2)] ^
wolfSSL 0:d92f9d21154c 1184 Te[2][GETBYTE(t3, 1)] ^
wolfSSL 0:d92f9d21154c 1185 Te[3][GETBYTE(t0, 0)] ^
wolfSSL 0:d92f9d21154c 1186 rk[1];
wolfSSL 0:d92f9d21154c 1187 s2 =
wolfSSL 0:d92f9d21154c 1188 Te[0][GETBYTE(t2, 3)] ^
wolfSSL 0:d92f9d21154c 1189 Te[1][GETBYTE(t3, 2)] ^
wolfSSL 0:d92f9d21154c 1190 Te[2][GETBYTE(t0, 1)] ^
wolfSSL 0:d92f9d21154c 1191 Te[3][GETBYTE(t1, 0)] ^
wolfSSL 0:d92f9d21154c 1192 rk[2];
wolfSSL 0:d92f9d21154c 1193 s3 =
wolfSSL 0:d92f9d21154c 1194 Te[0][GETBYTE(t3, 3)] ^
wolfSSL 0:d92f9d21154c 1195 Te[1][GETBYTE(t0, 2)] ^
wolfSSL 0:d92f9d21154c 1196 Te[2][GETBYTE(t1, 1)] ^
wolfSSL 0:d92f9d21154c 1197 Te[3][GETBYTE(t2, 0)] ^
wolfSSL 0:d92f9d21154c 1198 rk[3];
wolfSSL 0:d92f9d21154c 1199 }
wolfSSL 0:d92f9d21154c 1200
wolfSSL 0:d92f9d21154c 1201 /*
wolfSSL 0:d92f9d21154c 1202 * apply last round and
wolfSSL 0:d92f9d21154c 1203 * map cipher state to byte array block:
wolfSSL 0:d92f9d21154c 1204 */
wolfSSL 0:d92f9d21154c 1205
wolfSSL 0:d92f9d21154c 1206 s0 =
wolfSSL 0:d92f9d21154c 1207 (Te[4][GETBYTE(t0, 3)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1208 (Te[4][GETBYTE(t1, 2)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1209 (Te[4][GETBYTE(t2, 1)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1210 (Te[4][GETBYTE(t3, 0)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1211 rk[0];
wolfSSL 0:d92f9d21154c 1212 s1 =
wolfSSL 0:d92f9d21154c 1213 (Te[4][GETBYTE(t1, 3)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1214 (Te[4][GETBYTE(t2, 2)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1215 (Te[4][GETBYTE(t3, 1)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1216 (Te[4][GETBYTE(t0, 0)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1217 rk[1];
wolfSSL 0:d92f9d21154c 1218 s2 =
wolfSSL 0:d92f9d21154c 1219 (Te[4][GETBYTE(t2, 3)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1220 (Te[4][GETBYTE(t3, 2)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1221 (Te[4][GETBYTE(t0, 1)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1222 (Te[4][GETBYTE(t1, 0)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1223 rk[2];
wolfSSL 0:d92f9d21154c 1224 s3 =
wolfSSL 0:d92f9d21154c 1225 (Te[4][GETBYTE(t3, 3)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1226 (Te[4][GETBYTE(t0, 2)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1227 (Te[4][GETBYTE(t1, 1)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1228 (Te[4][GETBYTE(t2, 0)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1229 rk[3];
wolfSSL 0:d92f9d21154c 1230
wolfSSL 0:d92f9d21154c 1231 /* write out */
wolfSSL 0:d92f9d21154c 1232 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 1233 s0 = ByteReverseWord32(s0);
wolfSSL 0:d92f9d21154c 1234 s1 = ByteReverseWord32(s1);
wolfSSL 0:d92f9d21154c 1235 s2 = ByteReverseWord32(s2);
wolfSSL 0:d92f9d21154c 1236 s3 = ByteReverseWord32(s3);
wolfSSL 0:d92f9d21154c 1237 #endif
wolfSSL 0:d92f9d21154c 1238
wolfSSL 0:d92f9d21154c 1239 XMEMCPY(outBlock, &s0, sizeof(s0));
wolfSSL 0:d92f9d21154c 1240 XMEMCPY(outBlock + sizeof(s0), &s1, sizeof(s1));
wolfSSL 0:d92f9d21154c 1241 XMEMCPY(outBlock + 2 * sizeof(s0), &s2, sizeof(s2));
wolfSSL 0:d92f9d21154c 1242 XMEMCPY(outBlock + 3 * sizeof(s0), &s3, sizeof(s3));
wolfSSL 0:d92f9d21154c 1243 }
wolfSSL 0:d92f9d21154c 1244
wolfSSL 0:d92f9d21154c 1245 static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
wolfSSL 0:d92f9d21154c 1246 {
wolfSSL 0:d92f9d21154c 1247 word32 s0, s1, s2, s3;
wolfSSL 0:d92f9d21154c 1248 word32 t0, t1, t2, t3;
wolfSSL 0:d92f9d21154c 1249 word32 r = aes->rounds >> 1;
wolfSSL 0:d92f9d21154c 1250
wolfSSL 0:d92f9d21154c 1251 const word32* rk = aes->key;
wolfSSL 0:d92f9d21154c 1252 if (r > 7 || r == 0) {
wolfSSL 0:d92f9d21154c 1253 WOLFSSL_MSG("AesDecrypt encountered improper key, set it up");
wolfSSL 0:d92f9d21154c 1254 return; /* stop instead of segfaulting, set up your keys! */
wolfSSL 0:d92f9d21154c 1255 }
wolfSSL 0:d92f9d21154c 1256 #ifdef WOLFSSL_AESNI
wolfSSL 0:d92f9d21154c 1257 if (haveAESNI && aes->use_aesni) {
wolfSSL 0:d92f9d21154c 1258 #ifdef DEBUG_AESNI
wolfSSL 0:d92f9d21154c 1259 printf("about to aes decrypt\n");
wolfSSL 0:d92f9d21154c 1260 printf("in = %p\n", inBlock);
wolfSSL 0:d92f9d21154c 1261 printf("out = %p\n", outBlock);
wolfSSL 0:d92f9d21154c 1262 printf("aes->key = %p\n", aes->key);
wolfSSL 0:d92f9d21154c 1263 printf("aes->rounds = %d\n", aes->rounds);
wolfSSL 0:d92f9d21154c 1264 printf("sz = %d\n", AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1265 #endif
wolfSSL 0:d92f9d21154c 1266
wolfSSL 0:d92f9d21154c 1267 /* if input and output same will overwrite input iv */
wolfSSL 0:d92f9d21154c 1268 XMEMCPY(aes->tmp, inBlock, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1269 AES_ECB_decrypt(inBlock, outBlock, AES_BLOCK_SIZE, (byte*)aes->key,
wolfSSL 0:d92f9d21154c 1270 aes->rounds);
wolfSSL 0:d92f9d21154c 1271 return;
wolfSSL 0:d92f9d21154c 1272 }
wolfSSL 0:d92f9d21154c 1273 else {
wolfSSL 0:d92f9d21154c 1274 #ifdef DEBUG_AESNI
wolfSSL 0:d92f9d21154c 1275 printf("Skipping AES-NI\n");
wolfSSL 0:d92f9d21154c 1276 #endif
wolfSSL 0:d92f9d21154c 1277 }
wolfSSL 0:d92f9d21154c 1278 #endif
wolfSSL 0:d92f9d21154c 1279
wolfSSL 0:d92f9d21154c 1280 /*
wolfSSL 0:d92f9d21154c 1281 * map byte array block to cipher state
wolfSSL 0:d92f9d21154c 1282 * and add initial round key:
wolfSSL 0:d92f9d21154c 1283 */
wolfSSL 0:d92f9d21154c 1284 XMEMCPY(&s0, inBlock, sizeof(s0));
wolfSSL 0:d92f9d21154c 1285 XMEMCPY(&s1, inBlock + sizeof(s0), sizeof(s1));
wolfSSL 0:d92f9d21154c 1286 XMEMCPY(&s2, inBlock + 2 * sizeof(s0), sizeof(s2));
wolfSSL 0:d92f9d21154c 1287 XMEMCPY(&s3, inBlock + 3 * sizeof(s0), sizeof(s3));
wolfSSL 0:d92f9d21154c 1288
wolfSSL 0:d92f9d21154c 1289 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 1290 s0 = ByteReverseWord32(s0);
wolfSSL 0:d92f9d21154c 1291 s1 = ByteReverseWord32(s1);
wolfSSL 0:d92f9d21154c 1292 s2 = ByteReverseWord32(s2);
wolfSSL 0:d92f9d21154c 1293 s3 = ByteReverseWord32(s3);
wolfSSL 0:d92f9d21154c 1294 #endif
wolfSSL 0:d92f9d21154c 1295
wolfSSL 0:d92f9d21154c 1296 s0 ^= rk[0];
wolfSSL 0:d92f9d21154c 1297 s1 ^= rk[1];
wolfSSL 0:d92f9d21154c 1298 s2 ^= rk[2];
wolfSSL 0:d92f9d21154c 1299 s3 ^= rk[3];
wolfSSL 0:d92f9d21154c 1300
wolfSSL 0:d92f9d21154c 1301 /*
wolfSSL 0:d92f9d21154c 1302 * Nr - 1 full rounds:
wolfSSL 0:d92f9d21154c 1303 */
wolfSSL 0:d92f9d21154c 1304
wolfSSL 0:d92f9d21154c 1305 for (;;) {
wolfSSL 0:d92f9d21154c 1306 t0 =
wolfSSL 0:d92f9d21154c 1307 Td[0][GETBYTE(s0, 3)] ^
wolfSSL 0:d92f9d21154c 1308 Td[1][GETBYTE(s3, 2)] ^
wolfSSL 0:d92f9d21154c 1309 Td[2][GETBYTE(s2, 1)] ^
wolfSSL 0:d92f9d21154c 1310 Td[3][GETBYTE(s1, 0)] ^
wolfSSL 0:d92f9d21154c 1311 rk[4];
wolfSSL 0:d92f9d21154c 1312 t1 =
wolfSSL 0:d92f9d21154c 1313 Td[0][GETBYTE(s1, 3)] ^
wolfSSL 0:d92f9d21154c 1314 Td[1][GETBYTE(s0, 2)] ^
wolfSSL 0:d92f9d21154c 1315 Td[2][GETBYTE(s3, 1)] ^
wolfSSL 0:d92f9d21154c 1316 Td[3][GETBYTE(s2, 0)] ^
wolfSSL 0:d92f9d21154c 1317 rk[5];
wolfSSL 0:d92f9d21154c 1318 t2 =
wolfSSL 0:d92f9d21154c 1319 Td[0][GETBYTE(s2, 3)] ^
wolfSSL 0:d92f9d21154c 1320 Td[1][GETBYTE(s1, 2)] ^
wolfSSL 0:d92f9d21154c 1321 Td[2][GETBYTE(s0, 1)] ^
wolfSSL 0:d92f9d21154c 1322 Td[3][GETBYTE(s3, 0)] ^
wolfSSL 0:d92f9d21154c 1323 rk[6];
wolfSSL 0:d92f9d21154c 1324 t3 =
wolfSSL 0:d92f9d21154c 1325 Td[0][GETBYTE(s3, 3)] ^
wolfSSL 0:d92f9d21154c 1326 Td[1][GETBYTE(s2, 2)] ^
wolfSSL 0:d92f9d21154c 1327 Td[2][GETBYTE(s1, 1)] ^
wolfSSL 0:d92f9d21154c 1328 Td[3][GETBYTE(s0, 0)] ^
wolfSSL 0:d92f9d21154c 1329 rk[7];
wolfSSL 0:d92f9d21154c 1330
wolfSSL 0:d92f9d21154c 1331 rk += 8;
wolfSSL 0:d92f9d21154c 1332 if (--r == 0) {
wolfSSL 0:d92f9d21154c 1333 break;
wolfSSL 0:d92f9d21154c 1334 }
wolfSSL 0:d92f9d21154c 1335
wolfSSL 0:d92f9d21154c 1336 s0 =
wolfSSL 0:d92f9d21154c 1337 Td[0][GETBYTE(t0, 3)] ^
wolfSSL 0:d92f9d21154c 1338 Td[1][GETBYTE(t3, 2)] ^
wolfSSL 0:d92f9d21154c 1339 Td[2][GETBYTE(t2, 1)] ^
wolfSSL 0:d92f9d21154c 1340 Td[3][GETBYTE(t1, 0)] ^
wolfSSL 0:d92f9d21154c 1341 rk[0];
wolfSSL 0:d92f9d21154c 1342 s1 =
wolfSSL 0:d92f9d21154c 1343 Td[0][GETBYTE(t1, 3)] ^
wolfSSL 0:d92f9d21154c 1344 Td[1][GETBYTE(t0, 2)] ^
wolfSSL 0:d92f9d21154c 1345 Td[2][GETBYTE(t3, 1)] ^
wolfSSL 0:d92f9d21154c 1346 Td[3][GETBYTE(t2, 0)] ^
wolfSSL 0:d92f9d21154c 1347 rk[1];
wolfSSL 0:d92f9d21154c 1348 s2 =
wolfSSL 0:d92f9d21154c 1349 Td[0][GETBYTE(t2, 3)] ^
wolfSSL 0:d92f9d21154c 1350 Td[1][GETBYTE(t1, 2)] ^
wolfSSL 0:d92f9d21154c 1351 Td[2][GETBYTE(t0, 1)] ^
wolfSSL 0:d92f9d21154c 1352 Td[3][GETBYTE(t3, 0)] ^
wolfSSL 0:d92f9d21154c 1353 rk[2];
wolfSSL 0:d92f9d21154c 1354 s3 =
wolfSSL 0:d92f9d21154c 1355 Td[0][GETBYTE(t3, 3)] ^
wolfSSL 0:d92f9d21154c 1356 Td[1][GETBYTE(t2, 2)] ^
wolfSSL 0:d92f9d21154c 1357 Td[2][GETBYTE(t1, 1)] ^
wolfSSL 0:d92f9d21154c 1358 Td[3][GETBYTE(t0, 0)] ^
wolfSSL 0:d92f9d21154c 1359 rk[3];
wolfSSL 0:d92f9d21154c 1360 }
wolfSSL 0:d92f9d21154c 1361 /*
wolfSSL 0:d92f9d21154c 1362 * apply last round and
wolfSSL 0:d92f9d21154c 1363 * map cipher state to byte array block:
wolfSSL 0:d92f9d21154c 1364 */
wolfSSL 0:d92f9d21154c 1365 s0 =
wolfSSL 0:d92f9d21154c 1366 (Td[4][GETBYTE(t0, 3)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1367 (Td[4][GETBYTE(t3, 2)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1368 (Td[4][GETBYTE(t2, 1)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1369 (Td[4][GETBYTE(t1, 0)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1370 rk[0];
wolfSSL 0:d92f9d21154c 1371 s1 =
wolfSSL 0:d92f9d21154c 1372 (Td[4][GETBYTE(t1, 3)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1373 (Td[4][GETBYTE(t0, 2)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1374 (Td[4][GETBYTE(t3, 1)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1375 (Td[4][GETBYTE(t2, 0)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1376 rk[1];
wolfSSL 0:d92f9d21154c 1377 s2 =
wolfSSL 0:d92f9d21154c 1378 (Td[4][GETBYTE(t2, 3)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1379 (Td[4][GETBYTE(t1, 2)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1380 (Td[4][GETBYTE(t0, 1)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1381 (Td[4][GETBYTE(t3, 0)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1382 rk[2];
wolfSSL 0:d92f9d21154c 1383 s3 =
wolfSSL 0:d92f9d21154c 1384 (Td[4][GETBYTE(t3, 3)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1385 (Td[4][GETBYTE(t2, 2)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1386 (Td[4][GETBYTE(t1, 1)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1387 (Td[4][GETBYTE(t0, 0)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1388 rk[3];
wolfSSL 0:d92f9d21154c 1389
wolfSSL 0:d92f9d21154c 1390 /* write out */
wolfSSL 0:d92f9d21154c 1391 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 1392 s0 = ByteReverseWord32(s0);
wolfSSL 0:d92f9d21154c 1393 s1 = ByteReverseWord32(s1);
wolfSSL 0:d92f9d21154c 1394 s2 = ByteReverseWord32(s2);
wolfSSL 0:d92f9d21154c 1395 s3 = ByteReverseWord32(s3);
wolfSSL 0:d92f9d21154c 1396 #endif
wolfSSL 0:d92f9d21154c 1397
wolfSSL 0:d92f9d21154c 1398 XMEMCPY(outBlock, &s0, sizeof(s0));
wolfSSL 0:d92f9d21154c 1399 XMEMCPY(outBlock + sizeof(s0), &s1, sizeof(s1));
wolfSSL 0:d92f9d21154c 1400 XMEMCPY(outBlock + 2 * sizeof(s0), &s2, sizeof(s2));
wolfSSL 0:d92f9d21154c 1401 XMEMCPY(outBlock + 3 * sizeof(s0), &s3, sizeof(s3));
wolfSSL 0:d92f9d21154c 1402 }
wolfSSL 0:d92f9d21154c 1403
wolfSSL 0:d92f9d21154c 1404 #endif /* NEED_AES_TABLES */
wolfSSL 0:d92f9d21154c 1405
wolfSSL 0:d92f9d21154c 1406
wolfSSL 0:d92f9d21154c 1407 /* wc_AesSetKey */
wolfSSL 0:d92f9d21154c 1408 #ifdef STM32F2_CRYPTO
wolfSSL 0:d92f9d21154c 1409 int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
wolfSSL 0:d92f9d21154c 1410 int dir)
wolfSSL 0:d92f9d21154c 1411 {
wolfSSL 0:d92f9d21154c 1412 word32 *rk = aes->key;
wolfSSL 0:d92f9d21154c 1413
wolfSSL 0:d92f9d21154c 1414 if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
wolfSSL 0:d92f9d21154c 1415 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1416
wolfSSL 0:d92f9d21154c 1417 aes->rounds = keylen/4 + 6;
wolfSSL 0:d92f9d21154c 1418 XMEMCPY(rk, userKey, keylen);
wolfSSL 0:d92f9d21154c 1419 ByteReverseWords(rk, rk, keylen);
wolfSSL 0:d92f9d21154c 1420
wolfSSL 0:d92f9d21154c 1421 return wc_AesSetIV(aes, iv);
wolfSSL 0:d92f9d21154c 1422 }
wolfSSL 0:d92f9d21154c 1423
wolfSSL 0:d92f9d21154c 1424 int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 0:d92f9d21154c 1425 const byte* iv, int dir)
wolfSSL 0:d92f9d21154c 1426 {
wolfSSL 0:d92f9d21154c 1427 return wc_AesSetKey(aes, userKey, keylen, iv, dir);
wolfSSL 0:d92f9d21154c 1428 }
wolfSSL 0:d92f9d21154c 1429
wolfSSL 0:d92f9d21154c 1430 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 0:d92f9d21154c 1431 #if defined (HAVE_THREADX)
wolfSSL 0:d92f9d21154c 1432 #include "memory_pools.h"
wolfSSL 0:d92f9d21154c 1433 extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */
wolfSSL 0:d92f9d21154c 1434 #endif
wolfSSL 0:d92f9d21154c 1435
wolfSSL 0:d92f9d21154c 1436 #define AES_BUFFER_SIZE (AES_BLOCK_SIZE * 64)
wolfSSL 0:d92f9d21154c 1437 static unsigned char *AESBuffIn = NULL;
wolfSSL 0:d92f9d21154c 1438 static unsigned char *AESBuffOut = NULL;
wolfSSL 0:d92f9d21154c 1439 static byte *secReg;
wolfSSL 0:d92f9d21154c 1440 static byte *secKey;
wolfSSL 0:d92f9d21154c 1441 static volatile SECdescriptorType *secDesc;
wolfSSL 0:d92f9d21154c 1442
wolfSSL 0:d92f9d21154c 1443 static wolfSSL_Mutex Mutex_AesSEC;
wolfSSL 0:d92f9d21154c 1444
wolfSSL 0:d92f9d21154c 1445 #define SEC_DESC_AES_CBC_ENCRYPT 0x60300010
wolfSSL 0:d92f9d21154c 1446 #define SEC_DESC_AES_CBC_DECRYPT 0x60200010
wolfSSL 0:d92f9d21154c 1447
wolfSSL 0:d92f9d21154c 1448 extern volatile unsigned char __MBAR[];
wolfSSL 0:d92f9d21154c 1449
wolfSSL 0:d92f9d21154c 1450 int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
wolfSSL 0:d92f9d21154c 1451 int dir)
wolfSSL 0:d92f9d21154c 1452 {
wolfSSL 0:d92f9d21154c 1453 if (AESBuffIn == NULL) {
wolfSSL 0:d92f9d21154c 1454 #if defined (HAVE_THREADX)
wolfSSL 0:d92f9d21154c 1455 int s1, s2, s3, s4, s5 ;
wolfSSL 0:d92f9d21154c 1456 s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc,
wolfSSL 0:d92f9d21154c 1457 sizeof(SECdescriptorType), TX_NO_WAIT);
wolfSSL 0:d92f9d21154c 1458 s1 = tx_byte_allocate(&mp_ncached, (void *)&AESBuffIn,
wolfSSL 0:d92f9d21154c 1459 AES_BUFFER_SIZE, TX_NO_WAIT);
wolfSSL 0:d92f9d21154c 1460 s2 = tx_byte_allocate(&mp_ncached, (void *)&AESBuffOut,
wolfSSL 0:d92f9d21154c 1461 AES_BUFFER_SIZE, TX_NO_WAIT);
wolfSSL 0:d92f9d21154c 1462 s3 = tx_byte_allocate(&mp_ncached, (void *)&secKey,
wolfSSL 0:d92f9d21154c 1463 AES_BLOCK_SIZE*2, TX_NO_WAIT);
wolfSSL 0:d92f9d21154c 1464 s4 = tx_byte_allocate(&mp_ncached, (void *)&secReg,
wolfSSL 0:d92f9d21154c 1465 AES_BLOCK_SIZE, TX_NO_WAIT);
wolfSSL 0:d92f9d21154c 1466
wolfSSL 0:d92f9d21154c 1467 if(s1 || s2 || s3 || s4 || s5)
wolfSSL 0:d92f9d21154c 1468 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1469 #else
wolfSSL 0:d92f9d21154c 1470 #warning "Allocate non-Cache buffers"
wolfSSL 0:d92f9d21154c 1471 #endif
wolfSSL 0:d92f9d21154c 1472
wolfSSL 0:d92f9d21154c 1473 InitMutex(&Mutex_AesSEC);
wolfSSL 0:d92f9d21154c 1474 }
wolfSSL 0:d92f9d21154c 1475
wolfSSL 0:d92f9d21154c 1476 if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
wolfSSL 0:d92f9d21154c 1477 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1478
wolfSSL 0:d92f9d21154c 1479 if (aes == NULL)
wolfSSL 0:d92f9d21154c 1480 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1481
wolfSSL 0:d92f9d21154c 1482 aes->rounds = keylen/4 + 6;
wolfSSL 0:d92f9d21154c 1483 XMEMCPY(aes->key, userKey, keylen);
wolfSSL 0:d92f9d21154c 1484
wolfSSL 0:d92f9d21154c 1485 if (iv)
wolfSSL 0:d92f9d21154c 1486 XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1487
wolfSSL 0:d92f9d21154c 1488 return 0;
wolfSSL 0:d92f9d21154c 1489 }
wolfSSL 0:d92f9d21154c 1490 #elif defined(FREESCALE_MMCAU)
wolfSSL 0:d92f9d21154c 1491 int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
wolfSSL 0:d92f9d21154c 1492 int dir)
wolfSSL 0:d92f9d21154c 1493 {
wolfSSL 0:d92f9d21154c 1494 byte *rk = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 1495
wolfSSL 0:d92f9d21154c 1496 if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
wolfSSL 0:d92f9d21154c 1497 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1498
wolfSSL 0:d92f9d21154c 1499 if (rk == NULL)
wolfSSL 0:d92f9d21154c 1500 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1501
wolfSSL 0:d92f9d21154c 1502 aes->rounds = keylen/4 + 6;
wolfSSL 0:d92f9d21154c 1503 cau_aes_set_key(userKey, keylen*8, rk);
wolfSSL 0:d92f9d21154c 1504
wolfSSL 0:d92f9d21154c 1505 return wc_AesSetIV(aes, iv);
wolfSSL 0:d92f9d21154c 1506 }
wolfSSL 0:d92f9d21154c 1507
wolfSSL 0:d92f9d21154c 1508 int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 0:d92f9d21154c 1509 const byte* iv, int dir)
wolfSSL 0:d92f9d21154c 1510 {
wolfSSL 0:d92f9d21154c 1511 return wc_AesSetKey(aes, userKey, keylen, iv, dir);
wolfSSL 0:d92f9d21154c 1512 }
wolfSSL 0:d92f9d21154c 1513 #else
wolfSSL 0:d92f9d21154c 1514 static int wc_AesSetKeyLocal(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 0:d92f9d21154c 1515 const byte* iv, int dir)
wolfSSL 0:d92f9d21154c 1516 {
wolfSSL 0:d92f9d21154c 1517 word32 temp, *rk = aes->key;
wolfSSL 0:d92f9d21154c 1518 unsigned int i = 0;
wolfSSL 0:d92f9d21154c 1519
wolfSSL 0:d92f9d21154c 1520 #ifdef WOLFSSL_AESNI
wolfSSL 0:d92f9d21154c 1521 aes->use_aesni = 0;
wolfSSL 0:d92f9d21154c 1522 #endif /* WOLFSSL_AESNI */
wolfSSL 0:d92f9d21154c 1523 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 0:d92f9d21154c 1524 aes->left = 0;
wolfSSL 0:d92f9d21154c 1525 #endif /* WOLFSSL_AES_COUNTER */
wolfSSL 0:d92f9d21154c 1526
wolfSSL 0:d92f9d21154c 1527 aes->rounds = keylen/4 + 6;
wolfSSL 0:d92f9d21154c 1528
wolfSSL 0:d92f9d21154c 1529 XMEMCPY(rk, userKey, keylen);
wolfSSL 0:d92f9d21154c 1530 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 1531 ByteReverseWords(rk, rk, keylen);
wolfSSL 0:d92f9d21154c 1532 #endif
wolfSSL 0:d92f9d21154c 1533
wolfSSL 0:d92f9d21154c 1534 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 0:d92f9d21154c 1535 {
wolfSSL 0:d92f9d21154c 1536 word32 *akey1 = aes->key_ce;
wolfSSL 0:d92f9d21154c 1537 word32 *areg = aes->iv_ce ;
wolfSSL 0:d92f9d21154c 1538 aes->keylen = keylen ;
wolfSSL 0:d92f9d21154c 1539 XMEMCPY(akey1, userKey, keylen);
wolfSSL 0:d92f9d21154c 1540 if (iv)
wolfSSL 0:d92f9d21154c 1541 XMEMCPY(areg, iv, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1542 else
wolfSSL 0:d92f9d21154c 1543 XMEMSET(areg, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1544 }
wolfSSL 0:d92f9d21154c 1545 #endif
wolfSSL 0:d92f9d21154c 1546
wolfSSL 0:d92f9d21154c 1547 switch(keylen)
wolfSSL 0:d92f9d21154c 1548 {
wolfSSL 0:d92f9d21154c 1549 case 16:
wolfSSL 0:d92f9d21154c 1550 while (1)
wolfSSL 0:d92f9d21154c 1551 {
wolfSSL 0:d92f9d21154c 1552 temp = rk[3];
wolfSSL 0:d92f9d21154c 1553 rk[4] = rk[0] ^
wolfSSL 0:d92f9d21154c 1554 (Te[4][GETBYTE(temp, 2)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1555 (Te[4][GETBYTE(temp, 1)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1556 (Te[4][GETBYTE(temp, 0)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1557 (Te[4][GETBYTE(temp, 3)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1558 rcon[i];
wolfSSL 0:d92f9d21154c 1559 rk[5] = rk[1] ^ rk[4];
wolfSSL 0:d92f9d21154c 1560 rk[6] = rk[2] ^ rk[5];
wolfSSL 0:d92f9d21154c 1561 rk[7] = rk[3] ^ rk[6];
wolfSSL 0:d92f9d21154c 1562 if (++i == 10)
wolfSSL 0:d92f9d21154c 1563 break;
wolfSSL 0:d92f9d21154c 1564 rk += 4;
wolfSSL 0:d92f9d21154c 1565 }
wolfSSL 0:d92f9d21154c 1566 break;
wolfSSL 0:d92f9d21154c 1567
wolfSSL 0:d92f9d21154c 1568 case 24:
wolfSSL 0:d92f9d21154c 1569 /* for (;;) here triggers a bug in VC60 SP4 w/ Pro Pack */
wolfSSL 0:d92f9d21154c 1570 while (1)
wolfSSL 0:d92f9d21154c 1571 {
wolfSSL 0:d92f9d21154c 1572 temp = rk[ 5];
wolfSSL 0:d92f9d21154c 1573 rk[ 6] = rk[ 0] ^
wolfSSL 0:d92f9d21154c 1574 (Te[4][GETBYTE(temp, 2)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1575 (Te[4][GETBYTE(temp, 1)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1576 (Te[4][GETBYTE(temp, 0)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1577 (Te[4][GETBYTE(temp, 3)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1578 rcon[i];
wolfSSL 0:d92f9d21154c 1579 rk[ 7] = rk[ 1] ^ rk[ 6];
wolfSSL 0:d92f9d21154c 1580 rk[ 8] = rk[ 2] ^ rk[ 7];
wolfSSL 0:d92f9d21154c 1581 rk[ 9] = rk[ 3] ^ rk[ 8];
wolfSSL 0:d92f9d21154c 1582 if (++i == 8)
wolfSSL 0:d92f9d21154c 1583 break;
wolfSSL 0:d92f9d21154c 1584 rk[10] = rk[ 4] ^ rk[ 9];
wolfSSL 0:d92f9d21154c 1585 rk[11] = rk[ 5] ^ rk[10];
wolfSSL 0:d92f9d21154c 1586 rk += 6;
wolfSSL 0:d92f9d21154c 1587 }
wolfSSL 0:d92f9d21154c 1588 break;
wolfSSL 0:d92f9d21154c 1589
wolfSSL 0:d92f9d21154c 1590 case 32:
wolfSSL 0:d92f9d21154c 1591 while (1)
wolfSSL 0:d92f9d21154c 1592 {
wolfSSL 0:d92f9d21154c 1593 temp = rk[ 7];
wolfSSL 0:d92f9d21154c 1594 rk[ 8] = rk[ 0] ^
wolfSSL 0:d92f9d21154c 1595 (Te[4][GETBYTE(temp, 2)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1596 (Te[4][GETBYTE(temp, 1)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1597 (Te[4][GETBYTE(temp, 0)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1598 (Te[4][GETBYTE(temp, 3)] & 0x000000ff) ^
wolfSSL 0:d92f9d21154c 1599 rcon[i];
wolfSSL 0:d92f9d21154c 1600 rk[ 9] = rk[ 1] ^ rk[ 8];
wolfSSL 0:d92f9d21154c 1601 rk[10] = rk[ 2] ^ rk[ 9];
wolfSSL 0:d92f9d21154c 1602 rk[11] = rk[ 3] ^ rk[10];
wolfSSL 0:d92f9d21154c 1603 if (++i == 7)
wolfSSL 0:d92f9d21154c 1604 break;
wolfSSL 0:d92f9d21154c 1605 temp = rk[11];
wolfSSL 0:d92f9d21154c 1606 rk[12] = rk[ 4] ^
wolfSSL 0:d92f9d21154c 1607 (Te[4][GETBYTE(temp, 3)] & 0xff000000) ^
wolfSSL 0:d92f9d21154c 1608 (Te[4][GETBYTE(temp, 2)] & 0x00ff0000) ^
wolfSSL 0:d92f9d21154c 1609 (Te[4][GETBYTE(temp, 1)] & 0x0000ff00) ^
wolfSSL 0:d92f9d21154c 1610 (Te[4][GETBYTE(temp, 0)] & 0x000000ff);
wolfSSL 0:d92f9d21154c 1611 rk[13] = rk[ 5] ^ rk[12];
wolfSSL 0:d92f9d21154c 1612 rk[14] = rk[ 6] ^ rk[13];
wolfSSL 0:d92f9d21154c 1613 rk[15] = rk[ 7] ^ rk[14];
wolfSSL 0:d92f9d21154c 1614
wolfSSL 0:d92f9d21154c 1615 rk += 8;
wolfSSL 0:d92f9d21154c 1616 }
wolfSSL 0:d92f9d21154c 1617 break;
wolfSSL 0:d92f9d21154c 1618
wolfSSL 0:d92f9d21154c 1619 default:
wolfSSL 0:d92f9d21154c 1620 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1621 }
wolfSSL 0:d92f9d21154c 1622
wolfSSL 0:d92f9d21154c 1623 if (dir == AES_DECRYPTION)
wolfSSL 0:d92f9d21154c 1624 {
wolfSSL 0:d92f9d21154c 1625 unsigned int j;
wolfSSL 0:d92f9d21154c 1626 rk = aes->key;
wolfSSL 0:d92f9d21154c 1627
wolfSSL 0:d92f9d21154c 1628 /* invert the order of the round keys: */
wolfSSL 0:d92f9d21154c 1629 for (i = 0, j = 4* aes->rounds; i < j; i += 4, j -= 4) {
wolfSSL 0:d92f9d21154c 1630 temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
wolfSSL 0:d92f9d21154c 1631 temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
wolfSSL 0:d92f9d21154c 1632 temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
wolfSSL 0:d92f9d21154c 1633 temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
wolfSSL 0:d92f9d21154c 1634 }
wolfSSL 0:d92f9d21154c 1635 /* apply the inverse MixColumn transform to all round keys but the
wolfSSL 0:d92f9d21154c 1636 first and the last: */
wolfSSL 0:d92f9d21154c 1637 for (i = 1; i < aes->rounds; i++) {
wolfSSL 0:d92f9d21154c 1638 rk += 4;
wolfSSL 0:d92f9d21154c 1639 rk[0] =
wolfSSL 0:d92f9d21154c 1640 Td[0][Te[4][GETBYTE(rk[0], 3)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1641 Td[1][Te[4][GETBYTE(rk[0], 2)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1642 Td[2][Te[4][GETBYTE(rk[0], 1)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1643 Td[3][Te[4][GETBYTE(rk[0], 0)] & 0xff];
wolfSSL 0:d92f9d21154c 1644 rk[1] =
wolfSSL 0:d92f9d21154c 1645 Td[0][Te[4][GETBYTE(rk[1], 3)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1646 Td[1][Te[4][GETBYTE(rk[1], 2)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1647 Td[2][Te[4][GETBYTE(rk[1], 1)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1648 Td[3][Te[4][GETBYTE(rk[1], 0)] & 0xff];
wolfSSL 0:d92f9d21154c 1649 rk[2] =
wolfSSL 0:d92f9d21154c 1650 Td[0][Te[4][GETBYTE(rk[2], 3)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1651 Td[1][Te[4][GETBYTE(rk[2], 2)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1652 Td[2][Te[4][GETBYTE(rk[2], 1)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1653 Td[3][Te[4][GETBYTE(rk[2], 0)] & 0xff];
wolfSSL 0:d92f9d21154c 1654 rk[3] =
wolfSSL 0:d92f9d21154c 1655 Td[0][Te[4][GETBYTE(rk[3], 3)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1656 Td[1][Te[4][GETBYTE(rk[3], 2)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1657 Td[2][Te[4][GETBYTE(rk[3], 1)] & 0xff] ^
wolfSSL 0:d92f9d21154c 1658 Td[3][Te[4][GETBYTE(rk[3], 0)] & 0xff];
wolfSSL 0:d92f9d21154c 1659 }
wolfSSL 0:d92f9d21154c 1660 }
wolfSSL 0:d92f9d21154c 1661
wolfSSL 0:d92f9d21154c 1662 return wc_AesSetIV(aes, iv);
wolfSSL 0:d92f9d21154c 1663 }
wolfSSL 0:d92f9d21154c 1664
wolfSSL 0:d92f9d21154c 1665 int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
wolfSSL 0:d92f9d21154c 1666 int dir)
wolfSSL 0:d92f9d21154c 1667 {
wolfSSL 0:d92f9d21154c 1668
wolfSSL 0:d92f9d21154c 1669 if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
wolfSSL 0:d92f9d21154c 1670 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1671
wolfSSL 0:d92f9d21154c 1672 #ifdef HAVE_CAVIUM
wolfSSL 0:d92f9d21154c 1673 if (aes->magic == WOLFSSL_AES_CAVIUM_MAGIC)
wolfSSL 0:d92f9d21154c 1674 return wc_AesCaviumSetKey(aes, userKey, keylen, iv);
wolfSSL 0:d92f9d21154c 1675 #endif
wolfSSL 0:d92f9d21154c 1676
wolfSSL 0:d92f9d21154c 1677 #ifdef WOLFSSL_AESNI
wolfSSL 0:d92f9d21154c 1678 if (checkAESNI == 0) {
wolfSSL 0:d92f9d21154c 1679 haveAESNI = Check_CPU_support_AES();
wolfSSL 0:d92f9d21154c 1680 checkAESNI = 1;
wolfSSL 0:d92f9d21154c 1681 }
wolfSSL 0:d92f9d21154c 1682 if (haveAESNI) {
wolfSSL 0:d92f9d21154c 1683 aes->use_aesni = 1;
wolfSSL 0:d92f9d21154c 1684 if (iv)
wolfSSL 0:d92f9d21154c 1685 XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1686 if (dir == AES_ENCRYPTION)
wolfSSL 0:d92f9d21154c 1687 return AES_set_encrypt_key(userKey, keylen * 8, aes);
wolfSSL 0:d92f9d21154c 1688 else
wolfSSL 0:d92f9d21154c 1689 return AES_set_decrypt_key(userKey, keylen * 8, aes);
wolfSSL 0:d92f9d21154c 1690 }
wolfSSL 0:d92f9d21154c 1691 #endif /* WOLFSSL_AESNI */
wolfSSL 0:d92f9d21154c 1692
wolfSSL 0:d92f9d21154c 1693 return wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir);
wolfSSL 0:d92f9d21154c 1694 }
wolfSSL 0:d92f9d21154c 1695
wolfSSL 0:d92f9d21154c 1696 #if defined(WOLFSSL_AES_DIRECT) || defined(WOLFSSL_AES_COUNTER)
wolfSSL 0:d92f9d21154c 1697
wolfSSL 0:d92f9d21154c 1698 /* AES-CTR and AES-DIRECT need to use this for key setup, no aesni yet */
wolfSSL 0:d92f9d21154c 1699 int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
wolfSSL 0:d92f9d21154c 1700 const byte* iv, int dir)
wolfSSL 0:d92f9d21154c 1701 {
wolfSSL 0:d92f9d21154c 1702 return wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir);
wolfSSL 0:d92f9d21154c 1703 }
wolfSSL 0:d92f9d21154c 1704
wolfSSL 0:d92f9d21154c 1705 #endif /* WOLFSSL_AES_DIRECT || WOLFSSL_AES_COUNTER */
wolfSSL 0:d92f9d21154c 1706 #endif /* STM32F2_CRYPTO, wc_AesSetKey block */
wolfSSL 0:d92f9d21154c 1707
wolfSSL 0:d92f9d21154c 1708
wolfSSL 0:d92f9d21154c 1709 /* wc_AesSetIV is shared between software and hardware */
wolfSSL 0:d92f9d21154c 1710 int wc_AesSetIV(Aes* aes, const byte* iv)
wolfSSL 0:d92f9d21154c 1711 {
wolfSSL 0:d92f9d21154c 1712 if (aes == NULL)
wolfSSL 0:d92f9d21154c 1713 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 1714
wolfSSL 0:d92f9d21154c 1715 if (iv)
wolfSSL 0:d92f9d21154c 1716 XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1717 else
wolfSSL 0:d92f9d21154c 1718 XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1719
wolfSSL 0:d92f9d21154c 1720 return 0;
wolfSSL 0:d92f9d21154c 1721 }
wolfSSL 0:d92f9d21154c 1722
wolfSSL 0:d92f9d21154c 1723
wolfSSL 0:d92f9d21154c 1724 int wc_AesCbcDecryptWithKey(byte* out, const byte* in, word32 inSz,
wolfSSL 0:d92f9d21154c 1725 const byte* key, word32 keySz, const byte* iv)
wolfSSL 0:d92f9d21154c 1726 {
wolfSSL 0:d92f9d21154c 1727 int ret = 0;
wolfSSL 0:d92f9d21154c 1728 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 0:d92f9d21154c 1729 Aes* aes = NULL;
wolfSSL 0:d92f9d21154c 1730 #else
wolfSSL 0:d92f9d21154c 1731 Aes aes[1];
wolfSSL 0:d92f9d21154c 1732 #endif
wolfSSL 0:d92f9d21154c 1733
wolfSSL 0:d92f9d21154c 1734 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 0:d92f9d21154c 1735 aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:d92f9d21154c 1736 if (aes == NULL)
wolfSSL 0:d92f9d21154c 1737 return MEMORY_E;
wolfSSL 0:d92f9d21154c 1738 #endif
wolfSSL 0:d92f9d21154c 1739
wolfSSL 0:d92f9d21154c 1740 ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
wolfSSL 0:d92f9d21154c 1741 if (ret == 0)
wolfSSL 0:d92f9d21154c 1742 ret = wc_AesCbcDecrypt(aes, out, in, inSz);
wolfSSL 0:d92f9d21154c 1743
wolfSSL 0:d92f9d21154c 1744 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 0:d92f9d21154c 1745 XFREE(aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:d92f9d21154c 1746 #endif
wolfSSL 0:d92f9d21154c 1747
wolfSSL 0:d92f9d21154c 1748 return ret;
wolfSSL 0:d92f9d21154c 1749 }
wolfSSL 0:d92f9d21154c 1750
wolfSSL 0:d92f9d21154c 1751
wolfSSL 0:d92f9d21154c 1752 /* AES-DIRECT */
wolfSSL 0:d92f9d21154c 1753 #if defined(WOLFSSL_AES_DIRECT)
wolfSSL 0:d92f9d21154c 1754 #if defined(FREESCALE_MMCAU)
wolfSSL 0:d92f9d21154c 1755
wolfSSL 0:d92f9d21154c 1756 /* Allow direct access to one block encrypt */
wolfSSL 0:d92f9d21154c 1757 void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 0:d92f9d21154c 1758 {
wolfSSL 0:d92f9d21154c 1759 byte* key;
wolfSSL 0:d92f9d21154c 1760 key = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 1761
wolfSSL 0:d92f9d21154c 1762 return cau_aes_encrypt(in, key, aes->rounds, out);
wolfSSL 0:d92f9d21154c 1763 }
wolfSSL 0:d92f9d21154c 1764
wolfSSL 0:d92f9d21154c 1765 /* Allow direct access to one block decrypt */
wolfSSL 0:d92f9d21154c 1766 void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 0:d92f9d21154c 1767 {
wolfSSL 0:d92f9d21154c 1768 byte* key;
wolfSSL 0:d92f9d21154c 1769 key = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 1770
wolfSSL 0:d92f9d21154c 1771 return cau_aes_decrypt(in, key, aes->rounds, out);
wolfSSL 0:d92f9d21154c 1772 }
wolfSSL 0:d92f9d21154c 1773
wolfSSL 0:d92f9d21154c 1774 #elif defined(STM32F2_CRYPTO)
wolfSSL 0:d92f9d21154c 1775 #error "STM32F2 crypto doesn't yet support AES direct"
wolfSSL 0:d92f9d21154c 1776
wolfSSL 0:d92f9d21154c 1777 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 0:d92f9d21154c 1778 #error "Coldfire SEC doesn't yet support AES direct"
wolfSSL 0:d92f9d21154c 1779
wolfSSL 0:d92f9d21154c 1780 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 0:d92f9d21154c 1781 #error "PIC32MZ doesn't yet support AES direct"
wolfSSL 0:d92f9d21154c 1782
wolfSSL 0:d92f9d21154c 1783 #else
wolfSSL 0:d92f9d21154c 1784 /* Allow direct access to one block encrypt */
wolfSSL 0:d92f9d21154c 1785 void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 0:d92f9d21154c 1786 {
wolfSSL 0:d92f9d21154c 1787 wc_AesEncrypt(aes, in, out);
wolfSSL 0:d92f9d21154c 1788 }
wolfSSL 0:d92f9d21154c 1789
wolfSSL 0:d92f9d21154c 1790 /* Allow direct access to one block decrypt */
wolfSSL 0:d92f9d21154c 1791 void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
wolfSSL 0:d92f9d21154c 1792 {
wolfSSL 0:d92f9d21154c 1793 wc_AesDecrypt(aes, in, out);
wolfSSL 0:d92f9d21154c 1794 }
wolfSSL 0:d92f9d21154c 1795
wolfSSL 0:d92f9d21154c 1796 #endif /* FREESCALE_MMCAU, AES direct block */
wolfSSL 0:d92f9d21154c 1797 #endif /* WOLFSSL_AES_DIRECT */
wolfSSL 0:d92f9d21154c 1798
wolfSSL 0:d92f9d21154c 1799
wolfSSL 0:d92f9d21154c 1800 /* AES-CBC */
wolfSSL 0:d92f9d21154c 1801 #ifdef STM32F2_CRYPTO
wolfSSL 0:d92f9d21154c 1802 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 1803 {
wolfSSL 0:d92f9d21154c 1804 word32 *enc_key, *iv;
wolfSSL 0:d92f9d21154c 1805 CRYP_InitTypeDef AES_CRYP_InitStructure;
wolfSSL 0:d92f9d21154c 1806 CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
wolfSSL 0:d92f9d21154c 1807 CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
wolfSSL 0:d92f9d21154c 1808
wolfSSL 0:d92f9d21154c 1809 enc_key = aes->key;
wolfSSL 0:d92f9d21154c 1810 iv = aes->reg;
wolfSSL 0:d92f9d21154c 1811
wolfSSL 0:d92f9d21154c 1812 /* crypto structure initialization */
wolfSSL 0:d92f9d21154c 1813 CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
wolfSSL 0:d92f9d21154c 1814 CRYP_StructInit(&AES_CRYP_InitStructure);
wolfSSL 0:d92f9d21154c 1815 CRYP_IVStructInit(&AES_CRYP_IVInitStructure);
wolfSSL 0:d92f9d21154c 1816
wolfSSL 0:d92f9d21154c 1817 /* reset registers to their default values */
wolfSSL 0:d92f9d21154c 1818 CRYP_DeInit();
wolfSSL 0:d92f9d21154c 1819
wolfSSL 0:d92f9d21154c 1820 /* load key into correct registers */
wolfSSL 0:d92f9d21154c 1821 switch(aes->rounds)
wolfSSL 0:d92f9d21154c 1822 {
wolfSSL 0:d92f9d21154c 1823 case 10: /* 128-bit key */
wolfSSL 0:d92f9d21154c 1824 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
wolfSSL 0:d92f9d21154c 1825 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[0];
wolfSSL 0:d92f9d21154c 1826 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[1];
wolfSSL 0:d92f9d21154c 1827 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[2];
wolfSSL 0:d92f9d21154c 1828 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[3];
wolfSSL 0:d92f9d21154c 1829 break;
wolfSSL 0:d92f9d21154c 1830
wolfSSL 0:d92f9d21154c 1831 case 12: /* 192-bit key */
wolfSSL 0:d92f9d21154c 1832 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
wolfSSL 0:d92f9d21154c 1833 AES_CRYP_KeyInitStructure.CRYP_Key1Left = enc_key[0];
wolfSSL 0:d92f9d21154c 1834 AES_CRYP_KeyInitStructure.CRYP_Key1Right = enc_key[1];
wolfSSL 0:d92f9d21154c 1835 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[2];
wolfSSL 0:d92f9d21154c 1836 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[3];
wolfSSL 0:d92f9d21154c 1837 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[4];
wolfSSL 0:d92f9d21154c 1838 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[5];
wolfSSL 0:d92f9d21154c 1839 break;
wolfSSL 0:d92f9d21154c 1840
wolfSSL 0:d92f9d21154c 1841 case 14: /* 256-bit key */
wolfSSL 0:d92f9d21154c 1842 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
wolfSSL 0:d92f9d21154c 1843 AES_CRYP_KeyInitStructure.CRYP_Key0Left = enc_key[0];
wolfSSL 0:d92f9d21154c 1844 AES_CRYP_KeyInitStructure.CRYP_Key0Right = enc_key[1];
wolfSSL 0:d92f9d21154c 1845 AES_CRYP_KeyInitStructure.CRYP_Key1Left = enc_key[2];
wolfSSL 0:d92f9d21154c 1846 AES_CRYP_KeyInitStructure.CRYP_Key1Right = enc_key[3];
wolfSSL 0:d92f9d21154c 1847 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[4];
wolfSSL 0:d92f9d21154c 1848 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[5];
wolfSSL 0:d92f9d21154c 1849 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[6];
wolfSSL 0:d92f9d21154c 1850 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[7];
wolfSSL 0:d92f9d21154c 1851 break;
wolfSSL 0:d92f9d21154c 1852
wolfSSL 0:d92f9d21154c 1853 default:
wolfSSL 0:d92f9d21154c 1854 break;
wolfSSL 0:d92f9d21154c 1855 }
wolfSSL 0:d92f9d21154c 1856 CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
wolfSSL 0:d92f9d21154c 1857
wolfSSL 0:d92f9d21154c 1858 /* set iv */
wolfSSL 0:d92f9d21154c 1859 ByteReverseWords(iv, iv, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1860 AES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
wolfSSL 0:d92f9d21154c 1861 AES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
wolfSSL 0:d92f9d21154c 1862 AES_CRYP_IVInitStructure.CRYP_IV1Left = iv[2];
wolfSSL 0:d92f9d21154c 1863 AES_CRYP_IVInitStructure.CRYP_IV1Right = iv[3];
wolfSSL 0:d92f9d21154c 1864 CRYP_IVInit(&AES_CRYP_IVInitStructure);
wolfSSL 0:d92f9d21154c 1865
wolfSSL 0:d92f9d21154c 1866 /* set direction, mode, and datatype */
wolfSSL 0:d92f9d21154c 1867 AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
wolfSSL 0:d92f9d21154c 1868 AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;
wolfSSL 0:d92f9d21154c 1869 AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 0:d92f9d21154c 1870 CRYP_Init(&AES_CRYP_InitStructure);
wolfSSL 0:d92f9d21154c 1871
wolfSSL 0:d92f9d21154c 1872 /* enable crypto processor */
wolfSSL 0:d92f9d21154c 1873 CRYP_Cmd(ENABLE);
wolfSSL 0:d92f9d21154c 1874
wolfSSL 0:d92f9d21154c 1875 while (sz > 0)
wolfSSL 0:d92f9d21154c 1876 {
wolfSSL 0:d92f9d21154c 1877 /* flush IN/OUT FIFOs */
wolfSSL 0:d92f9d21154c 1878 CRYP_FIFOFlush();
wolfSSL 0:d92f9d21154c 1879
wolfSSL 0:d92f9d21154c 1880 CRYP_DataIn(*(uint32_t*)&in[0]);
wolfSSL 0:d92f9d21154c 1881 CRYP_DataIn(*(uint32_t*)&in[4]);
wolfSSL 0:d92f9d21154c 1882 CRYP_DataIn(*(uint32_t*)&in[8]);
wolfSSL 0:d92f9d21154c 1883 CRYP_DataIn(*(uint32_t*)&in[12]);
wolfSSL 0:d92f9d21154c 1884
wolfSSL 0:d92f9d21154c 1885 /* wait until the complete message has been processed */
wolfSSL 0:d92f9d21154c 1886 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 0:d92f9d21154c 1887
wolfSSL 0:d92f9d21154c 1888 *(uint32_t*)&out[0] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 1889 *(uint32_t*)&out[4] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 1890 *(uint32_t*)&out[8] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 1891 *(uint32_t*)&out[12] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 1892
wolfSSL 0:d92f9d21154c 1893 /* store iv for next call */
wolfSSL 0:d92f9d21154c 1894 XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1895
wolfSSL 0:d92f9d21154c 1896 sz -= 16;
wolfSSL 0:d92f9d21154c 1897 in += 16;
wolfSSL 0:d92f9d21154c 1898 out += 16;
wolfSSL 0:d92f9d21154c 1899 }
wolfSSL 0:d92f9d21154c 1900
wolfSSL 0:d92f9d21154c 1901 /* disable crypto processor */
wolfSSL 0:d92f9d21154c 1902 CRYP_Cmd(DISABLE);
wolfSSL 0:d92f9d21154c 1903
wolfSSL 0:d92f9d21154c 1904 return 0;
wolfSSL 0:d92f9d21154c 1905 }
wolfSSL 0:d92f9d21154c 1906
wolfSSL 0:d92f9d21154c 1907 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 1908 {
wolfSSL 0:d92f9d21154c 1909 word32 *dec_key, *iv;
wolfSSL 0:d92f9d21154c 1910 CRYP_InitTypeDef AES_CRYP_InitStructure;
wolfSSL 0:d92f9d21154c 1911 CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
wolfSSL 0:d92f9d21154c 1912 CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
wolfSSL 0:d92f9d21154c 1913
wolfSSL 0:d92f9d21154c 1914 dec_key = aes->key;
wolfSSL 0:d92f9d21154c 1915 iv = aes->reg;
wolfSSL 0:d92f9d21154c 1916
wolfSSL 0:d92f9d21154c 1917 /* crypto structure initialization */
wolfSSL 0:d92f9d21154c 1918 CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
wolfSSL 0:d92f9d21154c 1919 CRYP_StructInit(&AES_CRYP_InitStructure);
wolfSSL 0:d92f9d21154c 1920 CRYP_IVStructInit(&AES_CRYP_IVInitStructure);
wolfSSL 0:d92f9d21154c 1921
wolfSSL 0:d92f9d21154c 1922 /* if input and output same will overwrite input iv */
wolfSSL 0:d92f9d21154c 1923 XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1924
wolfSSL 0:d92f9d21154c 1925 /* reset registers to their default values */
wolfSSL 0:d92f9d21154c 1926 CRYP_DeInit();
wolfSSL 0:d92f9d21154c 1927
wolfSSL 0:d92f9d21154c 1928 /* load key into correct registers */
wolfSSL 0:d92f9d21154c 1929 switch(aes->rounds)
wolfSSL 0:d92f9d21154c 1930 {
wolfSSL 0:d92f9d21154c 1931 case 10: /* 128-bit key */
wolfSSL 0:d92f9d21154c 1932 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
wolfSSL 0:d92f9d21154c 1933 AES_CRYP_KeyInitStructure.CRYP_Key2Left = dec_key[0];
wolfSSL 0:d92f9d21154c 1934 AES_CRYP_KeyInitStructure.CRYP_Key2Right = dec_key[1];
wolfSSL 0:d92f9d21154c 1935 AES_CRYP_KeyInitStructure.CRYP_Key3Left = dec_key[2];
wolfSSL 0:d92f9d21154c 1936 AES_CRYP_KeyInitStructure.CRYP_Key3Right = dec_key[3];
wolfSSL 0:d92f9d21154c 1937 break;
wolfSSL 0:d92f9d21154c 1938
wolfSSL 0:d92f9d21154c 1939 case 12: /* 192-bit key */
wolfSSL 0:d92f9d21154c 1940 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
wolfSSL 0:d92f9d21154c 1941 AES_CRYP_KeyInitStructure.CRYP_Key1Left = dec_key[0];
wolfSSL 0:d92f9d21154c 1942 AES_CRYP_KeyInitStructure.CRYP_Key1Right = dec_key[1];
wolfSSL 0:d92f9d21154c 1943 AES_CRYP_KeyInitStructure.CRYP_Key2Left = dec_key[2];
wolfSSL 0:d92f9d21154c 1944 AES_CRYP_KeyInitStructure.CRYP_Key2Right = dec_key[3];
wolfSSL 0:d92f9d21154c 1945 AES_CRYP_KeyInitStructure.CRYP_Key3Left = dec_key[4];
wolfSSL 0:d92f9d21154c 1946 AES_CRYP_KeyInitStructure.CRYP_Key3Right = dec_key[5];
wolfSSL 0:d92f9d21154c 1947 break;
wolfSSL 0:d92f9d21154c 1948
wolfSSL 0:d92f9d21154c 1949 case 14: /* 256-bit key */
wolfSSL 0:d92f9d21154c 1950 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
wolfSSL 0:d92f9d21154c 1951 AES_CRYP_KeyInitStructure.CRYP_Key0Left = dec_key[0];
wolfSSL 0:d92f9d21154c 1952 AES_CRYP_KeyInitStructure.CRYP_Key0Right = dec_key[1];
wolfSSL 0:d92f9d21154c 1953 AES_CRYP_KeyInitStructure.CRYP_Key1Left = dec_key[2];
wolfSSL 0:d92f9d21154c 1954 AES_CRYP_KeyInitStructure.CRYP_Key1Right = dec_key[3];
wolfSSL 0:d92f9d21154c 1955 AES_CRYP_KeyInitStructure.CRYP_Key2Left = dec_key[4];
wolfSSL 0:d92f9d21154c 1956 AES_CRYP_KeyInitStructure.CRYP_Key2Right = dec_key[5];
wolfSSL 0:d92f9d21154c 1957 AES_CRYP_KeyInitStructure.CRYP_Key3Left = dec_key[6];
wolfSSL 0:d92f9d21154c 1958 AES_CRYP_KeyInitStructure.CRYP_Key3Right = dec_key[7];
wolfSSL 0:d92f9d21154c 1959 break;
wolfSSL 0:d92f9d21154c 1960
wolfSSL 0:d92f9d21154c 1961 default:
wolfSSL 0:d92f9d21154c 1962 break;
wolfSSL 0:d92f9d21154c 1963 }
wolfSSL 0:d92f9d21154c 1964
wolfSSL 0:d92f9d21154c 1965 /* set direction, mode, and datatype for key preparation */
wolfSSL 0:d92f9d21154c 1966 AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
wolfSSL 0:d92f9d21154c 1967 AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
wolfSSL 0:d92f9d21154c 1968 AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_32b;
wolfSSL 0:d92f9d21154c 1969 CRYP_Init(&AES_CRYP_InitStructure);
wolfSSL 0:d92f9d21154c 1970 CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
wolfSSL 0:d92f9d21154c 1971
wolfSSL 0:d92f9d21154c 1972 /* enable crypto processor */
wolfSSL 0:d92f9d21154c 1973 CRYP_Cmd(ENABLE);
wolfSSL 0:d92f9d21154c 1974
wolfSSL 0:d92f9d21154c 1975 /* wait until key has been prepared */
wolfSSL 0:d92f9d21154c 1976 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 0:d92f9d21154c 1977
wolfSSL 0:d92f9d21154c 1978 /* set direction, mode, and datatype for decryption */
wolfSSL 0:d92f9d21154c 1979 AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
wolfSSL 0:d92f9d21154c 1980 AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;
wolfSSL 0:d92f9d21154c 1981 AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 0:d92f9d21154c 1982 CRYP_Init(&AES_CRYP_InitStructure);
wolfSSL 0:d92f9d21154c 1983
wolfSSL 0:d92f9d21154c 1984 /* set iv */
wolfSSL 0:d92f9d21154c 1985 ByteReverseWords(iv, iv, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 1986
wolfSSL 0:d92f9d21154c 1987 AES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
wolfSSL 0:d92f9d21154c 1988 AES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
wolfSSL 0:d92f9d21154c 1989 AES_CRYP_IVInitStructure.CRYP_IV1Left = iv[2];
wolfSSL 0:d92f9d21154c 1990 AES_CRYP_IVInitStructure.CRYP_IV1Right = iv[3];
wolfSSL 0:d92f9d21154c 1991 CRYP_IVInit(&AES_CRYP_IVInitStructure);
wolfSSL 0:d92f9d21154c 1992
wolfSSL 0:d92f9d21154c 1993 /* enable crypto processor */
wolfSSL 0:d92f9d21154c 1994 CRYP_Cmd(ENABLE);
wolfSSL 0:d92f9d21154c 1995
wolfSSL 0:d92f9d21154c 1996 while (sz > 0)
wolfSSL 0:d92f9d21154c 1997 {
wolfSSL 0:d92f9d21154c 1998 /* flush IN/OUT FIFOs */
wolfSSL 0:d92f9d21154c 1999 CRYP_FIFOFlush();
wolfSSL 0:d92f9d21154c 2000
wolfSSL 0:d92f9d21154c 2001 CRYP_DataIn(*(uint32_t*)&in[0]);
wolfSSL 0:d92f9d21154c 2002 CRYP_DataIn(*(uint32_t*)&in[4]);
wolfSSL 0:d92f9d21154c 2003 CRYP_DataIn(*(uint32_t*)&in[8]);
wolfSSL 0:d92f9d21154c 2004 CRYP_DataIn(*(uint32_t*)&in[12]);
wolfSSL 0:d92f9d21154c 2005
wolfSSL 0:d92f9d21154c 2006 /* wait until the complete message has been processed */
wolfSSL 0:d92f9d21154c 2007 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 0:d92f9d21154c 2008
wolfSSL 0:d92f9d21154c 2009 *(uint32_t*)&out[0] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 2010 *(uint32_t*)&out[4] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 2011 *(uint32_t*)&out[8] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 2012 *(uint32_t*)&out[12] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 2013
wolfSSL 0:d92f9d21154c 2014 /* store iv for next call */
wolfSSL 0:d92f9d21154c 2015 XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2016
wolfSSL 0:d92f9d21154c 2017 sz -= 16;
wolfSSL 0:d92f9d21154c 2018 in += 16;
wolfSSL 0:d92f9d21154c 2019 out += 16;
wolfSSL 0:d92f9d21154c 2020 }
wolfSSL 0:d92f9d21154c 2021
wolfSSL 0:d92f9d21154c 2022 /* disable crypto processor */
wolfSSL 0:d92f9d21154c 2023 CRYP_Cmd(DISABLE);
wolfSSL 0:d92f9d21154c 2024
wolfSSL 0:d92f9d21154c 2025 return 0;
wolfSSL 0:d92f9d21154c 2026 }
wolfSSL 0:d92f9d21154c 2027
wolfSSL 0:d92f9d21154c 2028 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 0:d92f9d21154c 2029 static int wc_AesCbcCrypt(Aes* aes, byte* po, const byte* pi, word32 sz,
wolfSSL 0:d92f9d21154c 2030 word32 descHeader)
wolfSSL 0:d92f9d21154c 2031 {
wolfSSL 0:d92f9d21154c 2032 #ifdef DEBUG_WOLFSSL
wolfSSL 0:d92f9d21154c 2033 int i; int stat1, stat2; int ret;
wolfSSL 0:d92f9d21154c 2034 #endif
wolfSSL 0:d92f9d21154c 2035
wolfSSL 0:d92f9d21154c 2036 int size;
wolfSSL 0:d92f9d21154c 2037 volatile int v;
wolfSSL 0:d92f9d21154c 2038
wolfSSL 0:d92f9d21154c 2039 if ((pi == NULL) || (po == NULL))
wolfSSL 0:d92f9d21154c 2040 return BAD_FUNC_ARG; /*wrong pointer*/
wolfSSL 0:d92f9d21154c 2041
wolfSSL 0:d92f9d21154c 2042 LockMutex(&Mutex_AesSEC);
wolfSSL 0:d92f9d21154c 2043
wolfSSL 0:d92f9d21154c 2044 /* Set descriptor for SEC */
wolfSSL 0:d92f9d21154c 2045 secDesc->length1 = 0x0;
wolfSSL 0:d92f9d21154c 2046 secDesc->pointer1 = NULL;
wolfSSL 0:d92f9d21154c 2047
wolfSSL 0:d92f9d21154c 2048 secDesc->length2 = AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2049 secDesc->pointer2 = (byte *)secReg; /* Initial Vector */
wolfSSL 0:d92f9d21154c 2050
wolfSSL 0:d92f9d21154c 2051 switch(aes->rounds) {
wolfSSL 0:d92f9d21154c 2052 case 10: secDesc->length3 = 16 ; break ;
wolfSSL 0:d92f9d21154c 2053 case 12: secDesc->length3 = 24 ; break ;
wolfSSL 0:d92f9d21154c 2054 case 14: secDesc->length3 = 32 ; break ;
wolfSSL 0:d92f9d21154c 2055 }
wolfSSL 0:d92f9d21154c 2056 XMEMCPY(secKey, aes->key, secDesc->length3);
wolfSSL 0:d92f9d21154c 2057
wolfSSL 0:d92f9d21154c 2058 secDesc->pointer3 = (byte *)secKey;
wolfSSL 0:d92f9d21154c 2059 secDesc->pointer4 = AESBuffIn;
wolfSSL 0:d92f9d21154c 2060 secDesc->pointer5 = AESBuffOut;
wolfSSL 0:d92f9d21154c 2061 secDesc->length6 = 0x0;
wolfSSL 0:d92f9d21154c 2062 secDesc->pointer6 = NULL;
wolfSSL 0:d92f9d21154c 2063 secDesc->length7 = 0x0;
wolfSSL 0:d92f9d21154c 2064 secDesc->pointer7 = NULL;
wolfSSL 0:d92f9d21154c 2065 secDesc->nextDescriptorPtr = NULL;
wolfSSL 0:d92f9d21154c 2066
wolfSSL 0:d92f9d21154c 2067 while (sz) {
wolfSSL 0:d92f9d21154c 2068 secDesc->header = descHeader;
wolfSSL 0:d92f9d21154c 2069 XMEMCPY(secReg, aes->reg, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2070 if ((sz % AES_BUFFER_SIZE) == sz) {
wolfSSL 0:d92f9d21154c 2071 size = sz;
wolfSSL 0:d92f9d21154c 2072 sz = 0;
wolfSSL 0:d92f9d21154c 2073 } else {
wolfSSL 0:d92f9d21154c 2074 size = AES_BUFFER_SIZE;
wolfSSL 0:d92f9d21154c 2075 sz -= AES_BUFFER_SIZE;
wolfSSL 0:d92f9d21154c 2076 }
wolfSSL 0:d92f9d21154c 2077 secDesc->length4 = size;
wolfSSL 0:d92f9d21154c 2078 secDesc->length5 = size;
wolfSSL 0:d92f9d21154c 2079
wolfSSL 0:d92f9d21154c 2080 XMEMCPY(AESBuffIn, pi, size);
wolfSSL 0:d92f9d21154c 2081 if(descHeader == SEC_DESC_AES_CBC_DECRYPT) {
wolfSSL 0:d92f9d21154c 2082 XMEMCPY((void*)aes->tmp, (void*)&(pi[size-AES_BLOCK_SIZE]),
wolfSSL 0:d92f9d21154c 2083 AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2084 }
wolfSSL 0:d92f9d21154c 2085
wolfSSL 0:d92f9d21154c 2086 /* Point SEC to the location of the descriptor */
wolfSSL 0:d92f9d21154c 2087 MCF_SEC_FR0 = (uint32)secDesc;
wolfSSL 0:d92f9d21154c 2088 /* Initialize SEC and wait for encryption to complete */
wolfSSL 0:d92f9d21154c 2089 MCF_SEC_CCCR0 = 0x0000001a;
wolfSSL 0:d92f9d21154c 2090 /* poll SISR to determine when channel is complete */
wolfSSL 0:d92f9d21154c 2091 v=0;
wolfSSL 0:d92f9d21154c 2092
wolfSSL 0:d92f9d21154c 2093 while ((secDesc->header>> 24) != 0xff) v++;
wolfSSL 0:d92f9d21154c 2094
wolfSSL 0:d92f9d21154c 2095 #ifdef DEBUG_WOLFSSL
wolfSSL 0:d92f9d21154c 2096 ret = MCF_SEC_SISRH;
wolfSSL 0:d92f9d21154c 2097 stat1 = MCF_SEC_AESSR;
wolfSSL 0:d92f9d21154c 2098 stat2 = MCF_SEC_AESISR;
wolfSSL 0:d92f9d21154c 2099 if (ret & 0xe0000000) {
wolfSSL 0:d92f9d21154c 2100 db_printf("Aes_Cbc(i=%d):ISRH=%08x, AESSR=%08x, "
wolfSSL 0:d92f9d21154c 2101 "AESISR=%08x\n", i, ret, stat1, stat2);
wolfSSL 0:d92f9d21154c 2102 }
wolfSSL 0:d92f9d21154c 2103 #endif
wolfSSL 0:d92f9d21154c 2104
wolfSSL 0:d92f9d21154c 2105 XMEMCPY(po, AESBuffOut, size);
wolfSSL 0:d92f9d21154c 2106
wolfSSL 0:d92f9d21154c 2107 if (descHeader == SEC_DESC_AES_CBC_ENCRYPT) {
wolfSSL 0:d92f9d21154c 2108 XMEMCPY((void*)aes->reg, (void*)&(po[size-AES_BLOCK_SIZE]),
wolfSSL 0:d92f9d21154c 2109 AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2110 } else {
wolfSSL 0:d92f9d21154c 2111 XMEMCPY((void*)aes->reg, (void*)aes->tmp, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2112 }
wolfSSL 0:d92f9d21154c 2113
wolfSSL 0:d92f9d21154c 2114 pi += size;
wolfSSL 0:d92f9d21154c 2115 po += size;
wolfSSL 0:d92f9d21154c 2116 }
wolfSSL 0:d92f9d21154c 2117
wolfSSL 0:d92f9d21154c 2118 UnLockMutex(&Mutex_AesSEC);
wolfSSL 0:d92f9d21154c 2119 return 0;
wolfSSL 0:d92f9d21154c 2120 }
wolfSSL 0:d92f9d21154c 2121
wolfSSL 0:d92f9d21154c 2122 int wc_AesCbcEncrypt(Aes* aes, byte* po, const byte* pi, word32 sz)
wolfSSL 0:d92f9d21154c 2123 {
wolfSSL 0:d92f9d21154c 2124 return (wc_AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_ENCRYPT));
wolfSSL 0:d92f9d21154c 2125 }
wolfSSL 0:d92f9d21154c 2126
wolfSSL 0:d92f9d21154c 2127 int wc_AesCbcDecrypt(Aes* aes, byte* po, const byte* pi, word32 sz)
wolfSSL 0:d92f9d21154c 2128 {
wolfSSL 0:d92f9d21154c 2129 return (wc_AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_DECRYPT));
wolfSSL 0:d92f9d21154c 2130 }
wolfSSL 0:d92f9d21154c 2131
wolfSSL 0:d92f9d21154c 2132 #elif defined(FREESCALE_MMCAU)
wolfSSL 0:d92f9d21154c 2133 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 2134 {
wolfSSL 0:d92f9d21154c 2135 int i;
wolfSSL 0:d92f9d21154c 2136 int offset = 0;
wolfSSL 0:d92f9d21154c 2137 int len = sz;
wolfSSL 0:d92f9d21154c 2138
wolfSSL 0:d92f9d21154c 2139 byte *iv, *enc_key;
wolfSSL 0:d92f9d21154c 2140 byte temp_block[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 2141
wolfSSL 0:d92f9d21154c 2142 iv = (byte*)aes->reg;
wolfSSL 0:d92f9d21154c 2143 enc_key = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 2144
wolfSSL 0:d92f9d21154c 2145 if ((wolfssl_word)out % WOLFSSL_MMCAU_ALIGNMENT) {
wolfSSL 0:d92f9d21154c 2146 WOLFSSL_MSG("Bad cau_aes_encrypt alignment");
wolfSSL 0:d92f9d21154c 2147 return BAD_ALIGN_E;
wolfSSL 0:d92f9d21154c 2148 }
wolfSSL 0:d92f9d21154c 2149
wolfSSL 0:d92f9d21154c 2150 while (len > 0)
wolfSSL 0:d92f9d21154c 2151 {
wolfSSL 0:d92f9d21154c 2152 XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2153
wolfSSL 0:d92f9d21154c 2154 /* XOR block with IV for CBC */
wolfSSL 0:d92f9d21154c 2155 for (i = 0; i < AES_BLOCK_SIZE; i++)
wolfSSL 0:d92f9d21154c 2156 temp_block[i] ^= iv[i];
wolfSSL 0:d92f9d21154c 2157
wolfSSL 0:d92f9d21154c 2158 cau_aes_encrypt(temp_block, enc_key, aes->rounds, out + offset);
wolfSSL 0:d92f9d21154c 2159
wolfSSL 0:d92f9d21154c 2160 len -= AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2161 offset += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2162
wolfSSL 0:d92f9d21154c 2163 /* store IV for next block */
wolfSSL 0:d92f9d21154c 2164 XMEMCPY(iv, out + offset - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2165 }
wolfSSL 0:d92f9d21154c 2166
wolfSSL 0:d92f9d21154c 2167 return 0;
wolfSSL 0:d92f9d21154c 2168 }
wolfSSL 0:d92f9d21154c 2169
wolfSSL 0:d92f9d21154c 2170 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 2171 {
wolfSSL 0:d92f9d21154c 2172 int i;
wolfSSL 0:d92f9d21154c 2173 int offset = 0;
wolfSSL 0:d92f9d21154c 2174 int len = sz;
wolfSSL 0:d92f9d21154c 2175
wolfSSL 0:d92f9d21154c 2176 byte* iv, *dec_key;
wolfSSL 0:d92f9d21154c 2177 byte temp_block[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 2178
wolfSSL 0:d92f9d21154c 2179 iv = (byte*)aes->reg;
wolfSSL 0:d92f9d21154c 2180 dec_key = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 2181
wolfSSL 0:d92f9d21154c 2182 if ((wolfssl_word)out % WOLFSSL_MMCAU_ALIGNMENT) {
wolfSSL 0:d92f9d21154c 2183 WOLFSSL_MSG("Bad cau_aes_decrypt alignment");
wolfSSL 0:d92f9d21154c 2184 return BAD_ALIGN_E;
wolfSSL 0:d92f9d21154c 2185 }
wolfSSL 0:d92f9d21154c 2186
wolfSSL 0:d92f9d21154c 2187 while (len > 0)
wolfSSL 0:d92f9d21154c 2188 {
wolfSSL 0:d92f9d21154c 2189 XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2190
wolfSSL 0:d92f9d21154c 2191 cau_aes_decrypt(in + offset, dec_key, aes->rounds, out + offset);
wolfSSL 0:d92f9d21154c 2192
wolfSSL 0:d92f9d21154c 2193 /* XOR block with IV for CBC */
wolfSSL 0:d92f9d21154c 2194 for (i = 0; i < AES_BLOCK_SIZE; i++)
wolfSSL 0:d92f9d21154c 2195 (out + offset)[i] ^= iv[i];
wolfSSL 0:d92f9d21154c 2196
wolfSSL 0:d92f9d21154c 2197 /* store IV for next block */
wolfSSL 0:d92f9d21154c 2198 XMEMCPY(iv, temp_block, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2199
wolfSSL 0:d92f9d21154c 2200 len -= AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2201 offset += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2202 }
wolfSSL 0:d92f9d21154c 2203
wolfSSL 0:d92f9d21154c 2204 return 0;
wolfSSL 0:d92f9d21154c 2205 }
wolfSSL 0:d92f9d21154c 2206
wolfSSL 0:d92f9d21154c 2207 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 0:d92f9d21154c 2208 /* core hardware crypt engine driver */
wolfSSL 0:d92f9d21154c 2209 static void wc_AesCrypt(Aes *aes, byte* out, const byte* in, word32 sz,
wolfSSL 0:d92f9d21154c 2210 int dir, int algo, int cryptoalgo)
wolfSSL 0:d92f9d21154c 2211 {
wolfSSL 0:d92f9d21154c 2212 securityAssociation *sa_p ;
wolfSSL 0:d92f9d21154c 2213 bufferDescriptor *bd_p ;
wolfSSL 0:d92f9d21154c 2214
wolfSSL 0:d92f9d21154c 2215 volatile securityAssociation sa __attribute__((aligned (8)));
wolfSSL 0:d92f9d21154c 2216 volatile bufferDescriptor bd __attribute__((aligned (8)));
wolfSSL 0:d92f9d21154c 2217 volatile int k ;
wolfSSL 0:d92f9d21154c 2218
wolfSSL 0:d92f9d21154c 2219 /* get uncached address */
wolfSSL 0:d92f9d21154c 2220 sa_p = KVA0_TO_KVA1(&sa) ;
wolfSSL 0:d92f9d21154c 2221 bd_p = KVA0_TO_KVA1(&bd) ;
wolfSSL 0:d92f9d21154c 2222
wolfSSL 0:d92f9d21154c 2223 /* Sync cache and physical memory */
wolfSSL 0:d92f9d21154c 2224 if(PIC32MZ_IF_RAM(in)) {
wolfSSL 0:d92f9d21154c 2225 XMEMCPY((void *)KVA0_TO_KVA1(in), (void *)in, sz);
wolfSSL 0:d92f9d21154c 2226 }
wolfSSL 0:d92f9d21154c 2227 XMEMSET((void *)KVA0_TO_KVA1(out), 0, sz);
wolfSSL 0:d92f9d21154c 2228 /* Set up the Security Association */
wolfSSL 0:d92f9d21154c 2229 XMEMSET((byte *)KVA0_TO_KVA1(&sa), 0, sizeof(sa));
wolfSSL 0:d92f9d21154c 2230 sa_p->SA_CTRL.ALGO = algo ; /* AES */
wolfSSL 0:d92f9d21154c 2231 sa_p->SA_CTRL.LNC = 1;
wolfSSL 0:d92f9d21154c 2232 sa_p->SA_CTRL.LOADIV = 1;
wolfSSL 0:d92f9d21154c 2233 sa_p->SA_CTRL.FB = 1;
wolfSSL 0:d92f9d21154c 2234 sa_p->SA_CTRL.ENCTYPE = dir ; /* Encryption/Decryption */
wolfSSL 0:d92f9d21154c 2235 sa_p->SA_CTRL.CRYPTOALGO = cryptoalgo;
wolfSSL 0:d92f9d21154c 2236
wolfSSL 0:d92f9d21154c 2237 if(cryptoalgo == PIC32_CRYPTOALGO_AES_GCM){
wolfSSL 0:d92f9d21154c 2238 switch(aes->keylen) {
wolfSSL 0:d92f9d21154c 2239 case 32:
wolfSSL 0:d92f9d21154c 2240 sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_256 ;
wolfSSL 0:d92f9d21154c 2241 break ;
wolfSSL 0:d92f9d21154c 2242 case 24:
wolfSSL 0:d92f9d21154c 2243 sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_192 ;
wolfSSL 0:d92f9d21154c 2244 break ;
wolfSSL 0:d92f9d21154c 2245 case 16:
wolfSSL 0:d92f9d21154c 2246 sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_128 ;
wolfSSL 0:d92f9d21154c 2247 break ;
wolfSSL 0:d92f9d21154c 2248 }
wolfSSL 0:d92f9d21154c 2249 } else
wolfSSL 0:d92f9d21154c 2250 sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_128 ;
wolfSSL 0:d92f9d21154c 2251
wolfSSL 0:d92f9d21154c 2252 ByteReverseWords(
wolfSSL 0:d92f9d21154c 2253 (word32 *)KVA0_TO_KVA1(sa.SA_ENCKEY + 8 - aes->keylen/sizeof(word32)),
wolfSSL 0:d92f9d21154c 2254 (word32 *)aes->key_ce, aes->keylen);
wolfSSL 0:d92f9d21154c 2255 ByteReverseWords(
wolfSSL 0:d92f9d21154c 2256 (word32*)KVA0_TO_KVA1(sa.SA_ENCIV), (word32 *)aes->iv_ce, 16);
wolfSSL 0:d92f9d21154c 2257
wolfSSL 0:d92f9d21154c 2258 XMEMSET((byte *)KVA0_TO_KVA1(&bd), 0, sizeof(bd));
wolfSSL 0:d92f9d21154c 2259 /* Set up the Buffer Descriptor */
wolfSSL 0:d92f9d21154c 2260 bd_p->BD_CTRL.BUFLEN = sz;
wolfSSL 0:d92f9d21154c 2261 if(cryptoalgo == PIC32_CRYPTOALGO_AES_GCM) {
wolfSSL 0:d92f9d21154c 2262 if(sz % 0x10)
wolfSSL 0:d92f9d21154c 2263 bd_p->BD_CTRL.BUFLEN = (sz/0x10 + 1) * 0x10 ;
wolfSSL 0:d92f9d21154c 2264 }
wolfSSL 0:d92f9d21154c 2265 bd_p->BD_CTRL.LIFM = 1;
wolfSSL 0:d92f9d21154c 2266 bd_p->BD_CTRL.SA_FETCH_EN = 1;
wolfSSL 0:d92f9d21154c 2267 bd_p->BD_CTRL.LAST_BD = 1;
wolfSSL 0:d92f9d21154c 2268 bd_p->BD_CTRL.DESC_EN = 1;
wolfSSL 0:d92f9d21154c 2269
wolfSSL 0:d92f9d21154c 2270 bd_p->SA_ADDR = (unsigned int)KVA_TO_PA(&sa) ;
wolfSSL 0:d92f9d21154c 2271 bd_p->SRCADDR = (unsigned int)KVA_TO_PA(in) ;
wolfSSL 0:d92f9d21154c 2272 bd_p->DSTADDR = (unsigned int)KVA_TO_PA(out);
wolfSSL 0:d92f9d21154c 2273 bd_p->MSGLEN = sz ;
wolfSSL 0:d92f9d21154c 2274
wolfSSL 0:d92f9d21154c 2275 CECON = 1 << 6;
wolfSSL 0:d92f9d21154c 2276 while (CECON);
wolfSSL 0:d92f9d21154c 2277
wolfSSL 0:d92f9d21154c 2278 /* Run the engine */
wolfSSL 0:d92f9d21154c 2279 CEBDPADDR = (unsigned int)KVA_TO_PA(&bd) ;
wolfSSL 0:d92f9d21154c 2280 CEINTEN = 0x07;
wolfSSL 0:d92f9d21154c 2281 CECON = 0x27;
wolfSSL 0:d92f9d21154c 2282
wolfSSL 0:d92f9d21154c 2283 WAIT_ENGINE ;
wolfSSL 0:d92f9d21154c 2284
wolfSSL 0:d92f9d21154c 2285 if((cryptoalgo == PIC32_CRYPTOALGO_CBC) ||
wolfSSL 0:d92f9d21154c 2286 (cryptoalgo == PIC32_CRYPTOALGO_TCBC)||
wolfSSL 0:d92f9d21154c 2287 (cryptoalgo == PIC32_CRYPTOALGO_RCBC)) {
wolfSSL 0:d92f9d21154c 2288 /* set iv for the next call */
wolfSSL 0:d92f9d21154c 2289 if(dir == PIC32_ENCRYPTION) {
wolfSSL 0:d92f9d21154c 2290 XMEMCPY((void *)aes->iv_ce,
wolfSSL 0:d92f9d21154c 2291 (void*)KVA0_TO_KVA1(out + sz - AES_BLOCK_SIZE),
wolfSSL 0:d92f9d21154c 2292 AES_BLOCK_SIZE) ;
wolfSSL 0:d92f9d21154c 2293 } else {
wolfSSL 0:d92f9d21154c 2294 ByteReverseWords((word32*)aes->iv_ce,
wolfSSL 0:d92f9d21154c 2295 (word32 *)KVA0_TO_KVA1(in + sz - AES_BLOCK_SIZE),
wolfSSL 0:d92f9d21154c 2296 AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2297 }
wolfSSL 0:d92f9d21154c 2298 }
wolfSSL 0:d92f9d21154c 2299 XMEMCPY((byte *)out, (byte *)KVA0_TO_KVA1(out), sz) ;
wolfSSL 0:d92f9d21154c 2300 ByteReverseWords((word32*)out, (word32 *)out, sz);
wolfSSL 0:d92f9d21154c 2301 }
wolfSSL 0:d92f9d21154c 2302
wolfSSL 0:d92f9d21154c 2303 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 2304 {
wolfSSL 0:d92f9d21154c 2305 wc_AesCrypt(aes, out, in, sz, PIC32_ENCRYPTION, PIC32_ALGO_AES,
wolfSSL 0:d92f9d21154c 2306 PIC32_CRYPTOALGO_RCBC );
wolfSSL 0:d92f9d21154c 2307 return 0 ;
wolfSSL 0:d92f9d21154c 2308 }
wolfSSL 0:d92f9d21154c 2309
wolfSSL 0:d92f9d21154c 2310 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 2311 {
wolfSSL 0:d92f9d21154c 2312 wc_AesCrypt(aes, out, in, sz, PIC32_DECRYPTION, PIC32_ALGO_AES,
wolfSSL 0:d92f9d21154c 2313 PIC32_CRYPTOALGO_RCBC);
wolfSSL 0:d92f9d21154c 2314 return 0 ;
wolfSSL 0:d92f9d21154c 2315 }
wolfSSL 0:d92f9d21154c 2316
wolfSSL 0:d92f9d21154c 2317 #else
wolfSSL 0:d92f9d21154c 2318 int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 2319 {
wolfSSL 0:d92f9d21154c 2320 word32 blocks = sz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2321
wolfSSL 0:d92f9d21154c 2322 #ifdef HAVE_CAVIUM
wolfSSL 0:d92f9d21154c 2323 if (aes->magic == WOLFSSL_AES_CAVIUM_MAGIC)
wolfSSL 0:d92f9d21154c 2324 return wc_AesCaviumCbcEncrypt(aes, out, in, sz);
wolfSSL 0:d92f9d21154c 2325 #endif
wolfSSL 0:d92f9d21154c 2326
wolfSSL 0:d92f9d21154c 2327 #ifdef WOLFSSL_AESNI
wolfSSL 0:d92f9d21154c 2328 if (haveAESNI) {
wolfSSL 0:d92f9d21154c 2329 #ifdef DEBUG_AESNI
wolfSSL 0:d92f9d21154c 2330 printf("about to aes cbc encrypt\n");
wolfSSL 0:d92f9d21154c 2331 printf("in = %p\n", in);
wolfSSL 0:d92f9d21154c 2332 printf("out = %p\n", out);
wolfSSL 0:d92f9d21154c 2333 printf("aes->key = %p\n", aes->key);
wolfSSL 0:d92f9d21154c 2334 printf("aes->reg = %p\n", aes->reg);
wolfSSL 0:d92f9d21154c 2335 printf("aes->rounds = %d\n", aes->rounds);
wolfSSL 0:d92f9d21154c 2336 printf("sz = %d\n", sz);
wolfSSL 0:d92f9d21154c 2337 #endif
wolfSSL 0:d92f9d21154c 2338
wolfSSL 0:d92f9d21154c 2339 /* check alignment, decrypt doesn't need alignment */
wolfSSL 0:d92f9d21154c 2340 if ((wolfssl_word)in % 16) {
wolfSSL 0:d92f9d21154c 2341 #ifndef NO_WOLFSSL_ALLOC_ALIGN
wolfSSL 0:d92f9d21154c 2342 byte* tmp = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:d92f9d21154c 2343 WOLFSSL_MSG("AES-CBC encrypt with bad alignment");
wolfSSL 0:d92f9d21154c 2344 if (tmp == NULL) return MEMORY_E;
wolfSSL 0:d92f9d21154c 2345
wolfSSL 0:d92f9d21154c 2346 XMEMCPY(tmp, in, sz);
wolfSSL 0:d92f9d21154c 2347 AES_CBC_encrypt(tmp, tmp, (byte*)aes->reg, sz, (byte*)aes->key,
wolfSSL 0:d92f9d21154c 2348 aes->rounds);
wolfSSL 0:d92f9d21154c 2349 /* store iv for next call */
wolfSSL 0:d92f9d21154c 2350 XMEMCPY(aes->reg, tmp + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2351
wolfSSL 0:d92f9d21154c 2352 XMEMCPY(out, tmp, sz);
wolfSSL 0:d92f9d21154c 2353 XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:d92f9d21154c 2354 return 0;
wolfSSL 0:d92f9d21154c 2355 #else
wolfSSL 0:d92f9d21154c 2356 return BAD_ALIGN_E;
wolfSSL 0:d92f9d21154c 2357 #endif
wolfSSL 0:d92f9d21154c 2358 }
wolfSSL 0:d92f9d21154c 2359
wolfSSL 0:d92f9d21154c 2360 AES_CBC_encrypt(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
wolfSSL 0:d92f9d21154c 2361 aes->rounds);
wolfSSL 0:d92f9d21154c 2362 /* store iv for next call */
wolfSSL 0:d92f9d21154c 2363 XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2364
wolfSSL 0:d92f9d21154c 2365 return 0;
wolfSSL 0:d92f9d21154c 2366 }
wolfSSL 0:d92f9d21154c 2367 #endif
wolfSSL 0:d92f9d21154c 2368
wolfSSL 0:d92f9d21154c 2369 while (blocks--) {
wolfSSL 0:d92f9d21154c 2370 xorbuf((byte*)aes->reg, in, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2371 wc_AesEncrypt(aes, (byte*)aes->reg, (byte*)aes->reg);
wolfSSL 0:d92f9d21154c 2372 XMEMCPY(out, aes->reg, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2373
wolfSSL 0:d92f9d21154c 2374 out += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2375 in += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2376 }
wolfSSL 0:d92f9d21154c 2377
wolfSSL 0:d92f9d21154c 2378 return 0;
wolfSSL 0:d92f9d21154c 2379 }
wolfSSL 0:d92f9d21154c 2380
wolfSSL 0:d92f9d21154c 2381 int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 2382 {
wolfSSL 0:d92f9d21154c 2383 word32 blocks = sz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2384
wolfSSL 0:d92f9d21154c 2385 #ifdef HAVE_CAVIUM
wolfSSL 0:d92f9d21154c 2386 if (aes->magic == WOLFSSL_AES_CAVIUM_MAGIC)
wolfSSL 0:d92f9d21154c 2387 return wc_AesCaviumCbcDecrypt(aes, out, in, sz);
wolfSSL 0:d92f9d21154c 2388 #endif
wolfSSL 0:d92f9d21154c 2389
wolfSSL 0:d92f9d21154c 2390 #ifdef WOLFSSL_AESNI
wolfSSL 0:d92f9d21154c 2391 if (haveAESNI) {
wolfSSL 0:d92f9d21154c 2392 #ifdef DEBUG_AESNI
wolfSSL 0:d92f9d21154c 2393 printf("about to aes cbc decrypt\n");
wolfSSL 0:d92f9d21154c 2394 printf("in = %p\n", in);
wolfSSL 0:d92f9d21154c 2395 printf("out = %p\n", out);
wolfSSL 0:d92f9d21154c 2396 printf("aes->key = %p\n", aes->key);
wolfSSL 0:d92f9d21154c 2397 printf("aes->reg = %p\n", aes->reg);
wolfSSL 0:d92f9d21154c 2398 printf("aes->rounds = %d\n", aes->rounds);
wolfSSL 0:d92f9d21154c 2399 printf("sz = %d\n", sz);
wolfSSL 0:d92f9d21154c 2400 #endif
wolfSSL 0:d92f9d21154c 2401
wolfSSL 0:d92f9d21154c 2402 /* if input and output same will overwrite input iv */
wolfSSL 0:d92f9d21154c 2403 XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2404 AES_CBC_decrypt(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
wolfSSL 0:d92f9d21154c 2405 aes->rounds);
wolfSSL 0:d92f9d21154c 2406 /* store iv for next call */
wolfSSL 0:d92f9d21154c 2407 XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2408 return 0;
wolfSSL 0:d92f9d21154c 2409 }
wolfSSL 0:d92f9d21154c 2410 #endif
wolfSSL 0:d92f9d21154c 2411
wolfSSL 0:d92f9d21154c 2412 while (blocks--) {
wolfSSL 0:d92f9d21154c 2413 XMEMCPY(aes->tmp, in, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2414 wc_AesDecrypt(aes, (byte*)aes->tmp, out);
wolfSSL 0:d92f9d21154c 2415 xorbuf(out, (byte*)aes->reg, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2416 XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2417
wolfSSL 0:d92f9d21154c 2418 out += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2419 in += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2420 }
wolfSSL 0:d92f9d21154c 2421
wolfSSL 0:d92f9d21154c 2422 return 0;
wolfSSL 0:d92f9d21154c 2423 }
wolfSSL 0:d92f9d21154c 2424
wolfSSL 0:d92f9d21154c 2425 #endif /* STM32F2_CRYPTO, AES-CBC block */
wolfSSL 0:d92f9d21154c 2426
wolfSSL 0:d92f9d21154c 2427 /* AES-CTR */
wolfSSL 0:d92f9d21154c 2428 #ifdef WOLFSSL_AES_COUNTER
wolfSSL 0:d92f9d21154c 2429
wolfSSL 0:d92f9d21154c 2430 #ifdef STM32F2_CRYPTO
wolfSSL 0:d92f9d21154c 2431 void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 2432 {
wolfSSL 0:d92f9d21154c 2433 word32 *enc_key, *iv;
wolfSSL 0:d92f9d21154c 2434 CRYP_InitTypeDef AES_CRYP_InitStructure;
wolfSSL 0:d92f9d21154c 2435 CRYP_KeyInitTypeDef AES_CRYP_KeyInitStructure;
wolfSSL 0:d92f9d21154c 2436 CRYP_IVInitTypeDef AES_CRYP_IVInitStructure;
wolfSSL 0:d92f9d21154c 2437
wolfSSL 0:d92f9d21154c 2438 enc_key = aes->key;
wolfSSL 0:d92f9d21154c 2439 iv = aes->reg;
wolfSSL 0:d92f9d21154c 2440
wolfSSL 0:d92f9d21154c 2441 /* crypto structure initialization */
wolfSSL 0:d92f9d21154c 2442 CRYP_KeyStructInit(&AES_CRYP_KeyInitStructure);
wolfSSL 0:d92f9d21154c 2443 CRYP_StructInit(&AES_CRYP_InitStructure);
wolfSSL 0:d92f9d21154c 2444 CRYP_IVStructInit(&AES_CRYP_IVInitStructure);
wolfSSL 0:d92f9d21154c 2445
wolfSSL 0:d92f9d21154c 2446 /* reset registers to their default values */
wolfSSL 0:d92f9d21154c 2447 CRYP_DeInit();
wolfSSL 0:d92f9d21154c 2448
wolfSSL 0:d92f9d21154c 2449 /* load key into correct registers */
wolfSSL 0:d92f9d21154c 2450 switch(aes->rounds)
wolfSSL 0:d92f9d21154c 2451 {
wolfSSL 0:d92f9d21154c 2452 case 10: /* 128-bit key */
wolfSSL 0:d92f9d21154c 2453 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_128b;
wolfSSL 0:d92f9d21154c 2454 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[0];
wolfSSL 0:d92f9d21154c 2455 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[1];
wolfSSL 0:d92f9d21154c 2456 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[2];
wolfSSL 0:d92f9d21154c 2457 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[3];
wolfSSL 0:d92f9d21154c 2458 break;
wolfSSL 0:d92f9d21154c 2459
wolfSSL 0:d92f9d21154c 2460 case 12: /* 192-bit key */
wolfSSL 0:d92f9d21154c 2461 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_192b;
wolfSSL 0:d92f9d21154c 2462 AES_CRYP_KeyInitStructure.CRYP_Key1Left = enc_key[0];
wolfSSL 0:d92f9d21154c 2463 AES_CRYP_KeyInitStructure.CRYP_Key1Right = enc_key[1];
wolfSSL 0:d92f9d21154c 2464 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[2];
wolfSSL 0:d92f9d21154c 2465 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[3];
wolfSSL 0:d92f9d21154c 2466 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[4];
wolfSSL 0:d92f9d21154c 2467 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[5];
wolfSSL 0:d92f9d21154c 2468 break;
wolfSSL 0:d92f9d21154c 2469
wolfSSL 0:d92f9d21154c 2470 case 14: /* 256-bit key */
wolfSSL 0:d92f9d21154c 2471 AES_CRYP_InitStructure.CRYP_KeySize = CRYP_KeySize_256b;
wolfSSL 0:d92f9d21154c 2472 AES_CRYP_KeyInitStructure.CRYP_Key0Left = enc_key[0];
wolfSSL 0:d92f9d21154c 2473 AES_CRYP_KeyInitStructure.CRYP_Key0Right = enc_key[1];
wolfSSL 0:d92f9d21154c 2474 AES_CRYP_KeyInitStructure.CRYP_Key1Left = enc_key[2];
wolfSSL 0:d92f9d21154c 2475 AES_CRYP_KeyInitStructure.CRYP_Key1Right = enc_key[3];
wolfSSL 0:d92f9d21154c 2476 AES_CRYP_KeyInitStructure.CRYP_Key2Left = enc_key[4];
wolfSSL 0:d92f9d21154c 2477 AES_CRYP_KeyInitStructure.CRYP_Key2Right = enc_key[5];
wolfSSL 0:d92f9d21154c 2478 AES_CRYP_KeyInitStructure.CRYP_Key3Left = enc_key[6];
wolfSSL 0:d92f9d21154c 2479 AES_CRYP_KeyInitStructure.CRYP_Key3Right = enc_key[7];
wolfSSL 0:d92f9d21154c 2480 break;
wolfSSL 0:d92f9d21154c 2481
wolfSSL 0:d92f9d21154c 2482 default:
wolfSSL 0:d92f9d21154c 2483 break;
wolfSSL 0:d92f9d21154c 2484 }
wolfSSL 0:d92f9d21154c 2485 CRYP_KeyInit(&AES_CRYP_KeyInitStructure);
wolfSSL 0:d92f9d21154c 2486
wolfSSL 0:d92f9d21154c 2487 /* set iv */
wolfSSL 0:d92f9d21154c 2488 ByteReverseWords(iv, iv, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2489 AES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
wolfSSL 0:d92f9d21154c 2490 AES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
wolfSSL 0:d92f9d21154c 2491 AES_CRYP_IVInitStructure.CRYP_IV1Left = iv[2];
wolfSSL 0:d92f9d21154c 2492 AES_CRYP_IVInitStructure.CRYP_IV1Right = iv[3];
wolfSSL 0:d92f9d21154c 2493 CRYP_IVInit(&AES_CRYP_IVInitStructure);
wolfSSL 0:d92f9d21154c 2494
wolfSSL 0:d92f9d21154c 2495 /* set direction, mode, and datatype */
wolfSSL 0:d92f9d21154c 2496 AES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
wolfSSL 0:d92f9d21154c 2497 AES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_AES_CTR;
wolfSSL 0:d92f9d21154c 2498 AES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 0:d92f9d21154c 2499 CRYP_Init(&AES_CRYP_InitStructure);
wolfSSL 0:d92f9d21154c 2500
wolfSSL 0:d92f9d21154c 2501 /* enable crypto processor */
wolfSSL 0:d92f9d21154c 2502 CRYP_Cmd(ENABLE);
wolfSSL 0:d92f9d21154c 2503
wolfSSL 0:d92f9d21154c 2504 while (sz > 0)
wolfSSL 0:d92f9d21154c 2505 {
wolfSSL 0:d92f9d21154c 2506 /* flush IN/OUT FIFOs */
wolfSSL 0:d92f9d21154c 2507 CRYP_FIFOFlush();
wolfSSL 0:d92f9d21154c 2508
wolfSSL 0:d92f9d21154c 2509 CRYP_DataIn(*(uint32_t*)&in[0]);
wolfSSL 0:d92f9d21154c 2510 CRYP_DataIn(*(uint32_t*)&in[4]);
wolfSSL 0:d92f9d21154c 2511 CRYP_DataIn(*(uint32_t*)&in[8]);
wolfSSL 0:d92f9d21154c 2512 CRYP_DataIn(*(uint32_t*)&in[12]);
wolfSSL 0:d92f9d21154c 2513
wolfSSL 0:d92f9d21154c 2514 /* wait until the complete message has been processed */
wolfSSL 0:d92f9d21154c 2515 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 0:d92f9d21154c 2516
wolfSSL 0:d92f9d21154c 2517 *(uint32_t*)&out[0] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 2518 *(uint32_t*)&out[4] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 2519 *(uint32_t*)&out[8] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 2520 *(uint32_t*)&out[12] = CRYP_DataOut();
wolfSSL 0:d92f9d21154c 2521
wolfSSL 0:d92f9d21154c 2522 /* store iv for next call */
wolfSSL 0:d92f9d21154c 2523 XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2524
wolfSSL 0:d92f9d21154c 2525 sz -= 16;
wolfSSL 0:d92f9d21154c 2526 in += 16;
wolfSSL 0:d92f9d21154c 2527 out += 16;
wolfSSL 0:d92f9d21154c 2528 }
wolfSSL 0:d92f9d21154c 2529
wolfSSL 0:d92f9d21154c 2530 /* disable crypto processor */
wolfSSL 0:d92f9d21154c 2531 CRYP_Cmd(DISABLE);
wolfSSL 0:d92f9d21154c 2532 }
wolfSSL 0:d92f9d21154c 2533
wolfSSL 0:d92f9d21154c 2534 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 0:d92f9d21154c 2535 void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 2536 {
wolfSSL 0:d92f9d21154c 2537 int i ;
wolfSSL 0:d92f9d21154c 2538 char out_block[AES_BLOCK_SIZE] ;
wolfSSL 0:d92f9d21154c 2539 int odd ;
wolfSSL 0:d92f9d21154c 2540 int even ;
wolfSSL 0:d92f9d21154c 2541 char *tmp ; /* (char *)aes->tmp, for short */
wolfSSL 0:d92f9d21154c 2542
wolfSSL 0:d92f9d21154c 2543 tmp = (char *)aes->tmp ;
wolfSSL 0:d92f9d21154c 2544 if(aes->left) {
wolfSSL 0:d92f9d21154c 2545 if((aes->left + sz) >= AES_BLOCK_SIZE){
wolfSSL 0:d92f9d21154c 2546 odd = AES_BLOCK_SIZE - aes->left ;
wolfSSL 0:d92f9d21154c 2547 } else {
wolfSSL 0:d92f9d21154c 2548 odd = sz ;
wolfSSL 0:d92f9d21154c 2549 }
wolfSSL 0:d92f9d21154c 2550 XMEMCPY(tmp+aes->left, in, odd) ;
wolfSSL 0:d92f9d21154c 2551 if((odd+aes->left) == AES_BLOCK_SIZE){
wolfSSL 0:d92f9d21154c 2552 wc_AesCrypt(aes, out_block, tmp, AES_BLOCK_SIZE,
wolfSSL 0:d92f9d21154c 2553 PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCTR);
wolfSSL 0:d92f9d21154c 2554 XMEMCPY(out, out_block+aes->left, odd) ;
wolfSSL 0:d92f9d21154c 2555 aes->left = 0 ;
wolfSSL 0:d92f9d21154c 2556 XMEMSET(tmp, 0x0, AES_BLOCK_SIZE) ;
wolfSSL 0:d92f9d21154c 2557 /* Increment IV */
wolfSSL 0:d92f9d21154c 2558 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
wolfSSL 0:d92f9d21154c 2559 if (++((byte *)aes->iv_ce)[i])
wolfSSL 0:d92f9d21154c 2560 break ;
wolfSSL 0:d92f9d21154c 2561 }
wolfSSL 0:d92f9d21154c 2562 }
wolfSSL 0:d92f9d21154c 2563 in += odd ;
wolfSSL 0:d92f9d21154c 2564 out+= odd ;
wolfSSL 0:d92f9d21154c 2565 sz -= odd ;
wolfSSL 0:d92f9d21154c 2566 }
wolfSSL 0:d92f9d21154c 2567 odd = sz % AES_BLOCK_SIZE ; /* if there is tail flagment */
wolfSSL 0:d92f9d21154c 2568 if(sz / AES_BLOCK_SIZE) {
wolfSSL 0:d92f9d21154c 2569 even = (sz/AES_BLOCK_SIZE)*AES_BLOCK_SIZE ;
wolfSSL 0:d92f9d21154c 2570 wc_AesCrypt(aes, out, in, even, PIC32_ENCRYPTION, PIC32_ALGO_AES,
wolfSSL 0:d92f9d21154c 2571 PIC32_CRYPTOALGO_RCTR);
wolfSSL 0:d92f9d21154c 2572 out += even ;
wolfSSL 0:d92f9d21154c 2573 in += even ;
wolfSSL 0:d92f9d21154c 2574 do { /* Increment IV */
wolfSSL 0:d92f9d21154c 2575 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
wolfSSL 0:d92f9d21154c 2576 if (++((byte *)aes->iv_ce)[i])
wolfSSL 0:d92f9d21154c 2577 break ;
wolfSSL 0:d92f9d21154c 2578 }
wolfSSL 0:d92f9d21154c 2579 even -= AES_BLOCK_SIZE ;
wolfSSL 0:d92f9d21154c 2580 } while((int)even > 0) ;
wolfSSL 0:d92f9d21154c 2581 }
wolfSSL 0:d92f9d21154c 2582 if(odd) {
wolfSSL 0:d92f9d21154c 2583 XMEMSET(tmp+aes->left, 0x0, AES_BLOCK_SIZE - aes->left) ;
wolfSSL 0:d92f9d21154c 2584 XMEMCPY(tmp+aes->left, in, odd) ;
wolfSSL 0:d92f9d21154c 2585 wc_AesCrypt(aes, out_block, tmp, AES_BLOCK_SIZE,
wolfSSL 0:d92f9d21154c 2586 PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCTR);
wolfSSL 0:d92f9d21154c 2587 XMEMCPY(out, out_block+aes->left,odd) ;
wolfSSL 0:d92f9d21154c 2588 aes->left += odd ;
wolfSSL 0:d92f9d21154c 2589 }
wolfSSL 0:d92f9d21154c 2590 }
wolfSSL 0:d92f9d21154c 2591
wolfSSL 0:d92f9d21154c 2592 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 0:d92f9d21154c 2593 #error "Coldfire SEC doesn't currently support AES-CTR mode"
wolfSSL 0:d92f9d21154c 2594
wolfSSL 0:d92f9d21154c 2595 #elif defined(FREESCALE_MMCAU)
wolfSSL 0:d92f9d21154c 2596 #error "Freescale mmCAU doesn't currently support AES-CTR mode"
wolfSSL 0:d92f9d21154c 2597
wolfSSL 0:d92f9d21154c 2598 #else
wolfSSL 0:d92f9d21154c 2599 /* Increment AES counter */
wolfSSL 0:d92f9d21154c 2600 static INLINE void IncrementAesCounter(byte* inOutCtr)
wolfSSL 0:d92f9d21154c 2601 {
wolfSSL 0:d92f9d21154c 2602 int i;
wolfSSL 0:d92f9d21154c 2603
wolfSSL 0:d92f9d21154c 2604 /* in network byte order so start at end and work back */
wolfSSL 0:d92f9d21154c 2605 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
wolfSSL 0:d92f9d21154c 2606 if (++inOutCtr[i]) /* we're done unless we overflow */
wolfSSL 0:d92f9d21154c 2607 return;
wolfSSL 0:d92f9d21154c 2608 }
wolfSSL 0:d92f9d21154c 2609 }
wolfSSL 0:d92f9d21154c 2610
wolfSSL 0:d92f9d21154c 2611 void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
wolfSSL 0:d92f9d21154c 2612 {
wolfSSL 0:d92f9d21154c 2613 byte* tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
wolfSSL 0:d92f9d21154c 2614
wolfSSL 0:d92f9d21154c 2615 /* consume any unused bytes left in aes->tmp */
wolfSSL 0:d92f9d21154c 2616 while (aes->left && sz) {
wolfSSL 0:d92f9d21154c 2617 *(out++) = *(in++) ^ *(tmp++);
wolfSSL 0:d92f9d21154c 2618 aes->left--;
wolfSSL 0:d92f9d21154c 2619 sz--;
wolfSSL 0:d92f9d21154c 2620 }
wolfSSL 0:d92f9d21154c 2621
wolfSSL 0:d92f9d21154c 2622 /* do as many block size ops as possible */
wolfSSL 0:d92f9d21154c 2623 while (sz >= AES_BLOCK_SIZE) {
wolfSSL 0:d92f9d21154c 2624 wc_AesEncrypt(aes, (byte*)aes->reg, out);
wolfSSL 0:d92f9d21154c 2625 IncrementAesCounter((byte*)aes->reg);
wolfSSL 0:d92f9d21154c 2626 xorbuf(out, in, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2627
wolfSSL 0:d92f9d21154c 2628 out += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2629 in += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2630 sz -= AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2631 aes->left = 0;
wolfSSL 0:d92f9d21154c 2632 }
wolfSSL 0:d92f9d21154c 2633
wolfSSL 0:d92f9d21154c 2634 /* handle non block size remaining and sotre unused byte count in left */
wolfSSL 0:d92f9d21154c 2635 if (sz) {
wolfSSL 0:d92f9d21154c 2636 wc_AesEncrypt(aes, (byte*)aes->reg, (byte*)aes->tmp);
wolfSSL 0:d92f9d21154c 2637 IncrementAesCounter((byte*)aes->reg);
wolfSSL 0:d92f9d21154c 2638
wolfSSL 0:d92f9d21154c 2639 aes->left = AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2640 tmp = (byte*)aes->tmp;
wolfSSL 0:d92f9d21154c 2641
wolfSSL 0:d92f9d21154c 2642 while (sz--) {
wolfSSL 0:d92f9d21154c 2643 *(out++) = *(in++) ^ *(tmp++);
wolfSSL 0:d92f9d21154c 2644 aes->left--;
wolfSSL 0:d92f9d21154c 2645 }
wolfSSL 0:d92f9d21154c 2646 }
wolfSSL 0:d92f9d21154c 2647 }
wolfSSL 0:d92f9d21154c 2648
wolfSSL 0:d92f9d21154c 2649 #endif /* STM32F2_CRYPTO, AES-CTR block */
wolfSSL 0:d92f9d21154c 2650
wolfSSL 0:d92f9d21154c 2651 #endif /* WOLFSSL_AES_COUNTER */
wolfSSL 0:d92f9d21154c 2652
wolfSSL 0:d92f9d21154c 2653 #ifdef HAVE_AESGCM
wolfSSL 0:d92f9d21154c 2654
wolfSSL 0:d92f9d21154c 2655 /*
wolfSSL 0:d92f9d21154c 2656 * The IV for AES GCM, stored in struct Aes's member reg, is comprised of
wolfSSL 0:d92f9d21154c 2657 * three parts in order:
wolfSSL 0:d92f9d21154c 2658 * 1. The implicit IV. This is generated from the PRF using the shared
wolfSSL 0:d92f9d21154c 2659 * secrets between endpoints. It is 4 bytes long.
wolfSSL 0:d92f9d21154c 2660 * 2. The explicit IV. This is set by the user of the AES. It needs to be
wolfSSL 0:d92f9d21154c 2661 * unique for each call to encrypt. The explicit IV is shared with the
wolfSSL 0:d92f9d21154c 2662 * other end of the transaction in the clear.
wolfSSL 0:d92f9d21154c 2663 * 3. The counter. Each block of data is encrypted with its own sequence
wolfSSL 0:d92f9d21154c 2664 * number counter.
wolfSSL 0:d92f9d21154c 2665 */
wolfSSL 0:d92f9d21154c 2666
wolfSSL 0:d92f9d21154c 2667 #ifdef STM32F2_CRYPTO
wolfSSL 0:d92f9d21154c 2668 #error "STM32F2 crypto doesn't currently support AES-GCM mode"
wolfSSL 0:d92f9d21154c 2669
wolfSSL 0:d92f9d21154c 2670 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 0:d92f9d21154c 2671 #error "Coldfire SEC doesn't currently support AES-GCM mode"
wolfSSL 0:d92f9d21154c 2672
wolfSSL 0:d92f9d21154c 2673 #endif
wolfSSL 0:d92f9d21154c 2674
wolfSSL 0:d92f9d21154c 2675 enum {
wolfSSL 0:d92f9d21154c 2676 CTR_SZ = 4
wolfSSL 0:d92f9d21154c 2677 };
wolfSSL 0:d92f9d21154c 2678
wolfSSL 0:d92f9d21154c 2679
wolfSSL 0:d92f9d21154c 2680 static INLINE void InitGcmCounter(byte* inOutCtr)
wolfSSL 0:d92f9d21154c 2681 {
wolfSSL 0:d92f9d21154c 2682 inOutCtr[AES_BLOCK_SIZE - 4] = 0;
wolfSSL 0:d92f9d21154c 2683 inOutCtr[AES_BLOCK_SIZE - 3] = 0;
wolfSSL 0:d92f9d21154c 2684 inOutCtr[AES_BLOCK_SIZE - 2] = 0;
wolfSSL 0:d92f9d21154c 2685 inOutCtr[AES_BLOCK_SIZE - 1] = 1;
wolfSSL 0:d92f9d21154c 2686 }
wolfSSL 0:d92f9d21154c 2687
wolfSSL 0:d92f9d21154c 2688
wolfSSL 0:d92f9d21154c 2689 static INLINE void IncrementGcmCounter(byte* inOutCtr)
wolfSSL 0:d92f9d21154c 2690 {
wolfSSL 0:d92f9d21154c 2691 int i;
wolfSSL 0:d92f9d21154c 2692
wolfSSL 0:d92f9d21154c 2693 /* in network byte order so start at end and work back */
wolfSSL 0:d92f9d21154c 2694 for (i = AES_BLOCK_SIZE - 1; i >= AES_BLOCK_SIZE - CTR_SZ; i--) {
wolfSSL 0:d92f9d21154c 2695 if (++inOutCtr[i]) /* we're done unless we overflow */
wolfSSL 0:d92f9d21154c 2696 return;
wolfSSL 0:d92f9d21154c 2697 }
wolfSSL 0:d92f9d21154c 2698 }
wolfSSL 0:d92f9d21154c 2699
wolfSSL 0:d92f9d21154c 2700
wolfSSL 0:d92f9d21154c 2701 #if defined(GCM_SMALL) || defined(GCM_TABLE)
wolfSSL 0:d92f9d21154c 2702
wolfSSL 0:d92f9d21154c 2703 static INLINE void FlattenSzInBits(byte* buf, word32 sz)
wolfSSL 0:d92f9d21154c 2704 {
wolfSSL 0:d92f9d21154c 2705 /* Multiply the sz by 8 */
wolfSSL 0:d92f9d21154c 2706 word32 szHi = (sz >> (8*sizeof(sz) - 3));
wolfSSL 0:d92f9d21154c 2707 sz <<= 3;
wolfSSL 0:d92f9d21154c 2708
wolfSSL 0:d92f9d21154c 2709 /* copy over the words of the sz into the destination buffer */
wolfSSL 0:d92f9d21154c 2710 buf[0] = (szHi >> 24) & 0xff;
wolfSSL 0:d92f9d21154c 2711 buf[1] = (szHi >> 16) & 0xff;
wolfSSL 0:d92f9d21154c 2712 buf[2] = (szHi >> 8) & 0xff;
wolfSSL 0:d92f9d21154c 2713 buf[3] = szHi & 0xff;
wolfSSL 0:d92f9d21154c 2714 buf[4] = (sz >> 24) & 0xff;
wolfSSL 0:d92f9d21154c 2715 buf[5] = (sz >> 16) & 0xff;
wolfSSL 0:d92f9d21154c 2716 buf[6] = (sz >> 8) & 0xff;
wolfSSL 0:d92f9d21154c 2717 buf[7] = sz & 0xff;
wolfSSL 0:d92f9d21154c 2718 }
wolfSSL 0:d92f9d21154c 2719
wolfSSL 0:d92f9d21154c 2720
wolfSSL 0:d92f9d21154c 2721 static INLINE void RIGHTSHIFTX(byte* x)
wolfSSL 0:d92f9d21154c 2722 {
wolfSSL 0:d92f9d21154c 2723 int i;
wolfSSL 0:d92f9d21154c 2724 int carryOut = 0;
wolfSSL 0:d92f9d21154c 2725 int carryIn = 0;
wolfSSL 0:d92f9d21154c 2726 int borrow = x[15] & 0x01;
wolfSSL 0:d92f9d21154c 2727
wolfSSL 0:d92f9d21154c 2728 for (i = 0; i < AES_BLOCK_SIZE; i++) {
wolfSSL 0:d92f9d21154c 2729 carryOut = x[i] & 0x01;
wolfSSL 0:d92f9d21154c 2730 x[i] = (x[i] >> 1) | (carryIn ? 0x80 : 0);
wolfSSL 0:d92f9d21154c 2731 carryIn = carryOut;
wolfSSL 0:d92f9d21154c 2732 }
wolfSSL 0:d92f9d21154c 2733 if (borrow) x[0] ^= 0xE1;
wolfSSL 0:d92f9d21154c 2734 }
wolfSSL 0:d92f9d21154c 2735
wolfSSL 0:d92f9d21154c 2736 #endif /* defined(GCM_SMALL) || defined(GCM_TABLE) */
wolfSSL 0:d92f9d21154c 2737
wolfSSL 0:d92f9d21154c 2738
wolfSSL 0:d92f9d21154c 2739 #ifdef GCM_TABLE
wolfSSL 0:d92f9d21154c 2740
wolfSSL 0:d92f9d21154c 2741 static void GenerateM0(Aes* aes)
wolfSSL 0:d92f9d21154c 2742 {
wolfSSL 0:d92f9d21154c 2743 int i, j;
wolfSSL 0:d92f9d21154c 2744 byte (*m)[AES_BLOCK_SIZE] = aes->M0;
wolfSSL 0:d92f9d21154c 2745
wolfSSL 0:d92f9d21154c 2746 XMEMCPY(m[128], aes->H, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2747
wolfSSL 0:d92f9d21154c 2748 for (i = 64; i > 0; i /= 2) {
wolfSSL 0:d92f9d21154c 2749 XMEMCPY(m[i], m[i*2], AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2750 RIGHTSHIFTX(m[i]);
wolfSSL 0:d92f9d21154c 2751 }
wolfSSL 0:d92f9d21154c 2752
wolfSSL 0:d92f9d21154c 2753 for (i = 2; i < 256; i *= 2) {
wolfSSL 0:d92f9d21154c 2754 for (j = 1; j < i; j++) {
wolfSSL 0:d92f9d21154c 2755 XMEMCPY(m[i+j], m[i], AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2756 xorbuf(m[i+j], m[j], AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2757 }
wolfSSL 0:d92f9d21154c 2758 }
wolfSSL 0:d92f9d21154c 2759
wolfSSL 0:d92f9d21154c 2760 XMEMSET(m[0], 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2761 }
wolfSSL 0:d92f9d21154c 2762
wolfSSL 0:d92f9d21154c 2763 #endif /* GCM_TABLE */
wolfSSL 0:d92f9d21154c 2764
wolfSSL 0:d92f9d21154c 2765
wolfSSL 0:d92f9d21154c 2766 int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
wolfSSL 0:d92f9d21154c 2767 {
wolfSSL 0:d92f9d21154c 2768 int ret;
wolfSSL 0:d92f9d21154c 2769 byte iv[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 2770
wolfSSL 0:d92f9d21154c 2771 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 2772 byte* rk = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 2773 #endif
wolfSSL 0:d92f9d21154c 2774
wolfSSL 0:d92f9d21154c 2775 if (!((len == 16) || (len == 24) || (len == 32)))
wolfSSL 0:d92f9d21154c 2776 return BAD_FUNC_ARG;
wolfSSL 0:d92f9d21154c 2777
wolfSSL 0:d92f9d21154c 2778 XMEMSET(iv, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2779 ret = wc_AesSetKey(aes, key, len, iv, AES_ENCRYPTION);
wolfSSL 0:d92f9d21154c 2780
wolfSSL 0:d92f9d21154c 2781 if (ret == 0) {
wolfSSL 0:d92f9d21154c 2782 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 2783 cau_aes_encrypt(iv, rk, aes->rounds, aes->H);
wolfSSL 0:d92f9d21154c 2784 #else
wolfSSL 0:d92f9d21154c 2785 wc_AesEncrypt(aes, iv, aes->H);
wolfSSL 0:d92f9d21154c 2786 #endif
wolfSSL 0:d92f9d21154c 2787 #ifdef GCM_TABLE
wolfSSL 0:d92f9d21154c 2788 GenerateM0(aes);
wolfSSL 0:d92f9d21154c 2789 #endif /* GCM_TABLE */
wolfSSL 0:d92f9d21154c 2790 }
wolfSSL 0:d92f9d21154c 2791
wolfSSL 0:d92f9d21154c 2792 return ret;
wolfSSL 0:d92f9d21154c 2793 }
wolfSSL 0:d92f9d21154c 2794
wolfSSL 0:d92f9d21154c 2795
wolfSSL 0:d92f9d21154c 2796 #if defined(GCM_SMALL)
wolfSSL 0:d92f9d21154c 2797
wolfSSL 0:d92f9d21154c 2798 static void GMULT(byte* X, byte* Y)
wolfSSL 0:d92f9d21154c 2799 {
wolfSSL 0:d92f9d21154c 2800 byte Z[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 2801 byte V[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 2802 int i, j;
wolfSSL 0:d92f9d21154c 2803
wolfSSL 0:d92f9d21154c 2804 XMEMSET(Z, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2805 XMEMCPY(V, X, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2806 for (i = 0; i < AES_BLOCK_SIZE; i++)
wolfSSL 0:d92f9d21154c 2807 {
wolfSSL 0:d92f9d21154c 2808 byte y = Y[i];
wolfSSL 0:d92f9d21154c 2809 for (j = 0; j < 8; j++)
wolfSSL 0:d92f9d21154c 2810 {
wolfSSL 0:d92f9d21154c 2811 if (y & 0x80) {
wolfSSL 0:d92f9d21154c 2812 xorbuf(Z, V, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2813 }
wolfSSL 0:d92f9d21154c 2814
wolfSSL 0:d92f9d21154c 2815 RIGHTSHIFTX(V);
wolfSSL 0:d92f9d21154c 2816 y = y << 1;
wolfSSL 0:d92f9d21154c 2817 }
wolfSSL 0:d92f9d21154c 2818 }
wolfSSL 0:d92f9d21154c 2819 XMEMCPY(X, Z, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2820 }
wolfSSL 0:d92f9d21154c 2821
wolfSSL 0:d92f9d21154c 2822
wolfSSL 0:d92f9d21154c 2823 static void GHASH(Aes* aes, const byte* a, word32 aSz,
wolfSSL 0:d92f9d21154c 2824 const byte* c, word32 cSz, byte* s, word32 sSz)
wolfSSL 0:d92f9d21154c 2825 {
wolfSSL 0:d92f9d21154c 2826 byte x[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 2827 byte scratch[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 2828 word32 blocks, partial;
wolfSSL 0:d92f9d21154c 2829 byte* h = aes->H;
wolfSSL 0:d92f9d21154c 2830
wolfSSL 0:d92f9d21154c 2831 XMEMSET(x, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2832
wolfSSL 0:d92f9d21154c 2833 /* Hash in A, the Additional Authentication Data */
wolfSSL 0:d92f9d21154c 2834 if (aSz != 0 && a != NULL) {
wolfSSL 0:d92f9d21154c 2835 blocks = aSz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2836 partial = aSz % AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2837 while (blocks--) {
wolfSSL 0:d92f9d21154c 2838 xorbuf(x, a, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2839 GMULT(x, h);
wolfSSL 0:d92f9d21154c 2840 a += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2841 }
wolfSSL 0:d92f9d21154c 2842 if (partial != 0) {
wolfSSL 0:d92f9d21154c 2843 XMEMSET(scratch, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2844 XMEMCPY(scratch, a, partial);
wolfSSL 0:d92f9d21154c 2845 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2846 GMULT(x, h);
wolfSSL 0:d92f9d21154c 2847 }
wolfSSL 0:d92f9d21154c 2848 }
wolfSSL 0:d92f9d21154c 2849
wolfSSL 0:d92f9d21154c 2850 /* Hash in C, the Ciphertext */
wolfSSL 0:d92f9d21154c 2851 if (cSz != 0 && c != NULL) {
wolfSSL 0:d92f9d21154c 2852 blocks = cSz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2853 partial = cSz % AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2854 while (blocks--) {
wolfSSL 0:d92f9d21154c 2855 xorbuf(x, c, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2856 GMULT(x, h);
wolfSSL 0:d92f9d21154c 2857 c += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2858 }
wolfSSL 0:d92f9d21154c 2859 if (partial != 0) {
wolfSSL 0:d92f9d21154c 2860 XMEMSET(scratch, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2861 XMEMCPY(scratch, c, partial);
wolfSSL 0:d92f9d21154c 2862 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2863 GMULT(x, h);
wolfSSL 0:d92f9d21154c 2864 }
wolfSSL 0:d92f9d21154c 2865 }
wolfSSL 0:d92f9d21154c 2866
wolfSSL 0:d92f9d21154c 2867 /* Hash in the lengths of A and C in bits */
wolfSSL 0:d92f9d21154c 2868 FlattenSzInBits(&scratch[0], aSz);
wolfSSL 0:d92f9d21154c 2869 FlattenSzInBits(&scratch[8], cSz);
wolfSSL 0:d92f9d21154c 2870 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2871 GMULT(x, h);
wolfSSL 0:d92f9d21154c 2872
wolfSSL 0:d92f9d21154c 2873 /* Copy the result into s. */
wolfSSL 0:d92f9d21154c 2874 XMEMCPY(s, x, sSz);
wolfSSL 0:d92f9d21154c 2875 }
wolfSSL 0:d92f9d21154c 2876
wolfSSL 0:d92f9d21154c 2877 /* end GCM_SMALL */
wolfSSL 0:d92f9d21154c 2878 #elif defined(GCM_TABLE)
wolfSSL 0:d92f9d21154c 2879
wolfSSL 0:d92f9d21154c 2880 static const byte R[256][2] = {
wolfSSL 0:d92f9d21154c 2881 {0x00, 0x00}, {0x01, 0xc2}, {0x03, 0x84}, {0x02, 0x46},
wolfSSL 0:d92f9d21154c 2882 {0x07, 0x08}, {0x06, 0xca}, {0x04, 0x8c}, {0x05, 0x4e},
wolfSSL 0:d92f9d21154c 2883 {0x0e, 0x10}, {0x0f, 0xd2}, {0x0d, 0x94}, {0x0c, 0x56},
wolfSSL 0:d92f9d21154c 2884 {0x09, 0x18}, {0x08, 0xda}, {0x0a, 0x9c}, {0x0b, 0x5e},
wolfSSL 0:d92f9d21154c 2885 {0x1c, 0x20}, {0x1d, 0xe2}, {0x1f, 0xa4}, {0x1e, 0x66},
wolfSSL 0:d92f9d21154c 2886 {0x1b, 0x28}, {0x1a, 0xea}, {0x18, 0xac}, {0x19, 0x6e},
wolfSSL 0:d92f9d21154c 2887 {0x12, 0x30}, {0x13, 0xf2}, {0x11, 0xb4}, {0x10, 0x76},
wolfSSL 0:d92f9d21154c 2888 {0x15, 0x38}, {0x14, 0xfa}, {0x16, 0xbc}, {0x17, 0x7e},
wolfSSL 0:d92f9d21154c 2889 {0x38, 0x40}, {0x39, 0x82}, {0x3b, 0xc4}, {0x3a, 0x06},
wolfSSL 0:d92f9d21154c 2890 {0x3f, 0x48}, {0x3e, 0x8a}, {0x3c, 0xcc}, {0x3d, 0x0e},
wolfSSL 0:d92f9d21154c 2891 {0x36, 0x50}, {0x37, 0x92}, {0x35, 0xd4}, {0x34, 0x16},
wolfSSL 0:d92f9d21154c 2892 {0x31, 0x58}, {0x30, 0x9a}, {0x32, 0xdc}, {0x33, 0x1e},
wolfSSL 0:d92f9d21154c 2893 {0x24, 0x60}, {0x25, 0xa2}, {0x27, 0xe4}, {0x26, 0x26},
wolfSSL 0:d92f9d21154c 2894 {0x23, 0x68}, {0x22, 0xaa}, {0x20, 0xec}, {0x21, 0x2e},
wolfSSL 0:d92f9d21154c 2895 {0x2a, 0x70}, {0x2b, 0xb2}, {0x29, 0xf4}, {0x28, 0x36},
wolfSSL 0:d92f9d21154c 2896 {0x2d, 0x78}, {0x2c, 0xba}, {0x2e, 0xfc}, {0x2f, 0x3e},
wolfSSL 0:d92f9d21154c 2897 {0x70, 0x80}, {0x71, 0x42}, {0x73, 0x04}, {0x72, 0xc6},
wolfSSL 0:d92f9d21154c 2898 {0x77, 0x88}, {0x76, 0x4a}, {0x74, 0x0c}, {0x75, 0xce},
wolfSSL 0:d92f9d21154c 2899 {0x7e, 0x90}, {0x7f, 0x52}, {0x7d, 0x14}, {0x7c, 0xd6},
wolfSSL 0:d92f9d21154c 2900 {0x79, 0x98}, {0x78, 0x5a}, {0x7a, 0x1c}, {0x7b, 0xde},
wolfSSL 0:d92f9d21154c 2901 {0x6c, 0xa0}, {0x6d, 0x62}, {0x6f, 0x24}, {0x6e, 0xe6},
wolfSSL 0:d92f9d21154c 2902 {0x6b, 0xa8}, {0x6a, 0x6a}, {0x68, 0x2c}, {0x69, 0xee},
wolfSSL 0:d92f9d21154c 2903 {0x62, 0xb0}, {0x63, 0x72}, {0x61, 0x34}, {0x60, 0xf6},
wolfSSL 0:d92f9d21154c 2904 {0x65, 0xb8}, {0x64, 0x7a}, {0x66, 0x3c}, {0x67, 0xfe},
wolfSSL 0:d92f9d21154c 2905 {0x48, 0xc0}, {0x49, 0x02}, {0x4b, 0x44}, {0x4a, 0x86},
wolfSSL 0:d92f9d21154c 2906 {0x4f, 0xc8}, {0x4e, 0x0a}, {0x4c, 0x4c}, {0x4d, 0x8e},
wolfSSL 0:d92f9d21154c 2907 {0x46, 0xd0}, {0x47, 0x12}, {0x45, 0x54}, {0x44, 0x96},
wolfSSL 0:d92f9d21154c 2908 {0x41, 0xd8}, {0x40, 0x1a}, {0x42, 0x5c}, {0x43, 0x9e},
wolfSSL 0:d92f9d21154c 2909 {0x54, 0xe0}, {0x55, 0x22}, {0x57, 0x64}, {0x56, 0xa6},
wolfSSL 0:d92f9d21154c 2910 {0x53, 0xe8}, {0x52, 0x2a}, {0x50, 0x6c}, {0x51, 0xae},
wolfSSL 0:d92f9d21154c 2911 {0x5a, 0xf0}, {0x5b, 0x32}, {0x59, 0x74}, {0x58, 0xb6},
wolfSSL 0:d92f9d21154c 2912 {0x5d, 0xf8}, {0x5c, 0x3a}, {0x5e, 0x7c}, {0x5f, 0xbe},
wolfSSL 0:d92f9d21154c 2913 {0xe1, 0x00}, {0xe0, 0xc2}, {0xe2, 0x84}, {0xe3, 0x46},
wolfSSL 0:d92f9d21154c 2914 {0xe6, 0x08}, {0xe7, 0xca}, {0xe5, 0x8c}, {0xe4, 0x4e},
wolfSSL 0:d92f9d21154c 2915 {0xef, 0x10}, {0xee, 0xd2}, {0xec, 0x94}, {0xed, 0x56},
wolfSSL 0:d92f9d21154c 2916 {0xe8, 0x18}, {0xe9, 0xda}, {0xeb, 0x9c}, {0xea, 0x5e},
wolfSSL 0:d92f9d21154c 2917 {0xfd, 0x20}, {0xfc, 0xe2}, {0xfe, 0xa4}, {0xff, 0x66},
wolfSSL 0:d92f9d21154c 2918 {0xfa, 0x28}, {0xfb, 0xea}, {0xf9, 0xac}, {0xf8, 0x6e},
wolfSSL 0:d92f9d21154c 2919 {0xf3, 0x30}, {0xf2, 0xf2}, {0xf0, 0xb4}, {0xf1, 0x76},
wolfSSL 0:d92f9d21154c 2920 {0xf4, 0x38}, {0xf5, 0xfa}, {0xf7, 0xbc}, {0xf6, 0x7e},
wolfSSL 0:d92f9d21154c 2921 {0xd9, 0x40}, {0xd8, 0x82}, {0xda, 0xc4}, {0xdb, 0x06},
wolfSSL 0:d92f9d21154c 2922 {0xde, 0x48}, {0xdf, 0x8a}, {0xdd, 0xcc}, {0xdc, 0x0e},
wolfSSL 0:d92f9d21154c 2923 {0xd7, 0x50}, {0xd6, 0x92}, {0xd4, 0xd4}, {0xd5, 0x16},
wolfSSL 0:d92f9d21154c 2924 {0xd0, 0x58}, {0xd1, 0x9a}, {0xd3, 0xdc}, {0xd2, 0x1e},
wolfSSL 0:d92f9d21154c 2925 {0xc5, 0x60}, {0xc4, 0xa2}, {0xc6, 0xe4}, {0xc7, 0x26},
wolfSSL 0:d92f9d21154c 2926 {0xc2, 0x68}, {0xc3, 0xaa}, {0xc1, 0xec}, {0xc0, 0x2e},
wolfSSL 0:d92f9d21154c 2927 {0xcb, 0x70}, {0xca, 0xb2}, {0xc8, 0xf4}, {0xc9, 0x36},
wolfSSL 0:d92f9d21154c 2928 {0xcc, 0x78}, {0xcd, 0xba}, {0xcf, 0xfc}, {0xce, 0x3e},
wolfSSL 0:d92f9d21154c 2929 {0x91, 0x80}, {0x90, 0x42}, {0x92, 0x04}, {0x93, 0xc6},
wolfSSL 0:d92f9d21154c 2930 {0x96, 0x88}, {0x97, 0x4a}, {0x95, 0x0c}, {0x94, 0xce},
wolfSSL 0:d92f9d21154c 2931 {0x9f, 0x90}, {0x9e, 0x52}, {0x9c, 0x14}, {0x9d, 0xd6},
wolfSSL 0:d92f9d21154c 2932 {0x98, 0x98}, {0x99, 0x5a}, {0x9b, 0x1c}, {0x9a, 0xde},
wolfSSL 0:d92f9d21154c 2933 {0x8d, 0xa0}, {0x8c, 0x62}, {0x8e, 0x24}, {0x8f, 0xe6},
wolfSSL 0:d92f9d21154c 2934 {0x8a, 0xa8}, {0x8b, 0x6a}, {0x89, 0x2c}, {0x88, 0xee},
wolfSSL 0:d92f9d21154c 2935 {0x83, 0xb0}, {0x82, 0x72}, {0x80, 0x34}, {0x81, 0xf6},
wolfSSL 0:d92f9d21154c 2936 {0x84, 0xb8}, {0x85, 0x7a}, {0x87, 0x3c}, {0x86, 0xfe},
wolfSSL 0:d92f9d21154c 2937 {0xa9, 0xc0}, {0xa8, 0x02}, {0xaa, 0x44}, {0xab, 0x86},
wolfSSL 0:d92f9d21154c 2938 {0xae, 0xc8}, {0xaf, 0x0a}, {0xad, 0x4c}, {0xac, 0x8e},
wolfSSL 0:d92f9d21154c 2939 {0xa7, 0xd0}, {0xa6, 0x12}, {0xa4, 0x54}, {0xa5, 0x96},
wolfSSL 0:d92f9d21154c 2940 {0xa0, 0xd8}, {0xa1, 0x1a}, {0xa3, 0x5c}, {0xa2, 0x9e},
wolfSSL 0:d92f9d21154c 2941 {0xb5, 0xe0}, {0xb4, 0x22}, {0xb6, 0x64}, {0xb7, 0xa6},
wolfSSL 0:d92f9d21154c 2942 {0xb2, 0xe8}, {0xb3, 0x2a}, {0xb1, 0x6c}, {0xb0, 0xae},
wolfSSL 0:d92f9d21154c 2943 {0xbb, 0xf0}, {0xba, 0x32}, {0xb8, 0x74}, {0xb9, 0xb6},
wolfSSL 0:d92f9d21154c 2944 {0xbc, 0xf8}, {0xbd, 0x3a}, {0xbf, 0x7c}, {0xbe, 0xbe} };
wolfSSL 0:d92f9d21154c 2945
wolfSSL 0:d92f9d21154c 2946
wolfSSL 0:d92f9d21154c 2947 static void GMULT(byte *x, byte m[256][AES_BLOCK_SIZE])
wolfSSL 0:d92f9d21154c 2948 {
wolfSSL 0:d92f9d21154c 2949 int i, j;
wolfSSL 0:d92f9d21154c 2950 byte Z[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 2951 byte a;
wolfSSL 0:d92f9d21154c 2952
wolfSSL 0:d92f9d21154c 2953 XMEMSET(Z, 0, sizeof(Z));
wolfSSL 0:d92f9d21154c 2954
wolfSSL 0:d92f9d21154c 2955 for (i = 15; i > 0; i--) {
wolfSSL 0:d92f9d21154c 2956 xorbuf(Z, m[x[i]], AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2957 a = Z[15];
wolfSSL 0:d92f9d21154c 2958
wolfSSL 0:d92f9d21154c 2959 for (j = 15; j > 0; j--) {
wolfSSL 0:d92f9d21154c 2960 Z[j] = Z[j-1];
wolfSSL 0:d92f9d21154c 2961 }
wolfSSL 0:d92f9d21154c 2962
wolfSSL 0:d92f9d21154c 2963 Z[0] = R[a][0];
wolfSSL 0:d92f9d21154c 2964 Z[1] ^= R[a][1];
wolfSSL 0:d92f9d21154c 2965 }
wolfSSL 0:d92f9d21154c 2966 xorbuf(Z, m[x[0]], AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2967
wolfSSL 0:d92f9d21154c 2968 XMEMCPY(x, Z, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2969 }
wolfSSL 0:d92f9d21154c 2970
wolfSSL 0:d92f9d21154c 2971
wolfSSL 0:d92f9d21154c 2972 static void GHASH(Aes* aes, const byte* a, word32 aSz,
wolfSSL 0:d92f9d21154c 2973 const byte* c, word32 cSz, byte* s, word32 sSz)
wolfSSL 0:d92f9d21154c 2974 {
wolfSSL 0:d92f9d21154c 2975 byte x[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 2976 byte scratch[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 2977 word32 blocks, partial;
wolfSSL 0:d92f9d21154c 2978
wolfSSL 0:d92f9d21154c 2979 XMEMSET(x, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2980
wolfSSL 0:d92f9d21154c 2981 /* Hash in A, the Additional Authentication Data */
wolfSSL 0:d92f9d21154c 2982 if (aSz != 0 && a != NULL) {
wolfSSL 0:d92f9d21154c 2983 blocks = aSz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2984 partial = aSz % AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2985 while (blocks--) {
wolfSSL 0:d92f9d21154c 2986 xorbuf(x, a, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2987 GMULT(x, aes->M0);
wolfSSL 0:d92f9d21154c 2988 a += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 2989 }
wolfSSL 0:d92f9d21154c 2990 if (partial != 0) {
wolfSSL 0:d92f9d21154c 2991 XMEMSET(scratch, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2992 XMEMCPY(scratch, a, partial);
wolfSSL 0:d92f9d21154c 2993 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 2994 GMULT(x, aes->M0);
wolfSSL 0:d92f9d21154c 2995 }
wolfSSL 0:d92f9d21154c 2996 }
wolfSSL 0:d92f9d21154c 2997
wolfSSL 0:d92f9d21154c 2998 /* Hash in C, the Ciphertext */
wolfSSL 0:d92f9d21154c 2999 if (cSz != 0 && c != NULL) {
wolfSSL 0:d92f9d21154c 3000 blocks = cSz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3001 partial = cSz % AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3002 while (blocks--) {
wolfSSL 0:d92f9d21154c 3003 xorbuf(x, c, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3004 GMULT(x, aes->M0);
wolfSSL 0:d92f9d21154c 3005 c += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3006 }
wolfSSL 0:d92f9d21154c 3007 if (partial != 0) {
wolfSSL 0:d92f9d21154c 3008 XMEMSET(scratch, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3009 XMEMCPY(scratch, c, partial);
wolfSSL 0:d92f9d21154c 3010 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3011 GMULT(x, aes->M0);
wolfSSL 0:d92f9d21154c 3012 }
wolfSSL 0:d92f9d21154c 3013 }
wolfSSL 0:d92f9d21154c 3014
wolfSSL 0:d92f9d21154c 3015 /* Hash in the lengths of A and C in bits */
wolfSSL 0:d92f9d21154c 3016 FlattenSzInBits(&scratch[0], aSz);
wolfSSL 0:d92f9d21154c 3017 FlattenSzInBits(&scratch[8], cSz);
wolfSSL 0:d92f9d21154c 3018 xorbuf(x, scratch, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3019 GMULT(x, aes->M0);
wolfSSL 0:d92f9d21154c 3020
wolfSSL 0:d92f9d21154c 3021 /* Copy the result into s. */
wolfSSL 0:d92f9d21154c 3022 XMEMCPY(s, x, sSz);
wolfSSL 0:d92f9d21154c 3023 }
wolfSSL 0:d92f9d21154c 3024
wolfSSL 0:d92f9d21154c 3025 /* end GCM_TABLE */
wolfSSL 0:d92f9d21154c 3026 #elif defined(WORD64_AVAILABLE) && !defined(GCM_WORD32)
wolfSSL 0:d92f9d21154c 3027
wolfSSL 0:d92f9d21154c 3028 static void GMULT(word64* X, word64* Y)
wolfSSL 0:d92f9d21154c 3029 {
wolfSSL 0:d92f9d21154c 3030 word64 Z[2] = {0,0};
wolfSSL 0:d92f9d21154c 3031 word64 V[2] ;
wolfSSL 0:d92f9d21154c 3032 int i, j;
wolfSSL 0:d92f9d21154c 3033 V[0] = X[0] ; V[1] = X[1] ;
wolfSSL 0:d92f9d21154c 3034
wolfSSL 0:d92f9d21154c 3035 for (i = 0; i < 2; i++)
wolfSSL 0:d92f9d21154c 3036 {
wolfSSL 0:d92f9d21154c 3037 word64 y = Y[i];
wolfSSL 0:d92f9d21154c 3038 for (j = 0; j < 64; j++)
wolfSSL 0:d92f9d21154c 3039 {
wolfSSL 0:d92f9d21154c 3040 if (y & 0x8000000000000000ULL) {
wolfSSL 0:d92f9d21154c 3041 Z[0] ^= V[0];
wolfSSL 0:d92f9d21154c 3042 Z[1] ^= V[1];
wolfSSL 0:d92f9d21154c 3043 }
wolfSSL 0:d92f9d21154c 3044
wolfSSL 0:d92f9d21154c 3045 if (V[1] & 0x0000000000000001) {
wolfSSL 0:d92f9d21154c 3046 V[1] >>= 1;
wolfSSL 0:d92f9d21154c 3047 V[1] |= ((V[0] & 0x0000000000000001) ? 0x8000000000000000ULL : 0);
wolfSSL 0:d92f9d21154c 3048 V[0] >>= 1;
wolfSSL 0:d92f9d21154c 3049 V[0] ^= 0xE100000000000000ULL;
wolfSSL 0:d92f9d21154c 3050 }
wolfSSL 0:d92f9d21154c 3051 else {
wolfSSL 0:d92f9d21154c 3052 V[1] >>= 1;
wolfSSL 0:d92f9d21154c 3053 V[1] |= ((V[0] & 0x0000000000000001) ? 0x8000000000000000ULL : 0);
wolfSSL 0:d92f9d21154c 3054 V[0] >>= 1;
wolfSSL 0:d92f9d21154c 3055 }
wolfSSL 0:d92f9d21154c 3056 y <<= 1;
wolfSSL 0:d92f9d21154c 3057 }
wolfSSL 0:d92f9d21154c 3058 }
wolfSSL 0:d92f9d21154c 3059 X[0] = Z[0];
wolfSSL 0:d92f9d21154c 3060 X[1] = Z[1];
wolfSSL 0:d92f9d21154c 3061 }
wolfSSL 0:d92f9d21154c 3062
wolfSSL 0:d92f9d21154c 3063
wolfSSL 0:d92f9d21154c 3064 static void GHASH(Aes* aes, const byte* a, word32 aSz,
wolfSSL 0:d92f9d21154c 3065 const byte* c, word32 cSz, byte* s, word32 sSz)
wolfSSL 0:d92f9d21154c 3066 {
wolfSSL 0:d92f9d21154c 3067 word64 x[2] = {0,0};
wolfSSL 0:d92f9d21154c 3068 word32 blocks, partial;
wolfSSL 0:d92f9d21154c 3069 word64 bigH[2];
wolfSSL 0:d92f9d21154c 3070
wolfSSL 0:d92f9d21154c 3071 XMEMCPY(bigH, aes->H, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3072 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3073 ByteReverseWords64(bigH, bigH, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3074 #endif
wolfSSL 0:d92f9d21154c 3075
wolfSSL 0:d92f9d21154c 3076 /* Hash in A, the Additional Authentication Data */
wolfSSL 0:d92f9d21154c 3077 if (aSz != 0 && a != NULL) {
wolfSSL 0:d92f9d21154c 3078 word64 bigA[2];
wolfSSL 0:d92f9d21154c 3079 blocks = aSz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3080 partial = aSz % AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3081 while (blocks--) {
wolfSSL 0:d92f9d21154c 3082 XMEMCPY(bigA, a, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3083 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3084 ByteReverseWords64(bigA, bigA, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3085 #endif
wolfSSL 0:d92f9d21154c 3086 x[0] ^= bigA[0];
wolfSSL 0:d92f9d21154c 3087 x[1] ^= bigA[1];
wolfSSL 0:d92f9d21154c 3088 GMULT(x, bigH);
wolfSSL 0:d92f9d21154c 3089 a += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3090 }
wolfSSL 0:d92f9d21154c 3091 if (partial != 0) {
wolfSSL 0:d92f9d21154c 3092 XMEMSET(bigA, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3093 XMEMCPY(bigA, a, partial);
wolfSSL 0:d92f9d21154c 3094 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3095 ByteReverseWords64(bigA, bigA, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3096 #endif
wolfSSL 0:d92f9d21154c 3097 x[0] ^= bigA[0];
wolfSSL 0:d92f9d21154c 3098 x[1] ^= bigA[1];
wolfSSL 0:d92f9d21154c 3099 GMULT(x, bigH);
wolfSSL 0:d92f9d21154c 3100 }
wolfSSL 0:d92f9d21154c 3101 }
wolfSSL 0:d92f9d21154c 3102
wolfSSL 0:d92f9d21154c 3103 /* Hash in C, the Ciphertext */
wolfSSL 0:d92f9d21154c 3104 if (cSz != 0 && c != NULL) {
wolfSSL 0:d92f9d21154c 3105 word64 bigC[2];
wolfSSL 0:d92f9d21154c 3106 blocks = cSz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3107 partial = cSz % AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3108 while (blocks--) {
wolfSSL 0:d92f9d21154c 3109 XMEMCPY(bigC, c, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3110 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3111 ByteReverseWords64(bigC, bigC, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3112 #endif
wolfSSL 0:d92f9d21154c 3113 x[0] ^= bigC[0];
wolfSSL 0:d92f9d21154c 3114 x[1] ^= bigC[1];
wolfSSL 0:d92f9d21154c 3115 GMULT(x, bigH);
wolfSSL 0:d92f9d21154c 3116 c += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3117 }
wolfSSL 0:d92f9d21154c 3118 if (partial != 0) {
wolfSSL 0:d92f9d21154c 3119 XMEMSET(bigC, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3120 XMEMCPY(bigC, c, partial);
wolfSSL 0:d92f9d21154c 3121 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3122 ByteReverseWords64(bigC, bigC, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3123 #endif
wolfSSL 0:d92f9d21154c 3124 x[0] ^= bigC[0];
wolfSSL 0:d92f9d21154c 3125 x[1] ^= bigC[1];
wolfSSL 0:d92f9d21154c 3126 GMULT(x, bigH);
wolfSSL 0:d92f9d21154c 3127 }
wolfSSL 0:d92f9d21154c 3128 }
wolfSSL 0:d92f9d21154c 3129
wolfSSL 0:d92f9d21154c 3130 /* Hash in the lengths in bits of A and C */
wolfSSL 0:d92f9d21154c 3131 {
wolfSSL 0:d92f9d21154c 3132 word64 len[2] ;
wolfSSL 0:d92f9d21154c 3133 len[0] = aSz ; len[1] = cSz;
wolfSSL 0:d92f9d21154c 3134
wolfSSL 0:d92f9d21154c 3135 /* Lengths are in bytes. Convert to bits. */
wolfSSL 0:d92f9d21154c 3136 len[0] *= 8;
wolfSSL 0:d92f9d21154c 3137 len[1] *= 8;
wolfSSL 0:d92f9d21154c 3138
wolfSSL 0:d92f9d21154c 3139 x[0] ^= len[0];
wolfSSL 0:d92f9d21154c 3140 x[1] ^= len[1];
wolfSSL 0:d92f9d21154c 3141 GMULT(x, bigH);
wolfSSL 0:d92f9d21154c 3142 }
wolfSSL 0:d92f9d21154c 3143 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3144 ByteReverseWords64(x, x, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3145 #endif
wolfSSL 0:d92f9d21154c 3146 XMEMCPY(s, x, sSz);
wolfSSL 0:d92f9d21154c 3147 }
wolfSSL 0:d92f9d21154c 3148
wolfSSL 0:d92f9d21154c 3149 /* end defined(WORD64_AVAILABLE) && !defined(GCM_WORD32) */
wolfSSL 0:d92f9d21154c 3150 #else /* GCM_WORD32 */
wolfSSL 0:d92f9d21154c 3151
wolfSSL 0:d92f9d21154c 3152 static void GMULT(word32* X, word32* Y)
wolfSSL 0:d92f9d21154c 3153 {
wolfSSL 0:d92f9d21154c 3154 word32 Z[4] = {0,0,0,0};
wolfSSL 0:d92f9d21154c 3155 word32 V[4] ;
wolfSSL 0:d92f9d21154c 3156 int i, j;
wolfSSL 0:d92f9d21154c 3157
wolfSSL 0:d92f9d21154c 3158 V[0] = X[0]; V[1] = X[1]; V[2] = X[2]; V[3] = X[3];
wolfSSL 0:d92f9d21154c 3159
wolfSSL 0:d92f9d21154c 3160 for (i = 0; i < 4; i++)
wolfSSL 0:d92f9d21154c 3161 {
wolfSSL 0:d92f9d21154c 3162 word32 y = Y[i];
wolfSSL 0:d92f9d21154c 3163 for (j = 0; j < 32; j++)
wolfSSL 0:d92f9d21154c 3164 {
wolfSSL 0:d92f9d21154c 3165 if (y & 0x80000000) {
wolfSSL 0:d92f9d21154c 3166 Z[0] ^= V[0];
wolfSSL 0:d92f9d21154c 3167 Z[1] ^= V[1];
wolfSSL 0:d92f9d21154c 3168 Z[2] ^= V[2];
wolfSSL 0:d92f9d21154c 3169 Z[3] ^= V[3];
wolfSSL 0:d92f9d21154c 3170 }
wolfSSL 0:d92f9d21154c 3171
wolfSSL 0:d92f9d21154c 3172 if (V[3] & 0x00000001) {
wolfSSL 0:d92f9d21154c 3173 V[3] >>= 1;
wolfSSL 0:d92f9d21154c 3174 V[3] |= ((V[2] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 0:d92f9d21154c 3175 V[2] >>= 1;
wolfSSL 0:d92f9d21154c 3176 V[2] |= ((V[1] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 0:d92f9d21154c 3177 V[1] >>= 1;
wolfSSL 0:d92f9d21154c 3178 V[1] |= ((V[0] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 0:d92f9d21154c 3179 V[0] >>= 1;
wolfSSL 0:d92f9d21154c 3180 V[0] ^= 0xE1000000;
wolfSSL 0:d92f9d21154c 3181 } else {
wolfSSL 0:d92f9d21154c 3182 V[3] >>= 1;
wolfSSL 0:d92f9d21154c 3183 V[3] |= ((V[2] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 0:d92f9d21154c 3184 V[2] >>= 1;
wolfSSL 0:d92f9d21154c 3185 V[2] |= ((V[1] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 0:d92f9d21154c 3186 V[1] >>= 1;
wolfSSL 0:d92f9d21154c 3187 V[1] |= ((V[0] & 0x00000001) ? 0x80000000 : 0);
wolfSSL 0:d92f9d21154c 3188 V[0] >>= 1;
wolfSSL 0:d92f9d21154c 3189 }
wolfSSL 0:d92f9d21154c 3190 y <<= 1;
wolfSSL 0:d92f9d21154c 3191 }
wolfSSL 0:d92f9d21154c 3192 }
wolfSSL 0:d92f9d21154c 3193 X[0] = Z[0];
wolfSSL 0:d92f9d21154c 3194 X[1] = Z[1];
wolfSSL 0:d92f9d21154c 3195 X[2] = Z[2];
wolfSSL 0:d92f9d21154c 3196 X[3] = Z[3];
wolfSSL 0:d92f9d21154c 3197 }
wolfSSL 0:d92f9d21154c 3198
wolfSSL 0:d92f9d21154c 3199
wolfSSL 0:d92f9d21154c 3200 static void GHASH(Aes* aes, const byte* a, word32 aSz,
wolfSSL 0:d92f9d21154c 3201 const byte* c, word32 cSz, byte* s, word32 sSz)
wolfSSL 0:d92f9d21154c 3202 {
wolfSSL 0:d92f9d21154c 3203 word32 x[4] = {0,0,0,0};
wolfSSL 0:d92f9d21154c 3204 word32 blocks, partial;
wolfSSL 0:d92f9d21154c 3205 word32 bigH[4];
wolfSSL 0:d92f9d21154c 3206
wolfSSL 0:d92f9d21154c 3207 XMEMCPY(bigH, aes->H, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3208 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3209 ByteReverseWords(bigH, bigH, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3210 #endif
wolfSSL 0:d92f9d21154c 3211
wolfSSL 0:d92f9d21154c 3212 /* Hash in A, the Additional Authentication Data */
wolfSSL 0:d92f9d21154c 3213 if (aSz != 0 && a != NULL) {
wolfSSL 0:d92f9d21154c 3214 word32 bigA[4];
wolfSSL 0:d92f9d21154c 3215 blocks = aSz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3216 partial = aSz % AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3217 while (blocks--) {
wolfSSL 0:d92f9d21154c 3218 XMEMCPY(bigA, a, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3219 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3220 ByteReverseWords(bigA, bigA, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3221 #endif
wolfSSL 0:d92f9d21154c 3222 x[0] ^= bigA[0];
wolfSSL 0:d92f9d21154c 3223 x[1] ^= bigA[1];
wolfSSL 0:d92f9d21154c 3224 x[2] ^= bigA[2];
wolfSSL 0:d92f9d21154c 3225 x[3] ^= bigA[3];
wolfSSL 0:d92f9d21154c 3226 GMULT(x, bigH);
wolfSSL 0:d92f9d21154c 3227 a += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3228 }
wolfSSL 0:d92f9d21154c 3229 if (partial != 0) {
wolfSSL 0:d92f9d21154c 3230 XMEMSET(bigA, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3231 XMEMCPY(bigA, a, partial);
wolfSSL 0:d92f9d21154c 3232 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3233 ByteReverseWords(bigA, bigA, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3234 #endif
wolfSSL 0:d92f9d21154c 3235 x[0] ^= bigA[0];
wolfSSL 0:d92f9d21154c 3236 x[1] ^= bigA[1];
wolfSSL 0:d92f9d21154c 3237 x[2] ^= bigA[2];
wolfSSL 0:d92f9d21154c 3238 x[3] ^= bigA[3];
wolfSSL 0:d92f9d21154c 3239 GMULT(x, bigH);
wolfSSL 0:d92f9d21154c 3240 }
wolfSSL 0:d92f9d21154c 3241 }
wolfSSL 0:d92f9d21154c 3242
wolfSSL 0:d92f9d21154c 3243 /* Hash in C, the Ciphertext */
wolfSSL 0:d92f9d21154c 3244 if (cSz != 0 && c != NULL) {
wolfSSL 0:d92f9d21154c 3245 word32 bigC[4];
wolfSSL 0:d92f9d21154c 3246 blocks = cSz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3247 partial = cSz % AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3248 while (blocks--) {
wolfSSL 0:d92f9d21154c 3249 XMEMCPY(bigC, c, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3250 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3251 ByteReverseWords(bigC, bigC, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3252 #endif
wolfSSL 0:d92f9d21154c 3253 x[0] ^= bigC[0];
wolfSSL 0:d92f9d21154c 3254 x[1] ^= bigC[1];
wolfSSL 0:d92f9d21154c 3255 x[2] ^= bigC[2];
wolfSSL 0:d92f9d21154c 3256 x[3] ^= bigC[3];
wolfSSL 0:d92f9d21154c 3257 GMULT(x, bigH);
wolfSSL 0:d92f9d21154c 3258 c += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3259 }
wolfSSL 0:d92f9d21154c 3260 if (partial != 0) {
wolfSSL 0:d92f9d21154c 3261 XMEMSET(bigC, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3262 XMEMCPY(bigC, c, partial);
wolfSSL 0:d92f9d21154c 3263 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3264 ByteReverseWords(bigC, bigC, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3265 #endif
wolfSSL 0:d92f9d21154c 3266 x[0] ^= bigC[0];
wolfSSL 0:d92f9d21154c 3267 x[1] ^= bigC[1];
wolfSSL 0:d92f9d21154c 3268 x[2] ^= bigC[2];
wolfSSL 0:d92f9d21154c 3269 x[3] ^= bigC[3];
wolfSSL 0:d92f9d21154c 3270 GMULT(x, bigH);
wolfSSL 0:d92f9d21154c 3271 }
wolfSSL 0:d92f9d21154c 3272 }
wolfSSL 0:d92f9d21154c 3273
wolfSSL 0:d92f9d21154c 3274 /* Hash in the lengths in bits of A and C */
wolfSSL 0:d92f9d21154c 3275 {
wolfSSL 0:d92f9d21154c 3276 word32 len[4];
wolfSSL 0:d92f9d21154c 3277
wolfSSL 0:d92f9d21154c 3278 /* Lengths are in bytes. Convert to bits. */
wolfSSL 0:d92f9d21154c 3279 len[0] = (aSz >> (8*sizeof(aSz) - 3));
wolfSSL 0:d92f9d21154c 3280 len[1] = aSz << 3;
wolfSSL 0:d92f9d21154c 3281 len[2] = (cSz >> (8*sizeof(cSz) - 3));
wolfSSL 0:d92f9d21154c 3282 len[3] = cSz << 3;
wolfSSL 0:d92f9d21154c 3283
wolfSSL 0:d92f9d21154c 3284 x[0] ^= len[0];
wolfSSL 0:d92f9d21154c 3285 x[1] ^= len[1];
wolfSSL 0:d92f9d21154c 3286 x[2] ^= len[2];
wolfSSL 0:d92f9d21154c 3287 x[3] ^= len[3];
wolfSSL 0:d92f9d21154c 3288 GMULT(x, bigH);
wolfSSL 0:d92f9d21154c 3289 }
wolfSSL 0:d92f9d21154c 3290 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 3291 ByteReverseWords(x, x, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3292 #endif
wolfSSL 0:d92f9d21154c 3293 XMEMCPY(s, x, sSz);
wolfSSL 0:d92f9d21154c 3294 }
wolfSSL 0:d92f9d21154c 3295
wolfSSL 0:d92f9d21154c 3296 #endif /* end GCM_WORD32 */
wolfSSL 0:d92f9d21154c 3297
wolfSSL 0:d92f9d21154c 3298
wolfSSL 0:d92f9d21154c 3299 int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
wolfSSL 0:d92f9d21154c 3300 const byte* iv, word32 ivSz,
wolfSSL 0:d92f9d21154c 3301 byte* authTag, word32 authTagSz,
wolfSSL 0:d92f9d21154c 3302 const byte* authIn, word32 authInSz)
wolfSSL 0:d92f9d21154c 3303 {
wolfSSL 0:d92f9d21154c 3304 word32 blocks = sz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3305 word32 partial = sz % AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3306 const byte* p = in;
wolfSSL 0:d92f9d21154c 3307 byte* c = out;
wolfSSL 0:d92f9d21154c 3308 byte counter[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3309 byte *ctr ;
wolfSSL 0:d92f9d21154c 3310 byte scratch[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3311
wolfSSL 0:d92f9d21154c 3312 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3313 byte* key = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 3314 #endif
wolfSSL 0:d92f9d21154c 3315
wolfSSL 0:d92f9d21154c 3316 WOLFSSL_ENTER("AesGcmEncrypt");
wolfSSL 0:d92f9d21154c 3317
wolfSSL 0:d92f9d21154c 3318 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 0:d92f9d21154c 3319 ctr = (char *)aes->iv_ce ;
wolfSSL 0:d92f9d21154c 3320 #else
wolfSSL 0:d92f9d21154c 3321 ctr = counter ;
wolfSSL 0:d92f9d21154c 3322 #endif
wolfSSL 0:d92f9d21154c 3323
wolfSSL 0:d92f9d21154c 3324 XMEMSET(ctr, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3325 XMEMCPY(ctr, iv, ivSz);
wolfSSL 0:d92f9d21154c 3326 InitGcmCounter(ctr);
wolfSSL 0:d92f9d21154c 3327
wolfSSL 0:d92f9d21154c 3328 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 0:d92f9d21154c 3329 if(blocks)
wolfSSL 0:d92f9d21154c 3330 wc_AesCrypt(aes, out, in, blocks*AES_BLOCK_SIZE,
wolfSSL 0:d92f9d21154c 3331 PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_AES_GCM );
wolfSSL 0:d92f9d21154c 3332 #endif
wolfSSL 0:d92f9d21154c 3333 while (blocks--) {
wolfSSL 0:d92f9d21154c 3334 IncrementGcmCounter(ctr);
wolfSSL 0:d92f9d21154c 3335 #ifndef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 0:d92f9d21154c 3336 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3337 cau_aes_encrypt(ctr, key, aes->rounds, scratch);
wolfSSL 0:d92f9d21154c 3338 #else
wolfSSL 0:d92f9d21154c 3339 wc_AesEncrypt(aes, ctr, scratch);
wolfSSL 0:d92f9d21154c 3340 #endif
wolfSSL 0:d92f9d21154c 3341 xorbuf(scratch, p, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3342 XMEMCPY(c, scratch, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3343 #endif
wolfSSL 0:d92f9d21154c 3344 p += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3345 c += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3346 }
wolfSSL 0:d92f9d21154c 3347
wolfSSL 0:d92f9d21154c 3348 if (partial != 0) {
wolfSSL 0:d92f9d21154c 3349 IncrementGcmCounter(ctr);
wolfSSL 0:d92f9d21154c 3350 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3351 cau_aes_encrypt(ctr, key, aes->rounds, scratch);
wolfSSL 0:d92f9d21154c 3352 #else
wolfSSL 0:d92f9d21154c 3353 wc_AesEncrypt(aes, ctr, scratch);
wolfSSL 0:d92f9d21154c 3354 #endif
wolfSSL 0:d92f9d21154c 3355 xorbuf(scratch, p, partial);
wolfSSL 0:d92f9d21154c 3356 XMEMCPY(c, scratch, partial);
wolfSSL 0:d92f9d21154c 3357
wolfSSL 0:d92f9d21154c 3358 }
wolfSSL 0:d92f9d21154c 3359
wolfSSL 0:d92f9d21154c 3360 GHASH(aes, authIn, authInSz, out, sz, authTag, authTagSz);
wolfSSL 0:d92f9d21154c 3361 InitGcmCounter(ctr);
wolfSSL 0:d92f9d21154c 3362 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3363 cau_aes_encrypt(ctr, key, aes->rounds, scratch);
wolfSSL 0:d92f9d21154c 3364 #else
wolfSSL 0:d92f9d21154c 3365 wc_AesEncrypt(aes, ctr, scratch);
wolfSSL 0:d92f9d21154c 3366 #endif
wolfSSL 0:d92f9d21154c 3367 xorbuf(authTag, scratch, authTagSz);
wolfSSL 0:d92f9d21154c 3368
wolfSSL 0:d92f9d21154c 3369 return 0;
wolfSSL 0:d92f9d21154c 3370 }
wolfSSL 0:d92f9d21154c 3371
wolfSSL 0:d92f9d21154c 3372
wolfSSL 0:d92f9d21154c 3373 int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
wolfSSL 0:d92f9d21154c 3374 const byte* iv, word32 ivSz,
wolfSSL 0:d92f9d21154c 3375 const byte* authTag, word32 authTagSz,
wolfSSL 0:d92f9d21154c 3376 const byte* authIn, word32 authInSz)
wolfSSL 0:d92f9d21154c 3377 {
wolfSSL 0:d92f9d21154c 3378 word32 blocks = sz / AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3379 word32 partial = sz % AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3380 const byte* c = in;
wolfSSL 0:d92f9d21154c 3381 byte* p = out;
wolfSSL 0:d92f9d21154c 3382 byte counter[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3383 byte *ctr ;
wolfSSL 0:d92f9d21154c 3384 byte scratch[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3385
wolfSSL 0:d92f9d21154c 3386 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3387 byte* key = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 3388 #endif
wolfSSL 0:d92f9d21154c 3389
wolfSSL 0:d92f9d21154c 3390 WOLFSSL_ENTER("AesGcmDecrypt");
wolfSSL 0:d92f9d21154c 3391
wolfSSL 0:d92f9d21154c 3392 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 0:d92f9d21154c 3393 ctr = (char *)aes->iv_ce ;
wolfSSL 0:d92f9d21154c 3394 #else
wolfSSL 0:d92f9d21154c 3395 ctr = counter ;
wolfSSL 0:d92f9d21154c 3396 #endif
wolfSSL 0:d92f9d21154c 3397
wolfSSL 0:d92f9d21154c 3398 XMEMSET(ctr, 0, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3399 XMEMCPY(ctr, iv, ivSz);
wolfSSL 0:d92f9d21154c 3400 InitGcmCounter(ctr);
wolfSSL 0:d92f9d21154c 3401
wolfSSL 0:d92f9d21154c 3402 /* Calculate the authTag again using the received auth data and the
wolfSSL 0:d92f9d21154c 3403 * cipher text. */
wolfSSL 0:d92f9d21154c 3404 {
wolfSSL 0:d92f9d21154c 3405 byte Tprime[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3406 byte EKY0[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3407
wolfSSL 0:d92f9d21154c 3408 GHASH(aes, authIn, authInSz, in, sz, Tprime, sizeof(Tprime));
wolfSSL 0:d92f9d21154c 3409 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3410 cau_aes_encrypt(ctr, key, aes->rounds, EKY0);
wolfSSL 0:d92f9d21154c 3411 #else
wolfSSL 0:d92f9d21154c 3412 wc_AesEncrypt(aes, ctr, EKY0);
wolfSSL 0:d92f9d21154c 3413 #endif
wolfSSL 0:d92f9d21154c 3414 xorbuf(Tprime, EKY0, sizeof(Tprime));
wolfSSL 0:d92f9d21154c 3415
wolfSSL 0:d92f9d21154c 3416 if (ConstantCompare(authTag, Tprime, authTagSz) != 0) {
wolfSSL 0:d92f9d21154c 3417 return AES_GCM_AUTH_E;
wolfSSL 0:d92f9d21154c 3418 }
wolfSSL 0:d92f9d21154c 3419 }
wolfSSL 0:d92f9d21154c 3420
wolfSSL 0:d92f9d21154c 3421 #ifdef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 0:d92f9d21154c 3422 if(blocks)
wolfSSL 0:d92f9d21154c 3423 wc_AesCrypt(aes, out, in, blocks*AES_BLOCK_SIZE,
wolfSSL 0:d92f9d21154c 3424 PIC32_DECRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_AES_GCM );
wolfSSL 0:d92f9d21154c 3425 #endif
wolfSSL 0:d92f9d21154c 3426
wolfSSL 0:d92f9d21154c 3427 while (blocks--) {
wolfSSL 0:d92f9d21154c 3428 IncrementGcmCounter(ctr);
wolfSSL 0:d92f9d21154c 3429 #ifndef WOLFSSL_PIC32MZ_CRYPT
wolfSSL 0:d92f9d21154c 3430 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3431 cau_aes_encrypt(ctr, key, aes->rounds, scratch);
wolfSSL 0:d92f9d21154c 3432 #else
wolfSSL 0:d92f9d21154c 3433 wc_AesEncrypt(aes, ctr, scratch);
wolfSSL 0:d92f9d21154c 3434 #endif
wolfSSL 0:d92f9d21154c 3435 xorbuf(scratch, c, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3436 XMEMCPY(p, scratch, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3437 #endif
wolfSSL 0:d92f9d21154c 3438 p += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3439 c += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3440 }
wolfSSL 0:d92f9d21154c 3441 if (partial != 0) {
wolfSSL 0:d92f9d21154c 3442 IncrementGcmCounter(ctr);
wolfSSL 0:d92f9d21154c 3443 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3444 cau_aes_encrypt(ctr, key, aes->rounds, scratch);
wolfSSL 0:d92f9d21154c 3445 #else
wolfSSL 0:d92f9d21154c 3446 wc_AesEncrypt(aes, ctr, scratch);
wolfSSL 0:d92f9d21154c 3447 #endif
wolfSSL 0:d92f9d21154c 3448 xorbuf(scratch, c, partial);
wolfSSL 0:d92f9d21154c 3449 XMEMCPY(p, scratch, partial);
wolfSSL 0:d92f9d21154c 3450 }
wolfSSL 0:d92f9d21154c 3451 return 0;
wolfSSL 0:d92f9d21154c 3452 }
wolfSSL 0:d92f9d21154c 3453
wolfSSL 0:d92f9d21154c 3454
wolfSSL 0:d92f9d21154c 3455
wolfSSL 0:d92f9d21154c 3456 WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
wolfSSL 0:d92f9d21154c 3457 {
wolfSSL 0:d92f9d21154c 3458 return wc_AesGcmSetKey(&gmac->aes, key, len);
wolfSSL 0:d92f9d21154c 3459 }
wolfSSL 0:d92f9d21154c 3460
wolfSSL 0:d92f9d21154c 3461
wolfSSL 0:d92f9d21154c 3462 WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
wolfSSL 0:d92f9d21154c 3463 const byte* authIn, word32 authInSz,
wolfSSL 0:d92f9d21154c 3464 byte* authTag, word32 authTagSz)
wolfSSL 0:d92f9d21154c 3465 {
wolfSSL 0:d92f9d21154c 3466 return wc_AesGcmEncrypt(&gmac->aes, NULL, NULL, 0, iv, ivSz,
wolfSSL 0:d92f9d21154c 3467 authTag, authTagSz, authIn, authInSz);
wolfSSL 0:d92f9d21154c 3468 }
wolfSSL 0:d92f9d21154c 3469
wolfSSL 0:d92f9d21154c 3470 #endif /* HAVE_AESGCM */
wolfSSL 0:d92f9d21154c 3471
wolfSSL 0:d92f9d21154c 3472
wolfSSL 0:d92f9d21154c 3473 #ifdef HAVE_AESCCM
wolfSSL 0:d92f9d21154c 3474
wolfSSL 0:d92f9d21154c 3475 #ifdef STM32F2_CRYPTO
wolfSSL 0:d92f9d21154c 3476 #error "STM32F2 crypto doesn't currently support AES-CCM mode"
wolfSSL 0:d92f9d21154c 3477
wolfSSL 0:d92f9d21154c 3478 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 0:d92f9d21154c 3479 #error "Coldfire SEC doesn't currently support AES-CCM mode"
wolfSSL 0:d92f9d21154c 3480
wolfSSL 0:d92f9d21154c 3481 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 0:d92f9d21154c 3482 #error "PIC32MZ doesn't currently support AES-CCM mode"
wolfSSL 0:d92f9d21154c 3483
wolfSSL 0:d92f9d21154c 3484 #endif
wolfSSL 0:d92f9d21154c 3485
wolfSSL 0:d92f9d21154c 3486 void wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
wolfSSL 0:d92f9d21154c 3487 {
wolfSSL 0:d92f9d21154c 3488 byte nonce[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3489
wolfSSL 0:d92f9d21154c 3490 if (!((keySz == 16) || (keySz == 24) || (keySz == 32)))
wolfSSL 0:d92f9d21154c 3491 return;
wolfSSL 0:d92f9d21154c 3492
wolfSSL 0:d92f9d21154c 3493 XMEMSET(nonce, 0, sizeof(nonce));
wolfSSL 0:d92f9d21154c 3494 wc_AesSetKey(aes, key, keySz, nonce, AES_ENCRYPTION);
wolfSSL 0:d92f9d21154c 3495 }
wolfSSL 0:d92f9d21154c 3496
wolfSSL 0:d92f9d21154c 3497
wolfSSL 0:d92f9d21154c 3498 static void roll_x(Aes* aes, const byte* in, word32 inSz, byte* out)
wolfSSL 0:d92f9d21154c 3499 {
wolfSSL 0:d92f9d21154c 3500 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3501 byte* key = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 3502 #endif
wolfSSL 0:d92f9d21154c 3503
wolfSSL 0:d92f9d21154c 3504 /* process the bulk of the data */
wolfSSL 0:d92f9d21154c 3505 while (inSz >= AES_BLOCK_SIZE) {
wolfSSL 0:d92f9d21154c 3506 xorbuf(out, in, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3507 in += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3508 inSz -= AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3509
wolfSSL 0:d92f9d21154c 3510 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3511 cau_aes_encrypt(out, key, aes->rounds, out);
wolfSSL 0:d92f9d21154c 3512 #else
wolfSSL 0:d92f9d21154c 3513 wc_AesEncrypt(aes, out, out);
wolfSSL 0:d92f9d21154c 3514 #endif
wolfSSL 0:d92f9d21154c 3515 }
wolfSSL 0:d92f9d21154c 3516
wolfSSL 0:d92f9d21154c 3517 /* process remainder of the data */
wolfSSL 0:d92f9d21154c 3518 if (inSz > 0) {
wolfSSL 0:d92f9d21154c 3519 xorbuf(out, in, inSz);
wolfSSL 0:d92f9d21154c 3520 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3521 cau_aes_encrypt(out, key, aes->rounds, out);
wolfSSL 0:d92f9d21154c 3522 #else
wolfSSL 0:d92f9d21154c 3523 wc_AesEncrypt(aes, out, out);
wolfSSL 0:d92f9d21154c 3524 #endif
wolfSSL 0:d92f9d21154c 3525 }
wolfSSL 0:d92f9d21154c 3526 }
wolfSSL 0:d92f9d21154c 3527
wolfSSL 0:d92f9d21154c 3528
wolfSSL 0:d92f9d21154c 3529 static void roll_auth(Aes* aes, const byte* in, word32 inSz, byte* out)
wolfSSL 0:d92f9d21154c 3530 {
wolfSSL 0:d92f9d21154c 3531 word32 authLenSz;
wolfSSL 0:d92f9d21154c 3532 word32 remainder;
wolfSSL 0:d92f9d21154c 3533
wolfSSL 0:d92f9d21154c 3534 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3535 byte* key = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 3536 #endif
wolfSSL 0:d92f9d21154c 3537
wolfSSL 0:d92f9d21154c 3538 /* encode the length in */
wolfSSL 0:d92f9d21154c 3539 if (inSz <= 0xFEFF) {
wolfSSL 0:d92f9d21154c 3540 authLenSz = 2;
wolfSSL 0:d92f9d21154c 3541 out[0] ^= ((inSz & 0xFF00) >> 8);
wolfSSL 0:d92f9d21154c 3542 out[1] ^= (inSz & 0x00FF);
wolfSSL 0:d92f9d21154c 3543 }
wolfSSL 0:d92f9d21154c 3544 else if (inSz <= 0xFFFFFFFF) {
wolfSSL 0:d92f9d21154c 3545 authLenSz = 6;
wolfSSL 0:d92f9d21154c 3546 out[0] ^= 0xFF; out[1] ^= 0xFE;
wolfSSL 0:d92f9d21154c 3547 out[2] ^= ((inSz & 0xFF000000) >> 24);
wolfSSL 0:d92f9d21154c 3548 out[3] ^= ((inSz & 0x00FF0000) >> 16);
wolfSSL 0:d92f9d21154c 3549 out[4] ^= ((inSz & 0x0000FF00) >> 8);
wolfSSL 0:d92f9d21154c 3550 out[5] ^= (inSz & 0x000000FF);
wolfSSL 0:d92f9d21154c 3551 }
wolfSSL 0:d92f9d21154c 3552 /* Note, the protocol handles auth data up to 2^64, but we are
wolfSSL 0:d92f9d21154c 3553 * using 32-bit sizes right now, so the bigger data isn't handled
wolfSSL 0:d92f9d21154c 3554 * else if (inSz <= 0xFFFFFFFFFFFFFFFF) {} */
wolfSSL 0:d92f9d21154c 3555 else
wolfSSL 0:d92f9d21154c 3556 return;
wolfSSL 0:d92f9d21154c 3557
wolfSSL 0:d92f9d21154c 3558 /* start fill out the rest of the first block */
wolfSSL 0:d92f9d21154c 3559 remainder = AES_BLOCK_SIZE - authLenSz;
wolfSSL 0:d92f9d21154c 3560 if (inSz >= remainder) {
wolfSSL 0:d92f9d21154c 3561 /* plenty of bulk data to fill the remainder of this block */
wolfSSL 0:d92f9d21154c 3562 xorbuf(out + authLenSz, in, remainder);
wolfSSL 0:d92f9d21154c 3563 inSz -= remainder;
wolfSSL 0:d92f9d21154c 3564 in += remainder;
wolfSSL 0:d92f9d21154c 3565 }
wolfSSL 0:d92f9d21154c 3566 else {
wolfSSL 0:d92f9d21154c 3567 /* not enough bulk data, copy what is available, and pad zero */
wolfSSL 0:d92f9d21154c 3568 xorbuf(out + authLenSz, in, inSz);
wolfSSL 0:d92f9d21154c 3569 inSz = 0;
wolfSSL 0:d92f9d21154c 3570 }
wolfSSL 0:d92f9d21154c 3571 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3572 cau_aes_encrypt(out, key, aes->rounds, out);
wolfSSL 0:d92f9d21154c 3573 #else
wolfSSL 0:d92f9d21154c 3574 wc_AesEncrypt(aes, out, out);
wolfSSL 0:d92f9d21154c 3575 #endif
wolfSSL 0:d92f9d21154c 3576
wolfSSL 0:d92f9d21154c 3577 if (inSz > 0)
wolfSSL 0:d92f9d21154c 3578 roll_x(aes, in, inSz, out);
wolfSSL 0:d92f9d21154c 3579 }
wolfSSL 0:d92f9d21154c 3580
wolfSSL 0:d92f9d21154c 3581
wolfSSL 0:d92f9d21154c 3582 static INLINE void AesCcmCtrInc(byte* B, word32 lenSz)
wolfSSL 0:d92f9d21154c 3583 {
wolfSSL 0:d92f9d21154c 3584 word32 i;
wolfSSL 0:d92f9d21154c 3585
wolfSSL 0:d92f9d21154c 3586 for (i = 0; i < lenSz; i++) {
wolfSSL 0:d92f9d21154c 3587 if (++B[AES_BLOCK_SIZE - 1 - i] != 0) return;
wolfSSL 0:d92f9d21154c 3588 }
wolfSSL 0:d92f9d21154c 3589 }
wolfSSL 0:d92f9d21154c 3590
wolfSSL 0:d92f9d21154c 3591
wolfSSL 0:d92f9d21154c 3592 void wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
wolfSSL 0:d92f9d21154c 3593 const byte* nonce, word32 nonceSz,
wolfSSL 0:d92f9d21154c 3594 byte* authTag, word32 authTagSz,
wolfSSL 0:d92f9d21154c 3595 const byte* authIn, word32 authInSz)
wolfSSL 0:d92f9d21154c 3596 {
wolfSSL 0:d92f9d21154c 3597 byte A[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3598 byte B[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3599 byte lenSz;
wolfSSL 0:d92f9d21154c 3600 word32 i;
wolfSSL 0:d92f9d21154c 3601
wolfSSL 0:d92f9d21154c 3602 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3603 byte* key = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 3604 #endif
wolfSSL 0:d92f9d21154c 3605
wolfSSL 0:d92f9d21154c 3606 XMEMCPY(B+1, nonce, nonceSz);
wolfSSL 0:d92f9d21154c 3607 lenSz = AES_BLOCK_SIZE - 1 - (byte)nonceSz;
wolfSSL 0:d92f9d21154c 3608 B[0] = (authInSz > 0 ? 64 : 0)
wolfSSL 0:d92f9d21154c 3609 + (8 * (((byte)authTagSz - 2) / 2))
wolfSSL 0:d92f9d21154c 3610 + (lenSz - 1);
wolfSSL 0:d92f9d21154c 3611 for (i = 0; i < lenSz; i++)
wolfSSL 0:d92f9d21154c 3612 B[AES_BLOCK_SIZE - 1 - i] = (inSz >> (8 * i)) & 0xFF;
wolfSSL 0:d92f9d21154c 3613
wolfSSL 0:d92f9d21154c 3614 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3615 cau_aes_encrypt(B, key, aes->rounds, A);
wolfSSL 0:d92f9d21154c 3616 #else
wolfSSL 0:d92f9d21154c 3617 wc_AesEncrypt(aes, B, A);
wolfSSL 0:d92f9d21154c 3618 #endif
wolfSSL 0:d92f9d21154c 3619 if (authInSz > 0)
wolfSSL 0:d92f9d21154c 3620 roll_auth(aes, authIn, authInSz, A);
wolfSSL 0:d92f9d21154c 3621 if (inSz > 0)
wolfSSL 0:d92f9d21154c 3622 roll_x(aes, in, inSz, A);
wolfSSL 0:d92f9d21154c 3623 XMEMCPY(authTag, A, authTagSz);
wolfSSL 0:d92f9d21154c 3624
wolfSSL 0:d92f9d21154c 3625 B[0] = lenSz - 1;
wolfSSL 0:d92f9d21154c 3626 for (i = 0; i < lenSz; i++)
wolfSSL 0:d92f9d21154c 3627 B[AES_BLOCK_SIZE - 1 - i] = 0;
wolfSSL 0:d92f9d21154c 3628 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3629 cau_aes_encrypt(B, key, aes->rounds, A);
wolfSSL 0:d92f9d21154c 3630 #else
wolfSSL 0:d92f9d21154c 3631 wc_AesEncrypt(aes, B, A);
wolfSSL 0:d92f9d21154c 3632 #endif
wolfSSL 0:d92f9d21154c 3633 xorbuf(authTag, A, authTagSz);
wolfSSL 0:d92f9d21154c 3634
wolfSSL 0:d92f9d21154c 3635 B[15] = 1;
wolfSSL 0:d92f9d21154c 3636 while (inSz >= AES_BLOCK_SIZE) {
wolfSSL 0:d92f9d21154c 3637 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3638 cau_aes_encrypt(B, key, aes->rounds, A);
wolfSSL 0:d92f9d21154c 3639 #else
wolfSSL 0:d92f9d21154c 3640 wc_AesEncrypt(aes, B, A);
wolfSSL 0:d92f9d21154c 3641 #endif
wolfSSL 0:d92f9d21154c 3642 xorbuf(A, in, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3643 XMEMCPY(out, A, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3644
wolfSSL 0:d92f9d21154c 3645 AesCcmCtrInc(B, lenSz);
wolfSSL 0:d92f9d21154c 3646 inSz -= AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3647 in += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3648 out += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3649 }
wolfSSL 0:d92f9d21154c 3650 if (inSz > 0) {
wolfSSL 0:d92f9d21154c 3651 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3652 cau_aes_encrypt(B, key, aes->rounds, A);
wolfSSL 0:d92f9d21154c 3653 #else
wolfSSL 0:d92f9d21154c 3654 wc_AesEncrypt(aes, B, A);
wolfSSL 0:d92f9d21154c 3655 #endif
wolfSSL 0:d92f9d21154c 3656 xorbuf(A, in, inSz);
wolfSSL 0:d92f9d21154c 3657 XMEMCPY(out, A, inSz);
wolfSSL 0:d92f9d21154c 3658 }
wolfSSL 0:d92f9d21154c 3659
wolfSSL 0:d92f9d21154c 3660 ForceZero(A, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3661 ForceZero(B, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3662 }
wolfSSL 0:d92f9d21154c 3663
wolfSSL 0:d92f9d21154c 3664
wolfSSL 0:d92f9d21154c 3665 int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
wolfSSL 0:d92f9d21154c 3666 const byte* nonce, word32 nonceSz,
wolfSSL 0:d92f9d21154c 3667 const byte* authTag, word32 authTagSz,
wolfSSL 0:d92f9d21154c 3668 const byte* authIn, word32 authInSz)
wolfSSL 0:d92f9d21154c 3669 {
wolfSSL 0:d92f9d21154c 3670 byte A[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3671 byte B[AES_BLOCK_SIZE];
wolfSSL 0:d92f9d21154c 3672 byte* o;
wolfSSL 0:d92f9d21154c 3673 byte lenSz;
wolfSSL 0:d92f9d21154c 3674 word32 i, oSz;
wolfSSL 0:d92f9d21154c 3675 int result = 0;
wolfSSL 0:d92f9d21154c 3676
wolfSSL 0:d92f9d21154c 3677 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3678 byte* key = (byte*)aes->key;
wolfSSL 0:d92f9d21154c 3679 #endif
wolfSSL 0:d92f9d21154c 3680
wolfSSL 0:d92f9d21154c 3681 o = out;
wolfSSL 0:d92f9d21154c 3682 oSz = inSz;
wolfSSL 0:d92f9d21154c 3683 XMEMCPY(B+1, nonce, nonceSz);
wolfSSL 0:d92f9d21154c 3684 lenSz = AES_BLOCK_SIZE - 1 - (byte)nonceSz;
wolfSSL 0:d92f9d21154c 3685
wolfSSL 0:d92f9d21154c 3686 B[0] = lenSz - 1;
wolfSSL 0:d92f9d21154c 3687 for (i = 0; i < lenSz; i++)
wolfSSL 0:d92f9d21154c 3688 B[AES_BLOCK_SIZE - 1 - i] = 0;
wolfSSL 0:d92f9d21154c 3689 B[15] = 1;
wolfSSL 0:d92f9d21154c 3690
wolfSSL 0:d92f9d21154c 3691 while (oSz >= AES_BLOCK_SIZE) {
wolfSSL 0:d92f9d21154c 3692 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3693 cau_aes_encrypt(B, key, aes->rounds, A);
wolfSSL 0:d92f9d21154c 3694 #else
wolfSSL 0:d92f9d21154c 3695 wc_AesEncrypt(aes, B, A);
wolfSSL 0:d92f9d21154c 3696 #endif
wolfSSL 0:d92f9d21154c 3697 xorbuf(A, in, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3698 XMEMCPY(o, A, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3699
wolfSSL 0:d92f9d21154c 3700 AesCcmCtrInc(B, lenSz);
wolfSSL 0:d92f9d21154c 3701 oSz -= AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3702 in += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3703 o += AES_BLOCK_SIZE;
wolfSSL 0:d92f9d21154c 3704 }
wolfSSL 0:d92f9d21154c 3705 if (inSz > 0) {
wolfSSL 0:d92f9d21154c 3706 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3707 cau_aes_encrypt(B, key, aes->rounds, A);
wolfSSL 0:d92f9d21154c 3708 #else
wolfSSL 0:d92f9d21154c 3709 wc_AesEncrypt(aes, B, A);
wolfSSL 0:d92f9d21154c 3710 #endif
wolfSSL 0:d92f9d21154c 3711 xorbuf(A, in, oSz);
wolfSSL 0:d92f9d21154c 3712 XMEMCPY(o, A, oSz);
wolfSSL 0:d92f9d21154c 3713 }
wolfSSL 0:d92f9d21154c 3714
wolfSSL 0:d92f9d21154c 3715 for (i = 0; i < lenSz; i++)
wolfSSL 0:d92f9d21154c 3716 B[AES_BLOCK_SIZE - 1 - i] = 0;
wolfSSL 0:d92f9d21154c 3717 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3718 cau_aes_encrypt(B, key, aes->rounds, A);
wolfSSL 0:d92f9d21154c 3719 #else
wolfSSL 0:d92f9d21154c 3720 wc_AesEncrypt(aes, B, A);
wolfSSL 0:d92f9d21154c 3721 #endif
wolfSSL 0:d92f9d21154c 3722
wolfSSL 0:d92f9d21154c 3723 o = out;
wolfSSL 0:d92f9d21154c 3724 oSz = inSz;
wolfSSL 0:d92f9d21154c 3725
wolfSSL 0:d92f9d21154c 3726 B[0] = (authInSz > 0 ? 64 : 0)
wolfSSL 0:d92f9d21154c 3727 + (8 * (((byte)authTagSz - 2) / 2))
wolfSSL 0:d92f9d21154c 3728 + (lenSz - 1);
wolfSSL 0:d92f9d21154c 3729 for (i = 0; i < lenSz; i++)
wolfSSL 0:d92f9d21154c 3730 B[AES_BLOCK_SIZE - 1 - i] = (inSz >> (8 * i)) & 0xFF;
wolfSSL 0:d92f9d21154c 3731
wolfSSL 0:d92f9d21154c 3732 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3733 cau_aes_encrypt(B, key, aes->rounds, A);
wolfSSL 0:d92f9d21154c 3734 #else
wolfSSL 0:d92f9d21154c 3735 wc_AesEncrypt(aes, B, A);
wolfSSL 0:d92f9d21154c 3736 #endif
wolfSSL 0:d92f9d21154c 3737 if (authInSz > 0)
wolfSSL 0:d92f9d21154c 3738 roll_auth(aes, authIn, authInSz, A);
wolfSSL 0:d92f9d21154c 3739 if (inSz > 0)
wolfSSL 0:d92f9d21154c 3740 roll_x(aes, o, oSz, A);
wolfSSL 0:d92f9d21154c 3741
wolfSSL 0:d92f9d21154c 3742 B[0] = lenSz - 1;
wolfSSL 0:d92f9d21154c 3743 for (i = 0; i < lenSz; i++)
wolfSSL 0:d92f9d21154c 3744 B[AES_BLOCK_SIZE - 1 - i] = 0;
wolfSSL 0:d92f9d21154c 3745 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 3746 cau_aes_encrypt(B, key, aes->rounds, B);
wolfSSL 0:d92f9d21154c 3747 #else
wolfSSL 0:d92f9d21154c 3748 wc_AesEncrypt(aes, B, B);
wolfSSL 0:d92f9d21154c 3749 #endif
wolfSSL 0:d92f9d21154c 3750 xorbuf(A, B, authTagSz);
wolfSSL 0:d92f9d21154c 3751
wolfSSL 0:d92f9d21154c 3752 if (ConstantCompare(A, authTag, authTagSz) != 0) {
wolfSSL 0:d92f9d21154c 3753 /* If the authTag check fails, don't keep the decrypted data.
wolfSSL 0:d92f9d21154c 3754 * Unfortunately, you need the decrypted data to calculate the
wolfSSL 0:d92f9d21154c 3755 * check value. */
wolfSSL 0:d92f9d21154c 3756 XMEMSET(out, 0, inSz);
wolfSSL 0:d92f9d21154c 3757 result = AES_CCM_AUTH_E;
wolfSSL 0:d92f9d21154c 3758 }
wolfSSL 0:d92f9d21154c 3759
wolfSSL 0:d92f9d21154c 3760 ForceZero(A, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3761 ForceZero(B, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3762 o = NULL;
wolfSSL 0:d92f9d21154c 3763
wolfSSL 0:d92f9d21154c 3764 return result;
wolfSSL 0:d92f9d21154c 3765 }
wolfSSL 0:d92f9d21154c 3766
wolfSSL 0:d92f9d21154c 3767 #endif /* HAVE_AESCCM */
wolfSSL 0:d92f9d21154c 3768
wolfSSL 0:d92f9d21154c 3769
wolfSSL 0:d92f9d21154c 3770 #ifdef HAVE_CAVIUM
wolfSSL 0:d92f9d21154c 3771
wolfSSL 0:d92f9d21154c 3772 #include <wolfssl/ctaocrypt/logging.h>
wolfSSL 0:d92f9d21154c 3773 #include "cavium_common.h"
wolfSSL 0:d92f9d21154c 3774
wolfSSL 0:d92f9d21154c 3775 /* Initiliaze Aes for use with Nitrox device */
wolfSSL 0:d92f9d21154c 3776 int wc_AesInitCavium(Aes* aes, int devId)
wolfSSL 0:d92f9d21154c 3777 {
wolfSSL 0:d92f9d21154c 3778 if (aes == NULL)
wolfSSL 0:d92f9d21154c 3779 return -1;
wolfSSL 0:d92f9d21154c 3780
wolfSSL 0:d92f9d21154c 3781 if (CspAllocContext(CONTEXT_SSL, &aes->contextHandle, devId) != 0)
wolfSSL 0:d92f9d21154c 3782 return -1;
wolfSSL 0:d92f9d21154c 3783
wolfSSL 0:d92f9d21154c 3784 aes->devId = devId;
wolfSSL 0:d92f9d21154c 3785 aes->magic = WOLFSSL_AES_CAVIUM_MAGIC;
wolfSSL 0:d92f9d21154c 3786
wolfSSL 0:d92f9d21154c 3787 return 0;
wolfSSL 0:d92f9d21154c 3788 }
wolfSSL 0:d92f9d21154c 3789
wolfSSL 0:d92f9d21154c 3790
wolfSSL 0:d92f9d21154c 3791 /* Free Aes from use with Nitrox device */
wolfSSL 0:d92f9d21154c 3792 void wc_AesFreeCavium(Aes* aes)
wolfSSL 0:d92f9d21154c 3793 {
wolfSSL 0:d92f9d21154c 3794 if (aes == NULL)
wolfSSL 0:d92f9d21154c 3795 return;
wolfSSL 0:d92f9d21154c 3796
wolfSSL 0:d92f9d21154c 3797 if (aes->magic != WOLFSSL_AES_CAVIUM_MAGIC)
wolfSSL 0:d92f9d21154c 3798 return;
wolfSSL 0:d92f9d21154c 3799
wolfSSL 0:d92f9d21154c 3800 CspFreeContext(CONTEXT_SSL, aes->contextHandle, aes->devId);
wolfSSL 0:d92f9d21154c 3801 aes->magic = 0;
wolfSSL 0:d92f9d21154c 3802 }
wolfSSL 0:d92f9d21154c 3803
wolfSSL 0:d92f9d21154c 3804
wolfSSL 0:d92f9d21154c 3805 static int wc_AesCaviumSetKey(Aes* aes, const byte* key, word32 length,
wolfSSL 0:d92f9d21154c 3806 const byte* iv)
wolfSSL 0:d92f9d21154c 3807 {
wolfSSL 0:d92f9d21154c 3808 if (aes == NULL)
wolfSSL 0:d92f9d21154c 3809 return -1;
wolfSSL 0:d92f9d21154c 3810
wolfSSL 0:d92f9d21154c 3811 XMEMCPY(aes->key, key, length); /* key still holds key, iv still in reg */
wolfSSL 0:d92f9d21154c 3812 if (length == 16)
wolfSSL 0:d92f9d21154c 3813 aes->type = AES_128;
wolfSSL 0:d92f9d21154c 3814 else if (length == 24)
wolfSSL 0:d92f9d21154c 3815 aes->type = AES_192;
wolfSSL 0:d92f9d21154c 3816 else if (length == 32)
wolfSSL 0:d92f9d21154c 3817 aes->type = AES_256;
wolfSSL 0:d92f9d21154c 3818
wolfSSL 0:d92f9d21154c 3819 return wc_AesSetIV(aes, iv);
wolfSSL 0:d92f9d21154c 3820 }
wolfSSL 0:d92f9d21154c 3821
wolfSSL 0:d92f9d21154c 3822
wolfSSL 0:d92f9d21154c 3823 static int AesCaviumCbcEncrypt(Aes* aes, byte* out, const byte* in,
wolfSSL 0:d92f9d21154c 3824 word32 length)
wolfSSL 0:d92f9d21154c 3825 {
wolfSSL 0:d92f9d21154c 3826 wolfssl_word offset = 0;
wolfSSL 0:d92f9d21154c 3827 word32 requestId;
wolfSSL 0:d92f9d21154c 3828
wolfSSL 0:d92f9d21154c 3829 while (length > WOLFSSL_MAX_16BIT) {
wolfSSL 0:d92f9d21154c 3830 word16 slen = (word16)WOLFSSL_MAX_16BIT;
wolfSSL 0:d92f9d21154c 3831 if (CspEncryptAes(CAVIUM_BLOCKING, aes->contextHandle, CAVIUM_NO_UPDATE,
wolfSSL 0:d92f9d21154c 3832 aes->type, slen, (byte*)in + offset, out + offset,
wolfSSL 0:d92f9d21154c 3833 (byte*)aes->reg, (byte*)aes->key, &requestId,
wolfSSL 0:d92f9d21154c 3834 aes->devId) != 0) {
wolfSSL 0:d92f9d21154c 3835 WOLFSSL_MSG("Bad Cavium Aes Encrypt");
wolfSSL 0:d92f9d21154c 3836 return -1;
wolfSSL 0:d92f9d21154c 3837 }
wolfSSL 0:d92f9d21154c 3838 length -= WOLFSSL_MAX_16BIT;
wolfSSL 0:d92f9d21154c 3839 offset += WOLFSSL_MAX_16BIT;
wolfSSL 0:d92f9d21154c 3840 XMEMCPY(aes->reg, out + offset - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3841 }
wolfSSL 0:d92f9d21154c 3842 if (length) {
wolfSSL 0:d92f9d21154c 3843 word16 slen = (word16)length;
wolfSSL 0:d92f9d21154c 3844 if (CspEncryptAes(CAVIUM_BLOCKING, aes->contextHandle, CAVIUM_NO_UPDATE,
wolfSSL 0:d92f9d21154c 3845 aes->type, slen, (byte*)in + offset, out + offset,
wolfSSL 0:d92f9d21154c 3846 (byte*)aes->reg, (byte*)aes->key, &requestId,
wolfSSL 0:d92f9d21154c 3847 aes->devId) != 0) {
wolfSSL 0:d92f9d21154c 3848 WOLFSSL_MSG("Bad Cavium Aes Encrypt");
wolfSSL 0:d92f9d21154c 3849 return -1;
wolfSSL 0:d92f9d21154c 3850 }
wolfSSL 0:d92f9d21154c 3851 XMEMCPY(aes->reg, out + offset+length - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3852 }
wolfSSL 0:d92f9d21154c 3853 return 0;
wolfSSL 0:d92f9d21154c 3854 }
wolfSSL 0:d92f9d21154c 3855
wolfSSL 0:d92f9d21154c 3856 static int AesCaviumCbcDecrypt(Aes* aes, byte* out, const byte* in,
wolfSSL 0:d92f9d21154c 3857 word32 length)
wolfSSL 0:d92f9d21154c 3858 {
wolfSSL 0:d92f9d21154c 3859 word32 requestId;
wolfSSL 0:d92f9d21154c 3860 wolfssl_word offset = 0;
wolfSSL 0:d92f9d21154c 3861
wolfSSL 0:d92f9d21154c 3862 while (length > WOLFSSL_MAX_16BIT) {
wolfSSL 0:d92f9d21154c 3863 word16 slen = (word16)WOLFSSL_MAX_16BIT;
wolfSSL 0:d92f9d21154c 3864 XMEMCPY(aes->tmp, in + offset + slen - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3865 if (CspDecryptAes(CAVIUM_BLOCKING, aes->contextHandle, CAVIUM_NO_UPDATE,
wolfSSL 0:d92f9d21154c 3866 aes->type, slen, (byte*)in + offset, out + offset,
wolfSSL 0:d92f9d21154c 3867 (byte*)aes->reg, (byte*)aes->key, &requestId,
wolfSSL 0:d92f9d21154c 3868 aes->devId) != 0) {
wolfSSL 0:d92f9d21154c 3869 WOLFSSL_MSG("Bad Cavium Aes Decrypt");
wolfSSL 0:d92f9d21154c 3870 return -1;
wolfSSL 0:d92f9d21154c 3871 }
wolfSSL 0:d92f9d21154c 3872 length -= WOLFSSL_MAX_16BIT;
wolfSSL 0:d92f9d21154c 3873 offset += WOLFSSL_MAX_16BIT;
wolfSSL 0:d92f9d21154c 3874 XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3875 }
wolfSSL 0:d92f9d21154c 3876 if (length) {
wolfSSL 0:d92f9d21154c 3877 word16 slen = (word16)length;
wolfSSL 0:d92f9d21154c 3878 XMEMCPY(aes->tmp, in + offset + slen - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3879 if (CspDecryptAes(CAVIUM_BLOCKING, aes->contextHandle, CAVIUM_NO_UPDATE,
wolfSSL 0:d92f9d21154c 3880 aes->type, slen, (byte*)in + offset, out + offset,
wolfSSL 0:d92f9d21154c 3881 (byte*)aes->reg, (byte*)aes->key, &requestId,
wolfSSL 0:d92f9d21154c 3882 aes->devId) != 0) {
wolfSSL 0:d92f9d21154c 3883 WOLFSSL_MSG("Bad Cavium Aes Decrypt");
wolfSSL 0:d92f9d21154c 3884 return -1;
wolfSSL 0:d92f9d21154c 3885 }
wolfSSL 0:d92f9d21154c 3886 XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 3887 }
wolfSSL 0:d92f9d21154c 3888 return 0;
wolfSSL 0:d92f9d21154c 3889 }
wolfSSL 0:d92f9d21154c 3890
wolfSSL 0:d92f9d21154c 3891 #endif /* HAVE_CAVIUM */
wolfSSL 0:d92f9d21154c 3892
wolfSSL 0:d92f9d21154c 3893 #endif /* WOLFSSL_TI_CRYPT */
wolfSSL 0:d92f9d21154c 3894
wolfSSL 0:d92f9d21154c 3895 #endif /* HAVE_FIPS */
wolfSSL 0:d92f9d21154c 3896
wolfSSL 0:d92f9d21154c 3897 #endif /* NO_AES */
wolfSSL 0:d92f9d21154c 3898