Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/wallet/keys/hd_private.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,17 @@ hd_private hd_private::from_key(const hd_key& key, uint64_t prefixes) NOEXCEPT
const auto parent = source.read_4_bytes_big_endian();
const auto child = source.read_4_bytes_big_endian();
const auto chain = source.read_hash();
source.skip_byte();
const auto padding = source.read_byte();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a long time since I've looked at this, but isn't the skip_byte or padding check only required in the hardened key case? Quick glance I'm not seeing that distinction, so just curious if you can point me to that if it's correct.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct, the 0x00 byte used as HMAC input is only required for hardened derivation, we can see that in hd_private::derive_private().

But this skip_byte() is not in derivation. It is in parsing serialized extended private key data, where BIP32 always encodes private key data as 0x00 || ser256(k), regardless of whether the extended private key is a master key, a hardened child, or a non-hardened child.

I've updated the title to clarify that the byte-reading change relates to the serialized format and not key derivation. Thanks for pointing that out.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for satisfying my curiosity, makes a lot more sense now!

const auto secret = source.read_hash();

// Validate the prefix against the provided value.
if (prefix != to_prefix(prefixes))
return {};

// Validate the key padding.
if (padding != 0x00)
return {};

const hd_lineage lineage
{
prefixes,
Expand Down
8 changes: 8 additions & 0 deletions test/wallet/keys/hd_private.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ BOOST_AUTO_TEST_CASE(hd_private__constructor__null_key__decodes_to_invalid)
BOOST_REQUIRE(!xprv_null);
}

BOOST_AUTO_TEST_CASE(hd_private__constructor__nonzero_private_key_padding__invalid)
{
static const auto encoded = "tprvAk8SVSmAT8FFvWjCNUB546H9tsmRk5bii5WZvSY2zSnxSg4uuLFspzTZffwYUoYXqsP7WDdHTRS92WJzeXTki31ftCwzYCEnLAPt9a7aJyV";

const hd_private xprv_invalid(encoded);
BOOST_REQUIRE(!xprv_invalid);
}

BOOST_AUTO_TEST_CASE(hd_private__to_public__from_invalid_private__invalid)
{
// the 11...14rcJhr is a serialization of a null key;
Expand Down
Loading