Merge bitcoin/bitcoin#35320: key: validate BIP32 seed length in CExtKey::SetSeed

2cf9d79d84 key: validate BIP32 seed length in CExtKey::SetSeed (Muhammad)

Pull request description:

  BIP32 specifies that the seed must be between 128 and 512 bits (16 to 64 bytes). CExtKey::SetSeed currently accepts any length, which could result in weak master keys being generated.
  Add an Assert at the start of SetSeed to enforce the valid seed length range as a programming invariant. The existing BIP32 test vectors already provide sufficient coverage of valid seed lengths.

  To test:

  > cmake --build build -j --target test_bitcoin
  ./build/bin/test_bitcoin --run_test=bip32_tests

  Fixes #35308

ACKs for top commit:
  achow101:
    ACK 2cf9d79d84
  sedited:
    ACK 2cf9d79d84

Tree-SHA512: 0ac44f8a464172b465834e1b3edf457fc8a09eeaa89cdfb1286af577d3c90d75627af8b5c5edd66232d45c60812ebc7f6f402db6f804a56f727353b2bdca7c19
This commit is contained in:
Ava Chow
2026-07-23 13:25:07 -07:00

View File

@@ -367,6 +367,7 @@ bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
void CExtKey::SetSeed(std::span<const std::byte> seed)
{
Assert(16 <= seed.size() && seed.size() <= 64);
static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(UCharCast(seed.data()), seed.size()).Finalize(vout.data());