Unverified Commit 3f490811 authored by Stan Kladko's avatar Stan Kladko Committed by GitHub

Merge pull request #220 from skalenetwork/SECURE_ENCLAVE_CHANGES

Secure enclave changes
parents 553187f1 47a9b257
...@@ -16,3 +16,7 @@ ...@@ -16,3 +16,7 @@
[submodule "sgx-software-enable"] [submodule "sgx-software-enable"]
path = sgx-software-enable path = sgx-software-enable
url = https://github.com/intel/sgx-software-enable url = https://github.com/intel/sgx-software-enable
[submodule "secure_enclave/secp256k1-sgx"]
path = secure_enclave/secp256k1-sgx
url = https://github.com/bl4ck5un/secp256k1-sgx
branch = master
...@@ -197,7 +197,7 @@ vector <vector<string>> getVerificationVectorMult(const std::string& encryptedPo ...@@ -197,7 +197,7 @@ vector <vector<string>> getVerificationVectorMult(const std::string& encryptedPo
vector<vector<string>> result(t); vector<vector<string>> result(t);
for (size_t i = 0; i < t; ++i) { for (int i = 0; i < t; ++i) {
libff::alt_bn128_G2 current_coefficient; libff::alt_bn128_G2 current_coefficient;
current_coefficient.X.c0 = libff::alt_bn128_Fq(verificationVector[i][0].c_str()); current_coefficient.X.c0 = libff::alt_bn128_Fq(verificationVector[i][0].c_str());
current_coefficient.X.c1 = libff::alt_bn128_Fq(verificationVector[i][1].c_str()); current_coefficient.X.c1 = libff::alt_bn128_Fq(verificationVector[i][1].c_str());
...@@ -224,7 +224,7 @@ getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const ve ...@@ -224,7 +224,7 @@ getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const ve
CHECK_STATE(_encryptedPolyHex); CHECK_STATE(_encryptedPolyHex);
vector<char> hexEncrKey(BUF_LEN, 0); vector<char> hexEncrKey(BUF_LEN, 0);
vector<char> errMsg1(BUF_LEN, 0); vector<char> errMsg(BUF_LEN, 0);
vector <uint8_t> encrDKGPoly(BUF_LEN, 0); vector <uint8_t> encrDKGPoly(BUF_LEN, 0);
int errStatus = 0; int errStatus = 0;
uint64_t encLen = 0; uint64_t encLen = 0;
...@@ -238,10 +238,6 @@ getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const ve ...@@ -238,10 +238,6 @@ getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const ve
READ_LOCK(sgxInitMutex); READ_LOCK(sgxInitMutex);
status = trustedSetEncryptedDkgPoly(eid, &errStatus, errMsg1.data(), encrDKGPoly.data(), encLen);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg1.data());
string result; string result;
for (int i = 0; i < _n; i++) { for (int i = 0; i < _n; i++) {
...@@ -259,26 +255,22 @@ getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const ve ...@@ -259,26 +255,22 @@ getSecretShares(const string &_polyName, const char *_encryptedPolyHex, const ve
spdlog::debug("pubKeyB is {}", pub_keyB); spdlog::debug("pubKeyB is {}", pub_keyB);
sgx_status_t status = SGX_SUCCESS; sgx_status_t status = SGX_SUCCESS;
status = trustedGetEncryptedSecretShare(eid, &errStatus, errMsg1.data(), encryptedSkey.data(), &decLen, status = trustedGetEncryptedSecretShare(eid, &errStatus,
errMsg.data(),
encrDKGPoly.data(), encLen,
encryptedSkey.data(), &decLen,
currentShare.data(), sShareG2.data(), pubKeyB.data(), _t, _n, currentShare.data(), sShareG2.data(), pubKeyB.data(), _t, _n,
i + 1); i + 1);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg1.data()); HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
spdlog::debug("cur_share is {}", currentShare.data());
result += string(currentShare.data()); result += string(currentShare.data());
spdlog::debug("dec len is {}", decLen);
hexEncrKey = carray2Hex(encryptedSkey.data(), decLen); hexEncrKey = carray2Hex(encryptedSkey.data(), decLen);
string dhKeyName = "DKG_DH_KEY_" + _polyName + "_" + to_string(i) + ":"; string dhKeyName = "DKG_DH_KEY_" + _polyName + "_" + to_string(i) + ":";
spdlog::debug("hexEncr DH Key: { }", hexEncrKey.data());
spdlog::debug("name to write to db is {}", dhKeyName);
string shareG2_name = "shareG2_" + _polyName + "_" + to_string(i) + ":"; string shareG2_name = "shareG2_" + _polyName + "_" + to_string(i) + ":";
spdlog::debug("name to write to db is {}", shareG2_name);
spdlog::debug("s_shareG2: {}", sShareG2.data());
SGXWalletServer::writeDataToDB(dhKeyName, hexEncrKey.data()); SGXWalletServer::writeDataToDB(dhKeyName, hexEncrKey.data());
SGXWalletServer::writeDataToDB(shareG2_name, sShareG2.data()); SGXWalletServer::writeDataToDB(shareG2_name, sShareG2.data());
......
...@@ -157,6 +157,7 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) { ...@@ -157,6 +157,7 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) {
httpServer = make_shared<HttpServer>(BASE_PORT, certPath, keyPath, rootCAPath, _checkCerts, httpServer = make_shared<HttpServer>(BASE_PORT, certPath, keyPath, rootCAPath, _checkCerts,
NUM_THREADS); NUM_THREADS);
server = make_shared<SGXWalletServer>(*httpServer, server = make_shared<SGXWalletServer>(*httpServer,
JSONRPC_SERVER_V2); // hybrid server (json-rpc 1.0 & 2.0) JSONRPC_SERVER_V2); // hybrid server (json-rpc 1.0 & 2.0)
......
1.58.6 1.58.7
\ No newline at end of file \ No newline at end of file
...@@ -101,7 +101,7 @@ BOOST_THROW_EXCEPTION(runtime_error(__ERR_STRING__)); \ ...@@ -101,7 +101,7 @@ BOOST_THROW_EXCEPTION(runtime_error(__ERR_STRING__)); \
extern std::shared_timed_mutex sgxInitMutex; extern std::shared_timed_mutex sgxInitMutex;
extern uint64_t initTime; extern uint64_t initTime;
#if SGX_MODE == SIM #ifdef SGX_HW_SIM
#define ENCLAVE_RESTART_PERIOD_S 5 #define ENCLAVE_RESTART_PERIOD_S 5
#else #else
#define ENCLAVE_RESTART_PERIOD_S 60 * 10 #define ENCLAVE_RESTART_PERIOD_S 60 * 10
......
...@@ -82,8 +82,8 @@ string *stringFromFq(libff::alt_bn128_Fq *_fq) { ...@@ -82,8 +82,8 @@ string *stringFromFq(libff::alt_bn128_Fq *_fq) {
try { try {
_fq->as_bigint().to_mpz(t); _fq->as_bigint().to_mpz(t);
char *tmp = mpz_get_str(arr, 10, t); mpz_get_str(arr, 10, t);
ret = new string(tmp); ret = new string(arr);
} catch (exception &e) { } catch (exception &e) {
LOG_ERROR(e.what()); LOG_ERROR(e.what());
goto clean; goto clean;
...@@ -107,13 +107,13 @@ string *stringFromG1(libff::alt_bn128_G1 *_g1) { ...@@ -107,13 +107,13 @@ string *stringFromG1(libff::alt_bn128_G1 *_g1) {
try { try {
_g1->to_affine_coordinates(); _g1->to_affine_coordinates();
auto sX = stringFromFq(&_g1->X); sX = stringFromFq(&_g1->X);
if (!sX) { if (!sX) {
goto clean; goto clean;
} }
auto sY = stringFromFq(&_g1->Y); sY = stringFromFq(&_g1->Y);
if (!sY) { if (!sY) {
goto clean; goto clean;
...@@ -131,8 +131,8 @@ string *stringFromG1(libff::alt_bn128_G1 *_g1) { ...@@ -131,8 +131,8 @@ string *stringFromG1(libff::alt_bn128_G1 *_g1) {
clean: clean:
SAFE_FREE(sX); SAFE_DELETE(sX);
SAFE_FREE(sY); SAFE_DELETE(sY);
return ret; return ret;
...@@ -226,7 +226,7 @@ bool enclave_sign(const char *_keyString, const char *_hashXString, const char * ...@@ -226,7 +226,7 @@ bool enclave_sign(const char *_keyString, const char *_hashXString, const char *
} }
try { try {
auto key = keyFromString(_keyString); key = keyFromString(_keyString);
if (!key) { if (!key) {
LOG_ERROR("Null key"); LOG_ERROR("Null key");
...@@ -243,7 +243,7 @@ bool enclave_sign(const char *_keyString, const char *_hashXString, const char * ...@@ -243,7 +243,7 @@ bool enclave_sign(const char *_keyString, const char *_hashXString, const char *
sign.to_affine_coordinates(); sign.to_affine_coordinates();
auto r = stringFromG1(&sign); r = stringFromG1(&sign);
memset(sig, 0, BUF_LEN); memset(sig, 0, BUF_LEN);
......
Subproject commit 5f235e8e9e821cd972c4a57afdfe47a7fe83acd0
...@@ -163,7 +163,7 @@ void trustedEnclaveInit(uint64_t _logLevel) { ...@@ -163,7 +163,7 @@ void trustedEnclaveInit(uint64_t _logLevel) {
LOG_INFO("SECURITY WARNING: sgxwallet is running in INSECURE DEBUG MODE! NEVER USE IN PRODUCTION!"); LOG_INFO("SECURITY WARNING: sgxwallet is running in INSECURE DEBUG MODE! NEVER USE IN PRODUCTION!");
#endif #endif
#if SGX_MODE == SIM #ifdef SGX_HW_SIM
LOG_INFO("SECURITY WARNING: sgxwallet is running in INSECURE SIMULATION MODE! NEVER USE IN PRODUCTION!"); LOG_INFO("SECURITY WARNING: sgxwallet is running in INSECURE SIMULATION MODE! NEVER USE IN PRODUCTION!");
#endif #endif
...@@ -847,10 +847,14 @@ void trustedSetEncryptedDkgPoly(int *errStatus, char *errString, uint8_t *encryp ...@@ -847,10 +847,14 @@ void trustedSetEncryptedDkgPoly(int *errStatus, char *errString, uint8_t *encryp
LOG_INFO("SGX call completed"); LOG_INFO("SGX call completed");
} }
void trustedGetEncryptedSecretShare(int *errStatus, char *errString, uint8_t *encrypted_skey, uint64_t *dec_len,
void trustedGetEncryptedSecretShare(int *errStatus, char *errString,
uint8_t *_encrypted_poly, uint64_t _enc_len,
uint8_t *encrypted_skey, uint64_t *dec_len,
char *result_str, char *s_shareG2, char *pub_keyB, uint8_t _t, uint8_t _n, char *result_str, char *s_shareG2, char *pub_keyB, uint8_t _t, uint8_t _n,
uint8_t ind) { uint8_t ind) {
LOG_INFO(__FUNCTION__); LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE INIT_ERROR_STATE
...@@ -864,6 +868,11 @@ void trustedGetEncryptedSecretShare(int *errStatus, char *errString, uint8_t *en ...@@ -864,6 +868,11 @@ void trustedGetEncryptedSecretShare(int *errStatus, char *errString, uint8_t *en
LOG_DEBUG(__FUNCTION__); LOG_DEBUG(__FUNCTION__);
trustedSetEncryptedDkgPoly(&status, errString, _encrypted_poly, _enc_len);
CHECK_STATUS2("trustedSetEncryptedDkgPoly failed with status %d ");
SAFE_CHAR_BUF(skey, BUF_LEN); SAFE_CHAR_BUF(skey, BUF_LEN);
SAFE_CHAR_BUF(pub_key_x, BUF_LEN);SAFE_CHAR_BUF(pub_key_y, BUF_LEN); SAFE_CHAR_BUF(pub_key_x, BUF_LEN);SAFE_CHAR_BUF(pub_key_y, BUF_LEN);
...@@ -1118,8 +1127,6 @@ trustedGetBlsPubKey(int *errStatus, char *errString, uint8_t *encryptedPrivateKe ...@@ -1118,8 +1127,6 @@ trustedGetBlsPubKey(int *errStatus, char *errString, uint8_t *encryptedPrivateKe
uint8_t type = 0; uint8_t type = 0;
uint8_t exportable = 0; uint8_t exportable = 0;
int status = AES_decrypt(encryptedPrivateKey, key_len, skey_hex, BUF_LEN, int status = AES_decrypt(encryptedPrivateKey, key_len, skey_hex, BUF_LEN,
&type, &exportable); &type, &exportable);
......
...@@ -88,15 +88,15 @@ enclave { ...@@ -88,15 +88,15 @@ enclave {
[out, count = 3072] uint8_t* decrypted_dkg_secret [out, count = 3072] uint8_t* decrypted_dkg_secret
); );
public void trustedSetEncryptedDkgPoly(
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string,
[in, count = 3050] uint8_t* encrypted_poly,
uint64_t enc_len);
public void trustedGetEncryptedSecretShare( public void trustedGetEncryptedSecretShare(
[out]int *errStatus, [out]int *errStatus,
[out, count = SMALL_BUF_SIZE] char *err_string, [out, count = SMALL_BUF_SIZE] char *err_string,
[in, count = 3050] uint8_t* encrypted_poly,
uint64_t enc_len,
[out, count = SMALL_BUF_SIZE] uint8_t *encrypted_skey, [out, count = SMALL_BUF_SIZE] uint8_t *encrypted_skey,
[out] uint64_t* dec_len, [out] uint64_t* dec_len,
[out, count = 193] char* result_str, [out, count = 193] char* result_str,
......
...@@ -370,20 +370,15 @@ TEST_CASE_METHOD(TestFixture, "DKG AES encrypted secret shares test", "[dkg-aes- ...@@ -370,20 +370,15 @@ TEST_CASE_METHOD(TestFixture, "DKG AES encrypted secret shares test", "[dkg-aes-
REQUIRE(status == SGX_SUCCESS); REQUIRE(status == SGX_SUCCESS);
REQUIRE(errStatus == SGX_SUCCESS); REQUIRE(errStatus == SGX_SUCCESS);
uint64_t enc_len = encLen;
PRINT_SRC_LINE
status = trustedSetEncryptedDkgPoly(eid, &errStatus, errMsg.data(), encryptedDKGSecret.data(), enc_len);
REQUIRE(status == SGX_SUCCESS);
REQUIRE(errStatus == SGX_SUCCESS);
vector <uint8_t> encrPRDHKey(BUF_LEN, 0); vector <uint8_t> encrPRDHKey(BUF_LEN, 0);
string pub_keyB = SAMPLE_PUBLIC_KEY_B; string pub_keyB = SAMPLE_PUBLIC_KEY_B;
vector<char> s_shareG2(BUF_LEN, 0); vector<char> s_shareG2(BUF_LEN, 0);
PRINT_SRC_LINE PRINT_SRC_LINE
status = trustedGetEncryptedSecretShare(eid, &errStatus, errMsg.data(), encrPRDHKey.data(), &encLen, status = trustedGetEncryptedSecretShare(eid, &errStatus,errMsg.data(),
encryptedDKGSecret.data(), encLen,
encrPRDHKey.data(), &encLen,
result.data(), result.data(),
s_shareG2.data(), s_shareG2.data(),
(char *) pub_keyB.data(), 2, 2, 1); (char *) pub_keyB.data(), 2, 2, 1);
...@@ -779,8 +774,8 @@ TEST_CASE_METHOD(TestFixture, "AES encrypt/decrypt", "[aes-encrypt-decrypt]") { ...@@ -779,8 +774,8 @@ TEST_CASE_METHOD(TestFixture, "AES encrypt/decrypt", "[aes-encrypt-decrypt]") {
status = trustedDecryptKey(eid, &errStatus, errMsg.data(), encrypted_key.data(), encLen, decr_key.data()); status = trustedDecryptKey(eid, &errStatus, errMsg.data(), encrypted_key.data(), encLen, decr_key.data());
REQUIRE(status == 0); REQUIRE(status == 0);
REQUIRE(errStatus == 0);
REQUIRE(key.compare(decr_key.data()) == 0); REQUIRE(key.compare(decr_key.data()) == 0);
REQUIRE(errStatus == 0);
} }
......
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