wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue May 30 01:44:10 2017 +0000
Revision:
11:cee25a834751
wolfSSL 3.11.0

Who changed what in which revision?

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