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