Documentation
¶
Overview ¶
Package keyring seals small values (TOTP secrets, sign-in state, ceremony state) under a set of rotatable keys.
A Ring holds keys with IDs; the first one seals, and every one opens. Sealed values name the key that sealed them, so adding a new key at the front starts using it at once, values sealed under older keys keep opening, and an older key can be removed once nothing it sealed is still needed:
ring, _ := keyring.New([]keyring.Key{
{ID: "2026-12", Secret: newKey}, // seals
{ID: "2026-06", Secret: oldKey}, // still opens
}, nil)
Each purpose gets its own key, derived as HMAC-SHA256(secret, label), and values are sealed with AES-256-GCM. A value sealed for one label never opens under another.
Legacy values ¶
Before keyrings, iam sealed with a single key and no key ID: the 12-byte nonce followed by the ciphertext, under the same derivation. Pass that key as legacy to New to keep opening those values. A ring with no keys seals in the legacy format too, so configuring only the legacy key behaves exactly as earlier releases did.
Index ¶
- Constants
- Variables
- func TagKeyID(tag []byte) string
- type Key
- type Ring
- func (r *Ring) ActiveID() string
- func (r *Ring) Open(label string, sealed, aad []byte) (plain []byte, stale bool, err error)
- func (r *Ring) Seal(label string, plain, aad []byte) ([]byte, error)
- func (r *Ring) Secrets() [][]byte
- func (r *Ring) Tag(label string, msg []byte) (tag []byte, ok bool)
- func (r *Ring) Tags(label string, msg []byte) [][]byte
Constants ¶
const MaxIDLen = 64
MaxIDLen is the longest key ID.
const MinSecretSize = 32
MinSecretSize is the minimum key length (256 bits).
Variables ¶
var ErrOpen = errors.New("keyring: cannot open sealed value")
ErrOpen is returned when a value cannot be opened: it is malformed, was sealed under a key the ring does not hold, for another label or associated data, or has been tampered with.
Functions ¶
Types ¶
type Key ¶
type Key struct {
// ID names the key inside sealed values. It is not secret. 1 to
// MaxIDLen characters from A-Z, a-z, 0-9, '.', '_' and '-', unique
// within a ring. A date ("2026-12") works well.
ID string
// Secret is the key material: at least MinSecretSize random bytes.
Secret []byte
}
Key is one key of a Ring.
type Ring ¶
type Ring struct {
// contains filtered or unexported fields
}
Ring seals and opens values. It is safe for concurrent use and immutable: to rotate, build a new Ring.
func New ¶
New returns a ring. keys[0], if any, seals; with no keys, legacy seals in the legacy format. At least one of keys and legacy is required; an empty legacy key means none.
func (*Ring) ActiveID ¶
ActiveID returns the ID of the key that seals, or "" when the ring seals with the legacy key.
func (*Ring) Open ¶
Open returns the plaintext of a value sealed by Seal for the same label and aad. stale reports that the value was not sealed under the active key (an older key, or the legacy key while keyed keys exist): seal it again to move it to the active key.
func (*Ring) Seal ¶
Seal encrypts and authenticates plain, bound to label and aad, under the active key.
func (*Ring) Secrets ¶
Secrets returns every key's secret, the sealing one first and the legacy key last, for uses that are not sealing (an HMAC checked against each). The slices must not be modified.
func (*Ring) Tag ¶
Tag returns a keyed tag of msg under the active key, naming that key: 0x01 | len(id) | id | HMAC-SHA256(HMAC-SHA256(secret, label), msg). Use it to store a value that is checked but never read back (a recovery code), so that a copy of the store alone cannot be brute-forced. ok is false for a ring with no keyed keys (legacy only); the caller keeps its legacy scheme.