summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2023-12-26 08:39:28 +0000
committertb <>2023-12-26 08:39:28 +0000
commit94232d2a9c3fa826a4ec1c6d72bfdcf48e3698b4 (patch)
tree0257bc3ebee8b70ea150711c25c36ce114d10ea9 /src
parentcde27f69b709343577ecdf2bb6c5fa262b92ecdd (diff)
downloadopenbsd-94232d2a9c3fa826a4ec1c6d72bfdcf48e3698b4.tar.gz
openbsd-94232d2a9c3fa826a4ec1c6d72bfdcf48e3698b4.tar.bz2
openbsd-94232d2a9c3fa826a4ec1c6d72bfdcf48e3698b4.zip
EVP_CipherInit_ex() merge two code paths
Clean up the cipher context unconditionally if the cipher is being set. This allows doing the dance to retain the key wrap flag only once and makes it more obvious that allocating the cipher data doesn't leak. suggested by/ok jsing
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/evp/evp_enc.c13
1 files changed, 4 insertions, 9 deletions
diff --git a/src/lib/libcrypto/evp/evp_enc.c b/src/lib/libcrypto/evp/evp_enc.c
index 1bde05f493..7c25b59dce 100644
--- a/src/lib/libcrypto/evp/evp_enc.c
+++ b/src/lib/libcrypto/evp/evp_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: evp_enc.c,v 1.79 2023/12/23 13:05:06 tb Exp $ */ 1/* $OpenBSD: evp_enc.c,v 1.80 2023/12/26 08:39:28 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -93,23 +93,18 @@ EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *engine,
93 } 93 }
94 94
95 /* 95 /*
96 * If the ctx is reused and a cipher is passed in, reset the ctx but 96 * Set up cipher and context. Allocate cipher data and initialize ctx.
97 * remember enc and whether key wrap was enabled. 97 * On ctx reuse only retain encryption direction and key wrap flag.
98 */ 98 */
99 if (cipher != NULL && ctx->cipher != NULL) { 99 if (cipher != NULL) {
100 unsigned long flags = ctx->flags; 100 unsigned long flags = ctx->flags;
101 101
102 EVP_CIPHER_CTX_cleanup(ctx); 102 EVP_CIPHER_CTX_cleanup(ctx);
103
104 ctx->encrypt = enc; 103 ctx->encrypt = enc;
105 ctx->flags = flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW; 104 ctx->flags = flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
106 }
107 105
108 /* Set up cipher. Allocate cipher data and initialize if necessary. */
109 if (cipher != NULL) {
110 ctx->cipher = cipher; 106 ctx->cipher = cipher;
111 ctx->key_len = cipher->key_len; 107 ctx->key_len = cipher->key_len;
112 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
113 108
114 if (ctx->cipher->ctx_size != 0) { 109 if (ctx->cipher->ctx_size != 0) {
115 ctx->cipher_data = calloc(1, ctx->cipher->ctx_size); 110 ctx->cipher_data = calloc(1, ctx->cipher->ctx_size);