algorithm

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package algorithm holds keyforge's algorithm registry: the identifiers written in envelopes, their exact names, and the construction of the primitive for each.

Every protection of stored data and keys goes through an algorithm from this registry. The set is compiled in; configuration can narrow it and cannot add to it. Adding an algorithm is a release with a new identifier and known-answer vectors.

The registry currently holds AES-256-GCM only.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknown is returned for an identifier that is not in the registry.
	ErrUnknown = errors.New("algorithm: unknown algorithm")
	// ErrKeySize is returned when a key does not have the size the algorithm
	// requires.
	ErrKeySize = errors.New("algorithm: wrong key size")
)
View Source
var (
	// ErrNotPermitted is returned when an algorithm is outside the profile in
	// force. It is never answered with a fallback.
	ErrNotPermitted = errors.New("algorithm: not permitted by the profile")
	// ErrNoPermitted is returned when a write finds no algorithm the profile
	// and the purpose's list both permit. The operation fails closed.
	ErrNoPermitted = errors.New("algorithm: no permitted algorithm")
	// ErrInvalidSelection is returned for a purpose configuration that widens
	// a profile or names an algorithm that does not exist.
	ErrInvalidSelection = errors.New("algorithm: invalid selection")
	// ErrUnknownProfile is returned for a profile name or value that is not
	// defined.
	ErrUnknownProfile = errors.New("algorithm: unknown profile")
)

Functions

func New

func New(id ID, key []byte) (cipher.AEAD, error)

New returns the AEAD for id keyed with key. The AEAD generates a random nonce on Seal and prepends it to the ciphertext, and its NonceSize is zero. The cipher keeps its own expanded copy of the key; the caller keeps ownership of key and should zero it when done.

Types

type Config

type Config struct {
	// Profile is the active profile. The zero value means [DefaultProfile].
	Profile Profile
	// Deny removes algorithms from the active profile. It can only narrow.
	Deny []ID
}

Config configures a Selector.

type ID

type ID uint8

ID is an algorithm identifier as written in the envelope header.

const (
	// AES256GCM is AES-256-GCM with a random 96-bit nonce generated by
	// [cipher.NewGCMWithRandomNonce], the only GCM constructor permitted
	// under GODEBUG=fips140=only.
	AES256GCM ID = 1
)

func (ID) KeySize

func (id ID) KeySize() int

KeySize returns the key size in bytes, or 0 for an unknown identifier.

func (ID) NonceSize

func (id ID) NonceSize() int

NonceSize returns the size in bytes of the nonce stored in the envelope header, or 0 for an unknown identifier.

func (ID) String

func (id ID) String() string

String returns the algorithm's exact name, mode and size included, or a placeholder for an identifier that is not in the registry.

func (ID) TagSize

func (id ID) TagSize() int

TagSize returns the size in bytes of the authentication tag, or 0 for an unknown identifier.

func (ID) Valid

func (id ID) Valid() bool

Valid reports whether id is in the registry.

type NotPermittedError

type NotPermittedError struct {
	Algorithm ID
	Profile   Profile
}

NotPermittedError reports the algorithm and the profile it was refused by.

func (*NotPermittedError) Error

func (e *NotPermittedError) Error() string

Error implements error.

func (*NotPermittedError) Unwrap

func (e *NotPermittedError) Unwrap() error

Unwrap lets errors.Is match ErrNotPermitted.

type Profile

type Profile uint8

Profile is a named set of algorithms. The sets are compiled in and reviewed; configuration can narrow a profile and cannot add an algorithm to it.

const (
	// ProfileStandard is the default profile outside FIPS mode. Every
	// algorithm in the registry is a member.
	ProfileStandard Profile = 1
	// ProfileFIPS holds only algorithms that FIPS 140-3 approves. It is the
	// default when the process runs in FIPS mode, and may be selected outside
	// it, where it restricts the algorithm set without module validation.
	ProfileFIPS Profile = 2
)

func DefaultProfile

func DefaultProfile() Profile

DefaultProfile returns the profile a process uses when none is configured: FIPS when the process runs in FIPS 140-3 mode, and standard otherwise.

func ParseProfile

func ParseProfile(s string) (Profile, error)

ParseProfile parses "standard" or "fips".

func (Profile) Algorithms

func (p Profile) Algorithms() []ID

Algorithms returns the profile's members in preference order. The caller owns the returned slice.

func (Profile) Permits

func (p Profile) Permits(id ID) bool

Permits reports whether id is a member of the profile.

func (Profile) String

func (p Profile) String() string

String returns the profile's name as used in configuration, logs and status.

func (Profile) Valid

func (p Profile) Valid() bool

Valid reports whether p is a defined profile.

type Selection

type Selection struct {
	// Profile pins the purpose to a profile that is no wider than the active
	// one. The zero value inherits the active profile.
	Profile Profile
	// Allowed is the ordered preference list for writes, a subset of the
	// profile. Empty means the profile's own order.
	Allowed []ID
}

Selection is a purpose's algorithm configuration. It can only narrow what the active profile permits.

type Selector

type Selector struct {
	// contains filtered or unexported fields
}

Selector chooses algorithms for writes and checks algorithms on reads under the active profile. It is immutable and safe for concurrent use.

func NewSelector

func NewSelector(cfg Config) (*Selector, error)

NewSelector returns a selector for cfg. It fails if the profile or a denied algorithm is not defined.

func (*Selector) Check

func (s *Selector) Check(id ID, sel Selection) error

Check decides whether an envelope that names id may be read. Reads follow the profile in force, not the purpose's write list, so narrowing the list does not strand data already written. Migration is a re-encryption in a process whose profile permits the old algorithm.

An algorithm outside the profile fails with a *NotPermittedError that reports the algorithm and the profile; there is no fallback.

func (*Selector) CheckWrite

func (s *Selector) CheckWrite(id ID, sel Selection) error

CheckWrite decides whether id may be used to write under sel: it must pass Selector.Check and, when the purpose lists algorithms, be one of them. A data key created under an algorithm that has since left the list fails here, so the caller rotates to a new epoch instead of writing with it.

func (*Selector) Profile

func (s *Selector) Profile() Profile

Profile returns the active profile.

func (*Selector) Select

func (s *Selector) Select(sel Selection) (ID, error)

Select returns the algorithm to write with: the first entry of the purpose's list that the profile in force permits, or the profile's first permitted algorithm when the list is empty. If there is none it returns ErrNoPermitted; it never falls back to an algorithm outside the profile.

func (*Selector) String

func (s *Selector) String() string

String describes the selector for logs and status: the active profile and any denied algorithms.

func (*Selector) Validate

func (s *Selector) Validate(purposes map[string]Selection) error

Validate checks every purpose's configuration and reports all violations, so startup fails fast with the whole list. Purposes are checked in name order.

func (*Selector) ValidateSelection

func (s *Selector) ValidateSelection(sel Selection) error

ValidateSelection checks one purpose's configuration against the active profile: a pinned profile must be no wider, and every listed algorithm must exist, appear once, and be permitted. It also requires that a write has an algorithm to use.

Jump to

Keyboard shortcuts

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