forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbip32.c
562 lines (493 loc) · 17.7 KB
/
bip32.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/**
* Copyright (c) 2013-2014 Tomas Dzetkulic
* Copyright (c) 2013-2014 Pavol Rusnak
* Copyright (c) 2015 Douglas J. Bakkumk
* Copyright (c) 2022 bluezr
* Copyright (c) 2022 The Dogecoin Foundation
*
* 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 <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dogecoin/bip32.h>
#include <dogecoin/base58.h>
#include <dogecoin/ecc.h>
#include <dogecoin/hash.h>
#include <dogecoin/key.h>
#include <dogecoin/rmd160.h>
#include <dogecoin/sha2.h>
#include <dogecoin/mem.h>
#include <dogecoin/utils.h>
/**
* @brief This function writes 4 big endian bytes.
*
* @param data The buffer being written to.
* @param x The 4 bytes to be written, bundled as int.
*
* @return Nothing.
*/
static void write_be(uint8_t* data, uint32_t x)
{
data[0] = x >> 24;
data[1] = x >> 16;
data[2] = x >> 8;
data[3] = x;
}
/**
* @brief This function reads 4 big endian bytes.
*
* @param data The bytes to be read.
*
* @return The data buffer in int format.
*/
static uint32_t read_be(const uint8_t* data)
{
return (((uint32_t)data[0]) << 24) |
(((uint32_t)data[1]) << 16) |
(((uint32_t)data[2]) << 8) |
(((uint32_t)data[3]));
}
/**
* @brief This function allocates memory for a new
* HD node object and returns a pointer to it.
*
* @return The pointer to the new HD node object.
*/
dogecoin_hdnode* dogecoin_hdnode_new()
{
dogecoin_hdnode* hdnode;
hdnode = dogecoin_calloc(1, sizeof(*hdnode));
return hdnode;
}
/**
* @brief The function creates a new HD node object
* and copies all information from the old to the new.
*
* @param hdnode The HD node to be copied.
*
* @return The pointer to the new node.
*/
dogecoin_hdnode* dogecoin_hdnode_copy(const dogecoin_hdnode* hdnode)
{
dogecoin_hdnode* newnode = dogecoin_hdnode_new();
newnode->depth = hdnode->depth;
newnode->fingerprint = hdnode->fingerprint;
newnode->child_num = hdnode->child_num;
memcpy_safe(newnode->chain_code, hdnode->chain_code, sizeof(hdnode->chain_code));
memcpy_safe(newnode->private_key, hdnode->private_key, sizeof(hdnode->private_key));
memcpy_safe(newnode->public_key, hdnode->public_key, sizeof(hdnode->public_key));
return newnode;
}
/**
* @brief This function frees an HD node object in memory.
*
* @param hdnode The HD node to be freed.
*
* @return Nothing.
*/
void dogecoin_hdnode_free(dogecoin_hdnode* hdnode)
{
dogecoin_mem_zero(hdnode->chain_code, sizeof(hdnode->chain_code));
dogecoin_mem_zero(hdnode->private_key, sizeof(hdnode->private_key));
dogecoin_mem_zero(hdnode->public_key, sizeof(hdnode->public_key));
dogecoin_free(hdnode);
}
/**
* @brief This function generates a private and public
* keypair along with chain_code for a hierarchical
* deterministic wallet. This is derived from a seed
* which usually consists of 12 random mnemonic words.
*
* @param seed The byte string containing the seed phrase.
* @param seed_len The length of the phrase in characters.
* @param out The master node which stores the generated data.
*
* @return Nothing.
*/
dogecoin_bool dogecoin_hdnode_from_seed(const uint8_t* seed, int seed_len, dogecoin_hdnode* out)
{
uint8_t I[DOGECOIN_ECKEY_PKEY_LENGTH + DOGECOIN_BIP32_CHAINCODE_SIZE];
dogecoin_mem_zero(out, sizeof(dogecoin_hdnode));
out->depth = 0;
out->fingerprint = 0x00000000;
out->child_num = 0;
hmac_sha512((const uint8_t*)"Dogecoin seed", 12, seed, seed_len, I);
memcpy_safe(out->private_key, I, DOGECOIN_ECKEY_PKEY_LENGTH);
if (!dogecoin_ecc_verify_privatekey(out->private_key)) {
dogecoin_mem_zero(I, sizeof(I));
return false;
}
memcpy_safe(out->chain_code, I + DOGECOIN_ECKEY_PKEY_LENGTH, DOGECOIN_BIP32_CHAINCODE_SIZE);
dogecoin_hdnode_fill_public_key(out);
dogecoin_mem_zero(I, sizeof(I));
return true;
}
/**
* @brief This function derives a child key from the
* public key of the specified master HD node.
*
* @param inout The node to derive the child key from.
* @param i The index of the child key, specified in derivation path.
*
* @return Nothing.
*/
dogecoin_bool dogecoin_hdnode_public_ckd(dogecoin_hdnode* inout, uint32_t i)
{
uint8_t data[1 + 32 + 4];
uint8_t I[32 + DOGECOIN_BIP32_CHAINCODE_SIZE];
uint8_t fingerprint[32];
if (i & 0x80000000) { // private derivation
return false;
} else { // public derivation
memcpy_safe(data, inout->public_key, DOGECOIN_ECKEY_COMPRESSED_LENGTH);
}
write_be(data + DOGECOIN_ECKEY_COMPRESSED_LENGTH, i);
sha256_raw(inout->public_key, DOGECOIN_ECKEY_COMPRESSED_LENGTH, fingerprint);
rmd160(fingerprint, 32, fingerprint);
inout->fingerprint = (fingerprint[0] << 24) + (fingerprint[1] << 16) + (fingerprint[2] << 8) + fingerprint[3];
dogecoin_mem_zero(inout->private_key, 32);
int failed = 0;
hmac_sha512(inout->chain_code, 32, data, sizeof(data), I);
memcpy_safe(inout->chain_code, I + 32, DOGECOIN_BIP32_CHAINCODE_SIZE);
if (!dogecoin_ecc_public_key_tweak_add(inout->public_key, I))
failed = false;
if (!failed) {
inout->depth++;
inout->child_num = i;
}
// Wipe all stack data.
dogecoin_mem_zero(data, sizeof(data));
dogecoin_mem_zero(I, sizeof(I));
dogecoin_mem_zero(fingerprint, sizeof(fingerprint));
return failed ? false : true;
}
/**
* @brief This function derives a child key from the
* private key of the specified master HD node.
*
* @param inout The node to derive the child key from.
* @param i The index of the child key, specified in derivation path.
*
* @return Nothing.
*/
dogecoin_bool dogecoin_hdnode_private_ckd(dogecoin_hdnode* inout, uint32_t i)
{
uint8_t data[1 + DOGECOIN_ECKEY_PKEY_LENGTH + 4];
uint8_t I[DOGECOIN_ECKEY_PKEY_LENGTH + DOGECOIN_BIP32_CHAINCODE_SIZE];
uint8_t fingerprint[DOGECOIN_BIP32_CHAINCODE_SIZE];
uint8_t p[DOGECOIN_ECKEY_PKEY_LENGTH], z[DOGECOIN_ECKEY_PKEY_LENGTH];
if (i & 0x80000000) { // private derivation
data[0] = 0;
memcpy_safe(data + 1, inout->private_key, DOGECOIN_ECKEY_PKEY_LENGTH);
} else { // public derivation
memcpy_safe(data, inout->public_key, DOGECOIN_ECKEY_COMPRESSED_LENGTH);
}
write_be(data + DOGECOIN_ECKEY_COMPRESSED_LENGTH, i);
sha256_raw(inout->public_key, DOGECOIN_ECKEY_COMPRESSED_LENGTH, fingerprint);
rmd160(fingerprint, 32, fingerprint);
inout->fingerprint = (fingerprint[0] << 24) + (fingerprint[1] << 16) +
(fingerprint[2] << 8) + fingerprint[3];
dogecoin_mem_zero(fingerprint, sizeof(fingerprint));
memcpy_safe(p, inout->private_key, DOGECOIN_ECKEY_PKEY_LENGTH);
hmac_sha512(inout->chain_code, DOGECOIN_BIP32_CHAINCODE_SIZE, data, sizeof(data), I);
memcpy_safe(inout->chain_code, I + DOGECOIN_ECKEY_PKEY_LENGTH, DOGECOIN_BIP32_CHAINCODE_SIZE);
memcpy_safe(inout->private_key, I, DOGECOIN_ECKEY_PKEY_LENGTH);
memcpy_safe(z, inout->private_key, DOGECOIN_ECKEY_PKEY_LENGTH);
int failed = 0;
if (!dogecoin_ecc_verify_privatekey(z)) {
failed = 1;
}
memcpy_safe(inout->private_key, p, DOGECOIN_ECKEY_PKEY_LENGTH);
if (!dogecoin_ecc_private_key_tweak_add(inout->private_key, z)) {
failed = 1;
}
if (!failed) {
inout->depth++;
inout->child_num = i;
dogecoin_hdnode_fill_public_key(inout);
}
dogecoin_mem_zero(data, sizeof(data));
dogecoin_mem_zero(I, sizeof(I));
dogecoin_mem_zero(z, sizeof(z));
return true;
}
/**
* @brief This function derives a public key from a private
* key taken from the HD node provided.
*
* @param node The HD node which contains the private key.
*
* @return Nothing.
*/
void dogecoin_hdnode_fill_public_key(dogecoin_hdnode* node)
{
size_t outsize = DOGECOIN_ECKEY_COMPRESSED_LENGTH;
dogecoin_ecc_get_pubkey(node->private_key, node->public_key, &outsize, true);
}
/**
* @brief This function serializes the information inside an
* HD node and loads it into string variable str using WIF-
* encoding to represent the public or private key.
*
* @param node The HD node containing the key.
* @param version The version byte.
* @param use_public Whether to use public or private key.
* @param str The string to contain the encoded output.
* @param strsize The size of the string that will be returned.
*
* @return Nothing.
*/
static void dogecoin_hdnode_serialize(const dogecoin_hdnode* node, uint32_t version, char use_public, char* str, size_t strsize)
{
uint8_t node_data[78];
write_be(node_data, version);
node_data[4] = node->depth;
write_be(node_data + 5, node->fingerprint);
write_be(node_data + 9, node->child_num);
memcpy_safe(node_data + 13, node->chain_code, DOGECOIN_BIP32_CHAINCODE_SIZE);
if (use_public) {
memcpy_safe(node_data + 45, node->public_key, DOGECOIN_ECKEY_COMPRESSED_LENGTH);
} else {
node_data[45] = 0;
memcpy_safe(node_data + 46, node->private_key, DOGECOIN_ECKEY_PKEY_LENGTH);
}
dogecoin_base58_encode_check(node_data, 78, str, strsize);
}
/**
* @brief This function serializes the HD node information
* into a string using the public key.
*
* @param node The HD node containing the key.
* @param chain The preset chain parameters to use.
* @param str The string to contain the encoded public key.
* @param strsize The size of the string to be returned.
*
* @return Nothing.
*/
void dogecoin_hdnode_serialize_public(const dogecoin_hdnode* node, const dogecoin_chainparams* chain, char* str, size_t strsize)
{
dogecoin_hdnode_serialize(node, chain->b58prefix_bip32_pubkey, 1, str, strsize);
}
/**
* @brief This function serializes the HD node information
* into a string using the private key.
*
* @param node The HD node containing the key.
* @param chain The chain parameters to use.
* @param str The string to contain the encoded private key.
* @param strsize The size of the string to be returned.
*
* @return Nothing.
*/
void dogecoin_hdnode_serialize_private(const dogecoin_hdnode* node, const dogecoin_chainparams* chain, char* str, size_t strsize)
{
dogecoin_hdnode_serialize(node, chain->b58prefix_bip32_privkey, 0, str, strsize);
}
/**
* @brief This function applies the sha256 and rmd160 hash
* functions to public key and loads variable hash160_out
* with result.
*
* @param node The node containing the public key and its chain code.
* @param hash160_out The hash160 of the public key.
*
* @return Nothing.
*/
void dogecoin_hdnode_get_hash160(const dogecoin_hdnode* node, uint160 hash160_out)
{
uint256 hashout;
dogecoin_hash_sngl_sha256(node->public_key, DOGECOIN_ECKEY_COMPRESSED_LENGTH, hashout);
rmd160(hashout, sizeof(hashout), hash160_out);
}
/**
* @brief This function produces a dogecoin pay-to-
* public-key-hash (p2pkh) address from the public
* key of the given node.
*
* @param node The HD node containing the public key.
* @param chain The chain parameters to use.
* @param str The string containing the p2pkh address.
* @param strsize The size of the string to be returned.
*
* @return Nothing.
*/
void dogecoin_hdnode_get_p2pkh_address(const dogecoin_hdnode* node, const dogecoin_chainparams* chain, char* str, size_t strsize)
{
uint8_t hash160[sizeof(uint160) + 1];
hash160[0] = chain->b58prefix_pubkey_address;
dogecoin_hdnode_get_hash160(node, hash160 + 1);
dogecoin_base58_encode_check(hash160, sizeof(hash160), str, strsize);
}
/**
* @brief This function converts a public key from binary
* to hexadecimal string format.
*
* @param node The HD node containing the public key.
* @param str The hexadecimal string to be returned.
* @param strsize The size of the string to be returned.
*
* @return Nothing.
*/
dogecoin_bool dogecoin_hdnode_get_pub_hex(const dogecoin_hdnode* node, char* str, size_t* strsize)
{
dogecoin_pubkey pubkey;
dogecoin_pubkey_init(&pubkey);
memcpy_safe(&pubkey.pubkey, node->public_key, DOGECOIN_ECKEY_COMPRESSED_LENGTH);
pubkey.compressed = true;
return dogecoin_pubkey_get_hex(&pubkey, str, strsize);
}
/**
* @brief This function takes a string buffer containing HD
* node data and loads an HD node object with that data. The
* function will copy either a private key or a public key,
* depending on the prefix of the given buffer.
*
* @param str The buffer containing the node information.
* @param chain The chain parameters to use.
* @param node The HD node to be filled.
*
* @return Nothing.
*/
dogecoin_bool dogecoin_hdnode_deserialize(const char* str, const dogecoin_chainparams* chain, dogecoin_hdnode* node)
{
if (!str || !chain || !node) return false;
const size_t ndlen = sizeof(uint8_t) * sizeof(dogecoin_hdnode);
uint8_t* node_data = (uint8_t*)dogecoin_calloc(1, ndlen);
dogecoin_mem_zero(node, sizeof(dogecoin_hdnode));
if (!dogecoin_base58_decode_check(str, node_data, ndlen)) {
dogecoin_free(node_data);
return false;
}
uint32_t version = read_be(node_data);
if (version == chain->b58prefix_bip32_pubkey) { // public node
memcpy_safe(node->public_key, node_data + 45, DOGECOIN_ECKEY_COMPRESSED_LENGTH);
} else if (version == chain->b58prefix_bip32_privkey) { // private node
if (node_data[45]) { // invalid data
dogecoin_free(node_data);
return false;
}
memcpy_safe(node->private_key, node_data + 46, DOGECOIN_ECKEY_PKEY_LENGTH);
dogecoin_hdnode_fill_public_key(node);
} else {
dogecoin_free(node_data);
return false; // invalid version
}
node->depth = node_data[4];
node->fingerprint = read_be(node_data + 5);
node->child_num = read_be(node_data + 9);
memcpy_safe(node->chain_code, node_data + 13, DOGECOIN_BIP32_CHAINCODE_SIZE);
dogecoin_free(node_data);
return true;
}
/**
* @brief This function generates a child key from a given
* master key and loads it into an HD node.
*
* @param node The HD node to be filled with the derived key.
* @param keypath The derivation path of the desired key (e.g. "m/0h/0/0")
* @param keymaster The master key to derive the child from.
* @param chaincode A 32-byte value that is used to generate the child key.
* @param usepubckd Whether to use public or private key derivation function.
*
* @return Nothing.
*/
dogecoin_bool dogecoin_hd_generate_key(dogecoin_hdnode* node, const char* keypath, const uint8_t* keymaster, const uint8_t* chaincode, dogecoin_bool usepubckd)
{
static char delim[] = "/";
static char prime[] = "phH\'";
static char digits[] = "0123456789";
uint64_t idx = 0;
assert(strlens(keypath) < 1024);
char *pch, *kp = dogecoin_malloc(strlens(keypath) + 1);
char *saveptr; /* for strtok_r calls - fix for concurrent calls error*/
if (!kp) {
return false;
}
if (strlens(keypath) < strlens("m/")) {
goto err;
}
dogecoin_mem_zero(kp, strlens(keypath) + 1);
memcpy_safe(kp, keypath, strlens(keypath));
if (kp[0] != 'm' || kp[1] != '/') {
goto err;
}
node->depth = 0;
node->child_num = 0;
node->fingerprint = 0;
memcpy_safe(node->chain_code, chaincode, DOGECOIN_BIP32_CHAINCODE_SIZE);
if (usepubckd == true) {
memcpy_safe(node->public_key, keymaster, DOGECOIN_ECKEY_COMPRESSED_LENGTH);
} else {
memcpy_safe(node->private_key, keymaster, DOGECOIN_ECKEY_PKEY_LENGTH);
dogecoin_hdnode_fill_public_key(node);
}
pch = strtok_r(kp + 2, delim, &saveptr);
while (pch != NULL) {
size_t i = 0;
int prm = 0;
for (; i < strlens(pch); i++) {
if (strchr(prime, pch[i])) {
if ((i != strlens(pch) - 1) || usepubckd == true) {
goto err;
}
prm = 1;
} else if (!strchr(digits, pch[i])) {
goto err;
}
}
idx = strtoull(pch, NULL, 10);
if (idx > UINT32_MAX) {
goto err;
}
if (prm) {
if (dogecoin_hdnode_private_ckd_prime(node, idx) != true) {
goto err;
}
} else {
if ((usepubckd == true ? dogecoin_hdnode_public_ckd(node, idx) : dogecoin_hdnode_private_ckd(node, idx)) != true) {
goto err;
}
}
pch = strtok_r(NULL, delim, &saveptr);
}
dogecoin_free(kp);
return true;
err:
dogecoin_free(kp);
return false;
}
/**
* @brief This function checks if the HD node has a private key.
*
* @param node The HD node to check.
*
* @return A boolean value, true if node has a private key.
*/
dogecoin_bool dogecoin_hdnode_has_privkey(dogecoin_hdnode* node)
{
int i;
for (i = 0; i < DOGECOIN_ECKEY_PKEY_LENGTH; ++i) {
if (node->private_key[i] != 0)
return true;
}
return false;
}