2014-05-22 18:54:58 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2013-2014 Tomas Dzetkulic
|
|
|
|
* Copyright (c) 2013-2014 Pavol Rusnak
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-09-12 17:21:24 +00:00
|
|
|
#include <string.h>
|
2015-01-26 18:09:00 +00:00
|
|
|
#include <stdbool.h>
|
2013-09-12 17:21:24 +00:00
|
|
|
|
|
|
|
#include "bignum.h"
|
|
|
|
#include "hmac.h"
|
|
|
|
#include "ecdsa.h"
|
|
|
|
#include "bip32.h"
|
2014-01-14 14:29:46 +00:00
|
|
|
#include "sha2.h"
|
|
|
|
#include "ripemd160.h"
|
2014-05-22 19:52:04 +00:00
|
|
|
#include "base58.h"
|
2013-09-12 17:21:24 +00:00
|
|
|
|
2014-12-13 18:54:01 +00:00
|
|
|
int hdnode_from_xpub(uint32_t depth, uint32_t fingerprint, uint32_t child_num, const uint8_t *chain_code, const uint8_t *public_key, HDNode *out)
|
2013-11-25 14:37:49 +00:00
|
|
|
{
|
2014-12-19 23:49:14 +00:00
|
|
|
if (public_key[0] != 0x02 && public_key[0] != 0x03) { // invalid pubkey
|
2014-08-07 12:34:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-11-25 14:37:49 +00:00
|
|
|
out->depth = depth;
|
|
|
|
out->fingerprint = fingerprint;
|
|
|
|
out->child_num = child_num;
|
|
|
|
memcpy(out->chain_code, chain_code, 32);
|
2014-01-21 17:38:44 +00:00
|
|
|
memset(out->private_key, 0, 32);
|
2013-11-25 14:37:49 +00:00
|
|
|
memcpy(out->public_key, public_key, 33);
|
2014-08-07 12:34:33 +00:00
|
|
|
return 1;
|
2013-11-25 14:37:49 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 18:54:01 +00:00
|
|
|
int hdnode_from_xprv(uint32_t depth, uint32_t fingerprint, uint32_t child_num, const uint8_t *chain_code, const uint8_t *private_key, HDNode *out)
|
2013-09-12 17:21:24 +00:00
|
|
|
{
|
2014-08-07 12:34:33 +00:00
|
|
|
bignum256 a;
|
|
|
|
bn_read_be(private_key, &a);
|
|
|
|
if (bn_is_zero(&a) || !bn_is_less(&a, &order256k1)) { // == 0 or >= order
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-21 17:38:44 +00:00
|
|
|
out->depth = depth;
|
|
|
|
out->fingerprint = fingerprint;
|
|
|
|
out->child_num = child_num;
|
|
|
|
memcpy(out->chain_code, chain_code, 32);
|
|
|
|
memcpy(out->private_key, private_key, 32);
|
|
|
|
hdnode_fill_public_key(out);
|
2014-08-07 12:34:33 +00:00
|
|
|
return 1;
|
2014-01-21 17:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 18:54:01 +00:00
|
|
|
int hdnode_from_seed(const uint8_t *seed, int seed_len, HDNode *out)
|
2014-01-21 17:38:44 +00:00
|
|
|
{
|
|
|
|
uint8_t I[32 + 32];
|
2014-05-20 22:15:55 +00:00
|
|
|
memset(out, 0, sizeof(HDNode));
|
2013-09-12 17:21:24 +00:00
|
|
|
out->depth = 0;
|
|
|
|
out->fingerprint = 0x00000000;
|
|
|
|
out->child_num = 0;
|
2014-01-21 17:38:44 +00:00
|
|
|
hmac_sha512((uint8_t *)"Bitcoin seed", 12, seed, seed_len, I);
|
|
|
|
memcpy(out->private_key, I, 32);
|
2014-08-07 12:34:33 +00:00
|
|
|
bignum256 a;
|
|
|
|
bn_read_be(out->private_key, &a);
|
|
|
|
if (bn_is_zero(&a) || !bn_is_less(&a, &order256k1)) { // == 0 or >= order
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(out->chain_code, I + 32, 32);
|
2013-11-25 14:37:49 +00:00
|
|
|
hdnode_fill_public_key(out);
|
2014-08-07 12:34:33 +00:00
|
|
|
return 1;
|
2013-09-12 17:21:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 13:35:13 +00:00
|
|
|
int hdnode_private_ckd(HDNode *inout, uint32_t i)
|
2013-09-12 17:21:24 +00:00
|
|
|
{
|
|
|
|
uint8_t data[1 + 32 + 4];
|
2014-01-14 13:35:13 +00:00
|
|
|
uint8_t I[32 + 32];
|
2014-01-14 14:29:46 +00:00
|
|
|
uint8_t fingerprint[32];
|
2013-09-12 17:21:24 +00:00
|
|
|
bignum256 a, b;
|
|
|
|
|
2013-11-25 14:37:49 +00:00
|
|
|
if (i & 0x80000000) { // private derivation
|
2013-09-12 17:21:24 +00:00
|
|
|
data[0] = 0;
|
|
|
|
memcpy(data + 1, inout->private_key, 32);
|
2013-11-25 14:37:49 +00:00
|
|
|
} else { // public derivation
|
|
|
|
memcpy(data, inout->public_key, 33);
|
2013-09-12 17:21:24 +00:00
|
|
|
}
|
|
|
|
write_be(data + 33, i);
|
|
|
|
|
2014-02-19 20:26:42 +00:00
|
|
|
sha256_Raw(inout->public_key, 33, fingerprint);
|
2014-01-14 14:29:46 +00:00
|
|
|
ripemd160(fingerprint, 32, fingerprint);
|
|
|
|
inout->fingerprint = (fingerprint[0] << 24) + (fingerprint[1] << 16) + (fingerprint[2] << 8) + fingerprint[3];
|
|
|
|
|
2013-09-12 17:21:24 +00:00
|
|
|
bn_read_be(inout->private_key, &a);
|
|
|
|
|
2014-01-14 13:35:13 +00:00
|
|
|
hmac_sha512(inout->chain_code, 32, data, sizeof(data), I);
|
|
|
|
memcpy(inout->chain_code, I + 32, 32);
|
2014-01-21 17:38:44 +00:00
|
|
|
memcpy(inout->private_key, I, 32);
|
2013-09-12 17:21:24 +00:00
|
|
|
|
|
|
|
bn_read_be(inout->private_key, &b);
|
2014-08-07 12:34:33 +00:00
|
|
|
|
|
|
|
if (!bn_is_less(&b, &order256k1)) { // >= order
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-12 17:21:24 +00:00
|
|
|
bn_addmod(&a, &b, &order256k1);
|
|
|
|
|
2014-08-07 12:34:33 +00:00
|
|
|
if (bn_is_zero(&a)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-12 17:21:24 +00:00
|
|
|
inout->depth++;
|
|
|
|
inout->child_num = i;
|
|
|
|
bn_write_be(&a, inout->private_key);
|
|
|
|
|
2013-11-25 14:37:49 +00:00
|
|
|
hdnode_fill_public_key(inout);
|
2014-02-19 15:58:48 +00:00
|
|
|
|
2014-01-14 13:35:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hdnode_public_ckd(HDNode *inout, uint32_t i)
|
|
|
|
{
|
|
|
|
uint8_t data[1 + 32 + 4];
|
|
|
|
uint8_t I[32 + 32];
|
2014-01-14 14:29:46 +00:00
|
|
|
uint8_t fingerprint[32];
|
2014-01-14 13:35:13 +00:00
|
|
|
curve_point a, b;
|
|
|
|
bignum256 c;
|
|
|
|
|
|
|
|
if (i & 0x80000000) { // private derivation
|
|
|
|
return 0;
|
|
|
|
} else { // public derivation
|
|
|
|
memcpy(data, inout->public_key, 33);
|
|
|
|
}
|
|
|
|
write_be(data + 33, i);
|
|
|
|
|
2014-02-19 20:26:42 +00:00
|
|
|
sha256_Raw(inout->public_key, 33, fingerprint);
|
2014-01-14 14:29:46 +00:00
|
|
|
ripemd160(fingerprint, 32, fingerprint);
|
|
|
|
inout->fingerprint = (fingerprint[0] << 24) + (fingerprint[1] << 16) + (fingerprint[2] << 8) + fingerprint[3];
|
|
|
|
|
2014-01-14 13:35:13 +00:00
|
|
|
memset(inout->private_key, 0, 32);
|
|
|
|
if (!ecdsa_read_pubkey(inout->public_key, &a)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hmac_sha512(inout->chain_code, 32, data, sizeof(data), I);
|
|
|
|
memcpy(inout->chain_code, I + 32, 32);
|
|
|
|
bn_read_be(I, &c);
|
2014-08-07 12:34:33 +00:00
|
|
|
|
|
|
|
if (!bn_is_less(&c, &order256k1)) { // >= order
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-14 13:35:13 +00:00
|
|
|
scalar_multiply(&c, &b); // b = c * G
|
|
|
|
point_add(&a, &b); // b = a + b
|
2014-08-07 12:34:33 +00:00
|
|
|
|
|
|
|
#if USE_PUBKEY_VALIDATE
|
|
|
|
if (!ecdsa_validate_pubkey(&b)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-14 13:35:13 +00:00
|
|
|
inout->public_key[0] = 0x02 | (b.y.val[0] & 0x01);
|
|
|
|
bn_write_be(&b.x, inout->public_key + 1);
|
|
|
|
|
|
|
|
inout->depth++;
|
|
|
|
inout->child_num = i;
|
|
|
|
|
|
|
|
return 1;
|
2013-11-08 15:02:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 18:09:00 +00:00
|
|
|
#if USE_BIP32_CACHE
|
|
|
|
|
|
|
|
static bool private_ckd_cache_root_set = false;
|
|
|
|
static HDNode private_ckd_cache_root;
|
|
|
|
static int private_ckd_cache_index = 0;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
bool set;
|
|
|
|
size_t depth;
|
|
|
|
uint32_t i[BIP32_CACHE_MAXDEPTH];
|
|
|
|
HDNode node;
|
|
|
|
} private_ckd_cache[BIP32_CACHE_SIZE];
|
|
|
|
|
|
|
|
int hdnode_private_ckd_cached(HDNode *inout, const uint32_t *i, size_t i_count)
|
|
|
|
{
|
|
|
|
if (i_count == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (i_count == 1) {
|
|
|
|
if (hdnode_private_ckd(inout, i[0]) == 0) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
// if root is not set or not the same
|
|
|
|
if (!private_ckd_cache_root_set || memcmp(&private_ckd_cache_root, inout, sizeof(HDNode)) != 0) {
|
|
|
|
// clear the cache
|
|
|
|
private_ckd_cache_index = 0;
|
|
|
|
memset(private_ckd_cache, 0, sizeof(private_ckd_cache));
|
|
|
|
// setup new root
|
|
|
|
memcpy(&private_ckd_cache_root, inout, sizeof(HDNode));
|
|
|
|
private_ckd_cache_root_set = true;
|
|
|
|
} else {
|
|
|
|
// try to find parent
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < BIP32_CACHE_SIZE; j++) {
|
|
|
|
if (private_ckd_cache[j].set &&
|
|
|
|
private_ckd_cache[j].depth == i_count - 1 &&
|
|
|
|
memcmp(private_ckd_cache[j].i, i, (i_count - 1) * sizeof(uint32_t)) == 0) {
|
|
|
|
memcpy(inout, &(private_ckd_cache[j].node), sizeof(HDNode));
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// else derive parent
|
|
|
|
if (!found) {
|
|
|
|
size_t k;
|
|
|
|
for (k = 0; k < i_count - 1; k++) {
|
|
|
|
if (hdnode_private_ckd(inout, i[k]) == 0) return 0;
|
|
|
|
}
|
|
|
|
// and save it
|
2015-03-04 14:43:14 +00:00
|
|
|
memset(&(private_ckd_cache[private_ckd_cache_index]), 0, sizeof(private_ckd_cache[private_ckd_cache_index]));
|
2015-01-26 18:09:00 +00:00
|
|
|
private_ckd_cache[private_ckd_cache_index].set = true;
|
|
|
|
private_ckd_cache[private_ckd_cache_index].depth = i_count - 1;
|
|
|
|
memcpy(private_ckd_cache[private_ckd_cache_index].i, i, (i_count - 1) * sizeof(uint32_t));
|
|
|
|
memcpy(&(private_ckd_cache[private_ckd_cache_index].node), inout, sizeof(HDNode));
|
|
|
|
private_ckd_cache_index = (private_ckd_cache_index + 1) % BIP32_CACHE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdnode_private_ckd(inout, i[i_count - 1]) == 0) return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-01-21 17:38:44 +00:00
|
|
|
void hdnode_fill_public_key(HDNode *node)
|
2013-11-08 15:02:48 +00:00
|
|
|
{
|
2014-01-21 17:38:44 +00:00
|
|
|
ecdsa_get_public_key33(node->private_key, node->public_key);
|
2013-11-25 14:37:49 +00:00
|
|
|
}
|
2014-05-15 15:36:23 +00:00
|
|
|
|
2014-12-23 00:17:58 +00:00
|
|
|
void hdnode_serialize(const HDNode *node, uint32_t version, char use_public, char *str, int strsize)
|
2014-05-15 15:36:23 +00:00
|
|
|
{
|
2014-05-22 19:52:04 +00:00
|
|
|
uint8_t node_data[78];
|
2014-05-15 15:36:23 +00:00
|
|
|
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(node_data + 13, node->chain_code, 32);
|
|
|
|
if (use_public) {
|
|
|
|
memcpy(node_data + 45, node->public_key, 33);
|
|
|
|
} else {
|
|
|
|
node_data[45] = 0;
|
|
|
|
memcpy(node_data + 46, node->private_key, 32);
|
|
|
|
}
|
2014-12-23 00:17:58 +00:00
|
|
|
base58_encode_check(node_data, 78, str, strsize);
|
2014-05-15 15:36:23 +00:00
|
|
|
}
|
2014-05-15 17:35:28 +00:00
|
|
|
|
2014-12-23 00:17:58 +00:00
|
|
|
void hdnode_serialize_public(const HDNode *node, char *str, int strsize)
|
2014-05-15 17:35:28 +00:00
|
|
|
{
|
2014-12-23 00:17:58 +00:00
|
|
|
hdnode_serialize(node, 0x0488B21E, 1, str, strsize);
|
2014-05-15 17:35:28 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 00:17:58 +00:00
|
|
|
void hdnode_serialize_private(const HDNode *node, char *str, int strsize)
|
2014-05-15 17:35:28 +00:00
|
|
|
{
|
2014-12-23 00:17:58 +00:00
|
|
|
hdnode_serialize(node, 0x0488ADE4, 0, str, strsize);
|
2014-05-20 22:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for validity of curve point in case of public data not performed
|
|
|
|
int hdnode_deserialize(const char *str, HDNode *node)
|
|
|
|
{
|
2014-05-22 19:52:04 +00:00
|
|
|
uint8_t node_data[78];
|
2014-05-20 22:15:55 +00:00
|
|
|
memset(node, 0, sizeof(HDNode));
|
2014-12-23 00:17:58 +00:00
|
|
|
if (!base58_decode_check(str, node_data, sizeof(node_data))) {
|
2014-05-20 22:15:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint32_t version = read_be(node_data);
|
|
|
|
if (version == 0x0488B21E) { // public node
|
|
|
|
memcpy(node->public_key, node_data + 45, 33);
|
|
|
|
} else if (version == 0x0488ADE4) { // private node
|
|
|
|
if (node_data[45]) { // invalid data
|
2014-05-22 19:52:04 +00:00
|
|
|
return -2;
|
2014-05-20 22:15:55 +00:00
|
|
|
}
|
|
|
|
memcpy(node->private_key, node_data + 46, 32);
|
|
|
|
hdnode_fill_public_key(node);
|
|
|
|
} else {
|
2014-05-22 19:52:04 +00:00
|
|
|
return -3; // invalid version
|
2014-05-20 22:15:55 +00:00
|
|
|
}
|
|
|
|
node->depth = node_data[4];
|
|
|
|
node->fingerprint = read_be(node_data + 5);
|
|
|
|
node->child_num = read_be(node_data + 9);
|
|
|
|
memcpy(node->chain_code, node_data + 13, 32);
|
|
|
|
return 0;
|
2014-05-15 17:35:28 +00:00
|
|
|
}
|