When rotating KEKs across terabytes of secrets, re-wrap only the 32-byte DEKs in O(1) without touching ciphertexts.
TL;DR30-second simplified summary▾
Combines threshold unsealing, fresh 96-bit GCM nonces, and zero-downtime key rotation.
Root -> KEK -> DEK Separation.
Why this matters: Eliminate full-corpus re-encryption during key rotation.
Each secret gets its own 256-bit DEK encrypted with AES-256-GCM. The DEK itself is wrapped by a KEK and stored alongside the ciphertext.
| Choice | What it buys | What it costs |
|---|---|---|
| Single Master Key | Simple mental model | O(N * size) re-encryption on rotation |
| Envelope KMS (KEK -> DEK) | O(1) 32-byte DEK re-wrap; per-secret isolation | Requires storing wrapped DEK metadata |
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def envelope_encrypt(kek: bytes, plaintext: bytes, aad: bytes) -> dict:
dek = AESGCM.generate_key(bit_length=256)
nonce_data, nonce_dek = os.urandom(12), os.urandom(12)
ciphertext = AESGCM(dek).encrypt(nonce_data, plaintext, aad)
wrapped_dek = AESGCM(kek).encrypt(nonce_dek, dek, aad)
return {"wrapped_dek": wrapped_dek, "nonce_dek": nonce_dek, "nonce_data": nonce_data, "ciphertext": ciphertext}
Shamir Quorum Unseal & Nonce Safety.
Why this matters: Prevent single-admin key custody and AES-GCM nonce reuse.
In Shamir 3-of-5 unseal (or Cloud KMS Auto-Unseal), no single operator holds the Root Key. Fresh 96-bit random nonces per DEK prevent catastrophic GCM auth-key recovery.
- Problem
- How to rotate encryption keys across 10M stored secrets without downtime or multi-hour re-encryption backfills.
- Naive Solution
- Encrypt every secret directly with a single static master key and run a batch job to re-encrypt all blobs on rotation.
- Why It Fails
- Re-encrypting terabytes of secrets saturates storage I/O, risks partial-migration corruption, and exposes a single point of compromise.
- Alternatives
- Direct master-key batch re-encryption vs. Envelope encryption (Root -> KEK -> DEK) with lazy or O(1) DEK re-wrapping.
- Engineering Decision
- Encrypt each secret with a unique 256-bit DEK and wrap that DEK with a versioned KEK; rotate by re-wrapping only the 32-byte DEK.
- Trade-Offs
- Adds ~60 bytes of wrapped-DEK + nonce metadata per secret, but cuts rotation work by 1000x+ and bounds GCM nonce wear.
Compliance requires rotating the primary KEK every 90 days across 50 TB of encrypted secrets. Which approach completes safely with zero downtime?
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import java.security.SecureRandom;
public final class EnvelopeCipher {
private static final SecureRandom RNG = new SecureRandom();
public byte[] encryptWithFreshDek(SecretKey kek, byte[] plaintext) throws Exception {
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(256);
SecretKey dek = kg.generateKey();
byte[] iv = new byte[12];
RNG.nextBytes(iv);
Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
c.init(Cipher.ENCRYPT_MODE, dek, new GCMParameterSpec(128, iv));
return c.doFinal(plaintext);
}
}
Practice: Make the trade-off yourself.
Why does envelope encryption make KEK rotation O(1) per secret regardless of payload size?