source: azure_iot_hub_f767zi/trunk/wolfssl-4.4.0/wolfcrypt/src/des3.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

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