1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 12:28:09 +00:00

bip32: deserialize the fingerprint

This commit is contained in:
Jan Pochyla 2016-10-14 14:33:49 +02:00
parent 0a158f6c7d
commit f45bcc65f2
4 changed files with 30 additions and 27 deletions

View File

@ -474,7 +474,7 @@ int hdnode_serialize_private(const HDNode *node, uint32_t fingerprint, char *str
}
// check for validity of curve point in case of public data not performed
int hdnode_deserialize(const char *str, HDNode *node)
int hdnode_deserialize(const char *str, HDNode *node, uint32_t *fingerprint)
{
uint8_t node_data[78];
memset(node, 0, sizeof(HDNode));
@ -496,6 +496,9 @@ int hdnode_deserialize(const char *str, HDNode *node)
return -3; // invalid version
}
node->depth = node_data[4];
if (fingerprint) {
*fingerprint = read_be(node_data + 5);
}
node->child_num = read_be(node_data + 9);
memcpy(node->chain_code, node_data + 13, 32);
return 0;

View File

@ -78,7 +78,7 @@ int hdnode_serialize_public(const HDNode *node, uint32_t fingerprint, char *str,
int hdnode_serialize_private(const HDNode *node, uint32_t fingerprint, char *str, int strsize);
int hdnode_deserialize(const char *str, HDNode *node);
int hdnode_deserialize(const char *str, HDNode *node, uint32_t *fingerprint);
// Private
int hdnode_serialize(const HDNode *node, uint32_t fingerprint, uint32_t version, char use_public, char *str, int strsize);

48
tests.c
View File

@ -492,12 +492,12 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.public_key, fromhex("0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -512,12 +512,12 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.public_key, fromhex("035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -532,12 +532,12 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.public_key, fromhex("03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -552,12 +552,12 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.public_key, fromhex("0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -572,12 +572,12 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.public_key, fromhex("02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -592,12 +592,12 @@ START_TEST(test_bip32_vector_1)
ck_assert_mem_eq(node.public_key, fromhex("022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -624,12 +624,12 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.public_key, fromhex("03cbcaa9c98c877a26977d00825c956a238e8dddfbd322cce4f74b0b5bd6ace4a7"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -645,12 +645,12 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.public_key, fromhex("02fc9e5af0ac8d9b3cecfe2a888e2117ba3d089d8585886c9c826b6b22a98d12ea"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -666,12 +666,12 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.public_key, fromhex("03c01e7425647bdefa82b12d9bad5e3e6865bee0502694b94ca58b666abc0a5c3b"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -687,12 +687,12 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.public_key, fromhex("03a7d1d856deb74c508e05031f9895dab54626251b3806e16b4bd12e781a7df5b9"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -708,12 +708,12 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.public_key, fromhex("02d2b36900396c9282fa14628566582f206a5dd0bcc8d5e892611806cafb0301f0"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));
@ -729,12 +729,12 @@ START_TEST(test_bip32_vector_2)
ck_assert_mem_eq(node.public_key, fromhex("024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c"), 33);
hdnode_serialize_private(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
hdnode_fill_public_key(&node2);
ck_assert_mem_eq(&node, &node2, sizeof(HDNode));
hdnode_serialize_public(&node, fingerprint, str, sizeof(str));
ck_assert_str_eq(str, "xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt");
r = hdnode_deserialize(str, &node2); ck_assert_int_eq(r, 0);
r = hdnode_deserialize(str, &node2, NULL); ck_assert_int_eq(r, 0);
memcpy(&node3, &node, sizeof(HDNode));
memset(&node3.private_key, 0, 32);
ck_assert_mem_eq(&node2, &node3, sizeof(HDNode));

View File

@ -8,7 +8,7 @@
void process_job(uint32_t jobid, const char *xpub, uint32_t change, uint32_t from, uint32_t to)
{
HDNode node, child;
if (change > 1 || to <= from || hdnode_deserialize(xpub, &node) != 0) {
if (change > 1 || to <= from || hdnode_deserialize(xpub, &node, NULL) != 0) {
printf("%d error\n", jobid);
return;
}