Unverified Commit a2fdbcb7 authored by kladko's avatar kladko

SKALE-2341 Added tags for older commits

parent 4e26af8b
......@@ -55,76 +55,75 @@
#include "common.h"
std::string *FqToString(libff::alt_bn128_Fq*_fq) {
std::string *FqToString(libff::alt_bn128_Fq *_fq) {
mpz_t t;
mpz_init(t);
mpz_t t;
mpz_init(t);
_fq->as_bigint().to_mpz(t);
_fq->as_bigint().to_mpz(t);
char arr[mpz_sizeinbase(t, 10) + 2];
char arr[mpz_sizeinbase(t, 10) + 2];
char *tmp = mpz_get_str(arr, 10, t);
mpz_clear(t);
char *tmp = mpz_get_str(arr, 10, t);
mpz_clear(t);
return new std::string(tmp);
return new std::string(tmp);
}
int char2int(char _input) {
if (_input >= '0' && _input <= '9')
return _input - '0';
if (_input >= 'A' && _input <= 'F')
return _input - 'A' + 10;
if (_input >= 'a' && _input <= 'f')
return _input - 'a' + 10;
return -1;
if (_input >= '0' && _input <= '9')
return _input - '0';
if (_input >= 'A' && _input <= 'F')
return _input - 'A' + 10;
if (_input >= 'a' && _input <= 'f')
return _input - 'a' + 10;
return -1;
}
void carray2Hex(const unsigned char *d, int _len, char *_hexArray) {
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'};
char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
for (int j = 0; j < _len; j++) {
_hexArray[j * 2] = hexval[((d[j] >> 4) & 0xF)];
_hexArray[j * 2 + 1] = hexval[(d[j]) & 0x0F];
}
for (int j = 0; j < _len; j++) {
_hexArray[j * 2] = hexval[((d[j] >> 4) & 0xF)];
_hexArray[j * 2 + 1] = hexval[(d[j]) & 0x0F];
}
_hexArray[_len * 2] = 0;
_hexArray[_len * 2] = 0;
}
bool hex2carray(const char * _hex, uint64_t *_bin_len,
uint8_t* _bin ) {
bool hex2carray(const char *_hex, uint64_t *_bin_len,
uint8_t *_bin) {
int len = strnlen(_hex, 2 * BUF_LEN);
int len = strnlen(_hex, 2 * BUF_LEN);
if (len == 0 && len % 2 == 1)
return false;
if (len == 0 && len % 2 == 1)
return false;
*_bin_len = len / 2;
*_bin_len = len / 2;
for (int i = 0; i < len / 2; i++) {
int high = char2int((char)_hex[i * 2]);
int low = char2int((char)_hex[i * 2 + 1]);
for (int i = 0; i < len / 2; i++) {
int high = char2int((char) _hex[i * 2]);
int low = char2int((char) _hex[i * 2 + 1]);
if (high < 0 || low < 0) {
return false;
}
if (high < 0 || low < 0) {
return false;
}
_bin[i] = (unsigned char) (high * 16 + low);
}
_bin[i] = (unsigned char) (high * 16 + low);
}
return true;
return true;
}
bool hex2carray2(const char * _hex, uint64_t *_bin_len,
uint8_t* _bin, const int _max_length ) {
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);
......@@ -135,8 +134,8 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len,
*_bin_len = len / 2;
for (int i = 0; i < len / 2; i++) {
int high = char2int((char)_hex[i * 2]);
int low = char2int((char)_hex[i * 2 + 1]);
int high = char2int((char) _hex[i * 2]);
int low = char2int((char) _hex[i * 2 + 1]);
if (high < 0 || low < 0) {
return false;
......@@ -149,62 +148,62 @@ bool hex2carray2(const char * _hex, uint64_t *_bin_len,
}
bool sign(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t _n, size_t _signerIndex,
char* _sig) {
bool sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t _n, size_t _signerIndex,
char *_sig) {
//cerr << "ENTER SIGN" << endl;
//cerr << "ENTER SIGN" << endl;
auto keyStr = make_shared<string>(_encryptedKeyHex);
auto keyStr = make_shared<string>(_encryptedKeyHex);
auto hash = make_shared<array<uint8_t, 32>>();
auto hash = make_shared<array<uint8_t, 32>>();
uint64_t binLen;
uint64_t binLen;
if (!hex2carray(_hashHex, &binLen, hash->data())){
throw RPCException(INVALID_HEX, "Invalid hash");
}
// assert(binLen == hash->size());
if (!hex2carray(_hashHex, &binLen, hash->data())) {
throw RPCException(INVALID_HEX, "Invalid hash");
}
// assert(binLen == hash->size());
auto keyShare = make_shared<BLSPrivateKeyShareSGX>(keyStr, _t, _n);
auto keyShare = make_shared<BLSPrivateKeyShareSGX>(keyStr, _t, _n);
//cerr << "keyShare created" << endl;
// {
//cerr << "keyShare created" << endl;
// {
auto sigShare = keyShare->signWithHelperSGX(hash, _signerIndex);
// }
// }
auto sigShareStr = sigShare->toString();
auto sigShareStr = sigShare->toString();
strncpy(_sig, sigShareStr->c_str(), BUF_LEN);
strncpy(_sig, sigShareStr->c_str(), BUF_LEN);
//string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
//strncpy(_sig, sigShareStr.c_str(), BUF_LEN);
//string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
//strncpy(_sig, sigShareStr.c_str(), BUF_LEN);
// string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
// auto sig_ptr = make_shared<string>(test_sig);
// strncpy(_sig, sig_ptr->c_str(), BUF_LEN);
// string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
// auto sig_ptr = make_shared<string>(test_sig);
// strncpy(_sig, sig_ptr->c_str(), BUF_LEN);
//cerr<< "sig " << _sig <<endl;
//cerr<< "sig " << _sig <<endl;
return true;
return true;
}
bool sign_aes(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t _n, size_t _signerIndex,
char* _sig) {
bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t _n, size_t _signerIndex,
char *_sig) {
//cerr << "ENTER SIGN" << endl;
//cerr << "ENTER SIGN" << endl;
auto keyStr = make_shared<string>(_encryptedKeyHex);
auto keyStr = make_shared<string>(_encryptedKeyHex);
auto hash = make_shared<array<uint8_t, 32>>();
auto hash = make_shared<array<uint8_t, 32>>();
uint64_t binLen;
uint64_t binLen;
if (!hex2carray(_hashHex, &binLen, hash->data())){
throw RPCException(INVALID_HEX, "Invalid hash");
}
// assert(binLen == hash->size());
if (!hex2carray(_hashHex, &binLen, hash->data())) {
throw RPCException(INVALID_HEX, "Invalid hash");
}
// assert(binLen == hash->size());
......@@ -218,103 +217,102 @@ bool sign_aes(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, siz
// auto sigShareStr = sigShare->toString();
//
// strncpy(_sig, sigShareStr->c_str(), BUF_LEN);
shared_ptr<signatures::Bls> obj;
obj = make_shared<signatures::Bls>(signatures::Bls(_t, _n));
shared_ptr<signatures::Bls> obj;
obj = make_shared<signatures::Bls>(signatures::Bls(_t, _n));
std::pair<libff::alt_bn128_G1, std::string> hash_with_hint =
obj->HashtoG1withHint(hash);
std::pair<libff::alt_bn128_G1, std::string> hash_with_hint =
obj->HashtoG1withHint(hash);
int errStatus = 0;
int errStatus = 0;
string* xStr = FqToString(&(hash_with_hint.first.X));
string *xStr = FqToString(&(hash_with_hint.first.X));
if (xStr == nullptr) {
std::cerr << "Null xStr" << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("Null xStr"));
}
if (xStr == nullptr) {
std::cerr << "Null xStr" << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("Null xStr"));
}
string* yStr = FqToString(&(hash_with_hint.first.Y));
string *yStr = FqToString(&(hash_with_hint.first.Y));
if (yStr == nullptr) {
std::cerr << "Null yStr" << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
}
if (yStr == nullptr) {
std::cerr << "Null yStr" << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("Null yStr"));
}
char errMsg[BUF_LEN];
memset(errMsg, 0, BUF_LEN);
char errMsg[BUF_LEN];
memset(errMsg, 0, BUF_LEN);
char xStrArg[BUF_LEN];
char yStrArg[BUF_LEN];
char signature [BUF_LEN];
char xStrArg[BUF_LEN];
char yStrArg[BUF_LEN];
char signature[BUF_LEN];
memset(xStrArg, 0, BUF_LEN);
memset(yStrArg, 0, 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);
strncpy(xStrArg, xStr->c_str(), BUF_LEN);
strncpy(yStrArg, yStr->c_str(), BUF_LEN);
size_t sz = 0;
size_t sz = 0;
uint8_t encryptedKey[BUF_LEN];
uint8_t encryptedKey[BUF_LEN];
bool result = hex2carray(_encryptedKeyHex, &sz, encryptedKey);
bool result = hex2carray(_encryptedKeyHex, &sz, encryptedKey);
if (!result) {
cerr << "Invalid hex encrypted key" << endl;
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid hex encrypted key"));
}
if (!result) {
cerr << "Invalid hex encrypted key" << endl;
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid hex encrypted key"));
}
sgx_status_t status =
bls_sign_message_aes(eid, &errStatus, errMsg, encryptedKey,
sz, xStrArg, yStrArg, signature);
sgx_status_t status =
bls_sign_message_aes(eid, &errStatus, errMsg, encryptedKey,
sz, xStrArg, yStrArg, signature);
if (status != SGX_SUCCESS) {
cerr <<"SGX enclave call to bls_sign_message failed:" << status << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("SGX enclave call to bls_sign_message failed"));
}
if (status != SGX_SUCCESS) {
cerr << "SGX enclave call to bls_sign_message failed:" << status << std::endl;
BOOST_THROW_EXCEPTION(runtime_error("SGX enclave call to bls_sign_message failed"));
}
std::string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" +
hash_with_hint.second;
std::string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" +
hash_with_hint.second;
std::string sig = signature;
std::string sig = signature;
sig.append(":");
sig.append(hint);
sig.append(":");
sig.append(hint);
strncpy(_sig, sig.c_str(), BUF_LEN);
strncpy(_sig, sig.c_str(), BUF_LEN);
printf("_sig is: %s\n", sig.c_str());
printf("_sig is: %s\n", sig.c_str());
//string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
//strncpy(_sig, sigShareStr.c_str(), BUF_LEN);
//string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
//strncpy(_sig, sigShareStr.c_str(), BUF_LEN);
// string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
// auto sig_ptr = make_shared<string>(test_sig);
// strncpy(_sig, sig_ptr->c_str(), BUF_LEN);
// string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
// auto sig_ptr = make_shared<string>(test_sig);
// strncpy(_sig, sig_ptr->c_str(), BUF_LEN);
//cerr<< "sig " << _sig <<endl;
//cerr<< "sig " << _sig <<endl;
return true;
return true;
}
bool bls_sign(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t _n, size_t _signerIndex,
char* _sig) {
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);
}
if (!encryptKeys) {
return sign(_encryptedKeyHex, _hashHex, _t, _n, _signerIndex, _sig);
} else {
return sign_aes(_encryptedKeyHex, _hashHex, _t, _n, _signerIndex, _sig);
}
}
char* encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) {
char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) {
auto keyArray = make_shared<vector<char>>(BUF_LEN, 0);
auto encryptedKey = make_shared<vector<uint8_t>>(BUF_LEN, 0);
auto errMsg = make_shared<vector<char>>(BUF_LEN, 0);
......@@ -326,10 +324,9 @@ char* encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key)
//status = encrypt_key(eid, errStatus, errMsg, keyArray, encryptedKey, &encryptedLen);
status = encrypt_key_aes(eid, errStatus, errMsg->data(), keyArray->data(), encryptedKey->data(), &encryptedLen);
if (printDebugInfo) {
spdlog::debug("errStatus is {}",*errStatus);
spdlog::debug(" errMsg is ", errMsg->data() );
}
spdlog::debug("errStatus is {}", *errStatus);
spdlog::debug(" errMsg is ", errMsg->data());
if (status != SGX_SUCCESS) {
......@@ -365,7 +362,7 @@ char *decryptBLSKeyShareFromHex(int *errStatus, char *errMsg, const char *_encry
char *plaintextKey = (char *) calloc(BUF_LEN, 1);
//status = decrypt_key(eid, errStatus, errMsg, decoded, decodedLen, plaintextKey);
status = decrypt_key_aes(eid, errStatus, errMsg, decoded, decodedLen, plaintextKey);
status = decrypt_key_aes(eid, errStatus, errMsg, decoded, decodedLen, plaintextKey);
if (status != SGX_SUCCESS) {
return nullptr;
......
......@@ -36,46 +36,44 @@
#include "spdlog/spdlog.h"
#include "common.h"
vector<string> SplitString(const char* koefs, const char symbol){
string str(koefs);
string delim;
delim.push_back(symbol);
vector<string> G2_strings;
size_t prev = 0, pos = 0;
do
{
pos = str.find(delim, prev);
if (pos == string::npos) pos = str.length();
string token = str.substr(prev, pos-prev);
if (!token.empty()) {
string koef(token.c_str());
G2_strings.push_back(koef);
}
prev = pos + delim.length();
}
while (pos < str.length() && prev < str.length());
return G2_strings;
vector<string> SplitString(const char *koefs, const char symbol) {
string str(koefs);
string delim;
delim.push_back(symbol);
vector<string> G2_strings;
size_t prev = 0, pos = 0;
do {
pos = str.find(delim, prev);
if (pos == string::npos) pos = str.length();
string token = str.substr(prev, pos - prev);
if (!token.empty()) {
string koef(token.c_str());
G2_strings.push_back(koef);
}
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());
return G2_strings;
}
template<class T>
string ConvertToString(T field_elem, int base = 10) {
mpz_t t;
mpz_init(t);
mpz_t t;
mpz_init(t);
field_elem.as_bigint().to_mpz(t);
field_elem.as_bigint().to_mpz(t);
char arr[mpz_sizeinbase (t, base) + 2];
char arr[mpz_sizeinbase(t, base) + 2];
char * tmp = mpz_get_str(arr, base, t);
mpz_clear(t);
char *tmp = mpz_get_str(arr, base, t);
mpz_clear(t);
string output = tmp;
string output = tmp;
return output;
return output;
}
string gen_dkg_poly( int _t){
string gen_dkg_poly(int _t) {
vector<char> errMsg(1024, 0);
int err_status = 0;
......@@ -84,27 +82,25 @@ string gen_dkg_poly( int _t){
uint32_t enc_len = 0;
if (!encryptKeys)
status = gen_dkg_secret (eid, &err_status, errMsg.data(), encrypted_dkg_secret.data(), &enc_len, _t);
status = gen_dkg_secret(eid, &err_status, errMsg.data(), encrypted_dkg_secret.data(), &enc_len, _t);
else
status = gen_dkg_secret_aes (eid, &err_status, errMsg.data(), encrypted_dkg_secret.data(), &enc_len, _t);
if ( err_status != 0){
throw RPCException(-666, errMsg.data() ) ;
status = gen_dkg_secret_aes(eid, &err_status, errMsg.data(), encrypted_dkg_secret.data(), &enc_len, _t);
if (err_status != 0) {
throw RPCException(-666, errMsg.data());
}
if (printDebugInfo) {
spdlog::debug("gen_dkg_secret, status {}", err_status, " err msg ", errMsg.data());
spdlog::debug("in DKGCrypto encr len is {}", enc_len);
}
spdlog::debug("gen_dkg_secret, status {}", err_status, " err msg ", errMsg.data());
spdlog::debug("in DKGCrypto encr len is {}", enc_len);
uint64_t length = DKG_MAX_SEALED_LEN;
if (encryptKeys){
length = enc_len;
if (encryptKeys) {
length = enc_len;
}
//vector<char> hexEncrPoly(DKG_MAX_SEALED_LEN * 2 + 1, 0);//(4*BUF_LEN, 1);
//vector<char> hexEncrPoly(DKG_MAX_SEALED_LEN * 2 + 1, 0);//(4*BUF_LEN, 1);
vector<char> hexEncrPoly(2 * length + 1, 0);
assert( encrypted_dkg_secret.size() >= length);
assert(encrypted_dkg_secret.size() >= length);
//carray2Hex(encrypted_dkg_secret.data(), DKG_MAX_SEALED_LEN, hexEncrPoly.data());
carray2Hex(encrypted_dkg_secret.data(), length, hexEncrPoly.data());
string result(hexEncrPoly.data());
......@@ -112,343 +108,315 @@ string gen_dkg_poly( int _t){
return result;
}
vector <vector<string>> get_verif_vect(const char* encryptedPolyHex, int t, int n){
vector<vector<string>> get_verif_vect(const char *encryptedPolyHex, int t, int n) {
char* errMsg1 = (char*) calloc(1024,1);
//char errMsg1[BUF_LEN];
int err_status = 0;
char *errMsg1 = (char *) calloc(1024, 1);
//char errMsg1[BUF_LEN];
int err_status = 0;
if (printDebugInfo) {
// cerr << "got encr poly " << encryptedPolyHex << endl;
spdlog::debug("got encr poly size {}", char_traits<char>::length(encryptedPolyHex));
}
char* public_shares = (char*)calloc(10000, 1);
memset(public_shares, 0, 10000);
// char public_shares[10000];
uint64_t enc_len = 0;
char *public_shares = (char *) calloc(10000, 1);
memset(public_shares, 0, 10000);
// char public_shares[10000];
uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN * 2, 1);
memset(encr_dkg_poly, 0, DKG_MAX_SEALED_LEN * 2);
//uint8_t encr_dkg_poly[DKG_MAX_SEALED_LEN];
uint64_t enc_len = 0;
if (!hex2carray2(encryptedPolyHex, &enc_len, encr_dkg_poly, 6100)){
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
uint8_t *encr_dkg_poly = (uint8_t *) calloc(DKG_MAX_SEALED_LEN * 2, 1);
memset(encr_dkg_poly, 0, DKG_MAX_SEALED_LEN * 2);
//uint8_t encr_dkg_poly[DKG_MAX_SEALED_LEN];
if (!hex2carray2(encryptedPolyHex, &enc_len, encr_dkg_poly, 6100)) {
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
if (printDebugInfo) {
//cerr << "hex_encr_poly is " << encryptedPolyHex << std::endl;
spdlog::debug("hex_encr_poly length is {}", strlen(encryptedPolyHex));
spdlog::debug("enc len {}", enc_len);
// cerr << "encr raw poly: " << endl;
// for ( int i = 0 ; i < 3050; i++)
// printf(" %d ", encr_dkg_poly[i] );
}
uint32_t len = 0;
if (!encryptKeys)
status = get_public_shares(eid, &err_status, errMsg1, encr_dkg_poly, len, public_shares, t, n);
else {
uint32_t len = 0;
if (!encryptKeys)
status = get_public_shares(eid, &err_status, errMsg1, encr_dkg_poly, len, public_shares, t, n);
else {
status = get_public_shares_aes(eid, &err_status, errMsg1, encr_dkg_poly, enc_len, public_shares, t, n);
}
if (err_status != 0) {
throw RPCException(-666, errMsg1);
}
status = get_public_shares_aes(eid, &err_status, errMsg1, encr_dkg_poly, enc_len, public_shares, t, n);
}
if ( err_status != 0){
throw RPCException(-666, errMsg1 );
}
if (printDebugInfo) {
spdlog::debug("err msg is {}", errMsg1);
spdlog::debug("public_shares:");
spdlog::debug("{}", public_shares);
// cerr << "public_shares:" << endl;
// cerr << public_shares << endl;
spdlog::debug("{}", public_shares);;
spdlog::debug("get_public_shares status: {}", err_status);
//printf("\nget_public_shares status: %d error %s \n\n", err_status, errMsg1);
}
vector <string> G2_strings = SplitString( public_shares, ',');
vector <vector <string>> pub_shares_vect;
for ( uint64_t i = 0; i < G2_strings.size(); i++){
vector <string> koef_str = SplitString(G2_strings.at(i).c_str(), ':');
pub_shares_vect.push_back(koef_str);
}
vector<string> G2_strings = SplitString(public_shares, ',');
vector<vector<string>> pub_shares_vect;
for (uint64_t i = 0; i < G2_strings.size(); i++) {
vector<string> koef_str = SplitString(G2_strings.at(i).c_str(), ':');
pub_shares_vect.push_back(koef_str);
}
free(errMsg1);
free(public_shares);
free(encr_dkg_poly);
free(errMsg1);
free(public_shares);
free(encr_dkg_poly);
return pub_shares_vect;
return pub_shares_vect;
}
string get_secret_shares(const string& polyName, const char* encryptedPolyHex, const vector<string>& publicKeys, int t, int n){
//char* errMsg1 = (char*) calloc(1024,1);
char errMsg1[BUF_LEN];
int err_status = 0;
char hexEncrKey[BUF_LEN];
memset(hexEncrKey, 0, BUF_LEN);
uint64_t enc_len = 0;
// uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN, 1);
uint8_t encr_dkg_poly[DKG_MAX_SEALED_LEN];
memset(encr_dkg_poly, 0, DKG_MAX_SEALED_LEN);
if(!hex2carray2(encryptedPolyHex, &enc_len, encr_dkg_poly, 6100)){
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
std::cerr << "enc_len is " << enc_len << std::endl;
if (!encryptKeys)
status = set_encrypted_dkg_poly(eid, &err_status, errMsg1, encr_dkg_poly);
else
status = set_encrypted_dkg_poly_aes(eid, &err_status, errMsg1, encr_dkg_poly, &enc_len);
if ( status != SGX_SUCCESS || err_status != 0){
throw RPCException(-666, errMsg1 );
}
string result;
//char *hexEncrKey = (char *) calloc(2 * BUF_LEN, 1);
for ( int i = 0; i < n; i++){
uint8_t encryptedSkey[BUF_LEN];
memset(encryptedSkey, 0, BUF_LEN);
uint32_t dec_len;
char cur_share[193];
char s_shareG2[320];
string pub_keyB = publicKeys.at(i);//publicKeys.substr(128*i, 128*i + 128);
// if (DEBUG_PRINT) {
// spdlog::info("pub_keyB is {}", pub_keyB);
// }
char pubKeyB[129];
strncpy(pubKeyB, pub_keyB.c_str(), 128);
pubKeyB[128] = 0;
if (printDebugInfo) {
spdlog::debug("pubKeyB is {}", pub_keyB);
string get_secret_shares(const string &polyName, const char *encryptedPolyHex, const vector<string> &publicKeys, int t,
int n) {
//char* errMsg1 = (char*) calloc(1024,1);
char errMsg1[BUF_LEN];
int err_status = 0;
char hexEncrKey[BUF_LEN];
memset(hexEncrKey, 0, BUF_LEN);
uint64_t enc_len = 0;
// uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN, 1);
uint8_t encr_dkg_poly[DKG_MAX_SEALED_LEN];
memset(encr_dkg_poly, 0, DKG_MAX_SEALED_LEN);
if (!hex2carray2(encryptedPolyHex, &enc_len, encr_dkg_poly, 6100)) {
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
std::cerr << "enc_len is " << enc_len << std::endl;
if (!encryptKeys)
get_encr_sshare(eid, &err_status, errMsg1, encryptedSkey, &dec_len,
cur_share, s_shareG2, pubKeyB, t, n, i + 1 );
status = set_encrypted_dkg_poly(eid, &err_status, errMsg1, encr_dkg_poly);
else
get_encr_sshare_aes(eid, &err_status, errMsg1, encryptedSkey, &dec_len,
cur_share, s_shareG2, pubKeyB, t, n, i + 1 );
if (err_status != 0){
throw RPCException(-666, errMsg1);
}
if (printDebugInfo) {
spdlog::debug("cur_share is {}", cur_share);
status = set_encrypted_dkg_poly_aes(eid, &err_status, errMsg1, encr_dkg_poly, &enc_len);
if (status != SGX_SUCCESS || err_status != 0) {
throw RPCException(-666, errMsg1);
}
result += cur_share;
string result;
//char *hexEncrKey = (char *) calloc(2 * BUF_LEN, 1);
//uint32_t enc_len = BUF_LEN;
if (printDebugInfo) {
spdlog::debug("dec len is {}", dec_len);
}
for (int i = 0; i < n; i++) {
uint8_t encryptedSkey[BUF_LEN];
memset(encryptedSkey, 0, BUF_LEN);
uint32_t dec_len;
char cur_share[193];
char s_shareG2[320];
string pub_keyB = publicKeys.at(i);//publicKeys.substr(128*i, 128*i + 128);
// if (DEBUG_PRINT) {
// spdlog::info("pub_keyB is {}", pub_keyB);
// }
char pubKeyB[129];
strncpy(pubKeyB, pub_keyB.c_str(), 128);
pubKeyB[128] = 0;
carray2Hex(encryptedSkey, dec_len, hexEncrKey);
spdlog::debug("pubKeyB is {}", pub_keyB);
string dhKeyName = "DKG_DH_KEY_" + polyName + "_" + to_string(i) + ":";
if (!encryptKeys)
get_encr_sshare(eid, &err_status, errMsg1, encryptedSkey, &dec_len,
cur_share, s_shareG2, pubKeyB, t, n, i + 1);
else
get_encr_sshare_aes(eid, &err_status, errMsg1, encryptedSkey, &dec_len,
cur_share, s_shareG2, pubKeyB, t, n, i + 1);
if (err_status != 0) {
throw RPCException(-666, errMsg1);
}
spdlog::debug("hexEncr DH Key: { }" , hexEncrKey);
SGXWalletServer::writeDataToDB(dhKeyName, hexEncrKey);
spdlog::debug("cur_share is {}", cur_share);
string shareG2_name = "shareG2_" + polyName + "_" + to_string(i) + ":";
if (printDebugInfo) {
spdlog::debug("name to write to db is {}", dhKeyName);
spdlog::debug("name to write to db is {}", shareG2_name);
spdlog::debug("s_shareG2: {}", s_shareG2);
}
SGXWalletServer::writeDataToDB(shareG2_name, s_shareG2);
if (printDebugInfo) {
spdlog::debug("errMsg: {}", errMsg1);
// cerr << "iteration " << i <<" result length is " << result.length() << endl ;
// cerr << "iteration " << i <<" share length is " << strlen(cur_share) << endl;
// cerr << "iteration " << i <<" share is " << cur_share << endl;
result += cur_share;
spdlog::debug("dec len is {}", dec_len);
carray2Hex(encryptedSkey, dec_len, hexEncrKey);
string dhKeyName = "DKG_DH_KEY_" + polyName + "_" + to_string(i) + ":";
spdlog::debug("hexEncr DH Key: { }", hexEncrKey);
SGXWalletServer::writeDataToDB(dhKeyName, hexEncrKey);
string shareG2_name = "shareG2_" + polyName + "_" + to_string(i) + ":";
spdlog::debug("name to write to db is {}", dhKeyName);
spdlog::debug("name to write to db is {}", shareG2_name);
spdlog::debug("s_shareG2: {}", s_shareG2);
SGXWalletServer::writeDataToDB(shareG2_name, s_shareG2);
spdlog::debug("errMsg: {}", errMsg1);
}
}
//result += '\0';
//result += '\0';
//free(encr_dkg_poly);
// free(errMsg1);
//free(hexEncrKey);
//free(encr_dkg_poly);
// free(errMsg1);
//free(hexEncrKey);
return result;
return result;
}
bool VerifyShares(const char* publicShares, const char* encr_sshare, const char * encryptedKeyHex, int t, int n, int ind ){
bool
VerifyShares(const char *publicShares, const char *encr_sshare, const char *encryptedKeyHex, int t, int n, int ind) {
//char* errMsg1 = (char*) calloc(1024,1);
char errMsg1[BUF_LEN];
int err_status = 0;
uint64_t dec_key_len ;
uint64_t dec_key_len;
uint8_t encr_key[BUF_LEN];
memset(encr_key, 0, BUF_LEN);
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)){
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)) {
throw RPCException(INVALID_HEX, "Invalid encryptedPolyHex");
}
int result;
if (printDebugInfo) {
cerr << "encryptedKeyHex " << encryptedKeyHex << endl;
cerr << "dec_key_len " << dec_key_len << endl;
cerr << "encr_sshare length is " << strlen(encr_sshare) << endl;
//cerr << "public shares " << publicShares << endl;
spdlog::debug("publicShares length is {}", char_traits<char>::length(publicShares));
}
cerr << "encryptedKeyHex " << encryptedKeyHex << endl;
cerr << "dec_key_len " << dec_key_len << endl;
cerr << "encr_sshare length is " << strlen(encr_sshare) << endl;
//cerr << "public shares " << publicShares << endl;
spdlog::debug("publicShares length is {}", char_traits<char>::length(publicShares));
char pshares[8193];
memset(pshares, 0, 8193);
strncpy(pshares, publicShares, strlen(publicShares) );
strncpy(pshares, publicShares, strlen(publicShares));
if (!encryptKeys)
dkg_verification(eid, &err_status, errMsg1, pshares, encr_sshare, encr_key, dec_key_len, t, ind, &result);
dkg_verification(eid, &err_status, errMsg1, pshares, encr_sshare, encr_key, dec_key_len, t, ind, &result);
else
dkg_verification_aes(eid, &err_status, errMsg1, pshares, encr_sshare, encr_key, dec_key_len, t, ind, &result);
dkg_verification_aes(eid, &err_status, errMsg1, pshares, encr_sshare, encr_key, dec_key_len, t, ind, &result);
if (result == 2){
throw RPCException(INVALID_HEX, "Invalid public shares");
if (result == 2) {
throw RPCException(INVALID_HEX, "Invalid public shares");
}
if (printDebugInfo) {
spdlog::debug("errMsg1: {}", errMsg1);
spdlog::debug("result is: {}", result);
}
spdlog::debug("errMsg1: {}", errMsg1);
spdlog::debug("result is: {}", result);
//free(errMsg1);
return result;
}
bool CreateBLSShare( const string& blsKeyName, const char * s_shares, const char * encryptedKeyHex){
if (printDebugInfo) {
bool CreateBLSShare(const string &blsKeyName, const char *s_shares, const char *encryptedKeyHex) {
spdlog::debug("ENTER CreateBLSShare");
}
// char* errMsg1 = (char*) calloc(1024,1);
char errMsg1[BUF_LEN];
int err_status = 0;
uint64_t dec_key_len ;
uint8_t encr_bls_key[BUF_LEN];
memset(encr_bls_key, 0, BUF_LEN);
uint8_t encr_key[BUF_LEN];
memset(encr_key, 0, BUF_LEN);
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)){
throw RPCException(INVALID_HEX, "Invalid encryptedKeyHex");
}
uint32_t enc_bls_len = 0;
//cerr << "BEFORE create_bls_key IN ENCLAVE " << endl;
if (!encryptKeys)
create_bls_key(eid, &err_status, errMsg1, s_shares, encr_key, dec_key_len, encr_bls_key, &enc_bls_len);
else
create_bls_key_aes(eid, &err_status, errMsg1, s_shares, encr_key, dec_key_len, encr_bls_key, &enc_bls_len);
//cerr << "AFTER create_bls_key IN ENCLAVE er msg is " << errMsg1 << endl;
if ( err_status != 0){
//spdlog::info("ERROR IN ENCLAVE with status {}", err_status);
spdlog::error(errMsg1);
spdlog::error("status {}", err_status);
throw RPCException(ERROR_IN_ENCLAVE, "Create BLS private key failed in enclave");
}
else {
//char *hexBLSKey = (char *) calloc(2 * BUF_LEN, 1);
char hexBLSKey[2 * BUF_LEN];
//cerr << "BEFORE carray2Hex" << endl;
//cerr << "enc_bls_len " << enc_bls_len << endl;
carray2Hex(encr_bls_key, enc_bls_len, hexBLSKey);
// cerr << "BEFORE WRITE BLS KEY TO DB" << endl;
SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey);
if (printDebugInfo) {
spdlog::debug("hexBLSKey length is {}", char_traits<char>::length(hexBLSKey));
spdlog::debug("bls key {}", blsKeyName, " is ", hexBLSKey );
// char* errMsg1 = (char*) calloc(1024,1);
char errMsg1[BUF_LEN];
int err_status = 0;
uint64_t dec_key_len;
uint8_t encr_bls_key[BUF_LEN];
memset(encr_bls_key, 0, BUF_LEN);
uint8_t encr_key[BUF_LEN];
memset(encr_key, 0, BUF_LEN);
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)) {
throw RPCException(INVALID_HEX, "Invalid encryptedKeyHex");
}
uint32_t enc_bls_len = 0;
//cerr << "BEFORE create_bls_key IN ENCLAVE " << endl;
if (!encryptKeys)
create_bls_key(eid, &err_status, errMsg1, s_shares, encr_key, dec_key_len, encr_bls_key, &enc_bls_len);
else
create_bls_key_aes(eid, &err_status, errMsg1, s_shares, encr_key, dec_key_len, encr_bls_key, &enc_bls_len);
//cerr << "AFTER create_bls_key IN ENCLAVE er msg is " << errMsg1 << endl;
if (err_status != 0) {
//spdlog::info("ERROR IN ENCLAVE with status {}", err_status);
spdlog::error(errMsg1);
spdlog::error("status {}", err_status);
throw RPCException(ERROR_IN_ENCLAVE, "Create BLS private key failed in enclave");
} else {
//char *hexBLSKey = (char *) calloc(2 * BUF_LEN, 1);
char hexBLSKey[2 * BUF_LEN];
//cerr << "BEFORE carray2Hex" << endl;
//cerr << "enc_bls_len " << enc_bls_len << endl;
carray2Hex(encr_bls_key, enc_bls_len, hexBLSKey);
// cerr << "BEFORE WRITE BLS KEY TO DB" << endl;
SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey);
spdlog::debug("hexBLSKey length is {}", char_traits<char>::length(hexBLSKey));
spdlog::debug("bls key {}", blsKeyName, " is ", hexBLSKey);
//free(hexBLSKey);
return true;
}
//free(hexBLSKey);
return true;
}
}
vector<string> GetBLSPubKey(const char * encryptedKeyHex){
vector<string> GetBLSPubKey(const char *encryptedKeyHex) {
//char* errMsg1 = (char*) calloc(1024,1);
char errMsg1[BUF_LEN];
int err_status = 0;
uint64_t dec_key_len ;
uint64_t dec_key_len;
uint8_t encr_key[BUF_LEN];
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)){
if (!hex2carray(encryptedKeyHex, &dec_key_len, encr_key)) {
throw RPCException(INVALID_HEX, "Invalid encryptedKeyHex");
}
char pub_key[320];
if (printDebugInfo) {
spdlog::debug("dec_key_len is {}", dec_key_len);
}
spdlog::debug("dec_key_len is {}", dec_key_len);
if (!encryptKeys)
get_bls_pub_key(eid, &err_status, errMsg1, encr_key, dec_key_len, pub_key);
get_bls_pub_key(eid, &err_status, errMsg1, encr_key, dec_key_len, pub_key);
else
get_bls_pub_key_aes(eid, &err_status, errMsg1, encr_key, dec_key_len, pub_key);
if ( err_status != 0){
std::cerr << errMsg1 << " status is " << err_status << std::endl;
throw RPCException(ERROR_IN_ENCLAVE, "Failed to get BLS public key in enclave");
get_bls_pub_key_aes(eid, &err_status, errMsg1, encr_key, dec_key_len, pub_key);
if (err_status != 0) {
std::cerr << errMsg1 << " status is " << err_status << std::endl;
throw RPCException(ERROR_IN_ENCLAVE, "Failed to get BLS public key in enclave");
}
vector<string> pub_key_vect = SplitString(pub_key, ':');
if (printDebugInfo) {
spdlog::debug("errMsg1 is {}", errMsg1);
spdlog::debug("pub key is ");
for (int i = 0; i < 4; i++)
spdlog::debug("errMsg1 is {}", errMsg1);
spdlog::debug("pub key is ");
for (int i = 0; i < 4; i++)
spdlog::debug("{}", pub_key_vect.at(i));
}
return pub_key_vect;
}
string decrypt_DHKey(const string& polyName, int ind){
string decrypt_DHKey(const string &polyName, int ind) {
vector<char> errMsg1(1024,0);
int err_status = 0;
vector<char> errMsg1(1024, 0);
int err_status = 0;
string DH_key_name = polyName + "_" + to_string(ind) + ":";
shared_ptr<string> hexEncrKey_ptr = SGXWalletServer::readFromDb(DH_key_name, "DKG_DH_KEY_");
string DH_key_name = polyName + "_" + to_string(ind) + ":";
shared_ptr<string> hexEncrKey_ptr = SGXWalletServer::readFromDb(DH_key_name, "DKG_DH_KEY_");
if (printDebugInfo) {
spdlog::debug("encr DH key is {}", *hexEncrKey_ptr);
}
vector<char> hexEncrKey(2 * BUF_LEN, 0);
vector<char> hexEncrKey(2 * BUF_LEN, 0);
uint64_t DH_enc_len = 0;
uint8_t encrypted_DHkey[BUF_LEN];
if (!hex2carray(hexEncrKey_ptr->c_str(), &DH_enc_len, encrypted_DHkey)){
throw RPCException(INVALID_HEX, "Invalid hexEncrKey");
}
if (printDebugInfo) {
uint64_t DH_enc_len = 0;
uint8_t encrypted_DHkey[BUF_LEN];
if (!hex2carray(hexEncrKey_ptr->c_str(), &DH_enc_len, encrypted_DHkey)) {
throw RPCException(INVALID_HEX, "Invalid hexEncrKey");
}
spdlog::debug("encr DH key length is {}", DH_enc_len);
spdlog::debug("hex encr DH key length is {}", hexEncrKey_ptr->length());
}
char DHKey[ECDSA_SKEY_LEN];
if ( !encryptKeys)
decrypt_key(eid, &err_status, errMsg1.data(), encrypted_DHkey, DH_enc_len, DHKey);
else
decrypt_key_aes(eid, &err_status, errMsg1.data(), encrypted_DHkey, DH_enc_len, DHKey);
if (err_status != 0){
throw RPCException(/*ERROR_IN_ENCLAVE*/ err_status, "decrypt key failed in enclave");
}
char DHKey[ECDSA_SKEY_LEN];
if (!encryptKeys)
decrypt_key(eid, &err_status, errMsg1.data(), encrypted_DHkey, DH_enc_len, DHKey);
else
decrypt_key_aes(eid, &err_status, errMsg1.data(), encrypted_DHkey, DH_enc_len, DHKey);
if (err_status != 0) {
throw RPCException(/*ERROR_IN_ENCLAVE*/ err_status, "decrypt key failed in enclave");
}
return DHKey;
return DHKey;
}
vector<string> mult_G2(const string& x){
vector<string> mult_G2(const string &x) {
vector<string> result(4);
libff::init_alt_bn128_params();
libff::alt_bn128_Fr el(x.c_str());
......
......@@ -61,13 +61,10 @@ std::vector<std::string> genECDSAKey() {
throw RPCException(status, errMsg);
}
std::vector<std::string> keys(3);
if (printDebugInfo) {
std::cerr << "account key is " << errMsg << std::endl;
std::cerr << "enc_len is " << enc_len << std::endl;
// std::cerr << "enc_key is " << std::endl;
// for(int i = 0 ; i < 1024; i++)
// std::cerr << (int)encr_pr_key[i] << " " ;
}
char *hexEncrKey = (char *) calloc(BUF_LEN * 2, 1);
carray2Hex(encr_pr_key, enc_len, hexEncrKey);
keys.at(0) = hexEncrKey;
......@@ -77,10 +74,8 @@ std::vector<std::string> genECDSAKey() {
unsigned long seed = randGen();
if (printDebugInfo) {
spdlog::debug("seed is {}", seed);
std::cerr << "strlen is " << strlen(hexEncrKey) << std::endl;
}
gmp_randstate_t state;
gmp_randinit_default(state);
......@@ -133,13 +128,12 @@ std::string getECDSAPubKey(const char *_encryptedKeyHex) {
}
string pubKey = string(pubKeyX.data()) + string(pubKeyY.data());//concatPubKeyWith0x(pub_key_x, pub_key_y);//
if (printDebugInfo) {
spdlog::debug("enc_len is {}", enc_len);
spdlog::debug("pubkey is {}", pubKey);
spdlog::debug("pubkey length is {}", pubKey.length());
spdlog::debug("err str is {}", errMsg.data());
spdlog::debug("err status is {}", err_status);
}
return pubKey;
}
......
......@@ -62,10 +62,10 @@ std::shared_ptr<string> LevelDB::readString(const string &_key) {
auto status = db->Get(readOptions, _key, &*result);
if (printDebugInfo) {
spdlog::debug("key to read from db: {}",_key );
//std::cerr << "key to read from db: " << _key << std::endl;
}
throwExceptionOnError(status);
......@@ -83,10 +83,10 @@ void LevelDB::writeString(const string &_key, const string &_value) {
throwExceptionOnError(status);
if (printDebugInfo) {
spdlog::debug("written key: {}",_key );
// std::cerr << "written key " << _key << std::endl;
}
}
......@@ -100,10 +100,9 @@ void LevelDB::deleteDHDKGKey (const string &_key) {
throwExceptionOnError(status);
if (printDebugInfo) {
spdlog::debug("key deleted: {}",full_key );
//std::cerr << "key deleted " << full_key << std::endl;
}
}
void LevelDB::deleteTempNEK(const string &_key){
......@@ -130,10 +129,9 @@ void LevelDB::deleteKey(const string &_key){
throwExceptionOnError(status);
if (printDebugInfo) {
spdlog::debug("key deleted: {}",_key );
// std::cerr << "key deleted " << _key << std::endl;
}
}
......@@ -219,10 +217,9 @@ void LevelDB::writeDataUnique(const string & Name, const string &value) {
}
writeString(key, value);
if (printDebugInfo) {
spdlog::debug("{}",Name, " is written to db");
//std::cerr << Name << " is written to db " << std::endl;
}
}
......
......@@ -231,8 +231,6 @@ void init_SEK(){
gen_SEK();
}
else{
if (printDebugInfo)
spdlog::info("going to set SEK from db" );
set_SEK(encr_SEK_ptr);
}
}
......
......@@ -77,9 +77,8 @@ void initEnclave() {
}
#endif
if (printDebugInfo) {
spdlog::debug("SGX_DEBUG_FLAG = {}", SGX_DEBUG_FLAG);
}
status = sgx_create_enclave_search(ENCLAVE_NAME, SGX_DEBUG_FLAG, &token,
&updated, &eid, 0);
......
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