Unverified Commit 17dca977 authored by kladko's avatar kladko

SKALE-3213

parent a71e43c2
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
#include "third_party/spdlog/spdlog.h" #include "third_party/spdlog/spdlog.h"
#include "common.h" #include "common.h"
std::string *FqToString(libff::alt_bn128_Fq *_fq) { string *FqToString(libff::alt_bn128_Fq *_fq) {
mpz_t t; mpz_t t;
mpz_init(t); mpz_init(t);
...@@ -62,7 +62,7 @@ std::string *FqToString(libff::alt_bn128_Fq *_fq) { ...@@ -62,7 +62,7 @@ std::string *FqToString(libff::alt_bn128_Fq *_fq) {
mpz_get_str(arr, 10, t); mpz_get_str(arr, 10, t);
mpz_clear(t); mpz_clear(t);
return new std::string(arr); return new string(arr);
} }
int char2int(char _input) { int char2int(char _input) {
...@@ -167,19 +167,19 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz ...@@ -167,19 +167,19 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
shared_ptr<signatures::Bls> obj; shared_ptr<signatures::Bls> obj;
obj = make_shared<signatures::Bls>(signatures::Bls(_t, _n)); obj = make_shared<signatures::Bls>(signatures::Bls(_t, _n));
std::pair<libff::alt_bn128_G1, std::string> hash_with_hint = obj->HashtoG1withHint(hash); pair<libff::alt_bn128_G1, string> hash_with_hint = obj->HashtoG1withHint(hash);
string *xStr = FqToString(&(hash_with_hint.first.X)); string *xStr = FqToString(&(hash_with_hint.first.X));
if (xStr == nullptr) { if (xStr == nullptr) {
std::cerr << "Null xStr" << std::endl; cerr << "Null xStr" << endl;
BOOST_THROW_EXCEPTION(runtime_error("Null xStr")); BOOST_THROW_EXCEPTION(runtime_error("Null xStr"));
} }
string *yStr = FqToString(&(hash_with_hint.first.Y)); string *yStr = FqToString(&(hash_with_hint.first.Y));
if (yStr == nullptr) { if (yStr == nullptr) {
std::cerr << "Null yStr" << std::endl; cerr << "Null yStr" << endl;
delete xStr; delete xStr;
BOOST_THROW_EXCEPTION(runtime_error("Null yStr")); BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
} }
...@@ -208,33 +208,18 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz ...@@ -208,33 +208,18 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
if (!result) { if (!result) {
cerr << "Invalid hex encrypted key" << endl; cerr << "Invalid hex encrypted key" << endl;
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid hex encrypted key")); BOOST_THROW_EXCEPTION(invalid_argument("Invalid hex encrypted key"));
} }
int errStatus = 0; int errStatus = 0;
sgx_status_t status = sgx_status_t status =
trustedBlsSignMessageAES(eid, &errStatus, errMsg, encryptedKey, trustedBlsSignMessageAES(eid, &errStatus, errMsg, encryptedKey,
sz, xStrArg, yStrArg, signature); sz, xStrArg, yStrArg, signature);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg);
if (status != SGX_SUCCESS) { string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" + hash_with_hint.second;
string errString = string("SGX enclave call to ") +
__FUNCTION__ + " failed with errStatus:" + to_string(status) +
" Err message:" + errMsg;
BOOST_THROW_EXCEPTION(runtime_error(errString));
}
if (errStatus != 0) {
string errString = string("SGX enclave call to ") +
__FUNCTION__ + " failed with errStatus:" + to_string(errStatus) +
" Err message:" + errMsg;
BOOST_THROW_EXCEPTION(runtime_error(errString));
}
std::string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" + hash_with_hint.second;
std::string sig = signature; string sig = signature;
sig.append(":"); sig.append(":");
sig.append(hint); sig.append(hint);
...@@ -248,28 +233,20 @@ bool bls_sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz ...@@ -248,28 +233,20 @@ bool bls_sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
return sign_aes(_encryptedKeyHex, _hashHex, _t, _n, _sig); return sign_aes(_encryptedKeyHex, _hashHex, _t, _n, _sig);
} }
std::string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) { string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) {
auto keyArray = make_shared<vector<char>>(BUF_LEN, 0); auto keyArray = make_shared<vector<char>>(BUF_LEN, 0);
auto encryptedKey = make_shared<vector<uint8_t>>(BUF_LEN, 0); auto encryptedKey = make_shared<vector<uint8_t>>(BUF_LEN, 0);
auto errMsg = make_shared<vector<char>>(BUF_LEN, 0); auto errMsg = make_shared<vector<char>>(BUF_LEN, 0);
strncpy(keyArray->data(), _key, BUF_LEN); strncpy(keyArray->data(), _key, BUF_LEN);
*errStatus = -1; *errStatus = -1;
unsigned int encryptedLen = 0; unsigned int encryptedLen = 0;
status = trustedEncryptKeyAES(eid, errStatus, errMsg->data(), keyArray->data(), encryptedKey->data(), &encryptedLen); status = trustedEncryptKeyAES(eid, errStatus, errMsg->data(), keyArray->data(), encryptedKey->data(), &encryptedLen);
if (*errStatus != 0) { HANDLE_TRUSTED_FUNCTION_ERROR(status, *errStatus, errMsg->data());
BOOST_THROW_EXCEPTION(SGXException(-666, errMsg->data()));
}
if (status != SGX_SUCCESS) {
*errStatus = -1;
return "";
}
std::string result(2 * BUF_LEN, '\0'); string result(2 * BUF_LEN, '\0');
carray2Hex(encryptedKey->data(), encryptedLen, &result.front()); carray2Hex(encryptedKey->data(), encryptedLen, &result.front());
......
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#include "BLSSignature.h" #include "BLSSignature.h"
#include "BLSutils.h" #include "BLSutils.h"
#include "third_party/spdlog/spdlog.h"
#include "secure_enclave_u.h" #include "secure_enclave_u.h"
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
#include "sgxwallet.h" #include "sgxwallet.h"
...@@ -121,6 +123,7 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr( ...@@ -121,6 +123,7 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
BOOST_THROW_EXCEPTION(runtime_error("Null yStr")); BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
} }
char errMsg[BUF_LEN]; char errMsg[BUF_LEN];
memset(errMsg, 0, BUF_LEN); memset(errMsg, 0, BUF_LEN);
...@@ -144,26 +147,16 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr( ...@@ -144,26 +147,16 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
bool result = hex2carray(encryptedKeyHex->c_str(), &sz, encryptedKey); bool result = hex2carray(encryptedKeyHex->c_str(), &sz, encryptedKey);
if (!result) { if (!result) {
cerr << "Invalid hex encrypted key" << endl; spdlog::error("Invalid hex encrypted key");
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid hex encrypted key")); BOOST_THROW_EXCEPTION(invalid_argument("Invalid hex encrypted key"));
} }
cerr << "Key is " + *encryptedKeyHex << endl;
sgx_status_t status = sgx_status_t status =
trustedBlsSignMessageAES(eid, &errStatus, errMsg, encryptedKey, trustedBlsSignMessageAES(eid, &errStatus, errMsg, encryptedKey,
encryptedKeyHex->size() / 2, xStrArg, yStrArg, signature); encryptedKeyHex->size() / 2, xStrArg, yStrArg, signature);
printf("sig is: %s\n", signature); HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg );
if (status != SGX_SUCCESS) {
gmp_printf("SGX enclave call to trustedBlsSignMessage failed: 0x%04x\n", status);
BOOST_THROW_EXCEPTION(runtime_error("SGX enclave call to trustedBlsSignMessage failed"));
}
if (errStatus != 0) {
BOOST_THROW_EXCEPTION(runtime_error("Enclave trustedBlsSignMessage failed:" + to_string(errStatus) + ":" + errMsg ));
}
int sigLen; int sigLen;
......
...@@ -56,4 +56,22 @@ inline std::string className(const std::string &prettyFunction) { ...@@ -56,4 +56,22 @@ inline std::string className(const std::string &prettyFunction) {
throw InvalidStateException(__msg__, __CLASS_NAME__);} throw InvalidStateException(__msg__, __CLASS_NAME__);}
#define HANDLE_TRUSTED_FUNCTION_ERROR(__STATUS__, __ERR_STATUS__, __ERR_MSG__) \
if (__STATUS__ != SGX_SUCCESS) { \
string __ERR_STRING__ = string("SGX enclave call to ") + \
__FUNCTION__ + " failed with status:" \
+ to_string(__STATUS__) + \
" Err message:" + __ERR_MSG__; \
BOOST_THROW_EXCEPTION(runtime_error(__ERR_MSG__)); \
}\
\
if (__ERR_STATUS__ != 0) {\
string __ERR_STRING__ = string("SGX enclave call to ") +\
__FUNCTION__ + " failed with errStatus:" + \
to_string(__ERR_STATUS__) + \
" Err message:" + __ERR_MSG__;\
BOOST_THROW_EXCEPTION(runtime_error(__ERR_STRING__)); \
}
#endif //SGXWALLET_COMMON_H #endif //SGXWALLET_COMMON_H
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