From 5bf5cf921978de11017e2f5edce3317e3ad9506a Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Sat, 31 Aug 2013 13:12:55 -0400 Subject: [PATCH] adding aes_load and aes_save methods --- include/fc/crypto/aes.hpp | 13 +++++++++- include/fc/crypto/ripemd160.hpp | 2 ++ src/crypto/aes.cpp | 43 +++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/include/fc/crypto/aes.hpp b/include/fc/crypto/aes.hpp index befeec8..e1b2324 100644 --- a/include/fc/crypto/aes.hpp +++ b/include/fc/crypto/aes.hpp @@ -6,6 +6,7 @@ #include namespace fc { + class path; class aes_encoder { @@ -27,4 +28,14 @@ namespace fc { std::vector aes_encrypt( const fc::sha512& key, const std::vector& plain_text ); std::vector aes_decrypt( const fc::sha512& key, const std::vector& cipher_text ); -} + /** encrypts plain_text and then includes a checksum that enables us to verify the integrety of + * the file / key prior to decryption. + */ + void aes_save( const fc::path& file, const fc::sha512& key, std::vector plain_text ); + + /** + * recovers the plain_text saved via aes_save() + */ + std::vector aes_load( const fc::path& file, const fc::sha512& key ); + +} // namespace fc diff --git a/include/fc/crypto/ripemd160.hpp b/include/fc/crypto/ripemd160.hpp index dbc7d56..fa84df6 100644 --- a/include/fc/crypto/ripemd160.hpp +++ b/include/fc/crypto/ripemd160.hpp @@ -72,6 +72,8 @@ class ripemd160 void to_variant( const ripemd160& bi, variant& v ); void from_variant( const variant& v, ripemd160& bi ); + typedef ripemd160 uint160_t; + } // namespace fc namespace std diff --git a/src/crypto/aes.cpp b/src/crypto/aes.cpp index 5f1b2e6..2a2971f 100644 --- a/src/crypto/aes.cpp +++ b/src/crypto/aes.cpp @@ -3,6 +3,9 @@ #include #include +#include +#include + namespace fc { static int init = init_openssl(); @@ -188,4 +191,44 @@ std::vector aes_decrypt( const fc::sha512& key, const std::vector& c return plain_text; } + +/** encrypts plain_text and then includes a checksum that enables us to verify the integrety of + * the file / key prior to decryption. + */ +void aes_save( const fc::path& file, const fc::sha512& key, std::vector plain_text ) +{ try { + auto cipher = aes_encrypt( key, plain_text ); + fc::sha512::encoder check_enc; + fc::raw::pack( check_enc, key ); + fc::raw::pack( check_enc, cipher ); + auto check = check_enc.result(); + + fc::ofstream out(file); + fc::raw::pack( out, check ); + fc::raw::pack( out, cipher ); +} FC_RETHROW_EXCEPTIONS( warn, "", ("file",file) ) } + +/** + * recovers the plain_text saved via aes_save() + */ +std::vector aes_load( const fc::path& file, const fc::sha512& key ) +{ try { + FC_ASSERT( fc::exists( file ) ); + + fc::ifstream in( file, fc::ifstream::binary ); + fc::sha512 check; + std::vector cipher; + + fc::raw::unpack( in, check ); + fc::raw::unpack( in, cipher ); + + fc::sha512::encoder check_enc; + fc::raw::pack( check_enc, key ); + fc::raw::pack( check_enc, cipher ); + + FC_ASSERT( check_enc.result() == check ); + + return aes_decrypt( key, cipher ); +} FC_RETHROW_EXCEPTIONS( warn, "", ("file",file) ) } + } // namespace fc