Skip to main content
← CONCEPTS / SECURITY
An end-to-end domain learning journey

Envelope Encryption (KEK
-> DEK Hierarchy).

Sending 100k QPS of 1 MB payloads directly to an HSM saturates network links and hardware crypto appliances at ~2k QPS.
Start the journey3 sections
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

Encrypt data locally with a fast per-record DEK (AES-256-GCM), wrap the DEK with a cached tenant KEK, and reserve the HSM strictly for unwrapping KEKs.

TL;DR30-second simplified summary
▾

Envelope encryption decouples bulk data encryption (in app/pod RAM) from root-of-trust key wrapping (HSM/KMS), cutting HSM load by 99.9%.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
AES-256-GCM Authenticated Encryption
Provides confidentiality and integrity using a 96-bit nonce and optional Additional Authenticated Data (AAD).
Hardware Security Modules (HSM)
Tamper-resistant hardware appliances whose master keys never leave silicon, capped at ~2k-10k ops/sec.
Full walkthrough · 3 stages
01
L4 · FUNDAMENTALS

P — The HSM Bandwidth & Blast-Radius Bottleneck.

Direct HSM Crypto vs. Envelope Encryption

Before · Direct HSM Encryption
After · Envelope Encryption (KEK -> DEK)

An HSM appliance performs 2,000–10,000 crypto ops/sec and requires a network RTT per call. Shipping megabyte payloads to an HSM saturates NICs and stalls every microservice. Conversely, sharing one static AES key across 100M rows violates the AES-GCM 96-bit nonce birthday bound (~2^32 ops) and turns any key leak into a total database compromise.

02
L4 · FUNDAMENTALS

R & I — Key Wrapping & AAD Context Binding in Python.

Envelope Encryption Pipeline

Engineering Decision Framework
Problem
How do you encrypt 100M records at 100k QPS with hardware-backed root keys?
Naive Solution
Call HSM Encrypt() directly for every record payload.
Why It Fails
HSM appliances saturate at ~2k-10k ops/sec and add network RTT for every megabyte.
Alternatives
Envelope Encryption (HMK -> KEK -> DEK),Direct HSM encryption,Static app-memory AES key
Engineering Decision
Use Envelope Encryption with singleflight-cached KEKs (60s TTL) and fresh per-record DEKs.
Trade-Offs
Adds ~60 bytes of wrapped_dek + nonces per record, but reduces HSM load by 99.9%.
Decision Exercise · Make the Call

Why do we pass tenant_id as Additional Authenticated Data (AAD) in AES-256-GCM?

Every ciphertext envelope stores (kek_version, wrapped_dek, nonce, ciphertext_with_tag). Binding tenant_id and kek_version as AAD prevents an attacker with DB write access from copying a ciphertext from Tenant A's row into Tenant B's row:

import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

def envelope_encrypt(kek: bytes, tenant_id: str, plaintext: bytes) -> dict:
    dek = os.urandom(32)  # Fresh 256-bit per-record DEK
    aad = tenant_id.encode("utf-8")
    dek_nonce = os.urandom(12)
    wrapped_dek = AESGCM(kek).encrypt(dek_nonce, dek, aad)
    data_nonce = os.urandom(12)
    ciphertext = AESGCM(dek).encrypt(data_nonce, plaintext, aad)
    return {"wrapped_dek": wrapped_dek, "dek_nonce": dek_nonce, "data_nonce": data_nonce, "ct": ciphertext}
03
L5 · APPLIED DESIGN

M & E — Lazy DEK Re-Wrapping & When Not to Use It.

KEK Version Lifecycle

Key rotated100% DEKs re-wrappedV3_ACTIVE_ENCRYPTV2_DECRYPT_ONLYV1_DEPRECATED

When rotating KEK v2 -> v3, a background worker unwraps wrapped_dek with v2 and re-wraps it with v3 without ever touching the multi-megabyte payload. When NOT to use per-record DEKs: Tiny < 32-byte fields (e.g., 16-digit card tokens) where 60 bytes of envelope metadata triples storage; use a single deterministic/epoch DEK with key rotation instead.

Recommended Learning Flow