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