forked from skalenetwork/sgxwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BLSCrypto.cpp
270 lines (176 loc) · 7.09 KB
/
BLSCrypto.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
/*
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
*/
#include "leveldb/db.h"
#include "libff/algebra/curves/alt_bn128/alt_bn128_init.hpp"
#include <jsonrpccpp/server/connectors/httpserver.h>
#include <memory>
#include "third_party/intel/create_enclave.h"
#include <tools/utils.h>
#include "SGXException.h"
#include "SGXWalletServer.hpp"
#include "common.h"
#include "sgxwallet.h"
#include "sgxwallet_common.h"
#include "third_party/spdlog/spdlog.h"
#include "BLSCrypto.h"
#include "CryptoTools.h"
#include "LevelDB.h"
#include "SEKManager.h"
#include "ServerInit.h"
shared_ptr<string> FqToString(libff::alt_bn128_Fq *_fq) {
CHECK_STATE(_fq);
mpz_t t;
mpz_init(t);
_fq->as_bigint().to_mpz(t);
SAFE_CHAR_BUF(arr, mpz_sizeinbase(t, 10) + 2);
mpz_get_str(arr, 10, t);
mpz_clear(t);
return make_shared<string>(string(arr));
}
bool sign_aes(const char *_encryptedKeyHex, const char *_hashHex, size_t _t,
size_t _n, char *_sig) {
CHECK_STATE(_encryptedKeyHex);
CHECK_STATE(_hashHex);
CHECK_STATE(_sig);
auto hash = make_shared<array<uint8_t, 32>>();
uint64_t binLen;
if (!hex2carray(_hashHex, &binLen, hash->data(), hash->size())) {
throw SGXException(SIGN_AES_INVALID_HASH,
string(__FUNCTION__) + ":Invalid hash");
}
shared_ptr<libBLS::Bls> obj;
obj = make_shared<libBLS::Bls>(libBLS::Bls(_t, _n));
pair<libff::alt_bn128_G1, string> hash_with_hint =
obj->HashtoG1withHint(hash);
shared_ptr<string> xStr = FqToString(&(hash_with_hint.first.X));
CHECK_STATE(xStr);
shared_ptr<string> yStr = FqToString(&(hash_with_hint.first.Y));
CHECK_STATE(yStr);
vector<char> errMsg(BUF_LEN, 0);
SAFE_CHAR_BUF(xStrArg, BUF_LEN);
SAFE_CHAR_BUF(yStrArg, BUF_LEN);
SAFE_CHAR_BUF(signature, BUF_LEN);
strncpy(xStrArg, xStr->c_str(), BUF_LEN);
strncpy(yStrArg, yStr->c_str(), BUF_LEN);
size_t sz = 0;
SAFE_UINT8_BUF(encryptedKey, BUF_LEN);
bool result = hex2carray(_encryptedKeyHex, &sz, encryptedKey, BUF_LEN);
if (!result) {
BOOST_THROW_EXCEPTION(invalid_argument("Invalid hex encrypted key"));
}
int errStatus = 0;
sgx_status_t status = SGX_SUCCESS;
status = trustedBlsSignMessage(eid, &errStatus, errMsg.data(), encryptedKey,
sz, xStrArg, yStrArg, signature);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
string hint =
libBLS::ThresholdUtils::fieldElementToString(hash_with_hint.first.Y) +
":" + hash_with_hint.second;
string sig = signature;
sig.append(":");
sig.append(hint);
strncpy(_sig, sig.c_str(), BUF_LEN);
return true;
}
bool bls_sign(const char *_encryptedKeyHex, const char *_hashHex, size_t _t,
size_t _n, char *_sig) {
CHECK_STATE(_encryptedKeyHex);
CHECK_STATE(_hashHex);
return sign_aes(_encryptedKeyHex, _hashHex, _t, _n, _sig);
}
bool popProveSGX(const char *encryptedKeyHex, char *prove) {
CHECK_STATE(encryptedKeyHex);
SAFE_UINT8_BUF(encryptedKey, BUF_LEN);
size_t sz = 0;
if (!hex2carray(encryptedKeyHex, &sz, encryptedKey, BUF_LEN)) {
BOOST_THROW_EXCEPTION(invalid_argument("Invalid hex encrypted key"));
}
sgx_status_t status = SGX_SUCCESS;
vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;
SAFE_CHAR_BUF(pubKey, 320)
status = trustedGetBlsPubKey(eid, &errStatus, errMsg.data(), encryptedKey, sz,
pubKey);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
vector<string> pubKeyVect = splitString(pubKey, ':');
spdlog::debug("pub key is ");
for (int i = 0; i < 4; i++)
spdlog::debug("{}", pubKeyVect.at(i));
libff::alt_bn128_G2 publicKey;
publicKey.Z = libff::alt_bn128_Fq2::one();
publicKey.X.c0 = libff::alt_bn128_Fq(pubKeyVect[0].c_str());
publicKey.X.c1 = libff::alt_bn128_Fq(pubKeyVect[1].c_str());
publicKey.Y.c0 = libff::alt_bn128_Fq(pubKeyVect[2].c_str());
publicKey.Y.c1 = libff::alt_bn128_Fq(pubKeyVect[3].c_str());
pair<libff::alt_bn128_G1, string> hashPublicKeyWithHint =
libBLS::Bls::HashPublicKeyToG1WithHint(publicKey);
hashPublicKeyWithHint.first.to_affine_coordinates();
shared_ptr<string> xStr = FqToString(&(hashPublicKeyWithHint.first.X));
CHECK_STATE(xStr);
shared_ptr<string> yStr = FqToString(&(hashPublicKeyWithHint.first.Y));
CHECK_STATE(yStr);
SAFE_CHAR_BUF(xStrArg, BUF_LEN);
SAFE_CHAR_BUF(yStrArg, BUF_LEN);
strncpy(xStrArg, xStr->c_str(), BUF_LEN);
strncpy(yStrArg, yStr->c_str(), BUF_LEN);
errStatus = 0;
status = trustedBlsSignMessage(eid, &errStatus, errMsg.data(), encryptedKey,
sz, xStrArg, yStrArg, prove);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
string hint = libBLS::ThresholdUtils::fieldElementToString(
hashPublicKeyWithHint.first.Y) +
":" + hashPublicKeyWithHint.second;
string _prove = prove;
_prove.append(":");
_prove.append(hint);
strncpy(prove, _prove.c_str(), BUF_LEN);
return true;
}
bool generateBLSPrivateKeyAggegated(const char *blsKeyName) {
CHECK_STATE(blsKeyName);
vector<char> errMsg(BUF_LEN, 0);
int errStatus = 0;
int exportable = 0;
uint64_t encBlsLen = 0;
sgx_status_t status = SGX_SUCCESS;
SAFE_UINT8_BUF(encrBlsKey, BUF_LEN)
status = trustedGenerateBLSKey(eid, &errStatus, errMsg.data(), &exportable,
encrBlsKey, &encBlsLen);
HANDLE_TRUSTED_FUNCTION_ERROR(status, errStatus, errMsg.data());
vector<char> hexBLSKey = carray2Hex(encrBlsKey, encBlsLen);
SGXWalletServer::writeDataToDB(blsKeyName, hexBLSKey.data());
return true;
}
string encryptBLSKeyShare2Hex(int *errStatus, char *err_string,
const char *_key) {
CHECK_STATE(errStatus);
CHECK_STATE(err_string);
CHECK_STATE(_key);
auto keyArray = make_shared<vector<char>>(BUF_LEN, 0);
auto encryptedKey = make_shared<vector<uint8_t>>(BUF_LEN, 0);
vector<char> errMsg(BUF_LEN, 0);
strncpy(keyArray->data(), _key, BUF_LEN);
*errStatus = 0;
uint64_t encryptedLen = 0;
sgx_status_t status = SGX_SUCCESS;
status = trustedEncryptKey(eid, errStatus, errMsg.data(), keyArray->data(),
encryptedKey->data(), &encryptedLen);
HANDLE_TRUSTED_FUNCTION_ERROR(status, *errStatus, errMsg.data());
vector<char> resultBuf = carray2Hex(encryptedKey->data(), encryptedLen);
return string(resultBuf.begin(), resultBuf.end());
}