Skip to main content

๐Ÿ”ง 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?โ€‹

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!