wc_encrypt.c 20 KB

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