Skip to content

Commit

Permalink
Merge pull request #17 from microsoft/openssl3
Browse files Browse the repository at this point in the history
Support for OpenSSL 3.1
  • Loading branch information
achamayou authored Aug 18, 2023
2 parents 60415ba + eb99b51 commit dd81045
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 49 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
40 changes: 15 additions & 25 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 @@ -1390,7 +1391,8 @@ namespace merkle
if (index >= num_leaves())
throw std::runtime_error("leaf index out of bounds");
if (index - num_flushed >= leaf_nodes.size())
return uninserted_leaf_nodes.at(index - num_flushed - leaf_nodes.size())
return uninserted_leaf_nodes
.at(index - num_flushed - leaf_nodes.size())
->hash;
else
return leaf_nodes.at(index - num_flushed)->hash;
Expand Down Expand Up @@ -1622,7 +1624,8 @@ namespace merkle
if (index >= num_leaves())
throw std::runtime_error("leaf index out of bounds");
if (index - num_flushed >= leaf_nodes.size())
return uninserted_leaf_nodes.at(index - num_flushed - leaf_nodes.size());
return uninserted_leaf_nodes.at(
index - num_flushed - leaf_nodes.size());
else
return leaf_nodes.at(index - num_flushed);
}
Expand Down Expand Up @@ -1734,7 +1737,8 @@ namespace merkle
MERKLECPP_TRACE({
std::string nodes;
for (size_t i = 0; i < insertion_stack.size(); i++)
nodes += " " + insertion_stack.at(i).n->hash.to_string(TRACE_HASH_SIZE);
nodes +=
" " + insertion_stack.at(i).n->hash.to_string(TRACE_HASH_SIZE);
MERKLECPP_TOUT << " X " << (complete ? "complete" : "continue") << ":"
<< nodes << std::endl;
});
Expand Down Expand Up @@ -1882,27 +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);

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 @@ -1916,7 +1899,14 @@ namespace merkle
uint8_t block[32 * 2];
memcpy(&block[0], l.bytes, 32);
memcpy(&block[32], r.bytes, 32);
SHA256(block, sizeof(block), out.bytes);

const EVP_MD* md = EVP_sha256();
int rc =
EVP_Digest(&block[0], sizeof(block), out.bytes, nullptr, md, nullptr);
if (rc != 1)
{
throw std::runtime_error("EVP_Digest failed: " + std::to_string(rc));
}
}
#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 dd81045

Please sign in to comment.