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) {
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 ) {
cerr << "RPCException thrown" << endl;
throw SGXException(status, errMsg.data()) ;
......
......@@ -41,7 +41,7 @@ int AES_encrypt(char *message, uint8_t *encr_message, uint64_t encrLen) {
return -2;
}
auto len = strlen(message);
uint64_t len = strlen(message);
if (len + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE > encrLen ) {
LOG_ERROR("Output buffer too small");
......@@ -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);
auto msgLen = 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,
......@@ -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,
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,
NULL, 0,
(sgx_aes_gcm_128bit_tag_t *)encr_message);
......
......@@ -42,15 +42,19 @@
#include "EnclaveCommon.h"
#include <string.h>
int gen_session_key(char *skey_str, char *pb_keyB, char *common_key) {
int ret = -1;
LOG_INFO(__FUNCTION__);
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();
......@@ -110,6 +114,22 @@ int session_key_recover(const char *skey_str, const char *sshare, char *common_k
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) {
LOG_ERROR("session_key_recover: Null common_key");
goto clean;
......@@ -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) {
goto clean;
......@@ -191,7 +205,7 @@ int xor_encrypt(char *key, char *message, char *cypher) {
uint64_t key_length;
if (!hex2carray(key, &key_length, key_bin)) {
if (!hex2carray(key, &key_length, (uint8_t *) key_bin)) {
goto clean;
}
......@@ -205,7 +219,7 @@ int xor_encrypt(char *key, char *message, char *cypher) {
cypher_bin[i] = msg_bin[i] ^ key_bin[i];
}
carray2Hex(cypher_bin, 32, cypher);
carray2Hex((unsigned char*) cypher_bin, 32, cypher);
ret = 0;
......@@ -241,14 +255,14 @@ int xor_decrypt(char *key, char *cypher, char *message) {
SAFE_CHAR_BUF(key_bin,33)
uint64_t key_length;
if (!hex2carray(key, &key_length, key_bin)) {
if (!hex2carray(key, &key_length, (uint8_t*) key_bin)) {
goto clean;
}
uint64_t cypher_length;
SAFE_CHAR_BUF(cypher_bin, 33);
if (!hex2carray(cypher, &cypher_length, cypher_bin)) {
if (!hex2carray(cypher, &cypher_length, (uint8_t *) cypher_bin)) {
goto clean;
}
......@@ -256,7 +270,7 @@ int xor_decrypt(char *key, char *cypher, char *message) {
msg_bin[i] = cypher_bin[i] ^ key_bin[i];
}
carray2Hex(msg_bin, 32, message);
carray2Hex((unsigned char*) msg_bin, 32, message);
ret = 0;
......
......@@ -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> result;
string str(coeffs);
string delim;
CHECK_ARG_CLEAN(coeffs);
try {
delim.push_back(symbol);
......@@ -183,6 +187,8 @@ int gen_dkg_poly(char *secret, unsigned _t) {
int status = 1;
string result;
CHECK_ARG_CLEAN(secret);
try {
for (size_t i = 0; i < _t; ++i) {
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
libff::alt_bn128_Fr pow = libff::alt_bn128_Fr::one();
for (unsigned i = 0; i < pol.size(); ++i) {
result += pol[i] * pow;
result += pol.at(i) * pow;
pow *= point;
}
......@@ -247,6 +253,11 @@ void calc_secret_shares(const char *decrypted_coeffs,
string result;
char symbol = ':';
CHECK_ARG_CLEAN(decrypted_coeffs);
CHECK_ARG_CLEAN(secret_shares);
CHECK_ARG_CLEAN(_n > 0);
CHECK_ARG_CLEAN(_t <= _n);
try {
vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
......@@ -260,18 +271,27 @@ void calc_secret_shares(const char *decrypted_coeffs,
} catch (exception &e) {
LOG_ERROR(e.what());
return;
goto clean;
} catch (...) {
LOG_ERROR("Unknown throwable");
return;
goto clean;
}
clean:
;
}
int calc_secret_share(const char *decrypted_coeffs, char *s_share,
unsigned _t, unsigned _n, unsigned ind) {
int result = 1;
CHECK_ARG_CLEAN(decrypted_coeffs);
CHECK_ARG_CLEAN(s_share);
CHECK_ARG_CLEAN(_n > 0);
CHECK_ARG_CLEAN(_t <= _n);
try {
char 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,
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) {
......@@ -329,6 +328,9 @@ int calc_secret_shareG2(const char *s_share, char *s_shareG2) {
mpz_t share;
mpz_init(share);
CHECK_ARG_CLEAN(s_share);
CHECK_ARG_CLEAN(s_shareG2);
try {
......@@ -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,
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 {
// calculate for each node a list of public shares
string result;
char symbol = ':';
vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
if (poly.size() != _t) {
return 1;
goto clean;
}
for (size_t i = 0; i < _t; ++i) {
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,
result += pub_share_str + ",";
}
strncpy(public_shares, result.c_str(), result.length());
return 0;
ret = 0;
} catch (exception &e) {
LOG_ERROR(e.what());
return 1;
ret = 1;
} catch (...) {
LOG_ERROR("Unknown throwable");
return 1;
ret = 1;
}
clean:
return ret;
}
string ConvertHexToDec(string hex_str) {
try {
mpz_t dec;
mpz_init(dec);
mpz_t dec;
mpz_init(dec);
string ret = "";
try {
if (mpz_set_str(dec, hex_str.c_str(), 16) == -1) {
mpz_clear(dec);
return "";
goto clean;
}
char arr[mpz_sizeinbase(dec, 10) + 2];
char *result = mpz_get_str(arr, 10, dec);
mpz_clear(dec);
return result;
CHECK_ARG_CLEAN(result);
ret = result;
} catch (exception &e) {
LOG_ERROR(e.what());
return "";
goto clean;
} catch (...) {
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) {
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;
vector <libff::alt_bn128_G2> pub_shares;
uint64_t share_length = 256;
uint8_t coord_length = 64;
try {
for (size_t i = 0; i < _t; ++i) {
for (int i = 0; i < _t; i++) {
libff::alt_bn128_G2 pub_share;
uint64_t pos0 = share_length * i;
......@@ -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_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 == "") {
return 2;
ret = 2;
goto clean;
}
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());
......@@ -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();
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);
libff::alt_bn128_Fr sshare(tmp);
......@@ -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(),
ConvertToString(val2.X.c0).length());
return (val == sshare * libff::alt_bn128_G2::one());
ret = (val == sshare * libff::alt_bn128_G2::one());
} catch (exception &e) {
LOG_ERROR(e.what());
return 0;
goto clean;
} catch (...) {
LOG_ERROR("Unknown throwable");
return 0;
goto clean;
}
clean:
return ret;
}
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 {
mpz_t skey;
mpz_init(skey);
if (mpz_set_str(skey, skey_hex, 16) == -1) {
mpz_clear(skey);
return 1;
......@@ -522,4 +548,8 @@ int calc_bls_public_key(char *skey_hex, char *pub_key) {
LOG_ERROR("Unknown throwable");
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
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);
#endif
......
......@@ -21,6 +21,10 @@
@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
#include <gmp.h>
#else
......@@ -33,6 +37,12 @@
#include "Point.h"
#include "DomainParameters.h"
#define CHECK_ARG_ABORT(_EXPRESSION_) \
if (!(_EXPRESSION_)) { \
abort(); \
}
/*Initialize a curve*/
domain_parameters domain_parameters_init()
{
......@@ -53,10 +63,14 @@ domain_parameters domain_parameters_init()
/*Sets the name of a curve*/
void domain_parameters_set_name(domain_parameters curve, char* name)
{
CHECK_ARG_ABORT(name);
int len = strlen(name);
curve->name = (char*)calloc( sizeof(char) * (len+1), 1 );
curve->name[len] = '\0';
strncpy(curve->name, name, len+1);
}
/*Set domain parameters from decimal unsigned long ints*/
......@@ -70,6 +84,9 @@ void domain_parameters_set_ui(domain_parameters curve,
unsigned long int n,
unsigned long int h)
{
CHECK_ARG_ABORT(name);
domain_parameters_set_name(curve, name);
mpz_set_ui(curve->p, p);
mpz_set_ui(curve->a, a);
......@@ -77,11 +94,27 @@ void domain_parameters_set_ui(domain_parameters curve,
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)
{
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);
mpz_set_str(curve->p, p, 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
point_set_hex(curve->G, Gx, Gy);
mpz_set_str(curve->n, n, 16);
mpz_set_str(curve->h, h, 16);
}
/*Release memory*/
......@@ -104,7 +139,7 @@ void domain_parameters_clear(domain_parameters curve)
point_clear(curve->G);
mpz_clear(curve->n);
mpz_clear(curve->h);
free(curve->name);
SAFE_FREE(curve->name);
free(curve);
}
......@@ -21,7 +21,7 @@
@date 2019
*/
#define GMP_WITH_SGX
#define GMP_WITH_SGX 1
#include <string.h>
#include <cstdint>
......
......@@ -68,5 +68,11 @@ extern domain_parameters curve;
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
#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
......@@ -37,359 +37,27 @@
*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
return mpz_legendre(a, p);
}
......@@ -25,9 +25,6 @@
*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
......@@ -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() */
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
......@@ -27,6 +27,10 @@
#include <assert.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
#include <gmp.h>
#else
......@@ -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
*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)
return;
mpz_clear(p->x);
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);
/*Compare two points return 1 if not the same, returns 0 if they are the same*/
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
*This function is implemented as specified in SEC 1: Elliptic Curve Cryptography, section 2.3.3.*/
......
......@@ -25,13 +25,21 @@
#include <stdio.h>
#include <stdbool.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
#include <gmp.h>
#else
#include <../tgmp-build/include/sgx_tgmp.h>
#endif
#include "DomainParameters.h"
......@@ -50,10 +58,9 @@ signature signature_init() {
}
/*Set signature from strings of a base from 2-62*/
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 0;
......@@ -88,108 +95,107 @@ void signature_extract_public_key(point public_key, mpz_t private_key, domain_pa
}
#ifndef USER_SPACE
/*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));
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);
if (mpz_sizeinbase(message, 2) > mpz_sizeinbase(curve->n, 2)) {
LOG_ERROR("mpz_sizeinbase(message, 2) > mpz_sizeinbase(curve->n, 2))");
return;
}
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
......
......@@ -171,8 +171,7 @@ void get_global_random(unsigned char *_randBuff, uint64_t _size) {
int status;
int *errStatus = &status;
*errString = 0;
*errStatus = UNKNOWN_ERROR;
INIT_ERROR_STATE
CHECK_STATE(_size <= 32)
CHECK_STATE(_randBuff);
......@@ -181,8 +180,7 @@ void get_global_random(unsigned char *_randBuff, uint64_t _size) {
CHECK_STATE(sgx_sha256_init(&shaStateHandle) == SGX_SUCCESS);
CHECK_STATE(sgx_sha256_update(globalRandom, 32, shaStateHandle) == SGX_SUCCESS);
CHECK_STATE(sgx_sha256_get_hash(shaStateHandle, globalRandom) == SGX_SUCCESS);
CHECK_STATE(sgx_sha256_get_hash(shaStateHandle, globalRandom) == SGX_SUCCESS);
CHECK_STATE(sgx_sha256_get_hash(shaStateHandle, (sgx_sha256_hash_t *)globalRandom) == SGX_SUCCESS);
CHECK_STATE(sgx_sha256_close(shaStateHandle) == SGX_SUCCESS);
memcpy(_randBuff, globalRandom, _size);
......@@ -201,7 +199,7 @@ void trustedGenerateSEK(int *errStatus, char *errString,
SAFE_CHAR_BUF(SEK_raw, SGX_AESGCM_KEY_SIZE);;
uint32_t hex_aes_key_length = SGX_AESGCM_KEY_SIZE * 2;
carray2Hex(SEK_raw, SGX_AESGCM_KEY_SIZE, SEK_hex);
carray2Hex((uint8_t*) SEK_raw, SGX_AESGCM_KEY_SIZE, SEK_hex);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, hex_aes_key_length + 1);
......@@ -215,19 +213,23 @@ void trustedGenerateSEK(int *errStatus, char *errString,
*enc_len = sealedLen;
*errStatus = 0;
SET_SUCCESS
clean:
;
LOG_INFO("SGX call completed");
}
void trustedSetSEK(int *errStatus, char *errString, uint8_t *encrypted_SEK, uint64_t encr_len) {
void trustedSetSEK(int *errStatus, char *errString, uint8_t *encrypted_SEK) {
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encrypted_SEK);
SAFE_CHAR_BUF(aes_key_hex, BUF_LEN);
uint32_t dec_len;
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *) encrypted_SEK, NULL, 0, aes_key_hex, &encr_len);
(const sgx_sealed_data_t *) encrypted_SEK, NULL, 0,
(uint8_t *)aes_key_hex, &dec_len);
CHECK_STATUS2("sgx unseal SEK failed with status %d");
......@@ -238,6 +240,7 @@ void trustedSetSEK(int *errStatus, char *errString, uint8_t *encrypted_SEK, uint
SET_SUCCESS
clean:
;
LOG_INFO("SGX call completed");
}
void trustedSetSEK_backup(int *errStatus, char *errString,
......@@ -263,6 +266,7 @@ void trustedSetSEK_backup(int *errStatus, char *errString,
SET_SUCCESS
clean:
;
LOG_INFO("SGX call completed");
}
void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
......@@ -283,7 +287,7 @@ void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
point Pkey = point_init();
get_global_random(rand_char, 32);
get_global_random((unsigned char *)rand_char, 32);
mpz_import(seed, 32, 1, sizeof(rand_char[0]), 0, 0, rand_char);
......@@ -291,7 +295,7 @@ void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
signature_extract_public_key(Pkey, skey, curve);
int len = mpz_sizeinbase(Pkey->x, ECDSA_SKEY_BASE) + 2;SAFE_CHAR_BUF(arr_x, BUF_LEN);
SAFE_CHAR_BUF(arr_x, BUF_LEN);
mpz_get_str(arr_x, ECDSA_SKEY_BASE, Pkey->x);
int n_zeroes = 64 - strlen(arr_x);
for (int i = 0; i < n_zeroes; i++) {
......@@ -316,9 +320,9 @@ void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
}
strncpy(skey_str + n_zeroes, arr_skey_str, 65 - n_zeroes);
skey_str[ECDSA_SKEY_LEN - 1] = 0;
snprintf(errString, BUF_LEN, "skey len is %d\n", strlen(skey_str));
snprintf(errString, BUF_LEN, "skey len is %d\n", (int) strlen(skey_str));
int status = AES_encrypt(skey_str, encryptedPrivateKey, BUF_LEN);
int status = AES_encrypt((char *) skey_str, encryptedPrivateKey, BUF_LEN);
CHECK_STATUS("ecdsa private key encryption failed");
*enc_len = strlen(skey_str) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;
......@@ -332,6 +336,7 @@ void trustedGenerateEcdsaKeyAES(int *errStatus, char *errString,
mpz_clear(seed);
mpz_clear(skey);
point_clear(Pkey);
LOG_INFO("SGX call completed");
}
void trustedGetPublicEcdsaKeyAES(int *errStatus, char *errString,
......@@ -339,15 +344,17 @@ void trustedGetPublicEcdsaKeyAES(int *errStatus, char *errString,
LOG_DEBUG(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encryptedPrivateKey);
CHECK_STATE(pub_key_x);
CHECK_STATE(pub_key_y);
SAFE_CHAR_BUF(skey, ECDSA_SKEY_LEN);
mpz_t privateKeyMpz;
mpz_init(privateKeyMpz);
point Pkey = point_init();
point pKey = point_init();
point pKey_test = point_init();
CHECK_STATE(encryptedPrivateKey);
CHECK_STATE(pub_key_x);
CHECK_STATE(pub_key_y);
int status = AES_decrypt(encryptedPrivateKey, enc_len, skey, ECDSA_SKEY_LEN);
CHECK_STATUS2("AES_decrypt failed with status %d");
......@@ -359,12 +366,12 @@ void trustedGetPublicEcdsaKeyAES(int *errStatus, char *errString,
CHECK_STATUS("mpz_set_str failed for private key");
signature_extract_public_key(Pkey, privateKeyMpz, curve);
signature_extract_public_key(pKey, privateKeyMpz, curve);
point Pkey_test = point_init();
point_multiplication(Pkey_test, privateKeyMpz, curve->G, curve);
if (!point_cmp(Pkey, Pkey_test)) {
point_multiplication(pKey_test, privateKeyMpz, curve->G, curve);
if (!point_cmp(pKey, pKey_test)) {
snprintf(errString, BUF_LEN, "Points are not equal");
LOG_ERROR(errString);
*errStatus = -11;
......@@ -372,7 +379,7 @@ void trustedGetPublicEcdsaKeyAES(int *errStatus, char *errString,
}
SAFE_CHAR_BUF(arr_x, BUF_LEN);
mpz_get_str(arr_x, ECDSA_SKEY_BASE, Pkey->x);
mpz_get_str(arr_x, ECDSA_SKEY_BASE, pKey->x);
int n_zeroes = 64 - strlen(arr_x);
for (int i = 0; i < n_zeroes; i++) {
......@@ -382,7 +389,7 @@ void trustedGetPublicEcdsaKeyAES(int *errStatus, char *errString,
strncpy(pub_key_x + n_zeroes, arr_x, 1024 - n_zeroes);
SAFE_CHAR_BUF(arr_y, BUF_LEN);
mpz_get_str(arr_y, ECDSA_SKEY_BASE, Pkey->y);
mpz_get_str(arr_y, ECDSA_SKEY_BASE, pKey->y);
n_zeroes = 64 - strlen(arr_y);
for (int i = 0; i < n_zeroes; i++) {
......@@ -393,8 +400,9 @@ void trustedGetPublicEcdsaKeyAES(int *errStatus, char *errString,
SET_SUCCESS
clean:
mpz_clear(privateKeyMpz);
point_clear(Pkey);
point_clear(Pkey_test);
point_clear(pKey);
point_clear(pKey_test);
LOG_DEBUG("SGX call completed");
}
static uint64_t sigCounter = 0;
......@@ -478,11 +486,49 @@ void trustedEcdsaSignAES(int *errStatus, char *errString, uint8_t *encryptedPriv
mpz_clear(privateKeyMpz);
mpz_clear(msgMpz);
signature_free(sign);
LOG_DEBUG("SGX call completed");
}
void trustedDecryptKeyAES(int *errStatus, char *errString, uint8_t *encryptedPrivateKey,
uint32_t enc_len, char *key) {
LOG_DEBUG(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encryptedPrivateKey);
CHECK_STATE(key);
*errStatus = -9;
int status = AES_decrypt(encryptedPrivateKey, enc_len, key, 3072);
if (status != 0) {
*errStatus = status;
snprintf(errString, BUF_LEN, "aes decrypt failed with status %d", status);
LOG_ERROR(errString);
goto clean;
}
*errStatus = -10;
uint64_t keyLen = strnlen(key, MAX_KEY_LENGTH);
if (keyLen == MAX_KEY_LENGTH) {
snprintf(errString, BUF_LEN, "Key is not null terminated");
LOG_ERROR(errString);
goto clean;
}
SET_SUCCESS
clean:
;
}
void trustedEncryptKeyAES(int *errStatus, char *errString, const char *key,
uint8_t *encryptedPrivateKey, uint32_t *enc_len) {
LOG_DEBUG(__FUNCTION__);
LOG_INFO(__FUNCTION__);
*errString = 0;
*errStatus = UNKNOWN_ERROR;
......@@ -492,7 +538,7 @@ void trustedEncryptKeyAES(int *errStatus, char *errString, const char *key,
*errStatus = UNKNOWN_ERROR;
int status = AES_encrypt(key, encryptedPrivateKey, BUF_LEN);
int status = AES_encrypt((char *)key, encryptedPrivateKey, BUF_LEN);
CHECK_STATUS2("AES encrypt failed with status %d");
......@@ -523,42 +569,9 @@ void trustedEncryptKeyAES(int *errStatus, char *errString, const char *key,
SET_SUCCESS
clean:
;
LOG_INFO("SGX call completed");
}
void trustedDecryptKeyAES(int *errStatus, char *errString, uint8_t *encryptedPrivateKey,
uint32_t enc_len, char *key) {
LOG_DEBUG(__FUNCTION__);
INIT_ERROR_STATE
CHECK_STATE(encryptedPrivateKey);
CHECK_STATE(key);
*errStatus = -9;
int status = AES_decrypt(encryptedPrivateKey, enc_len, key, 3072);
if (status != 0) {
*errStatus = status;
snprintf(errString, BUF_LEN, "aes decrypt failed with status %d", status);
LOG_ERROR(errString);
goto clean;
}
*errStatus = -10;
uint64_t keyLen = strnlen(key, MAX_KEY_LENGTH);
if (keyLen == MAX_KEY_LENGTH) {
snprintf(errString, BUF_LEN, "Key is not null terminated");
LOG_ERROR(errString);
goto clean;
}
SET_SUCCESS
clean:
;
}
void trustedBlsSignMessageAES(int *errStatus, char *errString, uint8_t *encryptedPrivateKey,
uint32_t enc_len, char *_hashX,
......@@ -589,8 +602,12 @@ void trustedBlsSignMessageAES(int *errStatus, char *errString, uint8_t *encrypte
}
SET_SUCCESS
LOG_DEBUG("SGX call completed");
clean:
;
LOG_DEBUG("SGX call completed");
}
void
......@@ -630,6 +647,7 @@ trustedGenDkgSecretAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_s
SET_SUCCESS
clean:
;
LOG_INFO("SGX call completed");
}
void
......@@ -651,6 +669,7 @@ trustedDecryptDkgSecretAES(int *errStatus, char *errString, uint8_t *encrypted_d
clean:
;
LOG_INFO("SGX call completed");
}
......@@ -670,13 +689,14 @@ void trustedSetEncryptedDkgPolyAES(int *errStatus, char *errString, uint8_t *enc
SET_SUCCESS
clean:
;
LOG_INFO("SGX call completed");
}
void trustedGetEncryptedSecretShareAES(int *errStatus, char *errString, uint8_t *encrypted_skey, uint32_t *dec_len,
char *result_str, char *s_shareG2, char *pub_keyB, uint8_t _t, uint8_t _n,
uint8_t ind) {
LOG_DEBUG(__FUNCTION__);
LOG_INFO(__FUNCTION__);
INIT_ERROR_STATE
uint32_t enc_len;
......@@ -733,6 +753,7 @@ void trustedGetEncryptedSecretShareAES(int *errStatus, char *errString, uint8_t
clean:
;
LOG_INFO("SGX call completed");
}
void trustedGetPublicSharesAES(int *errStatus, char *errString, uint8_t *encrypted_dkg_secret, uint32_t enc_len,
......@@ -760,6 +781,7 @@ void trustedGetPublicSharesAES(int *errStatus, char *errString, uint8_t *encrypt
clean:
;
LOG_INFO("SGX call completed");
}
void trustedDkgVerifyAES(int *errStatus, char *errString, const char *public_shares, const char *s_share,
......@@ -807,6 +829,7 @@ void trustedDkgVerifyAES(int *errStatus, char *errString, const char *public_sha
clean:
mpz_clear(s);
LOG_INFO("SGX call completed");
}
void trustedCreateBlsKeyAES(int *errStatus, char *errString, const char *s_shares,
......@@ -910,6 +933,7 @@ void trustedCreateBlsKeyAES(int *errStatus, char *errString, const char *s_share
mpz_clear(bls_key);
mpz_clear(sum);
mpz_clear(q);
LOG_INFO("SGX call completed");
}
void
......@@ -937,4 +961,5 @@ trustedGetBlsPubKeyAES(int *errStatus, char *errString, uint8_t *encryptedPrivat
SET_SUCCESS
clean:
;
LOG_DEBUG("SGX call completed");
}
......@@ -3,6 +3,7 @@
#define ECDSA_ENCR_LEN 93
#define ECDSA_BIN_LEN 33
#define SMALL_BUF_SIZE 1024
#define VERY_SMALL_BUF_SIZE 512
#define TINY_BUF_SIZE 256
enclave {
......@@ -23,8 +24,7 @@ enclave {
public void trustedSetSEK(
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char *err_string,
[in, count = SMALL_BUF_SIZE] uint8_t *encrypted_SEK,
uint64_t encr_len);
[in, count = SMALL_BUF_SIZE] uint8_t *encrypted_SEK);
public void trustedSetSEK_backup(
[out] int *errStatus,
......@@ -137,11 +137,11 @@ enclave {
public void trustedBlsSignMessageAES (
[out] int *errStatus,
[out, count = SMALL_BUF_SIZE] char* err_string,
[in, count = SMALL_BUF_SIZE] uint8_t* encrypted_key,
[out, count = TINY_BUF_SIZE] char* err_string,
[in, count = TINY_BUF_SIZE] uint8_t* encrypted_key,
uint32_t enc_len,
[in, string] char* hashX ,
[in, string] char* hashY ,
[in, string] char* hashY,
[out, count = SMALL_BUF_SIZE] char* signature);
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