6 #include <openssl/evp.h> 7 #include <openssl/aes.h> 15 : key_ (new unsigned char[32]), iv_ (new unsigned char[16])
23 :
key_ (new unsigned char[32]),
iv_ (new unsigned char[16])
30 unsigned char * key,
int key_length)
31 :
key_ (new unsigned char[32]),
iv_ (new unsigned char[16])
33 memcpy (
key_.
get_ptr (), key, std::min (32, key_length));
49 i = EVP_BytesToKey (EVP_aes_256_cbc (), EVP_sha1 (),
50 (
unsigned char *)&salt, (
unsigned char *)password.c_str (),
51 (int)password.length (), rounds,
58 " Unable to initialize 256 bit AES. Only received %d bytes.\n",
70 unsigned char * source,
int size,
int max_size)
const 75 EVP_CIPHER_CTX * ctx = EVP_CIPHER_CTX_new ();
78 int ciphertext_len = 0;
81 size = std::min (size, max_size);
85 EVP_CIPHER_CTX_init (ctx);
88 result = EVP_EncryptInit_ex (ctx, EVP_aes_256_cbc (), NULL,
95 "AESBufferFilter::encode: Cannot init key/iv. Result=%d.\n",
103 result = EVP_EncryptUpdate (ctx,
111 "AESBufferFilter::encode: Cannot perform encrypt. Result=%d.\n",
117 ciphertext_len = len;
120 "AESBufferFilter::encode:EVP_EncryptUpdate:2: len=%d, ciphertext_len=%d.\n",
121 len, ciphertext_len);
125 result = EVP_EncryptFinal_ex (ctx, source + len, &len);
131 "AESBufferFilter::encode: Cannot finalize encrypt. Result=%d.\n",
137 ciphertext_len += len;
140 "AESBufferFilter::encode:EVP_EncryptFinal_ex: " 141 "len=%d, ciphertext_len=%d.\n",
142 len, ciphertext_len);
144 EVP_CIPHER_CTX_free (ctx);
146 return ciphertext_len;
151 unsigned char * source,
int size,
int max_size)
const 156 EVP_CIPHER_CTX * ctx = EVP_CIPHER_CTX_new ();
159 int plaintext_len (0);
162 size = std::min (size, max_size);
165 EVP_CIPHER_CTX_init (ctx);
167 result = EVP_DecryptInit_ex (ctx, EVP_aes_256_cbc (), NULL,
174 "AESBufferFilter::decode: Cannot init key/iv. Result=%d.\n",
181 "AESBufferFilter::decode:EVP_DecryptUpdate:1: len=%d, size=%d.\n",
184 result = EVP_DecryptUpdate (ctx, source, &len,
192 "AESBufferFilter::decode: Cannot perform decrypt. Result=%d.\n",
199 "AESBufferFilter::decode:EVP_DecryptUpdate:2: len=%d, plaintext_len=%d.\n",
202 result = EVP_DecryptFinal_ex (ctx, source + len, &len);
208 "AESBufferFilter::decode: Cannot finalize decrypt. Result=%d.\n",
214 plaintext_len += len;
217 "AESBufferFilter::decode:EVP_DecryptFinal_ex: " 218 "len=%d, plaintext_len=%d.\n",
221 EVP_CIPHER_CTX_free (ctx);
223 return plaintext_len;
virtual int encode(unsigned char *source, int size, int max_size) const
Encodes the buffer in place using AES encryption.
utility::ScopedArray< unsigned char > key_
the user's cypher key
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
int generate_key(const std::string &password)
Generates a 256 bit key from a password.
virtual int decode(unsigned char *source, int size, int max_size) const
Decodes the buffer in place using AES encryption.
T * get_ptr(void)
get the underlying pointer
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
AESBufferFilter()
Constructor.
virtual ~AESBufferFilter()
Destructor.
utility::ScopedArray< unsigned char > iv_
initialization vector
Encrypts a buffer with 256 bit AES via OpenSSL.