Encryption & Key Management

AES-256-GCM-SIV storage encryption, encrypted columns, HMAC blind indexes, and online key rotation.

2 min read Security & Governance

SpydDB provides two independent cryptographic layers that keep sensitive data protected at rest on disk, across backups, and inside memory dumps:

  1. Storage Encryption at Rest (AES-256-GCM-SIV) for database pages, WAL payloads, and double-write staging buffers.
  2. Column-Level Envelope Encryption (AES-256-GCM-SIV) for individual fields marked encrypted, paired with keyed HMAC-SHA-256 Blind Indexes for exact equality queries without decryption.

1. Storage Encryption at Rest (storage.encryption_key_path)#

Encrypt all data pages, WAL frames, and staging pages using a 32-byte key stored outside the data directory (0600 owner-only permissions or injected via env:):

sh
umask 077
head -c 32 /dev/urandom > /etc/spyd/storage.key
chmod 600 /etc/spyd/storage.key

Configure in server.json:

json
{
  "storage": {
    "encryption_key_path": "/etc/spyd/storage.key"
  }
}

Or pass keys from an external KMS / Secrets Manager agent via environment variables:

json
{
  "storage": {
    "encryption_key_path": "env:SPYDDB_STORAGE_KEY"
  },
  "security": {
    "column_encryption_enabled": true,
    "master_key_path": "env:SPYDDB_COLUMN_KEYRING"
  }
}
  • Page headers and LSN trailers are authenticated via AEAD associated data; corrupted or tampered pages fail closed immediately rather than returning unauthenticated bytes.
  • Group- or world-readable key files (chmod 644) are rejected at startup.

2. Column Encryption & Blind Indexes#

Mark sensitive PII fields (ssn, tax_id, card_token) with encrypted, and attach using blind for exact-match lookups and unique constraints:

spydql
create collection customers {
  id: u64 primary auto_increment,
  email: string(160) not null,
  ssn: string(20) encrypted,

  unique index uniq_customers_ssn on (ssn) using blind
}

-- Exact equality transparently routes through the keyed HMAC blind index:
customers | where ssn == $1 | pick id, email

Security guarantees:

  • Every ciphertext binds (collection_id, field_id, tenant_id) in its AEAD associated data — ciphertext cannot be transplanted across columns or tenants.
  • Range (>/<), prefix LIKE, or sorting directly over encrypted fields is rejected at plan time so the engine never falls back to scanning plaintext.
  • Backups, WAL archives, and PITR streams store encrypted envelopes verbatim without re-exposing plaintext.

3. Online Key Rotation & Safe Retirement (Zero Downtime)#

Column Encryption Keys (HTTP Admin API)#

sh
# 1. Mint a new active column key and re-encrypt all encrypted values & columnar segments online
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/admin/keys/column/rotate \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN"

# 2. After expired backups no longer need old keys, prove zero rows reference old keys and retire them
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/admin/keys/column/retire \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN"

Storage Keys Online (key_root)#

Place the new 32-byte key file inside configured key_root:

sh
# 1. Re-encrypt every storage page online under the new key file inside key_root
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/admin/keys/storage/rotate \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"key_file": "storage-v2.key"}'

# 2. Update server.json before next restart:
#    "storage": { "encryption_key_path": "/etc/spyd/keys/storage-v2.key", "encryption_old_key_paths": ["/etc/spyd/keys/storage-v1.key"] }

# 3. Checkpoint and retire old storage keys once WAL segments roll over
curl -fsS -X POST --cacert tls/ca.crt https://localhost:8080/v1/admin/keys/storage/retire \
  -H "Authorization: Bearer $SPYD_ADMIN_TOKEN"

Offline CLI Rotation (spyd-cli)#

sh
spyd-cli --embedded --data /var/lib/spyd \
  --storage-key /etc/spyd/keys/storage-v2.key \
  --old-storage-key /etc/spyd/keys/storage-v1.key \
  --rotate-storage --retire-storage-keys

Retain prior keys safely in your external secret store for as long as retained backups or WAL archives depend on them. Losing an encryption key renders ciphertexts unrecoverable.