VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.0/crypto/evp/evp_enc.c@ 99371

最後變更 在這個檔案從99371是 99366,由 vboxsync 提交於 23 月 前

openssl-3.1.0: Applied and adjusted our OpenSSL changes to 3.0.7. bugref:10418

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

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