wc_encrypt.c 19 KB

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