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