Coverage Report

Created: 2025-07-23 06:53

/src/wolfssl/wolfcrypt/src/wc_encrypt.c
Line
Count
Source (jump to first uncovered line)
1
/* wc_encrypt.c
2
 *
3
 * Copyright (C) 2006-2025 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
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
23
24
#include <wolfssl/wolfcrypt/aes.h>
25
#include <wolfssl/wolfcrypt/des3.h>
26
#include <wolfssl/wolfcrypt/hash.h>
27
#include <wolfssl/wolfcrypt/rc2.h>
28
#include <wolfssl/wolfcrypt/arc4.h>
29
#include <wolfssl/wolfcrypt/wc_encrypt.h>
30
#include <wolfssl/wolfcrypt/asn.h>
31
#include <wolfssl/wolfcrypt/coding.h>
32
#include <wolfssl/wolfcrypt/pwdbased.h>
33
34
#ifdef NO_INLINE
35
    #include <wolfssl/wolfcrypt/misc.h>
36
#else
37
    #define WOLFSSL_MISC_INCLUDED
38
    #include <wolfcrypt/src/misc.c>
39
#endif
40
41
#if !defined(NO_AES) && defined(HAVE_AES_CBC)
42
#ifdef HAVE_AES_DECRYPT
43
int wc_AesCbcDecryptWithKey(byte* out, const byte* in, word32 inSz,
44
                                  const byte* key, word32 keySz, const byte* iv)
45
0
{
46
0
    int  ret = 0;
47
#ifdef WOLFSSL_SMALL_STACK
48
    Aes* aes = NULL;
49
#else
50
0
    Aes  aes[1];
51
0
#endif
52
53
0
    if (out == NULL || in == NULL || key == NULL || iv == NULL) {
54
0
        return BAD_FUNC_ARG;
55
0
    }
56
57
#ifdef WOLFSSL_SMALL_STACK
58
    aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_TMP_BUFFER);
59
    if (aes == NULL)
60
        return MEMORY_E;
61
#endif
62
63
0
    ret = wc_AesInit(aes, NULL, INVALID_DEVID);
64
0
    if (ret == 0) {
65
0
        ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
66
0
        if (ret == 0)
67
0
            ret = wc_AesCbcDecrypt(aes, out, in, inSz);
68
69
0
        wc_AesFree(aes);
70
0
    }
71
72
#ifdef WOLFSSL_SMALL_STACK
73
    XFREE(aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
74
#endif
75
76
0
    return ret;
77
0
}
78
#endif /* HAVE_AES_DECRYPT */
79
80
int wc_AesCbcEncryptWithKey(byte* out, const byte* in, word32 inSz,
81
                            const byte* key, word32 keySz, const byte* iv)
82
0
{
83
0
    int  ret = 0;
84
#ifdef WOLFSSL_SMALL_STACK
85
    Aes* aes;
86
#else
87
0
    Aes  aes[1];
88
0
#endif
89
90
#ifdef WOLFSSL_SMALL_STACK
91
    aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_TMP_BUFFER);
92
    if (aes == NULL)
93
        return MEMORY_E;
94
#endif
95
96
0
    ret = wc_AesInit(aes, NULL, INVALID_DEVID);
97
0
    if (ret == 0) {
98
0
        ret = wc_AesSetKey(aes, key, keySz, iv, AES_ENCRYPTION);
99
0
        if (ret == 0)
100
0
            ret = wc_AesCbcEncrypt(aes, out, in, inSz);
101
102
0
        wc_AesFree(aes);
103
0
    }
104
105
#ifdef WOLFSSL_SMALL_STACK
106
    XFREE(aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
107
#endif
108
109
0
    return ret;
110
0
}
111
#endif /* !NO_AES && HAVE_AES_CBC */
112
113
114
#if !defined(NO_DES3) && !defined(WOLFSSL_TI_CRYPT)
115
int wc_Des_CbcEncryptWithKey(byte* out, const byte* in, word32 sz,
116
                             const byte* key, const byte* iv)
117
{
118
    int ret  = 0;
119
#ifdef WOLFSSL_SMALL_STACK
120
    Des* des;
121
#else
122
    Des  des[1];
123
#endif
124
125
#ifdef WOLFSSL_SMALL_STACK
126
    des = (Des*)XMALLOC(sizeof(Des), NULL, DYNAMIC_TYPE_TMP_BUFFER);
127
    if (des == NULL)
128
        return MEMORY_E;
129
#endif
130
131
    ret = wc_Des_SetKey(des, key, iv, DES_ENCRYPTION);
132
    if (ret == 0)
133
        ret = wc_Des_CbcEncrypt(des, out, in, sz);
134
135
#ifdef WOLFSSL_SMALL_STACK
136
    XFREE(des, NULL, DYNAMIC_TYPE_TMP_BUFFER);
137
#endif
138
139
    return ret;
140
}
141
142
int wc_Des_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
143
                             const byte* key, const byte* iv)
144
{
145
    int ret  = 0;
146
#ifdef WOLFSSL_SMALL_STACK
147
    Des* des;
148
#else
149
    Des  des[1];
150
#endif
151
152
#ifdef WOLFSSL_SMALL_STACK
153
    des = (Des*)XMALLOC(sizeof(Des), NULL, DYNAMIC_TYPE_TMP_BUFFER);
154
    if (des == NULL)
155
        return MEMORY_E;
156
#endif
157
158
    ret = wc_Des_SetKey(des, key, iv, DES_DECRYPTION);
159
    if (ret == 0)
160
        ret = wc_Des_CbcDecrypt(des, out, in, sz);
161
162
#ifdef WOLFSSL_SMALL_STACK
163
    XFREE(des, NULL, DYNAMIC_TYPE_TMP_BUFFER);
164
#endif
165
166
    return ret;
167
}
168
169
170
int wc_Des3_CbcEncryptWithKey(byte* out, const byte* in, word32 sz,
171
                              const byte* key, const byte* iv)
172
{
173
    int ret    = 0;
174
#ifdef WOLFSSL_SMALL_STACK
175
    Des3* des3;
176
#else
177
    Des3  des3[1];
178
#endif
179
180
#ifdef WOLFSSL_SMALL_STACK
181
    des3 = (Des3*)XMALLOC(sizeof(Des3), NULL, DYNAMIC_TYPE_TMP_BUFFER);
182
    if (des3 == NULL)
183
        return MEMORY_E;
184
#endif
185
186
    ret = wc_Des3Init(des3, NULL, INVALID_DEVID);
187
    if (ret == 0) {
188
        ret = wc_Des3_SetKey(des3, key, iv, DES_ENCRYPTION);
189
        if (ret == 0)
190
            ret = wc_Des3_CbcEncrypt(des3, out, in, sz);
191
        wc_Des3Free(des3);
192
    }
193
194
#ifdef WOLFSSL_SMALL_STACK
195
    XFREE(des3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
196
#endif
197
198
    return ret;
199
}
200
201
202
int wc_Des3_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
203
                              const byte* key, const byte* iv)
204
{
205
    int ret    = 0;
206
#ifdef WOLFSSL_SMALL_STACK
207
    Des3* des3;
208
#else
209
    Des3  des3[1];
210
#endif
211
212
#ifdef WOLFSSL_SMALL_STACK
213
    des3 = (Des3*)XMALLOC(sizeof(Des3), NULL, DYNAMIC_TYPE_TMP_BUFFER);
214
    if (des3 == NULL)
215
        return MEMORY_E;
216
#endif
217
218
    ret = wc_Des3Init(des3, NULL, INVALID_DEVID);
219
    if (ret == 0) {
220
        ret = wc_Des3_SetKey(des3, key, iv, DES_DECRYPTION);
221
        if (ret == 0)
222
            ret = wc_Des3_CbcDecrypt(des3, out, in, sz);
223
        wc_Des3Free(des3);
224
    }
225
226
#ifdef WOLFSSL_SMALL_STACK
227
    XFREE(des3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
228
#endif
229
230
    return ret;
231
}
232
233
#endif /* !NO_DES3 */
234
235
236
#if !defined(NO_ASN) && defined(WOLFSSL_ENCRYPTED_KEYS)
237
238
int wc_BufferKeyDecrypt(EncryptedInfo* info, byte* der, word32 derSz,
239
    const byte* password, int passwordSz, int hashType)
240
{
241
    int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
242
#ifdef WOLFSSL_SMALL_STACK
243
    byte* key      = NULL;
244
#else
245
    byte  key[WC_MAX_SYM_KEY_SIZE];
246
#endif
247
248
    (void)derSz;
249
    (void)passwordSz;
250
    (void)hashType;
251
252
    if (der == NULL || password == NULL || info == NULL || info->keySz == 0) {
253
        return BAD_FUNC_ARG;
254
    }
255
256
    /* use file's salt for key derivation, hex decode first */
257
    if (Base16_Decode(info->iv, info->ivSz, info->iv, &info->ivSz) != 0) {
258
        WOLFSSL_ERROR_VERBOSE(BUFFER_E);
259
        return BUFFER_E;
260
    }
261
    if (info->ivSz < PKCS5_SALT_SZ) {
262
        WOLFSSL_ERROR_VERBOSE(BUFFER_E);
263
        return BUFFER_E;
264
    }
265
266
#ifdef WOLFSSL_SMALL_STACK
267
    key = (byte*)XMALLOC(WC_MAX_SYM_KEY_SIZE, NULL, DYNAMIC_TYPE_SYMMETRIC_KEY);
268
    if (key == NULL) {
269
        return MEMORY_E;
270
    }
271
#endif
272
#ifdef WOLFSSL_CHECK_MEM_ZERO
273
    wc_MemZero_Add("wc_BufferKeyDecrypt key", key, WC_MAX_SYM_KEY_SIZE);
274
#endif
275
276
    (void)XMEMSET(key, 0, WC_MAX_SYM_KEY_SIZE);
277
278
#ifndef NO_PWDBASED
279
    if ((ret = wc_PBKDF1(key, password, passwordSz, info->iv, PKCS5_SALT_SZ, 1,
280
                                        (int)info->keySz, hashType)) != 0) {
281
#ifdef WOLFSSL_SMALL_STACK
282
        XFREE(key, NULL, DYNAMIC_TYPE_SYMMETRIC_KEY);
283
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
284
        wc_MemZero_Check(key, WC_MAX_SYM_KEY_SIZE);
285
#endif
286
        return ret;
287
    }
288
#endif
289
290
#ifndef NO_DES3
291
    if (info->cipherType == WC_CIPHER_DES)
292
        ret = wc_Des_CbcDecryptWithKey(der, der, derSz, key, info->iv);
293
    if (info->cipherType == WC_CIPHER_DES3)
294
        ret = wc_Des3_CbcDecryptWithKey(der, der, derSz, key, info->iv);
295
#endif /* NO_DES3 */
296
#if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)
297
    if (info->cipherType == WC_CIPHER_AES_CBC)
298
        ret = wc_AesCbcDecryptWithKey(der, der, derSz, key, info->keySz,
299
            info->iv);
300
#endif /* !NO_AES && HAVE_AES_CBC && HAVE_AES_DECRYPT */
301
302
    ForceZero(key, WC_MAX_SYM_KEY_SIZE);
303
#ifdef WOLFSSL_SMALL_STACK
304
    XFREE(key, NULL, DYNAMIC_TYPE_SYMMETRIC_KEY);
305
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
306
    wc_MemZero_Check(key, WC_MAX_SYM_KEY_SIZE);
307
#endif
308
309
    return ret;
310
}
311
312
int wc_BufferKeyEncrypt(EncryptedInfo* info, byte* der, word32 derSz,
313
    const byte* password, int passwordSz, int hashType)
314
{
315
    int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
316
#ifdef WOLFSSL_SMALL_STACK
317
    byte* key      = NULL;
318
#else
319
    byte  key[WC_MAX_SYM_KEY_SIZE];
320
#endif
321
322
    (void)derSz;
323
    (void)passwordSz;
324
    (void)hashType;
325
326
    if (der == NULL || password == NULL || info == NULL || info->keySz == 0 ||
327
            info->ivSz < PKCS5_SALT_SZ) {
328
        return BAD_FUNC_ARG;
329
    }
330
331
#ifdef WOLFSSL_SMALL_STACK
332
    key = (byte*)XMALLOC(WC_MAX_SYM_KEY_SIZE, NULL, DYNAMIC_TYPE_SYMMETRIC_KEY);
333
    if (key == NULL) {
334
        return MEMORY_E;
335
    }
336
#endif /* WOLFSSL_SMALL_STACK */
337
#ifdef WOLFSSL_CHECK_MEM_ZERO
338
    XMEMSET(key, 0xff, WC_MAX_SYM_KEY_SIZE);
339
    wc_MemZero_Add("wc_BufferKeyDecrypt key", key, WC_MAX_SYM_KEY_SIZE);
340
#endif
341
342
    (void)XMEMSET(key, 0, WC_MAX_SYM_KEY_SIZE);
343
344
#ifndef NO_PWDBASED
345
    if ((ret = wc_PBKDF1(key, password, passwordSz, info->iv, PKCS5_SALT_SZ, 1,
346
                                        (int)info->keySz, hashType)) != 0) {
347
#ifdef WOLFSSL_SMALL_STACK
348
        XFREE(key, NULL, DYNAMIC_TYPE_SYMMETRIC_KEY);
349
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
350
        wc_MemZero_Check(key, WC_MAX_SYM_KEY_SIZE);
351
#endif
352
        return ret;
353
    }
354
#endif
355
356
#ifndef NO_DES3
357
    if (info->cipherType == WC_CIPHER_DES)
358
        ret = wc_Des_CbcEncryptWithKey(der, der, derSz, key, info->iv);
359
    if (info->cipherType == WC_CIPHER_DES3)
360
        ret = wc_Des3_CbcEncryptWithKey(der, der, derSz, key, info->iv);
361
#endif /* NO_DES3 */
362
#if !defined(NO_AES) && defined(HAVE_AES_CBC)
363
    if (info->cipherType == WC_CIPHER_AES_CBC)
364
        ret = wc_AesCbcEncryptWithKey(der, der, derSz, key, info->keySz,
365
            info->iv);
366
#endif /* !NO_AES && HAVE_AES_CBC */
367
368
    ForceZero(key, WC_MAX_SYM_KEY_SIZE);
369
#ifdef WOLFSSL_SMALL_STACK
370
    XFREE(key, NULL, DYNAMIC_TYPE_SYMMETRIC_KEY);
371
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
372
    wc_MemZero_Check(key, WC_MAX_SYM_KEY_SIZE);
373
#endif
374
375
    return ret;
376
}
377
378
#endif /* !NO_ASN && WOLFSSL_ENCRYPTED_KEYS */
379
380
381
#if !defined(NO_PWDBASED) && !defined(NO_ASN)
382
383
#if defined(HAVE_PKCS8) || defined(HAVE_PKCS12)
384
/* Decrypt/Encrypt input in place from parameters based on id
385
 *
386
 * returns a negative value on fail case
387
 */
388
int wc_CryptKey(const char* password, int passwordSz, byte* salt,
389
                      int saltSz, int iterations, int id, byte* input,
390
                      int length, int version, byte* cbcIv, int enc, int shaOid)
391
0
{
392
0
    int typeH = WC_HASH_TYPE_NONE;
393
0
    word32 derivedLen = 0;
394
0
    int ret = 0;
395
#ifdef WOLFSSL_SMALL_STACK
396
    byte* key = NULL;
397
#else
398
0
    byte key[PKCS_MAX_KEY_SIZE];
399
0
#endif
400
401
0
    (void)input;
402
0
    (void)length;
403
0
    (void)enc;
404
405
0
    WOLFSSL_ENTER("wc_CryptKey");
406
407
0
    if (length < 0)
408
0
        return BAD_LENGTH_E;
409
410
0
    switch (id) {
411
    #ifndef NO_DES3
412
        #ifndef NO_MD5
413
        case PBE_MD5_DES:
414
            typeH = WC_MD5;
415
            derivedLen = 16;           /* may need iv for v1.5 */
416
            break;
417
        #endif
418
        #ifndef NO_SHA
419
        case PBE_SHA1_DES:
420
            typeH = WC_SHA;
421
            derivedLen = 16;           /* may need iv for v1.5 */
422
            break;
423
        #endif /* !NO_SHA */
424
        #if !defined(NO_SHA) || !defined(NO_SHA256)
425
        case PBE_SHA1_DES3:
426
            switch (shaOid) {
427
            #ifndef NO_SHA256
428
                case HMAC_SHA256_OID:
429
                    typeH = WC_SHA256;
430
                    derivedLen = 32;
431
                    break;
432
            #endif
433
            #ifndef NO_SHA
434
                default:
435
                    typeH = WC_SHA;
436
                    derivedLen = 32;           /* may need iv for v1.5 */
437
                    break;
438
            #endif
439
            }
440
        break;
441
        #endif
442
    #endif /* !NO_DES3 */
443
    #if !defined(NO_SHA) && !defined(NO_RC4)
444
        case PBE_SHA1_RC4_128:
445
            typeH = WC_SHA;
446
            derivedLen = 16;
447
            break;
448
    #endif
449
0
    #if defined(WOLFSSL_AES_256)
450
0
        case PBE_AES256_CBC:
451
0
            switch(shaOid) {
452
0
            #ifndef NO_SHA256
453
0
                case HMAC_SHA256_OID:
454
0
                    typeH = WC_SHA256;
455
0
                    derivedLen = 32;
456
0
                    break;
457
0
            #endif
458
0
            #ifndef NO_SHA
459
0
                default:
460
0
                    typeH = WC_SHA;
461
0
                    derivedLen = 32;
462
0
                    break;
463
0
            #endif
464
0
            }
465
0
            break;
466
0
    #endif /* WOLFSSL_AES_256 && !NO_SHA */
467
0
    #if defined(WOLFSSL_AES_128)
468
0
        case PBE_AES128_CBC:
469
0
            switch(shaOid) {
470
0
            #ifndef NO_SHA256
471
0
                case HMAC_SHA256_OID:
472
0
                    typeH = WC_SHA256;
473
0
                    derivedLen = 16;
474
0
                    break;
475
0
            #endif
476
0
            #ifndef NO_SHA
477
0
                default:
478
0
                    typeH = WC_SHA;
479
0
                    derivedLen = 16;
480
0
                    break;
481
0
            #endif
482
0
            }
483
0
            break;
484
0
    #endif /* WOLFSSL_AES_128 && !NO_SHA */
485
    #ifdef WC_RC2
486
        case PBE_SHA1_40RC2_CBC:
487
            typeH = WC_SHA;
488
            derivedLen = 5;
489
            break;
490
    #endif
491
0
        default:
492
0
            WOLFSSL_MSG("Unknown/Unsupported encrypt/decrypt id");
493
0
            (void)shaOid;
494
0
            ret = ALGO_ID_E;
495
0
            WOLFSSL_ERROR_VERBOSE(ret);
496
0
    }
497
498
    #ifdef WOLFSSL_SMALL_STACK
499
    if (ret == 0) {
500
        key = (byte*)XMALLOC(PKCS_MAX_KEY_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
501
        if (key == NULL)
502
            ret = MEMORY_E;
503
    }
504
    #endif
505
506
0
    if (ret == 0) {
507
    #ifdef WOLFSSL_CHECK_MEM_ZERO
508
        XMEMSET(key, 0xff, PKCS_MAX_KEY_SIZE);
509
        wc_MemZero_Add("wc_CryptKey key", key, PKCS_MAX_KEY_SIZE);
510
    #endif
511
512
0
        switch (version) {
513
0
    #ifndef NO_HMAC
514
0
            case PKCS5v2:
515
0
                PRIVATE_KEY_UNLOCK();
516
0
                ret = wc_PBKDF2(key, (byte*)password, passwordSz,
517
0
                                salt, saltSz, iterations, (int)derivedLen, typeH);
518
0
                PRIVATE_KEY_LOCK();
519
0
                break;
520
0
    #endif
521
0
    #ifndef NO_SHA
522
0
            case PKCS5:
523
0
                ret = wc_PBKDF1(key, (byte*)password, passwordSz,
524
0
                                salt, saltSz, iterations, (int)derivedLen, typeH);
525
0
                break;
526
0
    #endif
527
0
    #ifdef HAVE_PKCS12
528
0
            case PKCS12v1:
529
0
            {
530
0
                int  i, idx = 0;
531
0
                byte unicodePasswd[MAX_UNICODE_SZ];
532
533
0
                if ( (passwordSz * 2 + 2) > (int)sizeof(unicodePasswd)) {
534
0
                    ret = UNICODE_SIZE_E;
535
0
                    break;
536
0
                }
537
538
0
                for (i = 0; i < passwordSz; i++) {
539
0
                    unicodePasswd[idx++] = 0x00;
540
0
                    unicodePasswd[idx++] = (byte)password[i];
541
0
                }
542
                /* add trailing NULL */
543
0
                unicodePasswd[idx++] = 0x00;
544
0
                unicodePasswd[idx++] = 0x00;
545
546
0
                ret =  wc_PKCS12_PBKDF(key, unicodePasswd, idx, salt, saltSz,
547
0
                                    iterations, (int)derivedLen, typeH, 1);
548
0
                if (ret < 0)
549
0
                    break;
550
0
                if (id != PBE_SHA1_RC4_128) {
551
0
                    i = ret;
552
0
                    ret = wc_PKCS12_PBKDF(cbcIv, unicodePasswd, idx, salt,
553
0
                                    saltSz, iterations, 8, typeH, 2);
554
0
                    if (ret < 0)
555
0
                        break;
556
0
                    ret += i;
557
0
                }
558
0
                break;
559
0
            }
560
0
    #endif /* HAVE_PKCS12 */
561
0
            default:
562
0
                WOLFSSL_MSG("Unknown/Unsupported PKCS version");
563
0
                ret = ALGO_ID_E;
564
0
                WOLFSSL_ERROR_VERBOSE(ret);
565
0
        } /* switch (version) */
566
0
    }
567
568
0
    if (ret == 0) {
569
0
        switch (id) {
570
    #ifndef NO_DES3
571
        #if !defined(NO_SHA) || !defined(NO_MD5)
572
            case PBE_MD5_DES:
573
            case PBE_SHA1_DES:
574
            {
575
                Des    des;
576
                byte*  desIv = key + 8;
577
578
                if (version == PKCS5v2 || version == PKCS12v1)
579
                    desIv = cbcIv;
580
581
                if (enc) {
582
                    ret = wc_Des_SetKey(&des, key, desIv, DES_ENCRYPTION);
583
                }
584
                else {
585
                    ret = wc_Des_SetKey(&des, key, desIv, DES_DECRYPTION);
586
                }
587
                if (ret == 0) {
588
                    if (enc) {
589
                        wc_Des_CbcEncrypt(&des, input, input, (word32)length);
590
                    }
591
                    else {
592
                        wc_Des_CbcDecrypt(&des, input, input, (word32)length);
593
                    }
594
                }
595
                break;
596
            }
597
        #endif /* !NO_SHA || !NO_MD5 */
598
599
        #ifndef NO_SHA
600
            case PBE_SHA1_DES3:
601
            {
602
                Des3   des;
603
                byte*  desIv = key + 24;
604
605
                if (version == PKCS5v2 || version == PKCS12v1)
606
                    desIv = cbcIv;
607
608
                ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
609
                if (ret != 0) {
610
                    break;
611
                }
612
                if (enc) {
613
                    ret = wc_Des3_SetKey(&des, key, desIv, DES_ENCRYPTION);
614
                }
615
                else {
616
                    ret = wc_Des3_SetKey(&des, key, desIv, DES_DECRYPTION);
617
                }
618
                if (ret == 0) {
619
                    if (enc) {
620
                        ret = wc_Des3_CbcEncrypt(&des, input, input, (word32)length);
621
                    }
622
                    else {
623
                        ret = wc_Des3_CbcDecrypt(&des, input, input, (word32)length);
624
                    }
625
                }
626
                wc_Des3Free(&des);
627
                break;
628
            }
629
        #endif /* !NO_SHA */
630
    #endif
631
    #if !defined(NO_RC4) && !defined(NO_SHA)
632
            case PBE_SHA1_RC4_128:
633
            {
634
                Arc4    dec;
635
636
                wc_Arc4SetKey(&dec, key, derivedLen);
637
                wc_Arc4Process(&dec, input, input, (word32)length);
638
                break;
639
            }
640
    #endif
641
0
    #if !defined(NO_AES) && defined(HAVE_AES_CBC) && \
642
0
        (defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_128))
643
0
        #ifdef WOLFSSL_AES_256
644
0
            case PBE_AES256_CBC:
645
0
        #endif /* WOLFSSL_AES_256 */
646
0
        #ifdef WOLFSSL_AES_128
647
0
            case PBE_AES128_CBC:
648
0
        #endif /* WOLFSSL_AES_128 */
649
0
            {
650
0
                int free_aes;
651
652
            #ifdef WOLFSSL_SMALL_STACK
653
                Aes *aes;
654
                aes = (Aes *)XMALLOC(sizeof *aes, NULL, DYNAMIC_TYPE_AES);
655
                if (aes == NULL) {
656
                    ret = MEMORY_E;
657
                    break;
658
                }
659
            #else
660
0
                Aes aes[1];
661
0
            #endif
662
0
                free_aes = 0;
663
0
                ret = wc_AesInit(aes, NULL, INVALID_DEVID);
664
0
                if (ret == 0) {
665
0
                    free_aes = 1;
666
0
                    if (enc) {
667
0
                        ret = wc_AesSetKey(aes, key, derivedLen, cbcIv,
668
0
                                                            AES_ENCRYPTION);
669
0
                    }
670
0
                    else {
671
0
                    #ifdef HAVE_AES_DECRYPT
672
0
                        ret = wc_AesSetKey(aes, key, derivedLen, cbcIv,
673
0
                                                            AES_DECRYPTION);
674
                    #else
675
                        ret = NOT_COMPILED_IN;
676
                    #endif
677
0
                    }
678
0
                }
679
0
                if (ret == 0) {
680
0
                    if (enc)
681
0
                        ret = wc_AesCbcEncrypt(aes, input, input, (word32)length);
682
0
                    #ifdef HAVE_AES_DECRYPT
683
0
                    else
684
0
                        ret = wc_AesCbcDecrypt(aes, input, input, (word32)length);
685
0
                    #endif
686
0
                }
687
0
                if (free_aes)
688
0
                    wc_AesFree(aes);
689
0
                ForceZero(aes, sizeof(Aes));
690
            #ifdef WOLFSSL_SMALL_STACK
691
                XFREE(aes, NULL, DYNAMIC_TYPE_AES);
692
            #endif
693
0
                break;
694
0
            }
695
0
    #endif /* !NO_AES && HAVE_AES_CBC && (WOLFSSL_AES_256 || WOLFSSL_AES_128) */
696
    #ifdef WC_RC2
697
            case PBE_SHA1_40RC2_CBC:
698
            {
699
                Rc2 rc2;
700
                /* effective key size for RC2-40-CBC is 40 bits */
701
                ret = wc_Rc2SetKey(&rc2, key, derivedLen, cbcIv, 40);
702
                if (ret == 0) {
703
                    if (enc)
704
                        ret = wc_Rc2CbcEncrypt(&rc2, input, input, length);
705
                    else
706
                        ret = wc_Rc2CbcDecrypt(&rc2, input, input, length);
707
                }
708
                if (ret == 0) {
709
                    ForceZero(&rc2, sizeof(Rc2));
710
                }
711
                break;
712
            }
713
    #endif
714
715
0
            default:
716
0
                WOLFSSL_MSG("Unknown/Unsupported encrypt/decryption algorithm");
717
0
                ret = ALGO_ID_E;
718
0
                WOLFSSL_ERROR_VERBOSE(ret);
719
0
        }
720
0
    }
721
722
#ifdef WOLFSSL_SMALL_STACK
723
    if (key != NULL)
724
#endif
725
0
    {
726
0
        ForceZero(key, PKCS_MAX_KEY_SIZE);
727
    #ifdef WOLFSSL_SMALL_STACK
728
        XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
729
    #elif defined(WOLFSSL_CHECK_MEM_ZERO)
730
        wc_MemZero_Check(key, PKCS_MAX_KEY_SIZE);
731
    #endif
732
0
    }
733
734
0
    return ret;
735
0
}
736
737
#endif /* HAVE_PKCS8 || HAVE_PKCS12 */
738
#endif /* !NO_PWDBASED && !NO_ASN */