These are the notes for Cybersecurity Fundamentals (defensive), episode 27, the last episode of the foundations block. We've learned the algorithms (episodes 10 to 16). Today we invent, one problem at a time, how real systems still get them wrong and how to get them right: don't roll your own crypto, known plaintext, vetted libraries, the key lifecycle (generation, storage, rotation, revocation, destruction), the KMS (key management system), the HSM (hardware security module), key policies, envelope encryption with DEKs and KEKs, key wrapping and crypto-shredding, data at rest, in transit and in use, confidential computing, side channels and timing side channels, constant-time comparison, crypto agility, quantum computers, harvest now, decrypt later, post-quantum cryptography, ML-KEM and ML-DSA, and hybrid key exchange. Every term is explained the first time it appears, and the Glossary at the end defines them all again.
Ravi, Neha, every key, invoice, receipt, token and file here are made up, and everything runs locally on toy data. Every code sample runs with Python 3.13 and the cryptography package (50.0.1); outputs are real, copied from actual runs. Keys and nonces are random, so hex strings change on every run; sizes and results do not.
The problems you'll solve
| Problem | Idea it teaches |
|---|---|
| A developer invents his own encryption | Don't roll your own crypto, known plaintext, Kerckhoffs's principle, vetted libraries |
| The lock is perfect, so attackers go for the key | Key lifecycle, CSPRNG, KMS, HSM, key policy, rotation, revocation, destruction |
| Rotating one key over terabytes | Envelope encryption, DEK, KEK, wrapping, crypto-shredding |
| Data has to be decrypted to be used | Data at rest / in transit / in use, confidential computing, enclave |
| A correct algorithm leaks through time | Side channel, timing attack, constant-time comparison |
| Algorithms get old | Crypto agility, algorithm tags, upgrade on use, crypto inventory |
| Quantum computers | Harvest now, decrypt later, post-quantum cryptography, ML-KEM, ML-DSA, hybrid |
Problem 1: don't roll your own crypto
Ravi's shop stores invoices with customer names and card endings. A developer thinks AES sounds complicated, so he invents his own "encryption": XOR every byte with a secret word, chai42, repeated. XOR (exclusive or) flips bits wherever the key has a 1; applying the same key twice gives the original back. "Nobody knows my method, so it's safe."
But every invoice begins with the same text, INVOICE #. Text an attacker knows or can guess is called known plaintext.
Demo 1: one guessed word breaks it
# Ravi's developer invents "encryption": XOR every byte with a secret word, repeated
SECRET = b"chai42"
def my_encrypt(data):
return bytes(b ^ SECRET[i % len(SECRET)] for i, b in enumerate(data))
invoice = b"INVOICE #2291 | Neha | card 4417 | Rs 18,450"
cipher = my_encrypt(invoice)
print("encrypted:", cipher.hex()[:40], "...")
# An attacker knows every invoice starts with "INVOICE #". XOR that with the start of the ciphertext:
known = b"INVOICE #"
leaked = bytes(c ^ k for c, k in zip(cipher, known))
print("key bytes leaked:", leaked)
key = leaked[:6] # the pattern repeats every 6 bytes
print("decrypted:", bytes(b ^ key[i % 6] for i, b in enumerate(cipher)).decode())
Output:
encrypted: 2a2637267d712648425b060b52481d497a570b09 ...
key bytes leaked: b'chai42cha'
decrypted: INVOICE #2291 | Neha | card 4417 | Rs 18,450
XOR the known text with the start of the ciphertext and the key falls out (chai42cha); with the key, the whole invoice. Why? A repeating-key XOR leaks the key wherever the plaintext is known. Its secrecy depended on hiding the method: security through obscurity (episode 3). Kerckhoffs's principle says a system must stay secure even if everything except the key is public.
Demo 2: a vetted library
AES-256-GCM, the authenticated encryption (AEAD) from episode 12, from the cryptography package. Same invoice, same trick, and then one bit of the ciphertext is flipped.
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
key = AESGCM.generate_key(bit_length=256) # a vetted library: AES-256-GCM (episode 12)
invoice = b"INVOICE #2291 | Neha | card 4417 | Rs 18,450"
nonce = os.urandom(12)
cipher = AESGCM(key).encrypt(nonce, invoice, None)
print("encrypted:", cipher.hex()[:40], "...")
known = b"INVOICE #"
leaked = bytes(c ^ k for c, k in zip(cipher, known))
print("same trick, 'key bytes':", leaked)
try:
AESGCM(key).decrypt(nonce, cipher[:-1] + bytes([cipher[-1] ^ 1]), None) # flip one bit
except Exception as e:
print("tampered ciphertext:", type(e).__name__)
Output (hex and bytes are random each run):
encrypted: 7725c9338abf48a4ed8ebaf235b20fcd075d7727 ...
same trick, 'key bytes': b'>k\x9f|\xc3\xfc\r\x84\xce'
tampered ciphertext: InvalidTag
The trick yields random junk, not a key, and the flipped bit is caught (InvalidTag). Don't roll your own crypto means:
- Don't invent algorithms.
- Don't assemble low-level pieces yourself either (picking modes, padding, IVs, MACs by hand), because that's where most real-world failures happen.
- Use well-reviewed libraries with safe high-level APIs: libsodium, Google Tink, or
AESGCM/Fernetin Python'scryptography.
Problem 2: the key lifecycle, KMS and HSM
With AES-GCM the lock is perfect, so attackers go after the key. A key has a whole life, the key lifecycle:
| Stage | What it means | Get it right |
|---|---|---|
| Generation | Creating the key | A CSPRNG (cryptographically secure random generator, episode 10), the right length (AESGCM.generate_key(bit_length=256), secrets) |
| Storage | Where the key lives | Never in source code, config files, tickets or chat; in a KMS, HSM or secret manager |
| Use | Encrypting, decrypting, signing | Only by the apps allowed to (key policy), every use logged |
| Rotation | Replacing a key with a new one on a schedule | Limits how much one leaked key exposes |
| Revocation | Stopping trust in a key immediately | When it may have leaked, or its owner left |
| Destruction | Deleting every copy | Makes everything it encrypted unreadable forever: crypto-shredding when done on purpose |
Where keys should live
| A bank | Key management |
|---|---|
| The locker service: you ask, staff fetch, every visit logged | KMS (key management system): apps ask it to encrypt, decrypt or wrap keys; every use is logged |
| The steel vault itself, tamper-proof | HSM (hardware security module): a tamper-resistant device that creates and uses keys inside itself, so the most important keys never leave the hardware. A KMS can keep its master keys inside HSMs |
| The manager's list of who may open which locker | Key policy: which app or person may use which key for what, so a hacked web server can't decrypt payroll |
Problem 3: envelope encryption
All of Ravi's data (years of sales, invoices, backups) is encrypted under one key. Policy says rotate it yearly: decrypt and re-encrypt everything, days of work and a risky window. And a KMS handles small requests, not terabytes.
Envelope encryption uses two layers of keys:
- A DEK (data encryption key): a fresh key per file or record; encrypts the data locally, fast.
- A KEK (key encryption key), the master key: lives in the KMS; encrypts only the small DEKs. Encrypting a key with another key is wrapping it (decrypting it is unwrapping).
- Store the encrypted data and its wrapped DEK side by side. To read: ask the KMS to unwrap the DEK, then decrypt locally.
- Rotating the KEK = re-wrapping the tiny DEKs, never touching the data.
Demo 3: a toy KMS, two 50 MB files, a rotation
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
new_key = lambda: AESGCM.generate_key(bit_length=256)
class ToyKMS: # the master keys (KEKs) never leave this box
def __init__(self): self.keks = {"kek-v1": new_key()}
def wrap(self, kek_id, dek):
n = os.urandom(12); return n + AESGCM(self.keks[kek_id]).encrypt(n, dek, None)
def unwrap(self, kek_id, blob):
return AESGCM(self.keks[kek_id]).decrypt(blob[:12], blob[12:], None)
kms, files = ToyKMS(), {}
for name in ["sales-2025.csv", "sales-2026.csv"]:
dek, nonce = new_key(), os.urandom(12) # a fresh data key (DEK) per file
data = AESGCM(dek).encrypt(nonce, b"x" * 50_000_000, None) # 50 MB of sales data
files[name] = {"data": data, "nonce": nonce, "kek": "kek-v1", "wdek": kms.wrap("kek-v1", dek)}
print(f"{name}: data {len(data):,} bytes, wrapped DEK {len(files[name]['wdek'])} bytes")
kms.keks["kek-v2"] = new_key() # rotate the master key
for f in files.values(): # re-wrap the tiny keys, not the big data
f["wdek"], f["kek"] = kms.wrap("kek-v2", kms.unwrap(f["kek"], f["wdek"])), "kek-v2"
del kms.keks["kek-v1"] # retire and destroy the old master key
f = files["sales-2026.csv"]
plain = AESGCM(kms.unwrap(f["kek"], f["wdek"])).decrypt(f["nonce"], f["data"], None)
print("kek-v1 gone; sales-2026.csv decrypts:", f"{len(plain):,} bytes")
Output:
sales-2025.csv: data 50,000,016 bytes, wrapped DEK 60 bytes
sales-2026.csv: data 50,000,016 bytes, wrapped DEK 60 bytes
kek-v1 gone; sales-2026.csv decrypts: 50,000,000 bytes
Each file is 50 million bytes (plus a 16-byte GCM tag); each wrapped DEK is just 60 bytes (12-byte nonce + 32-byte key + 16-byte tag). Rotation touched only those 60-byte blobs, and after kek-v1 was destroyed the 2026 file still decrypts, because its DEK is now wrapped by kek-v2. Destroying one file's DEK crypto-shreds just that file (Practice 1).
Data at rest, in transit, in use
| State | Where the data is | Protection |
|---|---|---|
| At rest | Stored: disks, databases, backups | Disk, database or file encryption (often envelope encryption) |
| In transit | Moving across a network | TLS, VPNs, WPA3 (episodes 22, 25) |
| In use | Being processed in memory | Normally decrypted: anyone controlling the machine (even a cloud admin) could see it |
Confidential computing protects data in use: the processor runs code inside a hardware-protected area, an enclave or trusted execution environment (TEE), whose memory stays encrypted even from the machine's own operating system and administrators.
Problem 4: timing side channels
| An old safe | Code |
|---|---|
| Clicks on each correct digit; the lock itself is strong | A side channel: a leak through something other than the output (time, power use, sound, cache behaviour). Here, time |
| A thief listens and finds the code digit by digit: 10 tries per digit instead of a million | A timing attack: guess one piece at a time |
| A silent safe: same sound, right or wrong | Constant-time code: takes the same time whatever the secret input |
Demo 4: watch the click
A server checks an API token. The obvious comparison stops at the first mismatch. Each character checked is slowed to 2 ms here so we can see it (real differences are nanoseconds, but averaged over many measurements across a network they can still be detected).
import hmac, time
SECRET = "7Q2K9X" # the API token the server expects
def slow_equal(a, b): # the obvious way: stop at the first difference
for x, y in zip(a, b):
if x != y: return False
time.sleep(0.002) # (each byte checked takes a moment; exaggerated)
return len(a) == len(b)
SAFE = False # <- compare the obvious way, or in constant time
for guess in ["AAAAAA", "7AAAAA", "7QAAAA", "7Q2AAA", "7Q2KAA"]:
t = time.perf_counter()
hmac.compare_digest(guess, SECRET) if SAFE else slow_equal(guess, SECRET)
print(f"guess {guess}: {(time.perf_counter() - t) * 1000:5.1f} ms")
Output with SAFE = False (timings vary slightly each run):
guess AAAAAA: 0.0 ms
guess 7AAAAA: 2.1 ms
guess 7QAAAA: 4.2 ms
guess 7Q2AAA: 6.3 ms
guess 7Q2KAA: 8.3 ms
Every correct character adds about 2 ms. An attacker measures it and recovers the token one character at a time.
Output with SAFE = True (hmac.compare_digest):
guess AAAAAA: 0.0 ms
guess 7AAAAA: 0.0 ms
guess 7QAAAA: 0.0 ms
guess 7Q2AAA: 0.0 ms
guess 7Q2KAA: 0.0 ms
Flat: the time says nothing. Compare every secret in constant time: tokens, MACs, signatures, password hashes. Vetted libraries already do this internally, one more reason not to roll your own. (Python's == on strings also stops early, so never use it for secrets.)
Problem 5: crypto agility
Ravi's receipt system was built years ago and signs receipts with MD5. MD5 and SHA-1 are now broken (episode 13): algorithms age, and attacks only get better. But md5 is hard-coded in two hundred places, and old receipts were stored without saying which algorithm made them, so upgrading means a rewrite.
Crypto agility is designing so you can swap algorithms easily:
- One setting picks the algorithm for new records.
- Tag every stored value with its algorithm (and parameters), so old values can still be checked.
- Upgrade on use: when an old value is verified, re-create it with the current algorithm.
- Keep a crypto inventory: where you use which algorithm and key.
Demo 5: algorithm tags and upgrade on use
import hashlib, hmac
CURRENT = "sha3_256" # one setting: the algorithm used for NEW records
KEY = b"ravi-receipt-signing-key"
def tag(alg, msg): return f"{alg}${hmac.new(KEY, msg, alg).hexdigest()[:16]}" # record WHICH algorithm
def verify(stored, msg):
alg = stored.split("$")[0]
return hmac.compare_digest(stored, tag(alg, msg))
old = {"receipt-1001": tag("md5", b"receipt-1001"), "receipt-1002": tag("sha256", b"receipt-1002")}
for rid, stored in old.items():
ok, was = verify(stored, rid.encode()), stored.split("$")[0]
if ok and not stored.startswith(CURRENT + "$"):
stored = tag(CURRENT, rid.encode()) # upgrade on the next check
print(f"{rid}: {was:6} ok={ok} -> {stored}")
print("new receipts use:", tag(CURRENT, b"receipt-1003"))
Output:
receipt-1001: md5 ok=True -> sha3_256$64c350ef84602a34
receipt-1002: sha256 ok=True -> sha3_256$3002a1dc12cbba04
new receipts use: sha3_256$792b15096c1d798f
Both old receipts are verified with their own algorithm, then upgraded to SHA3-256; new receipts use SHA3 from the start. One setting changed, no rewrite. Password hashes (episode 14) work the same way: strings like $argon2id$v=19$m=65536,t=3,p=4$... name their algorithm and settings, so you can upgrade at the next login.
Problem 6: quantum computers and harvest now, decrypt later
A quantum computer computes using quantum physics. A large enough one could break the public-key algorithms we use for key exchange and signatures: RSA, elliptic-curve Diffie-Hellman and ECDSA (episodes 15, 16). Symmetric ciphers like AES and hashes like SHA-2/SHA-3 are much less affected (larger keys, like AES-256, keep them safe).
Nobody knows when such a machine will exist. But an attacker can record encrypted traffic today and store it until they can decrypt it: harvest now, decrypt later. As Entrust's Greg Wetmore put it (Computer Weekly): "bad actors will steal encrypted information today in order to decrypt it when quantum computers are available".
Demo 6: which data is at risk?
A rule of thumb: if years the data must stay secret + years your migration takes > years until such a computer exists, data recorded today is at risk.
# Is data you encrypt TODAY safe from a future quantum computer? A rule of thumb:
# if (years the data must stay secret) + (years your migration takes) > (years until such a computer exists),
# then traffic recorded today could be read while it still matters: "harvest now, decrypt later".
MIGRATION = 5 # years to move every system to post-quantum crypto
for quantum_in in (8, 15, 40): # nobody knows this number: try several guesses
for data, secret_for in [("today's OTP", 0), ("a sales report", 7), ("medical records", 30)]:
at_risk = secret_for + MIGRATION > quantum_in
print(f"quantum in {quantum_in:2} yrs | {data:15} | secret {secret_for:2} yrs | "
f"{'AT RISK' if at_risk else 'ok'}")
Output:
quantum in 8 yrs | today's OTP | secret 0 yrs | ok
quantum in 8 yrs | a sales report | secret 7 yrs | AT RISK
quantum in 8 yrs | medical records | secret 30 yrs | AT RISK
quantum in 15 yrs | today's OTP | secret 0 yrs | ok
quantum in 15 yrs | a sales report | secret 7 yrs | ok
quantum in 15 yrs | medical records | secret 30 yrs | AT RISK
quantum in 40 yrs | today's OTP | secret 0 yrs | ok
quantum in 40 yrs | a sales report | secret 7 yrs | ok
quantum in 40 yrs | medical records | secret 30 yrs | ok
A one-time password matters for seconds: always fine. Medical records must stay secret for 30 years: at risk unless the machine is more than 35 years away. Long-lived secrets must move first, before anyone knows the date. That's why governments set deadlines now: the UK's National Cyber Security Centre (NCSC) asks organisations to identify what needs upgrading and plan, ideally by 2028, carry out high-priority upgrades 2028-2031, and complete migration of all systems by 2035 (Computer Weekly).
Post-quantum cryptography
Post-quantum cryptography (PQC) means new public-key algorithms designed to resist quantum computers (as far as we know), running on ordinary computers. After a NIST call for proposals issued in December 2016, NIST released the first standards on 13 August 2024 (Cloud Security Alliance, 15 Aug 2024):
| Standard | Algorithm | Job | Replaces (for this job) |
|---|---|---|---|
| FIPS 203 | ML-KEM (Module-Lattice-Based Key-Encapsulation Mechanism) | Agree on a shared secret (a KEM, episode 15) | ECDH / RSA key exchange |
| FIPS 204 | ML-DSA (Module-Lattice-Based Digital Signature Algorithm) | Digital signatures | RSA, ECDSA, Ed25519 signatures |
| FIPS 205 | SLH-DSA (Stateless Hash-Based Digital Signature Algorithm, from SPHINCS+) | Digital signatures whose security rests on hash functions, not lattices | — |
"ML" stands for module lattice: these are built on hard maths problems about lattices (regular grids of points in many dimensions), instead of factoring or elliptic curves.
Hybrid key exchange runs a classic and a post-quantum exchange together and combines both secrets, so the connection stays safe as long as either algorithm holds: a careful way to deploy new algorithms while trust in them grows.
Demo 7: a real ML-KEM-768 key exchange
from cryptography.hazmat.primitives.asymmetric import mlkem, x25519
from cryptography.hazmat.primitives import serialization as s
# Post-quantum key exchange: ML-KEM-768 (FIPS 203). The server publishes a public key...
server = mlkem.MLKEM768PrivateKey.generate()
pub = server.public_key()
# ...the laptop "encapsulates": makes a fresh shared secret and a ciphertext only the server can open
secret_laptop, ciphertext = pub.encapsulate()
secret_server = server.decapsulate(ciphertext)
print("ML-KEM-768 shared secrets match:", secret_laptop == secret_server)
print("public key:", len(pub.public_bytes_raw()), "bytes, ciphertext:", len(ciphertext), "bytes")
x = x25519.X25519PrivateKey.generate().public_key() # today's classic key exchange (ep 15)
print("X25519 public key:", len(x.public_bytes(s.Encoding.Raw, s.PublicFormat.Raw)), "bytes")
Output:
ML-KEM-768 shared secrets match: True
public key: 1184 bytes, ciphertext: 1088 bytes
X25519 public key: 32 bytes
- The server makes a key pair; the laptop encapsulates (creates a fresh shared secret plus a ciphertext only the server can open); the server decapsulates. Both now hold the same 32-byte secret, to use with AES-GCM.
- Size: an ML-KEM-768 public key is 1,184 bytes and its ciphertext 1,088, versus 32 bytes for an X25519 public key. Bigger handshakes strain some old devices and networks, which is exactly why crypto agility matters.
The next problem
That completes our foundations: networks, cryptography, and using crypto correctly. Next we move to identity: when Priya logs in to the stock app, how does it know it's still her on the next click? Next episode: authentication fundamentals and session management.
The whole chain
- Home-made XOR breaks → vetted libraries, AES-GCM.
- Attackers go for keys → key lifecycle, KMS, HSM, key policy.
- Rotating terabytes → envelope encryption: DEK + KEK, wrapping, crypto-shredding.
- Data in memory → at rest, in transit, in use; confidential computing.
- Time leaks secrets → constant-time comparison.
- MD5 hard-coded → crypto agility, algorithm tags, upgrade on use.
- Record now, read later → post-quantum: ML-KEM, ML-DSA, hybrid.
...and you just invented modern key management.
Common mistakes (and what they print)
| Mistake | What you see | Why it's wrong | Fix |
|---|---|---|---|
| Home-made XOR "encryption" | key bytes leaked: b'chai42cha' |
Known plaintext reveals the key | AES-GCM from a vetted library |
| Key in source code | Anyone with the repo (or a leaked copy) can decrypt | Code is copied everywhere | KMS / HSM / secret manager, key policies |
Comparing secrets with == |
guess 7Q2KAA: 8.4 ms |
Time grows with each right character | hmac.compare_digest |
| Hard-coded, untagged algorithm | MD5 everywhere, can't upgrade | No record of which algorithm made a value | Algorithm tags + one setting + upgrade on use |
| One key for all data, never rotated | A single leak exposes everything, forever | No limit on blast radius | Envelope encryption, per-file DEKs, KEK rotation |
random for keys or OTPs |
Predictable values (episode 10) | Not a CSPRNG | secrets, os.urandom, library key generators |
| Waiting for quantum computers to arrive | Recorded traffic already harvested | Harvest now, decrypt later | Inventory now; long-lived data first; hybrid PQC |
Mini project: Ravi's crypto hygiene scanner
A tiny scanner that reads (made-up) source lines and flags the mistakes from this episode: hard-coded secrets, MD5/SHA-1, non-crypto randomness for secrets, ECB mode, == on secrets, and home-made ciphers.
# Ravi's crypto hygiene scanner: flags risky patterns in (made-up) source lines
import re
CHECKS = [
(r"(key|secret|token)\s*=\s*b?['\"][^'\"]{8,}['\"]", "hard-coded secret: move it to a KMS / secret manager"),
(r"\b(md5|sha1)\b", "deprecated hash: use SHA-256/SHA-3 (or a password KDF)"),
(r"\brandom\.(random|randint|choice)\b.*(key|token|otp)", "non-crypto random for a secret: use secrets"),
(r"modes\.ECB", "ECB mode leaks patterns: use AES-GCM"),
(r"(token|mac|signature|digest)\w*\s*==", "secret compared with ==: use hmac.compare_digest"),
(r"def my_(encrypt|cipher|crypt)", "home-made cipher: use a vetted library"),
]
SOURCE = {
"billing.py": ['API_KEY = "sk_demo_9f8e7d6c5b4a"', 'digest = hashlib.md5(data).hexdigest()'],
"auth.py": ['otp = random.randint(100000, 999999) # otp for login', 'if token == request_token:'],
"files.py": ['cipher = Cipher(algorithms.AES(k), modes.ECB())', 'def my_encrypt(data):'],
"reports.py": ['key = kms.unwrap("kek-v2", wrapped)', 'ok = hmac.compare_digest(tag, expected)'],
}
total = 0
for file, lines in SOURCE.items():
for n, line in enumerate(lines, 1):
for pattern, advice in CHECKS:
if re.search(pattern, line, re.I):
print(f"{file}:{n}: {advice}")
total += 1
print(f"{total} findings")
Output:
billing.py:1: hard-coded secret: move it to a KMS / secret manager
billing.py:2: deprecated hash: use SHA-256/SHA-3 (or a password KDF)
auth.py:1: non-crypto random for a secret: use secrets
auth.py:2: secret compared with ==: use hmac.compare_digest
files.py:1: ECB mode leaks patterns: use AES-GCM
files.py:2: home-made cipher: use a vetted library
6 findings
reports.py is clean: it unwraps a key through the KMS and compares in constant time. Real tools do this at scale (secret scanners in code hosting, static analysis with crypto rules). Ideas to extend it: scan real .py files in a folder; add a check for AES with a hard-coded IV; print a fix-it link per finding.
Practice
1. Crypto-shredding. Encrypt three files with envelope encryption, then destroy only one file's DEK. Show that the other two still decrypt.
Solution
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
kek = AESGCM.generate_key(bit_length=256)
def wrap(dek): n = os.urandom(12); return n + AESGCM(kek).encrypt(n, dek, None)
def unwrap(b): return AESGCM(kek).decrypt(b[:12], b[12:], None)
files = {}
for name in ["sales-2024.csv", "sales-2025.csv", "old-customers.csv"]:
dek, nonce = AESGCM.generate_key(bit_length=256), os.urandom(12)
files[name] = {"nonce": nonce, "data": AESGCM(dek).encrypt(nonce, name.encode() * 1000, None), "wdek": wrap(dek)}
files["old-customers.csv"]["wdek"] = None # crypto-shred: destroy only this file's key
for name, f in files.items():
if f["wdek"] is None:
print(f"{name:18} key destroyed: unreadable forever ({len(f['data']):,} bytes of noise)")
else:
plain = AESGCM(unwrap(f["wdek"])).decrypt(f["nonce"], f["data"], None)
print(f"{name:18} still readable: {len(plain):,} bytes")
Output:
sales-2024.csv still readable: 14,000 bytes
sales-2025.csv still readable: 14,000 bytes
old-customers.csv key destroyed: unreadable forever (17,016 bytes of noise)
Useful for "delete my data" requests where the data sits inside backups you can't edit: destroy the key, and every copy becomes noise.
2. Switch the agility demo to BLAKE2. Set CURRENT = "blake2b" and show what a new receipt looks like.
Solution
import hashlib, hmac
KEY = b"ravi-receipt-signing-key"
def tag(alg, msg): return f"{alg}${hmac.new(KEY, msg, alg).hexdigest()[:16]}"
for CURRENT in ("sha3_256", "blake2b"):
print(f"CURRENT = {CURRENT:9} -> receipt-1003 stored as {tag(CURRENT, b'receipt-1003')}")
print("algorithms this Python can use with hmac:", "blake2b" in hashlib.algorithms_available, "sha3_256" in hashlib.algorithms_available)
Output:
CURRENT = sha3_256 -> receipt-1003 stored as sha3_256$792b15096c1d798f
CURRENT = blake2b -> receipt-1003 stored as blake2b$ac0f0ad9c8dc9ce5
algorithms this Python can use with hmac: True True
Because each value carries its algorithm name, old SHA3 receipts and new BLAKE2 receipts can live side by side.
3. ML-KEM-1024. Compare the sizes of ML-KEM-768 and ML-KEM-1024.
Solution
from cryptography.hazmat.primitives.asymmetric import mlkem
for name, cls in [("ML-KEM-768", mlkem.MLKEM768PrivateKey), ("ML-KEM-1024", mlkem.MLKEM1024PrivateKey)]:
priv = cls.generate()
secret, ct = priv.public_key().encapsulate()
assert priv.decapsulate(ct) == secret
print(f"{name:11}: public key {len(priv.public_key().public_bytes_raw())} bytes, "
f"ciphertext {len(ct)} bytes, shared secret {len(secret)} bytes")
Output:
ML-KEM-768 : public key 1184 bytes, ciphertext 1088 bytes, shared secret 32 bytes
ML-KEM-1024: public key 1568 bytes, ciphertext 1568 bytes, shared secret 32 bytes
Both give a 32-byte shared secret; the larger parameter set gives a larger security margin at the cost of bigger keys and ciphertexts.
4. A post-quantum signature. Sign a receipt with ML-DSA-65 (FIPS 204), verify it, show that a changed receipt fails, and compare the signature size with Ed25519 (episode 16).
Solution
from cryptography.hazmat.primitives.asymmetric import mldsa, ed25519
from cryptography.exceptions import InvalidSignature
receipt = b"receipt-1003 | Neha | Rs 18,450"
key = mldsa.MLDSA65PrivateKey.generate() # ML-DSA-65 (FIPS 204)
sig = key.sign(receipt)
key.public_key().verify(sig, receipt)
print("ML-DSA-65 signature verified; size:", len(sig), "bytes")
try:
key.public_key().verify(sig, receipt.replace(b"18,450", b"1,845"))
except InvalidSignature:
print("changed receipt: InvalidSignature")
ed = ed25519.Ed25519PrivateKey.generate()
print("Ed25519 signature size (episode 16):", len(ed.sign(receipt)), "bytes")
Output:
ML-DSA-65 signature verified; size: 3309 bytes
changed receipt: InvalidSignature
Ed25519 signature size (episode 16): 64 bytes
Post-quantum signatures are much bigger (3,309 bytes vs 64), another reason migrations need planning.
5. Concept check. Which tool fits? (a) Re-encrypting 50 TB yearly is too slow; (b) a payment key must never exist outside tamper-proof hardware; (c) a cloud provider's admins must not see data while it's processed; (d) the shared secret in a TLS handshake must survive future quantum computers.
Solution
(a) Envelope encryption: rotate the KEK, re-wrap DEKs. (b) An HSM. (c) Confidential computing (an enclave / TEE). (d) ML-KEM, usually in a hybrid key exchange with a classic algorithm.
Quick quiz
Envelope encryption: 10,000 files, each with its own DEK, wrapped by one KEK. You rotate the KEK. What gets re-encrypted?
- A: all 10,000 files
- B: the 10,000 small wrapped DEKs
- C: nothing at all
- D: only the newest file
Answer
B. The KEK only protects the DEKs. Rotating it means unwrapping each DEK with the old KEK and wrapping it with the new one: 10,000 tiny operations, while the file data stays untouched.
Interview questions
- Why "don't roll your own crypto"? Designing and correctly assembling cryptography is extremely hard; home-made schemes fail to known-plaintext and other attacks, and even correct primitives are easily misused (modes, IVs, padding, MAC order). Use vetted libraries with safe high-level APIs.
- Describe the key lifecycle. Generation (CSPRNG, correct length), secure storage (KMS/HSM, never in code), controlled use (policies, logging), rotation (scheduled replacement), revocation (immediate distrust on compromise), destruction (delete all copies; crypto-shredding).
- KMS vs HSM? A KMS is a service that manages keys and performs crypto operations for apps under access policies with audit logs; an HSM is tamper-resistant hardware that generates and uses keys without exporting them. A KMS often keeps its root keys in HSMs.
- Explain envelope encryption. Data is encrypted locally with a per-object DEK; the DEK is wrapped by a KEK held in a KMS; wrapped DEKs are stored with the data. Rotating the KEK only re-wraps DEKs; destroying a DEK crypto-shreds one object.
- Data at rest, in transit, in use: how is each protected? At rest: storage/database/file encryption. In transit: TLS, VPNs, WPA3. In use: confidential computing with hardware enclaves (TEEs), plus access controls.
- What is a timing attack and how do you prevent it? Measuring how long an operation takes to learn about secret data, e.g. early-exit comparisons leaking how many characters matched. Use constant-time comparisons (
hmac.compare_digest,crypto/subtlein Go,MessageDigest.isEqualin Java) and vetted libraries. - What is crypto agility? The ability to replace algorithms and keys with minimal change: central configuration, algorithm identifiers stored with data, upgrade-on-use, and a crypto inventory.
- What is harvest now, decrypt later? Adversaries record encrypted traffic today and store it, to decrypt it once quantum computers can break the public-key exchange that protected it. Long-lived secrets should move to post-quantum key exchange first.
- What are ML-KEM and ML-DSA? NIST's first post-quantum standards (FIPS 203 and 204, 13 Aug 2024): a module-lattice key-encapsulation mechanism and a module-lattice digital signature algorithm. They're larger than classic algorithms (e.g. 1,184-byte ML-KEM-768 public keys).
- Why deploy post-quantum crypto in hybrid mode? Combining a classic and a post-quantum key exchange keeps the connection secure if either holds, protecting against both future quantum attacks and possible undiscovered weaknesses in the newer algorithms.
Glossary
| Term | Meaning in plain words | Example |
|---|---|---|
| Roll your own crypto | Inventing or hand-assembling cryptography instead of using vetted libraries | my_encrypt in Demo 1 |
| XOR | Exclusive or: flips bits where the key has 1s; doing it twice undoes it | b ^ SECRET[i % 6] |
| Known plaintext | Text an attacker knows or can guess inside a message | INVOICE # |
| Security through obscurity | Relying on hiding the method rather than the key (episode 3) | "Nobody knows my method" |
| Kerckhoffs's principle | A system should stay secure even if everything but the key is public | Why AES is published |
| Vetted library | Widely reviewed crypto code with safe interfaces | libsodium, Tink, cryptography |
| AES-GCM / AEAD | AES in Galois/Counter Mode: encryption plus tamper detection (episode 12) | AESGCM |
| InvalidTag | The error AES-GCM raises when data was changed | Demo 2 |
| Key lifecycle | The stages of a key's life: generate, store, use, rotate, revoke, destroy | The table in Problem 2 |
| CSPRNG | Cryptographically secure random number generator (episode 10) | secrets, os.urandom |
| Key rotation | Replacing a key with a new one on a schedule | kek-v1 → kek-v2 |
| Key revocation | Stopping trust in a key immediately | After a leak |
| Key destruction | Deleting every copy of a key | del kms.keks["kek-v1"] |
| Crypto-shredding | Destroying a key on purpose so its data becomes unreadable | Practice 1 |
| KMS | Key management system: a service that stores keys and does crypto for apps, with logs | ToyKMS |
| HSM | Hardware security module: tamper-resistant hardware that keeps keys inside | The bank vault |
| Key policy | Rules for which app or person may use which key for what | Web server can't decrypt payroll |
| Secret manager | A service that stores passwords, API keys and tokens for apps | Instead of API_KEY = "..." in code |
| Envelope encryption | Encrypting data with a DEK and the DEK with a KEK | Demo 3 |
| DEK | Data encryption key: encrypts the actual data, one per file or record | dek = new_key() |
| KEK | Key encryption key: the master key that wraps DEKs, kept in a KMS | kek-v2 |
| Wrapping / unwrapping | Encrypting / decrypting a key with another key | kms.wrap(...) |
| Data at rest | Stored data | A database file |
| Data in transit | Data moving across a network | An HTTPS request |
| Data in use | Data being processed in memory | A report being calculated |
| Confidential computing | Processing data inside hardware-protected, encrypted memory | Enclaves in cloud servers |
| Enclave / TEE | A hardware-protected area whose memory even the OS can't read | Trusted execution environment |
| Side channel | A leak through time, power, sound or cache rather than the output | The clicking safe |
| Timing attack | Learning secrets from how long operations take | Demo 4 |
| Constant-time comparison | Comparing secrets in a way that always takes the same time | hmac.compare_digest |
| API token | A secret string a program sends to prove it may use an API | 7Q2K9X |
| Crypto agility | Being able to swap algorithms with minimal change | Demo 5 |
| Algorithm tag | Storing which algorithm made a value, alongside it | sha3_256$... |
| Upgrade on use | Re-creating an old value with the current algorithm when it's next checked | Receipts 1001, 1002 |
| Crypto inventory | A list of where you use which algorithms and keys | Needed for PQC migration |
| MD5 / SHA-1 | Old hash functions, now broken for collision resistance (episode 13) | md5$... receipts |
| SHA-3 / BLAKE2 | Modern hash function families | sha3_256, blake2b |
| Quantum computer | A computer using quantum physics; a large one could break RSA and ECC | Not yet built at that scale |
| RSA / ECDH / ECDSA | Classic public-key algorithms for encryption, key exchange, signatures (episodes 15, 16) | Vulnerable to large quantum computers |
| Harvest now, decrypt later | Recording encrypted data today to decrypt it in the future | Long-lived medical records |
| NCSC | The UK's National Cyber Security Centre | 2028 / 2031 / 2035 milestones |
| Post-quantum cryptography (PQC) | Public-key algorithms designed to resist quantum computers | ML-KEM, ML-DSA |
| NIST | US National Institute of Standards and Technology | Published FIPS 203-205 |
| FIPS | US federal standards for information processing | FIPS 203 |
| KEM | Key encapsulation mechanism: creates a shared secret plus a ciphertext for the receiver (episode 15) | encapsulate() |
| Encapsulate / decapsulate | Make a shared secret + ciphertext with a public key / recover the secret with the private key | Demo 7 |
| ML-KEM | Module-Lattice-Based Key-Encapsulation Mechanism (FIPS 203) | ML-KEM-768 |
| ML-DSA | Module-Lattice-Based Digital Signature Algorithm (FIPS 204) | ML-DSA-65 |
| Lattice | A regular grid of points in many dimensions; PQC's hard problems live here | The "ML" in ML-KEM |
| Hybrid key exchange | A classic and a post-quantum exchange combined; safe if either holds | X25519 + ML-KEM |
| X25519 | A fast classic elliptic-curve key exchange (episode 15) | 32-byte public key |
| ECB mode | A block-cipher mode that leaks patterns (episode 11) | Flagged by the mini project |
Summary
- Don't roll your own crypto: a home-made XOR fell to one known word; AES-GCM from a vetted library leaked nothing and caught tampering.
- Attackers go for keys. Manage the whole lifecycle; keep keys in a KMS, with the most important inside HSMs, behind key policies.
- Envelope encryption: DEKs encrypt data, a KEK in the KMS wraps DEKs. Rotation re-wraps 60-byte keys; destroying a DEK crypto-shreds one file.
- Protect data at rest, in transit and in use (confidential computing).
- Compare secrets in constant time (
hmac.compare_digest): early-exit comparisons leak through timing. - Build crypto agility: one setting, algorithm tags, upgrade on use, a crypto inventory.
- Harvest now, decrypt later makes long-lived secrets urgent. NIST's ML-KEM (FIPS 203) and ML-DSA (FIPS 204), released 13 Aug 2024, are bigger but deployable, often in hybrid mode.
Next episode: authentication fundamentals and session management: identification vs authentication vs authorization, factors, sessions and cookies, fixation, timeouts and logout.
Sources: Cloud Security Alliance (15 Aug 2024), "NIST FIPS 203, 204, and 205 Finalized"; Encryption Consulting, "NIST PQC standards: FIPS 203, 204, 205"; Computer Weekly, "NCSC proposes three-step plan to move to quantum-safe encryption". Code outputs from Python 3.13 and cryptography 50.0.1. Fetched 3 Nov 2026.