-
Notifications
You must be signed in to change notification settings - Fork 1
/
BRKey.c
404 lines (337 loc) · 13.6 KB
/
BRKey.c
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
// BRKey.c
//
// Created by Aaron Voisine on 8/19/15.
// Copyright (c) 2015 breadwallet LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "BRKey.h"
#include "BRAddress.h"
#include "BRBase58.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
#define BITCOIN_PRIVKEY 191
#define BITCOIN_PRIVKEY_TEST 253
#if __BIG_ENDIAN__ || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) ||\
__ARMEB__ || __THUMBEB__ || __AARCH64EB__ || __MIPSEB__
#define WORDS_BIGENDIAN 1
#endif
#define DETERMINISTIC 1
#define USE_BASIC_CONFIG 1
#define ENABLE_MODULE_RECOVERY 1
#pragma clang diagnostic push
#pragma GCC diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wconversion"
#pragma clang diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma clang diagnostic ignored "-Wconditional-uninitialized"
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#include "secp256k1/src/basic-config.h"
#include "secp256k1/src/secp256k1.c"
#pragma clang diagnostic pop
#pragma GCC diagnostic pop
static secp256k1_context *_ctx = NULL;
static pthread_once_t _ctx_once = PTHREAD_ONCE_INIT;
static void _ctx_init()
{
_ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
}
// adds 256bit big endian ints a and b (mod secp256k1 order) and stores the result in a
// returns true on success
int BRSecp256k1ModAdd(UInt256 *a, const UInt256 *b)
{
pthread_once(&_ctx_once, _ctx_init);
return secp256k1_ec_privkey_tweak_add(_ctx, (unsigned char *)a, (const unsigned char *)b);
}
// multiplies 256bit big endian ints a and b (mod secp256k1 order) and stores the result in a
// returns true on success
int BRSecp256k1ModMul(UInt256 *a, const UInt256 *b)
{
pthread_once(&_ctx_once, _ctx_init);
return secp256k1_ec_privkey_tweak_mul(_ctx, (unsigned char *)a, (const unsigned char *)b);
}
// multiplies secp256k1 generator by 256bit big endian int i and stores the result in p
// returns true on success
int BRSecp256k1PointGen(BRECPoint *p, const UInt256 *i)
{
secp256k1_pubkey pubkey;
size_t pLen = sizeof(*p);
pthread_once(&_ctx_once, _ctx_init);
return (secp256k1_ec_pubkey_create(_ctx, &pubkey, (const unsigned char *)i) &&
secp256k1_ec_pubkey_serialize(_ctx, (unsigned char *)p, &pLen, &pubkey, SECP256K1_EC_COMPRESSED));
}
// multiplies secp256k1 generator by 256bit big endian int i and adds the result to ec-point p
// returns true on success
int BRSecp256k1PointAdd(BRECPoint *p, const UInt256 *i)
{
secp256k1_pubkey pubkey;
size_t pLen = sizeof(*p);
pthread_once(&_ctx_once, _ctx_init);
return (secp256k1_ec_pubkey_parse(_ctx, &pubkey, (const unsigned char *)p, sizeof(*p)) &&
secp256k1_ec_pubkey_tweak_add(_ctx, &pubkey, (const unsigned char *)i) &&
secp256k1_ec_pubkey_serialize(_ctx, (unsigned char *)p, &pLen, &pubkey, SECP256K1_EC_COMPRESSED));
}
// multiplies secp256k1 ec-point p by 256bit big endian int i and stores the result in p
// returns true on success
int BRSecp256k1PointMul(BRECPoint *p, const UInt256 *i)
{
secp256k1_pubkey pubkey;
size_t pLen = sizeof(*p);
pthread_once(&_ctx_once, _ctx_init);
return (secp256k1_ec_pubkey_parse(_ctx, &pubkey, (const unsigned char *)p, sizeof(*p)) &&
secp256k1_ec_pubkey_tweak_mul(_ctx, &pubkey, (const unsigned char *)i) &&
secp256k1_ec_pubkey_serialize(_ctx, (unsigned char *)p, &pLen, &pubkey, SECP256K1_EC_COMPRESSED));
}
// returns true if privKey is a valid private key
// supported formats are wallet import format (WIF), mini private key format, or hex string
int BRPrivKeyIsValid(const char *privKey)
{
uint8_t data[34];
size_t dataLen, strLen;
int r = 0;
assert(privKey != NULL);
dataLen = BRBase58CheckDecode(data, sizeof(data), privKey);
strLen = strlen(privKey);
if (dataLen == 33 || dataLen == 34) { // wallet import format: https://en.bitcoin.it/wiki/Wallet_import_format
#if BITCOIN_TESTNET
r = (data[0] == BITCOIN_PRIVKEY_TEST);
#else
r = (data[0] == BITCOIN_PRIVKEY);
#endif
}
else if ((strLen == 30 || strLen == 22) && privKey[0] == 'S') { // mini private key format
char s[strLen + 2];
strncpy(s, privKey, sizeof(s));
s[sizeof(s) - 2] = '?';
BRSHA256(data, s, sizeof(s) - 1);
mem_clean(s, sizeof(s));
r = (data[0] == 0);
}
else r = (strspn(privKey, "0123456789ABCDEFabcdef") == 64); // hex encoded key
mem_clean(data, sizeof(data));
return r;
}
// assigns secret to key and returns true on success
int BRKeySetSecret(BRKey *key, const UInt256 *secret, int compressed)
{
assert(key != NULL);
assert(secret != NULL);
pthread_once(&_ctx_once, _ctx_init);
BRKeyClean(key);
key->secret = UInt256Get(secret);
key->compressed = compressed;
return secp256k1_ec_seckey_verify(_ctx, key->secret.u8);
}
// assigns privKey to key and returns true on success
// privKey must be wallet import format (WIF), mini private key format, or hex string
int BRKeySetPrivKey(BRKey *key, const char *privKey)
{
size_t len = strlen(privKey);
uint8_t data[34], version = BITCOIN_PRIVKEY;
int r = 0;
#if BITCOIN_TESTNET
version = BITCOIN_PRIVKEY_TEST;
#endif
assert(key != NULL);
assert(privKey != NULL);
// mini private key format
if ((len == 30 || len == 22) && privKey[0] == 'S') {
if (! BRPrivKeyIsValid(privKey)) return 0;
BRSHA256(data, privKey, strlen(privKey));
r = BRKeySetSecret(key, (UInt256 *)data, 0);
}
else {
len = BRBase58CheckDecode(data, sizeof(data), privKey);
if (len == 0 || len == 28) len = BRBase58Decode(data, sizeof(data), privKey);
if (len < sizeof(UInt256) || len > sizeof(UInt256) + 2) { // treat as hex string
for (len = 0; privKey[len*2] && privKey[len*2 + 1] && len < sizeof(data); len++) {
if (sscanf(&privKey[len*2], "%2hhx", &data[len]) != 1) break;
}
}
if ((len == sizeof(UInt256) + 1 || len == sizeof(UInt256) + 2) && data[0] == version) {
r = BRKeySetSecret(key, (UInt256 *)&data[1], (len == sizeof(UInt256) + 2));
}
else if (len == sizeof(UInt256)) {
r = BRKeySetSecret(key, (UInt256 *)data, 0);
}
}
mem_clean(data, sizeof(data));
return r;
}
// assigns DER encoded pubKey to key and returns true on success
int BRKeySetPubKey(BRKey *key, const uint8_t *pubKey, size_t pkLen)
{
secp256k1_pubkey pk;
assert(key != NULL);
assert(pubKey != NULL);
assert(pkLen == 33 || pkLen == 65);
pthread_once(&_ctx_once, _ctx_init);
BRKeyClean(key);
memcpy(key->pubKey, pubKey, pkLen);
key->compressed = (pkLen <= 33);
return secp256k1_ec_pubkey_parse(_ctx, &pk, key->pubKey, pkLen);
}
// writes the WIF private key to privKey and returns the number of bytes writen, or pkLen needed if privKey is NULL
// returns 0 on failure
size_t BRKeyPrivKey(const BRKey *key, char *privKey, size_t pkLen)
{
uint8_t data[34];
assert(key != NULL);
if (secp256k1_ec_seckey_verify(_ctx, key->secret.u8)) {
data[0] = BITCOIN_PRIVKEY;
#if BITCOIN_TESTNET
data[0] = BITCOIN_PRIVKEY_TEST;
#endif
UInt256Set(&data[1], key->secret);
if (key->compressed) data[33] = 0x01;
pkLen = BRBase58CheckEncode(privKey, pkLen, data, (key->compressed) ? 34 : 33);
mem_clean(data, sizeof(data));
}
else pkLen = 0;
return pkLen;
}
// writes the DER encoded public key to pubKey and returns number of bytes written, or pkLen needed if pubKey is NULL
size_t BRKeyPubKey(BRKey *key, void *pubKey, size_t pkLen)
{
static uint8_t empty[65]; // static vars initialize to zero
size_t size = (key->compressed) ? 33 : 65;
secp256k1_pubkey pk;
assert(key != NULL);
if (memcmp(key->pubKey, empty, size) == 0) {
if (secp256k1_ec_pubkey_create(_ctx, &pk, key->secret.u8)) {
secp256k1_ec_pubkey_serialize(_ctx, key->pubKey, &size, &pk,
(key->compressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED));
}
else size = 0;
}
if (pubKey && size <= pkLen) memcpy(pubKey, key->pubKey, size);
return (! pubKey || size <= pkLen) ? size : 0;
}
// returns the ripemd160 hash of the sha256 hash of the public key
UInt160 BRKeyHash160(BRKey *key)
{
UInt160 hash = UINT160_ZERO;
size_t len;
secp256k1_pubkey pk;
assert(key != NULL);
len = BRKeyPubKey(key, NULL, 0);
if (len > 0 && secp256k1_ec_pubkey_parse(_ctx, &pk, key->pubKey, len)) BRHash160(&hash, key->pubKey, len);
return hash;
}
// writes the pay-to-pubkey-hash bitcoin address for key to addr
// returns the number of bytes written, or addrLen needed if addr is NULL
size_t BRKeyAddress(BRKey *key, char *addr, size_t addrLen)
{
UInt160 hash;
uint8_t data[21];
assert(key != NULL);
hash = BRKeyHash160(key);
data[0] = BITCOIN_PUBKEY_ADDRESS;
#if BITCOIN_TESTNET
data[0] = BITCOIN_PUBKEY_ADDRESS_TEST;
#endif
UInt160Set(&data[1], hash);
if (! UInt160IsZero(hash)) {
addrLen = BRBase58CheckEncode(addr, addrLen, data, sizeof(data));
}
else addrLen = 0;
return addrLen;
}
// signs md with key and writes signature to sig
// returns the number of bytes written, or sigLen needed if sig is NULL
// returns 0 on failure
size_t BRKeySign(const BRKey *key, void *sig, size_t sigLen, UInt256 md)
{
secp256k1_ecdsa_signature s;
assert(key != NULL);
if (secp256k1_ecdsa_sign(_ctx, &s, md.u8, key->secret.u8, secp256k1_nonce_function_rfc6979, NULL)) {
if (! secp256k1_ecdsa_signature_serialize_der(_ctx, sig, &sigLen, &s)) sigLen = 0;
}
else sigLen = 0;
return sigLen;
}
// returns true if the signature for md is verified to have been made by key
int BRKeyVerify(BRKey *key, UInt256 md, const void *sig, size_t sigLen)
{
secp256k1_pubkey pk;
secp256k1_ecdsa_signature s;
size_t len;
int r = 0;
assert(key != NULL);
assert(sig != NULL || sigLen == 0);
assert(sigLen > 0);
len = BRKeyPubKey(key, NULL, 0);
if (len > 0 && secp256k1_ec_pubkey_parse(_ctx, &pk, key->pubKey, len) &&
secp256k1_ecdsa_signature_parse_der(_ctx, &s, sig, sigLen)) {
if (secp256k1_ecdsa_verify(_ctx, &s, md.u8, &pk) == 1) r = 1; // success is 1, all other values are fail
}
return r;
}
// wipes key material from key
void BRKeyClean(BRKey *key)
{
assert(key != NULL);
var_clean(key);
}
// Pieter Wuille's compact signature encoding used for bitcoin message signing
// to verify a compact signature, recover a public key from the signature and verify that it matches the signer's pubkey
size_t BRKeyCompactSign(const BRKey *key, void *compactSig, size_t sigLen, UInt256 md)
{
size_t r = 0;
int recid = 0;
secp256k1_ecdsa_recoverable_signature s;
assert(key != NULL);
assert(sigLen >= 65 || compactSig == NULL);
if (! UInt256IsZero(key->secret)) { // can't sign with a public key
if (compactSig && sigLen >= 65 &&
secp256k1_ecdsa_sign_recoverable(_ctx, &s, md.u8, key->secret.u8, secp256k1_nonce_function_rfc6979, NULL) &&
secp256k1_ecdsa_recoverable_signature_serialize_compact(_ctx, (uint8_t *)compactSig + 1, &recid, &s)) {
((uint8_t *)compactSig)[0] = 27 + recid + (key->compressed ? 4 : 0);
r = 65;
}
else if (! compactSig) r = 65;
}
return r;
}
// assigns pubKey recovered from compactSig to key and returns true on success
int BRKeyRecoverPubKey(BRKey *key, UInt256 md, const void *compactSig, size_t sigLen)
{
int r = 0, compressed = 0, recid = 0;
uint8_t pubKey[65];
size_t len = sizeof(pubKey);
secp256k1_ecdsa_recoverable_signature s;
secp256k1_pubkey pk;
assert(key != NULL);
assert(compactSig != NULL);
assert(sigLen == 65);
if (sigLen == 65) {
if (((uint8_t *)compactSig)[0] - 27 >= 4) compressed = 1;
recid = (((uint8_t *)compactSig)[0] - 27) % 4;
if (secp256k1_ecdsa_recoverable_signature_parse_compact(_ctx, &s, (const uint8_t *)compactSig + 1, recid) &&
secp256k1_ecdsa_recover(_ctx, &pk, &s, md.u8) &&
secp256k1_ec_pubkey_serialize(_ctx, pubKey, &len, &pk,
(compressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED))) {
r = BRKeySetPubKey(key, pubKey, len);
}
}
return r;
}