SKALE-2794 fix codacy issues

parent 54cae443
...@@ -23,18 +23,15 @@ ...@@ -23,18 +23,15 @@
#include <memory> #include <memory>
#include "libff/algebra/curves/alt_bn128/alt_bn128_init.hpp" #include "libff/algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include "bls.h" #include "bls.h"
#include <bls/BLSutils.h> #include <bls/BLSutils.h>
#include "leveldb/db.h" #include "leveldb/db.h"
#include <jsonrpccpp/server/connectors/httpserver.h> #include <jsonrpccpp/server/connectors/httpserver.h>
#include "BLSPrivateKeyShareSGX.h" #include "BLSPrivateKeyShareSGX.h"
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
#include "create_enclave.h" #include "create_enclave.h"
#include "secure_enclave_u.h" #include "secure_enclave_u.h"
...@@ -54,7 +51,6 @@ ...@@ -54,7 +51,6 @@
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "common.h" #include "common.h"
std::string *FqToString(libff::alt_bn128_Fq *_fq) { std::string *FqToString(libff::alt_bn128_Fq *_fq) {
mpz_t t; mpz_t t;
mpz_init(t); mpz_init(t);
...@@ -79,7 +75,6 @@ int char2int(char _input) { ...@@ -79,7 +75,6 @@ int char2int(char _input) {
return -1; 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', char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
...@@ -92,12 +87,9 @@ void carray2Hex(const unsigned char *d, int _len, char *_hexArray) { ...@@ -92,12 +87,9 @@ void carray2Hex(const unsigned char *d, int _len, char *_hexArray) {
_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) if (len == 0 && len % 2 == 1)
return false; return false;
...@@ -121,7 +113,6 @@ bool hex2carray2(const char *_hex, uint64_t *_bin_len, ...@@ -121,7 +113,6 @@ bool hex2carray2(const char *_hex, uint64_t *_bin_len,
uint8_t *_bin, const int _max_length) { uint8_t *_bin, const int _max_length) {
int len = strnlen(_hex, _max_length); int len = strnlen(_hex, _max_length);
if (len == 0 && len % 2 == 1) if (len == 0 && len % 2 == 1)
return false; return false;
...@@ -166,8 +157,6 @@ bool sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t ...@@ -166,8 +157,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, bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t _n, size_t _signerIndex,
char *_sig) { char *_sig) {
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;
...@@ -181,8 +170,6 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz ...@@ -181,8 +170,6 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
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;
string *xStr = FqToString(&(hash_with_hint.first.X)); string *xStr = FqToString(&(hash_with_hint.first.X));
if (xStr == nullptr) { if (xStr == nullptr) {
...@@ -221,6 +208,8 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz ...@@ -221,6 +208,8 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid hex encrypted key")); BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid hex encrypted key"));
} }
int errStatus = 0;
sgx_status_t status = sgx_status_t status =
trustedBlsSignMessageAES(eid, &errStatus, errMsg, encryptedKey, trustedBlsSignMessageAES(eid, &errStatus, errMsg, encryptedKey,
sz, xStrArg, yStrArg, signature); sz, xStrArg, yStrArg, signature);
...@@ -230,8 +219,12 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz ...@@ -230,8 +219,12 @@ bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
BOOST_THROW_EXCEPTION(runtime_error("SGX enclave call to trustedBlsSignMessage failed")); BOOST_THROW_EXCEPTION(runtime_error("SGX enclave call to trustedBlsSignMessage failed"));
} }
std::string hint = BLSutils::ConvertToString(hash_with_hint.first.Y) + ":" + if (errStatus != 0) {
hash_with_hint.second; cerr << "SGX enclave call to trustedBlsSignMessage failed:" << errStatus << 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; std::string sig = signature;
...@@ -248,7 +241,7 @@ bool bls_sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz ...@@ -248,7 +241,7 @@ bool bls_sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, siz
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) { std::string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) {
auto keyArray = make_shared<vector<char>>(BUF_LEN, 0); auto keyArray = make_shared<vector<char>>(BUF_LEN, 0);
auto encryptedKey = make_shared<vector<uint8_t>>(BUF_LEN, 0); auto encryptedKey = make_shared<vector<uint8_t>>(BUF_LEN, 0);
auto errMsg = make_shared<vector<char>>(BUF_LEN, 0); auto errMsg = make_shared<vector<char>>(BUF_LEN, 0);
...@@ -271,9 +264,9 @@ char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) ...@@ -271,9 +264,9 @@ char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key)
return nullptr; return nullptr;
} }
char *result = (char *) calloc(2 * BUF_LEN, 1); std::string result(2 * BUF_LEN, '\0');
carray2Hex(encryptedKey->data(), encryptedLen, result); carray2Hex(encryptedKey->data(), encryptedLen, &result.front());
return result; return result;
} }
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "stddef.h" #include "stddef.h"
#include "stdint.h" #include "stdint.h"
#include <string>
EXTERNC bool bls_sign(const char* encryptedKeyHex, const char* hashHex, size_t t, size_t n, EXTERNC bool bls_sign(const char* encryptedKeyHex, const char* hashHex, size_t t, size_t n,
size_t signerIndex, char* _sig); size_t signerIndex, char* _sig);
...@@ -46,6 +47,6 @@ EXTERNC bool hex2carray2(const char * _hex, uint64_t *_bin_len, ...@@ -46,6 +47,6 @@ EXTERNC bool hex2carray2(const char * _hex, uint64_t *_bin_len,
char * encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key); std::string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key);
#endif //SGXWALLET_BLSCRYPTO_H #endif //SGXWALLET_BLSCRYPTO_H
...@@ -21,8 +21,6 @@ ...@@ -21,8 +21,6 @@
@date 2019 @date 2019
*/ */
#include "BLSSigShare.h" #include "BLSSigShare.h"
#include "BLSSignature.h" #include "BLSSignature.h"
#include "BLSutils.h" #include "BLSutils.h"
...@@ -37,7 +35,6 @@ ...@@ -37,7 +35,6 @@
#include "BLSPrivateKeyShareSGX.h" #include "BLSPrivateKeyShareSGX.h"
std::string *stringFromFq(libff::alt_bn128_Fq*_fq) { std::string *stringFromFq(libff::alt_bn128_Fq*_fq) {
mpz_t t; mpz_t t;
mpz_init(t); mpz_init(t);
...@@ -66,16 +63,12 @@ std::string *stringFromG1(libff::alt_bn128_G1 *_g1) { ...@@ -66,16 +63,12 @@ std::string *stringFromG1(libff::alt_bn128_G1 *_g1) {
return sG1; return sG1;
} }
BLSPrivateKeyShareSGX::BLSPrivateKeyShareSGX( BLSPrivateKeyShareSGX::BLSPrivateKeyShareSGX(
shared_ptr<string> _encryptedKeyHex, size_t _requiredSigners, shared_ptr<string> _encryptedKeyHex, size_t _requiredSigners,
size_t _totalSigners) { size_t _totalSigners) {
requiredSigners = _requiredSigners; requiredSigners = _requiredSigners;
totalSigners = _totalSigners; totalSigners = _totalSigners;
if (requiredSigners > totalSigners) { if (requiredSigners > totalSigners) {
throw std::invalid_argument("requiredSigners > totalSigners"); throw std::invalid_argument("requiredSigners > totalSigners");
} }
......
...@@ -162,7 +162,7 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k ...@@ -162,7 +162,7 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k
result["errorMessage"] = ""; result["errorMessage"] = "";
result["encryptedKeyShare"] = ""; result["encryptedKeyShare"] = "";
char *encryptedKeyShareHex = nullptr; string encryptedKeyShareHex;
try { try {
encryptedKeyShareHex = encryptBLSKeyShare2Hex(&errStatus, &errMsg.front(), _keyShare.c_str()); encryptedKeyShareHex = encryptBLSKeyShare2Hex(&errStatus, &errMsg.front(), _keyShare.c_str());
...@@ -171,22 +171,18 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k ...@@ -171,22 +171,18 @@ SGXWalletServer::importBLSKeyShareImpl(const string &_keyShare, const string &_k
throw SGXException(errStatus, errMsg.data()); throw SGXException(errStatus, errMsg.data());
} }
if (encryptedKeyShareHex == nullptr) { if (encryptedKeyShareHex.empty()) {
throw SGXException(UNKNOWN_ERROR, ""); throw SGXException(UNKNOWN_ERROR, "");
} }
result["encryptedKeyShare"] = string(encryptedKeyShareHex); result["encryptedKeyShare"] = encryptedKeyShareHex;
writeKeyShare(_keyShareName, encryptedKeyShareHex, _index, n, t); writeKeyShare(_keyShareName, encryptedKeyShareHex.data(), _index, n, t);
} catch (SGXException &_e) { } catch (SGXException &_e) {
result["status"] = _e.status; result["status"] = _e.status;
result["errorMessage"] = _e.errString; result["errorMessage"] = _e.errString;
} }
if (encryptedKeyShareHex != nullptr) {
free(encryptedKeyShareHex);
}
return result; return result;
} }
......
...@@ -111,9 +111,9 @@ shared_ptr <string> TestUtils::encryptTestKey() { ...@@ -111,9 +111,9 @@ shared_ptr <string> TestUtils::encryptTestKey() {
const char *key = TEST_BLS_KEY_SHARE; const char *key = TEST_BLS_KEY_SHARE;
int errStatus = -1; int errStatus = -1;
vector<char> errMsg(BUF_LEN, 0);; vector<char> errMsg(BUF_LEN, 0);;
char *encryptedKeyHex = encryptBLSKeyShare2Hex(&errStatus, errMsg.data(), key); string encryptedKeyHex = encryptBLSKeyShare2Hex(&errStatus, errMsg.data(), key);
CHECK_STATE(encryptedKeyHex != nullptr); CHECK_STATE(!encryptedKeyHex.empty());
CHECK_STATE(errStatus == 0); CHECK_STATE(errStatus == 0);
return make_shared<string>(encryptedKeyHex); return make_shared<string>(encryptedKeyHex);
......
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