Unverified Commit 32edd58f authored by Stan Kladko's avatar Stan Kladko Committed by GitHub

Merge pull request #55 from skalenetwork/SKALE-2201-verify-ecdsa

Skale 2201 verify ecdsa
parents dd91abd9 108d5ada
......@@ -20,7 +20,7 @@ COPY m4 ./m4
COPY scripts ./scripts
COPY secure_enclave ./secure_enclave
COPY spdlog ./spdlog
COPY SGXWALLET_VERSION ./
RUN autoreconf -vif
RUN libtoolize --force
......
......@@ -19,6 +19,7 @@ COPY m4 ./m4
COPY scripts ./scripts
COPY secure_enclave ./secure_enclave
COPY spdlog ./spdlog
COPY SGXWALLET_VERSION ./
RUN autoreconf -vif
RUN libtoolize --force
......
......@@ -126,13 +126,13 @@ 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, char * pub_key_x, char * pub_key_y) {
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( rand_char, 32);
unsigned char *rand_char = (unsigned char *) malloc(32);
sgx_read_rand(rand_char, 32);
mpz_t seed;
mpz_init(seed);
......@@ -155,38 +155,39 @@ void generate_ecdsa_key(int *err_status, char *err_string,
//Public key
point Pkey = point_init();
signature_generate_key(Pkey, skey, curve);
signature_extract_public_key(Pkey, skey, curve);
uint8_t base = 16;
int len = mpz_sizeinbase (Pkey->x, base) + 2;
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);
char *px = mpz_get_str(arr_x, base, Pkey->x);
//snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px);
int n_zeroes = 64 - strlen(arr_x);
for ( int i = 0; i < n_zeroes; i++){
for (int i = 0; i < n_zeroes; i++) {
pub_key_x[i] = '0';
}
strncpy(pub_key_x + n_zeroes, arr_x, 1024 - n_zeroes);
char arr_y[mpz_sizeinbase (Pkey->y, base) + 2];
char* py = mpz_get_str(arr_y, base, Pkey->y);
char arr_y[mpz_sizeinbase(Pkey->y, base) + 2];
char *py = mpz_get_str(arr_y, base, Pkey->y);
n_zeroes = 64 - strlen(arr_y);
for ( int i = 0; i < n_zeroes; i++){
for (int i = 0; i < n_zeroes; i++) {
pub_key_y[i] = '0';
}
strncpy(pub_key_y + n_zeroes, arr_y, 1024 - n_zeroes);
char skey_str[mpz_sizeinbase (skey, ECDSA_SKEY_BASE) + 2];
char* s = mpz_get_str(skey_str, ECDSA_SKEY_BASE, skey);
char skey_str[mpz_sizeinbase(skey, ECDSA_SKEY_BASE) + 2];
char *s = mpz_get_str(skey_str, ECDSA_SKEY_BASE, skey);
snprintf(err_string, BUF_LEN, "skey is %s len %d\n", skey_str, strlen(skey_str));
uint32_t sealedLen = sgx_calc_sealed_data_size(0, ECDSA_SKEY_LEN);
sgx_status_t status = sgx_seal_data(0, NULL, ECDSA_SKEY_LEN, (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");
sgx_status_t status = sgx_seal_data(0, NULL, ECDSA_SKEY_LEN, (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");
*err_status = status;
return;
}
......@@ -200,7 +201,7 @@ void generate_ecdsa_key(int *err_status, char *err_string,
void get_public_ecdsa_key(int *err_status, char *err_string,
uint8_t *encrypted_key, uint32_t dec_len, char * pub_key_x, char * pub_key_y) {
uint8_t *encrypted_key, uint32_t dec_len, char *pub_key_x, char *pub_key_y) {
//uint32_t dec_len = 0;
......@@ -210,10 +211,10 @@ void get_public_ecdsa_key(int *err_status, char *err_string,
char skey[ECDSA_SKEY_LEN];
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_key, NULL, 0, (uint8_t *)skey, &dec_len);
(const sgx_sealed_data_t *) encrypted_key, NULL, 0, (uint8_t *) skey, &dec_len);
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"sgx_unseal_data failed with status %d", status);
snprintf(err_string, BUF_LEN, "sgx_unseal_data failed with status %d", status);
*err_status = status;
return;
}
......@@ -223,8 +224,8 @@ void get_public_ecdsa_key(int *err_status, char *err_string,
mpz_t skey_mpz;
mpz_init(skey_mpz);
// mpz_import(skey_mpz, 32, 1, sizeof(skey[0]), 0, 0, skey);
if (mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE) == -1){
snprintf(err_string, BUF_LEN,"wrong string to init private key");
if (mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE) == -1) {
snprintf(err_string, BUF_LEN, "wrong string to init private key");
*err_status = -10;
mpz_clear(skey_mpz);
return;
......@@ -233,35 +234,35 @@ void get_public_ecdsa_key(int *err_status, char *err_string,
//Public key
point Pkey = point_init();
signature_generate_key(Pkey, skey_mpz, curve);
signature_extract_public_key(Pkey, skey_mpz, curve);
point Pkey_test = point_init();
point_multiplication(Pkey_test, skey_mpz, curve->G, curve);
if (!point_cmp(Pkey, Pkey_test)){
snprintf(err_string, BUF_LEN,"Points are not equal");
if (!point_cmp(Pkey, Pkey_test)) {
snprintf(err_string, BUF_LEN, "Points are not equal");
*err_status = -11;
return;
}
int base = 16;
int len = mpz_sizeinbase (Pkey->x, base) + 2;
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);
char *px = mpz_get_str(arr_x, base, Pkey->x);
//snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px);
int n_zeroes = 64 - strlen(arr_x);
for ( int i = 0; i < n_zeroes; i++){
for (int i = 0; i < n_zeroes; i++) {
pub_key_x[i] = '0';
}
strncpy(pub_key_x + n_zeroes, arr_x, 1024 - n_zeroes);
char arr_y[mpz_sizeinbase (Pkey->y, base) + 2];
char* py = mpz_get_str(arr_y, base, Pkey->y);
char arr_y[mpz_sizeinbase(Pkey->y, base) + 2];
char *py = mpz_get_str(arr_y, base, Pkey->y);
n_zeroes = 64 - strlen(arr_y);
for ( int i = 0; i < n_zeroes; i++){
for (int i = 0; i < n_zeroes; i++) {
pub_key_y[i] = '0';
}
strncpy(pub_key_y + n_zeroes, arr_y, 1024 - n_zeroes);
......@@ -272,87 +273,99 @@ void get_public_ecdsa_key(int *err_status, char *err_string,
}
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, uint8_t* sig_v, int base) {
unsigned char *hash, char *sig_r, char *sig_s, uint8_t *sig_v, int base) {
char* arr_m = NULL;
char* arr_r = NULL;
char* arr_s;
mpz_t skey_mpz;
mpz_init(skey_mpz);
mpz_t msg_mpz;
mpz_init(msg_mpz);
signature sign = signature_init();
domain_parameters curve = domain_parameters_init();
domain_parameters_load_curve(curve, secp256k1);
char skey[ECDSA_SKEY_LEN];
point publicKey = point_init();
char* secretKey = calloc(ECDSA_SKEY_LEN,1);
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_key, NULL, 0, skey, &dec_len);
(const sgx_sealed_data_t *) encrypted_key, NULL, 0, secretKey, &dec_len);
if (status != SGX_SUCCESS) {
*err_status = status;
snprintf(err_string, BUF_LEN,"sgx_unseal_data failed - encrypted_key with status %d", status);
return;
snprintf(err_string, BUF_LEN, "sgx_unseal_data failed - encrypted_key with status %d", status);
goto clean;
}
snprintf(err_string, BUF_LEN,"pr key is %s length %d ", skey, strlen(skey));
mpz_t skey_mpz;
mpz_init(skey_mpz);
if (mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE) == -1){
//snprintf(err_string, BUF_LEN, "pr key is %s length %d ", skey, strlen(skey));
if (mpz_set_str(skey_mpz, secretKey, ECDSA_SKEY_BASE) == -1) {
*err_status = -1;
snprintf(err_string, BUF_LEN ,"invalid secret key");
mpz_clear(skey_mpz);
return;
snprintf(err_string, BUF_LEN, "invalid secret key");
goto clean;
}
/*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);
if (mpz_set_str(msg_mpz, hash, 16) == -1){
if (mpz_set_str(msg_mpz, hash, 16) == -1) {
*err_status = -1;
snprintf(err_string, BUF_LEN ,"invalid message hash");
mpz_clear(msg_mpz);
return;
snprintf(err_string, BUF_LEN, "invalid message hash");
goto clean;
}
//mpz_set_str(msg_mpz,"4b688df40bcedbe641ddb16ff0a1842d9c67ea1c3bf63f3e0471baa664531d1a", 16);
signature sign = signature_init();
signature_sign(sign, msg_mpz, skey_mpz, curve);
signature_sign( sign, msg_mpz, skey_mpz, curve);
signature_extract_public_key(publicKey, skey_mpz, curve);
point Pkey = point_init();
signature_generate_key(Pkey, skey_mpz, curve);
if ( !signature_verify(msg_mpz, sign, Pkey, curve) ){
if (!signature_verify(msg_mpz, sign, publicKey, curve)) {
*err_status = -2;
snprintf(err_string, BUF_LEN,"signature is not verified! ");
return;
snprintf(err_string, BUF_LEN, "signature is not verified");
goto clean;
}
//char arr_x[mpz_sizeinbase (Pkey->x, 16) + 2];
//char* px = mpz_get_str(arr_x, 16, Pkey->x);
//snprintf(err_string, BUF_LEN,"pub key x %s ", arr_x);
char arr_m[mpz_sizeinbase (msg_mpz, 16) + 2];
char* msg = mpz_get_str(arr_m, 16, msg_mpz);
snprintf(err_string, BUF_LEN,"message is %s ", arr_m);
arr_m = calloc(mpz_sizeinbase(msg_mpz, 16) + 2 ,1);
mpz_get_str(arr_m, 16, msg_mpz);
//snprintf(err_string, BUF_LEN, "message is %s ", arr_m);
char arr_r[mpz_sizeinbase (sign->r, base) + 2];
char* r = mpz_get_str(arr_r, base, sign->r);
arr_r = calloc(mpz_sizeinbase(sign->r, base) + 2,1);
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);
arr_s = calloc(mpz_sizeinbase(sign->s, base) + 2, 1);
mpz_get_str(arr_s, base, sign->s);
strncpy(sig_s, arr_s, 1024);
*sig_v = sign->v;
clean:
mpz_clear(skey_mpz);
mpz_clear(msg_mpz);
domain_parameters_clear(curve);
signature_clear(sign);
point_clear(Pkey);
point_clear(publicKey);
free(secretKey);
signature_free(sign);
if (arr_m != NULL) {
free(arr_m);
}
if (arr_r != NULL) {
free(arr_r);
}
if (arr_s != NULL) {
free(arr_s);
}
return;
}
......@@ -376,7 +389,6 @@ void encrypt_key(int *err_status, char *err_string, char *key,
uint32_t sealedLen = sgx_calc_sealed_data_size(0, MAX_KEY_LENGTH);
if (sealedLen > BUF_LEN) {
*err_status = ENCRYPTED_KEY_TOO_LONG;
snprintf(err_string, BUF_LEN, "sealedLen > MAX_ENCRYPTED_KEY_LENGTH");
......@@ -386,8 +398,9 @@ void encrypt_key(int *err_status, char *err_string, char *key,
memset(encrypted_key, 0, BUF_LEN);
sgx_status_t status = sgx_seal_data(0, NULL, MAX_KEY_LENGTH, (uint8_t *) key, sealedLen, (sgx_sealed_data_t *) encrypted_key);
if ( status != SGX_SUCCESS) {
sgx_status_t status = sgx_seal_data(0, NULL, MAX_KEY_LENGTH, (uint8_t *) key, sealedLen,
(sgx_sealed_data_t *) encrypted_key);
if (status != SGX_SUCCESS) {
*err_status = SEAL_KEY_FAILED;
snprintf(err_string, BUF_LEN, "SGX seal data failed with status %d", status);
return;
......@@ -481,9 +494,8 @@ void bls_sign_message(int *err_status, char *err_string, uint8_t *encrypted_key,
char *_hashY, char *signature) {
char key[BUF_LEN];
char* sig = (char*) calloc(BUF_LEN, 1);
char *sig = (char *) calloc(BUF_LEN, 1);
// char sig[2 * BUF_LEN];
init();
......@@ -508,23 +520,25 @@ void bls_sign_message(int *err_status, char *err_string, uint8_t *encrypted_key,
free(sig);
}
void gen_dkg_secret (int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, uint32_t* enc_len, size_t _t){
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[DKG_BUFER_LENGTH]; //= (char*)malloc(DKG_BUFER_LENGTH);
if (gen_dkg_poly(dkg_secret, _t) != 0 ){
*err_status = - 1;
if (gen_dkg_poly(dkg_secret, _t) != 0) {
*err_status = -1;
return;
}
snprintf(err_string, BUF_LEN,"poly is %s ", dkg_secret);
snprintf(err_string, BUF_LEN, "poly is %s ", dkg_secret);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, DKG_BUFER_LENGTH);//sizeof(sgx_sealed_data_t) + sizeof(dkg_secret);
uint32_t sealedLen = sgx_calc_sealed_data_size(0,
DKG_BUFER_LENGTH);//sizeof(sgx_sealed_data_t) + sizeof(dkg_secret);
sgx_status_t status = sgx_seal_data(0, NULL, DKG_BUFER_LENGTH, (uint8_t*)dkg_secret, sealedLen,(sgx_sealed_data_t*)encrypted_dkg_secret);
sgx_status_t status = sgx_seal_data(0, NULL, DKG_BUFER_LENGTH, (uint8_t *) dkg_secret, sealedLen,
(sgx_sealed_data_t *) encrypted_dkg_secret);
if(status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"SGX seal data failed");
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN, "SGX seal data failed");
*err_status = status;
return;
}
......@@ -533,15 +547,16 @@ void gen_dkg_secret (int *err_status, char *err_string, uint8_t *encrypted_dkg_s
//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* dec_len){
void decrypt_dkg_secret(int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, uint8_t *decrypted_dkg_secret,
uint32_t *dec_len) {
//uint32_t dec_size = DKG_BUFER_LENGTH;//sgx_get_encrypt_txt_len( ( sgx_sealed_data_t *)encrypted_dkg_secret);
uint32_t decr_len;
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_dkg_secret, NULL, 0, decrypted_dkg_secret, &decr_len);
(const sgx_sealed_data_t *) encrypted_dkg_secret, NULL, 0, decrypted_dkg_secret, &decr_len);
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"sgx_unseal_data - encrypted_dkg_secret failed with status %d", status);
snprintf(err_string, BUF_LEN, "sgx_unseal_data - encrypted_dkg_secret failed with status %d", status);
*err_status = status;
return;
}
......@@ -549,20 +564,21 @@ void decrypt_dkg_secret (int *err_status, char* err_string, uint8_t* encrypted_d
*dec_len = decr_len;
}
void get_secret_shares(int *err_status, char* err_string, uint8_t* encrypted_dkg_secret, uint32_t* dec_len, char* secret_shares,
unsigned _t, unsigned _n){
void get_secret_shares(int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, uint32_t *dec_len,
char *secret_shares,
unsigned _t, unsigned _n) {
char decrypted_dkg_secret[DKG_BUFER_LENGTH]; //= (char*)malloc(DKG_BUFER_LENGTH);
//char decrypted_dkg_secret[DKG_MAX_SEALED_LEN];
uint32_t decr_len ;
uint32_t decr_len;
//uint32_t* decr_len_test = (char*)malloc(1);
decrypt_dkg_secret(err_status, err_string, encrypted_dkg_secret, (uint8_t*)decrypted_dkg_secret, &decr_len);
decrypt_dkg_secret(err_status, err_string, encrypted_dkg_secret, (uint8_t *) decrypted_dkg_secret, &decr_len);
//sgx_status_t status = sgx_unseal_data(
// (const sgx_sealed_data_t *)encrypted_dkg_secret, NULL, 0, (uint8_t*)decrypted_dkg_secret, &decr_len);
if (*err_status != 0) {
snprintf(err_string, BUF_LEN,"sgx_unseal_data - encrypted_dkg_secret failed with status %d", *err_status);
snprintf(err_string, BUF_LEN, "sgx_unseal_data - encrypted_dkg_secret failed with status %d", *err_status);
return;
}
......@@ -573,44 +589,45 @@ void get_secret_shares(int *err_status, char* err_string, uint8_t* encrypted_dkg
//free(decrypted_dkg_secret);
}
void get_public_shares(int *err_status, char* err_string, uint8_t* encrypted_dkg_secret, uint32_t enc_len, char* public_shares,
unsigned _t, unsigned _n){
void get_public_shares(int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, uint32_t enc_len,
char *public_shares,
unsigned _t, unsigned _n) {
//char decrypted_dkg_secret[DKG_MAX_SEALED_LEN * 2]; //= (char*)malloc(DKG_MAX_SEALED_LEN);
char* decrypted_dkg_secret = (char*)malloc(DKG_MAX_SEALED_LEN);
uint32_t decr_len ;
decrypt_dkg_secret(err_status, err_string, (uint8_t*)encrypted_dkg_secret, decrypted_dkg_secret, &decr_len);
if( *err_status != 0 ){
snprintf(err_string, BUF_LEN,"decrypt_dkg_secret failed with status %d", *err_status);
char *decrypted_dkg_secret = (char *) malloc(DKG_MAX_SEALED_LEN);
uint32_t decr_len;
decrypt_dkg_secret(err_status, err_string, (uint8_t *) encrypted_dkg_secret, decrypted_dkg_secret, &decr_len);
if (*err_status != 0) {
snprintf(err_string, BUF_LEN, "decrypt_dkg_secret failed with status %d", *err_status);
return;
}
//strncpy(err_string, decrypted_dkg_secret, 1024);
// strncpy(err_string, "before calc_public_shares ", 1024);
if ( calc_public_shares(decrypted_dkg_secret, public_shares, _t) != 0 ){
if (calc_public_shares(decrypted_dkg_secret, public_shares, _t) != 0) {
*err_status = -1;
snprintf(err_string, BUF_LEN,"t does not match polynomial in db");
snprintf(err_string, BUF_LEN, "t does not match polynomial in db");
return;
}
free(decrypted_dkg_secret);
}
void set_encrypted_dkg_poly(int *err_status, char *err_string, uint8_t* encrypted_poly){
void set_encrypted_dkg_poly(int *err_status, char *err_string, uint8_t *encrypted_poly) {
memset(Decrypted_dkg_poly, 0, DKG_BUFER_LENGTH);
uint32_t decr_len;
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_poly, NULL, 0, Decrypted_dkg_poly, &decr_len);
(const sgx_sealed_data_t *) encrypted_poly, NULL, 0, Decrypted_dkg_poly, &decr_len);
if (status != SGX_SUCCESS) {
*err_status = -1;
snprintf(err_string, BUF_LEN,"sgx_unseal_data - encrypted_poly failed with status %d", status);
snprintf(err_string, BUF_LEN, "sgx_unseal_data - encrypted_poly failed with status %d", status);
return;
}
}
void get_encr_sshare(int *err_status, char *err_string, 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 ){
void get_encr_sshare(int *err_status, char *err_string, 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) {
char skey[ECDSA_SKEY_LEN];
char pub_key_x[BUF_LEN];
......@@ -623,7 +640,7 @@ void get_encr_sshare(int *err_status, char *err_string, uint8_t *encrypted_skey,
uint32_t enc_len;
generate_ecdsa_key(err_status, err_string, encrypted_skey, &enc_len, pub_key_x, pub_key_y);
if ( *err_status != 0){
if (*err_status != 0) {
return;
}
// snprintf(err_string, BUF_LEN,"pub_key_x is %s", pub_key_x);
......@@ -631,41 +648,41 @@ void get_encr_sshare(int *err_status, char *err_string, uint8_t *encrypted_skey,
*dec_len = enc_len;
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_skey, NULL, 0, (uint8_t *)skey, &enc_len);
(const sgx_sealed_data_t *) encrypted_skey, NULL, 0, (uint8_t *) skey, &enc_len);
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"sgx_unseal_data failed - encrypted_skey with status %d", status);
snprintf(err_string, BUF_LEN, "sgx_unseal_data failed - encrypted_skey with status %d", status);
*err_status = status;
return;
}
snprintf(err_string, BUF_LEN,"unsealed random skey is %s\n", skey);
snprintf(err_string, BUF_LEN, "unsealed random skey is %s\n", skey);
char * common_key[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
char *common_key[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
gen_session_key(skey, pub_keyB, common_key);
//snprintf(err_string + 81, BUF_LEN,"pub_key_B is %s length is %d", pub_keyB, strlen(pub_keyB));
//snprintf(err_string + 88, BUF_LEN - 88,"\ncommon key is %s", common_key);
char* s_share[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
char *s_share[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
//char s_share[65];
if (calc_secret_share(Decrypted_dkg_poly, s_share, _t, _n, ind) != 0){
if (calc_secret_share(Decrypted_dkg_poly, s_share, _t, _n, ind) != 0) {
*err_status = -1;
snprintf(err_string, BUF_LEN,"\nt does not match poly degree\n");
snprintf(err_string, BUF_LEN, "\nt does not match poly degree\n");
return;
}
snprintf(err_string + 88, BUF_LEN,"\nsecret share is %s", s_share);
snprintf(err_string + 88, BUF_LEN, "\nsecret share is %s", s_share);
if (calc_secret_shareG2(s_share, s_shareG2) != 0){
if (calc_secret_shareG2(s_share, s_shareG2) != 0) {
*err_status = -1;
snprintf(err_string, BUF_LEN,"invalid decr secret share\n");
snprintf(err_string, BUF_LEN, "invalid decr secret share\n");
return;
}
char* cypher[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
char *cypher[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
xor_encrypt(common_key, s_share, cypher);
if (cypher == NULL){
if (cypher == NULL) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
snprintf(err_string, BUF_LEN, "invalid common_key");
return;
}
//snprintf(err_string, BUF_LEN ,"cypher is %s length is %d", cypher, strlen(cypher));
......@@ -685,8 +702,9 @@ void get_encr_sshare(int *err_status, char *err_string, uint8_t *encrypted_skey,
//free(cypher);
}
void complaint_response(int *err_status, char *err_string, uint8_t *encrypted_DHkey, uint8_t *encrypted_dkg_secret, uint32_t* dec_len,
char* DH_key, char* s_shareG2, uint8_t _t, uint8_t _n, uint8_t ind1){
void complaint_response(int *err_status, char *err_string, uint8_t *encrypted_DHkey, uint8_t *encrypted_dkg_secret,
uint32_t *dec_len,
char *DH_key, char *s_shareG2, uint8_t _t, uint8_t _n, uint8_t ind1) {
uint32_t enc_len;
......@@ -699,9 +717,9 @@ void complaint_response(int *err_status, char *err_string, uint8_t *encrypted_DH
char decrypted_dkg_secret[DKG_BUFER_LENGTH]; //= (char*)malloc(DKG_BUFER_LENGTH);
uint32_t decr_len;
decrypt_dkg_secret(err_status, err_string, encrypted_dkg_secret, (uint8_t*)decrypted_dkg_secret, &decr_len);
decrypt_dkg_secret(err_status, err_string, encrypted_dkg_secret, (uint8_t *) decrypted_dkg_secret, &decr_len);
if (*err_status != 0) {
snprintf(err_string, BUF_LEN,"sgx_unseal_data - encrypted_dkg_secret failed with status %d", *err_status);
snprintf(err_string, BUF_LEN, "sgx_unseal_data - encrypted_dkg_secret failed with status %d", *err_status);
return;
}
......@@ -714,16 +732,16 @@ void complaint_response(int *err_status, char *err_string, uint8_t *encrypted_DH
// free(decrypted_dkg_secret);
}
void dkg_verification(int *err_status, char* err_string, const char * public_shares, const char* s_share,
uint8_t* encrypted_key, uint64_t key_len, unsigned _t, int _ind, int * result){
void dkg_verification(int *err_status, char *err_string, const char *public_shares, const char *s_share,
uint8_t *encrypted_key, uint64_t key_len, unsigned _t, int _ind, int *result) {
//uint32_t dec_len = 625;
char skey[ECDSA_SKEY_LEN];
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_key, NULL, 0, (uint8_t*)skey, &key_len);
(const sgx_sealed_data_t *) encrypted_key, NULL, 0, (uint8_t *) skey, &key_len);
if (status != SGX_SUCCESS) {
*err_status = status;
snprintf(err_string, BUF_LEN,"sgx_unseal_key failed with status %d", status);
snprintf(err_string, BUF_LEN, "sgx_unseal_key failed with status %d", status);
return;
}
......@@ -735,16 +753,16 @@ void dkg_verification(int *err_status, char* err_string, const char * public_sha
char decr_sshare[ECDSA_SKEY_LEN];
session_key_recover(skey, s_share, common_key);
common_key[ECDSA_SKEY_LEN - 1] = 0;
if (common_key == NULL){
if (common_key == NULL) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
snprintf(err_string, BUF_LEN, "invalid common_key");
return;
}
xor_decrypt(common_key, encr_sshare, decr_sshare);
if (decr_sshare == NULL){
if (decr_sshare == NULL) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
snprintf(err_string, BUF_LEN, "invalid common_key");
return;
}
......@@ -759,32 +777,32 @@ void dkg_verification(int *err_status, char* err_string, const char * public_sha
mpz_t s;
mpz_init(s);
if (mpz_set_str(s, decr_sshare, 16) == -1){
if (mpz_set_str(s, decr_sshare, 16) == -1) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid decr secret share");
snprintf(err_string, BUF_LEN, "invalid decr secret share");
mpz_clear(s);
return;
}
*result = Verification(public_shares, s, _t, _ind);
snprintf(err_string, BUF_LEN,"common_key in verification is %s", common_key);
snprintf(err_string, BUF_LEN, "common_key in verification is %s", common_key);
}
void create_bls_key(int *err_status, char* err_string, const char* s_shares,
uint8_t* encrypted_key, uint64_t key_len, uint8_t * encr_bls_key, uint32_t *enc_bls_key_len){
void create_bls_key(int *err_status, char *err_string, const char *s_shares,
uint8_t *encrypted_key, uint64_t key_len, uint8_t *encr_bls_key, uint32_t *enc_bls_key_len) {
char skey[ECDSA_SKEY_LEN];
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_key, NULL, 0, (uint8_t*)skey, &key_len);
(const sgx_sealed_data_t *) encrypted_key, NULL, 0, (uint8_t *) skey, &key_len);
if (status != SGX_SUCCESS) {
*err_status = 1;
snprintf(err_string, BUF_LEN,"sgx_unseal_key failed with status %d", status);
snprintf(err_string, BUF_LEN, "sgx_unseal_key failed with status %d", status);
return;
}
int num_shares = strlen(s_shares)/192;
int num_shares = strlen(s_shares) / 192;
mpz_t sum;
mpz_init(sum);
......@@ -794,7 +812,7 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
//snprintf(err_string, BUF_LEN,"comon0 is %s len is %d\n", common_key, strlen(common_key));
for ( int i = 0; i < num_shares; i++) {
for (int i = 0; i < num_shares; i++) {
char encr_sshare[65];
strncpy(encr_sshare, s_shares + 192 * i, 64);
encr_sshare[64] = 0;
......@@ -807,9 +825,9 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
session_key_recover(skey, s_share, common_key);
common_key[64] = 0;
if (common_key == NULL){
if (common_key == NULL) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
snprintf(err_string, BUF_LEN, "invalid common_key");
mpz_clear(sum);
return;
}
......@@ -820,9 +838,9 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
char decr_sshare[65];
xor_decrypt(common_key, encr_sshare, decr_sshare);
if (decr_sshare == NULL){
if (decr_sshare == NULL) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
snprintf(err_string, BUF_LEN, "invalid common_key");
mpz_clear(sum);
return;
}
......@@ -834,9 +852,9 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
mpz_t decr_secret_share;
mpz_init(decr_secret_share);
if (mpz_set_str(decr_secret_share, decr_sshare, 16) == -1){
if (mpz_set_str(decr_secret_share, decr_sshare, 16) == -1) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid decrypted secret share");
snprintf(err_string, BUF_LEN, "invalid decrypted secret share");
mpz_clear(decr_secret_share);
return;
}
......@@ -856,14 +874,15 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
char key_share[mpz_sizeinbase(bls_key, 16) + 2];
char *key = mpz_get_str(key_share, 16, bls_key);
snprintf(err_string, BUF_LEN," bls private key is %s", key_share);
snprintf(err_string, BUF_LEN, " bls private key is %s", key_share);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, ECDSA_SKEY_LEN);
status = sgx_seal_data(0, NULL, ECDSA_SKEY_LEN, (uint8_t *)key_share, sealedLen,(sgx_sealed_data_t*)encr_bls_key);
if( status != SGX_SUCCESS) {
*err_status= -1;
snprintf(err_string, BUF_LEN,"seal bls private key failed with status %d ", status);
status = sgx_seal_data(0, NULL, ECDSA_SKEY_LEN, (uint8_t *) key_share, sealedLen,
(sgx_sealed_data_t *) encr_bls_key);
if (status != SGX_SUCCESS) {
*err_status = -1;
snprintf(err_string, BUF_LEN, "seal bls private key failed with status %d ", status);
mpz_clear(bls_key);
mpz_clear(sum);
mpz_clear(q);
......@@ -885,29 +904,29 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
mpz_clear(q);
}
void get_bls_pub_key(int *err_status, char* err_string, uint8_t* encrypted_key, uint64_t key_len, char* bls_pub_key){
void get_bls_pub_key(int *err_status, char *err_string, uint8_t *encrypted_key, uint64_t key_len, char *bls_pub_key) {
char skey_hex[ECDSA_SKEY_LEN];
uint32_t len = key_len;
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_key, NULL, 0, (uint8_t *)skey_hex, &len);
(const sgx_sealed_data_t *) encrypted_key, NULL, 0, (uint8_t *) skey_hex, &len);
if (status != SGX_SUCCESS) {
*err_status = 1;
snprintf(err_string, BUF_LEN,"sgx_unseal_data failed with status %d", status);
snprintf(err_string, BUF_LEN, "sgx_unseal_data failed with status %d", status);
return;
}
if (calc_bls_public_key(skey_hex, bls_pub_key) != 0){
if (calc_bls_public_key(skey_hex, bls_pub_key) != 0) {
*err_status = -1;
snprintf(err_string, BUF_LEN,"could not calculate bls public key");
snprintf(err_string, BUF_LEN, "could not calculate bls public key");
return;
}
}
void generate_SEK(int *err_status, char *err_string,
uint8_t *encrypted_SEK, uint32_t *enc_len, char* SEK_hex){
uint8_t *encrypted_SEK, uint32_t *enc_len, char *SEK_hex) {
uint8_t SEK_raw[SGX_AESGCM_KEY_SIZE];
//unsigned char* rand_char = (unsigned char*)malloc(16);
sgx_read_rand(SEK_raw, SGX_AESGCM_KEY_SIZE);
......@@ -918,12 +937,13 @@ void generate_SEK(int *err_status, char *err_string,
uint32_t sealedLen = sgx_calc_sealed_data_size(0, hex_aes_key_length + 1);
for ( uint8_t i = 0; i < 16; i++){
for (uint8_t i = 0; i < 16; i++) {
AES_key[i] = SEK_raw[i];
}
sgx_status_t status = sgx_seal_data(0, NULL, hex_aes_key_length + 1, SEK_hex, sealedLen,(sgx_sealed_data_t*)encrypted_SEK);
if( status != SGX_SUCCESS) {
sgx_status_t status = sgx_seal_data(0, NULL, hex_aes_key_length + 1, SEK_hex, sealedLen,
(sgx_sealed_data_t *) encrypted_SEK);
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN, "seal SEK failed");
*err_status = status;
return;
......@@ -935,7 +955,7 @@ void generate_SEK(int *err_status, char *err_string,
//free(rand_char);
}
void set_SEK(int *err_status, char *err_string, uint8_t *encrypted_SEK, uint64_t encr_len){
void set_SEK(int *err_status, char *err_string, uint8_t *encrypted_SEK, uint64_t encr_len) {
//memset(AES_key, 0, SGX_AESGCM_KEY_SIZE);
......@@ -943,28 +963,29 @@ void set_SEK(int *err_status, char *err_string, uint8_t *encrypted_SEK, uint64_t
memset(aes_key_hex, 0, SGX_AESGCM_KEY_SIZE * 2);
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, aes_key_hex, &encr_len);
if (status != SGX_SUCCESS) {
*err_status = status;
snprintf(err_string, BUF_LEN,"sgx unseal SEK failed with status %d", status);
snprintf(err_string, BUF_LEN, "sgx unseal SEK failed with status %d", status);
return;
}
uint64_t len;
hex2carray(aes_key_hex, &len, (uint8_t* )AES_key);
hex2carray(aes_key_hex, &len, (uint8_t *) AES_key);
}
void set_SEK_backup(int *err_status, char *err_string,
uint8_t *encrypted_SEK, uint32_t *enc_len, const char* SEK_hex){
uint8_t *encrypted_SEK, uint32_t *enc_len, const char *SEK_hex) {
uint64_t len;
hex2carray(SEK_hex, &len, (uint8_t* )AES_key);
hex2carray(SEK_hex, &len, (uint8_t *) AES_key);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, strlen(SEK_hex) + 1);
sgx_status_t status = sgx_seal_data(0, NULL, strlen(SEK_hex) + 1, SEK_hex, sealedLen,(sgx_sealed_data_t*)encrypted_SEK);
if( status != SGX_SUCCESS) {
sgx_status_t status = sgx_seal_data(0, NULL, strlen(SEK_hex) + 1, SEK_hex, sealedLen,
(sgx_sealed_data_t *) encrypted_SEK);
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN, "seal SEK failed with status %d", status);
*err_status = status;
return;
......@@ -976,13 +997,13 @@ void set_SEK_backup(int *err_status, char *err_string,
}
void generate_ecdsa_key_aes(int *err_status, char *err_string,
uint8_t *encrypted_key, uint32_t *enc_len, char * pub_key_x, char * pub_key_y) {
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( rand_char, 32);
unsigned char *rand_char = (unsigned char *) malloc(32);
sgx_read_rand(rand_char, 32);
mpz_t seed;
mpz_init(seed);
......@@ -998,37 +1019,37 @@ void generate_ecdsa_key_aes(int *err_status, char *err_string,
//Public key
point Pkey = point_init();
signature_generate_key(Pkey, skey, curve);
signature_extract_public_key(Pkey, skey, curve);
uint8_t base = 16;
int len = mpz_sizeinbase (Pkey->x, base) + 2;
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);
char *px = mpz_get_str(arr_x, base, Pkey->x);
//snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px);
int n_zeroes = 64 - strlen(arr_x);
for ( int i = 0; i < n_zeroes; i++){
for (int i = 0; i < n_zeroes; i++) {
pub_key_x[i] = '0';
}
strncpy(pub_key_x + n_zeroes, arr_x, 1024 - n_zeroes);
char arr_y[mpz_sizeinbase (Pkey->y, base) + 2];
char* py = mpz_get_str(arr_y, base, Pkey->y);
char arr_y[mpz_sizeinbase(Pkey->y, base) + 2];
char *py = mpz_get_str(arr_y, base, Pkey->y);
n_zeroes = 64 - strlen(arr_y);
for ( int i = 0; i < n_zeroes; i++){
for (int i = 0; i < n_zeroes; i++) {
pub_key_y[i] = '0';
}
strncpy(pub_key_y + n_zeroes, arr_y, 1024 - n_zeroes);
char skey_str[mpz_sizeinbase (skey, ECDSA_SKEY_BASE) + 2];
char* s = mpz_get_str(skey_str, ECDSA_SKEY_BASE, skey);
char skey_str[mpz_sizeinbase(skey, ECDSA_SKEY_BASE) + 2];
char *s = mpz_get_str(skey_str, ECDSA_SKEY_BASE, skey);
snprintf(err_string, BUF_LEN, "skey is %s len %d\n", skey_str, strlen(skey_str));
int stat = AES_encrypt(skey_str, encrypted_key);
if( stat != 0) {
snprintf(err_string, BUF_LEN,"ecdsa private key encryption failed");
if (stat != 0) {
snprintf(err_string, BUF_LEN, "ecdsa private key encryption failed");
*err_status = stat;
return;
}
......@@ -1036,8 +1057,8 @@ void generate_ecdsa_key_aes(int *err_status, char *err_string,
*enc_len = strlen(skey_str) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;
stat = AES_decrypt(encrypted_key, *enc_len, skey_str);
if( stat != 0) {
snprintf(err_string + 19 + strlen(skey_str), BUF_LEN,"ecdsa private key decr failed with status %d", stat);
if (stat != 0) {
snprintf(err_string + 19 + strlen(skey_str), BUF_LEN, "ecdsa private key decr failed with status %d", stat);
//*err_status = stat;
return;
}
......@@ -1048,7 +1069,7 @@ void generate_ecdsa_key_aes(int *err_status, char *err_string,
}
void get_public_ecdsa_key_aes(int *err_status, char *err_string,
uint8_t *encrypted_key, uint32_t enc_len, char * pub_key_x, char * pub_key_y) {
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);
......@@ -1058,7 +1079,7 @@ void get_public_ecdsa_key_aes(int *err_status, char *err_string,
int status = AES_decrypt(encrypted_key, enc_len, skey);
if (status != 0) {
snprintf(err_string, BUF_LEN,"AES_decrypt failed with status %d", status);
snprintf(err_string, BUF_LEN, "AES_decrypt failed with status %d", status);
*err_status = status;
return;
}
......@@ -1070,8 +1091,8 @@ void get_public_ecdsa_key_aes(int *err_status, char *err_string,
mpz_t skey_mpz;
mpz_init(skey_mpz);
// mpz_import(skey_mpz, 32, 1, sizeof(skey[0]), 0, 0, skey);
if (mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE) == -1){
snprintf(err_string, BUF_LEN,"wrong string to init private key - %s", skey);
if (mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE) == -1) {
snprintf(err_string, BUF_LEN, "wrong string to init private key - %s", skey);
*err_status = -10;
mpz_clear(skey_mpz);
return;
......@@ -1080,35 +1101,35 @@ void get_public_ecdsa_key_aes(int *err_status, char *err_string,
//Public key
point Pkey = point_init();
signature_generate_key(Pkey, skey_mpz, curve);
signature_extract_public_key(Pkey, skey_mpz, curve);
point Pkey_test = point_init();
point_multiplication(Pkey_test, skey_mpz, curve->G, curve);
if (!point_cmp(Pkey, Pkey_test)){
snprintf(err_string, BUF_LEN,"Points are not equal");
if (!point_cmp(Pkey, Pkey_test)) {
snprintf(err_string, BUF_LEN, "Points are not equal");
*err_status = -11;
return;
}
int base = 16;
int len = mpz_sizeinbase (Pkey->x, base) + 2;
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);
char *px = mpz_get_str(arr_x, base, Pkey->x);
//snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px);
int n_zeroes = 64 - strlen(arr_x);
for ( int i = 0; i < n_zeroes; i++){
for (int i = 0; i < n_zeroes; i++) {
pub_key_x[i] = '0';
}
strncpy(pub_key_x + n_zeroes, arr_x, 1024 - n_zeroes);
char arr_y[mpz_sizeinbase (Pkey->y, base) + 2];
char* py = mpz_get_str(arr_y, base, Pkey->y);
char arr_y[mpz_sizeinbase(Pkey->y, base) + 2];
char *py = mpz_get_str(arr_y, base, Pkey->y);
n_zeroes = 64 - strlen(arr_y);
for ( int i = 0; i < n_zeroes; i++){
for (int i = 0; i < n_zeroes; i++) {
pub_key_y[i] = '0';
}
strncpy(pub_key_y + n_zeroes, arr_y, 1024 - n_zeroes);
......@@ -1119,7 +1140,7 @@ void get_public_ecdsa_key_aes(int *err_status, char *err_string,
}
void ecdsa_sign_aes(int *err_status, char *err_string, uint8_t *encrypted_key, uint32_t enc_len,
unsigned char* hash, char * sig_r, char * sig_s, uint8_t* sig_v, int base) {
unsigned char *hash, char *sig_r, char *sig_s, uint8_t *sig_v, int base) {
domain_parameters curve = domain_parameters_init();
domain_parameters_load_curve(curve, secp256k1);
......@@ -1130,18 +1151,18 @@ void ecdsa_sign_aes(int *err_status, char *err_string, uint8_t *encrypted_key, u
if (status != 0) {
*err_status = status;
snprintf(err_string, BUF_LEN,"aes decrypt failed with status %d", status);
snprintf(err_string, BUF_LEN, "aes decrypt failed with status %d", status);
return;
}
skey[enc_len - SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE - 1] = '\0';
snprintf(err_string, BUF_LEN,"pr key is %s length %d ", skey, strlen(skey));
snprintf(err_string, BUF_LEN, "pr key is %s length %d ", skey, strlen(skey));
mpz_t skey_mpz;
mpz_init(skey_mpz);
if (mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE) == -1){
if (mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE) == -1) {
*err_status = -1;
snprintf(err_string, BUF_LEN ,"invalid secret key");
snprintf(err_string, BUF_LEN, "invalid secret key");
mpz_clear(skey_mpz);
return;
}
......@@ -1149,24 +1170,24 @@ void ecdsa_sign_aes(int *err_status, char *err_string, uint8_t *encrypted_key, u
mpz_t msg_mpz;
mpz_init(msg_mpz);
if (mpz_set_str(msg_mpz, hash, 16) == -1){
if (mpz_set_str(msg_mpz, hash, 16) == -1) {
*err_status = -1;
snprintf(err_string, BUF_LEN ,"invalid message hash");
snprintf(err_string, BUF_LEN, "invalid message hash");
mpz_clear(msg_mpz);
return;
}
signature sign = signature_init();
signature_sign( sign, msg_mpz, skey_mpz, curve);
signature_sign(sign, msg_mpz, skey_mpz, curve);
point Pkey = point_init();
signature_generate_key(Pkey, skey_mpz, curve);
signature_extract_public_key(Pkey, skey_mpz, curve);
if ( !signature_verify(msg_mpz, sign, Pkey, curve) ){
if (!signature_verify(msg_mpz, sign, Pkey, curve)) {
*err_status = -2;
snprintf(err_string, BUF_LEN,"signature is not verified! ");
snprintf(err_string, BUF_LEN, "signature is not verified! ");
return;
}
......@@ -1174,16 +1195,16 @@ void ecdsa_sign_aes(int *err_status, char *err_string, uint8_t *encrypted_key, u
//char* px = mpz_get_str(arr_x, 16, Pkey->x);
//snprintf(err_string, BUF_LEN,"pub key x %s ", arr_x);
char arr_m[mpz_sizeinbase (msg_mpz, 16) + 2];
char* msg = mpz_get_str(arr_m, 16, msg_mpz);
snprintf(err_string, BUF_LEN,"message is %s ", arr_m);
char arr_m[mpz_sizeinbase(msg_mpz, 16) + 2];
char *msg = mpz_get_str(arr_m, 16, msg_mpz);
snprintf(err_string, BUF_LEN, "message is %s ", arr_m);
char arr_r[mpz_sizeinbase (sign->r, base) + 2];
char* r = mpz_get_str(arr_r, base, sign->r);
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);
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 = sign->v;
......@@ -1191,7 +1212,7 @@ void ecdsa_sign_aes(int *err_status, char *err_string, uint8_t *encrypted_key, u
mpz_clear(skey_mpz);
mpz_clear(msg_mpz);
domain_parameters_clear(curve);
signature_clear(sign);
signature_free(sign);
point_clear(Pkey);
}
......@@ -1215,7 +1236,7 @@ void encrypt_key_aes(int *err_status, char *err_string, const char *key,
memset(encrypted_key, 0, BUF_LEN);
int stat = AES_encrypt(key, encrypted_key);
if ( stat != 0) {
if (stat != 0) {
*err_status = stat;
snprintf(err_string, BUF_LEN, "AES encrypt failed with status %d", stat);
return;
......@@ -1308,7 +1329,7 @@ void bls_sign_message_aes(int *err_status, char *err_string, uint8_t *encrypted_
int stat = AES_decrypt(encrypted_key, enc_len, key);
if ( stat != 0) {
if (stat != 0) {
*err_status = stat;
strncpy(signature, err_string, BUF_LEN);
return;
......@@ -1325,22 +1346,23 @@ void bls_sign_message_aes(int *err_status, char *err_string, uint8_t *encrypted_
//free(sig);
}
void gen_dkg_secret_aes (int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, uint32_t* enc_len, size_t _t){
void
gen_dkg_secret_aes(int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, uint32_t *enc_len, size_t _t) {
char dkg_secret[DKG_BUFER_LENGTH];// = (char*)calloc(DKG_BUFER_LENGTH, 1);
memset(dkg_secret, 0, DKG_BUFER_LENGTH);
if (gen_dkg_poly(dkg_secret, _t) != 0 ){
*err_status = - 1;
if (gen_dkg_poly(dkg_secret, _t) != 0) {
*err_status = -1;
return;
}
snprintf(err_string, BUF_LEN,"poly is %s ", dkg_secret);
snprintf(err_string, BUF_LEN, "poly is %s ", dkg_secret);
int status = AES_encrypt(dkg_secret, encrypted_dkg_secret);
if(status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"SGX AES encrypt DKG poly failed");
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN, "SGX AES encrypt DKG poly failed");
*err_status = status;
return;
}
......@@ -1352,46 +1374,49 @@ void gen_dkg_secret_aes (int *err_status, char *err_string, uint8_t *encrypted_d
memset(decr_dkg_secret, 0, DKG_BUFER_LENGTH);
status = AES_decrypt(encrypted_dkg_secret, *enc_len, decr_dkg_secret);
if(status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"aes decrypt dkg poly failed");
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN, "aes decrypt dkg poly failed");
*err_status = status;
return;
}
if ( strcmp(dkg_secret, decr_dkg_secret) != 0){
snprintf(err_string, BUF_LEN,"poly is %s ", dkg_secret);
snprintf(err_string + strlen(dkg_secret) + 8, BUF_LEN - strlen(dkg_secret) - 8,"encrypted poly is not equal to decrypted poly");
if (strcmp(dkg_secret, decr_dkg_secret) != 0) {
snprintf(err_string, BUF_LEN, "poly is %s ", dkg_secret);
snprintf(err_string + strlen(dkg_secret) + 8, BUF_LEN - strlen(dkg_secret) - 8,
"encrypted poly is not equal to decrypted poly");
*err_status = -333;
}
// free(dkg_secret);
}
void decrypt_dkg_secret_aes (int *err_status, char* err_string, uint8_t* encrypted_dkg_secret, uint8_t* decrypted_dkg_secret, uint32_t* dec_len){
void
decrypt_dkg_secret_aes(int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, uint8_t *decrypted_dkg_secret,
uint32_t *dec_len) {
int status = AES_decrypt(encrypted_dkg_secret, dec_len, decrypted_dkg_secret);
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"aes decrypt data - encrypted_dkg_secret failed with status %d", status);
snprintf(err_string, BUF_LEN, "aes decrypt data - encrypted_dkg_secret failed with status %d", status);
*err_status = status;
return;
}
//*dec_len = decr_len;
}
void set_encrypted_dkg_poly_aes(int *err_status, char *err_string, uint8_t* encrypted_poly, uint64_t* enc_len){
void set_encrypted_dkg_poly_aes(int *err_status, char *err_string, uint8_t *encrypted_poly, uint64_t *enc_len) {
memset(Decrypted_dkg_poly, 0, DKG_BUFER_LENGTH);
int status = AES_decrypt(encrypted_poly, *enc_len, Decrypted_dkg_poly);
if (status != SGX_SUCCESS) {
*err_status = -1;
snprintf(err_string, BUF_LEN,"sgx_unseal_data - encrypted_poly failed with status %d", status);
snprintf(err_string, BUF_LEN, "sgx_unseal_data - encrypted_poly failed with status %d", status);
return;
}
}
void get_encr_sshare_aes(int *err_status, char *err_string, 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 ){
void get_encr_sshare_aes(int *err_status, char *err_string, 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) {
char skey[ECDSA_SKEY_LEN];
memset(skey, 0, BUF_LEN);
......@@ -1405,7 +1430,7 @@ void get_encr_sshare_aes(int *err_status, char *err_string, uint8_t *encrypted_s
uint32_t enc_len;
generate_ecdsa_key_aes(err_status, err_string, encrypted_skey, &enc_len, pub_key_x, pub_key_y);
if ( *err_status != 0){
if (*err_status != 0) {
return;
}
// snprintf(err_string, BUF_LEN,"pub_key_x is %s", pub_key_x);
......@@ -1414,41 +1439,41 @@ void get_encr_sshare_aes(int *err_status, char *err_string, uint8_t *encrypted_s
skey[ECDSA_SKEY_LEN - 1] = 0;
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"AES_decrypt failed (in get_encr_sshare_aes) with status %d", status);
snprintf(err_string, BUF_LEN, "AES_decrypt failed (in get_encr_sshare_aes) with status %d", status);
*err_status = status;
return;
}
snprintf(err_string, BUF_LEN,"unsealed random skey is %s\n", skey);
snprintf(err_string, BUF_LEN, "unsealed random skey is %s\n", skey);
*dec_len = enc_len;// + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;
char * common_key[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
char *common_key[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
gen_session_key(skey, pub_keyB, common_key);
//snprintf(err_string + 81, BUF_LEN,"pub_key_B is %s length is %d", pub_keyB, strlen(pub_keyB));
//snprintf(err_string + 88, BUF_LEN - 88,"\ncommon key is %s", common_key);
char* s_share[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
char *s_share[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
//char s_share[65];
if (calc_secret_share(Decrypted_dkg_poly, s_share, _t, _n, ind) != 0){
if (calc_secret_share(Decrypted_dkg_poly, s_share, _t, _n, ind) != 0) {
*err_status = -1;
// snprintf(err_string, BUF_LEN,"t does not match poly degree");
snprintf(err_string, BUF_LEN, Decrypted_dkg_poly);
return;
}
snprintf(err_string + 88, BUF_LEN,"\nsecret share is %s", s_share);
snprintf(err_string + 88, BUF_LEN, "\nsecret share is %s", s_share);
if (calc_secret_shareG2(s_share, s_shareG2) != 0){
if (calc_secret_shareG2(s_share, s_shareG2) != 0) {
*err_status = -1;
snprintf(err_string, BUF_LEN,"invalid decr secret share");
snprintf(err_string, BUF_LEN, "invalid decr secret share");
return;
}
char* cypher[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
char *cypher[ECDSA_SKEY_LEN]; //= (char *)malloc(65);
xor_encrypt(common_key, s_share, cypher);
if (cypher == NULL){
if (cypher == NULL) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
snprintf(err_string, BUF_LEN, "invalid common_key");
return;
}
//snprintf(err_string, BUF_LEN ,"cypher is %s length is %d", cypher, strlen(cypher));
......@@ -1469,10 +1494,11 @@ void get_encr_sshare_aes(int *err_status, char *err_string, uint8_t *encrypted_s
}
void get_public_shares_aes(int *err_status, char* err_string, uint8_t* encrypted_dkg_secret, uint32_t enc_len, char* public_shares,
unsigned _t, unsigned _n){
void get_public_shares_aes(int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, uint32_t enc_len,
char *public_shares,
unsigned _t, unsigned _n) {
char* decrypted_dkg_secret = (char*)calloc(DKG_MAX_SEALED_LEN, 1);
char *decrypted_dkg_secret = (char *) calloc(DKG_MAX_SEALED_LEN, 1);
memset(decrypted_dkg_secret, 0, DKG_MAX_SEALED_LEN);
//char decrypted_dkg_secret[ DKG_MAX_SEALED_LEN];
......@@ -1480,24 +1506,24 @@ void get_public_shares_aes(int *err_status, char* err_string, uint8_t* encrypted
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"aes decrypt data - encrypted_dkg_secret failed with status %d", status);
snprintf(err_string, BUF_LEN, "aes decrypt data - encrypted_dkg_secret failed with status %d", status);
*err_status = status;
return;
}
//strncpy(err_string, decrypted_dkg_secret, 1024);
// strncpy(err_string, "before calc_public_shares ", 1024);
if ( calc_public_shares(decrypted_dkg_secret, public_shares, _t) != 0 ){
if (calc_public_shares(decrypted_dkg_secret, public_shares, _t) != 0) {
*err_status = -1;
snprintf(err_string, BUF_LEN,"t does not match polynomial in db");
snprintf(err_string, BUF_LEN, "t does not match polynomial in db");
return;
}
//free(decrypted_dkg_secret);
}
void dkg_verification_aes(int *err_status, char* err_string, const char * public_shares, const char* s_share,
uint8_t* encrypted_key, uint64_t enc_len, unsigned _t, int _ind, int * result){
void dkg_verification_aes(int *err_status, char *err_string, const char *public_shares, const char *s_share,
uint8_t *encrypted_key, uint64_t enc_len, unsigned _t, int _ind, int *result) {
//uint32_t dec_len = 625;
char skey[ECDSA_SKEY_LEN];
......@@ -1506,14 +1532,14 @@ void dkg_verification_aes(int *err_status, char* err_string, const char * public
//skey[ECDSA_SKEY_LEN - 1] = 0;
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"AES_decrypt failed (in dkg_verification_aes) with status %d", status);
snprintf(err_string, BUF_LEN, "AES_decrypt failed (in dkg_verification_aes) with status %d", status);
*err_status = status;
return;
}
char encr_sshare[ECDSA_SKEY_LEN];
memset(encr_sshare, 0, ECDSA_SKEY_LEN);
strncpy(encr_sshare, s_share, ECDSA_SKEY_LEN - 1 );
strncpy(encr_sshare, s_share, ECDSA_SKEY_LEN - 1);
//encr_sshare[ECDSA_SKEY_LEN - 1] = 0;
char common_key[ECDSA_SKEY_LEN];
......@@ -1521,18 +1547,18 @@ void dkg_verification_aes(int *err_status, char* err_string, const char * public
session_key_recover(skey, s_share, common_key);
//common_key[ECDSA_SKEY_LEN - 1] = 0;
if (common_key == NULL || strlen(common_key) == 0 ){
if (common_key == NULL || strlen(common_key) == 0) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
snprintf(err_string, BUF_LEN, "invalid common_key");
return;
}
char decr_sshare[ECDSA_SKEY_LEN];
memset(decr_sshare, 0, ECDSA_SKEY_LEN);
xor_decrypt(common_key, encr_sshare, decr_sshare);
if (decr_sshare == NULL){
if (decr_sshare == NULL) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
snprintf(err_string, BUF_LEN, "invalid common_key");
return;
}
//decr_sshare[ECDSA_SKEY_LEN - 1] = 0;
......@@ -1547,32 +1573,32 @@ void dkg_verification_aes(int *err_status, char* err_string, const char * public
mpz_t s;
mpz_init(s);
if (mpz_set_str(s, decr_sshare, 16) == -1){
if (mpz_set_str(s, decr_sshare, 16) == -1) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid decr secret share");
snprintf(err_string, BUF_LEN, "invalid decr secret share");
mpz_clear(s);
return;
}
*result = Verification(public_shares, s, _t, _ind);
snprintf(err_string, BUF_LEN,"secret share dec %s", public_shares);
snprintf(err_string, BUF_LEN, "secret share dec %s", public_shares);
}
void create_bls_key_aes(int *err_status, char* err_string, const char* s_shares,
uint8_t* encrypted_key, uint64_t key_len, uint8_t * encr_bls_key, uint32_t *enc_bls_key_len){
void create_bls_key_aes(int *err_status, char *err_string, const char *s_shares,
uint8_t *encrypted_key, uint64_t key_len, uint8_t *encr_bls_key, uint32_t *enc_bls_key_len) {
char skey[ECDSA_SKEY_LEN];
int status = AES_decrypt(encrypted_key, key_len, skey);
if (status != SGX_SUCCESS) {
*err_status = status;
snprintf(err_string, BUF_LEN,"aes decrypt failed with status %d", status);
snprintf(err_string, BUF_LEN, "aes decrypt failed with status %d", status);
return;
}
skey[ECDSA_SKEY_LEN - 1] = 0;
int num_shares = strlen(s_shares)/192;
int num_shares = strlen(s_shares) / 192;
mpz_t sum;
mpz_init(sum);
......@@ -1582,7 +1608,7 @@ void create_bls_key_aes(int *err_status, char* err_string, const char* s_shares,
//snprintf(err_string, BUF_LEN,"comon0 is %s len is %d\n", common_key, strlen(common_key));
for ( int i = 0; i < num_shares; i++) {
for (int i = 0; i < num_shares; i++) {
char encr_sshare[65];
strncpy(encr_sshare, s_shares + 192 * i, 64);
encr_sshare[64] = 0;
......@@ -1595,9 +1621,9 @@ void create_bls_key_aes(int *err_status, char* err_string, const char* s_shares,
session_key_recover(skey, s_share, common_key);
common_key[64] = 0;
if (common_key == NULL){
if (common_key == NULL) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
snprintf(err_string, BUF_LEN, "invalid common_key");
mpz_clear(sum);
return;
}
......@@ -1608,9 +1634,9 @@ void create_bls_key_aes(int *err_status, char* err_string, const char* s_shares,
char decr_sshare[65];
xor_decrypt(common_key, encr_sshare, decr_sshare);
if (decr_sshare == NULL){
if (decr_sshare == NULL) {
*err_status = 1;
snprintf(err_string, BUF_LEN ,"invalid common_key");
snprintf(err_string, BUF_LEN, "invalid common_key");
mpz_clear(sum);
return;
}
......@@ -1622,10 +1648,10 @@ void create_bls_key_aes(int *err_status, char* err_string, const char* s_shares,
mpz_t decr_secret_share;
mpz_init(decr_secret_share);
if (mpz_set_str(decr_secret_share, decr_sshare, 16) == -1){
if (mpz_set_str(decr_secret_share, decr_sshare, 16) == -1) {
*err_status = 111;
//snprintf(err_string, BUF_LEN ,"invalid decrypted secret share");
snprintf(err_string, BUF_LEN ,decr_sshare);
snprintf(err_string, BUF_LEN, decr_sshare);
mpz_clear(decr_secret_share);
return;
}
......@@ -1645,15 +1671,15 @@ void create_bls_key_aes(int *err_status, char* err_string, const char* s_shares,
char key_share[mpz_sizeinbase(bls_key, 16) + 2];
char *key = mpz_get_str(key_share, 16, bls_key);
snprintf(err_string, BUF_LEN," bls private key is %s", key_share);
snprintf(err_string, BUF_LEN, " bls private key is %s", key_share);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, ECDSA_SKEY_LEN);
status = AES_encrypt(key_share, encr_bls_key);
if( status != SGX_SUCCESS) {
*err_status= -1;
snprintf(err_string, BUF_LEN,"aes encrypt bls private key failed with status %d ", status);
if (status != SGX_SUCCESS) {
*err_status = -1;
snprintf(err_string, BUF_LEN, "aes encrypt bls private key failed with status %d ", status);
mpz_clear(bls_key);
mpz_clear(sum);
mpz_clear(q);
......@@ -1666,7 +1692,8 @@ void create_bls_key_aes(int *err_status, char* err_string, const char* s_shares,
mpz_clear(q);
}
void get_bls_pub_key_aes(int *err_status, char* err_string, uint8_t* encrypted_key, uint64_t key_len, char* bls_pub_key){
void
get_bls_pub_key_aes(int *err_status, char *err_string, uint8_t *encrypted_key, uint64_t key_len, char *bls_pub_key) {
char skey_hex[ECDSA_SKEY_LEN];
......@@ -1675,15 +1702,15 @@ void get_bls_pub_key_aes(int *err_status, char* err_string, uint8_t* encrypted_k
int status = AES_decrypt(encrypted_key, key_len, skey_hex);
if (status != SGX_SUCCESS) {
*err_status = 1;
snprintf(err_string, BUF_LEN,"aes_decrypt failed with status %d", status);
snprintf(err_string, BUF_LEN, "aes_decrypt failed with status %d", status);
return;
}
skey_hex[ECDSA_SKEY_LEN - 1] = 0;
if (calc_bls_public_key(skey_hex, bls_pub_key) != 0){
if (calc_bls_public_key(skey_hex, bls_pub_key) != 0) {
*err_status = -1;
snprintf(err_string, BUF_LEN,"could not calculate bls public key");
snprintf(err_string, BUF_LEN, "could not calculate bls public key");
return;
}
}
......
......@@ -6532,7 +6532,7 @@ void signature_copy(signature R, signature sig);
_Bool signature_cmp(signature sig1, signature sig2);
void signature_clear(signature sig);
void signature_free(signature sig);
void signature_generate_key(point public_key, mpz_t private_key, domain_parameters curve);
......
......@@ -32,10 +32,9 @@
#include "numbertheory.h"
/*Initialize a signature*/
signature signature_init()
{
signature signature_init() {
signature sig;
sig = malloc(sizeof(struct signature_s));
sig = calloc(sizeof(struct signature_s), 1);
mpz_init(sig->r);
mpz_init(sig->s);
sig->v = 0;
......@@ -43,8 +42,7 @@ signature signature_init()
}
/*Print signature to standart output stream*/
void signature_print(signature sig)
{
void signature_print(signature sig) {
/*printf("\nSignature (r,s): \n\t(");
mpz_out_str(stdout, 10, sig->r);
printf(",\n\t");
......@@ -53,79 +51,66 @@ void signature_print(signature sig)
}
/*Set signature from strings of a base from 2-62*/
void signature_set_str(signature sig, char *r, char *s, int base)
{
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);
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)
{
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)
{
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);
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)
{
void signature_extract_public_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)
{
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);
//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 *) 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);
sgx_read_rand(rand_char, 32);
;
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);
//mpz_set_str(k, "49a0d7b786ec9cde0d0721d72804befd06571c974b191efb42ecf322ba9ddd9a", 16);
// mpz_set_str(k, "DC87789C4C1A09C97FF4DE72C0D0351F261F10A2B9009C80AEE70DDEC77201A0", 16);
......@@ -137,9 +122,9 @@ void signature_sign(signature sig, mpz_t message, mpz_t private_key, domain_para
//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 :)
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
......@@ -147,20 +132,13 @@ void signature_sign(signature sig, mpz_t message, mpz_t private_key, domain_para
mpz_invert(t1, k, curve->n);
mpz_mul(t2, private_key, r); //t2 = d*r
mpz_add(t3, message, t2); //t3 = e+t2
mpz_clear(t2);
mpz_init(t2);
mpz_mod(t2, t3, curve->n); //t2 = t3 mod n
mpz_clear(t3);
mpz_init(t3);
mpz_mul(t3, t2, t1); //t3 = t2 * t1
mpz_mod(s, t3, curve->n); //s = t3 mod n
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_t rem;
mpz_init(rem);
mpz_mod_ui(rem, Q->y, 2);
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);
......@@ -169,74 +147,54 @@ void signature_sign(signature sig, mpz_t message, mpz_t private_key, domain_para
if (mpz_cmp(s_mul_2, curve->n) > 0) {
b = 1;
}
sig->v = mpz_get_ui(rem) ^ b ;
point_clear(Q);
mpz_clear(rem);
mpz_clear(s_mul_2);
sig->v = mpz_get_ui(rem) ^ b;
mpz_t n_div_2;
mpz_init(n_div_2);
mpz_cdiv_q_ui(n_div_2, curve->n , 2);
mpz_cdiv_q_ui(n_div_2, curve->n, 2);
if (mpz_cmp(s, n_div_2) > 0) {
mpz_t neg;
mpz_init(neg);
mpz_sub(neg, curve->n, s);
mpz_clear(s);
mpz_init(s);
mpz_set(s, neg);
mpz_clear(neg);
}
mpz_clear(n_div_2);
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);
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);
}
/*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);
bool signature_verify(mpz_t message, signature sig, point public_key, domain_parameters curve) {
//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);
mpz_t one, w, u1, u2, t, tt2;
mpz_init(one); mpz_init(w); mpz_init(u1);
mpz_init(u2); mpz_init(t); mpz_init(tt2);
mpz_set_ui(one, 1);
point x = point_init();
point t1 = point_init();
point t2 = point_init();
bool result = false;
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) {
goto clean;
}
//w = s¯¹ mod n
number_theory_inverse(w, sig->s, curve->n);
......@@ -255,25 +213,25 @@ bool signature_verify(mpz_t message, signature sig, point public_key, domain_par
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
result = mpz_cmp(sig->r, x->x) == 0 && !x->infinity;
clean:
point_clear(x);
point_clear(t1);
point_clear(t2);
mpz_clear(w);
mpz_clear(u1);
mpz_clear(u2);
mpz_clear(t);
mpz_clear(one); 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)
{
void signature_free(signature sig) {
mpz_clear(sig->r);
mpz_clear(sig->s);
free(sig);
......
......@@ -53,10 +53,10 @@ void signature_copy(signature R, signature sig);
bool signature_cmp(signature sig1, signature sig2);
/*Release signature*/
void signature_clear(signature sig);
void signature_free(signature sig);
/*Generates a public key for a private key*/
void signature_generate_key(point public_key, mpz_t private_key, domain_parameters curve);
void signature_extract_public_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);
......
......@@ -97,7 +97,7 @@ int main(int argc, char *argv[]) {
is_sgx_https = 0;
break;
case 'a':
is_aes = 0;
is_aes = 1;
break;
case 'b':
SEK_initializer = enter_SEK;
......
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