1515
1616#include " libhashkit/common.h"
1717
18- #include " libhashkit/rijndael.hpp"
19-
2018#include < cstring>
2119
22- #define AES_KEY_LENGTH 256 /* 128, 192, 256 */
23- #define AES_BLOCK_SIZE 16
20+ #ifdef WITH_OPENSSL
21+
22+ #include < openssl/evp.h>
23+
24+ #define DIGEST_ROUNDS 5
25+
26+ #define AES_KEY_NBYTES 32
27+ #define AES_IV_NBYTES 32
28+
29+ bool aes_initialize (const unsigned char *key, const size_t key_length,
30+ encryption_context_t *crypto_context) {
31+ unsigned char aes_key[AES_KEY_NBYTES];
32+ unsigned char aes_iv[AES_IV_NBYTES];
33+ if (aes_key == NULL || aes_iv == NULL ) {
34+ return false ;
35+ }
36+
37+ int i = EVP_BytesToKey (EVP_aes_256_cbc (), EVP_sha256 (), NULL , key, key_length, DIGEST_ROUNDS,
38+ aes_key, aes_iv);
39+ if (i != AES_KEY_NBYTES) {
40+ return false ;
41+ }
42+
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+ {
51+ return false ;
52+ }
53+ return true ;
54+ }
55+
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 ;
59+ int cipher_length = source_length + EVP_CIPHER_CTX_block_size (encryption_context);
60+ int final_length = 0 ;
61+ unsigned char *cipher_text = (unsigned char *) malloc (cipher_length);
62+ if (cipher_text == NULL ) {
63+ return NULL ;
64+ }
65+ 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
68+ || EVP_EncryptFinal_ex (encryption_context, cipher_text + cipher_length, &final_length) != 1 )
69+ {
70+ free (cipher_text);
71+ return NULL ;
72+ }
73+
74+ hashkit_string_st *destination = hashkit_string_create (cipher_length + final_length);
75+ if (destination == NULL ) {
76+ return NULL ;
77+ }
78+ char *dest = hashkit_string_c_str_mutable (destination);
79+ memcpy (dest, cipher_text, cipher_length + final_length);
80+ hashkit_string_set_length (destination, cipher_length + final_length);
81+ return destination;
82+ }
83+
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 ;
87+ int plain_text_length = source_length;
88+ int final_length = 0 ;
89+ unsigned char *plain_text = (unsigned char *) malloc (plain_text_length);
90+ if (plain_text == NULL ) {
91+ return NULL ;
92+ }
93+ if (EVP_DecryptInit_ex (decryption_context, NULL , NULL , NULL , NULL ) != 1
94+ || EVP_DecryptUpdate (decryption_context, plain_text, &plain_text_length, source, source_length)
95+ != 1
96+ || EVP_DecryptFinal_ex (decryption_context, plain_text + plain_text_length, &final_length) != 1 )
97+ {
98+ free (plain_text);
99+ return NULL ;
100+ }
101+
102+ hashkit_string_st *destination = hashkit_string_create (plain_text_length + final_length);
103+ if (destination == NULL ) {
104+ return NULL ;
105+ }
106+ char *dest = hashkit_string_c_str_mutable (destination);
107+ memcpy (dest, plain_text, plain_text_length + final_length);
108+ hashkit_string_set_length (destination, plain_text_length + final_length);
109+ return destination;
110+ }
111+
112+ encryption_context_t *aes_clone_cryptographic_context (encryption_context_t *source) {
113+ encryption_context_t *new_context = (encryption_context_t *) malloc (sizeof (encryption_context_t ));
114+ if (new_context == NULL )
115+ return NULL ;
116+
117+ new_context->encryption_context = EVP_CIPHER_CTX_new ();
118+ new_context->decryption_context = EVP_CIPHER_CTX_new ();
119+ if (new_context->encryption_context == NULL || new_context->decryption_context == NULL ) {
120+ free (new_context);
121+ return NULL ;
122+ }
123+ EVP_CIPHER_CTX_copy (new_context->encryption_context , source->encryption_context );
124+ EVP_CIPHER_CTX_copy (new_context->decryption_context , source->decryption_context );
125+ return new_context;
126+ }
127+
128+ #else
129+
130+ # include " libhashkit/rijndael.hpp"
131+
132+ # define AES_KEY_LENGTH 256 /* 128, 192, 256 */
133+ # define AES_BLOCK_SIZE 16
24134
25135enum encrypt_t { AES_ENCRYPT, AES_DECRYPT };
26136
@@ -49,7 +159,7 @@ aes_key_t *aes_create_key(const char *key, const size_t key_length) {
49159 if (ptr == rkey_end) {
50160 ptr = rkey; /* Just loop over tmp_key until we used all key */
51161 }
52- *ptr ^= (uint8_t )(*sptr);
162+ *ptr ^= (uint8_t ) (*sptr);
53163 }
54164
55165 _aes_key->decode_key .nr = rijndaelKeySetupDec (_aes_key->decode_key .rk , rkey, AES_KEY_LENGTH);
@@ -140,3 +250,4 @@ hashkit_string_st *aes_decrypt(aes_key_t *_aes_key, const char *source, size_t s
140250
141251 return destination;
142252}
253+ #endif
0 commit comments