SKALE-2794 refactoring

parent 17310b56
......@@ -183,22 +183,13 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
throw SGXException(INVALID_HEX, "Invalid hash");
}
// auto keyShare = make_shared<BLSPrivateKeyShareSGX>(keyStr, _t, _n);
//
// auto sigShare = keyShare->signWithHelperSGX(hash, _signerIndex);
//
// auto sigShareStr = sigShare->toString();
//
// strncpy(_sig, sigShareStr->c_str(), BUF_LEN);
shared_ptr<signatures::Bls> obj;
obj = make_shared<signatures::Bls>(signatures::Bls(_t, _n));
std::pair<libff::alt_bn128_G1, std::string> hash_with_hint =
obj->HashtoG1withHint(hash);
std::pair<libff::alt_bn128_G1, std::string> hash_with_hint = obj->HashtoG1withHint(hash);
int errStatus = 0;
string *xStr = FqToString(&(hash_with_hint.first.X));
if (xStr == nullptr) {
......@@ -213,7 +204,6 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
}
char errMsg[BUF_LEN];
memset(errMsg, 0, BUF_LEN);
......@@ -257,13 +247,6 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
strncpy(_sig, sig.c_str(), BUF_LEN);
//string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
//strncpy(_sig, sigShareStr.c_str(), BUF_LEN);
// string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
// auto sig_ptr = make_shared<string>(test_sig);
// strncpy(_sig, sig_ptr->c_str(), BUF_LEN);
return true;
}
......@@ -286,9 +269,7 @@ char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key)
spdlog::debug("errStatus is {}", *errStatus);
spdlog::debug(" errMsg is ", errMsg->data());
if (status != SGX_SUCCESS) {
*errStatus = -1;
return nullptr;
}
......@@ -297,7 +278,6 @@ char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key)
throw SGXException(-666, errMsg->data());
}
char *result = (char *) calloc(2 * BUF_LEN, 1);
carray2Hex(encryptedKey->data(), encryptedLen, result);
......
......@@ -31,25 +31,15 @@
#include <gmp.h>
#include <random>
#include "spdlog/spdlog.h"
#include "common.h"
#include "secure_enclave/Verify.h"
#include "BLSCrypto.h"
#include "ECDSACrypto.h"
string concatPubKeyWith0x(char *pub_key_x, char *pub_key_y) {
string px = pub_key_x;
string py = pub_key_y;
string result = "0x" + px + py;
return result;
}
void fillRandomBuffer(vector<unsigned char> &_buffer) {
ifstream devRandom("/dev/urandom", ios::in | ios::binary);
devRandom.exceptions(ifstream::failbit | ifstream::badbit);
......
......@@ -22,7 +22,6 @@
#include "sgxwallet_common.h"
#include "SGXException.h"
#include "LevelDB.h"
#include "BLSCrypto.h"
......@@ -60,22 +59,19 @@ void setFullOptions(int _printDebugInfo,
spdlog::info("encryptKeys set to " + std::to_string(encryptKeys));
}
void setOptions(int _printDebugInfo,
int _printTraceInfo, int _useHTTPS, int _autoconfirm) {
setFullOptions(_printDebugInfo,
_printTraceInfo, _useHTTPS, _autoconfirm, false);
}
bool isStringDec(string &_str) {
bool isStringDec(const string &_str) {
auto res = find_if_not(_str.begin(), _str.end(), [](char c) -> bool {
return isdigit(c);
});
return !_str.empty() && res == _str.end();
}
shared_ptr<SGXWalletServer> SGXWalletServer::server = nullptr;
shared_ptr<HttpServer> SGXWalletServer::httpServer = nullptr;
......@@ -144,7 +140,6 @@ int SGXWalletServer::initHttpsServer(bool _checkCerts) {
return 0;
}
int SGXWalletServer::initHttpServer() { //without ssl
httpServer = make_shared<HttpServer>(BASE_PORT + 3);
server = make_shared<SGXWalletServer>(*httpServer,
......@@ -161,7 +156,7 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k
Json::Value result;
int errStatus = UNKNOWN_ERROR;
char *errMsg = (char *) calloc(BUF_LEN, 1);
string errMsg(BUF_LEN, '\0');
result["status"] = 0;
result["errorMessage"] = "";
......@@ -170,14 +165,14 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k
char *encryptedKeyShareHex = nullptr;
try {
encryptedKeyShareHex = encryptBLSKeyShare2Hex(&errStatus, errMsg, _keyShare.c_str());
encryptedKeyShareHex = encryptBLSKeyShare2Hex(&errStatus, &errMsg.front(), _keyShare.c_str());
if (encryptedKeyShareHex == nullptr) {
throw SGXException(UNKNOWN_ERROR, "");
if (errStatus != 0) {
throw SGXException(errStatus, errMsg.data());
}
if (errStatus != 0) {
throw SGXException(errStatus, errMsg);
if (encryptedKeyShareHex == nullptr) {
throw SGXException(UNKNOWN_ERROR, "");
}
result["encryptedKeyShare"] = string(encryptedKeyShareHex);
......@@ -203,7 +198,8 @@ SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const strin
result["errorMessage"] = "Unknown server error";
result["signatureShare"] = "";
char *signature = (char *) calloc(BUF_LEN, 1);
// char *signature = (char *) calloc(BUF_LEN, 1); // remove
string signature(BUF_LEN, '\0');
shared_ptr <string> value = nullptr;
......@@ -237,7 +233,7 @@ SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const strin
}
try {
if (!bls_sign(value->c_str(), _messageHash.c_str(), t, n, _signerIndex, signature)) {
if (!bls_sign(value->c_str(), _messageHash.c_str(), t, n, _signerIndex, &signature.front())) {
result["status"] = -1;
result["errorMessage"] = "Could not sign";
return result;
......@@ -254,7 +250,6 @@ SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const strin
return result;
}
Json::Value SGXWalletServer::importECDSAKeyImpl(const string &_key, const string &_keyName) {
Json::Value result;
result["status"] = 0;
......@@ -263,14 +258,12 @@ Json::Value SGXWalletServer::importECDSAKeyImpl(const string &_key, const string
return result;
}
Json::Value SGXWalletServer::generateECDSAKeyImpl() {
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
result["encryptedKey"] = "";
vector <string> keys;
try {
......@@ -291,7 +284,6 @@ Json::Value SGXWalletServer::generateECDSAKeyImpl() {
result["publicKey"] = keys.at(1);
result["PublicKey"] = keys.at(1);
result["keyName"] = keyName;
} HANDLE_SGX_EXCEPTION(result)
return result;
......@@ -319,13 +311,11 @@ Json::Value SGXWalletServer::renameECDSAKeyImpl(const string &_keyName, const st
writeDataToDB(_keyName, *encryptedKey);
LevelDB::getLevelDb()->deleteTempNEK(_tempKeyName);
} HANDLE_SGX_EXCEPTION(result)
return result;
}
Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_keyName, const string &_messageHash) {
INIT_RESULT(result)
......@@ -366,7 +356,6 @@ Json::Value SGXWalletServer::ecdsaSignMessageHashImpl(int _base, const string &_
result["signature_v"] = signatureVector.at(0);
result["signature_r"] = signatureVector.at(1);
result["signature_s"] = signatureVector.at(2);
} HANDLE_SGX_EXCEPTION(result)
return result;
......@@ -391,7 +380,6 @@ Json::Value SGXWalletServer::getPublicECDSAKeyImpl(const string &_keyName) {
result["PublicKey"] = publicKey;
result["publicKey"] = publicKey;
} HANDLE_SGX_EXCEPTION(result)
return result;
......@@ -412,8 +400,6 @@ Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t
}
encrPolyHex = gen_dkg_poly(_t);
writeDataToDB(_polyName, encrPolyHex);
} HANDLE_SGX_EXCEPTION(result)
return result;
......@@ -610,11 +596,9 @@ Json::Value SGXWalletServer::complaintResponseImpl(const string &_polyName, int
result["share*G2"] = *shareG2_ptr;
result["dhKey"] = DHKey;
} HANDLE_SGX_EXCEPTION(result)
return result;
}
Json::Value SGXWalletServer::multG2Impl(const string &_x) {
......@@ -625,7 +609,6 @@ Json::Value SGXWalletServer::multG2Impl(const string &_x) {
for (uint8_t i = 0; i < 4; i++) {
result["x*G2"][i] = xG2_vect.at(i);
}
} HANDLE_SGX_EXCEPTION(result)
return result;
......@@ -653,14 +636,12 @@ Json::Value SGXWalletServer::getServerStatusImpl() {
return result;
}
Json::Value SGXWalletServer::getServerVersionImpl() {
INIT_RESULT(result)
result["version"] = TOSTRING(SGXWALLET_VERSION);
return result;
}
Json::Value SGXWalletServer::generateDKGPoly(const string &_polyName, int _t) {
WRITE_LOCK(m)
return generateDKGPolyImpl(_polyName, _t);
......@@ -696,7 +677,6 @@ Json::Value SGXWalletServer::getBLSPublicKeyShare(const string &blsKeyName) {
return getBLSPublicKeyShareImpl(blsKeyName);
}
Json::Value SGXWalletServer::generateECDSAKey() {
WRITE_LOCK(m)
return generateECDSAKeyImpl();
......@@ -712,14 +692,12 @@ Json::Value SGXWalletServer::getPublicECDSAKey(const string &_keyName) {
return getPublicECDSAKeyImpl(_keyName);
}
Json::Value SGXWalletServer::ecdsaSignMessageHash(int _base, const string &_keyShareName, const string &_messageHash) {
READ_LOCK(m)
spdlog::debug("MessageHash first {}", _messageHash);
return ecdsaSignMessageHashImpl(_base, _keyShareName, _messageHash);
}
Json::Value
SGXWalletServer::importBLSKeyShare(const string &_keyShare, const string &_keyShareName, int _t, int _n,
int index) {
......@@ -819,4 +797,3 @@ void SGXWalletServer::writeDataToDB(const string &Name, const string &value) {
LevelDB::getLevelDb()->writeString(key, value);
}
......@@ -30,7 +30,6 @@
#define EXTERNC
#endif
EXTERNC void setFullOptions(int _printDebugInfo,
int _printTraceInfo, int _useHTTPS, int _autoconfirm, int _encryptKeys);
......@@ -38,10 +37,4 @@ EXTERNC void setFullOptions(int _printDebugInfo,
EXTERNC void setOptions(int _printDebugInfo,
int _printTraceInfo, int _useHTTPS, int _autoconfirm);
#endif //SGXWALLET_SGXWALLETSERVER_H
......@@ -42,17 +42,11 @@ using namespace std;
#define TOSTRING(x) STRINGIFY(x)
class SGXWalletServer : public AbstractStubServer {
Lock m;
static shared_ptr<SGXWalletServer> server;
static shared_ptr<HttpServer> httpServer;
public:
static const char* getVersion() {
return TOSTRING(SGXWALLET_VERSION);
}
......
......@@ -71,11 +71,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "testw.h"
#include "TestUtils.h"
using namespace jsonrpc;
using namespace std;
default_random_engine TestUtils::randGen((unsigned int) time(0));
string TestUtils::stringFromFr(libff::alt_bn128_Fr &el) {
......@@ -105,9 +103,6 @@ string TestUtils::convertDecToHex(string dec, int numBytes) {
return result;
}
void TestUtils::resetDB() {
CHECK_STATE(system("bash -c \"rm -rf " SGXDATA_FOLDER "* \"") == 0);
}
......@@ -124,7 +119,6 @@ shared_ptr <string> TestUtils::encryptTestKey() {
return make_shared<string>(encryptedKeyHex);
}
vector <libff::alt_bn128_Fr> TestUtils::splitStringToFr(const char *coeffs, const char symbol) {
string str(coeffs);
string delim;
......@@ -275,9 +269,6 @@ void TestUtils::destroyEnclave() {
void TestUtils::doDKG(StubClient &c, int n, int t,
vector<string>& _ecdsaKeyNames, vector<string>& _blsKeyNames,
int schainID, int dkgID) {
Json::Value ethKeys[n];
Json::Value verifVects[n];
Json::Value pubEthKeys;
......@@ -287,12 +278,9 @@ void TestUtils::doDKG(StubClient &c, int n, int t,
vector<string> pubShares(n);
vector<string> polyNames(n);
_ecdsaKeyNames.clear();
_blsKeyNames.clear();
for (uint8_t i = 0; i < n; i++) {
ethKeys[i] = c.generateECDSAKey();
......@@ -364,7 +352,6 @@ void TestUtils::doDKG(StubClient &c, int n, int t,
map<size_t, shared_ptr<BLSPublicKeyShare>> pubKeyShares;
for (int i = 0; i < n; i++) {
string endName = polyNames[i].substr(4);
string blsName = "BLS_KEY" + polyNames[i].substr(4);
......@@ -376,11 +363,9 @@ void TestUtils::doDKG(StubClient &c, int n, int t,
CHECK_STATE(response["status"] == 0);
pubBLSKeys[i] = c.getBLSPublicKeyShare(blsName);
CHECK_STATE(pubBLSKeys[i]["status"] == 0);
}
for (int i = 0; i < t; i++) {
vector<string> pubKeyVect;
for (uint8_t j = 0; j < 4; j++) {
pubKeyVect.push_back(pubBLSKeys[i]["blsPublicKeyShare"][j].asString());
......@@ -390,7 +375,6 @@ void TestUtils::doDKG(StubClient &c, int n, int t,
pubKeyShares[i + 1] = make_shared<BLSPublicKeyShare>(pubKey);
}
// create pub key
BLSPublicKey blsPublicKey(make_shared<map<size_t, shared_ptr<BLSPublicKeyShare >>>(pubKeyShares), t,
......@@ -410,7 +394,6 @@ void TestUtils::doDKG(StubClient &c, int n, int t,
auto pubKey = pubKeyShares[i+1];
CHECK_STATE(pubKey->VerifySigWithHelper(hash_arr, make_shared<BLSSigShare>(sig), t, n));
}
shared_ptr<BLSSignature> commonSig = sigShareSet.merge();
......@@ -422,6 +405,4 @@ void TestUtils::doDKG(StubClient &c, int n, int t,
for (auto&& i : _blsKeyNames)
cerr << i << endl;
}
\ No newline at end of file
}
......@@ -5,7 +5,6 @@
#ifndef SGXWALLET_TESTUTILS_H
#define SGXWALLET_TESTUTILS_H
#include <libff/algebra/fields/fp.hpp>
#include <dkg/dkg.h>
#include <jsonrpccpp/server/connectors/httpserver.h>
......@@ -30,11 +29,9 @@ using namespace std;
using namespace jsonrpc;
class TestUtils {
public:
static default_random_engine randGen;
static string stringFromFr(libff::alt_bn128_Fr &el);
......
......@@ -29,8 +29,7 @@
#include "AESUtils.h"
int AES_encrypt(char *message, uint8_t *encr_message){
int AES_encrypt(char *message, uint8_t *encr_message) {
sgx_read_rand(encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE);
sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_key, (uint8_t*)message, strlen(message),
encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE,
......@@ -38,12 +37,10 @@ int AES_encrypt(char *message, uint8_t *encr_message){
NULL, 0,
(sgx_aes_gcm_128bit_tag_t *) encr_message);
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 len = length - SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE;
sgx_status_t status = sgx_rijndael128GCM_decrypt(&AES_key,
......@@ -53,6 +50,5 @@ int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message){
NULL, 0,
(sgx_aes_gcm_128bit_tag_t *)encr_message);
return status;
}
\ No newline at end of file
}
......@@ -50,53 +50,40 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "sgxwall.h"
#include "sgxwallet.h"
void SGXWallet::usage() {
cerr << "usage: sgxwallet\n";
exit(1);
}
void SGXWallet::printUsage() {
cerr << "Available flags:\n";
cerr << "-c do not verify client certificate\n";
cerr << "-s sign client certificate without human confirmation \n";
cerr << "-d turn on debug output\n";
cerr << "-v verbose mode: turn on debug output\n";
cerr << "-vv detailed verbose mode: turn on debug and trace outputs\n";
cerr << "-n launch SGXWalletServer using http (not https)\n";
cerr << "-c Do not verify client certificate\n";
cerr << "-s Sign client certificate without human confirmation \n";
cerr << "-d Turn on debug output\n";
cerr << "-v Verbose mode: turn on debug output\n";
cerr << "-vv Detailed verbose mode: turn on debug and trace outputs\n";
cerr << "-n Launch SGXWalletServer using http (not https)\n";
cerr << "-b Restore from back up (you will need to enter backup key) \n";
cerr << "-y Do not ask user to acknowledge receipt of backup key \n";
cerr << "-T Generate test keys \n";
}
enum log_level {L_TRACE = 0, L_DEBUG = 1, L_INFO = 2,L_WARNING = 3, L_ERROR = 4 };
void SGXWallet::serializeKeys(vector<string>& _ecdsaKeyNames, vector<string>& _blsKeyNames, string _fileName) {
void SGXWallet::serializeKeys(const vector<string>& _ecdsaKeyNames, const vector<string>& _blsKeyNames, const string& _fileName) {
Json::Value top(Json::objectValue);
Json::Value ecdsaKeysJson(Json::objectValue);
Json::Value blsKeysJson(Json::objectValue);
for (uint i = 0; i < _ecdsaKeyNames.size(); i++) {
auto key = to_string(i + 1);
string keyFull(3 - key.size(), '0');
keyFull.append(key);
ecdsaKeysJson[keyFull] = _ecdsaKeyNames[i];
blsKeysJson[keyFull] = _blsKeyNames[i];
ecdsaKeysJson[key] = _ecdsaKeyNames[i];
blsKeysJson[key] = _blsKeyNames[i];
}
top["ecdsaKeyNames"] = ecdsaKeysJson;
top["blsKeyNames"] = blsKeysJson;
ofstream fs;
fs.open(_fileName);
......@@ -104,8 +91,6 @@ void SGXWallet::serializeKeys(vector<string>& _ecdsaKeyNames, vector<string>& _b
fs << top;
fs.close();
}
......@@ -184,10 +169,7 @@ int main(int argc, char *argv[]) {
initAll(enclaveLogLevel, checkClientCertOption, autoSignClientCertOption);
ifstream is("sgx_data/4node.json");
if (generateTestKeys && !is.good()) {
if (generateTestKeys) {
cerr << "Generating test keys ..." << endl;
HttpClient client(RPC_ENDPOINT);
......@@ -199,21 +181,18 @@ int main(int argc, char *argv[]) {
int schainID = 1;
int dkgID = 1;
TestUtils::doDKG(c, 4, 3, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
TestUtils::doDKG(c, 4, 1, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
SGXWallet::serializeKeys(ecdsaKeyNames, blsKeyNames, "sgx_data/4node.json");
schainID = 2;
dkgID = 2;
TestUtils::doDKG(c, 16, 11, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
TestUtils::doDKG(c, 16, 5, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
SGXWallet::serializeKeys(ecdsaKeyNames, blsKeyNames, "sgx_data/16node.json");
cerr << "Successfully completed generating test keys into sgx_data" << endl;
}
while (true) {
......
......@@ -41,7 +41,7 @@ public:
static void usage();
static void printUsage();
static void serializeKeys(
vector<string>& _ecdsaKeyNames, vector<string>& _blsKeyNames, string _fileName);
static void serializeKeys( const vector<string>& _ecdsaKeyNames,
const vector<string>& _blsKeyNames, const string& _fileName);
};
\ No newline at end of file
};
......@@ -32,28 +32,18 @@
#include <stdbool.h>
extern int useHTTPS;
extern int encryptKeys;
extern int autoconfirm;
#define BUF_LEN 4096
#define MAX_KEY_LENGTH 128
#define MAX_COMPONENT_LENGTH 80
#define MAX_COMPONENT_HEX_LENGTH MAX_COMPONENT_LENGTH * 2
#define MAX_ENCRYPTED_KEY_LENGTH 1024
#define MAX_SIG_LEN 1024
#define MAX_ERR_LEN 1024
#define MAX_KEY_LENGTH 128
#define MAX_COMPONENT_LENGTH 80
#define MAX_COMPONENT_HEX_LENGTH MAX_COMPONENT_LENGTH * 2
#define MAX_ENCRYPTED_KEY_LENGTH 1024
#define MAX_SIG_LEN 1024
#define MAX_ERR_LEN 1024
#define SHA_256_LEN 32
#define ADD_ENTROPY_SIZE 32
......@@ -83,7 +73,6 @@ extern int autoconfirm;
#define CERT_REQUEST_DOES_NOT_EXIST -14
#define INVALID_ECDSA_KEY_NAME -20
#define INVALID_HEX -21
#define INVALID_ECSDA_SIGNATURE -22
......@@ -106,6 +95,4 @@ extern int autoconfirm;
#define TEST_VALUE "1234567890"
#endif //SGXWALLET_SGXWALLET_COMMON_H
......@@ -21,7 +21,6 @@
@date 2019
*/
#include <iostream>
#include "stubclient.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