DKGCrypto.cpp 12.1 KB
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
/*
    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 "DKGCrypto.h"
#include "BLSCrypto.h"
#include "sgxwallet.h"
#include <iostream>

#include <memory>
#include "SGXWalletServer.hpp"
#include "SGXException.h"

//#include <libBLS/libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>

#include "spdlog/spdlog.h"
#include "common.h"

#define  DKG_MAX_SEALED_LEN 3100

vector<string> splitString(const char *coeffs, const char symbol) {
    string str(coeffs);
    string delim;
    delim.push_back(symbol);
    vector<string> G2_strings;
    size_t prev = 0, pos = 0;
    do {
        pos = str.find(delim, prev);
        if (pos == string::npos) pos = str.length();
        string token = str.substr(prev, pos - prev);
        if (!token.empty()) {
            string coeff(token.c_str());
            G2_strings.push_back(coeff);
        }
        prev = pos + delim.length();
    } while (pos < str.length() && prev < str.length());

    return G2_strings;
}

template<class T>
string ConvertToString(T field_elem, int base = 10) {
    mpz_t t;
    mpz_init(t);

    field_elem.as_bigint().to_mpz(t);

    char arr[mpz_sizeinbase(t, base) + 2];

    char *tmp = mpz_get_str(arr, base, t);
    mpz_clear(t);

    string output = tmp;

    return output;
}

string gen_dkg_poly(int _t) {
    vector<char> errMsg(1024, 0);
    int errStatus = 0;

    vector<uint8_t> encrypted_dkg_secret(BUF_LEN, 0);

    uint32_t enc_len = 0;

    if (!encryptKeys)
        status = trustedGenDkgSecret(eid, &errStatus, errMsg.data(), encrypted_dkg_secret.data(), &enc_len, _t);
    else
        status = trustedGenDkgSecretAES(eid, &errStatus, errMsg.data(), encrypted_dkg_secret.data(), &enc_len, _t);
    if (errStatus != 0) {
        throw SGXException(-666, errMsg.data());
    }

    spdlog::debug("trustedGenDkgSecret, status {}", errStatus, " err msg ", errMsg.data());
    spdlog::debug("in DKGCrypto encr len is {}", enc_len);

    uint64_t length = DKG_MAX_SEALED_LEN;
    if (encryptKeys) {
        length = enc_len;
    }

    vector<char> hexEncrPoly(2 * length + 1, 0);
    CHECK_STATE(encrypted_dkg_secret.size() >= length);
    carray2Hex(encrypted_dkg_secret.data(), length, hexEncrPoly.data());
    string result(hexEncrPoly.data());

    return result;
}

vector<vector<string>> get_verif_vect(const char *encryptedPolyHex, int t, int n) {

    vector<char> errMsg1(BUF_LEN, 0);

    int errStatus = 0;


    spdlog::debug("got encr poly size {}", char_traits<char>::length(encryptedPolyHex));


    vector<char> pubShares(10000, 0);

    uint64_t encLen = 0;

    vector<uint8_t> encrDKGPoly(2 * BUF_LEN, 0);

    if (!hex2carray2(encryptedPolyHex, &encLen, encrDKGPoly.data(), 6100)) {
        throw SGXException(INVALID_HEX, "Invalid encryptedPolyHex");
    }


    spdlog::debug("hex_encr_poly length is {}", strlen(encryptedPolyHex));
    spdlog::debug("enc len {}", encLen);


    uint32_t len = 0;

    if (!encryptKeys)
        status = trustedGetPublicShares(eid, &errStatus, errMsg1.data(), encrDKGPoly.data(), len, pubShares.data(), t,
                                        n);
    else {

        status = trustedGetPublicSharesAES(eid, &errStatus, errMsg1.data(), encrDKGPoly.data(), encLen,
                                            pubShares.data(), t, n);
    }
    if (errStatus != 0) {
        throw SGXException(-666, errMsg1.data());
    }


    spdlog::debug("err msg is {}", errMsg1.data());

    spdlog::debug("public_shares:");
    spdlog::debug("{}", pubShares.data());;
    spdlog::debug("trustedGetPublicShares status: {}", errStatus);

    vector<string> g2Strings = splitString(pubShares.data(), ',');
    vector<vector<string>> pubSharesVect;
    for (uint64_t i = 0; i < g2Strings.size(); i++) {
        vector<string> coeffStr = splitString(g2Strings.at(i).c_str(), ':');
        pubSharesVect.push_back(coeffStr);
    }

    return pubSharesVect;
}

string trustedGetSecretShares(const string &_polyName, const char *_encryptedPolyHex, const vector<string> &_publicKeys,
                              int _t,
                              int _n) {

    vector<char> errMsg1(BUF_LEN, 0);
    vector<char> hexEncrKey(BUF_LEN, 0);
    int errStatus = 0;
    uint64_t encLen = 0;


    vector<uint8_t> encrDKGPoly(BUF_LEN, 0);

    if (!hex2carray2(_encryptedPolyHex, &encLen, encrDKGPoly.data(), 6100)) {
        throw SGXException(INVALID_HEX, "Invalid encryptedPolyHex");
    }


    if (!encryptKeys)
        status = trustedSetEncryptedDkgPoly(eid, &errStatus, errMsg1.data(), encrDKGPoly.data());
    else
        status = trustedSetEncryptedDkgPolyAES(eid, &errStatus, errMsg1.data(), encrDKGPoly.data(), &encLen);

    if (status != SGX_SUCCESS || errStatus != 0) {
        throw SGXException(-666, errMsg1.data());
    }

    string result;


    for (int i = 0; i < _n; i++) {
        vector<uint8_t> encryptedSkey(BUF_LEN, 0);
        uint32_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);


        if (!encryptKeys)
            trustedGetEncryptedSecretShare(eid, &errStatus, errMsg1.data(), encryptedSkey.data(), &decLen,
                                           currentShare.data(), sShareG2.data(), pubKeyB.data(), _t, _n, i + 1);
        else
            trustedGetEncryptedSecretShareAES(eid, &errStatus, errMsg1.data(), encryptedSkey.data(), &decLen,
                                               currentShare.data(), sShareG2.data(), pubKeyB.data(), _t, _n, i + 1);
        if (errStatus != 0) {
            throw SGXException(-666, errMsg1.data());
        }

        spdlog::debug("cur_share is {}", currentShare.data());

        result += string(currentShare.data());

        spdlog::debug("dec len is {}", decLen);
        carray2Hex(encryptedSkey.data(), decLen, hexEncrKey.data());
        string dhKeyName = "DKG_DH_KEY_" + _polyName + "_" + to_string(i) + ":";

        spdlog::debug("hexEncr DH Key: { }", hexEncrKey.data());
        SGXWalletServer::writeDataToDB(dhKeyName, hexEncrKey.data());

        string shareG2_name = "shareG2_" + _polyName + "_" + to_string(i) + ":";
        spdlog::debug("name to write to db is {}", dhKeyName);
        spdlog::debug("name to write to db is {}", shareG2_name);
        spdlog::debug("s_shareG2: {}", sShareG2.data());

        SGXWalletServer::writeDataToDB(shareG2_name, sShareG2.data());

        spdlog::debug("errMsg: {}", errMsg1.data());

    }

    return result;
}

bool
verifyShares(const char *publicShares, const char *encr_sshare, const char *encryptedKeyHex, int t, int n, int ind) {
    char errMsg[BUF_LEN];
    int errStatus = 0;

    uint64_t decKeyLen;
    uint8_t encr_key[BUF_LEN];
    memset(encr_key, 0, BUF_LEN);
    if (!hex2carray(encryptedKeyHex, &decKeyLen, encr_key)) {
        throw SGXException(INVALID_HEX, "Invalid encryptedPolyHex");
    }
    int result;

    spdlog::debug("publicShares length is {}", char_traits<char>::length(publicShares));

    char pshares[8193];
    memset(pshares, 0, 8193);
    strncpy(pshares, publicShares, strlen(publicShares));


    if (!encryptKeys)
        trustedDkgVerify(eid, &errStatus, errMsg, pshares, encr_sshare, encr_key, decKeyLen, t, ind, &result);
    else
        trustedDkgVerifyAES(eid, &errStatus, errMsg, pshares, encr_sshare, encr_key, decKeyLen, t, ind, &result);

    if (result == 2) {
        throw SGXException(INVALID_HEX, "Invalid public shares");
    }

    spdlog::debug("errMsg1: {}", errMsg);
    spdlog::debug("result is: {}", result);

    return result;
}

bool CreateBLSShare(const string &blsKeyName, const char *s_shares, const char *encryptedKeyHex) {

    spdlog::debug("ENTER CreateBLSShare");

    // char* errMsg1 = (char*) calloc(1024,1);
    char errMsg[BUF_LEN];
    int errStatus = 0;

    uint64_t decKeyLen;
    uint8_t encr_bls_key[BUF_LEN];
    memset(encr_bls_key, 0, BUF_LEN);
    uint8_t encr_key[BUF_LEN];
    memset(encr_key, 0, BUF_LEN);
    if (!hex2carray(encryptedKeyHex, &decKeyLen, encr_key)) {
        throw SGXException(INVALID_HEX, "Invalid encryptedKeyHex");
    }

    uint32_t enc_bls_len = 0;


    if (!encryptKeys)
        trustedCreateBlsKey(eid, &errStatus, errMsg, s_shares, encr_key, decKeyLen, encr_bls_key, &enc_bls_len);
    else
        trustedCreateBlsKeyAES(eid, &errStatus, errMsg, s_shares, encr_key, decKeyLen, encr_bls_key, &enc_bls_len);

    if (errStatus != 0) {

        spdlog::error(errMsg);
        spdlog::error("status {}", errStatus);
        throw SGXException(ERROR_IN_ENCLAVE, "Create BLS private key failed in enclave");
    } else {

        char hexBLSKey[2 * BUF_LEN];


        carray2Hex(encr_bls_key, enc_bls_len, hexBLSKey);

        SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey);

        return true;
    }

}

vector<string> GetBLSPubKey(const char *encryptedKeyHex) {

    char errMsg1[BUF_LEN];

    int errStatus = 0;

    uint64_t decKeyLen;
    uint8_t encrKey[BUF_LEN];
    if (!hex2carray(encryptedKeyHex, &decKeyLen, encrKey)) {
        throw SGXException(INVALID_HEX, "Invalid encryptedKeyHex");
    }

    char pubKey[320];
    spdlog::debug("decKeyLen is {}", decKeyLen);

    if (!encryptKeys)
        trustedGetBlsPubKey(eid, &errStatus, errMsg1, encrKey, decKeyLen, pubKey);
    else
        trustedGetBlsPubKeyAES(eid, &errStatus, errMsg1, encrKey, decKeyLen, pubKey);
    if (errStatus != 0) {
        spdlog::error(string(errMsg1) + " . Status is  {}", errStatus);
        throw SGXException(ERROR_IN_ENCLAVE, "Failed to get BLS public key in enclave");
    }
    vector<string> pubKeyVect = splitString(pubKey, ':');

    spdlog::debug("errMsg1 is {}", errMsg1);
    spdlog::debug("pub key is ");
    for (int i = 0; i < 4; i++)
        spdlog::debug("{}", pubKeyVect.at(i));

    return pubKeyVect;
}

string decryptDHKey(const string &polyName, int ind) {

    vector<char> errMsg1(1024, 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);

    vector<char> hexEncrKey(2 * BUF_LEN, 0);

    uint64_t dhEncLen = 0;
    uint8_t encryptedDHKey[BUF_LEN];
    if (!hex2carray(hexEncrKeyPtr->c_str(), &dhEncLen, encryptedDHKey)) {
        throw SGXException(INVALID_HEX, "Invalid hexEncrKey");
    }
    spdlog::debug("encr DH key length is {}", dhEncLen);
    spdlog::debug("hex encr DH key length is {}", hexEncrKeyPtr->length());


    char DHKey[ECDSA_SKEY_LEN];

    if (!encryptKeys)
        trustedDecryptKey(eid, &errStatus, errMsg1.data(), encryptedDHKey, dhEncLen, DHKey);
    else
        trustedDecryptKeyAES(eid, &errStatus, errMsg1.data(), encryptedDHKey, dhEncLen, DHKey);
    if (errStatus != 0) {
        throw SGXException(/*ERROR_IN_ENCLAVE*/ errStatus, "decrypt key failed in enclave");
    }

    return DHKey;
}

vector<string> mult_G2(const string &x) {
    vector<string> result(4);
    libff::init_alt_bn128_params();
    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;
}