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