From a9bc317603e57e1047ce6dfd2e59d900b1739e17 Mon Sep 17 00:00:00 2001 From: tb Date: Fri, 29 Dec 2023 06:59:24 +0000 Subject: [PATCH] Move the copy/copy_ex stuff down below the new/free/clear mess --- lib/libcrypto/evp/evp_digest.c | 127 +++++++++++++++++---------------- 1 file changed, 64 insertions(+), 63 deletions(-) diff --git a/lib/libcrypto/evp/evp_digest.c b/lib/libcrypto/evp/evp_digest.c index 583c454845d..75787d3f7d2 100644 --- a/lib/libcrypto/evp/evp_digest.c +++ b/lib/libcrypto/evp/evp_digest.c @@ -1,4 +1,4 @@ -/* $OpenBSD: evp_digest.c,v 1.2 2023/12/29 06:08:01 tb Exp $ */ +/* $OpenBSD: evp_digest.c,v 1.3 2023/12/29 06:59:24 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -217,68 +217,6 @@ EVP_Digest(const void *data, size_t count, return ret; } -int -EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) -{ - EVP_MD_CTX_init(out); - return EVP_MD_CTX_copy_ex(out, in); -} - -int -EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) -{ - unsigned char *tmp_buf; - - if ((in == NULL) || (in->digest == NULL)) { - EVPerror(EVP_R_INPUT_NOT_INITIALIZED); - return 0; - } - - if (out->digest == in->digest) { - tmp_buf = out->md_data; - EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE); - } else - tmp_buf = NULL; - EVP_MD_CTX_cleanup(out); - memcpy(out, in, sizeof *out); - out->md_data = NULL; - out->pctx = NULL; - - /* - * Because of the EVP_PKEY_CTX_dup() below, EVP_MD_CTX_cleanup() needs - * to free out->pctx in all cases (even if this flag is set on in). - */ - EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); - - if (in->md_data && out->digest->ctx_size) { - if (tmp_buf) { - out->md_data = tmp_buf; - } else { - out->md_data = calloc(1, out->digest->ctx_size); - if (out->md_data == NULL) { - EVPerror(ERR_R_MALLOC_FAILURE); - return 0; - } - } - memcpy(out->md_data, in->md_data, out->digest->ctx_size); - } - - out->update = in->update; - - if (in->pctx) { - out->pctx = EVP_PKEY_CTX_dup(in->pctx); - if (!out->pctx) { - EVP_MD_CTX_cleanup(out); - return 0; - } - } - - if (out->digest->copy) - return out->digest->copy(out, in); - - return 1; -} - EVP_MD_CTX * EVP_MD_CTX_new(void) { @@ -345,6 +283,69 @@ EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) return 1; } +int +EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) +{ + EVP_MD_CTX_init(out); + return EVP_MD_CTX_copy_ex(out, in); +} + +int +EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) +{ + unsigned char *tmp_buf; + + if ((in == NULL) || (in->digest == NULL)) { + EVPerror(EVP_R_INPUT_NOT_INITIALIZED); + return 0; + } + + if (out->digest == in->digest) { + tmp_buf = out->md_data; + EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE); + } else + tmp_buf = NULL; + EVP_MD_CTX_cleanup(out); + memcpy(out, in, sizeof *out); + out->md_data = NULL; + out->pctx = NULL; + + /* + * Because of the EVP_PKEY_CTX_dup() below, EVP_MD_CTX_cleanup() needs + * to free out->pctx in all cases (even if this flag is set on in). + */ + EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); + + if (in->md_data && out->digest->ctx_size) { + if (tmp_buf) { + out->md_data = tmp_buf; + } else { + out->md_data = calloc(1, out->digest->ctx_size); + if (out->md_data == NULL) { + EVPerror(ERR_R_MALLOC_FAILURE); + return 0; + } + } + memcpy(out->md_data, in->md_data, out->digest->ctx_size); + } + + out->update = in->update; + + if (in->pctx) { + out->pctx = EVP_PKEY_CTX_dup(in->pctx); + if (!out->pctx) { + EVP_MD_CTX_cleanup(out); + return 0; + } + } + + if (out->digest->copy) + return out->digest->copy(out, in); + + return 1; +} + + int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr) { -- 2.20.1