Files
keyfunc/internal/derive/derive.go
sneak 90a7c96cd1
All checks were successful
check / check (push) Successful in 34s
Skeleton, mnemonic input, derivation, and the ssh commands (closes #1)
The tool derives ed25519 SSH keys from a BIP-39 mnemonic and prints
them, either as an authorized_keys line or as an unencrypted OpenSSH
private key. Both README test vectors are asserted in the tests.

The mnemonic is looked for in the order the README gives, and refused
when it fails its checksum or when there is nowhere left to look.

The repository standards come with it: the vendored linter
configuration and policies, the script/ entrypoints with a thin
Makefile, and a Gitea workflow. Linting happens only inside the image
built from Dockerfile.lint, which pins the linter by hash, so the root
Dockerfile runs the formatting check, the tests and the build, and
script/cibuild runs the linter before it.

Model: opus-5
2026-09-07 14:44:45 +00:00

53 lines
1.4 KiB
Go

// Package derive turns a mnemonic into the bytes a key is made from.
package derive
import (
"fmt"
"git.eeqj.de/sneak/secret/pkg/bip85"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/chaincfg"
bip39 "github.com/tyler-smith/go-bip39"
)
const (
// purpose is the number BIP-85 reserves for itself.
purpose = 83696968
// Size is how many bytes every key type is given.
Size = 32
)
// Path returns the derivation path for an application number and a key
// index.
func Path(application, index uint32) string {
return fmt.Sprintf("m/%d'/%d'/%d'", purpose, application, index)
}
// Bytes returns the bytes for an application number and a key index.
// 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.
func Bytes(words string, application, index uint32) ([]byte, error) {
seed := bip39.NewSeed(words, "")
master, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
if err != nil {
return nil, fmt.Errorf("making the master key: %w", err)
}
entropy, err := bip85.DeriveBIP85Entropy(master, Path(application, index))
if err != nil {
return nil, fmt.Errorf("deriving entropy: %w", err)
}
out := make([]byte, Size)
_, err = bip85.NewBIP85DRNG(entropy).Read(out)
if err != nil {
return nil, fmt.Errorf("reading derived bytes: %w", err)
}
return out, nil
}