aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdutils/td/utils/crypto.cpp
diff options
context:
space:
mode:
authorlevlam <levlam@telegram.org>2022-09-19 18:17:51 +0300
committerlevlam <levlam@telegram.org>2022-09-19 18:17:51 +0300
commit275ee280d28a92f71c99170cbcea9abbb7accd8e (patch)
treecd39735f652a6feea08c94c8be7c2bb686f1deec /tdutils/td/utils/crypto.cpp
parent06e1ebf809a7bc09bfe6808fe6f2001924ab37f5 (diff)
Use thread-local EVP_MD_CTX in OpenSSL 3.0.
Diffstat (limited to 'tdutils/td/utils/crypto.cpp')
-rw-r--r--tdutils/td/utils/crypto.cpp13
1 files 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) {