๐ง Building Your Tower
Stacking Cipher Layersโ
In 10 minutes: Setting up multiple encryption layers
Prerequisite: Understanding cipher layers
๐ฏ The Simple Storyโ
Imagine building a tower of protection:
Tower (Layered Protection Layer by Layer):
๐๏ธ Layer 3 (Top Layer): MLS (Meeting Room)
โโ Protects: Group chats
โโ Threat: Eve joining group
๐๏ธ Layer 2: Signal (Two-Person Phone Booth)
โโ Protects: Private messages
โโ Threat: Past message compromise
๐๏ธ Layer 1: AES (Safe Deposit Box)
โโ Protects: Simple files
โโ Threat: Single lock picked
๐๏ธ Layer 0: Plaintext (Your Treasure)
โโ Base layer: Your data
Key principle: Order matters!
- Encryption: Bottom layer first (AES โ Signal โ MLS)
- Decryption: Top layer first (MLS โ Signal โ AES)
๐ง Mental Modelโ
Hold this picture in your head:
Layer Ordering (Building the Tower):
Bottom layer (First encrypt):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AES Cipher Layer โ โ Start here!
โ Fast, simple, password-based โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Build on top...
Middle layers:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Signal Cipher Layer โ
โ Two-person encryption โ
โ Forward secrecy (keys delete!)โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Build on top...
Top layer (Last encrypt):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MLS Cipher Layer โ
โ Group encryption โ
โ Meeting room metaphor โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
Top: Final ciphertext!
Plaintext โ AES โ Signal โ MLS โ Final
Think of it like building:
- AES = Foundation (base)
- Signal = Building on top (walls)
- MLS = Roof on top (ceiling)
You can't have the roof without the walls! Your castle goes: Foundation โ Walls โ Roof!
๐ Order Mattersโ
Why Order Matters?โ
Good Ordering (Recommended):โ
Bottom (first encrypt): AES
โโ Why? Fast, simple, password-based
Middle (next encrypt): Signal, DH
โโ Why? Handshakes and forward secrecy
โโ Note: These don't require group context
Top (last encrypt): MLS, ML-KEM
โโ Why? These need specific context!
- MLS: Group ID, member list
- ML-KEM: Public/private key pair
Rule of thumb:
- Fast/lower layers first (AES, DH)
- Complex/context-dependent layers last (Signal, MLS, ML-KEM)
๐ญ The Story: Alice's Encryption Castleโ
Alice is building her security castle to protect her secrets (messages).
Wrong Way: Random Orderingโ
Alice's Castle (WRONG ORDER):
Layer 1: MLS (Top) โ Starts here!
โโ Problem: Needs group context!
โโ Alice hasn't built the group yet!
Layer 2: Signal (Middle)
โโ Problem: Needs double ratchet state!
โโ Alice hasn't established it yet!
Layer 3: AES (Bottom)
โโ Added last but used first
Result: Can't build on top! Foundation missing!
Alice: "I can't build the roof without walls!"
Right Way: Foundation Firstโ
Alice's Castle (RIGHT ORDER):
Layer 1: AES (Foundation) โ Start here!
โโ Simple password encryption
โโ Easy to add (just needs password)
Layer 2: Signal (Walls)
โโ Build on AES foundation
โโ Add double ratchet next
Layer 3: MLS (Roof)
โโ Build on Signal walls + AES foundation
โโ Add group context on top
Alice: "Perfect! Foundation โ Walls โ Roof!"
๐๏ธ Adding Layers with CascadingCipherManagerโ
import { CascadingCipherManager, AESCipherLayer } from "cryptography/CascadingCipher";
// Step 1: Create manager
const manager = new CascadingCipherManager();
// Step 2: Add layers IN ORDER:
// Foundation (layer 0)
manager.addLayer(new AESCipherLayer());
// Why first? Simple, fast, no dependencies!
// Walls (layers 1-2)
manager.addLayer(new DHCipherLayer());
manager.addLayer(new SignalCipherLayer(signalWasm));
// Roof (layer 3)
manager.addLayer(new MLSCipherLayer(mlsManager, groupId));
// Why last? Needs group context!
โ ๏ธ Common Ordering Mistakesโ
Mistake 1: Fast Layers Lastโ
WRONG:
manager.addLayer(MLSCipherLayer); โ Top - needs context!
manager.addLayer(AESCipherLayer); โ Bottom - was simple but last
Problem:
- MLS tries to encrypt but doesn't have group context
- Manager can't find Group ID
- ERROR!
FIX:
manager.addLayer(AESCipherLayer()); // Add FIRST!
manager.addLayer(MLSCipherLayer()); // Add LAST!
Mistake 2: Layers Need Dependenciesโ
WRONG:
manager.addLayer(MLSCipherLayer); โ Needs MLSManager
manager.addLayer(SignalCipherLayer()); โ Needs Signal state
// Neither layer has their dependencies!
FIX:
mlsManager = await initialize();
const signalWasm = await loadSignalWasm();
manager.addLayer(AESCipherLayer());
manager.addLayer(SignalCipherLayer(signalWasm));
manager.addLayer(MLSCipherLayer(mlsManager));
๐ฎ Try It Yourselfโ
Question 1: Which order is better and why?
Show Answer This
Option A: MLS โ Signal โ AES Option B: AES โ DH โ Signal
Answer: Option B (Option A) WRONG!
Why? MLS and Signal both depend on context but in different ways:
- MLS needs: Group ID, member list initialized
- Signal needs: Double ratchet state established
If MLS or Signal layers are added before AES or DH, they try to encrypt but can't find what they need! AES and DH don't need dependencies - they're simpler!
Correct order: AES โ DH โ Signal โ MLS
Question 2: Can you reorder layers? What happens?
Answer: Yes! But you can't decrypt old ciphertext after reordering because the order changed! Old ciphertext expects Layer 1 โ Layer 2. New order is Layer 2 โ Layer 1. If you try to decrypt old ciphertext with new order, Layer 1โปยน expects data it never got! You need to re-encrypt all your data if you change order!
Question 3: What's the general rule for layer ordering?
Answer: Order from simple/dependency-free to complex/context-dependent:
Simple layers first (no dependencies:
- AES (just password)
- DH (just keys, optional shared secret)
Middle layers next (light dependencies:
- Signal (needs ratchet state)
Complex layers last (many dependencies:
- MLS (needs group context)
- ML-KEM (needs key pairs)
๐ก Real-World Example: Secure Chat Appโ
Layer ordering in a secure messaging app:
// Initial key exchange (first)
manager.addLayer(new DHCipherLayer());
// Add forward secrecy for 2-person chats (second)
manager.addLayer(new SignalCipherLayer(wasmModule));
// Add quantum resistance (third)
manager.addLayer(new MLKEMCipherLayer());
// Add fast simple layer for compatibility (optional, fourth)
manager.addLayer(new AESCipherLayer());
// Result: DH โ Signal โ ML-KEM โ AES
Layer purposes:
- DH: Establish initial shared secret
- Signal: Forward secrecy for 2-person messaging
- ML-KEM: Quantum-resistant protection
- AES: Compatibility, backup layer
โ Quick Checkโ
**Can you explain layer ordering to a 5-year-old?
Try saying this out loud:
"Imagine building a house! You don't put the roof on first! You start with the foundation, then build the walls, then put the roof on last. It's the same with encryption layers. Build the simple layers first (like AES), then the harder ones (like Signal and MLS) on top!"
๐ Key Takeawaysโ
โ
Order matters - Foundation first, then walls, then roof
โ
Simple layers first - AES, DH (no dependencies)
โ
Complex layers last - Signal, MLS, ML-KEM (need context)
โ
Dependencies check - Ensure layers have what they need
โ
Don't change order - Old ciphertexts can't decrypt with new order!
โ
General rule: Simple โ Complex layers
Now you know how to order layers. Next: Using the layer manager!