-
Notifications
You must be signed in to change notification settings - Fork 34
/
DKGCrypto.cpp
591 lines (426 loc) · 17.1 KB
/
DKGCrypto.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
183
184
185
186
187
188
189
190
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
Copyright (C) 2019-Present SKALE Labs
This file is part of sgxwallet.
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file DKGCrypto.cpp
@author Stan Kladko
@date 2019
*/
#include <iostream>
#include <memory>
#include "SGXException.h"
#include "common.h"
#include "sgxwallet.h"
#include "third_party/spdlog/spdlog.h"
#include "CryptoTools.h"
#include "DKGCrypto.h"
#include "SEKManager.h"
#include "SGXWalletServer.hpp"
template <class T> string ConvertToString(T field_elem, int base = 10) {
mpz_t t;
mpz_init(t);
field_elem.as_bigint().to_mpz(t);
SAFE_CHAR_BUF(arr, mpz_sizeinbase(t, base) + 2);
mpz_get_str(arr, base, t);
mpz_clear(t);
string output = arr;
return output;
}
string convertHexToDec(const string &hex_str) {
mpz_t dec;
mpz_init(dec);
string ret = "";
try {
if (mpz_set_str(dec, hex_str.c_str(), 16) == -1) {
goto clean;
}
SAFE_CHAR_BUF(arr, mpz_sizeinbase(dec, 10) + 2);
mpz_get_str(arr, 10, dec);
ret = arr;
} catch (exception &e) {
mpz_clear(dec);
throw SGXException(INCORRECT_STRING_CONVERSION, e.what());
} catch (...) {
mpz_clear(dec);
throw SGXException(EXCEPTION_IN_CONVERT_HEX_TO_DEC,
"Exception in convert hex to dec");
}
clean:
mpz_clear(dec);
return ret;
}
string convertG2ToString(const libff::alt_bn128_G2 &elem, int base,
const string &delim) {
string result = "";
try {
result += ConvertToString(elem.X.c0);
result += delim;
result += ConvertToString(elem.X.c1);
result += delim;
result += ConvertToString(elem.Y.c0);
result += delim;
result += ConvertToString(elem.Y.c1);
return result;
} catch (exception &e) {
throw SGXException(CONVERT_G2_INCORRECT_STRING_CONVERSION, e.what());
return result;
} catch (...) {
throw SGXException(EXCEPTION_IN_CONVERT_G2_STRING,
"Exception in convert G2 to string");
return result;
}
return result;
}
string gen_dkg_poly(int _t) {
vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;
uint64_t enc_len = 0;
vector<uint8_t> encrypted_dkg_secret(BUF_LEN, 0);
sgx_status_t status = SGX_SUCCESS;
status = trustedGenDkgSecret(eid, &errStatus, errMsg.data(),
encrypted_dkg_secret.data(), &enc_len, _t);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
uint64_t length = enc_len;
;
CHECK_STATE(encrypted_dkg_secret.size() >= length);
vector<char> hexEncrPoly = carray2Hex(encrypted_dkg_secret.data(), length);
string result(hexEncrPoly.data());
return result;
}
vector<vector<string>> get_verif_vect(const string &encryptedPolyHex, int t) {
auto encryptedPolyHexPtr = encryptedPolyHex.c_str();
CHECK_STATE(encryptedPolyHexPtr);
vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;
vector<char> pubShares(10000, 0);
uint64_t encLen = 0;
vector<uint8_t> encrDKGPoly(2 * BUF_LEN, 0);
if (!hex2carray(encryptedPolyHexPtr, &encLen, encrDKGPoly.data(), 6100)) {
throw SGXException(GET_VV_INVALID_POLY_HEX, ":Invalid encryptedPolyHex");
}
sgx_status_t status = SGX_SUCCESS;
status =
trustedGetPublicShares(eid, &errStatus, errMsg.data(), encrDKGPoly.data(),
encLen, pubShares.data(), t);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
vector<string> g2Strings = splitString(pubShares.data(), ',');
vector<vector<string>> pubSharesVect(t);
for (uint64_t i = 0; i < g2Strings.size(); i++) {
vector<string> coeffStr = splitString(g2Strings.at(i).c_str(), ':');
pubSharesVect[i] = coeffStr;
}
return pubSharesVect;
}
vector<vector<string>>
getVerificationVectorMult(const std::string &encryptedPolyHex, int t, int n,
size_t ind) {
auto verificationVector = get_verif_vect(encryptedPolyHex, t);
vector<vector<string>> result(t);
for (int i = 0; i < t; ++i) {
libff::alt_bn128_G2 current_coefficient;
current_coefficient.X.c0 =
libff::alt_bn128_Fq(verificationVector[i][0].c_str());
current_coefficient.X.c1 =
libff::alt_bn128_Fq(verificationVector[i][1].c_str());
current_coefficient.Y.c0 =
libff::alt_bn128_Fq(verificationVector[i][2].c_str());
current_coefficient.Y.c1 =
libff::alt_bn128_Fq(verificationVector[i][3].c_str());
current_coefficient.Z = libff::alt_bn128_Fq2::one();
current_coefficient =
libff::power(libff::alt_bn128_Fr(ind + 1), i) * current_coefficient;
current_coefficient.to_affine_coordinates();
auto g2_str = convertG2ToString(current_coefficient);
result[i] = splitString(g2_str.c_str(), ':');
}
return result;
}
string getSecretShares(const string &_polyName, const char *_encryptedPolyHex,
const vector<string> &_publicKeys, int _t, int _n) {
CHECK_STATE(_encryptedPolyHex);
vector<char> hexEncrKey(BUF_LEN, 0);
vector<char> errMsg(BUF_LEN, 0);
vector<uint8_t> encrDKGPoly(BUF_LEN, 0);
int errStatus = 0;
uint64_t encLen = 0;
if (!hex2carray(_encryptedPolyHex, &encLen, encrDKGPoly.data(), BUF_LEN)) {
throw SGXException(GET_SS_INVALID_HEX,
string(__FUNCTION__) + ":Invalid encryptedPolyHex");
}
READ_LOCK(sgxInitMutex);
string result;
for (int i = 0; i < _n; i++) {
vector<uint8_t> encryptedSkey(BUF_LEN, 0);
uint64_t decLen;
vector<char> currentShare(193, 0);
vector<char> sShareG2(320, 0);
string pub_keyB = _publicKeys.at(i);
vector<char> pubKeyB(129, 0);
strncpy(pubKeyB.data(), pub_keyB.c_str(), 128);
pubKeyB.at(128) = 0;
spdlog::debug("pubKeyB is {}", pub_keyB);
sgx_status_t status = SGX_SUCCESS;
status = trustedGetEncryptedSecretShare(
eid, &errStatus, errMsg.data(), encrDKGPoly.data(), encLen,
encryptedSkey.data(), &decLen, currentShare.data(), sShareG2.data(),
pubKeyB.data(), _t, _n, i + 1);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
result += string(currentShare.data());
hexEncrKey = carray2Hex(encryptedSkey.data(), decLen);
string dhKeyName = "DKG_DH_KEY_" + _polyName + "_" + to_string(i) + ":";
string shareG2_name = "shareG2_" + _polyName + "_" + to_string(i) + ":";
SGXWalletServer::writeDataToDB(dhKeyName, hexEncrKey.data());
SGXWalletServer::writeDataToDB(shareG2_name, sShareG2.data());
}
string encryptedSecretShareName = "encryptedSecretShare:" + _polyName;
SGXWalletServer::writeDataToDB(encryptedSecretShareName, result);
return result;
}
string getSecretSharesV2(const string &_polyName, const char *_encryptedPolyHex,
const vector<string> &_publicKeys, int _t, int _n) {
CHECK_STATE(_encryptedPolyHex);
vector<char> hexEncrKey(BUF_LEN, 0);
vector<char> errMsg(BUF_LEN, 0);
vector<uint8_t> encrDKGPoly(BUF_LEN, 0);
int errStatus = 0;
uint64_t encLen = 0;
if (!hex2carray(_encryptedPolyHex, &encLen, encrDKGPoly.data(), BUF_LEN)) {
throw SGXException(GET_SS_V2_INVALID_HEX,
string(__FUNCTION__) + ":Invalid encrypted poly Hex");
}
READ_LOCK(sgxInitMutex);
string result;
for (int i = 0; i < _n; i++) {
vector<uint8_t> encryptedSkey(BUF_LEN, 0);
uint64_t decLen;
vector<char> currentShare(193, 0);
vector<char> sShareG2(320, 0);
string pub_keyB = _publicKeys.at(i);
vector<char> pubKeyB(129, 0);
strncpy(pubKeyB.data(), pub_keyB.c_str(), 128);
pubKeyB.at(128) = 0;
spdlog::debug("pubKeyB is {}", pub_keyB);
sgx_status_t status = SGX_SUCCESS;
status = trustedGetEncryptedSecretShareV2(
eid, &errStatus, errMsg.data(), encrDKGPoly.data(), encLen,
encryptedSkey.data(), &decLen, currentShare.data(), sShareG2.data(),
pubKeyB.data(), _t, _n, i + 1);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
result += string(currentShare.data());
hexEncrKey = carray2Hex(encryptedSkey.data(), decLen);
string dhKeyName = "DKG_DH_KEY_" + _polyName + "_" + to_string(i) + ":";
string shareG2_name = "shareG2_" + _polyName + "_" + to_string(i) + ":";
SGXWalletServer::writeDataToDB(dhKeyName, hexEncrKey.data());
SGXWalletServer::writeDataToDB(shareG2_name, sShareG2.data());
}
string encryptedSecretShareName = "encryptedSecretShare:" + _polyName;
SGXWalletServer::writeDataToDB(encryptedSecretShareName, result);
return result;
}
bool verifyShares(const char *publicShares, const char *encr_sshare,
const char *encryptedKeyHex, int t, int n, int ind) {
CHECK_STATE(publicShares);
CHECK_STATE(encr_sshare);
CHECK_STATE(encryptedKeyHex);
vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;
uint64_t decKeyLen = 0;
int result = 0;
SAFE_UINT8_BUF(encr_key, BUF_LEN);
if (!hex2carray(encryptedKeyHex, &decKeyLen, encr_key, BUF_LEN)) {
throw SGXException(VERIFY_SHARES_INVALID_KEY_HEX,
string(__FUNCTION__) + ":Invalid encryptedPolyHex");
}
SAFE_CHAR_BUF(pshares, 8193);
strncpy(pshares, publicShares, strlen(publicShares));
sgx_status_t status = SGX_SUCCESS;
status = trustedDkgVerify(eid, &errStatus, errMsg.data(), pshares,
encr_sshare, encr_key, decKeyLen, t, ind, &result);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
if (result == 2) {
throw SGXException(VERIFY_SHARES_INVALID_PUBLIC_SHARES,
string(__FUNCTION__) + +":Invalid public shares");
}
return result;
}
bool verifySharesV2(const char *publicShares, const char *encr_sshare,
const char *encryptedKeyHex, int t, int n, int ind) {
CHECK_STATE(publicShares);
CHECK_STATE(encr_sshare);
CHECK_STATE(encryptedKeyHex);
vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;
uint64_t decKeyLen = 0;
int result = 0;
SAFE_UINT8_BUF(encr_key, BUF_LEN);
if (!hex2carray(encryptedKeyHex, &decKeyLen, encr_key, BUF_LEN)) {
throw SGXException(VERIFY_SHARES_V2_INVALID_POLY_HEX,
string(__FUNCTION__) + ":Invalid encryptedPolyHex");
}
SAFE_CHAR_BUF(pshares, 8193);
strncpy(pshares, publicShares, strlen(publicShares));
sgx_status_t status = SGX_SUCCESS;
status =
trustedDkgVerifyV2(eid, &errStatus, errMsg.data(), pshares, encr_sshare,
encr_key, decKeyLen, t, ind, &result);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
if (result == 2) {
throw SGXException(VERIFY_SHARES_V2_INVALID_PUBLIC_SHARES,
string(__FUNCTION__) + ":Invalid public shares");
}
return result;
}
bool createBLSShare(const string &blsKeyName, const char *s_shares,
const char *encryptedKeyHex) {
CHECK_STATE(s_shares);
CHECK_STATE(encryptedKeyHex);
vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;
uint64_t decKeyLen;
SAFE_UINT8_BUF(encr_bls_key, BUF_LEN);
SAFE_UINT8_BUF(encr_key, BUF_LEN);
if (!hex2carray(encryptedKeyHex, &decKeyLen, encr_key, BUF_LEN)) {
throw SGXException(CREATE_BLS_SHARE_INVALID_KEY_HEX,
string(__FUNCTION__) + ":Invalid encryptedKeyHex");
}
uint64_t enc_bls_len = 0;
sgx_status_t status = SGX_SUCCESS;
status = trustedCreateBlsKey(eid, &errStatus, errMsg.data(), s_shares,
encr_key, decKeyLen, encr_bls_key, &enc_bls_len);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
vector<char> hexBLSKey = carray2Hex(encr_bls_key, enc_bls_len);
SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey.data());
return true;
}
bool createBLSShareV2(const string &blsKeyName, const char *s_shares,
const char *encryptedKeyHex) {
CHECK_STATE(s_shares);
CHECK_STATE(encryptedKeyHex);
vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;
uint64_t decKeyLen;
SAFE_UINT8_BUF(encr_bls_key, BUF_LEN)
SAFE_UINT8_BUF(encr_key, BUF_LEN)
if (!hex2carray(encryptedKeyHex, &decKeyLen, encr_key, BUF_LEN)) {
throw SGXException(CREATE_BLS_SHARE_INVALID_KEY_HEX,
string(__FUNCTION__) + ":Invalid encryptedKeyHex");
}
uint64_t enc_bls_len = 0;
sgx_status_t status = SGX_SUCCESS;
status =
trustedCreateBlsKeyV2(eid, &errStatus, errMsg.data(), s_shares, encr_key,
decKeyLen, encr_bls_key, &enc_bls_len);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
vector<char> hexBLSKey = carray2Hex(encr_bls_key, enc_bls_len);
SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey.data());
return true;
}
vector<string> getBLSPubKey(const char *encryptedKeyHex) {
CHECK_STATE(encryptedKeyHex);
vector<char> errMsg1(BUF_LEN, 0);
int errStatus = 0;
uint64_t decKeyLen = 0;
SAFE_UINT8_BUF(encrKey, BUF_LEN);
if (!hex2carray(encryptedKeyHex, &decKeyLen, encrKey, BUF_LEN)) {
throw SGXException(GET_BLS_PUBKEY_INVALID_KEY_HEX,
string(__FUNCTION__) + ":Invalid encryptedKeyHex");
}
SAFE_CHAR_BUF(pubKey, 320)
sgx_status_t status = SGX_SUCCESS;
status = trustedGetBlsPubKey(eid, &errStatus, errMsg1.data(), encrKey,
decKeyLen, pubKey);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg1.data());
vector<string> pubKeyVect = splitString(pubKey, ':');
spdlog::debug("pub key is ");
for (int i = 0; i < 4; i++)
spdlog::debug("{}", pubKeyVect.at(i));
return pubKeyVect;
}
vector<string> calculateAllBlsPublicKeys(const vector<string> &public_shares) {
size_t n = public_shares.size();
size_t t = public_shares[0].length() / 256;
uint64_t share_length = 256;
uint8_t coord_length = 64;
vector<libff::alt_bn128_G2> public_keys(n, libff::alt_bn128_G2::zero());
vector<libff::alt_bn128_G2> public_values(t, libff::alt_bn128_G2::zero());
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < t; ++j) {
libff::alt_bn128_G2 public_share;
uint64_t pos0 = share_length * j;
string x_c0_str =
convertHexToDec(public_shares[i].substr(pos0, coord_length));
string x_c1_str = convertHexToDec(
public_shares[i].substr(pos0 + coord_length, coord_length));
string y_c0_str = convertHexToDec(
public_shares[i].substr(pos0 + 2 * coord_length, coord_length));
string y_c1_str = convertHexToDec(
public_shares[i].substr(pos0 + 3 * coord_length, coord_length));
if (x_c0_str == "" || x_c1_str == "" || y_c0_str == "" ||
y_c1_str == "") {
return {};
}
public_share.X.c0 = libff::alt_bn128_Fq(x_c0_str.c_str());
public_share.X.c1 = libff::alt_bn128_Fq(x_c1_str.c_str());
public_share.Y.c0 = libff::alt_bn128_Fq(y_c0_str.c_str());
public_share.Y.c1 = libff::alt_bn128_Fq(y_c1_str.c_str());
public_share.Z = libff::alt_bn128_Fq2::one();
public_values[j] = public_values[j] + public_share;
}
}
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < t; ++j) {
public_keys[i] =
public_keys[i] +
libff::power(libff::alt_bn128_Fr(i + 1), j) * public_values[j];
}
public_keys[i].to_affine_coordinates();
}
vector<string> result(n);
for (size_t i = 0; i < n; ++i) {
result[i] = convertG2ToString(public_keys[i]);
}
return result;
}
string decryptDHKey(const string &polyName, int ind) {
vector<char> errMsg1(BUF_LEN, 0);
int errStatus = 0;
string DH_key_name = polyName + "_" + to_string(ind) + ":";
shared_ptr<string> hexEncrKeyPtr =
SGXWalletServer::readFromDb(DH_key_name, "DKG_DH_KEY_");
spdlog::debug("encr DH key is {}", *hexEncrKeyPtr);
spdlog::debug("encr DH key length is {}", hexEncrKeyPtr->length());
vector<char> hexEncrKey(2 * BUF_LEN, 0);
uint64_t dhEncLen = 0;
SAFE_UINT8_BUF(encryptedDHKey, BUF_LEN);
if (!hex2carray(hexEncrKeyPtr->c_str(), &dhEncLen, encryptedDHKey, BUF_LEN)) {
throw SGXException(DECRYPT_DH_KEY_INVALID_KEY_HEX,
string(__FUNCTION__) + ":Invalid hexEncrKey");
}
spdlog::debug("encr DH key length is {}", dhEncLen);
SAFE_CHAR_BUF(DHKey, ECDSA_SKEY_LEN)
sgx_status_t status = SGX_SUCCESS;
status = trustedDecryptKey(eid, &errStatus, errMsg1.data(), encryptedDHKey,
dhEncLen, DHKey);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg1.data())
return DHKey;
}
vector<string> mult_G2(const string &x) {
vector<string> result(4);
libff::alt_bn128_Fr el(x.c_str());
libff::alt_bn128_G2 elG2 = el * libff::alt_bn128_G2::one();
elG2.to_affine_coordinates();
result[0] = ConvertToString(elG2.X.c0);
result[1] = ConvertToString(elG2.X.c1);
result[2] = ConvertToString(elG2.Y.c0);
result[3] = ConvertToString(elG2.Y.c1);
return result;
}