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