Unverified Commit 346d28b4 authored by Oleh Nikolaiev's avatar Oleh Nikolaiev Committed by GitHub

Merge pull request #193 from skalenetwork/bug/SKALE-3009-response-data

Bug/skale 3009 response data
parents 21fb34f7 a193af29
......@@ -8,9 +8,7 @@ jobs:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
steps:
- name: Login to docker
env:
GITHUB_TOKEN: ${{ secrets.DOCKER_SECRET }}
run: docker login -u skalelabs -p ${GITHUB_TOKEN}
run: docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
- uses: actions/checkout@v1
- name: submodule update
run: git submodule update --init --recursive
......
......@@ -11,9 +11,7 @@ jobs:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
steps:
- name: Login to docker
env:
GITHUB_TOKEN: ${{ secrets.DOCKER_SECRET }}
run: docker login -u skalelabs -p ${GITHUB_TOKEN}
run: docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
- uses: actions/checkout@v1
- name: submodule update
run: git submodule update --init --recursive
......
......@@ -8,9 +8,7 @@ jobs:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
steps:
- name: Login to docker
env:
GITHUB_TOKEN: ${{ secrets.DOCKER_SECRET }}
run: docker login -u skalelabs -p ${GITHUB_TOKEN}
run: docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
- uses: actions/checkout@v1
- name: submodule update
run: git submodule update --init --recursive
......
......@@ -10,9 +10,7 @@ jobs:
- name: Check that /dev/urandom exists
run: ls /dev/urandom
- name: Login to docker
env:
GITHUB_TOKEN: ${{ secrets.DOCKER_SECRET }}
run: docker login -u skalelabs -p ${GITHUB_TOKEN}
run: docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
- uses: actions/checkout@v1
- name: submodule update
run: git submodule update --init --recursive
......
......@@ -75,23 +75,22 @@ int char2int(char _input) {
return -1;
}
void carray2Hex(const unsigned char *d, uint64_t _len, char *_hexArray,
uint64_t _hexArrayLen) {
vector<char> carray2Hex(const unsigned char *d, uint64_t _len) {
CHECK_STATE(d);
CHECK_STATE(_hexArray);
vector<char> _hexArray( 2 * _len + 1);
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];
}
_hexArray[_len * 2] = 0;
return _hexArray;
}
......@@ -166,8 +165,7 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
CHECK_STATE(_hashHex);
CHECK_STATE(_sig);
auto hash = make_shared < array < uint8_t,
32 >> ();
auto hash = make_shared < array < uint8_t, 32 >> ();
uint64_t binLen;
......@@ -265,9 +263,7 @@ string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key
HANDLE_TRUSTED_FUNCTION_ERROR(status, *errStatus, errMsg.data());
SAFE_CHAR_BUF(resultBuf, 2 * BUF_LEN + 1);
carray2Hex(encryptedKey->data(), encryptedLen, resultBuf, 2 * BUF_LEN + 1);
vector<char> resultBuf = carray2Hex(encryptedKey->data(), encryptedLen);
return string(resultBuf);
return string(resultBuf.begin(), resultBuf.end());
}
......@@ -33,13 +33,14 @@
#include "stddef.h"
#include "stdint.h"
#include <string>
#include <vector>
EXTERNC bool bls_sign(const char* encryptedKeyHex, const char* hashHex, size_t t, size_t n, char* _sig);
EXTERNC int char2int(char _input);
EXTERNC void carray2Hex(const unsigned char *d, uint64_t , char* _hexArray,
uint64_t _hexArrayLen);
EXTERNC std::vector<char> carray2Hex(const unsigned char *d, uint64_t _len);
EXTERNC bool hex2carray(const char * _hex, uint64_t *_bin_len,
uint8_t* _bin, uint64_t _max_length );
......
......@@ -144,9 +144,8 @@ string gen_dkg_poly(int _t) {
uint64_t length = enc_len;;
vector<char> hexEncrPoly(BUF_LEN, 0);
CHECK_STATE(encrypted_dkg_secret.size() >= length);
carray2Hex(encrypted_dkg_secret.data(), length, hexEncrPoly.data(), BUF_LEN);
vector<char> hexEncrPoly = carray2Hex(encrypted_dkg_secret.data(), length);
string result(hexEncrPoly.data());
return result;
......@@ -271,7 +270,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);
hexEncrKey = carray2Hex(encryptedSkey.data(), decLen);
string dhKeyName = "DKG_DH_KEY_" + _polyName + "_" + to_string(i) + ":";
spdlog::debug("hexEncr DH Key: { }", hexEncrKey.data());
......@@ -351,11 +350,9 @@ bool createBLSShare(const string &blsKeyName, const char *s_shares, const char *
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
SAFE_CHAR_BUF(hexBLSKey, 2 * BUF_LEN)
vector<char> hexBLSKey = carray2Hex(encr_bls_key, enc_bls_len);
carray2Hex(encr_bls_key, enc_bls_len, hexBLSKey, 2 * BUF_LEN);
SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey);
SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey.data());
return true;
......@@ -452,24 +449,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;
}
......
......@@ -69,19 +69,14 @@ vector <string> genECDSAKey() {
vector <string> keys(3);
vector<char> hexEncrKey(BUF_LEN * 2, 0);
carray2Hex(encr_pr_key.data(), enc_len, hexEncrKey.data(),
BUF_LEN * 2);
vector<char> hexEncrKey = carray2Hex(encr_pr_key.data(), enc_len);
keys.at(0) = hexEncrKey.data();
keys.at(1) = string(pub_key_x.data()) + string(pub_key_y.data());
vector<unsigned char> randBuffer(32, 0);
fillRandomBuffer(randBuffer);
vector<char> rand_str(BUF_LEN, 0);
carray2Hex(randBuffer.data(), 32, rand_str.data(), BUF_LEN);
vector<char> rand_str = carray2Hex(randBuffer.data(), 32);
keys.at(2) = rand_str.data();
......
......@@ -71,9 +71,7 @@ void create_test_key() {
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
vector<char> hexEncrKey(2 * enc_len + 1, 0);
carray2Hex(encrypted_key, enc_len, hexEncrKey.data(), 2 * enc_len + 1);
vector<char> hexEncrKey = carray2Hex(encrypted_key, enc_len);
LevelDB::getLevelDb()->writeDataUnique("TEST_KEY", hexEncrKey.data());
}
......@@ -167,9 +165,7 @@ void gen_SEK() {
throw SGXException(-1, "strnlen(SEK,33) != 32");
}
vector<char> hexEncrKey(2 * enc_len + 1, 0);
carray2Hex(encrypted_SEK.data(), enc_len, hexEncrKey.data(), 2 * enc_len + 1);
vector<char> hexEncrKey = carray2Hex(encrypted_SEK.data(), enc_len);
spdlog::info(string("Encrypted storage encryption key:") + hexEncrKey.data());
......@@ -281,10 +277,7 @@ void enter_SEK() {
auto encrypted_SEK = check_and_set_SEK(sek);
vector<char> hexEncrKey(BUF_LEN, 0);
carray2Hex(encrypted_SEK->data(), encrypted_SEK->size(), hexEncrKey.data(),
BUF_LEN);
vector<char> hexEncrKey = carray2Hex(encrypted_SEK->data(), encrypted_SEK->size());
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, vector<char>& message) {
int ret = -1;
if (!cypher) {
return ret;
}
if (!key) {
return ret;
}
if (!message.data()) {
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];
}
message = carray2Hex((unsigned char*) msg_bin, 32);
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, vector<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>
......@@ -653,12 +654,40 @@ TEST_CASE_METHOD(TestFixture, "AES_DKG test", "[aes-dkg]") {
Json::Value complaintResponse = c.complaintResponse(polyNames[1], t, n, 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);
vector<char> message (65, 0);
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.data(), 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;
......
......@@ -43,7 +43,6 @@ testList = ["[first-run]",
"[bls-key-encrypt]",
"[dkg-aes-gen]",
"[dkg-aes-encr-sshares]",
"[dkg-verify]",
"[dkg-api]",
"[dkg-bls]",
"[dkg-poly-exists]",
......
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