Skip to content

Commit

Permalink
Support for OpenSSL 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Maffre committed Aug 18, 2023
1 parent b24fca7 commit 20536cb
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 56 deletions.
3 changes: 0 additions & 3 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 4 additions & 32 deletions merklecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <vector>

#ifdef HAVE_OPENSSL
# include <openssl/evp.h>
# include <openssl/sha.h>
#endif

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
21 changes: 0 additions & 21 deletions test/compare_hash_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -103,10 +102,6 @@ void compare_compression_hashes()
EverCryptTree mte;
#endif

#ifdef HAVE_OPENSSL
OpenSSLTree mto;
#endif

#ifdef HAVE_MBEDTLS
MbedTLSTree mtm;
#endif
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -329,10 +312,6 @@ int main()

bench<merkle::Tree>(hashes, "merklecpp", root_interval);

#ifdef HAVE_OPENSSL
bench<OpenSSLTree>(hashes, "OpenSSL", root_interval);
#endif

#ifdef HAVE_MBEDTLS
bench<MbedTLSTree>(hashes, "mbedTLS", root_interval);
#endif
Expand Down

0 comments on commit 20536cb

Please sign in to comment.