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