Unverified Commit f8a8cd25 authored by svetaro's avatar svetaro

SKALE-1512-add-DKG-to-SGX add recursive mutex to server functions and add dkg verification

parent 41429ab9
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
#include "sgxwallet.h" #include "sgxwallet.h"
#include <iostream> #include <iostream>
#include <memory>
#include "SGXWalletServer.hpp"
std::vector<std::string> SplitString(const char* koefs, const char symbol){ std::vector<std::string> SplitString(const char* koefs, const char symbol){
std::string str(koefs); std::string str(koefs);
std::string delim; std::string delim;
...@@ -100,7 +103,7 @@ std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyH ...@@ -100,7 +103,7 @@ std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyH
return pub_shares_vect; return pub_shares_vect;
} }
std::string get_secret_shares( const char* encryptedPolyHex, const std::string& publicKeys, int n, int t){ std::string get_secret_shares(const std::string& polyName, const char* encryptedPolyHex, const std::string& publicKeys, int n, int t){
char* errMsg1 = (char*) calloc(1024,1); char* errMsg1 = (char*) calloc(1024,1);
int err_status = 0; int err_status = 0;
...@@ -112,6 +115,8 @@ std::string get_secret_shares( const char* encryptedPolyHex, const std::string& ...@@ -112,6 +115,8 @@ std::string get_secret_shares( const char* encryptedPolyHex, const std::string&
status = set_encrypted_dkg_poly(eid, &err_status, errMsg1, encr_dkg_poly); status = set_encrypted_dkg_poly(eid, &err_status, errMsg1, encr_dkg_poly);
std::string result; std::string result;
char *hexEncrKey = (char *) calloc(2 * BUF_LEN, 1);
for ( int i = 0; i < n; i++){ for ( int i = 0; i < n; i++){
uint8_t encrypted_skey[BUF_LEN]; uint8_t encrypted_skey[BUF_LEN];
uint32_t dec_len; uint32_t dec_len;
...@@ -125,6 +130,14 @@ std::string get_secret_shares( const char* encryptedPolyHex, const std::string& ...@@ -125,6 +130,14 @@ std::string get_secret_shares( const char* encryptedPolyHex, const std::string&
result += cur_share; result += cur_share;
uint32_t enc_len = BUF_LEN;
carray2Hex(encrypted_skey, enc_len, hexEncrKey);
//std::cerr << "hexEncrKey: " << hexEncrKey << std::endl;
std::string name = "DKG_DH_KEY_" + polyName + "_" + std::to_string(i) + ":";
//writeDataToDB(name, hexEncrKey);
//std::cerr << errMsg1 << std::endl << std::endl; //std::cerr << errMsg1 << std::endl << std::endl;
//std::cerr << "iteration " << i <<" result length is " << result.length() << std::endl ; //std::cerr << "iteration " << i <<" result length is " << result.length() << std::endl ;
//std::cerr << "iteration " << i <<" share length is " << strlen(cur_share) << std::endl; //std::cerr << "iteration " << i <<" share length is " << strlen(cur_share) << std::endl;
...@@ -132,5 +145,35 @@ std::string get_secret_shares( const char* encryptedPolyHex, const std::string& ...@@ -132,5 +145,35 @@ std::string get_secret_shares( const char* encryptedPolyHex, const std::string&
} }
//result += '\0'; //result += '\0';
free(encr_dkg_poly);
free(errMsg1);
free(hexEncrKey);
return result; return result;
}
bool VerifyShares(const char* encryptedPolyHex, const char* encr_sshare, const char * encryptedKeyHex, int t, int n, int ind ){
char* errMsg1 = (char*) calloc(1024,1);
int err_status = 0;
uint64_t poly_len = 0;
uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN, 1);
hex2carray2(encryptedPolyHex, &poly_len, encr_dkg_poly, 6100);
uint64_t dec_key_len ;
uint8_t encr_key[BUF_LEN];
hex2carray(encryptedKeyHex, &dec_key_len, encr_key);
//std::cerr << "encryptedKeyHex " << encryptedKeyHex << std::endl;
//std::cerr << "dec_key_len " << dec_key_len << std::endl;
int result ;
dkg_verification(eid, &err_status, errMsg1, encr_dkg_poly, encr_sshare, encr_key, dec_key_len, t, ind, &result);
std::cerr << "errMsg1: " << errMsg1 << std::endl;
free(errMsg1);
free(encr_dkg_poly);
std::cerr << "result is " << result << std::endl;
return result;
} }
\ No newline at end of file
...@@ -14,6 +14,8 @@ std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyH ...@@ -14,6 +14,8 @@ std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyH
std::vector<std::string> SplitString(const char* koefs, const char symbol); std::vector<std::string> SplitString(const char* koefs, const char symbol);
std::string get_secret_shares( const char* encryptedPolyHex, const std::string& publicKeys, int n, int t); std::string get_secret_shares(const std::string& polyName, const char* encryptedPolyHex, const std::string& publicKeys, int n, int t);
bool VerifyShares(const char* encryptedPolyHex, const char* encr_sshare, const char * encryptedKeyHex, int t, int n, int ind);
#endif //SGXD_DKGCRYPTO_H #endif //SGXD_DKGCRYPTO_H
...@@ -41,7 +41,7 @@ int init_server() { ...@@ -41,7 +41,7 @@ int init_server() {
hs = new HttpServer(1025); hs = new HttpServer(1025);
s = new SGXWalletServer(*hs, s = new SGXWalletServer(*hs,
JSONRPC_SERVER_V2); // hybrid server (json-rpc 1.0 & 2.0) JSONRPC_SERVER_V2); // hybrid server (json-rpc 1.0 & 2.0)
if (!s->StartListening()) { if (!s->StartListening()) {
cerr << "Server could not start listening" << endl; cerr << "Server could not start listening" << endl;
exit(-1); exit(-1);
...@@ -302,7 +302,7 @@ Json::Value getSecretShareImpl(const std::string& polyName, const std::string& p ...@@ -302,7 +302,7 @@ Json::Value getSecretShareImpl(const std::string& polyName, const std::string& p
try { try {
std::shared_ptr<std::string> encr_poly_ptr = readFromDb(polyName, "DKGPoly:"); std::shared_ptr<std::string> encr_poly_ptr = readFromDb(polyName, "DKGPoly:");
std::string s = get_secret_shares( encr_poly_ptr->c_str(), publicKeys, n, t); std::string s = get_secret_shares(polyName, encr_poly_ptr->c_str(), publicKeys, n, t);
//std::cerr << "result is " << s << std::endl; //std::cerr << "result is " << s << std::endl;
result["SecretShare"] = s; result["SecretShare"] = s;
...@@ -316,28 +316,68 @@ Json::Value getSecretShareImpl(const std::string& polyName, const std::string& p ...@@ -316,28 +316,68 @@ Json::Value getSecretShareImpl(const std::string& polyName, const std::string& p
return result; return result;
} }
Json::Value DKGVerificationImpl(const std::string& polyName, const std::string& EthKeyName,
const std::string& SecretShare, int t, int n, int ind){
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
result["result"] = true;
try {
std::shared_ptr<std::string> encryptedPolyHex_ptr = readFromDb(polyName, "DKGPoly:");
std::string keyName = polyName + "_" + std::to_string(ind);
//std::shared_ptr<std::string> encryptedKeyHex_ptr = readFromDb(keyName, "DKG_DH_KEY_");
std::shared_ptr<std::string> encryptedKeyHex_ptr = readECDSAKey("test_key1");
if ( !VerifyShares(encryptedPolyHex_ptr->c_str(), SecretShare.c_str(), encryptedKeyHex_ptr->c_str(), t, n, ind )){
result["result"] = false;
}
} catch (RPCException &_e) {
std::cerr << " err str " << _e.errString << std::endl;
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
result["result"] = false;
}
return result;
}
Json::Value SGXWalletServer::generateDKGPoly(const std::string& polyName, int t){ Json::Value SGXWalletServer::generateDKGPoly(const std::string& polyName, int t){
return generateDKGPolyImpl(polyName, t); lock_guard<recursive_mutex> lock(m);
return generateDKGPolyImpl(polyName, t);
} }
Json::Value SGXWalletServer::getVerificationVector(const std::string& polyName, int n, int t){ Json::Value SGXWalletServer::getVerificationVector(const std::string& polyName, int n, int t){
lock_guard<recursive_mutex> lock(m);
return getVerificationVectorImpl(polyName, n, t); return getVerificationVectorImpl(polyName, n, t);
} }
Json::Value SGXWalletServer::getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t){ Json::Value SGXWalletServer::getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t){
lock_guard<recursive_mutex> lock(m);
return getSecretShareImpl(polyName, publicKeys, n, t); return getSecretShareImpl(polyName, publicKeys, n, t);
} }
Json::Value SGXWalletServer::DKGVerification( const std::string& polyName, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index){
lock_guard<recursive_mutex> lock(m);
return DKGVerificationImpl(polyName, EthKeyName, SecretShare, t, n, index);
}
Json::Value SGXWalletServer::generateECDSAKey(const std::string &_keyName) { Json::Value SGXWalletServer::generateECDSAKey(const std::string &_keyName) {
lock_guard<recursive_mutex> lock(m);
return generateECDSAKeyImpl(_keyName); return generateECDSAKeyImpl(_keyName);
} }
Json::Value SGXWalletServer::getPublicECDSAKey(const std::string &_keyName) { Json::Value SGXWalletServer::getPublicECDSAKey(const std::string &_keyName) {
lock_guard<recursive_mutex> lock(m);
return getPublicECDSAKeyImpl(_keyName); return getPublicECDSAKeyImpl(_keyName);
} }
Json::Value SGXWalletServer::ecdsaSignMessageHash(int base, const std::string &_keyName, const std::string &messageHash ) { Json::Value SGXWalletServer::ecdsaSignMessageHash(int base, const std::string &_keyName, const std::string &messageHash ) {
lock_guard<recursive_mutex> lock(m);
std::cerr << "entered ecdsaSignMessageHash" << std::endl; std::cerr << "entered ecdsaSignMessageHash" << std::endl;
std::cerr << "MessageHash first " << messageHash << std::endl; std::cerr << "MessageHash first " << messageHash << std::endl;
return ecdsaSignMessageHashImpl(base,_keyName, messageHash); return ecdsaSignMessageHashImpl(base,_keyName, messageHash);
...@@ -347,16 +387,19 @@ Json::Value SGXWalletServer::ecdsaSignMessageHash(int base, const std::string &_ ...@@ -347,16 +387,19 @@ Json::Value SGXWalletServer::ecdsaSignMessageHash(int base, const std::string &_
Json::Value Json::Value
SGXWalletServer::importBLSKeyShare(int index, const std::string &_keyShare, const std::string &_keyShareName, int n, SGXWalletServer::importBLSKeyShare(int index, const std::string &_keyShare, const std::string &_keyShareName, int n,
int t) { int t) {
lock_guard<recursive_mutex> lock(m);
return importBLSKeyShareImpl(index, _keyShare, _keyShareName, n, t); return importBLSKeyShareImpl(index, _keyShare, _keyShareName, n, t);
} }
Json::Value SGXWalletServer::blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash,int n, Json::Value SGXWalletServer::blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash,int n,
int t, int signerIndex) { int t, int signerIndex) {
lock_guard<recursive_mutex> lock(m);
return blsSignMessageHashImpl(keyShareName, messageHash, n,t, signerIndex); return blsSignMessageHashImpl(keyShareName, messageHash, n,t, signerIndex);
} }
Json::Value SGXWalletServer::importECDSAKey(const std::string &key, const std::string &keyName) { Json::Value SGXWalletServer::importECDSAKey(const std::string &key, const std::string &keyName) {
return importECDSAKeyImpl(key, keyName); lock_guard<recursive_mutex> lock(m);
return importECDSAKeyImpl(key, keyName);
} }
...@@ -443,5 +486,22 @@ void writeDKGPoly(const string &_polyName, const string &value) { ...@@ -443,5 +486,22 @@ void writeDKGPoly(const string &_polyName, const string &value) {
throw new RPCException(KEY_SHARE_ALREADY_EXISTS, "Poly with this name already exists"); throw new RPCException(KEY_SHARE_ALREADY_EXISTS, "Poly with this name already exists");
} }
levelDb->writeString(key, value);
}
void writeDataToDB(const string & Name, const string &value) {
Json::Value val;
Json::FastWriter writer;
val["value"] = value;
std::string json = writer.write(val);
auto key = Name;
if (levelDb->readString(Name) != nullptr) {
std::cerr << "already exists" << std::endl;
throw new RPCException(KEY_SHARE_ALREADY_EXISTS, "Data with this name already exists");
}
levelDb->writeString(key, value); levelDb->writeString(key, value);
} }
\ No newline at end of file
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "abstractstubserver.h" #include "abstractstubserver.h"
#include <mutex>
using namespace jsonrpc; using namespace jsonrpc;
using namespace std; using namespace std;
...@@ -13,7 +15,7 @@ class SGXWalletServer : public AbstractStubServer { ...@@ -13,7 +15,7 @@ class SGXWalletServer : public AbstractStubServer {
SGXWalletServer* server = nullptr; SGXWalletServer* server = nullptr;
std::recursive_mutex m;
public: public:
SGXWalletServer(AbstractServerConnector &connector, serverVersion_t type); SGXWalletServer(AbstractServerConnector &connector, serverVersion_t type);
...@@ -29,11 +31,13 @@ public: ...@@ -29,11 +31,13 @@ public:
virtual Json::Value generateDKGPoly(const std::string& polyName, int t); virtual Json::Value generateDKGPoly(const std::string& polyName, int t);
virtual Json::Value getVerificationVector(const std::string& polyName, int n, int t); virtual Json::Value getVerificationVector(const std::string& polyName, int n, int t);
virtual Json::Value getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t); virtual Json::Value getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t);
virtual Json::Value DKGVerification(const std::string& polyName, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index);
}; };
shared_ptr<string> readFromDb(const string & name, const string & prefix); shared_ptr<string> readFromDb(const string & name, const string & prefix);
void writeDataToDB(const string & Name, const string &value);
void writeKeyShare(const string &_keyShareName, const string &value, int index, int n, int t); void writeKeyShare(const string &_keyShareName, const string &value, int index, int n, int t);
shared_ptr<std::string> readKeyShare(const string& _keyShare); shared_ptr<std::string> readKeyShare(const string& _keyShare);
...@@ -55,5 +59,6 @@ Json::Value getPublicECDSAKeyImpl(const std::string& keyName); ...@@ -55,5 +59,6 @@ Json::Value getPublicECDSAKeyImpl(const std::string& keyName);
Json::Value generateDKGPolyImpl(const std::string& polyName, int t); Json::Value generateDKGPolyImpl(const std::string& polyName, int t);
Json::Value getVerificationVectorImpl(const std::string& polyName, int n, int t); Json::Value getVerificationVectorImpl(const std::string& polyName, int n, int t);
Json::Value getSecretShareImpl(const std::string& polyName, const std::string& publicKeys, int n, int t); Json::Value getSecretShareImpl(const std::string& polyName, const std::string& publicKeys, int n, int t);
Json::Value DKGVerificationImpl(const std::string& polyName, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index);
#endif //SGXWALLET_SGXWALLETSERVER_HPP #endif //SGXWALLET_SGXWALLETSERVER_HPP
\ No newline at end of file
...@@ -12,16 +12,19 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer> ...@@ -12,16 +12,19 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
public: public:
AbstractStubServer(jsonrpc::AbstractServerConnector &conn, jsonrpc::serverVersion_t type = jsonrpc::JSONRPC_SERVER_V2) : jsonrpc::AbstractServer<AbstractStubServer>(conn, type) AbstractStubServer(jsonrpc::AbstractServerConnector &conn, jsonrpc::serverVersion_t type = jsonrpc::JSONRPC_SERVER_V2) : jsonrpc::AbstractServer<AbstractStubServer>(conn, type)
{ {
this->bindAndAddMethod(jsonrpc::Procedure("importBLSKeyShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "index",jsonrpc::JSON_INTEGER,"keyShare",jsonrpc::JSON_STRING,"keyShareName",jsonrpc::JSON_STRING,"n",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::importBLSKeyShareI); this->bindAndAddMethod(jsonrpc::Procedure("importBLSKeyShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "index",jsonrpc::JSON_INTEGER,"keyShare",jsonrpc::JSON_STRING,"keyShareName",jsonrpc::JSON_STRING,"n",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::importBLSKeyShareI);
this->bindAndAddMethod(jsonrpc::Procedure("blsSignMessageHash", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyShareName",jsonrpc::JSON_STRING,"messageHash",jsonrpc::JSON_STRING,"n",jsonrpc::JSON_INTEGER,"signerIndex",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::blsSignMessageHashI); this->bindAndAddMethod(jsonrpc::Procedure("blsSignMessageHash", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyShareName",jsonrpc::JSON_STRING,"messageHash",jsonrpc::JSON_STRING,"n",jsonrpc::JSON_INTEGER,"signerIndex",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::blsSignMessageHashI);
this->bindAndAddMethod(jsonrpc::Procedure("importECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "key",jsonrpc::JSON_STRING,"keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::importECDSAKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("generateECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::generateECDSAKeyI); this->bindAndAddMethod(jsonrpc::Procedure("importECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "key",jsonrpc::JSON_STRING,"keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::importECDSAKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("getPublicECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::getPublicECDSAKeyI); this->bindAndAddMethod(jsonrpc::Procedure("generateECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::generateECDSAKeyI);
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("getPublicECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::getPublicECDSAKeyI);
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("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("getVerificationVector", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT,"polyName",jsonrpc::JSON_STRING, "n",jsonrpc::JSON_INTEGER,"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_STRING,"n",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getSecretShareI); 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, "n",jsonrpc::JSON_INTEGER,"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_STRING,"n",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getSecretShareI);
this->bindAndAddMethod(jsonrpc::Procedure("DKGVerification", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",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);
}
inline virtual void importBLSKeyShareI(const Json::Value &request, Json::Value &response) inline virtual void importBLSKeyShareI(const Json::Value &request, Json::Value &response)
{ {
...@@ -59,6 +62,10 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer> ...@@ -59,6 +62,10 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
{ {
response = this->getSecretShare(request["polyName"].asString(), request["publicKeys"].asString(), request["n"].asInt(),request["t"].asInt()); response = this->getSecretShare(request["polyName"].asString(), request["publicKeys"].asString(), request["n"].asInt(),request["t"].asInt());
} }
inline virtual void DKGVerificationI(const Json::Value &request, Json::Value &response)
{
response = this->DKGVerification(request["polyName"].asString(), request["EthKeyName"].asString(), request["SecretShare"].asString(), request["t"].asInt(), request["n"].asInt(), request["index"].asInt());
}
virtual Json::Value importBLSKeyShare(int index, const std::string& keyShare, const std::string& keyShareName, int n, int t) = 0; virtual Json::Value importBLSKeyShare(int index, const std::string& keyShare, const std::string& keyShareName, int n, int t) = 0;
virtual Json::Value blsSignMessageHash(const std::string& keyShareName, const std::string& messageHash, int n, int signerIndex, int t) = 0; virtual Json::Value blsSignMessageHash(const std::string& keyShareName, const std::string& messageHash, int n, int signerIndex, int t) = 0;
virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName) = 0; virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName) = 0;
...@@ -68,6 +75,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer> ...@@ -68,6 +75,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
virtual Json::Value generateDKGPoly(const std::string& polyName, int t) = 0; virtual Json::Value generateDKGPoly(const std::string& polyName, int t) = 0;
virtual Json::Value getVerificationVector(const std::string& polyName, int n, int t) = 0; virtual Json::Value getVerificationVector(const std::string& polyName, int n, int t) = 0;
virtual Json::Value getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t) = 0; virtual Json::Value getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t) = 0;
virtual Json::Value DKGVerification( const std::string& polyName, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index) = 0;
}; };
#endif //JSONRPC_CPP_STUB_ABSTRACTSTUBSERVER_H_ #endif //JSONRPC_CPP_STUB_ABSTRACTSTUBSERVER_H_
DKGUtils.o: DKGUtils.cpp DKGUtils.h \ DKGUtils.o: DKGUtils.cpp DKGUtils.h \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp \ /home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp \ ../sgx-sdk-build/sgxsdk/include/libcxx/iosfwd \
../sgx-sdk-build/sgxsdk/include/libcxx/vector \
../sgx-sdk-build/sgxsdk/include/libcxx/__config \ ../sgx-sdk-build/sgxsdk/include/libcxx/__config \
../sgx-sdk-build/sgxsdk/include/libcxx/__sgx \ ../sgx-sdk-build/sgxsdk/include/libcxx/__sgx \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/endian.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/endian.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/assert.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/assert.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \
../sgx-sdk-build/sgxsdk/include/libcxx/iosfwd \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/wchar.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/wchar.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/stdint.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/stdint.h \
../sgx-sdk-build/sgxsdk/include/libcxx/cstdio \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp \
../sgx-sdk-build/sgxsdk/include/libcxx/vector \
../sgx-sdk-build/sgxsdk/include/libcxx/__bit_reference \ ../sgx-sdk-build/sgxsdk/include/libcxx/__bit_reference \
../sgx-sdk-build/sgxsdk/include/libcxx/algorithm \ ../sgx-sdk-build/sgxsdk/include/libcxx/algorithm \
../sgx-sdk-build/sgxsdk/include/libcxx/initializer_list \ ../sgx-sdk-build/sgxsdk/include/libcxx/initializer_list \
../sgx-sdk-build/sgxsdk/include/libcxx/cstddef \ ../sgx-sdk-build/sgxsdk/include/libcxx/cstddef \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h \
../sgx-sdk-build/sgxsdk/include/libcxx/type_traits \ ../sgx-sdk-build/sgxsdk/include/libcxx/type_traits \
../sgx-sdk-build/sgxsdk/include/libcxx/cstring \ ../sgx-sdk-build/sgxsdk/include/libcxx/cstring \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h \
...@@ -48,17 +54,12 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \ ...@@ -48,17 +54,12 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \
../sgx-sdk-build/sgxsdk/include/libcxx/cstdlib \ ../sgx-sdk-build/sgxsdk/include/libcxx/cstdlib \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h \
../sgx-sdk-build/sgxsdk/include/libcxx/climits \ ../sgx-sdk-build/sgxsdk/include/libcxx/climits \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h \
../sgx-sdk-build/sgxsdk/include/libcxx/__split_buffer \ ../sgx-sdk-build/sgxsdk/include/libcxx/__split_buffer \
../trusted_libff/libff/common/utils.hpp \ ../trusted_libff/libff/common/utils.hpp \
../sgx-sdk-build/sgxsdk/include/libcxx/cassert \ ../sgx-sdk-build/sgxsdk/include/libcxx/cassert \
../sgx-sdk-build/sgxsdk/include/libcxx/iostream \ ../sgx-sdk-build/sgxsdk/include/libcxx/iostream \
../sgx-sdk-build/sgxsdk/include/libcxx/sstream \ ../sgx-sdk-build/sgxsdk/include/libcxx/sstream \
../sgx-sdk-build/sgxsdk/include/libcxx/string \ ../sgx-sdk-build/sgxsdk/include/libcxx/string \
../sgx-sdk-build/sgxsdk/include/libcxx/cstdio \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h \
../sgx-sdk-build/sgxsdk/include/libcxx/cwchar \ ../sgx-sdk-build/sgxsdk/include/libcxx/cwchar \
../sgx-sdk-build/sgxsdk/include/libcxx/cwctype \ ../sgx-sdk-build/sgxsdk/include/libcxx/cwctype \
../sgx-sdk-build/sgxsdk/include/libcxx/cctype \ ../sgx-sdk-build/sgxsdk/include/libcxx/cctype \
...@@ -70,7 +71,6 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \ ...@@ -70,7 +71,6 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \
../trusted_libff/libff/algebra/fields/fp.hpp \ ../trusted_libff/libff/algebra/fields/fp.hpp \
../trusted_libff/libff/algebra/exponentiation/exponentiation.hpp \ ../trusted_libff/libff/algebra/exponentiation/exponentiation.hpp \
../trusted_libff/libff/algebra/fields/bigint.hpp \ ../trusted_libff/libff/algebra/fields/bigint.hpp \
/home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \
../trusted_libff/libff/algebra/fields/bigint.tcc \ ../trusted_libff/libff/algebra/fields/bigint.tcc \
../sgx-sdk-build/sgxsdk/include/libcxx/random \ ../sgx-sdk-build/sgxsdk/include/libcxx/random \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h \
...@@ -98,11 +98,9 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \ ...@@ -98,11 +98,9 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \
DKGUtils.h: DKGUtils.h:
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp: /home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h:
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp:
../sgx-sdk-build/sgxsdk/include/libcxx/vector: ../sgx-sdk-build/sgxsdk/include/libcxx/iosfwd:
../sgx-sdk-build/sgxsdk/include/libcxx/__config: ../sgx-sdk-build/sgxsdk/include/libcxx/__config:
...@@ -114,14 +112,30 @@ DKGUtils.h: ...@@ -114,14 +112,30 @@ DKGUtils.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h: /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h:
../sgx-sdk-build/sgxsdk/include/libcxx/iosfwd:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/wchar.h: /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/wchar.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h: /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/stdint.h: /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/stdint.h:
../sgx-sdk-build/sgxsdk/include/libcxx/cstdio:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h:
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp:
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp:
../sgx-sdk-build/sgxsdk/include/libcxx/vector:
../sgx-sdk-build/sgxsdk/include/libcxx/__bit_reference: ../sgx-sdk-build/sgxsdk/include/libcxx/__bit_reference:
../sgx-sdk-build/sgxsdk/include/libcxx/algorithm: ../sgx-sdk-build/sgxsdk/include/libcxx/algorithm:
...@@ -130,8 +144,6 @@ DKGUtils.h: ...@@ -130,8 +144,6 @@ DKGUtils.h:
../sgx-sdk-build/sgxsdk/include/libcxx/cstddef: ../sgx-sdk-build/sgxsdk/include/libcxx/cstddef:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h:
../sgx-sdk-build/sgxsdk/include/libcxx/type_traits: ../sgx-sdk-build/sgxsdk/include/libcxx/type_traits:
../sgx-sdk-build/sgxsdk/include/libcxx/cstring: ../sgx-sdk-build/sgxsdk/include/libcxx/cstring:
...@@ -196,10 +208,6 @@ DKGUtils.h: ...@@ -196,10 +208,6 @@ DKGUtils.h:
../sgx-sdk-build/sgxsdk/include/libcxx/climits: ../sgx-sdk-build/sgxsdk/include/libcxx/climits:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h:
../sgx-sdk-build/sgxsdk/include/libcxx/__split_buffer: ../sgx-sdk-build/sgxsdk/include/libcxx/__split_buffer:
../trusted_libff/libff/common/utils.hpp: ../trusted_libff/libff/common/utils.hpp:
...@@ -212,12 +220,6 @@ DKGUtils.h: ...@@ -212,12 +220,6 @@ DKGUtils.h:
../sgx-sdk-build/sgxsdk/include/libcxx/string: ../sgx-sdk-build/sgxsdk/include/libcxx/string:
../sgx-sdk-build/sgxsdk/include/libcxx/cstdio:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h:
../sgx-sdk-build/sgxsdk/include/libcxx/cwchar: ../sgx-sdk-build/sgxsdk/include/libcxx/cwchar:
../sgx-sdk-build/sgxsdk/include/libcxx/cwctype: ../sgx-sdk-build/sgxsdk/include/libcxx/cwctype:
...@@ -240,8 +242,6 @@ DKGUtils.h: ...@@ -240,8 +242,6 @@ DKGUtils.h:
../trusted_libff/libff/algebra/fields/bigint.hpp: ../trusted_libff/libff/algebra/fields/bigint.hpp:
/home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h:
../trusted_libff/libff/algebra/fields/bigint.tcc: ../trusted_libff/libff/algebra/fields/bigint.tcc:
../sgx-sdk-build/sgxsdk/include/libcxx/random: ../sgx-sdk-build/sgxsdk/include/libcxx/random:
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include <string.h> #include <string.h>
//void gen_session_keys(mpz_t skey, char* pb_keyB){
void gen_session_key(char *skey_str, char* pb_keyB, char* common_key){ void gen_session_key(char *skey_str, char* pb_keyB, char* common_key){
char* pb_keyB_x = (char*)malloc(65); char* pb_keyB_x = (char*)malloc(65);
...@@ -53,6 +52,44 @@ void gen_session_key(char *skey_str, char* pb_keyB, char* common_key){ ...@@ -53,6 +52,44 @@ void gen_session_key(char *skey_str, char* pb_keyB, char* common_key){
free(pb_keyB_y); free(pb_keyB_y);
} }
void session_key_recover(const char *skey_str, const char* sshare, char* common_key){
char* pb_keyB_x = (char*)malloc(65);
strncpy(pb_keyB_x, sshare + 64, 64);
char* pb_keyB_y = (char*)malloc(65);
strncpy(pb_keyB_y, sshare + 128, 64);
domain_parameters curve = domain_parameters_init();
domain_parameters_load_curve(curve, secp256k1);
mpz_t skey;
mpz_init(skey);
mpz_set_str(skey, skey_str, 16);
point pub_keyB = point_init();
point_set_hex(pub_keyB, pb_keyB_x, pb_keyB_y);
point session_key = point_init();
point_multiplication(session_key, skey, pub_keyB, curve);
char arr_x[mpz_sizeinbase (session_key->x, 16) + 2];
char* x = mpz_get_str(arr_x, 16, session_key->x);
//strncpy(common_key, arr_x, 64);
int n_zeroes = 64 - strlen(arr_x);
for ( int i = 0; i < n_zeroes; i++){
common_key[i] = '0';
}
strncpy(common_key + n_zeroes, arr_x, strlen(arr_x));
//strncpy(common_key , pb_keyB_x, 64);
mpz_clear(skey);
point_clear(pub_keyB);
domain_parameters_clear(curve);
free(pb_keyB_x);
free(pb_keyB_y);
}
void xor_encrypt(char* key, char* message, char* cypher){ void xor_encrypt(char* key, char* message, char* cypher){
uint8_t cypher_bin[33]; uint8_t cypher_bin[33];
...@@ -75,3 +112,27 @@ void xor_encrypt(char* key, char* message, char* cypher){ ...@@ -75,3 +112,27 @@ void xor_encrypt(char* key, char* message, char* cypher){
free(key_bin); free(key_bin);
} }
void xor_decrypt(char* key, char* cypher, char* message){
uint8_t msg_bin[33];
//uint8_t key_bin[33];
uint8_t* key_bin = (uint8_t*)malloc(33);
uint64_t key_length;
hex2carray(key, &key_length, key_bin);
uint64_t cypher_length;
uint8_t cypher_bin[33];//[ECDSA_BIN_LEN];
hex2carray(cypher, &cypher_length, cypher_bin);
for (int i = 0; i < 32; i++){
msg_bin[i] = cypher_bin[i] ^ key_bin[i];
}
carray2Hex(msg_bin, 32, message);
free(key_bin);
}
...@@ -8,7 +8,11 @@ ...@@ -8,7 +8,11 @@
//void gen_session_keys(mpz_t skey, char* pub_key); //void gen_session_keys(mpz_t skey, char* pub_key);
void gen_session_key(char* skey, char* pub_keyB, char* common_key); void gen_session_key(char* skey, char* pub_keyB, char* common_key);
void session_key_recover(const char *skey_str, const char* sshare, char* common_key);
void xor_encrypt(char* key, char* message, char* cypher); void xor_encrypt(char* key, char* message, char* cypher);
void xor_decrypt(char* key, char* cypher, char* message);
#endif //SGXD_DRIVE_KEY_DKG_H #endif //SGXD_DRIVE_KEY_DKG_H
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "DKGUtils.h" #include "DKGUtils.h"
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp> #include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <../trusted_libff/libff/algebra/fields/fp.hpp> #include <../trusted_libff/libff/algebra/fields/fp.hpp>
...@@ -15,6 +16,7 @@ ...@@ -15,6 +16,7 @@
#include <stdio.h> #include <stdio.h>
std::string stringFromFr(libff::alt_bn128_Fr& _el) { std::string stringFromFr(libff::alt_bn128_Fr& _el) {
mpz_t t; mpz_t t;
...@@ -151,7 +153,34 @@ void calc_public_shares(const char* decrypted_koefs, char * public_shares, ...@@ -151,7 +153,34 @@ void calc_public_shares(const char* decrypted_koefs, char * public_shares,
result += ","; result += ",";
} }
strncpy(public_shares, result.c_str(), result.length()); strncpy(public_shares, result.c_str(), result.length());
//strncpy(public_shares, decrypted_koefs, 10000); }
int Verification (char * decrypted_koefs, mpz_t decr_secret_share, int _t, int ind ){
libff::init_alt_bn128_params();
char symbol = ':';
std::vector<libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_koefs, symbol);
std::vector<libff::alt_bn128_G2> pub_shares;
for (size_t i = 0; i < _t; ++i) {
libff::alt_bn128_G2 pub_share = poly.at(i) * libff::alt_bn128_G2::one();
pub_shares.push_back(pub_share);
}
libff::alt_bn128_G2 val = libff::alt_bn128_G2::zero();
for (int i = 0; i < _t; ++i) {
val = val + power(libff::alt_bn128_Fr(ind + 1), i) * pub_shares[i];
}
char arr[mpz_sizeinbase (decr_secret_share, 10) + 2];
char * tmp = mpz_get_str(arr, 10, decr_secret_share);
libff::alt_bn128_Fr sshare(tmp);
//strncpy(decrypted_koefs, ConvertToString(val.X.c0).c_str(), 1024);
libff::alt_bn128_G2 val2 = sshare * libff::alt_bn128_G2::one();
strncpy(decrypted_koefs, ConvertToString(val2.X.c0).c_str(), 1024);
return (val == sshare * libff::alt_bn128_G2::one());
} }
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#define EXTERNC #define EXTERNC
#endif #endif
#include <sgx_tgmp.h>
EXTERNC void gen_dkg_poly( char* secret, unsigned _t); EXTERNC void gen_dkg_poly( char* secret, unsigned _t);
EXTERNC void calc_secret_shares(const char* decrypted_koefs, char * secret_shares, EXTERNC void calc_secret_shares(const char* decrypted_koefs, char * secret_shares,
...@@ -22,5 +24,7 @@ EXTERNC void calc_secret_share(const char* decrypted_koefs, char * s_share, ...@@ -22,5 +24,7 @@ EXTERNC void calc_secret_share(const char* decrypted_koefs, char * s_share,
EXTERNC void calc_public_shares(const char* decrypted_koefs, char * public_shares, EXTERNC void calc_public_shares(const char* decrypted_koefs, char * public_shares,
unsigned _t); unsigned _t);
EXTERNC int Verification (char * decrypted_koefs, mpz_t decr_secret_share, int _t, int ind );
#endif //SGXD_DKGUTILS_H #endif //SGXD_DKGUTILS_H
...@@ -141,7 +141,7 @@ void generate_ecdsa_key(int *err_status, char *err_string, ...@@ -141,7 +141,7 @@ void generate_ecdsa_key(int *err_status, char *err_string,
mpz_mod(skey, seed, curve->p); mpz_mod(skey, seed, curve->p);
mpz_clear(seed); mpz_clear(seed);
//mpz_set_str(skey, "4160780231445160889237664391382223604576", 10); //mpz_set_str(skey, "4160780231445160889237664391382223604576", 10);
//mpz_set_str(skey, "4160780231445160889237664391382223604184857153814275770598791864649971919844", 10); //mpz_set_str(skey, "4160780231445160889237664391382223604184857153814275770598791864649971919844", 10);
//mpz_set_str(skey, "1", 10); //mpz_set_str(skey, "1", 10);
//mpz_set_str(skey, "ebb2c082fd7727890a28ac82f6bdf97bad8de9f5d7c9028692de1a255cad3e0f", 16); //mpz_set_str(skey, "ebb2c082fd7727890a28ac82f6bdf97bad8de9f5d7c9028692de1a255cad3e0f", 16);
...@@ -612,5 +612,53 @@ void get_encr_sshare(int *err_status, char *err_string, uint8_t *encrypted_skey, ...@@ -612,5 +612,53 @@ void get_encr_sshare(int *err_status, char *err_string, uint8_t *encrypted_skey,
free(cypher); free(cypher);
} }
void dkg_verification(int *err_status, char* err_string, const uint8_t * encrypted_dkg_secret, const char* s_share,
uint8_t* encrypted_key, uint64_t key_len, unsigned _t, int _ind, int * result){
char* decrypted_dkg_secret = (char*)malloc(DKG_BUFER_LENGTH);
uint32_t decr_len ;
decrypt_dkg_secret(err_status, err_string, encrypted_dkg_secret, (uint8_t*)decrypted_dkg_secret, &decr_len);
if (*err_status != 0) {
snprintf(err_string, BUF_LEN,"sgx_unseal_poly failed with status %d", *err_status);
return;
}
//uint32_t dec_len = 625;
char skey[ECDSA_SKEY_LEN];
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_key, NULL, 0, (uint8_t*)skey, &key_len);
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"sgx_unseal_key failed with status %d", status);
return;
}
char encr_sshare[65];
strncpy(encr_sshare, s_share, 64);
encr_sshare[64] = 0;
char common_key[65];
char decr_sshare[65];
session_key_recover(skey, s_share, common_key);
common_key[64] = 0;
xor_decrypt(common_key, encr_sshare, decr_sshare);
//snprintf(err_string, BUF_LEN,"sshare is %s", decr_sshare);
//snprintf(err_string, BUF_LEN,"encr_share is %s", encr_sshare);
//snprintf(err_string, BUF_LEN,"common_key is %s", common_key);
mpz_t s;
mpz_init(s);
mpz_set_str(s, decr_sshare, 16);
*result = Verification(decrypted_dkg_secret, s, _t, _ind);
snprintf(err_string, BUF_LEN,"val is %s", decrypted_dkg_secret);
free(decrypted_dkg_secret);
}
...@@ -86,7 +86,7 @@ enclave { ...@@ -86,7 +86,7 @@ enclave {
public void get_public_shares ( public void get_public_shares (
[user_check] int *err_status, [user_check] int *err_status,
[out, count = 1024] char* err_string, [out, count = 1024] char* err_string,
[in, count = 3050] uint8_t* decrypted_dkg_secret, [in, count = 3050] uint8_t* encrypted_dkg_secret,
uint32_t enc_len, uint32_t enc_len,
[out, count = 10000] char* public_shares, [out, count = 10000] char* public_shares,
unsigned _t, unsigned _t,
...@@ -117,6 +117,17 @@ enclave { ...@@ -117,6 +117,17 @@ enclave {
uint8_t _t, uint8_t _t,
uint8_t _n, uint8_t _n,
uint8_t ind); uint8_t ind);
public void dkg_verification(
[user_check] int *err_status,
[out, count = 1024] char* err_string,
[in, count = 3050] const uint8_t* encrypted_dkg_secret,
[in, count = 129] const char* s_share,
[in, count = 1024] uint8_t* encrypted_key,
uint64_t key_len,
unsigned _t,
int _ind,
[user_check] int* result);
}; };
......
...@@ -125,5 +125,23 @@ ...@@ -125,5 +125,23 @@
"errorMessage": "12345", "errorMessage": "12345",
"SecretShare": "123" "SecretShare": "123"
} }
},
{
"name": "DKGVerification",
"params": {
"polyName": "p2",
"EthKeyName":"key1",
"SecretShare": "123",
"n": 3,
"t": 3,
"index" : 2
},
"returns": {
"status": 0,
"errorMessage": "12345",
"result": true
}
} }
] ]
\ No newline at end of file
...@@ -94,26 +94,41 @@ class StubClient : public jsonrpc::Client ...@@ -94,26 +94,41 @@ class StubClient : public jsonrpc::Client
else else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
} }
Json::Value getVerificationVector(const std::string& polyName, int n, int t) throw (jsonrpc::JsonRpcException) Json::Value getVerificationVector(const std::string& polyName, int n, int t) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["polyName"] = polyName;
p["n"] = n;
p["t"] = t;
Json::Value result = this->CallMethod("getVerificationVector",p);
if (result.isObject())
return result;
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["polyName"] = polyName;
p["publicKeys"] = publicKeys;
p["n"] = n;
p["t"] = t;
Json::Value result = this->CallMethod("getSecretShare",p);
if (result.isObject())
return result;
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value DKGVerification(const std::string& polyName, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index) throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; Json::Value p;
p["polyName"] = polyName; p["EthKeyName"] = EthKeyName;
p["SecretShare"] = SecretShare;
p["index"] = index;
p["n"] = n; p["n"] = n;
p["t"] = t;
Json::Value result = this->CallMethod("getVerificationVector",p);
if (result.isObject())
return result;
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["polyName"] = polyName; p["polyName"] = polyName;
p["publicKeys"] = publicKeys;
p["n"] = n;
p["t"] = t; p["t"] = t;
Json::Value result = this->CallMethod("getSecretShare",p); Json::Value result = this->CallMethod("DKGVerification",p);
if (result.isObject()) if (result.isObject())
return result; return result;
else else
......
...@@ -531,6 +531,44 @@ TEST_CASE( "DKG encrypted secret shares test", "[dkg-encr_sshares]" ) { ...@@ -531,6 +531,44 @@ TEST_CASE( "DKG encrypted secret shares test", "[dkg-encr_sshares]" ) {
std::cerr << "secret share is " << result << std::endl; std::cerr << "secret share is " << result << std::endl;
} }
TEST_CASE( "DKG verification test", "[dkg-verify]" ) {
// init_all();
init_enclave();
uint8_t *encrypted_key = (uint8_t *) calloc(BUF_LEN, 1);
char *errMsg = (char *)calloc(1024, 1);
char *result = (char *)calloc(130, 1);
int err_status = 0;
uint32_t enc_len = 0;
uint8_t* encrypted_dkg_secret = (uint8_t*) calloc(DKG_MAX_SEALED_LEN, 1);
status = gen_dkg_secret (eid, &err_status, errMsg, encrypted_dkg_secret, &enc_len, 2);
REQUIRE(status == SGX_SUCCESS);
std::cerr << " poly generated" << std::endl;
status = set_encrypted_dkg_poly(eid, &err_status, errMsg, encrypted_dkg_secret);
REQUIRE(status == SGX_SUCCESS);
std::cerr << " poly set" << std::endl;
uint8_t *encr_pr_DHkey = (uint8_t *)calloc(1024, 1);
char *pub_key_x = (char *)calloc(1024, 1);
char *pub_key_y = (char *)calloc(1024, 1);
char *pub_keyB = "c0152c48bf640449236036075d65898fded1e242c00acb45519ad5f788ea7cbf9a5df1559e7fc87932eee5478b1b9023de19df654395574a690843988c3ff475";
status = get_encr_sshare(eid, &err_status, errMsg, encr_pr_DHkey, &enc_len, result,
pub_keyB, 2, 2, 1);
REQUIRE(status == SGX_SUCCESS);
printf(" get_encr_sshare completed with status: %d %s \n", err_status, errMsg);
std::cerr << "secret share is " << result << std::endl;
}
TEST_CASE("ECDSA keygen and signature test", "[ecdsa_test]") { TEST_CASE("ECDSA keygen and signature test", "[ecdsa_test]") {
...@@ -666,14 +704,16 @@ TEST_CASE("API test", "[api_test]") { ...@@ -666,14 +704,16 @@ TEST_CASE("API test", "[api_test]") {
cerr << "Client inited" << endl; cerr << "Client inited" << endl;
try { try {
// cout << c.generateECDSAKey("known_key1") << endl; // cout << c.generateECDSAKey("test_key2") << endl;
//cout<<c.getPublicECDSAKey("test_key"); //cout<<c.getPublicECDSAKey("test_key1");
//cout << c.ecdsaSignMessageHash(16, "known_key1","0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db" ); //cout << c.ecdsaSignMessageHash(16, "known_key1","0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db" );
// cout << c.blsSignMessageHash(TEST_BLS_KEY_NAME, "0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db", 2,2,1 ); // cout << c.blsSignMessageHash(TEST_BLS_KEY_NAME, "0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db", 2,2,1 );
// cout << c.generateDKGPoly("p2", 2); // cout << c.generateDKGPoly("p2", 2);
//cout << c.getVerificationVector("polyy", 5, 5); //cout << c.getVerificationVector("polyy", 5, 5);
// cout << c.getSecretShare("p2",
// "505f55a38f9c064da744f217d1cb993a17705e9839801958cda7c884e08ab4dad7fd8d22953d3ac7f0913de24fd67d7ed36741141b8a3da152d7ba954b0f14e232d69c361f0bc9e05f1cf8ef387122dc1d2f7cee7b6cda3537fc9427c02328b01f02fd94ec933134dc795a642864f8cb41ae263e11abaf992e21fcf9be732deb",
// 2,2);
cout << c.getSecretShare("p2", cout << c.getSecretShare("p2",
"669aa790e1c5f5199af82ab0b6f1965c382d23a2ebdda581454adba3fd082a30edab62b545f78f1e402ceef7340a0364a7046633d6151fe7e657d8b8a6352378b3e6fdfe2633256ae1662fcd23466d02ead907b5d4366136341cea5e46f5a7bb67d897d6e35f619810238aa143c416f61c640ed214eb9c67a34c4a31b7d25e6e", "669aa790e1c5f5199af82ab0b6f1965c382d23a2ebdda581454adba3fd082a30edab62b545f78f1e402ceef7340a0364a7046633d6151fe7e657d8b8a6352378b3e6fdfe2633256ae1662fcd23466d02ead907b5d4366136341cea5e46f5a7bb67d897d6e35f619810238aa143c416f61c640ed214eb9c67a34c4a31b7d25e6e",
...@@ -685,6 +725,13 @@ TEST_CASE("API test", "[api_test]") { ...@@ -685,6 +725,13 @@ TEST_CASE("API test", "[api_test]") {
// 3,3); // 3,3);
// std::string share_big = "501e364a6ea516f4812b013bcc150cbb435a2c465c9fd525951264969d8441a986798fd3317c1c3e60f868bb26c4cff837d9185f4be6015d8326437cb5b69480495859cd5a385430ece51252acdc234d8dbde75708b600ac50b2974e813ee26bd87140d88647fcc44df7262bbba24328e8ce622cd627a15b508ffa0db9ae81e0e110fab42cfe40da66b524218ca3c8e5aa3363fbcadef748dc3523a7ffb95b8f5d8141a5163db9f69d1ab223494ed71487c9bb032a74c08a222d897a5e49a617";
std::string share_big = "03f749e2fcc28021895d757ec16d1636784446f5effcd3096b045136d8ab02657b32adc577f421330b81f5b7063df3b08a0621a897df2584b9046ca416e50ecc27e8c3277e981f7e650f8640289be128eecf0105f89a20e5ffb164744c45cf191d627ce9ab6c44e2ef96f230f2a4de742ea43b6f74b56849138026610b2d965605ececba527048a0f29f46334b1cec1d23df036248b24eccca99057d24764acee66c1a3f2f44771d0d237bf9d18c4177277e3ce3dc4e83686a2647fce1565ee0";
std::string share = share_big.substr(0, 192);
cout << c.DKGVerification("p2", "test_key1", share, 2, 2, 0);
} catch (JsonRpcException &e) { } catch (JsonRpcException &e) {
cerr << e.what() << endl; cerr << e.what() << endl;
} }
......
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