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 /* des3.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_DES3
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/des3.h>
wolfSSL 0:1239e9b70ca2 36 #include <cyassl/ctaocrypt/error-crypt.h>
wolfSSL 0:1239e9b70ca2 37
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
wolfSSL 0:1239e9b70ca2 44
wolfSSL 0:1239e9b70ca2 45 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 46 static int Des3_CaviumSetKey(Des3* des3, const byte* key, const byte* iv);
wolfSSL 0:1239e9b70ca2 47 static int Des3_CaviumCbcEncrypt(Des3* des3, byte* out, const byte* in,
wolfSSL 0:1239e9b70ca2 48 word32 length);
wolfSSL 0:1239e9b70ca2 49 static int Des3_CaviumCbcDecrypt(Des3* des3, byte* out, const byte* in,
wolfSSL 0:1239e9b70ca2 50 word32 length);
wolfSSL 0:1239e9b70ca2 51 #endif
wolfSSL 0:1239e9b70ca2 52
wolfSSL 0:1239e9b70ca2 53
wolfSSL 0:1239e9b70ca2 54
wolfSSL 0:1239e9b70ca2 55
wolfSSL 0:1239e9b70ca2 56 #ifdef STM32F2_CRYPTO
wolfSSL 0:1239e9b70ca2 57 /*
wolfSSL 0:1239e9b70ca2 58 * STM32F2 hardware DES/3DES support through the STM32F2 standard
wolfSSL 0:1239e9b70ca2 59 * peripheral library. Documentation located in STM32F2xx Standard
wolfSSL 0:1239e9b70ca2 60 * Peripheral Library document (See note in README).
wolfSSL 0:1239e9b70ca2 61 */
wolfSSL 0:1239e9b70ca2 62 #include "stm32f2xx.h"
wolfSSL 0:1239e9b70ca2 63 #include "stm32f2xx_cryp.h"
wolfSSL 0:1239e9b70ca2 64
wolfSSL 0:1239e9b70ca2 65 int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 0:1239e9b70ca2 66 {
wolfSSL 0:1239e9b70ca2 67 word32 *dkey = des->key;
wolfSSL 0:1239e9b70ca2 68
wolfSSL 0:1239e9b70ca2 69 XMEMCPY(dkey, key, 8);
wolfSSL 0:1239e9b70ca2 70 ByteReverseWords(dkey, dkey, 8);
wolfSSL 0:1239e9b70ca2 71
wolfSSL 0:1239e9b70ca2 72 Des_SetIV(des, iv);
wolfSSL 0:1239e9b70ca2 73
wolfSSL 0:1239e9b70ca2 74 return 0;
wolfSSL 0:1239e9b70ca2 75 }
wolfSSL 0:1239e9b70ca2 76
wolfSSL 0:1239e9b70ca2 77 int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
wolfSSL 0:1239e9b70ca2 78 {
wolfSSL 0:1239e9b70ca2 79 word32 *dkey1 = des->key[0];
wolfSSL 0:1239e9b70ca2 80 word32 *dkey2 = des->key[1];
wolfSSL 0:1239e9b70ca2 81 word32 *dkey3 = des->key[2];
wolfSSL 0:1239e9b70ca2 82
wolfSSL 0:1239e9b70ca2 83 XMEMCPY(dkey1, key, 8); /* set key 1 */
wolfSSL 0:1239e9b70ca2 84 XMEMCPY(dkey2, key + 8, 8); /* set key 2 */
wolfSSL 0:1239e9b70ca2 85 XMEMCPY(dkey3, key + 16, 8); /* set key 3 */
wolfSSL 0:1239e9b70ca2 86
wolfSSL 0:1239e9b70ca2 87 ByteReverseWords(dkey1, dkey1, 8);
wolfSSL 0:1239e9b70ca2 88 ByteReverseWords(dkey2, dkey2, 8);
wolfSSL 0:1239e9b70ca2 89 ByteReverseWords(dkey3, dkey3, 8);
wolfSSL 0:1239e9b70ca2 90
wolfSSL 0:1239e9b70ca2 91 return Des3_SetIV(des, iv);
wolfSSL 0:1239e9b70ca2 92 }
wolfSSL 0:1239e9b70ca2 93
wolfSSL 0:1239e9b70ca2 94 void DesCrypt(Des* des, byte* out, const byte* in, word32 sz,
wolfSSL 0:1239e9b70ca2 95 int dir, int mode)
wolfSSL 0:1239e9b70ca2 96 {
wolfSSL 0:1239e9b70ca2 97 word32 *dkey, *iv;
wolfSSL 0:1239e9b70ca2 98 CRYP_InitTypeDef DES_CRYP_InitStructure;
wolfSSL 0:1239e9b70ca2 99 CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure;
wolfSSL 0:1239e9b70ca2 100 CRYP_IVInitTypeDef DES_CRYP_IVInitStructure;
wolfSSL 0:1239e9b70ca2 101
wolfSSL 0:1239e9b70ca2 102 dkey = des->key;
wolfSSL 0:1239e9b70ca2 103 iv = des->reg;
wolfSSL 0:1239e9b70ca2 104
wolfSSL 0:1239e9b70ca2 105 /* crypto structure initialization */
wolfSSL 0:1239e9b70ca2 106 CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
wolfSSL 0:1239e9b70ca2 107 CRYP_StructInit(&DES_CRYP_InitStructure);
wolfSSL 0:1239e9b70ca2 108 CRYP_IVStructInit(&DES_CRYP_IVInitStructure);
wolfSSL 0:1239e9b70ca2 109
wolfSSL 0:1239e9b70ca2 110 /* reset registers to their default values */
wolfSSL 0:1239e9b70ca2 111 CRYP_DeInit();
wolfSSL 0:1239e9b70ca2 112
wolfSSL 0:1239e9b70ca2 113 /* set direction, mode, and datatype */
wolfSSL 0:1239e9b70ca2 114 if (dir == DES_ENCRYPTION) {
wolfSSL 0:1239e9b70ca2 115 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
wolfSSL 0:1239e9b70ca2 116 } else { /* DES_DECRYPTION */
wolfSSL 0:1239e9b70ca2 117 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
wolfSSL 0:1239e9b70ca2 118 }
wolfSSL 0:1239e9b70ca2 119
wolfSSL 0:1239e9b70ca2 120 if (mode == DES_CBC) {
wolfSSL 0:1239e9b70ca2 121 DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC;
wolfSSL 0:1239e9b70ca2 122 } else { /* DES_ECB */
wolfSSL 0:1239e9b70ca2 123 DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB;
wolfSSL 0:1239e9b70ca2 124 }
wolfSSL 0:1239e9b70ca2 125
wolfSSL 0:1239e9b70ca2 126 DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 0:1239e9b70ca2 127 CRYP_Init(&DES_CRYP_InitStructure);
wolfSSL 0:1239e9b70ca2 128
wolfSSL 0:1239e9b70ca2 129 /* load key into correct registers */
wolfSSL 0:1239e9b70ca2 130 DES_CRYP_KeyInitStructure.CRYP_Key1Left = dkey[0];
wolfSSL 0:1239e9b70ca2 131 DES_CRYP_KeyInitStructure.CRYP_Key1Right = dkey[1];
wolfSSL 0:1239e9b70ca2 132 CRYP_KeyInit(&DES_CRYP_KeyInitStructure);
wolfSSL 0:1239e9b70ca2 133
wolfSSL 0:1239e9b70ca2 134 /* set iv */
wolfSSL 0:1239e9b70ca2 135 ByteReverseWords(iv, iv, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 136 DES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
wolfSSL 0:1239e9b70ca2 137 DES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
wolfSSL 0:1239e9b70ca2 138 CRYP_IVInit(&DES_CRYP_IVInitStructure);
wolfSSL 0:1239e9b70ca2 139
wolfSSL 0:1239e9b70ca2 140 /* enable crypto processor */
wolfSSL 0:1239e9b70ca2 141 CRYP_Cmd(ENABLE);
wolfSSL 0:1239e9b70ca2 142
wolfSSL 0:1239e9b70ca2 143 while (sz > 0)
wolfSSL 0:1239e9b70ca2 144 {
wolfSSL 0:1239e9b70ca2 145 /* flush IN/OUT FIFOs */
wolfSSL 0:1239e9b70ca2 146 CRYP_FIFOFlush();
wolfSSL 0:1239e9b70ca2 147
wolfSSL 0:1239e9b70ca2 148 /* if input and output same will overwrite input iv */
wolfSSL 0:1239e9b70ca2 149 XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 150
wolfSSL 0:1239e9b70ca2 151 CRYP_DataIn(*(uint32_t*)&in[0]);
wolfSSL 0:1239e9b70ca2 152 CRYP_DataIn(*(uint32_t*)&in[4]);
wolfSSL 0:1239e9b70ca2 153
wolfSSL 0:1239e9b70ca2 154 /* wait until the complete message has been processed */
wolfSSL 0:1239e9b70ca2 155 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 0:1239e9b70ca2 156
wolfSSL 0:1239e9b70ca2 157 *(uint32_t*)&out[0] = CRYP_DataOut();
wolfSSL 0:1239e9b70ca2 158 *(uint32_t*)&out[4] = CRYP_DataOut();
wolfSSL 0:1239e9b70ca2 159
wolfSSL 0:1239e9b70ca2 160 /* store iv for next call */
wolfSSL 0:1239e9b70ca2 161 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 162
wolfSSL 0:1239e9b70ca2 163 sz -= DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 164 in += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 165 out += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 166 }
wolfSSL 0:1239e9b70ca2 167
wolfSSL 0:1239e9b70ca2 168 /* disable crypto processor */
wolfSSL 0:1239e9b70ca2 169 CRYP_Cmd(DISABLE);
wolfSSL 0:1239e9b70ca2 170 }
wolfSSL 0:1239e9b70ca2 171
wolfSSL 0:1239e9b70ca2 172 void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 173 {
wolfSSL 0:1239e9b70ca2 174 DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_CBC);
wolfSSL 0:1239e9b70ca2 175 }
wolfSSL 0:1239e9b70ca2 176
wolfSSL 0:1239e9b70ca2 177 void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 178 {
wolfSSL 0:1239e9b70ca2 179 DesCrypt(des, out, in, sz, DES_DECRYPTION, DES_CBC);
wolfSSL 0:1239e9b70ca2 180 }
wolfSSL 0:1239e9b70ca2 181
wolfSSL 0:1239e9b70ca2 182 void Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 183 {
wolfSSL 0:1239e9b70ca2 184 DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_ECB);
wolfSSL 0:1239e9b70ca2 185 }
wolfSSL 0:1239e9b70ca2 186
wolfSSL 0:1239e9b70ca2 187 void Des3Crypt(Des3* des, byte* out, const byte* in, word32 sz,
wolfSSL 0:1239e9b70ca2 188 int dir)
wolfSSL 0:1239e9b70ca2 189 {
wolfSSL 0:1239e9b70ca2 190 word32 *dkey1, *dkey2, *dkey3, *iv;
wolfSSL 0:1239e9b70ca2 191 CRYP_InitTypeDef DES3_CRYP_InitStructure;
wolfSSL 0:1239e9b70ca2 192 CRYP_KeyInitTypeDef DES3_CRYP_KeyInitStructure;
wolfSSL 0:1239e9b70ca2 193 CRYP_IVInitTypeDef DES3_CRYP_IVInitStructure;
wolfSSL 0:1239e9b70ca2 194
wolfSSL 0:1239e9b70ca2 195 dkey1 = des->key[0];
wolfSSL 0:1239e9b70ca2 196 dkey2 = des->key[1];
wolfSSL 0:1239e9b70ca2 197 dkey3 = des->key[2];
wolfSSL 0:1239e9b70ca2 198 iv = des->reg;
wolfSSL 0:1239e9b70ca2 199
wolfSSL 0:1239e9b70ca2 200 /* crypto structure initialization */
wolfSSL 0:1239e9b70ca2 201 CRYP_KeyStructInit(&DES3_CRYP_KeyInitStructure);
wolfSSL 0:1239e9b70ca2 202 CRYP_StructInit(&DES3_CRYP_InitStructure);
wolfSSL 0:1239e9b70ca2 203 CRYP_IVStructInit(&DES3_CRYP_IVInitStructure);
wolfSSL 0:1239e9b70ca2 204
wolfSSL 0:1239e9b70ca2 205 /* reset registers to their default values */
wolfSSL 0:1239e9b70ca2 206 CRYP_DeInit();
wolfSSL 0:1239e9b70ca2 207
wolfSSL 0:1239e9b70ca2 208 /* set direction, mode, and datatype */
wolfSSL 0:1239e9b70ca2 209 if (dir == DES_ENCRYPTION) {
wolfSSL 0:1239e9b70ca2 210 DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
wolfSSL 0:1239e9b70ca2 211 } else {
wolfSSL 0:1239e9b70ca2 212 DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
wolfSSL 0:1239e9b70ca2 213 }
wolfSSL 0:1239e9b70ca2 214
wolfSSL 0:1239e9b70ca2 215 DES3_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
wolfSSL 0:1239e9b70ca2 216 DES3_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 0:1239e9b70ca2 217 CRYP_Init(&DES3_CRYP_InitStructure);
wolfSSL 0:1239e9b70ca2 218
wolfSSL 0:1239e9b70ca2 219 /* load key into correct registers */
wolfSSL 0:1239e9b70ca2 220 DES3_CRYP_KeyInitStructure.CRYP_Key1Left = dkey1[0];
wolfSSL 0:1239e9b70ca2 221 DES3_CRYP_KeyInitStructure.CRYP_Key1Right = dkey1[1];
wolfSSL 0:1239e9b70ca2 222 DES3_CRYP_KeyInitStructure.CRYP_Key2Left = dkey2[0];
wolfSSL 0:1239e9b70ca2 223 DES3_CRYP_KeyInitStructure.CRYP_Key2Right = dkey2[1];
wolfSSL 0:1239e9b70ca2 224 DES3_CRYP_KeyInitStructure.CRYP_Key3Left = dkey3[0];
wolfSSL 0:1239e9b70ca2 225 DES3_CRYP_KeyInitStructure.CRYP_Key3Right = dkey3[1];
wolfSSL 0:1239e9b70ca2 226 CRYP_KeyInit(&DES3_CRYP_KeyInitStructure);
wolfSSL 0:1239e9b70ca2 227
wolfSSL 0:1239e9b70ca2 228 /* set iv */
wolfSSL 0:1239e9b70ca2 229 ByteReverseWords(iv, iv, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 230 DES3_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
wolfSSL 0:1239e9b70ca2 231 DES3_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
wolfSSL 0:1239e9b70ca2 232 CRYP_IVInit(&DES3_CRYP_IVInitStructure);
wolfSSL 0:1239e9b70ca2 233
wolfSSL 0:1239e9b70ca2 234 /* enable crypto processor */
wolfSSL 0:1239e9b70ca2 235 CRYP_Cmd(ENABLE);
wolfSSL 0:1239e9b70ca2 236
wolfSSL 0:1239e9b70ca2 237 while (sz > 0)
wolfSSL 0:1239e9b70ca2 238 {
wolfSSL 0:1239e9b70ca2 239 /* flush IN/OUT FIFOs */
wolfSSL 0:1239e9b70ca2 240 CRYP_FIFOFlush();
wolfSSL 0:1239e9b70ca2 241
wolfSSL 0:1239e9b70ca2 242 CRYP_DataIn(*(uint32_t*)&in[0]);
wolfSSL 0:1239e9b70ca2 243 CRYP_DataIn(*(uint32_t*)&in[4]);
wolfSSL 0:1239e9b70ca2 244
wolfSSL 0:1239e9b70ca2 245 /* wait until the complete message has been processed */
wolfSSL 0:1239e9b70ca2 246 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 0:1239e9b70ca2 247
wolfSSL 0:1239e9b70ca2 248 *(uint32_t*)&out[0] = CRYP_DataOut();
wolfSSL 0:1239e9b70ca2 249 *(uint32_t*)&out[4] = CRYP_DataOut();
wolfSSL 0:1239e9b70ca2 250
wolfSSL 0:1239e9b70ca2 251 /* store iv for next call */
wolfSSL 0:1239e9b70ca2 252 XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 253
wolfSSL 0:1239e9b70ca2 254 sz -= DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 255 in += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 256 out += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 257 }
wolfSSL 0:1239e9b70ca2 258
wolfSSL 0:1239e9b70ca2 259 /* disable crypto processor */
wolfSSL 0:1239e9b70ca2 260 CRYP_Cmd(DISABLE);
wolfSSL 0:1239e9b70ca2 261
wolfSSL 0:1239e9b70ca2 262 }
wolfSSL 0:1239e9b70ca2 263
wolfSSL 0:1239e9b70ca2 264 int Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 265 {
wolfSSL 0:1239e9b70ca2 266 Des3Crypt(des, out, in, sz, DES_ENCRYPTION);
wolfSSL 0:1239e9b70ca2 267 return 0;
wolfSSL 0:1239e9b70ca2 268 }
wolfSSL 0:1239e9b70ca2 269
wolfSSL 0:1239e9b70ca2 270 int Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 271 {
wolfSSL 0:1239e9b70ca2 272 Des3Crypt(des, out, in, sz, DES_DECRYPTION);
wolfSSL 0:1239e9b70ca2 273 return 0;
wolfSSL 0:1239e9b70ca2 274 }
wolfSSL 0:1239e9b70ca2 275
wolfSSL 0:1239e9b70ca2 276
wolfSSL 0:1239e9b70ca2 277 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 0:1239e9b70ca2 278
wolfSSL 0:1239e9b70ca2 279 #include "sec.h"
wolfSSL 0:1239e9b70ca2 280 #include "mcf548x_sec.h"
wolfSSL 0:1239e9b70ca2 281
wolfSSL 0:1239e9b70ca2 282 #include "memory_pools.h"
wolfSSL 0:1239e9b70ca2 283 extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */
wolfSSL 0:1239e9b70ca2 284 #define DES_BUFFER_SIZE (DES_BLOCK_SIZE * 16)
wolfSSL 0:1239e9b70ca2 285 static unsigned char *DesBuffer = NULL ;
wolfSSL 0:1239e9b70ca2 286
wolfSSL 0:1239e9b70ca2 287 #define SEC_DESC_DES_CBC_ENCRYPT 0x20500010
wolfSSL 0:1239e9b70ca2 288 #define SEC_DESC_DES_CBC_DECRYPT 0x20400010
wolfSSL 0:1239e9b70ca2 289 #define SEC_DESC_DES3_CBC_ENCRYPT 0x20700010
wolfSSL 0:1239e9b70ca2 290 #define SEC_DESC_DES3_CBC_DECRYPT 0x20600010
wolfSSL 0:1239e9b70ca2 291
wolfSSL 0:1239e9b70ca2 292 extern volatile unsigned char __MBAR[];
wolfSSL 0:1239e9b70ca2 293
wolfSSL 0:1239e9b70ca2 294 static void Des_Cbc(Des* des, byte* out, const byte* in, word32 sz, word32 desc)
wolfSSL 0:1239e9b70ca2 295 {
wolfSSL 0:1239e9b70ca2 296 static volatile SECdescriptorType descriptor = { NULL } ;
wolfSSL 0:1239e9b70ca2 297 int ret ; int stat1,stat2 ;
wolfSSL 0:1239e9b70ca2 298 int i ; int size ;
wolfSSL 0:1239e9b70ca2 299 volatile int v ;
wolfSSL 0:1239e9b70ca2 300
wolfSSL 0:1239e9b70ca2 301 while(sz) {
wolfSSL 0:1239e9b70ca2 302 if((sz%DES_BUFFER_SIZE) == sz) {
wolfSSL 0:1239e9b70ca2 303 size = sz ;
wolfSSL 0:1239e9b70ca2 304 sz = 0 ;
wolfSSL 0:1239e9b70ca2 305 } else {
wolfSSL 0:1239e9b70ca2 306 size = DES_BUFFER_SIZE ;
wolfSSL 0:1239e9b70ca2 307 sz -= DES_BUFFER_SIZE ;
wolfSSL 0:1239e9b70ca2 308 }
wolfSSL 0:1239e9b70ca2 309
wolfSSL 0:1239e9b70ca2 310 descriptor.header = desc ;
wolfSSL 0:1239e9b70ca2 311 /*
wolfSSL 0:1239e9b70ca2 312 escriptor.length1 = 0x0;
wolfSSL 0:1239e9b70ca2 313 descriptor.pointer1 = NULL;
wolfSSL 0:1239e9b70ca2 314 */
wolfSSL 0:1239e9b70ca2 315 descriptor.length2 = des->ivlen ;
wolfSSL 0:1239e9b70ca2 316 descriptor.pointer2 = (byte *)des->iv ;
wolfSSL 0:1239e9b70ca2 317 descriptor.length3 = des->keylen ;
wolfSSL 0:1239e9b70ca2 318 descriptor.pointer3 = (byte *)des->key;
wolfSSL 0:1239e9b70ca2 319 descriptor.length4 = size;
wolfSSL 0:1239e9b70ca2 320 descriptor.pointer4 = (byte *)in ;
wolfSSL 0:1239e9b70ca2 321 descriptor.length5 = size;
wolfSSL 0:1239e9b70ca2 322 descriptor.pointer5 = DesBuffer ;
wolfSSL 0:1239e9b70ca2 323 /*
wolfSSL 0:1239e9b70ca2 324 descriptor.length6 = 0;
wolfSSL 0:1239e9b70ca2 325 descriptor.pointer6 = NULL;
wolfSSL 0:1239e9b70ca2 326 descriptor.length7 = 0x0;
wolfSSL 0:1239e9b70ca2 327 descriptor.pointer7 = NULL;
wolfSSL 0:1239e9b70ca2 328 descriptor.nextDescriptorPtr = NULL ;
wolfSSL 0:1239e9b70ca2 329 */
wolfSSL 0:1239e9b70ca2 330
wolfSSL 0:1239e9b70ca2 331 /* Initialize SEC and wait for encryption to complete */
wolfSSL 0:1239e9b70ca2 332 MCF_SEC_CCCR0 = 0x0000001A; //enable channel done notification
wolfSSL 0:1239e9b70ca2 333
wolfSSL 0:1239e9b70ca2 334 /* Point SEC to the location of the descriptor */
wolfSSL 0:1239e9b70ca2 335 MCF_SEC_FR0 = (uint32)&descriptor;
wolfSSL 0:1239e9b70ca2 336
wolfSSL 0:1239e9b70ca2 337 /* poll SISR to determine when channel is complete */
wolfSSL 0:1239e9b70ca2 338 while (!(MCF_SEC_SISRL) && !(MCF_SEC_SISRH))
wolfSSL 0:1239e9b70ca2 339 ;
wolfSSL 0:1239e9b70ca2 340
wolfSSL 0:1239e9b70ca2 341 for(v=0; v<500; v++) ;
wolfSSL 0:1239e9b70ca2 342
wolfSSL 0:1239e9b70ca2 343 ret = MCF_SEC_SISRH;
wolfSSL 0:1239e9b70ca2 344 stat1 = MCF_SEC_DSR ;
wolfSSL 0:1239e9b70ca2 345 stat2 = MCF_SEC_DISR ;
wolfSSL 0:1239e9b70ca2 346 if(ret & 0xe0000000)
wolfSSL 0:1239e9b70ca2 347 db_printf("Des_Cbc(%x):ISRH=%08x, DSR=%08x, DISR=%08x\n", desc, ret, stat1, stat2) ;
wolfSSL 0:1239e9b70ca2 348
wolfSSL 0:1239e9b70ca2 349 XMEMCPY(out, DesBuffer, size) ;
wolfSSL 0:1239e9b70ca2 350
wolfSSL 0:1239e9b70ca2 351 if((desc==SEC_DESC_DES3_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_ENCRYPT)) {
wolfSSL 0:1239e9b70ca2 352 XMEMCPY((void*)des->iv, (void*)&(out[size-DES_IVLEN]), DES_IVLEN) ;
wolfSSL 0:1239e9b70ca2 353 } else {
wolfSSL 0:1239e9b70ca2 354 XMEMCPY((void*)des->iv, (void*)&(in[size-DES_IVLEN]), DES_IVLEN) ;
wolfSSL 0:1239e9b70ca2 355 }
wolfSSL 0:1239e9b70ca2 356
wolfSSL 0:1239e9b70ca2 357 in += size ;
wolfSSL 0:1239e9b70ca2 358 out += size ;
wolfSSL 0:1239e9b70ca2 359
wolfSSL 0:1239e9b70ca2 360 }
wolfSSL 0:1239e9b70ca2 361 }
wolfSSL 0:1239e9b70ca2 362
wolfSSL 0:1239e9b70ca2 363
wolfSSL 0:1239e9b70ca2 364 void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 365 {
wolfSSL 0:1239e9b70ca2 366 Des_Cbc(des, out, in, sz, SEC_DESC_DES_CBC_ENCRYPT) ;
wolfSSL 0:1239e9b70ca2 367 }
wolfSSL 0:1239e9b70ca2 368
wolfSSL 0:1239e9b70ca2 369 void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 370 {
wolfSSL 0:1239e9b70ca2 371 Des_Cbc(des, out, in, sz, SEC_DESC_DES_CBC_DECRYPT) ;
wolfSSL 0:1239e9b70ca2 372 }
wolfSSL 0:1239e9b70ca2 373
wolfSSL 0:1239e9b70ca2 374 int Des3_CbcEncrypt(Des3* des3, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 375 {
wolfSSL 0:1239e9b70ca2 376 Des_Cbc((Des *)des3, out, in, sz, SEC_DESC_DES3_CBC_ENCRYPT) ;
wolfSSL 0:1239e9b70ca2 377 return 0;
wolfSSL 0:1239e9b70ca2 378 }
wolfSSL 0:1239e9b70ca2 379
wolfSSL 0:1239e9b70ca2 380 int Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 381 {
wolfSSL 0:1239e9b70ca2 382 Des_Cbc((Des *)des3, out, in, sz, SEC_DESC_DES3_CBC_DECRYPT) ;
wolfSSL 0:1239e9b70ca2 383 return 0;
wolfSSL 0:1239e9b70ca2 384 }
wolfSSL 0:1239e9b70ca2 385
wolfSSL 0:1239e9b70ca2 386
wolfSSL 0:1239e9b70ca2 387 int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 0:1239e9b70ca2 388 {
wolfSSL 0:1239e9b70ca2 389 int i ; int status ;
wolfSSL 0:1239e9b70ca2 390
wolfSSL 0:1239e9b70ca2 391 if(DesBuffer == NULL) {
wolfSSL 0:1239e9b70ca2 392 status = tx_byte_allocate(&mp_ncached,(void *)&DesBuffer,DES_BUFFER_SIZE,TX_NO_WAIT);
wolfSSL 0:1239e9b70ca2 393 }
wolfSSL 0:1239e9b70ca2 394
wolfSSL 0:1239e9b70ca2 395 XMEMCPY(des->key, key, DES_KEYLEN);
wolfSSL 0:1239e9b70ca2 396 des->keylen = DES_KEYLEN ;
wolfSSL 0:1239e9b70ca2 397 des->ivlen = 0 ;
wolfSSL 0:1239e9b70ca2 398 if (iv) {
wolfSSL 0:1239e9b70ca2 399 XMEMCPY(des->iv, iv, DES_IVLEN);
wolfSSL 0:1239e9b70ca2 400 des->ivlen = DES_IVLEN ;
wolfSSL 0:1239e9b70ca2 401 } else {
wolfSSL 0:1239e9b70ca2 402 for(i=0; i<DES_IVLEN; i++)
wolfSSL 0:1239e9b70ca2 403 des->iv[i] = 0x0 ;
wolfSSL 0:1239e9b70ca2 404 }
wolfSSL 0:1239e9b70ca2 405
wolfSSL 0:1239e9b70ca2 406 return 0;
wolfSSL 0:1239e9b70ca2 407 }
wolfSSL 0:1239e9b70ca2 408
wolfSSL 0:1239e9b70ca2 409 int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir)
wolfSSL 0:1239e9b70ca2 410 {
wolfSSL 0:1239e9b70ca2 411 int i ; int status ;
wolfSSL 0:1239e9b70ca2 412
wolfSSL 0:1239e9b70ca2 413 if(DesBuffer == NULL) {
wolfSSL 0:1239e9b70ca2 414 status = tx_byte_allocate(&mp_ncached,(void *)&DesBuffer,DES_BUFFER_SIZE,TX_NO_WAIT);
wolfSSL 0:1239e9b70ca2 415 }
wolfSSL 0:1239e9b70ca2 416
wolfSSL 0:1239e9b70ca2 417 XMEMCPY(des3->key, key, DES3_KEYLEN);
wolfSSL 0:1239e9b70ca2 418 des3->keylen = DES3_KEYLEN ;
wolfSSL 0:1239e9b70ca2 419 des3->ivlen = 0 ;
wolfSSL 0:1239e9b70ca2 420 if (iv) {
wolfSSL 0:1239e9b70ca2 421 XMEMCPY(des3->iv, iv, DES3_IVLEN);
wolfSSL 0:1239e9b70ca2 422 des3->ivlen = DES3_IVLEN ;
wolfSSL 0:1239e9b70ca2 423 } else {
wolfSSL 0:1239e9b70ca2 424 for(i=0; i<DES_IVLEN; i++)
wolfSSL 0:1239e9b70ca2 425 des3->iv[i] = 0x0 ;
wolfSSL 0:1239e9b70ca2 426 }
wolfSSL 0:1239e9b70ca2 427
wolfSSL 0:1239e9b70ca2 428 return 0;
wolfSSL 0:1239e9b70ca2 429 }
wolfSSL 0:1239e9b70ca2 430
wolfSSL 0:1239e9b70ca2 431 #elif defined FREESCALE_MMCAU
wolfSSL 0:1239e9b70ca2 432 /*
wolfSSL 0:1239e9b70ca2 433 * Freescale mmCAU hardware DES/3DES support through the CAU/mmCAU library.
wolfSSL 0:1239e9b70ca2 434 * Documentation located in ColdFire/ColdFire+ CAU and Kinetis mmCAU
wolfSSL 0:1239e9b70ca2 435 * Software Library User Guide (See note in README).
wolfSSL 0:1239e9b70ca2 436 */
wolfSSL 0:1239e9b70ca2 437 #include "cau_api.h"
wolfSSL 0:1239e9b70ca2 438
wolfSSL 0:1239e9b70ca2 439 const unsigned char parityLookup[128] =
wolfSSL 0:1239e9b70ca2 440 {
wolfSSL 0:1239e9b70ca2 441 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
wolfSSL 0:1239e9b70ca2 442 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
wolfSSL 0:1239e9b70ca2 443 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
wolfSSL 0:1239e9b70ca2 444 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
wolfSSL 0:1239e9b70ca2 445 };
wolfSSL 0:1239e9b70ca2 446
wolfSSL 0:1239e9b70ca2 447 int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 0:1239e9b70ca2 448 {
wolfSSL 0:1239e9b70ca2 449 int i = 0;
wolfSSL 0:1239e9b70ca2 450 byte* dkey = (byte*)des->key;
wolfSSL 0:1239e9b70ca2 451
wolfSSL 0:1239e9b70ca2 452 XMEMCPY(dkey, key, 8);
wolfSSL 0:1239e9b70ca2 453
wolfSSL 0:1239e9b70ca2 454 Des_SetIV(des, iv);
wolfSSL 0:1239e9b70ca2 455
wolfSSL 0:1239e9b70ca2 456 /* fix key parity, if needed */
wolfSSL 0:1239e9b70ca2 457 for (i = 0; i < 8; i++) {
wolfSSL 0:1239e9b70ca2 458 dkey[i] = ((dkey[i] & 0xFE) | parityLookup[dkey[i] >> 1]);
wolfSSL 0:1239e9b70ca2 459 }
wolfSSL 0:1239e9b70ca2 460
wolfSSL 0:1239e9b70ca2 461 return 0;
wolfSSL 0:1239e9b70ca2 462 }
wolfSSL 0:1239e9b70ca2 463
wolfSSL 0:1239e9b70ca2 464 int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
wolfSSL 0:1239e9b70ca2 465 {
wolfSSL 0:1239e9b70ca2 466 int i = 0, ret = 0;
wolfSSL 0:1239e9b70ca2 467 byte* dkey1 = (byte*)des->key[0];
wolfSSL 0:1239e9b70ca2 468 byte* dkey2 = (byte*)des->key[1];
wolfSSL 0:1239e9b70ca2 469 byte* dkey3 = (byte*)des->key[2];
wolfSSL 0:1239e9b70ca2 470
wolfSSL 0:1239e9b70ca2 471 XMEMCPY(dkey1, key, 8); /* set key 1 */
wolfSSL 0:1239e9b70ca2 472 XMEMCPY(dkey2, key + 8, 8); /* set key 2 */
wolfSSL 0:1239e9b70ca2 473 XMEMCPY(dkey3, key + 16, 8); /* set key 3 */
wolfSSL 0:1239e9b70ca2 474
wolfSSL 0:1239e9b70ca2 475 ret = Des3_SetIV(des, iv);
wolfSSL 0:1239e9b70ca2 476 if (ret != 0)
wolfSSL 0:1239e9b70ca2 477 return ret;
wolfSSL 0:1239e9b70ca2 478
wolfSSL 0:1239e9b70ca2 479 /* fix key parity if needed */
wolfSSL 0:1239e9b70ca2 480 for (i = 0; i < 8; i++)
wolfSSL 0:1239e9b70ca2 481 dkey1[i] = ((dkey1[i] & 0xFE) | parityLookup[dkey1[i] >> 1]);
wolfSSL 0:1239e9b70ca2 482
wolfSSL 0:1239e9b70ca2 483 for (i = 0; i < 8; i++)
wolfSSL 0:1239e9b70ca2 484 dkey2[i] = ((dkey2[i] & 0xFE) | parityLookup[dkey2[i] >> 1]);
wolfSSL 0:1239e9b70ca2 485
wolfSSL 0:1239e9b70ca2 486 for (i = 0; i < 8; i++)
wolfSSL 0:1239e9b70ca2 487 dkey3[i] = ((dkey3[i] & 0xFE) | parityLookup[dkey3[i] >> 1]);
wolfSSL 0:1239e9b70ca2 488
wolfSSL 0:1239e9b70ca2 489 return ret;
wolfSSL 0:1239e9b70ca2 490 }
wolfSSL 0:1239e9b70ca2 491
wolfSSL 0:1239e9b70ca2 492 void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 493 {
wolfSSL 0:1239e9b70ca2 494 int i;
wolfSSL 0:1239e9b70ca2 495 int offset = 0;
wolfSSL 0:1239e9b70ca2 496 int len = sz;
wolfSSL 0:1239e9b70ca2 497 byte *iv;
wolfSSL 0:1239e9b70ca2 498 byte temp_block[DES_BLOCK_SIZE];
wolfSSL 0:1239e9b70ca2 499
wolfSSL 0:1239e9b70ca2 500 iv = (byte*)des->reg;
wolfSSL 0:1239e9b70ca2 501
wolfSSL 0:1239e9b70ca2 502 while (len > 0)
wolfSSL 0:1239e9b70ca2 503 {
wolfSSL 0:1239e9b70ca2 504 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 505
wolfSSL 0:1239e9b70ca2 506 /* XOR block with IV for CBC */
wolfSSL 0:1239e9b70ca2 507 for (i = 0; i < DES_BLOCK_SIZE; i++)
wolfSSL 0:1239e9b70ca2 508 temp_block[i] ^= iv[i];
wolfSSL 0:1239e9b70ca2 509
wolfSSL 0:1239e9b70ca2 510 cau_des_encrypt(temp_block, (byte*)des->key, out + offset);
wolfSSL 0:1239e9b70ca2 511
wolfSSL 0:1239e9b70ca2 512 len -= DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 513 offset += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 514
wolfSSL 0:1239e9b70ca2 515 /* store IV for next block */
wolfSSL 0:1239e9b70ca2 516 XMEMCPY(iv, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 517 }
wolfSSL 0:1239e9b70ca2 518
wolfSSL 0:1239e9b70ca2 519 return;
wolfSSL 0:1239e9b70ca2 520 }
wolfSSL 0:1239e9b70ca2 521
wolfSSL 0:1239e9b70ca2 522 void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 523 {
wolfSSL 0:1239e9b70ca2 524 int i;
wolfSSL 0:1239e9b70ca2 525 int offset = 0;
wolfSSL 0:1239e9b70ca2 526 int len = sz;
wolfSSL 0:1239e9b70ca2 527 byte* iv;
wolfSSL 0:1239e9b70ca2 528 byte temp_block[DES_BLOCK_SIZE];
wolfSSL 0:1239e9b70ca2 529
wolfSSL 0:1239e9b70ca2 530 iv = (byte*)des->reg;
wolfSSL 0:1239e9b70ca2 531
wolfSSL 0:1239e9b70ca2 532 while (len > 0)
wolfSSL 0:1239e9b70ca2 533 {
wolfSSL 0:1239e9b70ca2 534 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 535
wolfSSL 0:1239e9b70ca2 536 cau_des_decrypt(in + offset, (byte*)des->key, out + offset);
wolfSSL 0:1239e9b70ca2 537
wolfSSL 0:1239e9b70ca2 538 /* XOR block with IV for CBC */
wolfSSL 0:1239e9b70ca2 539 for (i = 0; i < DES_BLOCK_SIZE; i++)
wolfSSL 0:1239e9b70ca2 540 (out + offset)[i] ^= iv[i];
wolfSSL 0:1239e9b70ca2 541
wolfSSL 0:1239e9b70ca2 542 /* store IV for next block */
wolfSSL 0:1239e9b70ca2 543 XMEMCPY(iv, temp_block, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 544
wolfSSL 0:1239e9b70ca2 545 len -= DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 546 offset += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 547 }
wolfSSL 0:1239e9b70ca2 548
wolfSSL 0:1239e9b70ca2 549 return;
wolfSSL 0:1239e9b70ca2 550 }
wolfSSL 0:1239e9b70ca2 551
wolfSSL 0:1239e9b70ca2 552 int Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 553 {
wolfSSL 0:1239e9b70ca2 554 int i;
wolfSSL 0:1239e9b70ca2 555 int offset = 0;
wolfSSL 0:1239e9b70ca2 556 int len = sz;
wolfSSL 0:1239e9b70ca2 557
wolfSSL 0:1239e9b70ca2 558 byte *iv;
wolfSSL 0:1239e9b70ca2 559 byte temp_block[DES_BLOCK_SIZE];
wolfSSL 0:1239e9b70ca2 560
wolfSSL 0:1239e9b70ca2 561 iv = (byte*)des->reg;
wolfSSL 0:1239e9b70ca2 562
wolfSSL 0:1239e9b70ca2 563 while (len > 0)
wolfSSL 0:1239e9b70ca2 564 {
wolfSSL 0:1239e9b70ca2 565 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 566
wolfSSL 0:1239e9b70ca2 567 /* XOR block with IV for CBC */
wolfSSL 0:1239e9b70ca2 568 for (i = 0; i < DES_BLOCK_SIZE; i++)
wolfSSL 0:1239e9b70ca2 569 temp_block[i] ^= iv[i];
wolfSSL 0:1239e9b70ca2 570
wolfSSL 0:1239e9b70ca2 571 cau_des_encrypt(temp_block , (byte*)des->key[0], out + offset);
wolfSSL 0:1239e9b70ca2 572 cau_des_decrypt(out + offset, (byte*)des->key[1], out + offset);
wolfSSL 0:1239e9b70ca2 573 cau_des_encrypt(out + offset, (byte*)des->key[2], out + offset);
wolfSSL 0:1239e9b70ca2 574
wolfSSL 0:1239e9b70ca2 575 len -= DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 576 offset += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 577
wolfSSL 0:1239e9b70ca2 578 /* store IV for next block */
wolfSSL 0:1239e9b70ca2 579 XMEMCPY(iv, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 580 }
wolfSSL 0:1239e9b70ca2 581
wolfSSL 0:1239e9b70ca2 582 return 0;
wolfSSL 0:1239e9b70ca2 583 }
wolfSSL 0:1239e9b70ca2 584
wolfSSL 0:1239e9b70ca2 585 int Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 586 {
wolfSSL 0:1239e9b70ca2 587 int i;
wolfSSL 0:1239e9b70ca2 588 int offset = 0;
wolfSSL 0:1239e9b70ca2 589 int len = sz;
wolfSSL 0:1239e9b70ca2 590
wolfSSL 0:1239e9b70ca2 591 byte* iv;
wolfSSL 0:1239e9b70ca2 592 byte temp_block[DES_BLOCK_SIZE];
wolfSSL 0:1239e9b70ca2 593
wolfSSL 0:1239e9b70ca2 594 iv = (byte*)des->reg;
wolfSSL 0:1239e9b70ca2 595
wolfSSL 0:1239e9b70ca2 596 while (len > 0)
wolfSSL 0:1239e9b70ca2 597 {
wolfSSL 0:1239e9b70ca2 598 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 599
wolfSSL 0:1239e9b70ca2 600 cau_des_decrypt(in + offset , (byte*)des->key[2], out + offset);
wolfSSL 0:1239e9b70ca2 601 cau_des_encrypt(out + offset, (byte*)des->key[1], out + offset);
wolfSSL 0:1239e9b70ca2 602 cau_des_decrypt(out + offset, (byte*)des->key[0], out + offset);
wolfSSL 0:1239e9b70ca2 603
wolfSSL 0:1239e9b70ca2 604 /* XOR block with IV for CBC */
wolfSSL 0:1239e9b70ca2 605 for (i = 0; i < DES_BLOCK_SIZE; i++)
wolfSSL 0:1239e9b70ca2 606 (out + offset)[i] ^= iv[i];
wolfSSL 0:1239e9b70ca2 607
wolfSSL 0:1239e9b70ca2 608 /* store IV for next block */
wolfSSL 0:1239e9b70ca2 609 XMEMCPY(iv, temp_block, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 610
wolfSSL 0:1239e9b70ca2 611 len -= DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 612 offset += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 613 }
wolfSSL 0:1239e9b70ca2 614
wolfSSL 0:1239e9b70ca2 615 return 0;
wolfSSL 0:1239e9b70ca2 616 }
wolfSSL 0:1239e9b70ca2 617
wolfSSL 0:1239e9b70ca2 618
wolfSSL 0:1239e9b70ca2 619 #elif defined(CYASSL_PIC32MZ_CRYPT)
wolfSSL 0:1239e9b70ca2 620
wolfSSL 0:1239e9b70ca2 621 #include "../../cyassl/ctaocrypt/port/pic32/pic32mz-crypt.h"
wolfSSL 0:1239e9b70ca2 622
wolfSSL 0:1239e9b70ca2 623 void Des_SetIV(Des* des, const byte* iv);
wolfSSL 0:1239e9b70ca2 624 int Des3_SetIV(Des3* des, const byte* iv);
wolfSSL 0:1239e9b70ca2 625
wolfSSL 0:1239e9b70ca2 626 int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 0:1239e9b70ca2 627 {
wolfSSL 0:1239e9b70ca2 628 word32 *dkey = des->key ;
wolfSSL 0:1239e9b70ca2 629 word32 *dreg = des->reg ;
wolfSSL 0:1239e9b70ca2 630
wolfSSL 0:1239e9b70ca2 631 XMEMCPY((byte *)dkey, (byte *)key, 8);
wolfSSL 0:1239e9b70ca2 632 ByteReverseWords(dkey, dkey, 8);
wolfSSL 0:1239e9b70ca2 633 XMEMCPY((byte *)dreg, (byte *)iv, 8);
wolfSSL 0:1239e9b70ca2 634 ByteReverseWords(dreg, dreg, 8);
wolfSSL 0:1239e9b70ca2 635
wolfSSL 0:1239e9b70ca2 636 return 0;
wolfSSL 0:1239e9b70ca2 637 }
wolfSSL 0:1239e9b70ca2 638
wolfSSL 0:1239e9b70ca2 639 int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
wolfSSL 0:1239e9b70ca2 640 {
wolfSSL 0:1239e9b70ca2 641 word32 *dkey1 = des->key[0];
wolfSSL 0:1239e9b70ca2 642 word32 *dreg = des->reg ;
wolfSSL 0:1239e9b70ca2 643
wolfSSL 0:1239e9b70ca2 644 XMEMCPY(dkey1, key, 24);
wolfSSL 0:1239e9b70ca2 645 ByteReverseWords(dkey1, dkey1, 24);
wolfSSL 0:1239e9b70ca2 646 XMEMCPY(dreg, iv, 8);
wolfSSL 0:1239e9b70ca2 647 ByteReverseWords(dreg, dreg, 8) ;
wolfSSL 0:1239e9b70ca2 648
wolfSSL 0:1239e9b70ca2 649 return 0;
wolfSSL 0:1239e9b70ca2 650 }
wolfSSL 0:1239e9b70ca2 651
wolfSSL 0:1239e9b70ca2 652 void DesCrypt(word32 *key, word32 *iv, byte* out, const byte* in, word32 sz,
wolfSSL 0:1239e9b70ca2 653 int dir, int algo, int cryptoalgo)
wolfSSL 0:1239e9b70ca2 654 {
wolfSSL 0:1239e9b70ca2 655 securityAssociation *sa_p ;
wolfSSL 0:1239e9b70ca2 656 bufferDescriptor *bd_p ;
wolfSSL 0:1239e9b70ca2 657 const byte *in_p, *in_l ;
wolfSSL 0:1239e9b70ca2 658 byte *out_p, *out_l ;
wolfSSL 0:1239e9b70ca2 659 volatile securityAssociation sa __attribute__((aligned (8)));
wolfSSL 0:1239e9b70ca2 660 volatile bufferDescriptor bd __attribute__((aligned (8)));
wolfSSL 0:1239e9b70ca2 661 volatile int k ;
wolfSSL 0:1239e9b70ca2 662
wolfSSL 0:1239e9b70ca2 663 /* get uncached address */
wolfSSL 0:1239e9b70ca2 664
wolfSSL 0:1239e9b70ca2 665 in_l = in;
wolfSSL 0:1239e9b70ca2 666 out_l = out ;
wolfSSL 0:1239e9b70ca2 667 sa_p = KVA0_TO_KVA1(&sa) ;
wolfSSL 0:1239e9b70ca2 668 bd_p = KVA0_TO_KVA1(&bd) ;
wolfSSL 0:1239e9b70ca2 669 in_p = KVA0_TO_KVA1(in_l) ;
wolfSSL 0:1239e9b70ca2 670 out_p= KVA0_TO_KVA1(out_l);
wolfSSL 0:1239e9b70ca2 671
wolfSSL 0:1239e9b70ca2 672 if(PIC32MZ_IF_RAM(in_p))
wolfSSL 0:1239e9b70ca2 673 XMEMCPY((void *)in_p, (void *)in, sz);
wolfSSL 0:1239e9b70ca2 674 XMEMSET((void *)out_p, 0, sz);
wolfSSL 0:1239e9b70ca2 675
wolfSSL 0:1239e9b70ca2 676 /* Set up the Security Association */
wolfSSL 0:1239e9b70ca2 677 XMEMSET((byte *)KVA0_TO_KVA1(&sa), 0, sizeof(sa));
wolfSSL 0:1239e9b70ca2 678 sa_p->SA_CTRL.ALGO = algo ;
wolfSSL 0:1239e9b70ca2 679 sa_p->SA_CTRL.LNC = 1;
wolfSSL 0:1239e9b70ca2 680 sa_p->SA_CTRL.LOADIV = 1;
wolfSSL 0:1239e9b70ca2 681 sa_p->SA_CTRL.FB = 1;
wolfSSL 0:1239e9b70ca2 682 sa_p->SA_CTRL.ENCTYPE = dir ; /* Encryption/Decryption */
wolfSSL 0:1239e9b70ca2 683 sa_p->SA_CTRL.CRYPTOALGO = cryptoalgo;
wolfSSL 0:1239e9b70ca2 684 sa_p->SA_CTRL.KEYSIZE = 1 ; /* KEY is 192 bits */
wolfSSL 0:1239e9b70ca2 685 XMEMCPY((byte *)KVA0_TO_KVA1(&sa.SA_ENCKEY[algo==PIC32_ALGO_TDES ? 2 : 6]),
wolfSSL 0:1239e9b70ca2 686 (byte *)key, algo==PIC32_ALGO_TDES ? 24 : 8);
wolfSSL 0:1239e9b70ca2 687 XMEMCPY((byte *)KVA0_TO_KVA1(&sa.SA_ENCIV[2]), (byte *)iv, 8);
wolfSSL 0:1239e9b70ca2 688
wolfSSL 0:1239e9b70ca2 689 XMEMSET((byte *)KVA0_TO_KVA1(&bd), 0, sizeof(bd));
wolfSSL 0:1239e9b70ca2 690 /* Set up the Buffer Descriptor */
wolfSSL 0:1239e9b70ca2 691 bd_p->BD_CTRL.BUFLEN = sz;
wolfSSL 0:1239e9b70ca2 692 bd_p->BD_CTRL.LIFM = 1;
wolfSSL 0:1239e9b70ca2 693 bd_p->BD_CTRL.SA_FETCH_EN = 1;
wolfSSL 0:1239e9b70ca2 694 bd_p->BD_CTRL.LAST_BD = 1;
wolfSSL 0:1239e9b70ca2 695 bd_p->BD_CTRL.DESC_EN = 1;
wolfSSL 0:1239e9b70ca2 696
wolfSSL 0:1239e9b70ca2 697 bd_p->SA_ADDR = (unsigned int)KVA_TO_PA(&sa) ; // (unsigned int)sa_p ;
wolfSSL 0:1239e9b70ca2 698 bd_p->SRCADDR = (unsigned int)KVA_TO_PA(in) ; // (unsigned int)in_p ;
wolfSSL 0:1239e9b70ca2 699 bd_p->DSTADDR = (unsigned int)KVA_TO_PA(out); // (unsigned int)out_p ;
wolfSSL 0:1239e9b70ca2 700 bd_p->NXTPTR = (unsigned int)KVA_TO_PA(&bd);
wolfSSL 0:1239e9b70ca2 701 bd_p->MSGLEN = sz ;
wolfSSL 0:1239e9b70ca2 702
wolfSSL 0:1239e9b70ca2 703 /* Fire in the hole! */
wolfSSL 0:1239e9b70ca2 704 CECON = 1 << 6;
wolfSSL 0:1239e9b70ca2 705 while (CECON);
wolfSSL 0:1239e9b70ca2 706
wolfSSL 0:1239e9b70ca2 707 /* Run the engine */
wolfSSL 0:1239e9b70ca2 708 CEBDPADDR = (unsigned int)KVA_TO_PA(&bd) ; // (unsigned int)bd_p ;
wolfSSL 0:1239e9b70ca2 709 CEINTEN = 0x07;
wolfSSL 0:1239e9b70ca2 710 CECON = 0x27;
wolfSSL 0:1239e9b70ca2 711
wolfSSL 0:1239e9b70ca2 712 WAIT_ENGINE ;
wolfSSL 0:1239e9b70ca2 713
wolfSSL 0:1239e9b70ca2 714 if((cryptoalgo == PIC32_CRYPTOALGO_CBC) ||
wolfSSL 0:1239e9b70ca2 715 (cryptoalgo == PIC32_CRYPTOALGO_TCBC)||
wolfSSL 0:1239e9b70ca2 716 (cryptoalgo == PIC32_CRYPTOALGO_RCBC)) {
wolfSSL 0:1239e9b70ca2 717 /* set iv for the next call */
wolfSSL 0:1239e9b70ca2 718 if(dir == PIC32_ENCRYPTION) {
wolfSSL 0:1239e9b70ca2 719 XMEMCPY((void *)iv, (void*)&(out_p[sz-DES_IVLEN]), DES_IVLEN) ;
wolfSSL 0:1239e9b70ca2 720 } else {
wolfSSL 0:1239e9b70ca2 721 ByteReverseWords((word32*)iv, (word32 *)&(in_p[sz-DES_IVLEN]), DES_IVLEN);
wolfSSL 0:1239e9b70ca2 722 }
wolfSSL 0:1239e9b70ca2 723
wolfSSL 0:1239e9b70ca2 724 }
wolfSSL 0:1239e9b70ca2 725
wolfSSL 0:1239e9b70ca2 726 ByteReverseWords((word32*)out, (word32 *)KVA0_TO_KVA1(out), sz);
wolfSSL 0:1239e9b70ca2 727 }
wolfSSL 0:1239e9b70ca2 728
wolfSSL 0:1239e9b70ca2 729 void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 730 {
wolfSSL 0:1239e9b70ca2 731 DesCrypt(des->key, des->reg, out, in, sz,
wolfSSL 0:1239e9b70ca2 732 PIC32_ENCRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC );
wolfSSL 0:1239e9b70ca2 733 }
wolfSSL 0:1239e9b70ca2 734
wolfSSL 0:1239e9b70ca2 735 void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 736 {
wolfSSL 0:1239e9b70ca2 737 DesCrypt(des->key, des->reg, out, in, sz,
wolfSSL 0:1239e9b70ca2 738 PIC32_DECRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC);
wolfSSL 0:1239e9b70ca2 739 }
wolfSSL 0:1239e9b70ca2 740
wolfSSL 0:1239e9b70ca2 741 int Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 742 {
wolfSSL 0:1239e9b70ca2 743 DesCrypt(des->key[0], des->reg, out, in, sz,
wolfSSL 0:1239e9b70ca2 744 PIC32_ENCRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC);
wolfSSL 0:1239e9b70ca2 745 return 0;
wolfSSL 0:1239e9b70ca2 746 }
wolfSSL 0:1239e9b70ca2 747
wolfSSL 0:1239e9b70ca2 748 int Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 749 {
wolfSSL 0:1239e9b70ca2 750 DesCrypt(des->key[0], des->reg, out, in, sz,
wolfSSL 0:1239e9b70ca2 751 PIC32_DECRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC);
wolfSSL 0:1239e9b70ca2 752 return 0;
wolfSSL 0:1239e9b70ca2 753 }
wolfSSL 0:1239e9b70ca2 754
wolfSSL 0:1239e9b70ca2 755 #else /* CTaoCrypt software implementation */
wolfSSL 0:1239e9b70ca2 756
wolfSSL 0:1239e9b70ca2 757 /* permuted choice table (key) */
wolfSSL 0:1239e9b70ca2 758 static const byte pc1[] = {
wolfSSL 0:1239e9b70ca2 759 57, 49, 41, 33, 25, 17, 9,
wolfSSL 0:1239e9b70ca2 760 1, 58, 50, 42, 34, 26, 18,
wolfSSL 0:1239e9b70ca2 761 10, 2, 59, 51, 43, 35, 27,
wolfSSL 0:1239e9b70ca2 762 19, 11, 3, 60, 52, 44, 36,
wolfSSL 0:1239e9b70ca2 763
wolfSSL 0:1239e9b70ca2 764 63, 55, 47, 39, 31, 23, 15,
wolfSSL 0:1239e9b70ca2 765 7, 62, 54, 46, 38, 30, 22,
wolfSSL 0:1239e9b70ca2 766 14, 6, 61, 53, 45, 37, 29,
wolfSSL 0:1239e9b70ca2 767 21, 13, 5, 28, 20, 12, 4
wolfSSL 0:1239e9b70ca2 768 };
wolfSSL 0:1239e9b70ca2 769
wolfSSL 0:1239e9b70ca2 770 /* number left rotations of pc1 */
wolfSSL 0:1239e9b70ca2 771 static const byte totrot[] = {
wolfSSL 0:1239e9b70ca2 772 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
wolfSSL 0:1239e9b70ca2 773 };
wolfSSL 0:1239e9b70ca2 774
wolfSSL 0:1239e9b70ca2 775 /* permuted choice key (table) */
wolfSSL 0:1239e9b70ca2 776 static const byte pc2[] = {
wolfSSL 0:1239e9b70ca2 777 14, 17, 11, 24, 1, 5,
wolfSSL 0:1239e9b70ca2 778 3, 28, 15, 6, 21, 10,
wolfSSL 0:1239e9b70ca2 779 23, 19, 12, 4, 26, 8,
wolfSSL 0:1239e9b70ca2 780 16, 7, 27, 20, 13, 2,
wolfSSL 0:1239e9b70ca2 781 41, 52, 31, 37, 47, 55,
wolfSSL 0:1239e9b70ca2 782 30, 40, 51, 45, 33, 48,
wolfSSL 0:1239e9b70ca2 783 44, 49, 39, 56, 34, 53,
wolfSSL 0:1239e9b70ca2 784 46, 42, 50, 36, 29, 32
wolfSSL 0:1239e9b70ca2 785 };
wolfSSL 0:1239e9b70ca2 786
wolfSSL 0:1239e9b70ca2 787 /* End of DES-defined tables */
wolfSSL 0:1239e9b70ca2 788
wolfSSL 0:1239e9b70ca2 789 /* bit 0 is left-most in byte */
wolfSSL 0:1239e9b70ca2 790 static const int bytebit[] = {
wolfSSL 0:1239e9b70ca2 791 0200,0100,040,020,010,04,02,01
wolfSSL 0:1239e9b70ca2 792 };
wolfSSL 0:1239e9b70ca2 793
wolfSSL 0:1239e9b70ca2 794 static const word32 Spbox[8][64] = {
wolfSSL 0:1239e9b70ca2 795 {
wolfSSL 0:1239e9b70ca2 796 0x01010400,0x00000000,0x00010000,0x01010404,
wolfSSL 0:1239e9b70ca2 797 0x01010004,0x00010404,0x00000004,0x00010000,
wolfSSL 0:1239e9b70ca2 798 0x00000400,0x01010400,0x01010404,0x00000400,
wolfSSL 0:1239e9b70ca2 799 0x01000404,0x01010004,0x01000000,0x00000004,
wolfSSL 0:1239e9b70ca2 800 0x00000404,0x01000400,0x01000400,0x00010400,
wolfSSL 0:1239e9b70ca2 801 0x00010400,0x01010000,0x01010000,0x01000404,
wolfSSL 0:1239e9b70ca2 802 0x00010004,0x01000004,0x01000004,0x00010004,
wolfSSL 0:1239e9b70ca2 803 0x00000000,0x00000404,0x00010404,0x01000000,
wolfSSL 0:1239e9b70ca2 804 0x00010000,0x01010404,0x00000004,0x01010000,
wolfSSL 0:1239e9b70ca2 805 0x01010400,0x01000000,0x01000000,0x00000400,
wolfSSL 0:1239e9b70ca2 806 0x01010004,0x00010000,0x00010400,0x01000004,
wolfSSL 0:1239e9b70ca2 807 0x00000400,0x00000004,0x01000404,0x00010404,
wolfSSL 0:1239e9b70ca2 808 0x01010404,0x00010004,0x01010000,0x01000404,
wolfSSL 0:1239e9b70ca2 809 0x01000004,0x00000404,0x00010404,0x01010400,
wolfSSL 0:1239e9b70ca2 810 0x00000404,0x01000400,0x01000400,0x00000000,
wolfSSL 0:1239e9b70ca2 811 0x00010004,0x00010400,0x00000000,0x01010004},
wolfSSL 0:1239e9b70ca2 812 {
wolfSSL 0:1239e9b70ca2 813 0x80108020,0x80008000,0x00008000,0x00108020,
wolfSSL 0:1239e9b70ca2 814 0x00100000,0x00000020,0x80100020,0x80008020,
wolfSSL 0:1239e9b70ca2 815 0x80000020,0x80108020,0x80108000,0x80000000,
wolfSSL 0:1239e9b70ca2 816 0x80008000,0x00100000,0x00000020,0x80100020,
wolfSSL 0:1239e9b70ca2 817 0x00108000,0x00100020,0x80008020,0x00000000,
wolfSSL 0:1239e9b70ca2 818 0x80000000,0x00008000,0x00108020,0x80100000,
wolfSSL 0:1239e9b70ca2 819 0x00100020,0x80000020,0x00000000,0x00108000,
wolfSSL 0:1239e9b70ca2 820 0x00008020,0x80108000,0x80100000,0x00008020,
wolfSSL 0:1239e9b70ca2 821 0x00000000,0x00108020,0x80100020,0x00100000,
wolfSSL 0:1239e9b70ca2 822 0x80008020,0x80100000,0x80108000,0x00008000,
wolfSSL 0:1239e9b70ca2 823 0x80100000,0x80008000,0x00000020,0x80108020,
wolfSSL 0:1239e9b70ca2 824 0x00108020,0x00000020,0x00008000,0x80000000,
wolfSSL 0:1239e9b70ca2 825 0x00008020,0x80108000,0x00100000,0x80000020,
wolfSSL 0:1239e9b70ca2 826 0x00100020,0x80008020,0x80000020,0x00100020,
wolfSSL 0:1239e9b70ca2 827 0x00108000,0x00000000,0x80008000,0x00008020,
wolfSSL 0:1239e9b70ca2 828 0x80000000,0x80100020,0x80108020,0x00108000},
wolfSSL 0:1239e9b70ca2 829 {
wolfSSL 0:1239e9b70ca2 830 0x00000208,0x08020200,0x00000000,0x08020008,
wolfSSL 0:1239e9b70ca2 831 0x08000200,0x00000000,0x00020208,0x08000200,
wolfSSL 0:1239e9b70ca2 832 0x00020008,0x08000008,0x08000008,0x00020000,
wolfSSL 0:1239e9b70ca2 833 0x08020208,0x00020008,0x08020000,0x00000208,
wolfSSL 0:1239e9b70ca2 834 0x08000000,0x00000008,0x08020200,0x00000200,
wolfSSL 0:1239e9b70ca2 835 0x00020200,0x08020000,0x08020008,0x00020208,
wolfSSL 0:1239e9b70ca2 836 0x08000208,0x00020200,0x00020000,0x08000208,
wolfSSL 0:1239e9b70ca2 837 0x00000008,0x08020208,0x00000200,0x08000000,
wolfSSL 0:1239e9b70ca2 838 0x08020200,0x08000000,0x00020008,0x00000208,
wolfSSL 0:1239e9b70ca2 839 0x00020000,0x08020200,0x08000200,0x00000000,
wolfSSL 0:1239e9b70ca2 840 0x00000200,0x00020008,0x08020208,0x08000200,
wolfSSL 0:1239e9b70ca2 841 0x08000008,0x00000200,0x00000000,0x08020008,
wolfSSL 0:1239e9b70ca2 842 0x08000208,0x00020000,0x08000000,0x08020208,
wolfSSL 0:1239e9b70ca2 843 0x00000008,0x00020208,0x00020200,0x08000008,
wolfSSL 0:1239e9b70ca2 844 0x08020000,0x08000208,0x00000208,0x08020000,
wolfSSL 0:1239e9b70ca2 845 0x00020208,0x00000008,0x08020008,0x00020200},
wolfSSL 0:1239e9b70ca2 846 {
wolfSSL 0:1239e9b70ca2 847 0x00802001,0x00002081,0x00002081,0x00000080,
wolfSSL 0:1239e9b70ca2 848 0x00802080,0x00800081,0x00800001,0x00002001,
wolfSSL 0:1239e9b70ca2 849 0x00000000,0x00802000,0x00802000,0x00802081,
wolfSSL 0:1239e9b70ca2 850 0x00000081,0x00000000,0x00800080,0x00800001,
wolfSSL 0:1239e9b70ca2 851 0x00000001,0x00002000,0x00800000,0x00802001,
wolfSSL 0:1239e9b70ca2 852 0x00000080,0x00800000,0x00002001,0x00002080,
wolfSSL 0:1239e9b70ca2 853 0x00800081,0x00000001,0x00002080,0x00800080,
wolfSSL 0:1239e9b70ca2 854 0x00002000,0x00802080,0x00802081,0x00000081,
wolfSSL 0:1239e9b70ca2 855 0x00800080,0x00800001,0x00802000,0x00802081,
wolfSSL 0:1239e9b70ca2 856 0x00000081,0x00000000,0x00000000,0x00802000,
wolfSSL 0:1239e9b70ca2 857 0x00002080,0x00800080,0x00800081,0x00000001,
wolfSSL 0:1239e9b70ca2 858 0x00802001,0x00002081,0x00002081,0x00000080,
wolfSSL 0:1239e9b70ca2 859 0x00802081,0x00000081,0x00000001,0x00002000,
wolfSSL 0:1239e9b70ca2 860 0x00800001,0x00002001,0x00802080,0x00800081,
wolfSSL 0:1239e9b70ca2 861 0x00002001,0x00002080,0x00800000,0x00802001,
wolfSSL 0:1239e9b70ca2 862 0x00000080,0x00800000,0x00002000,0x00802080},
wolfSSL 0:1239e9b70ca2 863 {
wolfSSL 0:1239e9b70ca2 864 0x00000100,0x02080100,0x02080000,0x42000100,
wolfSSL 0:1239e9b70ca2 865 0x00080000,0x00000100,0x40000000,0x02080000,
wolfSSL 0:1239e9b70ca2 866 0x40080100,0x00080000,0x02000100,0x40080100,
wolfSSL 0:1239e9b70ca2 867 0x42000100,0x42080000,0x00080100,0x40000000,
wolfSSL 0:1239e9b70ca2 868 0x02000000,0x40080000,0x40080000,0x00000000,
wolfSSL 0:1239e9b70ca2 869 0x40000100,0x42080100,0x42080100,0x02000100,
wolfSSL 0:1239e9b70ca2 870 0x42080000,0x40000100,0x00000000,0x42000000,
wolfSSL 0:1239e9b70ca2 871 0x02080100,0x02000000,0x42000000,0x00080100,
wolfSSL 0:1239e9b70ca2 872 0x00080000,0x42000100,0x00000100,0x02000000,
wolfSSL 0:1239e9b70ca2 873 0x40000000,0x02080000,0x42000100,0x40080100,
wolfSSL 0:1239e9b70ca2 874 0x02000100,0x40000000,0x42080000,0x02080100,
wolfSSL 0:1239e9b70ca2 875 0x40080100,0x00000100,0x02000000,0x42080000,
wolfSSL 0:1239e9b70ca2 876 0x42080100,0x00080100,0x42000000,0x42080100,
wolfSSL 0:1239e9b70ca2 877 0x02080000,0x00000000,0x40080000,0x42000000,
wolfSSL 0:1239e9b70ca2 878 0x00080100,0x02000100,0x40000100,0x00080000,
wolfSSL 0:1239e9b70ca2 879 0x00000000,0x40080000,0x02080100,0x40000100},
wolfSSL 0:1239e9b70ca2 880 {
wolfSSL 0:1239e9b70ca2 881 0x20000010,0x20400000,0x00004000,0x20404010,
wolfSSL 0:1239e9b70ca2 882 0x20400000,0x00000010,0x20404010,0x00400000,
wolfSSL 0:1239e9b70ca2 883 0x20004000,0x00404010,0x00400000,0x20000010,
wolfSSL 0:1239e9b70ca2 884 0x00400010,0x20004000,0x20000000,0x00004010,
wolfSSL 0:1239e9b70ca2 885 0x00000000,0x00400010,0x20004010,0x00004000,
wolfSSL 0:1239e9b70ca2 886 0x00404000,0x20004010,0x00000010,0x20400010,
wolfSSL 0:1239e9b70ca2 887 0x20400010,0x00000000,0x00404010,0x20404000,
wolfSSL 0:1239e9b70ca2 888 0x00004010,0x00404000,0x20404000,0x20000000,
wolfSSL 0:1239e9b70ca2 889 0x20004000,0x00000010,0x20400010,0x00404000,
wolfSSL 0:1239e9b70ca2 890 0x20404010,0x00400000,0x00004010,0x20000010,
wolfSSL 0:1239e9b70ca2 891 0x00400000,0x20004000,0x20000000,0x00004010,
wolfSSL 0:1239e9b70ca2 892 0x20000010,0x20404010,0x00404000,0x20400000,
wolfSSL 0:1239e9b70ca2 893 0x00404010,0x20404000,0x00000000,0x20400010,
wolfSSL 0:1239e9b70ca2 894 0x00000010,0x00004000,0x20400000,0x00404010,
wolfSSL 0:1239e9b70ca2 895 0x00004000,0x00400010,0x20004010,0x00000000,
wolfSSL 0:1239e9b70ca2 896 0x20404000,0x20000000,0x00400010,0x20004010},
wolfSSL 0:1239e9b70ca2 897 {
wolfSSL 0:1239e9b70ca2 898 0x00200000,0x04200002,0x04000802,0x00000000,
wolfSSL 0:1239e9b70ca2 899 0x00000800,0x04000802,0x00200802,0x04200800,
wolfSSL 0:1239e9b70ca2 900 0x04200802,0x00200000,0x00000000,0x04000002,
wolfSSL 0:1239e9b70ca2 901 0x00000002,0x04000000,0x04200002,0x00000802,
wolfSSL 0:1239e9b70ca2 902 0x04000800,0x00200802,0x00200002,0x04000800,
wolfSSL 0:1239e9b70ca2 903 0x04000002,0x04200000,0x04200800,0x00200002,
wolfSSL 0:1239e9b70ca2 904 0x04200000,0x00000800,0x00000802,0x04200802,
wolfSSL 0:1239e9b70ca2 905 0x00200800,0x00000002,0x04000000,0x00200800,
wolfSSL 0:1239e9b70ca2 906 0x04000000,0x00200800,0x00200000,0x04000802,
wolfSSL 0:1239e9b70ca2 907 0x04000802,0x04200002,0x04200002,0x00000002,
wolfSSL 0:1239e9b70ca2 908 0x00200002,0x04000000,0x04000800,0x00200000,
wolfSSL 0:1239e9b70ca2 909 0x04200800,0x00000802,0x00200802,0x04200800,
wolfSSL 0:1239e9b70ca2 910 0x00000802,0x04000002,0x04200802,0x04200000,
wolfSSL 0:1239e9b70ca2 911 0x00200800,0x00000000,0x00000002,0x04200802,
wolfSSL 0:1239e9b70ca2 912 0x00000000,0x00200802,0x04200000,0x00000800,
wolfSSL 0:1239e9b70ca2 913 0x04000002,0x04000800,0x00000800,0x00200002},
wolfSSL 0:1239e9b70ca2 914 {
wolfSSL 0:1239e9b70ca2 915 0x10001040,0x00001000,0x00040000,0x10041040,
wolfSSL 0:1239e9b70ca2 916 0x10000000,0x10001040,0x00000040,0x10000000,
wolfSSL 0:1239e9b70ca2 917 0x00040040,0x10040000,0x10041040,0x00041000,
wolfSSL 0:1239e9b70ca2 918 0x10041000,0x00041040,0x00001000,0x00000040,
wolfSSL 0:1239e9b70ca2 919 0x10040000,0x10000040,0x10001000,0x00001040,
wolfSSL 0:1239e9b70ca2 920 0x00041000,0x00040040,0x10040040,0x10041000,
wolfSSL 0:1239e9b70ca2 921 0x00001040,0x00000000,0x00000000,0x10040040,
wolfSSL 0:1239e9b70ca2 922 0x10000040,0x10001000,0x00041040,0x00040000,
wolfSSL 0:1239e9b70ca2 923 0x00041040,0x00040000,0x10041000,0x00001000,
wolfSSL 0:1239e9b70ca2 924 0x00000040,0x10040040,0x00001000,0x00041040,
wolfSSL 0:1239e9b70ca2 925 0x10001000,0x00000040,0x10000040,0x10040000,
wolfSSL 0:1239e9b70ca2 926 0x10040040,0x10000000,0x00040000,0x10001040,
wolfSSL 0:1239e9b70ca2 927 0x00000000,0x10041040,0x00040040,0x10000040,
wolfSSL 0:1239e9b70ca2 928 0x10040000,0x10001000,0x10001040,0x00000000,
wolfSSL 0:1239e9b70ca2 929 0x10041040,0x00041000,0x00041000,0x00001040,
wolfSSL 0:1239e9b70ca2 930 0x00001040,0x00040040,0x10000000,0x10041000}
wolfSSL 0:1239e9b70ca2 931 };
wolfSSL 0:1239e9b70ca2 932
wolfSSL 0:1239e9b70ca2 933
wolfSSL 0:1239e9b70ca2 934 static INLINE void IPERM(word32* left, word32* right)
wolfSSL 0:1239e9b70ca2 935 {
wolfSSL 0:1239e9b70ca2 936 word32 work;
wolfSSL 0:1239e9b70ca2 937
wolfSSL 0:1239e9b70ca2 938 *right = rotlFixed(*right, 4U);
wolfSSL 0:1239e9b70ca2 939 work = (*left ^ *right) & 0xf0f0f0f0;
wolfSSL 0:1239e9b70ca2 940 *left ^= work;
wolfSSL 0:1239e9b70ca2 941
wolfSSL 0:1239e9b70ca2 942 *right = rotrFixed(*right^work, 20U);
wolfSSL 0:1239e9b70ca2 943 work = (*left ^ *right) & 0xffff0000;
wolfSSL 0:1239e9b70ca2 944 *left ^= work;
wolfSSL 0:1239e9b70ca2 945
wolfSSL 0:1239e9b70ca2 946 *right = rotrFixed(*right^work, 18U);
wolfSSL 0:1239e9b70ca2 947 work = (*left ^ *right) & 0x33333333;
wolfSSL 0:1239e9b70ca2 948 *left ^= work;
wolfSSL 0:1239e9b70ca2 949
wolfSSL 0:1239e9b70ca2 950 *right = rotrFixed(*right^work, 6U);
wolfSSL 0:1239e9b70ca2 951 work = (*left ^ *right) & 0x00ff00ff;
wolfSSL 0:1239e9b70ca2 952 *left ^= work;
wolfSSL 0:1239e9b70ca2 953
wolfSSL 0:1239e9b70ca2 954 *right = rotlFixed(*right^work, 9U);
wolfSSL 0:1239e9b70ca2 955 work = (*left ^ *right) & 0xaaaaaaaa;
wolfSSL 0:1239e9b70ca2 956 *left = rotlFixed(*left^work, 1U);
wolfSSL 0:1239e9b70ca2 957 *right ^= work;
wolfSSL 0:1239e9b70ca2 958 }
wolfSSL 0:1239e9b70ca2 959
wolfSSL 0:1239e9b70ca2 960
wolfSSL 0:1239e9b70ca2 961 static INLINE void FPERM(word32* left, word32* right)
wolfSSL 0:1239e9b70ca2 962 {
wolfSSL 0:1239e9b70ca2 963 word32 work;
wolfSSL 0:1239e9b70ca2 964
wolfSSL 0:1239e9b70ca2 965 *right = rotrFixed(*right, 1U);
wolfSSL 0:1239e9b70ca2 966 work = (*left ^ *right) & 0xaaaaaaaa;
wolfSSL 0:1239e9b70ca2 967 *right ^= work;
wolfSSL 0:1239e9b70ca2 968
wolfSSL 0:1239e9b70ca2 969 *left = rotrFixed(*left^work, 9U);
wolfSSL 0:1239e9b70ca2 970 work = (*left ^ *right) & 0x00ff00ff;
wolfSSL 0:1239e9b70ca2 971 *right ^= work;
wolfSSL 0:1239e9b70ca2 972
wolfSSL 0:1239e9b70ca2 973 *left = rotlFixed(*left^work, 6U);
wolfSSL 0:1239e9b70ca2 974 work = (*left ^ *right) & 0x33333333;
wolfSSL 0:1239e9b70ca2 975 *right ^= work;
wolfSSL 0:1239e9b70ca2 976
wolfSSL 0:1239e9b70ca2 977 *left = rotlFixed(*left^work, 18U);
wolfSSL 0:1239e9b70ca2 978 work = (*left ^ *right) & 0xffff0000;
wolfSSL 0:1239e9b70ca2 979 *right ^= work;
wolfSSL 0:1239e9b70ca2 980
wolfSSL 0:1239e9b70ca2 981 *left = rotlFixed(*left^work, 20U);
wolfSSL 0:1239e9b70ca2 982 work = (*left ^ *right) & 0xf0f0f0f0;
wolfSSL 0:1239e9b70ca2 983 *right ^= work;
wolfSSL 0:1239e9b70ca2 984
wolfSSL 0:1239e9b70ca2 985 *left = rotrFixed(*left^work, 4U);
wolfSSL 0:1239e9b70ca2 986 }
wolfSSL 0:1239e9b70ca2 987
wolfSSL 0:1239e9b70ca2 988
wolfSSL 0:1239e9b70ca2 989 static int DesSetKey(const byte* key, int dir, word32* out)
wolfSSL 0:1239e9b70ca2 990 {
wolfSSL 0:1239e9b70ca2 991 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 992 byte* buffer = (byte*)XMALLOC(56+56+8, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 993
wolfSSL 0:1239e9b70ca2 994 if (buffer == NULL)
wolfSSL 0:1239e9b70ca2 995 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 996 #else
wolfSSL 0:1239e9b70ca2 997 byte buffer[56+56+8];
wolfSSL 0:1239e9b70ca2 998 #endif
wolfSSL 0:1239e9b70ca2 999
wolfSSL 0:1239e9b70ca2 1000 {
wolfSSL 0:1239e9b70ca2 1001 byte* const pc1m = buffer; /* place to modify pc1 into */
wolfSSL 0:1239e9b70ca2 1002 byte* const pcr = pc1m + 56; /* place to rotate pc1 into */
wolfSSL 0:1239e9b70ca2 1003 byte* const ks = pcr + 56;
wolfSSL 0:1239e9b70ca2 1004 register int i, j, l;
wolfSSL 0:1239e9b70ca2 1005 int m;
wolfSSL 0:1239e9b70ca2 1006
wolfSSL 0:1239e9b70ca2 1007 for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */
wolfSSL 0:1239e9b70ca2 1008 l = pc1[j] - 1; /* integer bit location */
wolfSSL 0:1239e9b70ca2 1009 m = l & 07; /* find bit */
wolfSSL 0:1239e9b70ca2 1010 pc1m[j] = (key[l >> 3] & /* find which key byte l is in */
wolfSSL 0:1239e9b70ca2 1011 bytebit[m]) /* and which bit of that byte */
wolfSSL 0:1239e9b70ca2 1012 ? 1 : 0; /* and store 1-bit result */
wolfSSL 0:1239e9b70ca2 1013 }
wolfSSL 0:1239e9b70ca2 1014
wolfSSL 0:1239e9b70ca2 1015 for (i = 0; i < 16; i++) { /* key chunk for each iteration */
wolfSSL 0:1239e9b70ca2 1016 XMEMSET(ks, 0, 8); /* Clear key schedule */
wolfSSL 0:1239e9b70ca2 1017
wolfSSL 0:1239e9b70ca2 1018 for (j = 0; j < 56; j++) /* rotate pc1 the right amount */
wolfSSL 0:1239e9b70ca2 1019 pcr[j] =
wolfSSL 0:1239e9b70ca2 1020 pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l : l-28];
wolfSSL 0:1239e9b70ca2 1021
wolfSSL 0:1239e9b70ca2 1022 /* rotate left and right halves independently */
wolfSSL 0:1239e9b70ca2 1023 for (j = 0; j < 48; j++) { /* select bits individually */
wolfSSL 0:1239e9b70ca2 1024 if (pcr[pc2[j] - 1]) { /* check bit that goes to ks[j] */
wolfSSL 0:1239e9b70ca2 1025 l= j % 6; /* mask it in if it's there */
wolfSSL 0:1239e9b70ca2 1026 ks[j/6] |= bytebit[l] >> 2;
wolfSSL 0:1239e9b70ca2 1027 }
wolfSSL 0:1239e9b70ca2 1028 }
wolfSSL 0:1239e9b70ca2 1029
wolfSSL 0:1239e9b70ca2 1030 /* Now convert to odd/even interleaved form for use in F */
wolfSSL 0:1239e9b70ca2 1031 out[2*i] = ((word32) ks[0] << 24)
wolfSSL 0:1239e9b70ca2 1032 | ((word32) ks[2] << 16)
wolfSSL 0:1239e9b70ca2 1033 | ((word32) ks[4] << 8)
wolfSSL 0:1239e9b70ca2 1034 | ((word32) ks[6]);
wolfSSL 0:1239e9b70ca2 1035
wolfSSL 0:1239e9b70ca2 1036 out[2*i + 1] = ((word32) ks[1] << 24)
wolfSSL 0:1239e9b70ca2 1037 | ((word32) ks[3] << 16)
wolfSSL 0:1239e9b70ca2 1038 | ((word32) ks[5] << 8)
wolfSSL 0:1239e9b70ca2 1039 | ((word32) ks[7]);
wolfSSL 0:1239e9b70ca2 1040 }
wolfSSL 0:1239e9b70ca2 1041
wolfSSL 0:1239e9b70ca2 1042 /* reverse key schedule order */
wolfSSL 0:1239e9b70ca2 1043 if (dir == DES_DECRYPTION) {
wolfSSL 0:1239e9b70ca2 1044 for (i = 0; i < 16; i += 2) {
wolfSSL 0:1239e9b70ca2 1045 word32 swap = out[i];
wolfSSL 0:1239e9b70ca2 1046 out[i] = out[DES_KS_SIZE - 2 - i];
wolfSSL 0:1239e9b70ca2 1047 out[DES_KS_SIZE - 2 - i] = swap;
wolfSSL 0:1239e9b70ca2 1048
wolfSSL 0:1239e9b70ca2 1049 swap = out[i + 1];
wolfSSL 0:1239e9b70ca2 1050 out[i + 1] = out[DES_KS_SIZE - 1 - i];
wolfSSL 0:1239e9b70ca2 1051 out[DES_KS_SIZE - 1 - i] = swap;
wolfSSL 0:1239e9b70ca2 1052 }
wolfSSL 0:1239e9b70ca2 1053 }
wolfSSL 0:1239e9b70ca2 1054
wolfSSL 0:1239e9b70ca2 1055 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 1056 XFREE(buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 1057 #endif
wolfSSL 0:1239e9b70ca2 1058 }
wolfSSL 0:1239e9b70ca2 1059
wolfSSL 0:1239e9b70ca2 1060 return 0;
wolfSSL 0:1239e9b70ca2 1061 }
wolfSSL 0:1239e9b70ca2 1062
wolfSSL 0:1239e9b70ca2 1063
wolfSSL 0:1239e9b70ca2 1064 static INLINE int Reverse(int dir)
wolfSSL 0:1239e9b70ca2 1065 {
wolfSSL 0:1239e9b70ca2 1066 return !dir;
wolfSSL 0:1239e9b70ca2 1067 }
wolfSSL 0:1239e9b70ca2 1068
wolfSSL 0:1239e9b70ca2 1069
wolfSSL 0:1239e9b70ca2 1070 int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 0:1239e9b70ca2 1071 {
wolfSSL 0:1239e9b70ca2 1072 Des_SetIV(des, iv);
wolfSSL 0:1239e9b70ca2 1073
wolfSSL 0:1239e9b70ca2 1074 return DesSetKey(key, dir, des->key);
wolfSSL 0:1239e9b70ca2 1075 }
wolfSSL 0:1239e9b70ca2 1076
wolfSSL 0:1239e9b70ca2 1077
wolfSSL 0:1239e9b70ca2 1078 int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
wolfSSL 0:1239e9b70ca2 1079 {
wolfSSL 0:1239e9b70ca2 1080 int ret;
wolfSSL 0:1239e9b70ca2 1081
wolfSSL 0:1239e9b70ca2 1082 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 1083 if (des->magic == CYASSL_3DES_CAVIUM_MAGIC)
wolfSSL 0:1239e9b70ca2 1084 return Des3_CaviumSetKey(des, key, iv);
wolfSSL 0:1239e9b70ca2 1085 #endif
wolfSSL 0:1239e9b70ca2 1086
wolfSSL 0:1239e9b70ca2 1087 ret = DesSetKey(key + (dir == DES_ENCRYPTION ? 0:16), dir, des->key[0]);
wolfSSL 0:1239e9b70ca2 1088 if (ret != 0)
wolfSSL 0:1239e9b70ca2 1089 return ret;
wolfSSL 0:1239e9b70ca2 1090
wolfSSL 0:1239e9b70ca2 1091 ret = DesSetKey(key + 8, Reverse(dir), des->key[1]);
wolfSSL 0:1239e9b70ca2 1092 if (ret != 0)
wolfSSL 0:1239e9b70ca2 1093 return ret;
wolfSSL 0:1239e9b70ca2 1094
wolfSSL 0:1239e9b70ca2 1095 ret = DesSetKey(key + (dir == DES_DECRYPTION ? 0:16), dir, des->key[2]);
wolfSSL 0:1239e9b70ca2 1096 if (ret != 0)
wolfSSL 0:1239e9b70ca2 1097 return ret;
wolfSSL 0:1239e9b70ca2 1098
wolfSSL 0:1239e9b70ca2 1099 return Des3_SetIV(des, iv);
wolfSSL 0:1239e9b70ca2 1100 }
wolfSSL 0:1239e9b70ca2 1101
wolfSSL 0:1239e9b70ca2 1102
wolfSSL 0:1239e9b70ca2 1103 static void DesRawProcessBlock(word32* lIn, word32* rIn, const word32* kptr)
wolfSSL 0:1239e9b70ca2 1104 {
wolfSSL 0:1239e9b70ca2 1105 word32 l = *lIn, r = *rIn, i;
wolfSSL 0:1239e9b70ca2 1106
wolfSSL 0:1239e9b70ca2 1107 for (i=0; i<8; i++)
wolfSSL 0:1239e9b70ca2 1108 {
wolfSSL 0:1239e9b70ca2 1109 word32 work = rotrFixed(r, 4U) ^ kptr[4*i+0];
wolfSSL 0:1239e9b70ca2 1110 l ^= Spbox[6][(work) & 0x3f]
wolfSSL 0:1239e9b70ca2 1111 ^ Spbox[4][(work >> 8) & 0x3f]
wolfSSL 0:1239e9b70ca2 1112 ^ Spbox[2][(work >> 16) & 0x3f]
wolfSSL 0:1239e9b70ca2 1113 ^ Spbox[0][(work >> 24) & 0x3f];
wolfSSL 0:1239e9b70ca2 1114 work = r ^ kptr[4*i+1];
wolfSSL 0:1239e9b70ca2 1115 l ^= Spbox[7][(work) & 0x3f]
wolfSSL 0:1239e9b70ca2 1116 ^ Spbox[5][(work >> 8) & 0x3f]
wolfSSL 0:1239e9b70ca2 1117 ^ Spbox[3][(work >> 16) & 0x3f]
wolfSSL 0:1239e9b70ca2 1118 ^ Spbox[1][(work >> 24) & 0x3f];
wolfSSL 0:1239e9b70ca2 1119
wolfSSL 0:1239e9b70ca2 1120 work = rotrFixed(l, 4U) ^ kptr[4*i+2];
wolfSSL 0:1239e9b70ca2 1121 r ^= Spbox[6][(work) & 0x3f]
wolfSSL 0:1239e9b70ca2 1122 ^ Spbox[4][(work >> 8) & 0x3f]
wolfSSL 0:1239e9b70ca2 1123 ^ Spbox[2][(work >> 16) & 0x3f]
wolfSSL 0:1239e9b70ca2 1124 ^ Spbox[0][(work >> 24) & 0x3f];
wolfSSL 0:1239e9b70ca2 1125 work = l ^ kptr[4*i+3];
wolfSSL 0:1239e9b70ca2 1126 r ^= Spbox[7][(work) & 0x3f]
wolfSSL 0:1239e9b70ca2 1127 ^ Spbox[5][(work >> 8) & 0x3f]
wolfSSL 0:1239e9b70ca2 1128 ^ Spbox[3][(work >> 16) & 0x3f]
wolfSSL 0:1239e9b70ca2 1129 ^ Spbox[1][(work >> 24) & 0x3f];
wolfSSL 0:1239e9b70ca2 1130 }
wolfSSL 0:1239e9b70ca2 1131
wolfSSL 0:1239e9b70ca2 1132 *lIn = l; *rIn = r;
wolfSSL 0:1239e9b70ca2 1133 }
wolfSSL 0:1239e9b70ca2 1134
wolfSSL 0:1239e9b70ca2 1135
wolfSSL 0:1239e9b70ca2 1136 static void DesProcessBlock(Des* des, const byte* in, byte* out)
wolfSSL 0:1239e9b70ca2 1137 {
wolfSSL 0:1239e9b70ca2 1138 word32 l, r;
wolfSSL 0:1239e9b70ca2 1139
wolfSSL 0:1239e9b70ca2 1140 XMEMCPY(&l, in, sizeof(l));
wolfSSL 0:1239e9b70ca2 1141 XMEMCPY(&r, in + sizeof(l), sizeof(r));
wolfSSL 0:1239e9b70ca2 1142 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 1143 l = ByteReverseWord32(l);
wolfSSL 0:1239e9b70ca2 1144 r = ByteReverseWord32(r);
wolfSSL 0:1239e9b70ca2 1145 #endif
wolfSSL 0:1239e9b70ca2 1146 IPERM(&l,&r);
wolfSSL 0:1239e9b70ca2 1147
wolfSSL 0:1239e9b70ca2 1148 DesRawProcessBlock(&l, &r, des->key);
wolfSSL 0:1239e9b70ca2 1149
wolfSSL 0:1239e9b70ca2 1150 FPERM(&l,&r);
wolfSSL 0:1239e9b70ca2 1151 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 1152 l = ByteReverseWord32(l);
wolfSSL 0:1239e9b70ca2 1153 r = ByteReverseWord32(r);
wolfSSL 0:1239e9b70ca2 1154 #endif
wolfSSL 0:1239e9b70ca2 1155 XMEMCPY(out, &r, sizeof(r));
wolfSSL 0:1239e9b70ca2 1156 XMEMCPY(out + sizeof(r), &l, sizeof(l));
wolfSSL 0:1239e9b70ca2 1157 }
wolfSSL 0:1239e9b70ca2 1158
wolfSSL 0:1239e9b70ca2 1159
wolfSSL 0:1239e9b70ca2 1160 static void Des3ProcessBlock(Des3* des, const byte* in, byte* out)
wolfSSL 0:1239e9b70ca2 1161 {
wolfSSL 0:1239e9b70ca2 1162 word32 l, r;
wolfSSL 0:1239e9b70ca2 1163
wolfSSL 0:1239e9b70ca2 1164 XMEMCPY(&l, in, sizeof(l));
wolfSSL 0:1239e9b70ca2 1165 XMEMCPY(&r, in + sizeof(l), sizeof(r));
wolfSSL 0:1239e9b70ca2 1166 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 1167 l = ByteReverseWord32(l);
wolfSSL 0:1239e9b70ca2 1168 r = ByteReverseWord32(r);
wolfSSL 0:1239e9b70ca2 1169 #endif
wolfSSL 0:1239e9b70ca2 1170 IPERM(&l,&r);
wolfSSL 0:1239e9b70ca2 1171
wolfSSL 0:1239e9b70ca2 1172 DesRawProcessBlock(&l, &r, des->key[0]);
wolfSSL 0:1239e9b70ca2 1173 DesRawProcessBlock(&r, &l, des->key[1]);
wolfSSL 0:1239e9b70ca2 1174 DesRawProcessBlock(&l, &r, des->key[2]);
wolfSSL 0:1239e9b70ca2 1175
wolfSSL 0:1239e9b70ca2 1176 FPERM(&l,&r);
wolfSSL 0:1239e9b70ca2 1177 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 1178 l = ByteReverseWord32(l);
wolfSSL 0:1239e9b70ca2 1179 r = ByteReverseWord32(r);
wolfSSL 0:1239e9b70ca2 1180 #endif
wolfSSL 0:1239e9b70ca2 1181 XMEMCPY(out, &r, sizeof(r));
wolfSSL 0:1239e9b70ca2 1182 XMEMCPY(out + sizeof(r), &l, sizeof(l));
wolfSSL 0:1239e9b70ca2 1183 }
wolfSSL 0:1239e9b70ca2 1184
wolfSSL 0:1239e9b70ca2 1185
wolfSSL 0:1239e9b70ca2 1186 void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 1187 {
wolfSSL 0:1239e9b70ca2 1188 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1189
wolfSSL 0:1239e9b70ca2 1190 while (blocks--) {
wolfSSL 0:1239e9b70ca2 1191 xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1192 DesProcessBlock(des, (byte*)des->reg, (byte*)des->reg);
wolfSSL 0:1239e9b70ca2 1193 XMEMCPY(out, des->reg, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1194
wolfSSL 0:1239e9b70ca2 1195 out += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1196 in += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1197 }
wolfSSL 0:1239e9b70ca2 1198 }
wolfSSL 0:1239e9b70ca2 1199
wolfSSL 0:1239e9b70ca2 1200
wolfSSL 0:1239e9b70ca2 1201 void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 1202 {
wolfSSL 0:1239e9b70ca2 1203 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1204 byte hold[DES_BLOCK_SIZE];
wolfSSL 0:1239e9b70ca2 1205
wolfSSL 0:1239e9b70ca2 1206 while (blocks--) {
wolfSSL 0:1239e9b70ca2 1207 XMEMCPY(des->tmp, in, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1208 DesProcessBlock(des, (byte*)des->tmp, out);
wolfSSL 0:1239e9b70ca2 1209 xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1210
wolfSSL 0:1239e9b70ca2 1211 XMEMCPY(hold, des->reg, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1212 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1213 XMEMCPY(des->tmp, hold, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1214
wolfSSL 0:1239e9b70ca2 1215 out += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1216 in += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1217 }
wolfSSL 0:1239e9b70ca2 1218 }
wolfSSL 0:1239e9b70ca2 1219
wolfSSL 0:1239e9b70ca2 1220
wolfSSL 0:1239e9b70ca2 1221 int Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 1222 {
wolfSSL 0:1239e9b70ca2 1223 word32 blocks;
wolfSSL 0:1239e9b70ca2 1224
wolfSSL 0:1239e9b70ca2 1225 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 1226 if (des->magic == CYASSL_3DES_CAVIUM_MAGIC)
wolfSSL 0:1239e9b70ca2 1227 return Des3_CaviumCbcEncrypt(des, out, in, sz);
wolfSSL 0:1239e9b70ca2 1228 #endif
wolfSSL 0:1239e9b70ca2 1229
wolfSSL 0:1239e9b70ca2 1230 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1231 while (blocks--) {
wolfSSL 0:1239e9b70ca2 1232 xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1233 Des3ProcessBlock(des, (byte*)des->reg, (byte*)des->reg);
wolfSSL 0:1239e9b70ca2 1234 XMEMCPY(out, des->reg, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1235
wolfSSL 0:1239e9b70ca2 1236 out += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1237 in += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1238 }
wolfSSL 0:1239e9b70ca2 1239 return 0;
wolfSSL 0:1239e9b70ca2 1240 }
wolfSSL 0:1239e9b70ca2 1241
wolfSSL 0:1239e9b70ca2 1242
wolfSSL 0:1239e9b70ca2 1243 int Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 1244 {
wolfSSL 0:1239e9b70ca2 1245 word32 blocks;
wolfSSL 0:1239e9b70ca2 1246
wolfSSL 0:1239e9b70ca2 1247 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 1248 if (des->magic == CYASSL_3DES_CAVIUM_MAGIC)
wolfSSL 0:1239e9b70ca2 1249 return Des3_CaviumCbcDecrypt(des, out, in, sz);
wolfSSL 0:1239e9b70ca2 1250 #endif
wolfSSL 0:1239e9b70ca2 1251
wolfSSL 0:1239e9b70ca2 1252 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1253 while (blocks--) {
wolfSSL 0:1239e9b70ca2 1254 XMEMCPY(des->tmp, in, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1255 Des3ProcessBlock(des, (byte*)des->tmp, out);
wolfSSL 0:1239e9b70ca2 1256 xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1257 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1258
wolfSSL 0:1239e9b70ca2 1259 out += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1260 in += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1261 }
wolfSSL 0:1239e9b70ca2 1262 return 0;
wolfSSL 0:1239e9b70ca2 1263 }
wolfSSL 0:1239e9b70ca2 1264
wolfSSL 0:1239e9b70ca2 1265 #ifdef CYASSL_DES_ECB
wolfSSL 0:1239e9b70ca2 1266
wolfSSL 0:1239e9b70ca2 1267 /* One block, compatibility only */
wolfSSL 0:1239e9b70ca2 1268 void Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 0:1239e9b70ca2 1269 {
wolfSSL 0:1239e9b70ca2 1270 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1271
wolfSSL 0:1239e9b70ca2 1272 while (blocks--) {
wolfSSL 0:1239e9b70ca2 1273 DesProcessBlock(des, in, out);
wolfSSL 0:1239e9b70ca2 1274
wolfSSL 0:1239e9b70ca2 1275 out += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1276 in += DES_BLOCK_SIZE;
wolfSSL 0:1239e9b70ca2 1277 }
wolfSSL 0:1239e9b70ca2 1278 }
wolfSSL 0:1239e9b70ca2 1279
wolfSSL 0:1239e9b70ca2 1280 #endif /* CYASSL_DES_ECB */
wolfSSL 0:1239e9b70ca2 1281
wolfSSL 0:1239e9b70ca2 1282 #endif /* STM32F2_CRYPTO */
wolfSSL 0:1239e9b70ca2 1283
wolfSSL 0:1239e9b70ca2 1284 void Des_SetIV(Des* des, const byte* iv)
wolfSSL 0:1239e9b70ca2 1285 {
wolfSSL 0:1239e9b70ca2 1286 if (des && iv)
wolfSSL 0:1239e9b70ca2 1287 XMEMCPY(des->reg, iv, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1288 else if (des)
wolfSSL 0:1239e9b70ca2 1289 XMEMSET(des->reg, 0, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1290 }
wolfSSL 0:1239e9b70ca2 1291
wolfSSL 0:1239e9b70ca2 1292
wolfSSL 0:1239e9b70ca2 1293 int Des3_SetIV(Des3* des, const byte* iv)
wolfSSL 0:1239e9b70ca2 1294 {
wolfSSL 0:1239e9b70ca2 1295 if (des && iv)
wolfSSL 0:1239e9b70ca2 1296 XMEMCPY(des->reg, iv, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1297 else if (des)
wolfSSL 0:1239e9b70ca2 1298 XMEMSET(des->reg, 0, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1299
wolfSSL 0:1239e9b70ca2 1300 return 0;
wolfSSL 0:1239e9b70ca2 1301 }
wolfSSL 0:1239e9b70ca2 1302
wolfSSL 0:1239e9b70ca2 1303
wolfSSL 0:1239e9b70ca2 1304 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 1305
wolfSSL 0:1239e9b70ca2 1306 #include <cyassl/ctaocrypt/logging.h>
wolfSSL 0:1239e9b70ca2 1307 #include "cavium_common.h"
wolfSSL 0:1239e9b70ca2 1308
wolfSSL 0:1239e9b70ca2 1309 /* Initiliaze Des3 for use with Nitrox device */
wolfSSL 0:1239e9b70ca2 1310 int Des3_InitCavium(Des3* des3, int devId)
wolfSSL 0:1239e9b70ca2 1311 {
wolfSSL 0:1239e9b70ca2 1312 if (des3 == NULL)
wolfSSL 0:1239e9b70ca2 1313 return -1;
wolfSSL 0:1239e9b70ca2 1314
wolfSSL 0:1239e9b70ca2 1315 if (CspAllocContext(CONTEXT_SSL, &des3->contextHandle, devId) != 0)
wolfSSL 0:1239e9b70ca2 1316 return -1;
wolfSSL 0:1239e9b70ca2 1317
wolfSSL 0:1239e9b70ca2 1318 des3->devId = devId;
wolfSSL 0:1239e9b70ca2 1319 des3->magic = CYASSL_3DES_CAVIUM_MAGIC;
wolfSSL 0:1239e9b70ca2 1320
wolfSSL 0:1239e9b70ca2 1321 return 0;
wolfSSL 0:1239e9b70ca2 1322 }
wolfSSL 0:1239e9b70ca2 1323
wolfSSL 0:1239e9b70ca2 1324
wolfSSL 0:1239e9b70ca2 1325 /* Free Des3 from use with Nitrox device */
wolfSSL 0:1239e9b70ca2 1326 void Des3_FreeCavium(Des3* des3)
wolfSSL 0:1239e9b70ca2 1327 {
wolfSSL 0:1239e9b70ca2 1328 if (des3 == NULL)
wolfSSL 0:1239e9b70ca2 1329 return;
wolfSSL 0:1239e9b70ca2 1330
wolfSSL 0:1239e9b70ca2 1331 if (des3->magic != CYASSL_3DES_CAVIUM_MAGIC)
wolfSSL 0:1239e9b70ca2 1332 return;
wolfSSL 0:1239e9b70ca2 1333
wolfSSL 0:1239e9b70ca2 1334 CspFreeContext(CONTEXT_SSL, des3->contextHandle, des3->devId);
wolfSSL 0:1239e9b70ca2 1335 des3->magic = 0;
wolfSSL 0:1239e9b70ca2 1336 }
wolfSSL 0:1239e9b70ca2 1337
wolfSSL 0:1239e9b70ca2 1338
wolfSSL 0:1239e9b70ca2 1339 static int Des3_CaviumSetKey(Des3* des3, const byte* key, const byte* iv)
wolfSSL 0:1239e9b70ca2 1340 {
wolfSSL 0:1239e9b70ca2 1341 if (des3 == NULL)
wolfSSL 0:1239e9b70ca2 1342 return -1;
wolfSSL 0:1239e9b70ca2 1343
wolfSSL 0:1239e9b70ca2 1344 /* key[0] holds key, iv in reg */
wolfSSL 0:1239e9b70ca2 1345 XMEMCPY(des3->key[0], key, DES_BLOCK_SIZE*3);
wolfSSL 0:1239e9b70ca2 1346
wolfSSL 0:1239e9b70ca2 1347 return Des3_SetIV(des3, iv);
wolfSSL 0:1239e9b70ca2 1348 }
wolfSSL 0:1239e9b70ca2 1349
wolfSSL 0:1239e9b70ca2 1350
wolfSSL 0:1239e9b70ca2 1351 static int Des3_CaviumCbcEncrypt(Des3* des3, byte* out, const byte* in,
wolfSSL 0:1239e9b70ca2 1352 word32 length)
wolfSSL 0:1239e9b70ca2 1353 {
wolfSSL 0:1239e9b70ca2 1354 word offset = 0;
wolfSSL 0:1239e9b70ca2 1355 word32 requestId;
wolfSSL 0:1239e9b70ca2 1356
wolfSSL 0:1239e9b70ca2 1357 while (length > CYASSL_MAX_16BIT) {
wolfSSL 0:1239e9b70ca2 1358 word16 slen = (word16)CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 1359 if (CspEncrypt3Des(CAVIUM_BLOCKING, des3->contextHandle,
wolfSSL 0:1239e9b70ca2 1360 CAVIUM_NO_UPDATE, slen, (byte*)in + offset,
wolfSSL 0:1239e9b70ca2 1361 out + offset, (byte*)des3->reg, (byte*)des3->key[0],
wolfSSL 0:1239e9b70ca2 1362 &requestId, des3->devId) != 0) {
wolfSSL 0:1239e9b70ca2 1363 CYASSL_MSG("Bad Cavium 3DES Cbc Encrypt");
wolfSSL 0:1239e9b70ca2 1364 return -1;
wolfSSL 0:1239e9b70ca2 1365 }
wolfSSL 0:1239e9b70ca2 1366 length -= CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 1367 offset += CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 1368 XMEMCPY(des3->reg, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1369 }
wolfSSL 0:1239e9b70ca2 1370 if (length) {
wolfSSL 0:1239e9b70ca2 1371 word16 slen = (word16)length;
wolfSSL 0:1239e9b70ca2 1372
wolfSSL 0:1239e9b70ca2 1373 if (CspEncrypt3Des(CAVIUM_BLOCKING, des3->contextHandle,
wolfSSL 0:1239e9b70ca2 1374 CAVIUM_NO_UPDATE, slen, (byte*)in + offset,
wolfSSL 0:1239e9b70ca2 1375 out + offset, (byte*)des3->reg, (byte*)des3->key[0],
wolfSSL 0:1239e9b70ca2 1376 &requestId, des3->devId) != 0) {
wolfSSL 0:1239e9b70ca2 1377 CYASSL_MSG("Bad Cavium 3DES Cbc Encrypt");
wolfSSL 0:1239e9b70ca2 1378 return -1;
wolfSSL 0:1239e9b70ca2 1379 }
wolfSSL 0:1239e9b70ca2 1380 XMEMCPY(des3->reg, out+offset+length - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1381 }
wolfSSL 0:1239e9b70ca2 1382 return 0;
wolfSSL 0:1239e9b70ca2 1383 }
wolfSSL 0:1239e9b70ca2 1384
wolfSSL 0:1239e9b70ca2 1385 static int Des3_CaviumCbcDecrypt(Des3* des3, byte* out, const byte* in,
wolfSSL 0:1239e9b70ca2 1386 word32 length)
wolfSSL 0:1239e9b70ca2 1387 {
wolfSSL 0:1239e9b70ca2 1388 word32 requestId;
wolfSSL 0:1239e9b70ca2 1389 word offset = 0;
wolfSSL 0:1239e9b70ca2 1390
wolfSSL 0:1239e9b70ca2 1391 while (length > CYASSL_MAX_16BIT) {
wolfSSL 0:1239e9b70ca2 1392 word16 slen = (word16)CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 1393 XMEMCPY(des3->tmp, in + offset + slen - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1394 if (CspDecrypt3Des(CAVIUM_BLOCKING, des3->contextHandle,
wolfSSL 0:1239e9b70ca2 1395 CAVIUM_NO_UPDATE, slen, (byte*)in+offset, out+offset,
wolfSSL 0:1239e9b70ca2 1396 (byte*)des3->reg, (byte*)des3->key[0], &requestId,
wolfSSL 0:1239e9b70ca2 1397 des3->devId) != 0) {
wolfSSL 0:1239e9b70ca2 1398 CYASSL_MSG("Bad Cavium 3Des Decrypt");
wolfSSL 0:1239e9b70ca2 1399 return -1;
wolfSSL 0:1239e9b70ca2 1400 }
wolfSSL 0:1239e9b70ca2 1401 length -= CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 1402 offset += CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 1403 XMEMCPY(des3->reg, des3->tmp, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1404 }
wolfSSL 0:1239e9b70ca2 1405 if (length) {
wolfSSL 0:1239e9b70ca2 1406 word16 slen = (word16)length;
wolfSSL 0:1239e9b70ca2 1407 XMEMCPY(des3->tmp, in + offset + slen - DES_BLOCK_SIZE,DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1408 if (CspDecrypt3Des(CAVIUM_BLOCKING, des3->contextHandle,
wolfSSL 0:1239e9b70ca2 1409 CAVIUM_NO_UPDATE, slen, (byte*)in+offset, out+offset,
wolfSSL 0:1239e9b70ca2 1410 (byte*)des3->reg, (byte*)des3->key[0], &requestId,
wolfSSL 0:1239e9b70ca2 1411 des3->devId) != 0) {
wolfSSL 0:1239e9b70ca2 1412 CYASSL_MSG("Bad Cavium 3Des Decrypt");
wolfSSL 0:1239e9b70ca2 1413 return -1;
wolfSSL 0:1239e9b70ca2 1414 }
wolfSSL 0:1239e9b70ca2 1415 XMEMCPY(des3->reg, des3->tmp, DES_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 1416 }
wolfSSL 0:1239e9b70ca2 1417 return 0;
wolfSSL 0:1239e9b70ca2 1418 }
wolfSSL 0:1239e9b70ca2 1419
wolfSSL 0:1239e9b70ca2 1420 #endif /* HAVE_CAVIUM */
wolfSSL 0:1239e9b70ca2 1421
wolfSSL 0:1239e9b70ca2 1422 #endif /* NO_DES3 */
wolfSSL 0:1239e9b70ca2 1423