Skip to content

Commit

Permalink
Merge pull request #149 from skalenetwork/SKALE-3151-cannot-decrypt-s…
Browse files Browse the repository at this point in the history
…torage-key

Skale 3151 cannot decrypt storage key
  • Loading branch information
kladkogex authored Aug 21, 2020
2 parents f5a5395 + f46249b commit 5dc4b50
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 66 deletions.
4 changes: 4 additions & 0 deletions SGXException.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class SGXException : public std::exception {

SGXException(int32_t _status, const char* _errString) : status(_status), errString(_errString) {}

std::string getMessage() {
return "SGXException:status:" + std::to_string(status) + ":" + errString;
}

};

#endif //SGXD_RPCEXCEPTION_H
51 changes: 36 additions & 15 deletions ServerInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "CSRManagerServer.h"
#include "BLSCrypto.h"
#include "ServerInit.h"
#include "SGXException.h"
#include "SGXWalletServer.hpp"

void initUserSpace() {
Expand Down Expand Up @@ -104,23 +105,43 @@ void initEnclave(uint32_t _logLevel) {
spdlog::info("Enclave libtgmp library and logging initialized successfully");
}

void initAll(uint32_t _logLevel, bool _checkCert, bool _autoSign) {

static atomic<int> sgxServerInited(0);
void initAll(uint32_t _logLevel, bool _checkCert, bool _autoSign) {

cout << "Running sgxwallet version:" << SGXWalletServer::getVersion() << endl;
static atomic<bool> sgxServerInited(false);
static mutex initMutex;

CHECK_STATE(sgxServerInited != 1)
sgxServerInited = 1;
initEnclave(_logLevel);
initUserSpace();
initSEK();
lock_guard <mutex> lock(initMutex);

if (useHTTPS) {
SGXWalletServer::initHttpsServer(_checkCert);
SGXRegistrationServer::initRegistrationServer(_autoSign);
CSRManagerServer::initCSRManagerServer();
} else {
SGXWalletServer::initHttpServer();
if (sgxServerInited)
return;

try {

cout << "Running sgxwallet version:" << SGXWalletServer::getVersion() << endl;

CHECK_STATE(sgxServerInited != 1)
sgxServerInited = 1;
initEnclave(_logLevel);
initUserSpace();
initSEK();

if (useHTTPS) {
SGXWalletServer::initHttpsServer(_checkCert);
SGXRegistrationServer::initRegistrationServer(_autoSign);
CSRManagerServer::initCSRManagerServer();
} else {
SGXWalletServer::initHttpServer();
}
sgxServerInited = true;
} catch (SGXException &_e) {
spdlog::error(_e.getMessage());
} catch (exception &_e) {
spdlog::error(_e.what());
}
}
catch (...) {
exception_ptr p = current_exception();
printf("Exception %s \n", p.__cxa_exception_type()->name());
spdlog::error("Unknown exception");
}
};
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.56.0
1.57.0
Binary file not shown.
4 changes: 2 additions & 2 deletions run_sgx_sim/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3'
services:
sgxwallet:
image: skalenetwork/sgxwallet_sim:latest
image: skalenetwork/sgxwallet_sim:develop-latest
ports:
- "1026:1026"
- "1027:1027"
Expand All @@ -16,5 +16,5 @@ services:
max-size: "10m"
max-file: "4"
restart: unless-stopped
command: -s -y
command: -s

2 changes: 2 additions & 0 deletions secure_enclave/EnclaveCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ extern domain_parameters curve;
#define SAFE_FREE(__X__) if (__X__) {free(__X__); __X__ = NULL;}
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
#define RANDOM_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; get_global_random( \
(unsigned char*) __X__, __Y__);

#define CHECK_ARG_CLEAN(_EXPRESSION_) \
if (!(_EXPRESSION_)) { \
Expand Down
1 change: 1 addition & 0 deletions secure_enclave/SIGNED_ENCLAVE_VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define SIGNED_ENCLAVE_VERSION "0"
140 changes: 94 additions & 46 deletions secure_enclave/secure_enclave.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "EnclaveConstants.h"
#include "EnclaveCommon.h"
#include "SIGNED_ENCLAVE_VERSION"


#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
Expand Down Expand Up @@ -128,7 +130,24 @@ void trustedEnclaveInit(uint32_t _logLevel) {

enclave_init();

LOG_INFO("Successfully inited enclave");
LOG_INFO("Successfully inited enclave. Signed enclave version:" SIGNED_ENCLAVE_VERSION );
#ifndef SGX_DEBUG
LOG_INFO("SECURITY WARNING: sgxwallet is running in INSECURE DEBUG MODE! NEVER USE IN PRODUCTION!");
#endif

#if SGX_DEBUG != 0
LOG_INFO("SECURITY WARNING: sgxwallet is running in INSECURE DEBUG MODE! NEVER USE IN PRODUCTION!");
#endif

#if SGX_MODE == SIM
LOG_INFO("SECURITY WARNING: sgxwallet is running in INSECURE SIMULATION MODE! NEVER USE IN PRODUCTION!");
#endif






}

void free_function(void *ptr, size_t sz) {
Expand Down Expand Up @@ -189,37 +208,43 @@ void get_global_random(unsigned char *_randBuff, uint64_t _size) {
}


void trustedGenerateSEK(int *errStatus, char *errString,
uint8_t *encrypted_SEK, uint32_t *enc_len, char *SEK_hex) {
void sealHexSEK(int *errStatus, char *errString,
uint8_t *encrypted_sek, uint32_t *enc_len, char *sek_hex) {
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE

CHECK_STATE(encrypted_SEK);
CHECK_STATE(SEK_hex);

CHECK_STATE(encrypted_sek);
CHECK_STATE(sek_hex);

SAFE_CHAR_BUF(SEK_raw, SGX_AESGCM_KEY_SIZE);;
uint64_t plaintextLen = strlen(sek_hex + 1);

uint64_t sealedLen = sgx_calc_sealed_data_size(0, plaintextLen);

uint32_t hex_aes_key_length = SGX_AESGCM_KEY_SIZE * 2;
carray2Hex((uint8_t*) SEK_raw, SGX_AESGCM_KEY_SIZE, SEK_hex);
sgx_attributes_t attribute_mask;
attribute_mask.flags = 0xfffffffffffffff3;
attribute_mask.xfrm = 0x0;
sgx_misc_select_t misc = 0xF0000000;

uint32_t sealedLen = sgx_calc_sealed_data_size(0, hex_aes_key_length + 1);
sgx_status_t status = sgx_seal_data_ex(SGX_KEYPOLICY_MRENCLAVE, attribute_mask, misc, 0, NULL, plaintextLen, (uint8_t *) sek_hex, sealedLen,
(sgx_sealed_data_t *) encrypted_sek);
CHECK_STATUS("seal SEK failed after SEK generation");

for (uint8_t i = 0; i < 16; i++) {
AES_key[i] = SEK_raw[i];
}
uint32_t encrypt_text_length = sgx_get_encrypt_txt_len((const sgx_sealed_data_t *)encrypted_sek);

CHECK_STATE(encrypt_text_length = plaintextLen);

sgx_attributes_t attribute_mask;
attribute_mask.flags = 0xfffffffffffffff3;
attribute_mask.xfrm = 0x0;

sgx_misc_select_t misc = 0xF0000000;
SAFE_CHAR_BUF(unsealedKey, BUF_LEN);
uint32_t decLen = BUF_LEN;

sgx_status_t status = sgx_seal_data_ex(SGX_KEYPOLICY_MRENCLAVE, attribute_mask, misc, 0, NULL, hex_aes_key_length + 1, (uint8_t *) SEK_hex, sealedLen,
(sgx_sealed_data_t *) encrypted_SEK);
CHECK_STATUS("seal SEK failed");
uint32_t add_text_length = sgx_get_add_mac_txt_len((const sgx_sealed_data_t *)encrypted_sek);
CHECK_STATE(add_text_length == 0);
CHECK_STATE(sgx_is_within_enclave(encrypted_sek,sizeof(sgx_sealed_data_t)));
status = sgx_unseal_data((const sgx_sealed_data_t *)encrypted_sek, NULL, NULL,
(uint8_t *) unsealedKey, &decLen );

CHECK_STATUS("seal/unseal SEK failed after SEK generation in unseal");
*enc_len = sealedLen;

SET_SUCCESS
Expand All @@ -228,22 +253,55 @@ void trustedGenerateSEK(int *errStatus, char *errString,
LOG_INFO("SGX call completed");
}

void trustedSetSEK(int *errStatus, char *errString, uint8_t *encrypted_SEK) {
void trustedGenerateSEK(int *errStatus, char *errString,
uint8_t *encrypted_sek, uint32_t *enc_len, char *sek_hex) {
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encrypted_SEK);

CHECK_STATE(encrypted_sek);
CHECK_STATE(sek_hex);

RANDOM_CHAR_BUF(SEK_raw, SGX_AESGCM_KEY_SIZE);

carray2Hex((uint8_t*) SEK_raw, SGX_AESGCM_KEY_SIZE, sek_hex);
memcpy(AES_key, SEK_raw, SGX_AESGCM_KEY_SIZE);

sealHexSEK(errStatus, errString, encrypted_sek, enc_len, sek_hex);

if (errStatus != 0) {
LOG_ERROR("sealHexSEK failed");
goto clean;
}

SET_SUCCESS
clean:
;
LOG_INFO("SGX call completed");
}

void trustedSetSEK(int *errStatus, char *errString, uint8_t *encrypted_sek) {
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encrypted_sek);
SAFE_CHAR_BUF(aes_key_hex, BUF_LEN);

uint32_t dec_len;
uint32_t dec_len = BUF_LEN;

sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *) encrypted_SEK, NULL, 0,
(const sgx_sealed_data_t *) encrypted_sek, NULL, 0,
(uint8_t *)aes_key_hex, &dec_len);

if (status == 0x3001) {
LOG_ERROR("Could not decrypt LevelDB storage! \n"
"If you upgraded sgxwallet software or if you are restoring from backup, please run sgxwallet with -b flag and "
"pass your backup key.");
}

CHECK_STATUS2("sgx unseal SEK failed with status %d");

uint64_t len;


hex2carray(aes_key_hex, &len, (uint8_t *) AES_key);

SET_SUCCESS
Expand All @@ -253,39 +311,31 @@ void trustedSetSEK(int *errStatus, char *errString, uint8_t *encrypted_SEK) {
}

void trustedSetSEK_backup(int *errStatus, char *errString,
uint8_t *encrypted_SEK, uint32_t *enc_len, const char *SEK_hex) {
uint8_t *encrypted_sek, uint32_t *enc_len, const char *sek_hex) {
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE

CHECK_STATE(encrypted_SEK);
CHECK_STATE(SEK_hex);
CHECK_STATE(encrypted_sek);
CHECK_STATE(sek_hex);

uint64_t len;
hex2carray(SEK_hex, &len, (uint8_t *) AES_key);

uint32_t sealedLen = sgx_calc_sealed_data_size(0, strlen(SEK_hex) + 1);
hex2carray(sek_hex, &len, (uint8_t *) AES_key);

sealHexSEK(errStatus, errString, encrypted_sek, enc_len, (char *)sek_hex);

sgx_attributes_t attribute_mask;
attribute_mask.flags = 0xfffffffffffffff3;
attribute_mask.xfrm = 0x0;

sgx_misc_select_t misc = 0xF0000000;

sgx_status_t status = sgx_seal_data_ex(SGX_KEYPOLICY_MRENCLAVE,
attribute_mask, misc, 0, NULL, strlen(SEK_hex) + 1, (uint8_t *) SEK_hex, sealedLen,
(sgx_sealed_data_t *) encrypted_SEK);

CHECK_STATUS2("seal SEK failed with status %d")

*enc_len = sealedLen;
if (errStatus != 0) {
LOG_ERROR("sealHexSEK failed");
goto clean;
}

SET_SUCCESS
clean:
;
LOG_INFO("SGX call completed");
}



void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
uint8_t *encryptedPrivateKey, uint32_t *enc_len, char *pub_key_x, char *pub_key_y) {
LOG_INFO(__FUNCTION__);
Expand All @@ -295,7 +345,7 @@ void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
CHECK_STATE(pub_key_x);
CHECK_STATE(pub_key_y);

SAFE_CHAR_BUF(rand_char, 32);
RANDOM_CHAR_BUF(rand_char, 32);

mpz_t seed;
mpz_init(seed);
Expand All @@ -304,8 +354,6 @@ void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,

point Pkey = point_init();

get_global_random((unsigned char *)rand_char, 32);

mpz_import(seed, 32, 1, sizeof(rand_char[0]), 0, 0, rand_char);

mpz_mod(skey, seed, curve->p);
Expand Down
18 changes: 18 additions & 0 deletions testw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ class TestFixture {
}
};

class TestFixtureNoReset {
public:
TestFixtureNoReset() {
setOptions(L_INFO, false, true);
initAll(L_INFO, false, true);
}

~TestFixtureNoReset() {
TestUtils::destroyEnclave();
}
};

class TestFixtureHTTPS {
public:
TestFixtureHTTPS() {
Expand Down Expand Up @@ -691,4 +703,10 @@ TEST_CASE_METHOD(TestFixture, "Many threads ecdsa dkg bls", "[many-threads-crypt
}
}

TEST_CASE_METHOD(TestFixture, "First run", "[first-run]") {
}

TEST_CASE_METHOD(TestFixtureNoReset, "Second run", "[second-run]") {
}


5 changes: 3 additions & 2 deletions testw.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@

topDir = os.getcwd() + "/sgxwallet"
print("Top directory is:" + topDir)

testList = [ "[cert-sign]",
testList = ["[first-run]",
"[second-run]",
"[cert-sign]",
"[get-server-status]",
"[get-server-version]",
"[backup-key]",
Expand Down

0 comments on commit 5dc4b50

Please sign in to comment.