Unverified Commit df32a65c authored by Stan Kladko's avatar Stan Kladko Committed by GitHub

Merge pull request #143 from skalenetwork/SKALE-3067-cleanup3

Skale 3067 cleanup3
parents 5683ee17 7d327c53
...@@ -180,7 +180,7 @@ void trustedSetSEK(std::shared_ptr<std::string> hex_encr_SEK) { ...@@ -180,7 +180,7 @@ void trustedSetSEK(std::shared_ptr<std::string> hex_encr_SEK) {
throw SGXException(INVALID_HEX, "Invalid encrypted SEK Hex"); throw SGXException(INVALID_HEX, "Invalid encrypted SEK Hex");
} }
status = trustedSetSEK(eid, &err_status, errMsg.data(), encr_SEK, len ); status = trustedSetSEK(eid, &err_status, errMsg.data(), encr_SEK );
if ( status != SGX_SUCCESS ) { if ( status != SGX_SUCCESS ) {
cerr << "RPCException thrown" << endl; cerr << "RPCException thrown" << endl;
throw SGXException(status, errMsg.data()) ; throw SGXException(status, errMsg.data()) ;
......
...@@ -41,7 +41,7 @@ int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen) { ...@@ -41,7 +41,7 @@ int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen) {
return -2; return -2;
} }
auto len = strlen(message); uint64_t len = strlen(message);
if (len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE > encrLen ) { if (len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE > encrLen ) {
LOG_ERROR("Output buffer too small"); LOG_ERROR("Output buffer too small");
...@@ -50,8 +50,6 @@ int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen) { ...@@ -50,8 +50,6 @@ int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen) {
sgx_read_rand(encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE); sgx_read_rand(encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE);
auto msgLen = strlen(message);
sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_key, (uint8_t*)message, strlen(message), sgx_status_t status = sgx_rijndael128GCM_encrypt(&AES_key, (uint8_t*)message, strlen(message),
encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE, encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE,
encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE, encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE,
...@@ -90,7 +88,7 @@ int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message, uint64_t ...@@ -90,7 +88,7 @@ int AES_decrypt(uint8_t *encr_message, uint64_t length, char *message, uint64_t
sgx_status_t status = sgx_rijndael128GCM_decrypt(&AES_key, sgx_status_t status = sgx_rijndael128GCM_decrypt(&AES_key,
encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE, len, encr_message + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE, len,
message, (unsigned char*) message,
encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE, encr_message + SGX_AESGCM_MAC_SIZE, SGX_AESGCM_IV_SIZE,
NULL, 0, NULL, 0,
(sgx_aes_gcm_128bit_tag_t *)encr_message); (sgx_aes_gcm_128bit_tag_t *)encr_message);
......
...@@ -42,15 +42,19 @@ ...@@ -42,15 +42,19 @@
#include "EnclaveCommon.h" #include "EnclaveCommon.h"
#include <string.h> #include <string.h>
int gen_session_key(char *skey_str, char *pb_keyB, char *common_key) { int gen_session_key(char *skey_str, char *pb_keyB, char *common_key) {
int ret = -1; int ret = -1;
LOG_INFO(__FUNCTION__); LOG_INFO(__FUNCTION__);
SAFE_CHAR_BUF(pb_keyB_x, 65);SAFE_CHAR_BUF(pb_keyB_y, 65); SAFE_CHAR_BUF(pb_keyB_x, 65);SAFE_CHAR_BUF(pb_keyB_y, 65);
mpz_t skey; mpz_t skey;
mpz_init(skey); mpz_init(skey);
point pub_keyB = point_init(); point pub_keyB = point_init();
...@@ -110,6 +114,22 @@ int session_key_recover(const char *skey_str, const char *sshare, char *common_k ...@@ -110,6 +114,22 @@ int session_key_recover(const char *skey_str, const char *sshare, char *common_k
int ret = -1; int ret = -1;
SAFE_CHAR_BUF(pb_keyB_x, 65);
SAFE_CHAR_BUF(pb_keyB_y, 65);
mpz_t skey;
mpz_init(skey);
point pub_keyB = point_init();
point session_key = point_init();
pb_keyB_x[64] = 0;
strncpy(pb_keyB_x, sshare + 64, 64);
strncpy(pb_keyB_y, sshare + 128, 64);
pb_keyB_y[64] = 0;
if (!common_key) { if (!common_key) {
LOG_ERROR("session_key_recover: Null common_key"); LOG_ERROR("session_key_recover: Null common_key");
goto clean; goto clean;
...@@ -128,18 +148,12 @@ int session_key_recover(const char *skey_str, const char *sshare, char *common_k ...@@ -128,18 +148,12 @@ int session_key_recover(const char *skey_str, const char *sshare, char *common_k
} }
SAFE_CHAR_BUF(pb_keyB_x, 65);
strncpy(pb_keyB_x, sshare + 64, 64);
pb_keyB_x[64] = 0;
SAFE_CHAR_BUF(pb_keyB_y, 65);
strncpy(pb_keyB_y, sshare + 128, 64);
pb_keyB_y[64] = 0;
mpz_t skey;
mpz_init(skey);
point pub_keyB = point_init();
point session_key = point_init();
if (mpz_set_str(skey, skey_str, 16) == -1) { if (mpz_set_str(skey, skey_str, 16) == -1) {
goto clean; goto clean;
...@@ -191,7 +205,7 @@ int xor_encrypt(char *key, char *message, char *cypher) { ...@@ -191,7 +205,7 @@ int xor_encrypt(char *key, char *message, char *cypher) {
uint64_t key_length; uint64_t key_length;
if (!hex2carray(key, &key_length, key_bin)) { if (!hex2carray(key, &key_length, (uint8_t *) key_bin)) {
goto clean; goto clean;
} }
...@@ -205,7 +219,7 @@ int xor_encrypt(char *key, char *message, char *cypher) { ...@@ -205,7 +219,7 @@ int xor_encrypt(char *key, char *message, char *cypher) {
cypher_bin[i] = msg_bin[i] ^ key_bin[i]; cypher_bin[i] = msg_bin[i] ^ key_bin[i];
} }
carray2Hex(cypher_bin, 32, cypher); carray2Hex((unsigned char*) cypher_bin, 32, cypher);
ret = 0; ret = 0;
...@@ -241,14 +255,14 @@ int xor_decrypt(char *key, char *cypher, char *message) { ...@@ -241,14 +255,14 @@ int xor_decrypt(char *key, char *cypher, char *message) {
SAFE_CHAR_BUF(key_bin,33) SAFE_CHAR_BUF(key_bin,33)
uint64_t key_length; uint64_t key_length;
if (!hex2carray(key, &key_length, key_bin)) { if (!hex2carray(key, &key_length, (uint8_t*) key_bin)) {
goto clean; goto clean;
} }
uint64_t cypher_length; uint64_t cypher_length;
SAFE_CHAR_BUF(cypher_bin, 33); SAFE_CHAR_BUF(cypher_bin, 33);
if (!hex2carray(cypher, &cypher_length, cypher_bin)) { if (!hex2carray(cypher, &cypher_length, (uint8_t *) cypher_bin)) {
goto clean; goto clean;
} }
...@@ -256,7 +270,7 @@ int xor_decrypt(char *key, char *cypher, char *message) { ...@@ -256,7 +270,7 @@ int xor_decrypt(char *key, char *cypher, char *message) {
msg_bin[i] = cypher_bin[i] ^ key_bin[i]; msg_bin[i] = cypher_bin[i] ^ key_bin[i];
} }
carray2Hex(msg_bin, 32, message); carray2Hex((unsigned char*) msg_bin, 32, message);
ret = 0; ret = 0;
......
...@@ -144,10 +144,14 @@ string ConvertG2ToString(const libff::alt_bn128_G2 &elem, int base = 10, const s ...@@ -144,10 +144,14 @@ string ConvertG2ToString(const libff::alt_bn128_G2 &elem, int base = 10, const s
vector <libff::alt_bn128_Fr> SplitStringToFr(const char *coeffs, const char symbol) { vector <libff::alt_bn128_Fr> SplitStringToFr(const char *coeffs, const char symbol) {
vector <libff::alt_bn128_Fr> result; vector <libff::alt_bn128_Fr> result;
string str(coeffs); string str(coeffs);
string delim; string delim;
CHECK_ARG_CLEAN(coeffs);
try { try {
delim.push_back(symbol); delim.push_back(symbol);
...@@ -183,6 +187,8 @@ int gen_dkg_poly(char *secret, unsigned _t) { ...@@ -183,6 +187,8 @@ int gen_dkg_poly(char *secret, unsigned _t) {
int status = 1; int status = 1;
string result; string result;
CHECK_ARG_CLEAN(secret);
try { try {
for (size_t i = 0; i < _t; ++i) { for (size_t i = 0; i < _t; ++i) {
libff::alt_bn128_Fr cur_coef = libff::alt_bn128_Fr::random_element(); libff::alt_bn128_Fr cur_coef = libff::alt_bn128_Fr::random_element();
...@@ -222,7 +228,7 @@ libff::alt_bn128_Fr PolynomialValue(const vector <libff::alt_bn128_Fr> &pol, lib ...@@ -222,7 +228,7 @@ libff::alt_bn128_Fr PolynomialValue(const vector <libff::alt_bn128_Fr> &pol, lib
libff::alt_bn128_Fr pow = libff::alt_bn128_Fr::one(); libff::alt_bn128_Fr pow = libff::alt_bn128_Fr::one();
for (unsigned i = 0; i < pol.size(); ++i) { for (unsigned i = 0; i < pol.size(); ++i) {
result += pol[i] * pow; result += pol.at(i) * pow;
pow *= point; pow *= point;
} }
...@@ -247,6 +253,11 @@ void calc_secret_shares(const char *decrypted_coeffs, ...@@ -247,6 +253,11 @@ void calc_secret_shares(const char *decrypted_coeffs,
string result; string result;
char symbol = ':'; char symbol = ':';
CHECK_ARG_CLEAN(decrypted_coeffs);
CHECK_ARG_CLEAN(secret_shares);
CHECK_ARG_CLEAN(_n > 0);
CHECK_ARG_CLEAN(_t <= _n);
try { try {
vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol); vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
...@@ -260,18 +271,27 @@ void calc_secret_shares(const char *decrypted_coeffs, ...@@ -260,18 +271,27 @@ void calc_secret_shares(const char *decrypted_coeffs,
} catch (exception &e) { } catch (exception &e) {
LOG_ERROR(e.what()); LOG_ERROR(e.what());
return; goto clean;
} catch (...) { } catch (...) {
LOG_ERROR("Unknown throwable"); LOG_ERROR("Unknown throwable");
return; goto clean;
} }
clean:
;
} }
int calc_secret_share(const char *decrypted_coeffs, char *s_share, int calc_secret_share(const char *decrypted_coeffs, char *s_share,
unsigned _t, unsigned _n, unsigned ind) { unsigned _t, unsigned _n, unsigned ind) {
int result = 1; int result = 1;
CHECK_ARG_CLEAN(decrypted_coeffs);
CHECK_ARG_CLEAN(s_share);
CHECK_ARG_CLEAN(_n > 0);
CHECK_ARG_CLEAN(_t <= _n);
try { try {
char symbol = ':'; char symbol = ':';
vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol); vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
...@@ -300,27 +320,6 @@ int calc_secret_share(const char *decrypted_coeffs, char *s_share, ...@@ -300,27 +320,6 @@ int calc_secret_share(const char *decrypted_coeffs, char *s_share,
return result; return result;
} }
void calc_secret_shareG2_old(const char *decrypted_coeffs, char *s_shareG2,
unsigned _t, unsigned ind) {
try {
char symbol = ':';
vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
libff::alt_bn128_Fr secret_share = PolynomialValue(poly, libff::alt_bn128_Fr(ind), _t);
libff::alt_bn128_G2 secret_shareG2 = secret_share * libff::alt_bn128_G2::one();
string secret_shareG2_str = ConvertG2ToString(secret_shareG2);
strncpy(s_shareG2, secret_shareG2_str.c_str(), secret_shareG2_str.length() + 1);
} catch (exception &e) {
LOG_ERROR(e.what());
} catch (...) {
LOG_ERROR("Unknown throwable");
}
}
int calc_secret_shareG2(const char *s_share, char *s_shareG2) { int calc_secret_shareG2(const char *s_share, char *s_shareG2) {
...@@ -329,6 +328,9 @@ int calc_secret_shareG2(const char *s_share, char *s_shareG2) { ...@@ -329,6 +328,9 @@ int calc_secret_shareG2(const char *s_share, char *s_shareG2) {
mpz_t share; mpz_t share;
mpz_init(share); mpz_init(share);
CHECK_ARG_CLEAN(s_share);
CHECK_ARG_CLEAN(s_shareG2);
try { try {
...@@ -370,13 +372,21 @@ int calc_secret_shareG2(const char *s_share, char *s_shareG2) { ...@@ -370,13 +372,21 @@ int calc_secret_shareG2(const char *s_share, char *s_shareG2) {
int calc_public_shares(const char *decrypted_coeffs, char *public_shares, int calc_public_shares(const char *decrypted_coeffs, char *public_shares,
unsigned _t) { unsigned _t) {
// calculate for each node a list of public shares
int ret = 1;
string result;
char symbol = ':';
CHECK_ARG_CLEAN(decrypted_coeffs);
CHECK_ARG_CLEAN(public_shares);
CHECK_ARG_CLEAN(_t > 0);
try { try {
// calculate for each node a list of public shares
string result;
char symbol = ':';
vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol); vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
if (poly.size() != _t) { if (poly.size() != _t) {
return 1; goto clean;
} }
for (size_t i = 0; i < _t; ++i) { for (size_t i = 0; i < _t; ++i) {
libff::alt_bn128_G2 pub_share = poly.at(i) * libff::alt_bn128_G2::one(); libff::alt_bn128_G2 pub_share = poly.at(i) * libff::alt_bn128_G2::one();
...@@ -385,58 +395,63 @@ int calc_public_shares(const char *decrypted_coeffs, char *public_shares, ...@@ -385,58 +395,63 @@ int calc_public_shares(const char *decrypted_coeffs, char *public_shares,
result += pub_share_str + ","; result += pub_share_str + ",";
} }
strncpy(public_shares, result.c_str(), result.length()); strncpy(public_shares, result.c_str(), result.length());
return 0; ret = 0;
} catch (exception &e) { } catch (exception &e) {
LOG_ERROR(e.what()); LOG_ERROR(e.what());
return 1; ret = 1;
} catch (...) { } catch (...) {
LOG_ERROR("Unknown throwable"); LOG_ERROR("Unknown throwable");
return 1; ret = 1;
} }
clean:
return ret;
} }
string ConvertHexToDec(string hex_str) { string ConvertHexToDec(string hex_str) {
try { mpz_t dec;
mpz_init(dec);
mpz_t dec; string ret = "";
mpz_init(dec);
try {
if (mpz_set_str(dec, hex_str.c_str(), 16) == -1) { if (mpz_set_str(dec, hex_str.c_str(), 16) == -1) {
mpz_clear(dec); goto clean;
return "";
} }
char arr[mpz_sizeinbase(dec, 10) + 2]; char arr[mpz_sizeinbase(dec, 10) + 2];
char *result = mpz_get_str(arr, 10, dec); char *result = mpz_get_str(arr, 10, dec);
CHECK_ARG_CLEAN(result);
mpz_clear(dec); ret = result;
return result;
} catch (exception &e) { } catch (exception &e) {
LOG_ERROR(e.what()); LOG_ERROR(e.what());
return ""; goto clean;
} catch (...) { } catch (...) {
LOG_ERROR("Unknown throwable"); LOG_ERROR("Unknown throwable");
return ""; goto clean;
} }
clean:
mpz_clear(dec);
return ret;
} }
int Verification(char *public_shares, mpz_t decr_secret_share, int _t, int ind) { int Verification(char *public_shares, mpz_t decr_secret_share, int _t, int ind) {
try { string pub_shares_str = public_shares;
vector <libff::alt_bn128_G2> pub_shares;
uint64_t share_length = 256;
uint8_t coord_length = 64;
int ret = 0;
CHECK_ARG_CLEAN(public_shares);
string pub_shares_str = public_shares; try {
vector <libff::alt_bn128_G2> pub_shares;
uint64_t share_length = 256;
uint8_t coord_length = 64;
for (size_t i = 0; i < _t; ++i) { for (int i = 0; i < _t; i++) {
libff::alt_bn128_G2 pub_share; libff::alt_bn128_G2 pub_share;
uint64_t pos0 = share_length * i; uint64_t pos0 = share_length * i;
...@@ -445,7 +460,8 @@ int Verification(char *public_shares, mpz_t decr_secret_share, int _t, int ind) ...@@ -445,7 +460,8 @@ int Verification(char *public_shares, mpz_t decr_secret_share, int _t, int ind)
string y_c0_str = ConvertHexToDec(pub_shares_str.substr(pos0 + 2 * coord_length, coord_length)); string y_c0_str = ConvertHexToDec(pub_shares_str.substr(pos0 + 2 * coord_length, coord_length));
string y_c1_str = ConvertHexToDec(pub_shares_str.substr(pos0 + 3 * coord_length, coord_length)); string y_c1_str = ConvertHexToDec(pub_shares_str.substr(pos0 + 3 * coord_length, coord_length));
if (x_c0_str == "" || x_c1_str == "" || y_c0_str == "" || y_c1_str == "") { if (x_c0_str == "" || x_c1_str == "" || y_c0_str == "" || y_c1_str == "") {
return 2; ret = 2;
goto clean;
} }
pub_share.X.c0 = libff::alt_bn128_Fq(x_c0_str.c_str()); pub_share.X.c0 = libff::alt_bn128_Fq(x_c0_str.c_str());
pub_share.X.c1 = libff::alt_bn128_Fq(x_c1_str.c_str()); pub_share.X.c1 = libff::alt_bn128_Fq(x_c1_str.c_str());
...@@ -458,10 +474,10 @@ int Verification(char *public_shares, mpz_t decr_secret_share, int _t, int ind) ...@@ -458,10 +474,10 @@ int Verification(char *public_shares, mpz_t decr_secret_share, int _t, int ind)
libff::alt_bn128_G2 val = libff::alt_bn128_G2::zero(); libff::alt_bn128_G2 val = libff::alt_bn128_G2::zero();
for (int i = 0; i < _t; ++i) { for (int i = 0; i < _t; ++i) {
val = val + power(libff::alt_bn128_Fr(ind + 1), i) * pub_shares[i]; val = val + power(libff::alt_bn128_Fr(ind + 1), i) * pub_shares.at(i);
} }
char arr[mpz_sizeinbase(decr_secret_share, 10) + 2]; SAFE_CHAR_BUF(arr, BUF_LEN);
char *tmp = mpz_get_str(arr, 10, decr_secret_share); char *tmp = mpz_get_str(arr, 10, decr_secret_share);
libff::alt_bn128_Fr sshare(tmp); libff::alt_bn128_Fr sshare(tmp);
...@@ -478,22 +494,32 @@ int Verification(char *public_shares, mpz_t decr_secret_share, int _t, int ind) ...@@ -478,22 +494,32 @@ int Verification(char *public_shares, mpz_t decr_secret_share, int _t, int ind)
strncpy(public_shares + ConvertToString(val.X.c0).length() + 1, ConvertToString(val2.X.c0).c_str(), strncpy(public_shares + ConvertToString(val.X.c0).length() + 1, ConvertToString(val2.X.c0).c_str(),
ConvertToString(val2.X.c0).length()); ConvertToString(val2.X.c0).length());
return (val == sshare * libff::alt_bn128_G2::one()); ret = (val == sshare * libff::alt_bn128_G2::one());
} catch (exception &e) { } catch (exception &e) {
LOG_ERROR(e.what()); LOG_ERROR(e.what());
return 0; goto clean;
} catch (...) { } catch (...) {
LOG_ERROR("Unknown throwable"); LOG_ERROR("Unknown throwable");
return 0; goto clean;
} }
clean:
return ret;
} }
int calc_bls_public_key(char *skey_hex, char *pub_key) { int calc_bls_public_key(char *skey_hex, char *pub_key) {
mpz_t skey;
mpz_init(skey);
int ret = 1;
CHECK_ARG_CLEAN(skey_hex);
CHECK_ARG_CLEAN(pub_key);
try { try {
mpz_t skey;
mpz_init(skey);
if (mpz_set_str(skey, skey_hex, 16) == -1) { if (mpz_set_str(skey, skey_hex, 16) == -1) {
mpz_clear(skey); mpz_clear(skey);
return 1; return 1;
...@@ -522,4 +548,8 @@ int calc_bls_public_key(char *skey_hex, char *pub_key) { ...@@ -522,4 +548,8 @@ int calc_bls_public_key(char *skey_hex, char *pub_key) {
LOG_ERROR("Unknown throwable"); LOG_ERROR("Unknown throwable");
return 1; return 1;
} }
clean:
mpz_clear(skey);
return ret;
} }
...@@ -52,8 +52,6 @@ EXTERNC int Verification ( char * public_shares, mpz_t decr_secret_share, int _t ...@@ -52,8 +52,6 @@ EXTERNC int Verification ( char * public_shares, mpz_t decr_secret_share, int _t
EXTERNC int calc_bls_public_key(char* skey, char* pub_key); EXTERNC int calc_bls_public_key(char* skey, char* pub_key);
EXTERNC void calc_secret_shareG2_old(const char* public_shares, char * s_shareG2,
unsigned _t, unsigned ind);
EXTERNC int calc_secret_shareG2(const char* s_share, char * s_shareG2); EXTERNC int calc_secret_shareG2(const char* s_share, char * s_shareG2);
#endif #endif
......
...@@ -21,6 +21,10 @@ ...@@ -21,6 +21,10 @@
@date 2019 @date 2019
*/ */
#define SAFE_FREE(__X__) if (__X__) {free(__X__); __X__ = NULL;}
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
#ifdef USER_SPACE #ifdef USER_SPACE
#include <gmp.h> #include <gmp.h>
#else #else
...@@ -33,6 +37,12 @@ ...@@ -33,6 +37,12 @@
#include "Point.h" #include "Point.h"
#include "DomainParameters.h" #include "DomainParameters.h"
#define CHECK_ARG_ABORT(_EXPRESSION_) \
if (!(_EXPRESSION_)) { \
abort(); \
}
/*Initialize a curve*/ /*Initialize a curve*/
domain_parameters domain_parameters_init() domain_parameters domain_parameters_init()
{ {
...@@ -53,10 +63,14 @@ domain_parameters domain_parameters_init() ...@@ -53,10 +63,14 @@ domain_parameters domain_parameters_init()
/*Sets the name of a curve*/ /*Sets the name of a curve*/
void domain_parameters_set_name(domain_parameters curve, char* name) void domain_parameters_set_name(domain_parameters curve, char* name)
{ {
CHECK_ARG_ABORT(name);
int len = strlen(name); int len = strlen(name);
curve->name = (char*)calloc( sizeof(char) * (len+1), 1 ); curve->name = (char*)calloc( sizeof(char) * (len+1), 1 );
curve->name[len] = '\0'; curve->name[len] = '\0';
strncpy(curve->name, name, len+1); strncpy(curve->name, name, len+1);
} }
/*Set domain parameters from decimal unsigned long ints*/ /*Set domain parameters from decimal unsigned long ints*/
...@@ -70,6 +84,9 @@ void domain_parameters_set_ui(domain_parameters curve, ...@@ -70,6 +84,9 @@ void domain_parameters_set_ui(domain_parameters curve,
unsigned long int n, unsigned long int n,
unsigned long int h) unsigned long int h)
{ {
CHECK_ARG_ABORT(name);
domain_parameters_set_name(curve, name); domain_parameters_set_name(curve, name);
mpz_set_ui(curve->p, p); mpz_set_ui(curve->p, p);
mpz_set_ui(curve->a, a); mpz_set_ui(curve->a, a);
...@@ -77,11 +94,27 @@ void domain_parameters_set_ui(domain_parameters curve, ...@@ -77,11 +94,27 @@ void domain_parameters_set_ui(domain_parameters curve,
point_set_ui(curve->G, Gx, Gy); point_set_ui(curve->G, Gx, Gy);
mpz_set_ui(curve->n, n); mpz_set_ui(curve->n, n);
mpz_set_ui(curve->h, h); mpz_set_ui(curve->h, h);
} }
/*Set domain parameters from hexadecimal string*/ /*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) void domain_parameters_set_hex(domain_parameters curve, char* name, char* p, char* a, char* b, char* Gx, char* Gy, char* n, char* h)
{ {
CHECK_ARG_ABORT(name);
CHECK_ARG_ABORT(p);
CHECK_ARG_ABORT(a);
CHECK_ARG_ABORT(b);
CHECK_ARG_ABORT(Gx);
CHECK_ARG_ABORT(Gy);
CHECK_ARG_ABORT(n);
CHECK_ARG_ABORT(h);
domain_parameters_set_name(curve, name); domain_parameters_set_name(curve, name);
mpz_set_str(curve->p, p, 16); mpz_set_str(curve->p, p, 16);
mpz_set_str(curve->a, a, 16); mpz_set_str(curve->a, a, 16);
...@@ -89,6 +122,8 @@ void domain_parameters_set_hex(domain_parameters curve, char* name, char* p, cha ...@@ -89,6 +122,8 @@ void domain_parameters_set_hex(domain_parameters curve, char* name, char* p, cha
point_set_hex(curve->G, Gx, Gy); point_set_hex(curve->G, Gx, Gy);
mpz_set_str(curve->n, n, 16); mpz_set_str(curve->n, n, 16);
mpz_set_str(curve->h, h, 16); mpz_set_str(curve->h, h, 16);
} }
/*Release memory*/ /*Release memory*/
...@@ -104,7 +139,7 @@ void domain_parameters_clear(domain_parameters curve) ...@@ -104,7 +139,7 @@ void domain_parameters_clear(domain_parameters curve)
point_clear(curve->G); point_clear(curve->G);
mpz_clear(curve->n); mpz_clear(curve->n);
mpz_clear(curve->h); mpz_clear(curve->h);
free(curve->name); SAFE_FREE(curve->name);
free(curve); free(curve);
} }
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
@date 2019 @date 2019
*/ */
#define GMP_WITH_SGX #define GMP_WITH_SGX 1
#include <string.h> #include <string.h>
#include <cstdint> #include <cstdint>
......
...@@ -68,5 +68,11 @@ extern domain_parameters curve; ...@@ -68,5 +68,11 @@ extern domain_parameters curve;
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;} #define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__); #define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
#define CHECK_ARG_CLEAN(_EXPRESSION_) \
if (!(_EXPRESSION_)) { \
LOG_ERROR("State check failed::");LOG_ERROR(#_EXPRESSION_); \
LOG_ERROR(__FILE__); LOG_ERROR(__FUNCTION__);\
goto clean;}
#endif //SGXWALLET_ENCLAVECOMMON_H #endif //SGXWALLET_ENCLAVECOMMON_H
...@@ -37,359 +37,27 @@ ...@@ -37,359 +37,27 @@
*Handbook of applied cryptography: Algorithm 2.143. */ *Handbook of applied cryptography: Algorithm 2.143. */
void number_theory_exp_modp(mpz_t R, mpz_t a, mpz_t k, mpz_t P) 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); 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() */ /*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) 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); 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 /*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*/ *http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm*/
void number_theory_inverse(mpz_t R, mpz_t A, mpz_t P) 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); 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 /*Calculates the legendre symbol of a and p
*Handbook of applied cryptography: Fact 2.146 */ *Handbook of applied cryptography: Fact 2.146 */
int number_theory_legendre(mpz_t a, mpz_t p) 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 return mpz_legendre(a, 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
} }
...@@ -25,9 +25,6 @@ ...@@ -25,9 +25,6 @@
*Handbook of applied cryptography: Algorithm 2.143. */ *Handbook of applied cryptography: Algorithm 2.143. */
void number_theory_exp_modp(mpz_t R, mpz_t a, mpz_t k, mpz_t P); 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 /*Calculate the multiplicative inverse of a mod p, using the extended euclidean algorithm
*Handbook of applied cryptography: Algorithm 2.107 *Handbook of applied cryptography: Algorithm 2.107
...@@ -41,8 +38,4 @@ int number_theory_legendre(mpz_t a, mpz_t p); ...@@ -41,8 +38,4 @@ int number_theory_legendre(mpz_t a, mpz_t p);
/*Calculate R = a^k mod P, wraps around number_theory_exp_modp() */ /*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); 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 #define EXTERNAL_NUMBER_THEORY_IMPLEMENTATION 0
...@@ -27,6 +27,10 @@ ...@@ -27,6 +27,10 @@
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#define SAFE_FREE(__X__) if (__X__) {free(__X__); __X__ = NULL;}
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
#ifdef USER_SPACE #ifdef USER_SPACE
#include <gmp.h> #include <gmp.h>
#else #else
...@@ -301,55 +305,6 @@ This is not the most effecient method of point multiplication, but it's faster t ...@@ -301,55 +305,6 @@ This is not the most effecient method of point multiplication, but it's faster 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 /*Compress a point to hexadecimal string
*This function is implemented as specified in SEC 1: Elliptic Curve Cryptography, section 2.3.3.*/ *This function is implemented as specified in SEC 1: Elliptic Curve Cryptography, section 2.3.3.*/
...@@ -387,6 +342,6 @@ void point_clear(point p) ...@@ -387,6 +342,6 @@ void point_clear(point p)
return; return;
mpz_clear(p->x); mpz_clear(p->x);
mpz_clear(p->y); mpz_clear(p->y);
free(p); SAFE_FREE(p);
} }
...@@ -66,9 +66,6 @@ EXTERNC int point_set_str(point p, const char *x, const char *y, int base); ...@@ -66,9 +66,6 @@ EXTERNC int point_set_str(point p, const char *x, const char *y, int base);
/*Compare two points return 1 if not the same, returns 0 if they are the same*/ /*Compare two points return 1 if not the same, returns 0 if they are the same*/
EXTERNC bool point_cmp(point P, point Q); EXTERNC 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.*/
EXTERNC void point_decompress(point P, char* zPoint, domain_parameters curve);
/*Compress a point to hexadecimal string /*Compress a point to hexadecimal string
*This function is implemented as specified in SEC 1: Elliptic Curve Cryptography, section 2.3.3.*/ *This function is implemented as specified in SEC 1: Elliptic Curve Cryptography, section 2.3.3.*/
......
...@@ -25,13 +25,21 @@ ...@@ -25,13 +25,21 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <assert.h> #include <assert.h>
#include <string.h>
#define SAFE_FREE(__X__) if (__X__) {free(__X__); __X__ = NULL;}
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
#ifdef USER_SPACE #ifdef USER_SPACE
#include <gmp.h> #include <gmp.h>
#else #else
#include <../tgmp-build/include/sgx_tgmp.h> #include <../tgmp-build/include/sgx_tgmp.h>
#endif #endif
#include "DomainParameters.h" #include "DomainParameters.h"
...@@ -50,10 +58,9 @@ signature signature_init() { ...@@ -50,10 +58,9 @@ signature signature_init() {
} }
/*Set signature from strings of a base from 2-62*/ /*Set signature from strings of a base from 2-62*/
int signature_set_str(signature sig, const char *r, const char *s, int base) { int signature_set_str(signature sig, const char *r, const char *s, int base) {
if ( mpz_set_str(sig->r, r, base) !=0 || mpz_set_str(sig->s, s, base) != 0) { if (mpz_set_str(sig->r, r, base) != 0 || mpz_set_str(sig->s, s, base) != 0) {
return 1; return 1;
} }
return 0; return 0;
...@@ -88,108 +95,107 @@ void signature_extract_public_key(point public_key, mpz_t private_key, domain_pa ...@@ -88,108 +95,107 @@ void signature_extract_public_key(point public_key, mpz_t private_key, domain_pa
} }
#ifndef USER_SPACE #ifndef USER_SPACE
/*Generate signature for a message*/ /*Generate signature for a message*/
void signature_sign(signature sig, mpz_t message, mpz_t private_key, domain_parameters curve) { 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 //message must not have a bit length longer than that of n
//see: Guide to Elliptic Curve Cryptography, section 4.4.1. if (mpz_sizeinbase(message, 2) > mpz_sizeinbase(curve->n, 2)) {
LOG_ERROR("mpz_sizeinbase(message, 2) > mpz_sizeinbase(curve->n, 2))");
{ return;
assert(mpz_sizeinbase(message, 2) <= mpz_sizeinbase(curve->n, 2));
point Q = point_init();
//Initializing variables
mpz_t k, x, r, t1, t2, t3, t4, t5, s, n_div_2, rem, neg, seed;
mpz_init(k);
mpz_init(x);
mpz_init(r);
mpz_init(t1);
mpz_init(t2);
mpz_init(t3);
mpz_init(s);
mpz_init(t4);
mpz_init(t5);
mpz_init(n_div_2);
mpz_init(rem);
mpz_init(neg);
mpz_init(seed);
unsigned char *rand_char = (unsigned char *) calloc(32, 1);
get_global_random(rand_char, 32);
signature_sign_start:
get_global_random(rand_char, 32);
mpz_import(seed, 32, 1, sizeof(rand_char[0]), 0, 0, rand_char);
mpz_mod(k, seed, curve->p);
//Calculate x
point_multiplication(Q, k, curve->G, curve);
mpz_set(x, Q->x);
//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;
//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_invert(t1, k, curve->n);
mpz_mul(t2, private_key, r); //t2 = d*r
mpz_add(t3, message, t2); //t3 = e+t2
mpz_mod(t4, t3, curve->n); //t2 = t3 mod n
mpz_mul(t5, t4, t1); //t3 = t2 * t1
mpz_mod(s, t5, curve->n); //s = t3 mod n
//Calculate v
mpz_mod_ui(rem, Q->y, 2);
mpz_t s_mul_2;
mpz_init(s_mul_2);
mpz_mul_ui(s_mul_2, s, 2);
unsigned b = 0;
if (mpz_cmp(s_mul_2, curve->n) > 0) {
b = 1;
}
sig->v = mpz_get_ui(rem) ^ b;
mpz_cdiv_q_ui(n_div_2, curve->n, 2);
if (mpz_cmp(s, n_div_2) > 0) {
mpz_sub(neg, curve->n, s);
mpz_set(s, neg);
}
//Set signature
mpz_set(sig->r, r);
mpz_set(sig->s, s);
clean:
free(rand_char);
point_clear(Q);
mpz_clear(k);
mpz_clear(r);
mpz_clear(s);
mpz_clear(x);
mpz_clear(rem);
mpz_clear(neg);
mpz_clear(t1);
mpz_clear(t2);
mpz_clear(t3);
mpz_clear(seed);
mpz_clear(n_div_2);
mpz_clear(s_mul_2);
} }
point Q = point_init();
//Initializing variables
mpz_t k, x, r, t1, t2, t3, t4, t5, s, n_div_2, rem, neg, seed;
mpz_init(k);
mpz_init(x);
mpz_init(r);
mpz_init(t1);
mpz_init(t2);
mpz_init(t3);
mpz_init(s);
mpz_init(t4);
mpz_init(t5);
mpz_init(n_div_2);
mpz_init(rem);
mpz_init(neg);
mpz_init(seed);
mpz_t s_mul_2;
mpz_init(s_mul_2);
SAFE_CHAR_BUF(rand_char, 32);
get_global_random(rand_char, 32);
signature_sign_start:
get_global_random(rand_char, 32);
mpz_import(seed, 32, 1, sizeof(rand_char[0]), 0, 0, rand_char);
mpz_mod(k, seed, curve->p);
//Calculate x
point_multiplication(Q, k, curve->G, curve);
mpz_set(x, Q->x);
//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;
//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_invert(t1, k, curve->n);
mpz_mul(t2, private_key, r); //t2 = d*r
mpz_add(t3, message, t2); //t3 = e+t2
mpz_mod(t4, t3, curve->n); //t2 = t3 mod n
mpz_mul(t5, t4, t1); //t3 = t2 * t1
mpz_mod(s, t5, curve->n); //s = t3 mod n
//Calculate v
mpz_mod_ui(rem, Q->y, 2);
mpz_mul_ui(s_mul_2, s, 2);
unsigned b = 0;
if (mpz_cmp(s_mul_2, curve->n) > 0) {
b = 1;
}
sig->v = mpz_get_ui(rem) ^ b;
mpz_cdiv_q_ui(n_div_2, curve->n, 2);
if (mpz_cmp(s, n_div_2) > 0) {
mpz_sub(neg, curve->n, s);
mpz_set(s, neg);
}
//Set signature
mpz_set(sig->r, r);
mpz_set(sig->s, s);
point_clear(Q);
mpz_clear(k);
mpz_clear(r);
mpz_clear(s);
mpz_clear(x);
mpz_clear(rem);
mpz_clear(neg);
mpz_clear(t1);
mpz_clear(t2);
mpz_clear(t3);
mpz_clear(seed);
mpz_clear(n_div_2);
mpz_clear(s_mul_2);
} }
#endif #endif
......
This diff is collapsed.
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#define ECDSA_ENCR_LEN 93 #define ECDSA_ENCR_LEN 93
#define ECDSA_BIN_LEN 33 #define ECDSA_BIN_LEN 33
#define SMALL_BUF_SIZE 1024 #define SMALL_BUF_SIZE 1024
#define VERY_SMALL_BUF_SIZE 512
#define TINY_BUF_SIZE 256 #define TINY_BUF_SIZE 256
enclave { enclave {
...@@ -23,8 +24,7 @@ enclave { ...@@ -23,8 +24,7 @@ enclave {
public void trustedSetSEK( public void trustedSetSEK(
[out] int *errStatus, [out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char *err_string, [out, count = SMALL_BUF_SIZE] char *err_string,
[in, count = SMALL_BUF_SIZE] uint8_t *encrypted_SEK, [in, count = SMALL_BUF_SIZE] uint8_t *encrypted_SEK);
uint64_t encr_len);
public void trustedSetSEK_backup( public void trustedSetSEK_backup(
[out] int *errStatus, [out] int *errStatus,
...@@ -137,11 +137,11 @@ enclave { ...@@ -137,11 +137,11 @@ enclave {
public void trustedBlsSignMessageAES ( public void trustedBlsSignMessageAES (
[out] int *errStatus, [out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string, [out, count = TINY_BUF_SIZE] char* err_string,
[in, count = SMALL_BUF_SIZE] uint8_t* encrypted_key, [in, count = TINY_BUF_SIZE] uint8_t* encrypted_key,
uint32_t enc_len, uint32_t enc_len,
[in, string] char* hashX , [in, string] char* hashX ,
[in, string] char* hashY , [in, string] char* hashY,
[out, count = SMALL_BUF_SIZE] char* signature); [out, count = SMALL_BUF_SIZE] char* signature);
public void trustedGetBlsPubKeyAES( public void trustedGetBlsPubKeyAES(
......
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