Skip to content

Add padding byte validation during extended private key de-serialization to match bip 32's spec#1871

Merged
thecodefactory merged 1 commit into
libbitcoin:masterfrom
KY-U:2026-06-validate-padding-byte-hd-master-key
Jun 9, 2026
Merged

Add padding byte validation during extended private key de-serialization to match bip 32's spec#1871
thecodefactory merged 1 commit into
libbitcoin:masterfrom
KY-U:2026-06-validate-padding-byte-hd-master-key

Conversation

@KY-U

@KY-U KY-U commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Validate padding byte is 0 in extended private key de-serialization in order to match BIP 32's spec.

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!

@KY-U KY-U changed the title Add padding byte validation to match bip 32's spec Add padding byte validation during extended private key de-serialization to match bip 32's spec Jun 9, 2026

@thecodefactory thecodefactory left a comment

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.

LGTM

@thecodefactory thecodefactory merged commit 94462a6 into libbitcoin:master Jun 9, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants