key: validate BIP32 seed length in CExtKey::SetSeed

BIP32 specifies that seed must be between 128 and 512 bits
(16 to 64 bytes). CExtKey::SetSeed currently accepts any length,
including empty seeds, which could lead to weak master keys.

Add an Assert at the start of SetSeed to enforce the valid seed
length range.

Fixes #35308
This commit is contained in:
Muhammad
2026-05-19 10:45:03 +01:00
parent 7802e578c3
commit 2cf9d79d84

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());