Unverified Commit 5ef86e52 authored by Sveta Rogova's avatar Sveta Rogova Committed by GitHub

Merge pull request #9 from skalenetwork/enhancement/SKALE-1779-Change-generateECDSAKey-in-SGX

Enhancement/skale 1779 change generate ecdsa key in sgx
parents 833cf64a 93b8db50
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include <gmp.h> #include <gmp.h>
#include <random> #include <random>
static std::default_random_engine rand_gen((unsigned int) time(0));
std::vector<std::string> gen_ecdsa_key(){ std::vector<std::string> gen_ecdsa_key(){
char *errMsg = (char *)calloc(1024, 1); char *errMsg = (char *)calloc(1024, 1);
int err_status = 0; int err_status = 0;
...@@ -28,8 +30,9 @@ std::vector<std::string> gen_ecdsa_key(){ ...@@ -28,8 +30,9 @@ std::vector<std::string> gen_ecdsa_key(){
//std::cerr << "in ECDSACrypto encr key x " << keys.at(0) << std::endl; //std::cerr << "in ECDSACrypto encr key x " << keys.at(0) << std::endl;
//std::cerr << "in ECDSACrypto encr_len %d " << enc_len << std::endl; //std::cerr << "in ECDSACrypto encr_len %d " << enc_len << std::endl;
std::default_random_engine rand_gen((unsigned int) time(0));
unsigned long seed = rand_gen(); unsigned long seed = rand_gen();
std::cerr << "seed is " << seed << std::endl;
gmp_randstate_t state; gmp_randstate_t state;
gmp_randinit_default(state); gmp_randinit_default(state);
......
...@@ -29,6 +29,19 @@ ...@@ -29,6 +29,19 @@
#include "SGXWalletServer.h" #include "SGXWalletServer.h"
#include "SGXWalletServer.hpp" #include "SGXWalletServer.hpp"
#include <algorithm>
bool isStringDec( std::string & str){
auto res = std::find_if_not(str.begin(), str.end(), [](char c)->bool{
return std::isdigit(c);
});
return !str.empty() && res == str.end();
// bool res =tr
// for (int i = 0; i < str.length; i++){
// }
}
SGXWalletServer::SGXWalletServer(AbstractServerConnector &connector, SGXWalletServer::SGXWalletServer(AbstractServerConnector &connector,
serverVersion_t type) serverVersion_t type)
: AbstractStubServer(connector, type) {} : AbstractStubServer(connector, type) {}
...@@ -179,7 +192,7 @@ Json::Value generateECDSAKeyImpl() { ...@@ -179,7 +192,7 @@ Json::Value generateECDSAKeyImpl() {
return result; return result;
} }
Json::Value renameESDSAKeyImpl(const std::string& KeyName, const std::string& tempKeyName){ Json::Value renameECDSAKeyImpl(const std::string& KeyName, const std::string& tempKeyName){
Json::Value result; Json::Value result;
result["status"] = 0; result["status"] = 0;
result["errorMessage"] = ""; result["errorMessage"] = "";
...@@ -189,18 +202,19 @@ Json::Value renameESDSAKeyImpl(const std::string& KeyName, const std::string& te ...@@ -189,18 +202,19 @@ Json::Value renameESDSAKeyImpl(const std::string& KeyName, const std::string& te
std::string prefix = tempKeyName.substr(0,8); std::string prefix = tempKeyName.substr(0,8);
if (prefix != "tmp_NEK:") { if (prefix != "tmp_NEK:") {
throw RPCException(UNKNOWN_ERROR, ""); throw RPCException(UNKNOWN_ERROR, "wrong temp key name");
} }
prefix = KeyName.substr(0,5); prefix = KeyName.substr(0,12);
if (prefix != "NODE_") { if (prefix != "NEK_NODE_ID:") {
throw RPCException(UNKNOWN_ERROR, ""); throw RPCException(UNKNOWN_ERROR, "wrong key name");
} }
std::string chain_str = "CHAIN_"; std::string postfix = KeyName.substr(12, KeyName.length());
if ( KeyName.find(chain_str) == std::string::npos){ if (!isStringDec(postfix)){
throw RPCException(UNKNOWN_ERROR, ""); throw RPCException(UNKNOWN_ERROR, "wrong key name");
} }
std::shared_ptr<std::string> key_ptr = readFromDb(tempKeyName,"");//readECDSAKey(_keyName); std::shared_ptr<std::string> key_ptr = readFromDb(tempKeyName);
std::cerr << "new key name is " << KeyName <<std::endl;
writeDataToDB(KeyName, *key_ptr); writeDataToDB(KeyName, *key_ptr);
levelDb->deleteTempNEK(tempKeyName); levelDb->deleteTempNEK(tempKeyName);
...@@ -233,7 +247,7 @@ Json::Value ecdsaSignMessageHashImpl(int base, const std::string &_keyName, cons ...@@ -233,7 +247,7 @@ Json::Value ecdsaSignMessageHashImpl(int base, const std::string &_keyName, cons
} }
std::cerr << "Hash handled " << cutHash << std::endl; std::cerr << "Hash handled " << cutHash << std::endl;
try { try {
std::shared_ptr<std::string> key_ptr = readECDSAKey(_keyName); std::shared_ptr<std::string> key_ptr = readFromDb(_keyName,"");
// std::cerr << "read encr key" << *key_ptr << std::endl; // std::cerr << "read encr key" << *key_ptr << std::endl;
sign_vect = ecdsa_sign_hash(key_ptr->c_str(),cutHash.c_str(), base); sign_vect = ecdsa_sign_hash(key_ptr->c_str(),cutHash.c_str(), base);
} catch (RPCException &_e) { } catch (RPCException &_e) {
...@@ -261,7 +275,7 @@ Json::Value getPublicECDSAKeyImpl(const std::string& keyName){ ...@@ -261,7 +275,7 @@ Json::Value getPublicECDSAKeyImpl(const std::string& keyName){
std::string Pkey; std::string Pkey;
try { try {
std::shared_ptr<std::string> key_ptr = readECDSAKey(keyName); std::shared_ptr<std::string> key_ptr = readFromDb(keyName,"");
Pkey = get_ecdsa_pubkey( key_ptr->c_str()); Pkey = get_ecdsa_pubkey( key_ptr->c_str());
} catch (RPCException &_e) { } catch (RPCException &_e) {
result["status"] = _e.status; result["status"] = _e.status;
...@@ -374,7 +388,7 @@ Json::Value DKGVerificationImpl(const std::string& publicShares, const std::stri ...@@ -374,7 +388,7 @@ Json::Value DKGVerificationImpl(const std::string& publicShares, const std::stri
try { try {
//std::string keyName = polyName + "_" + std::to_string(ind); //std::string keyName = polyName + "_" + std::to_string(ind);
//std::shared_ptr<std::string> encryptedKeyHex_ptr = readFromDb(EthKeyName, ""); //std::shared_ptr<std::string> encryptedKeyHex_ptr = readFromDb(EthKeyName, "");
std::shared_ptr<std::string> encryptedKeyHex_ptr = readECDSAKey(EthKeyName); std::shared_ptr<std::string> encryptedKeyHex_ptr = readFromDb(EthKeyName);
if ( !VerifyShares(publicShares.c_str(), SecretShare.c_str(), encryptedKeyHex_ptr->c_str(), t, n, ind )){ if ( !VerifyShares(publicShares.c_str(), SecretShare.c_str(), encryptedKeyHex_ptr->c_str(), t, n, ind )){
...@@ -421,7 +435,7 @@ Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::s ...@@ -421,7 +435,7 @@ Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::s
//std::cerr << sshares << std::endl; //std::cerr << sshares << std::endl;
//std::cerr << "length is " << strlen(sshares); //std::cerr << "length is " << strlen(sshares);
std::shared_ptr<std::string> encryptedKeyHex_ptr = readECDSAKey(EthKeyName); std::shared_ptr<std::string> encryptedKeyHex_ptr = readFromDb(EthKeyName);
bool res = CreateBLSShare(BLSKeyName, sshares, encryptedKeyHex_ptr->c_str()); bool res = CreateBLSShare(BLSKeyName, sshares, encryptedKeyHex_ptr->c_str());
if ( res){ if ( res){
...@@ -527,9 +541,9 @@ Json::Value SGXWalletServer::generateECDSAKey() { ...@@ -527,9 +541,9 @@ Json::Value SGXWalletServer::generateECDSAKey() {
return generateECDSAKeyImpl(); return generateECDSAKeyImpl();
} }
Json::Value SGXWalletServer::renameESDSAKey(const std::string& KeyName, const std::string& tempKeyName){ Json::Value SGXWalletServer::renameECDSAKey(const std::string& KeyName, const std::string& tempKeyName){
lock_guard<recursive_mutex> lock(m); lock_guard<recursive_mutex> lock(m);
return renameESDSAKeyImpl(KeyName, tempKeyName); return renameECDSAKeyImpl(KeyName, tempKeyName);
} }
Json::Value SGXWalletServer::getPublicECDSAKey(const std::string &_keyName) { Json::Value SGXWalletServer::getPublicECDSAKey(const std::string &_keyName) {
...@@ -666,9 +680,10 @@ void writeDataToDB(const string & Name, const string &value) { ...@@ -666,9 +680,10 @@ void writeDataToDB(const string & Name, const string &value) {
auto key = Name; auto key = Name;
if (levelDb->readString(Name) != nullptr) { if (levelDb->readString(Name) != nullptr) {
std::cerr << "already exists" << std::endl; std::cerr << "name " << Name << " already exists" << std::endl;
throw new RPCException(KEY_SHARE_ALREADY_EXISTS, "Data with this name already exists"); 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
...@@ -25,7 +25,7 @@ public: ...@@ -25,7 +25,7 @@ public:
virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName); virtual Json::Value importECDSAKey(const std::string& key, const std::string& keyName);
virtual Json::Value generateECDSAKey(); virtual Json::Value generateECDSAKey();
virtual Json::Value renameESDSAKey(const std::string& KeyName, const std::string& tempKeyName); virtual Json::Value renameECDSAKey(const std::string& KeyName, const std::string& tempKeyName);
virtual Json::Value ecdsaSignMessageHash(int base, const std::string& keyShareName, const std::string& messageHash); virtual Json::Value ecdsaSignMessageHash(int base, const std::string& keyShareName, const std::string& messageHash);
virtual Json::Value getPublicECDSAKey(const std::string& keyName); virtual Json::Value getPublicECDSAKey(const std::string& keyName);
...@@ -39,7 +39,7 @@ public: ...@@ -39,7 +39,7 @@ public:
}; };
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 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);
...@@ -56,7 +56,7 @@ Json::Value blsSignMessageHashImpl(const std::string& keyShareName, const std::s ...@@ -56,7 +56,7 @@ Json::Value blsSignMessageHashImpl(const std::string& keyShareName, const std::s
Json::Value importECDSAKeyImpl(const std::string& key, const std::string& keyName); Json::Value importECDSAKeyImpl(const std::string& key, const std::string& keyName);
Json::Value generateECDSAKeyImpl(); Json::Value generateECDSAKeyImpl();
Json::Value renameESDSAKeyImpl(const std::string& KeyName, const std::string& tempKeyName); Json::Value renameECDSAKeyImpl(const std::string& KeyName, const std::string& tempKeyName);
Json::Value ecdsaSignMessageHashImpl(int base, const std::string& keyName, const std::string& messageHash); Json::Value ecdsaSignMessageHashImpl(int base, const std::string& keyName, const std::string& messageHash);
Json::Value getPublicECDSAKeyImpl(const std::string& keyName); Json::Value getPublicECDSAKeyImpl(const std::string& keyName);
......
...@@ -17,7 +17,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer> ...@@ -17,7 +17,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
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("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, NULL), &AbstractStubServer::generateECDSAKeyI); this->bindAndAddMethod(jsonrpc::Procedure("generateECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, NULL), &AbstractStubServer::generateECDSAKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("renameESDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "KeyName",jsonrpc::JSON_STRING,"tempKeyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::renameESDSAKeyI); this->bindAndAddMethod(jsonrpc::Procedure("renameECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "KeyName",jsonrpc::JSON_STRING,"tempKeyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::renameECDSAKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("getPublicECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::getPublicECDSAKeyI); this->bindAndAddMethod(jsonrpc::Procedure("getPublicECDSAKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "keyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::getPublicECDSAKeyI);
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("ecdsaSignMessageHash", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "base",jsonrpc::JSON_INTEGER,"keyName",jsonrpc::JSON_STRING,"messageHash",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::ecdsaSignMessageHashI);
...@@ -47,9 +47,9 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer> ...@@ -47,9 +47,9 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
(void)request; (void)request;
response = this->generateECDSAKey(); response = this->generateECDSAKey();
} }
inline virtual void renameESDSAKeyI(const Json::Value &request, Json::Value &response) inline virtual void renameECDSAKeyI(const Json::Value &request, Json::Value &response)
{ {
response = this->renameESDSAKey(request["KeyName"].asString(), request["tempKeyName"].asString()); response = this->renameECDSAKey(request["KeyName"].asString(), request["tempKeyName"].asString());
} }
inline virtual void getPublicECDSAKeyI(const Json::Value &request, Json::Value &response) inline virtual void getPublicECDSAKeyI(const Json::Value &request, Json::Value &response)
{ {
...@@ -88,7 +88,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer> ...@@ -88,7 +88,7 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
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;
virtual Json::Value generateECDSAKey() = 0; virtual Json::Value generateECDSAKey() = 0;
virtual Json::Value renameESDSAKey(const std::string& KeyName, const std::string& tempKeyName) = 0; virtual Json::Value renameECDSAKey(const std::string& KeyName, const std::string& tempKeyName) = 0;
virtual Json::Value getPublicECDSAKey(const std::string& keyName) = 0; virtual Json::Value getPublicECDSAKey(const std::string& keyName) = 0;
virtual Json::Value ecdsaSignMessageHash(int base, const std::string& keyName, const std::string& messageHash) = 0; virtual Json::Value ecdsaSignMessageHash(int base, const std::string& keyName, const std::string& messageHash) = 0;
......
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
}, },
{ {
"name": "renameESDSAKey", "name": "renameECDSAKey",
"params": { "params": {
"tempKeyName": "key1", "tempKeyName": "key1",
"KeyName": "key2" "KeyName": "key2"
......
...@@ -61,12 +61,12 @@ class StubClient : public jsonrpc::Client ...@@ -61,12 +61,12 @@ 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 renameESDSAKey(const std::string& KeyName, const std::string& tempKeyName) throw (jsonrpc::JsonRpcException) Json::Value renameECDSAKey(const std::string& KeyName, const std::string& tempKeyName) throw (jsonrpc::JsonRpcException)
{ {
Json::Value p; Json::Value p;
p["KeyName"] = KeyName; p["KeyName"] = KeyName;
p["tempKeyName"] = tempKeyName; p["tempKeyName"] = tempKeyName;
Json::Value result = this->CallMethod("renameESDSAKey",p); Json::Value result = this->CallMethod("renameECDSAKey",p);
if (result.isObject()) if (result.isObject())
return result; return result;
else else
......
...@@ -722,9 +722,11 @@ TEST_CASE("API test", "[api_test]") { ...@@ -722,9 +722,11 @@ TEST_CASE("API test", "[api_test]") {
try { try {
//levelDb->deleteOlegKey("0"); //levelDb->deleteOlegKey("0");
//levelDb->deleteOlegKey("1"); //levelDb->deleteOlegKey("1");
levelDb->deleteDHDKGKey("p2_0:");
levelDb->deleteDHDKGKey("p2_1:");
cout << c.generateECDSAKey() << endl; //cout << c.generateECDSAKey() << endl;
// cout << c.renameESDSAKey("NODE_1CHAIN_1","tmp_NEK:bcacde0d26c0ea2c7e649992e7f791e1fba2492f5b7ae63dadb799075167c7fc"); // cout << c.renameESDSAKey("NODE_1CHAIN_1","tmp_NEK:bcacde0d26c0ea2c7e649992e7f791e1fba2492f5b7ae63dadb799075167c7fc");
//cout<<c.getPublicECDSAKey("test_key1"); //cout<<c.getPublicECDSAKey("test_key1");
//cout << c.ecdsaSignMessageHash(16, "known_key1","0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db" ); //cout << c.ecdsaSignMessageHash(16, "known_key1","0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db" );
...@@ -743,7 +745,7 @@ TEST_CASE("API test", "[api_test]") { ...@@ -743,7 +745,7 @@ TEST_CASE("API test", "[api_test]") {
Json::Value publicKeys; Json::Value publicKeys;
publicKeys.append("505f55a38f9c064da744f217d1cb993a17705e9839801958cda7c884e08ab4dad7fd8d22953d3ac7f0913de24fd67d7ed36741141b8a3da152d7ba954b0f14e2"); publicKeys.append("505f55a38f9c064da744f217d1cb993a17705e9839801958cda7c884e08ab4dad7fd8d22953d3ac7f0913de24fd67d7ed36741141b8a3da152d7ba954b0f14e2");
publicKeys.append("378b3e6fdfe2633256ae1662fcd23466d02ead907b5d4366136341cea5e46f5a7bb67d897d6e35f619810238aa143c416f61c640ed214eb9c67a34c4a31b7d25e6e"); publicKeys.append("378b3e6fdfe2633256ae1662fcd23466d02ead907b5d4366136341cea5e46f5a7bb67d897d6e35f619810238aa143c416f61c640ed214eb9c67a34c4a31b7d25e6e");
// cout << c.getSecretShare("p2", publicKeys, 2, 2); cout << c.getSecretShare("p2", publicKeys, 2, 2);
// cout << c.generateDKGPoly("p3", 3); // cout << c.generateDKGPoly("p3", 3);
// cout << c.getSecretShare("p3", // cout << c.getSecretShare("p3",
// "669aa790e1c5f5199af82ab0b6f1965c382d23a2ebdda581454adba3fd082a30edab62b545f78f1e402ceef7340a0364a7046633d6151fe7e657d8b8a6352378b3e6fdfe2633256ae1662fcd23466d02ead907b5d4366136341cea5e46f5a7bb67d897d6e35f619810238aa143c416f61c640ed214eb9c67a34c4a31b7d25e6e9d43f1c88581f53af993da1654c9f91829c1fe5344c4452ef8d2d8675c6a051c19029f6e4f82b035fb3552058cf22c5bbafd9e6456d579634987281765d130b0", // "669aa790e1c5f5199af82ab0b6f1965c382d23a2ebdda581454adba3fd082a30edab62b545f78f1e402ceef7340a0364a7046633d6151fe7e657d8b8a6352378b3e6fdfe2633256ae1662fcd23466d02ead907b5d4366136341cea5e46f5a7bb67d897d6e35f619810238aa143c416f61c640ed214eb9c67a34c4a31b7d25e6e9d43f1c88581f53af993da1654c9f91829c1fe5344c4452ef8d2d8675c6a051c19029f6e4f82b035fb3552058cf22c5bbafd9e6456d579634987281765d130b0",
......
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