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
- Variables
- func ReservedSubject(subjectID string) bool
- func SubjectUUID(subjectID string) uuid.UUID
- func ValidScope(s ports.KeyScope) error
- type KMS
- func (k *KMS) Decrypt(ctx context.Context, subjectID string, scope ports.KeyScope, ciphertext []byte) ([]byte, error)
- func (k *KMS) DestroyDEK(ctx context.Context, subjectID string, scope ports.KeyScope) error
- func (k *KMS) Encrypt(ctx context.Context, subjectID string, scope ports.KeyScope, plaintext []byte) ([]byte, error)
Constants ¶
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 )
const Version1Overhead = 1 + nonceSize + gcmTagSize
Version1Overhead is the byte overhead of the v1 format: version byte + GCM nonce + GCM tag.
Variables ¶
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.
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
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 ¶
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 ¶
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 ¶
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) DestroyDEK ¶
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.