evp_enc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /* crypto/evp/evp_enc.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. return FIPS_cipherinit(ctx, cipher, key, iv, enc);
  166. #endif
  167. ctx->cipher = cipher;
  168. if (ctx->cipher->ctx_size) {
  169. ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size);
  170. if (!ctx->cipher_data) {
  171. EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
  172. return 0;
  173. }
  174. } else {
  175. ctx->cipher_data = NULL;
  176. }
  177. ctx->key_len = cipher->key_len;
  178. ctx->flags = 0;
  179. if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
  180. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
  181. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  182. return 0;
  183. }
  184. }
  185. } else if (!ctx->cipher) {
  186. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
  187. return 0;
  188. }
  189. #ifndef OPENSSL_NO_ENGINE
  190. skip_to_init:
  191. #endif
  192. #ifdef OPENSSL_FIPS
  193. if (FIPS_mode())
  194. return FIPS_cipherinit(ctx, cipher, key, iv, enc);
  195. #endif
  196. /* we assume block size is a power of 2 in *cryptUpdate */
  197. OPENSSL_assert(ctx->cipher->block_size == 1
  198. || ctx->cipher->block_size == 8
  199. || ctx->cipher->block_size == 16);
  200. if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
  201. switch (EVP_CIPHER_CTX_mode(ctx)) {
  202. case EVP_CIPH_STREAM_CIPHER:
  203. case EVP_CIPH_ECB_MODE:
  204. break;
  205. case EVP_CIPH_CFB_MODE:
  206. case EVP_CIPH_OFB_MODE:
  207. ctx->num = 0;
  208. /* fall-through */
  209. case EVP_CIPH_CBC_MODE:
  210. OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
  211. (int)sizeof(ctx->iv));
  212. if (iv)
  213. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  214. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  215. break;
  216. case EVP_CIPH_CTR_MODE:
  217. ctx->num = 0;
  218. /* Don't reuse IV for CTR mode */
  219. if (iv)
  220. memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  221. break;
  222. default:
  223. return 0;
  224. break;
  225. }
  226. }
  227. if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  228. if (!ctx->cipher->init(ctx, key, iv, enc))
  229. return 0;
  230. }
  231. ctx->buf_len = 0;
  232. ctx->final_used = 0;
  233. ctx->block_mask = ctx->cipher->block_size - 1;
  234. return 1;
  235. }
  236. int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  237. const unsigned char *in, int inl)
  238. {
  239. if (ctx->encrypt)
  240. return EVP_EncryptUpdate(ctx, out, outl, in, inl);
  241. else
  242. return EVP_DecryptUpdate(ctx, out, outl, in, inl);
  243. }
  244. int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  245. {
  246. if (ctx->encrypt)
  247. return EVP_EncryptFinal_ex(ctx, out, outl);
  248. else
  249. return EVP_DecryptFinal_ex(ctx, out, outl);
  250. }
  251. int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  252. {
  253. if (ctx->encrypt)
  254. return EVP_EncryptFinal(ctx, out, outl);
  255. else
  256. return EVP_DecryptFinal(ctx, out, outl);
  257. }
  258. int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  259. const unsigned char *key, const unsigned char *iv)
  260. {
  261. return EVP_CipherInit(ctx, cipher, key, iv, 1);
  262. }
  263. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  264. ENGINE *impl, const unsigned char *key,
  265. const unsigned char *iv)
  266. {
  267. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
  268. }
  269. int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  270. const unsigned char *key, const unsigned char *iv)
  271. {
  272. return EVP_CipherInit(ctx, cipher, key, iv, 0);
  273. }
  274. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  275. ENGINE *impl, const unsigned char *key,
  276. const unsigned char *iv)
  277. {
  278. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
  279. }
  280. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  281. const unsigned char *in, int inl)
  282. {
  283. int i, j, bl;
  284. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  285. i = M_do_cipher(ctx, out, in, inl);
  286. if (i < 0)
  287. return 0;
  288. else
  289. *outl = i;
  290. return 1;
  291. }
  292. if (inl <= 0) {
  293. *outl = 0;
  294. return inl == 0;
  295. }
  296. if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
  297. if (M_do_cipher(ctx, out, in, inl)) {
  298. *outl = inl;
  299. return 1;
  300. } else {
  301. *outl = 0;
  302. return 0;
  303. }
  304. }
  305. i = ctx->buf_len;
  306. bl = ctx->cipher->block_size;
  307. OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
  308. if (i != 0) {
  309. if (i + inl < bl) {
  310. memcpy(&(ctx->buf[i]), in, inl);
  311. ctx->buf_len += inl;
  312. *outl = 0;
  313. return 1;
  314. } else {
  315. j = bl - i;
  316. memcpy(&(ctx->buf[i]), in, j);
  317. if (!M_do_cipher(ctx, out, ctx->buf, bl))
  318. return 0;
  319. inl -= j;
  320. in += j;
  321. out += bl;
  322. *outl = bl;
  323. }
  324. } else
  325. *outl = 0;
  326. i = inl & (bl - 1);
  327. inl -= i;
  328. if (inl > 0) {
  329. if (!M_do_cipher(ctx, out, in, inl))
  330. return 0;
  331. *outl += inl;
  332. }
  333. if (i != 0)
  334. memcpy(ctx->buf, &(in[inl]), i);
  335. ctx->buf_len = i;
  336. return 1;
  337. }
  338. int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  339. {
  340. int ret;
  341. ret = EVP_EncryptFinal_ex(ctx, out, outl);
  342. return ret;
  343. }
  344. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  345. {
  346. int n, ret;
  347. unsigned int i, b, bl;
  348. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  349. ret = M_do_cipher(ctx, out, NULL, 0);
  350. if (ret < 0)
  351. return 0;
  352. else
  353. *outl = ret;
  354. return 1;
  355. }
  356. b = ctx->cipher->block_size;
  357. OPENSSL_assert(b <= sizeof ctx->buf);
  358. if (b == 1) {
  359. *outl = 0;
  360. return 1;
  361. }
  362. bl = ctx->buf_len;
  363. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  364. if (bl) {
  365. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
  366. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  367. return 0;
  368. }
  369. *outl = 0;
  370. return 1;
  371. }
  372. n = b - bl;
  373. for (i = bl; i < b; i++)
  374. ctx->buf[i] = n;
  375. ret = M_do_cipher(ctx, out, ctx->buf, b);
  376. if (ret)
  377. *outl = b;
  378. return ret;
  379. }
  380. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  381. const unsigned char *in, int inl)
  382. {
  383. int fix_len;
  384. unsigned int b;
  385. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  386. fix_len = M_do_cipher(ctx, out, in, inl);
  387. if (fix_len < 0) {
  388. *outl = 0;
  389. return 0;
  390. } else
  391. *outl = fix_len;
  392. return 1;
  393. }
  394. if (inl <= 0) {
  395. *outl = 0;
  396. return inl == 0;
  397. }
  398. if (ctx->flags & EVP_CIPH_NO_PADDING)
  399. return EVP_EncryptUpdate(ctx, out, outl, in, inl);
  400. b = ctx->cipher->block_size;
  401. OPENSSL_assert(b <= sizeof ctx->final);
  402. if (ctx->final_used) {
  403. memcpy(out, ctx->final, b);
  404. out += b;
  405. fix_len = 1;
  406. } else
  407. fix_len = 0;
  408. if (!EVP_EncryptUpdate(ctx, out, outl, in, inl))
  409. return 0;
  410. /*
  411. * if we have 'decrypted' a multiple of block size, make sure we have a
  412. * copy of this last block
  413. */
  414. if (b > 1 && !ctx->buf_len) {
  415. *outl -= b;
  416. ctx->final_used = 1;
  417. memcpy(ctx->final, &out[*outl], b);
  418. } else
  419. ctx->final_used = 0;
  420. if (fix_len)
  421. *outl += b;
  422. return 1;
  423. }
  424. int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  425. {
  426. int ret;
  427. ret = EVP_DecryptFinal_ex(ctx, out, outl);
  428. return ret;
  429. }
  430. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  431. {
  432. int i, n;
  433. unsigned int b;
  434. *outl = 0;
  435. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  436. i = M_do_cipher(ctx, out, NULL, 0);
  437. if (i < 0)
  438. return 0;
  439. else
  440. *outl = i;
  441. return 1;
  442. }
  443. b = ctx->cipher->block_size;
  444. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  445. if (ctx->buf_len) {
  446. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
  447. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  448. return 0;
  449. }
  450. *outl = 0;
  451. return 1;
  452. }
  453. if (b > 1) {
  454. if (ctx->buf_len || !ctx->final_used) {
  455. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
  456. return (0);
  457. }
  458. OPENSSL_assert(b <= sizeof ctx->final);
  459. /*
  460. * The following assumes that the ciphertext has been authenticated.
  461. * Otherwise it provides a padding oracle.
  462. */
  463. n = ctx->final[b - 1];
  464. if (n == 0 || n > (int)b) {
  465. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  466. return (0);
  467. }
  468. for (i = 0; i < n; i++) {
  469. if (ctx->final[--b] != n) {
  470. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  471. return (0);
  472. }
  473. }
  474. n = ctx->cipher->block_size - n;
  475. for (i = 0; i < n; i++)
  476. out[i] = ctx->final[i];
  477. *outl = n;
  478. } else
  479. *outl = 0;
  480. return (1);
  481. }
  482. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
  483. {
  484. if (ctx) {
  485. EVP_CIPHER_CTX_cleanup(ctx);
  486. OPENSSL_free(ctx);
  487. }
  488. }
  489. int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
  490. {
  491. #ifndef OPENSSL_FIPS
  492. if (c->cipher != NULL) {
  493. if (c->cipher->cleanup && !c->cipher->cleanup(c))
  494. return 0;
  495. /* Cleanse cipher context data */
  496. if (c->cipher_data)
  497. OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
  498. }
  499. if (c->cipher_data)
  500. OPENSSL_free(c->cipher_data);
  501. #endif
  502. #ifndef OPENSSL_NO_ENGINE
  503. if (c->engine)
  504. /*
  505. * The EVP_CIPHER we used belongs to an ENGINE, release the
  506. * functional reference we held for this reason.
  507. */
  508. ENGINE_finish(c->engine);
  509. #endif
  510. #ifdef OPENSSL_FIPS
  511. FIPS_cipher_ctx_cleanup(c);
  512. #endif
  513. memset(c, 0, sizeof(EVP_CIPHER_CTX));
  514. return 1;
  515. }
  516. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
  517. {
  518. if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
  519. return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
  520. if (c->key_len == keylen)
  521. return 1;
  522. if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  523. c->key_len = keylen;
  524. return 1;
  525. }
  526. EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
  527. return 0;
  528. }
  529. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
  530. {
  531. if (pad)
  532. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  533. else
  534. ctx->flags |= EVP_CIPH_NO_PADDING;
  535. return 1;
  536. }
  537. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  538. {
  539. int ret;
  540. if (!ctx->cipher) {
  541. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
  542. return 0;
  543. }
  544. if (!ctx->cipher->ctrl) {
  545. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
  546. return 0;
  547. }
  548. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  549. if (ret == -1) {
  550. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
  551. EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  552. return 0;
  553. }
  554. return ret;
  555. }
  556. int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
  557. {
  558. if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
  559. return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
  560. if (RAND_bytes(key, ctx->key_len) <= 0)
  561. return 0;
  562. return 1;
  563. }
  564. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  565. {
  566. if ((in == NULL) || (in->cipher == NULL)) {
  567. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
  568. return 0;
  569. }
  570. #ifndef OPENSSL_NO_ENGINE
  571. /* Make sure it's safe to copy a cipher context using an ENGINE */
  572. if (in->engine && !ENGINE_init(in->engine)) {
  573. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
  574. return 0;
  575. }
  576. #endif
  577. EVP_CIPHER_CTX_cleanup(out);
  578. memcpy(out, in, sizeof *out);
  579. if (in->cipher_data && in->cipher->ctx_size) {
  580. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  581. if (!out->cipher_data) {
  582. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
  583. return 0;
  584. }
  585. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  586. }
  587. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  588. return in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out);
  589. return 1;
  590. }