SKALE-3009 add test for complaint

parent e075e5eb
......@@ -75,8 +75,7 @@ int char2int(char _input) {
return -1;
}
void carray2Hex(const unsigned char *d, uint64_t _len, char *_hexArray,
uint64_t _hexArrayLen) {
void carray2Hex(const unsigned char *d, uint64_t _len, char *_hexArray) {
CHECK_STATE(d);
CHECK_STATE(_hexArray);
......@@ -84,8 +83,6 @@ void carray2Hex(const unsigned char *d, uint64_t _len, char *_hexArray,
char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
CHECK_STATE(_hexArrayLen > 2 * _len);
for (uint64_t j = 0; j < _len; j++) {
_hexArray[j * 2] = hexval[((d[j] >> 4) & 0xF)];
_hexArray[j * 2 + 1] = hexval[(d[j]) & 0x0F];
......@@ -267,7 +264,7 @@ string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key
SAFE_CHAR_BUF(resultBuf, 2 * BUF_LEN + 1);
carray2Hex(encryptedKey->data(), encryptedLen, resultBuf, 2 * BUF_LEN + 1);
carray2Hex(encryptedKey->data(), encryptedLen, resultBuf);
return string(resultBuf);
}
......@@ -38,8 +38,8 @@ EXTERNC bool bls_sign(const char* encryptedKeyHex, const char* hashHex, size_t t
EXTERNC int char2int(char _input);
EXTERNC void carray2Hex(const unsigned char *d, uint64_t , char* _hexArray,
uint64_t _hexArrayLen);
EXTERNC void carray2Hex(const unsigned char *d, uint64_t , char* _hexArray);
EXTERNC bool hex2carray(const char * _hex, uint64_t *_bin_len,
uint8_t* _bin, uint64_t _max_length );
......
......@@ -146,7 +146,7 @@ string gen_dkg_poly(int _t) {
vector<char> hexEncrPoly(BUF_LEN, 0);
CHECK_STATE(encrypted_dkg_secret.size() >= length);
carray2Hex(encrypted_dkg_secret.data(), length, hexEncrPoly.data(), BUF_LEN);
carray2Hex(encrypted_dkg_secret.data(), length, hexEncrPoly.data());
string result(hexEncrPoly.data());
return result;
......@@ -245,7 +245,7 @@ getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const ve
result += string(currentShare.data());
spdlog::debug("dec len is {}", decLen);
carray2Hex(encryptedSkey.data(), decLen, hexEncrKey.data(), BUF_LEN);
carray2Hex(encryptedSkey.data(), decLen, hexEncrKey.data());
string dhKeyName = "DKG_DH_KEY_" + _polyName + "_" + to_string(i) + ":";
spdlog::debug("hexEncr DH Key: { }", hexEncrKey.data());
......@@ -327,7 +327,7 @@ bool createBLSShare(const string &blsKeyName, const char *s_shares, const char *
SAFE_CHAR_BUF(hexBLSKey, 2 * BUF_LEN)
carray2Hex(encr_bls_key, enc_bls_len, hexBLSKey, 2 * BUF_LEN);
carray2Hex(encr_bls_key, enc_bls_len, hexBLSKey);
SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey);
......@@ -426,24 +426,25 @@ string decryptDHKey(const string &polyName, int ind) {
shared_ptr <string> hexEncrKeyPtr = SGXWalletServer::readFromDb(DH_key_name, "DKG_DH_KEY_");
spdlog::debug("encr DH key is {}", *hexEncrKeyPtr);
spdlog::debug("encr DH key length is {}", hexEncrKeyPtr->length());
vector<char> hexEncrKey(2 * BUF_LEN, 0);
uint64_t dhEncLen = 0;SAFE_UINT8_BUF(encryptedDHKey, BUF_LEN);
uint64_t dhEncLen = 0;
SAFE_UINT8_BUF(encryptedDHKey, BUF_LEN)
if (!hex2carray(hexEncrKeyPtr->c_str(), &dhEncLen, encryptedDHKey, BUF_LEN)) {
throw SGXException(INVALID_HEX, "Invalid hexEncrKey");
}
spdlog::debug("encr DH key length is {}", dhEncLen);
spdlog::debug("hex encr DH key length is {}", hexEncrKeyPtr->length());
SAFE_CHAR_BUF(DHKey, ECDSA_SKEY_LEN);
SAFE_CHAR_BUF(DHKey, ECDSA_SKEY_LEN)
sgx_status_t status = SGX_SUCCESS;
RESTART_BEGIN
status = trustedDecryptKey(eid, &errStatus, errMsg1.data(), encryptedDHKey, dhEncLen, DHKey);
RESTART_END
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg1.data());
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg1.data())
return DHKey;
}
......
......@@ -71,8 +71,7 @@ vector <string> genECDSAKey() {
vector<char> hexEncrKey(BUF_LEN * 2, 0);
carray2Hex(encr_pr_key.data(), enc_len, hexEncrKey.data(),
BUF_LEN * 2);
carray2Hex(encr_pr_key.data(), enc_len, hexEncrKey.data());
keys.at(0) = hexEncrKey.data();
keys.at(1) = string(pub_key_x.data()) + string(pub_key_y.data());
......@@ -81,7 +80,7 @@ vector <string> genECDSAKey() {
vector<char> rand_str(BUF_LEN, 0);
carray2Hex(randBuffer.data(), 32, rand_str.data(), BUF_LEN);
carray2Hex(randBuffer.data(), 32, rand_str.data());
keys.at(2) = rand_str.data();
......
......@@ -73,7 +73,7 @@ void create_test_key() {
vector<char> hexEncrKey(2 * enc_len + 1, 0);
carray2Hex(encrypted_key, enc_len, hexEncrKey.data(), 2 * enc_len + 1);
carray2Hex(encrypted_key, enc_len, hexEncrKey.data());
LevelDB::getLevelDb()->writeDataUnique("TEST_KEY", hexEncrKey.data());
}
......@@ -169,7 +169,7 @@ void gen_SEK() {
vector<char> hexEncrKey(2 * enc_len + 1, 0);
carray2Hex(encrypted_SEK.data(), enc_len, hexEncrKey.data(), 2 * enc_len + 1);
carray2Hex(encrypted_SEK.data(), enc_len, hexEncrKey.data());
spdlog::info(string("Encrypted storage encryption key:") + hexEncrKey.data());
......@@ -283,8 +283,7 @@ void enter_SEK() {
vector<char> hexEncrKey(BUF_LEN, 0);
carray2Hex(encrypted_SEK->data(), encrypted_SEK->size(), hexEncrKey.data(),
BUF_LEN);
carray2Hex(encrypted_SEK->data(), encrypted_SEK->size(), hexEncrKey.data());
spdlog::info("Got sealed storage encryption key.");
......
......@@ -412,3 +412,126 @@ void TestUtils::doDKG(StubClient &c, int n, int t,
for (auto&& i : _blsKeyNames)
cerr << i << endl;
}
int sessionKeyRecoverDH(const char *skey_str, const char *sshare, char *common_key) {
int ret = -1;
SAFE_CHAR_BUF(pb_keyB_x, 65);
SAFE_CHAR_BUF(pb_keyB_y, 65);
mpz_t skey;
mpz_init(skey);
point pub_keyB = point_init();
point session_key = point_init();
pb_keyB_x[64] = 0;
strncpy(pb_keyB_x, sshare, 64);
strncpy(pb_keyB_y, sshare + 64, 64);
pb_keyB_y[64] = 0;
if (!common_key) {
mpz_clear(skey);
point_clear(pub_keyB);
point_clear(session_key);
return ret;
}
common_key[0] = 0;
if (!skey_str) {
mpz_clear(skey);
point_clear(pub_keyB);
point_clear(session_key);
return ret;
}
if (!sshare) {
mpz_clear(skey);
point_clear(pub_keyB);
point_clear(session_key);
return ret;
}
if (mpz_set_str(skey, skey_str, 16) == -1) {
mpz_clear(skey);
point_clear(pub_keyB);
point_clear(session_key);
return ret;
}
domain_parameters curve;
curve = domain_parameters_init();
domain_parameters_load_curve(curve, secp256k1);
if (point_set_hex(pub_keyB, pb_keyB_x, pb_keyB_y) != 0) {
return ret;
}
point_multiplication(session_key, skey, pub_keyB, curve);
SAFE_CHAR_BUF(arr_x, BUF_LEN);
mpz_get_str(arr_x, 16, session_key->x);
int n_zeroes = 64 - strlen(arr_x);
for (int i = 0; i < n_zeroes; i++) {
common_key[i] = '0';
}
strncpy(common_key + n_zeroes, arr_x, strlen(arr_x));
ret = 0;
mpz_clear(skey);
point_clear(pub_keyB);
point_clear(session_key);
return ret;
}
int xorDecryptDH(char *key, const char *cypher, char *message) {
int ret = -1;
if (!cypher) {
return ret;
}
if (!key) {
return ret;
}
if (!message) {
return ret;
}
SAFE_CHAR_BUF(msg_bin,33)
SAFE_CHAR_BUF(key_bin,33)
uint64_t key_length;
if (!hex2carray(key, &key_length, (uint8_t*) key_bin, 33)) {
return ret;
}
uint64_t cypher_length;
SAFE_CHAR_BUF(cypher_bin, 33);
if (!hex2carray(cypher, &cypher_length, (uint8_t *) cypher_bin, 33)) {
return ret;
}
for (int i = 0; i < 32; i++) {
msg_bin[i] = cypher_bin[i] ^ key_bin[i];
}
carray2Hex((unsigned char*) msg_bin, 32, message);
ret = 0;
return ret;
}
......@@ -78,4 +78,8 @@ public:
int schainID, int dkgID);
};
int sessionKeyRecoverDH(const char *skey_str, const char *sshare, char *common_key);
int xorDecryptDH(char *key, const char *cypher, char *message);
#endif //SGXWALLET_TESTW_H
......@@ -31,6 +31,7 @@
#include "sgxwallet_common.h"
#include "third_party/intel/create_enclave.h"
#include "secure_enclave_u.h"
#include "secure_enclave/DHDkg.h"
#include "third_party/intel/sgx_detect.h"
#include <gmp.h>
#include <sgx_urts.h>
......@@ -646,12 +647,41 @@ TEST_CASE_METHOD(TestFixture, "AES_DKG test", "[aes-dkg]") {
Json::Value complaintResponse = c.complaintResponse(polyNames[1], 0);
REQUIRE(complaintResponse["status"] == 0);
string dhKey = complaintResponse["dhKey"].asString();
string shareG2 = complaintResponse["share*G2"].asString();
string secretShare = secretShares[1]["secretShare"].asString().substr(0, 192);
SAFE_CHAR_BUF(message, 32)
SAFE_CHAR_BUF(encr_sshare, BUF_LEN)
strncpy(encr_sshare, pubEthKeys[0].asString().c_str(), 128);
SAFE_CHAR_BUF(common_key, BUF_LEN);
REQUIRE(sessionKeyRecoverDH(dhKey.c_str(), encr_sshare, common_key) == 0);
SAFE_CHAR_BUF(encr_sshare_check, BUF_LEN)
strncpy(encr_sshare_check, secretShare.c_str(), ECDSA_SKEY_LEN - 1);
REQUIRE(xorDecryptDH(common_key, encr_sshare_check, message) == 0);
mpz_t hex_share;
mpz_init(hex_share);
mpz_set_str(hex_share, message, 16);
libff::alt_bn128_Fr share(hex_share);
libff::alt_bn128_G2 decrypted_share_G2 = share * libff::alt_bn128_G2::one();
decrypted_share_G2.to_affine_coordinates();
mpz_clear(hex_share);
REQUIRE( convertG2ToString(decrypted_share_G2) == shareG2 );
BLSSigShareSet sigShareSet(t, n);
string hash = SAMPLE_HASH;
auto hash_arr = make_shared < array < uint8_t,
32 >> ();
auto hash_arr = make_shared < array < uint8_t, 32 > >();
uint64_t binLen;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment