Skip to content

Commit 72df8af

Browse files
committed
libhashkit/aes: simplify code
1 parent c8300fc commit 72df8af

File tree

4 files changed

+87
-132
lines changed

4 files changed

+87
-132
lines changed

src/libhashkit/aes.cc

Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,53 +26,68 @@
2626
#define AES_KEY_NBYTES 32
2727
#define AES_IV_NBYTES 32
2828

29-
bool aes_initialize(const unsigned char *key, const size_t key_length,
30-
encryption_context_t *crypto_context) {
29+
struct aes_key_t {
30+
EVP_CIPHER_CTX *encryption_context;
31+
EVP_CIPHER_CTX *decryption_context;
32+
};
33+
34+
35+
aes_key_t *aes_create_key(const char *key, const size_t key_length) {
3136
unsigned char aes_key[AES_KEY_NBYTES];
3237
unsigned char aes_iv[AES_IV_NBYTES];
38+
const unsigned char *ukey = (const unsigned char *) key;
39+
3340
if (!key) {
34-
return false;
41+
return NULL;
3542
}
3643

37-
int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), NULL, key, key_length, DIGEST_ROUNDS,
44+
int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), NULL, ukey, key_length, DIGEST_ROUNDS,
3845
aes_key, aes_iv);
3946
if (i != AES_KEY_NBYTES) {
40-
return false;
47+
return NULL;
4148
}
4249

43-
EVP_CIPHER_CTX_init(crypto_context->encryption_context);
44-
EVP_CIPHER_CTX_init(crypto_context->decryption_context);
45-
if (EVP_EncryptInit_ex(crypto_context->encryption_context, EVP_aes_256_cbc(), NULL, key, aes_iv)
46-
!= 1
47-
|| EVP_DecryptInit_ex(crypto_context->decryption_context, EVP_aes_256_cbc(), NULL, key,
48-
aes_iv)
49-
!= 1)
50+
aes_key_t *aes_ctx = (aes_key_t *) malloc(sizeof(aes_key_t));
51+
52+
if (!(aes_ctx->encryption_context = EVP_CIPHER_CTX_new())) {
53+
return NULL;
54+
}
55+
if (!(aes_ctx->decryption_context = EVP_CIPHER_CTX_new())) {
56+
EVP_CIPHER_CTX_free(aes_ctx->encryption_context);
57+
return NULL;
58+
}
59+
60+
EVP_CIPHER_CTX_init(aes_ctx->encryption_context);
61+
EVP_CIPHER_CTX_init(aes_ctx->decryption_context);
62+
if (EVP_EncryptInit_ex(aes_ctx->encryption_context, EVP_aes_256_cbc(), NULL, ukey, aes_iv) != 1
63+
|| EVP_DecryptInit_ex(aes_ctx->decryption_context, EVP_aes_256_cbc(), NULL, ukey, aes_iv) != 1)
5064
{
51-
return false;
65+
aes_free_key(aes_ctx);
66+
return NULL;
5267
}
53-
return true;
68+
69+
return aes_ctx;
5470
}
5571

56-
hashkit_string_st *aes_encrypt(encryption_context_t *crypto_context, const unsigned char *source,
57-
size_t source_length) {
58-
EVP_CIPHER_CTX *encryption_context = crypto_context->encryption_context;
72+
hashkit_string_st *aes_encrypt(aes_key_t *ctx, const char *source, size_t source_length) {
73+
EVP_CIPHER_CTX *encryption_context = ctx->encryption_context;
5974
int cipher_length = source_length + EVP_CIPHER_CTX_block_size(encryption_context);
6075
int final_length = 0;
76+
const unsigned char *usource = (const unsigned char *) source;
6177
unsigned char *cipher_text = (unsigned char *) malloc(cipher_length);
62-
if (cipher_text == NULL) {
78+
if (!cipher_text) {
6379
return NULL;
6480
}
6581
if (EVP_EncryptInit_ex(encryption_context, NULL, NULL, NULL, NULL) != 1
66-
|| EVP_EncryptUpdate(encryption_context, cipher_text, &cipher_length, source, source_length)
67-
!= 1
82+
|| EVP_EncryptUpdate(encryption_context, cipher_text, &cipher_length, usource, source_length) != 1
6883
|| EVP_EncryptFinal_ex(encryption_context, cipher_text + cipher_length, &final_length) != 1)
6984
{
7085
free(cipher_text);
7186
return NULL;
7287
}
7388

7489
hashkit_string_st *destination = hashkit_string_create(cipher_length + final_length);
75-
if (destination == NULL) {
90+
if (!destination) {
7691
return NULL;
7792
}
7893
char *dest = hashkit_string_c_str_mutable(destination);
@@ -81,28 +96,25 @@ hashkit_string_st *aes_encrypt(encryption_context_t *crypto_context, const unsig
8196
return destination;
8297
}
8398

84-
hashkit_string_st *aes_decrypt(encryption_context_t *crypto_context, const unsigned char *source,
85-
size_t source_length) {
86-
EVP_CIPHER_CTX *decryption_context = crypto_context->decryption_context;
99+
hashkit_string_st *aes_decrypt(aes_key_t *ctx, const char *source, size_t source_length) {
100+
EVP_CIPHER_CTX *decryption_context = ctx->decryption_context;
87101
int plain_text_length = source_length;
88102
int final_length = 0;
103+
const unsigned char *usource = (const unsigned char *) source;
89104
unsigned char *plain_text = (unsigned char *) malloc(plain_text_length);
90-
if (plain_text == NULL) {
105+
if (!plain_text) {
91106
return NULL;
92107
}
93108
if (EVP_DecryptInit_ex(decryption_context, NULL, NULL, NULL, NULL) != 1
94-
|| EVP_DecryptUpdate(decryption_context, plain_text, &plain_text_length, source,
95-
source_length)
96-
!= 1
97-
|| EVP_DecryptFinal_ex(decryption_context, plain_text + plain_text_length, &final_length)
98-
!= 1)
109+
|| EVP_DecryptUpdate(decryption_context, plain_text, &plain_text_length, usource, source_length) != 1
110+
|| EVP_DecryptFinal_ex(decryption_context, plain_text + plain_text_length, &final_length) != 1)
99111
{
100112
free(plain_text);
101113
return NULL;
102114
}
103115

104116
hashkit_string_st *destination = hashkit_string_create(plain_text_length + final_length);
105-
if (destination == NULL) {
117+
if (!destination) {
106118
return NULL;
107119
}
108120
char *dest = hashkit_string_c_str_mutable(destination);
@@ -111,22 +123,40 @@ hashkit_string_st *aes_decrypt(encryption_context_t *crypto_context, const unsig
111123
return destination;
112124
}
113125

114-
encryption_context_t *aes_clone_cryptographic_context(encryption_context_t *source) {
115-
encryption_context_t *new_context = (encryption_context_t *) malloc(sizeof(encryption_context_t));
116-
if (new_context == NULL)
126+
aes_key_t *aes_clone_key(aes_key_t *old_context) {
127+
if (!old_context) {
117128
return NULL;
129+
}
118130

119-
new_context->encryption_context = EVP_CIPHER_CTX_new();
120-
new_context->decryption_context = EVP_CIPHER_CTX_new();
121-
if (new_context->encryption_context == NULL || new_context->decryption_context == NULL) {
122-
free(new_context);
123-
return NULL;
131+
aes_key_t *new_context = (aes_key_t *) malloc(sizeof(aes_key_t));
132+
if (new_context) {
133+
new_context->encryption_context = EVP_CIPHER_CTX_new();
134+
new_context->decryption_context = EVP_CIPHER_CTX_new();
135+
if (!new_context->encryption_context || !new_context->decryption_context) {
136+
aes_free_key(new_context);
137+
return NULL;
138+
}
139+
EVP_CIPHER_CTX_copy(new_context->encryption_context, old_context->encryption_context);
140+
EVP_CIPHER_CTX_copy(new_context->decryption_context, old_context->decryption_context);
124141
}
125-
EVP_CIPHER_CTX_copy(new_context->encryption_context, source->encryption_context);
126-
EVP_CIPHER_CTX_copy(new_context->decryption_context, source->decryption_context);
142+
127143
return new_context;
128144
}
129145

146+
void aes_free_key(aes_key_t *context) {
147+
if (context) {
148+
if (context->encryption_context) {
149+
EVP_CIPHER_CTX_free(context->encryption_context);
150+
context->encryption_context = NULL;
151+
}
152+
if (context->decryption_context) {
153+
EVP_CIPHER_CTX_free(context->decryption_context);
154+
context->decryption_context = NULL;
155+
}
156+
free(context);
157+
}
158+
}
159+
130160
#else
131161

132162
# include "libhashkit/rijndael.hpp"
@@ -172,7 +202,7 @@ aes_key_t *aes_create_key(const char *key, const size_t key_length) {
172202
}
173203

174204
aes_key_t *aes_clone_key(aes_key_t *_aes_key) {
175-
if (_aes_key == NULL) {
205+
if (!_aes_key) {
176206
return NULL;
177207
}
178208

@@ -185,7 +215,7 @@ aes_key_t *aes_clone_key(aes_key_t *_aes_key) {
185215
}
186216

187217
hashkit_string_st *aes_encrypt(aes_key_t *_aes_key, const char *source, size_t source_length) {
188-
if (_aes_key == NULL) {
218+
if (!_aes_key) {
189219
return NULL;
190220
}
191221

@@ -214,7 +244,7 @@ hashkit_string_st *aes_encrypt(aes_key_t *_aes_key, const char *source, size_t s
214244
}
215245

216246
hashkit_string_st *aes_decrypt(aes_key_t *_aes_key, const char *source, size_t source_length) {
217-
if (_aes_key == NULL) {
247+
if (!_aes_key) {
218248
return NULL;
219249
}
220250

@@ -252,4 +282,11 @@ hashkit_string_st *aes_decrypt(aes_key_t *_aes_key, const char *source, size_t s
252282

253283
return destination;
254284
}
285+
286+
void aes_free_key(aes_key_t *key) {
287+
if (key) {
288+
free(key);
289+
}
290+
}
291+
255292
#endif

src/libhashkit/aes.h

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,14 @@
1515

1616
#pragma once
1717

18-
#ifdef HAVE_OPENSSL_CRYPTO
19-
20-
#include <openssl/evp.h>
21-
22-
typedef struct encryption_context {
23-
EVP_CIPHER_CTX *encryption_context;
24-
EVP_CIPHER_CTX *decryption_context;
25-
} encryption_context_t;
26-
27-
hashkit_string_st *aes_encrypt(encryption_context_t *crypto_context, const unsigned char *source,
28-
size_t source_length);
29-
30-
hashkit_string_st *aes_decrypt(encryption_context_t *crypto_context, const unsigned char *source,
31-
size_t source_length);
32-
33-
bool aes_initialize(const unsigned char *key, const size_t key_length,
34-
encryption_context_t *crypto_context);
35-
36-
encryption_context_t *aes_clone_cryptographic_context(encryption_context_t *source);
37-
#else
38-
3918
struct aes_key_t;
4019

4120
hashkit_string_st *aes_encrypt(aes_key_t *_aes_key, const char *source, size_t source_length);
4221

4322
hashkit_string_st *aes_decrypt(aes_key_t *_aes_key, const char *source, size_t source_length);
4423

45-
aes_key_t *aes_create_key(const char *key, const size_t key_length);
24+
aes_key_t *aes_create_key(const char *key, size_t key_length);
4625

4726
aes_key_t *aes_clone_key(aes_key_t *_aes_key);
48-
#endif
27+
28+
void aes_free_key(aes_key_t *_aes_key);

src/libhashkit/encrypt.cc

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,21 @@
1515

1616
#include "libhashkit/common.h"
1717

18-
#ifdef HAVE_OPENSSL_CRYPTO
19-
# include <openssl/evp.h>
20-
#endif
21-
2218
hashkit_string_st *hashkit_encrypt(hashkit_st *kit, const char *source, size_t source_length) {
23-
#ifdef HAVE_OPENSSL_CRYPTO
24-
return aes_encrypt((encryption_context_t *) kit->_key,
25-
(const unsigned char *) source, source_length);
26-
#else
2719
return aes_encrypt((aes_key_t *) kit->_key, source,
2820
source_length);
29-
#endif
3021
}
3122

3223
hashkit_string_st *hashkit_decrypt(hashkit_st *kit, const char *source, size_t source_length) {
33-
#ifdef HAVE_OPENSSL_CRYPTO
34-
return aes_decrypt((encryption_context_t *) kit->_key,
35-
(const unsigned char *) source, source_length);
36-
#else
3724
return aes_decrypt((aes_key_t *)kit->_key, source, source_length);
38-
#endif
3925
}
4026

41-
#ifdef HAVE_OPENSSL_CRYPTO
42-
bool hashkit_key(hashkit_st *kit, const char *key, const size_t key_length) {
43-
kit->_key = (encryption_context_t *) malloc(sizeof(encryption_context_t));
44-
((encryption_context_t *) kit->_key)->encryption_context = EVP_CIPHER_CTX_new();
45-
((encryption_context_t *) kit->_key)->decryption_context = EVP_CIPHER_CTX_new();
46-
if (((encryption_context_t *) kit->_key)->encryption_context == NULL
47-
|| ((encryption_context_t *) kit->_key)->decryption_context == NULL)
48-
{
49-
return false;
50-
}
51-
return aes_initialize((const unsigned char *) key, key_length,
52-
(encryption_context_t *) kit->_key);
53-
}
54-
#else
5527
bool hashkit_key(hashkit_st *kit, const char *key, const size_t key_length) {
5628
if (kit->_key) {
57-
free(kit->_key);
29+
aes_free_key((aes_key_t *) kit->_key);
5830
}
5931

6032
kit->_key = aes_create_key(key, key_length);
6133

6234
return bool(kit->_key);
6335
}
64-
#endif

src/libhashkit/hashkit.cc

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515

1616
#include "libhashkit/common.h"
1717

18-
#ifdef HAVE_OPENSSL_CRYPTO
19-
# include <openssl/evp.h>
20-
#endif
21-
2218
static inline void _hashkit_init(hashkit_st *self) {
2319
self->base_hash.function = hashkit_one_at_a_time;
2420
self->base_hash.context = NULL;
@@ -56,26 +52,11 @@ hashkit_st *hashkit_create(hashkit_st *self) {
5652
return self;
5753
}
5854

59-
#ifdef HAVE_OPENSSL_CRYPTO
60-
static void cryptographic_context_free(encryption_context_t *context) {
61-
EVP_CIPHER_CTX_free(context->encryption_context);
62-
EVP_CIPHER_CTX_free(context->decryption_context);
63-
free(context);
64-
}
65-
#endif
66-
6755
void hashkit_free(hashkit_st *self) {
68-
#ifdef HAVE_OPENSSL_CRYPTO
6956
if (self and self->_key) {
70-
cryptographic_context_free((encryption_context_t *)self->_key);
57+
aes_free_key((aes_key_t *) self->_key);
7158
self->_key = NULL;
7259
}
73-
#else
74-
if (self and self->_key) {
75-
free(self->_key);
76-
self->_key = NULL;
77-
}
78-
#endif
7960

8061
if (hashkit_is_allocated(self)) {
8162
free(self);
@@ -98,21 +79,7 @@ hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *source) {
9879
destination->base_hash = source->base_hash;
9980
destination->distribution_hash = source->distribution_hash;
10081
destination->flags = source->flags;
101-
#ifdef HAVE_OPENSSL_CRYPTO
102-
if (destination->_key) {
103-
cryptographic_context_free((encryption_context_t *)destination->_key);
104-
destination->_key = NULL;
105-
}
106-
if (source->_key) {
107-
destination->_key =
108-
aes_clone_cryptographic_context(((encryption_context_t *) source->_key));
109-
if (destination->_key) {
110-
111-
}
112-
}
113-
#else
114-
destination->_key = aes_clone_key(static_cast<aes_key_t *>(source->_key));
115-
#endif
82+
destination->_key = aes_clone_key((aes_key_t *) source->_key);
11683

11784
return destination;
11885
}

0 commit comments

Comments
 (0)