forked from breadwallet/breadwallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BRBIP32Sequence.c
283 lines (234 loc) · 10.7 KB
/
BRBIP32Sequence.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
//
// BRBIP32Sequence.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 "BRBIP32Sequence.h"
#include "BRCrypto.h"
#include "BRBase58.h"
#include <string.h>
#include <assert.h>
#define BIP32_HARD 0x80000000
#define BIP32_SEED_KEY "Bitcoin seed"
#define BIP32_XPRV "\x04\x88\xAD\xE4"
#define BIP32_XPUB "\x04\x88\xB2\x1E"
// BIP32 is a scheme for deriving chains of addresses from a seed value
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
// Private parent key -> private child key
//
// CKDpriv((kpar, cpar), i) -> (ki, ci) computes a child extended private key from the parent extended private key:
//
// - Check whether i >= 2^31 (whether the child is a hardened key).
// - If so (hardened child): let I = HMAC-SHA512(Key = cpar, Data = 0x00 || ser256(kpar) || ser32(i)).
// (Note: The 0x00 pads the private key to make it 33 bytes long.)
// - If not (normal child): let I = HMAC-SHA512(Key = cpar, Data = serP(point(kpar)) || ser32(i)).
// - Split I into two 32-byte sequences, IL and IR.
// - The returned child key ki is parse256(IL) + kpar (mod n).
// - The returned chain code ci is IR.
// - In case parse256(IL) >= n or ki = 0, the resulting key is invalid, and one should proceed with the next value for i
// (Note: this has probability lower than 1 in 2^127.)
//
static void _CKDpriv(UInt256 *k, UInt256 *c, uint32_t i)
{
uint8_t buf[sizeof(BRECPoint) + sizeof(i)];
UInt512 I;
if (i & BIP32_HARD) {
buf[0] = 0;
UInt256Set(&buf[1], *k);
}
else BRSecp256k1PointGen((BRECPoint *)buf, k);
UInt32SetBE(&buf[sizeof(BRECPoint)], i);
BRHMAC(&I, BRSHA512, sizeof(UInt512), c, sizeof(*c), buf, sizeof(buf)); // I = HMAC-SHA512(c, k|P(k) || i)
BRSecp256k1ModAdd(k, (UInt256 *)&I); // k = IL + k (mod n)
*c = *(UInt256 *)&I.u8[sizeof(UInt256)]; // c = IR
I = UINT512_ZERO;
memset(buf, 0, sizeof(buf));
}
// Public parent key -> public child key
//
// CKDpub((Kpar, cpar), i) -> (Ki, ci) computes a child extended public key from the parent extended public key.
// It is only defined for non-hardened child keys.
//
// - Check whether i >= 2^31 (whether the child is a hardened key).
// - If so (hardened child): return failure
// - If not (normal child): let I = HMAC-SHA512(Key = cpar, Data = serP(Kpar) || ser32(i)).
// - Split I into two 32-byte sequences, IL and IR.
// - The returned child key Ki is point(parse256(IL)) + Kpar.
// - The returned chain code ci is IR.
// - In case parse256(IL) >= n or Ki is the point at infinity, the resulting key is invalid, and one should proceed with
// the next value for i.
//
static void _CKDpub(BRECPoint *K, UInt256 *c, uint32_t i)
{
uint8_t buf[sizeof(*K) + sizeof(i)];
UInt512 I;
if ((i & BIP32_HARD) != BIP32_HARD) { // can't derive private child key from public parent key
*(BRECPoint *)buf = *K;
UInt32SetBE(&buf[sizeof(*K)], i);
BRHMAC(&I, BRSHA512, sizeof(UInt512), c, sizeof(*c), buf, sizeof(buf)); // I = HMAC-SHA512(c, P(K) || i)
*c = *(UInt256 *)&I.u8[sizeof(UInt256)]; // c = IR
BRSecp256k1PointAdd(K, (UInt256 *)&I); // K = P(IL) + K
I = UINT512_ZERO;
memset(buf, 0, sizeof(buf));
}
}
// returns the master public key for the default BIP32 wallet layout - derivation path N(m/0H)
BRMasterPubKey BRBIP32MasterPubKey(const void *seed, size_t seedLen)
{
BRMasterPubKey mpk = BR_MASTER_PUBKEY_NONE;
UInt512 I;
UInt256 secret, chain;
BRKey key;
assert(seed != NULL || seedLen == 0);
if (seed || seedLen == 0) {
BRHMAC(&I, BRSHA512, sizeof(UInt512), BIP32_SEED_KEY, strlen(BIP32_SEED_KEY), seed, seedLen);
secret = *(UInt256 *)&I;
chain = *(UInt256 *)&I.u8[sizeof(UInt256)];
I = UINT512_ZERO;
BRKeySetSecret(&key, &secret, 1);
mpk.fingerPrint = BRKeyHash160(&key).u32[0];
_CKDpriv(&secret, &chain, 0 | BIP32_HARD); // path m/0H
mpk.chainCode = chain;
BRKeySetSecret(&key, &secret, 1);
secret = chain = UINT256_ZERO;
BRKeyPubKey(&key, &mpk.pubKey, sizeof(mpk.pubKey)); // path N(m/0H)
BRKeyClean(&key);
}
return mpk;
}
// writes the public key for path N(m/0H/chain/index) to pubKey
// returns number of bytes written, or pubKeyLen needed if pubKey is NULL
size_t BRBIP32PubKey(uint8_t *pubKey, size_t pubKeyLen, BRMasterPubKey mpk, uint32_t chain, uint32_t index)
{
UInt256 chainCode = mpk.chainCode;
assert(memcmp(&mpk, &BR_MASTER_PUBKEY_NONE, sizeof(mpk)) != 0);
if (pubKey && sizeof(BRECPoint) <= pubKeyLen) {
*(BRECPoint *)pubKey = *(BRECPoint *)mpk.pubKey;
_CKDpub((BRECPoint *)pubKey, &chainCode, chain); // path N(m/0H/chain)
_CKDpub((BRECPoint *)pubKey, &chainCode, index); // index'th key in chain
chainCode = UINT256_ZERO;
}
return (! pubKey || sizeof(BRECPoint) <= pubKeyLen) ? sizeof(BRECPoint) : 0;
}
// sets the private key for path m/0H/chain/index to key
void BRBIP32PrivKey(BRKey *key, const void *seed, size_t seedLen, uint32_t chain, uint32_t index)
{
assert(key != NULL);
assert(seed != NULL || seedLen == 0);
BRBIP32PrivKeyList(key, 1, seed, seedLen, chain, &index);
}
// sets the private key for path m/0H/chain/index to each element in keys
void BRBIP32PrivKeyList(BRKey keys[], size_t keysCount, const void *seed, size_t seedLen, uint32_t chain,
const uint32_t indexes[])
{
UInt512 I;
UInt256 secret, chainCode, s, c;
assert(keys != NULL || keysCount == 0);
assert(seed != NULL || seedLen == 0);
assert(indexes != NULL || keysCount == 0);
if (keys && keysCount > 0 && (seed || seedLen == 0) && indexes) {
BRHMAC(&I, BRSHA512, sizeof(UInt512), BIP32_SEED_KEY, strlen(BIP32_SEED_KEY), seed, seedLen);
secret = *(UInt256 *)&I;
chainCode = *(UInt256 *)&I.u8[sizeof(UInt256)];
I = UINT512_ZERO;
_CKDpriv(&secret, &chainCode, 0 | BIP32_HARD); // path m/0H
_CKDpriv(&secret, &chainCode, chain); // path m/0H/chain
for (size_t i = 0; i < keysCount; i++) {
s = secret;
c = chainCode;
_CKDpriv(&s, &c, indexes[i]); // index'th key in chain
BRKeySetSecret(&keys[i], &s, 1);
}
secret = chainCode = c = s = UINT256_ZERO;
}
}
// writes the base58check encoded serialized master private key (xprv) to str
// returns number of bytes written including NULL terminator, or strLen needed if str is NULL
size_t BRBIP32SerializeMasterPrivKey(char *str, size_t strLen, const void *seed, size_t seedLen)
{
// TODO: XXX implement
return 0;
}
// writes a master private key to seed given a base58check encoded serialized master private key (xprv)
// returns number of bytes written, or seedLen needed if seed is NULL
size_t BRBIP32ParseMasterPrivKey(void *seed, size_t seedLen, const char *str)
{
// TODO: XXX implement
return 0;
}
// writes the base58check encoded serialized master public key (xpub) to str
// returns number of bytes written including NULL terminator, or strLen needed if str is NULL
size_t BRBIP32SerializeMasterPubKey(char *str, size_t strLen, BRMasterPubKey mpk)
{
// TODO: XXX implement
return 0;
}
// returns a master public key give a base58check encoded serialized master public key (xpub)
BRMasterPubKey BRBIP32ParseMasterPubKey(const char *str)
{
// TODO: XXX implement
return BR_MASTER_PUBKEY_NONE;
}
// key used for authenticated API calls, i.e. bitauth: https://github.com/bitpay/bitauth - path m/1H/0
void BRBIP32APIAuthKey(BRKey *key, const void *seed, size_t seedLen)
{
UInt512 I;
UInt256 secret, chainCode;
assert(key != NULL);
assert(seed != NULL || seedLen == 0);
if (key && (seed || seedLen == 0)) {
BRHMAC(&I, BRSHA512, sizeof(UInt512), BIP32_SEED_KEY, strlen(BIP32_SEED_KEY), seed, seedLen);
secret = *(UInt256 *)&I;
chainCode = *(UInt256 *)&I.u8[sizeof(UInt256)];
I = UINT512_ZERO;
_CKDpriv(&secret, &chainCode, 1 | BIP32_HARD); // path m/1H
_CKDpriv(&secret, &chainCode, 0); // path m/1H/0
BRKeySetSecret(key, &secret, 1);
secret = chainCode = UINT256_ZERO;
}
}
// key used for BitID: https://github.com/bitid/bitid/blob/master/BIP_draft.md
void BRBIP32BitIDKey(BRKey *key, const void *seed, size_t seedLen, uint32_t index, const char *uri)
{
assert(key != NULL);
assert(seed != NULL || seedLen == 0);
assert(uri != NULL);
if (key && (seed || seedLen == 0) && uri) {
UInt512 I;
UInt256 secret, chainCode, hash;
size_t uriLen = strlen(uri);
uint8_t data[sizeof(index) + uriLen];
UInt32SetLE(data, index);
memcpy(&data[sizeof(index)], uri, uriLen);
BRSHA256(&hash, data, sizeof(data));
BRHMAC(&I, BRSHA512, sizeof(UInt512), BIP32_SEED_KEY, strlen(BIP32_SEED_KEY), seed, seedLen);
secret = *(UInt256 *)&I;
chainCode = *(UInt256 *)&I.u8[sizeof(UInt256)];
I = UINT512_ZERO;
_CKDpriv(&secret, &chainCode, 13 | BIP32_HARD); // path m/13H
_CKDpriv(&secret, &chainCode, UInt32GetLE(&hash.u32[0]) | BIP32_HARD); // path m/13H/aH
_CKDpriv(&secret, &chainCode, UInt32GetLE(&hash.u32[1]) | BIP32_HARD); // path m/13H/aH/bH
_CKDpriv(&secret, &chainCode, UInt32GetLE(&hash.u32[2]) | BIP32_HARD); // path m/13H/aH/bH/cH
_CKDpriv(&secret, &chainCode, UInt32GetLE(&hash.u32[3]) | BIP32_HARD); // path m/13H/aH/bH/cH/dH
BRKeySetSecret(key, &secret, 1);
secret = chainCode = UINT256_ZERO;
}
}