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