Skip to content

Remove internal AES implementation and replace it with use of libcrypto#114

Merged
m6w6 merged 1 commit intoawesomized:v1.xfrom
TomasKorbar:v1.x
Jul 13, 2021
Merged

Remove internal AES implementation and replace it with use of libcrypto#114
m6w6 merged 1 commit intoawesomized:v1.xfrom
TomasKorbar:v1.x

Conversation

@TomasKorbar
Copy link
Copy Markdown
Contributor

Hi,
As we are planning to replace original libmemcached in Fedora with awesomized libmemcached
i want to propose replacement of internal AES implementation with use of openssl.
In my opinion it is safer and more maintainable to use cryptographic library.
All tests are passing.
Feel free to propose any change.

@remicollet
Copy link
Copy Markdown
Contributor

remicollet commented Jun 25, 2021

@TomasKorbar this break build with old openssl (such as 1.02k in RHEL-7)

Proposal

diff -up ./src/libhashkit/aes.cc.old ./src/libhashkit/aes.cc
--- ./src/libhashkit/aes.cc.old	2021-06-25 14:53:38.891064258 +0200
+++ ./src/libhashkit/aes.cc	2021-06-25 14:53:45.840043166 +0200
@@ -37,10 +37,10 @@ bool aes_initialize(const unsigned char
     return false;
   }
 
-  if (EVP_CIPHER_CTX_init(encryption_context) != 1 ||
-      EVP_EncryptInit_ex(encryption_context, EVP_aes_256_cbc(), NULL, key,
+  EVP_CIPHER_CTX_init(encryption_context);
+  EVP_CIPHER_CTX_init(decryption_context);
+  if (EVP_EncryptInit_ex(encryption_context, EVP_aes_256_cbc(), NULL, key,
                          aes_iv) != 1 ||
-      EVP_CIPHER_CTX_init(decryption_context) != 1 ||
       EVP_DecryptInit_ex(decryption_context, EVP_aes_256_cbc(), NULL, key,
                          aes_iv) != 1) {
     return false;

in Openssl 1.0

void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a);

In Openssl 1.1

#  define EVP_CIPHER_CTX_init(c)      EVP_CIPHER_CTX_reset(c)
int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c);

Copy link
Copy Markdown
Collaborator

@m6w6 m6w6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Please see the inline comments.

CMakeLists.txt Outdated
if(NOT CRYPTO_LIB)
message(FATAL_ERROR "crypto library not found")
endif()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This should be optional, and the build should use the bundled implementation when libcrypto was not found.
  • It should use the cmake module FindOpenSSL and lbhashkit should link against OpenSSL::Crypto if found.
  • It should be located in src/libhashkit/CMakeLists.txt

#include <libhashkit-1.0/string.h>

#include <openssl/evp.h>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be #ifdef'd for optional support

bool use_encryption;
EVP_CIPHER_CTX *encryption_context;
EVP_CIPHER_CTX *decryption_context;
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the ABI, so either allocate a private struct and reuse _key, or this may only be targeted at v2.0


HASHKIT_API
bool hashkit_key(hashkit_st *, const char *key, const size_t key_length);
bool hashkit_initialize_encryption(hashkit_st *kit, const char *key, const size_t key_length);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're way too far into the stability game, so changing the API for better names is not a good idea.

$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(libhashkit PUBLIC -lcrypto)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the FindOpenSSL and OpenSSL::Crypto comment above.

/* These are private */
#define memcached_is_allocated(__object) ((__object)->options.is_allocated)
#define memcached_is_encrypted(__object) ((__object)->hashkit._key)
#define memcached_is_encrypted(__object) ((__object)->hashkit.use_encryption)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be (!!(__object)->hashkit._key) or alike.

EVP_CIPHER_CTX_init(decryption_context) != 1 ||
EVP_DecryptInit_ex(decryption_context, EVP_aes_256_cbc(), NULL, key,
aes_iv) != 1) {
return false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment from Remi: #114 (comment)

@TomasKorbar
Copy link
Copy Markdown
Contributor Author

TomasKorbar commented Jul 2, 2021

Hi @m6w6 ,
I updated pull request according to your review. What do you think about it now?

@TomasKorbar
Copy link
Copy Markdown
Contributor Author

@m6w6 Any update on this?

@m6w6
Copy link
Copy Markdown
Collaborator

m6w6 commented Jul 9, 2021

Yes, the update is, that I'm currently camping and there have been thunderstorms tonight. 😉

@TomasKorbar
Copy link
Copy Markdown
Contributor Author

@m6w6 No pressure. Just wanted to keep this alive :) Enjoy your camping and i hope that the storms did not cause you any trouble.

@m6w6 m6w6 merged commit 2aab181 into awesomized:v1.x Jul 13, 2021
@m6w6
Copy link
Copy Markdown
Collaborator

m6w6 commented Jul 13, 2021

Merged, with a couple of fixes and simplifications, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants