From 68b9fafe9a418a7b37dfc903c1d38a80ffd8974d Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 5 Sep 2013 22:09:12 -0400 Subject: [PATCH 1/2] disable padding for aes_encoder and decoder --- CMakeLists.txt | 4 ++-- include/fc/crypto/aes.hpp | 8 ++++---- src/crypto/aes.cpp | 21 +++++++++++++++------ tests/aes_test.cpp | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3420b48..3846c18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,8 +141,8 @@ set( BOOST_LIBRARIES ${Boost_THREAD_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_FIL #add_executable( test_compress tests/compress.cpp ) #target_link_libraries( test_compress fc ${BOOST_LIBRARIES} ) -#add_executable( test_aes tests/aes_test.cpp ) -#target_link_libraries( test_aes fc ${BOOST_LIBRARIES} ) +add_executable( test_aes tests/aes_test.cpp ) +target_link_libraries( test_aes fc ${BOOST_LIBRARIES} ) #add_executable( test_sleep tests/sleep.cpp ) #target_link_libraries( test_sleep fc ${BOOST_LIBRARIES} ) diff --git a/include/fc/crypto/aes.hpp b/include/fc/crypto/aes.hpp index d551667..2b2130d 100644 --- a/include/fc/crypto/aes.hpp +++ b/include/fc/crypto/aes.hpp @@ -15,8 +15,8 @@ namespace fc { ~aes_encoder(); void init( const fc::sha256& key, const fc::uint128& init_value ); - uint32_t encode( const char* plaintxt, uint32_t len, const char* ciphertxt ); - uint32_t final_encode( const char* ciphertxt ); + uint32_t encode( const char* plaintxt, uint32_t len, char* ciphertxt ); + // uint32_t final_encode( char* ciphertxt ); private: struct impl; @@ -29,8 +29,8 @@ namespace fc { ~aes_decoder(); void init( const fc::sha256& key, const fc::uint128& init_value ); - uint32_t encode( const char* plaintxt, uint32_t len, const char* ciphertxt ); - uint32_t final_encode( const char* ciphertxt ); + uint32_t decode( const char* ciphertxt, uint32_t len, char* plaintext ); +// uint32_t final_decode( char* plaintext ); private: struct impl; diff --git a/src/crypto/aes.cpp b/src/crypto/aes.cpp index b5a82aa..eb5a5f7 100644 --- a/src/crypto/aes.cpp +++ b/src/crypto/aes.cpp @@ -39,9 +39,10 @@ void aes_encoder::init( const fc::sha256& key, const fc::uint128& init_value ) FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc encryption init", ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); } + EVP_CIPHER_CTX_set_padding( my->ctx, 0 ); } -uint32_t aes_encoder::encode( const char* plaintxt, uint32_t plaintext_len, const char* ciphertxt ) +uint32_t aes_encoder::encode( const char* plaintxt, uint32_t plaintext_len, char* ciphertxt ) { int ciphertext_len = 0; /* Provide the message to be encrypted, and obtain the encrypted output. @@ -52,9 +53,11 @@ uint32_t aes_encoder::encode( const char* plaintxt, uint32_t plaintext_len, cons FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc encryption update", ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); } + FC_ASSERT( ciphertext_len == plaintext_len ); return ciphertext_len; } -uint32_t aes_encoder::final_encode( const char* ciphertxt ) +#if 0 +uint32_t aes_encoder::final_encode( char* ciphertxt ) { int ciphertext_len = 0; /* Finalise the encryption. Further ciphertext bytes may be written at @@ -67,6 +70,7 @@ uint32_t aes_encoder::final_encode( const char* ciphertxt ) } return ciphertext_len; } +#endif struct aes_decoder::impl @@ -95,37 +99,42 @@ void aes_decoder::init( const fc::sha256& key, const fc::uint128& init_value ) FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc encryption init", ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); } + EVP_CIPHER_CTX_set_padding( my->ctx, 0 ); } aes_decoder::~aes_decoder() { } -uint32_t aes_decoder::encode( const char* plaintxt, uint32_t plaintext_len, const char* ciphertxt ) +uint32_t aes_decoder::decode( const char* ciphertxt, uint32_t plaintext_len, char* plaintext ) { int ciphertext_len = 0; /* Provide the message to be encrypted, and obtain the encrypted output. * * EVP_DecryptUpdate can be called multiple times if necessary * */ - if(1 != EVP_DecryptUpdate(my->ctx, (unsigned char*)ciphertxt, &ciphertext_len, (const unsigned char*)plaintxt, plaintext_len)) + if(1 != EVP_DecryptUpdate(my->ctx, (unsigned char*)plaintext, &ciphertext_len, (const unsigned char*)ciphertxt, plaintext_len)) { FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc encryption update", ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); } + FC_ASSERT( ciphertext_len == plaintext_len ); return ciphertext_len; } -uint32_t aes_decoder::final_encode( const char* ciphertxt ) +#if 0 +uint32_t aes_decoder::final_decode( char* plaintext ) { + return 0; int ciphertext_len = 0; /* Finalise the encryption. Further ciphertext bytes may be written at * * this stage. * */ - if(1 != EVP_DecryptFinal_ex(my->ctx, (unsigned char*)ciphertxt, &ciphertext_len)) + if(1 != EVP_DecryptFinal_ex(my->ctx, (unsigned char*)plaintext, &ciphertext_len)) { FC_THROW_EXCEPTION( exception, "error durring aes 256 cbc encryption final", ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); } return ciphertext_len; } +#endif diff --git a/tests/aes_test.cpp b/tests/aes_test.cpp index 00fc016..45ad343 100644 --- a/tests/aes_test.cpp +++ b/tests/aes_test.cpp @@ -1,7 +1,9 @@ #include #include +#include #include +#include int main( int argc, char** ) { std::string line; @@ -20,6 +22,19 @@ int main( int argc, char** ) std::cout<<"dcrypt.size: '"< Date: Thu, 5 Sep 2013 22:12:00 -0400 Subject: [PATCH 2/2] fix unit test --- tests/aes_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/aes_test.cpp b/tests/aes_test.cpp index 45ad343..4b85134 100644 --- a/tests/aes_test.cpp +++ b/tests/aes_test.cpp @@ -29,7 +29,7 @@ int main( int argc, char** ) fc::aes_encoder enc; enc.init( fc::sha256::hash((char*)&key,sizeof(key) ), fc::city_hash_crc_128( (char*)&key, sizeof(key) ) ); auto len = enc.encode( dcrypt.data(), dcrypt.size(), crypt.data() ); - enc.final_encode( crypt.data() + len ); + // enc.final_encode( crypt.data() + len ); std::cout<<"crypt: "<