From 275ee280d28a92f71c99170cbcea9abbb7accd8e Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 19 Sep 2022 18:17:51 +0300 Subject: [PATCH] Use thread-local EVP_MD_CTX in OpenSSL 3.0. --- tdutils/td/utils/crypto.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tdutils/td/utils/crypto.cpp b/tdutils/td/utils/crypto.cpp index 0f859acd7..cca3215f4 100644 --- a/tdutils/td/utils/crypto.cpp +++ b/tdutils/td/utils/crypto.cpp @@ -721,15 +721,22 @@ void AesCtrState::decrypt(Slice from, MutableSlice to) { #if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER) static void make_digest(Slice data, MutableSlice output, const EVP_MD *evp_md) { - EVP_MD_CTX *ctx = EVP_MD_CTX_new(); - LOG_IF(FATAL, ctx == nullptr); + static TD_THREAD_LOCAL EVP_MD_CTX *ctx; + if (unlikely(ctx == nullptr)) { + ctx = EVP_MD_CTX_new(); + LOG_IF(FATAL, ctx == nullptr); + detail::add_thread_local_destructor(create_destructor([] { + EVP_MD_CTX_free(ctx); + ctx = nullptr; + })); + } int res = EVP_DigestInit_ex(ctx, evp_md, nullptr); LOG_IF(FATAL, res != 1); res = EVP_DigestUpdate(ctx, data.ubegin(), data.size()); LOG_IF(FATAL, res != 1); res = EVP_DigestFinal_ex(ctx, output.ubegin(), nullptr); LOG_IF(FATAL, res != 1); - EVP_MD_CTX_free(ctx); + EVP_MD_CTX_reset(ctx); } static void init_thread_local_evp_md(const EVP_MD *&evp_md, const char *algorithm) {