From 20536cb67ae20b901991d11f2b3750cff86a5236 Mon Sep 17 00:00:00 2001 From: Julien Maffre Date: Fri, 18 Aug 2023 13:17:29 +0000 Subject: [PATCH] Support for OpenSSL 3 --- doc/index.rst | 3 --- merklecpp.h | 36 ++++----------------------------- test/compare_hash_functions.cpp | 21 ------------------- 3 files changed, 4 insertions(+), 56 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index c66aaea..80a2ae4 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -55,9 +55,6 @@ To enable these bindings, merklecpp requires the compiler macros .. doxygenfunction:: merkle::sha256_compress :project: merklecpp -.. doxygenfunction:: merkle::sha256_compress_openssl - :project: merklecpp - .. doxygenfunction:: merkle::sha256_openssl :project: merklecpp diff --git a/merklecpp.h b/merklecpp.h index 0505907..2cd0aea 100644 --- a/merklecpp.h +++ b/merklecpp.h @@ -17,6 +17,7 @@ #include #ifdef HAVE_OPENSSL +# include # include #endif @@ -1885,34 +1886,6 @@ namespace merkle // clang-format on #ifdef HAVE_OPENSSL - /// @brief OpenSSL's SHA256 compression function - /// @param l Left node hash - /// @param r Right node hash - /// @param out Output node hash - /// @note Some versions of OpenSSL may not provide SHA256_Transform. - static inline void sha256_compress_openssl( - const HashT<32>& l, const HashT<32>& r, HashT<32>& out) - { - unsigned char block[32 * 2]; - memcpy(&block[0], l.bytes, 32); - memcpy(&block[32], r.bytes, 32); - - const EVP_MD* md = EVP_sha256(); - int rc = EVP_Digest(&block[0], 32 * 2, h, nullptr, md, nullptr); - if (rc != 1) - { - throw std::logic_error(fmt::format("EVP_Digest failed: {}", rc)); - } - - // SHA256_CTX ctx; - // if (SHA256_Init(&ctx) != 1) - // printf("SHA256_Init error"); - // SHA256_Transform(&ctx, &block[0]); - - for (int i = 0; i < 8; i++) - ((uint32_t*)out.bytes)[i] = convert_endianness(((uint32_t*)ctx.h)[i]); - } - /// @brief OpenSSL SHA256 /// @param l Left node hash /// @param r Right node hash @@ -1928,13 +1901,12 @@ namespace merkle memcpy(&block[32], r.bytes, 32); const EVP_MD* md = EVP_sha256(); - int rc = EVP_Digest(&block[0], sizeof(block), h, nullptr, md, nullptr); + int rc = + EVP_Digest(&block[0], sizeof(block), out.bytes, nullptr, md, nullptr); if (rc != 1) { - throw std::logic_error(fmt::format("EVP_Digest failed: {}", rc)); + throw std::runtime_error("EVP_Digest failed: " + std::to_string(rc)); } - - // SHA256(block, sizeof(block), out.bytes); } #endif diff --git a/test/compare_hash_functions.cpp b/test/compare_hash_functions.cpp index 76d48e5..94d80e7 100644 --- a/test/compare_hash_functions.cpp +++ b/test/compare_hash_functions.cpp @@ -47,7 +47,6 @@ typedef merkle::TreeT<32, sha256_evercrypt> EverCryptFullTree; #endif #ifdef HAVE_OPENSSL -typedef merkle::TreeT<32, merkle::sha256_compress_openssl> OpenSSLTree; typedef merkle::TreeT<32, merkle::sha256_openssl> OpenSSLFullTree; #endif @@ -103,10 +102,6 @@ void compare_compression_hashes() EverCryptTree mte; #endif -#ifdef HAVE_OPENSSL - OpenSSLTree mto; -#endif - #ifdef HAVE_MBEDTLS MbedTLSTree mtm; #endif @@ -123,10 +118,6 @@ void compare_compression_hashes() mte.insert(h); #endif -#ifdef HAVE_OPENSSL - mto.insert(h); -#endif - #ifdef HAVE_MBEDTLS mtm.insert(h); #endif @@ -139,10 +130,6 @@ void compare_compression_hashes() compare_roots(mt, mte, "EverCrypt"); #endif -#ifdef HAVE_OPENSSL - compare_roots(mt, mto, "OpenSSL"); -#endif - #ifdef HAVE_MBEDTLS compare_roots(mt, mtm, "mbedTLS"); #endif @@ -155,10 +142,6 @@ void compare_compression_hashes() compare_roots(mt, mte, "EverCrypt"); #endif -#ifdef HAVE_OPENSSL - compare_roots(mt, mto, "OpenSSL"); -#endif - #ifdef HAVE_MBEDTLS compare_roots(mt, mtm, "mbedTLS"); #endif @@ -329,10 +312,6 @@ int main() bench(hashes, "merklecpp", root_interval); -#ifdef HAVE_OPENSSL - bench(hashes, "OpenSSL", root_interval); -#endif - #ifdef HAVE_MBEDTLS bench(hashes, "mbedTLS", root_interval); #endif