EnclaveCommon.cpp 7.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
    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/>.

19
    @file EnclaveCommon.cpp
20 21 22
    @author Stan Kladko
    @date 2019
*/
23

kladko's avatar
kladko committed
24
#define GMP_WITH_SGX 1
kladkogex's avatar
kladkogex committed
25

26
#include <string.h>
27
#include <cstdint>
28

29 30
#include "../SCIPR/libff/algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include "../SCIPR/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp"
31

32 33 34 35 36
#include "secure_enclave_t.h"

#include "EnclaveConstants.h"
#include "EnclaveCommon.h"

37
using namespace std;
38

kladko's avatar
kladko committed
39
thread_local uint8_t decryptedDkgPoly[DKG_BUFER_LENGTH];
kladko's avatar
kladko committed
40

kladko's avatar
kladko committed
41
uint8_t *getThreadLocalDecryptedDkgPoly() {
kladko's avatar
kladko committed
42
    return decryptedDkgPoly;
kladko's avatar
kladko committed
43 44 45
}


kladko's avatar
m  
kladko committed
46 47
string *stringFromKey(libff::alt_bn128_Fr *_key) {
    string *ret = nullptr;
kladko's avatar
kladko committed
48 49
    mpz_t t;
    mpz_init(t);
50

kladko's avatar
kladko committed
51
    SAFE_CHAR_BUF(arr, BUF_LEN);
52

kladko's avatar
kladko committed
53 54
    try {
        _key->as_bigint().to_mpz(t);
55

kladko's avatar
kladko committed
56
        char *tmp = mpz_get_str(arr, 10, t);
57

kladko's avatar
kladko committed
58 59 60 61
        if (!tmp) {
            LOG_ERROR("stringFromKey: mpz_get_str failed");
            goto clean;
        }
kladko's avatar
kladko committed
62
        ret = new string(tmp);
kladko's avatar
kladko committed
63 64
    } catch (exception &e) {
        LOG_ERROR(e.what());
kladko's avatar
kladko committed
65
        goto clean;
kladko's avatar
kladko committed
66 67
    } catch (...) {
        LOG_ERROR("Unknown throwable");
kladko's avatar
kladko committed
68
        goto clean;
kladko's avatar
kladko committed
69
    }
kladko's avatar
kladko committed
70 71 72 73

    clean:
    mpz_clear(t);
    return ret;
74 75
}

76
string *stringFromFq(libff::alt_bn128_Fq *_fq) {
kladko's avatar
kladko committed
77

kladko's avatar
m  
kladko committed
78
    string *ret = nullptr;
kladko's avatar
kladko committed
79
    mpz_t t;
80 81
    mpz_init(t);
    SAFE_CHAR_BUF(arr, BUF_LEN);
kladko's avatar
kladko committed
82

kladko's avatar
kladko committed
83 84 85
    try {
        _fq->as_bigint().to_mpz(t);
        char *tmp = mpz_get_str(arr, 10, t);
kladko's avatar
m  
kladko committed
86
        ret = new string(tmp);
kladko's avatar
kladko committed
87 88
    } catch (exception &e) {
        LOG_ERROR(e.what());
kladko's avatar
kladko committed
89
        goto clean;
kladko's avatar
kladko committed
90 91
    } catch (...) {
        LOG_ERROR("Unknown throwable");
kladko's avatar
kladko committed
92
        goto clean;
kladko's avatar
kladko committed
93
    }
kladko's avatar
kladko committed
94 95 96 97

    clean:
    mpz_clear(t);
    return ret;
98 99
}

100
string *stringFromG1(libff::alt_bn128_G1 *_g1) {
101

kladko's avatar
m  
kladko committed
102 103 104
    string *sX = nullptr;
    string *sY = nullptr;
    string *ret = nullptr;
kladko's avatar
kladko committed
105 106


kladko's avatar
kladko committed
107 108
    try {
        _g1->to_affine_coordinates();
109

kladko's avatar
kladko committed
110
        auto sX = stringFromFq(&_g1->X);
111

kladko's avatar
kladko committed
112 113 114 115 116
        if (!sX) {
            goto clean;
        }

        auto sY = stringFromFq(&_g1->Y);
117

kladko's avatar
kladko committed
118 119 120
        if (!sY) {
            goto clean;
        }
121

kladko's avatar
kladko committed
122
        ret = new string(*sX + ":" + *sY);
123

kladko's avatar
kladko committed
124 125
    } catch (exception &e) {
        LOG_ERROR(e.what());
kladko's avatar
kladko committed
126
        goto clean;
kladko's avatar
kladko committed
127 128
    } catch (...) {
        LOG_ERROR("Unknown throwable");
kladko's avatar
kladko committed
129
        goto clean;
kladko's avatar
kladko committed
130
    }
131

kladko's avatar
kladko committed
132 133 134 135 136 137 138
    clean:

    SAFE_FREE(sX);
    SAFE_FREE(sY);

    return ret;

kladko's avatar
kladko committed
139
}
140

kladko's avatar
kladko committed
141 142
libff::alt_bn128_Fr *keyFromString(const char *_keyStringHex) {

kladko's avatar
m  
kladko committed
143
    mpz_t skey;
144 145
    mpz_init(skey);
    SAFE_CHAR_BUF(skey_dec, BUF_LEN);
kladko's avatar
m  
kladko committed
146
    libff::alt_bn128_Fr *ret = nullptr;
kladko's avatar
kladko committed
147

148 149 150
    if (mpz_set_str(skey, _keyStringHex, 16) == -1) {
        goto clean;
    }
kladko's avatar
kladko committed
151

kladko's avatar
m  
kladko committed
152 153 154 155 156 157 158 159 160 161
    mpz_get_str(skey_dec, 10, skey);

    ret = new libff::alt_bn128_Fr(skey_dec);

    goto clean;

    clean:

    mpz_clear(skey);
    return ret;
kladkogex's avatar
kladkogex committed
162
}
163

kladkogex's avatar
kladkogex committed
164
int inited = 0;
kladkogex's avatar
kladkogex committed
165

kladko's avatar
kladko committed
166 167
domain_parameters curve;

168
void enclave_init() {
kladko's avatar
kladko committed
169 170 171

    LOG_INFO(__FUNCTION__ );

kladkogex's avatar
kladkogex committed
172 173 174
    if (inited == 1)
        return;
    inited = 1;
kladko's avatar
kladko committed
175

kladko's avatar
kladko committed
176

kladko's avatar
kladko committed
177 178
    LOG_INFO("Initing libff");
    try {
kladko's avatar
kladko committed
179

kladko's avatar
kladko committed
180
        LOG_INFO("Initing params");
kladko's avatar
kladko committed
181

kladko's avatar
kladko committed
182
        libff::init_alt_bn128_params();
kladko's avatar
kladko committed
183

kladko's avatar
kladko committed
184
        LOG_INFO("Initing curve");
kladko's avatar
kladko committed
185
        curve = domain_parameters_init();
kladko's avatar
kladko committed
186
        LOG_INFO("Initing curve domain");
kladko's avatar
kladko committed
187 188 189 190
        domain_parameters_load_curve(curve, secp256k1);
    } catch (exception& e) {
        LOG_ERROR("Exception in libff init");
        LOG_ERROR(e.what());
kladko's avatar
kladko committed
191
        abort();
kladko's avatar
kladko committed
192 193
    } catch (...) {
        LOG_ERROR("Unknown exception in libff");
kladko's avatar
kladko committed
194
        abort();
kladko's avatar
kladko committed
195 196
    }
    LOG_INFO("Inited libff");
kladkogex's avatar
kladkogex committed
197 198
}

kladkogex's avatar
kladkogex committed
199
bool enclave_sign(const char *_keyString, const char *_hashXString, const char *_hashYString,
kladko's avatar
kladko committed
200
                  char *sig) {
kladko's avatar
m  
kladko committed
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

    bool ret = false;

    libff::alt_bn128_Fr* key = nullptr;
    string * r = nullptr;


    if (!_keyString) {
        LOG_ERROR("Null key string");
        goto clean;
    }

    if (!_hashXString) {
        LOG_ERROR("Null hashX");
        goto clean;
    }

    if (!_hashYString) {
        LOG_ERROR("Null hashY");
        goto clean;
    }

    if (!sig) {
        LOG_ERROR("Null sig");
        goto clean;
    }

kladko's avatar
kladko committed
228 229
    try {
        auto key = keyFromString(_keyString);
230

kladko's avatar
m  
kladko committed
231 232 233
        if (!key) {
            LOG_ERROR("Null key");
            goto clean;
kladko's avatar
kladko committed
234
        }
kladkogex's avatar
kladkogex committed
235

kladko's avatar
kladko committed
236 237 238
        libff::alt_bn128_Fq hashX(_hashXString);
        libff::alt_bn128_Fq hashY(_hashYString);
        libff::alt_bn128_Fq hashZ = 1;
239

kladko's avatar
kladko committed
240
        libff::alt_bn128_G1 hash(hashX, hashY, hashZ);
241

kladko's avatar
kladko committed
242
        libff::alt_bn128_G1 sign = key->as_bigint() * hash;
243

kladko's avatar
kladko committed
244
        sign.to_affine_coordinates();
245

kladko's avatar
kladko committed
246
        auto r = stringFromG1(&sign);
247

kladko's avatar
kladko committed
248
        memset(sig, 0, BUF_LEN);
249

kladko's avatar
kladko committed
250
        strncpy(sig, r->c_str(), BUF_LEN);
251

kladko's avatar
m  
kladko committed
252
        ret =  true;
kladko's avatar
kladko committed
253 254 255

    } catch (exception &e) {
        LOG_ERROR(e.what());
kladko's avatar
m  
kladko committed
256
        goto clean;
kladko's avatar
kladko committed
257 258
    } catch (...) {
        LOG_ERROR("Unknown throwable");
kladko's avatar
m  
kladko committed
259
        goto clean;
kladko's avatar
kladko committed
260
    }
261

kladko's avatar
m  
kladko committed
262 263 264 265 266 267
    clean:

    SAFE_DELETE(key);
    SAFE_DELETE(r);
    return ret;

268
}
269

kladko's avatar
m  
kladko committed
270
void carray2Hex(const unsigned char *d, int _len, char *_hexArray) {
271 272 273 274 275 276 277 278 279 280
    char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
                       '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    for (int j = 0; j < _len; j++) {
        _hexArray[j * 2] = hexval[((d[j] >> 4) & 0xF)];
        _hexArray[j * 2 + 1] = hexval[(d[j]) & 0x0F];
    }

    _hexArray[_len * 2] = 0;
}
281

282
int char2int(char _input) {
kladko's avatar
kladko committed
283 284 285 286 287 288 289
    if (_input >= '0' && _input <= '9')
        return _input - '0';
    if (_input >= 'A' && _input <= 'F')
        return _input - 'A' + 10;
    if (_input >= 'a' && _input <= 'f')
        return _input - 'a' + 10;
    return -1;
290 291
}

kladko's avatar
kladko committed
292 293
bool hex2carray2(const char *_hex, uint64_t *_bin_len,
                 uint8_t *_bin, const int _max_length) {
294
    int len = strnlen(_hex, _max_length);
295 296 297 298 299 300 301

    if (len == 0 && len % 2 == 1)
        return false;

    *_bin_len = len / 2;

    for (int i = 0; i < len / 2; i++) {
kladko's avatar
kladko committed
302 303
        int high = char2int((char) _hex[i * 2]);
        int low = char2int((char) _hex[i * 2 + 1]);
304 305 306 307 308 309 310 311 312 313 314

        if (high < 0 || low < 0) {
            return false;
        }

        _bin[i] = (unsigned char) (high * 16 + low);
    }

    return true;
}

kladko's avatar
kladko committed
315 316 317
bool hex2carray(const char *_hex, uint64_t *_bin_len,
                uint8_t *_bin) {
    int len = strnlen(_hex, 2 * BUF_LEN);
318

kladko's avatar
kladko committed
319 320
    if (len == 0 && len % 2 == 1)
        return false;
321

kladko's avatar
kladko committed
322
    *_bin_len = len / 2;
323

kladko's avatar
kladko committed
324 325 326
    for (int i = 0; i < len / 2; i++) {
        int high = char2int((char) _hex[i * 2]);
        int low = char2int((char) _hex[i * 2 + 1]);
327

kladko's avatar
kladko committed
328 329 330
        if (high < 0 || low < 0) {
            return false;
        }
331

kladko's avatar
kladko committed
332 333
        _bin[i] = (unsigned char) (high * 16 + low);
    }
334

kladko's avatar
kladko committed
335
    return true;
336 337
}

kladko's avatar
kladko committed
338 339 340
enum log_level {
    L_TRACE = 0, L_DEBUG = 1, L_INFO = 2, L_WARNING = 3, L_ERROR = 4
};
341

342
uint32_t globalLogLevel_ = 2;
343

kladko's avatar
kladko committed
344
void logMsg(log_level _level, const char *_msg) {
345
    if (_level < globalLogLevel_)
346 347 348 349 350 351 352
        return;

    if (!_msg) {
        oc_printf("Null msg in logMsg");
        return;
    }

353
    oc_printf("***ENCLAVE_LOG***:");
354
    oc_printf(_msg);
355
    oc_printf("\n");
356 357
}

358

kladko's avatar
kladko committed
359
void LOG_INFO(const char *_msg) {
360 361
    logMsg(L_INFO, _msg);
};
kladko's avatar
kladko committed
362
void LOG_WARN(const char *_msg) {
363 364 365
    logMsg(L_WARNING, _msg);
};

kladko's avatar
kladko committed
366
void LOG_ERROR(const char *_msg) {
367 368
    logMsg(L_ERROR, _msg);
};
kladko's avatar
kladko committed
369
void LOG_DEBUG(const char *_msg) {
370 371
    logMsg(L_DEBUG, _msg);
};
kladko's avatar
kladko committed
372
void LOG_TRACE(const char *_msg) {
373 374
    logMsg(L_TRACE, _msg);
};