Skip to content

Commit dd4e9d3

Browse files
committed
SKALE-3067-cleanup
1 parent 8b7bc69 commit dd4e9d3

File tree

7 files changed

+73
-34
lines changed

7 files changed

+73
-34
lines changed

secure_enclave/DHDkg.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@
4242
#include "EnclaveCommon.h"
4343
#include <string.h>
4444

45+
46+
4547
int gen_session_key(char *skey_str, char *pb_keyB, char *common_key) {
4648

49+
50+
51+
4752
int ret = -1;
4853

4954
LOG_INFO(__FUNCTION__);
5055

5156
SAFE_CHAR_BUF(pb_keyB_x, 65);SAFE_CHAR_BUF(pb_keyB_y, 65);
5257

53-
5458
mpz_t skey;
5559
mpz_init(skey);
5660
point pub_keyB = point_init();

secure_enclave/DKGUtils.cpp

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,14 @@ string ConvertG2ToString(const libff::alt_bn128_G2 &elem, int base = 10, const s
144144

145145
vector <libff::alt_bn128_Fr> SplitStringToFr(const char *coeffs, const char symbol) {
146146

147+
148+
147149
vector <libff::alt_bn128_Fr> result;
148150
string str(coeffs);
149151
string delim;
150152

153+
CHECK_ARG_CLEAN(coeffs);
154+
151155
try {
152156

153157
delim.push_back(symbol);
@@ -183,6 +187,8 @@ int gen_dkg_poly(char *secret, unsigned _t) {
183187
int status = 1;
184188
string result;
185189

190+
CHECK_ARG_CLEAN(secret);
191+
186192
try {
187193
for (size_t i = 0; i < _t; ++i) {
188194
libff::alt_bn128_Fr cur_coef = libff::alt_bn128_Fr::random_element();
@@ -247,6 +253,14 @@ void calc_secret_shares(const char *decrypted_coeffs,
247253
string result;
248254
char symbol = ':';
249255

256+
CHECK_ARG_CLEAN(decrypted_coeffs);
257+
CHECK_ARG_CLEAN(secret_shares);
258+
CHECK_ARG_CLEAN(_n > 0);
259+
CHECK_ARG_CLEAN(_t <= _n);
260+
261+
262+
263+
250264
try {
251265

252266
vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
@@ -260,18 +274,27 @@ void calc_secret_shares(const char *decrypted_coeffs,
260274

261275
} catch (exception &e) {
262276
LOG_ERROR(e.what());
263-
return;
277+
goto clean;
264278
} catch (...) {
265279
LOG_ERROR("Unknown throwable");
266-
return;
280+
goto clean;
267281
}
282+
283+
clean:
284+
;
268285
}
269286

270287
int calc_secret_share(const char *decrypted_coeffs, char *s_share,
271288
unsigned _t, unsigned _n, unsigned ind) {
272289

290+
273291
int result = 1;
274292

293+
CHECK_ARG_CLEAN(decrypted_coeffs);
294+
CHECK_ARG_CLEAN(s_share);
295+
CHECK_ARG_CLEAN(_n > 0);
296+
CHECK_ARG_CLEAN(_t <= _n);
297+
275298
try {
276299
char symbol = ':';
277300
vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
@@ -300,35 +323,19 @@ int calc_secret_share(const char *decrypted_coeffs, char *s_share,
300323
return result;
301324
}
302325

303-
void calc_secret_shareG2_old(const char *decrypted_coeffs, char *s_shareG2,
304-
unsigned _t, unsigned ind) {
305-
306-
try {
307-
char symbol = ':';
308-
vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
309-
310-
libff::alt_bn128_Fr secret_share = PolynomialValue(poly, libff::alt_bn128_Fr(ind), _t);
311-
312-
libff::alt_bn128_G2 secret_shareG2 = secret_share * libff::alt_bn128_G2::one();
313-
314-
string secret_shareG2_str = ConvertG2ToString(secret_shareG2);
315326

316-
strncpy(s_shareG2, secret_shareG2_str.c_str(), secret_shareG2_str.length() + 1);
327+
int calc_secret_shareG2(const char *s_share, char *s_shareG2) {
317328

318-
} catch (exception &e) {
319-
LOG_ERROR(e.what());
320-
} catch (...) {
321-
LOG_ERROR("Unknown throwable");
322-
}
323-
}
324329

325-
int calc_secret_shareG2(const char *s_share, char *s_shareG2) {
326330

327331
int result = 1;
328332

329333
mpz_t share;
330334
mpz_init(share);
331335

336+
CHECK_ARG_CLEAN(s_share);
337+
CHECK_ARG_CLEAN(s_shareG2);
338+
332339
try {
333340

334341

@@ -370,13 +377,21 @@ int calc_secret_shareG2(const char *s_share, char *s_shareG2) {
370377
int calc_public_shares(const char *decrypted_coeffs, char *public_shares,
371378
unsigned _t) {
372379

380+
// calculate for each node a list of public shares
381+
int ret = 1;
382+
string result;
383+
char symbol = ':';
384+
385+
CHECK_ARG_CLEAN(decrypted_coeffs);
386+
CHECK_ARG_CLEAN(public_shares);
387+
CHECK_ARG_CLEAN(_t > 0);
388+
389+
373390
try {
374-
// calculate for each node a list of public shares
375-
string result;
376-
char symbol = ':';
391+
377392
vector <libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_coeffs, symbol);
378393
if (poly.size() != _t) {
379-
return 1;
394+
goto clean;
380395
}
381396
for (size_t i = 0; i < _t; ++i) {
382397
libff::alt_bn128_G2 pub_share = poly.at(i) * libff::alt_bn128_G2::one();
@@ -385,15 +400,18 @@ int calc_public_shares(const char *decrypted_coeffs, char *public_shares,
385400
result += pub_share_str + ",";
386401
}
387402
strncpy(public_shares, result.c_str(), result.length());
388-
return 0;
403+
ret = 0;
389404

390405
} catch (exception &e) {
391406
LOG_ERROR(e.what());
392-
return 1;
407+
ret = 1;
393408
} catch (...) {
394409
LOG_ERROR("Unknown throwable");
395-
return 1;
410+
ret = 1;
396411
}
412+
413+
clean:
414+
return ret;
397415
}
398416

399417
string ConvertHexToDec(string hex_str) {

secure_enclave/DKGUtils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ EXTERNC int Verification ( char * public_shares, mpz_t decr_secret_share, int _t
5252

5353
EXTERNC int calc_bls_public_key(char* skey, char* pub_key);
5454

55-
EXTERNC void calc_secret_shareG2_old(const char* public_shares, char * s_shareG2,
56-
unsigned _t, unsigned ind);
5755

5856
EXTERNC int calc_secret_shareG2(const char* s_share, char * s_shareG2);
5957
#endif

secure_enclave/DomainParameters.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
@date 2019
2222
*/
2323

24+
#define SAFE_FREE(__X__) if (__X__) {free(__X__); __X__ = NULL;}
25+
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
26+
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
27+
2428
#ifdef USER_SPACE
2529
#include <gmp.h>
2630
#else
@@ -104,7 +108,7 @@ void domain_parameters_clear(domain_parameters curve)
104108
point_clear(curve->G);
105109
mpz_clear(curve->n);
106110
mpz_clear(curve->h);
107-
free(curve->name);
111+
SAFE_FREE(curve->name);
108112
free(curve);
109113
}
110114

secure_enclave/EnclaveCommon.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,11 @@ extern domain_parameters curve;
6868
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
6969
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
7070

71+
#define CHECK_ARG_CLEAN(_EXPRESSION_) \
72+
if (!(_EXPRESSION_)) { \
73+
LOG_ERROR("State check failed::");LOG_ERROR(#_EXPRESSION_); \
74+
LOG_ERROR(__FILE__); LOG_ERROR(__FUNCTION__);\
75+
goto clean;}
76+
7177

7278
#endif //SGXWALLET_ENCLAVECOMMON_H

secure_enclave/Point.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include <assert.h>
2828
#include <stdbool.h>
2929

30+
#define SAFE_FREE(__X__) if (__X__) {free(__X__); __X__ = NULL;}
31+
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
32+
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
33+
3034
#ifdef USER_SPACE
3135
#include <gmp.h>
3236
#else
@@ -338,6 +342,6 @@ void point_clear(point p)
338342
return;
339343
mpz_clear(p->x);
340344
mpz_clear(p->y);
341-
free(p);
345+
SAFE_FREE(p);
342346
}
343347

secure_enclave/Signature.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
#include <stdbool.h>
2727
#include <assert.h>
2828

29+
#define SAFE_FREE(__X__) if (__X__) {free(__X__); __X__ = NULL;}
30+
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
31+
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
32+
33+
2934
#ifdef USER_SPACE
3035
#include <gmp.h>
3136

0 commit comments

Comments
 (0)