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