26
26
#define AES_KEY_NBYTES 32
27
27
#define AES_IV_NBYTES 32
28
28
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) {
31
36
unsigned char aes_key[AES_KEY_NBYTES];
32
37
unsigned char aes_iv[AES_IV_NBYTES];
38
+ const unsigned char *ukey = (const unsigned char *) key;
39
+
33
40
if (!key) {
34
- return false ;
41
+ return NULL ;
35
42
}
36
43
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,
38
45
aes_key, aes_iv);
39
46
if (i != AES_KEY_NBYTES) {
40
- return false ;
47
+ return NULL ;
41
48
}
42
49
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 )
50
64
{
51
- return false ;
65
+ aes_free_key (aes_ctx);
66
+ return NULL ;
52
67
}
53
- return true ;
68
+
69
+ return aes_ctx;
54
70
}
55
71
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 ;
59
74
int cipher_length = source_length + EVP_CIPHER_CTX_block_size (encryption_context);
60
75
int final_length = 0 ;
76
+ const unsigned char *usource = (const unsigned char *) source;
61
77
unsigned char *cipher_text = (unsigned char *) malloc (cipher_length);
62
- if (cipher_text == NULL ) {
78
+ if (! cipher_text) {
63
79
return NULL ;
64
80
}
65
81
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
68
83
|| EVP_EncryptFinal_ex (encryption_context, cipher_text + cipher_length, &final_length) != 1 )
69
84
{
70
85
free (cipher_text);
71
86
return NULL ;
72
87
}
73
88
74
89
hashkit_string_st *destination = hashkit_string_create (cipher_length + final_length);
75
- if (destination == NULL ) {
90
+ if (! destination) {
76
91
return NULL ;
77
92
}
78
93
char *dest = hashkit_string_c_str_mutable (destination);
@@ -81,28 +96,25 @@ hashkit_string_st *aes_encrypt(encryption_context_t *crypto_context, const unsig
81
96
return destination;
82
97
}
83
98
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 ;
87
101
int plain_text_length = source_length;
88
102
int final_length = 0 ;
103
+ const unsigned char *usource = (const unsigned char *) source;
89
104
unsigned char *plain_text = (unsigned char *) malloc (plain_text_length);
90
- if (plain_text == NULL ) {
105
+ if (! plain_text) {
91
106
return NULL ;
92
107
}
93
108
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 )
99
111
{
100
112
free (plain_text);
101
113
return NULL ;
102
114
}
103
115
104
116
hashkit_string_st *destination = hashkit_string_create (plain_text_length + final_length);
105
- if (destination == NULL ) {
117
+ if (! destination) {
106
118
return NULL ;
107
119
}
108
120
char *dest = hashkit_string_c_str_mutable (destination);
@@ -111,22 +123,40 @@ hashkit_string_st *aes_decrypt(encryption_context_t *crypto_context, const unsig
111
123
return destination;
112
124
}
113
125
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) {
117
128
return NULL ;
129
+ }
118
130
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 );
124
141
}
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
+
127
143
return new_context;
128
144
}
129
145
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
+
130
160
#else
131
161
132
162
# include " libhashkit/rijndael.hpp"
@@ -172,7 +202,7 @@ aes_key_t *aes_create_key(const char *key, const size_t key_length) {
172
202
}
173
203
174
204
aes_key_t *aes_clone_key (aes_key_t *_aes_key) {
175
- if (_aes_key == NULL ) {
205
+ if (! _aes_key) {
176
206
return NULL ;
177
207
}
178
208
@@ -185,7 +215,7 @@ aes_key_t *aes_clone_key(aes_key_t *_aes_key) {
185
215
}
186
216
187
217
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) {
189
219
return NULL ;
190
220
}
191
221
@@ -214,7 +244,7 @@ hashkit_string_st *aes_encrypt(aes_key_t *_aes_key, const char *source, size_t s
214
244
}
215
245
216
246
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) {
218
248
return NULL ;
219
249
}
220
250
@@ -252,4 +282,11 @@ hashkit_string_st *aes_decrypt(aes_key_t *_aes_key, const char *source, size_t s
252
282
253
283
return destination;
254
284
}
285
+
286
+ void aes_free_key (aes_key_t *key) {
287
+ if (key) {
288
+ free (key);
289
+ }
290
+ }
291
+
255
292
#endif
0 commit comments