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