evp_enc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /* crypto/evp/evp_enc.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/evp.h>
  61. #include <openssl/err.h>
  62. #include <openssl/rand.h>
  63. #ifndef OPENSSL_NO_ENGINE
  64. # include <openssl/engine.h>
  65. #endif
  66. #ifdef OPENSSL_FIPS
  67. # include <openssl/fips.h>
  68. #endif
  69. #include "evp_locl.h"
  70. #ifdef OPENSSL_FIPS
  71. # define M_do_cipher(ctx, out, in, inl) FIPS_cipher(ctx, out, in, inl)
  72. #else
  73. # define M_do_cipher(ctx, out, in, inl) ctx->cipher->do_cipher(ctx, out, in, inl)
  74. #endif
  75. const char EVP_version[] = "EVP" OPENSSL_VERSION_PTEXT;
  76. void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
  77. {
  78. memset(ctx, 0, sizeof(EVP_CIPHER_CTX));
  79. /* ctx->cipher=NULL; */
  80. }
  81. EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
  82. {
  83. EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof *ctx);
  84. if (ctx)
  85. EVP_CIPHER_CTX_init(ctx);
  86. return ctx;
  87. }
  88. int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  89. const unsigned char *key, const unsigned char *iv, int enc)
  90. {
  91. if (cipher)
  92. EVP_CIPHER_CTX_init(ctx);
  93. return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
  94. }
  95. int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  96. ENGINE *impl, const unsigned char *key,
  97. const unsigned char *iv, int enc)
  98. {
  99. if (enc == -1)
  100. enc = ctx->encrypt;
  101. else {
  102. if (enc)
  103. enc = 1;
  104. ctx->encrypt = enc;
  105. }
  106. #ifndef OPENSSL_NO_ENGINE
  107. /*
  108. * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
  109. * this context may already have an ENGINE! Try to avoid releasing the
  110. * previous handle, re-querying for an ENGINE, and having a
  111. * reinitialisation, when it may all be unecessary.
  112. */
  113. if (ctx->engine && ctx->cipher && (!cipher ||
  114. (cipher
  115. && (cipher->nid ==
  116. ctx->cipher->nid))))
  117. goto skip_to_init;
  118. #endif
  119. if (cipher) {
  120. /*
  121. * Ensure a context left lying around from last time is cleared (the
  122. * previous check attempted to avoid this if the same ENGINE and
  123. * EVP_CIPHER could be used).
  124. */
  125. if (ctx->cipher) {
  126. unsigned long flags = ctx->flags;
  127. EVP_CIPHER_CTX_cleanup(ctx);
  128. /* Restore encrypt and flags */
  129. ctx->encrypt = enc;
  130. ctx->flags = flags;
  131. }
  132. #ifndef OPENSSL_NO_ENGINE
  133. if (impl) {
  134. if (!ENGINE_init(impl)) {
  135. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  136. return 0;
  137. }
  138. } else
  139. /* Ask if an ENGINE is reserved for this job */
  140. impl = ENGINE_get_cipher_engine(cipher->nid);
  141. if (impl) {
  142. /* There's an ENGINE for this job ... (apparently) */
  143. const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
  144. if (!c) {
  145. /*
  146. * One positive side-effect of US's export control history,
  147. * is that we should at least be able to avoid using US
  148. * mispellings of "initialisation"?
  149. */
  150. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  151. return 0;
  152. }
  153. /* We'll use the ENGINE's private cipher definition */
  154. cipher = c;
  155. /*
  156. * Store the ENGINE functional reference so we know 'cipher' came
  157. * from an ENGINE and we need to release it when done.
  158. */
  159. ctx->engine = impl;
  160. } else
  161. ctx->engine = NULL;
  162. #endif
  163. #ifdef OPENSSL_FIPS
  164. if (FIPS_mode()) {
  165. const EVP_CIPHER *fcipher = NULL;
  166. if (cipher)
  167. fcipher = evp_get_fips_cipher(cipher);
  168. if (fcipher)
  169. cipher = fcipher;
  170. return FIPS_cipherinit(ctx, cipher, key, iv, enc);
  171. }
  172. #endif
  173. ctx->cipher = cipher;
  174. if (ctx->cipher->ctx_size) {
  175. ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size);
  176. if (!ctx->cipher_data) {
  177. ctx->cipher = NULL;
  178. EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
  179. return 0;
  180. }
  181. } else {
  182. ctx->cipher_data = NULL;
  183. }
  184. ctx->key_len = cipher->key_len;
  185. /* Preserve wrap enable flag, zero everything else */
  186. ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
  187. if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
  188. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
  189. ctx->cipher = NULL;
  190. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  191. return 0;
  192. }
  193. }
  194. } else if (!ctx->cipher) {
  195. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
  196. return 0;
  197. }
  198. #ifndef OPENSSL_NO_ENGINE
  199. skip_to_init:
  200. #endif
  201. #ifdef OPENSSL_FIPS
  202. if (FIPS_mode())
  203. return FIPS_cipherinit(ctx, cipher, key, iv, enc);
  204. #endif
  205. /* we assume block size is a power of 2 in *cryptUpdate */
  206. OPENSSL_assert(ctx->cipher->block_size == 1
  207. || ctx->cipher->block_size == 8
  208. || ctx->cipher->block_size == 16);
  209. if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
  210. && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
  211. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED);
  212. return 0;
  213. }
  214. if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
  215. switch (EVP_CIPHER_CTX_mode(ctx)) {
  216. case EVP_CIPH_STREAM_CIPHER:
  217. case EVP_CIPH_ECB_MODE:
  218. break;
  219. case EVP_CIPH_CFB_MODE:
  220. case EVP_CIPH_OFB_MODE:
  221. ctx->num = 0;
  222. /* fall-through */
  223. case EVP_CIPH_CBC_MODE:
  224. OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
  225. (int)sizeof(ctx->iv));
  226. if (iv)
  227. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  228. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  229. break;
  230. case EVP_CIPH_CTR_MODE:
  231. ctx->num = 0;
  232. /* Don't reuse IV for CTR mode */
  233. if (iv)
  234. memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  235. break;
  236. default:
  237. return 0;
  238. break;
  239. }
  240. }
  241. if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  242. if (!ctx->cipher->init(ctx, key, iv, enc))
  243. return 0;
  244. }
  245. ctx->buf_len = 0;
  246. ctx->final_used = 0;
  247. ctx->block_mask = ctx->cipher->block_size - 1;
  248. return 1;
  249. }
  250. int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  251. const unsigned char *in, int inl)
  252. {
  253. if (ctx->encrypt)
  254. return EVP_EncryptUpdate(ctx, out, outl, in, inl);
  255. else
  256. return EVP_DecryptUpdate(ctx, out, outl, in, inl);
  257. }
  258. int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  259. {
  260. if (ctx->encrypt)
  261. return EVP_EncryptFinal_ex(ctx, out, outl);
  262. else
  263. return EVP_DecryptFinal_ex(ctx, out, outl);
  264. }
  265. int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  266. {
  267. if (ctx->encrypt)
  268. return EVP_EncryptFinal(ctx, out, outl);
  269. else
  270. return EVP_DecryptFinal(ctx, out, outl);
  271. }
  272. int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  273. const unsigned char *key, const unsigned char *iv)
  274. {
  275. return EVP_CipherInit(ctx, cipher, key, iv, 1);
  276. }
  277. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  278. ENGINE *impl, const unsigned char *key,
  279. const unsigned char *iv)
  280. {
  281. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
  282. }
  283. int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  284. const unsigned char *key, const unsigned char *iv)
  285. {
  286. return EVP_CipherInit(ctx, cipher, key, iv, 0);
  287. }
  288. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  289. ENGINE *impl, const unsigned char *key,
  290. const unsigned char *iv)
  291. {
  292. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
  293. }
  294. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  295. const unsigned char *in, int inl)
  296. {
  297. int i, j, bl;
  298. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  299. i = M_do_cipher(ctx, out, in, inl);
  300. if (i < 0)
  301. return 0;
  302. else
  303. *outl = i;
  304. return 1;
  305. }
  306. if (inl <= 0) {
  307. *outl = 0;
  308. return inl == 0;
  309. }
  310. if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
  311. if (M_do_cipher(ctx, out, in, inl)) {
  312. *outl = inl;
  313. return 1;
  314. } else {
  315. *outl = 0;
  316. return 0;
  317. }
  318. }
  319. i = ctx->buf_len;
  320. bl = ctx->cipher->block_size;
  321. OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
  322. if (i != 0) {
  323. if (bl - i > inl) {
  324. memcpy(&(ctx->buf[i]), in, inl);
  325. ctx->buf_len += inl;
  326. *outl = 0;
  327. return 1;
  328. } else {
  329. j = bl - i;
  330. memcpy(&(ctx->buf[i]), in, j);
  331. if (!M_do_cipher(ctx, out, ctx->buf, bl))
  332. return 0;
  333. inl -= j;
  334. in += j;
  335. out += bl;
  336. *outl = bl;
  337. }
  338. } else
  339. *outl = 0;
  340. i = inl & (bl - 1);
  341. inl -= i;
  342. if (inl > 0) {
  343. if (!M_do_cipher(ctx, out, in, inl))
  344. return 0;
  345. *outl += inl;
  346. }
  347. if (i != 0)
  348. memcpy(ctx->buf, &(in[inl]), i);
  349. ctx->buf_len = i;
  350. return 1;
  351. }
  352. int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  353. {
  354. int ret;
  355. ret = EVP_EncryptFinal_ex(ctx, out, outl);
  356. return ret;
  357. }
  358. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  359. {
  360. int n, ret;
  361. unsigned int i, b, bl;
  362. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  363. ret = M_do_cipher(ctx, out, NULL, 0);
  364. if (ret < 0)
  365. return 0;
  366. else
  367. *outl = ret;
  368. return 1;
  369. }
  370. b = ctx->cipher->block_size;
  371. OPENSSL_assert(b <= sizeof ctx->buf);
  372. if (b == 1) {
  373. *outl = 0;
  374. return 1;
  375. }
  376. bl = ctx->buf_len;
  377. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  378. if (bl) {
  379. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
  380. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  381. return 0;
  382. }
  383. *outl = 0;
  384. return 1;
  385. }
  386. n = b - bl;
  387. for (i = bl; i < b; i++)
  388. ctx->buf[i] = n;
  389. ret = M_do_cipher(ctx, out, ctx->buf, b);
  390. if (ret)
  391. *outl = b;
  392. return ret;
  393. }
  394. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  395. const unsigned char *in, int inl)
  396. {
  397. int fix_len;
  398. unsigned int b;
  399. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  400. fix_len = M_do_cipher(ctx, out, in, inl);
  401. if (fix_len < 0) {
  402. *outl = 0;
  403. return 0;
  404. } else
  405. *outl = fix_len;
  406. return 1;
  407. }
  408. if (inl <= 0) {
  409. *outl = 0;
  410. return inl == 0;
  411. }
  412. if (ctx->flags & EVP_CIPH_NO_PADDING)
  413. return EVP_EncryptUpdate(ctx, out, outl, in, inl);
  414. b = ctx->cipher->block_size;
  415. OPENSSL_assert(b <= sizeof ctx->final);
  416. if (ctx->final_used) {
  417. memcpy(out, ctx->final, b);
  418. out += b;
  419. fix_len = 1;
  420. } else
  421. fix_len = 0;
  422. if (!EVP_EncryptUpdate(ctx, out, outl, in, inl))
  423. return 0;
  424. /*
  425. * if we have 'decrypted' a multiple of block size, make sure we have a
  426. * copy of this last block
  427. */
  428. if (b > 1 && !ctx->buf_len) {
  429. *outl -= b;
  430. ctx->final_used = 1;
  431. memcpy(ctx->final, &out[*outl], b);
  432. } else
  433. ctx->final_used = 0;
  434. if (fix_len)
  435. *outl += b;
  436. return 1;
  437. }
  438. int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  439. {
  440. int ret;
  441. ret = EVP_DecryptFinal_ex(ctx, out, outl);
  442. return ret;
  443. }
  444. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  445. {
  446. int i, n;
  447. unsigned int b;
  448. *outl = 0;
  449. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  450. i = M_do_cipher(ctx, out, NULL, 0);
  451. if (i < 0)
  452. return 0;
  453. else
  454. *outl = i;
  455. return 1;
  456. }
  457. b = ctx->cipher->block_size;
  458. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  459. if (ctx->buf_len) {
  460. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
  461. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  462. return 0;
  463. }
  464. *outl = 0;
  465. return 1;
  466. }
  467. if (b > 1) {
  468. if (ctx->buf_len || !ctx->final_used) {
  469. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
  470. return (0);
  471. }
  472. OPENSSL_assert(b <= sizeof ctx->final);
  473. /*
  474. * The following assumes that the ciphertext has been authenticated.
  475. * Otherwise it provides a padding oracle.
  476. */
  477. n = ctx->final[b - 1];
  478. if (n == 0 || n > (int)b) {
  479. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  480. return (0);
  481. }
  482. for (i = 0; i < n; i++) {
  483. if (ctx->final[--b] != n) {
  484. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  485. return (0);
  486. }
  487. }
  488. n = ctx->cipher->block_size - n;
  489. for (i = 0; i < n; i++)
  490. out[i] = ctx->final[i];
  491. *outl = n;
  492. } else
  493. *outl = 0;
  494. return (1);
  495. }
  496. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
  497. {
  498. if (ctx) {
  499. EVP_CIPHER_CTX_cleanup(ctx);
  500. OPENSSL_free(ctx);
  501. }
  502. }
  503. int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
  504. {
  505. #ifndef OPENSSL_FIPS
  506. if (c->cipher != NULL) {
  507. if (c->cipher->cleanup && !c->cipher->cleanup(c))
  508. return 0;
  509. /* Cleanse cipher context data */
  510. if (c->cipher_data)
  511. OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
  512. }
  513. if (c->cipher_data)
  514. OPENSSL_free(c->cipher_data);
  515. #endif
  516. #ifndef OPENSSL_NO_ENGINE
  517. if (c->engine)
  518. /*
  519. * The EVP_CIPHER we used belongs to an ENGINE, release the
  520. * functional reference we held for this reason.
  521. */
  522. ENGINE_finish(c->engine);
  523. #endif
  524. #ifdef OPENSSL_FIPS
  525. FIPS_cipher_ctx_cleanup(c);
  526. #endif
  527. memset(c, 0, sizeof(EVP_CIPHER_CTX));
  528. return 1;
  529. }
  530. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
  531. {
  532. if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
  533. return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
  534. if (c->key_len == keylen)
  535. return 1;
  536. if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  537. c->key_len = keylen;
  538. return 1;
  539. }
  540. EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
  541. return 0;
  542. }
  543. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
  544. {
  545. if (pad)
  546. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  547. else
  548. ctx->flags |= EVP_CIPH_NO_PADDING;
  549. return 1;
  550. }
  551. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  552. {
  553. int ret;
  554. if (!ctx->cipher) {
  555. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
  556. return 0;
  557. }
  558. if (!ctx->cipher->ctrl) {
  559. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
  560. return 0;
  561. }
  562. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  563. if (ret == -1) {
  564. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
  565. EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  566. return 0;
  567. }
  568. return ret;
  569. }
  570. int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
  571. {
  572. if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
  573. return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
  574. if (RAND_bytes(key, ctx->key_len) <= 0)
  575. return 0;
  576. return 1;
  577. }
  578. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  579. {
  580. if ((in == NULL) || (in->cipher == NULL)) {
  581. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
  582. return 0;
  583. }
  584. #ifndef OPENSSL_NO_ENGINE
  585. /* Make sure it's safe to copy a cipher context using an ENGINE */
  586. if (in->engine && !ENGINE_init(in->engine)) {
  587. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
  588. return 0;
  589. }
  590. #endif
  591. EVP_CIPHER_CTX_cleanup(out);
  592. memcpy(out, in, sizeof *out);
  593. if (in->cipher_data && in->cipher->ctx_size) {
  594. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  595. if (!out->cipher_data) {
  596. out->cipher = NULL;
  597. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
  598. return 0;
  599. }
  600. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  601. }
  602. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  603. if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
  604. out->cipher = NULL;
  605. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);
  606. return 0;
  607. }
  608. return 1;
  609. }