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