Coverage Report

Created: 2026-04-01 07:25

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