-
Notifications
You must be signed in to change notification settings - Fork 1
/
ecwrapper.cpp
executable file
·292 lines (269 loc) · 7.59 KB
/
ecwrapper.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
// Copyright (c) 2009-2014 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "ecwrapper.h"
#include "serialize.h"
#include "uint256.h"
#include <openssl/ecdsa.h>
#include <openssl/obj_mac.h>
#include "bignum.h"
namespace
{
/**
* Perform ECDSA key recovery (see SEC1 4.1.6) for curves over (mod p)-fields
* recid selects which key is recovered
* if check is non-zero, additional checks are performed
*/
int ECDSA_SIG_recover_key_GFp(EC_KEY* eckey, ECDSA_SIG* ecsig, const unsigned char* msg, int msglen, int recid, int check)
{
if (!eckey) return 0;
int ret = 0;
BN_CTX* ctx = NULL;
BIGNUM* x = NULL;
BIGNUM* e = NULL;
BIGNUM* order = NULL;
BIGNUM* sor = NULL;
BIGNUM* eor = NULL;
BIGNUM* field = NULL;
EC_POINT* R = NULL;
EC_POINT* O = NULL;
EC_POINT* Q = NULL;
BIGNUM* rr = NULL;
BIGNUM* zero = NULL;
int n = 0;
int i = recid / 2;
#if OPENSSL_VERSION_NUMER >= 0x10100000L
const BIGNUM *sig_r, *sig_s;
ECDSA_SIG_get0(ecsig, &sig_r, &sig_s);
#endif
const EC_GROUP* group = EC_KEY_get0_group(eckey);
if ((ctx = BN_CTX_new()) == NULL) {
ret = -1;
goto err;
}
BN_CTX_start(ctx);
order = BN_CTX_get(ctx);
if (!EC_GROUP_get_order(group, order, ctx)) {
ret = -2;
goto err;
}
x = BN_CTX_get(ctx);
if (!BN_copy(x, order)) {
ret = -1;
goto err;
}
if (!BN_mul_word(x, i)) {
ret = -1;
goto err;
}
#if OPENSSL_VERSION_NUMER >= 0x10100000L
if (!BN_add(x, x, sig_r)) {
#else
if (!BN_add(x, x, ecsig->r)) {
#endif
ret = -1;
goto err;
}
field = BN_CTX_get(ctx);
if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) {
ret = -2;
goto err;
}
if (BN_cmp(x, field) >= 0) {
ret = 0;
goto err;
}
if ((R = EC_POINT_new(group)) == NULL) {
ret = -2;
goto err;
}
if (!EC_POINT_set_compressed_coordinates_GFp(group, R, x, recid % 2, ctx)) {
ret = 0;
goto err;
}
if (check) {
if ((O = EC_POINT_new(group)) == NULL) {
ret = -2;
goto err;
}
if (!EC_POINT_mul(group, O, NULL, R, order, ctx)) {
ret = -2;
goto err;
}
if (!EC_POINT_is_at_infinity(group, O)) {
ret = 0;
goto err;
}
}
if ((Q = EC_POINT_new(group)) == NULL) {
ret = -2;
goto err;
}
n = EC_GROUP_get_degree(group);
e = BN_CTX_get(ctx);
if (!BN_bin2bn(msg, msglen, e)) {
ret = -1;
goto err;
}
if (8 * msglen > n) BN_rshift(e, e, 8 - (n & 7));
zero = BN_CTX_get(ctx);
if (!BN_zero(zero)) {
ret = -1;
goto err;
}
if (!BN_mod_sub(e, zero, e, order, ctx)) {
ret = -1;
goto err;
}
rr = BN_CTX_get(ctx);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (!BN_mod_inverse(rr, sig_r, order, ctx)) {
#else
if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) {
#endif
ret = -1;
goto err;
}
sor = BN_CTX_get(ctx);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (!BN_mod_mul(sor, sig_r, rr, order, ctx)) {
#else
if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) {
#endif
ret = -1;
goto err;
}
eor = BN_CTX_get(ctx);
if (!BN_mod_mul(eor, e, rr, order, ctx)) {
ret = -1;
goto err;
}
if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) {
ret = -2;
goto err;
}
if (!EC_KEY_set_public_key(eckey, Q)) {
ret = -2;
goto err;
}
ret = 1;
err:
if (ctx) {
BN_CTX_end(ctx);
BN_CTX_free(ctx);
}
if (R != NULL) EC_POINT_free(R);
if (O != NULL) EC_POINT_free(O);
if (Q != NULL) EC_POINT_free(Q);
return ret;
}
} // anon namespace
CECKey::CECKey()
{
pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
assert(pkey != NULL);
}
CECKey::~CECKey()
{
EC_KEY_free(pkey);
}
void CECKey::GetPubKey(std::vector<unsigned char>& pubkey, bool fCompressed)
{
EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
int nSize = i2o_ECPublicKey(pkey, NULL);
assert(nSize);
assert(nSize <= 65);
pubkey.clear();
pubkey.resize(nSize);
unsigned char* pbegin(begin_ptr(pubkey));
int nSize2 = i2o_ECPublicKey(pkey, &pbegin);
assert(nSize == nSize2);
}
bool CECKey::SetPubKey(const unsigned char* pubkey, size_t size)
{
return o2i_ECPublicKey(&pkey, &pubkey, size) != NULL;
}
bool CECKey::Verify(const uint256& hash, const std::vector<unsigned char>& vchSig)
{
if (vchSig.empty())
return false;
// New versions of OpenSSL will reject non-canonical DER signatures. de/re-serialize first.
unsigned char* norm_der = NULL;
ECDSA_SIG* norm_sig = ECDSA_SIG_new();
const unsigned char* sigptr = &vchSig[0];
assert(norm_sig);
if (d2i_ECDSA_SIG(&norm_sig, &sigptr, vchSig.size()) == NULL) {
/* As of OpenSSL 1.0.0p d2i_ECDSA_SIG frees and nulls the pointer on
* error. But OpenSSL's own use of this function redundantly frees the
* result. As ECDSA_SIG_free(NULL) is a no-op, and in the absence of a
* clear contract for the function behaving the same way is more
* conservative.
*/
ECDSA_SIG_free(norm_sig);
return false;
}
int derlen = i2d_ECDSA_SIG(norm_sig, &norm_der);
ECDSA_SIG_free(norm_sig);
if (derlen <= 0)
return false;
// -1 = error, 0 = bad sig, 1 = good
bool ret = ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), norm_der, derlen, pkey) == 1;
OPENSSL_free(norm_der);
return ret;
}
bool CECKey::Recover(const uint256& hash, const unsigned char* p64, int rec)
{
if (rec < 0 || rec >= 3)
return false;
ECDSA_SIG* sig = ECDSA_SIG_new();
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
BIGNUM *sig_r = NULL;
BIGNUM *sig_s = NULL;
if (!(sig_r = BN_bin2bn(&p64[0], 32, nullptr)) ||
!(sig_s = BN_bin2bn(&p64[32], 32, nullptr)) ||
!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
BN_free(sig_r);
BN_free(sig_s);
return false;
}
#else
BN_bin2bn(&p64[0], 32, sig->r);
BN_bin2bn(&p64[32], 32, sig->s);
#endif
bool ret = ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), rec, 0) == 1;
ECDSA_SIG_free(sig);
return ret;
}
bool CECKey::TweakPublic(const unsigned char vchTweak[32])
{
bool ret = true;
BN_CTX* ctx = BN_CTX_new();
BN_CTX_start(ctx);
BIGNUM* bnTweak = BN_CTX_get(ctx);
BIGNUM* bnOrder = BN_CTX_get(ctx);
BIGNUM* bnOne = BN_CTX_get(ctx);
const EC_GROUP* group = EC_KEY_get0_group(pkey);
EC_GROUP_get_order(group, bnOrder, ctx); // what a grossly inefficient way to get the (constant) group order...
BN_bin2bn(vchTweak, 32, bnTweak);
if (BN_cmp(bnTweak, bnOrder) >= 0)
ret = false; // extremely unlikely
EC_POINT* point = EC_POINT_dup(EC_KEY_get0_public_key(pkey), group);
BN_one(bnOne);
EC_POINT_mul(group, point, bnTweak, point, bnOne, ctx);
if (EC_POINT_is_at_infinity(group, point))
ret = false; // ridiculously unlikely
EC_KEY_set_public_key(pkey, point);
EC_POINT_free(point);
BN_CTX_end(ctx);
BN_CTX_free(ctx);
return ret;
}
bool CECKey::SanityCheck()
{
EC_KEY* pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
if (pkey == NULL)
return false;
EC_KEY_free(pkey);
// TODO Is there more EC functionality that could be missing?
return true;
}