Documentation
¶
Overview ¶
Package crypto provides AES-256-GCM encryption for sensitive data at rest. Used to encrypt tenant connection strings in the Platform DB.
Envelope encryption provides KEK/DEK two-layer encryption with AAD.
Each secret gets its own random DEK (Data Encryption Key). The DEK encrypts the data, and the KEK (Key Encryption Key) encrypts the DEK. This allows key rotation by re-encrypting only the DEKs (fast), not the data.
AAD (Additional Authenticated Data) binds the ciphertext to its context. If someone swaps encrypted blobs between rows, aead.Open() fails because the AAD won't match. Callers provide the AAD (e.g., "credential_id||device_id||tenant_id").
Usage:
enc, err := crypto.NewEncryptor("/run/secrets/bb_kek")
encDEK, encData, err := enc.Encrypt(plaintext, aad)
plaintext, err := enc.Decrypt(encDEK, encData, aad)
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidKey = errors.New("encryption key must be 32 bytes (AES-256)") ErrInvalidCiphertext = errors.New("invalid ciphertext") )
var ( ErrInvalidKEK = errors.New("KEK must be exactly 32 bytes") ErrDecryptFailed = errors.New("decryption failed") )
Functions ¶
Types ¶
type Encryptor ¶
type Encryptor struct {
// contains filtered or unexported fields
}
Encryptor provides KEK/DEK envelope encryption with AAD. Thread-safe: the KEK is read-only after construction.
func NewEncryptor ¶
NewEncryptor loads a KEK from a file and validates it is exactly 32 bytes. The file should contain raw bytes (not hex-encoded).
func NewEncryptorFromBytes ¶
NewEncryptorFromBytes creates an Encryptor from a raw KEK slice. Useful for testing. The slice is copied internally.
func (*Encryptor) Decrypt ¶
Decrypt decrypts the DEK with the KEK, then decrypts data with the DEK. Both use AES-256-GCM with the provided AAD. If AAD doesn't match what was used during encryption, decryption fails (prevents blob swapping).
Caller is responsible for rate limiting decryption operations.