wc_encrypt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /* wc_encrypt.c
  2. *
  3. * Copyright (C) 2006-2023 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. (int)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. XMEMSET(key, 0xff, WC_MAX_SYM_KEY_SIZE);
  288. wc_MemZero_Add("wc_BufferKeyDecrypt key", key, WC_MAX_SYM_KEY_SIZE);
  289. #endif
  290. (void)XMEMSET(key, 0, WC_MAX_SYM_KEY_SIZE);
  291. #ifndef NO_PWDBASED
  292. if ((ret = wc_PBKDF1(key, password, passwordSz, info->iv, PKCS5_SALT_SZ, 1,
  293. (int)info->keySz, hashType)) != 0) {
  294. #ifdef WOLFSSL_SMALL_STACK
  295. XFREE(key, NULL, DYNAMIC_TYPE_SYMMETRIC_KEY);
  296. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  297. wc_MemZero_Check(key, WC_MAX_SYM_KEY_SIZE);
  298. #endif
  299. return ret;
  300. }
  301. #endif
  302. #ifndef NO_DES3
  303. if (info->cipherType == WC_CIPHER_DES)
  304. ret = wc_Des_CbcEncryptWithKey(der, der, derSz, key, info->iv);
  305. if (info->cipherType == WC_CIPHER_DES3)
  306. ret = wc_Des3_CbcEncryptWithKey(der, der, derSz, key, info->iv);
  307. #endif /* NO_DES3 */
  308. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  309. if (info->cipherType == WC_CIPHER_AES_CBC)
  310. ret = wc_AesCbcEncryptWithKey(der, der, derSz, key, info->keySz,
  311. info->iv);
  312. #endif /* !NO_AES && HAVE_AES_CBC */
  313. ForceZero(key, WC_MAX_SYM_KEY_SIZE);
  314. #ifdef WOLFSSL_SMALL_STACK
  315. XFREE(key, NULL, DYNAMIC_TYPE_SYMMETRIC_KEY);
  316. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  317. wc_MemZero_Check(key, WC_MAX_SYM_KEY_SIZE);
  318. #endif
  319. return ret;
  320. }
  321. #endif /* !NO_ASN && WOLFSSL_ENCRYPTED_KEYS */
  322. #if !defined(NO_PWDBASED) && !defined(NO_ASN)
  323. #if defined(HAVE_PKCS8) || defined(HAVE_PKCS12)
  324. /* Decrypt/Encrypt input in place from parameters based on id
  325. *
  326. * returns a negative value on fail case
  327. */
  328. int wc_CryptKey(const char* password, int passwordSz, byte* salt,
  329. int saltSz, int iterations, int id, byte* input,
  330. int length, int version, byte* cbcIv, int enc, int shaOid)
  331. {
  332. int typeH = WC_HASH_TYPE_NONE;
  333. word32 derivedLen = 0;
  334. int ret = 0;
  335. #ifdef WOLFSSL_SMALL_STACK
  336. byte* key = NULL;
  337. #else
  338. byte key[PKCS_MAX_KEY_SIZE];
  339. #endif
  340. (void)input;
  341. (void)length;
  342. (void)enc;
  343. WOLFSSL_ENTER("wc_CryptKey");
  344. if (length < 0)
  345. return BAD_LENGTH_E;
  346. switch (id) {
  347. #ifndef NO_DES3
  348. #ifndef NO_MD5
  349. case PBE_MD5_DES:
  350. typeH = WC_MD5;
  351. derivedLen = 16; /* may need iv for v1.5 */
  352. break;
  353. #endif
  354. #ifndef NO_SHA
  355. case PBE_SHA1_DES:
  356. typeH = WC_SHA;
  357. derivedLen = 16; /* may need iv for v1.5 */
  358. break;
  359. #endif /* !NO_SHA */
  360. #if !defined(NO_SHA) || !defined(NO_SHA256)
  361. case PBE_SHA1_DES3:
  362. switch (shaOid) {
  363. #ifndef NO_SHA256
  364. case HMAC_SHA256_OID:
  365. typeH = WC_SHA256;
  366. derivedLen = 32;
  367. break;
  368. #endif
  369. #ifndef NO_SHA
  370. default:
  371. typeH = WC_SHA;
  372. derivedLen = 32; /* may need iv for v1.5 */
  373. break;
  374. #endif
  375. }
  376. break;
  377. #endif
  378. #endif /* !NO_DES3 */
  379. #if !defined(NO_SHA) && !defined(NO_RC4)
  380. case PBE_SHA1_RC4_128:
  381. typeH = WC_SHA;
  382. derivedLen = 16;
  383. break;
  384. #endif
  385. #if defined(WOLFSSL_AES_256)
  386. case PBE_AES256_CBC:
  387. switch(shaOid) {
  388. case HMAC_SHA256_OID:
  389. typeH = WC_SHA256;
  390. derivedLen = 32;
  391. break;
  392. #ifndef NO_SHA
  393. default:
  394. typeH = WC_SHA;
  395. derivedLen = 32;
  396. break;
  397. #endif
  398. }
  399. break;
  400. #endif /* WOLFSSL_AES_256 && !NO_SHA */
  401. #if defined(WOLFSSL_AES_128)
  402. case PBE_AES128_CBC:
  403. switch(shaOid) {
  404. case HMAC_SHA256_OID:
  405. typeH = WC_SHA256;
  406. derivedLen = 16;
  407. break;
  408. #ifndef NO_SHA
  409. default:
  410. typeH = WC_SHA;
  411. derivedLen = 16;
  412. break;
  413. #endif
  414. }
  415. break;
  416. #endif /* WOLFSSL_AES_128 && !NO_SHA */
  417. #ifdef WC_RC2
  418. case PBE_SHA1_40RC2_CBC:
  419. typeH = WC_SHA;
  420. derivedLen = 5;
  421. break;
  422. #endif
  423. default:
  424. WOLFSSL_MSG("Unknown/Unsupported encrypt/decrypt id");
  425. (void)shaOid;
  426. ret = ALGO_ID_E;
  427. WOLFSSL_ERROR_VERBOSE(ret);
  428. }
  429. #ifdef WOLFSSL_SMALL_STACK
  430. if (ret == 0) {
  431. key = (byte*)XMALLOC(PKCS_MAX_KEY_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  432. if (key == NULL)
  433. ret = MEMORY_E;
  434. }
  435. #endif
  436. if (ret == 0) {
  437. #ifdef WOLFSSL_CHECK_MEM_ZERO
  438. XMEMSET(key, 0xff, PKCS_MAX_KEY_SIZE);
  439. wc_MemZero_Add("wc_CryptKey key", key, PKCS_MAX_KEY_SIZE);
  440. #endif
  441. switch (version) {
  442. #ifndef NO_HMAC
  443. case PKCS5v2:
  444. ret = wc_PBKDF2(key, (byte*)password, passwordSz,
  445. salt, saltSz, iterations, (int)derivedLen, typeH);
  446. break;
  447. #endif
  448. #ifndef NO_SHA
  449. case PKCS5:
  450. ret = wc_PBKDF1(key, (byte*)password, passwordSz,
  451. salt, saltSz, iterations, (int)derivedLen, typeH);
  452. break;
  453. #endif
  454. #ifdef HAVE_PKCS12
  455. case PKCS12v1:
  456. {
  457. int i, idx = 0;
  458. byte unicodePasswd[MAX_UNICODE_SZ];
  459. if ( (passwordSz * 2 + 2) > (int)sizeof(unicodePasswd)) {
  460. ret = UNICODE_SIZE_E;
  461. break;
  462. }
  463. for (i = 0; i < passwordSz; i++) {
  464. unicodePasswd[idx++] = 0x00;
  465. unicodePasswd[idx++] = (byte)password[i];
  466. }
  467. /* add trailing NULL */
  468. unicodePasswd[idx++] = 0x00;
  469. unicodePasswd[idx++] = 0x00;
  470. ret = wc_PKCS12_PBKDF(key, unicodePasswd, idx, salt, saltSz,
  471. iterations, (int)derivedLen, typeH, 1);
  472. if (id != PBE_SHA1_RC4_128) {
  473. ret += wc_PKCS12_PBKDF(cbcIv, unicodePasswd, idx, salt,
  474. saltSz, iterations, 8, typeH, 2);
  475. }
  476. break;
  477. }
  478. #endif /* HAVE_PKCS12 */
  479. default:
  480. WOLFSSL_MSG("Unknown/Unsupported PKCS version");
  481. ret = ALGO_ID_E;
  482. WOLFSSL_ERROR_VERBOSE(ret);
  483. } /* switch (version) */
  484. }
  485. if (ret == 0) {
  486. switch (id) {
  487. #ifndef NO_DES3
  488. #if !defined(NO_SHA) || !defined(NO_MD5)
  489. case PBE_MD5_DES:
  490. case PBE_SHA1_DES:
  491. {
  492. Des des;
  493. byte* desIv = key + 8;
  494. if (version == PKCS5v2 || version == PKCS12v1)
  495. desIv = cbcIv;
  496. if (enc) {
  497. ret = wc_Des_SetKey(&des, key, desIv, DES_ENCRYPTION);
  498. }
  499. else {
  500. ret = wc_Des_SetKey(&des, key, desIv, DES_DECRYPTION);
  501. }
  502. if (ret == 0) {
  503. if (enc) {
  504. wc_Des_CbcEncrypt(&des, input, input, (word32)length);
  505. }
  506. else {
  507. wc_Des_CbcDecrypt(&des, input, input, (word32)length);
  508. }
  509. }
  510. break;
  511. }
  512. #endif /* !NO_SHA || !NO_MD5 */
  513. #ifndef NO_SHA
  514. case PBE_SHA1_DES3:
  515. {
  516. Des3 des;
  517. byte* desIv = key + 24;
  518. if (version == PKCS5v2 || version == PKCS12v1)
  519. desIv = cbcIv;
  520. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  521. if (ret != 0) {
  522. break;
  523. }
  524. if (enc) {
  525. ret = wc_Des3_SetKey(&des, key, desIv, DES_ENCRYPTION);
  526. }
  527. else {
  528. ret = wc_Des3_SetKey(&des, key, desIv, DES_DECRYPTION);
  529. }
  530. if (ret == 0) {
  531. if (enc) {
  532. ret = wc_Des3_CbcEncrypt(&des, input, input, (word32)length);
  533. }
  534. else {
  535. ret = wc_Des3_CbcDecrypt(&des, input, input, (word32)length);
  536. }
  537. }
  538. wc_Des3Free(&des);
  539. break;
  540. }
  541. #endif /* !NO_SHA */
  542. #endif
  543. #if !defined(NO_RC4) && !defined(NO_SHA)
  544. case PBE_SHA1_RC4_128:
  545. {
  546. Arc4 dec;
  547. wc_Arc4SetKey(&dec, key, derivedLen);
  548. wc_Arc4Process(&dec, input, input, (word32)length);
  549. break;
  550. }
  551. #endif
  552. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  553. #ifdef WOLFSSL_AES_256
  554. case PBE_AES256_CBC:
  555. case PBE_AES128_CBC:
  556. {
  557. int free_aes;
  558. #ifdef WOLFSSL_SMALL_STACK
  559. Aes *aes;
  560. aes = (Aes *)XMALLOC(sizeof *aes, NULL, DYNAMIC_TYPE_AES);
  561. if (aes == NULL) {
  562. ret = MEMORY_E;
  563. break;
  564. }
  565. #else
  566. Aes aes[1];
  567. #endif
  568. free_aes = 0;
  569. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  570. if (ret == 0) {
  571. free_aes = 1;
  572. if (enc) {
  573. ret = wc_AesSetKey(aes, key, derivedLen, cbcIv,
  574. AES_ENCRYPTION);
  575. }
  576. else {
  577. ret = wc_AesSetKey(aes, key, derivedLen, cbcIv,
  578. AES_DECRYPTION);
  579. }
  580. }
  581. if (ret == 0) {
  582. if (enc)
  583. ret = wc_AesCbcEncrypt(aes, input, input, (word32)length);
  584. else
  585. ret = wc_AesCbcDecrypt(aes, input, input, (word32)length);
  586. }
  587. if (free_aes)
  588. wc_AesFree(aes);
  589. ForceZero(aes, sizeof(Aes));
  590. #ifdef WOLFSSL_SMALL_STACK
  591. XFREE(aes, NULL, DYNAMIC_TYPE_AES);
  592. #endif
  593. break;
  594. }
  595. #endif /* WOLFSSL_AES_256 */
  596. #endif /* !NO_AES && HAVE_AES_CBC */
  597. #ifdef WC_RC2
  598. case PBE_SHA1_40RC2_CBC:
  599. {
  600. Rc2 rc2;
  601. /* effective key size for RC2-40-CBC is 40 bits */
  602. ret = wc_Rc2SetKey(&rc2, key, derivedLen, cbcIv, 40);
  603. if (ret == 0) {
  604. if (enc)
  605. ret = wc_Rc2CbcEncrypt(&rc2, input, input, length);
  606. else
  607. ret = wc_Rc2CbcDecrypt(&rc2, input, input, length);
  608. }
  609. if (ret == 0) {
  610. ForceZero(&rc2, sizeof(Rc2));
  611. }
  612. break;
  613. }
  614. #endif
  615. default:
  616. WOLFSSL_MSG("Unknown/Unsupported encrypt/decryption algorithm");
  617. ret = ALGO_ID_E;
  618. WOLFSSL_ERROR_VERBOSE(ret);
  619. }
  620. }
  621. #ifdef WOLFSSL_SMALL_STACK
  622. if (key != NULL)
  623. #endif
  624. {
  625. ForceZero(key, PKCS_MAX_KEY_SIZE);
  626. #ifdef WOLFSSL_SMALL_STACK
  627. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  628. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  629. wc_MemZero_Check(key, PKCS_MAX_KEY_SIZE);
  630. #endif
  631. }
  632. return ret;
  633. }
  634. #endif /* HAVE_PKCS8 || HAVE_PKCS12 */
  635. #endif /* !NO_PWDBASED && !NO_ASN */