6 #include <openssl/evp.h> 7 #include <openssl/aes.h> 15 madara::filters::AESBufferFilter::AESBufferFilter ()
16 : key_ (new unsigned char[32]), iv_ (new unsigned char[16])
18 memset ((
void *)key_.get_ptr (), 0, 32);
19 memset ((
void *)iv_.get_ptr (), 0, 16);
22 madara::filters::AESBufferFilter::AESBufferFilter (
23 const AESBufferFilter & input)
24 : key_ (new unsigned char[32]), iv_ (new unsigned char[16])
26 memcpy (key_.get_ptr (), input.key_.get_ptr (), 32);
27 memset ((
void *)iv_.get_ptr (), 0, 16);
30 madara::filters::AESBufferFilter::AESBufferFilter (
31 unsigned char * key,
int key_length)
32 : key_ (new unsigned char[32]), iv_ (new unsigned char[16])
34 memcpy (key_.get_ptr (), key, 32);
35 memset ((
void *)iv_.get_ptr (), 0, 16);
38 madara::filters::AESBufferFilter::~AESBufferFilter ()
42 int madara::filters::AESBufferFilter::generate_key (
50 i = EVP_BytesToKey (EVP_aes_256_cbc (), EVP_sha1 (),
51 (
unsigned char *)&salt, (
unsigned char *)password.c_str (),
52 (int)password.length (), rounds,
53 (
unsigned char *)key_.get_ptr (),
54 (
unsigned char *)iv_.get_ptr ());
59 " Unable to initialize 256 bit AES. Only received %d bytes.\n",
70 madara::filters::AESBufferFilter::encode (
71 unsigned char * source,
int size,
int max_size)
const 76 EVP_CIPHER_CTX * ctx = EVP_CIPHER_CTX_new ();
79 int ciphertext_len = 0;
82 size = std::min (size, max_size);
86 EVP_CIPHER_CTX_init (ctx);
89 result = EVP_EncryptInit_ex (ctx, EVP_aes_256_cbc (), NULL,
90 key_.get_ptr (), iv_.get_ptr ());
96 "AESBufferFilter::encode: Cannot init key/iv. Result=%d.\n",
104 result = EVP_EncryptUpdate (ctx,
112 "AESBufferFilter::encode: Cannot perform encrypt. Result=%d.\n",
118 ciphertext_len = len;
121 "AESBufferFilter::encode:EVP_EncryptUpdate:2: len=%d, ciphertext_len=%d.\n",
122 len, ciphertext_len);
126 result = EVP_EncryptFinal_ex (ctx, source + len, &len);
132 "AESBufferFilter::encode: Cannot finalize encrypt. Result=%d.\n",
138 ciphertext_len += len;
141 "AESBufferFilter::encode:EVP_EncryptFinal_ex: " 142 "len=%d, ciphertext_len=%d.\n",
143 len, ciphertext_len);
145 EVP_CIPHER_CTX_free (ctx);
147 return ciphertext_len;
151 madara::filters::AESBufferFilter::decode (
152 unsigned char * source,
int size,
int max_size)
const 157 EVP_CIPHER_CTX * ctx = EVP_CIPHER_CTX_new ();
160 int plaintext_len (0);
163 size = std::min (size, max_size);
166 EVP_CIPHER_CTX_init (ctx);
168 result = EVP_DecryptInit_ex (ctx, EVP_aes_256_cbc (), NULL,
169 key_.get_ptr (), iv_.get_ptr ());
175 "AESBufferFilter::decode: Cannot init key/iv. Result=%d.\n",
182 "AESBufferFilter::decode:EVP_DecryptUpdate:1: len=%d, size=%d.\n",
185 result = EVP_DecryptUpdate (ctx, source, &len,
193 "AESBufferFilter::decode: Cannot perform decrypt. Result=%d.\n",
200 "AESBufferFilter::decode:EVP_DecryptUpdate:2: len=%d, plaintext_len=%d.\n",
203 result = EVP_DecryptFinal_ex (ctx, source + len, &len);
209 "AESBufferFilter::decode: Cannot finalize decrypt. Result=%d.\n",
215 plaintext_len += len;
218 "AESBufferFilter::decode:EVP_DecryptFinal_ex: " 219 "len=%d, plaintext_len=%d.\n",
222 EVP_CIPHER_CTX_free (ctx);
224 return plaintext_len;
MADARA_Export utility::Refcounter< logger::Logger > global_logger
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.