// Package hd demonstrates private BIP32 derivation and BIP39 seed stretching. // Educational only: big.Int arithmetic is not constant-time. package hd import ( "crypto/hmac" "crypto/sha512" "encoding/binary" "errors" "math/big" "strconv" "strings" ) var order, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) const Hardened uint32 = 1 << 31 type ExtendedPrivate struct{ Key, Chain [32]byte } func mac(key, data []byte) []byte { h := hmac.New(sha512.New, key); h.Write(data); return h.Sum(nil) } func valid(k []byte) bool { n := new(big.Int).SetBytes(k); return n.Sign() > 0 && n.Cmp(order) < 0 } // Master implements BIP32 master key generation for 128–512 bit seeds. func Master(seed []byte) (ExtendedPrivate, error) { var out ExtendedPrivate if len(seed) < 16 || len(seed) > 64 { return out, errors.New("seed length") } i := mac([]byte("Bitcoin seed"), seed) if !valid(i[:32]) { return out, errors.New("invalid master scalar") } copy(out.Key[:], i[:32]) copy(out.Chain[:], i[32:]) return out, nil } // ChildHardened implements CKDpriv for hardened indices only. // Invalid children are reported, not silently retried at another index. func (p ExtendedPrivate) ChildHardened(index uint32) (ExtendedPrivate, error) { var out ExtendedPrivate if index < Hardened || !valid(p.Key[:]) { return out, errors.New("invalid hardened derivation input") } var data [37]byte copy(data[1:33], p.Key[:]) binary.BigEndian.PutUint32(data[33:], index) i := mac(p.Chain[:], data[:]) left := new(big.Int).SetBytes(i[:32]) if left.Cmp(order) >= 0 { return out, errors.New("invalid child tweak") } left.Add(left, new(big.Int).SetBytes(p.Key[:])) left.Mod(left, order) if left.Sign() == 0 { return out, errors.New("zero child scalar") } left.FillBytes(out.Key[:]) copy(out.Chain[:], i[32:]) return out, nil } // ParsePath parses absolute private derivation paths, with apostrophe hardening. func ParsePath(path string) ([]uint32, error) { parts := strings.Split(path, "/") if parts[0] != "m" { return nil, errors.New("expected m") } var out []uint32 for _, p := range parts[1:] { hard := strings.HasSuffix(p, "'") if hard { p = strings.TrimSuffix(p, "'") } if p == "" || strings.Trim(p, "0123456789") != "" { return nil, errors.New("invalid index") } n, e := strconv.ParseUint(p, 10, 31) if e != nil { return nil, e } v := uint32(n) if hard { v |= Hardened } out = append(out, v) } return out, nil } // SeedASCII demonstrates BIP39 PBKDF2 for ASCII inputs only (already NFKD). // It does not validate mnemonic words or checksum, or generate mnemonics. func SeedASCII(mnemonic, passphrase string) ([64]byte, error) { var out [64]byte for _, s := range []string{mnemonic, passphrase} { for _, r := range s { if r > 127 { return out, errors.New("ASCII only; full BIP39 requires NFKD") } } } salt := append([]byte("mnemonic"+passphrase), 0, 0, 0, 1) u := mac([]byte(mnemonic), salt) copy(out[:], u) for j := 1; j < 2048; j++ { u = mac([]byte(mnemonic), u) for k := range out { out[k] ^= u[k] } } return out, nil }