11#include <openssl/opensslconf.h>
12#ifndef OPENSSL_THREADS
13# error "OpenSSL must be configured to support threads"
15#include <openssl/crypto.h>
36 my->ctx.obj = EVP_CIPHER_CTX_new();
41 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
49 if(1 != EVP_EncryptInit_ex(my->ctx, EVP_aes_256_cbc(), NULL, (
unsigned char*)&key, (
unsigned char*)&init_value))
52 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
54 EVP_CIPHER_CTX_set_padding( my->ctx, 0 );
59 int ciphertext_len = 0;
63 if(1 != EVP_EncryptUpdate(my->ctx, (
unsigned char*)ciphertxt, &ciphertext_len, (
const unsigned char*)plaintxt, plaintext_len))
66 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
68 FC_ASSERT( ciphertext_len ==
static_cast<int>(plaintext_len),
"", (
"ciphertext_len",ciphertext_len)(
"plaintext_len",plaintext_len) );
69 return ciphertext_len;
72uint32_t aes_encoder::final_encode(
char* ciphertxt )
74 int ciphertext_len = 0;
78 if(1 != EVP_EncryptFinal_ex(my->ctx, (
unsigned char*)ciphertxt, &ciphertext_len))
81 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
83 return ciphertext_len;
95 my->ctx.obj = EVP_CIPHER_CTX_new();
100 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
108 if(1 != EVP_DecryptInit_ex(my->ctx, EVP_aes_256_cbc(), NULL, (
unsigned char*)&key, (
unsigned char*)&init_value))
111 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
113 EVP_CIPHER_CTX_set_padding( my->ctx, 0 );
118 int plaintext_len = 0;
122 if (1 != EVP_DecryptUpdate(my->ctx, (
unsigned char*)plaintext, &plaintext_len, (
const unsigned char*)ciphertxt, ciphertxt_len))
125 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
127 FC_ASSERT( ciphertxt_len ==
static_cast<unsigned>(plaintext_len),
"", (
"ciphertxt_len",ciphertxt_len)(
"plaintext_len",plaintext_len) );
128 return plaintext_len;
131uint32_t aes_decoder::final_decode(
char* plaintext )
134 int ciphertext_len = 0;
138 if(1 != EVP_DecryptFinal_ex(my->ctx, (
unsigned char*)plaintext, &ciphertext_len))
141 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
143 return ciphertext_len;
159unsigned aes_encrypt(
unsigned char *plaintext,
int plaintext_len,
unsigned char *key,
160 unsigned char *iv,
unsigned char *ciphertext)
162 evp_cipher_ctx ctx( EVP_CIPHER_CTX_new() );
165 unsigned ciphertext_len = 0;
171 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
179 if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
182 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
188 if(1 != EVP_EncryptUpdate(ctx, ciphertext, &
len, plaintext, plaintext_len))
191 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
193 ciphertext_len =
len;
198 if(1 != EVP_EncryptFinal_ex(ctx, ciphertext +
len, &
len))
201 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
203 ciphertext_len +=
len;
205 return ciphertext_len;
208unsigned aes_decrypt(
unsigned char *ciphertext,
int ciphertext_len,
unsigned char *key,
209 unsigned char *iv,
unsigned char *plaintext)
211 evp_cipher_ctx ctx( EVP_CIPHER_CTX_new() );
213 unsigned plaintext_len = 0;
219 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
227 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
230 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
236 if(1 != EVP_DecryptUpdate(ctx, plaintext, &
len, ciphertext, ciphertext_len))
239 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
247 if(1 != EVP_DecryptFinal_ex(ctx, plaintext +
len, &
len))
250 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
252 plaintext_len +=
len;
254 return plaintext_len;
257unsigned aes_cfb_decrypt(
unsigned char *ciphertext,
int ciphertext_len,
unsigned char *key,
258 unsigned char *iv,
unsigned char *plaintext)
260 evp_cipher_ctx ctx( EVP_CIPHER_CTX_new() );
262 unsigned plaintext_len = 0;
268 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
276 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cfb128(), NULL, key, iv))
279 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
285 if(1 != EVP_DecryptUpdate(ctx, plaintext, &
len, ciphertext, ciphertext_len))
288 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
296 if(1 != EVP_DecryptFinal_ex(ctx, plaintext +
len, &
len))
299 (
"s", ERR_error_string( ERR_get_error(),
nullptr) ) );
301 plaintext_len +=
len;
303 return plaintext_len;
308 std::vector<char> cipher_text(plain_text.size()+16);
309 auto cipher_len =
aes_encrypt( (
unsigned char*)plain_text.data(), (
int)plain_text.size(),
310 (
unsigned char*)&key, ((
unsigned char*)&key)+32,
311 (
unsigned char*)cipher_text.data() );
312 FC_ASSERT( cipher_len <= cipher_text.size() );
313 cipher_text.resize(cipher_len);
319 std::vector<char> plain_text( cipher_text.size() );
320 auto plain_len =
aes_decrypt( (
unsigned char*)cipher_text.data(), (
int)cipher_text.size(),
321 (
unsigned char*)&key, ((
unsigned char*)&key)+32,
322 (
unsigned char*)plain_text.data() );
323 plain_text.resize(plain_len);
337 auto check = check_enc.
result();
339 std::ofstream out(file.generic_string().c_str());
351 std::ifstream in( file.generic_string().c_str(), std::ifstream::binary );
353 std::vector<char> cipher;
uint32_t decode(const char *ciphertxt, uint32_t len, char *plaintext)
void init(const fc::sha256 &key, const fc::uint128 &init_value)
void init(const fc::sha256 &key, const fc::uint128 &init_value)
uint32_t encode(const char *plaintxt, uint32_t len, char *ciphertxt)
Used to generate a useful error report when an exception is thrown.
wraps boost::filesystem::path to provide platform independent path manipulation.
an implementation of 128 bit unsigned integer
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
void unpack(Stream &s, std::deque< T > &value)
void pack(Stream &s, const std::deque< T > &value)
unsigned aes_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext)
unsigned aes_cfb_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext)
bool exists(const path &p)
unsigned aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext)
void aes_save(const fc::path &file, const fc::sha512 &key, std::vector< char > plain_text)
std::vector< char > aes_load(const fc::path &file, const fc::sha512 &key)