diff --git a/node/encryptTransaction.js b/node/encryptTransaction.js index 95baf7f1..fa5ec184 100644 --- a/node/encryptTransaction.js +++ b/node/encryptTransaction.js @@ -50,4 +50,6 @@ async function encryptAndSend(eth, to, data) { } -module.exports.encryptAndSend = encryptAndSend \ No newline at end of file +module.exports.encryptAndSend = encryptAndSend +module.exports.encrypt_data = encrypt_data +module.exports.get_common_bls_public_key = get_common_bls_public_key \ No newline at end of file diff --git a/test/unit_tests_te.cpp b/test/unit_tests_te.cpp index 64725a1d..58c5ecf1 100644 --- a/test/unit_tests_te.cpp +++ b/test/unit_tests_te.cpp @@ -195,6 +195,48 @@ BOOST_AUTO_TEST_CASE( ConvertionToStringAndBack ) { BOOST_REQUIRE( ciphertext_with_aes == ciphertext_from_string ); } +BOOST_AUTO_TEST_CASE( ConvertionToStringAndBackWrongMagicString ) { + libBLS::ThresholdUtils::initCurve(); + + std::string message = + "Hello, SKALE users and fans, gl!Hello, SKALE users and fans, gl!"; // message should be 64 + // length + + libff::alt_bn128_Fr secret_key = libff::alt_bn128_Fr::random_element(); + + libff::alt_bn128_G2 public_key = secret_key * libff::alt_bn128_G2::one(); + + auto ciphertext_with_aes = libBLS::TE::encryptWithAES( message, public_key ); + + auto str = + libBLS::TE::aesCiphertextToString( ciphertext_with_aes.first, ciphertext_with_aes.second ); + + auto ciphertext_from_string = libBLS::TE::aesCiphertextFromString( str ); + + std::string random_bytes = "ff7e0d516baae0301df502d886ed08bb"; + + str = random_bytes + str.substr( 32, std::string::npos ); + + // auto V_old = std::get< 1 >( ciphertext_with_aes.first ); + // auto V_new = std::get< 1 >( ciphertext_from_string.first ); + + // auto W_old = std::get< 2 >( ciphertext_with_aes.first ); + // auto W_new = std::get< 2 >( ciphertext_from_string.first ); + + // auto U_old = std::get< 0 >( ciphertext_with_aes.first ); + // auto U_new = std::get< 0 >( ciphertext_from_string.first ); + + // BOOST_REQUIRE( U_old == U_new ); + // BOOST_REQUIRE( W_old == W_new ); + // BOOST_REQUIRE( V_old == V_new ); + // BOOST_REQUIRE( ciphertext_with_aes.first == ciphertext_from_string.first ); + // BOOST_REQUIRE( ciphertext_with_aes.second.size() == ciphertext_from_string.second.size() ); + // BOOST_REQUIRE( ciphertext_with_aes == ciphertext_from_string ); + + BOOST_REQUIRE_THROW( + libBLS::TE::aesCiphertextFromString( str ), libBLS::ThresholdUtils::IncorrectInput ); +} + BOOST_AUTO_TEST_CASE( ConvertionToStringAndBackTooShort ) { libBLS::ThresholdUtils::initCurve(); diff --git a/threshold_encryption/threshold_encryption.cpp b/threshold_encryption/threshold_encryption.cpp index 35ae2992..f56e12c2 100644 --- a/threshold_encryption/threshold_encryption.cpp +++ b/threshold_encryption/threshold_encryption.cpp @@ -33,6 +33,8 @@ namespace libBLS { +const std::string TE::MAGIC_STRING = "8b1ba8JdPjeWd4G0bFRB0KBohPCULTHN"; + TE::TE( const size_t t, const size_t n ) : t_( t ), n_( n ) { libff::init_alt_bn128_params(); libff::inhibit_profiling_info = true; @@ -309,7 +311,7 @@ std::string TE::aesCiphertextToString( std::string w_str = x + y; - return u_str + v_str + w_str + encrypted_data; + return MAGIC_STRING + u_str + v_str + w_str + encrypted_data; } std::pair< Ciphertext, std::vector< uint8_t > > TE::aesCiphertextFromString( @@ -317,7 +319,13 @@ std::pair< Ciphertext, std::vector< uint8_t > > TE::aesCiphertextFromString( ThresholdUtils::initCurve(); ThresholdUtils::initAES(); - if ( !ThresholdUtils::checkHex( str ) ) { + if ( str.substr( 0, 32 ) != MAGIC_STRING ) { + throw ThresholdUtils::IncorrectInput( + "Provided input was not encrypted properly: MAGIC_STRING doesn't match" ); + } + + std::string ciphertext = str.substr( 32, std::string::npos ); + if ( !ThresholdUtils::checkHex( ciphertext ) ) { throw ThresholdUtils::IncorrectInput( "Provided string contains non-hex symbols" ); } @@ -326,11 +334,11 @@ std::pair< Ciphertext, std::vector< uint8_t > > TE::aesCiphertextFromString( "Incoming string is too short to convert to aes ciphertext" ); } - std::string u_str = str.substr( 0, 256 ); - std::string v_str = str.substr( 256, 128 ); - std::string w_str = str.substr( 256 + 128, 128 ); + std::string u_str = ciphertext.substr( 0, 256 ); + std::string v_str = ciphertext.substr( 256, 128 ); + std::string w_str = ciphertext.substr( 256 + 128, 128 ); - std::string encrypted_data = str.substr( 256 + 128 + 128, std::string::npos ); + std::string encrypted_data = ciphertext.substr( 256 + 128 + 128, std::string::npos ); uint64_t bin_len; std::vector< uint8_t > aes_cipher( encrypted_data.size() / 2 ); diff --git a/threshold_encryption/threshold_encryption.h b/threshold_encryption/threshold_encryption.h index a753a997..a44a5ea1 100644 --- a/threshold_encryption/threshold_encryption.h +++ b/threshold_encryption/threshold_encryption.h @@ -74,6 +74,8 @@ class TE { static std::pair< Ciphertext, std::vector< uint8_t > > aesCiphertextFromString( const std::string& str ); + static const std::string MAGIC_STRING; + private: const size_t t_ = 0; diff --git a/tools/utils.cpp b/tools/utils.cpp index 67e8d355..c673addd 100644 --- a/tools/utils.cpp +++ b/tools/utils.cpp @@ -365,7 +365,6 @@ std::vector< uint8_t > ThresholdUtils::aesEncrypt( int actual_size = 0, final_size = 0; EVP_CIPHER_CTX* e_ctx = EVP_CIPHER_CTX_new(); - // EVP_CIPHER_CTX_ctrl(e_ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL); EVP_EncryptInit( e_ctx, EVP_aes_256_gcm(), ( const unsigned char* ) key.c_str(), iv ); EVP_EncryptUpdate( e_ctx, &output[64], &actual_size, ( const unsigned char* ) plaintext.data(), plaintext.length() );