From 51b1a9963dfeeaa2bc00b8ff3df4d74704a5d4f1 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 24 Feb 2023 11:19:34 +0100 Subject: [PATCH] QCryptographicHash: separate EVP and non-EVP OpenSSL3 operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These, too, will end up on different classes, eventually. The addData() function is the odd one out here, as it's EVP part it trivial. Pick-to: 6.5 Change-Id: I24c7334edd8ba218e4cfcb64d0ed67bc281c4525 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/tools/qcryptographichash.cpp | 27 +++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index d0c126876b..9445b4fad3 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -330,6 +330,10 @@ public: EVP_MD_ptr algorithm; EVP_MD_CTX_ptr context; bool initializationFailed = false; + + void evp_init(); + void evp_reset() noexcept; + void evp_finalizeUnchecked() noexcept; #endif union { @@ -539,14 +543,17 @@ void QCryptographicHashPrivate::init() method == QCryptographicHash::Blake2b_256 || method == QCryptographicHash::Blake2b_384) { new (&blake2bContext) blake2b_state; - return; } else if (method == QCryptographicHash::Blake2s_128 || method == QCryptographicHash::Blake2s_160 || method == QCryptographicHash::Blake2s_224) { new (&blake2sContext) blake2s_state; - return; + } else { + evp_init(); } +} +void QCryptographicHashPrivate::evp_init() +{ Q_ASSERT(!context); initializationFailed = true; @@ -652,14 +659,17 @@ void QCryptographicHashPrivate::reset() noexcept method == QCryptographicHash::Blake2b_256 || method == QCryptographicHash::Blake2b_384) { blake2b_init(&blake2bContext, hashLengthInternal(method)); - return; } else if (method == QCryptographicHash::Blake2s_128 || method == QCryptographicHash::Blake2s_160 || method == QCryptographicHash::Blake2s_224) { blake2s_init(&blake2sContext, hashLengthInternal(method)); - return; + } else { + evp_reset(); } +} +void QCryptographicHashPrivate::evp_reset() noexcept +{ if (!initializationFailed) { Q_ASSERT(context); Q_ASSERT(algorithm); @@ -950,7 +960,14 @@ void QCryptographicHashPrivate::finalizeUnchecked() noexcept blake2s_state copy = blake2sContext; result.resizeForOverwrite(length); blake2s_final(©, result.data(), length); - } else if (!initializationFailed) { + } else { + evp_finalizeUnchecked(); + } +} + +void QCryptographicHashPrivate::evp_finalizeUnchecked() noexcept +{ + if (!initializationFailed) { EVP_MD_CTX_ptr copy = EVP_MD_CTX_ptr(EVP_MD_CTX_new()); EVP_MD_CTX_copy_ex(copy.get(), context.get()); result.resizeForOverwrite(EVP_MD_get_size(algorithm.get()));