From 2ee12bf326810e45f12b50bc30c254b3bb7ff58b Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Sun, 28 Sep 2025 17:19:04 +0200 Subject: [PATCH] Make SHA256 30% faster when used for cloaking and other very small inputs, simply by re-using the context. The slowdown happened due to commit a541b8f4ad794200688831e184c9d145d74198cc in June 2021 when converting to OpenSSL 3+ code. Now it is basically back to the pre-openssl-v3 speeds. --- src/misc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/misc.c b/src/misc.c index 0779cce16..1e1e1b687 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1955,12 +1955,13 @@ void sha256hash_binary(char *dst, const char *src, unsigned long n) { #if OPENSSL_VERSION_NUMBER >= 0x30000000L unsigned int md_len; - EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); + static EVP_MD_CTX *mdctx = NULL; + if (!mdctx) + mdctx = EVP_MD_CTX_new(); if (EVP_DigestInit_ex(mdctx, sha256_function, NULL) != 1) abort(); EVP_DigestUpdate(mdctx, src, n); EVP_DigestFinal_ex(mdctx, dst, &md_len); - EVP_MD_CTX_free(mdctx); #else SHA256_CTX hash;