@ -191,28 +191,28 @@ Status OpensslCipherStream::Encrypt(uint64_t fileOffset, char* data,
EVP_CIPHER_CTX_set_padding ( ctx_ , 0 ) ;
EVP_CIPHER_CTX_set_padding ( ctx_ , 0 ) ;
if ( offset = = 0 ) {
if ( offset = = 0 ) {
unsigned char * out = ( unsigned char * ) malloc ( dataSize ) ;
//unsigned char *out = (unsigned char*)malloc(dataSize);
if ( 1 ! = EVP_EncryptUpdate ( ctx_ , out , & len , reinterpret_cast < const unsigned char * > ( data ) , static_cast < int > ( dataSize ) ) ) { err_str = " Failed to encrypt. " ; goto error ; }
if ( 1 ! = EVP_EncryptUpdate ( ctx_ , reinterpret_cast < unsigned char * > ( data ) , & len , reinterpret_cast < const unsigned char * > ( data ) , static_cast < int > ( dataSize ) ) ) { err_str = " Failed to encrypt. " ; goto error ; }
memcpy ( data , out , dataSize ) ;
//memcpy(data, out, dataSize);
//EVP_EncryptFinal_ex(ctx_, reinterpret_cast<unsigned char *>(data) + len, &len);
//EVP_EncryptFinal_ex(ctx_, reinterpret_cast<unsigned char *>(data) + len, &len);
} else {
} else {
unsigned char zero_block [ kBlockSize ] { 0 } ;
unsigned char zero_block [ kBlockSize ] { 0 } ;
unsigned char zero_block_out [ kBlockSize ] { 0 } ;
//unsigned char zero_block_out[kBlockSize]{0};
if ( 1 ! = EVP_EncryptUpdate ( ctx_ , zero_block_out , & len , zero_block , static_cast < int > ( kBlockSize ) ) ) { err_str = " Failed to encrypt zero block. " ; goto error ; }
if ( 1 ! = EVP_EncryptUpdate ( ctx_ , zero_block , & len , zero_block , static_cast < int > ( kBlockSize ) ) ) { err_str = " Failed to encrypt zero block. " ; goto error ; }
//unsigned char * end = reinterpret_cast<unsigned char *>(zero_block) + len;
//unsigned char * end = reinterpret_cast<unsigned char *>(zero_block) + len;
size_t n = std : : min ( kBlockSize - offset , dataSize ) ;
size_t n = std : : min ( kBlockSize - offset , dataSize ) ;
for ( size_t i = 0 ; i < n ; + + i ) data [ i ] ^ = zero_block_out [ offset + i ] ;
for ( size_t i = 0 ; i < n ; + + i ) data [ i ] ^ = zero_block [ offset + i ] ;
//memset(zero_block, 0, kBlockSize);
memset ( zero_block , 0 , kBlockSize ) ;
n = kBlockSize - offset ;
n = kBlockSize - offset ;
if ( dataSize > n ) {
if ( dataSize > n ) {
char * ptr = ( char * ) ( data + n ) ;
char * ptr = ( char * ) ( data + n ) ;
unsigned char * out = ( unsigned char * ) malloc ( dataSize - n ) ;
//unsigned char *out = (unsigned char*)malloc(dataSize - n);
if ( 1 ! = EVP_EncryptUpdate ( ctx_ , out , & len , reinterpret_cast < const unsigned char * > ( ptr ) , static_cast < int > ( dataSize - n ) ) ) { err_str = " Failed to encrypt remaining. " ; goto error ; }
if ( 1 ! = EVP_EncryptUpdate ( ctx_ , reinterpret_cast < unsigned char * > ( ptr ) , & len , reinterpret_cast < const unsigned char * > ( ptr ) , static_cast < int > ( dataSize - n ) ) ) { err_str = " Failed to encrypt remaining. " ; goto error ; }
memcpy ( ptr , out , dataSize - n ) ;
//memcpy(ptr, out, dataSize - n);
//end = reinterpret_cast<unsigned char *>(ptr) + len;
//end = reinterpret_cast<unsigned char *>(ptr) + len;
}
}
@ -270,7 +270,7 @@ Status OpensslProvider::AddCipher(const std::string& /*descriptor*/,
Status OpensslProvider : : CreateNewPrefix ( const std : : string & /*fname*/ ,
Status OpensslProvider : : CreateNewPrefix ( const std : : string & /*fname*/ ,
char * prefix , size_t prefixLength ) const {
char * prefix , size_t prefixLength ) const {
if ( 1 ! = RAND_bytes ( reinterpret_cast < unsigned char * > ( prefix ) , static_cast < int > ( OpensslCipherStream : : kBlockSize ) ) ) {
if ( 1 ! = RAND_bytes ( reinterpret_cast < unsigned char * > ( prefix ) , static_cast < int > ( prefixLength ) ) ) {
return handleErrors ( " Failed to get random numbers. " ) ; //Status::Aborted(ERR_reason_error_string(ERR_get_error()));
return handleErrors ( " Failed to get random numbers. " ) ; //Status::Aborted(ERR_reason_error_string(ERR_get_error()));
}
}
//if( 1 != EVP_CIPHER_up_ref(aes_cipher_)) return handleErrors("Failed to create OpensslCipherStream.");
//if( 1 != EVP_CIPHER_up_ref(aes_cipher_)) return handleErrors("Failed to create OpensslCipherStream.");