encryption

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

encryption

Envelope encryption for event-sourced Go systems — with PII crypto-shredding and secret key rotation.

Features

  • Envelope encryption — two-tier key hierarchy (system KEK → per-scope DEK → data)
  • PII encryption — per-subject keys with GDPR crypto-shredding (destroy key → data permanently unreadable)
  • Secret encryption — versioned keys with rotation (old ciphertext stays decryptable)
  • Pluggable cipher — ships AES-256-GCM, bring your own cipher.Cipher
  • Pluggable key store — ships PostgreSQL adapter, bring your own keystore.KeyStore
  • Deterministic hashing — HMAC-SHA256 for generating aggregate IDs from sensitive data
  • Memory hygiene — DEKs are zeroed after use via ZeroBytes
  • Zero external dependencies — only Go standard library

Getting Started

The library ships a PostgreSQL-backed key store. You need three things to get going: a system keyring (KEK), a PostgreSQL database with the migration applied, and the module wired together.

Migration

The keystore/postgres/migrations package embeds the SQL migration. Apply it to your database before using the store — either by executing it directly or by feeding it to your migration tool:

CREATE TABLE IF NOT EXISTS infrastructure.encryption_keys (
    scope         TEXT        NOT NULL,
    scope_id      TEXT        NOT NULL,
    key_version   INT         NOT NULL,
    encrypted_key BYTEA       NOT NULL,
    system_key_id TEXT        NOT NULL DEFAULT 'default',
    created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    revoked_at    TIMESTAMPTZ,
    PRIMARY KEY (scope, scope_id, key_version)
);

The full migration (with indexes and constraints) is embedded in migrations.FS and can be read at runtime:

import "github.com/eventsalsa/encryption/keystore/postgres/migrations"

data, _ := migrations.FS.ReadFile("001_encryption_keys.sql")

Both the schema (infrastructure) and table name (encryption_keys) are configurable via postgres.Config.

Wiring the Module
package main

import (
	"context"
	"database/sql"
	"log"

	"github.com/eventsalsa/encryption"
	_ "github.com/eventsalsa/encryption/cipher/aesgcm"
	"github.com/eventsalsa/encryption/keystore/postgres"
	"github.com/eventsalsa/encryption/systemkey"
)

func main() {
	ctx := context.Background()

	db, err := sql.Open("postgres", "postgres://localhost/myapp?sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}

	keyring := systemkey.NewKeyring(
		map[string][]byte{"key-1": loadKeyFromVault()}, // 32-byte AES key
		"key-1", // active key ID
	)

	store := postgres.NewStore(postgres.Config{}, db) // defaults: schema "infrastructure", table "encryption_keys"

	m := encryption.New(encryption.Config{
		Keyring: keyring,
		Store:   store,
	})

	// Create a DEK, encrypt, and decrypt.
	_, err = m.KeyManager.CreateKey(ctx, "user-pii", "user-123")
	if err != nil {
		log.Fatal(err)
	}

	ciphertext, version, err := m.Envelope.Encrypt(ctx, "user-pii", "user-123", "alice@example.com")
	if err != nil {
		log.Fatal(err)
	}

	plaintext, err := m.Envelope.Decrypt(ctx, "user-pii", "user-123", ciphertext, version)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("decrypted:", plaintext)
}

By default, all operations go through the *sql.DB connection pool — no transaction required for simple use cases.

Transaction Participation

In event-sourced systems you typically want key creation and event persistence to happen atomically. Use keystore.WithTx to attach an existing *sql.Tx to the context — all key store operations within that context will use the transaction instead of the pool:

import "github.com/eventsalsa/encryption/keystore"

tx, err := db.BeginTx(ctx, nil)
if err != nil {
	return err
}
defer tx.Rollback()

ctx = keystore.WithTx(ctx, tx)

// Both the key creation and the encrypt happen inside the transaction.
_, err = m.KeyManager.CreateKey(ctx, "user-pii", userID)
if err != nil {
	return err
}

ciphertext, version, err := m.Envelope.Encrypt(ctx, "user-pii", userID, email)
if err != nil {
	return err
}

// Persist the aggregate event in the same transaction...

return tx.Commit()
Custom Transaction Extractor

If your application already propagates transactions through its own context key (e.g., a Unit of Work or CQRS middleware), use NewStoreWithTxExtractor so the key store picks up your transaction automatically:

store := postgres.NewStoreWithTxExtractor(postgres.Config{}, db,
	func(ctx context.Context) *sql.Tx {
		return myuow.TxFromContext(ctx)
	},
)

Resolution order: custom extractor → keystore.TxFromContext → *sql.DB fallback.

Architecture

┌─────────────────────────────────────────────────────┐
│                    Application                      │
│                                                     │
│   pii.Adapter[ID]          secret.Adapter           │
│       │                        │                    │
│       └────────┬───────────────┘                    │
│                ▼                                    │
│        envelope.Encryptor                           │
│         │          │        │                       │
│         ▼          ▼        ▼                       │
│   systemkey     keystore   cipher                   │
│   .Keyring      .KeyStore  .Cipher                  │
│                                                     │
│   ┌────────┐  ┌──────────┐  ┌──────────────┐        │
│   │ KEK(s) │  │ Encrypted│  │ AES-256-GCM  │        │
│   │ in mem │  │ DEKs in  │  │ (or custom)  │        │
│   │        │  │ Postgres │  │              │        │
│   └────────┘  └──────────┘  └──────────────┘        │
└─────────────────────────────────────────────────────┘

Envelope encryption flow:

  Encrypt:
    1. Fetch encrypted DEK from KeyStore (by scope + scopeID)
    2. Decrypt DEK with system KEK from Keyring
    3. Encrypt plaintext with DEK using Cipher
    4. Return base64-encoded ciphertext + key version

  Decrypt:
    1. Fetch encrypted DEK for specific key version
    2. Decrypt DEK with system KEK from Keyring
    3. Decrypt ciphertext with DEK using Cipher
    4. Return plaintext
Package Overview
Package Role
encryption Top-level module wiring (New, NewWithDefaults, Config, Module)
cipher Cipher interface for symmetric encrypt/decrypt
cipher/aesgcm AES-256-GCM implementation (auto-registers as default on import)
systemkey Keyring interface + in-memory and file-based implementations for system KEKs
keystore KeyStore interface, EncryptedKey type, WithTx/TxFromContext context helpers
keystore/postgres PostgreSQL-backed KeyStore with configurable schema/table and tx extraction
keymanager Manager — DEK lifecycle: create, rotate, revoke, destroy
envelope Encryptor — envelope encrypt/decrypt engine
pii EncryptedValue, generic Encryptor[ID]/Decryptor[ID] interfaces, Adapter
secret EncryptedValue (with KeyVersion), Encryptor/Decryptor interfaces, Adapter
hash Hasher interface + HMAC-SHA256 implementation
encerr Shared sentinel errors (re-exported by root package)
testutil NewTestKeyring() + InMemoryKeyStore for testing

PII vs Secrets

The library provides two domain adapters on top of the envelope engine. Choose based on your use case:

PII Secret
Value type pii.EncryptedValue (opaque string) secret.EncryptedValue (Content + KeyVersion)
Key version Always 1 (hardcoded) Versioned, increments on rotation
Key rotation ✗ Not supported ✓ keymanager.RotateKey
Crypto-shredding ✓ Primary use case ✓ Supported
Key revocation ✗ Not applicable ✓ keymanager.RevokeKeys
Subject scoping Generic ID fmt.Stringer per subject Scope + ScopeID strings
Use cases User emails, names, addresses API keys, tokens, credentials
PII Adapter

The PII adapter is generic over the subject ID type. It hardcodes key version to 1 (no rotation) — the intended lifecycle is create once, then crypto-shred on deletion:

type UserID string
func (id UserID) String() string { return string(id) }

adapter := pii.NewAdapter[UserID](m.Envelope, "user-pii")

encrypted, err := adapter.Encrypt(ctx, UserID("user-123"), "alice@example.com")
plaintext, err := adapter.Decrypt(ctx, UserID("user-123"), encrypted)
Secret Adapter

The secret adapter tracks key versions, allowing rotation while keeping old ciphertext decryptable:

adapter := secret.NewAdapter(m.Envelope)

encrypted, err := adapter.Encrypt(ctx, "integration", "stripe-key", "sk_live_xxx")
plaintext, err := adapter.Decrypt(ctx, "integration", "stripe-key", encrypted)

GDPR Crypto-Shredding

In event-sourced systems, events are immutable — you cannot delete or modify them. Crypto-shredding solves GDPR's "right to be forgotten" by destroying the encryption key instead of the data:

// When a user requests account deletion:
err := m.KeyManager.DestroyKeys(ctx, "user-pii", userID.String())

After DestroyKeys:

  1. The DEK is permanently deleted from the key store (DELETE, not soft-revoke)
  2. All events containing that user's PII still exist but are permanently undecryptable
  3. Any Decrypt call returns encryption.ErrKeyNotFound
  4. The event store remains intact — no immutability violation

Custom Implementations

Custom Cipher

Implement cipher.Cipher to use a different encryption algorithm:

package chacha

import "github.com/eventsalsa/encryption/cipher"

type ChaCha20 struct{}

func (c *ChaCha20) Encrypt(key, plaintext []byte) ([]byte, error) { /* ... */ }
func (c *ChaCha20) Decrypt(key, ciphertext []byte) ([]byte, error) { /* ... */ }
func (c *ChaCha20) KeySize() int { return 32 }

var _ cipher.Cipher = (*ChaCha20)(nil)

Pass it to the module:

m := encryption.New(encryption.Config{
	Keyring: keyring,
	Store:   store,
	Cipher:  &chacha.ChaCha20{},
})
Custom KeyStore

Implement keystore.KeyStore to use a different storage backend. The interface is storage-agnostic — no SQL types in the signatures:

package dynamo

import (
	"context"
	"github.com/eventsalsa/encryption/keystore"
)

type Store struct{ /* ... */ }

func (s *Store) GetActiveKey(ctx context.Context, scope, scopeID string) (*keystore.EncryptedKey, error) { /* ... */ }
func (s *Store) GetKey(ctx context.Context, scope, scopeID string, version int) (*keystore.EncryptedKey, error) { /* ... */ }
func (s *Store) CreateKey(ctx context.Context, scope, scopeID string, version int, encryptedDEK []byte, systemKeyID string) error { /* ... */ }
func (s *Store) RevokeKeys(ctx context.Context, scope, scopeID string) error { /* ... */ }
func (s *Store) DestroyKeys(ctx context.Context, scope, scopeID string) error { /* ... */ }

var _ keystore.KeyStore = (*Store)(nil)

Testing

The testutil package provides in-memory implementations for integration tests and local development — no database required:

import (
	"testing"

	"github.com/eventsalsa/encryption"
	"github.com/eventsalsa/encryption/testutil"
	_ "github.com/eventsalsa/encryption/cipher/aesgcm"
)

func TestMyFeature(t *testing.T) {
	m := encryption.New(encryption.Config{
		Keyring: testutil.NewTestKeyring(),       // random 32-byte key, ID "test-key-1"
		Store:   testutil.NewInMemoryKeyStore(),   // thread-safe in-memory store
	})

	// Use m.KeyManager, m.Envelope, pii.NewAdapter, etc.
}

NewTestKeyring generates a random key on each call, so tests are naturally isolated. InMemoryKeyStore is safe for concurrent use with -race.

Build & Test

go build ./...
go test -race ./...
go vet ./...

Documentation

Overview

Package encryption provides envelope encryption for event-sourced systems.

It supports two encryption categories with distinct semantics:

  • PII (Personally Identifiable Information): per-subject encryption keys with no rotation, supporting GDPR crypto-shredding via key destruction.
  • Secrets (API credentials, tokens): versioned encryption keys with rotation support, where old ciphertext remains decryptable.

Architecture

The library uses envelope encryption: data is encrypted with a Data Encryption Key (DEK), and the DEK is encrypted with a system Key Encryption Key (KEK).

  • cipher/: pluggable symmetric encryption (default: AES-256-GCM)
  • systemkey/: system key (KEK) management
  • keystore/: encrypted DEK persistence
  • keymanager/: key lifecycle (create, rotate, revoke, destroy)
  • envelope/: envelope encryption engine
  • pii/: PII value types and adapters
  • secret/: secret value types and adapters
  • hash/: deterministic HMAC-SHA256 hashing

Quick Start

Use New or NewWithDefaults to create a fully wired Module:

m := encryption.New(encryption.Config{
    Keyring: keyring,
    Store:   store,
})

Package encryption provides envelope encryption for event-sourced systems.

This package defines shared sentinel errors used across all sub-packages.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyNotFound    = encerr.ErrKeyNotFound
	ErrKeyExists      = encerr.ErrKeyExists
	ErrEncryption     = encerr.ErrEncryption
	ErrDecryption     = encerr.ErrDecryption
	ErrInvalidKeySize = encerr.ErrInvalidKeySize
	ErrKeyRevoked     = encerr.ErrKeyRevoked
	ErrKeyDestroyed   = encerr.ErrKeyDestroyed
)

Sentinel errors re-exported from encerr for public use.

View Source
var DefaultCipherFactory func() cipher.Cipher

DefaultCipherFactory is called to create the default cipher when Config.Cipher is nil. It is set automatically by importing a cipher package that registers itself (e.g., cipher/aesgcm).

If no cipher package has been imported and Config.Cipher is nil, New will panic.

Functions

func ZeroBytes

func ZeroBytes(b []byte)

ZeroBytes overwrites a byte slice with zeros to clear sensitive key material from memory. Use with defer after decrypting DEKs:

defer encryption.ZeroBytes(dek)

Types

type Config

type Config struct {
	Keyring systemkey.Keyring
	Store   keystore.KeyStore
	Cipher  cipher.Cipher // optional — defaults to AES-256-GCM
}

Config holds all configuration needed for the encryption module.

type Module

type Module struct {
	KeyManager *keymanager.Manager
	Envelope   *envelope.Encryptor
	Hasher     hash.Hasher // nil if no HMAC key provided
}

Module is the assembled encryption module providing all components.

func New

func New(cfg Config) *Module

New creates a fully wired encryption module from the given config. If cfg.Cipher is nil, the cipher registered via DefaultCipherFactory is used (AES-256-GCM when cipher/aesgcm is imported).

func NewWithDefaults

func NewWithDefaults(keyring systemkey.Keyring, store keystore.KeyStore, opts ...Option) *Module

NewWithDefaults creates a module with AES-256-GCM cipher. Provide a KeyStore and Keyring; use options for additional configuration.

type Option

type Option func(*options)

Option is a functional option for NewWithDefaults.

func WithCipher

func WithCipher(c cipher.Cipher) Option

WithCipher sets a custom cipher implementation.

func WithHMACKey

func WithHMACKey(key []byte) Option

WithHMACKey enables the HMAC hasher with the given key.

Directories

Path Synopsis
Package cipher defines the symmetric encryption interface used throughout the encryption library.
Package cipher defines the symmetric encryption interface used throughout the encryption library.
aesgcm
Package aesgcm provides an AES-256-GCM implementation of the cipher.Cipher interface.
Package aesgcm provides an AES-256-GCM implementation of the cipher.Cipher interface.
Package encerr provides shared sentinel errors and utilities used across the encryption library's sub-packages.
Package encerr provides shared sentinel errors and utilities used across the encryption library's sub-packages.
Package envelope implements envelope encryption using a two-tier key hierarchy: system keys (KEKs) protect data encryption keys (DEKs), and DEKs protect application data.
Package envelope implements envelope encryption using a two-tier key hierarchy: system keys (KEKs) protect data encryption keys (DEKs), and DEKs protect application data.
examples
basic command
Example: basic envelope encryption with the encryption module.
Example: basic envelope encryption with the encryption module.
pii command
Example: PII encryption with crypto-shredding.
Example: PII encryption with crypto-shredding.
secrets command
Example: secret encryption with key rotation.
Example: secret encryption with key rotation.
Package keymanager orchestrates DEK lifecycle operations using envelope encryption.
Package keymanager orchestrates DEK lifecycle operations using envelope encryption.
Package testutil provides test helpers for encryption testing.
Package testutil provides test helpers for encryption testing.

Jump to

Keyboard shortcuts

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