12 #include <boost/endian/buffers.hpp>
13 #include <boost/thread/mutex.hpp>
14 #include <openssl/opensslconf.h>
15 #ifndef OPENSSL_THREADS
16 # error "OpenSSL must be configured to support threads"
18 #include <openssl/crypto.h>
43 my->ctx.obj = EVP_CIPHER_CTX_new();
48 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
56 boost::endian::little_uint64_buf_t iv[2];
59 if(1 != EVP_EncryptInit_ex(my->ctx, EVP_aes_256_cbc(), NULL, (
unsigned char*)&key, (
const unsigned char*)iv[0].data()))
62 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
64 EVP_CIPHER_CTX_set_padding( my->ctx, 0 );
69 int ciphertext_len = 0;
73 if(1 != EVP_EncryptUpdate(my->ctx, (
unsigned char*)ciphertxt, &ciphertext_len, (
const unsigned char*)plaintxt, plaintext_len))
76 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
78 FC_ASSERT( (uint32_t) ciphertext_len == plaintext_len,
"", (
"ciphertext_len",ciphertext_len)(
"plaintext_len",plaintext_len) );
79 return ciphertext_len;
82 uint32_t aes_encoder::final_encode(
char* ciphertxt )
84 int ciphertext_len = 0;
88 if(1 != EVP_EncryptFinal_ex(my->ctx, (
unsigned char*)ciphertxt, &ciphertext_len))
91 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
93 return ciphertext_len;
111 my->ctx.obj = EVP_CIPHER_CTX_new();
116 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
124 boost::endian::little_uint64_buf_t iv[2];
127 if(1 != EVP_DecryptInit_ex(my->ctx, EVP_aes_256_cbc(), NULL, (
unsigned char*)&key, (
const unsigned char*)iv[0].data()))
130 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
132 EVP_CIPHER_CTX_set_padding( my->ctx, 0 );
140 int plaintext_len = 0;
144 if (1 != EVP_DecryptUpdate(my->ctx, (
unsigned char*)plaintext, &plaintext_len, (
const unsigned char*)ciphertxt, ciphertxt_len))
147 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
149 FC_ASSERT( ciphertxt_len == (uint32_t)plaintext_len,
"", (
"ciphertxt_len",ciphertxt_len)(
"plaintext_len",plaintext_len) );
150 return plaintext_len;
153 uint32_t aes_decoder::final_decode(
char* plaintext )
156 int ciphertext_len = 0;
160 if(1 != EVP_DecryptFinal_ex(my->ctx, (
unsigned char*)plaintext, &ciphertext_len))
163 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
165 return ciphertext_len;
181 unsigned aes_encrypt(
unsigned char *plaintext,
int plaintext_len,
unsigned char *key,
182 unsigned char *iv,
unsigned char *ciphertext)
184 evp_cipher_ctx ctx( EVP_CIPHER_CTX_new() );
187 unsigned ciphertext_len = 0;
193 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
201 if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
204 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
210 if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
213 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
215 ciphertext_len = len;
220 if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
223 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
225 ciphertext_len += len;
227 return ciphertext_len;
230 unsigned aes_decrypt(
unsigned char *ciphertext,
int ciphertext_len,
unsigned char *key,
231 unsigned char *iv,
unsigned char *plaintext)
233 evp_cipher_ctx ctx( EVP_CIPHER_CTX_new() );
235 unsigned plaintext_len = 0;
241 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
249 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
252 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
258 if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
261 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
269 if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
272 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
274 plaintext_len += len;
276 return plaintext_len;
279 unsigned aes_cfb_decrypt(
unsigned char *ciphertext,
int ciphertext_len,
unsigned char *key,
280 unsigned char *iv,
unsigned char *plaintext)
282 evp_cipher_ctx ctx( EVP_CIPHER_CTX_new() );
284 unsigned plaintext_len = 0;
290 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
298 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cfb128(), NULL, key, iv))
301 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
307 if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
310 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
318 if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
321 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
323 plaintext_len += len;
325 return plaintext_len;
330 std::vector<char> cipher_text(plain_text.size()+16);
331 auto cipher_len =
aes_encrypt( (
unsigned char*)plain_text.data(), (
int)plain_text.size(),
332 (
unsigned char*)&key, ((
unsigned char*)&key)+32,
333 (
unsigned char*)cipher_text.
data() );
334 FC_ASSERT( cipher_len <= cipher_text.size() );
335 cipher_text.resize(cipher_len);
341 std::vector<char> plain_text( cipher_text.size() );
342 auto plain_len =
aes_decrypt( (
unsigned char*)cipher_text.data(), (
int)cipher_text.size(),
343 (
unsigned char*)&key, ((
unsigned char*)&key)+32,
344 (
unsigned char*)plain_text.
data() );
345 plain_text.resize(plain_len);
359 auto check = check_enc.
result();
375 std::vector<char> cipher;
409 return (
unsigned long)::GetCurrentThreadId();
417 if (
mode & CRYPTO_LOCK)
429 if (CRYPTO_get_id_callback() == NULL &&
430 CRYPTO_get_locking_callback() == NULL)
441 CRYPTO_set_id_callback(NULL);
442 CRYPTO_set_locking_callback(NULL);