7 #include <openssl/rand.h>
8 #include <openssl/err.h>
9 #include <openssl/aes.h>
10 #include <openssl/md5.h>
21 OpenSSL_add_all_algorithms();
35 MD5_Update( &ctx, _buf, _sz );
36 unsigned char hash[16];
37 MD5_Final( hash, &ctx );
40 for (
int i = 0; i < 16; ++i ) {
41 ss << std::setfill(
'0' ) << std::setw( 2 ) << std::hex << (
int )hash[i];
52 num_hash_rounds_( 16 ),
53 algorithm_(
"aes-256-cbc" ) {
62 salt_size_( _salt_sz ),
63 num_hash_rounds_( _num_rnds ),
104 _out_key.resize( _key_size );
105 const int rnd_err = RAND_bytes( &_out_key[0], _key_size );
106 if ( 1 != rnd_err ) {
108 ERR_error_string_n( ERR_get_error(), err,
sizeof( err ) );
109 const std::string msg = std::string(
"failed in RAND_bytes - " ) + err;
110 return ERROR( ERR_get_error(), msg );
121 std::string& _out_str ) {
122 std::stringstream ss;
123 for ( irods::buffer_crypt::array_t::size_type i = 0; i < _in_buf.size(); ++i ) {
124 ss << std::setfill( '0' ) << std::setw( 2 ) << std::hex << static_cast<unsigned int>( _in_buf[i] );
138 unsigned char* iv =
new unsigned char[
key_size_ ];
139 int rnd_err = RAND_bytes(
142 if ( 1 != rnd_err ) {
145 ERR_error_string_n( ERR_get_error(), err, 256 );
146 std::string msg(
"failed in RAND_bytes - " );
148 return ERROR( ERR_get_error(), msg );
176 auto* context = EVP_CIPHER_CTX_new();
178 auto* algo = EVP_get_cipherbyname(
algorithm_.c_str() );
182 "buffer_crypt::encrypt - algorithm not supported [%s]",
185 algo = EVP_aes_256_cbc();
188 int ret = EVP_EncryptInit_ex(
196 ERR_error_string_n( ERR_get_error(), err, 256 );
197 std::string msg(
"failed in EVP_EncryptInit_ex - " );
199 return ERROR( ERR_get_error(), msg );
205 int cipher_len = _in_buf.size() + AES_BLOCK_SIZE;
206 unsigned char* cipher_text =
new unsigned char[ cipher_len ] ;
209 ret = EVP_EncryptUpdate(
216 delete [] cipher_text;
218 ERR_error_string_n( ERR_get_error(), err, 256 );
219 std::string msg(
"failed in EVP_EncryptUpdate - " );
221 return ERROR( ERR_get_error(), msg );
227 ret = EVP_EncryptFinal_ex(
229 cipher_text + cipher_len,
232 delete [] cipher_text;
234 ERR_error_string_n( ERR_get_error(), err, 256 );
235 std::string msg(
"failed in EVP_EncryptFinal_ex - " );
237 return ERROR( ERR_get_error(), msg );
242 _out_buf.resize( cipher_len + final_len );
248 &cipher_text[ cipher_len + final_len ] );
250 delete [] cipher_text;
252 EVP_CIPHER_CTX_free( context );
267 auto* context = EVP_CIPHER_CTX_new();
269 auto* algo = EVP_get_cipherbyname(
algorithm_.c_str() );
273 "buffer_crypt::decrypt - algorithm not supported [%s]",
276 algo = EVP_aes_256_cbc();
279 int ret = EVP_DecryptInit_ex(
287 ERR_error_string_n( ERR_get_error(), err, 256 );
288 std::string msg(
"failed in EVP_DecryptInit_ex - " );
290 return ERROR( ERR_get_error(), msg );
297 auto plain_text = std::make_unique<unsigned char[]>(_in_buf.size() + AES_BLOCK_SIZE);
301 ret = EVP_DecryptUpdate(
309 ERR_error_string_n( ERR_get_error(), err, 256 );
310 std::string msg(
"failed in EVP_DecryptUpdate - " );
312 return ERROR( ERR_get_error(), msg );
318 ret = EVP_DecryptFinal_ex(
320 plain_text.get() + plain_len,
324 ERR_error_string_n( ERR_get_error(), err, 256 );
325 std::string msg(
"failed in EVP_DecryptFinal_ex - " );
327 return ERROR( ERR_get_error(), msg );
332 _out_buf.resize( plain_len + final_len );
338 plain_text.get() + plain_len + final_len );
340 EVP_CIPHER_CTX_free( context );