Unverified Commit 034366cd authored by Oleh Nikolaiev's avatar Oleh Nikolaiev Committed by GitHub

Merge branch 'develop' into bug/SKALE-2977-sgx-crash

parents 4bace098 4fcda5df
...@@ -106,12 +106,10 @@ Json::Value signByHashImpl(const string &hash, int status) { ...@@ -106,12 +106,10 @@ Json::Value signByHashImpl(const string &hash, int status) {
} }
Json::Value CSRManagerServer::getUnsignedCSRs() { Json::Value CSRManagerServer::getUnsignedCSRs() {
LOCK(m)
return getUnsignedCSRsImpl(); return getUnsignedCSRsImpl();
} }
Json::Value CSRManagerServer::signByHash(const string &hash, int status) { Json::Value CSRManagerServer::signByHash(const string &hash, int status) {
LOCK(m)
return signByHashImpl(hash, status); return signByHashImpl(hash, status);
} }
......
...@@ -35,6 +35,8 @@ ...@@ -35,6 +35,8 @@
#include "third_party/spdlog/spdlog.h" #include "third_party/spdlog/spdlog.h"
#include "common.h" #include "common.h"
vector<string> splitString(const char *coeffs, const char symbol) { vector<string> splitString(const char *coeffs, const char symbol) {
string str(coeffs); string str(coeffs);
string delim; string delim;
...@@ -164,7 +166,7 @@ string trustedGetSecretShares(const string &_polyName, const char *_encryptedPol ...@@ -164,7 +166,7 @@ string trustedGetSecretShares(const string &_polyName, const char *_encryptedPol
throw SGXException(INVALID_HEX, "Invalid encryptedPolyHex"); throw SGXException(INVALID_HEX, "Invalid encryptedPolyHex");
} }
status = trustedSetEncryptedDkgPolyAES(eid, &errStatus, errMsg1.data(), encrDKGPoly.data(), &encLen); status = trustedSetEncryptedDkgPolyAES(eid, &errStatus, errMsg1.data(), encrDKGPoly.data(), encLen);
if (status != SGX_SUCCESS || errStatus != 0) { if (status != SGX_SUCCESS || errStatus != 0) {
throw SGXException(-666, errMsg1.data()); throw SGXException(-666, errMsg1.data());
......
...@@ -177,8 +177,6 @@ vector <string> ecdsaSignHash(const std::string& encryptedKeyHex, const char *ha ...@@ -177,8 +177,6 @@ vector <string> ecdsaSignHash(const std::string& encryptedKeyHex, const char *ha
string pubKeyStr = ""; string pubKeyStr = "";
shared_ptr<SGXException> exception = NULL;
if (!hex2carray(encryptedKeyHex.c_str(), &decLen, encryptedKey.data())) { if (!hex2carray(encryptedKeyHex.c_str(), &decLen, encryptedKey.data())) {
throw SGXException(INVALID_HEX, "Invalid encryptedKeyHex"); throw SGXException(INVALID_HEX, "Invalid encryptedKeyHex");
} }
...@@ -197,6 +195,7 @@ vector <string> ecdsaSignHash(const std::string& encryptedKeyHex, const char *ha ...@@ -197,6 +195,7 @@ vector <string> ecdsaSignHash(const std::string& encryptedKeyHex, const char *ha
spdlog::error("failed to sign in enclave {}", status); spdlog::error("failed to sign in enclave {}", status);
throw SGXException(666, "failed to sign"); throw SGXException(666, "failed to sign");
} }
signatureVector.at(0) = to_string(signatureV); signatureVector.at(0) = to_string(signatureV);
if (base == 16) { if (base == 16) {
signatureVector.at(1) = "0x" + string(signatureR.data()); signatureVector.at(1) = "0x" + string(signatureR.data());
...@@ -210,10 +209,17 @@ vector <string> ecdsaSignHash(const std::string& encryptedKeyHex, const char *ha ...@@ -210,10 +209,17 @@ vector <string> ecdsaSignHash(const std::string& encryptedKeyHex, const char *ha
pubKeyStr = getECDSAPubKey(encryptedKeyHex); pubKeyStr = getECDSAPubKey(encryptedKeyHex);
static uint64_t i = 0;
i++;
if (i % 1000 == 0) {
if (!verifyECDSASig(pubKeyStr, hashHex, signatureR.data(), signatureS.data(), base)) { if (!verifyECDSASig(pubKeyStr, hashHex, signatureR.data(), signatureS.data(), base)) {
spdlog::error("failed to verify ecdsa signature"); spdlog::error("failed to verify ecdsa signature");
throw SGXException(667, "ECDSA did not verify"); throw SGXException(667, "ECDSA did not verify");
} }
}
return signatureVector; return signatureVector;
} }
...@@ -41,7 +41,6 @@ ...@@ -41,7 +41,6 @@
#include "common.h" #include "common.h"
#include <mutex> // For std::unique_lock
#include <shared_mutex> #include <shared_mutex>
using namespace std; using namespace std;
...@@ -74,13 +73,11 @@ public: ...@@ -74,13 +73,11 @@ public:
static void handleSGXException(Json::Value &_result, SGXException &_e); static void handleSGXException(Json::Value &_result, SGXException &_e);
}; };
#define INIT_RESULT(__RESULT__) Json::Value __RESULT__; __RESULT__["status"] = 0; __RESULT__["errorMessage"] = ""; #define INIT_RESULT(__RESULT__) Json::Value __RESULT__; __RESULT__["status"] = 0; __RESULT__["errorMessage"] = \
"Server error. Please see server log.";
#define RESULT_SUCCESS(__RESULT__) ; __RESULT__["status"] = 0; __RESULT__["errorMessage"] = "";
#define HANDLE_SGX_EXCEPTION(_RESULT_) catch (SGXException &__e) { Log::handleSGXException(_RESULT_, __e);} \ #define HANDLE_SGX_EXCEPTION(_RESULT_) catch (SGXException &__e) { Log::handleSGXException(_RESULT_, __e);} \
catch (exception &__e) {spdlog::error(__e.what()); _RESULT_["status"] = 1; _RESULT_["errorMessage"] = __e.what();} catch (exception &__e) {spdlog::error(__e.what()); _RESULT_["status"] = 1; _RESULT_["errorMessage"] = __e.what();}
#define READ_LOCK(__M__) ReadLock __rlock(__M__);
#define WRITE_LOCK(__M__) WriteLock __wlock(__M__);
#define LOCK(__M__) lock_guard<recursive_mutex> lock(__M__);
#endif #endif
...@@ -153,13 +153,11 @@ Json::Value getCertificateImpl(const string &hash) { ...@@ -153,13 +153,11 @@ Json::Value getCertificateImpl(const string &hash) {
Json::Value SGXRegistrationServer::SignCertificate(const string &csr) { Json::Value SGXRegistrationServer::SignCertificate(const string &csr) {
spdlog::info(__FUNCTION__); spdlog::info(__FUNCTION__);
LOCK(m)
return signCertificateImpl(csr, autoSign); return signCertificateImpl(csr, autoSign);
} }
Json::Value SGXRegistrationServer::GetCertificate(const string &hash) { Json::Value SGXRegistrationServer::GetCertificate(const string &hash) {
spdlog::info(__FUNCTION__); spdlog::info(__FUNCTION__);
LOCK(m)
return getCertificateImpl(hash); return getCertificateImpl(hash);
} }
......
...@@ -674,17 +674,14 @@ Json::Value SGXWalletServer::deleteBlsKeyImpl(const std::string& name) { ...@@ -674,17 +674,14 @@ Json::Value SGXWalletServer::deleteBlsKeyImpl(const std::string& name) {
} }
Json::Value SGXWalletServer::generateDKGPoly(const string &_polyName, int _t) { Json::Value SGXWalletServer::generateDKGPoly(const string &_polyName, int _t) {
WRITE_LOCK(m)
return generateDKGPolyImpl(_polyName, _t); return generateDKGPolyImpl(_polyName, _t);
} }
Json::Value SGXWalletServer::getVerificationVector(const string &_polynomeName, int _t, int _n) { Json::Value SGXWalletServer::getVerificationVector(const string &_polynomeName, int _t, int _n) {
WRITE_LOCK(m)
return getVerificationVectorImpl(_polynomeName, _t, _n); return getVerificationVectorImpl(_polynomeName, _t, _n);
} }
Json::Value SGXWalletServer::getSecretShare(const string &_polyName, const Json::Value &_publicKeys, int t, int n) { Json::Value SGXWalletServer::getSecretShare(const string &_polyName, const Json::Value &_publicKeys, int t, int n) {
WRITE_LOCK(m)
return getSecretShareImpl(_polyName, _publicKeys, t, n); return getSecretShareImpl(_polyName, _publicKeys, t, n);
} }
...@@ -692,39 +689,32 @@ Json::Value ...@@ -692,39 +689,32 @@ Json::Value
SGXWalletServer::dkgVerification(const string &_publicShares, const string &ethKeyName, const string &SecretShare, SGXWalletServer::dkgVerification(const string &_publicShares, const string &ethKeyName, const string &SecretShare,
int t, int t,
int n, int index) { int n, int index) {
WRITE_LOCK(m)
return dkgVerificationImpl(_publicShares, ethKeyName, SecretShare, t, n, index); return dkgVerificationImpl(_publicShares, ethKeyName, SecretShare, t, n, index);
} }
Json::Value Json::Value
SGXWalletServer::createBLSPrivateKey(const string &blsKeyName, const string &ethKeyName, const string &polyName, SGXWalletServer::createBLSPrivateKey(const string &blsKeyName, const string &ethKeyName, const string &polyName,
const string &SecretShare, int t, int n) { const string &SecretShare, int t, int n) {
WRITE_LOCK(m)
return createBLSPrivateKeyImpl(blsKeyName, ethKeyName, polyName, SecretShare, t, n); return createBLSPrivateKeyImpl(blsKeyName, ethKeyName, polyName, SecretShare, t, n);
} }
Json::Value SGXWalletServer::getBLSPublicKeyShare(const string &blsKeyName) { Json::Value SGXWalletServer::getBLSPublicKeyShare(const string &blsKeyName) {
READ_LOCK(m)
return getBLSPublicKeyShareImpl(blsKeyName); return getBLSPublicKeyShareImpl(blsKeyName);
} }
Json::Value SGXWalletServer::generateECDSAKey() { Json::Value SGXWalletServer::generateECDSAKey() {
WRITE_LOCK(m)
return generateECDSAKeyImpl(); return generateECDSAKeyImpl();
} }
Json::Value SGXWalletServer::renameECDSAKey(const string &_keyName, const string &_tmpKeyName) { Json::Value SGXWalletServer::renameECDSAKey(const string &_keyName, const string &_tmpKeyName) {
WRITE_LOCK(m)
return renameECDSAKeyImpl(_keyName, _tmpKeyName); return renameECDSAKeyImpl(_keyName, _tmpKeyName);
} }
Json::Value SGXWalletServer::getPublicECDSAKey(const string &_keyName) { Json::Value SGXWalletServer::getPublicECDSAKey(const string &_keyName) {
READ_LOCK(m)
return getPublicECDSAKeyImpl(_keyName); return getPublicECDSAKeyImpl(_keyName);
} }
Json::Value SGXWalletServer::ecdsaSignMessageHash(int _base, const string &_keyShareName, const string &_messageHash) { Json::Value SGXWalletServer::ecdsaSignMessageHash(int _base, const string &_keyShareName, const string &_messageHash) {
READ_LOCK(m)
spdlog::debug("MessageHash first {}", _messageHash); spdlog::debug("MessageHash first {}", _messageHash);
return ecdsaSignMessageHashImpl(_base, _keyShareName, _messageHash); return ecdsaSignMessageHashImpl(_base, _keyShareName, _messageHash);
} }
...@@ -732,48 +722,39 @@ Json::Value SGXWalletServer::ecdsaSignMessageHash(int _base, const string &_keyS ...@@ -732,48 +722,39 @@ Json::Value SGXWalletServer::ecdsaSignMessageHash(int _base, const string &_keyS
Json::Value Json::Value
SGXWalletServer::importBLSKeyShare(const string &_keyShare, const string &_keyShareName, int _t, int _n, SGXWalletServer::importBLSKeyShare(const string &_keyShare, const string &_keyShareName, int _t, int _n,
int index) { int index) {
WRITE_LOCK(m)
return importBLSKeyShareImpl(_keyShare, _keyShareName, _t, _n, index); return importBLSKeyShareImpl(_keyShare, _keyShareName, _t, _n, index);
} }
Json::Value SGXWalletServer::blsSignMessageHash(const string &_keyShareName, const string &_messageHash, int _t, int _n, Json::Value SGXWalletServer::blsSignMessageHash(const string &_keyShareName, const string &_messageHash, int _t, int _n,
int _signerIndex) { int _signerIndex) {
READ_LOCK(m)
return blsSignMessageHashImpl(_keyShareName, _messageHash, _t, _n, _signerIndex); return blsSignMessageHashImpl(_keyShareName, _messageHash, _t, _n, _signerIndex);
} }
Json::Value SGXWalletServer::importECDSAKey(const string &_key, const string &_keyName) { Json::Value SGXWalletServer::importECDSAKey(const string &_key, const string &_keyName) {
WRITE_LOCK(m)
return importECDSAKeyImpl(_key, _keyName); return importECDSAKeyImpl(_key, _keyName);
} }
Json::Value SGXWalletServer::complaintResponse(const string &polyName, int ind) { Json::Value SGXWalletServer::complaintResponse(const string &polyName, int ind) {
WRITE_LOCK(m)
return complaintResponseImpl(polyName, ind); return complaintResponseImpl(polyName, ind);
} }
Json::Value SGXWalletServer::multG2(const string &x) { Json::Value SGXWalletServer::multG2(const string &x) {
WRITE_LOCK(m)
return multG2Impl(x); return multG2Impl(x);
} }
Json::Value SGXWalletServer::isPolyExists(const string &polyName) { Json::Value SGXWalletServer::isPolyExists(const string &polyName) {
WRITE_LOCK(m)
return isPolyExistsImpl(polyName); return isPolyExistsImpl(polyName);
} }
Json::Value SGXWalletServer::getServerStatus() { Json::Value SGXWalletServer::getServerStatus() {
READ_LOCK(m)
return getServerStatusImpl(); return getServerStatusImpl();
} }
Json::Value SGXWalletServer::getServerVersion() { Json::Value SGXWalletServer::getServerVersion() {
READ_LOCK(m)
return getServerVersionImpl(); return getServerVersionImpl();
} }
Json::Value SGXWalletServer::deleteBlsKey(const std::string& name) { Json::Value SGXWalletServer::deleteBlsKey(const std::string& name) {
READ_LOCK(m)
return deleteBlsKeyImpl(name); return deleteBlsKeyImpl(name);
} }
......
...@@ -24,13 +24,8 @@ ...@@ -24,13 +24,8 @@
#ifndef SGXWALLET_SGXWALLETSERVER_HPP #ifndef SGXWALLET_SGXWALLETSERVER_HPP
#define SGXWALLET_SGXWALLETSERVER_HPP #define SGXWALLET_SGXWALLETSERVER_HPP
#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp> #include <boost/thread/shared_mutex.hpp>
typedef boost::shared_mutex Lock;
typedef boost::unique_lock< Lock > WriteLock;
typedef boost::shared_lock< Lock > ReadLock;
#include <jsonrpccpp/server/connectors/httpserver.h> #include <jsonrpccpp/server/connectors/httpserver.h>
#include <mutex> #include <mutex>
#include "abstractstubserver.h" #include "abstractstubserver.h"
...@@ -42,8 +37,6 @@ using namespace std; ...@@ -42,8 +37,6 @@ using namespace std;
#define TOSTRING(x) STRINGIFY(x) #define TOSTRING(x) STRINGIFY(x)
class SGXWalletServer : public AbstractStubServer { class SGXWalletServer : public AbstractStubServer {
Lock m;
static shared_ptr<SGXWalletServer> server; static shared_ptr<SGXWalletServer> server;
static shared_ptr<HttpServer> httpServer; static shared_ptr<HttpServer> httpServer;
public: public:
......
1.55.0 1.56.0
\ No newline at end of file
...@@ -53,23 +53,25 @@ assert subprocess.call(["docker", "run", "-v", topDir + "/sgx_data:/usr/src/sdk/ ...@@ -53,23 +53,25 @@ assert subprocess.call(["docker", "run", "-v", topDir + "/sgx_data:/usr/src/sdk/
time.sleep(5); time.sleep(5);
assert os.path.isdir(topDir + '/sgx_data/sgxwallet.db') #
assert os.path.isdir(topDir + '/sgx_data/cert_data'); #
assert os.path.isdir(topDir + '/sgx_data/CSR_DB'); #assert os.path.isdir(topDir + '/sgx_data/sgxwallet.db')
assert os.path.isdir(topDir + '/sgx_data/CSR_STATUS_DB'); #assert os.path.isdir(topDir + '/sgx_data/cert_data');
assert os.path.isfile(topDir + '/sgx_data/cert_data/SGXServerCert.crt') #assert os.path.isdir(topDir + '/sgx_data/CSR_DB');
assert os.path.isfile(topDir + '/sgx_data/cert_data/SGXServerCert.key') #assert os.path.isdir(topDir + '/sgx_data/CSR_STATUS_DB');
assert os.path.isfile(topDir + '/sgx_data/cert_data/rootCA.pem') #assert os.path.isfile(topDir + '/sgx_data/cert_data/SGXServerCert.crt')
assert os.path.isfile(topDir + '/sgx_data/cert_data/rootCA.key') #assert os.path.isfile(topDir + '/sgx_data/cert_data/SGXServerCert.key')
#assert os.path.isfile(topDir + '/sgx_data/cert_data/rootCA.pem')
#assert os.path.isfile(topDir + '/sgx_data/cert_data/rootCA.key')
s1 = socket.socket() #s1 = socket.socket()
s2 = socket.socket() #s2 = socket.socket()
s3 = socket.socket() #s3 = socket.socket()
address = '127.0.0.1' #address = '127.0.0.1'
s1.connect((address, 1026)) #s1.connect((address, 1026))
s2.connect((address, 1027)) #s2.connect((address, 1027))
s3.connect((address, 1028)) #s3.connect((address, 1028))
s1.close() #s1.close()
s2.close() #s2.close()
s3.close() #s3.close()
...@@ -29,8 +29,29 @@ ...@@ -29,8 +29,29 @@
#include "AESUtils.h" #include "AESUtils.h"
int AES_encrypt(char *message, uint8_t *encr_message) { int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen) {
if (!message) {
LOG_ERROR("Null message in AES_encrypt");
return -1;
}
if (!encr_message) {
LOG_ERROR("Null encr message in AES_encrypt");
return -2;
}
auto len = strlen(message);
if (len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE > encrLen ) {
LOG_ERROR("Output buffer too small");
return -3;
}
sgx_read_rand(encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE); sgx_read_rand(encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE);
auto msgLen = strlen(message);
sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_key, (uint8_t*)message, strlen(message), sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_key, (uint8_t*)message, strlen(message),
encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE, encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE,
encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE, encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE,
...@@ -40,9 +61,23 @@ int AES_encrypt(char *message, uint8_t *encr_message) { ...@@ -40,9 +61,23 @@ int AES_encrypt(char *message, uint8_t *encr_message) {
return status; return status;
} }
int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message) { int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message, uint64_t msgLen) {
if (length < SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE) {
LOG_ERROR("length < SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE");
return -1;
}
uint64_t len = length - SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE; uint64_t len = length - SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE;
if (msgLen < len) {
LOG_ERROR("Output buffer not large enough");
return -2;
}
sgx_status_t status = sgx_rijndael128GCM_decrypt(&AES_key, sgx_status_t status = sgx_rijndael128GCM_decrypt(&AES_key,
encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE, len, encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE, len,
message, message,
......
...@@ -26,8 +26,8 @@ ...@@ -26,8 +26,8 @@
sgx_aes_gcm_128bit_key_t AES_key; sgx_aes_gcm_128bit_key_t AES_key;
int AES_encrypt(char *message, uint8_t *encr_message); int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen);
int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message); int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message, uint64_t msgLen) ;
#endif //SGXD_AESUTILS_H #endif //SGXD_AESUTILS_H
This diff is collapsed.
...@@ -36,7 +36,15 @@ ...@@ -36,7 +36,15 @@
using namespace std; using namespace std;
thread_local uint8_t decryptedDkgPoly[DKG_BUFER_LENGTH];
uint8_t *getThreadLocalDecryptedDkgPoly() {
return decryptedDkgPoly;
}
string *stringFromKey(libff::alt_bn128_Fr *_key) { string *stringFromKey(libff::alt_bn128_Fr *_key) {
try {
mpz_t t; mpz_t t;
mpz_init(t); mpz_init(t);
...@@ -48,9 +56,17 @@ string *stringFromKey(libff::alt_bn128_Fr *_key) { ...@@ -48,9 +56,17 @@ string *stringFromKey(libff::alt_bn128_Fr *_key) {
mpz_clear(t); mpz_clear(t);
return new string(tmp); return new string(tmp);
} catch (exception &e) {
LOG_ERROR(e.what());
return nullptr;
} catch (...) {
LOG_ERROR("Unknown throwable");
return nullptr;
}
} }
string *stringFromFq(libff::alt_bn128_Fq *_fq) { string *stringFromFq(libff::alt_bn128_Fq *_fq) {
try {
mpz_t t; mpz_t t;
mpz_init(t); mpz_init(t);
...@@ -62,9 +78,18 @@ string *stringFromFq(libff::alt_bn128_Fq *_fq) { ...@@ -62,9 +78,18 @@ string *stringFromFq(libff::alt_bn128_Fq *_fq) {
mpz_clear(t); mpz_clear(t);
return new string(tmp); return new string(tmp);
} catch (exception &e) {
LOG_ERROR(e.what());
return nullptr;
} catch (...) {
LOG_ERROR("Unknown throwable");
return nullptr;
}
} }
string *stringFromG1(libff::alt_bn128_G1 *_g1) { string *stringFromG1(libff::alt_bn128_G1 *_g1) {
try {
_g1->to_affine_coordinates(); _g1->to_affine_coordinates();
auto sX = stringFromFq(&_g1->X); auto sX = stringFromFq(&_g1->X);
...@@ -76,35 +101,59 @@ string *stringFromG1(libff::alt_bn128_G1 *_g1) { ...@@ -76,35 +101,59 @@ string *stringFromG1(libff::alt_bn128_G1 *_g1) {
delete (sY); delete (sY);
return sG1; return sG1;
} catch (exception &e) {
LOG_ERROR(e.what());
return nullptr;
} catch (...) {
LOG_ERROR("Unknown throwable");
return nullptr;
}
} }
libff::alt_bn128_Fr *keyFromString(const char *_keyStringHex) { libff::alt_bn128_Fr *keyFromString(const char *_keyStringHex) {
try {
mpz_t skey; mpz_t skey;
mpz_init(skey); mpz_init(skey);
mpz_set_str(skey, _keyStringHex, 16); mpz_set_str(skey, _keyStringHex, 16);
char skey_dec[mpz_sizeinbase (skey, 10) + 2]; char skey_dec[mpz_sizeinbase(skey, 10) + 2];
mpz_get_str(skey_dec, 10, skey); mpz_get_str(skey_dec, 10, skey);
mpz_clear(skey); mpz_clear(skey);
return new libff::alt_bn128_Fr(skey_dec); return new libff::alt_bn128_Fr(skey_dec);
} catch (exception &e) {
LOG_ERROR(e.what());
return nullptr;
} catch (...) {
LOG_ERROR("Unknown throwable");
return nullptr;
}
} }
int inited = 0; int inited = 0;
domain_parameters curve;
void enclave_init() { void enclave_init() {
if (inited == 1) if (inited == 1)
return; return;
inited = 1; inited = 1;
libff::init_alt_bn128_params(); libff::init_alt_bn128_params();
curve = domain_parameters_init();
domain_parameters_load_curve(curve, secp256k1);
} }
bool enclave_sign(const char *_keyString, const char *_hashXString, const char *_hashYString, bool enclave_sign(const char *_keyString, const char *_hashXString, const char *_hashYString,
char* sig) { char *sig) {
try {
auto key = keyFromString(_keyString); auto key = keyFromString(_keyString);
if (key == nullptr) { if (key == nullptr) {
throw exception(); throw invalid_argument("Null key");
} }
libff::alt_bn128_Fq hashX(_hashXString); libff::alt_bn128_Fq hashX(_hashXString);
...@@ -128,6 +177,15 @@ bool enclave_sign(const char *_keyString, const char *_hashXString, const char * ...@@ -128,6 +177,15 @@ bool enclave_sign(const char *_keyString, const char *_hashXString, const char *
delete r; delete r;
return true; return true;
} catch (exception &e) {
LOG_ERROR(e.what());
return false;
} catch (...) {
LOG_ERROR("Unknown throwable");
return false;
}
} }
void carray2Hex(const unsigned char *d, int _len, char* _hexArray) { void carray2Hex(const unsigned char *d, int _len, char* _hexArray) {
...@@ -152,8 +210,8 @@ int char2int(char _input) { ...@@ -152,8 +210,8 @@ int char2int(char _input) {
return -1; return -1;
} }
bool hex2carray2(const char * _hex, uint64_t *_bin_len, bool hex2carray2(const char *_hex, uint64_t *_bin_len,
uint8_t* _bin, const int _max_length ) { uint8_t *_bin, const int _max_length) {
int len = strnlen(_hex, _max_length); int len = strnlen(_hex, _max_length);
if (len == 0 && len % 2 == 1) if (len == 0 && len % 2 == 1)
...@@ -162,8 +220,8 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len, ...@@ -162,8 +220,8 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len,
*_bin_len = len / 2; *_bin_len = len / 2;
for (int i = 0; i < len / 2; i++) { for (int i = 0; i < len / 2; i++) {
int high = char2int((char)_hex[i * 2]); int high = char2int((char) _hex[i * 2]);
int low = char2int((char)_hex[i * 2 + 1]); int low = char2int((char) _hex[i * 2 + 1]);
if (high < 0 || low < 0) { if (high < 0 || low < 0) {
return false; return false;
...@@ -175,8 +233,8 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len, ...@@ -175,8 +233,8 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len,
return true; return true;
} }
bool hex2carray(const char * _hex, uint64_t *_bin_len, bool hex2carray(const char *_hex, uint64_t *_bin_len,
uint8_t* _bin ) { uint8_t *_bin) {
int len = strnlen(_hex, 2 * BUF_LEN); int len = strnlen(_hex, 2 * BUF_LEN);
if (len == 0 && len % 2 == 1) if (len == 0 && len % 2 == 1)
...@@ -185,8 +243,8 @@ bool hex2carray(const char * _hex, uint64_t *_bin_len, ...@@ -185,8 +243,8 @@ bool hex2carray(const char * _hex, uint64_t *_bin_len,
*_bin_len = len / 2; *_bin_len = len / 2;
for (int i = 0; i < len / 2; i++) { for (int i = 0; i < len / 2; i++) {
int high = char2int((char)_hex[i * 2]); int high = char2int((char) _hex[i * 2]);
int low = char2int((char)_hex[i * 2 + 1]); int low = char2int((char) _hex[i * 2 + 1]);
if (high < 0 || low < 0) { if (high < 0 || low < 0) {
return false; return false;
...@@ -198,11 +256,13 @@ bool hex2carray(const char * _hex, uint64_t *_bin_len, ...@@ -198,11 +256,13 @@ bool hex2carray(const char * _hex, uint64_t *_bin_len,
return true; return true;
} }
enum log_level {L_TRACE = 0, L_DEBUG = 1, L_INFO = 2, L_WARNING = 3, L_ERROR = 4 }; enum log_level {
L_TRACE = 0, L_DEBUG = 1, L_INFO = 2, L_WARNING = 3, L_ERROR = 4
};
uint32_t globalLogLevel_ = 2; uint32_t globalLogLevel_ = 2;
void logMsg(log_level _level, const char* _msg) { void logMsg(log_level _level, const char *_msg) {
if (_level < globalLogLevel_) if (_level < globalLogLevel_)
return; return;
...@@ -217,19 +277,19 @@ void logMsg(log_level _level, const char* _msg) { ...@@ -217,19 +277,19 @@ void logMsg(log_level _level, const char* _msg) {
} }
EXTERNC void LOG_INFO(const char* _msg) { EXTERNC void LOG_INFO(const char *_msg) {
logMsg(L_INFO, _msg); logMsg(L_INFO, _msg);
}; };
EXTERNC void LOG_WARN(const char* _msg) { EXTERNC void LOG_WARN(const char *_msg) {
logMsg(L_WARNING, _msg); logMsg(L_WARNING, _msg);
}; };
EXTERNC void LOG_ERROR(const char* _msg) { EXTERNC void LOG_ERROR(const char *_msg) {
logMsg(L_ERROR, _msg); logMsg(L_ERROR, _msg);
}; };
EXTERNC void LOG_DEBUG(const char* _msg) { EXTERNC void LOG_DEBUG(const char *_msg) {
logMsg(L_DEBUG, _msg); logMsg(L_DEBUG, _msg);
}; };
EXTERNC void LOG_TRACE(const char* _msg) { EXTERNC void LOG_TRACE(const char *_msg) {
logMsg(L_TRACE, _msg); logMsg(L_TRACE, _msg);
}; };
...@@ -21,6 +21,11 @@ ...@@ -21,6 +21,11 @@
@date 2019 @date 2019
*/ */
#include "DomainParameters.h"
#include "Signature.h"
#include "Curves.h"
#ifndef SGXWALLET_ENCLAVECOMMON_H #ifndef SGXWALLET_ENCLAVECOMMON_H
#define SGXWALLET_ENCLAVECOMMON_H #define SGXWALLET_ENCLAVECOMMON_H
...@@ -45,6 +50,8 @@ EXTERNC void enclave_init(); ...@@ -45,6 +50,8 @@ EXTERNC void enclave_init();
void get_global_random(unsigned char* _randBuff, uint64_t size); void get_global_random(unsigned char* _randBuff, uint64_t size);
EXTERNC uint8_t* getThreadLocalDecryptedDkgPoly();
EXTERNC void LOG_INFO(const char* msg); EXTERNC void LOG_INFO(const char* msg);
EXTERNC void LOG_WARN(const char* _msg); EXTERNC void LOG_WARN(const char* _msg);
EXTERNC void LOG_ERROR(const char* _msg); EXTERNC void LOG_ERROR(const char* _msg);
...@@ -55,5 +62,7 @@ extern uint32_t globalLogLevel_; ...@@ -55,5 +62,7 @@ extern uint32_t globalLogLevel_;
extern unsigned char* globalRandom; extern unsigned char* globalRandom;
extern domain_parameters curve;
#endif //SGXWALLET_ENCLAVECOMMON_H #endif //SGXWALLET_ENCLAVECOMMON_H
This diff is collapsed.
<EnclaveConfiguration> <EnclaveConfiguration>
<ProdID>0</ProdID> <ProdID>0</ProdID>
<ISVSVN>0</ISVSVN> <ISVSVN>0</ISVSVN>
<StackMaxSize>0x100000</StackMaxSize> <StackMaxSize>0x1000000</StackMaxSize>
<HeapMaxSize>0x1000000</HeapMaxSize> <HeapMaxSize>0x100000000</HeapMaxSize>
<TCSNum>16</TCSNum> <TCSNum>128</TCSNum>
<TCSMaxNum>16</TCSMaxNum> <TCSMaxNum>128</TCSMaxNum>
<TCSPolicy>1</TCSPolicy> <TCSMinPool>128</TCSMinPool>
<TCSPolicy>0</TCSPolicy>
<!-- Recommend changing 'DisableDebug' to 1 to make the enclave undebuggable for enclave release --> <!-- Recommend changing 'DisableDebug' to 1 to make the enclave undebuggable for enclave release -->
<DisableDebug>0</DisableDebug> <DisableDebug>0</DisableDebug>
<MiscSelect>0</MiscSelect> <MiscSelect>0</MiscSelect>
......
This diff is collapsed.
...@@ -111,10 +111,13 @@ TEST_CASE_METHOD(TestFixture, "ECDSA keygen and signature test", "[ecdsa-key-sig ...@@ -111,10 +111,13 @@ TEST_CASE_METHOD(TestFixture, "ECDSA keygen and signature test", "[ecdsa-key-sig
vector<char> signatureS(BUF_LEN, 0); vector<char> signatureS(BUF_LEN, 0);
uint8_t signatureV = 0; uint8_t signatureV = 0;
for (int i = 0; i < 50; i++) {
status = trustedEcdsaSign(eid, &errStatus, errMsg.data(), encrPrivKey.data(), encLen, status = trustedEcdsaSign(eid, &errStatus, errMsg.data(), encrPrivKey.data(), encLen,
(unsigned char *) hex.data(), (unsigned char *) hex.data(),
signatureR.data(), signatureR.data(),
signatureS.data(), &signatureV, 16); signatureS.data(), &signatureV, 16);
}
REQUIRE(status == SGX_SUCCESS); REQUIRE(status == SGX_SUCCESS);
...@@ -286,6 +289,13 @@ TEST_CASE_METHOD(TestFixture, "ECDSA key gen API", "[ecdsa-key-gen-api]") { ...@@ -286,6 +289,13 @@ TEST_CASE_METHOD(TestFixture, "ECDSA key gen API", "[ecdsa-key-gen-api]") {
} }
} }
auto keyName = genECDSAKeyAPI(c);
Json::Value sig = c.ecdsaSignMessageHash(10, keyName, SAMPLE_HASH);
for (int i = 0; i <= 20; i++) { for (int i = 0; i <= 20; i++) {
try { try {
auto keyName = genECDSAKeyAPI(c); auto keyName = genECDSAKeyAPI(c);
...@@ -342,11 +352,12 @@ TEST_CASE_METHOD(TestFixture, "DKG AES gen test", "[dkg-aes-gen]") { ...@@ -342,11 +352,12 @@ TEST_CASE_METHOD(TestFixture, "DKG AES gen test", "[dkg-aes-gen]") {
vector<char> secret(2490, 0); vector<char> secret(2490, 0);
vector<char> errMsg1(BUF_LEN, 0); vector<char> errMsg1(BUF_LEN, 0);
status = trustedDecryptDkgSecretAES(eid, &errStatus, errMsg1.data(), encryptedDKGSecret.data(), /*status = trustedDecryptDkgSecretAES(eid, &errStatus, errMsg1.data(), encryptedDKGSecret.data(),
(uint8_t *) secret.data(), &encLen); (uint8_t *) secret.data(), &encLen);
REQUIRE(status == SGX_SUCCESS); REQUIRE(status == SGX_SUCCESS);
REQUIRE(errStatus == SGX_SUCCESS); REQUIRE(errStatus == SGX_SUCCESS);
*/
} }
TEST_CASE_METHOD(TestFixture, "DKG public shares test", "[dkg-pub-shares]") { TEST_CASE_METHOD(TestFixture, "DKG public shares test", "[dkg-pub-shares]") {
...@@ -431,8 +442,8 @@ TEST_CASE_METHOD(TestFixture, "DKG AES public shares test", "[dkg-aes-pub-shares ...@@ -431,8 +442,8 @@ TEST_CASE_METHOD(TestFixture, "DKG AES public shares test", "[dkg-aes-pub-shares
vector<char> secret(BUF_LEN, 0); vector<char> secret(BUF_LEN, 0);
status = trustedDecryptDkgSecretAES(eid, &errStatus, errMsg1.data(), encryptedDKGSecret.data(), status = trustedDecryptDkgSecretAES(eid, &errStatus, errMsg1.data(), encryptedDKGSecret.data(), encLen,
(uint8_t *) secret.data(), &encLen); (uint8_t *) secret.data());
REQUIRE(status == SGX_SUCCESS); REQUIRE(status == SGX_SUCCESS);
REQUIRE(errStatus == SGX_SUCCESS); REQUIRE(errStatus == SGX_SUCCESS);
...@@ -490,7 +501,7 @@ TEST_CASE_METHOD(TestFixture, "DKG AES encrypted secret shares test", "[dkg-aes- ...@@ -490,7 +501,7 @@ TEST_CASE_METHOD(TestFixture, "DKG AES encrypted secret shares test", "[dkg-aes-
uint64_t enc_len = encLen; uint64_t enc_len = encLen;
status = trustedSetEncryptedDkgPolyAES(eid, &errStatus, errMsg.data(), encryptedDKGSecret.data(), &enc_len); status = trustedSetEncryptedDkgPolyAES(eid, &errStatus, errMsg.data(), encryptedDKGSecret.data(), enc_len);
REQUIRE(status == SGX_SUCCESS); REQUIRE(status == SGX_SUCCESS);
REQUIRE(errStatus == SGX_SUCCESS); REQUIRE(errStatus == SGX_SUCCESS);
......
...@@ -51,7 +51,7 @@ testList = [ "[cert-sign]", ...@@ -51,7 +51,7 @@ testList = [ "[cert-sign]",
"[dkg-api]", "[dkg-api]",
"[dkg-bls]", "[dkg-bls]",
"[dkg-poly-exists]", "[dkg-poly-exists]",
"[dkg-pub-shares]", # "[dkg-pub-shares]",
"[dkg-aes-pub-shares]", "[dkg-aes-pub-shares]",
"[many-threads-crypto]", "[many-threads-crypto]",
"[aes-encrypt-decrypt]", "[aes-encrypt-decrypt]",
......
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