Skip to main content

Encryption at Rest, Field-Level & E2E

Level 8: Level 8: Security, Privacy & Multi-tenancyhard30 minencryptionenvelopecrypto-shredding

Use envelope encryption with per-tenant/per-user DEKs wrapped by an HSM-held KEK, pick granularity (disk, TDE, field, E2E) by how much breach exposure and searchability you can trade, and design keys so crypto-shredding gives you instant, backup-proof erasure.

Encryption at rest makes stolen storage useless

Encryption at rest exists to make stolen storage useless: a lost disk, a leaked backup, or an exfiltrated database snapshot should decrypt to nothing. The critical design lever is granularity, because it decides how much a single breach exposes and what you can still do with the data.

Envelope encryption is the engine

You do not encrypt terabytes directly with a master key. Instead a Data Encryption Key (DEK) encrypts the actual data, and a Key Encryption Key (KEK) living in a KMS or HSM wraps (encrypts) the DEK. You store the wrapped DEK next to the ciphertext; to read, you send the wrapped DEK to KMS, which unwraps it (the KEK never leaves the HSM), and you decrypt locally. This gives cheap key rotation (re-wrap DEKs, no data rewrite), a hardware-guarded root of trust, and per-tenant or per-record DEKs so one leaked DEK exposes one tenant, not everyone.

Check yourself
The wrapped DEK sits right next to the ciphertext it protects. An attacker steals the whole database snapshot. Do they have everything they need to decrypt?
 plaintext --AES-256-GCM(DEK)--> ciphertext   [stored together]
     DEK --wrap(KEK in KMS/HSM)--> wrapped DEK  [stored together]
     KEK  never leaves the HSM boundary
 breach of storage alone  => attacker has ciphertext + wrapped DEK, no KEK => useless

The granularity ladder

  • Full-disk / volume encryption (LUKS, cloud EBS encryption). Protects a physically stolen disk. But a running app and anyone with DB access see full plaintext, so it does nothing against a compromised app or a leaked query result. Zero searchability cost.
  • Database TDE (Transparent Data Encryption). The DB encrypts files/pages. Same weakness: it is transparent, so a valid connection reads plaintext. Protects backups and stolen data files. Full query/index functionality preserved.
  • Application / field-level encryption. Your app encrypts specific columns (SSN, card number) before writing, so the DB only ever holds ciphertext. A stolen snapshot and a compromised DB both reveal nothing for those fields. The cost is searchability: you cannot do WHERE ssn = ? or range queries on a randomized-encrypted column.
  • Client-side / end-to-end (E2EE). The client encrypts so the server never sees plaintext at all (Signal, WhatsApp, 1Password). Maximum protection, maximum functional cost: the server cannot search, index, or process the data, and you must solve key distribution and recovery.
Check yourself

For each threat, pick the weakest encryption tier that actually stops it.

A drive is pulled out of the datacenter
A database backup file leaks from object storage
A rogue DBA runs 'SELECT ssn FROM users'
An attacker with a live DB connection dumps query results
A subpoena demands plaintext the server can produce
Your own compromised app server reads what it stored

The searchability tradeoff has a nuance: deterministic encryption (same plaintext to same ciphertext) allows equality lookups and joins but leaks which rows share a value and enables frequency analysis; randomized encryption (fresh nonce each time, the AES-256-GCM default) leaks nothing but kills search. Real systems use randomized for most fields and reach for deterministic, blind indexes, or dedicated searchable-encryption schemes only where lookup is required, accepting the leakage.

Interview nuance: "crypto-shredding" is how encryption meets GDPR erasure and retention. If each user's data is encrypted under a per-user DEK, deleting that one key makes all their data unrecoverable instantly, even copies sitting in backups, replicas, and archives you cannot practically hard-delete. So a per-tenant/per-user key hierarchy is not just breach isolation, it is your "right to be forgotten" mechanism. And remember to encrypt backups and logs too; a plaintext backup or a log line full of PII is the most commonly forgotten copy.

Recap: use envelope encryption with per-tenant/per-user DEKs wrapped by an HSM-held KEK, pick granularity (disk, TDE, field, E2E) by how much breach exposure and searchability you can trade, and design keys so crypto-shredding gives you instant, backup-proof erasure.

Check yourself
A GDPR erasure request lands. The user's data lives in the primary DB, five replicas, a WAL archive, and 90 days of backups. What is the fastest complete erasure?

Apply

Your turn

The task this lesson builds to.

Design encryption for a health/finance app storing PII so a stolen DB snapshot or backup reveals nothing usable.

Think about

  1. How does envelope encryption (DEK/KEK) work?
  2. What is the searchability tradeoff across disk vs field vs client-side encryption?
  3. How does crypto-shredding support GDPR erasure?

Practice

Make it stick

A second problem on the same idea, so it survives past today.

Design the encryption architecture for a password manager like 1Password serving 100M vault items, where the company must never be able to read a customer's passwords even under subpoena, yet users must sync across devices, recover a lost password, and search their vault. Explain the key hierarchy and where each operation happens.

Think about

  1. Why does true E2EE mean all crypto happens client-side?
  2. How do the master password plus a device Secret Key root the key hierarchy?
  3. Why is server-side recovery impossible, and what replaces it?