evp_enc.c 20 KB

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