]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_enc.c
Rename the field 'provctx and data' to 'algctx' inside some objects containing
[thirdparty/openssl.git] / crypto / evp / evp_enc.c
CommitLineData
62867571 1/*
a28d06f3 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
d02b48c6
RE
8 */
9
e4468e6d
P
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
d02b48c6 13#include <stdio.h>
c9fb704c 14#include <limits.h>
c3a73daf 15#include <assert.h>
ec577822 16#include <openssl/evp.h>
7f060601 17#include <openssl/err.h>
3a87a9b9 18#include <openssl/rand.h>
3c27208f 19#include <openssl/engine.h>
df05f2ce
MC
20#include <openssl/params.h>
21#include <openssl/core_names.h>
6c9bc258 22#include "internal/cryptlib.h"
df05f2ce 23#include "internal/provider.h"
6c9bc258
TM
24#include "internal/core.h"
25#include "crypto/evp.h"
706457b7 26#include "evp_local.h"
d02b48c6 27
df05f2ce 28int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
0f113f3e 29{
df05f2ce 30 if (ctx == NULL)
8baf9968 31 return 1;
df05f2ce
MC
32
33 if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
34 goto legacy;
35
7c14d0c1 36 if (ctx->algctx != NULL) {
df05f2ce 37 if (ctx->cipher->freectx != NULL)
7c14d0c1
SL
38 ctx->cipher->freectx(ctx->algctx);
39 ctx->algctx = NULL;
df05f2ce
MC
40 }
41 if (ctx->fetched_cipher != NULL)
550f974a 42 EVP_CIPHER_free(ctx->fetched_cipher);
df05f2ce
MC
43 memset(ctx, 0, sizeof(*ctx));
44
45 return 1;
46
0be6cf0c 47 /* Remove legacy code below when legacy support is removed. */
df05f2ce
MC
48 legacy:
49
50 if (ctx->cipher != NULL) {
51 if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
8baf9968
RL
52 return 0;
53 /* Cleanse cipher context data */
df05f2ce
MC
54 if (ctx->cipher_data && ctx->cipher->ctx_size)
55 OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
8baf9968 56 }
df05f2ce 57 OPENSSL_free(ctx->cipher_data);
f844f9eb 58#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
df05f2ce 59 ENGINE_finish(ctx->engine);
8baf9968 60#endif
df05f2ce 61 memset(ctx, 0, sizeof(*ctx));
8baf9968 62 return 1;
0f113f3e 63}
d02b48c6 64
b40228a6 65EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
0f113f3e 66{
8baf9968
RL
67 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
68}
69
70void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
71{
543e740b
RS
72 if (ctx == NULL)
73 return;
8baf9968
RL
74 EVP_CIPHER_CTX_reset(ctx);
75 OPENSSL_free(ctx);
0f113f3e 76}
581f1c84 77
4b58d9b4
P
78static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
79 const EVP_CIPHER *cipher,
80 ENGINE *impl, const unsigned char *key,
81 const unsigned char *iv, int enc,
82 const OSSL_PARAM params[])
0f113f3e 83{
c12bf350 84 int n;
f844f9eb 85#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
df05f2ce 86 ENGINE *tmpimpl = NULL;
319e518a 87#endif
df05f2ce
MC
88 /*
89 * enc == 1 means we are encrypting.
90 * enc == 0 means we are decrypting.
91 * enc == -1 means, use the previously initialised value for encrypt/decrypt
92 */
93 if (enc == -1) {
0f113f3e 94 enc = ctx->encrypt;
df05f2ce 95 } else {
0f113f3e
MC
96 if (enc)
97 enc = 1;
98 ctx->encrypt = enc;
99 }
df05f2ce
MC
100
101 if (cipher == NULL && ctx->cipher == NULL) {
9311d0c4 102 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
df05f2ce
MC
103 return 0;
104 }
105
0be6cf0c 106 /* Code below to be removed when legacy support is dropped. */
df05f2ce 107
f844f9eb 108#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e
MC
109 /*
110 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
111 * this context may already have an ENGINE! Try to avoid releasing the
112 * previous handle, re-querying for an ENGINE, and having a
0d4fb843 113 * reinitialisation, when it may all be unnecessary.
0f113f3e 114 */
f6b94279 115 if (ctx->engine && ctx->cipher
a7f9e0a4 116 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
0f113f3e 117 goto skip_to_init;
df05f2ce
MC
118
119 if (cipher != NULL && impl == NULL) {
120 /* Ask if an ENGINE is reserved for this job */
121 tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
122 }
0b13e9f0 123#endif
df05f2ce
MC
124
125 /*
126 * If there are engines involved then we should use legacy handling for now.
127 */
128 if (ctx->engine != NULL
f844f9eb 129#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
319e518a
MC
130 || tmpimpl != NULL
131#endif
132 || impl != NULL) {
df05f2ce
MC
133 if (ctx->cipher == ctx->fetched_cipher)
134 ctx->cipher = NULL;
550f974a 135 EVP_CIPHER_free(ctx->fetched_cipher);
df05f2ce
MC
136 ctx->fetched_cipher = NULL;
137 goto legacy;
138 }
df05f2ce
MC
139 /*
140 * Ensure a context left lying around from last time is cleared
141 * (legacy code)
142 */
143 if (cipher != NULL && ctx->cipher != NULL) {
144 OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
145 ctx->cipher_data = NULL;
146 }
147
148
0be6cf0c 149 /* Start of non-legacy code below */
df05f2ce
MC
150
151 /* Ensure a context left lying around from last time is cleared */
152 if (cipher != NULL && ctx->cipher != NULL) {
153 unsigned long flags = ctx->flags;
154
155 EVP_CIPHER_CTX_reset(ctx);
156 /* Restore encrypt and flags */
157 ctx->encrypt = enc;
158 ctx->flags = flags;
159 }
160
7f612b1f 161 if (cipher == NULL)
df05f2ce
MC
162 cipher = ctx->cipher;
163
164 if (cipher->prov == NULL) {
f844f9eb 165#ifdef FIPS_MODULE
79c44b4e 166 /* We only do explicit fetches inside the FIPS module */
9311d0c4 167 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
319e518a
MC
168 return 0;
169#else
170 EVP_CIPHER *provciph =
068489a2
MC
171 EVP_CIPHER_fetch(NULL,
172 cipher->nid == NID_undef ? "NULL"
173 : OBJ_nid2sn(cipher->nid),
174 "");
319e518a 175
ec0ce188 176 if (provciph == NULL)
df05f2ce 177 return 0;
df05f2ce 178 cipher = provciph;
550f974a 179 EVP_CIPHER_free(ctx->fetched_cipher);
df05f2ce 180 ctx->fetched_cipher = provciph;
319e518a 181#endif
df05f2ce
MC
182 }
183
aea01d13
P
184 if (cipher->prov != NULL) {
185 if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
186 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
187 return 0;
188 }
189 EVP_CIPHER_free(ctx->fetched_cipher);
190 ctx->fetched_cipher = (EVP_CIPHER *)cipher;
191 }
df05f2ce 192 ctx->cipher = cipher;
7c14d0c1
SL
193 if (ctx->algctx == NULL) {
194 ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
195 if (ctx->algctx == NULL) {
9311d0c4 196 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
df05f2ce
MC
197 return 0;
198 }
199 }
200
201 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
0f113f3e 202 /*
df05f2ce
MC
203 * If this ctx was already set up for no padding then we need to tell
204 * the new cipher about it.
205 */
206 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
207 return 0;
208 }
209
210 if (enc) {
211 if (ctx->cipher->einit == NULL) {
9311d0c4 212 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
df05f2ce
MC
213 return 0;
214 }
215
7c14d0c1 216 return ctx->cipher->einit(ctx->algctx,
344cfa34 217 key,
33b40a10
MC
218 key == NULL ? 0
219 : EVP_CIPHER_CTX_key_length(ctx),
344cfa34 220 iv,
33b40a10 221 iv == NULL ? 0
4b58d9b4
P
222 : EVP_CIPHER_CTX_iv_length(ctx),
223 params);
df05f2ce
MC
224 }
225
226 if (ctx->cipher->dinit == NULL) {
9311d0c4 227 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
df05f2ce
MC
228 return 0;
229 }
230
7c14d0c1 231 return ctx->cipher->dinit(ctx->algctx,
344cfa34 232 key,
33b40a10
MC
233 key == NULL ? 0
234 : EVP_CIPHER_CTX_key_length(ctx),
344cfa34 235 iv,
33b40a10 236 iv == NULL ? 0
4b58d9b4
P
237 : EVP_CIPHER_CTX_iv_length(ctx),
238 params);
df05f2ce 239
0be6cf0c 240 /* Code below to be removed when legacy support is dropped. */
df05f2ce
MC
241 legacy:
242
243 if (cipher != NULL) {
244 /*
245 * Ensure a context left lying around from last time is cleared (we
246 * previously attempted to avoid this if the same ENGINE and
0f113f3e
MC
247 * EVP_CIPHER could be used).
248 */
249 if (ctx->cipher) {
250 unsigned long flags = ctx->flags;
c0ca39bd 251 EVP_CIPHER_CTX_reset(ctx);
0f113f3e
MC
252 /* Restore encrypt and flags */
253 ctx->encrypt = enc;
254 ctx->flags = flags;
255 }
f844f9eb 256#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
df05f2ce 257 if (impl != NULL) {
0f113f3e 258 if (!ENGINE_init(impl)) {
9311d0c4 259 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
0f113f3e
MC
260 return 0;
261 }
df05f2ce
MC
262 } else {
263 impl = tmpimpl;
264 }
265 if (impl != NULL) {
0f113f3e
MC
266 /* There's an ENGINE for this job ... (apparently) */
267 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
df05f2ce
MC
268
269 if (c == NULL) {
0f113f3e
MC
270 /*
271 * One positive side-effect of US's export control history,
272 * is that we should at least be able to avoid using US
0d4fb843 273 * misspellings of "initialisation"?
0f113f3e 274 */
9311d0c4 275 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
0f113f3e
MC
276 return 0;
277 }
278 /* We'll use the ENGINE's private cipher definition */
279 cipher = c;
280 /*
281 * Store the ENGINE functional reference so we know 'cipher' came
282 * from an ENGINE and we need to release it when done.
283 */
284 ctx->engine = impl;
df05f2ce 285 } else {
0f113f3e 286 ctx->engine = NULL;
df05f2ce 287 }
0b13e9f0 288#endif
544a2aea 289
0f113f3e
MC
290 ctx->cipher = cipher;
291 if (ctx->cipher->ctx_size) {
b51bce94 292 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
90945fa3 293 if (ctx->cipher_data == NULL) {
273a0218 294 ctx->cipher = NULL;
9311d0c4 295 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
296 return 0;
297 }
298 } else {
299 ctx->cipher_data = NULL;
300 }
301 ctx->key_len = cipher->key_len;
302 /* Preserve wrap enable flag, zero everything else */
303 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
304 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
305 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
273a0218 306 ctx->cipher = NULL;
9311d0c4 307 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
0f113f3e
MC
308 return 0;
309 }
310 }
0f113f3e 311 }
f844f9eb 312#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e 313 skip_to_init:
0b13e9f0 314#endif
1702c500
P
315 if (ctx->cipher == NULL)
316 return 0;
317
0f113f3e
MC
318 /* we assume block size is a power of 2 in *cryptUpdate */
319 OPENSSL_assert(ctx->cipher->block_size == 1
320 || ctx->cipher->block_size == 8
321 || ctx->cipher->block_size == 16);
322
323 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
324 && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
9311d0c4 325 ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
0f113f3e
MC
326 return 0;
327 }
328
f6c95e46
RS
329 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
330 & EVP_CIPH_CUSTOM_IV) == 0) {
0f113f3e
MC
331 switch (EVP_CIPHER_CTX_mode(ctx)) {
332
333 case EVP_CIPH_STREAM_CIPHER:
334 case EVP_CIPH_ECB_MODE:
335 break;
336
337 case EVP_CIPH_CFB_MODE:
338 case EVP_CIPH_OFB_MODE:
339
340 ctx->num = 0;
341 /* fall-through */
342
343 case EVP_CIPH_CBC_MODE:
c12bf350
P
344 n = EVP_CIPHER_CTX_iv_length(ctx);
345 if (!ossl_assert(n >= 0 && n <= (int)sizeof(ctx->iv)))
346 return 0;
0f113f3e
MC
347 if (iv)
348 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
349 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
350 break;
351
352 case EVP_CIPH_CTR_MODE:
353 ctx->num = 0;
354 /* Don't reuse IV for CTR mode */
355 if (iv)
356 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
357 break;
358
359 default:
360 return 0;
0f113f3e
MC
361 }
362 }
363
364 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
365 if (!ctx->cipher->init(ctx, key, iv, enc))
366 return 0;
367 }
368 ctx->buf_len = 0;
369 ctx->final_used = 0;
370 ctx->block_mask = ctx->cipher->block_size - 1;
371 return 1;
372}
d02b48c6 373
4b58d9b4
P
374int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
375 const unsigned char *key, const unsigned char *iv,
376 int enc, const OSSL_PARAM params[])
377{
378 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
379}
380
381int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
382 const unsigned char *key, const unsigned char *iv, int enc)
383{
384 if (cipher != NULL)
385 EVP_CIPHER_CTX_reset(ctx);
386 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
387}
388
389int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
390 ENGINE *impl, const unsigned char *key,
391 const unsigned char *iv, int enc)
392{
393 return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
394}
395
be06a934 396int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
0f113f3e
MC
397 const unsigned char *in, int inl)
398{
399 if (ctx->encrypt)
400 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
401 else
402 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
403}
d02b48c6 404
581f1c84 405int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
0f113f3e
MC
406{
407 if (ctx->encrypt)
408 return EVP_EncryptFinal_ex(ctx, out, outl);
409 else
410 return EVP_DecryptFinal_ex(ctx, out, outl);
411}
581f1c84 412
6b691a5c 413int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
0f113f3e
MC
414{
415 if (ctx->encrypt)
416 return EVP_EncryptFinal(ctx, out, outl);
417 else
418 return EVP_DecryptFinal(ctx, out, outl);
419}
d02b48c6 420
be06a934 421int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
0f113f3e
MC
422 const unsigned char *key, const unsigned char *iv)
423{
424 return EVP_CipherInit(ctx, cipher, key, iv, 1);
425}
18eda732 426
0f113f3e
MC
427int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
428 ENGINE *impl, const unsigned char *key,
429 const unsigned char *iv)
430{
431 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
432}
d02b48c6 433
4b58d9b4
P
434int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
435 const unsigned char *key, const unsigned char *iv,
436 const OSSL_PARAM params[])
437{
438 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
439}
440
be06a934 441int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
0f113f3e
MC
442 const unsigned char *key, const unsigned char *iv)
443{
444 return EVP_CipherInit(ctx, cipher, key, iv, 0);
445}
18eda732 446
0f113f3e
MC
447int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
448 ENGINE *impl, const unsigned char *key,
449 const unsigned char *iv)
450{
451 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
452}
d02b48c6 453
4b58d9b4
P
454int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
455 const unsigned char *key, const unsigned char *iv,
456 const OSSL_PARAM params[])
457{
458 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
459}
460
c3a73daf
AP
461/*
462 * According to the letter of standard difference between pointers
463 * is specified to be valid only within same object. This makes
464 * it formally challenging to determine if input and output buffers
465 * are not partially overlapping with standard pointer arithmetic.
466 */
467#ifdef PTRDIFF_T
468# undef PTRDIFF_T
469#endif
470#if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
471/*
472 * Then we have VMS that distinguishes itself by adhering to
5fc77684
AP
473 * sizeof(size_t)==4 even in 64-bit builds, which means that
474 * difference between two pointers might be truncated to 32 bits.
475 * In the context one can even wonder how comparison for
476 * equality is implemented. To be on the safe side we adhere to
477 * PTRDIFF_T even for comparison for equality.
c3a73daf
AP
478 */
479# define PTRDIFF_T uint64_t
480#else
481# define PTRDIFF_T size_t
482#endif
483
6d777689 484int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
c3a73daf
AP
485{
486 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
487 /*
488 * Check for partially overlapping buffers. [Binary logical
489 * operations are used instead of boolean to minimize number
490 * of conditional branches.]
491 */
83151b73
AP
492 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
493 (diff > (0 - (PTRDIFF_T)len)));
b153f092 494
83151b73 495 return overlapped;
c3a73daf
AP
496}
497
a8bf2f8f
RL
498static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
499 unsigned char *out, int *outl,
500 const unsigned char *in, int inl)
0f113f3e 501{
64846096
LP
502 int i, j, bl, cmpl = inl;
503
504 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
505 cmpl = (cmpl + 7) / 8;
0f113f3e 506
7141ba31
MC
507 bl = ctx->cipher->block_size;
508
0f113f3e 509 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
7141ba31 510 /* If block size > 1 then the cipher will have to do this check */
6d777689 511 if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
9311d0c4 512 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
5fc77684 513 return 0;
83151b73 514 }
5fc77684 515
0f113f3e
MC
516 i = ctx->cipher->do_cipher(ctx, out, in, inl);
517 if (i < 0)
518 return 0;
519 else
520 *outl = i;
521 return 1;
522 }
523
2c236894
MC
524 if (inl <= 0) {
525 *outl = 0;
526 return inl == 0;
527 }
6d777689 528 if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
9311d0c4 529 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
5fc77684 530 return 0;
83151b73 531 }
0f113f3e
MC
532
533 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
534 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
535 *outl = inl;
536 return 1;
537 } else {
538 *outl = 0;
539 return 0;
540 }
541 }
542 i = ctx->buf_len;
0f113f3e
MC
543 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
544 if (i != 0) {
3f358213 545 if (bl - i > inl) {
0f113f3e
MC
546 memcpy(&(ctx->buf[i]), in, inl);
547 ctx->buf_len += inl;
548 *outl = 0;
549 return 1;
550 } else {
551 j = bl - i;
c9fb704c
MC
552
553 /*
554 * Once we've processed the first j bytes from in, the amount of
555 * data left that is a multiple of the block length is:
556 * (inl - j) & ~(bl - 1)
557 * We must ensure that this amount of data, plus the one block that
558 * we process from ctx->buf does not exceed INT_MAX
559 */
560 if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
561 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
562 return 0;
563 }
0f113f3e 564 memcpy(&(ctx->buf[i]), in, j);
0f113f3e
MC
565 inl -= j;
566 in += j;
5fc77684
AP
567 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
568 return 0;
0f113f3e
MC
569 out += bl;
570 *outl = bl;
571 }
572 } else
573 *outl = 0;
574 i = inl & (bl - 1);
575 inl -= i;
576 if (inl > 0) {
577 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
578 return 0;
579 *outl += inl;
580 }
581
582 if (i != 0)
583 memcpy(ctx->buf, &(in[inl]), i);
584 ctx->buf_len = i;
585 return 1;
586}
d02b48c6 587
a8bf2f8f
RL
588
589int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
590 const unsigned char *in, int inl)
591{
df05f2ce
MC
592 int ret;
593 size_t soutl;
3b94944c 594 int blocksize;
df05f2ce 595
3d4c81b0 596 if (outl != NULL) {
597 *outl = 0;
598 } else {
9311d0c4 599 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
3d4c81b0 600 return 0;
601 }
602
a8bf2f8f
RL
603 /* Prevent accidental use of decryption context when encrypting */
604 if (!ctx->encrypt) {
9311d0c4 605 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
a8bf2f8f
RL
606 return 0;
607 }
608
d4d89a07 609 if (ctx->cipher == NULL) {
9311d0c4 610 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
d4d89a07
SS
611 return 0;
612 }
613
614 if (ctx->cipher->prov == NULL)
df05f2ce
MC
615 goto legacy;
616
30af356d 617 blocksize = ctx->cipher->block_size;
3b94944c
MC
618
619 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
9311d0c4 620 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
df05f2ce
MC
621 return 0;
622 }
7c14d0c1 623 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
3b94944c
MC
624 inl + (blocksize == 1 ? 0 : blocksize), in,
625 (size_t)inl);
df05f2ce 626
36e619d7
GV
627 if (ret) {
628 if (soutl > INT_MAX) {
9311d0c4 629 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
36e619d7
GV
630 return 0;
631 }
632 *outl = soutl;
df05f2ce 633 }
36e619d7 634
df05f2ce
MC
635 return ret;
636
0be6cf0c 637 /* Code below to be removed when legacy support is dropped. */
df05f2ce
MC
638 legacy:
639
a8bf2f8f
RL
640 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
641}
642
be06a934 643int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
0f113f3e
MC
644{
645 int ret;
646 ret = EVP_EncryptFinal_ex(ctx, out, outl);
647 return ret;
648}
581f1c84
DSH
649
650int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
0f113f3e
MC
651{
652 int n, ret;
653 unsigned int i, b, bl;
df05f2ce 654 size_t soutl;
3b94944c 655 int blocksize;
0f113f3e 656
3d4c81b0 657 if (outl != NULL) {
658 *outl = 0;
659 } else {
9311d0c4 660 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
3d4c81b0 661 return 0;
662 }
663
a8bf2f8f
RL
664 /* Prevent accidental use of decryption context when encrypting */
665 if (!ctx->encrypt) {
9311d0c4 666 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
a8bf2f8f
RL
667 return 0;
668 }
669
4894dcad 670 if (ctx->cipher == NULL) {
9311d0c4 671 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
4894dcad
P
672 return 0;
673 }
674 if (ctx->cipher->prov == NULL)
df05f2ce
MC
675 goto legacy;
676
3b94944c
MC
677 blocksize = EVP_CIPHER_CTX_block_size(ctx);
678
679 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
9311d0c4 680 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
df05f2ce
MC
681 return 0;
682 }
683
7c14d0c1 684 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
3b94944c 685 blocksize == 1 ? 0 : blocksize);
df05f2ce 686
36e619d7
GV
687 if (ret) {
688 if (soutl > INT_MAX) {
9311d0c4 689 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
36e619d7
GV
690 return 0;
691 }
692 *outl = soutl;
df05f2ce 693 }
df05f2ce
MC
694
695 return ret;
696
0be6cf0c 697 /* Code below to be removed when legacy support is dropped. */
df05f2ce
MC
698 legacy:
699
0f113f3e
MC
700 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
701 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
702 if (ret < 0)
703 return 0;
704 else
705 *outl = ret;
706 return 1;
707 }
708
709 b = ctx->cipher->block_size;
cbe29648 710 OPENSSL_assert(b <= sizeof(ctx->buf));
0f113f3e
MC
711 if (b == 1) {
712 *outl = 0;
713 return 1;
714 }
715 bl = ctx->buf_len;
716 if (ctx->flags & EVP_CIPH_NO_PADDING) {
717 if (bl) {
9311d0c4 718 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
0f113f3e
MC
719 return 0;
720 }
721 *outl = 0;
722 return 1;
723 }
724
725 n = b - bl;
726 for (i = bl; i < b; i++)
727 ctx->buf[i] = n;
728 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
729
730 if (ret)
731 *outl = b;
732
733 return ret;
734}
d02b48c6 735
be06a934 736int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
0f113f3e
MC
737 const unsigned char *in, int inl)
738{
df05f2ce 739 int fix_len, cmpl = inl, ret;
0f113f3e 740 unsigned int b;
df05f2ce 741 size_t soutl;
3b94944c 742 int blocksize;
0f113f3e 743
3d4c81b0 744 if (outl != NULL) {
745 *outl = 0;
746 } else {
9311d0c4 747 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
3d4c81b0 748 return 0;
749 }
750
a8bf2f8f
RL
751 /* Prevent accidental use of encryption context when decrypting */
752 if (ctx->encrypt) {
9311d0c4 753 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
a8bf2f8f
RL
754 return 0;
755 }
756
d2c2e49e 757 if (ctx->cipher == NULL) {
9311d0c4 758 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
d2c2e49e
P
759 return 0;
760 }
761 if (ctx->cipher->prov == NULL)
df05f2ce
MC
762 goto legacy;
763
3b94944c
MC
764 blocksize = EVP_CIPHER_CTX_block_size(ctx);
765
766 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
9311d0c4 767 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
df05f2ce
MC
768 return 0;
769 }
7c14d0c1 770 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
3b94944c
MC
771 inl + (blocksize == 1 ? 0 : blocksize), in,
772 (size_t)inl);
df05f2ce
MC
773
774 if (ret) {
775 if (soutl > INT_MAX) {
9311d0c4 776 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
df05f2ce
MC
777 return 0;
778 }
779 *outl = soutl;
780 }
781
782 return ret;
783
0be6cf0c 784 /* Code below to be removed when legacy support is dropped. */
df05f2ce
MC
785 legacy:
786
7141ba31
MC
787 b = ctx->cipher->block_size;
788
64846096
LP
789 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
790 cmpl = (cmpl + 7) / 8;
791
0f113f3e 792 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
6d777689 793 if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
9311d0c4 794 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
5fc77684 795 return 0;
83151b73 796 }
5fc77684 797
0f113f3e
MC
798 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
799 if (fix_len < 0) {
800 *outl = 0;
801 return 0;
802 } else
803 *outl = fix_len;
804 return 1;
805 }
806
2c236894
MC
807 if (inl <= 0) {
808 *outl = 0;
809 return inl == 0;
810 }
811
0f113f3e 812 if (ctx->flags & EVP_CIPH_NO_PADDING)
a8bf2f8f 813 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
0f113f3e 814
cbe29648 815 OPENSSL_assert(b <= sizeof(ctx->final));
0f113f3e
MC
816
817 if (ctx->final_used) {
5fc77684
AP
818 /* see comment about PTRDIFF_T comparison above */
819 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
6d777689 820 || ossl_is_partially_overlapping(out, in, b)) {
9311d0c4 821 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
5fc77684 822 return 0;
83151b73 823 }
c9fb704c
MC
824 /*
825 * final_used is only ever set if buf_len is 0. Therefore the maximum
826 * length output we will ever see from evp_EncryptDecryptUpdate is
827 * the maximum multiple of the block length that is <= inl, or just:
828 * inl & ~(b - 1)
829 * Since final_used has been set then the final output length is:
830 * (inl & ~(b - 1)) + b
831 * This must never exceed INT_MAX
832 */
833 if ((inl & ~(b - 1)) > INT_MAX - b) {
834 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
835 return 0;
836 }
0f113f3e
MC
837 memcpy(out, ctx->final, b);
838 out += b;
839 fix_len = 1;
840 } else
841 fix_len = 0;
842
a8bf2f8f 843 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
0f113f3e
MC
844 return 0;
845
846 /*
847 * if we have 'decrypted' a multiple of block size, make sure we have a
848 * copy of this last block
849 */
850 if (b > 1 && !ctx->buf_len) {
851 *outl -= b;
852 ctx->final_used = 1;
853 memcpy(ctx->final, &out[*outl], b);
854 } else
855 ctx->final_used = 0;
856
857 if (fix_len)
858 *outl += b;
859
860 return 1;
861}
d02b48c6 862
6b691a5c 863int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
0f113f3e
MC
864{
865 int ret;
866 ret = EVP_DecryptFinal_ex(ctx, out, outl);
867 return ret;
868}
581f1c84
DSH
869
870int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
0f113f3e
MC
871{
872 int i, n;
873 unsigned int b;
df05f2ce
MC
874 size_t soutl;
875 int ret;
3b94944c 876 int blocksize;
a8bf2f8f 877
3d4c81b0 878 if (outl != NULL) {
879 *outl = 0;
880 } else {
9311d0c4 881 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
3d4c81b0 882 return 0;
883 }
884
a8bf2f8f
RL
885 /* Prevent accidental use of encryption context when decrypting */
886 if (ctx->encrypt) {
9311d0c4 887 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
a8bf2f8f
RL
888 return 0;
889 }
890
d4d89a07 891 if (ctx->cipher == NULL) {
9311d0c4 892 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
d4d89a07
SS
893 return 0;
894 }
895
896 if (ctx->cipher->prov == NULL)
df05f2ce
MC
897 goto legacy;
898
3b94944c
MC
899 blocksize = EVP_CIPHER_CTX_block_size(ctx);
900
901 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
9311d0c4 902 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
df05f2ce
MC
903 return 0;
904 }
905
7c14d0c1 906 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
3b94944c 907 blocksize == 1 ? 0 : blocksize);
df05f2ce
MC
908
909 if (ret) {
910 if (soutl > INT_MAX) {
9311d0c4 911 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
df05f2ce
MC
912 return 0;
913 }
914 *outl = soutl;
915 }
916
917 return ret;
918
0be6cf0c 919 /* Code below to be removed when legacy support is dropped. */
df05f2ce
MC
920 legacy:
921
0f113f3e 922 *outl = 0;
0f113f3e
MC
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) {
9311d0c4 935 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
0f113f3e
MC
936 return 0;
937 }
938 *outl = 0;
939 return 1;
940 }
941 if (b > 1) {
942 if (ctx->buf_len || !ctx->final_used) {
9311d0c4 943 ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
26a7d938 944 return 0;
0f113f3e 945 }
cbe29648 946 OPENSSL_assert(b <= sizeof(ctx->final));
0f113f3e
MC
947
948 /*
949 * The following assumes that the ciphertext has been authenticated.
950 * Otherwise it provides a padding oracle.
951 */
952 n = ctx->final[b - 1];
953 if (n == 0 || n > (int)b) {
9311d0c4 954 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
26a7d938 955 return 0;
0f113f3e
MC
956 }
957 for (i = 0; i < n; i++) {
958 if (ctx->final[--b] != n) {
9311d0c4 959 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
26a7d938 960 return 0;
0f113f3e
MC
961 }
962 }
963 n = ctx->cipher->block_size - n;
964 for (i = 0; i < n; i++)
965 out[i] = ctx->final[i];
966 *outl = n;
967 } else
968 *outl = 0;
208fb891 969 return 1;
0f113f3e 970}
d02b48c6 971
6343829a 972int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
0f113f3e 973{
d23adad1
MC
974 if (c->cipher->prov != NULL) {
975 int ok;
976 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
977 size_t len = keylen;
459b15d4 978
d23adad1
MC
979 if (EVP_CIPHER_CTX_key_length(c) == keylen)
980 return 1;
981
982 /* Check the cipher actually understands this parameter */
983 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
984 OSSL_CIPHER_PARAM_KEYLEN) == NULL)
985 return 0;
986
987 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
7c14d0c1 988 ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
13273237 989
d23adad1
MC
990 return ok > 0 ? 1 : 0;
991 }
13273237 992
0be6cf0c 993 /* Code below to be removed when legacy support is dropped. */
d23adad1
MC
994
995 /*
996 * Note there have never been any built-in ciphers that define this flag
997 * since it was first introduced.
998 */
0f113f3e
MC
999 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1000 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
df05f2ce 1001 if (EVP_CIPHER_CTX_key_length(c) == keylen)
0f113f3e
MC
1002 return 1;
1003 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1004 c->key_len = keylen;
1005 return 1;
1006 }
9311d0c4 1007 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
0f113f3e
MC
1008 return 0;
1009}
49528751 1010
f2e5ca84 1011int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
0f113f3e 1012{
13273237 1013 int ok;
459b15d4 1014 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1c3ace68 1015 unsigned int pd = pad;
13273237 1016
0f113f3e
MC
1017 if (pad)
1018 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1019 else
1020 ctx->flags |= EVP_CIPH_NO_PADDING;
df05f2ce 1021
719bc0e8
SL
1022 if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1023 return 1;
1c3ace68 1024 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
7c14d0c1 1025 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
459b15d4 1026
13273237 1027 return ok != 0;
0f113f3e 1028}
f2e5ca84 1029
49528751
DSH
1030int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1031{
e870791a 1032 int ret = EVP_CTRL_RET_UNSUPPORTED;
459b15d4 1033 int set_params = 1;
1c3ace68 1034 size_t sz = arg;
6a41156c 1035 unsigned int i;
0d2bfe52
SL
1036 OSSL_PARAM params[4] = {
1037 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1038 };
d91f4568 1039
459b15d4 1040 if (ctx == NULL || ctx->cipher == NULL) {
9311d0c4 1041 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
0f113f3e
MC
1042 return 0;
1043 }
1044
13273237
RL
1045 if (ctx->cipher->prov == NULL)
1046 goto legacy;
1047
1048 switch (type) {
1049 case EVP_CTRL_SET_KEY_LENGTH:
1c3ace68 1050 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
13273237
RL
1051 break;
1052 case EVP_CTRL_RAND_KEY: /* Used by DES */
4a42e264
SL
1053 set_params = 0;
1054 params[0] =
1055 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1c3ace68 1056 ptr, sz);
4a42e264
SL
1057 break;
1058
d6d74cf4
RL
1059 case EVP_CTRL_INIT:
1060 /*
0be6cf0c 1061 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
d6d74cf4
RL
1062 * As a matter of fact, this should be dead code, but some caller
1063 * might still do a direct control call with this command, so...
1064 * Legacy methods return 1 except for exceptional circumstances, so
1065 * we do the same here to not be disruptive.
1066 */
1067 return 1;
13273237 1068 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
459b15d4 1069 default:
6a36f209 1070 goto end;
459b15d4
SL
1071 case EVP_CTRL_AEAD_SET_IVLEN:
1072 if (arg < 0)
1073 return 0;
1c3ace68 1074 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
13273237 1075 break;
11b44359
SL
1076 case EVP_CTRL_AEAD_SET_IV_FIXED:
1077 params[0] = OSSL_PARAM_construct_octet_string(
1078 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1079 break;
1080 case EVP_CTRL_GCM_IV_GEN:
1081 set_params = 0;
1082 if (arg < 0)
1083 sz = 0; /* special case that uses the iv length */
1084 params[0] = OSSL_PARAM_construct_octet_string(
1085 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1086 break;
1087 case EVP_CTRL_GCM_SET_IV_INV:
1088 if (arg < 0)
1089 return 0;
1090 params[0] = OSSL_PARAM_construct_octet_string(
1091 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
459b15d4 1092 break;
6a41156c
SL
1093 case EVP_CTRL_GET_RC5_ROUNDS:
1094 set_params = 0; /* Fall thru */
1095 case EVP_CTRL_SET_RC5_ROUNDS:
1096 if (arg < 0)
1097 return 0;
1098 i = (unsigned int)arg;
1099 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1100 break;
eb173822
SL
1101 case EVP_CTRL_SET_SPEED:
1102 if (arg < 0)
1103 return 0;
1104 i = (unsigned int)arg;
1105 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1106 break;
459b15d4 1107 case EVP_CTRL_AEAD_GET_TAG:
1c3ace68
SL
1108 set_params = 0; /* Fall thru */
1109 case EVP_CTRL_AEAD_SET_TAG:
459b15d4 1110 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1c3ace68 1111 ptr, sz);
459b15d4
SL
1112 break;
1113 case EVP_CTRL_AEAD_TLS1_AAD:
0d2bfe52 1114 /* This one does a set and a get - since it returns a size */
459b15d4
SL
1115 params[0] =
1116 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1c3ace68 1117 ptr, sz);
7c14d0c1 1118 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
459b15d4 1119 if (ret <= 0)
6a36f209 1120 goto end;
459b15d4
SL
1121 params[0] =
1122 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
7c14d0c1 1123 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
459b15d4 1124 if (ret <= 0)
6a36f209 1125 goto end;
459b15d4 1126 return sz;
f816aa47
SL
1127#ifndef OPENSSL_NO_RC2
1128 case EVP_CTRL_GET_RC2_KEY_BITS:
1129 set_params = 0; /* Fall thru */
1130 case EVP_CTRL_SET_RC2_KEY_BITS:
1131 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1132 break;
1133#endif /* OPENSSL_NO_RC2 */
0d2bfe52
SL
1134#if !defined(OPENSSL_NO_MULTIBLOCK)
1135 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1136 params[0] = OSSL_PARAM_construct_size_t(
1137 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
7c14d0c1 1138 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
0d2bfe52
SL
1139 if (ret <= 0)
1140 return 0;
1141
1142 params[0] = OSSL_PARAM_construct_size_t(
1143 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1144 params[1] = OSSL_PARAM_construct_end();
7c14d0c1 1145 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
0d2bfe52
SL
1146 if (ret <= 0)
1147 return 0;
1148 return sz;
1149 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1150 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1151 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1152
1153 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1154 return 0;
1155
1156 params[0] = OSSL_PARAM_construct_octet_string(
1157 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1158 params[1] = OSSL_PARAM_construct_uint(
1159 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
7c14d0c1 1160 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
0d2bfe52
SL
1161 if (ret <= 0)
1162 return ret;
1163 /* Retrieve the return values changed by the set */
1164 params[0] = OSSL_PARAM_construct_size_t(
1165 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1166 params[1] = OSSL_PARAM_construct_uint(
1167 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1168 params[2] = OSSL_PARAM_construct_end();
7c14d0c1 1169 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
0d2bfe52
SL
1170 if (ret <= 0)
1171 return 0;
1172 return sz;
1173 }
1174 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1175 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1176 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1177
1178 params[0] = OSSL_PARAM_construct_octet_string(
1179 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1180
1181 params[1] = OSSL_PARAM_construct_octet_string(
1182 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1183 p->len);
1184 params[2] = OSSL_PARAM_construct_uint(
1185 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
7c14d0c1 1186 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
0d2bfe52
SL
1187 if (ret <= 0)
1188 return ret;
1189 params[0] = OSSL_PARAM_construct_size_t(
1190 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1191 params[1] = OSSL_PARAM_construct_end();
7c14d0c1 1192 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
0d2bfe52
SL
1193 if (ret <= 0)
1194 return 0;
1195 return sz;
1196 }
1197#endif /* OPENSSL_NO_MULTIBLOCK */
1198 case EVP_CTRL_AEAD_SET_MAC_KEY:
1199 if (arg < 0)
1200 return -1;
1201 params[0] = OSSL_PARAM_construct_octet_string(
1202 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1203 break;
13273237 1204 }
459b15d4
SL
1205
1206 if (set_params)
7c14d0c1 1207 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
459b15d4 1208 else
7c14d0c1 1209 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
6a36f209 1210 goto end;
13273237 1211
0be6cf0c 1212 /* Code below to be removed when legacy support is dropped. */
459b15d4
SL
1213legacy:
1214 if (ctx->cipher->ctrl == NULL) {
9311d0c4 1215 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
0f113f3e
MC
1216 return 0;
1217 }
1218
1219 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
552be00d 1220
6a36f209 1221 end:
e870791a 1222 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
9311d0c4 1223 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
0f113f3e
MC
1224 return 0;
1225 }
1226 return ret;
49528751 1227}
216659eb 1228
ae3ff60e
RL
1229int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1230{
1231 if (cipher != NULL && cipher->get_params != NULL)
1232 return cipher->get_params(params);
1233 return 0;
1234}
1235
1236int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1237{
92d9d0ae 1238 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL)
7c14d0c1 1239 return ctx->cipher->set_ctx_params(ctx->algctx, params);
ae3ff60e
RL
1240 return 0;
1241}
1242
1243int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1244{
92d9d0ae 1245 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
7c14d0c1 1246 return ctx->cipher->get_ctx_params(ctx->algctx, params);
ae3ff60e
RL
1247 return 0;
1248}
1249
1250const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1251{
1252 if (cipher != NULL && cipher->gettable_params != NULL)
18ec26ba
P
1253 return cipher->gettable_params(
1254 ossl_provider_ctx(EVP_CIPHER_provider(cipher)));
ae3ff60e
RL
1255 return NULL;
1256}
1257
41f7ecf3 1258const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
ae3ff60e 1259{
7c14d0c1 1260 void *provctx;
292b4184
P
1261
1262 if (cipher != NULL && cipher->settable_ctx_params != NULL) {
7c14d0c1
SL
1263 provctx = ossl_provider_ctx(EVP_CIPHER_provider(cipher));
1264 return cipher->settable_ctx_params(NULL, provctx);
292b4184 1265 }
ae3ff60e
RL
1266 return NULL;
1267}
1268
41f7ecf3 1269const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
ae3ff60e 1270{
7c14d0c1 1271 void *provctx;
292b4184
P
1272
1273 if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
7c14d0c1
SL
1274 provctx = ossl_provider_ctx(EVP_CIPHER_provider(cipher));
1275 return cipher->gettable_ctx_params(NULL, provctx);
292b4184
P
1276 }
1277 return NULL;
1278}
1279
1280const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1281{
1282 void *alg;
1283
1284 if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1285 alg = ossl_provider_ctx(EVP_CIPHER_provider(cctx->cipher));
7c14d0c1 1286 return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
292b4184
P
1287 }
1288 return NULL;
1289}
1290
1291const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1292{
7c14d0c1 1293 void *provctx;
292b4184
P
1294
1295 if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
7c14d0c1
SL
1296 provctx = ossl_provider_ctx(EVP_CIPHER_provider(cctx->cipher));
1297 return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
292b4184 1298 }
ae3ff60e
RL
1299 return NULL;
1300}
1301
11eef7e7 1302#ifndef FIPS_MODULE
b4250010 1303static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
11eef7e7
SL
1304{
1305 const EVP_CIPHER *cipher = ctx->cipher;
1306 const OSSL_PROVIDER *prov;
1307
1308 if (cipher == NULL)
1309 return NULL;
1310
1311 prov = EVP_CIPHER_provider(cipher);
a829b735 1312 return ossl_provider_libctx(prov);
11eef7e7
SL
1313}
1314#endif
1315
216659eb 1316int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
0f113f3e
MC
1317{
1318 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1319 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
4a42e264 1320
f844f9eb 1321#ifdef FIPS_MODULE
4a42e264
SL
1322 return 0;
1323#else
1324 {
1325 int kl;
b4250010 1326 OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
4a42e264
SL
1327
1328 kl = EVP_CIPHER_CTX_key_length(ctx);
11eef7e7 1329 if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl) <= 0)
4a42e264
SL
1330 return 0;
1331 return 1;
1332 }
f844f9eb 1333#endif /* FIPS_MODULE */
0f113f3e 1334}
216659eb 1335
c2bf7208 1336int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
0f113f3e
MC
1337{
1338 if ((in == NULL) || (in->cipher == NULL)) {
9311d0c4 1339 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
0f113f3e
MC
1340 return 0;
1341 }
df05f2ce
MC
1342
1343 if (in->cipher->prov == NULL)
1344 goto legacy;
1345
1346 if (in->cipher->dupctx == NULL) {
9311d0c4 1347 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
df05f2ce
MC
1348 return 0;
1349 }
1350
1351 EVP_CIPHER_CTX_reset(out);
1352
1353 *out = *in;
7c14d0c1 1354 out->algctx = NULL;
df05f2ce 1355
70c35fd1 1356 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
df05f2ce
MC
1357 out->fetched_cipher = NULL;
1358 return 0;
1359 }
1360
7c14d0c1
SL
1361 out->algctx = in->cipher->dupctx(in->algctx);
1362 if (out->algctx == NULL) {
9311d0c4 1363 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
df05f2ce
MC
1364 return 0;
1365 }
1366
1367 return 1;
1368
0be6cf0c 1369 /* Code below to be removed when legacy support is dropped. */
df05f2ce
MC
1370 legacy:
1371
f844f9eb 1372#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e
MC
1373 /* Make sure it's safe to copy a cipher context using an ENGINE */
1374 if (in->engine && !ENGINE_init(in->engine)) {
9311d0c4 1375 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
0f113f3e
MC
1376 return 0;
1377 }
c2bf7208
DSH
1378#endif
1379
c0ca39bd 1380 EVP_CIPHER_CTX_reset(out);
b4faea50 1381 memcpy(out, in, sizeof(*out));
0f113f3e
MC
1382
1383 if (in->cipher_data && in->cipher->ctx_size) {
1384 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
90945fa3 1385 if (out->cipher_data == NULL) {
273a0218 1386 out->cipher = NULL;
9311d0c4 1387 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1388 return 0;
1389 }
1390 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1391 }
1392
1393 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
273a0218
BE
1394 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1395 out->cipher = NULL;
9311d0c4 1396 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
273a0218
BE
1397 return 0;
1398 }
0f113f3e
MC
1399 return 1;
1400}
df05f2ce 1401
550f974a
RL
1402EVP_CIPHER *evp_cipher_new(void)
1403{
1404 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1405
1406 if (cipher != NULL) {
1407 cipher->lock = CRYPTO_THREAD_lock_new();
1408 if (cipher->lock == NULL) {
1409 OPENSSL_free(cipher);
1410 return NULL;
1411 }
1412 cipher->refcnt = 1;
1413 }
1414 return cipher;
1415}
1416
32040838
RL
1417/*
1418 * FIPS module note: since internal fetches will be entirely
1419 * provider based, we know that none of its code depends on legacy
1420 * NIDs or any functionality that use them.
1421 */
f844f9eb 1422#ifndef FIPS_MODULE
83abd33c 1423/* After removal of legacy support get rid of the need for legacy NIDs */
32040838
RL
1424static void set_legacy_nid(const char *name, void *vlegacy_nid)
1425{
1426 int nid;
1427 int *legacy_nid = vlegacy_nid;
6a835fcf
RL
1428 /*
1429 * We use lowest level function to get the associated method, because
1430 * higher level functions such as EVP_get_cipherbyname() have changed
1431 * to look at providers too.
1432 */
1433 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
32040838
RL
1434
1435 if (*legacy_nid == -1) /* We found a clash already */
1436 return;
6a835fcf 1437 if (legacy_method == NULL)
32040838 1438 return;
6a835fcf 1439 nid = EVP_CIPHER_nid(legacy_method);
32040838
RL
1440 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1441 *legacy_nid = -1;
1442 return;
1443 }
1444 *legacy_nid = nid;
1445}
1446#endif
1447
309a78aa
RL
1448static void *evp_cipher_from_algorithm(const int name_id,
1449 const OSSL_ALGORITHM *algodef,
1450 OSSL_PROVIDER *prov)
df05f2ce 1451{
309a78aa 1452 const OSSL_DISPATCH *fns = algodef->implementation;
df05f2ce
MC
1453 EVP_CIPHER *cipher = NULL;
1454 int fnciphcnt = 0, fnctxcnt = 0;
1455
f7c16d48 1456 if ((cipher = evp_cipher_new()) == NULL) {
9311d0c4 1457 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
df05f2ce 1458 return NULL;
6b9e3724 1459 }
df05f2ce 1460
f844f9eb 1461#ifndef FIPS_MODULE
32040838 1462 cipher->nid = NID_undef;
d84f5515
MC
1463 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1464 || cipher->nid == -1) {
32040838
RL
1465 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1466 EVP_CIPHER_free(cipher);
1467 return NULL;
f7c16d48 1468 }
ed71e917
MC
1469#endif
1470
32040838 1471 cipher->name_id = name_id;
6c9bc258
TM
1472 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1473 EVP_CIPHER_free(cipher);
1474 return NULL;
1475 }
309a78aa 1476 cipher->description = algodef->algorithm_description;
32040838 1477
df05f2ce
MC
1478 for (; fns->function_id != 0; fns++) {
1479 switch (fns->function_id) {
1480 case OSSL_FUNC_CIPHER_NEWCTX:
1481 if (cipher->newctx != NULL)
1482 break;
363b1e5d 1483 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
df05f2ce
MC
1484 fnctxcnt++;
1485 break;
1486 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1487 if (cipher->einit != NULL)
1488 break;
363b1e5d 1489 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
df05f2ce
MC
1490 fnciphcnt++;
1491 break;
1492 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1493 if (cipher->dinit != NULL)
1494 break;
363b1e5d 1495 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
df05f2ce
MC
1496 fnciphcnt++;
1497 break;
1498 case OSSL_FUNC_CIPHER_UPDATE:
1499 if (cipher->cupdate != NULL)
1500 break;
363b1e5d 1501 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
df05f2ce
MC
1502 fnciphcnt++;
1503 break;
1504 case OSSL_FUNC_CIPHER_FINAL:
1505 if (cipher->cfinal != NULL)
1506 break;
363b1e5d 1507 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
df05f2ce
MC
1508 fnciphcnt++;
1509 break;
718b133a
MC
1510 case OSSL_FUNC_CIPHER_CIPHER:
1511 if (cipher->ccipher != NULL)
1512 break;
363b1e5d 1513 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
718b133a 1514 break;
df05f2ce
MC
1515 case OSSL_FUNC_CIPHER_FREECTX:
1516 if (cipher->freectx != NULL)
1517 break;
363b1e5d 1518 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
df05f2ce
MC
1519 fnctxcnt++;
1520 break;
1521 case OSSL_FUNC_CIPHER_DUPCTX:
1522 if (cipher->dupctx != NULL)
1523 break;
363b1e5d 1524 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
df05f2ce 1525 break;
df05f2ce
MC
1526 case OSSL_FUNC_CIPHER_GET_PARAMS:
1527 if (cipher->get_params != NULL)
1528 break;
363b1e5d 1529 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
df05f2ce 1530 break;
92d9d0ae
RL
1531 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1532 if (cipher->get_ctx_params != NULL)
718b133a 1533 break;
363b1e5d 1534 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
718b133a 1535 break;
92d9d0ae
RL
1536 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1537 if (cipher->set_ctx_params != NULL)
df05f2ce 1538 break;
363b1e5d 1539 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
df05f2ce 1540 break;
ae3ff60e
RL
1541 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1542 if (cipher->gettable_params != NULL)
1543 break;
363b1e5d 1544 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
ae3ff60e
RL
1545 break;
1546 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1547 if (cipher->gettable_ctx_params != NULL)
1548 break;
1549 cipher->gettable_ctx_params =
363b1e5d 1550 OSSL_FUNC_cipher_gettable_ctx_params(fns);
ae3ff60e
RL
1551 break;
1552 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1553 if (cipher->settable_ctx_params != NULL)
1554 break;
1555 cipher->settable_ctx_params =
363b1e5d 1556 OSSL_FUNC_cipher_settable_ctx_params(fns);
ae3ff60e 1557 break;
df05f2ce
MC
1558 }
1559 }
718b133a
MC
1560 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1561 || (fnciphcnt == 0 && cipher->ccipher == NULL)
13273237 1562 || fnctxcnt != 2) {
df05f2ce
MC
1563 /*
1564 * In order to be a consistent set of functions we must have at least
1565 * a complete set of "encrypt" functions, or a complete set of "decrypt"
11dbdc07
MC
1566 * functions, or a single "cipher" function. In all cases we need both
1567 * the "newctx" and "freectx" functions.
df05f2ce 1568 */
550f974a 1569 EVP_CIPHER_free(cipher);
9311d0c4 1570 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
df05f2ce
MC
1571 return NULL;
1572 }
1573 cipher->prov = prov;
1574 if (prov != NULL)
7c95390e 1575 ossl_provider_up_ref(prov);
df05f2ce 1576
ae69da05
MC
1577 if (!evp_cipher_cache_constants(cipher)) {
1578 EVP_CIPHER_free(cipher);
1579 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1580 cipher = NULL;
1581 }
1582
df05f2ce
MC
1583 return cipher;
1584}
1585
70c35fd1 1586static int evp_cipher_up_ref(void *cipher)
df05f2ce 1587{
70c35fd1 1588 return EVP_CIPHER_up_ref(cipher);
df05f2ce
MC
1589}
1590
1591static void evp_cipher_free(void *cipher)
1592{
550f974a 1593 EVP_CIPHER_free(cipher);
df05f2ce
MC
1594}
1595
b4250010 1596EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
df05f2ce
MC
1597 const char *properties)
1598{
0211740f
RL
1599 EVP_CIPHER *cipher =
1600 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
309a78aa 1601 evp_cipher_from_algorithm, evp_cipher_up_ref,
0211740f
RL
1602 evp_cipher_free);
1603
0211740f 1604 return cipher;
df05f2ce 1605}
c540f00f 1606
550f974a
RL
1607int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1608{
1609 int ref = 0;
1610
f6c95e46
RS
1611 if (cipher->origin == EVP_ORIG_DYNAMIC)
1612 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
550f974a
RL
1613 return 1;
1614}
1615
f6c95e46
RS
1616void evp_cipher_free_int(EVP_CIPHER *cipher)
1617{
6c9bc258 1618 OPENSSL_free(cipher->type_name);
f6c95e46
RS
1619 ossl_provider_free(cipher->prov);
1620 CRYPTO_THREAD_lock_free(cipher->lock);
1621 OPENSSL_free(cipher);
1622}
1623
550f974a
RL
1624void EVP_CIPHER_free(EVP_CIPHER *cipher)
1625{
1626 int i;
1627
f6c95e46 1628 if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
550f974a
RL
1629 return;
1630
1631 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1632 if (i > 0)
1633 return;
f6c95e46 1634 evp_cipher_free_int(cipher);
550f974a
RL
1635}
1636
b4250010 1637void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
251e610c
RL
1638 void (*fn)(EVP_CIPHER *mac, void *arg),
1639 void *arg)
c540f00f
RL
1640{
1641 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1642 (void (*)(void *, void *))fn, arg,
309a78aa 1643 evp_cipher_from_algorithm, evp_cipher_free);
c540f00f 1644}