From 72df8af3b9cc00f590afa31371be571c1169a268 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 12 Jul 2021 15:59:57 +0200 Subject: [PATCH] libhashkit/aes: simplify code --- src/libhashkit/aes.cc | 125 ++++++++++++++++++++++++-------------- src/libhashkit/aes.h | 26 +------- src/libhashkit/encrypt.cc | 31 +--------- src/libhashkit/hashkit.cc | 37 +---------- 4 files changed, 87 insertions(+), 132 deletions(-) diff --git a/src/libhashkit/aes.cc b/src/libhashkit/aes.cc index 156bcd3d..86a41dd7 100644 --- a/src/libhashkit/aes.cc +++ b/src/libhashkit/aes.cc @@ -26,45 +26,60 @@ #define AES_KEY_NBYTES 32 #define AES_IV_NBYTES 32 -bool aes_initialize(const unsigned char *key, const size_t key_length, - encryption_context_t *crypto_context) { +struct aes_key_t { + EVP_CIPHER_CTX *encryption_context; + EVP_CIPHER_CTX *decryption_context; +}; + + +aes_key_t *aes_create_key(const char *key, const size_t key_length) { unsigned char aes_key[AES_KEY_NBYTES]; unsigned char aes_iv[AES_IV_NBYTES]; + const unsigned char *ukey = (const unsigned char *) key; + if (!key) { - return false; + return NULL; } - int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), NULL, key, key_length, DIGEST_ROUNDS, + int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), NULL, ukey, key_length, DIGEST_ROUNDS, aes_key, aes_iv); if (i != AES_KEY_NBYTES) { - return false; + return NULL; } - EVP_CIPHER_CTX_init(crypto_context->encryption_context); - EVP_CIPHER_CTX_init(crypto_context->decryption_context); - if (EVP_EncryptInit_ex(crypto_context->encryption_context, EVP_aes_256_cbc(), NULL, key, aes_iv) - != 1 - || EVP_DecryptInit_ex(crypto_context->decryption_context, EVP_aes_256_cbc(), NULL, key, - aes_iv) - != 1) + aes_key_t *aes_ctx = (aes_key_t *) malloc(sizeof(aes_key_t)); + + if (!(aes_ctx->encryption_context = EVP_CIPHER_CTX_new())) { + return NULL; + } + if (!(aes_ctx->decryption_context = EVP_CIPHER_CTX_new())) { + EVP_CIPHER_CTX_free(aes_ctx->encryption_context); + return NULL; + } + + EVP_CIPHER_CTX_init(aes_ctx->encryption_context); + EVP_CIPHER_CTX_init(aes_ctx->decryption_context); + if (EVP_EncryptInit_ex(aes_ctx->encryption_context, EVP_aes_256_cbc(), NULL, ukey, aes_iv) != 1 + || EVP_DecryptInit_ex(aes_ctx->decryption_context, EVP_aes_256_cbc(), NULL, ukey, aes_iv) != 1) { - return false; + aes_free_key(aes_ctx); + return NULL; } - return true; + + return aes_ctx; } -hashkit_string_st *aes_encrypt(encryption_context_t *crypto_context, const unsigned char *source, - size_t source_length) { - EVP_CIPHER_CTX *encryption_context = crypto_context->encryption_context; +hashkit_string_st *aes_encrypt(aes_key_t *ctx, const char *source, size_t source_length) { + EVP_CIPHER_CTX *encryption_context = ctx->encryption_context; int cipher_length = source_length + EVP_CIPHER_CTX_block_size(encryption_context); int final_length = 0; + const unsigned char *usource = (const unsigned char *) source; unsigned char *cipher_text = (unsigned char *) malloc(cipher_length); - if (cipher_text == NULL) { + if (!cipher_text) { return NULL; } if (EVP_EncryptInit_ex(encryption_context, NULL, NULL, NULL, NULL) != 1 - || EVP_EncryptUpdate(encryption_context, cipher_text, &cipher_length, source, source_length) - != 1 + || EVP_EncryptUpdate(encryption_context, cipher_text, &cipher_length, usource, source_length) != 1 || EVP_EncryptFinal_ex(encryption_context, cipher_text + cipher_length, &final_length) != 1) { free(cipher_text); @@ -72,7 +87,7 @@ hashkit_string_st *aes_encrypt(encryption_context_t *crypto_context, const unsig } hashkit_string_st *destination = hashkit_string_create(cipher_length + final_length); - if (destination == NULL) { + if (!destination) { return NULL; } char *dest = hashkit_string_c_str_mutable(destination); @@ -81,28 +96,25 @@ hashkit_string_st *aes_encrypt(encryption_context_t *crypto_context, const unsig return destination; } -hashkit_string_st *aes_decrypt(encryption_context_t *crypto_context, const unsigned char *source, - size_t source_length) { - EVP_CIPHER_CTX *decryption_context = crypto_context->decryption_context; +hashkit_string_st *aes_decrypt(aes_key_t *ctx, const char *source, size_t source_length) { + EVP_CIPHER_CTX *decryption_context = ctx->decryption_context; int plain_text_length = source_length; int final_length = 0; + const unsigned char *usource = (const unsigned char *) source; unsigned char *plain_text = (unsigned char *) malloc(plain_text_length); - if (plain_text == NULL) { + if (!plain_text) { return NULL; } if (EVP_DecryptInit_ex(decryption_context, NULL, NULL, NULL, NULL) != 1 - || EVP_DecryptUpdate(decryption_context, plain_text, &plain_text_length, source, - source_length) - != 1 - || EVP_DecryptFinal_ex(decryption_context, plain_text + plain_text_length, &final_length) - != 1) + || EVP_DecryptUpdate(decryption_context, plain_text, &plain_text_length, usource, source_length) != 1 + || EVP_DecryptFinal_ex(decryption_context, plain_text + plain_text_length, &final_length) != 1) { free(plain_text); return NULL; } hashkit_string_st *destination = hashkit_string_create(plain_text_length + final_length); - if (destination == NULL) { + if (!destination) { return NULL; } char *dest = hashkit_string_c_str_mutable(destination); @@ -111,22 +123,40 @@ hashkit_string_st *aes_decrypt(encryption_context_t *crypto_context, const unsig return destination; } -encryption_context_t *aes_clone_cryptographic_context(encryption_context_t *source) { - encryption_context_t *new_context = (encryption_context_t *) malloc(sizeof(encryption_context_t)); - if (new_context == NULL) +aes_key_t *aes_clone_key(aes_key_t *old_context) { + if (!old_context) { return NULL; + } - new_context->encryption_context = EVP_CIPHER_CTX_new(); - new_context->decryption_context = EVP_CIPHER_CTX_new(); - if (new_context->encryption_context == NULL || new_context->decryption_context == NULL) { - free(new_context); - return NULL; + aes_key_t *new_context = (aes_key_t *) malloc(sizeof(aes_key_t)); + if (new_context) { + new_context->encryption_context = EVP_CIPHER_CTX_new(); + new_context->decryption_context = EVP_CIPHER_CTX_new(); + if (!new_context->encryption_context || !new_context->decryption_context) { + aes_free_key(new_context); + return NULL; + } + EVP_CIPHER_CTX_copy(new_context->encryption_context, old_context->encryption_context); + EVP_CIPHER_CTX_copy(new_context->decryption_context, old_context->decryption_context); } - EVP_CIPHER_CTX_copy(new_context->encryption_context, source->encryption_context); - EVP_CIPHER_CTX_copy(new_context->decryption_context, source->decryption_context); + return new_context; } +void aes_free_key(aes_key_t *context) { + if (context) { + if (context->encryption_context) { + EVP_CIPHER_CTX_free(context->encryption_context); + context->encryption_context = NULL; + } + if (context->decryption_context) { + EVP_CIPHER_CTX_free(context->decryption_context); + context->decryption_context = NULL; + } + free(context); + } +} + #else # include "libhashkit/rijndael.hpp" @@ -172,7 +202,7 @@ 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) { - if (_aes_key == NULL) { + if (!_aes_key) { return NULL; } @@ -185,7 +215,7 @@ aes_key_t *aes_clone_key(aes_key_t *_aes_key) { } hashkit_string_st *aes_encrypt(aes_key_t *_aes_key, const char *source, size_t source_length) { - if (_aes_key == NULL) { + if (!_aes_key) { return NULL; } @@ -214,7 +244,7 @@ hashkit_string_st *aes_encrypt(aes_key_t *_aes_key, const char *source, size_t s } hashkit_string_st *aes_decrypt(aes_key_t *_aes_key, const char *source, size_t source_length) { - if (_aes_key == NULL) { + if (!_aes_key) { return NULL; } @@ -252,4 +282,11 @@ hashkit_string_st *aes_decrypt(aes_key_t *_aes_key, const char *source, size_t s return destination; } + +void aes_free_key(aes_key_t *key) { + if (key) { + free(key); + } +} + #endif diff --git a/src/libhashkit/aes.h b/src/libhashkit/aes.h index 243d501f..4d3e6d7f 100644 --- a/src/libhashkit/aes.h +++ b/src/libhashkit/aes.h @@ -15,34 +15,14 @@ #pragma once -#ifdef HAVE_OPENSSL_CRYPTO - -#include - -typedef struct encryption_context { - EVP_CIPHER_CTX *encryption_context; - EVP_CIPHER_CTX *decryption_context; -} encryption_context_t; - -hashkit_string_st *aes_encrypt(encryption_context_t *crypto_context, const unsigned char *source, - size_t source_length); - -hashkit_string_st *aes_decrypt(encryption_context_t *crypto_context, const unsigned char *source, - size_t source_length); - -bool aes_initialize(const unsigned char *key, const size_t key_length, - encryption_context_t *crypto_context); - -encryption_context_t *aes_clone_cryptographic_context(encryption_context_t *source); -#else - struct aes_key_t; hashkit_string_st *aes_encrypt(aes_key_t *_aes_key, const char *source, size_t source_length); hashkit_string_st *aes_decrypt(aes_key_t *_aes_key, const char *source, size_t source_length); -aes_key_t *aes_create_key(const char *key, const size_t key_length); +aes_key_t *aes_create_key(const char *key, size_t key_length); aes_key_t *aes_clone_key(aes_key_t *_aes_key); -#endif + +void aes_free_key(aes_key_t *_aes_key); diff --git a/src/libhashkit/encrypt.cc b/src/libhashkit/encrypt.cc index effa299f..ff269c05 100644 --- a/src/libhashkit/encrypt.cc +++ b/src/libhashkit/encrypt.cc @@ -15,50 +15,21 @@ #include "libhashkit/common.h" -#ifdef HAVE_OPENSSL_CRYPTO -# include -#endif - hashkit_string_st *hashkit_encrypt(hashkit_st *kit, const char *source, size_t source_length) { -#ifdef HAVE_OPENSSL_CRYPTO - return aes_encrypt((encryption_context_t *) kit->_key, - (const unsigned char *) source, source_length); -#else return aes_encrypt((aes_key_t *) kit->_key, source, source_length); -#endif } hashkit_string_st *hashkit_decrypt(hashkit_st *kit, const char *source, size_t source_length) { -#ifdef HAVE_OPENSSL_CRYPTO - return aes_decrypt((encryption_context_t *) kit->_key, - (const unsigned char *) source, source_length); -#else return aes_decrypt((aes_key_t *)kit->_key, source, source_length); -#endif } -#ifdef HAVE_OPENSSL_CRYPTO -bool hashkit_key(hashkit_st *kit, const char *key, const size_t key_length) { - kit->_key = (encryption_context_t *) malloc(sizeof(encryption_context_t)); - ((encryption_context_t *) kit->_key)->encryption_context = EVP_CIPHER_CTX_new(); - ((encryption_context_t *) kit->_key)->decryption_context = EVP_CIPHER_CTX_new(); - if (((encryption_context_t *) kit->_key)->encryption_context == NULL - || ((encryption_context_t *) kit->_key)->decryption_context == NULL) - { - return false; - } - return aes_initialize((const unsigned char *) key, key_length, - (encryption_context_t *) kit->_key); -} -#else bool hashkit_key(hashkit_st *kit, const char *key, const size_t key_length) { if (kit->_key) { - free(kit->_key); + aes_free_key((aes_key_t *) kit->_key); } kit->_key = aes_create_key(key, key_length); return bool(kit->_key); } -#endif diff --git a/src/libhashkit/hashkit.cc b/src/libhashkit/hashkit.cc index e61b014d..63b7f62e 100644 --- a/src/libhashkit/hashkit.cc +++ b/src/libhashkit/hashkit.cc @@ -15,10 +15,6 @@ #include "libhashkit/common.h" -#ifdef HAVE_OPENSSL_CRYPTO -# include -#endif - static inline void _hashkit_init(hashkit_st *self) { self->base_hash.function = hashkit_one_at_a_time; self->base_hash.context = NULL; @@ -56,26 +52,11 @@ hashkit_st *hashkit_create(hashkit_st *self) { return self; } -#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); - free(context); -} -#endif - void hashkit_free(hashkit_st *self) { -#ifdef HAVE_OPENSSL_CRYPTO if (self and self->_key) { - cryptographic_context_free((encryption_context_t *)self->_key); + aes_free_key((aes_key_t *) self->_key); self->_key = NULL; } -#else - if (self and self->_key) { - free(self->_key); - self->_key = NULL; - } -#endif if (hashkit_is_allocated(self)) { free(self); @@ -98,21 +79,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 HAVE_OPENSSL_CRYPTO - if (destination->_key) { - cryptographic_context_free((encryption_context_t *)destination->_key); - destination->_key = NULL; - } - if (source->_key) { - destination->_key = - aes_clone_cryptographic_context(((encryption_context_t *) source->_key)); - if (destination->_key) { - - } - } -#else - destination->_key = aes_clone_key(static_cast(source->_key)); -#endif + destination->_key = aes_clone_key((aes_key_t *) source->_key); return destination; } -- 2.30.2