kmslocal

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 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 kmslocal is Dilion's default ports.KMS: AES-256-GCM envelope encryption with per-(subject, scope) data keys stored in dilion_pii.subject_keys, wrapped by a master key held in process memory.

It is the batteries-included default (project.md §2.4/§2.7). Deployments with a real key manager inject their own ports.KMS (AWS KMS, Vault, HSM) instead; the master key never leaves this process here, so its protection is only as good as the host.

Crypto-shredding (DestroyDEK) overwrites the wrapped DEK with random bytes and stamps shredded_at, after which Decrypt fails permanently. Per §2.10 this is a *supporting* control: ciphertext in an old backup taken before the shred still contains the old wrapped DEK, so restore-time erasure replay remains the primary guarantee.

Index

Constants

View Source
const (
	// Version is the format byte prefixed to every ciphertext this package
	// produces: 0x01 || nonce || AES-256-GCM(ciphertext||tag).
	Version byte = 0x01

	// KeySize is the required master key (KEK) and generated DEK size.
	KeySize = 32
)
View Source
const Version1Overhead = 1 + nonceSize + gcmTagSize

Version1Overhead is the byte overhead of the v1 format: version byte + GCM nonce + GCM tag.

Variables

View Source
var (
	// ErrShredded is returned when the subject key has been destroyed. It is
	// terminal: the plaintext is unrecoverable by design.
	ErrShredded = errors.New("kmslocal: subject key destroyed (crypto-shredded)")
	// ErrNoKey is returned when decrypting for a subject/scope that never had a key.
	ErrNoKey = errors.New("kmslocal: no key for subject/scope")
	// ErrMalformed is returned for ciphertext that is truncated or has an
	// unknown version byte.
	ErrMalformed = errors.New("kmslocal: malformed ciphertext")
	// ErrBadMasterKey is returned by every operation when New was given a key
	// that is not KeySize bytes.
	ErrBadMasterKey = errors.New("kmslocal: master key must be 32 bytes")
)

Sentinel errors. Callers (erasure pipeline, PII service) branch on these.

View Source
var ErrReservedSubject = errors.New("kmslocal: subject id is reserved")

ErrReservedSubject refuses a UUID subject id that is the storage key of a pseudo-subject.

Functions

func ReservedSubject added in v0.0.5

func ReservedSubject(subjectID string) bool

ReservedSubject reports whether subjectID is a UUID that SubjectUUID also produces for a pseudo-subject. Such an id would reach the pseudo-subject's key: erasing a "user" with the UUID of "system" would shred every webhook secret. The UUIDs are derivable from this source, so they are refused as data subjects outright.

func SubjectUUID

func SubjectUUID(subjectID string) uuid.UUID

SubjectUUID maps a ports.KMS subject id onto the uuid primary key of dilion_pii.subject_keys. Data subjects are canonical user UUIDs and map to themselves; non-UUID subjects (platform pseudo-subjects such as "system", which holds webhook secrets) are mapped deterministically with UUIDv5 so they can share the table without widening the column.

func ValidScope

func ValidScope(s ports.KeyScope) error

ValidScope rejects scopes outside the DB check constraint.

Types

type KMS

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

KMS implements ports.KMS.

func New

func New(pool *pgxpool.Pool, masterKey []byte) *KMS

New returns a KMS wrapping subject DEKs with masterKey (exactly 32 bytes). A bad key is reported from every operation rather than panicking, so an embedder's misconfiguration surfaces as a request error, not a crash.

DEKs are intentionally not cached in memory: every operation re-reads dilion_pii.subject_keys so a shred performed by another process takes effect immediately.

func (*KMS) Decrypt

func (k *KMS) Decrypt(ctx context.Context, subjectID string, scope ports.KeyScope, ciphertext []byte) ([]byte, error)

func (*KMS) DestroyDEK

func (k *KMS) DestroyDEK(ctx context.Context, subjectID string, scope ports.KeyScope) error

DestroyDEK crypto-shreds the subject key: shredded_at is stamped and the wrapped DEK is overwritten with random bytes so it cannot be unwrapped again. It is idempotent, and records a tombstone even when no key existed yet so a later Encrypt cannot silently mint a fresh key for an erased subject.

func (*KMS) Encrypt

func (k *KMS) Encrypt(ctx context.Context, subjectID string, scope ports.KeyScope, plaintext []byte) ([]byte, error)

Jump to

Keyboard shortcuts

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