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