SKALE-2794 refactor code

parent da0c0687
......@@ -242,7 +242,7 @@ verifyShares(const char *publicShares, const char *encr_sshare, const char *encr
trustedDkgVerifyAES(eid, &errStatus, errMsg, pshares, encr_sshare, encr_key, decKeyLen, t, ind, &result);
if (errStatus != 0) {
throw SGXException(-666, errMsg1.data());
throw SGXException(-666, errMsg);
}
if (result == 2) {
......
......@@ -43,7 +43,7 @@ bool case_insensitive_match(string s1, string s2) {
return s1.compare(s2);
}
void create_test_key(){
void create_test_key() {
int errStatus = 0;
vector<char> errMsg(1024,0);
uint32_t enc_len;
......@@ -54,30 +54,34 @@ void create_test_key(){
std::string key = TEST_VALUE;
status = trustedEncryptKeyAES(eid, &errStatus, errMsg.data(), key.c_str(), encrypted_key, &enc_len);
if ( status != 0){
if ( status != SGX_SUCCESS ) {
std::cerr << "encrypt test key failed with status " << status << std::endl;
throw SGXException(status, errMsg.data()) ;
}
if ( errStatus != 0 ) {
std::cerr << "encrypt test key failed with status " << errStatus << std::endl;
throw SGXException(errStatus, errMsg.data()) ;
}
vector<char> hexEncrKey(2 * enc_len + 1, 0);
carray2Hex(encrypted_key, enc_len, hexEncrKey.data());
uint64_t test_len;
vector<uint8_t>test_encr_key(1024, 0);
if (!hex2carray(hexEncrKey.data(), &test_len, test_encr_key.data())){
if (!hex2carray(hexEncrKey.data(), &test_len, test_encr_key.data())) {
std::cerr << "wrong encrypted test key" << std::endl;
}
LevelDB::getLevelDb() -> writeDataUnique("TEST_KEY", hexEncrKey.data());
}
bool check_SEK(std::string SEK){
std::shared_ptr <std::string> test_key_ptr = LevelDB::getLevelDb() -> readString("TEST_KEY");
bool check_SEK(const std::string& SEK) {
std::shared_ptr <std::string> test_key_ptr = LevelDB::getLevelDb() -> readString("TEST_KEY");
vector<uint8_t> encr_test_key(BUF_LEN, 0);
uint64_t len;
if ( !hex2carray(test_key_ptr->c_str(), &len, encr_test_key.data())){
if (!hex2carray(test_key_ptr->c_str(), &len, encr_test_key.data())) {
spdlog::error("wrong test key" );
exit(-1);
}
......@@ -91,20 +95,25 @@ bool check_SEK(std::string SEK){
uint32_t l = len;
status = trustedSetSEK_backup(eid, &err_status, errMsg.data(), encr_SEK.data(), &l, SEK.c_str() );
if (status != SGX_SUCCESS){
if (status != SGX_SUCCESS) {
cerr << "RPCException thrown with status " << status << endl;
throw SGXException(status, errMsg.data());
}
if ( err_status != 0 ) {
cerr << "RPCException thrown with status " << err_status << endl;
throw SGXException(err_status, errMsg.data());
}
status = trustedDecryptKeyAES(eid, &err_status, errMsg.data(), encr_test_key.data(), len, decr_key.data());
if (status != SGX_SUCCESS || err_status != 0){
if (status != SGX_SUCCESS || err_status != 0) {
spdlog::error("failed to decrypt test key" );
spdlog::error(errMsg.data());
exit(-1);
}
std::string test_key = TEST_VALUE;
if (test_key.compare(decr_key.data()) != 0){
if (test_key.compare(decr_key.data()) != 0) {
std::cerr << "decrypted key is " << decr_key.data() << std::endl;
spdlog::error("Invalid SEK" );
return false;
......@@ -112,7 +121,7 @@ bool check_SEK(std::string SEK){
return true;
}
void gen_SEK(){
void gen_SEK() {
vector<char> errMsg(1024,0);
int err_status = 0;
vector<uint8_t> encr_SEK(1024, 0);
......@@ -122,10 +131,14 @@ void gen_SEK(){
memset(SEK, 0, 65);
status = trustedGenerateSEK(eid, &err_status, errMsg.data(), encr_SEK.data(), &enc_len, SEK);
if ( status != SGX_SUCCESS || err_status != 0 ) {
if ( status != SGX_SUCCESS ) {
throw SGXException(status, errMsg.data()) ;
}
if ( err_status != 0 ) {
throw SGXException(err_status, errMsg.data()) ;
}
vector<char> hexEncrKey(2 * enc_len + 1, 0);
carray2Hex(encr_SEK.data(), enc_len, hexEncrKey.data());
......@@ -140,7 +153,7 @@ void gen_SEK(){
std::cout << " DO YOU CONFIRM THAT YOU COPIED THE KEY? (if you confirm type - I confirm)"
<< std::endl;
std::getline(std::cin, buffer);
} while (case_insensitive_match(confirm_str, buffer)); //(strcmp(confirm_str.c_str(), buffer.c_str()) != 0);
} while (case_insensitive_match(confirm_str, buffer));
}
LevelDB::getLevelDb()->writeDataUnique("SEK", hexEncrKey.data());
......@@ -148,7 +161,7 @@ void gen_SEK(){
create_test_key();
}
void trustedSetSEK(std::shared_ptr<std::string> hex_encr_SEK){
void trustedSetSEK(std::shared_ptr<std::string> hex_encr_SEK) {
vector<char> errMsg(1024,0);
int err_status = 0;
......@@ -157,25 +170,30 @@ void trustedSetSEK(std::shared_ptr<std::string> hex_encr_SEK){
uint64_t len;
if (!hex2carray(hex_encr_SEK->c_str(), &len, encr_SEK)){
if (!hex2carray(hex_encr_SEK->c_str(), &len, encr_SEK)) {
throw SGXException(INVALID_HEX, "Invalid encrypted SEK Hex");
}
status = trustedSetSEK(eid, &err_status, errMsg.data(), encr_SEK, len );
if ( status != SGX_SUCCESS || err_status != 0 ){
if ( status != SGX_SUCCESS ) {
cerr << "RPCException thrown" << endl;
throw SGXException(status, errMsg.data()) ;
}
if ( err_status != 0 ) {
cerr << "RPCException thrown" << endl;
throw SGXException(err_status, errMsg.data()) ;
}
}
void enter_SEK(){
void enter_SEK() {
vector<char> errMsg(1024,0);
int err_status = 0;
vector<uint8_t> encr_SEK(BUF_LEN, 0);
uint32_t enc_len;
std::shared_ptr <std::string> test_key_ptr = LevelDB::getLevelDb() -> readString("TEST_KEY");
if (test_key_ptr == nullptr){
if (test_key_ptr == nullptr) {
spdlog::error("empty db" );
exit(-1);
}
......@@ -183,18 +201,23 @@ void enter_SEK(){
std::string SEK;
std::cout << "ENTER BACKUP KEY" << std::endl;
std::cin >> SEK;
while (!checkHex(SEK, 16) || !check_SEK(SEK)){
while (!checkHex(SEK, 16) || !check_SEK(SEK)) {
std::cout << "KEY IS INVALID.TRY ONCE MORE" << std::endl;
SEK = "";
std::cin >> SEK;
}
status = trustedSetSEK_backup(eid, &err_status, errMsg.data(), encr_SEK.data(), &enc_len, SEK.c_str());
if (status != SGX_SUCCESS){
if (status != SGX_SUCCESS) {
cerr << "RPCException thrown with status " << status << endl;
throw SGXException(status, errMsg.data());
}
if ( err_status != 0 ) {
cerr << "RPCException thrown" << endl;
throw SGXException(err_status, errMsg.data()) ;
}
vector<char> hexEncrKey(2 * enc_len + 1, 0);
carray2Hex(encr_SEK.data(), enc_len, hexEncrKey.data());
......@@ -203,7 +226,7 @@ void enter_SEK(){
LevelDB::getLevelDb() -> writeDataUnique("SEK", hexEncrKey.data());
}
void initSEK(){
void initSEK() {
std::shared_ptr<std::string> encr_SEK_ptr = LevelDB::getLevelDb()->readString("SEK");
if (encryptKeys) {
enter_SEK();
......
......@@ -42,8 +42,7 @@
using namespace std;
string stringFromFr(libff::alt_bn128_Fr& _el) {
string stringFromFr(const libff::alt_bn128_Fr& _el) {
mpz_t t;
mpz_init(t);
......@@ -57,8 +56,7 @@ string stringFromFr(libff::alt_bn128_Fr& _el) {
return string(tmp);
}
template<class T>
string ConvertToString(T field_elem, int base = 10) {
template<class T> string ConvertToString(const T& field_elem, int base = 10) {
mpz_t t;
mpz_init(t);
......@@ -74,7 +72,7 @@ string ConvertToString(T field_elem, int base = 10) {
return output;
}
string ConvertG2ToString(const libff::alt_bn128_G2 & elem, int base = 10, string delim = ":"){
string ConvertG2ToString(const libff::alt_bn128_G2 & elem, int base = 10, string delim = ":") {
string result;
result += ConvertToString(elem.X.c0);
result += delim;
......@@ -87,7 +85,7 @@ string ConvertG2ToString(const libff::alt_bn128_G2 & elem, int base = 10, string
return result;
}
vector<libff::alt_bn128_Fr> SplitStringToFr(const char* coeffs, const char symbol){
vector<libff::alt_bn128_Fr> SplitStringToFr(const char* coeffs, const char symbol) {
string str(coeffs);
string delim;
delim.push_back(symbol);
......@@ -109,7 +107,7 @@ vector<libff::alt_bn128_Fr> SplitStringToFr(const char* coeffs, const char symbo
return tokens;
}
int gen_dkg_poly( char* secret, unsigned _t ){
int gen_dkg_poly( char* secret, unsigned _t ) {
libff::init_alt_bn128_params();
string result;
for (size_t i = 0; i < _t; ++i) {
......@@ -147,11 +145,11 @@ void calc_secret_shares(const char* decrypted_coeffs, char * secret_shares,
// calculate for each node a list of secret values that will be used for verification
string result;
char symbol = ':';
vector<libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
vector<libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
for (size_t i = 0; i < _n; ++i) {
for (size_t i = 0; i < _n; ++i) {
libff::alt_bn128_Fr secret_share = PolynomialValue(poly, libff::alt_bn128_Fr(i + 1), _t);
result += ConvertToString(secret_share);//stringFromFr(secret_share);
result += ConvertToString(secret_share);
result += ":";
}
strncpy(secret_shares, result.c_str(), result.length() + 1);
......@@ -161,8 +159,8 @@ int calc_secret_share(const char* decrypted_coeffs, char * s_share,
unsigned _t, unsigned _n, unsigned ind) {
libff::init_alt_bn128_params();
char symbol = ':';
vector<libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
if ( poly.size() != _t){
vector<libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
if ( poly.size() != _t) {
return 1;
}
......@@ -190,7 +188,7 @@ void calc_secret_shareG2_old(const char* decrypted_coeffs, char * s_shareG2,
strncpy(s_shareG2, secret_shareG2_str.c_str(), secret_shareG2_str.length() + 1);
}
int calc_secret_shareG2(const char* s_share, char * s_shareG2){
int calc_secret_shareG2(const char* s_share, char * s_shareG2) {
libff::init_alt_bn128_params();
mpz_t share;
......@@ -225,7 +223,7 @@ int calc_public_shares(const char* decrypted_coeffs, char * public_shares,
string result;
char symbol = ':';
vector<libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
if (poly.size() != _t){
if (poly.size() != _t) {
return 1;
}
for (size_t i = 0; i < _t; ++i) {
......@@ -255,7 +253,7 @@ string ConvertHexToDec(string hex_str){
return result;
}
int Verification ( char * public_shares, mpz_t decr_secret_share, int _t, int ind ) {
int Verification( char * public_shares, mpz_t decr_secret_share, int _t, int ind ) {
string pub_shares_str = public_shares;
libff::init_alt_bn128_params();
......@@ -307,7 +305,7 @@ int Verification ( char * public_shares, mpz_t decr_secret_share, int _t, int in
return (val == sshare * libff::alt_bn128_G2::one());
}
int calc_bls_public_key(char* skey_hex, char* pub_key){
int calc_bls_public_key(char* skey_hex, char* pub_key) {
libff::init_alt_bn128_params();
mpz_t skey;
......@@ -333,7 +331,3 @@ int calc_bls_public_key(char* skey_hex, char* pub_key){
return 0;
}
......@@ -36,7 +36,6 @@
using namespace std;
string *stringFromKey(libff::alt_bn128_Fr *_key) {
mpz_t t;
mpz_init(t);
......@@ -79,7 +78,6 @@ string *stringFromG1(libff::alt_bn128_G1 *_g1) {
return sG1;
}
libff::alt_bn128_Fr *keyFromString(const char *_keyStringHex) {
mpz_t skey;
mpz_init(skey);
......@@ -92,7 +90,6 @@ libff::alt_bn128_Fr *keyFromString(const char *_keyStringHex) {
return new libff::alt_bn128_Fr(skey_dec);
}
int inited = 0;
void enclave_init() {
......@@ -116,10 +113,9 @@ bool enclave_sign(const char *_keyString, const char *_hashXString, const char *
libff::alt_bn128_Fq hashY(_hashYString);
libff::alt_bn128_Fq hashZ = 1;
libff::alt_bn128_G1 hash(hashX, hashY, hashZ);
libff::alt_bn128_G1 sign = key->as_bigint() * hash; // sign
libff::alt_bn128_G1 sign = key->as_bigint() * hash;
sign.to_affine_coordinates();
......@@ -135,7 +131,6 @@ bool enclave_sign(const char *_keyString, const char *_hashXString, const char *
}
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'};
......@@ -145,7 +140,6 @@ void carray2Hex(const unsigned char *d, int _len, char* _hexArray) {
}
_hexArray[_len * 2] = 0;
}
int char2int(char _input) {
......@@ -160,7 +154,7 @@ int char2int(char _input) {
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);
int len = strnlen(_hex, _max_length);
if (len == 0 && len % 2 == 1)
return false;
......@@ -204,13 +198,11 @@ bool hex2carray(const char * _hex, uint64_t *_bin_len,
return true;
}
enum log_level {L_TRACE = 0, L_DEBUG = 1, L_INFO = 2,L_WARNING = 3, L_ERROR = 4 };
enum log_level {L_TRACE = 0, L_DEBUG = 1, L_INFO = 2, L_WARNING = 3, L_ERROR = 4 };
uint32_t globalLogLevel_ = 2;
void logMsg(log_level _level, const char* _msg) {
if (_level < globalLogLevel_)
return;
......
......@@ -56,7 +56,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "LevelDB.h"
#include "SGXWalletServer.hpp"
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
......@@ -75,11 +74,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "TestUtils.h"
#include "testw.h"
using namespace jsonrpc;
using namespace std;
class TestFixture {
public:
TestFixture() {
......@@ -277,14 +274,12 @@ string genECDSAKeyAPI(StubClient &_c) {
return keyName;
}
TEST_CASE_METHOD(TestFixture, "ECDSA key gen API", "[ecdsa-key-gen-api]") {
HttpClient client(RPC_ENDPOINT);
StubClient c(client, JSONRPC_CLIENT_V2);
for (int i = 0; i <= 20; i++) {
try {
auto keyName = genECDSAKeyAPI(c);
Json::Value sig = c.ecdsaSignMessageHash(16, keyName, SAMPLE_HASH);
......@@ -299,7 +294,6 @@ TEST_CASE_METHOD(TestFixture, "ECDSA key gen API", "[ecdsa-key-gen-api]") {
for (int i = 0; i <= 20; i++) {
try {
auto keyName = genECDSAKeyAPI(c);
Json::Value sig = c.ecdsaSignMessageHash(10, keyName, SAMPLE_HASH);
......@@ -406,12 +400,6 @@ TEST_CASE_METHOD(TestFixture, "DKG public shares test", "[dkg-pub-shares]") {
for (uint32_t i = 0; i < pubSharesDkg.size(); i++) {
libff::alt_bn128_G2 el = pubSharesDkg.at(i);
el.to_affine_coordinates();
libff::alt_bn128_Fq x_c0_el = el.X.c0;
mpz_t x_c0;
mpz_init(x_c0);
x_c0_el.as_bigint().to_mpz(x_c0);
mpz_clear(x_c0);
}
REQUIRE(pubSharesG2 == pubSharesDkg);
}
......@@ -461,12 +449,6 @@ TEST_CASE_METHOD(TestFixture, "DKG AES public shares test", "[dkg-aes-pub-shares
for (uint32_t i = 0; i < pubSharesDkg.size(); i++) {
libff::alt_bn128_G2 el = pubSharesDkg.at(i);
el.to_affine_coordinates();
libff::alt_bn128_Fq x_c0_el = el.X.c0;
mpz_t x_c0;
mpz_init(x_c0);
x_c0_el.as_bigint().to_mpz(x_c0);
mpz_clear(x_c0);
}
REQUIRE(pubSharesG2 == pubSharesDkg);
}
......@@ -483,7 +465,6 @@ TEST_CASE_METHOD(TestFixture, "DKG encrypted secret shares test", "[dkg-encr-ssh
REQUIRE(status == SGX_SUCCESS);
REQUIRE(errStatus == SGX_SUCCESS);
status = trustedSetEncryptedDkgPoly(eid, &errStatus, errMsg.data(), encryptedDKGSecret.data());
REQUIRE(status == SGX_SUCCESS);
REQUIRE(errStatus == SGX_SUCCESS);
......@@ -568,12 +549,10 @@ TEST_CASE_METHOD(TestFixture, "DKG_BLS test", "[dkg-bls]") {
REQUIRE(blsKeyNames.size() == 4);
schainID = TestUtils::randGen();
dkgID = TestUtils::randGen();
TestUtils::doDKG(c, 16, 5, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
}
......@@ -616,6 +595,7 @@ TEST_CASE_METHOD(TestFixture, "DKG API test", "[dkg-api]") {
string polyName = SAMPLE_POLY_NAME;
Json::Value genPoly = c.generateDKGPoly(polyName, 2);
REQUIRE(genPoly["status"].asInt() == 0);
Json::Value publicKeys;
publicKeys.append(SAMPLE_DKG_PUB_KEY_1);
......
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