Documentation
¶
Overview ¶
Package keys owns the binary's persistent X25519 static keypair used by Mobile Protocol v2 (Noise_IK). Each pyrycode daemon holds one keypair per daemon-name, persisted at ~/.pyry/<daemon-name>/static_key.json. The pure type, validator, and key generator live alongside the I/O wrapper that mints and loads them on first run.
SECURITY: the returned StaticKey.PrivateKey() bytes are the X25519 static secret. Never log them, never wrap them into error context, never emit them on a wire. Compromise of these bytes lets any holder impersonate this binary to every paired phone.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCorruptKeyFile = errors.New("keys: corrupt static key file")
ErrCorruptKeyFile is returned (wrapped) when an existing static_key.json fails to decode, fails schema validation, or whose public_key disagrees with the public point recomputed from private_key. Match with errors.Is.
var ErrInsecureKeyDirMode = errors.New("keys: insecure key directory mode")
ErrInsecureKeyDirMode is returned (wrapped) when the parent directory of static_key.json has any group/other readable, writable, or executable bit set, or when the path exists but is not a directory. The operator's fix is to chmod the directory to 0700 (or remove the imposter) and restart; the daemon refuses to start to make the weakened mode visible. Match with errors.Is.
var ErrInsecureKeyFileMode = errors.New("keys: insecure key file mode")
ErrInsecureKeyFileMode is returned (wrapped) when static_key.json exists with a mode other than exactly 0600. No auto-chmod, no silent fallback — the operator chmods the file back to 0600 by hand and restarts. Match with errors.Is.
var ErrInvalidDaemonName = errors.New("keys: invalid daemon name")
ErrInvalidDaemonName is returned (wrapped) when the daemonName argument fails the package's allowlist validator. Match with errors.Is.
Functions ¶
This section is empty.
Types ¶
type StaticKey ¶
type StaticKey struct {
// contains filtered or unexported fields
}
StaticKey is the binary's persistent X25519 keypair for Mobile Protocol v2. The raw 32-byte private scalar and public point are accessed via the PrivateKey and PublicKey methods, which return copies by value so callers cannot mutate package-internal state.
func LoadOrCreate ¶
LoadOrCreate returns the StaticKey stored at <baseDir>/<daemonName>/static_key.json, generating and persisting a fresh keypair if the file does not exist.
baseDir is typically <home>/.pyry; daemonName is the operator-supplied per-daemon label. daemonName is validated against the package's allowlist before any filesystem access — on rejection LoadOrCreate returns ErrInvalidDaemonName (wrapped) and does not touch the filesystem. The package does NOT validate baseDir; callers resolve home and trust the resulting parent path (the allowlist defends against daemonName injection, not against an attacker-controlled baseDir).
On first run: parent directory created with mode 0700 if absent; keypair minted from crypto/rand via crypto/ecdh.X25519().GenerateKey; encoded as JSON per docs/protocol-mobile.md § Static keys — binary side; written atomically (sibling temp file in the parent dir → chmod 0600 → encode → Sync → Close → Rename).
On subsequent runs: the file is read and decoded. Schema-version and algorithm constants are checked, base64 fields are decoded to exactly 32 bytes, and the stored public_key is verified against the public point recomputed from private_key. Any mismatch returns ErrCorruptKeyFile wrapped with the file path; the error message NEVER contains the file contents. The file is NEVER overwritten on the existing-file path, even on validation failure — keys are bound to every paired device and silent regeneration would invalidate every pairing without operator awareness.
LoadOrCreate is not safe for concurrent use against the same path; bootstrap runs once on daemon startup before any goroutines fan out.
Before any read or write, the parent directory is required to exist with a mode that has no group/other bits set (ErrInsecureKeyDirMode otherwise); on the read path the file is required to be exactly mode 0600 (ErrInsecureKeyFileMode otherwise) and is opened with O_NOFOLLOW so a between-stat-and-open symlink swap cannot redirect the load.
func (*StaticKey) PrivateKey ¶
PrivateKey returns the raw 32-byte X25519 private scalar by value.
SECURITY: callers MUST NOT log, wrap-into-error, or otherwise emit the returned bytes. Compromise of these bytes lets any holder impersonate this binary to every paired phone.