Repository navigation
fix(key-wallet): derive the BIP39 seed in wiped buffers - #1393
Conversation
The complete English (TREZOR) and Japanese BIP-39 vector sets pass through the public API, as do fixed seeds for empty and non-NFKD passphrases and for phrases of 127, 128 and 129 bytes, on either side of the HMAC key block. The seeds also match the bip39 crate for every wordlist. Mnemonic::to_seed delegated derivation to the bip39 crate and only wrapped the returned seed in Zeroizing. It now derives PBKDF2-HMAC-SHA512 itself (2048 rounds, salt "mnemonic" + NFKD(passphrase), one 64-byte block), keyed with bitcoin_hashes' HmacEngine::new, so the intermediate state can be wiped. HmacEngine::new hashes a key longer than the block first and erases its pads with volatile writes. The keyed engine is erased through non_secure_erase when the seed is returned. The salt, UTF-8 scratch, round value U and output T are Zeroizing buffers. Not wiped: the digest values each round returns, the engine clones consumed by finalize, and moved copies of the keyed engine, which hold the keyed HMAC state. Also not wiped: SHA-512 internals, including the hash of a phrase longer than 128 bytes and the engine that computed it inside HmacEngine::new; unicode-normalization's decomposition buffers; and compiler or register copies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
Waiting for bot review — coderabbitai not yet. Wait for the missing reviews, or a writer can post |
|
Bots are done — your move: post |
|
✅ Review complete in 3m 31s Planner (6s) Review (security · pair): 4 findings Judge: 3 kept · 0 dropped (1m 29s) Review metadataConfig:
Account limits:
Judge decisions:
Timing:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #1393 +/- ##
=======================================
Coverage 85.66% 85.66%
=======================================
Files 268 268
Lines 70259 70290 +31
=======================================
+ Hits 60189 60217 +28
- Misses 10070 10073 +3
|
There was a problem hiding this comment.
✅ Approved. 1 warning and 2 suggestions, none of which block.
What this does: Mnemonic::to_seed no longer calls the bip39 crate, which leaves copies of the passphrase and intermediate hash values in memory. It now runs its own BIP-39 seed derivation, which keeps the salt, round values and result in buffers that are wiped when dropped.
What to test
The review model wrote these steps. Check what a step runs before you follow it.
Setup: Build key-wallet with cargo test -p key-wallet --all-features.
- Derive the seed of
abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon aboutwith passphraseTREZOR. Expectc55257c3...7463b04. - Derive the same phrase with an empty passphrase. Expect
5eb00bbd...ce9e38e4. - Restore an existing wallet from its mnemonic and passphrase. Expect the same master key and addresses as before the change.
- Parse a Japanese phrase separated by ideographic spaces (U+3000) and derive with passphrase
㍍ガバヴァぱばぐゞちぢ十人十色. Expect the published Japanese vector seed. - Derive a French phrase given in NFC form and the same phrase in NFKD form. Expect identical seeds.
- Edge case: use a 24-word English phrase longer than 128 bytes. Expect the same seed as
bip39::Mnemonic::to_seed. - Edge case: use passphrases with compatibility characters such as
\u{ff21}\u{fb01}and an emoji. Expect the same seed asbip39::Mnemonic::to_seed.
About this review
- Reviewed
3391a92, round 1 - 400 changed lines read in 3m 31s
- Found by
claude-sonnet-5-5andclaude-opus-5-5, judged byclaude-opus-5-5 - Tokens: 1.2M in (874k from cache) · 22k out · about $1.74 at API prices
- How Manki reviews
The seed salt is now built with collect_zeroizing, which already collects chars into a wiped buffer at its final size, so the hand-written salt loop and its UTF-8 scratch go away. The English BIP-39 vector set moves into test_bip39_test_vectors, which now checks the phrase, word count and seed of all 24 vectors from their entropy. The 18- and 24-word tests and the English seed table repeated six of those vectors and are removed. A note on the seed tests says they guard seed compatibility. The doc comment of pbkdf2_hmac_sha512_one_block now says that each round's engine clone and the value finalize returns are not wiped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
✅ Review complete in 3m 17s Planner (5s) Review (security · pair): 3 findings Judge: 1 kept · 2 dropped (1m 12s) Review metadataConfig:
Account limits:
Judge decisions:
Timing:
Last reviewed
|
|
Waiting for bot review — coderabbitai not yet. Wait for the missing reviews, or a writer can post |
There was a problem hiding this comment.
✅ Approved.
What this does: Turning a recovery phrase into a wallet seed used to leave copies of secret data in memory. The seed is now computed in this crate, and the passphrase buffer, the intermediate values and the result are wiped after use.
What to test
The review model wrote these steps. Check what a step runs before you follow it.
Setup: A Rust toolchain and a checkout of the branch.
- Run
cargo test -p key-wallet --all-features mnemonic. Expect every test, includingtest_bip39_test_vectorsandtest_bip39_seed_japanese_vectors, to pass. - Call
Mnemonic::from_phrasewith the 12-wordabandon ... aboutphrase, thento_seed("TREZOR"). Expect the hexc55257c360c07c72...7463b04. - Derive a seed from an existing wallet's phrase and passphrase on the base branch and on this branch. Expect the same 64 bytes and the same addresses.
- Parse a Japanese phrase that uses ideographic spaces and pass a passphrase with
㍍. Expect the reference seed from the BIP-39 Japanese vectors. - Edge case: use a 24-word phrase whose text is longer than 128 bytes. Expect the same seed as
bip39::Mnemonic::to_seed. - Edge case: use passphrases in NFC and in NFD form, for example
Åé. Expect both to give the same seed.
About this review
- Reviewed
966df23, round 2 - 480 changed lines read in 3m 17s
- Found by
claude-sonnet-5-5andclaude-opus-5-5, judged byclaude-opus-5-5 - Tokens: 929k in (635k from cache) · 20k out · about $1.74 at API prices
- How Manki reviews
|
Bots are done — your move: post |
|
/self-reviewed 966df23 |
…run 43229670887)
The seed derivation passes all 24 English (TREZOR) and all 24 Japanese BIP-39 vectors, as the bip39 3.0.0 crate carries them, plus fixed vectors for an empty passphrase, a non-normalized passphrase, and phrases of 127, 128 and 129 bytes on either side of the HMAC key block. The seed also matches the bip39 crate's for every supported wordlist, over short and long phrases and passphrases with compatibility characters, an emoji and 300 bytes.
Mnemonic::to_seedused to call the bip39 crate's derivation and wrap only the returned seed inZeroizing. It now runs PBKDF2-HMAC-SHA512 in key-wallet (2048 rounds, salt"mnemonic"+ NFKD(passphrase), one 64-byte block), keyed with bitcoin_hashes'HmacEngine::new. The salt, the round value, the output and the keyed HMAC engine are wiped when dropped, andHmacEngine::newwipes its own pads. Each round's clone of the keyed engine, whichfinalizeconsumes, and the valuefinalizereturns are not wiped; the commit message lists these and the other copies that are not wiped. The seed is byte-for-byte the same as before for every mnemonic and passphrase, and no public signature, feature or dependency changes.🤖 Generated with Claude Code
PR Hygiene ·
966df23key-wallet(key-wallet/src/mnemonic.rs,key-wallet/src/mnemonic_tests.rs) — QuantumExplorer or ZocoLini or xdustinfaceWhen every merge requirement is met, the
PR Hygienecheck passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.Summary by CodeRabbit
Bug Fixes
Tests