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;
397 static unsigned long get_thread_id();
398 static void locking_callback(
int mode,
int type,
const char *file,
int line);
409 return (
unsigned long)::GetCurrentThreadId();
417 if (mode & CRYPTO_LOCK)
418 openssl_mutexes[type].lock();
420 openssl_mutexes[type].unlock();
429 if (CRYPTO_get_id_callback() == NULL &&
430 CRYPTO_get_locking_callback() == NULL)
432 openssl_mutexes =
new boost::mutex[CRYPTO_num_locks()];
433 CRYPTO_set_id_callback(&get_thread_id);
434 CRYPTO_set_locking_callback(&locking_callback);
439 if (CRYPTO_get_id_callback() == &get_thread_id)
441 CRYPTO_set_id_callback(NULL);
442 CRYPTO_set_locking_callback(NULL);
443 delete[] openssl_mutexes;
444 openssl_mutexes =
nullptr;
bool exists(const path &p)
void init(const fc::sha256 &key, const uint128_t &init_value)
unsigned aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext)
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
static void locking_callback(int mode, int type, const char *file, int line)
unsigned aes_cfb_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext)
static boost::mutex * openssl_mutexes
Used to generate a useful error report when an exception is thrown.At each level in the stack where t...
openssl_thread_config openssl_thread_config_manager
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
static unsigned long get_thread_id()
uint64_t uint128_hi64(const uint128_t &x)
void aes_save(const fc::path &file, const fc::sha512 &key, std::vector< char > plain_text)
static thread & current()
void init(const fc::sha256 &key, const uint128_t &init_value)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
std::vector< char > aes_load(const fc::path &file, const fc::sha512 &key)
Defines exception's used by fc.
uint64_t uint128_lo64(const uint128_t &x)
unsigned aes_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext)
uint32_t decode(const char *ciphertxt, uint32_t len, char *plaintext)
wraps boost::filesystem::path to provide platform independent path manipulation.
uint32_t encode(const char *plaintxt, uint32_t len, char *ciphertxt)