VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.2/crypto/evp/evp_enc.c@ 95165

Last change on this file since 95165 was 94404, checked in by vboxsync, 3 years ago

libs/openssl: Update to 3.0.2 and switch to it, bugref:10128

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