Unverified Commit 4a694991 authored by Stan Kladko's avatar Stan Kladko Committed by GitHub

Merge pull request #3 from skalenetwork/enhancement/SKALE-XXXX-add-ECDSA-signatures-to-SGX

Enhancement/skale xxxx add ecdsa signatures to sgx
parents a0db96f7 97dc071c
//
// Created by kladko on 9/23/19.
//
#include "ECDSACrypto.h"
#include "BLSCrypto.h"
#include "sgxwallet.h"
#include <iostream>
std::vector<std::string> gen_ecdsa_key(){
char *errMsg = (char *)calloc(1024, 1);
int err_status = 0;
uint8_t* encr_pr_key = (uint8_t *)calloc(1024, 1);
char *pub_key_x = (char *)calloc(1024, 1);
char *pub_key_y = (char *)calloc(1024, 1);
uint32_t enc_len = 0;
status = generate_ecdsa_key(eid, &err_status, errMsg, encr_pr_key, &enc_len, pub_key_x, pub_key_y );
std::vector<std::string> keys(2);
char *hexEncrKey = (char *) calloc(2 * BUF_LEN, 1);
carray2Hex(encr_pr_key, enc_len, hexEncrKey);
keys.at(0) = hexEncrKey;
keys.at(1) = std::string(pub_key_x) + std::string(pub_key_y);
std::cerr << "in ECDSACrypto encr key x " << keys.at(0) << std::endl;
return keys;
}
std::vector<std::string> ecdsa_sign_hash(const char* encryptedKeyHex, const char* hashHex){
std::vector<std::string> signature_vect(3);
char *errMsg = (char *)calloc(1024, 1);
int err_status = 0;
char* signature_r = (char*)malloc(1024);
char* signature_s = (char*)malloc(1024);
char* signature_v = (char*)calloc(4,1);
uint64_t dec_len = 0;
uint8_t encr_key[BUF_LEN];
hex2carray(encryptedKeyHex, &dec_len, encr_key);
status = ecdsa_sign1(eid, &err_status, errMsg, encr_key, dec_len, (unsigned char*)hashHex, signature_r, signature_s, signature_v );
signature_vect.at(0) = signature_v;
signature_vect.at(1) = "0x" + std::string(signature_r);
signature_vect.at(2) = "0x" + std::string(signature_s);
return signature_vect;
}
\ No newline at end of file
//
// Created by kladko on 9/23/19.
//
#ifndef SGXD_ECDSACRYPTO_H
#define SGXD_ECDSACRYPTO_H
#include <vector>
#include <string>
/*#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif*/
std::vector<std::string> gen_ecdsa_key();
std::vector<std::string> ecdsa_sign_hash(const char* encryptedKeyHex, const char* hashHex);
#endif //SGXD_ECDSACRYPTO_H
......@@ -65,7 +65,8 @@ bin_PROGRAMS = sgxwallet testw
COMMON_SRC = sgx_stub.c sgx_detect_linux.c create_enclave.c oc_alloc.c
COMMON_ENCLAVE_SRC = secure_enclave_u.c secure_enclave_u.h
sgxwallet_SOURCES = sgxwallet.c SGXWalletServer.cpp RPCException.cpp BLSCrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp $(COMMON_SRC)
sgxwallet_SOURCES = sgxwallet.c SGXWalletServer.cpp RPCException.cpp BLSCrypto.cpp ECDSACrypto.cpp ServerInit.cpp BLSPrivateKeyShareSGX.cpp LevelDB.cpp $(COMMON_SRC)
nodist_sgxwallet_SOURCES = $(COMMON_ENCLAVE_SRC)
EXTRA_sgxwallet_DEPENDENCIES = secure_enclave.signed.so
......@@ -88,9 +89,15 @@ secure_enclave.signed.so: secure_enclave/secure_enclave.signed.so
## Use the variables, not the actual library names to ensure these
## targets work on simulation builds.
sgxwallet_LDADD=-l$(SGX_URTS_LIB) -Lleveldb/build -LlibBLS/build -LlibBLS/build/libff/libff -l:libbls.a -l:libleveldb.a -l:libff.a -lgmp -ldl -l:libsgx_capable.a -l:libsgx_tprotected_fs.a -ljsonrpccpp-stub -lpthread -ljsonrpccpp-common -ljsonrpccpp-server -ljsoncpp -lprocps intel-sgx-ssl/Linux/package/lib64/libsgx_usgxssl.a
sgxwallet_LDADD=-l$(SGX_URTS_LIB) -Lleveldb/build -LlibBLS/build -LlibBLS/build/libff/libff -l:libbls.a -l:libleveldb.a \
-l:libff.a -lgmp -ldl -l:libsgx_capable.a -l:libsgx_tprotected_fs.a -ljsonrpccpp-stub -lpthread -ljsonrpccpp-common \
-ljsonrpccpp-server -ljsonrpccpp-client -ljsoncpp -lcurl -lprocps intel-sgx-ssl/Linux/package/lib64/libsgx_usgxssl.a
testw_SOURCES=testw.cpp SGXWalletServer.cpp RPCException.cpp BLSCrypto.cpp ServerInit.cpp LevelDB.cpp BLSPrivateKeyShareSGX.cpp $(COMMON_SRC)
testw_SOURCES=testw.cpp stubclient.cpp SGXWalletServer.cpp RPCException.cpp BLSCrypto.cpp ServerInit.cpp LevelDB.cpp \
BLSPrivateKeyShareSGX.cpp ECDSACrypto.cpp $(COMMON_SRC)
nodist_testw_SOURCES=${nodist_sgxwallet_SOURCES}
EXTRA_testw_DEPENDENCIES=${EXTRA_sgxwallet_DEPENDENCIES}
testw_LDADD= ${sgxwallet_LDADD}
......@@ -23,6 +23,7 @@
#include "RPCException.h"
#include "LevelDB.h"
#include "BLSCrypto.h"
#include "ECDSACrypto.h"
#include "SGXWalletServer.h"
#include "SGXWalletServer.hpp"
......@@ -30,11 +31,20 @@ SGXWalletServer::SGXWalletServer(AbstractServerConnector &connector,
serverVersion_t type)
: AbstractStubServer(connector, type) {}
SGXWalletServer* s = nullptr;
HttpServer* hs = nullptr;
int init_server() {
HttpServer httpserver(1025);
SGXWalletServer s(httpserver,
JSONRPC_SERVER_V1V2); // hybrid server (json-rpc 1.0 & 2.0)
s.StartListening();
hs = new HttpServer(1025);
s = new SGXWalletServer(*hs,
JSONRPC_SERVER_V2); // hybrid server (json-rpc 1.0 & 2.0)
if (!s->StartListening()) {
cerr << "Server could not start listening" << endl;
exit(-1);
}
return 0;
}
......@@ -136,19 +146,34 @@ Json::Value importECDSAKeyImpl(const std::string &key, const std::string &keyNam
Json::Value generateECDSAKeyImpl(const std::string &_keyName) {
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
result["encryptedKey"] = "";
cerr << "Calling method" << endl;
std::vector<std::string>keys;
try {
writeECDSAKey(_keyName, "");
keys = gen_ecdsa_key();
if (keys.size() == 0 ) {
throw RPCException(UNKNOWN_ERROR, "");
}
writeECDSAKey(_keyName, keys.at(0));
} catch (RPCException &_e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
}
result["encryptedKey"] = keys.at(0);
result["PublicKey"] = keys.at(1);
std::cerr << "in SGXWalletServer encr key x " << keys.at(0) << std::endl;
return result;
}
......@@ -157,16 +182,24 @@ Json::Value ecdsaSignMessageHashImpl(const std::string &_keyName, const std::str
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
result["signature"] = "";
result["signature_v"] = "";
result["signature_r"] = "";
result["signature_s"] = "";
std::vector<std::string> sign_vect(3);
try {
readECDSAKey(_keyName);
std::shared_ptr<std::string> key_ptr = readECDSAKey(_keyName);
sign_vect = ecdsa_sign_hash ((*key_ptr).c_str(), messageHash.c_str());
} catch (RPCException &_e) {
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
}
result["signature_v"] = sign_vect.at(0);
result["signature_r"] = sign_vect.at(1);
result["signature_s"] = sign_vect.at(2);
return result;
}
......@@ -228,11 +261,28 @@ void writeKeyShare(const string &_keyShareName, const string &value, int index,
levelDb->writeString(key, value);
}
shared_ptr <std::string> readECDSAKey(const string &_keyShare) {
return nullptr;
shared_ptr <std::string> readECDSAKey(const string &_keyName) {
auto keyStr = levelDb->readString("ECDSAKEY:" + _keyName);
if (keyStr == nullptr) {
throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "Key share with this name does not exists");
}
return keyStr;
}
void writeECDSAKey(const string &_keyShare, const string &value) {
void writeECDSAKey(const string &_keyName, const string &value) {
Json::Value val;
Json::FastWriter writer;
val["value"] = value;
std::string json = writer.write(val);
auto key = "ECDSAKEY:" + _keyName;
if (levelDb->readString(_keyName) != nullptr) {
throw new RPCException(KEY_SHARE_DOES_NOT_EXIST, "Key with this name already exists");
}
levelDb->writeString(key, value);
}
\ No newline at end of file
......@@ -12,6 +12,8 @@ using namespace std;
class SGXWalletServer : public AbstractStubServer {
SGXWalletServer* server = nullptr;
public:
SGXWalletServer(AbstractServerConnector &connector, serverVersion_t type);
......@@ -33,7 +35,7 @@ void writeKeyShare(const string &_keyShareName, const string &value, int index,
shared_ptr<std::string> readKeyShare(const string& _keyShare);
void writeECDSAKey(const string& _key, const string& value);
void writeECDSAKey(const string& _keyName, const string& value);
shared_ptr<std::string> readECDSAKey(const string& _key);
......@@ -42,7 +44,7 @@ Json::Value importBLSKeyShareImpl(int index, const std::string& keyShare, const
Json::Value blsSignMessageHashImpl(const std::string& keyShareName, const std::string& messageHash);
Json::Value importECDSAKeyImpl(const std::string& key, const std::string& keyName);
Json::Value generateECDSAKeyImpl(const std::string& keyName);
Json::Value ecdsaSignMessageHashImpl(const std::string& keyShareName, const std::string& messageHash);
Json::Value ecdsaSignMessageHashImpl(const std::string& keyName, const std::string& messageHash);
......
This diff is collapsed.
This diff is collapsed.
ECDSAUtils.o: ECDSAUtils.c ECDSAUtils.h ../tgmp-build/include/sgx_tgmp.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h \
domain_parameters.h point.h signature.h curves.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h
ECDSAUtils.h:
../tgmp-build/include/sgx_tgmp.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h:
domain_parameters.h:
point.h:
signature.h:
curves.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h:
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
curves.o: curves.c ../tgmp-build/include/sgx_tgmp.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h \
domain_parameters.h curves.h point.h
../tgmp-build/include/sgx_tgmp.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h:
domain_parameters.h:
curves.h:
point.h:
domain_parameters.o: domain_parameters.c ../tgmp-build/include/sgx_tgmp.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h \
domain_parameters.h point.h
../tgmp-build/include/sgx_tgmp.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h:
domain_parameters.h:
point.h:
numbertheory.o: numbertheory.c \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h \
../tgmp-build/include/sgx_tgmp.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h \
numbertheory.h
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h:
../tgmp-build/include/sgx_tgmp.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h:
numbertheory.h:
point.o: point.c \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h \
../tgmp-build/include/sgx_tgmp.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/assert.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h \
domain_parameters.h point.h numbertheory.h
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h:
../tgmp-build/include/sgx_tgmp.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/assert.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h:
domain_parameters.h:
point.h:
numbertheory.h:
secure_enclave.o: secure_enclave.c \
../intel-sgx-ssl/Linux/package/include/openssl/ecdsa.h \
../intel-sgx-ssl/Linux/package/include/openssl/ec.h \
../intel-sgx-ssl/Linux/package/include/openssl/opensslconf.h \
../intel-sgx-ssl/Linux/package/include/openssl/opensslv.h \
../intel-sgx-ssl/Linux/package/include/openssl/asn1.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/time.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \
../intel-sgx-ssl/Linux/package/include/openssl/e_os2.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/inttypes.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/stdint.h \
../intel-sgx-ssl/Linux/package/include/openssl/bio.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h \
../intel-sgx-ssl/Linux/package/include/openssl/crypto.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h \
../intel-sgx-ssl/Linux/package/include/openssl/safestack.h \
../intel-sgx-ssl/Linux/package/include/openssl/stack.h \
../intel-sgx-ssl/Linux/package/include/openssl/ossl_typ.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h \
../intel-sgx-ssl/Linux/package/include/openssl/cryptoerr.h \
../intel-sgx-ssl/Linux/package/include/openssl/symhacks.h \
../intel-sgx-ssl/Linux/package/include/pthread.h \
../intel-sgx-ssl/Linux/package/include/openssl/bioerr.h \
../intel-sgx-ssl/Linux/package/include/openssl/asn1err.h \
../intel-sgx-ssl/Linux/package/include/openssl/bn.h \
../intel-sgx-ssl/Linux/package/include/openssl/bnerr.h \
../intel-sgx-ssl/Linux/package/include/openssl/ecerr.h \
../intel-sgx-ssl/Linux/package/include/openssl/evp.h \
../intel-sgx-ssl/Linux/package/include/openssl/evperr.h \
../intel-sgx-ssl/Linux/package/include/openssl/objects.h \
../intel-sgx-ssl/Linux/package/include/openssl/obj_mac.h \
../intel-sgx-ssl/Linux/package/include/openssl/objectserr.h \
../intel-sgx-ssl/Linux/package/include/openssl/err.h \
../intel-sgx-ssl/Linux/package/include/openssl/lhash.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/errno.h \
../intel-sgx-ssl/Linux/package/include/openssl/rand.h \
../intel-sgx-ssl/Linux/package/include/openssl/randerr.h \
../intel-sgx-ssl/Linux/package/include/tSgxSSL_api.h secure_enclave_t.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdint.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/wchar.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_edger8r.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_defs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_eid.h \
/home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_tcrypto.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_attributes.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_key.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_report.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_tseal.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_tcrypto.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/math.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/float.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h \
domain_parameters.h point.h signature.h curves.h ../sgxwallet_common.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/unistd.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/endian.h
../intel-sgx-ssl/Linux/package/include/openssl/ecdsa.h:
../intel-sgx-ssl/Linux/package/include/openssl/ec.h:
../intel-sgx-ssl/Linux/package/include/openssl/opensslconf.h:
../intel-sgx-ssl/Linux/package/include/openssl/opensslv.h:
../intel-sgx-ssl/Linux/package/include/openssl/asn1.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/time.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h:
../intel-sgx-ssl/Linux/package/include/openssl/e_os2.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/inttypes.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/stdint.h:
../intel-sgx-ssl/Linux/package/include/openssl/bio.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h:
../intel-sgx-ssl/Linux/package/include/openssl/crypto.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h:
../intel-sgx-ssl/Linux/package/include/openssl/safestack.h:
../intel-sgx-ssl/Linux/package/include/openssl/stack.h:
../intel-sgx-ssl/Linux/package/include/openssl/ossl_typ.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h:
../intel-sgx-ssl/Linux/package/include/openssl/cryptoerr.h:
../intel-sgx-ssl/Linux/package/include/openssl/symhacks.h:
../intel-sgx-ssl/Linux/package/include/pthread.h:
../intel-sgx-ssl/Linux/package/include/openssl/bioerr.h:
../intel-sgx-ssl/Linux/package/include/openssl/asn1err.h:
../intel-sgx-ssl/Linux/package/include/openssl/bn.h:
../intel-sgx-ssl/Linux/package/include/openssl/bnerr.h:
../intel-sgx-ssl/Linux/package/include/openssl/ecerr.h:
../intel-sgx-ssl/Linux/package/include/openssl/evp.h:
../intel-sgx-ssl/Linux/package/include/openssl/evperr.h:
../intel-sgx-ssl/Linux/package/include/openssl/objects.h:
../intel-sgx-ssl/Linux/package/include/openssl/obj_mac.h:
../intel-sgx-ssl/Linux/package/include/openssl/objectserr.h:
../intel-sgx-ssl/Linux/package/include/openssl/err.h:
../intel-sgx-ssl/Linux/package/include/openssl/lhash.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/errno.h:
../intel-sgx-ssl/Linux/package/include/openssl/rand.h:
../intel-sgx-ssl/Linux/package/include/openssl/randerr.h:
../intel-sgx-ssl/Linux/package/include/tSgxSSL_api.h:
secure_enclave_t.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdint.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/wchar.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_edger8r.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_defs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_eid.h:
/home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_tcrypto.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_attributes.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_key.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_report.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_tseal.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_tcrypto.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/math.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/float.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h:
domain_parameters.h:
point.h:
signature.h:
curves.h:
../sgxwallet_common.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/unistd.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/endian.h:
secure_enclave_t.o: secure_enclave_t.c secure_enclave_t.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdint.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/stdint.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/wchar.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_edger8r.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_defs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_eid.h \
/home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_lfence.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/errno.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/mbusafecrt.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h
secure_enclave_t.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdint.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/stdint.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/wchar.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_edger8r.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_defs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_eid.h:
/home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_lfence.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/errno.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/mbusafecrt.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h:
sha1.o: sha1.c \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdint.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/stdint.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdint.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/stdint.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/string.h:
signature.o: signature.c \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h \
../tgmp-build/include/sgx_tgmp.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/assert.h \
domain_parameters.h point.h signature.h numbertheory.h random.h
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdlib.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/cdefs.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/_types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdio.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdarg.h:
../tgmp-build/include/sgx_tgmp.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stddef.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/limits.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/assert.h:
domain_parameters.h:
point.h:
signature.h:
numbertheory.h:
random.h:
......@@ -85,6 +85,7 @@ CLEANFILES+= secure_enclave_t.c secure_enclave_t.h
secure_enclave_SOURCES = secure_enclave_t.c secure_enclave_t.h \
secure_enclave.c \
curves.c domain_parameters.c numbertheory.c point.c signature.c \
DKGUtils.cpp BLSEnclave.cpp ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.cpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g2.cpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.cpp $(ENCLAVE_KEY) $(ENCLAVE_CONFIG)
......@@ -101,9 +102,9 @@ AM_LDFLAGS += $(TGMP_LDFLAGS)
## This line is REQUIRED. It can't be generically defined for
## automake, so you must specify it for your enclave. Note that you
## can't say $(ENCLAVE)_LDADD here: you must spell out the enclave name.
## can't say $(ENCLAVE)_LDADD here: you must spell out the enclave name.../intel-sgx-ssl/Linux/package/lib64/libsgx_tsgxssl.a
## If you add flags to it, you MUST include @SGX_ENCLAVE_LDADD@ as part
## of the definition to make sure you pick up the right linker flags
## of the definition to make sure you pick up the right linker flags../intel-sgx-ssl/Linux/package/lib64/libsgx_tsgxssl.a
## and SGX trusted libraries.
secure_enclave_LDADD = @SGX_ENCLAVE_LDADD@
......@@ -114,8 +115,8 @@ secure_enclave_LDADD = @SGX_ENCLAVE_LDADD@
## --startgroup and --endgroup flags. (This would be where you'd add
## SGXSSL libraries, and your trusted c++ library
SGX_EXTRA_TLIBS=-lsgx_tgmp -lsgx_tservice -lsgx_urts -lsgx_tcxx ../intel-sgx-ssl/Linux/package/lib64/libsgx_tsgxssl_crypto.a \
../intel-sgx-ssl/Linux/package/lib64/libsgx_tsgxssl.a
SGX_EXTRA_TLIBS=-lsgx_tgmp -lsgx_tservice -lsgx_urts -lsgx_tcxx -lsgx_tcrypto
......
## This line must come first when building an Intel SGX enclave.
include $(top_srcdir)/build-aux/sgx_enclave.am
## It sets the following Automake variables:
##
## EXEEXT=.so
## AM_CPPFLAGS = -I$(SGXSDK_INCDIR)
## AM_LDFLAGS = -L$(SGXSDK_LIBDIR)
## libexec_PROGRAMS = $(ENCLAVE)
## CLEANFILES = $(ENCLAVE).signed.so
##
## and places required compiler flags in:
##
## AM_CFLAGS
## AM_CXXFLAGS
##
## It adds a pattern rule for building proxy functions from EDL files:
##
## %_u.h %_u.c: %.edl
##
## And creates build targets for a signed enclave, generating a
## temporary private signing key, and a basic enclave config file:
##
## $(ENCLAVE_CONFIG):
## $(ENCLAVE_KEY):
## $(ENCLAVE).signed$(EXEEXT): $(ENCLAVE)$(EXEEXT)
##
## And sets these Makefile variables:
##
## SGXSDK
## SGXSDK_BINDIR
## SGXSDK_INCDIR
## SGXSDK_LIBDIR
## SGXSSL
## SGXSSL_BINDIR
## SGXSSL_INCDIR
## SGXSSL_LIBDIR
## SGX_TRTS_LIB
## SGX_TSERVICE_LIB
## SGX_EDGER8R
## SGX_SIGN
## The name of your enclave, enclave config file, and private key
## file go in these variables. The ENCLAVE variable creates the
## following automake target defn:
##
## libexec_PROGRAMS=$(ENCLAVE)
ENCLAVE=secure_enclave
ENCLAVE_CONFIG=$(ENCLAVE).config.xml
ENCLAVE_KEY=$(ENCLAVE)_private.pem
## Provide additional flags to sgx_sign when signing the enclave.
## This is almost never necessary. If you don't know if you need
## this, you probably don't.
## SGX_SIGN_FLAGS =
## Additional Automake flags needed to build the enclave.
##
AM_CPPFLAGS += -Wall -Wno-implicit-function-declaration $(TGMP_CPPFLAGS) -I../trusted_libff -I../sgx-sdk-build/sgxsdk/include/libcxx \
-I../intel-sgx-ssl/Linux/package/include
AM_CXXFLAGS += -fno-builtin
## Additional files to remove with 'make clean'. This list needs
## to include your edger8r genreated files.
CLEANFILES+= secure_enclave_t.c secure_enclave_t.h
## Supply additional flags to edger8r here.
##
## SGX_EDGER8R_FLAGS=
## Put your sources here. Don't forget to list the _t.c and _t.h
## files. You can't use the $(ENCLAVE) variable in the build
## target name (i.e., $(ENCLAVE)_SOURCES will not work).
secure_enclave_SOURCES = secure_enclave_t.c secure_enclave_t.h \
secure_enclave.c \
<<<<<<< HEAD
DKGUtils.cpp BLSUtils.cpp ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.cpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g2.cpp \
=======
BLSEnclave.cpp ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.cpp \
>>>>>>> master
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.cpp $(ENCLAVE_KEY) $(ENCLAVE_CONFIG)
## Add additional linker flags to AM_LDFLAGS here. Don't put
## libraries flags here (see below).
##
## Be sure to use += to add to, and not replace, the default
## AM_LDFLAGS.
AM_LDFLAGS += $(TGMP_LDFLAGS)
## This line is REQUIRED. It can't be generically defined for
## automake, so you must specify it for your enclave. Note that you
## can't say $(ENCLAVE)_LDADD here: you must spell out the enclave name.
## If you add flags to it, you MUST include @SGX_ENCLAVE_LDADD@ as part
## of the definition to make sure you pick up the right linker flags
## and SGX trusted libraries.
secure_enclave_LDADD = @SGX_ENCLAVE_LDADD@
## Place any additional trusted libraries that your enclave may need in
## SGX_EXTRA_TLIBS. This will ensure they get place inside the
## --startgroup and --endgroup flags. (This would be where you'd add
## SGXSSL libraries, and your trusted c++ library
SGX_EXTRA_TLIBS=-lsgx_tgmp -lsgx_tservice -lsgx_urts -lsgx_tcxx ../intel-sgx-ssl/Linux/package/lib64/libsgx_tsgxssl_crypto.a
## This line is OPTIONAL, and comes with a WARNING.
##
## In general, you shouldn't need to use the program-specific LDFLAGS
## instead of AM_LDFLAGS. But, if you need to, then you'll need to ensure
## @SGX_ENCLAVE_LDFLAGS@ is included in the definition as this will
## override AM_LDFLAGS.
##
## secure_enclave_LDFLAGS = @SGX_ENCLAVE_LDFLAGS@
##
......@@ -107,7 +107,9 @@ am__installdirs = "$(DESTDIR)$(libexecdir)"
PROGRAMS = $(libexec_PROGRAMS)
am__objects_1 =
am_secure_enclave_OBJECTS = secure_enclave_t.$(OBJEXT) \
secure_enclave.$(OBJEXT) DKGUtils.$(OBJEXT) \
secure_enclave.$(OBJEXT) curves.$(OBJEXT) \
domain_parameters.$(OBJEXT) numbertheory.$(OBJEXT) \
point.$(OBJEXT) signature.$(OBJEXT) DKGUtils.$(OBJEXT) \
BLSEnclave.$(OBJEXT) alt_bn128_init.$(OBJEXT) \
alt_bn128_g2.$(OBJEXT) alt_bn128_g1.$(OBJEXT) $(am__objects_1) \
$(am__objects_1)
......@@ -139,8 +141,10 @@ am__maybe_remake_depfiles = depfiles
am__depfiles_remade = ./$(DEPDIR)/BLSEnclave.Po \
./$(DEPDIR)/DKGUtils.Po ./$(DEPDIR)/alt_bn128_g1.Po \
./$(DEPDIR)/alt_bn128_g2.Po ./$(DEPDIR)/alt_bn128_init.Po \
./$(DEPDIR)/curves.Po ./$(DEPDIR)/domain_parameters.Po \
./$(DEPDIR)/numbertheory.Po ./$(DEPDIR)/point.Po \
./$(DEPDIR)/secure_enclave.Po ./$(DEPDIR)/secure_enclave_t.Po \
./$(DEPDIR)/signed_enclave_debug.Po \
./$(DEPDIR)/signature.Po ./$(DEPDIR)/signed_enclave_debug.Po \
./$(DEPDIR)/signed_enclave_rel.Po
am__mv = mv -f
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
......@@ -339,14 +343,13 @@ ENCLAVE_CONFIG = $(ENCLAVE).config.xml
ENCLAVE_KEY = $(ENCLAVE)_private.pem
secure_enclave_SOURCES = secure_enclave_t.c secure_enclave_t.h \
secure_enclave.c \
curves.c domain_parameters.c numbertheory.c point.c signature.c \
DKGUtils.cpp BLSEnclave.cpp ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.cpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g2.cpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.cpp $(ENCLAVE_KEY) $(ENCLAVE_CONFIG)
secure_enclave_LDADD = @SGX_ENCLAVE_LDADD@
SGX_EXTRA_TLIBS = -lsgx_tgmp -lsgx_tservice -lsgx_urts -lsgx_tcxx ../intel-sgx-ssl/Linux/package/lib64/libsgx_tsgxssl_crypto.a \
../intel-sgx-ssl/Linux/package/lib64/libsgx_tsgxssl.a
SGX_EXTRA_TLIBS = -lsgx_tgmp -lsgx_tservice -lsgx_urts -lsgx_tcxx -lsgx_tcrypto
all: all-am
.SUFFIXES:
......@@ -439,8 +442,13 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/alt_bn128_g1.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/alt_bn128_g2.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/alt_bn128_init.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/curves.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/domain_parameters.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/numbertheory.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/point.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/secure_enclave.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/secure_enclave_t.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/signature.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/signed_enclave_debug.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/signed_enclave_rel.Po@am__quote@ # am--include-marker
......@@ -653,8 +661,13 @@ distclean: distclean-am
-rm -f ./$(DEPDIR)/alt_bn128_g1.Po
-rm -f ./$(DEPDIR)/alt_bn128_g2.Po
-rm -f ./$(DEPDIR)/alt_bn128_init.Po
-rm -f ./$(DEPDIR)/curves.Po
-rm -f ./$(DEPDIR)/domain_parameters.Po
-rm -f ./$(DEPDIR)/numbertheory.Po
-rm -f ./$(DEPDIR)/point.Po
-rm -f ./$(DEPDIR)/secure_enclave.Po
-rm -f ./$(DEPDIR)/secure_enclave_t.Po
-rm -f ./$(DEPDIR)/signature.Po
-rm -f ./$(DEPDIR)/signed_enclave_debug.Po
-rm -f ./$(DEPDIR)/signed_enclave_rel.Po
-rm -f Makefile
......@@ -707,8 +720,13 @@ maintainer-clean: maintainer-clean-am
-rm -f ./$(DEPDIR)/alt_bn128_g1.Po
-rm -f ./$(DEPDIR)/alt_bn128_g2.Po
-rm -f ./$(DEPDIR)/alt_bn128_init.Po
-rm -f ./$(DEPDIR)/curves.Po
-rm -f ./$(DEPDIR)/domain_parameters.Po
-rm -f ./$(DEPDIR)/numbertheory.Po
-rm -f ./$(DEPDIR)/point.Po
-rm -f ./$(DEPDIR)/secure_enclave.Po
-rm -f ./$(DEPDIR)/secure_enclave_t.Po
-rm -f ./$(DEPDIR)/signature.Po
-rm -f ./$(DEPDIR)/signed_enclave_debug.Po
-rm -f ./$(DEPDIR)/signed_enclave_rel.Po
-rm -f Makefile
......
#include <../tgmp-build/include/sgx_tgmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "domain_parameters.h"
#include "curves.h"
#include "point.h"
/*Load a curve depending on it's curve number, defined by the enum*/
void domain_parameters_load_curve(domain_parameters out, curve_list curve)
{
switch(curve)
{
case secp112r1:
domain_parameters_set_hex(out, "secp112r1",
"DB7C2ABF62E35E668076BEAD208B",
"DB7C2ABF62E35E668076BEAD2088",
"659EF8BA043916EEDE8911702B22",
"09487239995A5EE76B55F9C2F098",
"A89CE5AF8724C0A23E0E0FF77500",
"DB7C2ABF62E35E7628DFAC6561C5",
"01");
break;
//See remark in curves.h
/*case secp112r2:
domain_parameters_set_hex(out, "secp112r2",
"DB7C2ABF62E35E668076BEAD208B",
"6127C24C05F38A0AAAF65C0EF02C",
"51DEF1815DB5ED74FCC34C85D709",
"4BA30AB5E892B4E1649DD0928643",
"ADCD46F5882E3747DEF36E956E97",
"36DF0AAFD8B8D7597CA10520D04B",
"04");
*/
break;
case secp128r1:
domain_parameters_set_hex(out, "secp128r1",
"FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF",
"FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC",
"E87579C11079F43DD824993C2CEE5ED3",
"161FF7528B899B2D0C28607CA52C5B86",
"CF5AC8395BAFEB13C02DA292DDED7A83",
"FFFFFFFE0000000075A30D1B9038A115",
"01");
break;
//See remark in curves.h
/*case secp128r2:
domain_parameters_set_hex(out, "secp128r2",
"FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF",
"D6031998D1B3BBFEBF59CC9BBFF9AEE1",
"5EEEFCA380D02919DC2C6558BB6D8A5D",
"7B6AA5D85E572983E6FB32A7CDEBC140",
"27B6916A894D3AEE7106FE805FC34B44",
"3FFFFFFF7FFFFFFFBE0024720613B5A3",
"04");
*/
break;
case secp160k1:
domain_parameters_set_hex(out, "secp160k1",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73",
"0000000000000000000000000000000000000000",
"0000000000000000000000000000000000000007",
"3B4C382CE37AA192A4019E763036F4F5DD4D7EBB",
"938CF935318FDCED6BC28286531733C3F03C4FEE",
"0100000000000000000001B8FA16DFAB9ACA16B6B3",
"01");
break;
case secp160r1:
domain_parameters_set_hex(out, "secp160r1",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC",
"1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45",
"4A96B5688EF573284664698968C38BB913CBFC82",
"23A628553168947D59DCC912042351377AC5FB32",
"0100000000000000000001F4C8F927AED3CA752257",
"01");
break;
case secp160r2:
domain_parameters_set_hex(out, "secp160r2",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC70",
"B4E134D3FB59EB8BAB57274904664D5AF50388BA",
"52DCB034293A117E1F4FF11B30F7199D3144CE6D",
"FEAFFEF2E331F296E071FA0DF9982CFEA7D43F2E",
"0100000000000000000000351EE786A818F3A1A16B",
"01");
break;
case secp192k1:
domain_parameters_set_hex(out, "secp192k1",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37",
"000000000000000000000000000000000000000000000000",
"000000000000000000000000000000000000000000000003",
"DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D",
"9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D",
"FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D",
"01");
break;
case secp192r1:
domain_parameters_set_hex(out, "secp192r1",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC",
"64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1",
"188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012",
"07192B95FFC8DA78631011ED6B24CDD573F977A11E794811",
"FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831",
"01");
break;
case secp224k1:
domain_parameters_set_hex(out, "secp224k1",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D",
"00000000000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000000005",
"A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C",
"7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5",
"010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7",
"01");
break;
case secp224r1:
domain_parameters_set_hex(out, "secp224r1",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
"B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
"B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
"BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
"01");
break;
case secp256k1:
domain_parameters_set_hex(out, "secp256k1",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F",
"0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000007",
"79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
"483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",
"01");
break;
case secp256r1:
domain_parameters_set_hex(out, "secp256r1",
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF",
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC",
"5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B",
"6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296",
"4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5",
"FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551",
"01");
break;
case secp384r1:
domain_parameters_set_hex(out, "secp384r1",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC",
"B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF",
"AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7",
"3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973",
"01");
break;
case secp521r1:
domain_parameters_set_hex(out, "secp521r1",
"01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
"01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC",
"0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00",
"00C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66",
"011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650",
"01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409",
"01");
break;
default:
break;
//Write error message
//fprintf(stderr, "\nCurve number not available!\n");
//exit(4); //Exit with 4, internal inconsistency
}
}
/*Curves that can be loaded using domain_parameters_load_curve()*/
typedef enum { secp112r1 = 0,
//secp112r2, //See remark below
secp128r1,
//secp128r2, //See remark below
secp160k1,
secp160r1,
secp160r2,
secp192k1,
secp192r1,
secp224k1,
secp224r1,
secp256k1,
secp256r1,
secp384r1,
secp521r1 } curve_list;
/*Number of curves that can be loaded using domain_parameters_load_curve()*/
#define NUMBER_OF_CURVES (secp521r1+1)
/*Load a curve depending on it's curve number, defined by the enum*/
void domain_parameters_load_curve(domain_parameters out, curve_list curve);
/* REMARK:
For some weird reason secp112r2 and secp128r2 doesn't want to be stable. Actually they work once in a while. However running the benchmark command gives -1 as operation time, sometimes and only sometimes!
Benchmark is supposed to write -1 as operation time if the operation failed.
As I don't see any reason to have extent my headache I'm putting this problem to rest by commenting these curves out. Furthermore it's very limited in terms of what can be done to debug these curves, since I don't have any test vectors for them.
It's probably related to a bug in the implementation, perhaps an exception I didn't handle or round of, who knows.
*/
#include <../tgmp-build/include/sgx_tgmp.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "domain_parameters.h"
#include "point.h"
/*Initialize a curve*/
domain_parameters domain_parameters_init()
{
domain_parameters curve;
curve = malloc(sizeof(struct domain_parameters_s));
//Initialize all members
mpz_init(curve->p);
mpz_init(curve->a);
mpz_init(curve->b);
curve->G = point_init();
mpz_init(curve->n);
mpz_init(curve->h);
return curve;
}
/*Sets the name of a curve*/
void domain_parameters_set_name(domain_parameters curve, char* name)
{
int len = strlen(name);
curve->name = (char*)malloc( sizeof(char) * (len+1) );
curve->name[len] = '\0';
strncpy(curve->name, name, len+1);
}
/*Set domain parameters from decimal unsigned long ints*/
void domain_parameters_set_ui(domain_parameters curve,
char* name,
unsigned long int p,
unsigned long int a,
unsigned long int b,
unsigned long int Gx,
unsigned long int Gy,
unsigned long int n,
unsigned long int h)
{
domain_parameters_set_name(curve, name);
mpz_set_ui(curve->p, p);
mpz_set_ui(curve->a, a);
mpz_set_ui(curve->b, b);
point_set_ui(curve->G, Gx, Gy);
mpz_set_ui(curve->n, n);
mpz_set_ui(curve->h, h);
}
/*Set domain parameters from hexadecimal string*/
void domain_parameters_set_hex(domain_parameters curve, char* name, char* p, char* a, char* b, char* Gx, char* Gy, char* n, char* h)
{
domain_parameters_set_name(curve, name);
mpz_set_str(curve->p, p, 16);
mpz_set_str(curve->a, a, 16);
mpz_set_str(curve->b, b, 16);
point_set_hex(curve->G, Gx, Gy);
mpz_set_str(curve->n, n, 16);
mpz_set_str(curve->h, h, 16);
}
/*Release memory*/
void domain_parameters_clear(domain_parameters curve)
{
mpz_clear(curve->p);
mpz_clear(curve->a);
mpz_clear(curve->b);
point_clear(curve->G);
mpz_clear(curve->n);
mpz_clear(curve->h);
free(curve->name);
free(curve);
}
/*Type that represents a point*/
typedef struct point_s* point;
struct point_s
{
mpz_t x;
mpz_t y;
bool infinity;
};
/*Type that represents a curve*/
typedef struct domain_parameters_s* domain_parameters;
struct domain_parameters_s
{
char* name;
mpz_t p; //Prime
mpz_t a; //'a' parameter of the elliptic curve
mpz_t b; //'b' parameter of the elliptic curve
point G; //Generator point of the curve, also known as base point.
mpz_t n;
mpz_t h;
};
/*Initialize a curve*/
domain_parameters domain_parameters_init();
/*Sets the name of a curve*/
void domain_parameters_set_name(domain_parameters curve, char* name);
/*Set domain parameters from decimal unsigned long ints*/
void domain_parameters_set_ui(domain_parameters curve,
char* name,
unsigned long int p,
unsigned long int a,
unsigned long int b,
unsigned long int Gx,
unsigned long int Gy,
unsigned long int n,
unsigned long int h);
/*Set domain parameters from hexadecimal string*/
void domain_parameters_set_hex(domain_parameters curve, char* name, char* p, char* a, char* b, char* Gx, char* Gy, char* n, char* h);
/*Release memory*/
void domain_parameters_clear(domain_parameters curve);
#include <stdlib.h>
#include <stdio.h>
#include <../tgmp-build/include/sgx_tgmp.h>
#include <stdbool.h>
#include "numbertheory.h"
/*Calculate R = a^k mod P, using repeated square-and-multiply algorithm
*Handbook of applied cryptography: Algorithm 2.143. */
void number_theory_exp_modp(mpz_t R, mpz_t a, mpz_t k, mpz_t P)
{
#if EXTERNAL_NUMBER_THEORY_IMPLEMENTATION
//Do this using gmp number theory implementation
mpz_powm(R, a, k, P);
#else
//Variable A and b
mpz_t A;mpz_init(A);
mpz_t b;mpz_init(b);
int i; //Illiterator
int t = mpz_sizeinbase(k, 2); //Set t = bit length
//Temporary variables
mpz_t t1; mpz_init(t1);
mpz_t t2; mpz_init(t2);
//Set b = 1
mpz_set_ui(b, 1);
//If k = 0, return b; if not run through the bit loop
if(mpz_sgn(k))
{
//Set A = a
mpz_set(A, a);
//If k_0 = 1
if(mpz_tstbit(k,0))
mpz_set(b,a);
for(i = 1; i < t; i++)
{
//Set A = A² mod P
mpz_set(t1, A);
mpz_mul(t2, t1, A);
mpz_mod(A, t2, P);
//If k_i = 1
if(mpz_tstbit(k,i))
{
//Set b = A * b mod P
mpz_mul(t1, A, b);
mpz_mod(b, t1, P);
}
}
}
//Return b
mpz_set(R, b);
//Clear variables
mpz_clear(A);
mpz_clear(b);
mpz_clear(t1);
mpz_clear(t2);
#endif
}
/*Calculate R = a^k mod P, wraps around number_theory_exp_modp() */
void number_theory_exp_modp_ui(mpz_t R, mpz_t a, unsigned long int k, mpz_t P)
{
#if EXTERNAL_NUMBER_THEORY_IMPLEMENTATION
//Do this using gmp number theory implementation
mpz_powm_ui(R, a, k, P);
#else
mpz_t K;
//Initialize and set a once
mpz_init_set_ui(K, k);
//Calculate exponentiation
number_theory_exp_modp(R, a, K, P);
//Release memory
mpz_clear(K);
#endif
}
/*Calculates R² mod P = a, the squareroot of a mod P
*Handbook of applied cryptography: Algorithm 3.36, 3.37 and 3.34 */
void number_theory_squareroot_modp(mpz_t R, mpz_t a, mpz_t P)
{
//Calculate the legendre symbol
int legendre = number_theory_legendre(a, P);
//Initialize temporary variables
mpz_t t1;mpz_init(t1);
mpz_t t2;mpz_init(t2);
mpz_t t3;mpz_init(t3);
mpz_t t4;mpz_init(t4);
mpz_t t5;mpz_init(t5);
//Is a a quadratic nonresidue?
if(legendre == -1)
{
//Set R = 0
mpz_set_ui(R, 0);
}else{
//If P mod 4 = 3
mpz_mod_ui(t1, P, 4);
if(mpz_cmp_ui(t1, 3) == 0) //Algorithm 3.36, a specialization of algorithm 3.34
{
//Calculate R = a^((P+1)/4)
mpz_add_ui(t1, P, 1); //t1 = P - 1
mpz_divexact_ui(t3, t1, 4);//t3 = t1 / 4
number_theory_exp_modp(R, a, t3, P); //R = a^t3 mod P
}else{ //Algorithm 3.37, a specialization of algorithm 3.34
//If P mod 8 = 5
mpz_mod_ui(t1, P, 8);
if(mpz_cmp_ui(t1, 5) == 0)
{
//Initialize d
mpz_t d;mpz_init(d);
//Calculate d = a^((P-1)/4)
mpz_sub_ui(t1, P, 1); //t1 = P - 1
mpz_divexact_ui(t3, t1, 4);//t3 = t1 / 4
number_theory_exp_modp(d, a, t3, P); //d = a^t3 mod P
//If d = 1
if(mpz_cmp_ui(d, 1) == 0)
{
//Calculate R = a^((P+3)/8)
mpz_add_ui(t1, P, 3); //t1 = P - 3
mpz_divexact_ui(t3, t1, 8);//t3 = t1 / 8
number_theory_exp_modp(R, a, t3, P); //R = a^t3 mod P
}else{
//If d = P - 1
mpz_sub_ui(t1, P, 1);
if(mpz_cmp(d, t1) == 0){
//Calculate R = 2a*(4a)^((P-5)/8)
mpz_mul_ui(t1, a, 4); //t1 = 4*a
mpz_mod(t4, t1, P); //t4 = t1 mod P
mpz_sub_ui(t1, P, 5); //t1 = P - 5
mpz_divexact_ui(t3, t1, 8);//t3 = t1 / 8
number_theory_exp_modp(t1, t4, t3, P); //t1 = (t4)^t3 mod P
mpz_mul_ui(t2, a, 2); //t2 = 2*a
mpz_mod(t3, t2, P); //t3 = t2 mod P
mpz_mul(t2, t1, t3); //t2 = t1*t2
mpz_mod(R, t2, P); //R = t2 mod P
}
}
//Clear d
mpz_clear(d);
}else{ //Algorithm 3.34
//Select b random quadratic nonresidue
mpz_t b; mpz_init(b);
gmp_randstate_t rstate; //Initialize random algorithm
gmp_randinit_default(rstate);
do
mpz_urandomm(b, rstate, P);
while(number_theory_legendre(b, P) != -1);
gmp_randclear(rstate);
//Find s and t, such as p-1 = 2^s*t, where t is odd
mpz_sub_ui(t1, P, 1); //t1 = p-1
unsigned long int s = mpz_scan1(t1, 0);
/* Scans the binary representation of t1 for 1 from behind, this gives us the
* number of times t1 can be devided with 2 before it gives an odd. This bit
* manipulation ought to be faster than repeated division by 2.
* Example:
* prime = 113 binary = 1110001
* prime - 1 = 112 binary = 1110000
* 112 / 2^4 = 7, 7 is odd.
*/
mpz_ui_pow_ui(t2, 2, s);//t2 = 2^s
mpz_t t; mpz_init(t);
mpz_divexact(t, t1, t2);//t = t1 / t2
//Computation of a^-1 mod p
mpz_t a_inv; mpz_init(a_inv);
number_theory_inverse(a_inv, a, P);
//Initialize variable for c and d
mpz_t c;mpz_init(c);
mpz_t d;mpz_init(d);
//Set c = b^t mod p
number_theory_exp_modp(c, b, t, P);
//Set R = a^((t+1)/2) mod p
mpz_add_ui(t1, t, 1); //t1 = t+1
mpz_divexact_ui(t2, t1, 2); //t2 = t1 / 2
number_theory_exp_modp(R , a, t2, P); //R = a^t2 mod p
unsigned long int i;
for(i = 1; i < s; i++)
{
//Set d = (R²*a_inv)^(2^(s-i-1)) mod p
number_theory_exp_modp_ui(t1, R, 2, P); //t1 = R²
mpz_mul(t2, t1, a_inv); //t2 = t1 * a_inv
mpz_mod(t5, t2, P); //t5 = t2 mod p
mpz_set_ui(t1, s-i-1); //t1 = s-i-1
mpz_set_ui(t2, 2); //t2 = 2
number_theory_exp_modp(t3, t2, t1, P); //t3 = t2^t1 mod p
number_theory_exp_modp(d , t5, t3, P); //d = t5^t3 mod p
//If d-(-1) mod p == 0, since d<p then we can use P-1 == d instead
mpz_sub_ui(t1, P, 1);
if(mpz_cmp(d, t1) == 0)
{
//Set R = R*c mod p
mpz_mul(t1, R, c); //t1 = R*c
mpz_mod(R, t1, P); //R = t1 mod p
}
//Set c = c² mod p
number_theory_exp_modp_ui(t1, c, 2, P); //t1 = c² mod p
mpz_set(c, t1); //c = t1
}
//Clear variables
mpz_clear(b);
mpz_clear(t);
mpz_clear(a_inv);
mpz_clear(c);
mpz_clear(d);
}
}
}
//TODO: implement algorithm 3.39
/*Algorithm 3.39 requires operations on the polynomial field Fx over F, and polynomial exponentiation, thus polynomial multiplication and reduction. According to Handbook of applied cryptography this algorithm should be faster than 3.34, when s in p-1 = 2^s*t, where t is odd, is large. But I've decided to settle with the two specializations of 3.34 and algorithm 3.34.*/
//Clear variables
mpz_clear(t1);
mpz_clear(t2);
mpz_clear(t3);
mpz_clear(t4);
mpz_clear(t5);
}
/*Calculate the multiplicative inverse of a mod p, using the extended euclidean algorithm
*Handbook of applied cryptography: Algorithm 2.107
*http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm*/
void number_theory_inverse(mpz_t R, mpz_t A, mpz_t P)
{
#if EXTERNAL_NUMBER_THEORY_IMPLEMENTATION
//Do this using gmp number theory implementation
mpz_invert(R, A, P);
#else
//Initialize variables
mpz_t a;mpz_init(a);
mpz_t b;mpz_init(b);
mpz_t q;mpz_init(q);
mpz_t r;mpz_init(r);
mpz_t x;mpz_init(x);
mpz_t lastx;mpz_init(lastx);
mpz_t y;mpz_init(y);
mpz_t lasty;mpz_init(lasty);
mpz_t t1;mpz_init(t1);
mpz_t t2;mpz_init(t2);
//Copy b, since we don't want to alter P or A
mpz_set(b, P);
mpz_set(a, A);
//Set variables
mpz_set_ui(x, 0);
mpz_set_ui(y, 1);
mpz_set_ui(lastx, 1);
mpz_set_ui(lasty, 0);
//while b != 0
while(mpz_sgn(b) != 0)
{
//r = a mod b;
mpz_mod(r, a, b);
//q = (a - r)/b
mpz_sub(t1, a, r);
mpz_divexact(q,t1,b);
//Set a = b
mpz_set(a, b);
//temp := x
//x := lastx-quotient*x
//lastx := temp
mpz_set(t1, x);
mpz_mul(t2, q, x);
mpz_sub(x, lastx, t2);
mpz_mod(lastx, t1, P);//We must keep it mod p, so why not just do it where instead of using set
//temp := y
//y := lasty-quotient*y
//lasty := temp
mpz_set(t1, y);
mpz_mul(t2, q, y);
mpz_sub(y, lasty, t2);
mpz_mod(lasty, t1, P);//We must keep it mod p, so why not just do it where instead of using set
//Set b = r
mpz_set(b, r);
}
/*d = a, greatest common divisitor
*lastx = x
*lasty = y
*in d = a*x+b*y
*Thus x is the multiplicative inverse of a mod b
*if d = 1, since otherwise there's no mulitplicative inverse.
*But when b is a prime, a must be coprime thus d=1
*/
//Set the result
mpz_set(R, lastx);
//Clear variables
mpz_clear(a);
mpz_clear(b);
mpz_clear(r);
mpz_clear(q);
mpz_clear(x);
mpz_clear(y);
mpz_clear(lastx);
mpz_clear(lasty);
mpz_clear(t1);
mpz_clear(t2);
#endif
}
/*Calculates the legendre symbol of a and p
*Handbook of applied cryptography: Fact 2.146 */
int number_theory_legendre(mpz_t a, mpz_t p)
{
#if EXTERNAL_NUMBER_THEORY_IMPLEMENTATION
//Do this using gmp number theory implementation
return mpz_legendre(a, p);
#else
//Initializing variables
mpz_t t1;mpz_init(t1);
mpz_t t2;mpz_init(t2);
mpz_t t3;mpz_init(t3);
//Legendre = a ^ ((p-1)/2) mod p
mpz_sub_ui(t1, p,1); //t1 = p - 1
mpz_set_ui(t2, 2); //t2 = 2
mpz_divexact(t3,t1,t2); //t3 = t1 / 2
number_theory_exp_modp(t2,a,t3,p); //t2 = a^t3 mod p
//Store return value, so we can release memory
int value;
/*Exponentiation modulo a prime, can't give a negativ number, hence -1 can't be the result however if -1 was suppose to be the result, the result must be p-1, therefore we shall check if t2 == t1 since t1 is still p-1
*/
if(mpz_cmp(t1,t2) == 0)
value = -1;
else
value = mpz_get_si(t2);
//Clear variables
mpz_clear(t1);
mpz_clear(t2);
mpz_clear(t3);
//Return
return value;
#endif
}
/*Calculate R = a^k mod P, using repeated square-and-multiply algorithm
*Handbook of applied cryptography: Algorithm 2.143. */
void number_theory_exp_modp(mpz_t R, mpz_t a, mpz_t k, mpz_t P);
/*Calculates R² mod P = a, the squareroot of a mod P
*Handbook of applied cryptography: Algorithm 3.36, 3.37 and 3.34 */
void number_theory_squareroot_modp(mpz_t R, mpz_t a, mpz_t P);
/*Calculate the multiplicative inverse of a mod p, using the extended euclidean algorithm
*Handbook of applied cryptography: Algorithm 2.107
*http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm */
void number_theory_inverse(mpz_t R, mpz_t A, mpz_t P);
/*Calculates the legendre symbol of a and p
*Handbook of applied cryptography: Fact 2.146 */
int number_theory_legendre(mpz_t a, mpz_t p);
/*Calculate R = a^k mod P, wraps around number_theory_exp_modp() */
void number_theory_exp_modp_ui(mpz_t R, mpz_t a, unsigned long int k, mpz_t P);
/*Use GMP number theory implementation instead of the algorithms I've implemented.
*My algorithms should be bugfree they've been extensively tested, but they far slower
*than GMP implementations. GMP has no implementation of squareroot, but all the other
*functions are implemented in GMP. Set 1 to use GMP, 0 to use my implementation. */
#define EXTERNAL_NUMBER_THEORY_IMPLEMENTATION 0
#include <stdlib.h>
#include <stdio.h>
#include <../tgmp-build/include/sgx_tgmp.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include "domain_parameters.h"
#include "point.h"
#include "numbertheory.h"
/*Initialize a point*/
point point_init()
{
point p;
p = malloc(sizeof(struct point_s));
mpz_init(p->x);
mpz_init(p->y);
p->infinity = false;
return p;
}
/*Set point to be a infinity*/
void point_at_infinity(point p)
{
p->infinity = true;
}
/*Print point to standart output stream*/
void point_print(point p)
{
/* //Write something if point is a infinity
if(p->infinity)
{
printf("Point is at infinity!");
}else{
printf("\nPoint: (\n\t");
//mpz_out_str(stdout, 10, p->x);
printf("\n,\n\t");
//mpz_out_str(stdout, 10, p->y);
printf("\n)\n");
}*/
}
/*Set a point from another point*/
void point_set(point R, point P)
{
//Copy the point
mpz_set(R->x, P->x);
mpz_set(R->y, P->y);
//Including infinity settings
R->infinity = P->infinity;
}
/*Set point from strings of a base from 2-62*/
void point_set_str(point p, char *x, char *y, int base)
{
mpz_set_str(p->x, x, base);
mpz_set_str(p->y, y, base);
}
/*Set point from hexadecimal strings*/
void point_set_hex(point p, char *x, char *y)
{
point_set_str(p,x,y,16);
}
/*Set point from decimal unsigned long ints*/
void point_set_ui(point p, unsigned long int x, unsigned long int y)
{
mpz_set_ui(p->x, x);
mpz_set_ui(p->y, y);
}
/*Make R a copy of P*/
void point_copy(point R, point P)
{
//Same as point set
point_set(R, P);
}
/*Addition of point P + Q = result*/
void point_addition(point result, point P, point Q, domain_parameters curve)
{
//If Q is at infinity, set result to P
if(Q->infinity)
{
point_set(result, P);
//If P is at infinity set result to be Q
}else if(P->infinity){
point_set(result, Q);
//If the points are the same use point doubling
}else if(point_cmp(P,Q))
{
point_doubling(result, Q, curve);
}else{
//Calculate the inverse point
point iQ = point_init();
point_inverse(iQ, Q, curve);
bool is_inverse = point_cmp(iQ,P);
point_clear(iQ);
//If it is the inverse
if(is_inverse)
{
//result must be point at infinity
point_at_infinity(result);
}else{
//Initialize slope variable
mpz_t s;mpz_init(s);
//Initialize temporary variables
mpz_t t1;mpz_init(t1);
mpz_t t2;mpz_init(t2);
mpz_t t3;mpz_init(t3);
mpz_t t4;mpz_init(t4);
mpz_t t5;mpz_init(t5);
/*
Modulo algebra rules:
(b1 + b2) mod n = (b2 mod n) + (b1 mod n) mod n
(b1 * b2) mod n = (b2 mod n) * (b1 mod n) mod n
*/
//Calculate slope
//s = (Py - Qy)/(Px-Qx) mod p
mpz_sub(t1, P->y, Q->y);
mpz_sub(t2, P->x, Q->x);
//Using Modulo to stay within the group!
number_theory_inverse(t3, t2, curve->p); //Handle errors
mpz_mul(t4, t1, t3);
mpz_mod(s, t4, curve->p);
//Calculate Rx using algorithm shown to the right of the commands
//Rx = s² - Px - Qx = (s² mod p) - (Px mod p) - (Qx mod p) mod p
number_theory_exp_modp_ui(t1, s, 2, curve->p); //t1 = s² mod p
mpz_mod(t2, P->x, curve->p); //t2 = Px mod p
mpz_mod(t3, Q->x, curve->p); //t3 = Qx mod p
mpz_sub(t4, t1, t2); //t4 = t1 - t2
mpz_sub(t5, t4, t3); //t5 = t4 - t3
mpz_mod(result->x, t5, curve->p); //R->x = t5 mod p
//Calculate Ry using algorithm shown to the right of the commands
//Ry = s(Px-Rx) - Py mod p
mpz_sub(t1, P->x, result->x); //t1 = Px - Rx
mpz_mul(t2, s, t1); //t2 = s*t1
mpz_sub(t3, t2, P->y); //t3 = t2 - Py
mpz_mod(result->y, t3, curve->p); //Ry = t3 mod p
//Clear variables, release memory
mpz_clear(t1);
mpz_clear(t2);
mpz_clear(t3);
mpz_clear(t4);
mpz_clear(t5);
mpz_clear(s);
}
}
}
/*Set R to the additive inverse of P, in the curve curve*/
void point_inverse(point R, point P, domain_parameters curve)
{
//If at infinity
if(P->infinity)
{
R->infinity = true;
}else{
//Set Rx = Px
mpz_set(R->x, P->x);
//Set Ry = -Py mod p = p - Ry (Since, Ry < p and Ry is positive)
mpz_sub(R->y, curve->p, P->y);
}
}
/*Set point R = 2P*/
void point_doubling(point R, point P, domain_parameters curve)
{
//If at infinity
if(P->infinity)
{
R->infinity = true;
}else{
//Initialize slope variable
mpz_t s;mpz_init(s);
//Initialize temporary variables
mpz_t t1;mpz_init(t1);
mpz_t t2;mpz_init(t2);
mpz_t t3;mpz_init(t3);
mpz_t t4;mpz_init(t4);
mpz_t t5;mpz_init(t5);
//Calculate slope
//s = (3*Px² + a) / (2*Py) mod p
number_theory_exp_modp_ui(t1, P->x, 2, curve->p); //t1 = Px² mod p
mpz_mul_ui(t2, t1, 3); //t2 = 3 * t1
mpz_mod(t3, t2, curve->p); //t3 = t2 mod p
mpz_add(t4, t3, curve->a); //t4 = t3 + a
mpz_mod(t5, t4, curve->p); //t5 = t4 mod p
mpz_mul_ui(t1, P->y, 2); //t1 = 2*Py
number_theory_inverse(t2, t1, curve->p); //t2 = t1^-1 mod p
mpz_mul(t1, t5, t2); //t1 = t5 * t2
mpz_mod(s, t1, curve->p); //s = t1 mod p
//Calculate Rx
//Rx = s² - 2*Px mod p
number_theory_exp_modp_ui(t1, s, 2, curve->p);//t1 = s² mod p
mpz_mul_ui(t2, P->x, 2); //t2 = Px*2
mpz_mod(t3, t2, curve->p); //t3 = t2 mod p
mpz_sub(t4, t1, t3); //t4 = t1 - t3
mpz_mod(R->x, t4, curve->p); //Rx = t4 mod p
//Calculate Ry using algorithm shown to the right of the commands
//Ry = s(Px-Rx) - Py mod p
mpz_sub(t1, P->x, R->x); //t1 = Px - Rx
mpz_mul(t2, s, t1); //t2 = s*t1
mpz_sub(t3, t2, P->y); //t3 = t2 - Py
mpz_mod(R->y, t3, curve->p); //Ry = t3 mod p
//Clear variables, release memory
mpz_clear(t1);
mpz_clear(t2);
mpz_clear(t3);
mpz_clear(t4);
mpz_clear(t5);
mpz_clear(s);
}
}
/*Compare two points return 1 if not the same, returns 0 if they are the same*/
bool point_cmp(point P, point Q)
{
//If at infinity
if(P->infinity && Q->infinity)
return true;
else if(P->infinity || Q->infinity)
return false;
else
return !mpz_cmp(P->x,Q->x) && !mpz_cmp(P->y,Q->y);
}
/*Perform scalar multiplication to P, with the factor multiplier, over the curve curve*/
void point_multiplication(point R, mpz_t multiplier, point P, domain_parameters curve)
{
//If at infinity R is also at infinity
if(P->infinity)
{
R->infinity = true;
}else{
//Initializing variables
point x = point_init();
point_copy(x, P);
point t = point_init();
point_copy(t, x);
//Set R = point at infinity
point_at_infinity(R);
/*
Loops through the integer bit per bit, if a bit is 1 then x is added to the result. Looping through the multiplier in this manner allows us to use as many point doubling operations as possible. No reason to say 5P=P+P+P+P+P, when you might as well just use 5P=2(2P)+P.
This is not the most effecient method of point multiplication, but it's faster than P+P+P+... which is not computational feasiable.
*/
int bits = mpz_sizeinbase(multiplier, 2);
unsigned long int bit = 0;
while(bit <= bits)
{
if(mpz_tstbit(multiplier, bit))
{
point_addition(t, x, R, curve);
point_copy(R, t);
}
point_doubling(t, x, curve);
point_copy(x, t);
bit++;
}
//Release temporary variables
point_clear(x);
point_clear(t);
}
}
/*Decompress a point from hexadecimal representation
*This function is implemented as specified in SEC 1: Elliptic Curve Cryptography, section 2.3.4.*/
void point_decompress(point P, char* zPoint, domain_parameters curve)
{
//Initialiser variabler
mpz_t x;mpz_init(x);
mpz_t a;mpz_init(a);
mpz_t b;mpz_init(b);
mpz_t t1;mpz_init(t1);
mpz_t t2;mpz_init(t2);
mpz_t t3;mpz_init(t3);
mpz_t t4;mpz_init(t4);
//Get x coordinate
mpz_set_str(x, zPoint + 2, 16);
//alpha = x^3+a*x+b mod p
number_theory_exp_modp_ui(t1, x, 3, curve->p);//t1 = x^3 mod p
mpz_mul(t3, x, curve->a); //t3 = a*x
mpz_mod(t2, t3, curve->p); //t2 = t3 mod p
mpz_add(t3, t1, t2); //t3 = t1 + t2
mpz_add(t4, t3, curve->b); //t4 = t3 + b
mpz_mod(a, t4, curve->p); //a = t4 mod p
//beta = sqrt(alpha) mod p
number_theory_squareroot_modp(b, a, curve->p);
//Get y mod 2 from input
mpz_set_ui(t2, zPoint[1] == '2' ? 0 : 1);
//Set x
mpz_set(P->x, x);
//t2 = beta mod p
mpz_mod_ui(t1, b, 2);
if(mpz_cmp(t1, t2))
mpz_set(P->y, b); //y = beta
else
mpz_sub(P->y, curve->p, b);//y = p -beta
//Release variables
mpz_clear(x);
mpz_clear(a);
mpz_clear(b);
mpz_clear(t1);
mpz_clear(t2);
mpz_clear(t3);
mpz_clear(t4);
}
/*Compress a point to hexadecimal string
*This function is implemented as specified in SEC 1: Elliptic Curve Cryptography, section 2.3.3.*/
char* point_compress(point P)
{
//Point should not be at infinity
assert(!P->infinity);
//Reserve memory
int l = mpz_sizeinbase(P->x, 16) + 2;
char* result = (char*)malloc(l + 1);
result[l] = '\0';
mpz_t t1;mpz_init(t1);
//Add x coordinat in hex to result
mpz_get_str(result +2, 16, P->x);
//Determine if it's odd or even
mpz_mod_ui(t1, P->y, 2);
if(mpz_cmp_ui(t1, 0))
strncpy(result, "02", 2);
else
strncpy(result, "03", 2);
mpz_clear(t1);
return result;
}
/*Release point*/
void point_clear(point p)
{
mpz_clear(p->x);
mpz_clear(p->y);
free(p);
}
/*Initialize a point*/
point point_init();
/*Release point*/
void point_clear(point p);
/*Set point to be a infinity*/
void point_at_infinity(point p);
/*Set R to the additive inverse of P, in the curve curve*/
void point_inverse(point R, point P, domain_parameters curve);
/*Print point to standart output stream*/
void point_print(point p);
/*Set point from hexadecimal strings*/
void point_set_hex(point p, char *x, char *y);
/*Set point from decimal unsigned long ints*/
void point_set_ui(point p, unsigned long int x, unsigned long int y);
/*Addition of point P + Q = result*/
void point_addition(point result, point P, point Q, domain_parameters curve);
/*Set point R = 2P*/
void point_doubling(point R, point P, domain_parameters curve);
/*Perform scalar multiplication to P, with the factor multiplier, over the curve curve*/
void point_multiplication(point R, mpz_t multiplier, point P, domain_parameters curve);
/*Set point from strings of a base from 2-62*/
void point_set_str(point p, char *x, char *y, int base);
/*Compare two points return 1 if not the same, returns 0 if they are the same*/
bool point_cmp(point P, point Q);
/*Decompress a point from hexadecimal representation
*This function is implemented as specified in SEC 1: Elliptic Curve Cryptography, section 2.3.4.*/
void point_decompress(point P, char* zPoint, domain_parameters curve);
/*Compress a point to hexadecimal string
*This function is implemented as specified in SEC 1: Elliptic Curve Cryptography, section 2.3.3.*/
char* point_compress(point P);
/*Make R a copy of P*/
void point_copy(point R, point P);
/*Set a point from another point*/
void point_set(point R, point P);
#include <stdio.h>
#include <stdlib.h>
#include <../tgmp-build/include/sgx_tgmp.h>
#include "random.h"
/*Seeds the random state with information from /dev/random
*This may take time, but it's needed to ensure true randomness*/
void random_seeding(gmp_randstate_t r_state)
{
//Open the random device for reading
FILE* ran = fopen(RANDOM_DEVICE, "r");
//input variables
char i1, i2, i3, i4;
//Read 4 bytes, cause that's the most we can put in an unsigned long int
i1 = fgetc(ran);
if(i1 == EOF)
goto end;
i2 = fgetc(ran);
if(i2 == EOF)
goto end;
i3 = fgetc(ran);
if(i3 == EOF)
goto end;
i4 = fgetc(ran);
if(i4 == EOF)
goto end;
//abs() returns long (signed long), therefor there must be two, since DO NOT want to loose any randomness
gmp_randseed_ui(r_state, (unsigned long int)abs(i1)* (unsigned long int)abs(i2*i3*i4));
//Define end
end:
//Close file resources
fclose(ran);
}
/*Seeds the random state with information from /dev/random
*This may take time, but it's needed to ensure true randomness*/
void random_seeding(gmp_randstate_t r_state);
/*Operating system dependent random device, please use true random
*Linux has /dev/random as true RNG and /dev/urandom as pseudo random device
*Note: /dev/random may be slow, whereas /dev/urandom is not as secure*/
#define RANDOM_DEVICE "/dev/urandom"
/*Time spent reading from random device is not included in benchmark and other timings.
*To see difference between real execution time and execution time use Unix "time" command*/
......@@ -31,6 +31,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <openssl/ecdsa.h>
#include <openssl/ec.h>
#include <openssl/bn.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include "tSgxSSL_api.h"
#include "secure_enclave_t.h"
#include "sgx_tcrypto.h"
#include "sgx_tseal.h"
......@@ -41,15 +49,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <string.h>
#include <stdio.h>
#include <openssl/ec.h>
#include <openssl/bn.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include "tSgxSSL_api.h"
#include <stdbool.h>
#include "domain_parameters.h"
#include "point.h"
#include "signature.h"
#include "curves.h"
#include <string.h>
#include <sgx_tcrypto.h>
#include "../sgxwallet_common.h"
void *(*gmp_realloc_func)(void *, size_t, size_t);
void *(*oc_realloc_func)(void *, size_t, size_t);
......@@ -117,7 +129,64 @@ void e_mpf_div(mpf_t *c_un, mpf_t *a_un, mpf_t *b_un) {}
void generate_ecdsa_key(int *err_status, char *err_string,
uint8_t *encrypted_key, uint32_t *enc_len) {
uint8_t *encrypted_key, uint32_t *enc_len, char * pub_key_x, char * pub_key_y) {
domain_parameters curve = domain_parameters_init();
domain_parameters_load_curve(curve, secp256k1);
unsigned char* rand_char = (unsigned char*)malloc(32);
sgx_read_rand( (unsigned char*)rand_char, 32);
mpz_t seed;
mpz_init(seed);
mpz_import(seed, 32, 1, sizeof(rand_char[0]), 0, 0, rand_char);
free(rand_char);
mpz_t skey;
mpz_init(skey);
mpz_mod(skey, seed, curve->p);
mpz_clear(seed);
//mpz_set_str(skey, "4160780231445160889237664391382223604184857153814275770598791864649971919844", 10);
//Public key
point Pkey = point_init();
signature_generate_key(Pkey, skey, curve);
uint8_t base = 16;
int len = mpz_sizeinbase (Pkey->x, base) + 2;
//snprintf(err_string, BUF_LEN, "len = %d\n", len);
char arr_x[len];
char* px = mpz_get_str(arr_x, base, Pkey->x);
//snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px);
strncpy(pub_key_x, arr_x, 1024);
char arr_y[mpz_sizeinbase (Pkey->y, base) + 2];
char* py = mpz_get_str(arr_y, base, Pkey->y);
strncpy(pub_key_y, arr_y, 1024);
char skey_str[mpz_sizeinbase (skey, 10) + 2];
char* s = mpz_get_str(skey_str, 10, skey);
// snprintf(err_string, BUF_LEN, "skey is %s\n", skey_str);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, 39);
sgx_status_t status = sgx_seal_data(0, NULL, 39, (uint8_t *)skey_str, sealedLen,(sgx_sealed_data_t*)encrypted_key);
if( status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"seal ecsdsa private key failed");
return;
}
*enc_len = sealedLen;
mpz_clear(skey);
domain_parameters_clear(curve);
point_clear(Pkey);
}
......@@ -267,79 +336,6 @@ void bls_sign_message(int *err_status, char *err_string, uint8_t *encrypted_key,
}
void ecdsa_sign_message(int *err_status, char *err_string, uint8_t *encrypted_key,
uint32_t enc_len, uint8_t *message, char *signature) {
*err_status = -1;
char key[BUF_LEN];
decrypt_key(err_status, err_string, encrypted_key, enc_len, key);
if (*err_status != 0) {
return;
}
//strncpy(signature, ecdsaSig, MAX_SIG_LEN);
unsigned char entropy_buf[ADD_ENTROPY_SIZE] = {0};
RAND_add(entropy_buf, sizeof(entropy_buf), ADD_ENTROPY_SIZE);
RAND_seed(entropy_buf, sizeof(entropy_buf));
// Initialize SGXSSL crypto
OPENSSL_init_crypto(0, NULL);
RAND_add(entropy_buf, sizeof(entropy_buf), ADD_ENTROPY_SIZE);
RAND_seed(entropy_buf, sizeof(entropy_buf));
EC_KEY *ec = NULL;
int eccgroup;
eccgroup = OBJ_txt2nid("secp384r1");
ec = EC_KEY_new_by_curve_name(eccgroup);
if (ec == NULL) {
return;
}
EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE);
int ret = EC_KEY_generate_key(ec);
if (!ret) {
return;
}
EVP_PKEY *ec_pkey = EVP_PKEY_new();
if (ec_pkey == NULL) {
return;
}
EVP_PKEY_assign_EC_KEY(ec_pkey, ec);
// DONE
char buffer[100];
unsigned char sig;
unsigned int siglen;
int i;
for (i = 0; i < 1000; i++) {
// Add context
EVP_MD_CTX *context = EVP_MD_CTX_new();
// Init, update, final
EVP_SignInit_ex(context, EVP_sha1(), NULL);
EVP_SignUpdate(context, &buffer, 100);
EVP_SignFinal(context, &sig, &siglen, ec_pkey);
}
*err_status = 0;
}
void gen_dkg_secret (int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, uint32_t* enc_len, size_t _t){
char* dkg_secret = (char*)malloc(DKG_BUFER_LENGTH);
......@@ -355,6 +351,7 @@ void gen_dkg_secret (int *err_status, char *err_string, uint8_t *encrypted_dkg_s
}
*enc_len = sealedLen;
free(dkg_secret);
}
void decrypt_dkg_secret (int *err_status, char* err_string, uint8_t* encrypted_dkg_secret, uint8_t* decrypted_dkg_secret, uint32_t enc_len){
......@@ -383,3 +380,87 @@ void get_public_shares(int *err_status, char* err_string, uint8_t* encrypted_dkg
decrypt_dkg_secret(err_status, err_string, (uint8_t*)encrypted_dkg_secret, decrypted_dkg_secret, enc_len);
calc_public_shares(decrypted_dkg_secret, public_shares, _t);
}
void ecdsa_sign1(int *err_status, char *err_string, uint8_t *encrypted_key,
uint32_t dec_len, unsigned char* hash, char * sig_r, char * sig_s, char* sig_v) {
domain_parameters curve = domain_parameters_init();
domain_parameters_load_curve(curve, secp256k1);
char skey[SGX_ECP256_KEY_SIZE];
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_key, NULL, 0, skey, &dec_len);
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"sgx_unseal_data failed with status %d", status);
return;
}
mpz_t skey_mpz;
mpz_init(skey_mpz);
mpz_set_str(skey_mpz, skey, 10);
/*mpz_t test_skey;
mpz_init(test_skey);
mpz_set_str(test_skey, "4160780231445160889237664391382223604184857153814275770598791864649971919844", 10);
if(!mpz_cmp(skey,test_skey)){
snprintf(err_string, BUF_LEN,"keys are not equal ");
}*/
mpz_t msg_mpz;
mpz_init(msg_mpz);
mpz_set_str(msg_mpz, skey, 10);
signature sign = signature_init();
signature_sign( sign, msg_mpz, skey_mpz, curve);
point Pkey = point_init();
signature_generate_key(Pkey, skey_mpz, curve);
if ( !signature_verify(msg_mpz, sign, Pkey, curve) ){
snprintf(err_string, BUF_LEN,"signature is not verified! ");
return;
}
uint8_t base = 16;
char arr_r[mpz_sizeinbase (sign->r, base) + 2];
char* r = mpz_get_str(arr_r, base, sign->r);
strncpy(sig_r, arr_r, 1024);
char arr_s[mpz_sizeinbase (sign->s, base) + 2];
char* s = mpz_get_str(arr_s, base, sign->s);
strncpy(sig_s, arr_s, 1024);
sig_v[0] = '0';
sig_v[1] = 'x';
sig_v[2] = '1';
sig_v[3] = 'b';
mpz_t rem;
mpz_init(rem);
mpz_mod_ui(rem, sign->r, 2);
int r_gr_n = mpz_cmp(sign->r, curve->n);
if (mpz_sgn(rem) && r_gr_n < 0){
sig_v[3] = 'c';
}
else if (mpz_sgn(rem) > 0 && r_gr_n > 0){
sig_v[3] = 'e';
}
else if (mpz_sgn(rem) == 0 && r_gr_n > 0){
sig_v[3] = 'd';
}
mpz_clear(skey_mpz);
mpz_clear(msg_mpz);
mpz_clear(rem);
domain_parameters_clear(curve);
signature_clear(sign);
}
This diff is collapsed.
......@@ -26,7 +26,10 @@ from "sgx_tsgxssl.edl" import *;
public void generate_ecdsa_key (
[user_check] int *err_status,
[out, count = 1024] char* err_string,
[out, count = 1024] uint8_t* encrypted_key, [user_check] uint32_t *enc_len);
[out, count = 2048] uint8_t* encrypted_key,
[user_check] uint32_t *enc_len,
[out, count = 1024] char * pub_key_x,
[out, count = 1024] char * pub_key_y);
public void encrypt_key (
......@@ -51,14 +54,6 @@ from "sgx_tsgxssl.edl" import *;
[in, count = 1024] char* hashY ,
[out, count = 1024] char* signature);
public void ecdsa_sign_message (
[user_check] int *err_status,
[out, count = 1024] char* err_string,
[in, count = 1024] uint8_t* encrypted_key,
uint32_t enc_len,
[in, count = 1024] uint8_t* hash,
[out, count = 1024] char* signature);
public void gen_dkg_secret (
[user_check] int *err_status,
[out, count = 1024] char* err_string,
......@@ -90,6 +85,16 @@ from "sgx_tsgxssl.edl" import *;
[out, count = 4000] char* public_shares,
unsigned _t,
unsigned _n);
public void ecdsa_sign1(
[user_check] int *err_status,
[out, count = 2048] char* err_string,
[in, count = 2048] uint8_t* encrypted_key,
uint32_t dec_len,
[in, count = 1024] unsigned char* hash,
[out, count = 1024] char* sig_r,
[out, count = 1024] char* sig_s,
[out, count = 4] char* sig_v);
};
......
This diff is collapsed.
#include <stdlib.h>
#include <stdio.h>
#include <../tgmp-build/include/sgx_tgmp.h>
#include <stdbool.h>
#include <assert.h>
#include "domain_parameters.h"
#include "point.h"
#include "signature.h"
#include "numbertheory.h"
#include "random.h"
/*Initialize a signature*/
signature signature_init()
{
signature sig;
sig = malloc(sizeof(struct signature_s));
mpz_init(sig->r);
mpz_init(sig->s);
return sig;
}
/*Print signature to standart output stream*/
void signature_print(signature sig)
{
/*printf("\nSignature (r,s): \n\t(");
mpz_out_str(stdout, 10, sig->r);
printf(",\n\t");
mpz_out_str(stdout, 10, sig->s);
printf(")\n");*/
}
/*Set signature from strings of a base from 2-62*/
void signature_set_str(signature sig, char *r, char *s, int base)
{
mpz_set_str(sig->r, r, base);
mpz_set_str(sig->s, s, base);
}
/*Set signature from hexadecimal strings*/
void signature_set_hex(signature sig, char *r, char *s)
{
signature_set_str(sig,r,s,16);
}
/*Set signature from decimal unsigned long ints*/
void signature_set_ui(signature sig, unsigned long int r, unsigned long int s)
{
mpz_set_ui(sig->r, r);
mpz_set_ui(sig->s, s);
}
/*Make R a copy of P*/
void signature_copy(signature R, signature sig)
{
mpz_set(R->r, sig->r);
mpz_set(R->s, sig->s);
}
/*Compare two signatures return 1 if not the same, returns 0 if they are the same*/
bool signature_cmp(signature sig1, signature sig2)
{
return !mpz_cmp(sig1->r,sig2->r) && !mpz_cmp(sig1->s,sig2->s);
}
/*Generates a public key for a private key*/
void signature_generate_key(point public_key, mpz_t private_key, domain_parameters curve)
{
point_multiplication(public_key, private_key, curve->G, curve);
}
/*Generate signature for a message*/
void signature_sign(signature sig, mpz_t message, mpz_t private_key, domain_parameters curve)
{
//message must not have a bit length longer than that of n
//see: Guide to Elliptic Curve Cryptography, section 4.4.1.
assert(mpz_sizeinbase(message, 2) <= mpz_sizeinbase(curve->n, 2));
//Initializing variables
mpz_t k;mpz_init(k);
mpz_t x;mpz_init(x);
point Q = point_init();
mpz_t r;mpz_init(r);
mpz_t t1;mpz_init(t1);
mpz_t t2;mpz_init(t2);
mpz_t t3;mpz_init(t3);
mpz_t s;mpz_init(s);
unsigned char* rand_char = (unsigned char*)malloc(32);
sgx_read_rand( rand_char, 32);
gmp_randstate_t r_state;
signature_sign_start:
//Set k
sgx_read_rand( rand_char, 32);
mpz_t seed;
mpz_init(seed);
mpz_import(seed, 32, 1, sizeof(rand_char[0]), 0, 0, rand_char);
free(rand_char);
mpz_mod(k, seed, curve->p);
mpz_clear(seed);
//Calculate x
point_multiplication(Q, k, curve->G, curve);
mpz_set(x, Q->x);
point_clear(Q);
//Calculate r
mpz_mod(r, x, curve->n);
if(!mpz_sgn(r)) //Start over if r=0, note haven't been tested memory might die :)
goto signature_sign_start;
mpz_clear(x);
//Calculate s
//s = k¯¹(e+d*r) mod n = (k¯¹ mod n) * ((e+d*r) mod n) mod n
number_theory_inverse(t1, k, curve->n);//t1 = k¯¹ mod n
mpz_mul(t2, private_key, r);//t2 = d*r
mpz_add(t3, message, t2); //t3 = e+t2
mpz_mod(t2, t3, curve->n); //t2 = t3 mod n
mpz_mul(t3, t2, t1); //t3 = t2 * t1
mpz_mod(s, t3, curve->n); //s = t3 mod n
mpz_clear(t1);
mpz_clear(t2);
mpz_clear(t3);
//Set signature
mpz_set(sig->r, r);
mpz_set(sig->s, s);
//Release k,r and s
mpz_clear(k);
mpz_clear(r);
mpz_clear(s);
}
/*Verify the integrity of a message using it's signature*/
bool signature_verify(mpz_t message, signature sig, point public_key, domain_parameters curve)
{
//verify r and s are within [1, n-1]
mpz_t one;mpz_init(one);
mpz_set_ui(one, 1);
if( mpz_cmp(sig->r,one) < 0 &&
mpz_cmp(curve->n,sig->r) <= 0 &&
mpz_cmp(sig->s,one) < 0 &&
mpz_cmp(curve->n,sig->s) <= 0)
{
mpz_clear(one);
return false;
}
mpz_clear(one);
//Initialize variables
mpz_t w;mpz_init(w);
mpz_t u1;mpz_init(u1);
mpz_t u2;mpz_init(u2);
mpz_t t;mpz_init(t);
mpz_t tt2;mpz_init(tt2);
point x = point_init();
point t1 = point_init();
point t2 = point_init();
//w = s¯¹ mod n
number_theory_inverse(w, sig->s, curve->n);
//u1 = message * w mod n
mpz_mod(tt2, message, curve->n);
mpz_mul(t, tt2, w);
mpz_mod(u1, t, curve->n);
//u2 = r*w mod n
mpz_mul(t, sig->r, w);
mpz_mod(u2, t, curve->n);
//x = u1*G+u2*Q
point_multiplication(t1, u1, curve->G, curve);
point_multiplication(t2, u2, public_key, curve);
point_addition(x, t1, t2, curve);
//Get the result, by comparing x value with r and verifying that x is NOT at infinity
bool result = mpz_cmp(sig->r, x->x) == 0 && !x->infinity;
//release memory
point_clear(x);
point_clear(t1);
point_clear(t2);
mpz_clear(w);
mpz_clear(u1);
mpz_clear(u2);
mpz_clear(t);
mpz_clear(tt2);
//Return result
return result;
}
/*Release signature*/
void signature_clear(signature sig)
{
mpz_clear(sig->r);
mpz_clear(sig->s);
free(sig);
}
/*Type for representing a signature*/
struct signature_s
{
mpz_t r;
mpz_t s;
};
typedef struct signature_s* signature;
/*Initialize a signature*/
signature signature_init();
/*Set signature from strings of a base from 2-62*/
void signature_set_str(signature sig, char *r, char *s, int base);
/*Set signature from hexadecimal strings*/
void signature_set_hex(signature sig, char *r, char *s);
/*Set signature from decimal unsigned long ints*/
void signature_set_ui(signature sig, unsigned long int r, unsigned long int s);
/*Print signature to standart output stream*/
void signature_print(signature sig);
/*Make R a copy of P*/
void signature_copy(signature R, signature sig);
/*Compare two signatures return 1 if not the same, returns 0 if they are the same*/
bool signature_cmp(signature sig1, signature sig2);
/*Release signature*/
void signature_clear(signature sig);
/*Generates a public key for a private key*/
void signature_generate_key(point public_key, mpz_t private_key, domain_parameters curve);
/*Generate signature for a message*/
void signature_sign(signature sig, mpz_t message, mpz_t private_key, domain_parameters curve);
/*Verify the integrity of a message using it's signature*/
bool signature_verify(mpz_t message, signature sig, point public_key, domain_parameters curve);
......@@ -51,7 +51,8 @@
"returns": {
"status": 0,
"errorMessage": "12345",
"encryptedKey": "12345"
"encryptedKey": "12345",
"PublicKey": "12345"
}
},
......@@ -60,13 +61,15 @@
{
"name": "ecdsaSignMessageHash",
"params": {
"keyShareName": "key1",
"keyName": "key1",
"messageHash": "1122334455"
},
"returns": {
"status": 0,
"errorMessage": "12345",
"signature": "12345"
"signature_v": "12345",
"signature_r": "12345",
"signature_s": "12345"
}
}
]
\ No newline at end of file
//
// Created by kladko on 9/23/19.
//
#include <iostream>
#include "stubclient.h"
#include <jsonrpccpp/client/connectors/httpclient.h>
using namespace jsonrpc;
using namespace std;
int init_client() {
HttpClient client("http://localhost:1025");
StubClient c(client, JSONRPC_CLIENT_V2);
Json::Value params;
try {
cout << c.generateECDSAKey("test_key") << endl;
} catch (JsonRpcException &e) {
cerr << e.what() << endl;
}
return 0;
}
......@@ -30,7 +30,8 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <libff/algebra/fields/fp.hpp>
#include <dkg/dkg.h>
......@@ -61,10 +62,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "SGXWalletServer.hpp"
#include <sgx_tcrypto.h>
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include "stubclient.h"
std::string stringFromFr(libff::alt_bn128_Fr& el) {
mpz_t t;
......@@ -311,7 +316,7 @@ std::vector<libff::alt_bn128_Fr> SplitStringToFr(const char* koefs, const char*
return tokens;
}
TEST_CASE( "DKG auto secret shares test", "[dkg-s_shares]" ) {
/*TEST_CASE( "DKG auto secret shares test", "[dkg-s_shares]" ) {
init_all();
......@@ -358,4 +363,76 @@ TEST_CASE( "DKG auto secret shares test", "[dkg-s_shares]" ) {
free(encrypted_dkg_secret);
free(secret_shares);
}*/
TEST_CASE("ECDSA keygen and signature test", "[ecdsa_test]") {
init_all();
char *errMsg = (char *)calloc(1024, 1);
int err_status = 0;
uint8_t *encr_pr_key = (uint8_t *)calloc(1024, 1);
char *pub_key_x = (char *)calloc(1024, 1);
char *pub_key_y = (char *)calloc(1024, 1);
uint32_t enc_len = 0;
//printf("before %p\n", pub_key_x);
status = generate_ecdsa_key(eid, &err_status, errMsg, encr_pr_key, &enc_len, pub_key_x, pub_key_y );
printf("\nerrMsg %s\n", errMsg );
REQUIRE(status == SGX_SUCCESS);
printf("\npub_key_x %s: \n", pub_key_x);
printf("\npub_key_y %s: \n", pub_key_y);
printf("\nencr priv_key %s: \n");
for ( int i = 0; i < 1024 ; i++)
printf("%u ", encr_pr_key[i]);
char* hex = "38433e5ce087dcc1be82fcc834eae83c256b3db87d34f84440d0b708daa0c6f7";
char* signature_r = (char *)calloc(1024, 1);
char* signature_s = (char *)calloc(1024, 1);
char* signature_v = (char*)calloc(4,1);
status = ecdsa_sign1(eid, &err_status, errMsg, encr_pr_key, enc_len, (unsigned char*)hex, signature_r, signature_s, signature_v );
REQUIRE(status == SGX_SUCCESS);
printf("\nsignature r : %s ", signature_r);
printf("\nsignature s: %s ", signature_s);
printf("\nsignature v: %s ", signature_v);
printf("\n %s \n", errMsg);
}
#include "stubclient.h"
#include <jsonrpccpp/client/connectors/httpclient.h>
using namespace jsonrpc;
using namespace std;
TEST_CASE("API test", "[api_test]") {
cerr << "API test started" << endl;
init_all();
//HttpServer httpserver(1025);
//SGXWalletServer s(httpserver,
// JSONRPC_SERVER_V1); // hybrid server (json-rpc 1.0 & 2.0)
// s.StartListening();
cerr << "Server inited" << endl;
HttpClient client("http://localhost:1025");
StubClient c(client, JSONRPC_CLIENT_V2);
cerr << "Client inited" << endl;
try {
//cout << c.generateECDSAKey("test_key") << endl;
cout << c.ecdsaSignMessageHash("test_key","38433e5ce087dcc1be82fcc834eae83c256b3db87d34f84440d0b708daa0c6f7" );
} catch (JsonRpcException &e) {
cerr << e.what() << endl;
}
}
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