Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
wolfSSL
Date:
Sat Jul 12 07:18:23 2014 +0000
Revision:
0:1239e9b70ca2
CyaSSL 3.0.0;

Who changed what in which revision?

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