Skip to content

Commit

Permalink
Merge pull request #167 from skalenetwork/enhancement/SKALE-4723-add-…
Browse files Browse the repository at this point in the history
…MAGIC_STRING

Enhancement/skale 4723 add magic string
  • Loading branch information
olehnikolaiev authored Nov 11, 2021
2 parents 385f1f3 + f1b9bc3 commit ff286fc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
4 changes: 3 additions & 1 deletion node/encryptTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ async function encryptAndSend(eth, to, data) {
}


module.exports.encryptAndSend = encryptAndSend
module.exports.encryptAndSend = encryptAndSend
module.exports.encrypt_data = encrypt_data
module.exports.get_common_bls_public_key = get_common_bls_public_key
42 changes: 42 additions & 0 deletions test/unit_tests_te.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
20 changes: 14 additions & 6 deletions threshold_encryption/threshold_encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -309,15 +311,21 @@ 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(
const std::string& str ) {
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" );
}

Expand All @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions threshold_encryption/threshold_encryption.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion tools/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down

0 comments on commit ff286fc

Please sign in to comment.