Refuse a key index with no hardened child
All checks were successful
check / check (push) Successful in 23s
All checks were successful
check / check (push) Successful in 23s
Every element of the derivation path is hardened, so an index above 2147483647 has no child to derive: the hardened offset wrapped around and the tool silently produced a non-hardened key that no other implementation reading the path as written would reproduce. Such an index is now refused with a message before anything is derived, and tests pin the refusal at both the derivation and the command level. Also use the hook path variable in script/install-precommit instead of repeating the literal beside it. Model: opus-5
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
package derive
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"git.eeqj.de/sneak/secret/pkg/bip85"
|
||||
@@ -16,8 +17,18 @@ const (
|
||||
|
||||
// Size is how many bytes every key type is given.
|
||||
Size = 32
|
||||
|
||||
// MaxIndex is the largest key index there is. Every element of
|
||||
// the path is hardened, and a hardened BIP-32 child index stops
|
||||
// here.
|
||||
MaxIndex = 1<<31 - 1
|
||||
)
|
||||
|
||||
// ErrIndexTooLarge is returned for a key index above MaxIndex. Such an
|
||||
// index has no hardened child to derive, so there is no key to give
|
||||
// back rather than a key nobody else would reproduce.
|
||||
var ErrIndexTooLarge = errors.New("the key index is too large")
|
||||
|
||||
// Path returns the derivation path for an application number and a key
|
||||
// index.
|
||||
func Path(application, index uint32) string {
|
||||
@@ -28,7 +39,15 @@ func Path(application, index uint32) string {
|
||||
// The mnemonic becomes a seed with an empty passphrase, the seed
|
||||
// becomes a master key, the master key gives BIP-85 entropy at the
|
||||
// path, and the entropy seeds the generator the bytes are read from.
|
||||
// An index above MaxIndex is refused before any of that happens.
|
||||
func Bytes(words string, application, index uint32) ([]byte, error) {
|
||||
if index > MaxIndex {
|
||||
return nil, fmt.Errorf(
|
||||
"%w: %d is above %d",
|
||||
ErrIndexTooLarge, index, MaxIndex,
|
||||
)
|
||||
}
|
||||
|
||||
seed := bip39.NewSeed(words, "")
|
||||
|
||||
master, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
|
||||
|
||||
@@ -38,6 +38,16 @@ func TestEveryIndexGivesItsOwnBytes(t *testing.T) {
|
||||
require.False(t, bytes.Equal(first, second))
|
||||
}
|
||||
|
||||
func TestAnIndexWithNoHardenedChildIsRefused(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := derive.Bytes(example(), application, derive.MaxIndex+1)
|
||||
require.ErrorIs(t, err, derive.ErrIndexTooLarge)
|
||||
|
||||
_, err = derive.Bytes(example(), application, derive.MaxIndex)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestTheSameInputAlwaysGivesTheSameBytes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user