package trust // The signing half of publisher signatures, moved from yet's // internal/trust (2026-11-27, YABAR-036a) without changing behavior or // file formats: key pairs in minisign's own file formats, the package // signature over an OCI digest (SignDigest), detached signatures over // any bytes (Sign, VerifyMessage), plain Ed25519 for DSSE envelopes, // and PEM for cosign and openssl. Where keys are kept for auto-signing // and pushing signatures to a registry stay in yet. import ( "bytes" "crypto" "crypto/ed25519" "crypto/sha512" "fmt" "os" "path/filepath" "aead.dev/minisign" ) // KeyPair is a minisign key pair. The private key never leaves the host's // filesystem; only the public key is shared with other hosts (as a plain // ".pub" file) and embedded in signatures. type KeyPair struct { Public minisign.PublicKey Private minisign.PrivateKey } // GenerateKey creates a new minisign key pair. func GenerateKey() (KeyPair, error) { pub, priv, err := minisign.GenerateKey(nil) if err != nil { return KeyPair{}, err } return KeyPair{Public: pub, Private: priv}, nil } // SaveKeyPair writes kp's private key to path (owner-only permissions) and // its public key to path+".pub" (world-readable: it is meant to be shared). // This mirrors the real minisign CLI's `-p file.pub -s file` convention, so // `trust policy allow` only ever needs the ".pub" file, never the private // key -- a relying party registering a publisher's trust never needs (and // should never be handed) their private key. // // If password is non-empty, the private key is scrypt-encrypted exactly as // `minisign -G` with a passphrase would; if empty, it is written // unencrypted, matching the minisign CLI's own behavior when a blank // passphrase is given. Either way the file is only ever readable by its // owner; a password additionally protects it if the file itself leaks. func SaveKeyPair(path string, kp KeyPair, password string) error { if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { return err } var privData []byte var err error if password != "" { privData, err = minisign.EncryptKey(password, kp.Private) } else { privData, err = kp.Private.MarshalText() } if err != nil { return fmt.Errorf("encode private key: %w", err) } if err := writeOwnerOnly(path, privData); err != nil { return err } pubData, err := MarshalPublicKey(kp.Public) if err != nil { return fmt.Errorf("encode public key: %w", err) } return writeFile(path+".pub", pubData, 0o644) } // LoadPrivateKey reads a key pair previously written by SaveKeyPair, // decrypting it with password if it is encrypted (password is ignored, and // may be empty, for an unencrypted key). func LoadPrivateKey(path, password string) (KeyPair, error) { data, err := os.ReadFile(path) if err != nil { return KeyPair{}, err } return ParsePrivateKey(data, password) } // ParsePrivateKey parses a minisign private key file's contents, the // same bytes LoadPrivateKey reads from disk, so a key can also come from // somewhere other than a file (a yaseva secret, see KeyStore). func ParsePrivateKey(data []byte, password string) (KeyPair, error) { var err error var priv minisign.PrivateKey if minisign.IsEncrypted(data) { priv, err = minisign.DecryptKey(password, data) if err != nil { return KeyPair{}, fmt.Errorf("decrypt private key (wrong password?): %w", err) } } else if err := priv.UnmarshalText(data); err != nil { return KeyPair{}, fmt.Errorf("parse private key: %w", err) } pub, ok := priv.Public().(minisign.PublicKey) if !ok { return KeyPair{}, fmt.Errorf("derive public key: unexpected type %T", priv.Public()) } return KeyPair{Public: pub, Private: priv}, nil } // LoadPublicKey reads a standalone ".pub" file, for a trust policy that // must never require (or accept) private key material. func LoadPublicKey(path string) (minisign.PublicKey, error) { return minisign.PublicKeyFromFile(path) } // MarshalPublicKey encodes pub as a minisign ".pub" file. It is // aead.dev/minisign's own encoding, except that the comment line carries // the 16-digit key ID (the library writes it without leading zeros). func MarshalPublicKey(pub minisign.PublicKey) ([]byte, error) { data, err := pub.MarshalText() if err != nil { return nil, err } _, rest, ok := bytes.Cut(data, []byte("\n")) if !ok { return data, nil } out := fmt.Appendf(nil, "untrusted comment: minisign public key: %s\n", ID(pub)) out = append(out, rest...) if !bytes.HasSuffix(out, []byte("\n")) { out = append(out, '\n') } return out, nil } func writeOwnerOnly(path string, data []byte) error { return writeFile(path, data, 0o600) } func writeFile(path string, data []byte, mode os.FileMode) error { tmp, err := os.CreateTemp(filepath.Dir(path), ".key-*") if err != nil { return err } defer func() { _ = os.Remove(tmp.Name()) }() if err := os.Chmod(tmp.Name(), mode); err != nil { _ = tmp.Close() return err } if _, err := tmp.Write(data); err != nil { _ = tmp.Close() return err } if err := tmp.Close(); err != nil { return err } return os.Rename(tmp.Name(), path) } // SignEd25519 signs message with kp as a plain Ed25519 signature // (minisign's non-prehashed "Ed" algorithm): the raw 64 bytes DSSE // envelopes carry and cosign and other Ed25519 verifiers check against // ExportPEM's key. Package signatures (Sign) keep using minisign's own // format. func SignEd25519(kp KeyPair, message []byte) ([]byte, error) { text, err := kp.Private.Sign(nil, message, crypto.Hash(0)) if err != nil { return nil, err } var sig minisign.Signature if err := sig.UnmarshalText(text); err != nil { return nil, err } if sig.Algorithm != minisign.EdDSA { return nil, fmt.Errorf("unexpected minisign algorithm %d", sig.Algorithm) } return sig.Signature[:], nil } // VerifyEd25519 checks an Ed25519 signature over message against pub: // plain Ed25519 (what SignEd25519 makes) or Ed25519ph with SHA-512 // (what cosign makes with an Ed25519 key). func VerifyEd25519(pub minisign.PublicKey, message, sig []byte) bool { raw := Ed25519Key(pub) if raw == nil || len(sig) != ed25519.SignatureSize { return false } if ed25519.Verify(raw, message, sig) { return true } digest := sha512.Sum512(message) return ed25519.VerifyWithOptions(raw, digest[:], sig, &ed25519.Options{Hash: crypto.SHA512}) == nil }