encryption

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package encryption provides bounded, versioned AES-256-GCM envelopes using Go's random-nonce AEAD. Keys are explicit application configuration; the package never generates a replacement for a missing decryption key. Bind each value to its purpose and owning record through Context. It does not provide streaming encryption, key custody or a distributed key-usage counter.

Index

Constants

View Source
const MaxBindingBytes = 8192
View Source
const MaxKeys = 32
View Source
const MaxPlaintextBytes = 1 << 20

Variables

This section is empty.

Functions

func EnvelopePrefix

func EnvelopePrefix(id KeyID) (string, error)

EnvelopePrefix returns the text every envelope encrypted under id starts with. Storage adapters use it to select records that still use another key during rotation; it is not secret and never authenticates anything.

Types

type Ciphertext

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

Ciphertext is a bounded, canonical versioned envelope. Parsing validates its structure only; authentication happens in Keyring.Decrypt. Encoded is the explicit persistence boundary. Ordinary formatting and JSON redact it.

func ParseCiphertext

func ParseCiphertext(encoded string) (Ciphertext, error)

func (Ciphertext) Encoded

func (c Ciphertext) Encoded() string

func (Ciphertext) Format

func (Ciphertext) Format(s fmt.State, _ rune)

func (Ciphertext) IsZero

func (c Ciphertext) IsZero() bool

func (Ciphertext) KeyID

func (c Ciphertext) KeyID() KeyID

func (Ciphertext) LogValue

func (Ciphertext) LogValue() slog.Value

func (Ciphertext) MarshalJSON

func (Ciphertext) MarshalJSON() ([]byte, error)

type Context

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

Context authenticates a purpose and exact owning-record binding alongside an envelope. It is not stored in the envelope and must be reconstructed from trusted record identity at decryption. Include tenant and record generation when relevant. Empty bindings are rejected.

func NewContext

func NewContext(purpose Purpose, binding secret.String) (Context, error)

func (Context) Format

func (Context) Format(s fmt.State, _ rune)

func (Context) LogValue

func (Context) LogValue() slog.Value

func (Context) MarshalJSON

func (Context) MarshalJSON() ([]byte, error)

func (Context) Validate

func (c Context) Validate() error

type Key

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

Key owns one immutable AES-256 key and a local encryption budget shared by copies and keyrings. Loading the same material again or on another process creates a separate counter: operators must rotate before aggregate use across all instances/restarts exceeds 2^32 encryptions with the same material.

func GenerateKey

func GenerateKey(id KeyID) (Key, error)

GenerateKey creates a key for explicit installation in a secret store. Persist its Secret before encrypting durable data; do not generate keys at each boot.

func ParseKey

func ParseKey(id KeyID, encoded secret.String) (Key, error)

ParseKey accepts exactly 32 bytes encoded as canonical unpadded base64url. It performs no I/O and never logs or includes key material in errors.

func (Key) Format

func (Key) Format(s fmt.State, _ rune)

func (Key) ID

func (k Key) ID() KeyID

func (Key) LogValue

func (Key) LogValue() slog.Value

func (Key) MarshalJSON

func (Key) MarshalJSON() ([]byte, error)

func (Key) Secret

func (k Key) Secret() secret.String

type KeyID

type KeyID string

KeyID is a non-secret stable name persisted in encrypted envelopes. Never change the material assigned to an existing ID.

type Keyring

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

Keyring encrypts with one active key and decrypts with its retained keys. It is immutable and safe for concurrent use. Retain old keys until every record has been re-encrypted; unknown IDs never fall back to the active key.

func NewKeyring

func NewKeyring(active KeyID, keys ...Key) (*Keyring, error)

func (*Keyring) ActiveID

func (r *Keyring) ActiveID() KeyID

func (*Keyring) Decrypt

func (r *Keyring) Decrypt(ctx context.Context, binding Context, ciphertext Ciphertext) (secret.String, error)

Decrypt returns no plaintext on any failure. Missing keys and failed authentication share one error. It never accepts legacy or unauthenticated formats, retries other keys, or silently replaces a missing key.

func (*Keyring) Encrypt

func (r *Keyring) Encrypt(ctx context.Context, binding Context, plaintext secret.String) (Ciphertext, error)

Encrypt owns its output and generates its nonce in Go's standard AEAD. Context checks bound entry/exit; the bounded local cipher operation is not preemptible. An admitted encryption consumes budget even if cancellation discards output.

func (Keyring) Format

func (Keyring) Format(s fmt.State, _ rune)

func (Keyring) LogValue

func (Keyring) LogValue() slog.Value

func (Keyring) MarshalJSON

func (Keyring) MarshalJSON() ([]byte, error)

func (*Keyring) Reencrypt

func (r *Keyring) Reencrypt(ctx context.Context, binding Context, ciphertext Ciphertext) (Ciphertext, error)

Reencrypt authenticates with the retained source key and creates a fresh envelope using the active key, preserving the exact context and plaintext. Callers own atomic record replacement; this method does no persistence.

func (*Keyring) Validate

func (r *Keyring) Validate() error

type Purpose

type Purpose string

Purpose separates uses of encryption, for example auth.mfa.totp.v1.

Jump to

Keyboard shortcuts

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