forked from unboundsecurity/blockchain-crypto-mpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpc_crypto_context.cpp
174 lines (141 loc) · 4.76 KB
/
mpc_crypto_context.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
/*
* 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 "crypto.h"
#include "mpc_crypto_context.h"
error_t mpc_crypto_context_t::get_share(mpc_crypto_share_t*& share) const
{
if (!is_finished()) return ub::error(E_NOT_READY);
share = create_share();
get_share_core(*share);
share->set_uid(share->calc_uid());
return 0;
}
error_t mpc_crypto_context_t::set_share(const mpc_crypto_share_t& share)
{
if (share.get_type()!=get_share_type()) return ub::error(E_BADARG);
set_share_core(share);
return 0;
}
MPCCRYPTO_API void MPCCrypto_freeContext(MPCCryptoContext* context)
{
delete (mpc_crypto_context_t*)context;
}
MPCCRYPTO_API int MPCCrypto_contextToBuf(MPCCryptoContext* context, uint8_t* out, int* out_size)
{
ub::converter_t converter(out);
converter.convert(*(mpc_crypto_context_t*)context);
*out_size = converter.get_size();
return 0;
}
MPCCRYPTO_API int MPCCrypto_contextFromBuf(const uint8_t* in, int in_size, MPCCryptoContext** out_context)
{
ub::convertable_t* obj = ub::convertable_t::factory_t::create(ub::mem_t(in, in_size));
if (!obj) return ub::error(E_FORMAT);
mpc_crypto_context_t* context = dynamic_cast<mpc_crypto_context_t*>(obj);
if (!context)
{
delete obj;
return ub::error(E_FORMAT);
}
*out_context = (MPCCryptoContext*)obj;
return 0;
}
MPCCRYPTO_API int MPCCrypto_contextInfo(MPCCryptoContext* context, mpc_crypto_context_info_t* info)
{
error_t rv = 0;
if (!context || !info) return rv = ub::error(E_BADARG);
((mpc_crypto_context_t*)context)->get_info(*info);
return 0;
}
MPCCRYPTO_API int MPCCrypto_getShare(MPCCryptoContext* context_ptr, MPCCryptoShare** out_share)
{
error_t rv = 0;
if (!context_ptr) return rv = ub::error(E_BADARG);
mpc_crypto_context_t* context = (mpc_crypto_context_t*)context_ptr;
mpc_crypto_share_t* share = nullptr;
if (rv = context->get_share(share)) return rv;
*out_share = (MPCCryptoShare*)share;
return 0;
}
MPCCRYPTO_API int MPCCrypto_step(MPCCryptoContext* ctx, MPCCryptoMessage* in, MPCCryptoMessage** out, unsigned* out_flags)
{
error_t rv = 0;
mpc_crypto_context_t* context = (mpc_crypto_context_t*)ctx;
mpc_crypto_message_t* message_in = (mpc_crypto_message_t*)in;
mpc_crypto_message_t* message_out = new mpc_crypto_message_t();
*out_flags = 0;
rv = context->step(*message_in, *message_out, *out_flags);
if (rv)
{
delete message_out;
return rv;
}
if (message_out->is_empty())
{
delete message_out;
message_out = nullptr;
}
*out = (MPCCryptoMessage*)message_out;
return 0;
}
mpc_crypto_context_t::mpc_crypto_context_t() :
peer(0),
uid(crypto::gen_random_int<uint64_t>()),
share_uid(0),
current_step(0)
{
}
void mpc_crypto_context_t::convert(ub::converter_t& converter)
{
converter.convert_code_type(CODE_TYPE);
converter.convert(uid);
converter.convert(share_uid);
converter.convert(peer);
converter.convert(current_step);
}
void mpc_crypto_context_t::get_info(mpc_crypto_context_info_t& info) const
{
info.peer = peer;
info.uid = uid;
info.share_uid = share_uid;
}
// --------------------------------------------- refresh ---------------------------------------------
MPCCRYPTO_API int MPCCrypto_initRefreshKey(int peer, MPCCryptoShare* share_ptr, MPCCryptoContext** context)
{
error_t rv = 0;
if (!share_ptr) return rv = ub::error(E_BADARG);
mpc_crypto_share_t* share = (mpc_crypto_share_t*)share_ptr;
mpc_crypto_context_t* refresh_oper = share->create_refresh_oper();
if (!refresh_oper) return rv = ub::error(E_BADARG);
refresh_oper->set_peer(peer);
refresh_oper->set_share_uid(share->get_uid());
rv = refresh_oper->set_share(*share);
if (rv)
{
delete refresh_oper;
return rv;
}
*context = (MPCCryptoContext*)refresh_oper;
return 0;
}