/src/wolfssl-openssl-api/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 | 0 | { |
199 | 0 | int ret = 0; |
200 | 0 | WC_DECLARE_VAR(key, byte, WC_MAX_SYM_KEY_SIZE, 0); |
201 | |
|
202 | 0 | (void)derSz; |
203 | 0 | (void)passwordSz; |
204 | 0 | (void)hashType; |
205 | |
|
206 | 0 | if (der == NULL || password == NULL || info == NULL || info->keySz == 0 || |
207 | 0 | info->keySz > WC_MAX_SYM_KEY_SIZE) { |
208 | 0 | return BAD_FUNC_ARG; |
209 | 0 | } |
210 | | |
211 | | /* use file's salt for key derivation, hex decode first */ |
212 | 0 | if (Base16_Decode(info->iv, info->ivSz, info->iv, &info->ivSz) != 0) { |
213 | 0 | WOLFSSL_ERROR_VERBOSE(BUFFER_E); |
214 | 0 | return BUFFER_E; |
215 | 0 | } |
216 | 0 | if (info->ivSz < PKCS5_SALT_SZ) { |
217 | 0 | WOLFSSL_ERROR_VERBOSE(BUFFER_E); |
218 | 0 | return BUFFER_E; |
219 | 0 | } |
220 | | |
221 | 0 | WC_ALLOC_VAR_EX(key, byte, WC_MAX_SYM_KEY_SIZE, NULL, |
222 | 0 | 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 | 0 | (void)XMEMSET(key, 0, WC_MAX_SYM_KEY_SIZE); |
228 | |
|
229 | 0 | #ifndef NO_PWDBASED |
230 | 0 | ret = wc_PBKDF1(key, password, passwordSz, info->iv, PKCS5_SALT_SZ, 1, |
231 | 0 | (int)info->keySz, hashType); |
232 | 0 | #endif |
233 | |
|
234 | 0 | if (ret == 0) { |
235 | 0 | switch (info->cipherType) { |
236 | 0 | case WC_CIPHER_DES: |
237 | 0 | #ifndef NO_DES3 |
238 | 0 | ret = wc_Des_CbcDecryptWithKey(der, der, derSz, key, info->iv); |
239 | | #else |
240 | | ret = NOT_COMPILED_IN; |
241 | | #endif |
242 | 0 | break; |
243 | 0 | case WC_CIPHER_DES3: |
244 | 0 | #ifndef NO_DES3 |
245 | 0 | ret = wc_Des3_CbcDecryptWithKey(der, der, derSz, key, info->iv); |
246 | | #else |
247 | | ret = NOT_COMPILED_IN; |
248 | | #endif |
249 | 0 | break; |
250 | 0 | case WC_CIPHER_AES_CBC: |
251 | 0 | #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT) |
252 | 0 | ret = wc_AesCbcDecryptWithKey(der, der, derSz, key, info->keySz, |
253 | 0 | info->iv); |
254 | | #else |
255 | | ret = NOT_COMPILED_IN; |
256 | | #endif /* !NO_AES && HAVE_AES_CBC && HAVE_AES_DECRYPT */ |
257 | 0 | break; |
258 | 0 | default: |
259 | 0 | ret = ALGO_ID_E; |
260 | 0 | break; |
261 | 0 | } |
262 | 0 | } |
263 | | |
264 | 0 | ForceZero(key, WC_MAX_SYM_KEY_SIZE); |
265 | 0 | #ifdef WOLFSSL_SMALL_STACK |
266 | 0 | 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 | 0 | return ret; |
272 | 0 | } |
273 | | |
274 | | int wc_BufferKeyEncrypt(EncryptedInfo* info, byte* der, word32 derSz, |
275 | | const byte* password, int passwordSz, int hashType) |
276 | 0 | { |
277 | 0 | int ret = 0; |
278 | 0 | WC_DECLARE_VAR(key, byte, WC_MAX_SYM_KEY_SIZE, 0); |
279 | |
|
280 | 0 | (void)derSz; |
281 | 0 | (void)passwordSz; |
282 | 0 | (void)hashType; |
283 | |
|
284 | 0 | if (der == NULL || password == NULL || info == NULL || info->keySz == 0 || |
285 | 0 | info->keySz > WC_MAX_SYM_KEY_SIZE || info->ivSz < PKCS5_SALT_SZ) { |
286 | 0 | return BAD_FUNC_ARG; |
287 | 0 | } |
288 | | |
289 | 0 | WC_ALLOC_VAR_EX(key, byte, WC_MAX_SYM_KEY_SIZE, NULL, |
290 | 0 | 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 | 0 | (void)XMEMSET(key, 0, WC_MAX_SYM_KEY_SIZE); |
297 | |
|
298 | 0 | #ifndef NO_PWDBASED |
299 | 0 | ret = wc_PBKDF1(key, password, passwordSz, info->iv, PKCS5_SALT_SZ, 1, |
300 | 0 | (int)info->keySz, hashType); |
301 | 0 | #endif |
302 | |
|
303 | 0 | if (ret == 0) { |
304 | 0 | switch (info->cipherType) { |
305 | 0 | case WC_CIPHER_DES: |
306 | 0 | #ifndef NO_DES3 |
307 | 0 | ret = wc_Des_CbcEncryptWithKey(der, der, derSz, key, info->iv); |
308 | | #else |
309 | | ret = NOT_COMPILED_IN; |
310 | | #endif |
311 | 0 | break; |
312 | 0 | case WC_CIPHER_DES3: |
313 | 0 | #ifndef NO_DES3 |
314 | 0 | ret = wc_Des3_CbcEncryptWithKey(der, der, derSz, key, info->iv); |
315 | | #else |
316 | | ret = NOT_COMPILED_IN; |
317 | | #endif |
318 | 0 | break; |
319 | 0 | case WC_CIPHER_AES_CBC: |
320 | 0 | #if !defined(NO_AES) && defined(HAVE_AES_CBC) |
321 | 0 | ret = wc_AesCbcEncryptWithKey(der, der, derSz, key, info->keySz, |
322 | 0 | info->iv); |
323 | | #else |
324 | | ret = NOT_COMPILED_IN; |
325 | | #endif /* !NO_AES && HAVE_AES_CBC */ |
326 | 0 | break; |
327 | 0 | default: |
328 | 0 | ret = ALGO_ID_E; |
329 | 0 | break; |
330 | 0 | } |
331 | 0 | } |
332 | | |
333 | 0 | ForceZero(key, WC_MAX_SYM_KEY_SIZE); |
334 | 0 | #ifdef WOLFSSL_SMALL_STACK |
335 | 0 | 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 | 0 | return ret; |
341 | 0 | } |
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 | #ifndef 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 | 0 | break; |
484 | 0 | #endif |
485 | 0 | #ifndef NO_SHA |
486 | 0 | case PKCS5: |
487 | 0 | ret = wc_PBKDF1(key, (const byte*)password, passwordSz, |
488 | 0 | salt, saltSz, iterations, (int)derivedLen, typeH); |
489 | 0 | break; |
490 | 0 | #endif |
491 | 0 | #ifdef HAVE_PKCS12 |
492 | 0 | case PKCS12v1: |
493 | 0 | { |
494 | 0 | int i, idx = 0; |
495 | 0 | byte unicodePasswd[MAX_UNICODE_SZ]; |
496 | |
|
497 | 0 | if (passwordSz < 0 || |
498 | 0 | passwordSz >= MAX_UNICODE_SZ || |
499 | 0 | (passwordSz * 2 + 2) > MAX_UNICODE_SZ) { |
500 | 0 | ret = UNICODE_SIZE_E; |
501 | 0 | break; |
502 | 0 | } |
503 | | |
504 | 0 | for (i = 0; i < passwordSz; i++) { |
505 | 0 | unicodePasswd[idx++] = 0x00; |
506 | 0 | unicodePasswd[idx++] = (byte)password[i]; |
507 | 0 | } |
508 | | /* add trailing NULL */ |
509 | 0 | unicodePasswd[idx++] = 0x00; |
510 | 0 | unicodePasswd[idx++] = 0x00; |
511 | |
|
512 | 0 | ret = wc_PKCS12_PBKDF(key, unicodePasswd, idx, salt, saltSz, |
513 | 0 | iterations, (int)derivedLen, typeH, 1); |
514 | 0 | if (ret < 0) { |
515 | 0 | ForceZero(unicodePasswd, MAX_UNICODE_SZ); |
516 | 0 | break; |
517 | 0 | } |
518 | 0 | if (id != PBE_SHA1_RC4_128) { |
519 | 0 | i = ret; |
520 | 0 | ret = wc_PKCS12_PBKDF(cbcIv, unicodePasswd, idx, salt, |
521 | 0 | saltSz, iterations, 8, typeH, 2); |
522 | 0 | if (ret < 0) { |
523 | 0 | ForceZero(unicodePasswd, MAX_UNICODE_SZ); |
524 | 0 | break; |
525 | 0 | } |
526 | 0 | ret += i; |
527 | 0 | } |
528 | 0 | ForceZero(unicodePasswd, MAX_UNICODE_SZ); |
529 | 0 | break; |
530 | 0 | } |
531 | 0 | #endif /* HAVE_PKCS12 */ |
532 | 0 | default: |
533 | 0 | WOLFSSL_MSG("Unknown/Unsupported PKCS version"); |
534 | 0 | ret = ALGO_ID_E; |
535 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
536 | 0 | } /* switch (version) */ |
537 | 0 | } |
538 | | |
539 | 0 | if (ret == 0) { |
540 | 0 | switch (id) { |
541 | 0 | #ifndef NO_DES3 |
542 | 0 | #if !defined(NO_SHA) || !defined(NO_MD5) |
543 | 0 | case PBE_MD5_DES: |
544 | 0 | case PBE_SHA1_DES: |
545 | 0 | { |
546 | 0 | Des des; |
547 | 0 | byte* desIv = key + 8; |
548 | |
|
549 | 0 | if (version == PKCS5v2 || version == PKCS12v1) |
550 | 0 | desIv = cbcIv; |
551 | |
|
552 | 0 | if (enc) { |
553 | 0 | ret = wc_Des_SetKey(&des, key, desIv, DES_ENCRYPTION); |
554 | 0 | } |
555 | 0 | else { |
556 | 0 | ret = wc_Des_SetKey(&des, key, desIv, DES_DECRYPTION); |
557 | 0 | } |
558 | 0 | if (ret == 0) { |
559 | 0 | if (enc) { |
560 | 0 | wc_Des_CbcEncrypt(&des, input, input, (word32)length); |
561 | 0 | } |
562 | 0 | else { |
563 | 0 | wc_Des_CbcDecrypt(&des, input, input, (word32)length); |
564 | 0 | } |
565 | 0 | } |
566 | 0 | ForceZero(&des, sizeof(Des)); |
567 | 0 | break; |
568 | 0 | } |
569 | 0 | #endif /* !NO_SHA || !NO_MD5 */ |
570 | | |
571 | 0 | #ifndef NO_SHA |
572 | 0 | case PBE_SHA1_DES3: |
573 | 0 | { |
574 | 0 | Des3 des; |
575 | 0 | byte* desIv = key + 24; |
576 | |
|
577 | 0 | if (version == PKCS5v2 || version == PKCS12v1) |
578 | 0 | desIv = cbcIv; |
579 | |
|
580 | 0 | ret = wc_Des3Init(&des, NULL, INVALID_DEVID); |
581 | 0 | if (ret != 0) { |
582 | 0 | break; |
583 | 0 | } |
584 | 0 | if (enc) { |
585 | 0 | ret = wc_Des3_SetKey(&des, key, desIv, DES_ENCRYPTION); |
586 | 0 | } |
587 | 0 | else { |
588 | 0 | ret = wc_Des3_SetKey(&des, key, desIv, DES_DECRYPTION); |
589 | 0 | } |
590 | 0 | if (ret == 0) { |
591 | 0 | if (enc) { |
592 | 0 | ret = wc_Des3_CbcEncrypt(&des, input, input, (word32)length); |
593 | 0 | } |
594 | 0 | else { |
595 | 0 | ret = wc_Des3_CbcDecrypt(&des, input, input, (word32)length); |
596 | 0 | } |
597 | 0 | } |
598 | 0 | wc_Des3Free(&des); |
599 | 0 | break; |
600 | 0 | } |
601 | 0 | #endif /* !NO_SHA */ |
602 | 0 | #endif |
603 | 0 | #if !defined(NO_RC4) && !defined(NO_SHA) |
604 | 0 | case PBE_SHA1_RC4_128: |
605 | 0 | { |
606 | 0 | Arc4 dec; |
607 | |
|
608 | 0 | wc_Arc4SetKey(&dec, key, derivedLen); |
609 | 0 | wc_Arc4Process(&dec, input, input, (word32)length); |
610 | 0 | ForceZero(&dec, sizeof(Arc4)); |
611 | 0 | break; |
612 | 0 | } |
613 | 0 | #endif |
614 | 0 | #if !defined(NO_AES) && defined(HAVE_AES_CBC) && \ |
615 | 0 | (defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_128)) |
616 | 0 | #ifdef WOLFSSL_AES_256 |
617 | 0 | case PBE_AES256_CBC: |
618 | 0 | #endif /* WOLFSSL_AES_256 */ |
619 | 0 | #ifdef WOLFSSL_AES_128 |
620 | 0 | case PBE_AES128_CBC: |
621 | 0 | #endif /* WOLFSSL_AES_128 */ |
622 | 0 | { |
623 | 0 | int free_aes; |
624 | |
|
625 | 0 | #ifdef WOLFSSL_SMALL_STACK |
626 | 0 | Aes *aes; |
627 | 0 | aes = (Aes *)XMALLOC(sizeof *aes, NULL, DYNAMIC_TYPE_AES); |
628 | 0 | if (aes == NULL) { |
629 | 0 | ret = MEMORY_E; |
630 | 0 | break; |
631 | 0 | } |
632 | | #else |
633 | | Aes aes[1]; |
634 | | #endif |
635 | 0 | free_aes = 0; |
636 | 0 | ret = wc_AesInit(aes, NULL, INVALID_DEVID); |
637 | 0 | if (ret == 0) { |
638 | 0 | free_aes = 1; |
639 | 0 | if (enc) { |
640 | 0 | ret = wc_AesSetKey(aes, key, derivedLen, cbcIv, |
641 | 0 | AES_ENCRYPTION); |
642 | 0 | } |
643 | 0 | else { |
644 | 0 | #ifdef HAVE_AES_DECRYPT |
645 | 0 | ret = wc_AesSetKey(aes, key, derivedLen, cbcIv, |
646 | 0 | AES_DECRYPTION); |
647 | | #else |
648 | | ret = NOT_COMPILED_IN; |
649 | | #endif |
650 | 0 | } |
651 | 0 | } |
652 | 0 | if (ret == 0) { |
653 | 0 | if (enc) |
654 | 0 | ret = wc_AesCbcEncrypt(aes, input, input, (word32)length); |
655 | 0 | #ifdef HAVE_AES_DECRYPT |
656 | 0 | else |
657 | 0 | ret = wc_AesCbcDecrypt(aes, input, input, (word32)length); |
658 | 0 | #endif |
659 | 0 | } |
660 | 0 | if (free_aes) |
661 | 0 | wc_AesFree(aes); |
662 | 0 | ForceZero(aes, sizeof(Aes)); |
663 | 0 | WC_FREE_VAR_EX(aes, NULL, DYNAMIC_TYPE_AES); |
664 | 0 | break; |
665 | 0 | } |
666 | 0 | #endif /* !NO_AES && HAVE_AES_CBC && (WOLFSSL_AES_256 || WOLFSSL_AES_128) */ |
667 | | #ifdef WC_RC2 |
668 | | case PBE_SHA1_40RC2_CBC: |
669 | | { |
670 | | Rc2 rc2; |
671 | | /* effective key size for RC2-40-CBC is 40 bits */ |
672 | | ret = wc_Rc2SetKey(&rc2, key, derivedLen, cbcIv, 40); |
673 | | if (ret == 0) { |
674 | | if (enc) |
675 | | ret = wc_Rc2CbcEncrypt(&rc2, input, input, length); |
676 | | else |
677 | | ret = wc_Rc2CbcDecrypt(&rc2, input, input, length); |
678 | | } |
679 | | wc_Rc2Free(&rc2); |
680 | | break; |
681 | | } |
682 | | #endif |
683 | | |
684 | 0 | default: |
685 | 0 | WOLFSSL_MSG("Unknown/Unsupported encrypt/decryption algorithm"); |
686 | 0 | ret = ALGO_ID_E; |
687 | 0 | WOLFSSL_ERROR_VERBOSE(ret); |
688 | 0 | } |
689 | 0 | } |
690 | | |
691 | 0 | #ifdef WOLFSSL_SMALL_STACK |
692 | 0 | if (key != NULL) |
693 | 0 | #endif |
694 | 0 | { |
695 | 0 | ForceZero(key, PKCS_MAX_KEY_SIZE); |
696 | 0 | #ifdef WOLFSSL_SMALL_STACK |
697 | 0 | XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
698 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
699 | | wc_MemZero_Check(key, PKCS_MAX_KEY_SIZE); |
700 | | #endif |
701 | 0 | } |
702 | |
|
703 | 0 | return ret; |
704 | 0 | } |
705 | | |
706 | | #endif /* HAVE_PKCS8 || HAVE_PKCS12 */ |
707 | | #endif /* !NO_PWDBASED && !NO_ASN */ |