1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-29 19:08:12 +00:00

use both private and public bip32 versions

This commit is contained in:
Pavol Rusnak 2014-02-19 16:58:48 +01:00
parent e17e0590f5
commit 1034b28f90
3 changed files with 26 additions and 13 deletions

23
bip32.c
View File

@ -7,9 +7,11 @@
#include "sha2.h" #include "sha2.h"
#include "ripemd160.h" #include "ripemd160.h"
void hdnode_from_xpub(uint8_t version_byte, uint32_t version, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *public_key, HDNode *out) void hdnode_from_xpub(uint8_t version_byte, uint32_t version_private, uint32_t version_public, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *public_key, HDNode *out)
{ {
out->version = version; out->version = version_public;
out->version_private = version_private;
out->version_public = version_public;
out->depth = depth; out->depth = depth;
out->fingerprint = fingerprint; out->fingerprint = fingerprint;
out->child_num = child_num; out->child_num = child_num;
@ -20,9 +22,11 @@ void hdnode_from_xpub(uint8_t version_byte, uint32_t version, uint32_t depth, ui
hdnode_fill_address(out); hdnode_fill_address(out);
} }
void hdnode_from_xprv(uint8_t version_byte, uint32_t version, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *private_key, HDNode *out) void hdnode_from_xprv(uint8_t version_byte, uint32_t version_private, uint32_t version_public, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *private_key, HDNode *out)
{ {
out->version = version; out->version = version_private;
out->version_private = version_private;
out->version_public = version_public;
out->depth = depth; out->depth = depth;
out->fingerprint = fingerprint; out->fingerprint = fingerprint;
out->child_num = child_num; out->child_num = child_num;
@ -33,10 +37,12 @@ void hdnode_from_xprv(uint8_t version_byte, uint32_t version, uint32_t depth, ui
hdnode_fill_address(out); hdnode_fill_address(out);
} }
void hdnode_from_seed(uint8_t version_byte, uint32_t version, uint8_t *seed, int seed_len, HDNode *out) void hdnode_from_seed(uint8_t version_byte, uint32_t version_private, uint32_t version_public, uint8_t *seed, int seed_len, HDNode *out)
{ {
uint8_t I[32 + 32]; uint8_t I[32 + 32];
out->version = version; out->version = version_private;
out->version_private = version_private;
out->version_public = version_public;
out->depth = 0; out->depth = 0;
out->fingerprint = 0x00000000; out->fingerprint = 0x00000000;
out->child_num = 0; out->child_num = 0;
@ -55,6 +61,8 @@ int hdnode_private_ckd(HDNode *inout, uint32_t i)
uint8_t fingerprint[32]; uint8_t fingerprint[32];
bignum256 a, b; bignum256 a, b;
inout->version = inout->version_private;
if (i & 0x80000000) { // private derivation if (i & 0x80000000) { // private derivation
data[0] = 0; data[0] = 0;
memcpy(data + 1, inout->private_key, 32); memcpy(data + 1, inout->private_key, 32);
@ -82,6 +90,7 @@ int hdnode_private_ckd(HDNode *inout, uint32_t i)
hdnode_fill_public_key(inout); hdnode_fill_public_key(inout);
hdnode_fill_address(inout); hdnode_fill_address(inout);
return 1; return 1;
} }
@ -93,6 +102,8 @@ int hdnode_public_ckd(HDNode *inout, uint32_t i)
curve_point a, b; curve_point a, b;
bignum256 c; bignum256 c;
inout->version = inout->version_public;
if (i & 0x80000000) { // private derivation if (i & 0x80000000) { // private derivation
return 0; return 0;
} else { // public derivation } else { // public derivation

10
bip32.h
View File

@ -11,15 +11,17 @@ typedef struct {
uint8_t chain_code[32]; uint8_t chain_code[32];
uint8_t private_key[32]; uint8_t private_key[32];
uint8_t public_key[33]; uint8_t public_key[33];
uint8_t version_byte;
char address[35]; char address[35];
uint8_t version_byte;
uint32_t version_private;
uint32_t version_public;
} HDNode; } HDNode;
void hdnode_from_xpub(uint8_t version_byte, uint32_t version, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *public_key, HDNode *out); void hdnode_from_xpub(uint8_t version_byte, uint32_t version_private, uint32_t version_public, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *public_key, HDNode *out);
void hdnode_from_xprv(uint8_t version_byte, uint32_t version, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *private_key, HDNode *out); void hdnode_from_xprv(uint8_t version_byte, uint32_t version_private, uint32_t version_public, uint32_t depth, uint32_t fingerprint, uint32_t child_num, uint8_t *chain_code, uint8_t *private_key, HDNode *out);
void hdnode_from_seed(uint8_t version_byte, uint32_t version, uint8_t *seed, int seed_len, HDNode *out); void hdnode_from_seed(uint8_t version_byte, uint32_t version_private, uint32_t version_public, uint8_t *seed, int seed_len, HDNode *out);
#define hdnode_private_ckd_prime(X, I) hdnode_private_ckd((X), ((I) | 0x80000000)) #define hdnode_private_ckd_prime(X, I) hdnode_private_ckd((X), ((I) | 0x80000000))

View File

@ -80,7 +80,7 @@ START_TEST(test_bip32_vector_1)
HDNode node; HDNode node;
// init m // init m
hdnode_from_seed(0x00, 0x0488B21E, fromhex("000102030405060708090a0b0c0d0e0f"), 16, &node); hdnode_from_seed(0x00, 0x0488ADE4, 0x0488B21E, fromhex("000102030405060708090a0b0c0d0e0f"), 16, &node);
// [Chain m] // [Chain m]
ck_assert_int_eq(node.fingerprint, 0x00000000); ck_assert_int_eq(node.fingerprint, 0x00000000);
@ -138,7 +138,7 @@ START_TEST(test_bip32_vector_2)
int r; int r;
// init m // init m
hdnode_from_seed(0x00, 0x0488B21E, fromhex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"), 64, &node); hdnode_from_seed(0x00, 0x0488ADE4, 0x0488B21E, fromhex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"), 64, &node);
// [Chain m] // [Chain m]
ck_assert_int_eq(node.fingerprint, 0x00000000); ck_assert_int_eq(node.fingerprint, 0x00000000);
@ -193,7 +193,7 @@ START_TEST(test_bip32_vector_2)
ck_assert_str_eq(node.address, "14UKfRV9ZPUp6ZC9PLhqbRtxdihW9em3xt"); ck_assert_str_eq(node.address, "14UKfRV9ZPUp6ZC9PLhqbRtxdihW9em3xt");
// init m // init m
hdnode_from_seed(0x00, 0x0488B21E, fromhex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"), 64, &node); hdnode_from_seed(0x00, 0x0488ADE4, 0x0488B21E, fromhex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"), 64, &node);
// test public derivation // test public derivation
// [Chain m/0] // [Chain m/0]