secure_enclave.c 37.6 KB
Newer Older
kladkogex's avatar
kladkogex committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*

Copyright 2018 Intel Corporation

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

34
#include "secure_enclave_t.h"
kladkogex's avatar
kladkogex committed
35 36
#include "sgx_tcrypto.h"
#include "sgx_tseal.h"
37 38
#include <sgx_tgmp.h>
#include <sgx_trts.h>
kladkogex's avatar
kladkogex committed
39

kladkogex's avatar
kladkogex committed
40
#include <math.h>
41
#include <string.h>
42
#include <stdio.h>
43

svetaro's avatar
svetaro committed
44 45 46 47 48
#include <stdbool.h>
#include "domain_parameters.h"
#include "point.h"
#include "signature.h"
#include "curves.h"
49 50

#include "DH_dkg.h"
svetaro's avatar
svetaro committed
51 52

#include <sgx_tcrypto.h>
53

54 55 56 57
#include "AESUtils.h"

//#include "../sgxwallet_common.h"
#include "enclave_common.h"
58

59
uint8_t Decrypted_dkg_poly[DKG_BUFER_LENGTH];
60

61

kladkogex's avatar
kladkogex committed
62
void *(*gmp_realloc_func)(void *, size_t, size_t);
kladkogex's avatar
kladkogex committed
63

kladkogex's avatar
kladkogex committed
64
void *(*oc_realloc_func)(void *, size_t, size_t);
kladkogex's avatar
kladkogex committed
65

kladkogex's avatar
kladkogex committed
66
void (*gmp_free_func)(void *, size_t);
kladkogex's avatar
kladkogex committed
67

kladkogex's avatar
kladkogex committed
68 69 70
void (*oc_free_func)(void *, size_t);

void *reallocate_function(void *, size_t, size_t);
kladkogex's avatar
kladkogex committed
71

kladkogex's avatar
kladkogex committed
72 73 74
void free_function(void *, size_t);


75
void tgmp_init() {
kladkogex's avatar
kladkogex committed
76 77
    oc_realloc_func = &reallocate_function;
    oc_free_func = &free_function;
kladkogex's avatar
kladkogex committed
78

kladkogex's avatar
kladkogex committed
79 80
    mp_get_memory_functions(NULL, &gmp_realloc_func, &gmp_free_func);
    mp_set_memory_functions(NULL, oc_realloc_func, oc_free_func);
kladkogex's avatar
kladkogex committed
81 82
}

83
void free_function(void *ptr, size_t sz) {
kladkogex's avatar
kladkogex committed
84 85 86 87 88 89 90 91 92
    if (sgx_is_within_enclave(ptr, sz))
        gmp_free_func(ptr, sz);
    else {
        sgx_status_t status;

        status = oc_free(ptr, sz);
        if (status != SGX_SUCCESS)
            abort();
    }
kladkogex's avatar
kladkogex committed
93 94
}

95
void *reallocate_function(void *ptr, size_t osize, size_t nsize) {
kladkogex's avatar
kladkogex committed
96 97
    uint64_t nptr;
    sgx_status_t status;
kladkogex's avatar
kladkogex committed
98

kladkogex's avatar
kladkogex committed
99 100 101
    if (sgx_is_within_enclave(ptr, osize)) {
        return gmp_realloc_func(ptr, osize, nsize);
    }
kladkogex's avatar
kladkogex committed
102

kladkogex's avatar
kladkogex committed
103 104 105
    status = oc_realloc(&nptr, ptr, osize, nsize);
    if (status != SGX_SUCCESS)
        abort();
kladkogex's avatar
kladkogex committed
106

kladkogex's avatar
kladkogex committed
107 108 109 110 111
    /*
     * If the entire range of allocated memory is not outside the enclave
     * then something truly terrible has happened. In theory, we could
     * free() and try again, but would you trust the OS at this point?
     */
kladkogex's avatar
kladkogex committed
112

kladkogex's avatar
kladkogex committed
113 114
    if (!sgx_is_outside_enclave((void *) ptr, nsize))
        abort();
kladkogex's avatar
kladkogex committed
115

kladkogex's avatar
kladkogex committed
116
    return (void *) nptr;
kladkogex's avatar
kladkogex committed
117 118
}

119
void e_mpz_add(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un) {}
kladkogex's avatar
kladkogex committed
120

121
void e_mpz_mul(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un) {}
kladkogex's avatar
kladkogex committed
122

123
void e_mpz_div(mpz_t *c_un, mpz_t *a_un, mpz_t *b_un) {}
kladkogex's avatar
kladkogex committed
124

125
void e_mpf_div(mpf_t *c_un, mpf_t *a_un, mpf_t *b_un) {}
kladkogex's avatar
kladkogex committed
126

127

128
void generate_ecdsa_key(int *err_status, char *err_string,
svetaro's avatar
svetaro committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
                        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);

  mpz_t seed;
  mpz_init(seed);
  mpz_import(seed, 32, 1, sizeof(rand_char[0]), 0, 0, rand_char);

  free(rand_char);

  mpz_t skey;
  mpz_init(skey);
  mpz_mod(skey, seed, curve->p);
  mpz_clear(seed);

148
  //mpz_set_str(skey, "e7af72d241d4dd77bc080ce9234d742f6b22e35b3a660e8c197517b909f63ca8", 16);
149
   //mpz_set_str(skey, "4160780231445160889237664391382223604576", 10);
svetaro's avatar
svetaro committed
150 151 152
  //mpz_set_str(skey, "4160780231445160889237664391382223604184857153814275770598791864649971919844", 10);
  //mpz_set_str(skey, "1", 10);
  //mpz_set_str(skey, "ebb2c082fd7727890a28ac82f6bdf97bad8de9f5d7c9028692de1a255cad3e0f", 16);
153
 // mpz_set_str(skey, "D30519BCAE8D180DBFCC94FE0B8383DC310185B0BE97B4365083EBCECCD75759", 16);
svetaro's avatar
svetaro committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

  //Public key
  point Pkey = point_init();

  signature_generate_key(Pkey, skey, curve);

  uint8_t base = 16;

  int len = mpz_sizeinbase (Pkey->x, base) + 2;
  //snprintf(err_string, BUF_LEN, "len = %d\n", len);
  char arr_x[len];
  char* px = mpz_get_str(arr_x, base, Pkey->x);
  //snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px);
  int n_zeroes = 64 - strlen(arr_x);
  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);
  n_zeroes = 64 - strlen(arr_y);
  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);
183
  snprintf(err_string, BUF_LEN, "skey is %s len %d\n", skey_str, strlen(skey_str));
svetaro's avatar
svetaro committed
184 185 186 187 188 189

  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");
190
    *err_status = status;
svetaro's avatar
svetaro committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    return;
  }

  *enc_len = sealedLen;

  mpz_clear(skey);
  domain_parameters_clear(curve);
  point_clear(Pkey);
}


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) {

  //uint32_t dec_len = 0;

  domain_parameters curve = domain_parameters_init();
  domain_parameters_load_curve(curve, secp256k1);

  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);

  if (status != SGX_SUCCESS) {
    snprintf(err_string, BUF_LEN,"sgx_unseal_data failed with status %d", status);
217
    *err_status = status;
svetaro's avatar
svetaro committed
218 219 220 221 222 223 224 225 226 227
    return;
  }

  //strncpy(err_string, skey, 1024);

  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");
228
    *err_status = -10;
229 230
    mpz_clear(skey_mpz);
    return;
svetaro's avatar
svetaro committed
231 232 233 234 235 236 237 238 239 240 241 242
  }

  //Public key
  point Pkey = point_init();

  signature_generate_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");
243
    *err_status = -11;
244
    return;
svetaro's avatar
svetaro committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  }

  int base = 16;

  int len = mpz_sizeinbase (Pkey->x, base) + 2;
  //snprintf(err_string, BUF_LEN, "len = %d\n", len);
  char arr_x[len];
  char* px = mpz_get_str(arr_x, base, Pkey->x);
  //snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px);
  int n_zeroes = 64 - strlen(arr_x);
  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);
  n_zeroes = 64 - strlen(arr_y);
  for ( int i = 0; i < n_zeroes; i++){
    pub_key_y[i] = '0';
  }
  strncpy(pub_key_y + n_zeroes, arr_y, 1024 - n_zeroes);

  mpz_clear(skey_mpz);
  domain_parameters_clear(curve);
  point_clear(Pkey);
272 273
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
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) {

    domain_parameters curve = domain_parameters_init();
    domain_parameters_load_curve(curve, secp256k1);

    char skey[ECDSA_SKEY_LEN];

    sgx_status_t status = sgx_unseal_data(
            (const sgx_sealed_data_t *)encrypted_key, NULL, 0, skey, &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,"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){
        *err_status = -1;
        snprintf(err_string, BUF_LEN ,"invalid secret key");
        mpz_clear(skey_mpz);
        return;
    }

    /*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){
        *err_status = -1;
        snprintf(err_string, BUF_LEN ,"invalid message hash");
        mpz_clear(msg_mpz);
        return;
    }
    //mpz_set_str(msg_mpz,"4b688df40bcedbe641ddb16ff0a1842d9c67ea1c3bf63f3e0471baa664531d1a", 16);

    signature sign = signature_init();

    signature_sign( sign, msg_mpz, skey_mpz, curve);

    point Pkey = point_init();

    signature_generate_key(Pkey, skey_mpz, curve);

    if ( !signature_verify(msg_mpz, sign, Pkey, curve) ){
        *err_status = -2;
         snprintf(err_string, BUF_LEN,"signature is not verified! ");
        return;
    }

    //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);

    char arr_r[mpz_sizeinbase (sign->r, base) + 2];
    char* r = mpz_get_str(arr_r, base, sign->r);
    strncpy(sig_r, arr_r, 1024);

    char arr_s[mpz_sizeinbase (sign->s, base) + 2];
    char* s = mpz_get_str(arr_s, base, sign->s);
    strncpy(sig_s, arr_s, 1024);

    *sig_v = sign->v;

    mpz_clear(skey_mpz);
    mpz_clear(msg_mpz);
    domain_parameters_clear(curve);
    signature_clear(sign);
    point_clear(Pkey);

}

359

360 361
void encrypt_key(int *err_status, char *err_string, char *key,
                 uint8_t *encrypted_key, uint32_t *enc_len) {
362

363
    //init();
364

kladkogex's avatar
kladkogex committed
365
    *err_status = UNKNOWN_ERROR;
366

kladkogex's avatar
kladkogex committed
367
    memset(err_string, 0, BUF_LEN);
368

kladkogex's avatar
kladkogex committed
369
    checkKey(err_status, err_string, key);
370

kladkogex's avatar
kladkogex committed
371 372 373
    if (*err_status != 0) {
        snprintf(err_string + strlen(err_string), BUF_LEN, "check_key failed");
        return;
374 375
    }

kladkogex's avatar
kladkogex committed
376
    uint32_t sealedLen = sgx_calc_sealed_data_size(0, MAX_KEY_LENGTH);
377 378 379



kladkogex's avatar
kladkogex committed
380
    if (sealedLen > BUF_LEN) {
kladkogex's avatar
kladkogex committed
381
        *err_status = ENCRYPTED_KEY_TOO_LONG;
kladkogex's avatar
kladkogex committed
382 383 384
        snprintf(err_string, BUF_LEN, "sealedLen > MAX_ENCRYPTED_KEY_LENGTH");
        return;
    }
385 386


kladkogex's avatar
kladkogex committed
387
    memset(encrypted_key, 0, BUF_LEN);
388

389 390
    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) {
kladkogex's avatar
kladkogex committed
391
        *err_status = SEAL_KEY_FAILED;
392
        snprintf(err_string, BUF_LEN, "SGX seal data failed with status %d", status);
kladkogex's avatar
kladkogex committed
393 394
        return;
    }
395

kladkogex's avatar
kladkogex committed
396
    *enc_len = sealedLen;
397

kladkogex's avatar
kladkogex committed
398 399
    char decryptedKey[BUF_LEN];
    memset(decryptedKey, 0, BUF_LEN);
400

kladkogex's avatar
kladkogex committed
401
    decrypt_key(err_status, err_string, encrypted_key, sealedLen, decryptedKey);
402

kladkogex's avatar
kladkogex committed
403 404 405 406
    if (*err_status != 0) {
        snprintf(err_string + strlen(err_string), BUF_LEN, ":decrypt_key failed");
        return;
    }
407

kladkogex's avatar
kladkogex committed
408
    uint64_t decryptedKeyLen = strnlen(decryptedKey, MAX_KEY_LENGTH);
409

kladkogex's avatar
kladkogex committed
410 411
    if (decryptedKeyLen == MAX_KEY_LENGTH) {
        snprintf(err_string, BUF_LEN, "Decrypted key is not null terminated");
kladkogex's avatar
kladkogex committed
412 413
        return;
    }
414

415

kladkogex's avatar
kladkogex committed
416
    *err_status = -8;
417

kladkogex's avatar
kladkogex committed
418 419
    if (strncmp(key, decryptedKey, MAX_KEY_LENGTH) != 0) {
        snprintf(err_string, BUF_LEN, "Decrypted key does not match original key");
kladkogex's avatar
kladkogex committed
420
        return;
kladkogex's avatar
kladkogex committed
421
    }
422

kladkogex's avatar
kladkogex committed
423
    *err_status = 0;
kladkogex's avatar
kladkogex committed
424
}
425

kladkogex's avatar
kladkogex committed
426
void decrypt_key(int *err_status, char *err_string, uint8_t *encrypted_key,
kladkogex's avatar
kladkogex committed
427
                 uint32_t enc_len, char *key) {
428

kladkogex's avatar
kladkogex committed
429
    init();
430

kladkogex's avatar
kladkogex committed
431
    uint32_t decLen;
432

kladkogex's avatar
kladkogex committed
433
    *err_status = -9;
434

kladkogex's avatar
kladkogex committed
435 436
    sgx_status_t status = sgx_unseal_data(
            (const sgx_sealed_data_t *) encrypted_key, NULL, 0, (uint8_t *) key, &decLen);
kladkogex's avatar
kladkogex committed
437

kladkogex's avatar
kladkogex committed
438
    if (status != SGX_SUCCESS) {
439
        *err_status = status;
kladkogex's avatar
kladkogex committed
440 441 442
        snprintf(err_string, BUF_LEN, "sgx_unseal_data failed with status %d", status);
        return;
    }
443

444
    //snprintf(err_string, BUF_LEN, "decr key is %s", key);
445

446 447
    if (decLen > MAX_KEY_LENGTH) {
        snprintf(err_string, BUF_LEN, "wrong decLen");//"decLen != MAX_KEY_LENGTH");
kladkogex's avatar
kladkogex committed
448 449
        return;
    }
450

kladkogex's avatar
kladkogex committed
451
    *err_status = -10;
452

453

kladkogex's avatar
kladkogex committed
454
    uint64_t keyLen = strnlen(key, MAX_KEY_LENGTH);
455 456


kladkogex's avatar
kladkogex committed
457 458 459
    if (keyLen == MAX_KEY_LENGTH) {
        snprintf(err_string, BUF_LEN, "Key is not null terminated");
        return;
kladkogex's avatar
kladkogex committed
460
    }
461

kladkogex's avatar
kladkogex committed
462
    // check that key is padded with 0s
463

464 465 466 467 468 469 470 471
//    for (int i = keyLen; i < MAX_KEY_LENGTH; i++) {
//        if (key[i] != 0) {
//            snprintf(err_string, BUF_LEN, "Unpadded key");
//            return;
//        }
//    }

    //strncpy(key, "2f993bb09f16c402a27dae868c02791bca7fcf564f1c9e2ba50b142b843a4b60", BUF_LEN);
472

kladkogex's avatar
kladkogex committed
473
    *err_status = 0;
474 475
    return;

kladkogex's avatar
kladkogex committed
476
}
477

478

kladkogex's avatar
kladkogex committed
479 480 481
void bls_sign_message(int *err_status, char *err_string, uint8_t *encrypted_key,
                      uint32_t enc_len, char *_hashX,
                      char *_hashY, char *signature) {
482

483 484


kladkogex's avatar
kladkogex committed
485
    char key[BUF_LEN];
kladkogex's avatar
kladkogex committed
486
    char* sig = (char*) calloc(BUF_LEN, 1);
487

kladkogex's avatar
kladkogex committed
488
    init();
489 490


kladkogex's avatar
kladkogex committed
491
    decrypt_key(err_status, err_string, encrypted_key, enc_len, key);
492

kladkogex's avatar
kladkogex committed
493
    if (*err_status != 0) {
494
        strncpy(signature, err_string, BUF_LEN);
kladkogex's avatar
kladkogex committed
495 496
        return;
    }
497

kladkogex's avatar
kladkogex committed
498
    enclave_sign(key, _hashX, _hashY, sig);
499

kladkogex's avatar
kladkogex committed
500
    strncpy(signature, sig, BUF_LEN);
501

kladkogex's avatar
kladkogex committed
502 503 504 505
    if (strnlen(signature, BUF_LEN) < 10) {
        *err_status = -1;
        return;
    }
506

507 508 509

}

510
void gen_dkg_secret (int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, uint32_t* enc_len, size_t _t){
511

512
  char* dkg_secret = (char*)malloc(DKG_BUFER_LENGTH);
513

514 515 516 517
  if (gen_dkg_poly(dkg_secret, _t) != 0 ){
    *err_status = - 1;
     return;
  }
518

519 520
  snprintf(err_string, BUF_LEN,"poly is %s ", dkg_secret);

521
  uint32_t sealedLen = sgx_calc_sealed_data_size(0, DKG_BUFER_LENGTH);//sizeof(sgx_sealed_data_t) +  sizeof(dkg_secret);
522

523
  sgx_status_t status = sgx_seal_data(0, NULL, DKG_BUFER_LENGTH, (uint8_t*)dkg_secret, sealedLen,(sgx_sealed_data_t*)encrypted_dkg_secret);
524

525
  if(status !=  SGX_SUCCESS) {
526
    snprintf(err_string, BUF_LEN,"SGX seal data failed");
527 528
    *err_status = status;
    return;
529
  }
530 531

  *enc_len = sealedLen;
svetaro's avatar
svetaro committed
532
  free(dkg_secret);
533
}
534

535
void decrypt_dkg_secret (int *err_status, char* err_string, uint8_t* encrypted_dkg_secret, uint8_t* decrypted_dkg_secret, uint32_t* dec_len){
536

537
  //uint32_t dec_size = DKG_BUFER_LENGTH;//sgx_get_encrypt_txt_len( ( sgx_sealed_data_t *)encrypted_dkg_secret);
538
  uint32_t decr_len;
539
  sgx_status_t status = sgx_unseal_data(
540
      (const sgx_sealed_data_t *)encrypted_dkg_secret, NULL, 0, decrypted_dkg_secret, &decr_len);
541

542
  if (status != SGX_SUCCESS) {
543
    snprintf(err_string, BUF_LEN,"sgx_unseal_data - encrypted_dkg_secret failed with status %d", status);
544
    *err_status = status;
545
    return;
546
  }
547 548

  *dec_len = decr_len;
549 550
}

551
void get_secret_shares(int *err_status, char* err_string, uint8_t* encrypted_dkg_secret, uint32_t* dec_len, char* secret_shares,
552
    unsigned _t, unsigned _n){
553 554 555 556 557 558 559 560 561 562 563

  char* decrypted_dkg_secret = (char*)malloc(DKG_BUFER_LENGTH);

  //char decrypted_dkg_secret[DKG_MAX_SEALED_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);
  //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) {
564
    snprintf(err_string, BUF_LEN,"sgx_unseal_data - encrypted_dkg_secret failed with status %d", *err_status);
565 566 567 568 569 570 571 572
    return;
  }

  *dec_len = decr_len;

 // strncpy(err_string, decrypted_dkg_secret, 1024);
 calc_secret_shares(decrypted_dkg_secret, secret_shares, _t, _n);
 free(decrypted_dkg_secret);
573
}
574

575 576
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){
svetaro's avatar
svetaro committed
577
  char* decrypted_dkg_secret = (char*)malloc(DKG_MAX_SEALED_LEN);
578 579 580 581 582 583 584 585
  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);
586 587 588
  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");
svetaro's avatar
svetaro committed
589 590
    return;
  }
591
}
svetaro's avatar
svetaro committed
592 593


594
void set_encrypted_dkg_poly(int *err_status, char *err_string, uint8_t* encrypted_poly){
svetaro's avatar
svetaro committed
595

596 597 598
  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);
svetaro's avatar
svetaro committed
599

600
  if (status != SGX_SUCCESS) {
601
    *err_status = -1;
602
    snprintf(err_string, BUF_LEN,"sgx_unseal_data - encrypted_poly failed with status %d", status);
603 604
    return;
  }
svetaro's avatar
svetaro committed
605

606
}
svetaro's avatar
svetaro committed
607

608
void get_encr_sshare(int *err_status, char *err_string, uint8_t *encrypted_skey, uint32_t* dec_len,
609
    char* result_str, char * s_shareG2, char* pub_keyB, uint8_t _t, uint8_t _n, uint8_t ind ){
svetaro's avatar
svetaro committed
610

611 612 613
  char skey[ECDSA_SKEY_LEN];
  char *pub_key_x = (char *)calloc(1024, 1);
  char *pub_key_y = (char *)calloc(1024, 1);
svetaro's avatar
svetaro committed
614

615
  uint32_t enc_len;
svetaro's avatar
svetaro committed
616

617
  generate_ecdsa_key(err_status, err_string, encrypted_skey, &enc_len, pub_key_x, pub_key_y);
618 619 620
  if ( *err_status != 0){
    return;
  }
621
 // snprintf(err_string, BUF_LEN,"pub_key_x is %s", pub_key_x);
622

623 624
 *dec_len = enc_len;

625
  sgx_status_t status = sgx_unseal_data(
626 627
      (const sgx_sealed_data_t *)encrypted_skey, NULL, 0, (uint8_t *)skey, &enc_len);

628
  if (status != SGX_SUCCESS) {
629
    snprintf(err_string, BUF_LEN,"sgx_unseal_data failed - encrypted_skey with status %d", status);
630
    *err_status = status;
631 632
    return;
  }
633
  snprintf(err_string, BUF_LEN,"unsealed random skey is %s\n", skey);
634

635
  char * common_key = (char *)malloc(65);
636
  gen_session_key(skey, pub_keyB, common_key);
637 638
  //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);
639 640 641 642

  char* s_share = (char *)malloc(65);
  //char s_share[65];

643 644 645 646 647
  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");
    return;
  }
648
  snprintf(err_string + 88, BUF_LEN,"\nsecret share is %s", s_share);
649

650 651 652 653 654
  if (calc_secret_shareG2(s_share, s_shareG2) != 0){
    *err_status = -1;
    snprintf(err_string, BUF_LEN,"invalid decr secret share\n");
    return;
  }
655

656 657
  char* cypher = (char *)malloc(65);
  xor_encrypt(common_key, s_share, cypher);
658 659 660 661 662
  if (cypher == NULL){
      *err_status = 1;
      snprintf(err_string, BUF_LEN ,"invalid common_key");
      return;
  }
663
  //snprintf(err_string, BUF_LEN ,"cypher is %s length is %d", cypher, strlen(cypher));
664 665 666 667

  strncpy(result_str, cypher, strlen(cypher));
  strncpy(result_str + strlen(cypher), pub_key_x, strlen(pub_key_x));
  strncpy(result_str + strlen(pub_key_x) + strlen(pub_key_y), pub_key_y, strlen(pub_key_y));
668 669

  // snprintf(err_string, BUF_LEN,"s_share is %s length is %d", result_str, strlen(result_str));
670 671 672 673 674 675 676 677 678

  //mpz_clear(skey);
  //free(skey);
  free(common_key);
  free(pub_key_x);
  free(pub_key_y);
  free(s_share);
  free(cypher);
}
svetaro's avatar
svetaro committed
679

680 681
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){
682

683
  uint32_t enc_len;
684

685 686 687 688 689 690
//  sgx_status_t status = sgx_unseal_data(
//      (const sgx_sealed_data_t *)encrypted_DHkey, NULL, 0, (uint8_t *)DH_key, &enc_len);
//  if (status != SGX_SUCCESS) {
//    snprintf(err_string, BUF_LEN,"sgx_unseal_data - encrypted_DHkey failed with status %d", status);
//    return;
//  }
691

692 693 694 695
  char* decrypted_dkg_secret = (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);
  if (*err_status != 0) {
696
    snprintf(err_string, BUF_LEN,"sgx_unseal_data - encrypted_dkg_secret failed with status %d", *err_status);
697 698
    return;
  }
699

700 701 702
  calc_secret_shareG2_old(decrypted_dkg_secret, s_shareG2, _t, ind1);

  //snprintf(err_string, BUF_LEN,"poly:%s", decrypted_dkg_secret);
703
 // snprintf(err_string, BUF_LEN,"what the ...");
704 705

  //snprintf(err_string, BUF_LEN,"s_shareG2:%s", s_shareG2);
706 707
  free(decrypted_dkg_secret);
}
708 709 710 711

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){

712 713 714 715 716
  //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);
  if (status != SGX_SUCCESS) {
717
    *err_status = status;
718 719 720 721 722 723 724 725 726 727 728 729
    snprintf(err_string, BUF_LEN,"sgx_unseal_key failed with status %d", status);
    return;
  }

  char encr_sshare[65];
  strncpy(encr_sshare, s_share, 64);
  encr_sshare[64] = 0;

  char common_key[65];
  char decr_sshare[65];
  session_key_recover(skey, s_share, common_key);
  common_key[64] = 0;
730 731 732 733 734
  if (common_key == NULL){
    *err_status = 1;
    snprintf(err_string, BUF_LEN ,"invalid common_key");
    return;
  }
735 736

  xor_decrypt(common_key, encr_sshare, decr_sshare);
737 738 739 740 741
  if (decr_sshare == NULL){
      *err_status = 1;
      snprintf(err_string, BUF_LEN ,"invalid common_key");
      return;
  }
742

743 744 745 746 747 748 749 750

   //snprintf(err_string, BUF_LEN,"encr_share is %s length is %d", encr_sshare, strlen(encr_sshare));
  //snprintf(err_string, BUF_LEN,"s_share is %s length is %d", s_share, strlen(s_share));

//  snprintf(err_string, BUF_LEN,"sshare is %s\n", decr_sshare);
//  snprintf(err_string + 75, BUF_LEN - 75,"common_key is %s\n", common_key);
//  snprintf(err_string + 153, BUF_LEN - 153," s_key is %s", skey);

751 752 753

  mpz_t s;
  mpz_init(s);
754 755 756 757 758 759
  if (mpz_set_str(s, decr_sshare, 16) == -1){
      *err_status = 1;
      snprintf(err_string, BUF_LEN ,"invalid decr secret share");
      mpz_clear(s);
      return;
  }
760

761
  *result = Verification(public_shares, s, _t, _ind);
762

763
  snprintf(err_string, BUF_LEN,"common_key in verification is %s", common_key);
764 765 766

}

767
void create_bls_key(int *err_status, char* err_string, const char* s_shares,
768
                      uint8_t* encrypted_key, uint64_t key_len, uint8_t * encr_bls_key, uint32_t *enc_bls_key_len){
769 770 771 772 773

  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);
  if (status != SGX_SUCCESS) {
774
    *err_status = 1;
775 776 777 778 779 780 781 782 783 784
    snprintf(err_string, BUF_LEN,"sgx_unseal_key failed with status %d", status);
    return;
  }

  int num_shares = strlen(s_shares)/192;

  mpz_t sum;
  mpz_init(sum);
  mpz_set_ui(sum, 0);

785 786 787 788

  //snprintf(err_string, BUF_LEN,"comon0 is %s len is %d\n", common_key, strlen(common_key));


789 790 791 792 793 794
  for ( int i = 0; i < num_shares; i++) {
    char encr_sshare[65];
    strncpy(encr_sshare, s_shares + 192 * i, 64);
    encr_sshare[64] = 0;

    char s_share[193];
795
    strncpy(s_share, s_shares + 192 * i, 192);
796
    s_share[192] = 0;
797

798 799 800 801
    char common_key[65];
    session_key_recover(skey, s_share, common_key);
    common_key[64] = 0;

802 803 804
    if (common_key == NULL){
      *err_status = 1;
      snprintf(err_string, BUF_LEN ,"invalid common_key");
805
      mpz_clear(sum);
806 807
      return;
    }
808 809 810 811 812

    //snprintf(err_string + 85*(i+1) , BUF_LEN,"common is %s len is %d\n", common_key, strlen(common_key));

    //snprintf(err_string + 201*i , BUF_LEN,"secret is %s",s_share);

813 814
    char decr_sshare[65];
    xor_decrypt(common_key, encr_sshare, decr_sshare);
815 816 817
    if (decr_sshare == NULL){
        *err_status = 1;
        snprintf(err_string, BUF_LEN ,"invalid common_key");
818
        mpz_clear(sum);
819 820
        return;
    }
821 822
    //decr_sshare[64] = 0;

823 824 825 826
    //snprintf(err_string + 158 * i, BUF_LEN,"decr sshare is %s", decr_sshare);
    //snprintf(err_string + 158 * i + 79, BUF_LEN," common_key is %s", common_key);


827 828
    mpz_t decr_secret_share;
    mpz_init(decr_secret_share);
829 830 831
    if (mpz_set_str(decr_secret_share, decr_sshare, 16) == -1){
        *err_status = 1;
        snprintf(err_string, BUF_LEN ,"invalid decrypted secret share");
832
        mpz_clear(decr_secret_share);
833 834
        return;
    }
835 836 837 838 839 840 841

    mpz_addmul_ui(sum, decr_secret_share, 1);
    mpz_clear(decr_secret_share);
  }

   mpz_t q;
   mpz_init(q);
842
   mpz_set_str(q, "21888242871839275222246405745257275088548364400416034343698204186575808495617", 10);
843 844 845 846 847 848

   mpz_t bls_key;
   mpz_init(bls_key);

   mpz_mod(bls_key, sum, q);

849 850
   char key_share[mpz_sizeinbase(bls_key, 16) + 2];
   char *key = mpz_get_str(key_share, 16, bls_key);
851
   snprintf(err_string, BUF_LEN," bls private key is %s", key_share);
852 853
   uint32_t sealedLen = sgx_calc_sealed_data_size(0, ECDSA_SKEY_LEN);

854

855
   status = sgx_seal_data(0, NULL, ECDSA_SKEY_LEN, (uint8_t *)key_share, sealedLen,(sgx_sealed_data_t*)encr_bls_key);
856
   if( status !=  SGX_SUCCESS) {
857
    *err_status= -1;
858
    snprintf(err_string, BUF_LEN,"seal bls private key failed with status %d ", status);
859 860 861
    mpz_clear(bls_key);
    mpz_clear(sum);
    mpz_clear(q);
862 863
    return;
   }
864
  *enc_bls_key_len = sealedLen;
865

866 867 868 869 870 871 872 873 874 875 876

//  mpz_t s;
//  mpz_init(s);
//  mpz_set_str(s, decr_sshare, 16);



  //snprintf(err_string, BUF_LEN,"val is %s", decrypted_dkg_secret);

  mpz_clear(bls_key);
  mpz_clear(sum);
877 878 879 880 881
  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){

882
  char skey_hex[ECDSA_SKEY_LEN];
883

884
  uint32_t len = key_len;
885

886 887
  sgx_status_t status = sgx_unseal_data(
      (const sgx_sealed_data_t *)encrypted_key, NULL, 0, (uint8_t *)skey_hex, &len);
888
  if (status != SGX_SUCCESS) {
889
    *err_status = 1;
890 891 892 893
    snprintf(err_string, BUF_LEN,"sgx_unseal_data failed with status %d", status);
    return;
  }

894 895 896 897 898
  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");
    return;
  }
899
}
900

901 902
void generate_SEK(int *err_status, char *err_string,
                        uint8_t *encrypted_SEK, uint32_t *enc_len){
903 904 905 906 907 908 909 910 911 912
  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);
  uint32_t hex_aes_key_length = SGX_AESGCM_KEY_SIZE * 2;
  uint8_t SEK[hex_aes_key_length];
  carray2Hex(SEK_raw, SGX_AESGCM_KEY_SIZE, SEK);


  uint32_t sealedLen = sgx_calc_sealed_data_size(0, hex_aes_key_length + 1);
  memcpy(err_string, SEK, BUF_LEN);
913

914 915 916
  for ( uint8_t i = 0; i < SGX_AESGCM_KEY_SIZE; i++){
    AES_key[i] = SEK_raw[i];
  }
917

918

919

920
  sgx_status_t status = sgx_seal_data(0, NULL, hex_aes_key_length + 1, SEK, sealedLen,(sgx_sealed_data_t*)encrypted_SEK);
921
  if( status !=  SGX_SUCCESS) {
922
    snprintf(err_string, BUF_LEN, "seal SEK failed");
923 924 925 926 927
    *err_status = status;
    return;
  }

  *enc_len = sealedLen;
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
  //free(rand_char);
}

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) {

  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);

  mpz_t seed;
  mpz_init(seed);
  mpz_import(seed, 32, 1, sizeof(rand_char[0]), 0, 0, rand_char);
943

944
  free(rand_char);
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

  mpz_t skey;
  mpz_init(skey);
  mpz_mod(skey, seed, curve->p);
  mpz_clear(seed);

  //Public key
  point Pkey = point_init();

  signature_generate_key(Pkey, skey, curve);

  uint8_t base = 16;

  int len = mpz_sizeinbase (Pkey->x, base) + 2;
  //snprintf(err_string, BUF_LEN, "len = %d\n", len);
  char arr_x[len];
  char* px = mpz_get_str(arr_x, base, Pkey->x);
  //snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px);
  int n_zeroes = 64 - strlen(arr_x);
  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);
  n_zeroes = 64 - strlen(arr_y);
  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);
  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");
    *err_status = stat;
    return;
  }

  *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);
    //*err_status = stat;
    return;
  }

  mpz_clear(skey);
  domain_parameters_clear(curve);
  point_clear(Pkey);
}

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) {

  domain_parameters curve = domain_parameters_init();
  domain_parameters_load_curve(curve, secp256k1);

  char skey[ECDSA_SKEY_LEN];

  int status = AES_decrypt(encrypted_key, enc_len, skey);

  if (status != 0) {
    snprintf(err_string, BUF_LEN,"AES_decrypt failed with status %d", status);
    *err_status = status;
    return;
  }

  skey[enc_len - SGX_AESGCM_MAC_SIZE - SGX_AESGCM_IV_SIZE] = '\0';

  strncpy(err_string, skey, 1024);

  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);
    *err_status = -10;
    mpz_clear(skey_mpz);
    return;
  }

  //Public key
  point Pkey = point_init();

  signature_generate_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");
    *err_status = -11;
    return;
  }

  int base = 16;

  int len = mpz_sizeinbase (Pkey->x, base) + 2;
  //snprintf(err_string, BUF_LEN, "len = %d\n", len);
  char arr_x[len];
  char* px = mpz_get_str(arr_x, base, Pkey->x);
  //snprintf(err_string, BUF_LEN, "arr=%p px=%p\n", arr_x, px);
  int n_zeroes = 64 - strlen(arr_x);
  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);
  n_zeroes = 64 - strlen(arr_y);
  for ( int i = 0; i < n_zeroes; i++){
    pub_key_y[i] = '0';
  }
  strncpy(pub_key_y + n_zeroes, arr_y, 1024 - n_zeroes);

  mpz_clear(skey_mpz);
  domain_parameters_clear(curve);
  point_clear(Pkey);
1072 1073
}

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
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) {

  domain_parameters curve = domain_parameters_init();
  domain_parameters_load_curve(curve, secp256k1);

  char skey[ECDSA_SKEY_LEN];

  int status = AES_decrypt(encrypted_key, enc_len, skey);

  if (status != 0) {
    *err_status = 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));
  mpz_t skey_mpz;
  mpz_init(skey_mpz);
  if (mpz_set_str(skey_mpz, skey, ECDSA_SKEY_BASE) == -1){
    *err_status = -1;
    snprintf(err_string, BUF_LEN ,"invalid secret key");
    mpz_clear(skey_mpz);
    return;
  }


  mpz_t msg_mpz;
  mpz_init(msg_mpz);
  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;
  }

  signature sign = signature_init();

  signature_sign( sign, msg_mpz, skey_mpz, curve);

  point Pkey = point_init();

  signature_generate_key(Pkey, skey_mpz, curve);

  if ( !signature_verify(msg_mpz, sign, Pkey, curve) ){
    *err_status = -2;
    snprintf(err_string, BUF_LEN,"signature is not verified! ");
    return;
  }

  //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);

  char arr_r[mpz_sizeinbase (sign->r, base) + 2];
  char* r = mpz_get_str(arr_r, base, sign->r);
  strncpy(sig_r, arr_r, 1024);

  char arr_s[mpz_sizeinbase (sign->s, base) + 2];
  char* s = mpz_get_str(arr_s, base, sign->s);
  strncpy(sig_s, arr_s, 1024);

  *sig_v = sign->v;

  mpz_clear(skey_mpz);
  mpz_clear(msg_mpz);
  domain_parameters_clear(curve);
  signature_clear(sign);
  point_clear(Pkey);

}

void encrypt_key_aes(int *err_status, char *err_string, char *key,
                 uint8_t *encrypted_key, uint32_t *enc_len) {

  //init();

  *err_status = UNKNOWN_ERROR;

  memset(err_string, 0, BUF_LEN);

  checkKey(err_status, err_string, key);

  if (*err_status != 0) {
    snprintf(err_string + strlen(err_string), BUF_LEN, "check_key failed");
    return;
  }



  memset(encrypted_key, 0, BUF_LEN);


  int stat = AES_encrypt(key, encrypted_key);
  if ( stat != 0) {
    *err_status = stat;
    snprintf(err_string, BUF_LEN, "AES encrypt failed with status %d", stat);
    return;
  }

  *enc_len = strlen(key) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;

  char decryptedKey[BUF_LEN];
  memset(decryptedKey, 0, BUF_LEN);

  stat = AES_decrypt(encrypted_key, *enc_len, decryptedKey);

  if (stat != 0) {
    *err_status = stat;
    snprintf(err_string, BUF_LEN, ":decrypt_key failed with status %d", stat);
    return;
  }

  uint64_t decryptedKeyLen = strnlen(decryptedKey, MAX_KEY_LENGTH);

  if (decryptedKeyLen == MAX_KEY_LENGTH) {
    snprintf(err_string, BUF_LEN, "Decrypted key is not null terminated");
    return;
  }


  *err_status = -8;

  if (strncmp(key, decryptedKey, MAX_KEY_LENGTH) != 0) {
    snprintf(err_string, BUF_LEN, "Decrypted key does not match original key");
    return;
  }

  *err_status = 0;
}

void decrypt_key_aes(int *err_status, char *err_string, uint8_t *encrypted_key,
                 uint32_t enc_len, char *key) {

  init();

  uint32_t decLen;

  *err_status = -9;

  int status = AES_decrypt(encrypted_key, enc_len, key);


  if (status != 0) {
    *err_status = status;
    snprintf(err_string, BUF_LEN, "aes decrypt failed with status %d", status);
    return;
  }

  //snprintf(err_string, BUF_LEN, "decr key is %s", key);

  if (decLen > MAX_KEY_LENGTH) {
    snprintf(err_string, BUF_LEN, "wrong decLen");//"decLen != MAX_KEY_LENGTH");
    return;
  }

  *err_status = -10;


  uint64_t keyLen = strnlen(key, MAX_KEY_LENGTH);


  if (keyLen == MAX_KEY_LENGTH) {
    snprintf(err_string, BUF_LEN, "Key is not null terminated");
    return;
  }

  *err_status = 0;
  return;

}

1252
void bls_sign_message_test(int *err_status, char *err_string, uint8_t *encrypted_key,
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
                      uint32_t enc_len, char *_hashX,
                      char *_hashY, char *signature) {



  char key[BUF_LEN];
  char* sig = (char*) calloc(BUF_LEN, 1);

  init();


  int stat = AES_decrypt(encrypted_key, enc_len, key);

  if ( stat != 0) {
    *err_status = stat;
    strncpy(signature, err_string, BUF_LEN);
    return;
  }

  enclave_sign(key, _hashX, _hashY, sig);

  strncpy(signature, sig, BUF_LEN);

  if (strnlen(signature, BUF_LEN) < 10) {
    *err_status = -1;
    return;
  }
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
}

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 = (char*)calloc(DKG_BUFER_LENGTH, 1);

  if (gen_dkg_poly(dkg_secret, _t) != 0 ){
    *err_status = - 1;
    return;
  }
1290

1291 1292 1293 1294 1295 1296 1297 1298 1299
  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");
    *err_status = status;
    return;
  }
1300

1301 1302
  *enc_len = strlen(dkg_secret) + SGX_AESGCM_MAC_SIZE + SGX_AESGCM_IV_SIZE;
  free(dkg_secret);
1303 1304
}

1305
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){
1306

1307
  int status = AES_decrypt(encrypted_dkg_secret, dec_len, decrypted_dkg_secret);
1308

1309 1310 1311 1312 1313 1314 1315
  if (status != SGX_SUCCESS) {
    snprintf(err_string, BUF_LEN,"aes decrypt data - encrypted_dkg_secret failed with status %d", status);
    *err_status = status;
    return;
  }

  //*dec_len = decr_len;
1316 1317
}

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
void set_encrypted_dkg_poly_aes(int *err_status, char *err_string, uint8_t* encrypted_poly,  uint64_t* enc_len){

  uint32_t decr_len;
  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);
    return;
  }

}
1330