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

Envelope Encryption Hierarchy &
Shamir Quorum Unseal.

Encrypting gigabytes of secrets directly under one master key forces full ciphertext re-encryption on every key rotation; envelope encryption isolates a Root Key, KEK, and per-secret DEK for O(1) rotation.
Start the journey2 sections· Last modified Dec 2, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

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.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
AES-256-GCM AEAD
Authenticated encryption providing both confidentiality and tamper-detection tags.
Shamir's Secret Sharing
Splits a master key into N shares requiring any K threshold shares to reconstruct.
Full walkthrough · 2 stages
01
STAGE 01beginner

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.

Before · Single Master Key
After · Envelope KMS (KEK + DEK)
ChoiceWhat it buysWhat it costs
Single Master KeySimple mental modelO(N * size) re-encryption on rotation
Envelope KMS (KEK -> DEK)O(1) 32-byte DEK re-wrap; per-secret isolationRequires 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}
02
STAGE 02intermediate

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.

Engineering Decision Framework
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.
Decision Exercise · Make the Call

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 · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why does envelope encryption make KEK rotation O(1) per secret regardless of payload size?

Recommended Learning Flow