VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.5/crypto/evp/evp_enc.c@ 104078

Last change on this file since 104078 was 104078, checked in by vboxsync, 12 months ago

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette