BLSCrypto.cpp 5.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
    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 BLSCrypto.cpp
    @author Stan Kladko
    @date 2019
*/

24
#include <memory>
25 26 27 28 29 30


#include "libff/algebra/curves/alt_bn128/alt_bn128_init.hpp"

#include "bls.h"

31 32

#include "leveldb/db.h"
33
#include <jsonrpccpp/server/connectors/httpserver.h>
34
#include "BLSPrivateKeyShareSGX.h"
35

36

37 38 39 40 41 42
#include "sgxwallet_common.h"
#include "create_enclave.h"
#include "secure_enclave_u.h"
#include "sgx_detect.h"
#include <gmp.h>
#include <sgx_urts.h>
43

44
#include "sgxwallet.h"
45

46 47
#include "SGXWalletServer.h"

48
#include "BLSCrypto.h"
kladkogex's avatar
kladkogex committed
49
#include "ServerInit.h"
50

51 52
#include "RPCException.h"

53
#include "spdlog/spdlog.h"
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
int char2int(char _input) {
  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;
}



void  carray2Hex(const unsigned char *d, int _len, char* _hexArray) {

  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;

}


bool hex2carray(const char * _hex, uint64_t  *_bin_len,
                uint8_t* _bin ) {

  int len = strnlen(_hex, 2 * BUF_LEN);


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

  *_bin_len = len / 2;

  for (int i = 0; i < len / 2; i++) {
    int high = char2int((char)_hex[i * 2]);
    int low = char2int((char)_hex[i * 2 + 1]);

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

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

  return true;

}

108 109
bool hex2carray2(const char * _hex, uint64_t  *_bin_len,
                uint8_t* _bin, const int _max_length ) {
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    int len = strnlen(_hex, _max_length);//2 * BUF_LEN);


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

    *_bin_len = len / 2;

    for (int i = 0; i < len / 2; i++) {
        int high = char2int((char)_hex[i * 2]);
        int low = char2int((char)_hex[i * 2 + 1]);

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

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

    return true;

}
133 134


kladkogex's avatar
kladkogex committed
135
bool sign(const char* _encryptedKeyHex, const char* _hashHex, size_t _t, size_t _n, size_t _signerIndex,
136 137
    char* _sig) {

138
  //std::cerr << "ENTER SIGN" << std::endl;
139 140 141 142 143 144 145

  auto keyStr = std::make_shared<std::string>(_encryptedKeyHex);

  auto hash = std::make_shared<std::array<uint8_t, 32>>();

  uint64_t binLen;

146 147 148
  if (!hex2carray(_hashHex, &binLen, hash->data())){
      throw RPCException(INVALID_HEX, "Invalid hash");
  }
149
 // assert(binLen == hash->size());
150

151 152


153
  auto keyShare = std::make_shared<BLSPrivateKeyShareSGX>(keyStr, _t, _n);
154

155
  //std::cerr << "keyShare created" << std::endl;
156 157 158
 // {
    auto sigShare = keyShare->signWithHelperSGX(hash, _signerIndex);
 // }
159

kladkogex's avatar
kladkogex committed
160 161 162 163
  auto sigShareStr = sigShare->toString();

  strncpy(_sig, sigShareStr->c_str(), BUF_LEN);

164 165 166 167 168 169 170
  //std::string sigShareStr = keyShare->signWithHelperSGXstr(hash, _signerIndex);
  //strncpy(_sig, sigShareStr.c_str(), BUF_LEN);

 // std::string test_sig = "8175162913343900215959836578795929492705714455632345516427532159927644835012:15265825550804683171644566522808807137117748565649051208189914766494241035855:9810286616503120081238481858289626967170509983220853777870754480048381194141:5";
 // auto sig_ptr = std::make_shared<std::string>(test_sig);
 // strncpy(_sig, sig_ptr->c_str(), BUF_LEN);

171
  //std::cerr<< "sig " << _sig <<std::endl;
172

173 174 175 176
  return true;

}

kladkogex's avatar
kladkogex committed
177 178 179 180 181 182 183 184 185 186 187 188 189

char *encryptBLSKeyShare2Hex(int *errStatus, char *err_string, const char *_key) {
    char *keyArray = (char *) calloc(BUF_LEN, 1);
    uint8_t *encryptedKey = (uint8_t *) calloc(BUF_LEN, 1);
    char *errMsg = (char *) calloc(BUF_LEN, 1);
    strncpy((char *) keyArray, (char *) _key, BUF_LEN);

    *errStatus = -1;

    unsigned int encryptedLen = 0;

    status = encrypt_key(eid, errStatus, errMsg, keyArray, encryptedKey, &encryptedLen);

190 191 192
    if (DEBUG_PRINT) {
      spdlog::info("errStatus is {}",*errStatus, " errMsg is ", errMsg );
    }
193

kladkogex's avatar
kladkogex committed
194
    if (status != SGX_SUCCESS) {
195

kladkogex's avatar
kladkogex committed
196 197 198 199 200
        *errStatus = -1;
        return nullptr;
    }

    if (*errStatus != 0) {
201
        throw RPCException(-666, errMsg);
kladkogex's avatar
kladkogex committed
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
    }


    char *result = (char *) calloc(2 * BUF_LEN, 1);

    carray2Hex(encryptedKey, encryptedLen, result);

    return result;
}

char *decryptBLSKeyShareFromHex(int *errStatus, char *errMsg, const char *_encryptedKey) {


    *errStatus = -1;

    uint64_t decodedLen = 0;

    uint8_t decoded[BUF_LEN];

    if (!(hex2carray(_encryptedKey, &decodedLen, decoded))) {
        return nullptr;
    }

    char *plaintextKey = (char *) calloc(BUF_LEN, 1);

    status = decrypt_key(eid, errStatus, errMsg, decoded, decodedLen, plaintextKey);

    if (status != SGX_SUCCESS) {
        return nullptr;
    }

    if (*errStatus != 0) {
        return nullptr;
    }

    return plaintextKey;

}