evp_enc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <assert.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/evp.h>
  13. #include <openssl/err.h>
  14. #include <openssl/rand.h>
  15. #include <openssl/rand_drbg.h>
  16. #include <openssl/engine.h>
  17. #include "internal/evp_int.h"
  18. #include "evp_locl.h"
  19. int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c)
  20. {
  21. if (c == NULL)
  22. return 1;
  23. if (c->cipher != NULL) {
  24. if (c->cipher->cleanup && !c->cipher->cleanup(c))
  25. return 0;
  26. /* Cleanse cipher context data */
  27. if (c->cipher_data && c->cipher->ctx_size)
  28. OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
  29. }
  30. OPENSSL_free(c->cipher_data);
  31. #ifndef OPENSSL_NO_ENGINE
  32. ENGINE_finish(c->engine);
  33. #endif
  34. memset(c, 0, sizeof(*c));
  35. return 1;
  36. }
  37. EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
  38. {
  39. return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
  40. }
  41. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
  42. {
  43. EVP_CIPHER_CTX_reset(ctx);
  44. OPENSSL_free(ctx);
  45. }
  46. int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  47. const unsigned char *key, const unsigned char *iv, int enc)
  48. {
  49. if (cipher != NULL)
  50. EVP_CIPHER_CTX_reset(ctx);
  51. return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
  52. }
  53. int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  54. ENGINE *impl, const unsigned char *key,
  55. const unsigned char *iv, int enc)
  56. {
  57. if (enc == -1)
  58. enc = ctx->encrypt;
  59. else {
  60. if (enc)
  61. enc = 1;
  62. ctx->encrypt = enc;
  63. }
  64. #ifndef OPENSSL_NO_ENGINE
  65. /*
  66. * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
  67. * this context may already have an ENGINE! Try to avoid releasing the
  68. * previous handle, re-querying for an ENGINE, and having a
  69. * reinitialisation, when it may all be unnecessary.
  70. */
  71. if (ctx->engine && ctx->cipher
  72. && (cipher == NULL || cipher->nid == ctx->cipher->nid))
  73. goto skip_to_init;
  74. #endif
  75. if (cipher) {
  76. /*
  77. * Ensure a context left lying around from last time is cleared (the
  78. * previous check attempted to avoid this if the same ENGINE and
  79. * EVP_CIPHER could be used).
  80. */
  81. if (ctx->cipher) {
  82. unsigned long flags = ctx->flags;
  83. EVP_CIPHER_CTX_reset(ctx);
  84. /* Restore encrypt and flags */
  85. ctx->encrypt = enc;
  86. ctx->flags = flags;
  87. }
  88. #ifndef OPENSSL_NO_ENGINE
  89. if (impl) {
  90. if (!ENGINE_init(impl)) {
  91. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  92. return 0;
  93. }
  94. } else
  95. /* Ask if an ENGINE is reserved for this job */
  96. impl = ENGINE_get_cipher_engine(cipher->nid);
  97. if (impl) {
  98. /* There's an ENGINE for this job ... (apparently) */
  99. const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
  100. if (!c) {
  101. /*
  102. * One positive side-effect of US's export control history,
  103. * is that we should at least be able to avoid using US
  104. * misspellings of "initialisation"?
  105. */
  106. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  107. return 0;
  108. }
  109. /* We'll use the ENGINE's private cipher definition */
  110. cipher = c;
  111. /*
  112. * Store the ENGINE functional reference so we know 'cipher' came
  113. * from an ENGINE and we need to release it when done.
  114. */
  115. ctx->engine = impl;
  116. } else
  117. ctx->engine = NULL;
  118. #endif
  119. ctx->cipher = cipher;
  120. if (ctx->cipher->ctx_size) {
  121. ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
  122. if (ctx->cipher_data == NULL) {
  123. ctx->cipher = NULL;
  124. EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
  125. return 0;
  126. }
  127. } else {
  128. ctx->cipher_data = NULL;
  129. }
  130. ctx->key_len = cipher->key_len;
  131. /* Preserve wrap enable flag, zero everything else */
  132. ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
  133. if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
  134. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
  135. ctx->cipher = NULL;
  136. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  137. return 0;
  138. }
  139. }
  140. } else if (!ctx->cipher) {
  141. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
  142. return 0;
  143. }
  144. #ifndef OPENSSL_NO_ENGINE
  145. skip_to_init:
  146. #endif
  147. /* we assume block size is a power of 2 in *cryptUpdate */
  148. OPENSSL_assert(ctx->cipher->block_size == 1
  149. || ctx->cipher->block_size == 8
  150. || ctx->cipher->block_size == 16);
  151. if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
  152. && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
  153. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED);
  154. return 0;
  155. }
  156. if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) {
  157. switch (EVP_CIPHER_CTX_mode(ctx)) {
  158. case EVP_CIPH_STREAM_CIPHER:
  159. case EVP_CIPH_ECB_MODE:
  160. break;
  161. case EVP_CIPH_CFB_MODE:
  162. case EVP_CIPH_OFB_MODE:
  163. ctx->num = 0;
  164. /* fall-through */
  165. case EVP_CIPH_CBC_MODE:
  166. OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
  167. (int)sizeof(ctx->iv));
  168. if (iv)
  169. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  170. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  171. break;
  172. case EVP_CIPH_CTR_MODE:
  173. ctx->num = 0;
  174. /* Don't reuse IV for CTR mode */
  175. if (iv)
  176. memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  177. break;
  178. default:
  179. return 0;
  180. }
  181. }
  182. if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  183. if (!ctx->cipher->init(ctx, key, iv, enc))
  184. return 0;
  185. }
  186. ctx->buf_len = 0;
  187. ctx->final_used = 0;
  188. ctx->block_mask = ctx->cipher->block_size - 1;
  189. return 1;
  190. }
  191. int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  192. const unsigned char *in, int inl)
  193. {
  194. if (ctx->encrypt)
  195. return EVP_EncryptUpdate(ctx, out, outl, in, inl);
  196. else
  197. return EVP_DecryptUpdate(ctx, out, outl, in, inl);
  198. }
  199. int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  200. {
  201. if (ctx->encrypt)
  202. return EVP_EncryptFinal_ex(ctx, out, outl);
  203. else
  204. return EVP_DecryptFinal_ex(ctx, out, outl);
  205. }
  206. int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  207. {
  208. if (ctx->encrypt)
  209. return EVP_EncryptFinal(ctx, out, outl);
  210. else
  211. return EVP_DecryptFinal(ctx, out, outl);
  212. }
  213. int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  214. const unsigned char *key, const unsigned char *iv)
  215. {
  216. return EVP_CipherInit(ctx, cipher, key, iv, 1);
  217. }
  218. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  219. ENGINE *impl, const unsigned char *key,
  220. const unsigned char *iv)
  221. {
  222. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
  223. }
  224. int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  225. const unsigned char *key, const unsigned char *iv)
  226. {
  227. return EVP_CipherInit(ctx, cipher, key, iv, 0);
  228. }
  229. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  230. ENGINE *impl, const unsigned char *key,
  231. const unsigned char *iv)
  232. {
  233. return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
  234. }
  235. /*
  236. * According to the letter of standard difference between pointers
  237. * is specified to be valid only within same object. This makes
  238. * it formally challenging to determine if input and output buffers
  239. * are not partially overlapping with standard pointer arithmetic.
  240. */
  241. #ifdef PTRDIFF_T
  242. # undef PTRDIFF_T
  243. #endif
  244. #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
  245. /*
  246. * Then we have VMS that distinguishes itself by adhering to
  247. * sizeof(size_t)==4 even in 64-bit builds, which means that
  248. * difference between two pointers might be truncated to 32 bits.
  249. * In the context one can even wonder how comparison for
  250. * equality is implemented. To be on the safe side we adhere to
  251. * PTRDIFF_T even for comparison for equality.
  252. */
  253. # define PTRDIFF_T uint64_t
  254. #else
  255. # define PTRDIFF_T size_t
  256. #endif
  257. int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
  258. {
  259. PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
  260. /*
  261. * Check for partially overlapping buffers. [Binary logical
  262. * operations are used instead of boolean to minimize number
  263. * of conditional branches.]
  264. */
  265. int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
  266. (diff > (0 - (PTRDIFF_T)len)));
  267. return overlapped;
  268. }
  269. static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
  270. unsigned char *out, int *outl,
  271. const unsigned char *in, int inl)
  272. {
  273. int i, j, bl, cmpl = inl;
  274. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
  275. cmpl = (cmpl + 7) / 8;
  276. bl = ctx->cipher->block_size;
  277. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  278. /* If block size > 1 then the cipher will have to do this check */
  279. if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {
  280. EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  281. return 0;
  282. }
  283. i = ctx->cipher->do_cipher(ctx, out, in, inl);
  284. if (i < 0)
  285. return 0;
  286. else
  287. *outl = i;
  288. return 1;
  289. }
  290. if (inl <= 0) {
  291. *outl = 0;
  292. return inl == 0;
  293. }
  294. if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
  295. EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  296. return 0;
  297. }
  298. if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
  299. if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
  300. *outl = inl;
  301. return 1;
  302. } else {
  303. *outl = 0;
  304. return 0;
  305. }
  306. }
  307. i = ctx->buf_len;
  308. OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
  309. if (i != 0) {
  310. if (bl - i > inl) {
  311. memcpy(&(ctx->buf[i]), in, inl);
  312. ctx->buf_len += inl;
  313. *outl = 0;
  314. return 1;
  315. } else {
  316. j = bl - i;
  317. memcpy(&(ctx->buf[i]), in, j);
  318. inl -= j;
  319. in += j;
  320. if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
  321. return 0;
  322. out += bl;
  323. *outl = bl;
  324. }
  325. } else
  326. *outl = 0;
  327. i = inl & (bl - 1);
  328. inl -= i;
  329. if (inl > 0) {
  330. if (!ctx->cipher->do_cipher(ctx, out, in, inl))
  331. return 0;
  332. *outl += inl;
  333. }
  334. if (i != 0)
  335. memcpy(ctx->buf, &(in[inl]), i);
  336. ctx->buf_len = i;
  337. return 1;
  338. }
  339. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  340. const unsigned char *in, int inl)
  341. {
  342. /* Prevent accidental use of decryption context when encrypting */
  343. if (!ctx->encrypt) {
  344. EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_INVALID_OPERATION);
  345. return 0;
  346. }
  347. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  348. }
  349. int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  350. {
  351. int ret;
  352. ret = EVP_EncryptFinal_ex(ctx, out, outl);
  353. return ret;
  354. }
  355. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  356. {
  357. int n, ret;
  358. unsigned int i, b, bl;
  359. /* Prevent accidental use of decryption context when encrypting */
  360. if (!ctx->encrypt) {
  361. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
  362. return 0;
  363. }
  364. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  365. ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
  366. if (ret < 0)
  367. return 0;
  368. else
  369. *outl = ret;
  370. return 1;
  371. }
  372. b = ctx->cipher->block_size;
  373. OPENSSL_assert(b <= sizeof(ctx->buf));
  374. if (b == 1) {
  375. *outl = 0;
  376. return 1;
  377. }
  378. bl = ctx->buf_len;
  379. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  380. if (bl) {
  381. EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
  382. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  383. return 0;
  384. }
  385. *outl = 0;
  386. return 1;
  387. }
  388. n = b - bl;
  389. for (i = bl; i < b; i++)
  390. ctx->buf[i] = n;
  391. ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
  392. if (ret)
  393. *outl = b;
  394. return ret;
  395. }
  396. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  397. const unsigned char *in, int inl)
  398. {
  399. int fix_len, cmpl = inl;
  400. unsigned int b;
  401. /* Prevent accidental use of encryption context when decrypting */
  402. if (ctx->encrypt) {
  403. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);
  404. return 0;
  405. }
  406. b = ctx->cipher->block_size;
  407. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
  408. cmpl = (cmpl + 7) / 8;
  409. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  410. if (b == 1 && is_partially_overlapping(out, in, cmpl)) {
  411. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  412. return 0;
  413. }
  414. fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
  415. if (fix_len < 0) {
  416. *outl = 0;
  417. return 0;
  418. } else
  419. *outl = fix_len;
  420. return 1;
  421. }
  422. if (inl <= 0) {
  423. *outl = 0;
  424. return inl == 0;
  425. }
  426. if (ctx->flags & EVP_CIPH_NO_PADDING)
  427. return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
  428. OPENSSL_assert(b <= sizeof(ctx->final));
  429. if (ctx->final_used) {
  430. /* see comment about PTRDIFF_T comparison above */
  431. if (((PTRDIFF_T)out == (PTRDIFF_T)in)
  432. || is_partially_overlapping(out, in, b)) {
  433. EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
  434. return 0;
  435. }
  436. memcpy(out, ctx->final, b);
  437. out += b;
  438. fix_len = 1;
  439. } else
  440. fix_len = 0;
  441. if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
  442. return 0;
  443. /*
  444. * if we have 'decrypted' a multiple of block size, make sure we have a
  445. * copy of this last block
  446. */
  447. if (b > 1 && !ctx->buf_len) {
  448. *outl -= b;
  449. ctx->final_used = 1;
  450. memcpy(ctx->final, &out[*outl], b);
  451. } else
  452. ctx->final_used = 0;
  453. if (fix_len)
  454. *outl += b;
  455. return 1;
  456. }
  457. int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  458. {
  459. int ret;
  460. ret = EVP_DecryptFinal_ex(ctx, out, outl);
  461. return ret;
  462. }
  463. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  464. {
  465. int i, n;
  466. unsigned int b;
  467. /* Prevent accidental use of encryption context when decrypting */
  468. if (ctx->encrypt) {
  469. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
  470. return 0;
  471. }
  472. *outl = 0;
  473. if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
  474. i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
  475. if (i < 0)
  476. return 0;
  477. else
  478. *outl = i;
  479. return 1;
  480. }
  481. b = ctx->cipher->block_size;
  482. if (ctx->flags & EVP_CIPH_NO_PADDING) {
  483. if (ctx->buf_len) {
  484. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
  485. EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
  486. return 0;
  487. }
  488. *outl = 0;
  489. return 1;
  490. }
  491. if (b > 1) {
  492. if (ctx->buf_len || !ctx->final_used) {
  493. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
  494. return 0;
  495. }
  496. OPENSSL_assert(b <= sizeof(ctx->final));
  497. /*
  498. * The following assumes that the ciphertext has been authenticated.
  499. * Otherwise it provides a padding oracle.
  500. */
  501. n = ctx->final[b - 1];
  502. if (n == 0 || n > (int)b) {
  503. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  504. return 0;
  505. }
  506. for (i = 0; i < n; i++) {
  507. if (ctx->final[--b] != n) {
  508. EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
  509. return 0;
  510. }
  511. }
  512. n = ctx->cipher->block_size - n;
  513. for (i = 0; i < n; i++)
  514. out[i] = ctx->final[i];
  515. *outl = n;
  516. } else
  517. *outl = 0;
  518. return 1;
  519. }
  520. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
  521. {
  522. if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
  523. return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
  524. if (c->key_len == keylen)
  525. return 1;
  526. if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
  527. c->key_len = keylen;
  528. return 1;
  529. }
  530. EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
  531. return 0;
  532. }
  533. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
  534. {
  535. if (pad)
  536. ctx->flags &= ~EVP_CIPH_NO_PADDING;
  537. else
  538. ctx->flags |= EVP_CIPH_NO_PADDING;
  539. return 1;
  540. }
  541. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  542. {
  543. int ret;
  544. if (!ctx->cipher) {
  545. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
  546. return 0;
  547. }
  548. if (!ctx->cipher->ctrl) {
  549. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
  550. return 0;
  551. }
  552. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  553. if (ret == -1) {
  554. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
  555. EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  556. return 0;
  557. }
  558. return ret;
  559. }
  560. int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
  561. {
  562. if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
  563. return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
  564. if (RAND_priv_bytes(key, ctx->key_len) <= 0)
  565. return 0;
  566. return 1;
  567. }
  568. int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  569. {
  570. if ((in == NULL) || (in->cipher == NULL)) {
  571. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
  572. return 0;
  573. }
  574. #ifndef OPENSSL_NO_ENGINE
  575. /* Make sure it's safe to copy a cipher context using an ENGINE */
  576. if (in->engine && !ENGINE_init(in->engine)) {
  577. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
  578. return 0;
  579. }
  580. #endif
  581. EVP_CIPHER_CTX_reset(out);
  582. memcpy(out, in, sizeof(*out));
  583. if (in->cipher_data && in->cipher->ctx_size) {
  584. out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
  585. if (out->cipher_data == NULL) {
  586. out->cipher = NULL;
  587. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
  588. return 0;
  589. }
  590. memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
  591. }
  592. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  593. if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
  594. out->cipher = NULL;
  595. EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);
  596. return 0;
  597. }
  598. return 1;
  599. }