Merge branch 'phoenix' of https://github.com/InvictusInnovations/fc into phoenix

This commit is contained in:
dnotestein 2013-09-07 10:55:22 -04:00
commit 0931a5bef6
4 changed files with 36 additions and 12 deletions

View file

@ -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} )

View file

@ -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;

View file

@ -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

View file

@ -1,7 +1,9 @@
#include <iostream>
#include <fc/crypto/aes.hpp>
#include <fc/crypto/city.hpp>
#include <fc/exception/exception.hpp>
#include <fc/variant.hpp>
int main( int argc, char** )
{
std::string line;
@ -20,6 +22,19 @@ int main( int argc, char** )
std::cout<<"dcrypt.size: '"<<dcrypt.size()<<"'\n";
std::cout<<"line: '"<<line<<"'\n";
std::cout<<"dcrypt: '"<<dcrypt.data()<<"'\n";
std::cout<<"dcrypt: "<<fc::variant(dcrypt).as_string()<<"\n";
std::cout<<"crypt: "<<fc::variant(crypt).as_string()<<"\n";
memset( crypt.data(), 0, crypt.size() );
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 );
std::cout<<"crypt: "<<fc::variant(crypt).as_string()<<"\n";
fc::aes_decoder dec;
dec.init( fc::sha256::hash((char*)&key,sizeof(key) ), fc::city_hash_crc_128( (char*)&key, sizeof(key) ) );
}
catch ( fc::exception& e )
{