upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bip32.cpp
blob: 64239747bba2f8fddbf157d40347783939fe6b26 (plain)
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
// Copyright (c) 2026 Bitflash developers
// Distributed under the MIT/X11 software license.

#include "bip32.h"
#include "bip39_english.h"

#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/ec.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/obj_mac.h>
#include <openssl/rand.h>
#include <openssl/sha.h>

#include <algorithm>
#include <cctype>
#include <cstring>
#include <map>
#include <sstream>

namespace bitflash
{

static bool IsSupportedEntropySize(size_t n)
{
    return n == 16 || n == 20 || n == 24 || n == 28 || n == 32;
}

static bool GetBit(const unsigned char* data, size_t bit)
{
    return (data[bit / 8] >> (7 - (bit % 8))) & 1;
}

static void AppendBits(std::vector<bool>& out, const unsigned char* data,
                       size_t nBits)
{
    for (size_t i = 0; i < nBits; i++)
        out.push_back(GetBit(data, i));
}

static std::string NormalizeMnemonic(const std::string& in)
{
    std::string out;
    bool inSpace = true;
    for (unsigned char c : in)
    {
        if (std::isspace(c))
        {
            if (!inSpace)
                out.push_back(' ');
            inSpace = true;
        }
        else
        {
            out.push_back((char)std::tolower(c));
            inSpace = false;
        }
    }
    if (!out.empty() && out.back() == ' ')
        out.pop_back();
    return out;
}

static std::vector<std::string> SplitWords(const std::string& phrase)
{
    std::vector<std::string> words;
    std::istringstream in(phrase);
    std::string word;
    while (in >> word)
        words.push_back(word);
    return words;
}

static const std::map<std::string, int>& WordIndex()
{
    static std::map<std::string, int> m;
    if (m.empty())
    {
        for (int i = 0; i < 2048; i++)
            m[BIP39_ENGLISH_WORDS[i]] = i;
    }
    return m;
}

static bool IsValidPrivateKey(const std::vector<unsigned char>& key)
{
    if (key.size() != 32)
        return false;
    bool fNonZero = false;
    for (unsigned char c : key)
        fNonZero = fNonZero || c != 0;
    if (!fNonZero)
        return false;

    bool ok = false;
    BIGNUM* bn = BN_bin2bn(&key[0], 32, NULL);
    BIGNUM* order = BN_new();
    EC_GROUP* group = EC_GROUP_new_by_curve_name(NID_secp256k1);
    if (bn && order && group && EC_GROUP_get_order(group, order, NULL))
        ok = BN_cmp(bn, order) < 0;
    BN_free(bn);
    BN_free(order);
    EC_GROUP_free(group);
    return ok;
}

static bool HmacSha512(const unsigned char* key, int keyLen,
                       const unsigned char* data, size_t dataLen,
                       unsigned char out[64])
{
    unsigned int len = 64;
    return HMAC(EVP_sha512(), key, keyLen, data, dataLen, out, &len) != NULL &&
           len == 64;
}

bool BIP39EntropyToMnemonic(const std::vector<unsigned char>& entropy,
                            std::string& mnemonicOut,
                            std::string& errorOut)
{
    mnemonicOut.clear();
    errorOut.clear();
    if (!IsSupportedEntropySize(entropy.size()))
    {
        errorOut = "entropy must be 128, 160, 192, 224, or 256 bits";
        return false;
    }

    unsigned char checksum[SHA256_DIGEST_LENGTH];
    SHA256(&entropy[0], entropy.size(), checksum);

    const size_t entropyBits = entropy.size() * 8;
    const size_t checksumBits = entropyBits / 32;

    std::vector<bool> bits;
    bits.reserve(entropyBits + checksumBits);
    AppendBits(bits, &entropy[0], entropyBits);
    AppendBits(bits, checksum, checksumBits);

    const size_t words = bits.size() / 11;
    for (size_t i = 0; i < words; i++)
    {
        int idx = 0;
        for (int j = 0; j < 11; j++)
            idx = (idx << 1) | (bits[i * 11 + j] ? 1 : 0);
        if (i != 0)
            mnemonicOut += " ";
        mnemonicOut += BIP39_ENGLISH_WORDS[idx];
    }
    return true;
}

bool BIP39GenerateMnemonic(std::string& mnemonicOut,
                           std::string& errorOut,
                           unsigned int entropyBytes)
{
    mnemonicOut.clear();
    errorOut.clear();
    if (!IsSupportedEntropySize(entropyBytes))
    {
        errorOut = "entropy must be 16, 20, 24, 28, or 32 bytes";
        return false;
    }

    std::vector<unsigned char> entropy(entropyBytes);
    if (RAND_bytes(&entropy[0], entropy.size()) != 1)
    {
        errorOut = "RAND_bytes failed";
        return false;
    }
    bool ok = BIP39EntropyToMnemonic(entropy, mnemonicOut, errorOut);
    OPENSSL_cleanse(&entropy[0], entropy.size());
    return ok;
}

bool BIP39ValidateMnemonic(const std::string& mnemonic,
                           std::string& normalizedOut,
                           std::string& errorOut)
{
    normalizedOut = NormalizeMnemonic(mnemonic);
    errorOut.clear();
    std::vector<std::string> words = SplitWords(normalizedOut);
    const size_t nWords = words.size();
    if (nWords != 12 && nWords != 15 && nWords != 18 && nWords != 21 && nWords != 24)
    {
        errorOut = "mnemonic must contain 12, 15, 18, 21, or 24 words";
        return false;
    }

    const std::map<std::string, int>& index = WordIndex();
    std::vector<bool> bits;
    bits.reserve(nWords * 11);
    for (const std::string& word : words)
    {
        std::map<std::string, int>::const_iterator mi = index.find(word);
        if (mi == index.end())
        {
            errorOut = "mnemonic contains a word outside the BIP39 English list";
            return false;
        }
        int value = mi->second;
        for (int i = 10; i >= 0; i--)
            bits.push_back(((value >> i) & 1) != 0);
    }

    const size_t totalBits = bits.size();
    const size_t entropyBits = totalBits * 32 / 33;
    const size_t checksumBits = totalBits - entropyBits;
    std::vector<unsigned char> entropy(entropyBits / 8, 0);
    for (size_t i = 0; i < entropyBits; i++)
        if (bits[i])
            entropy[i / 8] |= (unsigned char)(1 << (7 - (i % 8)));

    unsigned char checksum[SHA256_DIGEST_LENGTH];
    SHA256(&entropy[0], entropy.size(), checksum);
    for (size_t i = 0; i < checksumBits; i++)
    {
        if (bits[entropyBits + i] != GetBit(checksum, i))
        {
            errorOut = "mnemonic checksum failed";
            return false;
        }
    }
    return true;
}

bool BIP39MnemonicToSeed(const std::string& mnemonic,
                         const std::string& passphrase,
                         std::vector<unsigned char>& seedOut,
                         std::string& errorOut)
{
    seedOut.assign(64, 0);
    std::string normalized;
    if (!BIP39ValidateMnemonic(mnemonic, normalized, errorOut))
    {
        seedOut.clear();
        return false;
    }

    const std::string salt = "mnemonic" + passphrase;
    if (!PKCS5_PBKDF2_HMAC(normalized.c_str(), normalized.size(),
                           (const unsigned char*)salt.data(), salt.size(),
                           2048, EVP_sha512(), 64, &seedOut[0]))
    {
        seedOut.clear();
        errorOut = "PBKDF2-HMAC-SHA512 failed";
        return false;
    }
    return true;
}

bool BIP32MasterFromSeed(const std::vector<unsigned char>& seed,
                         BIP32PrivateNode& nodeOut,
                         std::string& errorOut)
{
    nodeOut.privateKey.clear();
    nodeOut.chainCode.clear();
    errorOut.clear();
    if (seed.size() < 16 || seed.size() > 64)
    {
        errorOut = "BIP32 seed must be between 128 and 512 bits";
        return false;
    }

    unsigned char I[64];
    static const unsigned char key[] = "Bitcoin seed";
    if (!HmacSha512(key, sizeof(key) - 1, &seed[0], seed.size(), I))
    {
        errorOut = "HMAC-SHA512 failed";
        return false;
    }

    nodeOut.privateKey.assign(I, I + 32);
    nodeOut.chainCode.assign(I + 32, I + 64);
    OPENSSL_cleanse(I, sizeof(I));

    if (!IsValidPrivateKey(nodeOut.privateKey))
    {
        nodeOut.privateKey.clear();
        nodeOut.chainCode.clear();
        errorOut = "BIP32 master key is invalid";
        return false;
    }
    return true;
}

bool BIP32DeriveHardenedChild(const BIP32PrivateNode& parent,
                              unsigned int childIndex,
                              BIP32PrivateNode& childOut,
                              std::string& errorOut)
{
    childOut.privateKey.clear();
    childOut.chainCode.clear();
    errorOut.clear();
    if (!IsValidPrivateKey(parent.privateKey) || parent.chainCode.size() != 32)
    {
        errorOut = "parent private key or chain code is invalid";
        return false;
    }
    if (childIndex >= 0x80000000U)
    {
        errorOut = "child index must be non-hardened; hardening is applied here";
        return false;
    }

    const unsigned int hardened = childIndex | 0x80000000U;
    unsigned char data[37];
    data[0] = 0x00;
    memcpy(data + 1, &parent.privateKey[0], 32);
    data[33] = (unsigned char)((hardened >> 24) & 0xff);
    data[34] = (unsigned char)((hardened >> 16) & 0xff);
    data[35] = (unsigned char)((hardened >> 8) & 0xff);
    data[36] = (unsigned char)(hardened & 0xff);

    unsigned char I[64];
    if (!HmacSha512(&parent.chainCode[0], 32, data, sizeof(data), I))
    {
        OPENSSL_cleanse(data, sizeof(data));
        errorOut = "HMAC-SHA512 failed";
        return false;
    }
    OPENSSL_cleanse(data, sizeof(data));

    std::vector<unsigned char> IL(I, I + 32);
    childOut.chainCode.assign(I + 32, I + 64);
    OPENSSL_cleanse(I, sizeof(I));

    BIGNUM* bnIL = BN_bin2bn(&IL[0], 32, NULL);
    BIGNUM* bnParent = BN_bin2bn(&parent.privateKey[0], 32, NULL);
    BIGNUM* bnOrder = BN_new();
    BIGNUM* bnChild = BN_new();
    BN_CTX* ctx = BN_CTX_new();
    EC_GROUP* group = EC_GROUP_new_by_curve_name(NID_secp256k1);

    bool ok = false;
    if (bnIL && bnParent && bnOrder && bnChild && ctx && group &&
        EC_GROUP_get_order(group, bnOrder, NULL) &&
        BN_cmp(bnIL, bnOrder) < 0 &&
        BN_mod_add(bnChild, bnIL, bnParent, bnOrder, ctx) &&
        !BN_is_zero(bnChild))
    {
        childOut.privateKey.assign(32, 0);
        int n = BN_num_bytes(bnChild);
        BN_bn2bin(bnChild, &childOut.privateKey[32 - n]);
        ok = true;
    }

    BN_free(bnIL);
    BN_free(bnParent);
    BN_free(bnOrder);
    BN_free(bnChild);
    BN_CTX_free(ctx);
    EC_GROUP_free(group);
    OPENSSL_cleanse(&IL[0], IL.size());

    if (!ok)
    {
        childOut.privateKey.clear();
        childOut.chainCode.clear();
        errorOut = "BIP32 child derivation produced an invalid key";
        return false;
    }
    return true;
}

} // namespace bitflash