Coverage Report

Created: 2026-11-12 06:59

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