Add basic aria and camellia ciphers modes to default provider
[openssl.git] / crypto / evp / evp_enc.c
1 /*
2  * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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/rand_drbg.h>
17 #include <openssl/engine.h>
18 #include <openssl/params.h>
19 #include <openssl/core_names.h>
20 #include "internal/evp_int.h"
21 #include "internal/provider.h"
22 #include "evp_locl.h"
23
24 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
25 {
26     if (ctx == NULL)
27         return 1;
28
29     if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
30         goto legacy;
31
32     if (ctx->provctx != NULL) {
33         if (ctx->cipher->freectx != NULL)
34             ctx->cipher->freectx(ctx->provctx);
35         ctx->provctx = NULL;
36     }
37     if (ctx->fetched_cipher != NULL)
38         EVP_CIPHER_meth_free(ctx->fetched_cipher);
39     memset(ctx, 0, sizeof(*ctx));
40
41     return 1;
42
43     /* TODO(3.0): Remove legacy code below */
44  legacy:
45
46     if (ctx->cipher != NULL) {
47         if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
48             return 0;
49         /* Cleanse cipher context data */
50         if (ctx->cipher_data && ctx->cipher->ctx_size)
51             OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
52     }
53     OPENSSL_free(ctx->cipher_data);
54 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
55     ENGINE_finish(ctx->engine);
56 #endif
57     memset(ctx, 0, sizeof(*ctx));
58     return 1;
59 }
60
61 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
62 {
63     return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
64 }
65
66 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
67 {
68     EVP_CIPHER_CTX_reset(ctx);
69     OPENSSL_free(ctx);
70 }
71
72 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
73                    const unsigned char *key, const unsigned char *iv, int enc)
74 {
75     if (cipher != NULL)
76         EVP_CIPHER_CTX_reset(ctx);
77     return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
78 }
79
80 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
81                       ENGINE *impl, const unsigned char *key,
82                       const unsigned char *iv, int enc)
83 {
84 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
85     ENGINE *tmpimpl = NULL;
86 #endif
87     const EVP_CIPHER *tmpcipher;
88
89     /*
90      * enc == 1 means we are encrypting.
91      * enc == 0 means we are decrypting.
92      * enc == -1 means, use the previously initialised value for encrypt/decrypt
93      */
94     if (enc == -1) {
95         enc = ctx->encrypt;
96     } else {
97         if (enc)
98             enc = 1;
99         ctx->encrypt = enc;
100     }
101
102     if (cipher == NULL && ctx->cipher == NULL) {
103         EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
104         return 0;
105     }
106
107     /* TODO(3.0): Legacy work around code below. Remove this */
108
109 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
110     /*
111      * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
112      * this context may already have an ENGINE! Try to avoid releasing the
113      * previous handle, re-querying for an ENGINE, and having a
114      * reinitialisation, when it may all be unnecessary.
115      */
116     if (ctx->engine && ctx->cipher
117         && (cipher == NULL || cipher->nid == ctx->cipher->nid))
118         goto skip_to_init;
119
120     if (cipher != NULL && impl == NULL) {
121          /* Ask if an ENGINE is reserved for this job */
122         tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
123     }
124 #endif
125
126     /*
127      * If there are engines involved then we should use legacy handling for now.
128      */
129     if (ctx->engine != NULL
130 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
131             || tmpimpl != NULL
132 #endif
133             || impl != NULL) {
134         if (ctx->cipher == ctx->fetched_cipher)
135             ctx->cipher = NULL;
136         EVP_CIPHER_meth_free(ctx->fetched_cipher);
137         ctx->fetched_cipher = NULL;
138         goto legacy;
139     }
140
141     tmpcipher = (cipher == NULL) ? ctx->cipher : cipher;
142
143     if (tmpcipher->prov == NULL) {
144         switch(tmpcipher->nid) {
145         case NID_aes_256_ecb:
146         case NID_aes_192_ecb:
147         case NID_aes_128_ecb:
148         case NID_aes_256_cbc:
149         case NID_aes_192_cbc:
150         case NID_aes_128_cbc:
151         case NID_aes_256_ofb128:
152         case NID_aes_192_ofb128:
153         case NID_aes_128_ofb128:
154         case NID_aes_256_cfb128:
155         case NID_aes_192_cfb128:
156         case NID_aes_128_cfb128:
157         case NID_aes_256_cfb1:
158         case NID_aes_192_cfb1:
159         case NID_aes_128_cfb1:
160         case NID_aes_256_cfb8:
161         case NID_aes_192_cfb8:
162         case NID_aes_128_cfb8:
163         case NID_aes_256_ctr:
164         case NID_aes_192_ctr:
165         case NID_aes_128_ctr:
166         case NID_aes_256_gcm:
167         case NID_aes_192_gcm:
168         case NID_aes_128_gcm:
169         case NID_aria_256_gcm:
170         case NID_aria_192_gcm:
171         case NID_aria_128_gcm:
172         case NID_aes_256_ccm:
173         case NID_aes_192_ccm:
174         case NID_aes_128_ccm:
175         case NID_aria_256_ccm:
176         case NID_aria_192_ccm:
177         case NID_aria_128_ccm:
178         case NID_aria_256_ecb:
179         case NID_aria_192_ecb:
180         case NID_aria_128_ecb:
181         case NID_aria_256_cbc:
182         case NID_aria_192_cbc:
183         case NID_aria_128_cbc:
184         case NID_aria_256_ofb128:
185         case NID_aria_192_ofb128:
186         case NID_aria_128_ofb128:
187         case NID_aria_256_cfb128:
188         case NID_aria_192_cfb128:
189         case NID_aria_128_cfb128:
190         case NID_aria_256_cfb1:
191         case NID_aria_192_cfb1:
192         case NID_aria_128_cfb1:
193         case NID_aria_256_cfb8:
194         case NID_aria_192_cfb8:
195         case NID_aria_128_cfb8:
196         case NID_aria_256_ctr:
197         case NID_aria_192_ctr:
198         case NID_aria_128_ctr:
199         case NID_camellia_256_ecb:
200         case NID_camellia_192_ecb:
201         case NID_camellia_128_ecb:
202         case NID_camellia_256_cbc:
203         case NID_camellia_192_cbc:
204         case NID_camellia_128_cbc:
205         case NID_camellia_256_ofb128:
206         case NID_camellia_192_ofb128:
207         case NID_camellia_128_ofb128:
208         case NID_camellia_256_cfb128:
209         case NID_camellia_192_cfb128:
210         case NID_camellia_128_cfb128:
211         case NID_camellia_256_cfb1:
212         case NID_camellia_192_cfb1:
213         case NID_camellia_128_cfb1:
214         case NID_camellia_256_cfb8:
215         case NID_camellia_192_cfb8:
216         case NID_camellia_128_cfb8:
217         case NID_camellia_256_ctr:
218         case NID_camellia_192_ctr:
219         case NID_camellia_128_ctr:
220             break;
221         default:
222             goto legacy;
223         }
224     }
225
226     /*
227      * Ensure a context left lying around from last time is cleared
228      * (legacy code)
229      */
230     if (cipher != NULL && ctx->cipher != NULL) {
231         OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
232         ctx->cipher_data = NULL;
233     }
234
235
236     /* TODO(3.0): Start of non-legacy code below */
237
238     /* Ensure a context left lying around from last time is cleared */
239     if (cipher != NULL && ctx->cipher != NULL) {
240         unsigned long flags = ctx->flags;
241
242         EVP_CIPHER_CTX_reset(ctx);
243         /* Restore encrypt and flags */
244         ctx->encrypt = enc;
245         ctx->flags = flags;
246     }
247
248     if (cipher == NULL)
249         cipher = ctx->cipher;
250
251     if (cipher->prov == NULL) {
252 #ifdef FIPS_MODE
253         /* We only do explict fetches inside the FIPS module */
254         EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
255         return 0;
256 #else
257         EVP_CIPHER *provciph =
258             EVP_CIPHER_fetch(NULL, OBJ_nid2sn(cipher->nid), "");
259
260         if (provciph == NULL) {
261             EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
262             return 0;
263         }
264         cipher = provciph;
265         EVP_CIPHER_meth_free(ctx->fetched_cipher);
266         ctx->fetched_cipher = provciph;
267 #endif
268     }
269
270     ctx->cipher = cipher;
271     if (ctx->provctx == NULL) {
272         ctx->provctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
273         if (ctx->provctx == NULL) {
274             EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
275             return 0;
276         }
277     }
278
279     if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
280         /*
281          * If this ctx was already set up for no padding then we need to tell
282          * the new cipher about it.
283          */
284         if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
285             return 0;
286     }
287
288     switch (EVP_CIPHER_mode(ctx->cipher)) {
289     case EVP_CIPH_CFB_MODE:
290     case EVP_CIPH_OFB_MODE:
291     case EVP_CIPH_CBC_MODE:
292         /* For these modes we remember the original IV for later use */
293         if (!ossl_assert(EVP_CIPHER_CTX_iv_length(ctx) <= (int)sizeof(ctx->oiv))) {
294             EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
295             return 0;
296         }
297         if (iv != NULL)
298             memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
299     }
300
301     if (enc) {
302         if (ctx->cipher->einit == NULL) {
303             EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
304             return 0;
305         }
306
307         return ctx->cipher->einit(ctx->provctx,
308                                   key,
309                                   key == NULL ? 0
310                                               : EVP_CIPHER_CTX_key_length(ctx),
311                                   iv,
312                                   iv == NULL ? 0
313                                              : EVP_CIPHER_CTX_iv_length(ctx));
314     }
315
316     if (ctx->cipher->dinit == NULL) {
317         EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
318         return 0;
319     }
320
321     return ctx->cipher->dinit(ctx->provctx,
322                               key,
323                               key == NULL ? 0
324                                           : EVP_CIPHER_CTX_key_length(ctx),
325                               iv,
326                               iv == NULL ? 0
327                                          : EVP_CIPHER_CTX_iv_length(ctx));
328
329     /* TODO(3.0): Remove legacy code below */
330  legacy:
331
332     if (cipher != NULL) {
333         /*
334          * Ensure a context left lying around from last time is cleared (we
335          * previously attempted to avoid this if the same ENGINE and
336          * EVP_CIPHER could be used).
337          */
338         if (ctx->cipher) {
339             unsigned long flags = ctx->flags;
340             EVP_CIPHER_CTX_reset(ctx);
341             /* Restore encrypt and flags */
342             ctx->encrypt = enc;
343             ctx->flags = flags;
344         }
345 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
346         if (impl != NULL) {
347             if (!ENGINE_init(impl)) {
348                 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
349                 return 0;
350             }
351         } else {
352             impl = tmpimpl;
353         }
354         if (impl != NULL) {
355             /* There's an ENGINE for this job ... (apparently) */
356             const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
357
358             if (c == NULL) {
359                 /*
360                  * One positive side-effect of US's export control history,
361                  * is that we should at least be able to avoid using US
362                  * misspellings of "initialisation"?
363                  */
364                 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
365                 return 0;
366             }
367             /* We'll use the ENGINE's private cipher definition */
368             cipher = c;
369             /*
370              * Store the ENGINE functional reference so we know 'cipher' came
371              * from an ENGINE and we need to release it when done.
372              */
373             ctx->engine = impl;
374         } else {
375             ctx->engine = NULL;
376         }
377 #endif
378
379         ctx->cipher = cipher;
380         if (ctx->cipher->ctx_size) {
381             ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
382             if (ctx->cipher_data == NULL) {
383                 ctx->cipher = NULL;
384                 EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
385                 return 0;
386             }
387         } else {
388             ctx->cipher_data = NULL;
389         }
390         ctx->key_len = cipher->key_len;
391         /* Preserve wrap enable flag, zero everything else */
392         ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
393         if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
394             if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
395                 ctx->cipher = NULL;
396                 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
397                 return 0;
398             }
399         }
400     }
401 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
402  skip_to_init:
403 #endif
404     if (ctx->cipher == NULL)
405         return 0;
406
407     /* we assume block size is a power of 2 in *cryptUpdate */
408     OPENSSL_assert(ctx->cipher->block_size == 1
409                    || ctx->cipher->block_size == 8
410                    || ctx->cipher->block_size == 16);
411
412     if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
413         && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
414         EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED);
415         return 0;
416     }
417
418     if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) {
419         switch (EVP_CIPHER_CTX_mode(ctx)) {
420
421         case EVP_CIPH_STREAM_CIPHER:
422         case EVP_CIPH_ECB_MODE:
423             break;
424
425         case EVP_CIPH_CFB_MODE:
426         case EVP_CIPH_OFB_MODE:
427
428             ctx->num = 0;
429             /* fall-through */
430
431         case EVP_CIPH_CBC_MODE:
432
433             OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
434                            (int)sizeof(ctx->iv));
435             if (iv)
436                 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
437             memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
438             break;
439
440         case EVP_CIPH_CTR_MODE:
441             ctx->num = 0;
442             /* Don't reuse IV for CTR mode */
443             if (iv)
444                 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
445             break;
446
447         default:
448             return 0;
449         }
450     }
451
452     if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
453         if (!ctx->cipher->init(ctx, key, iv, enc))
454             return 0;
455     }
456     ctx->buf_len = 0;
457     ctx->final_used = 0;
458     ctx->block_mask = ctx->cipher->block_size - 1;
459     return 1;
460 }
461
462 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
463                      const unsigned char *in, int inl)
464 {
465     if (ctx->encrypt)
466         return EVP_EncryptUpdate(ctx, out, outl, in, inl);
467     else
468         return EVP_DecryptUpdate(ctx, out, outl, in, inl);
469 }
470
471 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
472 {
473     if (ctx->encrypt)
474         return EVP_EncryptFinal_ex(ctx, out, outl);
475     else
476         return EVP_DecryptFinal_ex(ctx, out, outl);
477 }
478
479 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
480 {
481     if (ctx->encrypt)
482         return EVP_EncryptFinal(ctx, out, outl);
483     else
484         return EVP_DecryptFinal(ctx, out, outl);
485 }
486
487 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
488                     const unsigned char *key, const unsigned char *iv)
489 {
490     return EVP_CipherInit(ctx, cipher, key, iv, 1);
491 }
492
493 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
494                        ENGINE *impl, const unsigned char *key,
495                        const unsigned char *iv)
496 {
497     return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
498 }
499
500 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
501                     const unsigned char *key, const unsigned char *iv)
502 {
503     return EVP_CipherInit(ctx, cipher, key, iv, 0);
504 }
505
506 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
507                        ENGINE *impl, const unsigned char *key,
508                        const unsigned char *iv)
509 {
510     return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
511 }
512
513 /*
514  * According to the letter of standard difference between pointers
515  * is specified to be valid only within same object. This makes
516  * it formally challenging to determine if input and output buffers
517  * are not partially overlapping with standard pointer arithmetic.
518  */
519 #ifdef PTRDIFF_T
520 # undef PTRDIFF_T
521 #endif
522 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
523 /*
524  * Then we have VMS that distinguishes itself by adhering to
525  * sizeof(size_t)==4 even in 64-bit builds, which means that
526  * difference between two pointers might be truncated to 32 bits.
527  * In the context one can even wonder how comparison for
528  * equality is implemented. To be on the safe side we adhere to
529  * PTRDIFF_T even for comparison for equality.
530  */
531 # define PTRDIFF_T uint64_t
532 #else
533 # define PTRDIFF_T size_t
534 #endif
535
536 int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
537 {
538     PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
539     /*
540      * Check for partially overlapping buffers. [Binary logical
541      * operations are used instead of boolean to minimize number
542      * of conditional branches.]
543      */
544     int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
545                                                 (diff > (0 - (PTRDIFF_T)len)));
546
547     return overlapped;
548 }
549
550 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
551                                     unsigned char *out, int *outl,
552                                     const unsigned char *in, int inl)
553 {
554     int i, j, bl, cmpl = inl;
555
556     if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
557         cmpl = (cmpl + 7) / 8;
558
559     bl = ctx->cipher->block_size;
560
561     if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
562         /* If block size > 1 then the cipher will have to do this check */
563         if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {
564             EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
565             return 0;
566         }
567
568         i = ctx->cipher->do_cipher(ctx, out, in, inl);
569         if (i < 0)
570             return 0;
571         else
572             *outl = i;
573         return 1;
574     }
575
576     if (inl <= 0) {
577         *outl = 0;
578         return inl == 0;
579     }
580     if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
581         EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
582         return 0;
583     }
584
585     if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
586         if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
587             *outl = inl;
588             return 1;
589         } else {
590             *outl = 0;
591             return 0;
592         }
593     }
594     i = ctx->buf_len;
595     OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
596     if (i != 0) {
597         if (bl - i > inl) {
598             memcpy(&(ctx->buf[i]), in, inl);
599             ctx->buf_len += inl;
600             *outl = 0;
601             return 1;
602         } else {
603             j = bl - i;
604             memcpy(&(ctx->buf[i]), in, j);
605             inl -= j;
606             in += j;
607             if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
608                 return 0;
609             out += bl;
610             *outl = bl;
611         }
612     } else
613         *outl = 0;
614     i = inl & (bl - 1);
615     inl -= i;
616     if (inl > 0) {
617         if (!ctx->cipher->do_cipher(ctx, out, in, inl))
618             return 0;
619         *outl += inl;
620     }
621
622     if (i != 0)
623         memcpy(ctx->buf, &(in[inl]), i);
624     ctx->buf_len = i;
625     return 1;
626 }
627
628
629 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
630                       const unsigned char *in, int inl)
631 {
632     int ret;
633     size_t soutl;
634     int blocksize;
635
636     /* Prevent accidental use of decryption context when encrypting */
637     if (!ctx->encrypt) {
638         EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_INVALID_OPERATION);
639         return 0;
640     }
641
642     if (ctx->cipher == NULL) {
643         EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_NO_CIPHER_SET);
644         return 0;
645     }
646
647     if (ctx->cipher->prov == NULL)
648         goto legacy;
649
650     blocksize = EVP_CIPHER_CTX_block_size(ctx);
651
652     if (ctx->cipher->cupdate == NULL  || blocksize < 1) {
653         EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_UPDATE_ERROR);
654         return 0;
655     }
656     ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,
657                                inl + (blocksize == 1 ? 0 : blocksize), in,
658                                (size_t)inl);
659
660     if (ret) {
661         if (soutl > INT_MAX) {
662             EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_UPDATE_ERROR);
663             return 0;
664         }
665         *outl = soutl;
666     }
667
668     return ret;
669
670     /* TODO(3.0): Remove legacy code below */
671  legacy:
672
673     return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
674 }
675
676 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
677 {
678     int ret;
679     ret = EVP_EncryptFinal_ex(ctx, out, outl);
680     return ret;
681 }
682
683 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
684 {
685     int n, ret;
686     unsigned int i, b, bl;
687     size_t soutl;
688     int blocksize;
689
690     /* Prevent accidental use of decryption context when encrypting */
691     if (!ctx->encrypt) {
692         EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
693         return 0;
694     }
695
696     if (ctx->cipher == NULL) {
697         EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_NO_CIPHER_SET);
698         return 0;
699     }
700     if (ctx->cipher->prov == NULL)
701         goto legacy;
702
703     blocksize = EVP_CIPHER_CTX_block_size(ctx);
704
705     if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
706         EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_FINAL_ERROR);
707         return 0;
708     }
709
710     ret = ctx->cipher->cfinal(ctx->provctx, out, &soutl,
711                               blocksize == 1 ? 0 : blocksize);
712
713     if (ret) {
714         if (soutl > INT_MAX) {
715             EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_FINAL_ERROR);
716             return 0;
717         }
718         *outl = soutl;
719     }
720
721     return ret;
722
723     /* TODO(3.0): Remove legacy code below */
724  legacy:
725
726     if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
727         ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
728         if (ret < 0)
729             return 0;
730         else
731             *outl = ret;
732         return 1;
733     }
734
735     b = ctx->cipher->block_size;
736     OPENSSL_assert(b <= sizeof(ctx->buf));
737     if (b == 1) {
738         *outl = 0;
739         return 1;
740     }
741     bl = ctx->buf_len;
742     if (ctx->flags & EVP_CIPH_NO_PADDING) {
743         if (bl) {
744             EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
745                    EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
746             return 0;
747         }
748         *outl = 0;
749         return 1;
750     }
751
752     n = b - bl;
753     for (i = bl; i < b; i++)
754         ctx->buf[i] = n;
755     ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
756
757     if (ret)
758         *outl = b;
759
760     return ret;
761 }
762
763 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
764                       const unsigned char *in, int inl)
765 {
766     int fix_len, cmpl = inl, ret;
767     unsigned int b;
768     size_t soutl;
769     int blocksize;
770
771     /* Prevent accidental use of encryption context when decrypting */
772     if (ctx->encrypt) {
773         EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);
774         return 0;
775     }
776
777     if (ctx->cipher == NULL) {
778         EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_NO_CIPHER_SET);
779         return 0;
780     }
781     if (ctx->cipher->prov == NULL)
782         goto legacy;
783
784     blocksize = EVP_CIPHER_CTX_block_size(ctx);
785
786     if (ctx->cipher->cupdate == NULL || blocksize < 1) {
787         EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_UPDATE_ERROR);
788         return 0;
789     }
790     ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,
791                                inl + (blocksize == 1 ? 0 : blocksize), in,
792                                (size_t)inl);
793
794     if (ret) {
795         if (soutl > INT_MAX) {
796             EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_UPDATE_ERROR);
797             return 0;
798         }
799         *outl = soutl;
800     }
801
802     return ret;
803
804     /* TODO(3.0): Remove legacy code below */
805  legacy:
806
807     b = ctx->cipher->block_size;
808
809     if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
810         cmpl = (cmpl + 7) / 8;
811
812     if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
813         if (b == 1 && is_partially_overlapping(out, in, cmpl)) {
814             EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
815             return 0;
816         }
817
818         fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
819         if (fix_len < 0) {
820             *outl = 0;
821             return 0;
822         } else
823             *outl = fix_len;
824         return 1;
825     }
826
827     if (inl <= 0) {
828         *outl = 0;
829         return inl == 0;
830     }
831
832     if (ctx->flags & EVP_CIPH_NO_PADDING)
833         return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
834
835     OPENSSL_assert(b <= sizeof(ctx->final));
836
837     if (ctx->final_used) {
838         /* see comment about PTRDIFF_T comparison above */
839         if (((PTRDIFF_T)out == (PTRDIFF_T)in)
840             || is_partially_overlapping(out, in, b)) {
841             EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
842             return 0;
843         }
844         memcpy(out, ctx->final, b);
845         out += b;
846         fix_len = 1;
847     } else
848         fix_len = 0;
849
850     if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
851         return 0;
852
853     /*
854      * if we have 'decrypted' a multiple of block size, make sure we have a
855      * copy of this last block
856      */
857     if (b > 1 && !ctx->buf_len) {
858         *outl -= b;
859         ctx->final_used = 1;
860         memcpy(ctx->final, &out[*outl], b);
861     } else
862         ctx->final_used = 0;
863
864     if (fix_len)
865         *outl += b;
866
867     return 1;
868 }
869
870 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
871 {
872     int ret;
873     ret = EVP_DecryptFinal_ex(ctx, out, outl);
874     return ret;
875 }
876
877 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
878 {
879     int i, n;
880     unsigned int b;
881     size_t soutl;
882     int ret;
883     int blocksize;
884
885     /* Prevent accidental use of encryption context when decrypting */
886     if (ctx->encrypt) {
887         EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
888         return 0;
889     }
890
891     if (ctx->cipher == NULL) {
892         EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_NO_CIPHER_SET);
893         return 0;
894     }
895
896     if (ctx->cipher->prov == NULL)
897         goto legacy;
898
899     blocksize = EVP_CIPHER_CTX_block_size(ctx);
900
901     if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
902         EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_FINAL_ERROR);
903         return 0;
904     }
905
906     ret = ctx->cipher->cfinal(ctx->provctx, out, &soutl,
907                               blocksize == 1 ? 0 : blocksize);
908
909     if (ret) {
910         if (soutl > INT_MAX) {
911             EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_FINAL_ERROR);
912             return 0;
913         }
914         *outl = soutl;
915     }
916
917     return ret;
918
919     /* TODO(3.0): Remove legacy code below */
920  legacy:
921
922     *outl = 0;
923     if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
924         i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
925         if (i < 0)
926             return 0;
927         else
928             *outl = i;
929         return 1;
930     }
931
932     b = ctx->cipher->block_size;
933     if (ctx->flags & EVP_CIPH_NO_PADDING) {
934         if (ctx->buf_len) {
935             EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
936                    EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
937             return 0;
938         }
939         *outl = 0;
940         return 1;
941     }
942     if (b > 1) {
943         if (ctx->buf_len || !ctx->final_used) {
944             EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
945             return 0;
946         }
947         OPENSSL_assert(b <= sizeof(ctx->final));
948
949         /*
950          * The following assumes that the ciphertext has been authenticated.
951          * Otherwise it provides a padding oracle.
952          */
953         n = ctx->final[b - 1];
954         if (n == 0 || n > (int)b) {
955             EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
956             return 0;
957         }
958         for (i = 0; i < n; i++) {
959             if (ctx->final[--b] != n) {
960                 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
961                 return 0;
962             }
963         }
964         n = ctx->cipher->block_size - n;
965         for (i = 0; i < n; i++)
966             out[i] = ctx->final[i];
967         *outl = n;
968     } else
969         *outl = 0;
970     return 1;
971 }
972
973 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
974 {
975     int ok;
976     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
977
978     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
979     ok = evp_do_ciph_ctx_setparams(c->cipher, c->provctx, params);
980
981     if (ok != EVP_CTRL_RET_UNSUPPORTED)
982         return ok;
983
984     /* TODO(3.0) legacy code follows */
985     if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
986         return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
987     if (EVP_CIPHER_CTX_key_length(c) == keylen)
988         return 1;
989     if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
990         c->key_len = keylen;
991         return 1;
992     }
993     EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
994     return 0;
995 }
996
997 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
998 {
999     int ok;
1000     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1001
1002     if (pad)
1003         ctx->flags &= ~EVP_CIPH_NO_PADDING;
1004     else
1005         ctx->flags |= EVP_CIPH_NO_PADDING;
1006
1007     params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_PADDING, &pad);
1008     ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1009
1010     return ok != 0;
1011 }
1012
1013 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1014 {
1015     int ret = EVP_CTRL_RET_UNSUPPORTED;
1016     int set_params = 1;
1017     size_t sz;
1018     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1019
1020     if (ctx == NULL || ctx->cipher == NULL) {
1021         EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
1022         return 0;
1023     }
1024
1025     if (ctx->cipher->prov == NULL)
1026         goto legacy;
1027
1028     switch (type) {
1029     case EVP_CTRL_SET_KEY_LENGTH:
1030         params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_KEYLEN, &arg);
1031         break;
1032     case EVP_CTRL_RAND_KEY:      /* Used by DES */
1033     case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1034     case EVP_CTRL_INIT: /* TODO(3.0) Purely legacy, no provider counterpart */
1035     default:
1036         return EVP_CTRL_RET_UNSUPPORTED;
1037     case EVP_CTRL_GET_IV:
1038         set_params = 0;
1039         params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV,
1040                                                       ptr, (size_t)arg);
1041         break;
1042     case EVP_CTRL_AEAD_SET_IVLEN:
1043         if (arg < 0)
1044             return 0;
1045         sz = (size_t)arg;
1046         params[0] =
1047             OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, &sz);
1048         break;
1049     case EVP_CTRL_GCM_SET_IV_FIXED:
1050         params[0] =
1051             OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED,
1052                                               ptr, (size_t)arg);
1053         break;
1054     case EVP_CTRL_AEAD_SET_TAG:
1055         params[0] =
1056             OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1057                                               ptr, (size_t)arg);
1058         break;
1059     case EVP_CTRL_AEAD_GET_TAG:
1060         set_params = 0;
1061         params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1062                                                       ptr, (size_t)arg);
1063         break;
1064     case EVP_CTRL_AEAD_TLS1_AAD:
1065         /* This one does a set and a get - since it returns a padding size */
1066         params[0] =
1067             OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1068                                               ptr, (size_t)arg);
1069         ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1070         if (ret <= 0)
1071             return ret;
1072         params[0] =
1073             OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1074         ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1075         if (ret <= 0)
1076             return 0;
1077         return sz;
1078     }
1079
1080     if (set_params)
1081         ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1082     else
1083         ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1084     return ret;
1085
1086 /* TODO(3.0): Remove legacy code below */
1087 legacy:
1088     if (ctx->cipher->ctrl == NULL) {
1089         EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
1090         return 0;
1091     }
1092
1093     ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1094     if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1095         EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
1096                EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1097         return 0;
1098     }
1099     return ret;
1100 }
1101
1102 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1103 {
1104     if (cipher != NULL && cipher->get_params != NULL)
1105         return cipher->get_params(params);
1106     return 0;
1107 }
1108
1109 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1110 {
1111     if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL)
1112         return ctx->cipher->set_ctx_params(ctx->provctx, params);
1113     return 0;
1114 }
1115
1116 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1117 {
1118     if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1119         return ctx->cipher->get_ctx_params(ctx->provctx, params);
1120     return 0;
1121 }
1122
1123 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1124 {
1125     if (cipher != NULL && cipher->gettable_params != NULL)
1126         return cipher->gettable_params();
1127     return NULL;
1128 }
1129
1130 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(const EVP_CIPHER *cipher)
1131 {
1132     if (cipher != NULL && cipher->settable_ctx_params != NULL)
1133         return cipher->settable_ctx_params();
1134     return NULL;
1135 }
1136
1137 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(const EVP_CIPHER *cipher)
1138 {
1139     if (cipher != NULL && cipher->gettable_ctx_params != NULL)
1140         return cipher->gettable_ctx_params();
1141     return NULL;
1142 }
1143
1144 #if !defined(FIPS_MODE)
1145 /* TODO(3.0): No support for RAND yet in the FIPS module */
1146 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1147 {
1148     int kl;
1149     if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1150         return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1151     kl = EVP_CIPHER_CTX_key_length(ctx);
1152     if (kl <= 0 || RAND_priv_bytes(key, kl) <= 0)
1153         return 0;
1154     return 1;
1155 }
1156 #endif
1157
1158 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1159 {
1160     if ((in == NULL) || (in->cipher == NULL)) {
1161         EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
1162         return 0;
1163     }
1164
1165     if (in->cipher->prov == NULL)
1166         goto legacy;
1167
1168     if (in->cipher->dupctx == NULL) {
1169         EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_NOT_ABLE_TO_COPY_CTX);
1170         return 0;
1171     }
1172
1173     EVP_CIPHER_CTX_reset(out);
1174
1175     *out = *in;
1176     out->provctx = NULL;
1177
1178     if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1179         out->fetched_cipher = NULL;
1180         return 0;
1181     }
1182
1183     out->provctx = in->cipher->dupctx(in->provctx);
1184     if (out->provctx == NULL) {
1185         EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_NOT_ABLE_TO_COPY_CTX);
1186         return 0;
1187     }
1188
1189     return 1;
1190
1191     /* TODO(3.0): Remove legacy code below */
1192  legacy:
1193
1194 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
1195     /* Make sure it's safe to copy a cipher context using an ENGINE */
1196     if (in->engine && !ENGINE_init(in->engine)) {
1197         EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
1198         return 0;
1199     }
1200 #endif
1201
1202     EVP_CIPHER_CTX_reset(out);
1203     memcpy(out, in, sizeof(*out));
1204
1205     if (in->cipher_data && in->cipher->ctx_size) {
1206         out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1207         if (out->cipher_data == NULL) {
1208             out->cipher = NULL;
1209             EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
1210             return 0;
1211         }
1212         memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1213     }
1214
1215     if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1216         if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1217             out->cipher = NULL;
1218             EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);
1219             return 0;
1220         }
1221     return 1;
1222 }
1223
1224 static void *evp_cipher_from_dispatch(const char *name,
1225                                       const OSSL_DISPATCH *fns,
1226                                       OSSL_PROVIDER *prov)
1227 {
1228     EVP_CIPHER *cipher = NULL;
1229     int fnciphcnt = 0, fnctxcnt = 0;
1230
1231     /*
1232      * The legacy NID is set by EVP_CIPHER_fetch() if the name exists in
1233      * the object database.
1234      */
1235     if ((cipher = EVP_CIPHER_meth_new(0, 0, 0)) == NULL
1236         || (cipher->name = OPENSSL_strdup(name)) == NULL) {
1237         EVP_CIPHER_meth_free(cipher);
1238         EVPerr(0, ERR_R_MALLOC_FAILURE);
1239         return NULL;
1240     }
1241
1242     for (; fns->function_id != 0; fns++) {
1243         switch (fns->function_id) {
1244         case OSSL_FUNC_CIPHER_NEWCTX:
1245             if (cipher->newctx != NULL)
1246                 break;
1247             cipher->newctx = OSSL_get_OP_cipher_newctx(fns);
1248             fnctxcnt++;
1249             break;
1250         case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1251             if (cipher->einit != NULL)
1252                 break;
1253             cipher->einit = OSSL_get_OP_cipher_encrypt_init(fns);
1254             fnciphcnt++;
1255             break;
1256         case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1257             if (cipher->dinit != NULL)
1258                 break;
1259             cipher->dinit = OSSL_get_OP_cipher_decrypt_init(fns);
1260             fnciphcnt++;
1261             break;
1262         case OSSL_FUNC_CIPHER_UPDATE:
1263             if (cipher->cupdate != NULL)
1264                 break;
1265             cipher->cupdate = OSSL_get_OP_cipher_update(fns);
1266             fnciphcnt++;
1267             break;
1268         case OSSL_FUNC_CIPHER_FINAL:
1269             if (cipher->cfinal != NULL)
1270                 break;
1271             cipher->cfinal = OSSL_get_OP_cipher_final(fns);
1272             fnciphcnt++;
1273             break;
1274         case OSSL_FUNC_CIPHER_CIPHER:
1275             if (cipher->ccipher != NULL)
1276                 break;
1277             cipher->ccipher = OSSL_get_OP_cipher_cipher(fns);
1278             break;
1279         case OSSL_FUNC_CIPHER_FREECTX:
1280             if (cipher->freectx != NULL)
1281                 break;
1282             cipher->freectx = OSSL_get_OP_cipher_freectx(fns);
1283             fnctxcnt++;
1284             break;
1285         case OSSL_FUNC_CIPHER_DUPCTX:
1286             if (cipher->dupctx != NULL)
1287                 break;
1288             cipher->dupctx = OSSL_get_OP_cipher_dupctx(fns);
1289             break;
1290         case OSSL_FUNC_CIPHER_GET_PARAMS:
1291             if (cipher->get_params != NULL)
1292                 break;
1293             cipher->get_params = OSSL_get_OP_cipher_get_params(fns);
1294             break;
1295         case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1296             if (cipher->get_ctx_params != NULL)
1297                 break;
1298             cipher->get_ctx_params = OSSL_get_OP_cipher_get_ctx_params(fns);
1299             break;
1300         case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1301             if (cipher->set_ctx_params != NULL)
1302                 break;
1303             cipher->set_ctx_params = OSSL_get_OP_cipher_set_ctx_params(fns);
1304             break;
1305         case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1306             if (cipher->gettable_params != NULL)
1307                 break;
1308             cipher->gettable_params = OSSL_get_OP_cipher_gettable_params(fns);
1309             break;
1310         case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1311             if (cipher->gettable_ctx_params != NULL)
1312                 break;
1313             cipher->gettable_ctx_params =
1314                 OSSL_get_OP_cipher_gettable_ctx_params(fns);
1315             break;
1316         case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1317             if (cipher->settable_ctx_params != NULL)
1318                 break;
1319             cipher->settable_ctx_params =
1320                 OSSL_get_OP_cipher_settable_ctx_params(fns);
1321             break;
1322         }
1323     }
1324     if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1325             || (fnciphcnt == 0 && cipher->ccipher == NULL)
1326             || fnctxcnt != 2) {
1327         /*
1328          * In order to be a consistent set of functions we must have at least
1329          * a complete set of "encrypt" functions, or a complete set of "decrypt"
1330          * functions, or a single "cipher" function. In all cases we need both
1331          * the "newctx" and "freectx" functions.
1332          */
1333         EVP_CIPHER_meth_free(cipher);
1334         EVPerr(EVP_F_EVP_CIPHER_FROM_DISPATCH, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1335         return NULL;
1336     }
1337     cipher->prov = prov;
1338     if (prov != NULL)
1339         ossl_provider_up_ref(prov);
1340
1341     return cipher;
1342 }
1343
1344 static int evp_cipher_up_ref(void *cipher)
1345 {
1346     return EVP_CIPHER_up_ref(cipher);
1347 }
1348
1349 static void evp_cipher_free(void *cipher)
1350 {
1351     EVP_CIPHER_meth_free(cipher);
1352 }
1353
1354 EVP_CIPHER *EVP_CIPHER_fetch(OPENSSL_CTX *ctx, const char *algorithm,
1355                              const char *properties)
1356 {
1357     EVP_CIPHER *cipher =
1358         evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1359                           evp_cipher_from_dispatch, evp_cipher_up_ref,
1360                           evp_cipher_free);
1361
1362 #ifndef FIPS_MODE
1363     /* TODO(3.x) get rid of the need for legacy NIDs */
1364     if (cipher != NULL) {
1365         /*
1366          * FIPS module note: since internal fetches will be entirely
1367          * provider based, we know that none of its code depends on legacy
1368          * NIDs or any functionality that use them.
1369          */
1370         cipher->nid = OBJ_sn2nid(algorithm);
1371     }
1372 #endif
1373
1374     return cipher;
1375 }
1376
1377 void EVP_CIPHER_do_all_ex(OPENSSL_CTX *libctx,
1378                           void (*fn)(EVP_CIPHER *mac, void *arg),
1379                           void *arg)
1380 {
1381     evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1382                        (void (*)(void *, void *))fn, arg,
1383                        evp_cipher_from_dispatch, evp_cipher_free);
1384 }