Documentation
¶
Index ¶
- Constants
- Variables
- func CanonicalBindingBytes(ed25519Pub ed25519.PublicKey, createdAtMillis int64) []byte
- func DebugLogs() bool
- func Ed25519PublicKeyToX25519(pub ed25519.PublicKey) ([]byte, error)
- func IdentityDir(root string) string
- func MarshalBinding(b *Binding) ([]byte, error)
- func OnionSeedLabel(name string) string
- func OnionSeedLabelGen(name string, gen int) string
- func ServiceDocSigningBytes(doc []byte) []byte
- func ServiceSeedLabel(name string) string
- func ServiceSeedLabelGen(name string, gen int) string
- func ShortAddress(a string) string
- func StoreBundle(root string, b *Bundle) error
- func Validate(addr string) bool
- func VerifyBinding(b *Binding, identityDilithiumPub []byte) error
- func VerifyBindingForAddress(pinnedAddr string, dilithiumPub, bindingBytes []byte) (ed25519.PublicKey, error)
- func VerifyDilithium(pub, msg, sig []byte) (bool, error)
- func VerifyServiceDoc(pinnedAddr string, dilithiumPub, bindingBytes, doc, sig []byte) error
- type Address
- type Binding
- type Bundle
- type Identity
- func (id *Identity) Address() Address
- func (id *Identity) BindOperationalKey(now time.Time) (*Binding, error)
- func (id *Identity) CreateBinding(ed25519Pub ed25519.PublicKey, now time.Time) (*Binding, error)
- func (id *Identity) DeriveOnionSeed(name string) ([]byte, error)
- func (id *Identity) DeriveOnionSeedGen(name string, gen int) ([]byte, error)
- func (id *Identity) DeriveServiceSeed(name string) ([]byte, error)
- func (id *Identity) DeriveServiceSeedGen(name string, gen int) ([]byte, error)
- func (id *Identity) DilithiumPublic() []byte
- func (id *Identity) DilithiumSign(msg []byte) ([]byte, error)
- func (id *Identity) Ed25519Public() ed25519.PublicKey
- func (id *Identity) Ed25519Sign(msg []byte) []byte
- func (id *Identity) Seed() []byte
- func (id *Identity) ToLibp2pKey() (libp2pcrypto.PrivKey, error)
- func (id *Identity) ToX25519PrivateKey() []byte
- func (id *Identity) ToX25519PublicKey() ([]byte, error)
- type Keystore
- func (ks *Keystore) GetOrGenerateBundle(name string, parent *Identity) (*Bundle, error)
- func (ks *Keystore) HasBundle(name string) bool
- func (ks *Keystore) RemoveBundle(name string) error
- func (ks *Keystore) RotateBundle(name string) error
- func (ks *Keystore) ServiceGeneration(name string) (int, error)
- func (ks *Keystore) ServiceRoot(name string) string
Constants ¶
const ( // DilithiumLabel is the HKDF info label for the Dilithium sub-seed. // The .v1 suffix reserves headroom for a future re-key-without-changing-seed. DilithiumLabel = "ensemble.id.dilithium.v1" // Ed25519Label is the HKDF info label for the Ed25519 sub-seed. Ed25519Label = "ensemble.id.ed25519.v1" // SeedSize is the size of the master identity seed in bytes. SeedSize = 32 // DilithiumPublicKeySize is the size of an ML-DSA-65 public key in bytes. DilithiumPublicKeySize = mldsa65.PublicKeySize // DilithiumSignatureSize is the size of an ML-DSA-65 signature in bytes. DilithiumSignatureSize = mldsa65.SignatureSize )
const BindingCanonicalLabel = "ensemble.v1.idbinding"
BindingCanonicalLabel domain-separates the bytes signed by a Binding from any other Dilithium signing context. Anyone re-implementing the binding in a different language MUST use this exact byte sequence; otherwise signatures will not interop.
const NodeService = "node"
NodeService is the reserved name for the daemon's primary identity.
const ServiceDocLabel = "ensemble.v1.servicedoc"
ServiceDocLabel domain-separates service-document signatures from every other ed25519 signing context (wire envelopes use SigDomainEnvelope, admin auth its own form). Anyone re-implementing verification in another language MUST use this exact byte sequence.
const VersionByte = 0x21
VersionByte is the address version prefix. 0x21 produces addresses starting with "E" in Base58Check — the project's identity convention (see CLAUDE.md).
Variables ¶
var ErrBundleNotFound = errors.New("identity: bundle not found")
ErrBundleNotFound is returned by LoadBundle when no identity has been persisted under the given root yet.
var ErrInvalidBinding = errors.New("identity: binding verification failed")
ErrInvalidBinding is returned when a Binding fails any verification step (tampered fields, wrong identity pubkey, or invalid Dilithium signature).
var ErrInvalidSeed = errors.New("identity: seed must be 32 bytes")
ErrInvalidSeed is returned when DeriveIdentity is called with a seed of the wrong length.
Functions ¶
func CanonicalBindingBytes ¶
CanonicalBindingBytes returns the deterministic byte sequence that CreateBinding signs and VerifyBinding verifies against.
Layout (byte-exact):
BindingCanonicalLabel ("ensemble.v1.idbinding", 21 bytes)
0x00 (label terminator)
uint16 big-endian (length of ed25519_pub, currently always 32)
ed25519_pub (raw bytes)
int64 big-endian (created_at_millis)
Every length is fixed today; the prefixes are present so that a future schema extension (e.g., a wider operational pubkey, or extra signed fields) can be parsed unambiguously.
func DebugLogs ¶
func DebugLogs() bool
DebugLogs reports whether the daemon is running with verbose (debug) log output enabled. Toggled by the EnsembleLogLevel environment variable; "debug" (case-insensitive) enables full-detail logging of peer addresses and other potentially correlation-leaking fields.
func Ed25519PublicKeyToX25519 ¶
Ed25519PublicKeyToX25519 converts a raw Ed25519 public key to X25519.
func IdentityDir ¶
IdentityDir returns the absolute path of the identity bundle directory under the given root (typically a daemon's DataDir).
func MarshalBinding ¶
MarshalBinding serialises a Binding into its proto wire form.
func OnionSeedLabel ¶
OnionSeedLabel returns the HKDF info label for the per-service Tor onion keypair seed: ensemble.service.<name>.onion.v1. Distinct from ServiceSeedLabel so a compromise / rotation of one role does not require rotating the other. Exposed so tests can assert label stability.
func OnionSeedLabelGen ¶
OnionSeedLabelGen is OnionSeedLabel for a specific rotation generation.
func ServiceDocSigningBytes ¶
ServiceDocSigningBytes returns the canonical byte sequence signed for a service document: label || 0x00 || doc. A single variable-length field needs no length prefix — the label terminator makes the form unambiguous.
func ServiceSeedLabel ¶
ServiceSeedLabel returns the HKDF info label for the per-service seed derived from a parent identity for the service named name. The label is namespaced (ensemble.service.<name>.v1) so a future schema change can rotate every service's derived seed by bumping the .v1 suffix without breaking parent identity material. Exposed so tests can assert label stability.
func ServiceSeedLabelGen ¶
ServiceSeedLabelGen is ServiceSeedLabel for a specific rotation generation.
func ShortAddress ¶
ShortAddress returns a redacted form of an address-like string for default log output: first 6 chars + "..." + last 4 chars. Inputs shorter than 10 chars are returned unchanged (truncation would expose more than it hides). Used at default log level so a passive log observer cannot enumerate the full peer set a node interacts with.
func StoreBundle ¶
StoreBundle writes the 5-file identity bundle under ${root}/identity/. Each individual file uses tmp+rename so it is never torn (you read either the old bytes or the new bytes, never a partial line). Bundle-level atomicity is NOT provided — a crash partway through the five writes can leave a mix of old and new files. LoadBundle's cross-checks catch that case and refuse to load (see the package doc comment for the recovery path). The directory is created with 0700 if absent.
func Validate ¶
Validate checks that an address has a valid Base58Check checksum and the recognised version byte.
func VerifyBinding ¶
VerifyBinding checks the Dilithium signature against the given identity pubkey. Returns nil on success; otherwise ErrInvalidBinding (the underlying reason is intentionally hidden so callers can't use error strings as a timing oracle for which field failed verification).
func VerifyBindingForAddress ¶
func VerifyBindingForAddress(pinnedAddr string, dilithiumPub, bindingBytes []byte) (ed25519.PublicKey, error)
VerifyBindingForAddress checks the address→Dilithium→binding→ed25519 chain:
- dilithiumPub must hash to pinnedAddr (the address IS the key pin),
- bindingBytes must unmarshal to an IdentityBinding that verifies under dilithiumPub.
On success it returns the bound ed25519 operational pubkey, so a caller can then verify that identity's per-message signatures locally without re-parsing the binding. This is the single source of truth for "does this binding belong to this address", shared by VerifyServiceDoc and the VerifyBinding RPC.
Reasons are descriptive: this is an operator-/consumer-side verification of caller-supplied bytes diagnosing a bad hand-off, not a signing oracle an attacker probes (the low-level VerifyBinding stays opaque for that).
func VerifyDilithium ¶
VerifyDilithium verifies an ML-DSA-65 signature against a marshalled Dilithium public key.
func VerifyServiceDoc ¶
VerifyServiceDoc checks the full publication chain for doc against the publisher's pinned E-address:
- dilithiumPub must hash to pinnedAddr (the address IS the key pin),
- bindingBytes must unmarshal to an IdentityBinding that verifies under dilithiumPub,
- sig must verify under the binding's ed25519 key over the domain-separated doc.
Returns nil only when every link holds. Distinct errors are fine here — the verifier is an operator-side consumer diagnosing a bad subscription, not a signing oracle an attacker probes (cf. ErrInvalidBinding's opacity).
Types ¶
type Address ¶
type Address string
Address is a Bitcoin-style Base58Check-encoded identity string.
func AddressFromDilithiumPub ¶
AddressFromDilithiumPub computes the canonical Base58Check address from a marshalled ML-DSA-65 public key. Pipeline: dilPub -> SHA-256 -> RIPEMD-160 -> Base58Check(VersionByte 0x21). The resulting address starts with "E".
type Binding ¶
Binding authorises an ed25519 operational pubkey under a Dilithium identity.
func UnmarshalBinding ¶
UnmarshalBinding parses the proto wire form. Note: parsing does NOT verify the signature — callers must invoke VerifyBinding before trusting the contents.
type Bundle ¶
Bundle is the in-memory representation of a persisted identity. The Identity drives signing; the Binding accompanies it on the wire and in CONN_CONFIRM.
func DeriveBundle ¶
DeriveBundle builds a complete identity bundle from a 32-byte master seed: both keypairs (via DeriveIdentity) plus a freshly signed IdentityBinding authorising the derived operational ed25519 key, stamped with now.
The Dilithium + ed25519 keypairs and the address are fully determined by the seed alone — recover the same seed on any machine and you get the same identity. Only the binding's created_at (and therefore its deterministic Dilithium signature) depends on now; the binding is a re-attestation of the same operational key, not part of the seed-invariant identity. This is the single derive→bind→bundle path shared by first-boot generation, per-service bundle generation, and the `ensemble init` recovery flow.
func LoadBundle ¶
LoadBundle reads the 5-file identity bundle from ${root}/identity/. The loaded Identity is reconstructed from the on-disk Dilithium private key + ed25519 seed (rather than re-derived from a master seed) so a bundle whose master seed is unknown to the caller can still be loaded faithfully.
Returns ErrBundleNotFound if any required file is missing — callers can use that to drive first-boot generation.
LoadBundle DOES verify the binding against the persisted Dilithium pubkey. A bundle that loads but whose binding fails verification is treated as corrupt and the error returned reflects that.
type Identity ¶
type Identity struct {
// contains filtered or unexported fields
}
Identity is a unified node/service identity. The address is anchored to the Dilithium pubkey; the ed25519 key is an operational sub-key bound to the identity via the IdentityBinding (T2).
func DeriveIdentity ¶
DeriveIdentity deterministically derives both keypairs from a 32-byte seed using HKDF-SHA256 with distinct info labels per key role.
func GenerateIdentity ¶
GenerateIdentity creates a fresh Identity from a cryptographically random seed. Equivalent to DeriveIdentity(crypto-random 32-byte seed). Used by tests that don't need a deterministic identity.
func (*Identity) Address ¶
Address returns the canonical post-quantum identity address: Base58Check (VersionByte || ripemd160(sha256(dilithium_pub))).
func (*Identity) BindOperationalKey ¶
BindOperationalKey is shorthand for `id.CreateBinding(id.Ed25519Public(), now)` — produces a Binding authorising this identity's own ed25519 sub-key. First-boot identities use this; rotation flows pass an alternate ed25519 pubkey to CreateBinding directly.
func (*Identity) CreateBinding ¶
CreateBinding signs the operational ed25519 pubkey with this identity's Dilithium private key. Returns a Binding ready to marshal + persist.
func (*Identity) DeriveOnionSeed ¶
DeriveOnionSeed deterministically derives a 32-byte seed for the named service's Tor v3 onion keypair. Same construction as DeriveServiceSeed (HKDF-SHA256 over the ed25519 sub-seed) with the onion-specific label, so the chain is master_seed → ed25519_seed → onion_seed and the onion key is recoverable from the master seed alone — closing the "seed-only recovery does not reproduce the onion" gap that the per-service identity HKDF work alone did not address (the Tor onion key was previously random-generated by tor on first AddOnion and persisted to disk).
Returns a 32-byte seed suitable for bineed25519.GenerateKey via bytes.NewReader. The clamping / sha512 expansion bine performs on the 32-byte input is deterministic, so the resulting keypair — and therefore the .onion address — is fully determined by (master_seed, service name).
func (*Identity) DeriveOnionSeedGen ¶
DeriveOnionSeedGen is DeriveOnionSeed for a specific rotation generation. Generation 0 reproduces the legacy onion seed exactly; a higher generation derives an unrelated seed (new .onion address) for a rotated service identity. Ticket 7a9b8f66.
func (*Identity) DeriveServiceSeed ¶
DeriveServiceSeed deterministically derives a 32-byte seed for the named service from this identity's ed25519 sub-seed via HKDF-SHA256, with the label produced by ServiceSeedLabel(name). The ed25519 sub-seed is itself HKDF-derived from the master seed in DeriveIdentity, so the full chain is master_seed → ed25519_seed → service_seed and the service seed is recoverable from the master seed alone (no per-service backup required).
The ed25519 sub-seed is chosen as the HKDF parent (rather than the master seed directly) because it is byte-stable across both DeriveIdentity(seed) and LoadBundle(root) — i.e. callable on any in-memory Identity, regardless of how it was reconstructed. Identity.Seed() returns nil for bundle-loaded identities, which would break the seed-derived contract on daemon restart.
Security trade-off: a compromise of the master seed (or any identity derived from it) reproduces every per-service identity exactly — the per-service material is no longer an independent random sample. The alternative (random per-service seeds) loses recoverability without an extra backup channel for each service. See ensemble ticket 9ef9dd21 for the full analysis.
func (*Identity) DeriveServiceSeedGen ¶
DeriveServiceSeedGen is DeriveServiceSeed for a specific rotation generation. Generation 0 reproduces the legacy service seed exactly; a higher generation derives an unrelated seed (new Dilithium + ed25519 keys, new address) for a rotated service identity. Ticket 7a9b8f66.
func (*Identity) DilithiumPublic ¶
DilithiumPublic returns the marshalled ML-DSA-65 public key bytes.
func (*Identity) DilithiumSign ¶
DilithiumSign produces an ML-DSA-65 signature over msg. Reserved for identity-level proofs (the IdentityBinding in T2); per-envelope signing uses Ed25519Sign.
func (*Identity) Ed25519Public ¶
Ed25519Public returns the Ed25519 public key.
func (*Identity) Ed25519Sign ¶
Ed25519Sign produces an Ed25519 signature over msg. Used for per-envelope wire-message authentication.
func (*Identity) ToLibp2pKey ¶
func (id *Identity) ToLibp2pKey() (libp2pcrypto.PrivKey, error)
ToLibp2pKey returns the operational ed25519 private key wrapped as a libp2p PrivKey. Used by transport.NewHost — the libp2p host runs under the operational key (it changes if the operational key rotates; the identity Address does not).
func (*Identity) ToX25519PrivateKey ¶
ToX25519PrivateKey converts the Identity's ed25519 operational private key to an X25519 private key for ECDH (IPInfo encryption in signaling).
func (*Identity) ToX25519PublicKey ¶
ToX25519PublicKey converts the Identity's ed25519 operational public key to an X25519 public key.
type Keystore ¶
type Keystore struct {
// contains filtered or unexported fields
}
Keystore is a directory-rooted holder for per-service post-quantum identity bundles. Each service's bundle lives under <dir>/services/<name>/identity/ in the five-file layout written by StoreBundle / LoadBundle. The bundle methods (ServiceRoot, GetOrGenerateBundle, HasBundle, RemoveBundle) live in service_keystore.go.
func NewKeystore ¶
NewKeystore creates a keystore rooted at dir.
func (*Keystore) GetOrGenerateBundle ¶
GetOrGenerateBundle loads the persisted PQ identity bundle for name, or — if none exists yet (or the on-disk bundle is incomplete) — derives a fresh one from parent.DeriveServiceSeed(name) and persists it atomically before returning.
A missing or partial bundle (e.g. an operator deleted binding.bin) is treated as "regenerate": LoadBundle reports ErrBundleNotFound, and StoreBundle then rewrites all five files consistently from the freshly derived identity. The keypair material will be byte-identical to the previous bundle if parent and name are unchanged; only the binding's created_at timestamp will differ.
parent must be non-nil — the contract is that every service identity is derivable from a parent, so service-seed recovery from master_seed is always possible. Passing nil is a programmer error and rejected explicitly.
func (*Keystore) HasBundle ¶
HasBundle reports whether a PQ identity bundle is persisted for name. It checks for the Dilithium private key, the load-bearing secret of the bundle.
func (*Keystore) RemoveBundle ¶
RemoveBundle wipes the persisted identity for name. It removes the whole per-service directory but NOT the rotation-generation sidecar, so the next GetOrGenerateBundle re-derives at the *same* generation — i.e. byte-identical keys when parent + name are unchanged. This is the "reset/recover" primitive (recover a corrupt/partial bundle), NOT rotation; use RotateBundle to get fresh keys. Removing a non-existent bundle is not an error.
func (*Keystore) RotateBundle ¶
RotateBundle rotates the persisted identity for name: it bumps the rotation generation (persisted in the sidecar, which survives the wipe) and then removes the bundle directory, so the next GetOrGenerateBundle derives a *fresh, unrelated* identity — new Dilithium + ed25519 keys, new address, and (when the onion derivation reads the same generation) a new .onion. This is the real rotation primitive backing DeleteService's "fresh keypair" contract, for recovery after a suspected service-key compromise. Ticket 7a9b8f66.
func (*Keystore) ServiceGeneration ¶
ServiceGeneration returns the current rotation generation for name (0 if the service has never been rotated / has no sidecar yet). Each RotateBundle bumps it; GetOrGenerateBundle and the onion derivation fold it into the HKDF label so a higher generation yields fresh, unrelated keys. Ticket 7a9b8f66.
func (*Keystore) ServiceRoot ¶
ServiceRoot returns the per-service directory <dir>/services/<name>. The PQ identity bundle for the service lives under <ServiceRoot>/identity/.