Unverified Commit 5f6a68f7 authored by Oleh Nikolaiev's avatar Oleh Nikolaiev Committed by GitHub

Merge pull request #326 from skalenetwork/feature/SKALE-3951-add-zeromq

Feature/skale 3951 add zeromq
parents 152e6db8 b02835f1
//
// Created by kladko on 15.12.20.
//
#include "BLSSignReqMessage.h"
#include "SGXWalletServer.hpp"
Json::Value BLSSignReqMessage::process() {
auto keyName = getStringRapid("keyShareName");
auto hash = getStringRapid("messageHash");
auto t = getUint64Rapid("t");
auto n = getUint64Rapid("n");
auto result = SGXWalletServer::blsSignMessageHashImpl(keyName, hash, t, n);
result["type"] = ZMQMessage::BLS_SIGN_RSP;
return result;
}
\ No newline at end of file
/*
Copyright (C) 2018-2019 SKALE Labs
This file is part of libBLS.
libBLS is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
@file BLSReqSignMessage.h
@author Stan Kladko
@date 2020
*/
#ifndef SGXWALLET_BLSSIGNREQMSG_H
#define SGXWALLET_BLSSIGNREQMSG_H
#include "ZMQMessage.h"
class BLSSignReqMessage : public ZMQMessage {
public:
BLSSignReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
#endif //SGXWALLET_BLSSIGNREQMSG_H
......@@ -150,7 +150,7 @@ string gen_dkg_poly(int _t) {
return result;
}
vector <vector<string>> get_verif_vect(const string &encryptedPolyHex, int t, int n) {
vector <vector<string>> get_verif_vect(const string &encryptedPolyHex, int t) {
auto encryptedPolyHexPtr = encryptedPolyHex.c_str();
......@@ -174,7 +174,7 @@ vector <vector<string>> get_verif_vect(const string &encryptedPolyHex, int t, in
sgx_status_t status = SGX_SUCCESS;
status = trustedGetPublicShares(eid, &errStatus, errMsg.data(), encrDKGPoly.data(), encLen,
pubShares.data(), t, n);
pubShares.data(), t);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
......@@ -189,7 +189,7 @@ vector <vector<string>> get_verif_vect(const string &encryptedPolyHex, int t, in
}
vector <vector<string>> getVerificationVectorMult(const std::string &encryptedPolyHex, int t, int n, size_t ind) {
auto verificationVector = get_verif_vect(encryptedPolyHex, t, n);
auto verificationVector = get_verif_vect(encryptedPolyHex, t);
vector <vector<string>> result(t);
......
......@@ -33,7 +33,7 @@ using namespace std;
string gen_dkg_poly( int _t);
vector <vector<string>> get_verif_vect(const string& encryptedPolyHex, int t, int n);
vector <vector<string>> get_verif_vect(const string& encryptedPolyHex, int t);
vector <vector<string>> getVerificationVectorMult(const std::string& encryptedPolyHex, int t, int n, size_t ind);
......
/*
Copyright (C) 2018-2019 SKALE Labs
This file is part of libBLS.
libBLS is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
@file ECDSASignReqMessage.cpp
@author Stan Kladko
@date 2020
*/
#include "SGXWalletServer.hpp"
#include "ECDSASignReqMessage.h"
Json::Value ECDSASignReqMessage::process() {
auto base = getUint64Rapid("base");
auto keyName = getStringRapid("keyName");
auto hash = getStringRapid("messageHash");
auto result = SGXWalletServer::ecdsaSignMessageHashImpl(base, keyName, hash);
result["type"] = ZMQMessage::ECDSA_SIGN_RSP;
return result;
}
\ No newline at end of file
/*
Copyright (C) 2018- SKALE Labs
This file is part of libBLS.
libBLS is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
@file ECDSAReqSignMessage.h
@author Stan Kladko
@date 2020
*/
#ifndef SGXWALLET_ECDSASIGNREQMESSAGE_H
#define SGXWALLET_ECDSASIGNREQMESSAGE_H
#include "ZMQMessage.h"
class ECDSASignReqMessage : public ZMQMessage {
public:
ECDSASignReqMessage(shared_ptr <rapidjson::Document> &_d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
#endif //SGXWALLET_ECDSASIGNREQMESSAGE_H
......@@ -70,11 +70,13 @@ bin_PROGRAMS = sgxwallet testw sgx_util
## have to be explicitly listed
COMMON_SRC = SGXException.cpp ExitHandler.cpp ZMQClient.cpp BLSSignRspMessage.cpp ECDSASignRspMessage.cpp ECDSASignReqMessage.cpp BLSSignReqMessage.cpp ZMQMessage.cpp ZMQServer.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \
COMMON_SRC = SGXException.cpp ExitHandler.cpp zmq_src/ZMQClient.cpp zmq_src/RspMessage.cpp zmq_src/ReqMessage.cpp \
zmq_src/ZMQMessage.cpp zmq_src/ZMQServer.cpp \
InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp \
SGXWalletServer.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp BLSCrypto.cpp \
DKGCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp ServerDataChecker.cpp SEKManager.cpp \
third_party/intel/sgx_stub.c third_party/intel/sgx_detect_linux.c third_party/intel/create_enclave.c third_party/intel/oc_alloc.c \
ECDSAImpl.c TestUtils.cpp sgxwallet.c SGXInfoServer.cpp ECDSACrypto.cpp
third_party/intel/sgx_stub.c third_party/intel/sgx_detect_linux.c third_party/intel/create_enclave.c \
third_party/intel/oc_alloc.c ECDSAImpl.c TestUtils.cpp sgxwallet.c SGXInfoServer.cpp ECDSACrypto.cpp
COMMON_ENCLAVE_SRC = secure_enclave_u.c secure_enclave_u.h
sgxwallet_SOURCES = sgxwall.cpp $(COMMON_SRC)
......@@ -110,18 +112,20 @@ sgxwallet_LDADD=-l$(SGX_URTS_LIB) -l$(SGX_UAE_SERVICE_LIB) -LlibBLS/deps/deps_in
-ljsonrpccpp-stub -ljsonrpccpp-server -ljsonrpccpp-client -ljsonrpccpp-common -ljsoncpp -lmicrohttpd \
-lboost_system -lboost_thread -lgnutls -lgcrypt -lidn2 -lcurl -lssl -lcrypto -lz -lpthread -lstdc++fs
testw_SOURCES=testw.cpp $(COMMON_SRC)
nodist_testw_SOURCES=${nodist_sgxwallet_SOURCES}
EXTRA_testw_DEPENDENCIES=${EXTRA_sgxwallet_DEPENDENCIES}
testw_LDADD= ${sgxwallet_LDADD}
testw_LDADD=${sgxwallet_LDADD}
sgx_util_SOURCES= SGXException.cpp ExitHandler.cpp InvalidStateException.cpp Exception.cpp InvalidArgumentException.cpp Log.cpp sgx_util.cpp stubclient.cpp LevelDB.cpp SGXRegistrationServer.cpp CSRManagerServer.cpp
sgx_util_SOURCES=SGXException.cpp ExitHandler.cpp InvalidStateException.cpp Exception.cpp \
InvalidArgumentException.cpp Log.cpp sgx_util.cpp stubclient.cpp LevelDB.cpp \
SGXRegistrationServer.cpp CSRManagerServer.cpp
sgx_util_LDADD=-LlibBLS/deps/deps_inst/x86_or_x64/lib -Lleveldb/build -LlibBLS/build \
-LlibBLS/build/libff/libff \
-Llibzmq/build/lib/ \
-l:libzmq.a \
-l:libbls.a -l:libleveldb.a \
-l:libff.a -lgmp -ljsonrpccpp-stub -ljsonrpccpp-server -ljsonrpccpp-client -ljsonrpccpp-common -ljsoncpp -lmicrohttpd -lgnutls -lgcrypt -lidn2 -lcurl -lssl -lcrypto -lz -lpthread -ldl
-l:libff.a -lgmp -ljsonrpccpp-stub -ljsonrpccpp-server -ljsonrpccpp-client -ljsonrpccpp-common \
-ljsoncpp -lmicrohttpd -lgnutls -lgcrypt -lidn2 -lcurl -lssl -lcrypto -lz -lpthread -ldl
......@@ -154,7 +154,6 @@ Json::Value SGXRegistrationServer::GetCertificate(const string &hash) {
RETURN_SUCCESS(result)
}
void SGXRegistrationServer::initRegistrationServer(bool _autoSign) {
httpServer = make_shared<HttpServer>(BASE_PORT + 1);
server = make_shared<SGXRegistrationServer>(*httpServer,
......
......@@ -49,6 +49,12 @@
#include "Log.h"
#ifdef SGX_HW_SIM
#define NUM_THREADS 16
#else
#define NUM_THREADS 200
#endif
using namespace std;
std::shared_timed_mutex sgxInitMutex;
......@@ -111,20 +117,12 @@ void SGXWalletServer::printDB() {
LevelDB::getLevelDb()->visitKeys(&v, 100000000);
}
#ifdef SGX_HW_SIM
#define NUM_THREADS 16
#else
#define NUM_THREADS 200
#endif
bool SGXWalletServer::verifyCert(string &_certFileName) {
string rootCAPath = string(SGXDATA_FOLDER) + "cert_data/rootCA.pem";
string verifyCert = "cert/verify_client_cert " + rootCAPath + " " + _certFileName;
return system(verifyCert.c_str()) == 0;
}
void SGXWalletServer::createCertsIfNeeded() {
string rootCAPath = string(SGXDATA_FOLDER) + "cert_data/rootCA.pem";
......@@ -170,7 +168,6 @@ void SGXWalletServer::createCertsIfNeeded() {
}
}
void SGXWalletServer::initHttpsServer(bool _checkCerts) {
COUNT_STATISTICS
spdlog::info("Entering {}", __FUNCTION__);
......@@ -269,7 +266,6 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k
RETURN_SUCCESS(result);
}
map <string, string> SGXWalletServer::blsRequests;
recursive_mutex SGXWalletServer::blsRequestsLock;
......@@ -288,7 +284,6 @@ void SGXWalletServer::checkForDuplicate(map <string, string> &_map, recursive_mu
_map[_key] = _value;
}
Json::Value
SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const string &_messageHash, int t, int n) {
spdlog::trace("Entering {}", __FUNCTION__);
......@@ -305,10 +300,8 @@ SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const strin
shared_ptr <string> value = nullptr;
checkForDuplicate(blsRequests, blsRequestsLock, _keyShareName, _messageHash);
try {
if (!checkName(_keyShareName, "BLS_KEY")) {
throw SGXException(BLS_SIGN_INVALID_KS_NAME, string(__FUNCTION__) + ":Invalid BLSKey name");
......@@ -342,9 +335,7 @@ SGXWalletServer::blsSignMessageHashImpl(const string &_keyShareName, const strin
result["signatureShare"] = string(signature.data());
RETURN_SUCCESS(result);
}
Json::Value SGXWalletServer::importECDSAKeyImpl(const string &_keyShare,
......@@ -501,7 +492,7 @@ Json::Value SGXWalletServer::generateDKGPolyImpl(const string &_polyName, int _t
RETURN_SUCCESS(result)
}
Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName, int _t, int _n) {
Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName, int _t) {
COUNT_STATISTICS
spdlog::info("Entering {}", __FUNCTION__);
INIT_RESULT(result)
......@@ -511,13 +502,13 @@ Json::Value SGXWalletServer::getVerificationVectorImpl(const string &_polyName,
if (!checkName(_polyName, "POLY")) {
throw SGXException(INVALID_DKG_GETVV_POLY_NAME, string(__FUNCTION__) + ":Invalid polynomial name");
}
if (!check_n_t(_t, _n)) {
throw SGXException(INVALID_DKG_GETVV_PARAMS, string(__FUNCTION__) + ":Invalid parameters n or t ");
if (_t <= 0) {
throw SGXException(INVALID_DKG_GETVV_PARAMS, string(__FUNCTION__) + ":Invalid t ");
}
shared_ptr <string> encrPoly = readFromDb(_polyName);
verifVector = get_verif_vect(*encrPoly, _t, _n);
verifVector = get_verif_vect(*encrPoly, _t);
for (int i = 0; i < _t; i++) {
vector <string> currentCoef = verifVector.at(i);
......@@ -648,7 +639,6 @@ SGXWalletServer::createBLSPrivateKeyImpl(const string &_blsKeyName, const string
string(__FUNCTION__) + ":Error while creating BLS key share");
}
for (int i = 0; i < _n; i++) {
string name = _polyName + "_" + to_string(i) + ":";
LevelDB::getLevelDb()->deleteDHDKGKey(name);
......@@ -978,7 +968,6 @@ SGXWalletServer::createBLSPrivateKeyV2Impl(const string &_blsKeyName, const stri
string(__FUNCTION__) + ":Error while creating BLS key share");
}
for (int i = 0; i < _n; i++) {
string name = _polyName + "_" + to_string(i) + ":";
LevelDB::getLevelDb()->deleteDHDKGKey(name);
......@@ -987,7 +976,6 @@ SGXWalletServer::createBLSPrivateKeyV2Impl(const string &_blsKeyName, const stri
}
LevelDB::getLevelDb()->deleteKey(_polyName);
string encryptedSecretShareName = "encryptedSecretShare:" + _polyName;
LevelDB::getLevelDb()->deleteKey(encryptedSecretShareName);
......@@ -1000,8 +988,8 @@ Json::Value SGXWalletServer::generateDKGPoly(const string &_polyName, int _t) {
return generateDKGPolyImpl(_polyName, _t);
}
Json::Value SGXWalletServer::getVerificationVector(const string &_polynomeName, int _t, int _n) {
return getVerificationVectorImpl(_polynomeName, _t, _n);
Json::Value SGXWalletServer::getVerificationVector(const string &_polynomeName, int _t) {
return getVerificationVectorImpl(_polynomeName, _t);
}
Json::Value SGXWalletServer::getSecretShare(const string &_polyName, const Json::Value &_publicKeys, int t, int n) {
......
......@@ -82,7 +82,7 @@ public:
virtual Json::Value generateDKGPoly(const string &_polyName, int _t);
virtual Json::Value getVerificationVector(const string &_polynomeName, int _t, int _n);
virtual Json::Value getVerificationVector(const string &_polynomeName, int _t);
virtual Json::Value getSecretShare(const string &_polyName, const Json::Value &_publicKeys, int t, int n);
......@@ -140,7 +140,7 @@ public:
static Json::Value generateDKGPolyImpl(const string &_polyName, int _t);
static Json::Value getVerificationVectorImpl(const string &_polyName, int _t, int _n);
static Json::Value getVerificationVectorImpl(const string &_polyName, int _t);
static Json::Value getSecretShareImpl(const string &_polyName, const Json::Value &_pubKeys, int _t, int _n);
......@@ -172,7 +172,7 @@ public:
static Json::Value dkgVerificationV2Impl(const string &_publicShares, const string &_ethKeyName, const string &_secretShare, int _t, int _n, int _index);
virtual Json::Value createBLSPrivateKeyV2Impl(const std::string& blsKeyName, const std::string& ethKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n);
static Json::Value createBLSPrivateKeyV2Impl(const std::string& blsKeyName, const std::string& ethKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n);
static void printDB();
......
......@@ -58,7 +58,7 @@
#include "BLSCrypto.h"
#include "ServerInit.h"
#include "SGXException.h"
#include "ZMQServer.h"
#include "zmq_src/ZMQServer.h"
#include "SGXWalletServer.hpp"
uint32_t enclaveLogLevel = 0;
......@@ -103,10 +103,8 @@ void initUserSpace() {
}
uint64_t initEnclave() {
#ifndef SGX_HW_SIM
unsigned long support;
support = get_sgx_support();
......@@ -161,11 +159,9 @@ uint64_t initEnclave() {
return SGX_SUCCESS;
}
void initAll(uint32_t _logLevel, bool _checkCert,
bool _checkZMQSig, bool _autoSign, bool _generateTestKeys) {
static atomic<bool> sgxServerInited(false);
static mutex initMutex;
enclaveLogLevel = _logLevel;
......@@ -237,5 +233,4 @@ void exitAll() {
CSRManagerServer::exitServer();
SGXInfoServer::exitServer();
ZMQServer::exitZMQServer();
}
......@@ -42,7 +42,6 @@
#include "SGXWalletServer.hpp"
#include "catch.hpp"
#include "ZMQClient.h"
#include "BLSSigShare.h"
#include "BLSSigShareSet.h"
#include "BLSPublicKeyShare.h"
......@@ -73,7 +72,6 @@ string TestUtils::stringFromFr(libff::alt_bn128_Fr &el) {
return string(arr);
}
string TestUtils::convertDecToHex(string dec, int numBytes) {
mpz_t num;
mpz_init(num);
......@@ -190,7 +188,6 @@ void TestUtils::sendRPCRequest() {
CHECK_STATE(sig["status"].asInt() == 0);
}
CHECK_STATE(ethKeys[i]["status"] == 0);
string polyName =
"POLY:SCHAIN_ID:" + to_string(schainID) + ":NODE_ID:" + to_string(i) + ":DKG_ID:" + to_string(dkgID);
......@@ -199,7 +196,7 @@ void TestUtils::sendRPCRequest() {
polyNames[i] = polyName;
for (int i3 = 0; i3 <= testCount; i3++) {
verifVects[i] = c.getVerificationVector(polyName, t, n);
verifVects[i] = c.getVerificationVector(polyName, t);
CHECK_STATE(verifVects[i]["status"] == 0);
}
......@@ -250,7 +247,6 @@ void TestUtils::sendRPCRequest() {
publicShares["publicShares"][i] = pubShares[i];
}
Json::Value blsPublicKeys;
for (int i6 = 0; i6 <= testCount; i6++) {
......@@ -263,7 +259,6 @@ void TestUtils::sendRPCRequest() {
string blsName = "BLS_KEY" + polyNames[i].substr(4);
string secretShare = secretShares[i]["secretShare"].asString();
auto response = c.createBLSPrivateKey(blsName, ethKeys[i]["keyName"].asString(), polyNames[i], secShares[i],
t, n);
CHECK_STATE(response["status"] == 0);
......@@ -321,7 +316,7 @@ void TestUtils::sendRPCRequestV2() {
auto response = c.generateDKGPoly(polyName, t);
CHECK_STATE(response["status"] == 0);
polyNames[i] = polyName;
verifVects[i] = c.getVerificationVector(polyName, t, n);
verifVects[i] = c.getVerificationVector(polyName, t);
CHECK_STATE(verifVects[i]["status"] == 0);
pubEthKeys.append(ethKeys[i]["publicKey"]);
......@@ -399,47 +394,41 @@ void TestUtils::sendRPCRequestV2() {
sigShareSet.merge();
}
void TestUtils::sendRPCRequestZMQ() {
HttpClient client(RPC_ENDPOINT);
StubClient c(client, JSONRPC_CLIENT_V2);
auto client = make_shared<ZMQClient>(ZMQ_IP, ZMQ_PORT, true, "./sgx_data/cert_data/rootCA.pem",
"./sgx_data/cert_data/rootCA.key");
int n = 16, t = 16;
Json::Value ethKeys[n];
vector<string> ethKeys(n);
Json::Value verifVects[n];
Json::Value pubEthKeys;
Json::Value secretShares[n];
vector<string> secretShares(n);
Json::Value pubBLSKeys[n];
Json::Value blsSigShares[n];
vector <string> pubShares(n);
vector <string> polyNames(n);
vector<string> blsSigShares(n);
vector<string> pubShares(n);
vector<string> polyNames(n);
static atomic<int> counter(1);
int schainID = counter.fetch_add(1);
int dkgID = counter.fetch_add(1);
for (uint8_t i = 0; i < n; i++) {
ethKeys[i] = c.generateECDSAKey();
CHECK_STATE(ethKeys[i]["status"] == 0);
auto generatedKey = client->generateECDSAKey();
ethKeys[i] = generatedKey.second;
string polyName =
"POLY:SCHAIN_ID:" + to_string(schainID) + ":NODE_ID:" + to_string(i) + ":DKG_ID:" + to_string(dkgID);
auto response = c.generateDKGPoly(polyName, t);
CHECK_STATE(response["status"] == 0);
CHECK_STATE(client->generateDKGPoly(polyName, t));
polyNames[i] = polyName;
verifVects[i] = c.getVerificationVector(polyName, t, n);
CHECK_STATE(verifVects[i]["status"] == 0);
verifVects[i] = client->getVerificationVector(polyName, t);
pubEthKeys.append(ethKeys[i]["publicKey"]);
pubEthKeys.append(generatedKey.first);
}
for (uint8_t i = 0; i < n; i++) {
secretShares[i] = c.getSecretShareV2(polyNames[i], pubEthKeys, t, n);
secretShares[i] = client->getSecretShare(polyNames[i], pubEthKeys, t, n);
for (uint8_t k = 0; k < t; k++) {
for (uint8_t j = 0; j < 4; j++) {
string pubShare = verifVects[i]["verificationVector"][k][j].asString();
string pubShare = verifVects[i][k][j].asString();
pubShares[i] += convertDecToHex(pubShare);
}
}
......@@ -449,10 +438,10 @@ void TestUtils::sendRPCRequestZMQ() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
string secretShare = secretShares[i]["secretShare"].asString().substr(192 * j, 192);
secShares[i] += secretShares[j]["secretShare"].asString().substr(192 * i, 192);
Json::Value verif = c.dkgVerificationV2(pubShares[i], ethKeys[j]["keyName"].asString(), secretShare, t, n, j);
CHECK_STATE(verif["status"] == 0);
string secretShare = secretShares[i].substr(192 * j, 192);
secShares[i] += secretShares[j].substr(192 * i, 192);
bool verif = client->dkgVerification(pubShares[i], ethKeys[j], secretShare, t, n, j);
CHECK_STATE(verif);
}
BLSSigShareSet sigShareSet(t, n);
......@@ -472,16 +461,30 @@ void TestUtils::sendRPCRequestZMQ() {
publicShares["publicShares"][i] = pubShares[i];
}
Json::Value blsPublicKeys = c.calculateAllBLSPublicKeys(publicShares, t, n);
CHECK_STATE(blsPublicKeys["status"] == 0);
Json::Value blsPublicKeys = client->getAllBlsPublicKeys(publicShares, t, n);
for (int i = 0; i < t; i++) {
string blsName = "BLS_KEY" + polyNames[i].substr(4);
string secretShare = secretShares[i];
CHECK_STATE(client->createBLSPrivateKey(blsName, ethKeys[i], polyNames[i], secShares[i], t, n));
pubBLSKeys[i] = client->getBLSPublicKey(blsName);
libff::alt_bn128_G2 publicKey(libff::alt_bn128_Fq2(libff::alt_bn128_Fq(pubBLSKeys[i][0].asCString()),
libff::alt_bn128_Fq(pubBLSKeys[i][1].asCString())),
libff::alt_bn128_Fq2(libff::alt_bn128_Fq(pubBLSKeys[i][2].asCString()),
libff::alt_bn128_Fq(pubBLSKeys[i][3].asCString())),
libff::alt_bn128_Fq2::one());
string public_key_str = convertG2ToString(publicKey);
CHECK_STATE(public_key_str == blsPublicKeys[i].asString());
string hash = SAMPLE_HASH;
blsSigShares[i] = c.blsSignMessageHash(blsName, hash, t, n);
CHECK_STATE(blsSigShares[i]["status"] == 0);
blsSigShares[i] = client->blsSignMessageHash(blsName, hash, t, n);
CHECK_STATE(blsSigShares[i].length() > 0);
shared_ptr <string> sig_share_ptr = make_shared<string>(blsSigShares[i]["signatureShare"].asString());
shared_ptr <string> sig_share_ptr = make_shared<string>(blsSigShares[i]);
BLSSigShare sig(sig_share_ptr, i + 1, t, n);
sigShareSet.addSigShare(make_shared<BLSSigShare>(sig));
}
......@@ -527,7 +530,7 @@ void TestUtils::doDKG(StubClient &c, int n, int t,
Json::Value response = c.generateDKGPoly(polyName, t);
CHECK_STATE(response["status"] == 0);
polyNames[i] = polyName;
verifVects[i] = c.getVerificationVector(polyName, t, n);
verifVects[i] = c.getVerificationVector(polyName, t);
CHECK_STATE(verifVects[i]["status"] == 0);
pubEthKeys.append(ethKeys[i]["publicKey"]);
}
......@@ -668,7 +671,7 @@ void TestUtils::doDKGV2(StubClient &c, int n, int t,
Json::Value response = c.generateDKGPoly(polyName, t);
CHECK_STATE(response["status"] == 0);
polyNames[i] = polyName;
verifVects[i] = c.getVerificationVector(polyName, t, n);
verifVects[i] = c.getVerificationVector(polyName, t);
CHECK_STATE(verifVects[i]["status"] == 0);
pubEthKeys.append(ethKeys[i]["publicKey"]);
}
......@@ -810,7 +813,7 @@ void TestUtils::doZMQBLS(shared_ptr<ZMQClient> _zmqClient, StubClient &c, int n,
Json::Value response = c.generateDKGPoly(polyName, t);
CHECK_STATE(response["status"] == 0);
polyNames[i] = polyName;
verifVects[i] = c.getVerificationVector(polyName, t, n);
verifVects[i] = c.getVerificationVector(polyName, t);
CHECK_STATE(verifVects[i]["status"] == 0);
pubEthKeys.append(ethKeys[i]["publicKey"]);
}
......
......@@ -25,10 +25,7 @@
#define SGXWALLET_TESTUTILS_H
#include <dkg/dkg.h>
#include <jsonrpccpp/server/connectors/httpserver.h>
#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
// #include <libff/algebra/exponentiation/exponentiation.hpp>
// #include <libff/algebra/fields/fp.hpp>
#include <dkg/dkg.h>
#include "sgxwallet_common.h"
#include "third_party/intel/create_enclave.h"
......@@ -41,7 +38,7 @@
#include <sgx_tcrypto.h>
#include "stubclient.h"
#include <jsonrpccpp/server/connectors/httpserver.h>
#include "ZMQClient.h"
#include "zmq_src/ZMQClient.h"
#include "abstractstubserver.h"
using namespace std;
......
1.77.0
\ No newline at end of file
1.78.0
\ No newline at end of file
......@@ -45,7 +45,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
this->bindAndAddMethod(jsonrpc::Procedure("ecdsaSignMessageHash", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "base",jsonrpc::JSON_INTEGER,"keyName",jsonrpc::JSON_STRING,"messageHash",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::ecdsaSignMessageHashI);
this->bindAndAddMethod(jsonrpc::Procedure("generateDKGPoly", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::generateDKGPolyI);
this->bindAndAddMethod(jsonrpc::Procedure("getVerificationVector", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT,"polyName",jsonrpc::JSON_STRING, "t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getVerificationVectorI);
this->bindAndAddMethod(jsonrpc::Procedure("getVerificationVector", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName", jsonrpc::JSON_STRING, "t", jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getVerificationVectorI);
this->bindAndAddMethod(jsonrpc::Procedure("getSecretShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING,"publicKeys",jsonrpc::JSON_ARRAY, "n",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getSecretShareI);
this->bindAndAddMethod(jsonrpc::Procedure("dkgVerification", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "publicShares",jsonrpc::JSON_STRING, "ethKeyName",jsonrpc::JSON_STRING, "secretShare",jsonrpc::JSON_STRING,"t",jsonrpc::JSON_INTEGER, "n",jsonrpc::JSON_INTEGER, "index",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::dkgVerificationI);
this->bindAndAddMethod(jsonrpc::Procedure("createBLSPrivateKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "blsKeyName",jsonrpc::JSON_STRING, "ethKeyName",jsonrpc::JSON_STRING, "polyName", jsonrpc::JSON_STRING, "secretShare",jsonrpc::JSON_STRING,"t", jsonrpc::JSON_INTEGER,"n",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::createBLSPrivateKeyI);
......@@ -97,7 +97,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
}
inline virtual void getVerificationVectorI(const Json::Value &request, Json::Value &response)
{
response = this->getVerificationVector(request["polyName"].asString(), request["t"].asInt(), request["n"].asInt());
response = this->getVerificationVector(request["polyName"].asString(), request["t"].asInt());
}
inline virtual void getSecretShareI(const Json::Value &request, Json::Value &response)
{
......@@ -169,7 +169,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
virtual Json::Value ecdsaSignMessageHash(int base, const std::string& keyName, const std::string& messageHash) = 0;
virtual Json::Value generateDKGPoly(const std::string& polyName, int t) = 0;
virtual Json::Value getVerificationVector(const std::string& polyName, int t, int n) = 0;
virtual Json::Value getVerificationVector(const std::string& polyName, int t) = 0;
virtual Json::Value getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int t, int n) = 0;
virtual Json::Value dkgVerification( const std::string& publicShares, const std::string& ethKeyName, const std::string& SecretShare, int t, int n, int index) = 0;
virtual Json::Value createBLSPrivateKey(const std::string& blsKeyName, const std::string& ethKeyName, const std::string& polyName, const std::string& SecretShare, int t, int n) = 0;
......
......@@ -987,14 +987,14 @@ void trustedGetEncryptedSecretShareV2(int *errStatus, char *errString,
void trustedGetPublicShares(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret, uint64_t enc_len,
char *public_shares,
unsigned _t, unsigned _n) {
unsigned _t) {
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encrypted_dkg_secret);
CHECK_STATE(public_shares);
CHECK_STATE(_t <= _n && _n > 0)
CHECK_STATE(_t > 0)
SAFE_CHAR_BUF(decrypted_dkg_secret, DKG_MAX_SEALED_LEN);
......
......@@ -121,8 +121,7 @@ enclave {
[in, count = 3050] uint8_t* encrypted_dkg_secret,
uint64_t enc_len,
[out, count = 10000] char* public_shares,
unsigned _t,
unsigned _n);
unsigned _t);
public void trustedDkgVerify(
[out] int *errStatus,
......
......@@ -36,7 +36,7 @@
#include "TestUtils.h"
#include "ZMQServer.h"
#include "zmq_src/ZMQServer.h"
#include "testw.h"
#include "sgxwall.h"
......@@ -182,7 +182,6 @@ int main(int argc, char *argv[]) {
initAll(enclaveLogLevel, checkClientCertOption, checkClientCertOption, autoSignClientCertOption, generateTestKeys);
cerr << "Completed initAll." << endl;
//check if test keys already exist
string TEST_KEYS_4_NODE = "sgx_data/4node.json";
......@@ -194,7 +193,6 @@ int main(int argc, char *argv[]) {
cerr << "Found test keys." << endl;
}
if (generateTestKeys && !keysExist && !ExitHandler::shouldExit()) {
cerr << "Generating test keys ..." << endl;
......@@ -221,8 +219,6 @@ int main(int argc, char *argv[]) {
cerr << "Successfully completed generating test keys into sgx_data" << endl;
}
while ( !ExitHandler::shouldExit() ) {
sleep(10);
}
......
......@@ -98,11 +98,10 @@ class StubClient : public jsonrpc::Client
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value getVerificationVector(const std::string& polyName, int t, int n)
Json::Value getVerificationVector(const std::string& polyName, int t)
{
Json::Value p;
p["polyName"] = polyName;
p["n"] = n;
p["t"] = t;
Json::Value result = this->CallMethod("getVerificationVector",p);
if (result.isObject())
......
......@@ -57,15 +57,14 @@
#include "SGXRegistrationServer.h"
#include "SGXWalletServer.h"
#include "ZMQClient.h"
#include "ZMQServer.h"
#include "zmq_src/ZMQClient.h"
#include "zmq_src/ZMQServer.h"
#include "sgxwallet.h"
#include "TestUtils.h"
#include "testw.h"
#define PRINT_SRC_LINE cerr << "Executing line " << to_string(__LINE__) << endl;
using namespace jsonrpc;
using namespace std;
......@@ -97,7 +96,6 @@ public:
}
};
class TestFixtureZMQSign {
public:
TestFixtureZMQSign() {
......@@ -112,7 +110,6 @@ public:
}
};
class TestFixtureNoResetFromBackup {
public:
TestFixtureNoResetFromBackup() {
......@@ -127,7 +124,6 @@ public:
}
};
class TestFixtureNoReset {
public:
TestFixtureNoReset() {
......@@ -161,7 +157,6 @@ TEST_CASE_METHOD(TestFixture, "ECDSA AES keygen and signature test", "[ecdsa-aes
vector<char> signatureS(BUF_LEN, 0);
uint8_t signatureV = 0;
for (int i = 0; i < 50; i++) {
PRINT_SRC_LINE
status = trustedEcdsaSign(eid, &errStatus, errMsg.data(), encrPrivKey.data(), encLen,
......@@ -174,7 +169,6 @@ TEST_CASE_METHOD(TestFixture, "ECDSA AES keygen and signature test", "[ecdsa-aes
}
TEST_CASE_METHOD(TestFixture, "ECDSA AES key gen", "[ecdsa-aes-key-gen]") {
vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;
......@@ -191,7 +185,6 @@ TEST_CASE_METHOD(TestFixture, "ECDSA AES key gen", "[ecdsa-aes-key-gen]") {
REQUIRE(errStatus == SGX_SUCCESS);
}
TEST_CASE_METHOD(TestFixture, "ECDSA AES get public key", "[ecdsa-aes-get-pub-key]") {
int errStatus = 0;
vector<char> errMsg(BUF_LEN, 0);
......@@ -219,7 +212,6 @@ TEST_CASE_METHOD(TestFixture, "ECDSA AES get public key", "[ecdsa-aes-get-pub-ke
REQUIRE(errStatus == SGX_SUCCESS);
}
/* Do later
TEST_CASE_METHOD("BLS key encrypt/decrypt", "[bls-key-encrypt-decrypt]") {
resetDB();
......@@ -244,14 +236,9 @@ TEST_CASE_METHOD("BLS key encrypt/decrypt", "[bls-key-encrypt-decrypt]") {
printf("Decrypted key len %d\n", (int) strlen(plaintextKey));
printf("Decrypted key: %s\n", plaintextKey);
free(plaintextKey);
}
*/
string genECDSAKeyAPI(StubClient &_c) {
Json::Value genKey = _c.generateECDSAKey();
CHECK_STATE(genKey["status"].asInt() == 0);
......@@ -281,10 +268,8 @@ 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++) {
try {
PRINT_SRC_LINE
......@@ -308,7 +293,6 @@ TEST_CASE_METHOD(TestFixture, "BLS key encrypt", "[bls-key-encrypt]") {
sleep(3);
}
TEST_CASE_METHOD(TestFixture, "DKG AES gen test", "[dkg-aes-gen]") {
vector <uint8_t> encryptedDKGSecret(BUF_LEN, 0);
vector<char> errMsg(BUF_LEN, 0);
......@@ -333,7 +317,6 @@ TEST_CASE_METHOD(TestFixture, "DKG AES gen test", "[dkg-aes-gen]") {
sleep(3);
}
TEST_CASE_METHOD(TestFixture, "DKG AES public shares test", "[dkg-aes-pub-shares]") {
vector <uint8_t> encryptedDKGSecret(BUF_LEN, 0);
vector<char> errMsg(BUF_LEN, 0);
......@@ -353,7 +336,7 @@ TEST_CASE_METHOD(TestFixture, "DKG AES public shares test", "[dkg-aes-pub-shares
vector<char> pubShares(10000, 0);
PRINT_SRC_LINE
status = trustedGetPublicShares(eid, &errStatus, errMsg1.data(),
encryptedDKGSecret.data(), encLen, pubShares.data(), t, n);
encryptedDKGSecret.data(), encLen, pubShares.data(), t);
REQUIRE(status == SGX_SUCCESS);
REQUIRE(errStatus == SGX_SUCCESS);
......@@ -443,11 +426,8 @@ TEST_CASE_METHOD(TestFixture, "DKG AES encrypted secret shares version 2 test",
REQUIRE(errStatus == SGX_SUCCESS);
}
/*
* ( "verification test", "[verify]" ) {
char* pubshares = "0d72c21fc5a43452ad5f36699822309149ce6ce2cdce50dafa896e873f1b8ddd12f65a2e9c39c617a1f695f076b33b236b47ed773901fc2762f8b6f63277f5e30d7080be8e98c97f913d1920357f345dc0916c1fcb002b7beb060aa8b6b473a011bfafe9f8a5d8ea4c643ca4101e5119adbef5ae64f8dfb39cd10f1e69e31c591858d7eaca25b4c412fe909ca87ca7aadbf6d97d32d9b984e93d436f13d43ec31f40432cc750a64ac239cad6b8f78c1f1dd37427e4ff8c1cc4fe1c950fcbcec10ebfd79e0c19d0587adafe6db4f3c63ea9a329724a8804b63a9422e6898c0923209e828facf3a073254ec31af4231d999ba04eb5b7d1e0056d742a65b766f2f3";
char *sec_share = "11592366544581417165283270001305852351194685098958224535357729125789505948557";
mpz_t sshare;
......@@ -455,15 +435,8 @@ TEST_CASE_METHOD(TestFixture, "DKG AES encrypted secret shares version 2 test",
mpz_set_str(sshare, "11592366544581417165283270001305852351194685098958224535357729125789505948557", 10);
int result = Verification(pubshares, sshare, 2, 0);
REQUIRE(result == 1);
}*/
TEST_CASE_METHOD(TestFixture, "DKG_BLS test", "[dkg-bls]") {
HttpClient client(RPC_ENDPOINT);
StubClient c(client, JSONRPC_CLIENT_V2);
......@@ -506,7 +479,6 @@ TEST_CASE_METHOD(TestFixture, "DKG_BLS V2 test", "[dkg-bls-v2]") {
TestUtils::doDKGV2(c, 16, 5, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
}
TEST_CASE_METHOD(TestFixture, "DKG_BLS ZMQ test", "[dkgblszmq]") {
HttpClient client(RPC_ENDPOINT);
StubClient c(client, JSONRPC_CLIENT_V2);
......@@ -551,6 +523,24 @@ TEST_CASE_METHOD(TestFixture, "Delete Bls Key", "[delete-bls-key]") {
REQUIRE(c.deleteBlsKey(name)["deleted"] == true);
}
TEST_CASE_METHOD(TestFixture, "Delete Bls Key Zmq", "[delete-bls-key-zmq]") {
auto client = make_shared<ZMQClient>(ZMQ_IP, ZMQ_PORT, true, "./sgx_data/cert_data/rootCA.pem",
"./sgx_data/cert_data/rootCA.key");
std::string name = "BLS_KEY:SCHAIN_ID:123456789:NODE_ID:0:DKG_ID:0";
libff::alt_bn128_Fr key = libff::alt_bn128_Fr(
"6507625568967977077291849236396320012317305261598035438182864059942098934847");
std::string key_str = TestUtils::stringFromFr(key);
REQUIRE(!client->importBLSKeyShare(key_str, name));
key_str = "0xe632f7fde2c90a073ec43eaa90dca7b82476bf28815450a11191484934b9c3f";
REQUIRE(client->importBLSKeyShare(key_str, name));
REQUIRE_NOTHROW(client->blsSignMessageHash(name, SAMPLE_HASH, 1, 1));
REQUIRE(client->deleteBLSKey(name));
}
TEST_CASE_METHOD(TestFixture, "Import ECDSA Key", "[import-ecdsa-key]") {
HttpClient client(RPC_ENDPOINT);
StubClient c(client, JSONRPC_CLIENT_V2);
......@@ -567,6 +557,21 @@ TEST_CASE_METHOD(TestFixture, "Import ECDSA Key", "[import-ecdsa-key]") {
REQUIRE(c.ecdsaSignMessageHash(16, name, SAMPLE_HASH)["status"] == 0);
}
TEST_CASE_METHOD(TestFixture, "Import ECDSA Key Zmq", "[import-ecdsa-key-zmq]") {
auto client = make_shared<ZMQClient>(ZMQ_IP, ZMQ_PORT, true, "./sgx_data/cert_data/rootCA.pem",
"./sgx_data/cert_data/rootCA.key");
std::string name = "NEK:abcdef";
REQUIRE_THROWS(client->importECDSAKey("6507625568967977077291849236396320012317305261598035438182864059942098934847",
name));
string key_str = "0xe632f7fde2c90a073ec43eaa90dca7b82476bf28815450a11191484934b9c3f";
string response = client->importECDSAKey(key_str, name);
REQUIRE(response == client->getECDSAPublicKey(name));
REQUIRE_NOTHROW(client->ecdsaSignMessageHash(16, name, SAMPLE_HASH));
}
TEST_CASE_METHOD(TestFixture, "Backup Key", "[backup-key]") {
HttpClient client(RPC_ENDPOINT);
StubClient c(client, JSONRPC_CLIENT_V2);
......@@ -587,6 +592,13 @@ TEST_CASE_METHOD(TestFixture, "Get ServerStatus", "[get-server-status]") {
sleep(3);
}
TEST_CASE_METHOD(TestFixture, "Get ServerStatusZmq", "[get-server-status-zmq]") {
auto client = make_shared<ZMQClient>(ZMQ_IP, ZMQ_PORT, true, "./sgx_data/cert_data/rootCA.pem",
"./sgx_data/cert_data/rootCA.key");
REQUIRE_NOTHROW(client->getServerStatus());
sleep(3);
}
TEST_CASE_METHOD(TestFixture, "Get ServerVersion", "[get-server-version]") {
HttpClient client(RPC_ENDPOINT);
StubClient c(client, JSONRPC_CLIENT_V2);
......@@ -594,6 +606,12 @@ TEST_CASE_METHOD(TestFixture, "Get ServerVersion", "[get-server-version]") {
sleep(3);
}
TEST_CASE_METHOD(TestFixture, "Get ServerVersionZmq", "[get-server-version-zmq]") {
auto client = make_shared<ZMQClient>(ZMQ_IP, ZMQ_PORT, true, "./sgx_data/cert_data/rootCA.pem",
"./sgx_data/cert_data/rootCA.key");
REQUIRE(client->getServerVersion() == SGXWalletServer::getVersion());
sleep(3);
}
TEST_CASE_METHOD(TestFixtureHTTPS, "Cert request sign", "[cert-sign]") {
......@@ -617,15 +635,12 @@ TEST_CASE_METHOD(TestFixtureHTTPS, "Cert request sign", "[cert-sign]") {
REQUIRE(result["status"] == 0);
PRINT_SRC_LINE
result = SGXRegistrationServer::getServer()->SignCertificate("Haha");
REQUIRE(result["status"] != 0);
}
TEST_CASE_METHOD(TestFixture, "DKG API V2 test", "[dkg-api-v2]") {
HttpClient client(RPC_ENDPOINT);
StubClient c(client, JSONRPC_CLIENT_V2);
......@@ -644,7 +659,7 @@ TEST_CASE_METHOD(TestFixture, "DKG API V2 test", "[dkg-api-v2]") {
Json::Value genPolyWrongName = c.generateDKGPoly("poly", 2);
REQUIRE(genPolyWrongName["status"].asInt() != 0);
Json::Value verifVectWrongName = c.getVerificationVector("poly", 2, 2);
Json::Value verifVectWrongName = c.getVerificationVector("poly", 2);
REQUIRE(verifVectWrongName["status"].asInt() != 0);
Json::Value secretSharesWrongName = c.getSecretShareV2("poly", publicKeys, 2, 2);
......@@ -654,16 +669,12 @@ TEST_CASE_METHOD(TestFixture, "DKG API V2 test", "[dkg-api-v2]") {
Json::Value genPolyWrong_t = c.generateDKGPoly(polyName, 33);
REQUIRE(genPolyWrong_t["status"].asInt() != 0);
Json::Value verifVectWrong_t = c.getVerificationVector(polyName, 1, 2);
Json::Value verifVectWrong_t = c.getVerificationVector(polyName, 1);
REQUIRE(verifVectWrong_t["status"].asInt() != 0);
Json::Value secretSharesWrong_t = c.getSecretShareV2(polyName, publicKeys, 3, 3);
REQUIRE(secretSharesWrong_t["status"].asInt() != 0);
// wrong_n
Json::Value verifVectWrong_n = c.getVerificationVector(polyName, 2, 1);
REQUIRE(verifVectWrong_n["status"].asInt() != 0);
Json::Value publicKeys1;
publicKeys1.append(SAMPLE_DKG_PUB_KEY_1);
Json::Value secretSharesWrong_n = c.getSecretShareV2(polyName, publicKeys1, 2, 1);
......@@ -678,14 +689,60 @@ TEST_CASE_METHOD(TestFixture, "DKG API V2 test", "[dkg-api-v2]") {
REQUIRE_NOTHROW(c.getSecretShare(polyName, publicKeys, 2, 2));
REQUIRE(Skeys == c.getSecretShare(polyName, publicKeys, 2, 2));
Json::Value verifVect = c.getVerificationVector(polyName, 2, 2);
REQUIRE_NOTHROW(c.getVerificationVector(polyName, 2, 2));
REQUIRE(verifVect == c.getVerificationVector(polyName, 2, 2));
Json::Value verifVect = c.getVerificationVector(polyName, 2);
REQUIRE_NOTHROW(c.getVerificationVector(polyName, 2));
REQUIRE(verifVect == c.getVerificationVector(polyName, 2));
Json::Value verificationWrongSkeys = c.dkgVerificationV2("", "", "", 2, 2, 1);
REQUIRE(verificationWrongSkeys["status"].asInt() != 0);
}
TEST_CASE_METHOD(TestFixture, "DKG API V2 ZMQ test", "[dkg-api-v2-zmq]") {
auto client = make_shared<ZMQClient>(ZMQ_IP, ZMQ_PORT, true, "./sgx_data/cert_data/rootCA.pem",
"./sgx_data/cert_data/rootCA.key");
string polyName = SAMPLE_POLY_NAME;
PRINT_SRC_LINE
REQUIRE(client->generateDKGPoly(polyName, 2));
Json::Value publicKeys;
publicKeys.append(SAMPLE_DKG_PUB_KEY_1);
publicKeys.append(SAMPLE_DKG_PUB_KEY_2);
// wrongName
REQUIRE(!client->generateDKGPoly("poly", 2));
REQUIRE_THROWS(client->getVerificationVector("poly", 2));
REQUIRE_THROWS(client->getSecretShare("poly", publicKeys, 2, 2));
// wrong_t
REQUIRE(!client->generateDKGPoly(polyName, 33));
REQUIRE_THROWS(client->getVerificationVector(polyName, 0));
REQUIRE_THROWS(client->getSecretShare(polyName, publicKeys, 3, 3));
Json::Value publicKeys1;
publicKeys1.append(SAMPLE_DKG_PUB_KEY_1);
REQUIRE_THROWS(client->getSecretShare(polyName, publicKeys1, 2, 1));
//wrong number of publicKeys
REQUIRE_THROWS(client->getSecretShare(polyName, publicKeys, 2, 3));
//wrong verif
string Skeys = client->getSecretShare(polyName, publicKeys, 2, 2);
REQUIRE_NOTHROW(client->getSecretShare(polyName, publicKeys, 2, 2));
REQUIRE(Skeys == client->getSecretShare(polyName, publicKeys, 2, 2));
Json::Value verifVect = client->getVerificationVector(polyName, 2);
REQUIRE_NOTHROW(client->getVerificationVector(polyName, 2));
REQUIRE(verifVect == client->getVerificationVector(polyName, 2));
REQUIRE_THROWS(client->dkgVerification("", "", "", 2, 2, 1));
}
TEST_CASE_METHOD(TestFixture, "PolyExists test", "[dkg-poly-exists]") {
HttpClient client(RPC_ENDPOINT);
StubClient c(client, JSONRPC_CLIENT_V2);
......@@ -705,7 +762,19 @@ TEST_CASE_METHOD(TestFixture, "PolyExists test", "[dkg-poly-exists]") {
REQUIRE(!polyDoesNotExist["IsExist"].asBool());
}
TEST_CASE_METHOD(TestFixture, "PolyExistsZmq test", "[dkg-poly-exists-zmq]") {
auto client = make_shared<ZMQClient>(ZMQ_IP, ZMQ_PORT, true, "./sgx_data/cert_data/rootCA.pem",
"./sgx_data/cert_data/rootCA.key");
string polyName = SAMPLE_POLY_NAME;
REQUIRE_NOTHROW(client->generateDKGPoly(polyName, 2));
bool polyExists = client->isPolyExists(polyName);
REQUIRE(polyExists);
bool polyDoesNotExist = client->isPolyExists("Vasya");
REQUIRE(!polyDoesNotExist);
}
TEST_CASE_METHOD(TestFixture, "AES_DKG V2 test", "[aes-dkg-v2]") {
HttpClient client(RPC_ENDPOINT);
......@@ -735,7 +804,7 @@ TEST_CASE_METHOD(TestFixture, "AES_DKG V2 test", "[aes-dkg-v2]") {
polyNames[i] = polyName;
PRINT_SRC_LINE
verifVects[i] = c.getVerificationVector(polyName, t, n);
verifVects[i] = c.getVerificationVector(polyName, t);
REQUIRE(verifVects[i]["status"] == 0);
pubEthKeys.append(ethKeys[i]["publicKey"]);
......@@ -831,8 +900,7 @@ TEST_CASE_METHOD(TestFixture, "AES_DKG V2 test", "[aes-dkg-v2]") {
string hash = SAMPLE_HASH;
auto hash_arr = make_shared < array < uint8_t,
32 > > ();
auto hash_arr = make_shared < array < uint8_t, 32 > > ();
uint64_t binLen;
......@@ -846,8 +914,7 @@ TEST_CASE_METHOD(TestFixture, "AES_DKG V2 test", "[aes-dkg-v2]") {
string endName = polyNames[i].substr(4);
string blsName = "BLS_KEY" + polyNames[i].substr(4);
auto response = c.createBLSPrivateKeyV2(blsName, ethKeys[i]["keyName"].asString(), polyNames[i], secShares[i],
t,
n);
t, n);
REQUIRE(response["status"] == 0);
PRINT_SRC_LINE
......@@ -879,6 +946,156 @@ TEST_CASE_METHOD(TestFixture, "AES_DKG V2 test", "[aes-dkg-v2]") {
REQUIRE(common_public.VerifySigWithHelper(hash_arr, commonSig, t, n));
}
TEST_CASE_METHOD(TestFixture, "AES_DKG V2 ZMQ test", "[aes-dkg-v2-zmq]") {
auto client = make_shared<ZMQClient>(ZMQ_IP, ZMQ_PORT, true, "./sgx_data/cert_data/rootCA.pem",
"./sgx_data/cert_data/rootCA.key");
int n = 2, t = 2;
vector<string> ethKeys(n);
Json::Value verifVects[n];
Json::Value pubEthKeys;
vector<string> secretShares(n);
Json::Value pubBLSKeys[n];
vector<string> blsSigShares(n);
vector<string> pubShares(n);
vector<string> polyNames(n);
int schainID = TestUtils::randGen();
int dkgID = TestUtils::randGen();
for (uint8_t i = 0; i < n; i++) {
auto generatedKey = client->generateECDSAKey();
ethKeys[i] = generatedKey.second;
string polyName =
"POLY:SCHAIN_ID:" + to_string(schainID) + ":NODE_ID:" + to_string(i) + ":DKG_ID:" + to_string(dkgID);
CHECK_STATE(client->generateDKGPoly(polyName, t));
polyNames[i] = polyName;
verifVects[i] = client->getVerificationVector(polyName, t);
pubEthKeys.append(generatedKey.first);
}
for (uint8_t i = 0; i < n; i++) {
secretShares[i] = client->getSecretShare(polyNames[i], pubEthKeys, t, n);
for (uint8_t k = 0; k < t; k++) {
for (uint8_t j = 0; j < 4; j++) {
string pubShare = verifVects[i][k][j].asString();
pubShares[i] += TestUtils::convertDecToHex(pubShare);
}
}
}
int k = 0;
vector <string> secShares(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
string secretShare = secretShares[i].substr(192 * j, 192);
secShares[i] += secretShares[j].substr(192 * i, 192);
REQUIRE(client->dkgVerification(pubShares[i], ethKeys[j], secretShare, t, n, j));
k++;
}
auto complaintResponse = client->complaintResponse(polyNames[1], t, n, 0);
string dhKey = std::get<0>(complaintResponse);
string shareG2 = std::get<1>(complaintResponse);
string secretShare = secretShares[1].substr(0, 192);
vector<char> message(65, 0);
SAFE_CHAR_BUF(encr_sshare, BUF_LEN)
strncpy(encr_sshare, pubEthKeys[0].asString().c_str(), 128);
SAFE_CHAR_BUF(common_key, BUF_LEN);
REQUIRE(sessionKeyRecoverDH(dhKey.c_str(), encr_sshare, common_key) == 0);
uint8_t key_to_hash[33];
uint64_t len;
REQUIRE( hex2carray(common_key, &len, key_to_hash, 64) );
auto hashed_key = cryptlite::sha256::hash_hex(string((char*)key_to_hash, 32));
SAFE_CHAR_BUF(derived_key, 33)
uint64_t key_length;
REQUIRE(hex2carray(&hashed_key[0], &key_length, (uint8_t *) derived_key, 33));
SAFE_CHAR_BUF(encr_sshare_check, BUF_LEN)
strncpy(encr_sshare_check, secretShare.c_str(), ECDSA_SKEY_LEN - 1);
REQUIRE(xorDecryptDHV2(derived_key, encr_sshare_check, message) == 0);
mpz_t hex_share;
mpz_init(hex_share);
mpz_set_str(hex_share, message.data(), 16);
libff::alt_bn128_Fr share(hex_share);
libff::alt_bn128_G2 decrypted_share_G2 = share * libff::alt_bn128_G2::one();
decrypted_share_G2.to_affine_coordinates();
mpz_clear(hex_share);
REQUIRE(convertG2ToString(decrypted_share_G2) == shareG2);
Json::Value verificationVectorMult = std::get<2>(complaintResponse);
libff::alt_bn128_G2 verificationValue = libff::alt_bn128_G2::zero();
for (int i = 0; i < t; ++i) {
libff::alt_bn128_G2 value;
value.Z = libff::alt_bn128_Fq2::one();
value.X.c0 = libff::alt_bn128_Fq(verificationVectorMult[i][0].asCString());
value.X.c1 = libff::alt_bn128_Fq(verificationVectorMult[i][1].asCString());
value.Y.c0 = libff::alt_bn128_Fq(verificationVectorMult[i][2].asCString());
value.Y.c1 = libff::alt_bn128_Fq(verificationVectorMult[i][3].asCString());
verificationValue = verificationValue + value;
}
verificationValue.to_affine_coordinates();
REQUIRE(verificationValue == decrypted_share_G2);
BLSSigShareSet sigShareSet(t, n);
string hash = SAMPLE_HASH;
auto hash_arr = make_shared < array < uint8_t, 32 > > ();
uint64_t binLen;
if (!hex2carray(hash.c_str(), &binLen, hash_arr->data(), 32)) {
throw SGXException(TEST_INVALID_HEX, "Invalid hash");
}
map <size_t, shared_ptr<BLSPublicKeyShare>> coeffs_pkeys_map;
for (int i = 0; i < t; i++) {
string blsName = "BLS_KEY" + polyNames[i].substr(4);
REQUIRE(client->createBLSPrivateKey(blsName, ethKeys[i], polyNames[i], secShares[i], t, n));
pubBLSKeys[i] = client->getBLSPublicKey(blsName);
string hash = SAMPLE_HASH;
blsSigShares[i] = client->blsSignMessageHash(blsName, hash, t, n);
REQUIRE(blsSigShares[i].length() > 0);
shared_ptr <string> sig_share_ptr = make_shared<string>(blsSigShares[i]);
BLSSigShare sig(sig_share_ptr, i + 1, t, n);
sigShareSet.addSigShare(make_shared<BLSSigShare>(sig));
vector <string> pubKey_vect;
for (uint8_t j = 0; j < 4; j++) {
pubKey_vect.push_back(pubBLSKeys[i][j].asString());
}
BLSPublicKeyShare pubKey(make_shared < vector < string >> (pubKey_vect), t, n);
REQUIRE(pubKey.VerifySigWithHelper(hash_arr, make_shared<BLSSigShare>(sig), t, n));
coeffs_pkeys_map[i + 1] = make_shared<BLSPublicKeyShare>(pubKey);
}
shared_ptr <BLSSignature> commonSig = sigShareSet.merge();
BLSPublicKey
common_public(make_shared < map < size_t, shared_ptr < BLSPublicKeyShare >>>(coeffs_pkeys_map), t, n);
REQUIRE(common_public.VerifySigWithHelper(hash_arr, commonSig, t, n));
}
TEST_CASE_METHOD(TestFixture, "AES encrypt/decrypt", "[aes-encrypt-decrypt]") {
int errStatus = 0;
vector<char> errMsg(BUF_LEN, 0);
......@@ -902,8 +1119,6 @@ TEST_CASE_METHOD(TestFixture, "AES encrypt/decrypt", "[aes-encrypt-decrypt]") {
sleep(3);
}
TEST_CASE_METHOD(TestFixture, "Many threads ecdsa dkg v2 bls", "[many-threads-crypto-v2]") {
vector <thread> threads;
int num_threads = 4;
......@@ -916,7 +1131,17 @@ TEST_CASE_METHOD(TestFixture, "Many threads ecdsa dkg v2 bls", "[many-threads-cr
}
}
TEST_CASE_METHOD(TestFixture, "Many threads ecdsa dkg v2 bls zmq", "[many-threads-crypto-v2-zmq]") {
vector <thread> threads;
int num_threads = 4;
for (int i = 0; i < num_threads; i++) {
threads.push_back(thread(TestUtils::sendRPCRequestZMQ));
}
for (auto &thread : threads) {
thread.join();
}
}
TEST_CASE_METHOD(TestFixture, "First run", "[first-run]") {
......@@ -935,8 +1160,6 @@ TEST_CASE_METHOD(TestFixture, "First run", "[first-run]") {
}
sleep(3);
}
TEST_CASE_METHOD(TestFixtureNoReset, "Second run", "[second-run]") {
......@@ -959,15 +1182,12 @@ TEST_CASE_METHOD(TestFixtureNoReset, "Second run", "[second-run]") {
}
}
TEST_CASE_METHOD(TestFixtureZMQSign, "ZMQ-ecdsa", "[zmq-ecdsa]") {
HttpClient htp(RPC_ENDPOINT);
StubClient c(htp, JSONRPC_CLIENT_V2);
string ip = ZMQ_IP;
auto client = make_shared<ZMQClient>(ip, ZMQ_PORT, true, "./sgx_data/cert_data/rootCA.pem",
auto client = make_shared<ZMQClient>(ZMQ_IP, ZMQ_PORT, true, "./sgx_data/cert_data/rootCA.pem",
"./sgx_data/cert_data/rootCA.key");
string keyName = "";
......@@ -977,10 +1197,8 @@ TEST_CASE_METHOD(TestFixtureZMQSign, "ZMQ-ecdsa", "[zmq-ecdsa]") {
int end = 10000000;
string sh = string(SAMPLE_HASH);
std::vector <std::thread> workers;
PRINT_SRC_LINE
for (int j = 0; j < 2; j++) {
......@@ -1003,6 +1221,4 @@ TEST_CASE_METHOD(TestFixtureZMQSign, "ZMQ-ecdsa", "[zmq-ecdsa]") {
}
TEST_CASE_METHOD(TestFixtureNoResetFromBackup, "Backup restore", "[backup-restore]") {
}
TEST_CASE_METHOD(TestFixtureNoResetFromBackup, "Backup restore", "[backup-restore]") {}
......@@ -29,18 +29,21 @@ username = getpass.getuser()
topDir = os.getcwd() + "/sgxwallet"
print("Top directory is:" + topDir)
testList = [ "[zmq-ecdsa]",
"[dkgzmqbls]",
"[first-run]",
"[second-run]",
"[many-threads-crypto]",
"[many-threads-crypto-v2]",
"[many-threads-crypto-v2-zmq]"
"[backup-restore]",
"[cert-sign]",
"[get-server-status]",
"[get-server-status-zmq]",
"[get-server-version]",
"[get-server-version-zmq]",
"[backup-key]",
"[delete-bls-key]",
"[delete-bls-key-zmq]",
"[import-ecdsa-key]",
"[import-ecdsa-key-zmq]",
"[ecdsa-aes-key-gen]",
"[ecdsa-aes-key-sig-gen]",
"[ecdsa-aes-get-pub-key]",
......@@ -49,15 +52,17 @@ testList = [ "[zmq-ecdsa]",
"[dkg-aes-gen]",
"[dkg-aes-encr-sshares]",
"[dkg-aes-encr-sshares-v2]",
"[dkg-api]",
"[dkg-api-v2]",
"[dkg-api-v2-zmq]",
"[dkg-bls]",
"[dkgzmqbls]",
"[dkg-bls-v2]",
"[dkg-poly-exists]",
"[dkg-poly-exists-zmq]",
"[dkg-aes-pub-shares]",
"[aes-encrypt-decrypt]",
"[aes-dkg]",
"[aes-dkg-v2]"
"[aes-dkg-v2]",
"[aes-dkg-v2-zmq]"
]
......
/*
Copyright (C) 2018-2019 SKALE Labs
This file is part of libBLS.
This file is part of sgxwallet.
libBLS is free software: you can redistribute it and/or modify
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file BLSRspSignMessage.h
@file BLSRspSignMessage.cpp
@author Stan Kladko
@date 2020
*/
......
/*
Copyright (C) 2018-2019 SKALE Labs
This file is part of libBLS.
This file is part of sgxwallet.
libBLS is free software: you can redistribute it and/or modify
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file BLSRspSignMessage.h
@author Stan Kladko
......
/*
Copyright (C) 2018- SKALE Labs
This file is part of libBLS.
This file is part of sgxwallet.
libBLS is free software: you can redistribute it and/or modify
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file ECDSARspSignMessage.cpp
@author Stan Kladko
......
/*
Copyright (C) 2018- SKALE Labs
This file is part of libBLS.
This file is part of sgxwallet.
libBLS is free software: you can redistribute it and/or modify
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libBLS is distributed in the hope that it will be useful,
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with libBLS. If not, see <https://www.gnu.org/licenses/>.
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file ECDSARspSignMessage.h
@author Stan Kladko
......
/*
Copyright (C) 2018- SKALE Labs
This file is part of sgxwallet.
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file ReqMessage.cpp
@author Oleh Nikolaiev
@date 2021
*/
#include "SGXWalletServer.hpp"
#include "ReqMessage.h"
Json::Value ECDSASignReqMessage::process() {
auto base = getInt64Rapid("base");
auto keyName = getStringRapid("keyName");
auto hash = getStringRapid("messageHash");
auto result = SGXWalletServer::ecdsaSignMessageHashImpl(base, keyName, hash);
result["type"] = ZMQMessage::ECDSA_SIGN_RSP;
return result;
}
Json::Value BLSSignReqMessage::process() {
auto keyName = getStringRapid("keyShareName");
auto hash = getStringRapid("messageHash");
auto t = getInt64Rapid("t");
auto n = getInt64Rapid("n");
auto result = SGXWalletServer::blsSignMessageHashImpl(keyName, hash, t, n);
result["type"] = ZMQMessage::BLS_SIGN_RSP;
return result;
}
Json::Value importBLSReqMessage::process() {
auto keyName = getStringRapid("keyShareName");
auto keyShare = getStringRapid("keyShare");
auto result = SGXWalletServer::importBLSKeyShareImpl(keyShare, keyName);
result["type"] = ZMQMessage::IMPORT_BLS_RSP;
return result;
}
Json::Value importECDSAReqMessage::process() {
auto keyName = getStringRapid("keyName");
auto key = getStringRapid("key");
auto result = SGXWalletServer::importECDSAKeyImpl(key, keyName);
result["type"] = ZMQMessage::IMPORT_ECDSA_RSP;
return result;
}
Json::Value generateECDSAReqMessage::process() {
auto result = SGXWalletServer::generateECDSAKeyImpl();
result["type"] = ZMQMessage::GENERATE_ECDSA_RSP;
return result;
}
Json::Value getPublicECDSAReqMessage::process() {
auto keyName = getStringRapid("keyName");
auto result = SGXWalletServer::getPublicECDSAKeyImpl(keyName);
result["type"] = ZMQMessage::GET_PUBLIC_ECDSA_RSP;
return result;
}
Json::Value generateDKGPolyReqMessage::process() {
auto polyName = getStringRapid("polyName");
auto t = getInt64Rapid("t");
auto result = SGXWalletServer::generateDKGPolyImpl(polyName, t);
result["type"] = ZMQMessage::GENERATE_DKG_POLY_RSP;
return result;
}
Json::Value getVerificationVectorReqMessage::process() {
auto polyName = getStringRapid("polyName");
auto t = getInt64Rapid("t");
auto result = SGXWalletServer::getVerificationVectorImpl(polyName, t);
result["type"] = ZMQMessage::GET_VV_RSP;
return result;
}
Json::Value getSecretShareReqMessage::process() {
auto polyName = getStringRapid("polyName");
auto t = getInt64Rapid("t");
auto n = getInt64Rapid("n");
auto pubKeys = getJsonValueRapid("publicKeys");
auto result = SGXWalletServer::getSecretShareV2Impl(polyName, pubKeys, t, n);
result["type"] = ZMQMessage::GET_SECRET_SHARE_RSP;
return result;
}
Json::Value dkgVerificationReqMessage::process() {
auto ethKeyName = getStringRapid("ethKeyName");
auto t = getInt64Rapid("t");
auto n = getInt64Rapid("n");
auto idx = getInt64Rapid("index");
auto pubShares = getStringRapid("publicShares");
auto secretShare = getStringRapid("secretShare");
auto result = SGXWalletServer::dkgVerificationV2Impl(pubShares, ethKeyName, secretShare, t, n, idx);
result["type"] = ZMQMessage::DKG_VERIFY_RSP;
return result;
}
Json::Value createBLSPrivateKeyReqMessage::process() {
auto blsKeyName = getStringRapid("blsKeyName");
auto ethKeyName = getStringRapid("ethKeyName");
auto polyName = getStringRapid("polyName");
auto secretShare = getStringRapid("secretShare");
auto t = getInt64Rapid("t");
auto n = getInt64Rapid("n");
auto result = SGXWalletServer::createBLSPrivateKeyV2Impl(blsKeyName, ethKeyName, polyName, secretShare, t, n);
result["type"] = ZMQMessage::CREATE_BLS_PRIVATE_RSP;
return result;
}
Json::Value getBLSPublicReqMessage::process() {
auto blsKeyName = getStringRapid("blsKeyName");
auto result = SGXWalletServer::getBLSPublicKeyShareImpl(blsKeyName);
result["type"] = ZMQMessage::GET_BLS_PUBLIC_RSP;
return result;
}
Json::Value getAllBLSPublicKeysReqMessage::process() {
auto t = getInt64Rapid("t");
auto n = getInt64Rapid("n");
auto pubShares = getJsonValueRapid("publicShares");
auto result = SGXWalletServer::calculateAllBLSPublicKeysImpl(pubShares, t, n);
result["type"] = ZMQMessage::GET_ALL_BLS_PUBLIC_RSP;
return result;
}
Json::Value complaintResponseReqMessage::process() {
auto polyName = getStringRapid("polyName");
auto t = getInt64Rapid("t");
auto n = getInt64Rapid("n");
auto idx = getInt64Rapid("ind");
auto result = SGXWalletServer::complaintResponseImpl(polyName, t, n, idx);
result["type"] = ZMQMessage::COMPLAINT_RESPONSE_RSP;
return result;
}
Json::Value multG2ReqMessage::process() {
auto x = getStringRapid("x");
auto result = SGXWalletServer::multG2Impl(x);
result["type"] = ZMQMessage::MULT_G2_RSP;
return result;
}
Json::Value isPolyExistsReqMessage::process() {
auto polyName = getStringRapid("polyName");
auto result = SGXWalletServer::isPolyExistsImpl(polyName);
result["type"] = ZMQMessage::IS_POLY_EXISTS_RSP;
return result;
}
Json::Value getServerStatusReqMessage::process() {
auto result = SGXWalletServer::getServerStatusImpl();
result["type"] = ZMQMessage::GET_SERVER_STATUS_RSP;
return result;
}
Json::Value getServerVersionReqMessage::process() {
auto result = SGXWalletServer::getServerVersionImpl();
result["type"] = ZMQMessage::GET_SERVER_VERSION_RSP;
return result;
}
Json::Value deleteBLSKeyReqMessage::process() {
auto blsKeyName = getStringRapid("blsKeyName");
auto result = SGXWalletServer::deleteBlsKeyImpl(blsKeyName);
result["type"] = ZMQMessage::DELETE_BLS_KEY_RSP;
return result;
}
/*
Copyright (C) 2018- SKALE Labs
This file is part of sgxwallet.
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file ReqMessage.h
@author Oleh Nikolaiev
@date 2021
*/
#ifndef SGXWALLET_REQMESSAGE_H
#define SGXWALLET_REQMESSAGE_H
#include "ZMQMessage.h"
class ECDSASignReqMessage : public ZMQMessage {
public:
ECDSASignReqMessage(shared_ptr <rapidjson::Document> &_d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class BLSSignReqMessage : public ZMQMessage {
public:
BLSSignReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class importBLSReqMessage : public ZMQMessage {
public:
importBLSReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class importECDSAReqMessage : public ZMQMessage {
public:
importECDSAReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class generateECDSAReqMessage : public ZMQMessage {
public:
generateECDSAReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class getPublicECDSAReqMessage : public ZMQMessage {
public:
getPublicECDSAReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class generateDKGPolyReqMessage : public ZMQMessage {
public:
generateDKGPolyReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class getVerificationVectorReqMessage : public ZMQMessage {
public:
getVerificationVectorReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class getSecretShareReqMessage : public ZMQMessage {
public:
getSecretShareReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class dkgVerificationReqMessage : public ZMQMessage {
public:
dkgVerificationReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class createBLSPrivateKeyReqMessage : public ZMQMessage {
public:
createBLSPrivateKeyReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class getBLSPublicReqMessage : public ZMQMessage {
public:
getBLSPublicReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class getAllBLSPublicKeysReqMessage : public ZMQMessage {
public:
getAllBLSPublicKeysReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class complaintResponseReqMessage : public ZMQMessage {
public:
complaintResponseReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class multG2ReqMessage : public ZMQMessage {
public:
multG2ReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class isPolyExistsReqMessage : public ZMQMessage {
public:
isPolyExistsReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class getServerStatusReqMessage : public ZMQMessage {
public:
getServerStatusReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class getServerVersionReqMessage : public ZMQMessage {
public:
getServerVersionReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class deleteBLSKeyReqMessage : public ZMQMessage {
public:
deleteBLSKeyReqMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
#endif //SGXWALLET_REQMESSAGE_H
/*
Copyright (C) 2018- SKALE Labs
This file is part of sgxwallet.
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file RspMessage.cpp
@author Oleh Nikolaiev
@date 2021
*/
#include "SGXWalletServer.hpp"
#include "RspMessage.h"
Json::Value ECDSASignRspMessage::process() {
assert(false);
}
string ECDSASignRspMessage::getSignature() {
string r = getStringRapid("signature_r");
string v = getStringRapid("signature_v");
string s = getStringRapid("signature_s");
auto ret = v + ":" + r.substr( 2 ) + ":" + s.substr( 2 );
return ret;
}
Json::Value BLSSignRspMessage::process() {
assert(false);
}
Json::Value importBLSRspMessage::process() {
assert(false);
}
Json::Value importECDSARspMessage::process() {
assert(false);
}
Json::Value generateECDSARspMessage::process() {
assert(false);
}
Json::Value getPublicECDSARspMessage::process() {
assert(false);
}
Json::Value generateDKGPolyRspMessage::process() {
assert(false);
}
Json::Value getVerificationVectorRspMessage::process() {
assert(false);
}
Json::Value getSecretShareRspMessage::process() {
assert(false);
}
Json::Value dkgVerificationRspMessage::process() {
assert(false);
}
Json::Value createBLSPrivateKeyRspMessage::process() {
assert(false);
}
Json::Value getBLSPublicRspMessage::process() {
assert(false);
}
Json::Value getAllBLSPublicKeysRspMessage::process() {
assert(false);
}
Json::Value complaintResponseRspMessage::process() {
assert(false);
}
Json::Value multG2RspMessage::process() {
assert(false);
}
Json::Value isPolyExistsRspMessage::process() {
assert(false);
}
Json::Value getServerStatusRspMessage::process() {
assert(false);
}
Json::Value getServerVersionRspMessage::process() {
assert(false);
}
Json::Value deleteBLSKeyRspMessage::process() {
assert(false);
}
/*
Copyright (C) 2018- SKALE Labs
This file is part of sgxwallet.
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file RspMessage.h
@author Oleh Nikolaiev
@date 2021
*/
#ifndef SGXWALLET_RSPMESSAGE_H
#define SGXWALLET_RSPMESSAGE_H
#include "ZMQMessage.h"
class ECDSASignRspMessage : public ZMQMessage {
public:
ECDSASignRspMessage(shared_ptr <rapidjson::Document> &_d) : ZMQMessage(_d) {};
virtual Json::Value process();
string getSignature();
};
class BLSSignRspMessage : public ZMQMessage {
public:
BLSSignRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
string getSigShare() {
return getStringRapid("signatureShare");
}
};
class importBLSRspMessage : public ZMQMessage {
public:
importBLSRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class importECDSARspMessage : public ZMQMessage {
public:
importECDSARspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
string getECDSAPublicKey() {
return getStringRapid("publicKey");
}
};
class generateECDSARspMessage : public ZMQMessage {
public:
generateECDSARspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
string getECDSAPublicKey() {
return getStringRapid("publicKey");
}
string getKeyName() {
return getStringRapid("keyName");
}
};
class getPublicECDSARspMessage : public ZMQMessage {
public:
getPublicECDSARspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
string getECDSAPublicKey() {
return getStringRapid("publicKey");
}
};
class generateDKGPolyRspMessage : public ZMQMessage {
public:
generateDKGPolyRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class getVerificationVectorRspMessage : public ZMQMessage {
public:
getVerificationVectorRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
Json::Value getVerificationVector() {
return getJsonValueRapid("verificationVector");
}
};
class getSecretShareRspMessage : public ZMQMessage {
public:
getSecretShareRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
string getSecretShare() {
return getStringRapid("secretShare");
}
};
class dkgVerificationRspMessage : public ZMQMessage {
public:
dkgVerificationRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
bool isCorrect() {
return getBoolRapid("result");
}
};
class createBLSPrivateKeyRspMessage : public ZMQMessage {
public:
createBLSPrivateKeyRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class getBLSPublicRspMessage : public ZMQMessage {
public:
getBLSPublicRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
Json::Value getBLSPublicKey() {
return getJsonValueRapid("blsPublicKeyShare");
}
};
class getAllBLSPublicKeysRspMessage : public ZMQMessage {
public:
getAllBLSPublicKeysRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
Json::Value getPublicKeys() {
return getJsonValueRapid("publicKeys");
}
};
class complaintResponseRspMessage : public ZMQMessage {
public:
complaintResponseRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
string getDHKey() {
return getStringRapid("dhKey");
}
string getShare() {
return getStringRapid("share*G2");
}
Json::Value getVerificationVectorMult() {
return getJsonValueRapid("verificationVectorMult");
}
};
class multG2RspMessage : public ZMQMessage {
public:
multG2RspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
Json::Value getResult() {
return getJsonValueRapid("x*G2");
}
};
class isPolyExistsRspMessage : public ZMQMessage {
public:
isPolyExistsRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
bool isExists() {
return getBoolRapid("IsExist");
}
};
class getServerStatusRspMessage : public ZMQMessage {
public:
getServerStatusRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
};
class getServerVersionRspMessage : public ZMQMessage {
public:
getServerVersionRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
string getVersion() {
return getStringRapid("version");
}
};
class deleteBLSKeyRspMessage : public ZMQMessage {
public:
deleteBLSKeyRspMessage(shared_ptr<rapidjson::Document>& _d) : ZMQMessage(_d) {};
virtual Json::Value process();
bool isSuccessful() {
return getBoolRapid("deleted");
}
};
#endif //SGXWALLET_RSPMESSAGE_H
/*
Copyright (C) 2018-2019 SKALE Labs
This file is part of skale-consensus.
This file is part of sgxwallet.
skale-consensus is free software: you can redistribute it and/or modify
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
skale-consensus is distributed in the hope that it will be useful,
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with skale-consensus. If not, see <https://www.gnu.org/licenses/>.
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file ZMQClient.cpp
@author Stan Kladko
......@@ -33,10 +33,8 @@
#include "sgxwallet_common.h"
#include "common.h"
#include "BLSCrypto.h"
#include "BLSSignReqMessage.h"
#include "BLSSignRspMessage.h"
#include "ECDSASignReqMessage.h"
#include "ECDSASignRspMessage.h"
#include "ReqMessage.h"
#include "RspMessage.h"
#include "ZMQClient.h"
......@@ -57,12 +55,10 @@ shared_ptr <ZMQMessage> ZMQClient::doRequestReply(Json::Value &_req) {
string reqStr = fastWriter.write(_req);
reqStr = reqStr.substr(0, reqStr.size() - 1);
CHECK_STATE(reqStr.front() == '{');
CHECK_STATE(reqStr.at(reqStr.size() - 1) == '}');
auto resultStr = doZmqRequestReply(reqStr);
try {
......@@ -71,7 +67,6 @@ shared_ptr <ZMQMessage> ZMQClient::doRequestReply(Json::Value &_req) {
CHECK_STATE(resultStr.front() == '{')
CHECK_STATE(resultStr.back() == '}')
return ZMQMessage::parse(resultStr.c_str(), resultStr.size(), false, false);
} catch (std::exception &e) {
spdlog::error(string("Error in doRequestReply:") + e.what());
......@@ -80,11 +75,8 @@ shared_ptr <ZMQMessage> ZMQClient::doRequestReply(Json::Value &_req) {
spdlog::error("Error in doRequestReply");
throw;
}
}
string ZMQClient::doZmqRequestReply(string &_req) {
stringstream request;
......@@ -127,15 +119,12 @@ string ZMQClient::doZmqRequestReply(string &_req) {
}
}
string ZMQClient::readFileIntoString(const string &_fileName) {
ifstream t(_fileName);
string str((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
return str;
}
void ZMQClient::verifySig(EVP_PKEY* _pubkey, const string& _str, const string& _sig) {
CHECK_STATE(_pubkey);
......@@ -164,11 +153,6 @@ void ZMQClient::verifySig(EVP_PKEY* _pubkey, const string& _str, const string& _
CHECK_STATE(EVP_DigestVerifyUpdate(mdctx, msgToSign.c_str(), msgToSign.size()) == 1);
/* First call EVP_DigestSignFinal with a NULL sig parameter to obtain the length of the
* signature. Length is returned in slen */
CHECK_STATE2(EVP_DigestVerifyFinal(mdctx, binSig.data(), binLen) == 1,
ZMQ_COULD_NOT_VERIFY_SIG);
......@@ -177,7 +161,6 @@ void ZMQClient::verifySig(EVP_PKEY* _pubkey, const string& _str, const string& _
return;
}
string ZMQClient::signString(EVP_PKEY* _pkey, const string& _str) {
CHECK_STATE(_pkey);
......@@ -186,8 +169,6 @@ string ZMQClient::signString(EVP_PKEY* _pkey, const string& _str) {
static std::regex r("\\s+");
auto msgToSign = std::regex_replace(_str, r, "");
EVP_MD_CTX *mdctx = NULL;
int ret = 0;
unsigned char *signature = NULL;
......@@ -196,10 +177,8 @@ string ZMQClient::signString(EVP_PKEY* _pkey, const string& _str) {
CHECK_STATE(mdctx = EVP_MD_CTX_create());
CHECK_STATE((EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, _pkey) == 1));
CHECK_STATE(EVP_DigestSignUpdate(mdctx, msgToSign.c_str(), msgToSign.size()) == 1);
/* First call EVP_DigestSignFinal with a NULL sig parameter to obtain the length of the
......@@ -278,7 +257,6 @@ ZMQClient::ZMQClient(const string &ip, uint16_t port, bool _sign, const string &
certFileName = _certFileName;
certKeyName = _certKeyName;
url = "tcp://" + ip + ":" + to_string(port);
}
......@@ -331,6 +309,192 @@ string ZMQClient::ecdsaSignMessageHash(int base, const std::string &keyName, con
return result->getSignature();
}
bool ZMQClient::importBLSKeyShare(const std::string& keyShare, const std::string& keyName) {
Json::Value p;
p["type"] = ZMQMessage::IMPORT_BLS_REQ;
p["keyShareName"] = keyName;
p["keyShare"] = keyShare;
auto result = dynamic_pointer_cast<importBLSRspMessage>(doRequestReply(p));
CHECK_STATE(result);
return result->getStatus() == 0;
}
string ZMQClient::importECDSAKey(const std::string& keyShare, const std::string& keyName) {
Json::Value p;
p["type"] = ZMQMessage::IMPORT_ECDSA_REQ;
p["keyName"] = keyName;
p["key"] = keyShare;
auto result = dynamic_pointer_cast<importECDSARspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->getECDSAPublicKey();
}
pair<string, string> ZMQClient::generateECDSAKey() {
Json::Value p;
p["type"] = ZMQMessage::GENERATE_ECDSA_REQ;
auto result = dynamic_pointer_cast<generateECDSARspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return {result->getECDSAPublicKey(), result->getKeyName()};
}
string ZMQClient::getECDSAPublicKey(const string& keyName) {
Json::Value p;
p["type"] = ZMQMessage::GET_PUBLIC_ECDSA_REQ;
p["keyName"] = keyName;
auto result = dynamic_pointer_cast<getPublicECDSARspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->getECDSAPublicKey();
}
bool ZMQClient::generateDKGPoly(const string& polyName, int t) {
Json::Value p;
p["type"] = ZMQMessage::GENERATE_DKG_POLY_REQ;
p["polyName"] = polyName;
p["t"] = t;
auto result = dynamic_pointer_cast<generateDKGPolyRspMessage>(doRequestReply(p));
CHECK_STATE(result);
return result->getStatus() == 0;
}
Json::Value ZMQClient::getVerificationVector(const string& polyName, int t) {
Json::Value p;
p["type"] = ZMQMessage::GET_VV_REQ;
p["polyName"] = polyName;
p["t"] = t;
auto result = dynamic_pointer_cast<getVerificationVectorRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->getVerificationVector();
}
string ZMQClient::getSecretShare(const string& polyName, const Json::Value& pubKeys, int t, int n) {
Json::Value p;
p["type"] = ZMQMessage::GET_SECRET_SHARE_REQ;
p["polyName"] = polyName;
p["publicKeys"] = pubKeys;
p["t"] = t;
p["n"] = n;
auto result = dynamic_pointer_cast<getSecretShareRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->getSecretShare();
}
bool ZMQClient::dkgVerification(const string& publicShares, const string& ethKeyName,
const string& secretShare, int t, int n, int idx) {
Json::Value p;
p["type"] = ZMQMessage::DKG_VERIFY_REQ;
p["ethKeyName"] = ethKeyName;
p["publicShares"] = publicShares;
p["secretShare"] = secretShare;
p["t"] = t;
p["n"] = n;
p["index"] = idx;
auto result = dynamic_pointer_cast<dkgVerificationRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->isCorrect();
}
bool ZMQClient::createBLSPrivateKey(const string& blsKeyName, const string& ethKeyName, const string& polyName,
const string& secretShare, int t, int n) {
Json::Value p;
p["type"] = ZMQMessage::CREATE_BLS_PRIVATE_REQ;
p["ethKeyName"] = ethKeyName;
p["polyName"] = polyName;
p["blsKeyName"] = blsKeyName;
p["secretShare"] = secretShare;
p["t"] = t;
p["n"] = n;
auto result = dynamic_pointer_cast<createBLSPrivateKeyRspMessage>(doRequestReply(p));
CHECK_STATE(result);
return result->getStatus() == 0;
}
Json::Value ZMQClient::getBLSPublicKey(const string& blsKeyName) {
Json::Value p;
p["type"] = ZMQMessage::GET_BLS_PUBLIC_REQ;
p["blsKeyName"] = blsKeyName;
auto result = dynamic_pointer_cast<getBLSPublicRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->getBLSPublicKey();
}
Json::Value ZMQClient::getAllBlsPublicKeys(const Json::Value& publicShares, int n, int t) {
Json::Value p;
p["type"] = ZMQMessage::GET_ALL_BLS_PUBLIC_REQ;
p["publicShares"] = publicShares["publicShares"];
p["t"] = t;
p["n"] = n;
auto result = dynamic_pointer_cast<getAllBLSPublicKeysRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->getPublicKeys();
}
tuple<string, string, Json::Value> ZMQClient::complaintResponse(const string& polyName, int t, int n, int idx) {
Json::Value p;
p["type"] = ZMQMessage::COMPLAINT_RESPONSE_REQ;
p["polyName"] = polyName;
p["t"] = t;
p["n"] = n;
p["ind"] = idx;
auto result = dynamic_pointer_cast<complaintResponseRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return {result->getDHKey(), result->getShare(), result->getVerificationVectorMult()};
}
Json::Value ZMQClient::multG2(const string& x) {
Json::Value p;
p["type"] = ZMQMessage::MULT_G2_REQ;
p["x"] = x;
auto result = dynamic_pointer_cast<multG2RspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->getResult();
}
bool ZMQClient::isPolyExists(const string& polyName) {
Json::Value p;
p["type"] = ZMQMessage::IS_POLY_EXISTS_REQ;
p["polyName"] = polyName;
auto result = dynamic_pointer_cast<isPolyExistsRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->isExists();
}
void ZMQClient::getServerStatus() {
Json::Value p;
p["type"] = ZMQMessage::GET_SERVER_STATUS_REQ;
auto result = dynamic_pointer_cast<getServerStatusRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
}
string ZMQClient::getServerVersion() {
Json::Value p;
p["type"] = ZMQMessage::GET_SERVER_VERSION_REQ;
auto result = dynamic_pointer_cast<getServerVersionRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->getVersion();
}
bool ZMQClient::deleteBLSKey(const string& blsKeyName) {
Json::Value p;
p["type"] = ZMQMessage::DELETE_BLS_KEY_REQ;
p["blsKeyName"] = blsKeyName;
auto result = dynamic_pointer_cast<deleteBLSKeyRspMessage>(doRequestReply(p));
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->isSuccessful();
}
uint64_t ZMQClient::getProcessID() {
return syscall(__NR_gettid);
......
/*
Copyright (C) 2018-2019 SKALE Labs
This file is part of skale-consensus.
This file is part of sgxwallet.
skale-consensus is free software: you can redistribute it and/or modify
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
skale-consensus is distributed in the hope that it will be useful,
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with skale-consensus. If not, see <https://www.gnu.org/licenses/>.
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file ZMQClient.h
@author Stan Kladko
@date 2021
*/
#ifndef SGXWALLET_ZMQCLIENT_H
#define SGXWALLET_ZMQCLIENT_H
......@@ -40,28 +37,21 @@
#include <jsonrpccpp/client.h>
#include "ZMQMessage.h"
#define REQUEST_TIMEOUT 10000 // msecs, (> 1000!)
class ZMQClient {
private:
EVP_PKEY* pkey = 0;
EVP_PKEY* pubkey = 0;
X509* x509Cert = 0;
bool sign = true;
string certFileName = "";
string certKeyName = "";
string certificate = "";
string key = "";
recursive_mutex mutex;
zmq::context_t ctx;
......@@ -82,11 +72,10 @@ private:
public:
ZMQClient(const string &ip, uint16_t port, bool _sign, const string& _certPathName,
const string& _certKeyName);
void reconnect() ;
void reconnect();
static pair<EVP_PKEY*, X509*> readPublicKeyFromCertStr(const string& _cert);
......@@ -98,8 +87,42 @@ public:
string ecdsaSignMessageHash(int base, const std::string &keyName, const std::string &messageHash);
};
bool importBLSKeyShare(const std::string& keyShare, const std::string& keyName);
string importECDSAKey(const std::string& keyShare, const std::string& keyName);
pair<string, string> generateECDSAKey();
string getECDSAPublicKey(const string& keyName);
bool generateDKGPoly(const string& polyName, int t);
Json::Value getVerificationVector(const string& polyName, int t);
string getSecretShare(const string& polyName, const Json::Value& pubKeys, int t, int n);
bool dkgVerification(const string& publicShares, const string& ethKeyName,
const string& secretShare, int t, int n, int idx);
bool createBLSPrivateKey(const string& blsKeyName, const string& ethKeyName, const string& polyName,
const string& secretShare, int t, int n);
Json::Value getBLSPublicKey(const string& blsKeyName);
Json::Value getAllBlsPublicKeys(const Json::Value& publicShares, int n, int t);
tuple<string, string, Json::Value> complaintResponse(const string& polyName, int t, int n, int idx);
Json::Value multG2(const string& x);
bool isPolyExists(const string& polyName);
void getServerStatus();
string getServerVersion();
bool deleteBLSKey(const string& blsKeyName);
};
#endif //SGXWALLET_ZMQCLIENT_H
/*
Copyright (C) 2020 SKALE Labs
This file is part of skale-consensus.
This file is part of sgxwallet.
skale-consensus is free software: you can redistribute it and/or modify
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
skale-consensus is distributed in the hope that it will be useful,
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with skale-consensus. If not, see <https://www.gnu.org/licenses/>.
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file ZMQMessage.cpp
@author Stan Kladko
......@@ -29,21 +29,43 @@
#include "ZMQClient.h"
#include "SGXWalletServer.hpp"
#include "BLSSignReqMessage.h"
#include "BLSSignRspMessage.h"
#include "ECDSASignReqMessage.h"
#include "ECDSASignRspMessage.h"
#include "ReqMessage.h"
#include "RspMessage.h"
#include "ZMQMessage.h"
uint64_t ZMQMessage::getUint64Rapid(const char *_name) {
uint64_t ZMQMessage::getInt64Rapid(const char *_name) {
CHECK_STATE(_name);
CHECK_STATE(d->HasMember(_name));
const rapidjson::Value &a = (*d)[_name];
CHECK_STATE(a.IsUint64());
return a.GetUint64();
CHECK_STATE(a.IsInt64());
return a.GetInt64();
};
Json::Value ZMQMessage::getJsonValueRapid(const char *_name) {
CHECK_STATE(_name);
CHECK_STATE(d->HasMember(_name));
const rapidjson::Value &a = (*d)[_name];
rapidjson::StringBuffer buffer;
rapidjson::Writer< rapidjson::StringBuffer > writer(buffer);
a.Accept(writer);
std::string strRequest = buffer.GetString();
Json::Reader reader;
Json::Value root;
reader.parse(strRequest, root, false);
return root;
}
bool ZMQMessage::getBoolRapid(const char *_name) {
CHECK_STATE(_name);
CHECK_STATE(d->HasMember(_name));
const rapidjson::Value &a = (*d)[_name];
CHECK_STATE(a.IsBool());
return a.GetBool();
}
string ZMQMessage::getStringRapid(const char *_name) {
CHECK_STATE(_name);
CHECK_STATE(d->HasMember(_name));
......@@ -51,9 +73,6 @@ string ZMQMessage::getStringRapid(const char *_name) {
return (*d)[_name].GetString();
};
shared_ptr <ZMQMessage> ZMQMessage::parse(const char *_msg,
size_t _size, bool _isRequest,
bool _verifySig) {
......@@ -125,12 +144,11 @@ shared_ptr <ZMQMessage> ZMQMessage::parse(const char *_msg,
auto msgToVerify = buffer.GetString();
ZMQClient::verifySig(publicKey,msgToVerify, *msgSig );
ZMQClient::verifySig(publicKey, msgToVerify, *msgSig );
}
}
shared_ptr <ZMQMessage> result;
if (_isRequest) {
......@@ -141,30 +159,175 @@ shared_ptr <ZMQMessage> ZMQMessage::parse(const char *_msg,
}
shared_ptr <ZMQMessage> ZMQMessage::buildRequest(string &_type, shared_ptr <rapidjson::Document> _d) {
if (_type == ZMQMessage::BLS_SIGN_REQ) {
return make_shared<BLSSignReqMessage>(_d);
} else if (_type == ZMQMessage::ECDSA_SIGN_REQ) {
return
make_shared<ECDSASignReqMessage>(_d);
} else {
BOOST_THROW_EXCEPTION(SGXException(-301, "Incorrect zmq message type: " +
string(_type)));
Requests r;
try {
int t = requests.at( _type );
r = static_cast<Requests>(t);
} catch ( std::out_of_range& ) {
BOOST_THROW_EXCEPTION(SGXException(-301, "Incorrect zmq message type: " + string(_type)));
}
shared_ptr<ZMQMessage> ret = nullptr;
switch (r) {
case ENUM_BLS_SIGN_REQ:
ret = make_shared<BLSSignReqMessage>(_d);
break;
case ENUM_ECDSA_SIGN_REQ:
ret = make_shared<ECDSASignReqMessage>(_d);
break;
case ENUM_IMPORT_BLS_REQ:
ret = make_shared<importBLSReqMessage>(_d);
break;
case ENUM_IMPORT_ECDSA_REQ:
ret = make_shared<importECDSAReqMessage>(_d);
break;
case ENUM_GENERATE_ECDSA_REQ:
ret = make_shared<generateECDSAReqMessage>(_d);
break;
case ENUM_GET_PUBLIC_ECDSA_REQ:
ret = make_shared<getPublicECDSAReqMessage>(_d);
break;
case ENUM_GENERATE_DKG_POLY_REQ:
ret = make_shared<generateDKGPolyReqMessage>(_d);
break;
case ENUM_GET_VV_REQ:
ret = make_shared<getVerificationVectorReqMessage>(_d);
break;
case ENUM_GET_SECRET_SHARE_REQ:
ret = make_shared<getSecretShareReqMessage>(_d);
break;
case ENUM_DKG_VERIFY_REQ:
ret = make_shared<dkgVerificationReqMessage>(_d);
break;
case ENUM_CREATE_BLS_PRIVATE_REQ:
ret = make_shared<createBLSPrivateKeyReqMessage>(_d);
break;
case ENUM_GET_BLS_PUBLIC_REQ:
ret = make_shared<getBLSPublicReqMessage>(_d);
break;
case ENUM_GET_ALL_BLS_PUBLIC_REQ:
ret = make_shared<getAllBLSPublicKeysReqMessage>(_d);
break;
case ENUM_COMPLAINT_RESPONSE_REQ:
ret = make_shared<complaintResponseReqMessage>(_d);
break;
case ENUM_MULT_G2_REQ:
ret = make_shared<multG2ReqMessage>(_d);
break;
case ENUM_IS_POLY_EXISTS_REQ:
ret = make_shared<isPolyExistsReqMessage>(_d);
break;
case ENUM_GET_SERVER_STATUS_REQ:
ret = make_shared<getServerStatusReqMessage>(_d);
break;
case ENUM_GET_SERVER_VERSION_REQ:
ret = make_shared<getServerVersionReqMessage>(_d);
break;
case ENUM_DELETE_BLS_KEY_REQ:
ret = make_shared<deleteBLSKeyReqMessage>(_d);
break;
default:
break;
}
return ret;
}
shared_ptr <ZMQMessage> ZMQMessage::buildResponse(string &_type, shared_ptr <rapidjson::Document> _d) {
if (_type == ZMQMessage::BLS_SIGN_RSP) {
return
make_shared<BLSSignRspMessage>(_d);
} else if (_type == ZMQMessage::ECDSA_SIGN_RSP) {
return
make_shared<ECDSASignRspMessage>(_d);
} else {
Responses r;
try {
int t = responses.at( _type );
r = static_cast<Responses>(t);
} catch ( std::out_of_range& ) {
BOOST_THROW_EXCEPTION(InvalidStateException("Incorrect zmq message request type: " + string(_type),
__CLASS_NAME__)
);
}
shared_ptr<ZMQMessage> ret = nullptr;
switch (r) {
case ENUM_BLS_SIGN_RSP:
ret = make_shared<BLSSignRspMessage>(_d);
break;
case ENUM_ECDSA_SIGN_RSP:
ret = make_shared<ECDSASignRspMessage>(_d);
break;
case ENUM_IMPORT_BLS_RSP:
ret = make_shared<importBLSRspMessage>(_d);
break;
case ENUM_IMPORT_ECDSA_RSP:
ret = make_shared<importECDSARspMessage>(_d);
break;
case ENUM_GENERATE_ECDSA_RSP:
ret = make_shared<generateECDSARspMessage>(_d);
break;
case ENUM_GET_PUBLIC_ECDSA_RSP:
ret = make_shared<getPublicECDSARspMessage>(_d);
break;
case ENUM_GENERATE_DKG_POLY_RSP:
ret = make_shared<generateDKGPolyRspMessage>(_d);
break;
case ENUM_GET_VV_RSP:
ret = make_shared<getVerificationVectorRspMessage>(_d);
break;
case ENUM_GET_SECRET_SHARE_RSP:
ret = make_shared<getSecretShareRspMessage>(_d);
break;
case ENUM_DKG_VERIFY_RSP:
ret = make_shared<dkgVerificationRspMessage>(_d);
break;
case ENUM_CREATE_BLS_PRIVATE_RSP:
ret = make_shared<createBLSPrivateKeyRspMessage>(_d);
break;
case ENUM_GET_BLS_PUBLIC_RSP:
ret = make_shared<getBLSPublicRspMessage>(_d);
break;
case ENUM_GET_ALL_BLS_PUBLIC_RSP:
ret = make_shared<getAllBLSPublicKeysRspMessage>(_d);
break;
case ENUM_COMPLAINT_RESPONSE_RSP:
ret = make_shared<complaintResponseRspMessage>(_d);
break;
case ENUM_MULT_G2_RSP:
ret = make_shared<multG2RspMessage>(_d);
break;
case ENUM_IS_POLY_EXISTS_RSP:
ret = make_shared<isPolyExistsRspMessage>(_d);
break;
case ENUM_GET_SERVER_STATUS_RSP:
ret = make_shared<getServerStatusRspMessage>(_d);
break;
case ENUM_GET_SERVER_VERSION_RSP:
ret = make_shared<getServerVersionRspMessage>(_d);
break;
case ENUM_DELETE_BLS_KEY_RSP:
ret = make_shared<deleteBLSKeyRspMessage>(_d);
break;
default:
break;
}
return ret;
}
cache::lru_cache<string, pair < EVP_PKEY * , X509 *>>
ZMQMessage::verifiedCerts(256);
\ No newline at end of file
cache::lru_cache<string, pair < EVP_PKEY * , X509 *>> ZMQMessage::verifiedCerts(256);
const std::map<string, int> ZMQMessage::requests{
{BLS_SIGN_REQ, 0}, {ECDSA_SIGN_REQ, 1}, {IMPORT_BLS_REQ, 2}, {IMPORT_ECDSA_REQ, 3},
{GENERATE_ECDSA_REQ, 4}, {GET_PUBLIC_ECDSA_REQ, 5}, {GENERATE_DKG_POLY_REQ, 6},
{GET_VV_REQ, 7}, {GET_SECRET_SHARE_REQ, 8}, {DKG_VERIFY_REQ, 9},
{CREATE_BLS_PRIVATE_REQ, 10}, {GET_BLS_PUBLIC_REQ, 11}, {GET_ALL_BLS_PUBLIC_REQ, 12},
{COMPLAINT_RESPONSE_REQ, 13}, {MULT_G2_REQ, 14}, {IS_POLY_EXISTS_REQ, 15},
{GET_SERVER_STATUS_REQ, 16}, {GET_SERVER_VERSION_REQ, 17}, {DELETE_BLS_KEY_REQ, 18}
};
const std::map<string, int> ZMQMessage::responses {
{BLS_SIGN_RSP, 0}, {ECDSA_SIGN_RSP, 1}, {IMPORT_BLS_RSP, 2}, {IMPORT_ECDSA_RSP, 3},
{GENERATE_ECDSA_RSP, 4}, {GET_PUBLIC_ECDSA_RSP, 5}, {GENERATE_DKG_POLY_RSP, 6},
{GET_VV_RSP, 7}, {GET_SECRET_SHARE_RSP, 8}, {DKG_VERIFY_RSP, 9},
{CREATE_BLS_PRIVATE_RSP, 10}, {GET_BLS_PUBLIC_RSP, 11}, {GET_ALL_BLS_PUBLIC_RSP, 12},
{COMPLAINT_RESPONSE_RSP, 13}, {MULT_G2_RSP, 14}, {IS_POLY_EXISTS_RSP, 15},
{GET_SERVER_STATUS_RSP, 16}, {GET_SERVER_VERSION_RSP, 17}, {DELETE_BLS_KEY_RSP, 18}
};
......@@ -49,29 +49,81 @@ class ZMQMessage {
shared_ptr<rapidjson::Document> d;
static cache::lru_cache<string, pair<EVP_PKEY*, X509*>> verifiedCerts;
protected:
public:
static constexpr const char *BLS_SIGN_REQ = "BLSSignReq";
static constexpr const char *BLS_SIGN_RSP = "BLSSignRsp";
static constexpr const char *ECDSA_SIGN_REQ = "ECDSASignReq";
static constexpr const char *ECDSA_SIGN_RSP = "ECDSASignRsp";
explicit ZMQMessage(shared_ptr<rapidjson::Document> &_d) : d(_d) {
};
static constexpr const char *IMPORT_BLS_REQ = "importBLSReq";
static constexpr const char *IMPORT_BLS_RSP = "importBLSRps";
static constexpr const char *IMPORT_ECDSA_REQ = "importECDSAReq";
static constexpr const char *IMPORT_ECDSA_RSP = "importECDSARsp";
static constexpr const char *GENERATE_ECDSA_REQ = "generateECDSAReq";
static constexpr const char *GENERATE_ECDSA_RSP = "generateECDSARsp";
static constexpr const char *GET_PUBLIC_ECDSA_REQ = "getPublicECDSAReq";
static constexpr const char *GET_PUBLIC_ECDSA_RSP = "getPublicECDSARsp";
static constexpr const char *GENERATE_DKG_POLY_REQ = "generateDKGPolyReq";
static constexpr const char *GENERATE_DKG_POLY_RSP = "generateDKGPolyRsp";
static constexpr const char *GET_VV_REQ = "getVerificationVectorReq";
static constexpr const char *GET_VV_RSP = "getVerificationVectorRsp";
static constexpr const char *GET_SECRET_SHARE_REQ = "getSecretShareReq";
static constexpr const char *GET_SECRET_SHARE_RSP = "getSecretShareRsp";
static constexpr const char *DKG_VERIFY_REQ = "dkgVerificationReq";
static constexpr const char *DKG_VERIFY_RSP = "dkgVerificationRsp";
static constexpr const char *CREATE_BLS_PRIVATE_REQ = "createBLSPrivateReq";
static constexpr const char *CREATE_BLS_PRIVATE_RSP = "createBLSPrivateRsp";
static constexpr const char *GET_BLS_PUBLIC_REQ = "getBLSPublicReq";
static constexpr const char *GET_BLS_PUBLIC_RSP = "getBLSPublicRsp";
static constexpr const char *GET_ALL_BLS_PUBLIC_REQ = "getAllBLSPublicReq";
static constexpr const char *GET_ALL_BLS_PUBLIC_RSP = "getAllBLSPublicRsp";
static constexpr const char *COMPLAINT_RESPONSE_REQ = "complaintResponseReq";
static constexpr const char *COMPLAINT_RESPONSE_RSP = "complaintResponseRsp";
static constexpr const char *MULT_G2_REQ = "multG2Req";
static constexpr const char *MULT_G2_RSP = "multG2Rsp";
static constexpr const char *IS_POLY_EXISTS_REQ = "isPolyExistsReq";
static constexpr const char *IS_POLY_EXISTS_RSP = "isPolyExistsRsp";
static constexpr const char *GET_SERVER_STATUS_REQ = "getServerStatusReq";
static constexpr const char *GET_SERVER_STATUS_RSP = "getServerStatusRsp";
static constexpr const char *GET_SERVER_VERSION_REQ = "getServerVersionReq";
static constexpr const char *GET_SERVER_VERSION_RSP = "getServerVersionRsp";
static constexpr const char *DELETE_BLS_KEY_REQ = "deleteBLSKeyReq";
static constexpr const char *DELETE_BLS_KEY_RSP = "deleteBLSKeyRsp";
static const std::map<string, int> requests;
static const std::map<string, int> responses;
enum Requests { ENUM_BLS_SIGN_REQ, ENUM_ECDSA_SIGN_REQ, ENUM_IMPORT_BLS_REQ, ENUM_IMPORT_ECDSA_REQ, ENUM_GENERATE_ECDSA_REQ, ENUM_GET_PUBLIC_ECDSA_REQ,
ENUM_GENERATE_DKG_POLY_REQ, ENUM_GET_VV_REQ, ENUM_GET_SECRET_SHARE_REQ, ENUM_DKG_VERIFY_REQ, ENUM_CREATE_BLS_PRIVATE_REQ,
ENUM_GET_BLS_PUBLIC_REQ, ENUM_GET_ALL_BLS_PUBLIC_REQ, ENUM_COMPLAINT_RESPONSE_REQ, ENUM_MULT_G2_REQ, ENUM_IS_POLY_EXISTS_REQ,
ENUM_GET_SERVER_STATUS_REQ, ENUM_GET_SERVER_VERSION_REQ, ENUM_DELETE_BLS_KEY_REQ };
enum Responses { ENUM_BLS_SIGN_RSP, ENUM_ECDSA_SIGN_RSP, ENUM_IMPORT_BLS_RSP, ENUM_IMPORT_ECDSA_RSP, ENUM_GENERATE_ECDSA_RSP, ENUM_GET_PUBLIC_ECDSA_RSP,
ENUM_GENERATE_DKG_POLY_RSP, ENUM_GET_VV_RSP, ENUM_GET_SECRET_SHARE_RSP, ENUM_DKG_VERIFY_RSP, ENUM_CREATE_BLS_PRIVATE_RSP,
ENUM_GET_BLS_PUBLIC_RSP, ENUM_GET_ALL_BLS_PUBLIC_RSP, ENUM_COMPLAINT_RESPONSE_RSP, ENUM_MULT_G2_RSP, ENUM_IS_POLY_EXISTS_RSP,
ENUM_GET_SERVER_STATUS_RSP, ENUM_GET_SERVER_VERSION_RSP, ENUM_DELETE_BLS_KEY_RSP };
explicit ZMQMessage(shared_ptr<rapidjson::Document> &_d) : d(_d) {};
string getStringRapid(const char *_name);
uint64_t getUint64Rapid(const char *_name);
uint64_t getInt64Rapid(const char *_name);
Json::Value getJsonValueRapid(const char *_name);
bool getBoolRapid(const char *_name);
uint64_t getStatus() {
return getUint64Rapid("status");
return getInt64Rapid("status");
}
std::string rapidToString() {
rapidjson::StringBuffer buffer;
rapidjson::Writer< rapidjson::StringBuffer > writer( buffer );
d->Accept( writer );
std::string strRequest = buffer.GetString();
return strRequest;
}
static shared_ptr <ZMQMessage> parse(const char* _msg, size_t _size, bool _isRequest,
......
......@@ -42,7 +42,6 @@ ZMQServer::ZMQServer(bool _checkSignature, const string &_caCertFile)
: checkSignature(_checkSignature),
caCertFile(_caCertFile), ctx(make_shared<zmq::context_t>(1)) {
socket = make_shared<zmq::socket_t>(*ctx, ZMQ_ROUTER);
if (_checkSignature) {
......@@ -56,10 +55,8 @@ ZMQServer::ZMQServer(bool _checkSignature, const string &_caCertFile)
int linger = 0;
zmq_setsockopt(*socket, ZMQ_LINGER, &linger, sizeof(linger));
}
void ZMQServer::run() {
auto port = BASE_PORT + 5;
......@@ -85,10 +82,8 @@ void ZMQServer::run() {
}
spdlog::info("Exited zmq server loop");
}
std::atomic<bool> ZMQServer::isExitRequested(false);
void ZMQServer::exitZMQServer() {
......@@ -99,7 +94,6 @@ void ZMQServer::exitZMQServer() {
spdlog::info("Exited zmq server.");
}
void ZMQServer::initZMQServer(bool _checkSignature) {
static bool initedServer = false;
CHECK_STATE(!initedServer)
......@@ -123,15 +117,11 @@ void ZMQServer::initZMQServer(bool _checkSignature) {
serverThread->detach();
spdlog::info("Inited zmq server ...");
}
shared_ptr <std::thread> ZMQServer::serverThread = nullptr;
ZMQServer::~ZMQServer() {
}
ZMQServer::~ZMQServer() {}
void ZMQServer::doOneServerLoop() {
......@@ -142,10 +132,8 @@ void ZMQServer::doOneServerLoop() {
result["errorMessage"] = "";
zmq::message_t identity;
zmq::message_t identit2;
zmq::message_t copied_id;
string stringToParse = "";
try {
......@@ -156,7 +144,6 @@ void ZMQServer::doOneServerLoop() {
int pollResult = 0;
do {
pollResult = zmq_poll(items, 1, 1000);
if (isExitRequested) {
......@@ -164,21 +151,18 @@ void ZMQServer::doOneServerLoop() {
}
} while (pollResult == 0);
if (!socket->recv(&identity)) {
// something terrible happened
spdlog::error("Fatal error: socket->recv(&identity) returned false");
exit(-11);
}
if (!identity.more()) {
// something terrible happened
spdlog::error("Fatal error: zmq_msg_more(identity) returned false");
exit(-12);
}
copied_id.copy(&identity);
zmq::message_t reqMsg;
......@@ -189,7 +173,6 @@ void ZMQServer::doOneServerLoop() {
exit(-13);
}
stringToParse = string((char *) reqMsg.data(), reqMsg.size());
CHECK_STATE(stringToParse.front() == '{')
......@@ -201,8 +184,7 @@ void ZMQServer::doOneServerLoop() {
CHECK_STATE2(parsedMsg, ZMQ_COULD_NOT_PARSE);
result = parsedMsg->process();
}
catch (std::exception &e) {
} catch (std::exception &e) {
if (isExitRequested) {
return;
}
......@@ -245,9 +227,7 @@ void ZMQServer::doOneServerLoop() {
exit(-16);
}
} catch (
std::exception &e
) {
} catch ( std::exception &e ) {
if (isExitRequested) {
return;
}
......
......@@ -61,8 +61,6 @@ public:
static void initZMQServer(bool _checkSignature);
static void exitZMQServer();
private:
shared_ptr<zmq::context_t> ctx;
shared_ptr<zmq::socket_t> socket;
......@@ -74,5 +72,4 @@ private:
};
#endif //SGXWALLET_ZMQServer_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