keyring

package
v2.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const MaxIDLen = 64

MaxIDLen is the longest key ID.

View Source
const MinSecretSize = 32

MinSecretSize is the minimum key length (256 bits).

Variables

View Source
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

func TagKeyID

func TagKeyID(tag []byte) string

TagKeyID returns the ID of the key a Tag names, or "" when tag is not a Tag (for example a legacy 32-byte hash).

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

func New(keys []Key, legacy []byte) (*Ring, error)

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

func (r *Ring) ActiveID() string

ActiveID returns the ID of the key that seals, or "" when the ring seals with the legacy key.

func (*Ring) Open

func (r *Ring) Open(label string, sealed, aad []byte) (plain []byte, stale bool, err error)

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

func (r *Ring) Seal(label string, plain, aad []byte) ([]byte, error)

Seal encrypts and authenticates plain, bound to label and aad, under the active key.

func (*Ring) Secrets

func (r *Ring) Secrets() [][]byte

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

func (r *Ring) Tag(label string, msg []byte) (tag []byte, ok bool)

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.

func (*Ring) Tags

func (r *Ring) Tags(label string, msg []byte) [][]byte

Tags returns msg's tag under every keyed key, the active one first: the candidates to look up when checking a value Tag stored under any of them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL