Skip to content

Validate BIP32 seed length before master key generation - #162

Open
Harshil-1603 wants to merge 5 commits into
diybitcoinhardware:masterfrom
Harshil-1603:fix/bip32-seed-length
Open

Harshil-1603 wants to merge 5 commits into
diybitcoinhardware:masterfrom
Harshil-1603:fix/bip32-seed-length

Conversation

@Harshil-1603

Copy link
Copy Markdown

Fixes #127.

HDKey.from_seed() currently derives a master key even from an empty seed. Validate the length before HMAC generation and raise HDError outside the 16–64 byte range specified by BIP32. Update the docstring, API documentation, and changelog to describe the accepted range.

Tests reject lengths 0, 1, 15, 65, and 128, and accept 16, 17, 32, 63, and 64 bytes. Existing BIP32 derivation vectors remain unchanged. This tightens behavior for callers that previously supplied seeds outside the BIP32 range.

Validation on CPython 3.11.15 with the repository's hash-pinned CI dependencies:

  • The rejection regression failed before the fix; the valid-length control passed before and after.
  • Full pytest suite: 125 passed, 17 skipped.
  • Source distribution and wheel build, repository artifact verification, and installed-wheel smoke test passed.
  • Black 26.3.1 checks passed for the changed library module and new test methods; existing formatting elsewhere in the test file was left unchanged. git diff --check passed.

MicroPython and the other Python versions in the CI matrix were not run locally.

AI assistance: implementation and validation were assisted by OpenAI Codex (gpt-6.1-sol).

Assisted-by: OpenAI gpt-6.1-sol
Assisted-by: OpenAI gpt-6.1-sol
@fametrano

Copy link
Copy Markdown

Tested ACK 025a993: embit now raises HDError for every seed length the new tests reject, as BIP32 requires and btclib-wallet does, and every length they accept gives the same xprv as btclib-wallet. I ran those lengths through both libraries, and the full suite on Python 3.12, where the rejection test fails on master; details on request.

jdlcdl
jdlcdl previously approved these changes Nov 6, 2026
@jdlcdl

jdlcdl commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

ACK at 025a993 with intention to squash into 1 commit, rebase and merge atop branch "hardened"

In the context of generating a master wallet, bip32 clearly states:

Generate a seed byte sequence S of a chosen length (between 128 and 512 bits; 256 bits is advised) from a (P)RNG

odudex
odudex previously approved these changes Nov 10, 2026

@odudex odudex left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Fix is correct
Some nits, none blocking:

  1. Changelog section. The entry sits under ### Fixed, but it's a behaviour change that can break callers. hardened files similar "now rejects" entries under ### Changed, so I'd move it there while resolving the merge conflict (to hardened branch).
  2. Could be squashed. Squashing the 4 commits on merge could also make the changelog conflict disappear into one resolution.

@Harshil-1603
Harshil-1603 dismissed stale reviews from odudex and jdlcdl via fb791e0 November 11, 2026 02:59
@jdlcdl

jdlcdl commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

ACK at fb791e0 with intention to merge all 5 commits into "hardened", (two commit conflicts resolved for CHANGELOG.md by me, so really 4 commits plus merge-commit) just after pr124

Thank you harshil, odudex, and fametrano.

@odudex

odudex commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

ACK at fb791e0

@miketlk miketlk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All looks good to me, but I'd squash the two latest commits modifying CHANGELOG.md.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BIP32: Missing seed bit-length validation in master key generation

5 participants