evp_enc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. 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. static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
  295. unsigned char *out, int *outl,
  296. const unsigned char *in, int inl)
  297. {
  298. int i, j, bl;
  299. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  300. i = M_do_cipher(ctx, out, in, inl);
  301. if (i < 0)
  302. return 0;
  303. else
  304. *outl = i;
  305. return 1;
  306. }
  307. if (inl <= 0) {
  308. *outl = 0;
  309. return inl == 0;
  310. }
  311. if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
  312. if (M_do_cipher(ctx, out, in, inl)) {
  313. *outl = inl;
  314. return 1;
  315. } else {
  316. *outl = 0;
  317. return 0;
  318. }
  319. }
  320. i = ctx->buf_len;
  321. bl = ctx->cipher->block_size;
  322. OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
  323. if (i != 0) {
  324. if (bl - i > inl) {
  325. memcpy(&(ctx->buf[i]), in, inl);
  326. ctx->buf_len += inl;
  327. *outl = 0;
  328. return 1;
  329. } else {
  330. j = bl - i;
  331. memcpy(&(ctx->buf[i]), in, j);
  332. if (!M_do_cipher(ctx, out, ctx->buf, bl))
  333. return 0;
  334. inl -= j;
  335. in += j;
  336. out += bl;
  337. *outl = bl;
  338. }
  339. } else
  340. *outl = 0;
  341. i = inl & (bl - 1);
  342. inl -= i;
  343. if (inl > 0) {
  344. if (!M_do_cipher(ctx, out, in, inl))
  345. return 0;
  346. *outl += inl;
  347. }
  348. if (i != 0)
  349. memcpy(ctx->buf, &(in[inl]), i);
  350. ctx->buf_len = i;
  351. return 1;
  352. }
  353. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  354. const unsigned char *in, int inl)
  355. {
  356. /* Prevent accidental use of decryption context when encrypting */
  357. if (!ctx->encrypt) {
  358. EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_INVALID_OPERATION);
  359. return 0;
  360. }
  361. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  362. }
  363. int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  364. {
  365. int ret;
  366. ret = EVP_EncryptFinal_ex(ctx, out, outl);
  367. return ret;
  368. }
  369. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  370. {
  371. int n, ret;
  372. unsigned int i, b, bl;
  373. /* Prevent accidental use of decryption context when encrypting */
  374. if (!ctx->encrypt) {
  375. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
  376. return 0;
  377. }
  378. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  379. ret = M_do_cipher(ctx, out, NULL, 0);
  380. if (ret < 0)
  381. return 0;
  382. else
  383. *outl = ret;
  384. return 1;
  385. }
  386. b = ctx->cipher->block_size;
  387. OPENSSL_assert(b <= sizeof(ctx->buf));
  388. if (b == 1) {
  389. *outl = 0;
  390. return 1;
  391. }
  392. bl = ctx->buf_len;
  393. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  394. if (bl) {
  395. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
  396. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  397. return 0;
  398. }
  399. *outl = 0;
  400. return 1;
  401. }
  402. n = b - bl;
  403. for (i = bl; i < b; i++)
  404. ctx->buf[i] = n;
  405. ret = M_do_cipher(ctx, out, ctx->buf, b);
  406. if (ret)
  407. *outl = b;
  408. return ret;
  409. }
  410. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  411. const unsigned char *in, int inl)
  412. {
  413. int fix_len;
  414. unsigned int b;
  415. /* Prevent accidental use of encryption context when decrypting */
  416. if (ctx->encrypt) {
  417. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);
  418. return 0;
  419. }
  420. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  421. fix_len = M_do_cipher(ctx, out, in, inl);
  422. if (fix_len < 0) {
  423. *outl = 0;
  424. return 0;
  425. } else
  426. *outl = fix_len;
  427. return 1;
  428. }
  429. if (inl <= 0) {
  430. *outl = 0;
  431. return inl == 0;
  432. }
  433. if (ctx->flags & EVP_CIPH_NO_PADDING)
  434. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  435. b = ctx->cipher->block_size;
  436. OPENSSL_assert(b <= sizeof(ctx->final));
  437. if (ctx->final_used) {
  438. memcpy(out, ctx->final, b);
  439. out += b;
  440. fix_len = 1;
  441. } else
  442. fix_len = 0;
  443. if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
  444. return 0;
  445. /*
  446. * if we have 'decrypted' a multiple of block size, make sure we have a
  447. * copy of this last block
  448. */
  449. if (b > 1 && !ctx->buf_len) {
  450. *outl -= b;
  451. ctx->final_used = 1;
  452. memcpy(ctx->final, &out[*outl], b);
  453. } else
  454. ctx->final_used = 0;
  455. if (fix_len)
  456. *outl += b;
  457. return 1;
  458. }
  459. int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  460. {
  461. int ret;
  462. ret = EVP_DecryptFinal_ex(ctx, out, outl);
  463. return ret;
  464. }
  465. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  466. {
  467. int i, n;
  468. unsigned int b;
  469. /* Prevent accidental use of encryption context when decrypting */
  470. if (ctx->encrypt) {
  471. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
  472. return 0;
  473. }
  474. *outl = 0;
  475. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  476. i = M_do_cipher(ctx, out, NULL, 0);
  477. if (i < 0)
  478. return 0;
  479. else
  480. *outl = i;
  481. return 1;
  482. }
  483. b = ctx->cipher->block_size;
  484. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  485. if (ctx->buf_len) {
  486. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
  487. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  488. return 0;
  489. }
  490. *outl = 0;
  491. return 1;
  492. }
  493. if (b > 1) {
  494. if (ctx->buf_len || !ctx->final_used) {
  495. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
  496. return (0);
  497. }
  498. OPENSSL_assert(b <= sizeof(ctx->final));
  499. /*
  500. * The following assumes that the ciphertext has been authenticated.
  501. * Otherwise it provides a padding oracle.
  502. */
  503. n = ctx->final[b - 1];
  504. if (n == 0 || n > (int)b) {
  505. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  506. return (0);
  507. }
  508. for (i = 0; i < n; i++) {
  509. if (ctx->final[--b] != n) {
  510. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  511. return (0);
  512. }
  513. }
  514. n = ctx->cipher->block_size - n;
  515. for (i = 0; i < n; i++)
  516. out[i] = ctx->final[i];
  517. *outl = n;
  518. } else
  519. *outl = 0;
  520. return (1);
  521. }
  522. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
  523. {
  524. if (ctx) {
  525. EVP_CIPHER_CTX_cleanup(ctx);
  526. OPENSSL_free(ctx);
  527. }
  528. }
  529. int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
  530. {
  531. #ifndef OPENSSL_FIPS
  532. if (c->cipher != NULL) {
  533. if (c->cipher->cleanup && !c->cipher->cleanup(c))
  534. return 0;
  535. /* Cleanse cipher context data */
  536. if (c->cipher_data)
  537. OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
  538. }
  539. if (c->cipher_data)
  540. OPENSSL_free(c->cipher_data);
  541. #endif
  542. #ifndef OPENSSL_NO_ENGINE
  543. if (c->engine)
  544. /*
  545. * The EVP_CIPHER we used belongs to an ENGINE, release the
  546. * functional reference we held for this reason.
  547. */
  548. ENGINE_finish(c->engine);
  549. #endif
  550. #ifdef OPENSSL_FIPS
  551. FIPS_cipher_ctx_cleanup(c);
  552. #endif
  553. memset(c, 0, sizeof(EVP_CIPHER_CTX));
  554. return 1;
  555. }
  556. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
  557. {
  558. if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
  559. return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
  560. if (c->key_len == keylen)
  561. return 1;
  562. if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  563. c->key_len = keylen;
  564. return 1;
  565. }
  566. EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
  567. return 0;
  568. }
  569. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
  570. {
  571. if (pad)
  572. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  573. else
  574. ctx->flags |= EVP_CIPH_NO_PADDING;
  575. return 1;
  576. }
  577. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  578. {
  579. int ret;
  580. if (!ctx->cipher) {
  581. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
  582. return 0;
  583. }
  584. if (!ctx->cipher->ctrl) {
  585. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
  586. return 0;
  587. }
  588. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  589. if (ret == -1) {
  590. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
  591. EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  592. return 0;
  593. }
  594. return ret;
  595. }
  596. int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
  597. {
  598. if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
  599. return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
  600. if (RAND_bytes(key, ctx->key_len) <= 0)
  601. return 0;
  602. return 1;
  603. }
  604. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  605. {
  606. if ((in == NULL) || (in->cipher == NULL)) {
  607. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
  608. return 0;
  609. }
  610. #ifndef OPENSSL_NO_ENGINE
  611. /* Make sure it's safe to copy a cipher context using an ENGINE */
  612. if (in->engine && !ENGINE_init(in->engine)) {
  613. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
  614. return 0;
  615. }
  616. #endif
  617. EVP_CIPHER_CTX_cleanup(out);
  618. memcpy(out, in, sizeof(*out));
  619. if (in->cipher_data && in->cipher->ctx_size) {
  620. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  621. if (!out->cipher_data) {
  622. out->cipher = NULL;
  623. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
  624. return 0;
  625. }
  626. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  627. }
  628. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  629. if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
  630. out->cipher = NULL;
  631. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);
  632. return 0;
  633. }
  634. return 1;
  635. }