Skip to content

Commit

Permalink
Merge pull request #102 from skalenetwork/feature/SKALE-2002-backup
Browse files Browse the repository at this point in the history
Feature/skale 2002 backup
  • Loading branch information
olehnikolaiev authored Jun 11, 2020
2 parents 343ffc8 + e06fc91 commit baaf17c
Show file tree
Hide file tree
Showing 25 changed files with 404 additions and 651 deletions.
29 changes: 1 addition & 28 deletions BLSCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@


std::string *FqToString(libff::alt_bn128_Fq *_fq) {

mpz_t t;
mpz_init(t);

Expand All @@ -82,7 +81,6 @@ int char2int(char _input) {


void carray2Hex(const unsigned char *d, int _len, char *_hexArray) {

char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

Expand All @@ -92,13 +90,11 @@ void carray2Hex(const unsigned char *d, int _len, char *_hexArray) {
}

_hexArray[_len * 2] = 0;

}


bool hex2carray(const char *_hex, uint64_t *_bin_len,
uint8_t *_bin) {

int len = strnlen(_hex, 2 * BUF_LEN);


Expand All @@ -119,12 +115,10 @@ bool hex2carray(const char *_hex, uint64_t *_bin_len,
}

return true;

}

bool hex2carray2(const char *_hex, uint64_t *_bin_len,
uint8_t *_bin, const int _max_length) {

int len = strnlen(_hex, _max_length);//2 * BUF_LEN);


Expand All @@ -145,12 +139,10 @@ bool hex2carray2(const char *_hex, uint64_t *_bin_len,
}

return true;

}

bool sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t _n, size_t _signerIndex,
char *_sig) {

auto keyStr = make_shared<string>(_encryptedKeyHex);

auto hash = make_shared<array<uint8_t, 32>>();
Expand All @@ -161,9 +153,6 @@ bool sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t
throw SGXException(INVALID_HEX, "Invalid hash");
}




auto keyShare = make_shared<BLSPrivateKeyShareSGX>(keyStr, _t, _n);

auto sigShare = keyShare->signWithHelperSGX(hash, _signerIndex);
Expand All @@ -184,7 +173,6 @@ bool sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t

bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t _n, size_t _signerIndex,
char *_sig) {

auto keyStr = make_shared<string>(_encryptedKeyHex);

auto hash = make_shared<array<uint8_t, 32>>();
Expand All @@ -195,8 +183,6 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
throw SGXException(INVALID_HEX, "Invalid hash");
}



// auto keyShare = make_shared<BLSPrivateKeyShareSGX>(keyStr, _t, _n);
//
// auto sigShare = keyShare->signWithHelperSGX(hash, _signerIndex);
Expand Down Expand Up @@ -243,7 +229,6 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz

size_t sz = 0;


uint8_t encryptedKey[BUF_LEN];

bool result = hex2carray(_encryptedKeyHex, &sz, encryptedKey);
Expand All @@ -257,17 +242,14 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
trustedBlsSignMessageAES(eid, &errStatus, errMsg, encryptedKey,
sz, xStrArg, yStrArg, signature);


if (status != SGX_SUCCESS) {
cerr << "SGX enclave call to trustedBlsSignMessage failed:" << status << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("SGX enclave call to trustedBlsSignMessage failed"));
}


std::string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" +
hash_with_hint.second;


std::string sig = signature;

sig.append(":");
Expand All @@ -289,12 +271,7 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz

bool bls_sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t _n, size_t _signerIndex,
char *_sig) {

if (!encryptKeys) {
return sign(_encryptedKeyHex, _hashHex, _t, _n, _signerIndex, _sig);
} else {
return sign_aes(_encryptedKeyHex, _hashHex, _t, _n, _signerIndex, _sig);
}
return sign_aes(_encryptedKeyHex, _hashHex, _t, _n, _signerIndex, _sig);
}

char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) {
Expand Down Expand Up @@ -331,8 +308,6 @@ char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key)
}

char *decryptBLSKeyShareFromHex(int *errStatus, char *errMsg, const char *_encryptedKey) {


*errStatus = -1;

uint64_t decodedLen = 0;
Expand All @@ -345,7 +320,6 @@ char *decryptBLSKeyShareFromHex(int *errStatus, char *errMsg, const char *_encry

char *plaintextKey = (char *) calloc(BUF_LEN, 1);

//status = trustedDecryptKey(eid, errStatus, errMsg, decoded, decodedLen, plaintextKey);
status = trustedDecryptKeyAES(eid, errStatus, errMsg, decoded, decodedLen, plaintextKey);

if (status != SGX_SUCCESS) {
Expand All @@ -357,5 +331,4 @@ char *decryptBLSKeyShareFromHex(int *errStatus, char *errMsg, const char *_encry
}

return plaintextKey;

}
130 changes: 2 additions & 128 deletions BLSPrivateKeyShareSGX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@


std::string *stringFromFq(libff::alt_bn128_Fq*_fq) {

mpz_t t;
mpz_init(t);

Expand All @@ -54,21 +53,17 @@ std::string *stringFromFq(libff::alt_bn128_Fq*_fq) {
}

std::string *stringFromG1(libff::alt_bn128_G1 *_g1) {


auto sX = stringFromFq(&_g1->X);
auto sY = stringFromFq(&_g1->Y);
auto sZ = stringFromFq(&_g1->Z);


auto sG1 = new std::string(*sX + ":" + *sY + ":" + *sZ);

delete(sX);
delete(sY);
delete(sZ);

return sG1;

}


Expand Down Expand Up @@ -105,9 +100,6 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
size_t _signerIndex) {
shared_ptr<signatures::Bls> obj;

// if (_signerIndex == 0) {
// BOOST_THROW_EXCEPTION(runtime_error("Zero signer index"));
// }
if (hash_byte_arr == nullptr) {
std::cerr << "Hash is null" << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("Hash is null"));
Expand All @@ -121,7 +113,6 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr(

int errStatus = 0;


string* xStr = stringFromFq(&(hash_with_hint.first.X));

if (xStr == nullptr) {
Expand All @@ -136,7 +127,6 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
}


char errMsg[BUF_LEN];
memset(errMsg, 0, BUF_LEN);

Expand All @@ -152,7 +142,6 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr(

size_t sz = 0;


uint8_t encryptedKey[BUF_LEN];

bool result = hex2carray(encryptedKeyHex->c_str(), &sz, encryptedKey);
Expand All @@ -168,17 +157,13 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
trustedBlsSignMessage(eid, &errStatus, errMsg, encryptedKey,
encryptedKeyHex->size() / 2, xStrArg, yStrArg, signature);

// strncpy(signature, "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855", 1024);

printf("sig is: %s\n", signature);


if (status != SGX_SUCCESS) {
gmp_printf("SGX enclave call to trustedBlsSignMessage failed: 0x%04x\n", status);
gmp_printf("SGX enclave call to trustedBlsSignMessage failed: 0x%04x\n", status);
BOOST_THROW_EXCEPTION(runtime_error("SGX enclave call to trustedBlsSignMessage failed"));
}


if (errStatus != 0) {
BOOST_THROW_EXCEPTION(runtime_error("Enclave trustedBlsSignMessage failed:" + to_string(errStatus) + ":" + errMsg ));
return nullptr;
Expand All @@ -190,137 +175,26 @@ std::string BLSPrivateKeyShareSGX::signWithHelperSGXstr(
BOOST_THROW_EXCEPTION(runtime_error("Signature is too short:" + to_string(sigLen)));
}




std::string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" +
hash_with_hint.second;



std::string sig = signature;

sig.append(":");
sig.append(hint);



return sig;
}

std::shared_ptr<BLSSigShare> BLSPrivateKeyShareSGX::signWithHelperSGX(
std::shared_ptr<std::array<uint8_t, 32>> hash_byte_arr,
size_t _signerIndex) {
/* shared_ptr<signatures::Bls> obj;
if (_signerIndex == 0) {
BOOST_THROW_EXCEPTION(runtime_error("Zero signer index"));
}
if (hash_byte_arr == nullptr) {
BOOST_THROW_EXCEPTION(runtime_error("Hash is null"));
}
obj = make_shared<signatures::Bls>(
signatures::Bls(requiredSigners, totalSigners));
std::pair<libff::alt_bn128_G1, std::string> hash_with_hint =
obj->HashtoG1withHint(hash_byte_arr);
int errStatus = 0;
string* xStr = stringFromFq(&(hash_with_hint.first.X));
if (xStr == nullptr) {
BOOST_THROW_EXCEPTION(runtime_error("Null xStr"));
}
string* yStr = stringFromFq(&(hash_with_hint.first.Y));
if (xStr == nullptr) {
BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
}
char errMsg[BUF_LEN];
memset(errMsg, 0, BUF_LEN);
char xStrArg[BUF_LEN];
char yStrArg[BUF_LEN];
char signature [BUF_LEN];
memset(xStrArg, 0, BUF_LEN);
memset(yStrArg, 0, BUF_LEN);
strncpy(xStrArg, xStr->c_str(), BUF_LEN);
strncpy(yStrArg, yStr->c_str(), BUF_LEN);
size_t sz = 0;
uint8_t encryptedKey[BUF_LEN];
bool result = hex2carray(encryptedKeyHex->c_str(), &sz, encryptedKey);
if (!result) {
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid hex encrypted key"));
}
cerr << "Key is " + *encryptedKeyHex << endl;
// sgx_status_t status =
// trustedBlsSignMessage(eid, &errStatus, errMsg, encryptedKey,
// encryptedKeyHex->size() / 2, xStrArg, yStrArg, signature);
strncpy(signature, "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855", 1024);
printf("---: %s\n", signature);
// if (status != SGX_SUCCESS) {
// gmp_printf("SGX enclave call to trustedBlsSignMessage failed: 0x%04x\n", status);
// BOOST_THROW_EXCEPTION(runtime_error("SGX enclave call to trustedBlsSignMessage failed"));
// }
// if (errStatus != 0) {
// BOOST_THROW_EXCEPTION(runtime_error("Enclave trustedBlsSignMessage failed:" + to_string(errStatus) + ":" + errMsg ));
// return nullptr;
// }
int sigLen;
if ((sigLen = strnlen(signature, 10)) < 10) {
BOOST_THROW_EXCEPTION(runtime_error("Signature too short:" + to_string(sigLen)));
}
std::string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" +
hash_with_hint.second;
auto sig = make_shared<string>(signature);
sig->append(":");
sig->append(hint);*/


std::string signature = signWithHelperSGXstr(hash_byte_arr, _signerIndex);


auto sig = make_shared<string>(signature);

//BLSSigShare* sig_test = new BLSSigShare(sig, _signerIndex, requiredSigners, totalSigners);

//std::string hello = "hello";
//std::cout << "HINT " << *((void**)&(sig_test->hint)) << std::endl;

//std::shared_ptr<BLSSigShare> s; s.reset( sig_test );//(sig, _signerIndex, requiredSigners,
//totalSigners);

std::shared_ptr<BLSSigShare> s = std::make_shared<BLSSigShare>(sig, _signerIndex, requiredSigners,
totalSigners);

return s;
}
}
3 changes: 1 addition & 2 deletions CSRManagerServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ shared_ptr<jsonrpc::HttpServer> CSRManagerServer::hs3 = nullptr;
CSRManagerServer::CSRManagerServer(AbstractServerConnector &connector,
serverVersion_t type) : abstractCSRManagerServer(connector, type) {}


Json::Value getUnsignedCSRsImpl() {
spdlog::info(__FUNCTION__);
INIT_RESULT(result)
Expand Down Expand Up @@ -137,4 +136,4 @@ int CSRManagerServer::initCSRManagerServer() {
spdlog::info("CSR manager server started on port {}", BASE_PORT + 2);
}
return 0;
};
};
4 changes: 0 additions & 4 deletions CSRManagerServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@
#include "abstractCSRManagerServer.h"
#include "LevelDB.h"




using namespace jsonrpc;
using namespace std;

class CSRManagerServer : public abstractCSRManagerServer {

recursive_mutex m;

static shared_ptr<HttpServer> hs3;
Expand Down
Loading

0 comments on commit baaf17c

Please sign in to comment.