Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
sPymbed
Date:
Tue Nov 19 14:32:16 2019 +0000
Revision:
16:048e5e270a58
Parent:
15:117db924cf7c
working ssl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* des3.c
wolfSSL 15:117db924cf7c 2 *
wolfSSL 15:117db924cf7c 3 * Copyright (C) 2006-2017 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22
wolfSSL 15:117db924cf7c 23 #ifdef HAVE_CONFIG_H
wolfSSL 15:117db924cf7c 24 #include <config.h>
wolfSSL 15:117db924cf7c 25 #endif
wolfSSL 15:117db924cf7c 26
wolfSSL 15:117db924cf7c 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 15:117db924cf7c 28 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 15:117db924cf7c 29 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 15:117db924cf7c 30
wolfSSL 15:117db924cf7c 31
wolfSSL 15:117db924cf7c 32 #ifndef NO_DES3
wolfSSL 15:117db924cf7c 33
wolfSSL 15:117db924cf7c 34 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 35 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
wolfSSL 15:117db924cf7c 36
wolfSSL 15:117db924cf7c 37 /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
wolfSSL 15:117db924cf7c 38 #define FIPS_NO_WRAPPERS
wolfSSL 15:117db924cf7c 39
wolfSSL 15:117db924cf7c 40 #ifdef USE_WINDOWS_API
wolfSSL 15:117db924cf7c 41 #pragma code_seg(".fipsA$i")
wolfSSL 15:117db924cf7c 42 #pragma const_seg(".fipsB$i")
wolfSSL 15:117db924cf7c 43 #endif
wolfSSL 15:117db924cf7c 44 #endif
wolfSSL 15:117db924cf7c 45
wolfSSL 15:117db924cf7c 46 #include <wolfssl/wolfcrypt/des3.h>
wolfSSL 15:117db924cf7c 47
wolfSSL 15:117db924cf7c 48 /* fips wrapper calls, user can call direct */
wolfSSL 15:117db924cf7c 49 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 50 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
wolfSSL 15:117db924cf7c 51
wolfSSL 15:117db924cf7c 52 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 53 {
wolfSSL 15:117db924cf7c 54 return Des_SetKey(des, key, iv, dir);
wolfSSL 15:117db924cf7c 55 }
wolfSSL 15:117db924cf7c 56 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 57 {
wolfSSL 15:117db924cf7c 58 if (des == NULL || key == NULL || dir < 0) {
wolfSSL 15:117db924cf7c 59 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 60 }
wolfSSL 15:117db924cf7c 61
wolfSSL 15:117db924cf7c 62 return Des3_SetKey_fips(des, key, iv, dir);
wolfSSL 15:117db924cf7c 63 }
wolfSSL 15:117db924cf7c 64 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 65 {
wolfSSL 15:117db924cf7c 66 return Des_CbcEncrypt(des, out, in, sz);
wolfSSL 15:117db924cf7c 67 }
wolfSSL 15:117db924cf7c 68 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 69 {
wolfSSL 15:117db924cf7c 70 return Des_CbcDecrypt(des, out, in, sz);
wolfSSL 15:117db924cf7c 71 }
wolfSSL 15:117db924cf7c 72 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 73 {
wolfSSL 15:117db924cf7c 74 if (des == NULL || out == NULL || in == NULL) {
wolfSSL 15:117db924cf7c 75 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 76 }
wolfSSL 15:117db924cf7c 77 return Des3_CbcEncrypt_fips(des, out, in, sz);
wolfSSL 15:117db924cf7c 78 }
wolfSSL 15:117db924cf7c 79 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 80 {
wolfSSL 15:117db924cf7c 81 if (des == NULL || out == NULL || in == NULL) {
wolfSSL 15:117db924cf7c 82 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 83 }
wolfSSL 15:117db924cf7c 84 return Des3_CbcDecrypt_fips(des, out, in, sz);
wolfSSL 15:117db924cf7c 85 }
wolfSSL 15:117db924cf7c 86
wolfSSL 15:117db924cf7c 87 #ifdef WOLFSSL_DES_ECB
wolfSSL 15:117db924cf7c 88 /* One block, compatibility only */
wolfSSL 15:117db924cf7c 89 int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 90 {
wolfSSL 15:117db924cf7c 91 return Des_EcbEncrypt(des, out, in, sz);
wolfSSL 15:117db924cf7c 92 }
wolfSSL 15:117db924cf7c 93 int wc_Des3_EcbEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 94 {
wolfSSL 15:117db924cf7c 95 return Des3_EcbEncrypt(des, out, in, sz);
wolfSSL 15:117db924cf7c 96 }
wolfSSL 15:117db924cf7c 97 #endif /* WOLFSSL_DES_ECB */
wolfSSL 15:117db924cf7c 98
wolfSSL 15:117db924cf7c 99 void wc_Des_SetIV(Des* des, const byte* iv)
wolfSSL 15:117db924cf7c 100 {
wolfSSL 15:117db924cf7c 101 Des_SetIV(des, iv);
wolfSSL 15:117db924cf7c 102 }
wolfSSL 15:117db924cf7c 103 int wc_Des3_SetIV(Des3* des, const byte* iv)
wolfSSL 15:117db924cf7c 104 {
wolfSSL 15:117db924cf7c 105 return Des3_SetIV_fips(des, iv);
wolfSSL 15:117db924cf7c 106 }
wolfSSL 15:117db924cf7c 107
wolfSSL 15:117db924cf7c 108 int wc_Des3Init(Des3* des3, void* heap, int devId)
wolfSSL 15:117db924cf7c 109 {
wolfSSL 15:117db924cf7c 110 (void)des3;
wolfSSL 15:117db924cf7c 111 (void)heap;
wolfSSL 15:117db924cf7c 112 (void)devId;
wolfSSL 15:117db924cf7c 113 /* FIPS doesn't support:
wolfSSL 15:117db924cf7c 114 return Des3Init(des3, heap, devId); */
wolfSSL 15:117db924cf7c 115 return 0;
wolfSSL 15:117db924cf7c 116 }
wolfSSL 15:117db924cf7c 117 void wc_Des3Free(Des3* des3)
wolfSSL 15:117db924cf7c 118 {
wolfSSL 15:117db924cf7c 119 (void)des3;
wolfSSL 15:117db924cf7c 120 /* FIPS doesn't support:
wolfSSL 15:117db924cf7c 121 Des3Free(des3); */
wolfSSL 15:117db924cf7c 122 }
wolfSSL 15:117db924cf7c 123
wolfSSL 15:117db924cf7c 124 #else /* else build without fips, or for FIPS v2 */
wolfSSL 15:117db924cf7c 125
wolfSSL 15:117db924cf7c 126
wolfSSL 15:117db924cf7c 127 #if defined(WOLFSSL_TI_CRYPT)
wolfSSL 15:117db924cf7c 128 #include <wolfcrypt/src/port/ti/ti-des3.c>
wolfSSL 15:117db924cf7c 129 #else
wolfSSL 15:117db924cf7c 130
wolfSSL 15:117db924cf7c 131
wolfSSL 15:117db924cf7c 132 #ifdef NO_INLINE
wolfSSL 15:117db924cf7c 133 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 15:117db924cf7c 134 #else
wolfSSL 15:117db924cf7c 135 #define WOLFSSL_MISC_INCLUDED
wolfSSL 15:117db924cf7c 136 #include <wolfcrypt/src/misc.c>
wolfSSL 15:117db924cf7c 137 #endif
wolfSSL 15:117db924cf7c 138
wolfSSL 15:117db924cf7c 139
wolfSSL 15:117db924cf7c 140 /* Hardware Acceleration */
wolfSSL 15:117db924cf7c 141 #if defined(STM32_CRYPTO)
wolfSSL 15:117db924cf7c 142
wolfSSL 15:117db924cf7c 143 /*
wolfSSL 15:117db924cf7c 144 * STM32F2/F4 hardware DES/3DES support through the standard
wolfSSL 15:117db924cf7c 145 * peripheral library. (See note in README).
wolfSSL 15:117db924cf7c 146 */
wolfSSL 15:117db924cf7c 147
wolfSSL 15:117db924cf7c 148 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 149 {
wolfSSL 15:117db924cf7c 150 word32 *dkey = des->key;
wolfSSL 15:117db924cf7c 151
wolfSSL 15:117db924cf7c 152 (void)dir;
wolfSSL 15:117db924cf7c 153
wolfSSL 15:117db924cf7c 154 XMEMCPY(dkey, key, 8);
wolfSSL 15:117db924cf7c 155 #ifndef WOLFSSL_STM32_CUBEMX
wolfSSL 15:117db924cf7c 156 ByteReverseWords(dkey, dkey, 8);
wolfSSL 15:117db924cf7c 157 #endif
wolfSSL 15:117db924cf7c 158
wolfSSL 15:117db924cf7c 159 wc_Des_SetIV(des, iv);
wolfSSL 15:117db924cf7c 160
wolfSSL 15:117db924cf7c 161 return 0;
wolfSSL 15:117db924cf7c 162 }
wolfSSL 15:117db924cf7c 163
wolfSSL 15:117db924cf7c 164 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 165 {
wolfSSL 15:117db924cf7c 166 #ifndef WOLFSSL_STM32_CUBEMX
wolfSSL 15:117db924cf7c 167 word32 *dkey1 = des->key[0];
wolfSSL 15:117db924cf7c 168 word32 *dkey2 = des->key[1];
wolfSSL 15:117db924cf7c 169 word32 *dkey3 = des->key[2];
wolfSSL 15:117db924cf7c 170
wolfSSL 15:117db924cf7c 171 (void)dir;
wolfSSL 15:117db924cf7c 172
wolfSSL 15:117db924cf7c 173 XMEMCPY(dkey1, key, 8); /* set key 1 */
wolfSSL 15:117db924cf7c 174 XMEMCPY(dkey2, key + 8, 8); /* set key 2 */
wolfSSL 15:117db924cf7c 175 XMEMCPY(dkey3, key + 16, 8); /* set key 3 */
wolfSSL 15:117db924cf7c 176
wolfSSL 15:117db924cf7c 177 ByteReverseWords(dkey1, dkey1, 8);
wolfSSL 15:117db924cf7c 178 ByteReverseWords(dkey2, dkey2, 8);
wolfSSL 15:117db924cf7c 179 ByteReverseWords(dkey3, dkey3, 8);
wolfSSL 15:117db924cf7c 180 #else
wolfSSL 15:117db924cf7c 181 (void)dir;
wolfSSL 15:117db924cf7c 182 XMEMCPY(des->key[0], key, DES3_KEYLEN); /* CUBEMX wants keys in sequential memory */
wolfSSL 15:117db924cf7c 183 #endif
wolfSSL 15:117db924cf7c 184
wolfSSL 15:117db924cf7c 185 return wc_Des3_SetIV(des, iv);
wolfSSL 15:117db924cf7c 186 }
wolfSSL 15:117db924cf7c 187
wolfSSL 15:117db924cf7c 188 static void DesCrypt(Des* des, byte* out, const byte* in, word32 sz,
wolfSSL 15:117db924cf7c 189 int dir, int mode)
wolfSSL 15:117db924cf7c 190 {
wolfSSL 15:117db924cf7c 191 #ifdef WOLFSSL_STM32_CUBEMX
wolfSSL 15:117db924cf7c 192 CRYP_HandleTypeDef hcryp;
wolfSSL 15:117db924cf7c 193
wolfSSL 15:117db924cf7c 194 XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef));
wolfSSL 15:117db924cf7c 195 hcryp.Instance = CRYP;
wolfSSL 15:117db924cf7c 196 hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
wolfSSL 15:117db924cf7c 197 hcryp.Init.DataType = CRYP_DATATYPE_8B;
wolfSSL 15:117db924cf7c 198 hcryp.Init.pKey = (uint8_t*)des->key;
wolfSSL 15:117db924cf7c 199 hcryp.Init.pInitVect = (uint8_t*)des->reg;
wolfSSL 15:117db924cf7c 200
wolfSSL 15:117db924cf7c 201 HAL_CRYP_Init(&hcryp);
wolfSSL 15:117db924cf7c 202
wolfSSL 15:117db924cf7c 203 while (sz > 0)
wolfSSL 15:117db924cf7c 204 {
wolfSSL 15:117db924cf7c 205 /* if input and output same will overwrite input iv */
wolfSSL 15:117db924cf7c 206 XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 207
wolfSSL 15:117db924cf7c 208 if (mode == DES_CBC) {
wolfSSL 15:117db924cf7c 209 if (dir == DES_ENCRYPTION) {
wolfSSL 15:117db924cf7c 210 HAL_CRYP_DESCBC_Encrypt(&hcryp, (uint8_t*)in,
wolfSSL 15:117db924cf7c 211 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
wolfSSL 15:117db924cf7c 212 }
wolfSSL 15:117db924cf7c 213 else {
wolfSSL 15:117db924cf7c 214 HAL_CRYP_DESCBC_Decrypt(&hcryp, (uint8_t*)in,
wolfSSL 15:117db924cf7c 215 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
wolfSSL 15:117db924cf7c 216 }
wolfSSL 15:117db924cf7c 217 }
wolfSSL 15:117db924cf7c 218 else {
wolfSSL 15:117db924cf7c 219 if (dir == DES_ENCRYPTION) {
wolfSSL 15:117db924cf7c 220 HAL_CRYP_DESECB_Encrypt(&hcryp, (uint8_t*)in,
wolfSSL 15:117db924cf7c 221 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
wolfSSL 15:117db924cf7c 222 }
wolfSSL 15:117db924cf7c 223 else {
wolfSSL 15:117db924cf7c 224 HAL_CRYP_DESECB_Decrypt(&hcryp, (uint8_t*)in,
wolfSSL 15:117db924cf7c 225 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
wolfSSL 15:117db924cf7c 226 }
wolfSSL 15:117db924cf7c 227 }
wolfSSL 15:117db924cf7c 228
wolfSSL 15:117db924cf7c 229 /* store iv for next call */
wolfSSL 15:117db924cf7c 230 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 231
wolfSSL 15:117db924cf7c 232 sz -= DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 233 in += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 234 out += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 235 }
wolfSSL 15:117db924cf7c 236
wolfSSL 15:117db924cf7c 237 HAL_CRYP_DeInit(&hcryp);
wolfSSL 15:117db924cf7c 238 #else
wolfSSL 15:117db924cf7c 239 word32 *dkey, *iv;
wolfSSL 15:117db924cf7c 240 CRYP_InitTypeDef DES_CRYP_InitStructure;
wolfSSL 15:117db924cf7c 241 CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure;
wolfSSL 15:117db924cf7c 242 CRYP_IVInitTypeDef DES_CRYP_IVInitStructure;
wolfSSL 15:117db924cf7c 243
wolfSSL 15:117db924cf7c 244 dkey = des->key;
wolfSSL 15:117db924cf7c 245 iv = des->reg;
wolfSSL 15:117db924cf7c 246
wolfSSL 15:117db924cf7c 247 /* crypto structure initialization */
wolfSSL 15:117db924cf7c 248 CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure);
wolfSSL 15:117db924cf7c 249 CRYP_StructInit(&DES_CRYP_InitStructure);
wolfSSL 15:117db924cf7c 250 CRYP_IVStructInit(&DES_CRYP_IVInitStructure);
wolfSSL 15:117db924cf7c 251
wolfSSL 15:117db924cf7c 252 /* reset registers to their default values */
wolfSSL 15:117db924cf7c 253 CRYP_DeInit();
wolfSSL 15:117db924cf7c 254
wolfSSL 15:117db924cf7c 255 /* set direction, mode, and datatype */
wolfSSL 15:117db924cf7c 256 if (dir == DES_ENCRYPTION) {
wolfSSL 15:117db924cf7c 257 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
wolfSSL 15:117db924cf7c 258 } else { /* DES_DECRYPTION */
wolfSSL 15:117db924cf7c 259 DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
wolfSSL 15:117db924cf7c 260 }
wolfSSL 15:117db924cf7c 261
wolfSSL 15:117db924cf7c 262 if (mode == DES_CBC) {
wolfSSL 15:117db924cf7c 263 DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC;
wolfSSL 15:117db924cf7c 264 } else { /* DES_ECB */
wolfSSL 15:117db924cf7c 265 DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB;
wolfSSL 15:117db924cf7c 266 }
wolfSSL 15:117db924cf7c 267
wolfSSL 15:117db924cf7c 268 DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 15:117db924cf7c 269 CRYP_Init(&DES_CRYP_InitStructure);
wolfSSL 15:117db924cf7c 270
wolfSSL 15:117db924cf7c 271 /* load key into correct registers */
wolfSSL 15:117db924cf7c 272 DES_CRYP_KeyInitStructure.CRYP_Key1Left = dkey[0];
wolfSSL 15:117db924cf7c 273 DES_CRYP_KeyInitStructure.CRYP_Key1Right = dkey[1];
wolfSSL 15:117db924cf7c 274 CRYP_KeyInit(&DES_CRYP_KeyInitStructure);
wolfSSL 15:117db924cf7c 275
wolfSSL 15:117db924cf7c 276 /* set iv */
wolfSSL 15:117db924cf7c 277 ByteReverseWords(iv, iv, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 278 DES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
wolfSSL 15:117db924cf7c 279 DES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
wolfSSL 15:117db924cf7c 280 CRYP_IVInit(&DES_CRYP_IVInitStructure);
wolfSSL 15:117db924cf7c 281
wolfSSL 15:117db924cf7c 282 /* enable crypto processor */
wolfSSL 15:117db924cf7c 283 CRYP_Cmd(ENABLE);
wolfSSL 15:117db924cf7c 284
wolfSSL 15:117db924cf7c 285 while (sz > 0)
wolfSSL 15:117db924cf7c 286 {
wolfSSL 15:117db924cf7c 287 /* flush IN/OUT FIFOs */
wolfSSL 15:117db924cf7c 288 CRYP_FIFOFlush();
wolfSSL 15:117db924cf7c 289
wolfSSL 15:117db924cf7c 290 /* if input and output same will overwrite input iv */
wolfSSL 15:117db924cf7c 291 XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 292
wolfSSL 15:117db924cf7c 293 CRYP_DataIn(*(uint32_t*)&in[0]);
wolfSSL 15:117db924cf7c 294 CRYP_DataIn(*(uint32_t*)&in[4]);
wolfSSL 15:117db924cf7c 295
wolfSSL 15:117db924cf7c 296 /* wait until the complete message has been processed */
wolfSSL 15:117db924cf7c 297 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 15:117db924cf7c 298
wolfSSL 15:117db924cf7c 299 *(uint32_t*)&out[0] = CRYP_DataOut();
wolfSSL 15:117db924cf7c 300 *(uint32_t*)&out[4] = CRYP_DataOut();
wolfSSL 15:117db924cf7c 301
wolfSSL 15:117db924cf7c 302 /* store iv for next call */
wolfSSL 15:117db924cf7c 303 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 304
wolfSSL 15:117db924cf7c 305 sz -= DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 306 in += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 307 out += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 308 }
wolfSSL 15:117db924cf7c 309
wolfSSL 15:117db924cf7c 310 /* disable crypto processor */
wolfSSL 15:117db924cf7c 311 CRYP_Cmd(DISABLE);
wolfSSL 15:117db924cf7c 312 #endif /* WOLFSSL_STM32_CUBEMX */
wolfSSL 15:117db924cf7c 313 }
wolfSSL 15:117db924cf7c 314
wolfSSL 15:117db924cf7c 315 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 316 {
wolfSSL 15:117db924cf7c 317 DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_CBC);
wolfSSL 15:117db924cf7c 318 return 0;
wolfSSL 15:117db924cf7c 319 }
wolfSSL 15:117db924cf7c 320
wolfSSL 15:117db924cf7c 321 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 322 {
wolfSSL 15:117db924cf7c 323 DesCrypt(des, out, in, sz, DES_DECRYPTION, DES_CBC);
wolfSSL 15:117db924cf7c 324 return 0;
wolfSSL 15:117db924cf7c 325 }
wolfSSL 15:117db924cf7c 326
wolfSSL 15:117db924cf7c 327 int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 328 {
wolfSSL 15:117db924cf7c 329 DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_ECB);
wolfSSL 15:117db924cf7c 330 return 0;
wolfSSL 15:117db924cf7c 331 }
wolfSSL 15:117db924cf7c 332
wolfSSL 15:117db924cf7c 333 static void Des3Crypt(Des3* des, byte* out, const byte* in, word32 sz,
wolfSSL 15:117db924cf7c 334 int dir)
wolfSSL 15:117db924cf7c 335 {
wolfSSL 15:117db924cf7c 336 #ifdef WOLFSSL_STM32_CUBEMX
wolfSSL 15:117db924cf7c 337 CRYP_HandleTypeDef hcryp;
wolfSSL 15:117db924cf7c 338
wolfSSL 15:117db924cf7c 339 XMEMSET(&hcryp, 0, sizeof(CRYP_HandleTypeDef));
wolfSSL 15:117db924cf7c 340 hcryp.Instance = CRYP;
wolfSSL 15:117db924cf7c 341 hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
wolfSSL 15:117db924cf7c 342 hcryp.Init.DataType = CRYP_DATATYPE_8B;
wolfSSL 15:117db924cf7c 343 hcryp.Init.pKey = (uint8_t*)des->key;
wolfSSL 15:117db924cf7c 344 hcryp.Init.pInitVect = (uint8_t*)des->reg;
wolfSSL 15:117db924cf7c 345
wolfSSL 15:117db924cf7c 346 HAL_CRYP_Init(&hcryp);
wolfSSL 15:117db924cf7c 347
wolfSSL 15:117db924cf7c 348 while (sz > 0)
wolfSSL 15:117db924cf7c 349 {
wolfSSL 15:117db924cf7c 350 if (dir == DES_ENCRYPTION) {
wolfSSL 15:117db924cf7c 351 HAL_CRYP_TDESCBC_Encrypt(&hcryp, (byte*)in,
wolfSSL 15:117db924cf7c 352 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
wolfSSL 15:117db924cf7c 353 }
wolfSSL 15:117db924cf7c 354 else {
wolfSSL 15:117db924cf7c 355 HAL_CRYP_TDESCBC_Decrypt(&hcryp, (byte*)in,
wolfSSL 15:117db924cf7c 356 DES_BLOCK_SIZE, out, STM32_HAL_TIMEOUT);
wolfSSL 15:117db924cf7c 357 }
wolfSSL 15:117db924cf7c 358
wolfSSL 15:117db924cf7c 359 /* store iv for next call */
wolfSSL 15:117db924cf7c 360 XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 361
wolfSSL 15:117db924cf7c 362 sz -= DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 363 in += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 364 out += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 365 }
wolfSSL 15:117db924cf7c 366
wolfSSL 15:117db924cf7c 367 HAL_CRYP_DeInit(&hcryp);
wolfSSL 15:117db924cf7c 368 #else
wolfSSL 15:117db924cf7c 369 word32 *dkey1, *dkey2, *dkey3, *iv;
wolfSSL 15:117db924cf7c 370 CRYP_InitTypeDef DES3_CRYP_InitStructure;
wolfSSL 15:117db924cf7c 371 CRYP_KeyInitTypeDef DES3_CRYP_KeyInitStructure;
wolfSSL 15:117db924cf7c 372 CRYP_IVInitTypeDef DES3_CRYP_IVInitStructure;
wolfSSL 15:117db924cf7c 373
wolfSSL 15:117db924cf7c 374 dkey1 = des->key[0];
wolfSSL 15:117db924cf7c 375 dkey2 = des->key[1];
wolfSSL 15:117db924cf7c 376 dkey3 = des->key[2];
wolfSSL 15:117db924cf7c 377 iv = des->reg;
wolfSSL 15:117db924cf7c 378
wolfSSL 15:117db924cf7c 379 /* crypto structure initialization */
wolfSSL 15:117db924cf7c 380 CRYP_KeyStructInit(&DES3_CRYP_KeyInitStructure);
wolfSSL 15:117db924cf7c 381 CRYP_StructInit(&DES3_CRYP_InitStructure);
wolfSSL 15:117db924cf7c 382 CRYP_IVStructInit(&DES3_CRYP_IVInitStructure);
wolfSSL 15:117db924cf7c 383
wolfSSL 15:117db924cf7c 384 /* reset registers to their default values */
wolfSSL 15:117db924cf7c 385 CRYP_DeInit();
wolfSSL 15:117db924cf7c 386
wolfSSL 15:117db924cf7c 387 /* set direction, mode, and datatype */
wolfSSL 15:117db924cf7c 388 if (dir == DES_ENCRYPTION) {
wolfSSL 15:117db924cf7c 389 DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
wolfSSL 15:117db924cf7c 390 } else {
wolfSSL 15:117db924cf7c 391 DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
wolfSSL 15:117db924cf7c 392 }
wolfSSL 15:117db924cf7c 393
wolfSSL 15:117db924cf7c 394 DES3_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC;
wolfSSL 15:117db924cf7c 395 DES3_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b;
wolfSSL 15:117db924cf7c 396 CRYP_Init(&DES3_CRYP_InitStructure);
wolfSSL 15:117db924cf7c 397
wolfSSL 15:117db924cf7c 398 /* load key into correct registers */
wolfSSL 15:117db924cf7c 399 DES3_CRYP_KeyInitStructure.CRYP_Key1Left = dkey1[0];
wolfSSL 15:117db924cf7c 400 DES3_CRYP_KeyInitStructure.CRYP_Key1Right = dkey1[1];
wolfSSL 15:117db924cf7c 401 DES3_CRYP_KeyInitStructure.CRYP_Key2Left = dkey2[0];
wolfSSL 15:117db924cf7c 402 DES3_CRYP_KeyInitStructure.CRYP_Key2Right = dkey2[1];
wolfSSL 15:117db924cf7c 403 DES3_CRYP_KeyInitStructure.CRYP_Key3Left = dkey3[0];
wolfSSL 15:117db924cf7c 404 DES3_CRYP_KeyInitStructure.CRYP_Key3Right = dkey3[1];
wolfSSL 15:117db924cf7c 405 CRYP_KeyInit(&DES3_CRYP_KeyInitStructure);
wolfSSL 15:117db924cf7c 406
wolfSSL 15:117db924cf7c 407 /* set iv */
wolfSSL 15:117db924cf7c 408 ByteReverseWords(iv, iv, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 409 DES3_CRYP_IVInitStructure.CRYP_IV0Left = iv[0];
wolfSSL 15:117db924cf7c 410 DES3_CRYP_IVInitStructure.CRYP_IV0Right = iv[1];
wolfSSL 15:117db924cf7c 411 CRYP_IVInit(&DES3_CRYP_IVInitStructure);
wolfSSL 15:117db924cf7c 412
wolfSSL 15:117db924cf7c 413 /* enable crypto processor */
wolfSSL 15:117db924cf7c 414 CRYP_Cmd(ENABLE);
wolfSSL 15:117db924cf7c 415
wolfSSL 15:117db924cf7c 416 while (sz > 0)
wolfSSL 15:117db924cf7c 417 {
wolfSSL 15:117db924cf7c 418 /* flush IN/OUT FIFOs */
wolfSSL 15:117db924cf7c 419 CRYP_FIFOFlush();
wolfSSL 15:117db924cf7c 420
wolfSSL 15:117db924cf7c 421 CRYP_DataIn(*(uint32_t*)&in[0]);
wolfSSL 15:117db924cf7c 422 CRYP_DataIn(*(uint32_t*)&in[4]);
wolfSSL 15:117db924cf7c 423
wolfSSL 15:117db924cf7c 424 /* wait until the complete message has been processed */
wolfSSL 15:117db924cf7c 425 while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
wolfSSL 15:117db924cf7c 426
wolfSSL 15:117db924cf7c 427 *(uint32_t*)&out[0] = CRYP_DataOut();
wolfSSL 15:117db924cf7c 428 *(uint32_t*)&out[4] = CRYP_DataOut();
wolfSSL 15:117db924cf7c 429
wolfSSL 15:117db924cf7c 430 /* store iv for next call */
wolfSSL 15:117db924cf7c 431 XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 432
wolfSSL 15:117db924cf7c 433 sz -= DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 434 in += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 435 out += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 436 }
wolfSSL 15:117db924cf7c 437
wolfSSL 15:117db924cf7c 438 /* disable crypto processor */
wolfSSL 15:117db924cf7c 439 CRYP_Cmd(DISABLE);
wolfSSL 15:117db924cf7c 440 #endif /* WOLFSSL_STM32_CUBEMX */
wolfSSL 15:117db924cf7c 441 }
wolfSSL 15:117db924cf7c 442
wolfSSL 15:117db924cf7c 443 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 444 {
wolfSSL 15:117db924cf7c 445 Des3Crypt(des, out, in, sz, DES_ENCRYPTION);
wolfSSL 15:117db924cf7c 446 return 0;
wolfSSL 15:117db924cf7c 447 }
wolfSSL 15:117db924cf7c 448
wolfSSL 15:117db924cf7c 449 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 450 {
wolfSSL 15:117db924cf7c 451 Des3Crypt(des, out, in, sz, DES_DECRYPTION);
wolfSSL 15:117db924cf7c 452 return 0;
wolfSSL 15:117db924cf7c 453 }
wolfSSL 15:117db924cf7c 454
wolfSSL 15:117db924cf7c 455 #elif defined(HAVE_COLDFIRE_SEC)
wolfSSL 15:117db924cf7c 456
wolfSSL 15:117db924cf7c 457 #include <wolfssl/ctaocrypt/types.h>
wolfSSL 15:117db924cf7c 458
wolfSSL 15:117db924cf7c 459 #include "sec.h"
wolfSSL 15:117db924cf7c 460 #include "mcf5475_sec.h"
wolfSSL 15:117db924cf7c 461 #include "mcf5475_siu.h"
wolfSSL 15:117db924cf7c 462
wolfSSL 15:117db924cf7c 463 #if defined (HAVE_THREADX)
wolfSSL 15:117db924cf7c 464 #include "memory_pools.h"
wolfSSL 15:117db924cf7c 465 extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */
wolfSSL 15:117db924cf7c 466 #endif
wolfSSL 15:117db924cf7c 467
wolfSSL 15:117db924cf7c 468 #define DES_BUFFER_SIZE (DES_BLOCK_SIZE * 64)
wolfSSL 15:117db924cf7c 469 static unsigned char *desBuffIn = NULL;
wolfSSL 15:117db924cf7c 470 static unsigned char *desBuffOut = NULL;
wolfSSL 15:117db924cf7c 471 static byte *secIV;
wolfSSL 15:117db924cf7c 472 static byte *secKey;
wolfSSL 15:117db924cf7c 473 static volatile SECdescriptorType *secDesc;
wolfSSL 15:117db924cf7c 474
wolfSSL 15:117db924cf7c 475 static wolfSSL_Mutex Mutex_DesSEC;
wolfSSL 15:117db924cf7c 476
wolfSSL 15:117db924cf7c 477 #define SEC_DESC_DES_CBC_ENCRYPT 0x20500010
wolfSSL 15:117db924cf7c 478 #define SEC_DESC_DES_CBC_DECRYPT 0x20400010
wolfSSL 15:117db924cf7c 479 #define SEC_DESC_DES3_CBC_ENCRYPT 0x20700010
wolfSSL 15:117db924cf7c 480 #define SEC_DESC_DES3_CBC_DECRYPT 0x20600010
wolfSSL 15:117db924cf7c 481
wolfSSL 15:117db924cf7c 482 #define DES_IVLEN 8
wolfSSL 15:117db924cf7c 483 #define DES_KEYLEN 8
wolfSSL 15:117db924cf7c 484 #define DES3_IVLEN 8
wolfSSL 15:117db924cf7c 485 #define DES3_KEYLEN 24
wolfSSL 15:117db924cf7c 486
wolfSSL 15:117db924cf7c 487 extern volatile unsigned char __MBAR[];
wolfSSL 15:117db924cf7c 488
wolfSSL 15:117db924cf7c 489 static void wc_Des_Cbc(byte* out, const byte* in, word32 sz,
wolfSSL 15:117db924cf7c 490 byte *key, byte *iv, word32 desc)
wolfSSL 15:117db924cf7c 491 {
wolfSSL 15:117db924cf7c 492 #ifdef DEBUG_WOLFSSL
wolfSSL 15:117db924cf7c 493 int ret; int stat1,stat2;
wolfSSL 15:117db924cf7c 494 #endif
wolfSSL 15:117db924cf7c 495 int size;
wolfSSL 15:117db924cf7c 496 volatile int v;
wolfSSL 15:117db924cf7c 497
wolfSSL 15:117db924cf7c 498 wc_LockMutex(&Mutex_DesSEC) ;
wolfSSL 15:117db924cf7c 499
wolfSSL 15:117db924cf7c 500 secDesc->length1 = 0x0;
wolfSSL 15:117db924cf7c 501 secDesc->pointer1 = NULL;
wolfSSL 15:117db924cf7c 502 if((desc==SEC_DESC_DES_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_DECRYPT)){
wolfSSL 15:117db924cf7c 503 secDesc->length2 = DES_IVLEN;
wolfSSL 15:117db924cf7c 504 secDesc->length3 = DES_KEYLEN;
wolfSSL 15:117db924cf7c 505 } else {
wolfSSL 15:117db924cf7c 506 secDesc->length2 = DES3_IVLEN;
wolfSSL 15:117db924cf7c 507 secDesc->length3 = DES3_KEYLEN;
wolfSSL 15:117db924cf7c 508 }
wolfSSL 15:117db924cf7c 509 secDesc->pointer2 = secIV;
wolfSSL 15:117db924cf7c 510 secDesc->pointer3 = secKey;
wolfSSL 15:117db924cf7c 511 secDesc->pointer4 = desBuffIn;
wolfSSL 15:117db924cf7c 512 secDesc->pointer5 = desBuffOut;
wolfSSL 15:117db924cf7c 513 secDesc->length6 = 0;
wolfSSL 15:117db924cf7c 514 secDesc->pointer6 = NULL;
wolfSSL 15:117db924cf7c 515 secDesc->length7 = 0x0;
wolfSSL 15:117db924cf7c 516 secDesc->pointer7 = NULL;
wolfSSL 15:117db924cf7c 517 secDesc->nextDescriptorPtr = NULL;
wolfSSL 15:117db924cf7c 518
wolfSSL 15:117db924cf7c 519 while(sz) {
wolfSSL 15:117db924cf7c 520 XMEMCPY(secIV, iv, secDesc->length2);
wolfSSL 15:117db924cf7c 521 if((sz%DES_BUFFER_SIZE) == sz) {
wolfSSL 15:117db924cf7c 522 size = sz;
wolfSSL 15:117db924cf7c 523 sz = 0;
wolfSSL 15:117db924cf7c 524 } else {
wolfSSL 15:117db924cf7c 525 size = DES_BUFFER_SIZE;
wolfSSL 15:117db924cf7c 526 sz -= DES_BUFFER_SIZE;
wolfSSL 15:117db924cf7c 527 }
wolfSSL 15:117db924cf7c 528
wolfSSL 15:117db924cf7c 529 XMEMCPY(desBuffIn, in, size);
wolfSSL 15:117db924cf7c 530 XMEMCPY(secKey, key, secDesc->length3);
wolfSSL 15:117db924cf7c 531
wolfSSL 15:117db924cf7c 532 secDesc->header = desc;
wolfSSL 15:117db924cf7c 533 secDesc->length4 = size;
wolfSSL 15:117db924cf7c 534 secDesc->length5 = size;
wolfSSL 15:117db924cf7c 535 /* Point SEC to the location of the descriptor */
wolfSSL 15:117db924cf7c 536 MCF_SEC_FR0 = (uint32)secDesc;
wolfSSL 15:117db924cf7c 537 /* Initialize SEC and wait for encryption to complete */
wolfSSL 15:117db924cf7c 538 MCF_SEC_CCCR0 = 0x0000001a;
wolfSSL 15:117db924cf7c 539 /* poll SISR to determine when channel is complete */
wolfSSL 15:117db924cf7c 540 v=0;
wolfSSL 15:117db924cf7c 541 while((secDesc->header>> 24) != 0xff) {
wolfSSL 15:117db924cf7c 542 if(v++ > 1000)break;
wolfSSL 15:117db924cf7c 543 }
wolfSSL 15:117db924cf7c 544
wolfSSL 15:117db924cf7c 545 #ifdef DEBUG_WOLFSSL
wolfSSL 15:117db924cf7c 546 ret = MCF_SEC_SISRH;
wolfSSL 15:117db924cf7c 547 stat1 = MCF_SEC_DSR;
wolfSSL 15:117db924cf7c 548 stat2 = MCF_SEC_DISR;
wolfSSL 15:117db924cf7c 549 if(ret & 0xe0000000) {
wolfSSL 15:117db924cf7c 550 /* db_printf("Des_Cbc(%x):ISRH=%08x, DSR=%08x, DISR=%08x\n", desc, ret, stat1, stat2); */
wolfSSL 15:117db924cf7c 551 }
wolfSSL 15:117db924cf7c 552 #endif
wolfSSL 15:117db924cf7c 553
wolfSSL 15:117db924cf7c 554 XMEMCPY(out, desBuffOut, size);
wolfSSL 15:117db924cf7c 555
wolfSSL 15:117db924cf7c 556 if ((desc==SEC_DESC_DES3_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_ENCRYPT)) {
wolfSSL 15:117db924cf7c 557 XMEMCPY((void*)iv, (void*)&(out[size-secDesc->length2]), secDesc->length2);
wolfSSL 15:117db924cf7c 558 } else {
wolfSSL 15:117db924cf7c 559 XMEMCPY((void*)iv, (void*)&(in[size-secDesc->length2]), secDesc->length2);
wolfSSL 15:117db924cf7c 560 }
wolfSSL 15:117db924cf7c 561
wolfSSL 15:117db924cf7c 562 in += size;
wolfSSL 15:117db924cf7c 563 out += size;
wolfSSL 15:117db924cf7c 564
wolfSSL 15:117db924cf7c 565 }
wolfSSL 15:117db924cf7c 566 wc_UnLockMutex(&Mutex_DesSEC) ;
wolfSSL 15:117db924cf7c 567
wolfSSL 15:117db924cf7c 568 }
wolfSSL 15:117db924cf7c 569
wolfSSL 15:117db924cf7c 570
wolfSSL 15:117db924cf7c 571 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 572 {
wolfSSL 15:117db924cf7c 573 wc_Des_Cbc(out, in, sz, (byte *)des->key, (byte *)des->reg, SEC_DESC_DES_CBC_ENCRYPT);
wolfSSL 15:117db924cf7c 574 return 0;
wolfSSL 15:117db924cf7c 575 }
wolfSSL 15:117db924cf7c 576
wolfSSL 15:117db924cf7c 577 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 578 {
wolfSSL 15:117db924cf7c 579 wc_Des_Cbc(out, in, sz, (byte *)des->key, (byte *)des->reg, SEC_DESC_DES_CBC_DECRYPT);
wolfSSL 15:117db924cf7c 580 return 0;
wolfSSL 15:117db924cf7c 581 }
wolfSSL 15:117db924cf7c 582
wolfSSL 15:117db924cf7c 583 int wc_Des3_CbcEncrypt(Des3* des3, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 584 {
wolfSSL 15:117db924cf7c 585 wc_Des_Cbc(out, in, sz, (byte *)des3->key, (byte *)des3->reg, SEC_DESC_DES3_CBC_ENCRYPT);
wolfSSL 15:117db924cf7c 586 return 0;
wolfSSL 15:117db924cf7c 587 }
wolfSSL 15:117db924cf7c 588
wolfSSL 15:117db924cf7c 589
wolfSSL 15:117db924cf7c 590 int wc_Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 591 {
wolfSSL 15:117db924cf7c 592 wc_Des_Cbc(out, in, sz, (byte *)des3->key, (byte *)des3->reg, SEC_DESC_DES3_CBC_DECRYPT);
wolfSSL 15:117db924cf7c 593 return 0;
wolfSSL 15:117db924cf7c 594 }
wolfSSL 15:117db924cf7c 595
wolfSSL 15:117db924cf7c 596 static void setParity(byte *buf, int len)
wolfSSL 15:117db924cf7c 597 {
wolfSSL 15:117db924cf7c 598 int i, j;
wolfSSL 15:117db924cf7c 599 byte v;
wolfSSL 15:117db924cf7c 600 int bits;
wolfSSL 15:117db924cf7c 601
wolfSSL 15:117db924cf7c 602 for (i=0; i<len; i++) {
wolfSSL 15:117db924cf7c 603 v = buf[i] >> 1;
wolfSSL 15:117db924cf7c 604 buf[i] = v << 1;
wolfSSL 15:117db924cf7c 605 bits = 0;
wolfSSL 15:117db924cf7c 606 for (j=0; j<7; j++) {
wolfSSL 15:117db924cf7c 607 bits += (v&0x1);
wolfSSL 15:117db924cf7c 608 v = v >> 1;
wolfSSL 15:117db924cf7c 609 }
wolfSSL 15:117db924cf7c 610 buf[i] |= (1 - (bits&0x1));
wolfSSL 15:117db924cf7c 611 }
wolfSSL 15:117db924cf7c 612
wolfSSL 15:117db924cf7c 613 }
wolfSSL 15:117db924cf7c 614
wolfSSL 15:117db924cf7c 615 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 616 {
wolfSSL 15:117db924cf7c 617 if(desBuffIn == NULL) {
wolfSSL 15:117db924cf7c 618 #if defined (HAVE_THREADX)
wolfSSL 15:117db924cf7c 619 int s1, s2, s3, s4, s5;
wolfSSL 15:117db924cf7c 620 s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc,
wolfSSL 15:117db924cf7c 621 sizeof(SECdescriptorType), TX_NO_WAIT);
wolfSSL 15:117db924cf7c 622 s1 = tx_byte_allocate(&mp_ncached,(void *)&desBuffIn, DES_BUFFER_SIZE, TX_NO_WAIT);
wolfSSL 15:117db924cf7c 623 s2 = tx_byte_allocate(&mp_ncached,(void *)&desBuffOut, DES_BUFFER_SIZE, TX_NO_WAIT);
wolfSSL 15:117db924cf7c 624 /* Don't know des or des3 to be used. Allocate larger buffers */
wolfSSL 15:117db924cf7c 625 s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, DES3_KEYLEN,TX_NO_WAIT);
wolfSSL 15:117db924cf7c 626 s4 = tx_byte_allocate(&mp_ncached,(void *)&secIV, DES3_IVLEN, TX_NO_WAIT);
wolfSSL 15:117db924cf7c 627 #else
wolfSSL 15:117db924cf7c 628 #warning "Allocate non-Cache buffers"
wolfSSL 15:117db924cf7c 629 #endif
wolfSSL 15:117db924cf7c 630
wolfSSL 15:117db924cf7c 631 InitMutex(&Mutex_DesSEC);
wolfSSL 15:117db924cf7c 632 }
wolfSSL 15:117db924cf7c 633
wolfSSL 15:117db924cf7c 634 XMEMCPY(des->key, key, DES_KEYLEN);
wolfSSL 15:117db924cf7c 635 setParity((byte *)des->key, DES_KEYLEN);
wolfSSL 15:117db924cf7c 636
wolfSSL 15:117db924cf7c 637 if (iv) {
wolfSSL 15:117db924cf7c 638 XMEMCPY(des->reg, iv, DES_IVLEN);
wolfSSL 15:117db924cf7c 639 } else {
wolfSSL 15:117db924cf7c 640 XMEMSET(des->reg, 0x0, DES_IVLEN);
wolfSSL 15:117db924cf7c 641 }
wolfSSL 15:117db924cf7c 642 return 0;
wolfSSL 15:117db924cf7c 643 }
wolfSSL 15:117db924cf7c 644
wolfSSL 15:117db924cf7c 645 int wc_Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 646 {
wolfSSL 15:117db924cf7c 647
wolfSSL 15:117db924cf7c 648 if(desBuffIn == NULL) {
wolfSSL 15:117db924cf7c 649 #if defined (HAVE_THREADX)
wolfSSL 15:117db924cf7c 650 int s1, s2, s3, s4, s5;
wolfSSL 15:117db924cf7c 651 s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc,
wolfSSL 15:117db924cf7c 652 sizeof(SECdescriptorType), TX_NO_WAIT);
wolfSSL 15:117db924cf7c 653 s1 = tx_byte_allocate(&mp_ncached,(void *)&desBuffIn, DES_BUFFER_SIZE, TX_NO_WAIT);
wolfSSL 15:117db924cf7c 654 s2 = tx_byte_allocate(&mp_ncached,(void *)&desBuffOut, DES_BUFFER_SIZE, TX_NO_WAIT);
wolfSSL 15:117db924cf7c 655 s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, DES3_KEYLEN,TX_NO_WAIT);
wolfSSL 15:117db924cf7c 656 s4 = tx_byte_allocate(&mp_ncached,(void *)&secIV, DES3_IVLEN, TX_NO_WAIT);
wolfSSL 15:117db924cf7c 657 #else
wolfSSL 15:117db924cf7c 658 #warning "Allocate non-Cache buffers"
wolfSSL 15:117db924cf7c 659 #endif
wolfSSL 15:117db924cf7c 660
wolfSSL 15:117db924cf7c 661 InitMutex(&Mutex_DesSEC);
wolfSSL 15:117db924cf7c 662 }
wolfSSL 15:117db924cf7c 663
wolfSSL 15:117db924cf7c 664 XMEMCPY(des3->key[0], key, DES3_KEYLEN);
wolfSSL 15:117db924cf7c 665 setParity((byte *)des3->key[0], DES3_KEYLEN);
wolfSSL 15:117db924cf7c 666
wolfSSL 15:117db924cf7c 667 if (iv) {
wolfSSL 15:117db924cf7c 668 XMEMCPY(des3->reg, iv, DES3_IVLEN);
wolfSSL 15:117db924cf7c 669 } else {
wolfSSL 15:117db924cf7c 670 XMEMSET(des3->reg, 0x0, DES3_IVLEN);
wolfSSL 15:117db924cf7c 671 }
wolfSSL 15:117db924cf7c 672 return 0;
wolfSSL 15:117db924cf7c 673
wolfSSL 15:117db924cf7c 674 }
wolfSSL 15:117db924cf7c 675 #elif defined(FREESCALE_LTC_DES)
wolfSSL 15:117db924cf7c 676
wolfSSL 15:117db924cf7c 677 #include "fsl_ltc.h"
wolfSSL 15:117db924cf7c 678 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 679 {
wolfSSL 15:117db924cf7c 680 byte* dkey = (byte*)des->key;
wolfSSL 15:117db924cf7c 681
wolfSSL 15:117db924cf7c 682 XMEMCPY(dkey, key, 8);
wolfSSL 15:117db924cf7c 683
wolfSSL 15:117db924cf7c 684 wc_Des_SetIV(des, iv);
wolfSSL 15:117db924cf7c 685
wolfSSL 15:117db924cf7c 686 return 0;
wolfSSL 15:117db924cf7c 687 }
wolfSSL 15:117db924cf7c 688
wolfSSL 15:117db924cf7c 689 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 690 {
wolfSSL 15:117db924cf7c 691 int ret = 0;
wolfSSL 15:117db924cf7c 692 byte* dkey1 = (byte*)des->key[0];
wolfSSL 15:117db924cf7c 693 byte* dkey2 = (byte*)des->key[1];
wolfSSL 15:117db924cf7c 694 byte* dkey3 = (byte*)des->key[2];
wolfSSL 15:117db924cf7c 695
wolfSSL 15:117db924cf7c 696 XMEMCPY(dkey1, key, 8); /* set key 1 */
wolfSSL 15:117db924cf7c 697 XMEMCPY(dkey2, key + 8, 8); /* set key 2 */
wolfSSL 15:117db924cf7c 698 XMEMCPY(dkey3, key + 16, 8); /* set key 3 */
wolfSSL 15:117db924cf7c 699
wolfSSL 15:117db924cf7c 700 ret = wc_Des3_SetIV(des, iv);
wolfSSL 15:117db924cf7c 701 if (ret != 0)
wolfSSL 15:117db924cf7c 702 return ret;
wolfSSL 15:117db924cf7c 703
wolfSSL 15:117db924cf7c 704 return ret;
wolfSSL 15:117db924cf7c 705 }
wolfSSL 15:117db924cf7c 706
wolfSSL 15:117db924cf7c 707 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 708 {
wolfSSL 15:117db924cf7c 709 status_t status;
wolfSSL 15:117db924cf7c 710 status = LTC_DES_EncryptCbc(LTC_BASE, in, out, sz, (byte*)des->reg, (byte*)des->key);
wolfSSL 15:117db924cf7c 711 if (status == kStatus_Success)
wolfSSL 15:117db924cf7c 712 return 0;
wolfSSL 15:117db924cf7c 713 else
wolfSSL 15:117db924cf7c 714 return -1;
wolfSSL 15:117db924cf7c 715 }
wolfSSL 15:117db924cf7c 716
wolfSSL 15:117db924cf7c 717 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 718 {
wolfSSL 15:117db924cf7c 719 status_t status;
wolfSSL 15:117db924cf7c 720 status = LTC_DES_DecryptCbc(LTC_BASE, in, out, sz, (byte*)des->reg, (byte*)des->key);
wolfSSL 15:117db924cf7c 721 if (status == kStatus_Success)
wolfSSL 15:117db924cf7c 722 return 0;
wolfSSL 15:117db924cf7c 723 else
wolfSSL 15:117db924cf7c 724 return -1;
wolfSSL 15:117db924cf7c 725 }
wolfSSL 15:117db924cf7c 726
wolfSSL 15:117db924cf7c 727 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 728 {
wolfSSL 15:117db924cf7c 729 status_t status;
wolfSSL 15:117db924cf7c 730 status = LTC_DES3_EncryptCbc(LTC_BASE,
wolfSSL 15:117db924cf7c 731 in,
wolfSSL 15:117db924cf7c 732 out,
wolfSSL 15:117db924cf7c 733 sz,
wolfSSL 15:117db924cf7c 734 (byte*)des->reg,
wolfSSL 15:117db924cf7c 735 (byte*)des->key[0],
wolfSSL 15:117db924cf7c 736 (byte*)des->key[1],
wolfSSL 15:117db924cf7c 737 (byte*)des->key[2]);
wolfSSL 15:117db924cf7c 738 if (status == kStatus_Success)
wolfSSL 15:117db924cf7c 739 return 0;
wolfSSL 15:117db924cf7c 740 else
wolfSSL 15:117db924cf7c 741 return -1;
wolfSSL 15:117db924cf7c 742 }
wolfSSL 15:117db924cf7c 743
wolfSSL 15:117db924cf7c 744 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 745 {
wolfSSL 15:117db924cf7c 746 status_t status;
wolfSSL 15:117db924cf7c 747 status = LTC_DES3_DecryptCbc(LTC_BASE,
wolfSSL 15:117db924cf7c 748 in,
wolfSSL 15:117db924cf7c 749 out,
wolfSSL 15:117db924cf7c 750 sz,
wolfSSL 15:117db924cf7c 751 (byte*)des->reg,
wolfSSL 15:117db924cf7c 752 (byte*)des->key[0],
wolfSSL 15:117db924cf7c 753 (byte*)des->key[1],
wolfSSL 15:117db924cf7c 754 (byte*)des->key[2]);
wolfSSL 15:117db924cf7c 755 if (status == kStatus_Success)
wolfSSL 15:117db924cf7c 756 return 0;
wolfSSL 15:117db924cf7c 757 else
wolfSSL 15:117db924cf7c 758 return -1;
wolfSSL 15:117db924cf7c 759
wolfSSL 15:117db924cf7c 760 }
wolfSSL 15:117db924cf7c 761
wolfSSL 15:117db924cf7c 762 #elif defined(FREESCALE_MMCAU)
wolfSSL 15:117db924cf7c 763 /*
wolfSSL 15:117db924cf7c 764 * Freescale mmCAU hardware DES/3DES support through the CAU/mmCAU library.
wolfSSL 15:117db924cf7c 765 * Documentation located in ColdFire/ColdFire+ CAU and Kinetis mmCAU
wolfSSL 15:117db924cf7c 766 * Software Library User Guide (See note in README).
wolfSSL 15:117db924cf7c 767 */
wolfSSL 15:117db924cf7c 768 #ifdef FREESCALE_MMCAU_CLASSIC
wolfSSL 15:117db924cf7c 769 #include "cau_api.h"
wolfSSL 15:117db924cf7c 770 #else
wolfSSL 15:117db924cf7c 771 #include "fsl_mmcau.h"
wolfSSL 15:117db924cf7c 772 #endif
wolfSSL 15:117db924cf7c 773
wolfSSL 15:117db924cf7c 774 const unsigned char parityLookup[128] = {
wolfSSL 15:117db924cf7c 775 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 15:117db924cf7c 776 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 15:117db924cf7c 777 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 15:117db924cf7c 778 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 15:117db924cf7c 779 };
wolfSSL 15:117db924cf7c 780
wolfSSL 15:117db924cf7c 781 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 782 {
wolfSSL 15:117db924cf7c 783 int i = 0;
wolfSSL 15:117db924cf7c 784 byte* dkey = (byte*)des->key;
wolfSSL 15:117db924cf7c 785
wolfSSL 15:117db924cf7c 786 XMEMCPY(dkey, key, 8);
wolfSSL 15:117db924cf7c 787
wolfSSL 15:117db924cf7c 788 wc_Des_SetIV(des, iv);
wolfSSL 15:117db924cf7c 789
wolfSSL 15:117db924cf7c 790 /* fix key parity, if needed */
wolfSSL 15:117db924cf7c 791 for (i = 0; i < 8; i++) {
wolfSSL 15:117db924cf7c 792 dkey[i] = ((dkey[i] & 0xFE) | parityLookup[dkey[i] >> 1]);
wolfSSL 15:117db924cf7c 793 }
wolfSSL 15:117db924cf7c 794
wolfSSL 15:117db924cf7c 795 return 0;
wolfSSL 15:117db924cf7c 796 }
wolfSSL 15:117db924cf7c 797
wolfSSL 15:117db924cf7c 798 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 799 {
wolfSSL 15:117db924cf7c 800 int i = 0, ret = 0;
wolfSSL 15:117db924cf7c 801 byte* dkey1 = (byte*)des->key[0];
wolfSSL 15:117db924cf7c 802 byte* dkey2 = (byte*)des->key[1];
wolfSSL 15:117db924cf7c 803 byte* dkey3 = (byte*)des->key[2];
wolfSSL 15:117db924cf7c 804
wolfSSL 15:117db924cf7c 805 XMEMCPY(dkey1, key, 8); /* set key 1 */
wolfSSL 15:117db924cf7c 806 XMEMCPY(dkey2, key + 8, 8); /* set key 2 */
wolfSSL 15:117db924cf7c 807 XMEMCPY(dkey3, key + 16, 8); /* set key 3 */
wolfSSL 15:117db924cf7c 808
wolfSSL 15:117db924cf7c 809 ret = wc_Des3_SetIV(des, iv);
wolfSSL 15:117db924cf7c 810 if (ret != 0)
wolfSSL 15:117db924cf7c 811 return ret;
wolfSSL 15:117db924cf7c 812
wolfSSL 15:117db924cf7c 813 /* fix key parity if needed */
wolfSSL 15:117db924cf7c 814 for (i = 0; i < 8; i++)
wolfSSL 15:117db924cf7c 815 dkey1[i] = ((dkey1[i] & 0xFE) | parityLookup[dkey1[i] >> 1]);
wolfSSL 15:117db924cf7c 816
wolfSSL 15:117db924cf7c 817 for (i = 0; i < 8; i++)
wolfSSL 15:117db924cf7c 818 dkey2[i] = ((dkey2[i] & 0xFE) | parityLookup[dkey2[i] >> 1]);
wolfSSL 15:117db924cf7c 819
wolfSSL 15:117db924cf7c 820 for (i = 0; i < 8; i++)
wolfSSL 15:117db924cf7c 821 dkey3[i] = ((dkey3[i] & 0xFE) | parityLookup[dkey3[i] >> 1]);
wolfSSL 15:117db924cf7c 822
wolfSSL 15:117db924cf7c 823 return ret;
wolfSSL 15:117db924cf7c 824 }
wolfSSL 15:117db924cf7c 825
wolfSSL 15:117db924cf7c 826 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 827 {
wolfSSL 15:117db924cf7c 828 int i;
wolfSSL 15:117db924cf7c 829 int offset = 0;
wolfSSL 15:117db924cf7c 830 int len = sz;
wolfSSL 15:117db924cf7c 831 int ret = 0;
wolfSSL 15:117db924cf7c 832 byte *iv;
wolfSSL 15:117db924cf7c 833 byte temp_block[DES_BLOCK_SIZE];
wolfSSL 15:117db924cf7c 834
wolfSSL 15:117db924cf7c 835 iv = (byte*)des->reg;
wolfSSL 15:117db924cf7c 836
wolfSSL 15:117db924cf7c 837 #ifdef FREESCALE_MMCAU_CLASSIC
wolfSSL 15:117db924cf7c 838 if ((wolfssl_word)out % WOLFSSL_MMCAU_ALIGNMENT) {
wolfSSL 15:117db924cf7c 839 WOLFSSL_MSG("Bad cau_des_encrypt alignment");
wolfSSL 15:117db924cf7c 840 return BAD_ALIGN_E;
wolfSSL 15:117db924cf7c 841 }
wolfSSL 15:117db924cf7c 842 #endif
wolfSSL 15:117db924cf7c 843
wolfSSL 15:117db924cf7c 844 while (len > 0)
wolfSSL 15:117db924cf7c 845 {
wolfSSL 15:117db924cf7c 846 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 847
wolfSSL 15:117db924cf7c 848 /* XOR block with IV for CBC */
wolfSSL 15:117db924cf7c 849 for (i = 0; i < DES_BLOCK_SIZE; i++)
wolfSSL 15:117db924cf7c 850 temp_block[i] ^= iv[i];
wolfSSL 15:117db924cf7c 851
wolfSSL 15:117db924cf7c 852 ret = wolfSSL_CryptHwMutexLock();
wolfSSL 15:117db924cf7c 853 if(ret != 0) {
wolfSSL 15:117db924cf7c 854 return ret;
wolfSSL 15:117db924cf7c 855 }
wolfSSL 15:117db924cf7c 856 #ifdef FREESCALE_MMCAU_CLASSIC
wolfSSL 15:117db924cf7c 857 cau_des_encrypt(temp_block, (byte*)des->key, out + offset);
wolfSSL 15:117db924cf7c 858 #else
wolfSSL 15:117db924cf7c 859 MMCAU_DES_EncryptEcb(temp_block, (byte*)des->key, out + offset);
wolfSSL 15:117db924cf7c 860 #endif
wolfSSL 15:117db924cf7c 861 wolfSSL_CryptHwMutexUnLock();
wolfSSL 15:117db924cf7c 862
wolfSSL 15:117db924cf7c 863 len -= DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 864 offset += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 865
wolfSSL 15:117db924cf7c 866 /* store IV for next block */
wolfSSL 15:117db924cf7c 867 XMEMCPY(iv, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 868 }
wolfSSL 15:117db924cf7c 869
wolfSSL 15:117db924cf7c 870 return ret;
wolfSSL 15:117db924cf7c 871 }
wolfSSL 15:117db924cf7c 872
wolfSSL 15:117db924cf7c 873 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 874 {
wolfSSL 15:117db924cf7c 875 int i;
wolfSSL 15:117db924cf7c 876 int offset = 0;
wolfSSL 15:117db924cf7c 877 int len = sz;
wolfSSL 15:117db924cf7c 878 int ret = 0;
wolfSSL 15:117db924cf7c 879 byte* iv;
wolfSSL 15:117db924cf7c 880 byte temp_block[DES_BLOCK_SIZE];
wolfSSL 15:117db924cf7c 881
wolfSSL 15:117db924cf7c 882 iv = (byte*)des->reg;
wolfSSL 15:117db924cf7c 883
wolfSSL 15:117db924cf7c 884 #ifdef FREESCALE_MMCAU_CLASSIC
wolfSSL 15:117db924cf7c 885 if ((wolfssl_word)out % WOLFSSL_MMCAU_ALIGNMENT) {
wolfSSL 15:117db924cf7c 886 WOLFSSL_MSG("Bad cau_des_decrypt alignment");
wolfSSL 15:117db924cf7c 887 return BAD_ALIGN_E;
wolfSSL 15:117db924cf7c 888 }
wolfSSL 15:117db924cf7c 889 #endif
wolfSSL 15:117db924cf7c 890
wolfSSL 15:117db924cf7c 891 while (len > 0)
wolfSSL 15:117db924cf7c 892 {
wolfSSL 15:117db924cf7c 893 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 894
wolfSSL 15:117db924cf7c 895 ret = wolfSSL_CryptHwMutexLock();
wolfSSL 15:117db924cf7c 896 if(ret != 0) {
wolfSSL 15:117db924cf7c 897 return ret;
wolfSSL 15:117db924cf7c 898 }
wolfSSL 15:117db924cf7c 899
wolfSSL 15:117db924cf7c 900 #ifdef FREESCALE_MMCAU_CLASSIC
wolfSSL 15:117db924cf7c 901 cau_des_decrypt(in + offset, (byte*)des->key, out + offset);
wolfSSL 15:117db924cf7c 902 #else
wolfSSL 15:117db924cf7c 903 MMCAU_DES_DecryptEcb(in + offset, (byte*)des->key, out + offset);
wolfSSL 15:117db924cf7c 904 #endif
wolfSSL 15:117db924cf7c 905 wolfSSL_CryptHwMutexUnLock();
wolfSSL 15:117db924cf7c 906
wolfSSL 15:117db924cf7c 907 /* XOR block with IV for CBC */
wolfSSL 15:117db924cf7c 908 for (i = 0; i < DES_BLOCK_SIZE; i++)
wolfSSL 15:117db924cf7c 909 (out + offset)[i] ^= iv[i];
wolfSSL 15:117db924cf7c 910
wolfSSL 15:117db924cf7c 911 /* store IV for next block */
wolfSSL 15:117db924cf7c 912 XMEMCPY(iv, temp_block, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 913
wolfSSL 15:117db924cf7c 914 len -= DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 915 offset += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 916 }
wolfSSL 15:117db924cf7c 917
wolfSSL 15:117db924cf7c 918 return ret;
wolfSSL 15:117db924cf7c 919 }
wolfSSL 15:117db924cf7c 920
wolfSSL 15:117db924cf7c 921 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 922 {
wolfSSL 15:117db924cf7c 923 int i;
wolfSSL 15:117db924cf7c 924 int offset = 0;
wolfSSL 15:117db924cf7c 925 int len = sz;
wolfSSL 15:117db924cf7c 926 int ret = 0;
wolfSSL 15:117db924cf7c 927
wolfSSL 15:117db924cf7c 928 byte *iv;
wolfSSL 15:117db924cf7c 929 byte temp_block[DES_BLOCK_SIZE];
wolfSSL 15:117db924cf7c 930
wolfSSL 15:117db924cf7c 931 iv = (byte*)des->reg;
wolfSSL 15:117db924cf7c 932
wolfSSL 15:117db924cf7c 933 #ifdef FREESCALE_MMCAU_CLASSIC
wolfSSL 15:117db924cf7c 934 if ((wolfssl_word)out % WOLFSSL_MMCAU_ALIGNMENT) {
wolfSSL 15:117db924cf7c 935 WOLFSSL_MSG("Bad 3ede cau_des_encrypt alignment");
wolfSSL 15:117db924cf7c 936 return BAD_ALIGN_E;
wolfSSL 15:117db924cf7c 937 }
wolfSSL 15:117db924cf7c 938 #endif
wolfSSL 15:117db924cf7c 939
wolfSSL 15:117db924cf7c 940 while (len > 0)
wolfSSL 15:117db924cf7c 941 {
wolfSSL 15:117db924cf7c 942 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 943
wolfSSL 15:117db924cf7c 944 /* XOR block with IV for CBC */
wolfSSL 15:117db924cf7c 945 for (i = 0; i < DES_BLOCK_SIZE; i++)
wolfSSL 15:117db924cf7c 946 temp_block[i] ^= iv[i];
wolfSSL 15:117db924cf7c 947
wolfSSL 15:117db924cf7c 948 ret = wolfSSL_CryptHwMutexLock();
wolfSSL 15:117db924cf7c 949 if(ret != 0) {
wolfSSL 15:117db924cf7c 950 return ret;
wolfSSL 15:117db924cf7c 951 }
wolfSSL 15:117db924cf7c 952 #ifdef FREESCALE_MMCAU_CLASSIC
wolfSSL 15:117db924cf7c 953 cau_des_encrypt(temp_block, (byte*)des->key[0], out + offset);
wolfSSL 15:117db924cf7c 954 cau_des_decrypt(out + offset, (byte*)des->key[1], out + offset);
wolfSSL 15:117db924cf7c 955 cau_des_encrypt(out + offset, (byte*)des->key[2], out + offset);
wolfSSL 15:117db924cf7c 956 #else
wolfSSL 15:117db924cf7c 957 MMCAU_DES_EncryptEcb(temp_block , (byte*)des->key[0], out + offset);
wolfSSL 15:117db924cf7c 958 MMCAU_DES_DecryptEcb(out + offset, (byte*)des->key[1], out + offset);
wolfSSL 15:117db924cf7c 959 MMCAU_DES_EncryptEcb(out + offset, (byte*)des->key[2], out + offset);
wolfSSL 15:117db924cf7c 960 #endif
wolfSSL 15:117db924cf7c 961 wolfSSL_CryptHwMutexUnLock();
wolfSSL 15:117db924cf7c 962
wolfSSL 15:117db924cf7c 963 len -= DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 964 offset += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 965
wolfSSL 15:117db924cf7c 966 /* store IV for next block */
wolfSSL 15:117db924cf7c 967 XMEMCPY(iv, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 968 }
wolfSSL 15:117db924cf7c 969
wolfSSL 15:117db924cf7c 970 return ret;
wolfSSL 15:117db924cf7c 971 }
wolfSSL 15:117db924cf7c 972
wolfSSL 15:117db924cf7c 973 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 974 {
wolfSSL 15:117db924cf7c 975 int i;
wolfSSL 15:117db924cf7c 976 int offset = 0;
wolfSSL 15:117db924cf7c 977 int len = sz;
wolfSSL 15:117db924cf7c 978 int ret = 0;
wolfSSL 15:117db924cf7c 979
wolfSSL 15:117db924cf7c 980 byte* iv;
wolfSSL 15:117db924cf7c 981 byte temp_block[DES_BLOCK_SIZE];
wolfSSL 15:117db924cf7c 982
wolfSSL 15:117db924cf7c 983 iv = (byte*)des->reg;
wolfSSL 15:117db924cf7c 984
wolfSSL 15:117db924cf7c 985 #ifdef FREESCALE_MMCAU_CLASSIC
wolfSSL 15:117db924cf7c 986 if ((wolfssl_word)out % WOLFSSL_MMCAU_ALIGNMENT) {
wolfSSL 15:117db924cf7c 987 WOLFSSL_MSG("Bad 3ede cau_des_decrypt alignment");
wolfSSL 15:117db924cf7c 988 return BAD_ALIGN_E;
wolfSSL 15:117db924cf7c 989 }
wolfSSL 15:117db924cf7c 990 #endif
wolfSSL 15:117db924cf7c 991
wolfSSL 15:117db924cf7c 992 while (len > 0)
wolfSSL 15:117db924cf7c 993 {
wolfSSL 15:117db924cf7c 994 XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 995
wolfSSL 15:117db924cf7c 996 ret = wolfSSL_CryptHwMutexLock();
wolfSSL 15:117db924cf7c 997 if(ret != 0) {
wolfSSL 15:117db924cf7c 998 return ret;
wolfSSL 15:117db924cf7c 999 }
wolfSSL 15:117db924cf7c 1000 #ifdef FREESCALE_MMCAU_CLASSIC
wolfSSL 15:117db924cf7c 1001 cau_des_decrypt(in + offset, (byte*)des->key[2], out + offset);
wolfSSL 15:117db924cf7c 1002 cau_des_encrypt(out + offset, (byte*)des->key[1], out + offset);
wolfSSL 15:117db924cf7c 1003 cau_des_decrypt(out + offset, (byte*)des->key[0], out + offset);
wolfSSL 15:117db924cf7c 1004 #else
wolfSSL 15:117db924cf7c 1005 MMCAU_DES_DecryptEcb(in + offset , (byte*)des->key[2], out + offset);
wolfSSL 15:117db924cf7c 1006 MMCAU_DES_EncryptEcb(out + offset, (byte*)des->key[1], out + offset);
wolfSSL 15:117db924cf7c 1007 MMCAU_DES_DecryptEcb(out + offset, (byte*)des->key[0], out + offset);
wolfSSL 15:117db924cf7c 1008 #endif
wolfSSL 15:117db924cf7c 1009 wolfSSL_CryptHwMutexUnLock();
wolfSSL 15:117db924cf7c 1010
wolfSSL 15:117db924cf7c 1011 /* XOR block with IV for CBC */
wolfSSL 15:117db924cf7c 1012 for (i = 0; i < DES_BLOCK_SIZE; i++)
wolfSSL 15:117db924cf7c 1013 (out + offset)[i] ^= iv[i];
wolfSSL 15:117db924cf7c 1014
wolfSSL 15:117db924cf7c 1015 /* store IV for next block */
wolfSSL 15:117db924cf7c 1016 XMEMCPY(iv, temp_block, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1017
wolfSSL 15:117db924cf7c 1018 len -= DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1019 offset += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1020 }
wolfSSL 15:117db924cf7c 1021
wolfSSL 15:117db924cf7c 1022 return ret;
wolfSSL 15:117db924cf7c 1023 }
wolfSSL 15:117db924cf7c 1024
wolfSSL 15:117db924cf7c 1025
wolfSSL 15:117db924cf7c 1026 #elif defined(WOLFSSL_PIC32MZ_CRYPT)
wolfSSL 15:117db924cf7c 1027
wolfSSL 15:117db924cf7c 1028 /* PIC32MZ DES hardware requires size multiple of block size */
wolfSSL 15:117db924cf7c 1029 #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
wolfSSL 15:117db924cf7c 1030
wolfSSL 15:117db924cf7c 1031 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 1032 {
wolfSSL 15:117db924cf7c 1033 if (des == NULL || key == NULL || iv == NULL)
wolfSSL 15:117db924cf7c 1034 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1035
wolfSSL 15:117db924cf7c 1036 XMEMCPY(des->key, key, DES_KEYLEN);
wolfSSL 15:117db924cf7c 1037 XMEMCPY(des->reg, iv, DES_IVLEN);
wolfSSL 15:117db924cf7c 1038
wolfSSL 15:117db924cf7c 1039 return 0;
wolfSSL 15:117db924cf7c 1040 }
wolfSSL 15:117db924cf7c 1041
wolfSSL 15:117db924cf7c 1042 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 1043 {
wolfSSL 15:117db924cf7c 1044 if (des == NULL || key == NULL || iv == NULL)
wolfSSL 15:117db924cf7c 1045 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1046
wolfSSL 15:117db924cf7c 1047 XMEMCPY(des->key[0], key, DES3_KEYLEN);
wolfSSL 15:117db924cf7c 1048 XMEMCPY(des->reg, iv, DES3_IVLEN);
wolfSSL 15:117db924cf7c 1049
wolfSSL 15:117db924cf7c 1050 return 0;
wolfSSL 15:117db924cf7c 1051 }
wolfSSL 15:117db924cf7c 1052
wolfSSL 15:117db924cf7c 1053 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1054 {
wolfSSL 15:117db924cf7c 1055 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1056
wolfSSL 15:117db924cf7c 1057 if (des == NULL || out == NULL || in == NULL)
wolfSSL 15:117db924cf7c 1058 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1059
wolfSSL 15:117db924cf7c 1060 return wc_Pic32DesCrypt(des->key, DES_KEYLEN, des->reg, DES_IVLEN,
wolfSSL 15:117db924cf7c 1061 out, in, (blocks * DES_BLOCK_SIZE),
wolfSSL 15:117db924cf7c 1062 PIC32_ENCRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC);
wolfSSL 15:117db924cf7c 1063 }
wolfSSL 15:117db924cf7c 1064
wolfSSL 15:117db924cf7c 1065 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1066 {
wolfSSL 15:117db924cf7c 1067 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1068
wolfSSL 15:117db924cf7c 1069 if (des == NULL || out == NULL || in == NULL)
wolfSSL 15:117db924cf7c 1070 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1071
wolfSSL 15:117db924cf7c 1072 return wc_Pic32DesCrypt(des->key, DES_KEYLEN, des->reg, DES_IVLEN,
wolfSSL 15:117db924cf7c 1073 out, in, (blocks * DES_BLOCK_SIZE),
wolfSSL 15:117db924cf7c 1074 PIC32_DECRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC);
wolfSSL 15:117db924cf7c 1075 }
wolfSSL 15:117db924cf7c 1076
wolfSSL 15:117db924cf7c 1077 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1078 {
wolfSSL 15:117db924cf7c 1079 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1080
wolfSSL 15:117db924cf7c 1081 if (des == NULL || out == NULL || in == NULL)
wolfSSL 15:117db924cf7c 1082 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1083
wolfSSL 15:117db924cf7c 1084 return wc_Pic32DesCrypt(des->key[0], DES3_KEYLEN, des->reg, DES3_IVLEN,
wolfSSL 15:117db924cf7c 1085 out, in, (blocks * DES_BLOCK_SIZE),
wolfSSL 15:117db924cf7c 1086 PIC32_ENCRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC);
wolfSSL 15:117db924cf7c 1087 }
wolfSSL 15:117db924cf7c 1088
wolfSSL 15:117db924cf7c 1089 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1090 {
wolfSSL 15:117db924cf7c 1091 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1092
wolfSSL 15:117db924cf7c 1093 if (des == NULL || out == NULL || in == NULL)
wolfSSL 15:117db924cf7c 1094 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1095
wolfSSL 15:117db924cf7c 1096 return wc_Pic32DesCrypt(des->key[0], DES3_KEYLEN, des->reg, DES3_IVLEN,
wolfSSL 15:117db924cf7c 1097 out, in, (blocks * DES_BLOCK_SIZE),
wolfSSL 15:117db924cf7c 1098 PIC32_DECRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC);
wolfSSL 15:117db924cf7c 1099 }
wolfSSL 15:117db924cf7c 1100
wolfSSL 15:117db924cf7c 1101 #ifdef WOLFSSL_DES_ECB
wolfSSL 15:117db924cf7c 1102 int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1103 {
wolfSSL 15:117db924cf7c 1104 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1105
wolfSSL 15:117db924cf7c 1106 if (des == NULL || out == NULL || in == NULL)
wolfSSL 15:117db924cf7c 1107 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1108
wolfSSL 15:117db924cf7c 1109 return wc_Pic32DesCrypt(des->key, DES_KEYLEN, des->reg, DES_IVLEN,
wolfSSL 15:117db924cf7c 1110 out, in, (blocks * DES_BLOCK_SIZE),
wolfSSL 15:117db924cf7c 1111 PIC32_ENCRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_ECB);
wolfSSL 15:117db924cf7c 1112 }
wolfSSL 15:117db924cf7c 1113
wolfSSL 15:117db924cf7c 1114 int wc_Des3_EcbEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1115 {
wolfSSL 15:117db924cf7c 1116 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1117
wolfSSL 15:117db924cf7c 1118 if (des == NULL || out == NULL || in == NULL)
wolfSSL 15:117db924cf7c 1119 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1120
wolfSSL 15:117db924cf7c 1121 return wc_Pic32DesCrypt(des->key[0], DES3_KEYLEN, des->reg, DES3_IVLEN,
wolfSSL 15:117db924cf7c 1122 out, in, (blocks * DES_BLOCK_SIZE),
wolfSSL 15:117db924cf7c 1123 PIC32_ENCRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TECB);
wolfSSL 15:117db924cf7c 1124 }
wolfSSL 15:117db924cf7c 1125 #endif /* WOLFSSL_DES_ECB */
wolfSSL 15:117db924cf7c 1126
wolfSSL 15:117db924cf7c 1127 #else
wolfSSL 15:117db924cf7c 1128 #define NEED_SOFT_DES
wolfSSL 15:117db924cf7c 1129
wolfSSL 15:117db924cf7c 1130 #endif
wolfSSL 15:117db924cf7c 1131
wolfSSL 15:117db924cf7c 1132
wolfSSL 15:117db924cf7c 1133 #ifdef NEED_SOFT_DES
wolfSSL 15:117db924cf7c 1134
wolfSSL 15:117db924cf7c 1135 /* permuted choice table (key) */
wolfSSL 15:117db924cf7c 1136 static const byte pc1[] = {
wolfSSL 15:117db924cf7c 1137 57, 49, 41, 33, 25, 17, 9,
wolfSSL 15:117db924cf7c 1138 1, 58, 50, 42, 34, 26, 18,
wolfSSL 15:117db924cf7c 1139 10, 2, 59, 51, 43, 35, 27,
wolfSSL 15:117db924cf7c 1140 19, 11, 3, 60, 52, 44, 36,
wolfSSL 15:117db924cf7c 1141
wolfSSL 15:117db924cf7c 1142 63, 55, 47, 39, 31, 23, 15,
wolfSSL 15:117db924cf7c 1143 7, 62, 54, 46, 38, 30, 22,
wolfSSL 15:117db924cf7c 1144 14, 6, 61, 53, 45, 37, 29,
wolfSSL 15:117db924cf7c 1145 21, 13, 5, 28, 20, 12, 4
wolfSSL 15:117db924cf7c 1146 };
wolfSSL 15:117db924cf7c 1147
wolfSSL 15:117db924cf7c 1148 /* number left rotations of pc1 */
wolfSSL 15:117db924cf7c 1149 static const byte totrot[] = {
wolfSSL 15:117db924cf7c 1150 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
wolfSSL 15:117db924cf7c 1151 };
wolfSSL 15:117db924cf7c 1152
wolfSSL 15:117db924cf7c 1153 /* permuted choice key (table) */
wolfSSL 15:117db924cf7c 1154 static const byte pc2[] = {
wolfSSL 15:117db924cf7c 1155 14, 17, 11, 24, 1, 5,
wolfSSL 15:117db924cf7c 1156 3, 28, 15, 6, 21, 10,
wolfSSL 15:117db924cf7c 1157 23, 19, 12, 4, 26, 8,
wolfSSL 15:117db924cf7c 1158 16, 7, 27, 20, 13, 2,
wolfSSL 15:117db924cf7c 1159 41, 52, 31, 37, 47, 55,
wolfSSL 15:117db924cf7c 1160 30, 40, 51, 45, 33, 48,
wolfSSL 15:117db924cf7c 1161 44, 49, 39, 56, 34, 53,
wolfSSL 15:117db924cf7c 1162 46, 42, 50, 36, 29, 32
wolfSSL 15:117db924cf7c 1163 };
wolfSSL 15:117db924cf7c 1164
wolfSSL 15:117db924cf7c 1165 /* End of DES-defined tables */
wolfSSL 15:117db924cf7c 1166
wolfSSL 15:117db924cf7c 1167 /* bit 0 is left-most in byte */
wolfSSL 15:117db924cf7c 1168 static const int bytebit[] = {
wolfSSL 15:117db924cf7c 1169 0200,0100,040,020,010,04,02,01
wolfSSL 15:117db924cf7c 1170 };
wolfSSL 15:117db924cf7c 1171
wolfSSL 15:117db924cf7c 1172 static const word32 Spbox[8][64] = {
wolfSSL 15:117db924cf7c 1173 { 0x01010400,0x00000000,0x00010000,0x01010404,
wolfSSL 15:117db924cf7c 1174 0x01010004,0x00010404,0x00000004,0x00010000,
wolfSSL 15:117db924cf7c 1175 0x00000400,0x01010400,0x01010404,0x00000400,
wolfSSL 15:117db924cf7c 1176 0x01000404,0x01010004,0x01000000,0x00000004,
wolfSSL 15:117db924cf7c 1177 0x00000404,0x01000400,0x01000400,0x00010400,
wolfSSL 15:117db924cf7c 1178 0x00010400,0x01010000,0x01010000,0x01000404,
wolfSSL 15:117db924cf7c 1179 0x00010004,0x01000004,0x01000004,0x00010004,
wolfSSL 15:117db924cf7c 1180 0x00000000,0x00000404,0x00010404,0x01000000,
wolfSSL 15:117db924cf7c 1181 0x00010000,0x01010404,0x00000004,0x01010000,
wolfSSL 15:117db924cf7c 1182 0x01010400,0x01000000,0x01000000,0x00000400,
wolfSSL 15:117db924cf7c 1183 0x01010004,0x00010000,0x00010400,0x01000004,
wolfSSL 15:117db924cf7c 1184 0x00000400,0x00000004,0x01000404,0x00010404,
wolfSSL 15:117db924cf7c 1185 0x01010404,0x00010004,0x01010000,0x01000404,
wolfSSL 15:117db924cf7c 1186 0x01000004,0x00000404,0x00010404,0x01010400,
wolfSSL 15:117db924cf7c 1187 0x00000404,0x01000400,0x01000400,0x00000000,
wolfSSL 15:117db924cf7c 1188 0x00010004,0x00010400,0x00000000,0x01010004},
wolfSSL 15:117db924cf7c 1189 { 0x80108020,0x80008000,0x00008000,0x00108020,
wolfSSL 15:117db924cf7c 1190 0x00100000,0x00000020,0x80100020,0x80008020,
wolfSSL 15:117db924cf7c 1191 0x80000020,0x80108020,0x80108000,0x80000000,
wolfSSL 15:117db924cf7c 1192 0x80008000,0x00100000,0x00000020,0x80100020,
wolfSSL 15:117db924cf7c 1193 0x00108000,0x00100020,0x80008020,0x00000000,
wolfSSL 15:117db924cf7c 1194 0x80000000,0x00008000,0x00108020,0x80100000,
wolfSSL 15:117db924cf7c 1195 0x00100020,0x80000020,0x00000000,0x00108000,
wolfSSL 15:117db924cf7c 1196 0x00008020,0x80108000,0x80100000,0x00008020,
wolfSSL 15:117db924cf7c 1197 0x00000000,0x00108020,0x80100020,0x00100000,
wolfSSL 15:117db924cf7c 1198 0x80008020,0x80100000,0x80108000,0x00008000,
wolfSSL 15:117db924cf7c 1199 0x80100000,0x80008000,0x00000020,0x80108020,
wolfSSL 15:117db924cf7c 1200 0x00108020,0x00000020,0x00008000,0x80000000,
wolfSSL 15:117db924cf7c 1201 0x00008020,0x80108000,0x00100000,0x80000020,
wolfSSL 15:117db924cf7c 1202 0x00100020,0x80008020,0x80000020,0x00100020,
wolfSSL 15:117db924cf7c 1203 0x00108000,0x00000000,0x80008000,0x00008020,
wolfSSL 15:117db924cf7c 1204 0x80000000,0x80100020,0x80108020,0x00108000},
wolfSSL 15:117db924cf7c 1205 { 0x00000208,0x08020200,0x00000000,0x08020008,
wolfSSL 15:117db924cf7c 1206 0x08000200,0x00000000,0x00020208,0x08000200,
wolfSSL 15:117db924cf7c 1207 0x00020008,0x08000008,0x08000008,0x00020000,
wolfSSL 15:117db924cf7c 1208 0x08020208,0x00020008,0x08020000,0x00000208,
wolfSSL 15:117db924cf7c 1209 0x08000000,0x00000008,0x08020200,0x00000200,
wolfSSL 15:117db924cf7c 1210 0x00020200,0x08020000,0x08020008,0x00020208,
wolfSSL 15:117db924cf7c 1211 0x08000208,0x00020200,0x00020000,0x08000208,
wolfSSL 15:117db924cf7c 1212 0x00000008,0x08020208,0x00000200,0x08000000,
wolfSSL 15:117db924cf7c 1213 0x08020200,0x08000000,0x00020008,0x00000208,
wolfSSL 15:117db924cf7c 1214 0x00020000,0x08020200,0x08000200,0x00000000,
wolfSSL 15:117db924cf7c 1215 0x00000200,0x00020008,0x08020208,0x08000200,
wolfSSL 15:117db924cf7c 1216 0x08000008,0x00000200,0x00000000,0x08020008,
wolfSSL 15:117db924cf7c 1217 0x08000208,0x00020000,0x08000000,0x08020208,
wolfSSL 15:117db924cf7c 1218 0x00000008,0x00020208,0x00020200,0x08000008,
wolfSSL 15:117db924cf7c 1219 0x08020000,0x08000208,0x00000208,0x08020000,
wolfSSL 15:117db924cf7c 1220 0x00020208,0x00000008,0x08020008,0x00020200},
wolfSSL 15:117db924cf7c 1221 { 0x00802001,0x00002081,0x00002081,0x00000080,
wolfSSL 15:117db924cf7c 1222 0x00802080,0x00800081,0x00800001,0x00002001,
wolfSSL 15:117db924cf7c 1223 0x00000000,0x00802000,0x00802000,0x00802081,
wolfSSL 15:117db924cf7c 1224 0x00000081,0x00000000,0x00800080,0x00800001,
wolfSSL 15:117db924cf7c 1225 0x00000001,0x00002000,0x00800000,0x00802001,
wolfSSL 15:117db924cf7c 1226 0x00000080,0x00800000,0x00002001,0x00002080,
wolfSSL 15:117db924cf7c 1227 0x00800081,0x00000001,0x00002080,0x00800080,
wolfSSL 15:117db924cf7c 1228 0x00002000,0x00802080,0x00802081,0x00000081,
wolfSSL 15:117db924cf7c 1229 0x00800080,0x00800001,0x00802000,0x00802081,
wolfSSL 15:117db924cf7c 1230 0x00000081,0x00000000,0x00000000,0x00802000,
wolfSSL 15:117db924cf7c 1231 0x00002080,0x00800080,0x00800081,0x00000001,
wolfSSL 15:117db924cf7c 1232 0x00802001,0x00002081,0x00002081,0x00000080,
wolfSSL 15:117db924cf7c 1233 0x00802081,0x00000081,0x00000001,0x00002000,
wolfSSL 15:117db924cf7c 1234 0x00800001,0x00002001,0x00802080,0x00800081,
wolfSSL 15:117db924cf7c 1235 0x00002001,0x00002080,0x00800000,0x00802001,
wolfSSL 15:117db924cf7c 1236 0x00000080,0x00800000,0x00002000,0x00802080},
wolfSSL 15:117db924cf7c 1237 { 0x00000100,0x02080100,0x02080000,0x42000100,
wolfSSL 15:117db924cf7c 1238 0x00080000,0x00000100,0x40000000,0x02080000,
wolfSSL 15:117db924cf7c 1239 0x40080100,0x00080000,0x02000100,0x40080100,
wolfSSL 15:117db924cf7c 1240 0x42000100,0x42080000,0x00080100,0x40000000,
wolfSSL 15:117db924cf7c 1241 0x02000000,0x40080000,0x40080000,0x00000000,
wolfSSL 15:117db924cf7c 1242 0x40000100,0x42080100,0x42080100,0x02000100,
wolfSSL 15:117db924cf7c 1243 0x42080000,0x40000100,0x00000000,0x42000000,
wolfSSL 15:117db924cf7c 1244 0x02080100,0x02000000,0x42000000,0x00080100,
wolfSSL 15:117db924cf7c 1245 0x00080000,0x42000100,0x00000100,0x02000000,
wolfSSL 15:117db924cf7c 1246 0x40000000,0x02080000,0x42000100,0x40080100,
wolfSSL 15:117db924cf7c 1247 0x02000100,0x40000000,0x42080000,0x02080100,
wolfSSL 15:117db924cf7c 1248 0x40080100,0x00000100,0x02000000,0x42080000,
wolfSSL 15:117db924cf7c 1249 0x42080100,0x00080100,0x42000000,0x42080100,
wolfSSL 15:117db924cf7c 1250 0x02080000,0x00000000,0x40080000,0x42000000,
wolfSSL 15:117db924cf7c 1251 0x00080100,0x02000100,0x40000100,0x00080000,
wolfSSL 15:117db924cf7c 1252 0x00000000,0x40080000,0x02080100,0x40000100},
wolfSSL 15:117db924cf7c 1253 { 0x20000010,0x20400000,0x00004000,0x20404010,
wolfSSL 15:117db924cf7c 1254 0x20400000,0x00000010,0x20404010,0x00400000,
wolfSSL 15:117db924cf7c 1255 0x20004000,0x00404010,0x00400000,0x20000010,
wolfSSL 15:117db924cf7c 1256 0x00400010,0x20004000,0x20000000,0x00004010,
wolfSSL 15:117db924cf7c 1257 0x00000000,0x00400010,0x20004010,0x00004000,
wolfSSL 15:117db924cf7c 1258 0x00404000,0x20004010,0x00000010,0x20400010,
wolfSSL 15:117db924cf7c 1259 0x20400010,0x00000000,0x00404010,0x20404000,
wolfSSL 15:117db924cf7c 1260 0x00004010,0x00404000,0x20404000,0x20000000,
wolfSSL 15:117db924cf7c 1261 0x20004000,0x00000010,0x20400010,0x00404000,
wolfSSL 15:117db924cf7c 1262 0x20404010,0x00400000,0x00004010,0x20000010,
wolfSSL 15:117db924cf7c 1263 0x00400000,0x20004000,0x20000000,0x00004010,
wolfSSL 15:117db924cf7c 1264 0x20000010,0x20404010,0x00404000,0x20400000,
wolfSSL 15:117db924cf7c 1265 0x00404010,0x20404000,0x00000000,0x20400010,
wolfSSL 15:117db924cf7c 1266 0x00000010,0x00004000,0x20400000,0x00404010,
wolfSSL 15:117db924cf7c 1267 0x00004000,0x00400010,0x20004010,0x00000000,
wolfSSL 15:117db924cf7c 1268 0x20404000,0x20000000,0x00400010,0x20004010},
wolfSSL 15:117db924cf7c 1269 { 0x00200000,0x04200002,0x04000802,0x00000000,
wolfSSL 15:117db924cf7c 1270 0x00000800,0x04000802,0x00200802,0x04200800,
wolfSSL 15:117db924cf7c 1271 0x04200802,0x00200000,0x00000000,0x04000002,
wolfSSL 15:117db924cf7c 1272 0x00000002,0x04000000,0x04200002,0x00000802,
wolfSSL 15:117db924cf7c 1273 0x04000800,0x00200802,0x00200002,0x04000800,
wolfSSL 15:117db924cf7c 1274 0x04000002,0x04200000,0x04200800,0x00200002,
wolfSSL 15:117db924cf7c 1275 0x04200000,0x00000800,0x00000802,0x04200802,
wolfSSL 15:117db924cf7c 1276 0x00200800,0x00000002,0x04000000,0x00200800,
wolfSSL 15:117db924cf7c 1277 0x04000000,0x00200800,0x00200000,0x04000802,
wolfSSL 15:117db924cf7c 1278 0x04000802,0x04200002,0x04200002,0x00000002,
wolfSSL 15:117db924cf7c 1279 0x00200002,0x04000000,0x04000800,0x00200000,
wolfSSL 15:117db924cf7c 1280 0x04200800,0x00000802,0x00200802,0x04200800,
wolfSSL 15:117db924cf7c 1281 0x00000802,0x04000002,0x04200802,0x04200000,
wolfSSL 15:117db924cf7c 1282 0x00200800,0x00000000,0x00000002,0x04200802,
wolfSSL 15:117db924cf7c 1283 0x00000000,0x00200802,0x04200000,0x00000800,
wolfSSL 15:117db924cf7c 1284 0x04000002,0x04000800,0x00000800,0x00200002},
wolfSSL 15:117db924cf7c 1285 { 0x10001040,0x00001000,0x00040000,0x10041040,
wolfSSL 15:117db924cf7c 1286 0x10000000,0x10001040,0x00000040,0x10000000,
wolfSSL 15:117db924cf7c 1287 0x00040040,0x10040000,0x10041040,0x00041000,
wolfSSL 15:117db924cf7c 1288 0x10041000,0x00041040,0x00001000,0x00000040,
wolfSSL 15:117db924cf7c 1289 0x10040000,0x10000040,0x10001000,0x00001040,
wolfSSL 15:117db924cf7c 1290 0x00041000,0x00040040,0x10040040,0x10041000,
wolfSSL 15:117db924cf7c 1291 0x00001040,0x00000000,0x00000000,0x10040040,
wolfSSL 15:117db924cf7c 1292 0x10000040,0x10001000,0x00041040,0x00040000,
wolfSSL 15:117db924cf7c 1293 0x00041040,0x00040000,0x10041000,0x00001000,
wolfSSL 15:117db924cf7c 1294 0x00000040,0x10040040,0x00001000,0x00041040,
wolfSSL 15:117db924cf7c 1295 0x10001000,0x00000040,0x10000040,0x10040000,
wolfSSL 15:117db924cf7c 1296 0x10040040,0x10000000,0x00040000,0x10001040,
wolfSSL 15:117db924cf7c 1297 0x00000000,0x10041040,0x00040040,0x10000040,
wolfSSL 15:117db924cf7c 1298 0x10040000,0x10001000,0x10001040,0x00000000,
wolfSSL 15:117db924cf7c 1299 0x10041040,0x00041000,0x00041000,0x00001040,
wolfSSL 15:117db924cf7c 1300 0x00001040,0x00040040,0x10000000,0x10041000}
wolfSSL 15:117db924cf7c 1301 };
wolfSSL 15:117db924cf7c 1302
wolfSSL 15:117db924cf7c 1303 static WC_INLINE void IPERM(word32* left, word32* right)
wolfSSL 15:117db924cf7c 1304 {
wolfSSL 15:117db924cf7c 1305 word32 work;
wolfSSL 15:117db924cf7c 1306
wolfSSL 15:117db924cf7c 1307 *right = rotlFixed(*right, 4U);
wolfSSL 15:117db924cf7c 1308 work = (*left ^ *right) & 0xf0f0f0f0;
wolfSSL 15:117db924cf7c 1309 *left ^= work;
wolfSSL 15:117db924cf7c 1310
wolfSSL 15:117db924cf7c 1311 *right = rotrFixed(*right^work, 20U);
wolfSSL 15:117db924cf7c 1312 work = (*left ^ *right) & 0xffff0000;
wolfSSL 15:117db924cf7c 1313 *left ^= work;
wolfSSL 15:117db924cf7c 1314
wolfSSL 15:117db924cf7c 1315 *right = rotrFixed(*right^work, 18U);
wolfSSL 15:117db924cf7c 1316 work = (*left ^ *right) & 0x33333333;
wolfSSL 15:117db924cf7c 1317 *left ^= work;
wolfSSL 15:117db924cf7c 1318
wolfSSL 15:117db924cf7c 1319 *right = rotrFixed(*right^work, 6U);
wolfSSL 15:117db924cf7c 1320 work = (*left ^ *right) & 0x00ff00ff;
wolfSSL 15:117db924cf7c 1321 *left ^= work;
wolfSSL 15:117db924cf7c 1322
wolfSSL 15:117db924cf7c 1323 *right = rotlFixed(*right^work, 9U);
wolfSSL 15:117db924cf7c 1324 work = (*left ^ *right) & 0xaaaaaaaa;
wolfSSL 15:117db924cf7c 1325 *left = rotlFixed(*left^work, 1U);
wolfSSL 15:117db924cf7c 1326 *right ^= work;
wolfSSL 15:117db924cf7c 1327 }
wolfSSL 15:117db924cf7c 1328
wolfSSL 15:117db924cf7c 1329 static WC_INLINE void FPERM(word32* left, word32* right)
wolfSSL 15:117db924cf7c 1330 {
wolfSSL 15:117db924cf7c 1331 word32 work;
wolfSSL 15:117db924cf7c 1332
wolfSSL 15:117db924cf7c 1333 *right = rotrFixed(*right, 1U);
wolfSSL 15:117db924cf7c 1334 work = (*left ^ *right) & 0xaaaaaaaa;
wolfSSL 15:117db924cf7c 1335 *right ^= work;
wolfSSL 15:117db924cf7c 1336
wolfSSL 15:117db924cf7c 1337 *left = rotrFixed(*left^work, 9U);
wolfSSL 15:117db924cf7c 1338 work = (*left ^ *right) & 0x00ff00ff;
wolfSSL 15:117db924cf7c 1339 *right ^= work;
wolfSSL 15:117db924cf7c 1340
wolfSSL 15:117db924cf7c 1341 *left = rotlFixed(*left^work, 6U);
wolfSSL 15:117db924cf7c 1342 work = (*left ^ *right) & 0x33333333;
wolfSSL 15:117db924cf7c 1343 *right ^= work;
wolfSSL 15:117db924cf7c 1344
wolfSSL 15:117db924cf7c 1345 *left = rotlFixed(*left^work, 18U);
wolfSSL 15:117db924cf7c 1346 work = (*left ^ *right) & 0xffff0000;
wolfSSL 15:117db924cf7c 1347 *right ^= work;
wolfSSL 15:117db924cf7c 1348
wolfSSL 15:117db924cf7c 1349 *left = rotlFixed(*left^work, 20U);
wolfSSL 15:117db924cf7c 1350 work = (*left ^ *right) & 0xf0f0f0f0;
wolfSSL 15:117db924cf7c 1351 *right ^= work;
wolfSSL 15:117db924cf7c 1352
wolfSSL 15:117db924cf7c 1353 *left = rotrFixed(*left^work, 4U);
wolfSSL 15:117db924cf7c 1354 }
wolfSSL 15:117db924cf7c 1355
wolfSSL 15:117db924cf7c 1356 static int DesSetKey(const byte* key, int dir, word32* out)
wolfSSL 15:117db924cf7c 1357 {
wolfSSL 15:117db924cf7c 1358 #define DES_KEY_BUFFER_SIZE (56+56+8)
wolfSSL 15:117db924cf7c 1359 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1360 byte* buffer = (byte*)XMALLOC(DES_KEY_BUFFER_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1361
wolfSSL 15:117db924cf7c 1362 if (buffer == NULL)
wolfSSL 15:117db924cf7c 1363 return MEMORY_E;
wolfSSL 15:117db924cf7c 1364 #else
wolfSSL 15:117db924cf7c 1365 byte buffer[DES_KEY_BUFFER_SIZE];
wolfSSL 15:117db924cf7c 1366 #endif
wolfSSL 15:117db924cf7c 1367
wolfSSL 15:117db924cf7c 1368 {
wolfSSL 15:117db924cf7c 1369 byte* const pc1m = buffer; /* place to modify pc1 into */
wolfSSL 15:117db924cf7c 1370 byte* const pcr = pc1m + 56; /* place to rotate pc1 into */
wolfSSL 15:117db924cf7c 1371 byte* const ks = pcr + 56;
wolfSSL 15:117db924cf7c 1372 register int i, j, l;
wolfSSL 15:117db924cf7c 1373 int m;
wolfSSL 15:117db924cf7c 1374
wolfSSL 15:117db924cf7c 1375 for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */
wolfSSL 15:117db924cf7c 1376 l = pc1[j] - 1; /* integer bit location */
wolfSSL 15:117db924cf7c 1377 m = l & 07; /* find bit */
wolfSSL 15:117db924cf7c 1378 pc1m[j] = (key[l >> 3] & /* find which key byte l is in */
wolfSSL 15:117db924cf7c 1379 bytebit[m]) /* and which bit of that byte */
wolfSSL 15:117db924cf7c 1380 ? 1 : 0; /* and store 1-bit result */
wolfSSL 15:117db924cf7c 1381 }
wolfSSL 15:117db924cf7c 1382
wolfSSL 15:117db924cf7c 1383 for (i = 0; i < 16; i++) { /* key chunk for each iteration */
wolfSSL 15:117db924cf7c 1384 XMEMSET(ks, 0, 8); /* Clear key schedule */
wolfSSL 15:117db924cf7c 1385
wolfSSL 15:117db924cf7c 1386 for (j = 0; j < 56; j++) /* rotate pc1 the right amount */
wolfSSL 15:117db924cf7c 1387 pcr[j] =
wolfSSL 15:117db924cf7c 1388 pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l : l-28];
wolfSSL 15:117db924cf7c 1389
wolfSSL 15:117db924cf7c 1390 /* rotate left and right halves independently */
wolfSSL 15:117db924cf7c 1391 for (j = 0; j < 48; j++) { /* select bits individually */
wolfSSL 15:117db924cf7c 1392 if (pcr[pc2[j] - 1]) { /* check bit that goes to ks[j] */
wolfSSL 15:117db924cf7c 1393 l= j % 6; /* mask it in if it's there */
wolfSSL 15:117db924cf7c 1394 ks[j/6] |= bytebit[l] >> 2;
wolfSSL 15:117db924cf7c 1395 }
wolfSSL 15:117db924cf7c 1396 }
wolfSSL 15:117db924cf7c 1397
wolfSSL 15:117db924cf7c 1398 /* Now convert to odd/even interleaved form for use in F */
wolfSSL 15:117db924cf7c 1399 out[2*i] = ((word32) ks[0] << 24)
wolfSSL 15:117db924cf7c 1400 | ((word32) ks[2] << 16)
wolfSSL 15:117db924cf7c 1401 | ((word32) ks[4] << 8)
wolfSSL 15:117db924cf7c 1402 | ((word32) ks[6]);
wolfSSL 15:117db924cf7c 1403
wolfSSL 15:117db924cf7c 1404 out[2*i + 1] = ((word32) ks[1] << 24)
wolfSSL 15:117db924cf7c 1405 | ((word32) ks[3] << 16)
wolfSSL 15:117db924cf7c 1406 | ((word32) ks[5] << 8)
wolfSSL 15:117db924cf7c 1407 | ((word32) ks[7]);
wolfSSL 15:117db924cf7c 1408 }
wolfSSL 15:117db924cf7c 1409
wolfSSL 15:117db924cf7c 1410 /* reverse key schedule order */
wolfSSL 15:117db924cf7c 1411 if (dir == DES_DECRYPTION) {
wolfSSL 15:117db924cf7c 1412 for (i = 0; i < 16; i += 2) {
wolfSSL 15:117db924cf7c 1413 word32 swap = out[i];
wolfSSL 15:117db924cf7c 1414 out[i] = out[DES_KS_SIZE - 2 - i];
wolfSSL 15:117db924cf7c 1415 out[DES_KS_SIZE - 2 - i] = swap;
wolfSSL 15:117db924cf7c 1416
wolfSSL 15:117db924cf7c 1417 swap = out[i + 1];
wolfSSL 15:117db924cf7c 1418 out[i + 1] = out[DES_KS_SIZE - 1 - i];
wolfSSL 15:117db924cf7c 1419 out[DES_KS_SIZE - 1 - i] = swap;
wolfSSL 15:117db924cf7c 1420 }
wolfSSL 15:117db924cf7c 1421 }
wolfSSL 15:117db924cf7c 1422
wolfSSL 15:117db924cf7c 1423 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1424 XFREE(buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1425 #endif
wolfSSL 15:117db924cf7c 1426 }
wolfSSL 15:117db924cf7c 1427
wolfSSL 15:117db924cf7c 1428 return 0;
wolfSSL 15:117db924cf7c 1429 }
wolfSSL 15:117db924cf7c 1430
wolfSSL 15:117db924cf7c 1431 int wc_Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 1432 {
wolfSSL 15:117db924cf7c 1433 wc_Des_SetIV(des, iv);
wolfSSL 15:117db924cf7c 1434
wolfSSL 15:117db924cf7c 1435 return DesSetKey(key, dir, des->key);
wolfSSL 15:117db924cf7c 1436 }
wolfSSL 15:117db924cf7c 1437
wolfSSL 15:117db924cf7c 1438 int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
wolfSSL 15:117db924cf7c 1439 {
wolfSSL 15:117db924cf7c 1440 int ret;
wolfSSL 15:117db924cf7c 1441
wolfSSL 15:117db924cf7c 1442 if (des == NULL || key == NULL || dir < 0) {
wolfSSL 15:117db924cf7c 1443 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1444 }
wolfSSL 15:117db924cf7c 1445
wolfSSL 15:117db924cf7c 1446 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
wolfSSL 15:117db924cf7c 1447 if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES) {
wolfSSL 15:117db924cf7c 1448 /* key_raw holds orignal key copy */
wolfSSL 15:117db924cf7c 1449 des->key_raw = key;
wolfSSL 15:117db924cf7c 1450 des->iv_raw = iv;
wolfSSL 15:117db924cf7c 1451
wolfSSL 15:117db924cf7c 1452 /* continue on to set normal key for smaller DES operations */
wolfSSL 15:117db924cf7c 1453 }
wolfSSL 15:117db924cf7c 1454 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1455
wolfSSL 15:117db924cf7c 1456 ret = DesSetKey(key + (dir == DES_ENCRYPTION ? 0:16), dir, des->key[0]);
wolfSSL 15:117db924cf7c 1457 if (ret != 0)
wolfSSL 15:117db924cf7c 1458 return ret;
wolfSSL 15:117db924cf7c 1459
wolfSSL 15:117db924cf7c 1460 ret = DesSetKey(key + 8, !dir, des->key[1]);
wolfSSL 15:117db924cf7c 1461 if (ret != 0)
wolfSSL 15:117db924cf7c 1462 return ret;
wolfSSL 15:117db924cf7c 1463
wolfSSL 15:117db924cf7c 1464 ret = DesSetKey(key + (dir == DES_DECRYPTION ? 0:16), dir, des->key[2]);
wolfSSL 15:117db924cf7c 1465 if (ret != 0)
wolfSSL 15:117db924cf7c 1466 return ret;
wolfSSL 15:117db924cf7c 1467
wolfSSL 15:117db924cf7c 1468 return wc_Des3_SetIV(des, iv);
wolfSSL 15:117db924cf7c 1469 }
wolfSSL 15:117db924cf7c 1470
wolfSSL 15:117db924cf7c 1471 static void DesRawProcessBlock(word32* lIn, word32* rIn, const word32* kptr)
wolfSSL 15:117db924cf7c 1472 {
wolfSSL 15:117db924cf7c 1473 word32 l = *lIn, r = *rIn, i;
wolfSSL 15:117db924cf7c 1474
wolfSSL 15:117db924cf7c 1475 for (i=0; i<8; i++)
wolfSSL 15:117db924cf7c 1476 {
wolfSSL 15:117db924cf7c 1477 word32 work = rotrFixed(r, 4U) ^ kptr[4*i+0];
wolfSSL 15:117db924cf7c 1478 l ^= Spbox[6][(work) & 0x3f]
wolfSSL 15:117db924cf7c 1479 ^ Spbox[4][(work >> 8) & 0x3f]
wolfSSL 15:117db924cf7c 1480 ^ Spbox[2][(work >> 16) & 0x3f]
wolfSSL 15:117db924cf7c 1481 ^ Spbox[0][(work >> 24) & 0x3f];
wolfSSL 15:117db924cf7c 1482 work = r ^ kptr[4*i+1];
wolfSSL 15:117db924cf7c 1483 l ^= Spbox[7][(work) & 0x3f]
wolfSSL 15:117db924cf7c 1484 ^ Spbox[5][(work >> 8) & 0x3f]
wolfSSL 15:117db924cf7c 1485 ^ Spbox[3][(work >> 16) & 0x3f]
wolfSSL 15:117db924cf7c 1486 ^ Spbox[1][(work >> 24) & 0x3f];
wolfSSL 15:117db924cf7c 1487
wolfSSL 15:117db924cf7c 1488 work = rotrFixed(l, 4U) ^ kptr[4*i+2];
wolfSSL 15:117db924cf7c 1489 r ^= Spbox[6][(work) & 0x3f]
wolfSSL 15:117db924cf7c 1490 ^ Spbox[4][(work >> 8) & 0x3f]
wolfSSL 15:117db924cf7c 1491 ^ Spbox[2][(work >> 16) & 0x3f]
wolfSSL 15:117db924cf7c 1492 ^ Spbox[0][(work >> 24) & 0x3f];
wolfSSL 15:117db924cf7c 1493 work = l ^ kptr[4*i+3];
wolfSSL 15:117db924cf7c 1494 r ^= Spbox[7][(work) & 0x3f]
wolfSSL 15:117db924cf7c 1495 ^ Spbox[5][(work >> 8) & 0x3f]
wolfSSL 15:117db924cf7c 1496 ^ Spbox[3][(work >> 16) & 0x3f]
wolfSSL 15:117db924cf7c 1497 ^ Spbox[1][(work >> 24) & 0x3f];
wolfSSL 15:117db924cf7c 1498 }
wolfSSL 15:117db924cf7c 1499
wolfSSL 15:117db924cf7c 1500 *lIn = l; *rIn = r;
wolfSSL 15:117db924cf7c 1501 }
wolfSSL 15:117db924cf7c 1502
wolfSSL 15:117db924cf7c 1503 static void DesProcessBlock(Des* des, const byte* in, byte* out)
wolfSSL 15:117db924cf7c 1504 {
wolfSSL 15:117db924cf7c 1505 word32 l, r;
wolfSSL 15:117db924cf7c 1506
wolfSSL 15:117db924cf7c 1507 XMEMCPY(&l, in, sizeof(l));
wolfSSL 15:117db924cf7c 1508 XMEMCPY(&r, in + sizeof(l), sizeof(r));
wolfSSL 15:117db924cf7c 1509 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 1510 l = ByteReverseWord32(l);
wolfSSL 15:117db924cf7c 1511 r = ByteReverseWord32(r);
wolfSSL 15:117db924cf7c 1512 #endif
wolfSSL 15:117db924cf7c 1513 IPERM(&l,&r);
wolfSSL 15:117db924cf7c 1514
wolfSSL 15:117db924cf7c 1515 DesRawProcessBlock(&l, &r, des->key);
wolfSSL 15:117db924cf7c 1516
wolfSSL 15:117db924cf7c 1517 FPERM(&l,&r);
wolfSSL 15:117db924cf7c 1518 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 1519 l = ByteReverseWord32(l);
wolfSSL 15:117db924cf7c 1520 r = ByteReverseWord32(r);
wolfSSL 15:117db924cf7c 1521 #endif
wolfSSL 15:117db924cf7c 1522 XMEMCPY(out, &r, sizeof(r));
wolfSSL 15:117db924cf7c 1523 XMEMCPY(out + sizeof(r), &l, sizeof(l));
wolfSSL 15:117db924cf7c 1524 }
wolfSSL 15:117db924cf7c 1525
wolfSSL 15:117db924cf7c 1526 static void Des3ProcessBlock(Des3* des, const byte* in, byte* out)
wolfSSL 15:117db924cf7c 1527 {
wolfSSL 15:117db924cf7c 1528 word32 l, r;
wolfSSL 15:117db924cf7c 1529
wolfSSL 15:117db924cf7c 1530 XMEMCPY(&l, in, sizeof(l));
wolfSSL 15:117db924cf7c 1531 XMEMCPY(&r, in + sizeof(l), sizeof(r));
wolfSSL 15:117db924cf7c 1532 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 1533 l = ByteReverseWord32(l);
wolfSSL 15:117db924cf7c 1534 r = ByteReverseWord32(r);
wolfSSL 15:117db924cf7c 1535 #endif
wolfSSL 15:117db924cf7c 1536 IPERM(&l,&r);
wolfSSL 15:117db924cf7c 1537
wolfSSL 15:117db924cf7c 1538 DesRawProcessBlock(&l, &r, des->key[0]);
wolfSSL 15:117db924cf7c 1539 DesRawProcessBlock(&r, &l, des->key[1]);
wolfSSL 15:117db924cf7c 1540 DesRawProcessBlock(&l, &r, des->key[2]);
wolfSSL 15:117db924cf7c 1541
wolfSSL 15:117db924cf7c 1542 FPERM(&l,&r);
wolfSSL 15:117db924cf7c 1543 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 1544 l = ByteReverseWord32(l);
wolfSSL 15:117db924cf7c 1545 r = ByteReverseWord32(r);
wolfSSL 15:117db924cf7c 1546 #endif
wolfSSL 15:117db924cf7c 1547 XMEMCPY(out, &r, sizeof(r));
wolfSSL 15:117db924cf7c 1548 XMEMCPY(out + sizeof(r), &l, sizeof(l));
wolfSSL 15:117db924cf7c 1549 }
wolfSSL 15:117db924cf7c 1550
wolfSSL 15:117db924cf7c 1551 int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1552 {
wolfSSL 15:117db924cf7c 1553 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1554
wolfSSL 15:117db924cf7c 1555 while (blocks--) {
wolfSSL 15:117db924cf7c 1556 xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1557 DesProcessBlock(des, (byte*)des->reg, (byte*)des->reg);
wolfSSL 15:117db924cf7c 1558 XMEMCPY(out, des->reg, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1559
wolfSSL 15:117db924cf7c 1560 out += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1561 in += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1562 }
wolfSSL 15:117db924cf7c 1563 return 0;
wolfSSL 15:117db924cf7c 1564 }
wolfSSL 15:117db924cf7c 1565
wolfSSL 15:117db924cf7c 1566 int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1567 {
wolfSSL 15:117db924cf7c 1568 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1569
wolfSSL 15:117db924cf7c 1570 while (blocks--) {
wolfSSL 15:117db924cf7c 1571 XMEMCPY(des->tmp, in, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1572 DesProcessBlock(des, (byte*)des->tmp, out);
wolfSSL 15:117db924cf7c 1573 xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1574 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1575
wolfSSL 15:117db924cf7c 1576 out += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1577 in += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1578 }
wolfSSL 15:117db924cf7c 1579 return 0;
wolfSSL 15:117db924cf7c 1580 }
wolfSSL 15:117db924cf7c 1581
wolfSSL 15:117db924cf7c 1582 int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1583 {
wolfSSL 15:117db924cf7c 1584 word32 blocks;
wolfSSL 15:117db924cf7c 1585
wolfSSL 15:117db924cf7c 1586 if (des == NULL || out == NULL || in == NULL) {
wolfSSL 15:117db924cf7c 1587 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1588 }
wolfSSL 15:117db924cf7c 1589
wolfSSL 15:117db924cf7c 1590 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
wolfSSL 15:117db924cf7c 1591 if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES &&
wolfSSL 15:117db924cf7c 1592 sz >= WC_ASYNC_THRESH_DES3_CBC) {
wolfSSL 15:117db924cf7c 1593 #if defined(HAVE_CAVIUM)
wolfSSL 15:117db924cf7c 1594 return NitroxDes3CbcEncrypt(des, out, in, sz);
wolfSSL 15:117db924cf7c 1595 #elif defined(HAVE_INTEL_QA)
wolfSSL 15:117db924cf7c 1596 return IntelQaSymDes3CbcEncrypt(&des->asyncDev, out, in, sz,
wolfSSL 15:117db924cf7c 1597 des->key_raw, DES3_KEYLEN, (byte*)des->iv_raw, DES3_IVLEN);
wolfSSL 15:117db924cf7c 1598 #else /* WOLFSSL_ASYNC_CRYPT_TEST */
wolfSSL 15:117db924cf7c 1599 if (wc_AsyncTestInit(&des->asyncDev, ASYNC_TEST_DES3_CBC_ENCRYPT)) {
wolfSSL 15:117db924cf7c 1600 WC_ASYNC_TEST* testDev = &des->asyncDev.test;
wolfSSL 15:117db924cf7c 1601 testDev->des.des = des;
wolfSSL 15:117db924cf7c 1602 testDev->des.out = out;
wolfSSL 15:117db924cf7c 1603 testDev->des.in = in;
wolfSSL 15:117db924cf7c 1604 testDev->des.sz = sz;
wolfSSL 15:117db924cf7c 1605 return WC_PENDING_E;
wolfSSL 15:117db924cf7c 1606 }
wolfSSL 15:117db924cf7c 1607 #endif
wolfSSL 15:117db924cf7c 1608 }
wolfSSL 15:117db924cf7c 1609 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1610
wolfSSL 15:117db924cf7c 1611 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1612 while (blocks--) {
wolfSSL 15:117db924cf7c 1613 xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1614 Des3ProcessBlock(des, (byte*)des->reg, (byte*)des->reg);
wolfSSL 15:117db924cf7c 1615 XMEMCPY(out, des->reg, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1616
wolfSSL 15:117db924cf7c 1617 out += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1618 in += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1619 }
wolfSSL 15:117db924cf7c 1620 return 0;
wolfSSL 15:117db924cf7c 1621 }
wolfSSL 15:117db924cf7c 1622
wolfSSL 15:117db924cf7c 1623
wolfSSL 15:117db924cf7c 1624 int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1625 {
wolfSSL 15:117db924cf7c 1626 word32 blocks;
wolfSSL 15:117db924cf7c 1627
wolfSSL 15:117db924cf7c 1628 if (des == NULL || out == NULL || in == NULL) {
wolfSSL 15:117db924cf7c 1629 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1630 }
wolfSSL 15:117db924cf7c 1631
wolfSSL 15:117db924cf7c 1632 #if defined(WOLFSSL_ASYNC_CRYPT)
wolfSSL 15:117db924cf7c 1633 if (des->asyncDev.marker == WOLFSSL_ASYNC_MARKER_3DES &&
wolfSSL 15:117db924cf7c 1634 sz >= WC_ASYNC_THRESH_DES3_CBC) {
wolfSSL 15:117db924cf7c 1635 #if defined(HAVE_CAVIUM)
wolfSSL 15:117db924cf7c 1636 return NitroxDes3CbcDecrypt(des, out, in, sz);
wolfSSL 15:117db924cf7c 1637 #elif defined(HAVE_INTEL_QA)
wolfSSL 15:117db924cf7c 1638 return IntelQaSymDes3CbcDecrypt(&des->asyncDev, out, in, sz,
wolfSSL 15:117db924cf7c 1639 des->key_raw, DES3_KEYLEN, (byte*)des->iv_raw, DES3_IVLEN);
wolfSSL 15:117db924cf7c 1640 #else /* WOLFSSL_ASYNC_CRYPT_TEST */
wolfSSL 15:117db924cf7c 1641 if (wc_AsyncTestInit(&des->asyncDev, ASYNC_TEST_DES3_CBC_DECRYPT)) {
wolfSSL 15:117db924cf7c 1642 WC_ASYNC_TEST* testDev = &des->asyncDev.test;
wolfSSL 15:117db924cf7c 1643 testDev->des.des = des;
wolfSSL 15:117db924cf7c 1644 testDev->des.out = out;
wolfSSL 15:117db924cf7c 1645 testDev->des.in = in;
wolfSSL 15:117db924cf7c 1646 testDev->des.sz = sz;
wolfSSL 15:117db924cf7c 1647 return WC_PENDING_E;
wolfSSL 15:117db924cf7c 1648 }
wolfSSL 15:117db924cf7c 1649 #endif
wolfSSL 15:117db924cf7c 1650 }
wolfSSL 15:117db924cf7c 1651 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1652
wolfSSL 15:117db924cf7c 1653 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1654 while (blocks--) {
wolfSSL 15:117db924cf7c 1655 XMEMCPY(des->tmp, in, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1656 Des3ProcessBlock(des, (byte*)des->tmp, out);
wolfSSL 15:117db924cf7c 1657 xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1658 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1659
wolfSSL 15:117db924cf7c 1660 out += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1661 in += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1662 }
wolfSSL 15:117db924cf7c 1663 return 0;
wolfSSL 15:117db924cf7c 1664 }
wolfSSL 15:117db924cf7c 1665
wolfSSL 15:117db924cf7c 1666 #ifdef WOLFSSL_DES_ECB
wolfSSL 15:117db924cf7c 1667 /* One block, compatibility only */
wolfSSL 15:117db924cf7c 1668 int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1669 {
wolfSSL 15:117db924cf7c 1670 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1671
wolfSSL 15:117db924cf7c 1672 if (des == NULL || out == NULL || in == NULL) {
wolfSSL 15:117db924cf7c 1673 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1674 }
wolfSSL 15:117db924cf7c 1675
wolfSSL 15:117db924cf7c 1676 while (blocks--) {
wolfSSL 15:117db924cf7c 1677 DesProcessBlock(des, in, out);
wolfSSL 15:117db924cf7c 1678
wolfSSL 15:117db924cf7c 1679 out += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1680 in += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1681 }
wolfSSL 15:117db924cf7c 1682 return 0;
wolfSSL 15:117db924cf7c 1683 }
wolfSSL 15:117db924cf7c 1684
wolfSSL 15:117db924cf7c 1685 int wc_Des3_EcbEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
wolfSSL 15:117db924cf7c 1686 {
wolfSSL 15:117db924cf7c 1687 word32 blocks = sz / DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1688
wolfSSL 15:117db924cf7c 1689 if (des == NULL || out == NULL || in == NULL) {
wolfSSL 15:117db924cf7c 1690 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1691 }
wolfSSL 15:117db924cf7c 1692
wolfSSL 15:117db924cf7c 1693 while (blocks--) {
wolfSSL 15:117db924cf7c 1694 Des3ProcessBlock(des, in, out);
wolfSSL 15:117db924cf7c 1695
wolfSSL 15:117db924cf7c 1696 out += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1697 in += DES_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 1698 }
wolfSSL 15:117db924cf7c 1699 return 0;
wolfSSL 15:117db924cf7c 1700 }
wolfSSL 15:117db924cf7c 1701 #endif /* WOLFSSL_DES_ECB */
wolfSSL 15:117db924cf7c 1702
wolfSSL 15:117db924cf7c 1703 #endif /* NEED_SOFT_DES */
wolfSSL 15:117db924cf7c 1704
wolfSSL 15:117db924cf7c 1705
wolfSSL 15:117db924cf7c 1706 void wc_Des_SetIV(Des* des, const byte* iv)
wolfSSL 15:117db924cf7c 1707 {
wolfSSL 15:117db924cf7c 1708 if (des && iv)
wolfSSL 15:117db924cf7c 1709 XMEMCPY(des->reg, iv, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1710 else if (des)
wolfSSL 15:117db924cf7c 1711 XMEMSET(des->reg, 0, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1712 }
wolfSSL 15:117db924cf7c 1713
wolfSSL 15:117db924cf7c 1714 int wc_Des3_SetIV(Des3* des, const byte* iv)
wolfSSL 15:117db924cf7c 1715 {
wolfSSL 15:117db924cf7c 1716 if (des == NULL) {
wolfSSL 15:117db924cf7c 1717 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1718 }
wolfSSL 15:117db924cf7c 1719 if (des && iv)
wolfSSL 15:117db924cf7c 1720 XMEMCPY(des->reg, iv, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1721 else if (des)
wolfSSL 15:117db924cf7c 1722 XMEMSET(des->reg, 0, DES_BLOCK_SIZE);
wolfSSL 15:117db924cf7c 1723
wolfSSL 15:117db924cf7c 1724 return 0;
wolfSSL 15:117db924cf7c 1725 }
wolfSSL 15:117db924cf7c 1726
wolfSSL 15:117db924cf7c 1727
wolfSSL 15:117db924cf7c 1728 /* Initialize Des3 for use with async device */
wolfSSL 15:117db924cf7c 1729 int wc_Des3Init(Des3* des3, void* heap, int devId)
wolfSSL 15:117db924cf7c 1730 {
wolfSSL 15:117db924cf7c 1731 int ret = 0;
wolfSSL 15:117db924cf7c 1732 if (des3 == NULL)
wolfSSL 15:117db924cf7c 1733 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 1734
wolfSSL 15:117db924cf7c 1735 des3->heap = heap;
wolfSSL 15:117db924cf7c 1736
wolfSSL 15:117db924cf7c 1737 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
wolfSSL 15:117db924cf7c 1738 ret = wolfAsync_DevCtxInit(&des3->asyncDev, WOLFSSL_ASYNC_MARKER_3DES,
wolfSSL 15:117db924cf7c 1739 des3->heap, devId);
wolfSSL 15:117db924cf7c 1740 #else
wolfSSL 15:117db924cf7c 1741 (void)devId;
wolfSSL 15:117db924cf7c 1742 #endif
wolfSSL 15:117db924cf7c 1743
wolfSSL 15:117db924cf7c 1744 return ret;
wolfSSL 15:117db924cf7c 1745 }
wolfSSL 15:117db924cf7c 1746
wolfSSL 15:117db924cf7c 1747 /* Free Des3 from use with async device */
wolfSSL 15:117db924cf7c 1748 void wc_Des3Free(Des3* des3)
wolfSSL 15:117db924cf7c 1749 {
wolfSSL 15:117db924cf7c 1750 if (des3 == NULL)
wolfSSL 15:117db924cf7c 1751 return;
wolfSSL 15:117db924cf7c 1752
wolfSSL 15:117db924cf7c 1753 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
wolfSSL 15:117db924cf7c 1754 wolfAsync_DevCtxFree(&des3->asyncDev, WOLFSSL_ASYNC_MARKER_3DES);
wolfSSL 15:117db924cf7c 1755 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 1756 }
wolfSSL 15:117db924cf7c 1757
wolfSSL 15:117db924cf7c 1758 #endif /* WOLFSSL_TI_CRYPT */
wolfSSL 15:117db924cf7c 1759 #endif /* HAVE_FIPS */
wolfSSL 15:117db924cf7c 1760 #endif /* NO_DES3 */
wolfSSL 15:117db924cf7c 1761