crypto

package
v0.0.0-...-5957f53 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
var (
	ErrInvalidKey        = errors.New("encryption key must be 32 bytes (AES-256)")
	ErrInvalidCiphertext = errors.New("invalid ciphertext")
)
View Source
var (
	ErrInvalidKEK    = errors.New("KEK must be exactly 32 bytes")
	ErrDecryptFailed = errors.New("decryption failed")
)

Functions

func Decrypt

func Decrypt(masterKey []byte, ciphertext string) (string, error)

Decrypt decrypts base64-encoded ciphertext produced by Encrypt.

func Encrypt

func Encrypt(masterKey []byte, plaintext string) (string, error)

Encrypt encrypts plaintext using AES-256-GCM with a random nonce. Returns base64-encoded ciphertext with nonce prepended.

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

func NewEncryptor(kekPath string) (*Encryptor, error)

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

func NewEncryptorFromBytes(kek []byte) (*Encryptor, error)

NewEncryptorFromBytes creates an Encryptor from a raw KEK slice. Useful for testing. The slice is copied internally.

func (*Encryptor) Decrypt

func (e *Encryptor) Decrypt(encDEK, encData, aad []byte) ([]byte, error)

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.

func (*Encryptor) Encrypt

func (e *Encryptor) Encrypt(plaintext, aad []byte) (encDEK, encData []byte, err error)

Encrypt encrypts plaintext with a fresh random DEK, then encrypts the DEK with the KEK. Both use AES-256-GCM. AAD is bound to both layers.

Returns (encryptedDEK, encryptedData) as raw bytes for storage. Caller is responsible for zeroing plaintext after use if sensitive.

Jump to

Keyboard shortcuts

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