Skip to content

Commit

Permalink
Merge pull request #403 from skalenetwork/enhancement/add-linter
Browse files Browse the repository at this point in the history
Add clang linter
  • Loading branch information
kladkogex authored Mar 14, 2023
2 parents 22d6c98 + 6c714c5 commit 7204fcf
Show file tree
Hide file tree
Showing 95 changed files with 22,631 additions and 21,038 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: clang-format Check
on: [push]
jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: DoozyX/[email protected]
with:
source: '.'
exclude: './CMakeFiles ./cmake ./deps ./build ./cppzmq ./gmp-build ./intel-sgx-ssl ./jsonrpc ./leveldb ./libBLS ./libzmq ./linux-sgx-driver ./rapidjson ./sgx-gmp ./sgx-sdk-build ./sgx-software-enable ./tgmp-build ./third_party/* ./newer_lcov'
extensions: 'h,hpp,hxx,cpp,cxx,cc,ipp'
clangFormatVersion: 10
275 changes: 144 additions & 131 deletions BLSCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,237 +21,250 @@
@date 2019
*/

#include <memory>
#include "libff/algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include "leveldb/db.h"
#include "libff/algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include <jsonrpccpp/server/connectors/httpserver.h>
#include <memory>

#include "third_party/intel/create_enclave.h"


#include <tools/utils.h>


#include "sgxwallet_common.h"
#include "sgxwallet.h"
#include "SGXException.h"
#include "third_party/spdlog/spdlog.h"
#include "common.h"
#include "SGXWalletServer.hpp"
#include "common.h"
#include "sgxwallet.h"
#include "sgxwallet_common.h"
#include "third_party/spdlog/spdlog.h"

#include "SEKManager.h"
#include "LevelDB.h"
#include "ServerInit.h"
#include "BLSCrypto.h"
#include "CryptoTools.h"

#include "LevelDB.h"
#include "SEKManager.h"
#include "ServerInit.h"

shared_ptr<string> FqToString(libff::alt_bn128_Fq *_fq) {

CHECK_STATE(_fq);
CHECK_STATE(_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);

SAFE_CHAR_BUF(arr, mpz_sizeinbase(t, 10) + 2);
SAFE_CHAR_BUF(arr, mpz_sizeinbase(t, 10) + 2);

mpz_get_str(arr, 10, t);
mpz_clear(t);
mpz_get_str(arr, 10, t);
mpz_clear(t);

return make_shared<string>(string(arr));
return make_shared<string>(string(arr));
}

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

CHECK_STATE(_encryptedKeyHex);
CHECK_STATE(_hashHex);
CHECK_STATE(_sig);
CHECK_STATE(_encryptedKeyHex);
CHECK_STATE(_hashHex);
CHECK_STATE(_sig);

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(), hash->size())) {
throw SGXException(SIGN_AES_INVALID_HASH, string(__FUNCTION__) + ":Invalid hash");
}
if (!hex2carray(_hashHex, &binLen, hash->data(), hash->size())) {
throw SGXException(SIGN_AES_INVALID_HASH,
string(__FUNCTION__) + ":Invalid hash");
}

shared_ptr <libBLS::Bls> obj;
obj = make_shared<libBLS::Bls>(libBLS::Bls(_t, _n));
shared_ptr<libBLS::Bls> obj;
obj = make_shared<libBLS::Bls>(libBLS::Bls(_t, _n));

pair <libff::alt_bn128_G1, string> hash_with_hint = obj->HashtoG1withHint(hash);
pair<libff::alt_bn128_G1, string> hash_with_hint =
obj->HashtoG1withHint(hash);

shared_ptr<string> xStr = FqToString(&(hash_with_hint.first.X));
shared_ptr<string> xStr = FqToString(&(hash_with_hint.first.X));

CHECK_STATE(xStr);
CHECK_STATE(xStr);

shared_ptr<string> yStr = FqToString(&(hash_with_hint.first.Y));
shared_ptr<string> yStr = FqToString(&(hash_with_hint.first.Y));

CHECK_STATE(yStr);
CHECK_STATE(yStr);

vector<char> errMsg(BUF_LEN, 0);
vector<char> errMsg(BUF_LEN, 0);

SAFE_CHAR_BUF(xStrArg, BUF_LEN);SAFE_CHAR_BUF(yStrArg, BUF_LEN);SAFE_CHAR_BUF(signature, BUF_LEN);
SAFE_CHAR_BUF(xStrArg, BUF_LEN);
SAFE_CHAR_BUF(yStrArg, BUF_LEN);
SAFE_CHAR_BUF(signature, 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;

SAFE_UINT8_BUF(encryptedKey, BUF_LEN);
SAFE_UINT8_BUF(encryptedKey, BUF_LEN);

bool result = hex2carray(_encryptedKeyHex, &sz, encryptedKey, BUF_LEN);
bool result = hex2carray(_encryptedKeyHex, &sz, encryptedKey, BUF_LEN);

if (!result) {
BOOST_THROW_EXCEPTION(invalid_argument("Invalid hex encrypted key"));
}
if (!result) {
BOOST_THROW_EXCEPTION(invalid_argument("Invalid hex encrypted key"));
}

int errStatus = 0;
int errStatus = 0;

sgx_status_t status = SGX_SUCCESS;
sgx_status_t status = SGX_SUCCESS;

status = trustedBlsSignMessage(eid, &errStatus, errMsg.data(), encryptedKey,
sz, xStrArg, yStrArg, signature);
status = trustedBlsSignMessage(eid, &errStatus, errMsg.data(), encryptedKey,
sz, xStrArg, yStrArg, signature);

HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());

string hint = libBLS::ThresholdUtils::fieldElementToString(hash_with_hint.first.Y) + ":" + hash_with_hint.second;
string hint =
libBLS::ThresholdUtils::fieldElementToString(hash_with_hint.first.Y) +
":" + hash_with_hint.second;

string sig = signature;
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);

return true;
return true;
}

bool bls_sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t, size_t _n, char *_sig) {
CHECK_STATE(_encryptedKeyHex);
CHECK_STATE(_hashHex);
return sign_aes(_encryptedKeyHex, _hashHex, _t, _n, _sig);
bool bls_sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t,
size_t _n, char *_sig) {
CHECK_STATE(_encryptedKeyHex);
CHECK_STATE(_hashHex);
return sign_aes(_encryptedKeyHex, _hashHex, _t, _n, _sig);
}

bool popProveSGX( const char* encryptedKeyHex, char* prove ) {
CHECK_STATE(encryptedKeyHex);
bool popProveSGX(const char *encryptedKeyHex, char *prove) {
CHECK_STATE(encryptedKeyHex);

SAFE_UINT8_BUF(encryptedKey, BUF_LEN);
SAFE_UINT8_BUF(encryptedKey, BUF_LEN);

size_t sz = 0;
size_t sz = 0;

if (!hex2carray(encryptedKeyHex, &sz, encryptedKey, BUF_LEN)) {
BOOST_THROW_EXCEPTION(invalid_argument("Invalid hex encrypted key"));
}
if (!hex2carray(encryptedKeyHex, &sz, encryptedKey, BUF_LEN)) {
BOOST_THROW_EXCEPTION(invalid_argument("Invalid hex encrypted key"));
}

sgx_status_t status = SGX_SUCCESS;
sgx_status_t status = SGX_SUCCESS;

vector<char> errMsg(BUF_LEN, 0);
vector<char> errMsg(BUF_LEN, 0);

int errStatus = 0;
int errStatus = 0;

SAFE_CHAR_BUF(pubKey, 320)
SAFE_CHAR_BUF(pubKey, 320)

status = trustedGetBlsPubKey(eid, &errStatus, errMsg.data(), encryptedKey, sz, pubKey);
status = trustedGetBlsPubKey(eid, &errStatus, errMsg.data(), encryptedKey, sz,
pubKey);

HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());

vector <string> pubKeyVect = splitString(pubKey, ':');
vector<string> pubKeyVect = splitString(pubKey, ':');

spdlog::debug("pub key is ");
for (int i = 0; i < 4; i++)
spdlog::debug("{}", pubKeyVect.at(i));
spdlog::debug("pub key is ");
for (int i = 0; i < 4; i++)
spdlog::debug("{}", pubKeyVect.at(i));

libff::alt_bn128_G2 publicKey;
publicKey.Z = libff::alt_bn128_Fq2::one();
publicKey.X.c0 = libff::alt_bn128_Fq(pubKeyVect[0].c_str());
publicKey.X.c1 = libff::alt_bn128_Fq(pubKeyVect[1].c_str());
publicKey.Y.c0 = libff::alt_bn128_Fq(pubKeyVect[2].c_str());
publicKey.Y.c1 = libff::alt_bn128_Fq(pubKeyVect[3].c_str());
libff::alt_bn128_G2 publicKey;
publicKey.Z = libff::alt_bn128_Fq2::one();
publicKey.X.c0 = libff::alt_bn128_Fq(pubKeyVect[0].c_str());
publicKey.X.c1 = libff::alt_bn128_Fq(pubKeyVect[1].c_str());
publicKey.Y.c0 = libff::alt_bn128_Fq(pubKeyVect[2].c_str());
publicKey.Y.c1 = libff::alt_bn128_Fq(pubKeyVect[3].c_str());

pair <libff::alt_bn128_G1, string> hashPublicKeyWithHint = libBLS::Bls::HashPublicKeyToG1WithHint( publicKey );
pair<libff::alt_bn128_G1, string> hashPublicKeyWithHint =
libBLS::Bls::HashPublicKeyToG1WithHint(publicKey);

hashPublicKeyWithHint.first.to_affine_coordinates();
hashPublicKeyWithHint.first.to_affine_coordinates();

shared_ptr<string> xStr = FqToString(&(hashPublicKeyWithHint.first.X));
shared_ptr<string> xStr = FqToString(&(hashPublicKeyWithHint.first.X));

CHECK_STATE(xStr);
CHECK_STATE(xStr);

shared_ptr<string> yStr = FqToString(&(hashPublicKeyWithHint.first.Y));
shared_ptr<string> yStr = FqToString(&(hashPublicKeyWithHint.first.Y));

CHECK_STATE(yStr);
CHECK_STATE(yStr);

SAFE_CHAR_BUF(xStrArg, BUF_LEN);SAFE_CHAR_BUF(yStrArg, BUF_LEN);
SAFE_CHAR_BUF(xStrArg, BUF_LEN);
SAFE_CHAR_BUF(yStrArg, 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);

errStatus = 0;
errStatus = 0;

status = trustedBlsSignMessage(eid, &errStatus, errMsg.data(), encryptedKey, sz, xStrArg, yStrArg, prove);
status = trustedBlsSignMessage(eid, &errStatus, errMsg.data(), encryptedKey,
sz, xStrArg, yStrArg, prove);

HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());

string hint = libBLS::ThresholdUtils::fieldElementToString(hashPublicKeyWithHint.first.Y) + ":" + hashPublicKeyWithHint.second;
string hint = libBLS::ThresholdUtils::fieldElementToString(
hashPublicKeyWithHint.first.Y) +
":" + hashPublicKeyWithHint.second;

string _prove = prove;
string _prove = prove;

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

strncpy(prove, _prove.c_str(), BUF_LEN);
strncpy(prove, _prove.c_str(), BUF_LEN);

return true;
return true;
}

bool generateBLSPrivateKeyAggegated(const char* blsKeyName) {
CHECK_STATE(blsKeyName);
bool generateBLSPrivateKeyAggegated(const char *blsKeyName) {
CHECK_STATE(blsKeyName);

vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;
vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;

int exportable = 0;
int exportable = 0;

uint64_t encBlsLen = 0;
uint64_t encBlsLen = 0;

sgx_status_t status = SGX_SUCCESS;
sgx_status_t status = SGX_SUCCESS;

SAFE_UINT8_BUF(encrBlsKey, BUF_LEN)
SAFE_UINT8_BUF(encrBlsKey, BUF_LEN)

status = trustedGenerateBLSKey(eid, &errStatus, errMsg.data(), &exportable, encrBlsKey, &encBlsLen);
status = trustedGenerateBLSKey(eid, &errStatus, errMsg.data(), &exportable,
encrBlsKey, &encBlsLen);

HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());

vector<char> hexBLSKey = carray2Hex(encrBlsKey, encBlsLen);
vector<char> hexBLSKey = carray2Hex(encrBlsKey, encBlsLen);

SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey.data());
SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey.data());

return true;
return true;
}

string encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) {
CHECK_STATE(errStatus);
CHECK_STATE(err_string);
CHECK_STATE(_key);
auto keyArray = make_shared < vector < char >> (BUF_LEN, 0);
auto encryptedKey = make_shared < vector < uint8_t >> (BUF_LEN, 0);
string encryptBLSKeyShare2Hex(int *errStatus, char *err_string,
const char *_key) {
CHECK_STATE(errStatus);
CHECK_STATE(err_string);
CHECK_STATE(_key);
auto keyArray = make_shared<vector<char>>(BUF_LEN, 0);
auto encryptedKey = make_shared<vector<uint8_t>>(BUF_LEN, 0);

vector<char> errMsg(BUF_LEN, 0);
vector<char> errMsg(BUF_LEN, 0);

strncpy(keyArray->data(), _key, BUF_LEN);
*errStatus = 0;
strncpy(keyArray->data(), _key, BUF_LEN);
*errStatus = 0;

uint64_t encryptedLen = 0;
uint64_t encryptedLen = 0;

sgx_status_t status = SGX_SUCCESS;
sgx_status_t status = SGX_SUCCESS;

status = trustedEncryptKey(eid, errStatus, errMsg.data(), keyArray->data(), encryptedKey->data(),
&encryptedLen);
status = trustedEncryptKey(eid, errStatus, errMsg.data(), keyArray->data(),
encryptedKey->data(), &encryptedLen);

HANDLE_TRUSTED_FUNCTION_ERROR(status, *errStatus, errMsg.data());
HANDLE_TRUSTED_FUNCTION_ERROR(status, *errStatus, errMsg.data());

vector<char> resultBuf = carray2Hex(encryptedKey->data(), encryptedLen);
vector<char> resultBuf = carray2Hex(encryptedKey->data(), encryptedLen);

return string(resultBuf.begin(), resultBuf.end());
return string(resultBuf.begin(), resultBuf.end());
}
Loading

0 comments on commit 7204fcf

Please sign in to comment.