From b7f446e55146456e368c3926347f4c771afcea8c Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 12 Jul 2021 15:08:57 +0200 Subject: [PATCH] libhashkit/aes: make using openssl configurable --- CMakeConfig.txt | 3 +++ src/libhashkit/CMakeLists.txt | 16 ++++++++++------ src/libhashkit/aes.cc | 4 ++-- src/libhashkit/aes.h | 4 ++-- src/libhashkit/encrypt.cc | 10 +++++----- src/libhashkit/hashkit.cc | 8 ++++---- 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/CMakeConfig.txt b/CMakeConfig.txt index 973ff824..d8afcaef 100644 --- a/CMakeConfig.txt +++ b/CMakeConfig.txt @@ -65,6 +65,9 @@ if(NOT DEFINED ENV{ENABLE_MEMASLAP}) endif() option(ENABLE_MEMASLAP "enable memaslap client" $ENV{ENABLE_MEMASLAP}) +option(ENABLE_OPENSSL_CRYPTO + "enable OpenSSL's libcrypto instead of bundled AES implementation" + $ENV{ENABLE_OPENSSL_CRYPTO}) if(BUILD_TESTING) set(MEMCACHED_BINARY "$ENV{MEMCACHED_BINARY}" diff --git a/src/libhashkit/CMakeLists.txt b/src/libhashkit/CMakeLists.txt index d0e03d15..ed3f7f1d 100644 --- a/src/libhashkit/CMakeLists.txt +++ b/src/libhashkit/CMakeLists.txt @@ -40,12 +40,16 @@ target_include_directories(libhashkit PUBLIC $ $) -find_package(OpenSSL) -if(NOT OPENSSL_FOUND) - message(WARNING "crypto library not found") -else() - add_compile_definitions(WITH_OPENSSL) - target_link_libraries(libhashkit PUBLIC OpenSSL::Crypto) +if(ENABLE_OPENSSL_CRYPTO) + find_package(OpenSSL) + if(OPENSSL_FOUND) + if(OPENSSL_CRYPTO_LIBRARY) + target_compile_definitions(libhashkit PRIVATE HAVE_OPENSSL_CRYPTO) + target_link_libraries(libhashkit PUBLIC OpenSSL::Crypto) + else() + message(WARNING "Could not find OpenSSL::Crypto") + endif() + endif() endif() configure_file(hashkitcon.h.in hashkitcon.h @ONLY) diff --git a/src/libhashkit/aes.cc b/src/libhashkit/aes.cc index d4fdad5a..d65a9d91 100644 --- a/src/libhashkit/aes.cc +++ b/src/libhashkit/aes.cc @@ -17,7 +17,7 @@ #include -#ifdef WITH_OPENSSL +#ifdef HAVE_OPENSSL_CRYPTO #include @@ -250,4 +250,4 @@ hashkit_string_st *aes_decrypt(aes_key_t *_aes_key, const char *source, size_t s return destination; } -#endif \ No newline at end of file +#endif diff --git a/src/libhashkit/aes.h b/src/libhashkit/aes.h index e021c5f1..243d501f 100644 --- a/src/libhashkit/aes.h +++ b/src/libhashkit/aes.h @@ -15,7 +15,7 @@ #pragma once -#ifdef WITH_OPENSSL +#ifdef HAVE_OPENSSL_CRYPTO #include @@ -45,4 +45,4 @@ hashkit_string_st *aes_decrypt(aes_key_t *_aes_key, const char *source, size_t s aes_key_t *aes_create_key(const char *key, const size_t key_length); aes_key_t *aes_clone_key(aes_key_t *_aes_key); -#endif \ No newline at end of file +#endif diff --git a/src/libhashkit/encrypt.cc b/src/libhashkit/encrypt.cc index dbc051ae..e7898a6a 100644 --- a/src/libhashkit/encrypt.cc +++ b/src/libhashkit/encrypt.cc @@ -15,12 +15,12 @@ #include "libhashkit/common.h" -#ifdef WITH_OPENSSL +#ifdef HAVE_OPENSSL_CRYPTO # include #endif hashkit_string_st *hashkit_encrypt(hashkit_st *kit, const char *source, size_t source_length) { -#ifdef WITH_OPENSSL +#ifdef HAVE_OPENSSL_CRYPTO return aes_encrypt((encryption_context_t *) kit->_cryptographic_context, (const unsigned char *) source, source_length); #else @@ -30,7 +30,7 @@ hashkit_string_st *hashkit_encrypt(hashkit_st *kit, const char *source, size_t s } hashkit_string_st *hashkit_decrypt(hashkit_st *kit, const char *source, size_t source_length) { -#ifdef WITH_OPENSSL +#ifdef HAVE_OPENSSL_CRYPTO return aes_decrypt((encryption_context_t *) kit->_cryptographic_context, (const unsigned char *) source, source_length); #else @@ -38,7 +38,7 @@ hashkit_string_st *hashkit_decrypt(hashkit_st *kit, const char *source, size_t s #endif } -#ifdef WITH_OPENSSL +#ifdef HAVE_OPENSSL_CRYPTO bool hashkit_key(hashkit_st *kit, const char *key, const size_t key_length) { kit->_cryptographic_context = (encryption_context_t *) malloc(sizeof(encryption_context_t)); ((encryption_context_t *) kit->_cryptographic_context)->encryption_context = EVP_CIPHER_CTX_new(); @@ -61,4 +61,4 @@ bool hashkit_key(hashkit_st *kit, const char *key, const size_t key_length) { return bool(kit->_cryptographic_context); } -#endif \ No newline at end of file +#endif diff --git a/src/libhashkit/hashkit.cc b/src/libhashkit/hashkit.cc index 46cf6368..d15d7372 100644 --- a/src/libhashkit/hashkit.cc +++ b/src/libhashkit/hashkit.cc @@ -15,7 +15,7 @@ #include "libhashkit/common.h" -#ifdef WITH_OPENSSL +#ifdef HAVE_OPENSSL_CRYPTO # include #endif @@ -56,7 +56,7 @@ hashkit_st *hashkit_create(hashkit_st *self) { return self; } -#ifdef WITH_OPENSSL +#ifdef HAVE_OPENSSL_CRYPTO static void cryptographic_context_free(encryption_context_t *context) { EVP_CIPHER_CTX_free(context->encryption_context); EVP_CIPHER_CTX_free(context->decryption_context); @@ -65,7 +65,7 @@ static void cryptographic_context_free(encryption_context_t *context) { #endif void hashkit_free(hashkit_st *self) { -#ifdef WITH_OPENSSL +#ifdef HAVE_OPENSSL_CRYPTO if (self and self->_cryptographic_context) { cryptographic_context_free((encryption_context_t *)self->_cryptographic_context); self->_cryptographic_context = NULL; @@ -98,7 +98,7 @@ hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *source) { destination->base_hash = source->base_hash; destination->distribution_hash = source->distribution_hash; destination->flags = source->flags; -#ifdef WITH_OPENSSL +#ifdef HAVE_OPENSSL_CRYPTO if (destination->_cryptographic_context) { cryptographic_context_free((encryption_context_t *)destination->_cryptographic_context); destination->_cryptographic_context = NULL; -- 2.30.2