forked from unboundsecurity/blockchain-crypto-mpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpc_crypto_eddsa.cpp
340 lines (276 loc) · 8.95 KB
/
mpc_crypto_eddsa.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
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
/*
* NOTICE
*
* The blockchain-crypto-mpc software is licensed under a proprietary license or the GPL v.3.
* If you choose to receive it under the GPL v.3 license, the following applies:
* Blockchain-crypto-mpc is a Multiparty Computation (MPC)-based cryptographic library for securing blockchain wallets and applications.
*
* Copyright (C) 2018, Unbound Tech Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "mpc_crypto_eddsa.h"
// --------------------------------------- mpc_eddsa_share_t ----------------------------------------------
mpc_eddsa_share_t::mpc_eddsa_share_t()
{
}
void mpc_eddsa_share_t::convert(ub::converter_t& converter)
{
converter.convert_code_type(CODE_TYPE);
converter.convert(core);
mpc_crypto_share_t::convert(converter);
}
// ----------------------------------------- refresh -----------------------------------
mpc_crypto_context_t* mpc_eddsa_share_t::create_refresh_oper()
{
return new mpc_eddsa_refresh_t;
}
void mpc_eddsa_refresh_t::convert(ub::converter_t& converter)
{
converter.convert_code_type(CODE_TYPE);
converter.convert(agree_random);
converter.convert(share);
mpc_crypto_context_t::convert(converter);
}
error_t mpc_eddsa_refresh_t::party1_step1(message1_t& out)
{
agree_random.peer1_step1(out);
return 0;
}
error_t mpc_eddsa_refresh_t::party2_step1(const message1_t& in, message2_t& out)
{
agree_random.peer2_step1(in, out);
return 0;
}
error_t mpc_eddsa_refresh_t::party1_step2(const message2_t& in, message3_t& out)
{
buf_t agree_buf;
agree_random.peer1_step2(in, out, agree_buf);
bool add = (agree_buf[64*3] & 1) == 0;
mem_t refresh_data = mem_t(agree_buf.data(), 64*3);
share.refresh(add, refresh_data);
return 0;
}
error_t mpc_eddsa_refresh_t::party2_step2(const message3_t& in, none_message_t& out)
{
buf_t agree_buf;
agree_random.peer2_step2(in, agree_buf);
bool add = (agree_buf[64*3] & 1) != 0;
mem_t refresh_data = mem_t(agree_buf.data(), 64*3);
share.refresh(add, refresh_data);
return 0;
}
// --------------------------------------- mpc_eddsa_gen_t ----------------------------------------------
mpc_eddsa_gen_t::mpc_eddsa_gen_t() : agree_random(32)
{
}
void mpc_eddsa_gen_t::convert(ub::converter_t& converter)
{
converter.convert_code_type(CODE_TYPE);
converter.convert(agree_random);
converter.convert(ctx);
converter.convert(share);
mpc_crypto_context_t::convert(converter);
}
error_t mpc_eddsa_gen_t::party1_step1(message1_t& out)
{
error_t rv = 0;
agree_random.peer1_step1(out);
return rv;
}
error_t mpc_eddsa_gen_t::party2_step1(const message1_t& in, message2_t& out)
{
error_t rv = 0;
if (rv = agree_random.peer2_step1(in, out)) return rv;
return rv;
}
error_t mpc_eddsa_gen_t::party1_step2(const message2_t& in, message3_t& out)
{
error_t rv = 0;
buf_t session_id;
if (rv = agree_random.peer1_step2(in, out.agree_msg3, session_id)) return rv;
if (rv = ctx.peer1_step1(session_id, share, out.gen_msg1)) return rv;
return rv;
}
error_t mpc_eddsa_gen_t::party2_step2(const message3_t& in, message4_t& out)
{
error_t rv = 0;
buf_t session_id;
if (rv = agree_random.peer2_step2(in.agree_msg3, session_id)) return rv;
if (rv = ctx.peer2_step1(session_id, share, in.gen_msg1, out)) return rv;
return rv;
}
error_t mpc_eddsa_gen_t::party1_step3(const message4_t& in, message5_t& out)
{
return ctx.peer1_step2(share, in, out);
}
error_t mpc_eddsa_gen_t::party2_step3(const message5_t& in, message6_t& out)
{
return ctx.peer2_step2(share, in, out);
}
error_t mpc_eddsa_gen_t::party1_step4(const message6_t& in, none_message_t& out)
{
return ctx.peer1_step3(share, in);
}
// --------------------------------------- mpc_eddsa_sign_t ----------------------------------------------
mpc_eddsa_sign_t::mpc_eddsa_sign_t() : refresh(false)
{
}
void mpc_eddsa_sign_t::convert(ub::converter_t& converter)
{
converter.convert_code_type(CODE_TYPE);
converter.convert(refresh);
converter.convert(ctx);
converter.convert(share);
converter.convert(result);
mpc_crypto_context_t::convert(converter);
}
error_t mpc_eddsa_sign_t::party1_step1(message1_t& out)
{
error_t rv = 0;
if (rv = ctx.peer1_step1(ctx.data_to_sign, true, share, out.sign_msg1)) return rv;
out.refresh = refresh;
out.data_to_sign = ctx.data_to_sign;
return rv;
}
error_t mpc_eddsa_sign_t::party2_step1(const message1_t& in, message2_t& out)
{
error_t rv = 0;
if (in.data_to_sign!=ctx.data_to_sign) return rv = ub::error(E_BADARG);
if (in.refresh!=refresh) return rv = ub::error(E_BADARG);
if (rv = ctx.peer2_step1(in.data_to_sign, true, share, in.sign_msg1, out)) return rv;
refresh = in.refresh;
return rv;
}
error_t mpc_eddsa_sign_t::party1_step2(const message2_t& in, message3_t& out)
{
error_t rv = 0;
if (rv = ctx.peer1_step2(share, in, out)) return rv;
return rv;
}
error_t mpc_eddsa_sign_t::party2_step2(const message3_t& in, message4_t& out)
{
error_t rv = 0;
if (rv = ctx.peer2_step2(share, in, out)) return rv;
return rv;
}
error_t mpc_eddsa_sign_t::party1_step3(const message4_t& in, message5_t& out)
{
error_t rv = 0;
if (rv = ctx.peer1_step3(share, in, out)) return rv;
return rv;
}
static buf_t calc_mgf(mem_t seed, int size)
{
buf_t out(size);
byte_ptr out_ptr = out.data();
int n = 0;
while (size>0)
{
sha256_t sha256; sha256.update(seed).update(n++);
if (size < 32)
{
byte_t hash[32];
sha256.final(hash);
memmove(out_ptr, hash, size);
break;
}
sha256.final(out_ptr);
out_ptr += 32;
size -= 32;
}
return out;
}
error_t mpc_eddsa_sign_t::party2_step3(const message5_t& in, message6_t& out)
{
error_t rv = 0;
if (rv = ctx.peer2_step3(share, in, out)) return rv;
if (refresh)
{
buf_t agree_buf = calc_mgf(ctx.session_id, 64*3+1);
mem_t refresh_data = mem_t(agree_buf.data(), 64*3);
bool add = (agree_buf[64*3] & 1) == 0;
share.refresh(add, refresh_data);
}
return rv;
}
error_t mpc_eddsa_sign_t::party1_step4(const message6_t& in, none_message_t& out)
{
error_t rv = 0;
if (rv = ctx.peer1_step4(share, in, result)) return rv;
if (refresh)
{
buf_t agree_buf = calc_mgf(ctx.session_id, 64*3+1);
mem_t refresh_data = mem_t(agree_buf.data(), 64*3);
bool add = (agree_buf[64*3] & 1) != 0;
share.refresh(add, refresh_data);
}
return rv;
}
// --------------------------- interface -------------------------------------------
MPCCRYPTO_API int MPCCrypto_getEddsaPublic(MPCCryptoShare* share_ptr, uint8_t* pub_key)
{
error_t rv = 0;
if (!share_ptr) return rv = ub::error(E_BADARG);
mpc_eddsa_share_t* share = dynamic_cast<mpc_eddsa_share_t*>((mpc_crypto_share_t*)share_ptr);
if (!share) return rv = ub::error(E_BADARG);
share->copy_pub_key(pub_key);
return 0;
}
MPCCRYPTO_API int MPCCrypto_initGenerateEddsaKey(int peer, MPCCryptoContext** context)
{
error_t rv = 0;
mpc_eddsa_gen_t* gen = new mpc_eddsa_gen_t();
gen->set_peer(peer);
*context = (MPCCryptoContext*)gen;
return 0;
}
MPCCRYPTO_API int MPCCrypto_initEddsaSign(int peer, MPCCryptoShare* share_ptr, const uint8_t* in, int in_size, int refresh, MPCCryptoContext** context)
{
error_t rv = 0;
if (!share_ptr) return rv = ub::error(E_BADARG);
mpc_eddsa_share_t* share = dynamic_cast<mpc_eddsa_share_t*>((mpc_crypto_share_t*)share_ptr);
if (!share) return rv = ub::error(E_BADARG);
mpc_eddsa_sign_t* sign = new mpc_eddsa_sign_t();
sign->set_peer(peer);
sign->set_data_to_sign(mem_t(in, in_size));
sign->set_share_uid(share->get_uid());
sign->set_refresh(refresh!=0);
if (rv = sign->set_share(*share))
{
delete sign;
return rv;
}
*context = (MPCCryptoContext*)sign;
return 0;
}
MPCCRYPTO_API int MPCCrypto_getResultEddsaSign(MPCCryptoContext* context, uint8_t* out) // 64 bytes length
{
error_t rv = 0;
if (!context) return rv = ub::error(E_BADARG);
mpc_eddsa_sign_t* ctx = dynamic_cast<mpc_eddsa_sign_t*>((mpc_crypto_context_t*)context);
if (!ctx) return rv = ub::error(E_BADARG);
ctx->copy_result(out);
return 0;
}
MPCCRYPTO_API int MPCCrypto_verifyEddsa(const uint8_t* pub_key, const uint8_t* in, int in_size, const uint8_t* signature) // |pub_key|=32, |signature|=64
{
error_t rv = 0;
crypto::eddsa_key_t key;
key.set_pub_key(pub_key);
bool ok = key.verify(mem_t(in, in_size), mem_t(signature, 64));
if (!ok) return rv = ub::error(E_CRYPTO);
return 0;
}