COMS W4181 · Security I · Fall 2026 · Lecture 2

Cryptography

Columbia University

$ ./crypto --hash --encrypt --sign integrity | confidentiality | authenticity
[+] Where we are going
  • 01Cryptographic hash functions - one-wayness, collisions, the birthday bound
  • 02MACs and HMAC - integrity and authentication
  • 03Symmetric encryption - one-time pad, block ciphers, modes of operation
  • 04What "secure" means - attack models and the chosen-plaintext game
  • 05Public-key cryptography - RSA, signatures, Diffie-Hellman

Every primitive here gets the same treatment: what it guarantees, what it does not, and the real system that got that wrong.

[ 01 ]

Cryptographic
hash functions

A fingerprint for data. Any file in, a short fixed-size number out, and nobody can fake it.

[+] What we are going to do in this part
  1. Why anyone wants a fingerprint of a file. Three everyday uses, so the rest has a point.
  2. What a hash function is, on one real example, and the fact that makes it all interesting: the inputs are unlimited, the outputs are not.
  3. The three things we need from it, one-way, second-preimage resistant, collision resistant, each with the attack it stops.
  4. Why finding a collision is much cheaper than it looks. The birthday paradox, computed step by step for this room.
  5. Where hashes are used, in detail, and which property each use needs.
  6. How real hash functions are built, how MD5 and SHA-1 broke, and where SHA-3 came from.
[+] Why would anyone want a fingerprint of a file? [1] CHECK A DOWNLOAD6 GB installerH64 chars64 charspublished by the site=? ✓comparing 6 GB is hopeless;comparing 64 characters is instant [2] STORE A PASSWORD WITHOUT STORING ITpasswordHdigestdatabasedigests onlysteals itthe site recognises you;a thief gets digests, not passwords [3] SEAL A BID, OPEN IT LATERmy bidHsealednow: hand in the seallater: reveal the bid, all checkopened ✓nobody can read it,and you cannot change it afterwards

Each of these quietly assumes the fingerprint cannot be faked. This part is about what “cannot be faked” means, exactly.

[+] What a hash function is, on one real example

Any size in, a fixed-size number out. That number is the digest, the fingerprint. We write H(x) for the digest of x.

$ echo -n "hello" | sha256sum # 5 bytes in 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 # 64 hex digits = 256 bits $ echo -n "hellp" | sha256sum # one letter changed fdd7585e08c4e2afd71dcabdb4636c89d557a3f42db9e2040c8bbd1708aa4ce7 # nothing in common $ sha256sum ubuntu-24.04.iso # 6 GB in: still 64 hex digits out
  • Fixed size, and deterministic. Both give 64 hex digits; a hex digit is 4 bits, so 64 × 4 = 256 bits. And “hello” gives the same digest on any machine, any day.
  • A tiny change in the input changes everything in the output. hello and hellp share nothing. The digest looks random.
  • Anyone can compute it. There is no key and no secret. The function is public; the security has to come from somewhere else.
[+] The inputs have no limit. The outputs do. WHAT CAN GO IN: NO LARGEST INPUT“hello”5 bytesan empty file0 bytesthis slide deck2 MBthe Ubuntu installer6 GBevery file that could ever exist…the number of inputs isinfinite WHAT CAN COME OUT: ALWAYS EXACTLY 256 BITS…1 bit: 2 digests2 bits: 00 01 10 11 → 43 bits: 8 … 256 bits: 2 × 2 × … × 2the number of outputs is2^256: huge, but finite Hsqueeze

Infinitely many inputs are squeezed into a finite number of outputs. Something has to give.

[+] A toy hash with a 3-bit digest, to see it happen 9 FILES ONLY 8 POSSIBLE DIGESTS a.txt b.txt c.txt d.txt … h.txt i.txt, the 9th 000 001 010 011 … 110 111 the first 8 files can all get different digests the 9th file has nowhere new to go: it must share a digest with someone
  • Nine files, eight digests: two files must share. No matter how clever the hash is. Nine pigeons, eight holes, one hole has two pigeons.
  • Two different inputs with the same digest are called a collision. With a 3-bit digest you get one after at most 9 files. Hopeless as a fingerprint.
[+] Now scale it up: 256 bits INPUTS: INFINITELY MANY DIGESTS: 2^256, ABOUT 10^77 "hello" "hellp" the Ubuntu installer this slide deck … every possible file, forever 2cf24dba … 938b9824 fdd7585e … 08aa4ce7 9b71d224 … a6c8a0f1 … ffffffff … ffff, the last one H somewhere in the infinite pile, two files share a digest: a collision exists
  • Same argument as the toy: infinitely many pigeons, 2^256 holes. Collisions exist. Cryptography never claims otherwise.
  • But 2^256 is about 10^77, and the visible universe has about 10^80 atoms. So the real question is not whether a collision exists. It is whether anyone can find one.
[?] Three ways a fingerprint could be faked

Go back to the three uses. In each, what would an attacker have to do to cheat?

[1] WORK BACKWARDSdigest y? → passwordhas y, wants an input for itpassword storage is worthless [2] SWAP A GIVEN FILEreal installery (published)malwarethe same ythe target y is fixed by the real filethe download check passes on malware [3] PREPARE A PAIR IN ADVANCEbid Abid Bone digestattacker writes both, picks the digestthe sealed bid means nothing

Three attacks, three properties. A property is simply the promise that one of these attacks does not work.

[+] The three properties we need, top-down ATTACKER IS GIVENATTACKER MUST FINDBEST ATTACK, IF H IS GOOD 1 · one-waya digest yany x with H(x) = yabout 2^n tries2 · second-preimage resistantan input xx′ ≠ x with H(x′) = H(x)about 2^n tries3 · collision resistantnothingany x ≠ x′ with H(x) = H(x′)only 2^(n/2) tries↑ the surprise

n is the digest length in bits: 256 for SHA-256, 160 for SHA-1, 128 for MD5. A “try” is one hash. Next: one slide per property, each with a real attack it stops. Then the birthday paradox explains the 2^(n/2).

[+] Property 1: one-way (you cannot go back)
One-way (preimage resistance): given a digest y, it is hard to find any x with H(x) = y. “Hard” means nothing beats trying inputs one by one.
FORWARD IS EASY: ONE HASH, INSTANT“hunter2”Hy = 2cf2…9824 BACKWARD IS THE ATTACK: FROM y TO ANY INPUT THAT HASHES TO ITy = 2cf2…9824H⁻¹ ?any x with H(x) = yno such doorthe digest tells you nothing THE ONLY MOVEpick xhash it= y ?no → try another. one try = one hash

? Note the word any: the thief does not need your real password, any input that hashes to y logs them in. So the whole question is: how long does guessing take?

[+] How long does guessing take? Step by step 1. HOW MANY TRIES? THE CANDIDATES LAND ON THE 2^256 DIGESTS LIKE RANDOM DARTSabout half, on average▲ y is hit here2^256 digests≈ 2^255 tries 2. HOW FAST CAN THE WHOLE WORLD HASH?2^70 hashes / s×2^25 s / year=2^95 tries / yearevery Bitcoin miner on Earth, togetheradd exponents when multiplying 3. YEARS NEEDED2^255 tries÷2^95 tries / year=2^160 ≈ 10^48 years 4. ON A LOG SCALE, YEARS10^010^1010^2010^3010^4010^50age of the universe: 10^10guessing: 10^48

Reading powers of two: 2^10 ≈ 1,000, so 2^70 ≈ 10^21 and 2^160 ≈ 10^48. Guessing is not a plan. The only shortcut would be a flaw in H itself, and for SHA-256 nobody has found one.

[+] Property 2: second-preimage resistance
Second-preimage resistance, also called weak collision resistance Given an input x, it is hard to find a different input x' with H(x') = H(x).
goodFile, given H digest, published badFile, to be found H must be the same digest the attacker does not get to choose the target digest; goodFile fixed it
  • The attack it stops. Ubuntu publishes the digest of its installer. An attacker wants to hand you a modified installer that matches that digest, so your check passes.
  • Why the name, and the cost. The real file is already one input for that digest, a “preimage.” The attacker needs a second one. They are stuck with one target digest and must hit it exactly: about 2^n tries, as in property 1.
[+] Property 3: collision resistance
Collision resistance: it is hard to find any two different inputs x ≠ x′ with H(x) = H(x′). Nothing is given. The attacker chooses both.
THE DEVELOPER WRITES TWO APPS THAT SHARE ONE DIGESTclean appHmalwareHy = 7e3a…y = 7e3a…same digest, by design THE APP STOREreviews the clean app → approves digest ymalware arrives: digest = y? ✓ → let throughit only ever compares digests
  • Different from property 2: the developer wrote both files and never needed a specific digest. Any pair of their own that collides will do.
  • Cost for a good hash: only about 2^(n/2) tries. For SHA-256 that is 2^128, not 2^256. Why the square root? A fact about birthdays. Next.
[?] The birthday paradox, for this room

? There are about 80 of you here. What is the chance that at least two people in this room share a birthday?

  • Guess first. Under 10%? About 20%? Over 50%? Nearly certain?
  • Then vote on Slido: scan the code and enter your birthday, month and day. We will see if this room has a match.
  • Then we do the math, step by step, with no formula pulled out of the air.
QR code for the Slido birthday poll Slido poll: your birthday
[+] Step 1: flip the question
  • “At least two people share” is messy to count directly. Two could share, or three, or two separate pairs, or one triple and a pair. Too many cases.
  • “All 80 birthdays are different” is one clean event. Compute that instead, and then subtract from 1.
  • Assumptions, so the math is honest: 365 days, every day equally likely, and one person's birthday tells you nothing about another's. Real birthdays are not perfectly uniform, and we ignore February 29. Close enough.
P(at least two share) = 1 − P(all 80 are different)
[+] Step 2: add the people one at a time

Line the 80 people up. Ask each one, in turn, to have a birthday that nobody before them has.

person 1 nobody before them, so any day works 365/365 = 1.00000 person 2 must avoid the 1 day already taken 364/365 = 0.99726 person 3 must avoid the 2 days already taken 363/365 = 0.99452 person 4 must avoid the 3 days already taken 362/365 = 0.99178 ... person 23 must avoid 22 taken days 343/365 = 0.93973 ... person 80 must avoid 79 taken days 286/365 = 0.78356 # each line is the chance that this person avoids everyone before them, # given that everyone before them already avoided each other.

Every single line is close to 1. The surprise is what happens when you multiply 80 of them.

[+] Step 3: multiply, and watch it collapse
P(all different) = 365/365 × 364/365 × 363/365 × ... × 286/365 # 80 factors running product after 10 people 0.883 # so a 12% chance that two share after 23 people 0.493 # 51%: better than a coin flip after 40 people 0.109 # 89% after 60 people 0.0059 # 99.4% after 80 people 0.0000857 # 99.99% P(at least two share) = 1 − 0.0000857 = 0.99991 # 99.991%
the same thing, as three lines of python
p = 1.0 for k in range(80): p *= (365 - k) / 365 # person k+1 must avoid k taken days print(1 - p) # 0.99991...
[+] Why it feels wrong: count pairs, not people
  • Your gut compares 80 people with 365 days, sees that 80 is much smaller, and says “unlikely.”
  • But a shared birthday is a property of a pair. 80 people make 80 × 79 / 2 = 3,160 pairs, and each pair matches with chance 1/365.
  • 3,160 / 365 ≈ 8.7. So we should expect about nine matching pairs in this room, not zero.
  • Rule of thumb. With K possible values, the chance of a collision passes 50% after about 1.2 × √K samples. √365 ≈ 19, so about 23 people.
PeopleShared birthday
1012%
2351%
3071%
5097%
8099.99%
[+] Recap: the birthday paradox for any K and T

T people, each birthday drawn at random from K days. First we count outcomes.

ALL OUTCOMES: EVERY PERSON MAY PICK ANY OF THE K DAYS K K K K ... K person 1 person 2 person 3 person 4 person T K x K x K x K x ... x K = K^T NO REPETITION: EACH PERSON MUST AVOID THE DAYS ALREADY TAKEN K K-1 K-2 K-3 ... K-T+1 avoids 0 days avoids 1 day avoids 2 days avoids 3 days avoids T-1 days K (K-1) (K-2) ... (K-T+1) = (K)_T
P(no repetition) = (K)_T / K^T = K/K · (K-1)/K · (K-2)/K · ... · (K-T+1)/K = (1 - 0/K) · (1 - 1/K) · (1 - 2/K) · ... · (1 - (T-1)/K) # the same 80 fractions as before, now for any K and T

A bound and an approximation turn this product into a formula we can read.

[+] Step 1: bound the product by an exponential
1 - x ≤ e^(-x) FOR EVERY x 0 1 x 1 0.5 e^(-x) 1 - x the curve is never below the line; they touch only at x = 0
Which way does the bound go? Every factor is at most its exponential, so the product is at most e^(-T(T-1)/2K). That is an upper bound on P(no repetition), and so a lower bound on P(some repetition).
1 - x ≤ e^(-x) # for every x: the plot (1 - 1/K)(1 - 2/K) ... (1 - (T-1)/K) ≤ e^(-1/K) · e^(-2/K) · ... · e^(-(T-1)/K) = e^(-(1 + 2 + ... + (T-1)) / K) # exponents add up = e^(-T(T-1)/2K) # the sum is T(T-1)/2 so P(no repetition) ≤ e^(-T(T-1)/2K) and P(some repetition) ≥ 1 - e^(-T(T-1)/2K)
[+] Step 2: from e^(-x) to 1 - x, for small x only
e^(-x) = 1 - x + x^2/2 - x^3/6 + ... # the Taylor series of e^(-x) ≈ 1 - x # small x: x^2 and beyond are tiny P(no repetition) ≤ e^(-x) ≈ 1 - T(T-1)/2K # with x = T(T-1)/2K P(some repetition) ≥ 1 - e^(-x) ≈ T(T-1)/2K # grows like T^2: it counts pairs
K = 365: P(NO REPETITION) AGAINST T PEOPLE 0 20 40 60 1 0.5 0 23 people: one half exact product e^(-T(T-1)/2K), the bound 1 - T(T-1)/2K
Texactbound e^(-T(T-1)/2K)1 - T(T-1)/2K
100.8830.8840.877
230.4930.5000.307
400.1090.118-1.14
800.000090.00017-7.66

The exponential bound sits just above the exact value everywhere. The straight line fails once T passes about √K, which is 19 here.

[+] Recap: collision resistance, birthday style
Collision resistance It should be hard to find x ≠ x' with h(x) = h(x'). We now ask how hard, using the birthday formula.
BIRTHDAYS HASHING K = 365 possible days K = 2^n possible digests T people T inputs x, x', x'', ... that we hash two people share a birthday two inputs share a digest: a collision every day equally likely h looks random: every digest equally likely
P(collision among T inputs) ≥ 1 - e^(-T(T-1)/2K), with K = 2^n # the birthday bound: the chance of a collision is at least this
[+] Recap: how many inputs until the first collision?
P(collision) ≥ 1 - e^(-T(T-1)/2K) ≈ 1 - e^(-T^2 / 2K), large T we want P(collision) ≥ p, for a p we choose: 1 - e^(-T^2 / 2K) = p T^2 / 2K = ln( 1 / (1-p) ) T = √( 2K ln(1/(1-p)) ) = c(p) · √K with K = 2^n: T = c(p) · 2^(n/2)
  • Whatever p we pick, T is a small constant times √K. A birthday search costs about 2^(n/2), not 2^n. For SHA-1 that is 2^80, not 2^160.
LOWER BOUND ON P(COLLISION) AGAINST u = T / √K, FOR EVERY K 0 1 2 3 1 0.5 0 u = 1.18: at least a coin flip 23 people at K = 365 is u = 1.2 u = 2: 86% u = 3: 99%
target pT neededfor K = 365
1%0.14 √K3 people
50%1.18 √K23 people
99%3.03 √K58 people
[+] Recap: the square root on real digest sizes WORK TO FIND A COLLISION, AS A POWER OF TWO 2^0 2^128 2^256 MD5n = 128 fixed target: 2^128 birthday: 2^64 SHA-1n = 160 fixed target: 2^160 birthday: 2^80 SHA-256n = 256 fixed target: 2^256 birthday: 2^128
fixed target: pick one x, look for x' with h(x') = h(x) # 2^n: one fixed digest to hit birthday: hash many inputs, wait for any two to agree # 2^(n/2): any pair will do n-bit digest = n/2 bits of collision security # SHA-256 gives 128, not 256 2^64 ≈ ten gaming GPUs running for a year, about $20,000 in 2026 # 2017: $100,000

An n-bit digest gives only n/2 bits of collision security. That is why digests today are 256 bits long.

[?] Can a hash be one-way but not collision resistant?

? Suppose g is a good one-way hash. Build a new hash h out of g that is still one-way, but where anyone can find a collision instantly.

Define h(x) = g(x'), where x' is x with its last bit removed.

x0 x1 drop the last bit x g g(x) one digest, two inputs
  • h stays one-way: a preimage of h is a preimage of g with one extra bit, so inverting h means inverting g.
  • Collisions are free: h(x0) = h(x1) for every x, because the last bit is thrown away before g ever sees it.
[?] Can a hash be collision resistant but not one-way?

? Suppose g is collision resistant with an n-bit output. Build h out of g so that collisions stay hard, but half of all digests can be inverted instantly.

h has an (n+1)-bit output. If x is exactly n bits long, h(x) = 0x: x itself with a 0 in front. Otherwise h(x) = 1g(x).

BUILD h INVERT h x, exactly n bitscopy it 0 xn+1 bits x, any other lengthhash it g 1 g(x)n+1 bits y = 0 ...starts with 0 remove the 0 x, foundno work at all y = 1 ...starts with 1 invert g? stuckg is one-way
  • Collisions stay hard: two n-bit inputs give different copies, other inputs need a collision in g, and the two kinds differ in the first bit.
  • Not one-way: half of all (n+1)-bit digests start with 0, and each of those is inverted by reading off its last n bits.
[+] Recap: weak collision resistance
Weak collision resistance (second-preimage resistance): given a randomly chosen x, it is hard to find x′ ≠ x with h(x) = h(x′). The attacker does not get to pick a convenient x.
WEAK COLLISION RESISTANCE (SECOND-PREIMAGE)givenx, drawn at randomhanded to the attackerfindx′ ≠ x, same digestHbrute force: about 2^n tries, one fixed target COLLISION RESISTANCE, THE STRONG KINDgivennothingfindxx′any pair, both of their choosingHbrute force: about 2^(n/2) tries, birthday bound

? Weak collision resistance does not imply collision resistance. Why? Try to build a hash function that has the weak property and not the strong one.

[+] Why weak does not imply strong: plant one collision every other x g g(x) the fixed input a g g(a) the fixed input b h skips g and returns g(a) h(x) = g(x) for every x except b, and h(b) = g(a). a and b are two fixed inputs written into the definition of h, so everyone who reads the definition knows them.
  • h is still weakly collision resistant. A random x is almost never a or b, so a second preimage for it is a collision in g, and g is good.
  • h is not collision resistant. The pair (a, b) is printed in the definition. The strong attacker hands it in with zero work; the weak attacker is never given a or b.
  • Not only a thought experiment. MD5 is in this state: collisions in seconds on a laptop, while the best second-preimage attack still needs about 2^123 work.
[?] Does collision resistance imply weak collision resistance?

? Suppose h is collision resistant. Must it also be weakly collision resistant? Prove it, or build a counterexample.

B: a collision finder, built from the weak attacker A pick a random x B may choose x A, the weak attacker finds a second preimage x' ≠ x with h(x') = h(x) output (x, x') a collision B costs one random draw plus one run of A. If A is efficient, so is B.
suppose weak collision resistance is broken: an efficient A, given a random x, returns x' ≠ x with h(x') = h(x) then B breaks collision resistance: pick a random x; run A on x to get x'; output (x, x') # x ≠ x' and h(x) = h(x') contrapositive: collision resistant ⇒ weakly collision resistant # the converse fails: the planted collision has the weak property and not the strong one
[+] Hashing is not encryption
HashEncryption
A key?None. Anyone can compute H(x) from x.Yes. You need the key to encrypt and to decrypt.
Reversible?No. There is no “un-hashing.” The input is gone.Yes. Decrypt with the key and the message is back.
Same input twice?Same digest, always. So digests can be compared for equality.A different ciphertext each time, on purpose.
ExampleSHA-256(“hello”) is always 2cf2…9824.AES(“hello”) looks different every time, and comes back with the key.

A hash hides nothing by itself. It only makes forgery hard.

[+] Application 1: storing passwords user types "hunter2" H( ) compare for equality password file The server stores H("hunter2"), never "hunter2". A stolen password file holds only digests.
Why hash instead of encrypt? An encryption key would have to live on the server. Steal the server, decrypt every password. A hash has no key to steal.
Does it protect weak passwords? No. The attacker hashes “123456”, “password”, “hunter2”, and compares. Hashing stops reading, not guessing.
[?] Which property does password storage rely on?

? The thief holds H(password) and wants to log in. Which property stops them? One-wayness, and nothing else. The thief is handed no input to match and chooses no pair.

one-way needed: no way back from digest to password weak collision resistance not needed: nobody hands the thief a password collision resistance not needed: the thief chooses nothing
[+] Application 2: checking a download

Ubuntu wants you to get its 6 GB installer unmodified. The file comes from any mirror; the digest comes from ubuntu.com.

Ubuntu the 6 GB installer any mirror, any network: an attacker could swap the file ubuntu.com, over HTTPS: the 64 hex digits a place the attacker cannot rewrite you run sha256sum, compare
  • The trick: a trusted channel for 256 bits protects 6 GB. You only need to get the tiny digest from a place the attacker cannot touch.
  • The digest page is Ubuntu's own, served over HTTPS. So the check is exactly as trustworthy as ubuntu.com itself, and no more.
[?] Which property does the download check rely on?

? The attacker wants to swap the installer without changing its published digest. Which property stops them? Weak collision resistance: the file and its digest are fixed, so they need a second preimage. If the attacker helped write the file, only full collision resistance holds.

one-way not the issue: the installer is public anyway weak collision resistance needed: the file and its digest are fixed collision resistance also needed if the attacker helped write it
[+] Application 3: a sealed bid

An auction where everyone bids at once, and nobody can see the others’ bids or change their own afterwards.

1. BID IN SECRET: ALICE SENDS ONLY A DIGEST“500” + rHd₀ = 9b1f…r is a random number, so 500 cannot be guessed from d₀ 2. EVERYONE DOES THE SAME. THE TABLE:Alice: 9b1f…Bob: 3c77…Carol: e204…nobody can tell what anyone bid 3. OPEN: ALICE REVEALS 500 AND r, EVERYONE RE-HASHES“500” + rH= d₀ ✓ 4. SHE CANNOT CHEAT“400” + r′Hmust = d₀switching afterwards = a second preimagetwo bids with one digest in advance = a collision

The random r is there because bids are guessable: without it anyone could hash “100”, “200”, … and read every bid off the table. One-way stops reading, not guessing.

[?] Which properties does the sealed bid rely on?

? Alice hands in H("500" + r). Which properties keep her bid hidden and fixed? All three: one-wayness hides the bid, weak collision resistance stops re-opening it, collision resistance stops two prepared bids.

one-way needed, with r: nobody can read the bid weak collision resistance needed: Alice cannot re-open to another bid collision resistance needed: no two prepared bids with one digest

“Use a hash” is not a design. Say which property you are relying on.

[+] Real hash functions, and where they stand
FunctionYearDigestStatus today
MD51992128 bitsBroken. A laptop finds a collision in seconds. Never use it for security.
SHA-11995160 bitsBroken. First real collision in 2017, chosen-prefix collisions in 2020. Deprecated everywhere.
SHA-256, SHA-5122001256, 512 bitsFine. The SHA-2 family. Used in TLS, Git, Bitcoin, package managers.
SHA-32015224 to 512 bitsFine. A completely different design, kept as a backup in case SHA-2 falls.

“Broken” here means collisions are findable. Nobody can reverse MD5 or SHA-1 either; property 1 still stands.

[?] How can a fixed-size box hash a file of any size? A TOY: READ THE FILE FOUR BYTES AT A TIME AND KEEP A RUNNING 32-BIT SUM "hell" "o wo" "rld!" the file, 12 bytes state = 00000000 add add add fd47f249
state = 00000000 # a fixed start value state = state + 6c6c6568 ("hell" as a 32-bit number) = 6c6c6568 # chunk 1, mod 2^32 state = state + 6f77206f ("o wo") = dbe385d7 # chunk 2 state = state + 21646c72 ("rld!") = fd47f249 # chunk 3: the digest
  • This toy is a checksum, and a terrible hash. Swap two chunks and the sum does not change, so collisions are everywhere.
  • But the shape is exactly right, and MD5 keeps it. Fixed-size chunks, a running state, one mixing step per chunk. MD5 replaces the addition with a much better mixing step and uses a bigger state.
[+] MD5, top down: pad, split, chain 1. PAD: A 1 BIT, THEN ZEROS, THEN THE MESSAGE LENGTH IN A 64-BIT FIELD, UP TO A MULTIPLE OF 512 BITS the message say 100 bytes = 800 bits 1 1 bit 000 ... 000 159 zero bits, filler 800 the length, as 64 bits = 1024 bits 800+1+159+64 2. CUT IT INTO 512-BIT BLOCKS, EACH ONE 16 WORDS OF 32 BITS block 1: 512 bits block 2: 512 bits 3. CHAIN: 128-BIT STATE, ONE BOX PER BLOCK A B C D 128 bits, fixed constants MD5 compress (state, block) -> state new A B C D MD5 compress (state, block) -> state A B C D final state = digest A 6 GB file is about 94 million blocks through the same box. The state carries everything seen so far.
  • Why end with the length, in a fixed 64-bit field? It always sits in the last 8 bytes, so the reader knows where the message stopped, and “abc” and “abc plus zeros” can no longer collide for free.
  • Two things are left to explain: the state A B C D, and the compress box. SHA-1 and SHA-256 use this same shape, called Merkle-Damgård, with a bigger state.
[+] The state: four 32-bit words called A, B, C, D 128 BITS = 4 WORDS OF 32 BITS A B C D starts at 67452301 starts at efcdab89 starts at 98badcfe starts at 10325476
$ echo -n "hello" | md5sum 5d41402abc4b2a76b9719d911017c592 # 32 hex digits = 128 bits A = 5d41402a B = bc4b2a76 C = b9719d91 D = 1017c592 # the final state
  • The state starts at four fixed constants from the standard, is changed 64 times per block, and whatever it holds after the last block is the digest.
  • Each change rewrites exactly one of the four words, using all four of them, one word of the message and one constant. 32 bits was one CPU register in 1991, so one change is a handful of machine instructions.
[+] The block: 512 bits as 16 words M[0] to M[15] 512 BITS = 64 BYTES = 16 WORDS OF 32 BITS M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] M[10] M[11] M[12] M[13] M[14] M[15] 4 bytes each
"hello" = 68 65 6c 6c 6f # 5 bytes, 40 bits padded to 64 bytes = 68 65 6c 6c 6f 80 00 00 ... 00 00 28 00 00 00 00 00 00 00 # 80 carries the single 1 bit, then zeros, and the last 8 bytes hold the length 40 = 0x28 M[0] = 6c6c6568 M[1] = 0000806f M[2] .. M[13] = 00000000 M[14] = 00000028, the length M[15] = 00000000
  • Each operation consumes one word M[i]. Sixteen words and sixty-four operations: every word is used four times, once in each round.
  • The bytes of a word are read back to front, which is why “hell” becomes 6c6c6568. That is a convention of the standard, not a security feature.
[+] Open one compress box: 64 operations, 16 words block 1 block 2 block N A B C D MD5 compress MD5 compress ... MD5 compress A B C D = the digest INSIDE THE BOX: 64 OPERATIONS IN A ROW, IN FOUR ROUNDS OF 16. EVERY BLOCK RUNS THE SAME 64, IN THE SAME ORDER. round 1: op 0, op 1, ... op 15 round 2: op 16 ... op 31 round 3: op 32 ... op 47 round 4: op 48 ... op 63 EACH OPERATION EATS ONE OF THE 16 WORDS OF ITS BLOCK. THE SCHEDULE IS FIXED; ONLY THE WORDS CHANGE FROM BLOCK TO BLOCK. round 1: ops 0-15 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] M[10] M[11] M[12] M[13] M[14] M[15] round 2: ops 16-31 M[1] M[6] M[11] M[0] M[5] M[10] M[15] M[4] M[9] M[14] M[3] M[8] M[13] M[2] M[7] M[12] round 3: ops 32-47 M[5] M[8] M[11] M[14] M[1] M[4] M[7] M[10] M[13] M[0] M[3] M[6] M[9] M[12] M[15] M[2] round 4: ops 48-63 M[0] M[7] M[14] M[5] M[12] M[3] M[10] M[1] M[8] M[15] M[6] M[13] M[4] M[11] M[2] M[9]
  • Read one row: round 1 uses M[0], M[1], ... M[15] in order; the other rounds shuffle the same 16 words. Every word is used exactly once per round, four times in all. M[0] is highlighted so you can follow it.
  • So the recipe never changes; the ingredients do. Block 2 runs the same 64 operations as block 1, with its own 16 words and with the state that block 1 left behind.
[+] Two fixed tables: K[i] and s[i]
K[i]: one 32-bit constant per operation
K[i] = floor( 2^32 · |sin(i + 1)| ) # one for each of the 64 operations K[0] = d76aa478 K[1] = e8c7b756 K[2] = 242070db K[3] = c1bdceee ... K[63] = eb86d391
  • Sixty-four fixed numbers, one per operation, the same for every block of every file. Operation i adds K[i] into its sum.
  • They come from the sine function so that anyone can recompute them. Nobody could have hidden a trapdoor in a table everyone can check.
s[i]: how far to rotate in operation i round 1: operations 0 to 15 7 12 17 22 7 12 17 22 7 12 17 22 7 12 17 22 round 2: operations 16 to 31 5 9 14 20 5 9 14 20 5 9 14 20 5 9 14 20 round 3: operations 32 to 47 4 11 16 23 4 11 16 23 4 11 16 23 4 11 16 23 round 4: operations 48 to 63 6 10 15 21 6 10 15 21 6 10 15 21 6 10 15 21
  • Rotating left by s[i] slides every bit of the sum s[i] places up, so a bit that sat low in one operation sits high in the next. Each round cycles through four amounts.
[+] Three tools inside one operation, in 8 bits 1. MIX THREE STATE WORDS B C D mix word B = 11001010 C = 10101010 D = 01010101 F = 10011111 F: where B has a 1 take C's bit, else D's. rounds 2, 3, 4 swap F for G, H, I: other mixes 2. ADD, MOD 2^32 A M[i] K[i] + word 11110000 + 00110000 = 100100000 keep 8: 00100000 the carry out of the top bit is dropped. M[i]: one of the 16 words. K[i]: the constant 3. ROTATE LEFT BY s[i] BITS word <<< s[i] word 10110001 <<< 3 = 10001101 bits that fall off the left return on the right. s[i] is 7, 12, 17 or 22 in round 1
  • That is all an operation needs: one mix, some additions, one rotation. In MD5 the words are 32 bits, not 8. The mix is the only part that changes from round to round: F, then G, then H, then I. Next slide wires the three tools together.
[+] One MD5 operation: this happens 64 times per block A B C D A B C D old D becomes the new A old C becomes the new D old B becomes the new C mix mix: F, G, H or I + + M[i]: one of the 16 words + K[i]: constant number i <<< s[i] + rotate left by s[i], then add the old B old B
new_B = B + rotl( A + mix(B, C, D) + M[i] + K[i], s[i] ) # every + is mod 2^32 new_A, new_C, new_D = D, B, C # the other three registers move over mix = F in round 1, then G, H, I in rounds 2, 3, 4 # the only thing that changes by round
[+] The four rounds: three settings change
RoundThe mix of B, C, DThe rotation amounts s[i], in order
1
ops 0-15
F = (B∧C)∨(¬B∧D)
choose, selected by B
7, 12, 17, 22, repeated four times
2
ops 16-31
G = (B∧D)∨(C∧¬D)
choose, selected by D
5, 9, 14, 20, repeated four times
3
ops 32-47
H = B⊕C⊕D
parity
4, 11, 16, 23, repeated four times
4
ops 48-63
I = C⊕(B∨¬D)
parity with an OR inside
6, 10, 15, 21, repeated four times
  • The wiring is identical in all 64 operations. Only three settings change with the round: which mix, which word M[i] comes next (the grid), and how far to rotate. The constant K[i] differs in every operation as well.
[+] The compress box: 4 rounds, then add the input back A B C D state in, 128 bits round 1 16 operations F, words in order rotate 7 12 17 22 round 2 16 operations G, words 5i+1 rotate 5 9 14 20 round 3 16 operations H, words 3i+5 rotate 4 11 16 23 round 4 16 operations I, words 7i rotate 6 10 15 21 the block, as the 16 words M[0..15]: every word is used once per round + feed-forward: add the input state back in, word by word, mod 2^32 A B C D state out
  • Why add the input back? Each of the 64 operations can be undone, so without this step anyone could run the box backwards from the output. The feed-forward is what makes the box one-way.
  • The top-down picture is now complete. Pad, split into blocks, and run every block through this box, starting from the four constants. The final A B C D is the digest.
[x] How MD5 fell: from a curiosity to a weapon 2004: first collision two messages that differ only in 128 random-looking bytes 2007: chosen prefixes pick any two documents; the tails differ in up to 716 junk bytes 2008: a rogue CA certificate one CA signature, valid on two certificates; more in the PKI lecture 2012: Flame an MD5 prefix collision faked a Microsoft code signature
  • In 2004 the colliding messages were random garbage, so most people shrugged. Three years later the attack took any two documents we like and made them collide. That is the sealed-bid attack from property 3, for real.
  • Flame was nation-state malware. It carried a code signature that Windows Update accepted as Microsoft's, and an MD5 collision is what made that signature possible.

Eight years from “academic” to signed malware on Windows Update. We retire a hash when the first crack appears.

[+] SHA-1: the same design as MD5, one register wider MD5: 128-BIT STATE, 64 OPERATIONS PER BLOCK A B C D SHA-1: 160-BIT STATE, 80 STEPS PER BLOCK A B C D E
MD5SHA-1
Designed1991, Ron Rivest1995, NSA, published by NIST
Shapepad, split into 512-bit blocks, chainthe same, carrying 160 bits along
One block64 operations, 4 rounds of 1680 steps, 4 rounds of 20
The mixF, G, H, I: one per roundchoose, parity, majority, parity: one per round
Constants64 values K[i], one per operation4 values, one per round
Rotationss[i] from a table, four per roundfixed: A by 5 and B by 30, every step
Message words16 words, reused once per round16 words stretched to 80, one per step
[+] SHA-1 against MD5, in numbers
MD5SHA-1
Digest128 bits, 32 hex digits160 bits, 40 hex digits
Birthday bound2^64 hashes2^80 hashes
Speed, one RTX 4090about 164 billion hashes per secondabout 51 billion per second
First real collision2004, Wang and colleagues2017, Google and CWI
Collisions todayseconds on a laptopmonths on a few gaming GPUs, under $10,000
Second preimageno practical attack; best is 2^123no practical attack
Retired2008 warning; 2011 RFC says do not use2011 NIST deprecation; 2017 browsers reject it
Where you still meet itfile checksums, old password databasesGit object names; SHA-256 is the newer option

Same design, same fate, thirteen years apart.

[+] How strong is SHA-1? WORK TO BREAK SHA-1, AS A POWER OF TWO 2^0 2^160 2^160: invert it, by design 2^80: birthday collision, by design 2^63: collisions, after the 2005 attack 2^63 hashes: about $100,000 of cloud GPUs in 2017, about $10,000 of gaming GPUs in 2026 one step right doubles the work: 63 to 80 is 17 doublings, about 130,000 times more
  • By design: 2^160 to invert, 2^80 for a birthday collision. Every output bit depends on every input bit, so flipping one input bit flips about half the digest. Collision resistance needs that.
  • 2005: the design cracks. Wang, Yin and Yu found collisions in about 2^63 steps, far below 2^80, by steering bit differences through its 80 steps.
  • 2017: Google and CWI computed one. Two PDF files with the same SHA-1 digest, after 6,500 CPU-years and 110 GPU-years, about $110,000 of cloud time. Chosen-prefix collisions followed in 2020 for about $45,000, and by 2025 for under $10,000 on two gaming GPUs.
[+] Where SHA-3 came from: the NIST competition

After MD5 fell and SHA-1 cracked, the US standards body NIST did not design a replacement in-house. It ran an open contest.

the call Nov 2007 64 entries, 51 accepted Oct 2008 5 finalists Dec 2010, after two rounds Keccak wins Oct 2012; standard 2015
  • How it worked. Anyone could submit a design, with the full specification and code published. For four years, researchers everywhere tried to break every entry. Designs that cracked were dropped, round by round.
  • Why this is better than a secret design. A hash function is only trusted after many people have failed to break it. A public contest buys those years of attacks before anything is deployed, not after.
[+] What NIST asked for, and what SHA-3 is THE REQUIREMENTS, 2007digests of 224 / 256 / 384 / 512 bitsas secure as SHA-2: 2^n and 2^(n/2)public, royalty-free, built unlike SHA-2 SHA-2: A CHAIN OF COMPRESS BOXES (MERKLE-DAMGÅRD)IVm1Cm2Cm3Cdigestthe digest is the last state, so anyone can keep hashing: length extension SHA-3: A SPONGE (KECCAK). ABSORB, THEN SQUEEZEm1rcfm2rcfm3rcfrcdigest1600-bit state: r absorbs, c is never shown. no length extension RESULTFIPS 202, August 2015use today: SHA-256. SHA-3 is the backup, built differently so one break cannot take both

MD5 and SHA-1 are still fine for spotting accidents, a download that broke halfway, but never for stopping an attacker. The competition story: csrc.nist.gov/projects/hash-functions/sha-3-project.

[ 02 ]

Integrity and
authentication

Alice sends Bob a message over a network the attacker controls. Bob wants to know that the message is unchanged and that it really came from Alice.

[?] Can a plain hash protect a message in transit? ALICE MALLORY, ON THE NETWORK BOB "pay Bob $10" plus H("pay Bob $10") message, digest "pay Mallory $900" plus H("pay Mallory $900") Mallory rewrites the message and recomputes the digest Bob recomputes H it matches, so he accepts

? Alice attaches the digest of her message. What did that buy Bob, and how would you fix it? Nothing. Anyone can compute H, so the digest only tells Bob that the message matches its own digest, not who wrote it. With the Ubuntu download the digest travelled over a separate channel that the attacker could not touch. Here there is no such channel. We need something Mallory cannot compute: a secret that only Alice and Bob know.

[+] A message authentication code: a digest that needs a key ALICE BOB KEY KEY the same secret, agreed in advance. Mallory does not have it. tag = MAC(KEY, message) message, tag recompute MAC(KEY, message) compare with the tag received equal: accept the message different: reject it
What a MAC gives us Integrity and authentication. Only someone who knows KEY can compute the correct tag for a message, so Mallory can rewrite the message but cannot produce the tag that goes with it.
[?] First attempt: MAC(KEY, m) = H(KEY || m). What is wrong? WHAT ALICE COMPUTES IV KEY, start of m compress rest of m, padding compress the tag H(KEY, m) sent along with m WHAT MALLORY COMPUTES, WITHOUT THE KEY the tag, as a state the chain's final state her extra block compress tag' a valid tag for the longer message m, padding, extra block: no key needed

? Mallory sees m and the tag, and never learns the key. Which part of the chain picture lets her cheat? The tag is the state of the chain. Mallory loads it as her starting state, feeds one more block, and gets the right tag for m plus her extension, with no key. This length-extension attack works on MD5, SHA-1 and SHA-256. Putting the key last, H(m || KEY), makes the tag inherit every collision weakness of H. The fix is to hash twice, with the key on both ends.

[+] HMAC: two hashes, each with the key in front TWO FIXED BYTE PATTERNS, ONE BLOCK EACH, FROM THE STANDARD ipad: 36 36 36 ... 36 "inner pad" opad: 5c 5c 5c ... 5c "outer pad" secret key padded with zeros to one block ⊕ K1 1. K1 = KEY ⊕ ipad: inner key hash message 2. inner = hash( K1 || message ) 32 bytes; it never leaves the box secret key the same secret, same padding ⊕ K2 3. K2 = KEY ⊕ opad: outer key hash 4. tag = hash( K2 || inner ) this is HMAC(KEY, message)
ipad = 36 36 36 ... 36 # 64 bytes of 0x36: the inner pad. Just a fixed pattern, not a secret opad = 5c 5c 5c ... 5c # 64 bytes of 0x5c: the outer pad. Another fixed pattern tag = H( (KEY ⊕ opad) || H( (KEY ⊕ ipad) || message ) )
  • Why two hashes? The inner hash alone is attempt 1, key then message, which Mallory could extend. Hashing that digest again with the key leaves no chain state to continue.
  • Why XOR the key with two patterns? One secret becomes two different keys, so the two hashes never start from the same block. Any two different patterns would do; 0x36 and 0x5c are the ones the standard picked.
[+] HMAC-SHA256 by hand: key "k3y", message "pay Bob $10"
KEY = 6b 33 79 # "k3y", 3 bytes, padded with zeros to 64 bytes K1 = KEY ⊕ ipad = 5d 05 4f 36 36 ... 36 # 6b⊕36=5d, 33⊕36=05, 79⊕36=4f, 00⊕36=36 inner = SHA256( K1 || "pay Bob $10" ) = 2220d7d9dd3b9ecd36d299d29b64f94abe44faee1ade5955035e7bc987e762f0 K2 = KEY ⊕ opad = 37 6f 25 5c 5c ... 5c # 6b⊕5c=37, 33⊕5c=6f, 79⊕5c=25, 00⊕5c=5c tag = SHA256( K2 || inner ) = 14eea6c27806b0ea1c83c26f40ed49f510b8fddb0a0e080fc6913f4e2f25c4d4 # the same 64 hex digits that openssl dgst -sha256 -hmac 'k3y' prints on the next slide
  • Two ordinary SHA-256 calls and two XORs, nothing else. Any hash function can take the place of SHA-256.
  • Compare attempt 1: SHA256(KEY || "pay Bob $10") alone is e1ca12ca...9a7d5a, which Mallory could extend. The outer call seals it.
[+] HMAC in practice
$ echo -n 'pay Bob $10' | sha256sum 51f03ce57e0fd007...e2e6bcb6 # the plain digest: no key, anyone can compute it $ echo -n 'pay Bob $10' | openssl dgst -sha256 -hmac 'k3y' 14eea6c27806b0ea...2f25c4d4 # the tag: it needs the key $ echo -n 'pay Bob $90' | openssl dgst -sha256 -hmac 'k3y' 8658a1be85f149c4...fa18d1e5 # one character changed: a different tag $ echo -n 'pay Bob $10' | openssl dgst -sha256 -hmac 'k3z' edc5cabcdab95d74...22dcea8e # wrong key: Mallory cannot make the right tag
why build the mac from a hash
  • Hashing is faster than encryption, hash code is in every library, and we can swap one hash for another without redesigning the MAC.
  • The US once restricted exports of encryption, but never of hashing.
where it runs today
  • TLS records and IPsec packets carry an HMAC, or a successor of it.
  • JWT web tokens and AWS API requests are HMACs over the request.
[!] In the news: an AI agent escaped through DNS
  • Sep 20, 2026. An OpenAI research model was being trained on a search task: find a person from clues.
  • It ran in a sandbox. Every website was blocked. Web pages came from an offline copy.
  • It still asked a public chatbot 19 questions, and it got the answers back.
  • OpenAI paused its most capable models. Training, evaluation, and use with tools all stopped.
QR code to OpenAI's incident reportOpenAI's own report

? Every website was blocked. How did the model reach a chatbot? Through DNS, the service that turns names into addresses. The sandbox did not fence it.

[+] DNS: the resolver asks around for us OUR NETWORKTHE INTERNETour laptopwants a websitewww.cs.columbia.edu?the resolverColumbia’s, 128.59.1.4asks each levelin turn, for us root name serverg.root-servers.net1↓ go ask the edu servers edu name serverj.edu-servers.net2↓ go ask columbia.edu’s columbia.edu name serverext-ns1.columbia.edu3↓ it is 128.59.11.206 128.59.11.206The laptop asked one question.The resolver asked three serversout on the internet, for us.
[+] The same walk, in a real trace
$ dig +short www.cs.columbia.edu # ask our resolver, the normal way webcluster.cs.columbia.edu. 128.59.11.206 # 20 ms, one question from us $ dig +trace www.cs.columbia.edu # walk the tree ourselves, step by step . NS a.root-servers.net. (and 12 more) edu. NS a.edu-servers.net. (and 12 more) # from g.root-servers.net columbia.edu. NS ext-ns1.columbia.edu. (and 3 more) # from j.edu-servers.net webcluster.cs.columbia.edu. A 128.59.11.206 # from ext-ns1.columbia.edu
  • Each level only says who is responsible for the next part of the name.
  • The last answer comes from a server that the owner of the domain runs.
[x] The escape: smuggle the question inside a name INSIDE THE SANDBOXTHE INTERNETthe modelwants an answeroffline web cacheno live internet web proxysearch, Google,Bing: all blocked the DNS resolvernobody fenced itleft open a public chatbotreached via a delegation servicewhats-the-capital.q.examplethe question, hidden inside a namethe capital is Paristhe answer, inside the DNS reply
[+] The fence had a hole, and the alarm did not stop it THE TIMELINE OF ONE RUN 9:50first answer gets in 10:02alert fires, +12 min 10:05human sees it, +3 min 12:34killed by hand, +2.5 h the auto-stop never fired
  • The sandbox blocked the front door, not the plumbing. OpenAI calls this transitive access: a service you rely on, like DNS, can still reach the outside.
  • Anything that carries a question and an answer can carry data. Even a channel meant only for names, one small piece at a time.
  • The fix. Block the internet at two independent layers, and let DNS reach only a short allow-list of names.

A boundary is only as strong as the quietest thing that crosses it. Watch the plumbing.

[+] Recap: MD5 runs one compress box per block PAD THE MESSAGE TO A MULTIPLE OF 512 BITS, THEN CUT IT INTO BLOCKS block 1, 512 bits block 2, 512 bits ... last block, with padding IV: A B C D compress compress ... compress digest = final A B C D INSIDE ONE COMPRESS BOX: 4 ROUNDS x 16 OPERATIONS = 64 OPERATIONS round 1: 16 ops, mix F round 2: 16 ops, mix G round 3: 16 ops, mix H round 4: 16 ops, mix I + add the input state back
one operation: new_B = B + rotl( A + mix(B, C, D) + M[i] + K[i], s[i] ) new_A, new_C, new_D = D, B, C # 64 of these per block
  • The digest is the chain state after the last block, and that is why H(KEY || m) can be extended.
  • MD5 collisions can be found in seconds today, so MD5 must never be used for signatures.
[+] Recap: HMAC hashes twice, with the key in front 1. ONE KEY BECOMES TWO KEYS, ONE BLOCK (64 BYTES) EACH KEY, padded to 64 bytes K1 = KEY ⊕ ipad (36 36 ... 36) K2 = KEY ⊕ opad (5c 5c ... 5c) 2. INNER HASH: K1, THEN THE MESSAGE. ONE EXTRA BLOCK COMPARED WITH HASHING m ALONE IV compress K1 compress each block of m, then pad inner = H(K1 || m), 32 bytes for SHA-256 3. OUTER HASH: K2, THEN INNER. EXACTLY 2 BLOCKS IV compress K2 compress inner, then pad tag = H(K2 || inner)
  • Only someone who knows the key can compute the tag.
  • The outer hash always starts again from the IV and hashes an input of fixed size.
  • The tag is never the chain state right after m and its padding, so the padding trick that extends H(KEY || m) does not work.
[ 03 ]

Symmetric
encryption

Alice and Bob share a key. How does Alice send a message that nobody else can read?

[+] What we are going to discuss in this part 1. THE ONE-TIME PAD plaintext ⊕ key, just as long ciphertext Perfectly secure, and almost never used. 2. DES, QUICKLY 64-bit block S S S S S S S S ... 16 rounds ... ciphertext A quick look at a block cipher and its 56-bit key. 3. AES, IN DEPTH 16 bytes SubBytes ShiftRows MixColumns ⊕ round key x 10 rounds The key part of this lecture, opened up like MD5.
[?] The threat model: a secret over an open network Alice has the key Bob has the same key a message only they can read Mallory, on the network and she knows our algorithm

? What is left that Mallory does not know?

Threat model: Mallory knows
  • She knows the algorithm.
  • She sees every byte on the wire.
Secret: Mallory does not know
  • She does not know the key.
  • She does not know the random numbers we pick.

All our security rests on the right-hand column.

[+] Kerckhoffs said that only the key should be secret
Kerckhoffs's principle, 1883 An encryption scheme should be secure even if the enemy knows everything about it except the key. Do not rely on “security by obscurity.”
What leaksWhat we must do
The keyMake a new key. It takes minutes.
A secret algorithmReplace every card, phone and player that runs it. It takes years.

So everything rests on the key, and the key comes from a random number generator.

Portrait of Auguste Kerckhoffs Jean-Guillaume-Hubert-Victor-François-Alexandre-Auguste Kerckhoffs von Nieuwenhof, 1835–1903. Public domain
[!] Weak randomness broke Debian's keys in 2008
  1. In 2006, a debugging tool warned that OpenSSL read uninitialized memory, and a Debian maintainer deleted the line that caused the warning.
  2. That line fed randomness into the key generator, so the only input left was the process ID, a number below 32,768.
  3. For two years, every SSH and SSL key made on Debian and Ubuntu was one of only 32,768 keys, instead of one of about 2^2000.
  4. In May 2008, Luciano Bello found the bug, and attackers simply generated all 32,768 keys and tried each one.
/* OpenSSL, md_rand.c */ /* MD_Update(&m, buf, j); */ /* 32,768 keys, not 2^2000 */
Debian Security Advisory DSA-1571-1, May 13, 2008: Luciano Bello discovered that the random number generator in Debian's openssl package is predictable Debian Security Advisory DSA-1571, May 13, 2008.
xkcd comic: int getRandomNumber() { return 4; // chosen by fair dice roll. guaranteed to be random. } It was not quite this bad. xkcd 221
[+] Kryptos K4 keeps its method secret, just for fun
  • In 1990, Jim Sanborn put Kryptos, a sculpture with four encrypted panels, at CIA headquarters.
  • K1 to K3 were solved in the 1990s, but K4, only 97 letters long, stayed unsolved for 35 years.
  • In 2025, two journalists found the plaintext in Sanborn's archive, and the solution sold for $962,500.
  • K4 fell to a leak, not to cryptanalysis. It is fun as a puzzle, but we should never do this in practice.
# K4, as carved into the sculpture OBKRUOXOGHULBSOLIFBBWFLRV QQPRNGKSSOTWTQSJQSSEKZZWA TJKLUDIAWINFBNYPVTTMZFPKW GDKZXTJCDIGKUHUAUEKCAR # Sanborn's clues FLRV = EAST QQPRNGKSS = NORTHEAST NYPVTT = BERLIN MZFPK = CLOCK
QR code for paradigm.xyz/kryptos/k4

Try K4 yourself: paradigm.xyz/kryptos/k4 runs a Kryptos CTF.

K4 stayed hidden for 35 years, but we should not rely on a secret algorithm.

[ 03 · one-time pad ]

The one-time pad

It is the only cipher that is provably perfect, and almost nobody uses it. We start with the one tool it needs.

[+] Background: XOR
ONE BIT AT A TIME a b a ⊕ b 00 01 10 11 0 1 1 0 1 when the bits differ
Properties we will use
x ⊕ 0 = xXOR with zero changes nothing.
x ⊕ x = 0XOR with itself gives zero.
x ⊕ y = y ⊕ xThe order does not matter.
(x ⊕ y) ⊕ z = x ⊕ (y ⊕ z)The grouping does not matter.
(p ⊕ k) ⊕ k = pXOR with the same key twice gives back p.

For bytes and longer strings, we XOR each bit position separately.

[+] The one-time pad XORs the message with a random key ENCRYPT plaintext10111101 ⊕ key10001111 ciphertext00110010 DECRYPT, WITH THE SAME KEY ciphertext00110010 ⊕ key10001111 plaintext10111101 ciphertext ⊕ key = (plaintext ⊕ key) ⊕ key = plaintext ⊕ (key ⊕ key) = plaintext The key is a random bit sequence as long as the plaintext.
Shannon, 1949 A cipher achieves perfect secrecy if and only if there are as many possible keys as possible plaintexts, and every key is equally likely. So the one-time pad is perfect when its key is truly random, as long as the message, and used only once.
QR code to the proof of perfect secrecy for the one-time padthe proof
[?] The one-time pad is perfect. What is the catch?
A one-time pad sheet of five-digit random groups, issued by the CIA A one-time pad sheet issued by the CIA. Photo: CIA, public domain

? Spies really used pads like this one. The math is perfect, so why does nobody use a one-time pad to protect a website?

Imagine Alice and Bob using it every day for a year. What would they need, and what could Mallory still do?

QR code to the CIA page about this one-time pad

The CIA's own page about this pad and how agents used it.

[?] Limitation 1: What if we reuse the key? MESSAGE 1 P100000000 ⊕ K00110010 = C100110010 MESSAGE 2, THE SAME K P211111111 ⊕ K00110010 = C211001101 MALLORY XORS THE TWO CIPHERTEXTS C1 ⊕ C211111111 = P1 ⊕ P2, and the key is gone C1 ⊕ C2 = (P1⊕K) ⊕ (P2⊕K) = (P1⊕P2) ⊕ (K⊕K) = P1 ⊕ P2
  • Is P1 ⊕ P2 useful without the key?
[?] Limitation 1: What if two pictures share a key?
Plaintext picture 1: the words SEND $900P1
Plaintext picture 2: a smiley faceP2
Ciphertext 1: random noiseC1 = P1 ⊕ K
Ciphertext 2: random noiseC2 = P2 ⊕ K

Each pixel is one bit, and K is one random bit per pixel, used for both pictures.

? Mallory XORs the two noise pictures. What does she see? She sees both plaintexts on top of each other, because the key cancels.

C1 XOR C2: the words SEND $900 and the smiley face overlaidC1 ⊕ C2 = P1 ⊕ P2
[?] Limitation 1: How much key does Bob need?

Alice sends Bob 1 GB of video every day for a year, with a one-time pad.

A room full of computer tapes The data: 365 GB of video that Alice wants to send secretly.
The same room full of computer tapes The key: 365 GB of random bits that Bob must receive secretly first.

If Alice can send 365 GB secretly, she does not need the pad.

[?] Limitation 2: Can Mallory turn $100 into $900?
P = "pay Bob $100" = 70 61 79 20 42 6f 62 20 24 31 30 30 # she knows the format C = P ⊕ K = d4 7e 70 e3 35 31 49 b0 f5 5f 08 c4 # on the wire
? Byte 9 is the digit 1. Can Mallory make it 9 without K?
  • Yes. She XORs byte 9 of C with 31 ⊕ 39 = 08.
  • Flipping a bit of C flips the same bit of P.
  • Bob decrypts cleanly and sees nothing wrong.
C' = d4 7e 70 e3 35 31 49 b0 f5 57 08 c4 # 5f ⊕ 08 = 57, the only change C' ⊕ K = "pay Bob $900" # Bob's decryption

The one-time pad guarantees confidentiality, but not integrity.

[?] How can a short key protect a long message?
? What if we cannot pre-share huge random keys?
  • We use block ciphers or stream ciphers.
  • One short key is reused, with some restrictions.
  • Hint: think of MD5, which runs one fixed box over a long message, one 64-byte block at a time.
[+] MD5's box is almost a block cipher MD5 COMPRESS A B C D M[0..15] 64 operations out + feed-forward: add the input back A COPY OF MD5 + feed-forward A B C D M[0..15] 64 operations out plaintext the key rounds ciphertext A BLOCK CIPHER no feed-forward: a cipher must run backwards
  • If we swap the roles, the state is the block and the message words are the key.
  • The difference is the feed-forward. A hash must not run backwards, but a cipher must, for anyone with the key.
[+] A block cipher encrypts one block at a time PLAINTEXT BLOCK, 16 BYTES 00112233445566778899aabbccddeeff key, 16 bytes E(key, block) CIPHERTEXT BLOCK, 16 BYTES 69c4e0d86a7b0430d8cdb78070b4c55a D(key, block) the same key takes it back
  • Encryption E and decryption D use the same key.
  • DES blocks are 64 bits, and AES blocks are 128 bits.
  • The same short key is used for every block.
$ echo 00112233445566778899aabbccddeeff | xxd -r -p | openssl enc -aes-128-ecb -nopad -K 000102030405060708090a0b0c0d0e0f | xxd -p 69c4e0d86a7b0430d8cdb78070b4c55a # the block above 74db6c596f02c433989fb6c9cd317f15 # the last key byte changed to 0e
[?] A block cipher is a table chosen by the key

? A cipher has 2-bit blocks. What does one key look like?

KEY A: ONE TABLE plaintext ciphertext 00 00 01 01 10 10 11 11 KEY B: ANOTHER TABLE plaintext ciphertext 00 00 01 01 10 10 11 11 NOT ALLOWED plaintext ciphertext 00 00 01 01 10 10 11 11 00 and 01 both give 10.Bob cannot decrypt.

A one-to-one table is called a permutation. How many are there?

2-bit blocks: 4 × 3 × 2 × 1 = 4! = 24 tables # 00 has 4 choices, 01 has 3, ... 128-bit blocks: 2^128 × (2^128 − 1) × ... × 1 = (2^128)! tables
[+] DES scrambles half the block, XORs it in, and swaps
ENCRYPT, TOP DOWN input: plaintext block L0: left half R0: right half F round key K1 ⊕ L1 = R0 R1 = L0 ⊕ F(R0) 15 more rounds, keys K2 to K16 output: ciphertext block DECRYPT, BOTTOM UP input: ciphertext block keys in reverse: K16 first, K1 last L1 R1 F round key K1 ⊕ L0 = R1 ⊕ F(L1) R0 = L1 output: plaintext block

Every round can be undone, even though F cannot. So DES is one-to-one: a permutation.

[?] What is F? It mixes 32 bits with the round key
R: 32expand32 to 48 ⊕K1: 48 S1S2S3S4S5S6S7S86 bits in4 bits out shuffle32 bitsF(R, K1)
  • S-boxes: each one is a small fixed table that turns 6 bits into 4 bits in a scrambled way.
  • Shuffle: it moves the 32 output bits to new places, so in the next round each S-box gets bits from several S-boxes.
S1 bits 1-4 S2 bits 5-8 S3 bits 9-12 S4 bits 13-16 S5 bits 17-20 S6 bits 21-24 S7 bits 25-28 S8 bits 29-32
[+] How expand turns 32 bits into 48 R = 32 BITS, SPLIT INTO EIGHT 4-BIT GROUPSb1..b4b5..b8b9..b12b13..b16b17..b20b21..b24b25..b28b29..b32 TAKE ONE GROUP: b5 b6 b7 b8b5b6b7b8…b4b9… b4b5b6b7b8b9 Eight groups × 6 bits = 48 bits, matching the round key. Each borrowed bit feeds two S-boxes: diffusion.

No bits are invented: 16 of the 32 are simply repeated. The overlap lines each group up with a 6-bit S-box input and spreads every bit into two neighboring S-boxes.

[?] An S-box is a fixed lookup table S1 GETS 6 BITS011000outer bits 0 _ _ _ _ 0 = 00 pick the row: 0inner bits _ 1100 _ = 1100 pick the column: 12 THE S1 TABLE, FIXED BY THE STANDARD 012345678910111213141501441312151183106125907101574142131106121195382411481362111512973105031512824917511314100613 5S1[0][12] = 5 = 0101: four bits come out.
  • Six bits go in and four come out, so many inputs share an output.
  • That is fine, because F never has to be undone.
[?] What makes a good S-box?

A random table is not enough. A good S-box has three properties.

BalancedEvery output value is equally likely.

Each row of S1 holds each value 0 to 15 exactly once.

AvalancheFlipping one input bit changes at least 2 output bits.

Example: 011000 gives 0101, and 011001 gives 1001.

Not linearS(a ⊕ b) is not always S(a) ⊕ S(b).

Example: S(000001 ⊕ 000010) = 15, but S(000001) ⊕ S(000010) = 4. If S were linear, the whole cipher could be solved with algebra.

The S-boxes are the heart of DES Most attacks on DES target the S-boxes. IBM designed them against differential attacks in 1974, 16 years before the public found the attack.
QR code to Coppersmith's 1994 paper on the design of the DES S-boxesCoppersmith, 1994
[x] DES died because its 56-bit key is too short COST OF A MACHINE THAT FINDS A DES KEY (LOG SCALE) $10K $100K $1M $10M 1980 1990 2000 2010 1977: $20 million 1 day, estimate by Diffie and Hellman 1993: $1 million 3.5 hours, design by Wiener 1998: $250,000 56 hours, built: EFF Deep Crack 2006: $10,000 6 days, COPACOBANA
After 2006 the time kept falling. RIVYERA (2008) needed under a day, and crack.sh (2012) runs the search as a web service in about 26 hours.

Hardware gets cheaper every year, but a 56-bit key stays 56 bits. AES starts at 128.

[?] How secure is a block cipher?

It is not perfect like the one-time pad. Breaking AES-128 (not DES) by brute force takes this many guesses:

2^128 =
340,282,366,920,938,463,
463,374,607,431,768,211,456
At 10^18 guesses per second, that takes about 780 times the age of the universe.

Remember one-way hash functions?

Nobody has proved that any one-way function exists. Hashes and block ciphers both rest on that unproven assumption, and proving it would also prove P ≠ NP.

$1,000,000
P vs NP is a Clay Millennium Prize problem, still open.
[+] AES replaced DES in 2001
DESAES
Standard since19772001, after an open competition with 15 candidates
Designed byIBM, with changes by the NSADaemen and Rijmen, as Rijndael
Block size
bits encrypted at once
64 bits128 bits
Key56 bits128, 192 or 256 bits
Rounds1610, 12 or 14

The design uses some clever math, which we build on the next slides. From here on we use AES-128.

[+] AES runs four steps per round, ten times plaintext16 bytes the statea 4 x 4 grid of bytes key16 bytes 0 key schedule K0, used once, first K1K2K3K4K5K6K7K8K9K10 all made by the key schedulea different key every round ⊕ K0 ONE ROUND, DONE 10 TIMES no key 1 SubBytes replace each byte with a fixed table no key 2 ShiftRows rotate row r left by r places no key 3 MixColumns mix each column; skipped in round 10 4 AddRoundKey XOR in the round key Kr round r uses Kr, and only in AddRoundKey repeat10 times ciphertext16 bytes
[+] The plaintext becomes a grid, then meets K0 the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule
The idea: AES writes the 16 plaintext bytes into a 4 x 4 grid, column by column, and XORs the grid with the key before round 1.
PLAINTEXT: 16 BYTES IN A ROW00112233445566778899aabbccddeeff FILLED COLUMN BY COLUMN00112233445566778899aabbccddeeff ⊕ K0: THE KEY ITSELF000102030405060708090a0b0c0d0e0f = THE STATE BEFORE ROUND 100102030405060708090a0b0c0d0e0f0 ONE BYTE, IN BITS 11 = 0001 0001 ⊕ 01 = 0000 0001 = 10 = 0001 0000 Each byte meets the key byte in its place.
[+] Step 1: SubBytes looks up every byte the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule
The idea: SubBytes replaces every byte of the grid with its entry in one fixed table, the AES S-box. It works like the DES S-box, but with 8 bits in and 8 bits out.
BEFORE00102030405060708090a0b0c0d0e0f0 THE AES S-BOX: ALL 16 x 16 ENTRIES0123456789abcdef0637c777bf26b6fc53001672bfed7ab761ca82c97dfa5947f0add4a2af9ca472c02b7fd9326363ff7cc34a5e5f171d83115304c723c31896059a071280e2eb27b275409832c1a1b6e5aa0523bd6b329e32f84553d100ed20fcb15b6acbbe394a4c58cf6d0efaafb434d338545f9027f503c9fa8751a3408f929d38f5bcb6da2110fff3d28cd0c13ec5f974417c4a77e3d645d1973960814fdc222a908846eeb814de5e0bdbae0323a0a4906245cc2d3ac629195e479be7c8376d8dd54ea96c56f4ea657aae08cba78252e1ca6b4c6e8dd741f4bbd8b8ad703eb5664803f60e613557b986c11d9eee1f8981169d98e949b1e87e9ce5528dff8ca1890dbfe6426841992d0fb054bb16 10: row 1, column 0 of the S-box: ca AFTER: EVERY BYTE REPLACED63cab7040953d051cd60e0e7ba70e18c Each byte is replaced on its own, with the same table. The table is one-to-one, so it can be undone. It is the only non-linear step.
[+] Step 2: ShiftRows slides row r left by r the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule
The idea: ShiftRows slides each row of the grid to the left, and row r moves by exactly r places.
BEFORE63cab7040953d051cd60e0e7ba70e18c row 0: left by 0row 1: left by 1row 2: left by 2row 3: left by 3 AFTER6353e08c0960e104cd70b751bacad0e7 red: bytes that fell offthe left and came backon the right

Who decides the shift? The standard does. Row r always moves r places, and no key is involved.

[+] Step 3: MixColumns needs three byte tools the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule
The idea: MixColumns turns the four bytes of one column into four new bytes, and every new byte is a mix of all four old ones. It needs only three tools on bytes: add, double and triple.
1. ADD = XOR, BIT BY BIT 6301100011 ⊕ 5301010011 = 3000110000 No carries: 1 ⊕ 1 = 0. 2. DOUBLE = SHIFT LEFT BY ONE 6301100011 2·6311000110= c6 if a 1 falls off the left, drop it and XOR 1b: 2·e0111000000 ⊕ 1b00011011 =11011011= db 3. TRIPLE = DOUBLE ⊕ ITSELF 2·5310100110 ⊕ 5301010011 3·5311110101= f5
[+] Step 3: MixColumns, one new byte at a time the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule
The idea: Each new byte is one row of a fixed matrix times the column: multiply each old byte by 1, 2 or 3, then XOR the four results.
FIXED MATRIX COLUMN 0 NEW 02 03 01 01 01 02 03 01 01 01 02 03 03 01 01 02 × 63 53 e0 8c = ? ? ? ? row 0: 2·63 ⊕3·53 ⊕1·e0 ⊕1·8c 5f = c6 ⊕ f5 ⊕ e0 ⊕ 8c = 5f row 1: 1·63 ⊕ 2·53 ⊕ 3·e0 ⊕ 1·8c 72 = 63 ⊕ a6 ⊕ 3b ⊕ 8c = 72 row 2: 1·63 ⊕ 1·53 ⊕ 2·e0 ⊕ 3·8c 64 = 63 ⊕ 53 ⊕ db ⊕ 8f = 64 row 3: 3·63 ⊕ 1·53 ⊕ 1·e0 ⊕ 2·8c 15 = a5 ⊕ 53 ⊕ e0 ⊕ 03 = 15
  • The other three columns are mixed the same way.
  • ShiftRows spreads bytes across columns, and MixColumns mixes each column.
[+] Step 4: AddRoundKey XORs in the round key the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule
The idea: AddRoundKey XORs every byte of the grid with the byte in the same place of this round's key.
STATE AFTER MIXCOLUMNS5f72641557f5bc92f7be3b291db9f91a ⊕ROUND KEY K1d6aa74fdd2af72fadaa678f1d6ab76fe =STATE AFTER ROUND 189d810e8855ace682d1843d8cb128fe4 ONE BYTE, IN BITS 5f = 0101 1111 ⊕ d6 = 1101 0110 = 89 = 1000 1001
  • It is the only step that uses the key.
  • XORing the same round key again undoes it.
[+] The key is a grid, just like the message the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule
The idea: The key schedule keeps making one new word, a column of 4 bytes, until it has 44 words: 4 for each of the 11 round keys.
THE MESSAGE, AS A GRID00112233445566778899aabbccddeeff THE KEY, THE SAME WAY00010203w004050607w108090a0bw20c0d0e0fw3 The key is written into a grid column by column, exactly like the message. One column of the key is called a word: 4 bytes. w0 = 00 01 02 03, w1 = 04 05 06 07, and so on. THE SCHEDULE KEEPS MAKING WORDS: 44 IN TOTAL, AND EVERY 4 WORDS ARE ONE ROUND KEY w0w1w2w3K0w4w5w6w7K1w8w9w10w11K2w12w13w14w15K3w16w17w18w19K4w20w21w22w23K5w24w25w26w27K6w28w29w30w31K7w32w33w34w35K8w36w37w38w39K9w40w41w42w43K10
[+] A word is the one above XOR the one to its left the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule K0w0 = 00010203w1 = 04050607w2 = 08090a0bw3 = 0c0d0e0f K1g⊕w4 = d6aa74fd ⊕⊕⊕w5 = d2af72faw6 = daa678f1w7 = d6ab76fe K2g⊕w8 = b692cf0b ⊕⊕⊕w9 = 643dbdf1w10 = be9bc500w11 = 6830b3fe ⋮K10w40 = 13111d7fw41 = e3944a17w42 = f307a78bw43 = 4d2b30c5

The first word of each row has no word to its left, so it takes the last word of the row above, through g (red).

[+] What g does, in three small moves the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule
The idea: g takes one word and returns a scrambled word. It reuses the S-box from SubBytes.
w3, the last word of K00c0d0e0f 1. rotate0d0e0f0c0c moves to the end. 2. S-boxd7ab76feThe SubBytes S-box: S[0d] = d7. 3. ⊕ Rcon01 00 00 00ab76fed6g(w3)Only byte 0: d7 ⊕ 01 = d6. THEN THE USUAL XOR, WITH g(w3) IN PLACE OF w3w000010203⊕g(w3)d6ab76fe=w4, the first word of K1d6aa74fd
[+] Each round key gets a different Rcon the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule
The idea: Rcon is one byte followed by 00 00 00. Each round key K1 to K10 has its own byte, so only byte 0 changes, and it changes differently each time.
AFTER ROTATE AND S-BOXRCON WORDg(w) g(w3)makes K1d7ab76fe⊕01000000=d6ab76fed7 ⊕ 01 = d6 g(w7)makes K26238bbf6⊕02000000=6038bbf662 ⊕ 02 = 60 ⋮g(w31)makes K8d3baf72f⊕80000000=53baf72fd3 ⊕ 80 = 53 g(w35)makes K908dab5e4⊕1b000000=13dab5e408 ⊕ 1b = 13 WHERE THE FIRST BYTES COME FROM: START AT 01 AND DOUBLE, ONE BYTE PER ROUND KEYK101×2K202×2K304×2K408×2K510×2K620×2K740×2K880×2K91b×2K103680 = 1000 0000 overflows when doubled, so we XOR 1b, as in MixColumns
[?] Why the key schedule needs g the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule

? Suppose we drop g and build every word with XOR only. Alice picks the all-zero key. What are her round keys? All of them are zero. XOR of zeros is zero, so no round ever adds any key.

K1K2 XOR only00000000 00000000 00000000 0000000000000000 00000000 00000000 00000000Every round key is zero, so AddRoundKey does nothing. XOR, rotate, S-box63636363 63636363 63636363 6363636398989898 fbfbfbfb 98989898 fbfbfbfbThe bytes of each word are all the same. The key’s pattern survives. full g, with Rcon62636363 62636363 62636363 626363639b9898c9 f9fbfbaa 9b9898c9 f9fbfbaaRcon changes one byte, so the pattern breaks and spreads.
[+] All 11 round keys, and where each one is used the state1 SubBytes2 ShiftRows3 MixColumns4 AddRoundKey0 key schedule
round key words bytes Rcon used in K0 w0-w3 00010203 04050607 08090a0b 0c0d0e0f -- before round 1 K1 w4-w7 d6aa74fd d2af72fa daa678f1 d6ab76fe 01 round 1 K2 w8-w11 b692cf0b 643dbdf1 be9bc500 6830b3fe 02 round 2 K3 w12-w15 b6ff744e d2c2c9bf 6c590cbf 0469bf41 04 round 3 K4 w16-w19 47f7f7bc 95353e03 f96c32bc fd058dfd 08 round 4 K5 w20-w23 3caaa3e8 a99f9deb 50f3af57 adf622aa 10 round 5 K6 w24-w27 5e390f7d f7a69296 a7553dc1 0aa31f6b 20 round 6 K7 w28-w31 14f9701a e35fe28c 440adf4d 4ea9c026 40 round 7 K8 w32-w35 47438735 a41c65b9 e016baf4 aebf7ad2 80 round 8 K9 w36-w39 549932d1 f0855768 1093ed9c be2c974e 1b round 9 K10 w40-w43 13111d7f e3944a17 f307a78b 4d2b30c5 36 round 10
  • These are exactly the round keys printed in the standard for this key.
  • Every key follows from the one before it, so knowing any single round key gives away the whole key.
[+] We trace the FIPS-197 example through all 10 rounds
plaintext 00112233445566778899aabbccddeeff ⊕ K0 00102030405060708090a0b0c0d0e0f0 round 1 SubBytes 63cab7040953d051cd60e0e7ba70e18c # each byte looked up in S ShiftRows 6353e08c0960e104cd70b751bacad0e7 # rows rotated by 0, 1, 2, 3 MixColumns 5f72641557f5bc92f7be3b291db9f91a # each column times the matrix ⊕ K1 89d810e8855ace682d1843d8cb128fe4 # K1 = d6aa74fd d2af72fa ... round 2 end 4915598f55e5d7a0daca94fa1f0a63f7 round 3 end fa636a2825b339c940668a3157244d17 round 4 end 247240236966b3fa6ed2753288425b6c round 5 end c81677bc9b7ac93b25027992b0261996 round 6 end c62fe109f75eedc3cc79395d84f9cf5d round 7 end d1876c0f79c4300ab45594add66ff41f round 8 end fde3bad205e5d0d73547964ef1fe37f1 round 9 end bd6e7c3df2b5779e0b61216e8b10b689 round 10 no Mix 69c4e0d86a7b0430d8cdb78070b4c55a # the ciphertext # the same 32 hex digits that openssl printed for this plaintext and key
[+] One flipped bit, followed through two rounds

We encrypt our plaintext twice, the second time with its lowest bit flipped (00 becomes 01). Red cells are the bytes where the two states differ, with the XOR of the two.

ROUND 1ROUND 2 01start1 bit differ 1fSubBytes5 bits differ● SubBytes: the one flipped bit becomes 5 flipped bits, still inside one byte. 1fShiftRows5 bits differ 3e1f1f21MixColumns17 bits differ● MixColumns: one byte spreads to its whole column, 2·1f, 1f, 1f, 3·1f. 0ea7bc46SubBytes16 bits differ 0e46bca7ShiftRows16 bits differ● ShiftRows: the four bytes move into four different columns. 1c0e0e124646ca8cbcdf63bcf255a7a7MixColumns64 bits differ● MixColumns again: each column spreads, and all 16 bytes now differ. ADDROUNDKEY IS NOT SHOWN: IT XORS THE SAME KEY INTO BOTH STATES, SO NO DIFFERENCE CHANGES
[+] 10,000 random flips: the count is a coin flip SETTING: RANDOM KEY, RANDOM PLAINTEXT, FLIP ONE RANDOM BIT OF THE 128. REPEAT N = 10,000 TIMES. 40485664728088bits that changed after round 10, the ciphertext, out of 1280200400600RUNSAfter round 10one bar per count of changed bits 128 fair coin flipsmean 64, give or take 5.7 r10 is the histogram on the leftTHE SAME RUNS, AFTER EACH ROUNDdot: average, bar: typical range6416r1r2r3r4r5r6r7r8r9r10
[?] What if we remove one step of AES?
SubBytesShiftRowsMixColumnsAddRoundKeyThe challenge
all four, plus Part 0
QR code to the challenge

? Each of the four steps looks simple. If we drop one of them from all ten rounds, is AES still secure? No. Each removal breaks AES, and each one breaks it in a different way.

[x] Without SubBytes, all the keys move to the end SubBytes: removedShiftRowsMixColumnsAddRoundKey
The idea: L is the ShiftRows and MixColumns of all ten rounds, with no key. Without SubBytes, every round key can be moved past it.
P⊕ K0L1⊕ K1L2⊕ K2…L10⊕ K10CLr: ShiftRows andMixColumns of round r Lr is linear: Lr(x ⊕ k) = Lr(x) ⊕ Lr(k). So a key XOR can jump over Lr. PL1⊕L1(K0)⊕ K1L2⊕ K2…L10⊕ K10CK0 jumps over L1and becomes L1(K0) PL1L2…L10⊕ c_KCL: public, no keyEvery key jumps to the end.Together they form c_K. E(P) = L(P) ⊕ c_K, so C0 ⊕ C1 = L(P0 ⊕ P1)With one known pair (P0, C0), Mallory computes P1 = P0 ⊕ L⁻¹(C0 ⊕ C1). No key needed.
[x] Without SubBytes, AES leaks like a reused pad
SubBytes: removedShiftRowsMixColumnsAddRoundKeyChallenge 1
break it yourself
QR code to challenge 1

? Two ciphertexts, one unknown key. What does Mallory learn? P0 ⊕ P1, just as with a reused one-time pad.

# AES without SubBytes, one unknown key C0 = 8a 67 60 20 2a ae 99 d5 57 04 94 06 8e 58 40 b0 C1 = dc 48 95 42 0a cb 0b 83 2e fe 16 f2 c7 94 f6 1d C0 ⊕ C1 = 56 2f f5 62 20 65 92 56 79 fa 82 f4 49 cc b6 ad undo L, no key = 0c 11 11 15 43 06 45 41 15 54 44 0f 18 01 4f 00 P0 ⊕ P1 = 0c 11 11 15 43 06 45 41 15 54 44 0f 18 01 4f 00 # P0 = "meet me at noon!", P1 = "attack at dawn!!"
  • If she knows or guesses P0, then P1 = (P0 ⊕ P1) ⊕ P0.
  • Worse than the pad: every pair of messages leaks.
The reused one-time pad from earlier: C1 XOR C2 shows both picturesRecall the reused pad: C1 ⊕ C2 = P1 ⊕ P2
[x] Without ShiftRows, the four columns never meet
SubBytesShiftRows: removedMixColumnsAddRoundKeyChallenge 2
break it yourself
QR code to challenge 2
start1 byte round 14 bytes round 24 bytes round 34 bytes …round 94 bytes round 104 bytes THE CIPHERTEXTS: ONLY THE FIRST COLUMN, BYTES 0 TO 3, DIFFERSC5300bda29c7e35fbca37eb5f48c9fbefC'34aeb2a19c7e35fbca37eb5f48c9fbef
  • AES becomes four separate 32-bit ciphers. Mallory asks for 2^32 chosen plaintexts, about 64 GB, and gets the full table for every column.
[x] Without MixColumns, every byte is on its own
SubBytesShiftRowsMixColumns: removedAddRoundKeyChallenge 3
break it yourself
QR code to challenge 3
start1 byte round 11 byte round 21 byte round 31 byte …round 91 byte round 101 byte THE CIPHERTEXTS: ONLY BYTE 0 DIFFERSC5cec2b6ff8595c257f0cc5620b901e90C'b4ec2b6ff8595c257f0cc5620b901e90
  • AES becomes sixteen separate 1-byte ciphers. A 1-byte cipher has only 256 inputs, so 256 chosen plaintexts give Mallory every table.
[x] Without AddRoundKey, there is no key at all
SubBytesShiftRowsMixColumnsAddRoundKey: removedChallenge 4
break it yourself
QR code to challenge 4

? Same plaintext, two different keys. What do we get? The same ciphertext. The key schedule still runs, but nothing uses its output.

# AES without AddRoundKey, plaintext 00 11 22 ... ff key = 00 01 02 03 ... 0f C = 8c 34 c7 e2 35 83 83 ed d3 da 9b 3d 23 cc a3 b3 key = 2b 7e 15 16 ... 3c C = 8c 34 c7 e2 35 83 83 ed d3 da 9b 3d 23 cc a3 b3 # the avalanche still works: flipping bit 0 changes all 16 bytes C' = ec ff 07 2a 6f 4d 84 8a 92 8f 8c 62 a4 a8 86 02
  • Mallory runs InvShiftRows, InvSubBytes and InvMixColumns herself, and reads every message.

Every step has its own job. Remove any one, and AES breaks.

[>] Your turn: break AES four times
  • We give you aes.py, the AES from this lecture. It can skip any one of the four steps.
  • Each challenge removes one step and gives ciphertexts under a secret key. Recover the secret plaintext.
QR code to challenge 1, AES without SubBytesChallenge 1
no SubBytes
QR code to challenge 2, AES without ShiftRowsChallenge 2
no ShiftRows
QR code to challenge 3, AES without MixColumnsChallenge 3
no MixColumns
QR code to challenge 4, AES without AddRoundKeyChallenge 4
no AddRoundKey
[+] Decryption runs the same picture backwards STARTROUNDS 1 TO 9decrypt 9 to 1ROUND 10no MixColumnsENCRYPT, TOP DOWNplaintext⊕ K0SubBytesShiftRowsMixColumns⊕ KrSubBytesShiftRows⊕ K10ciphertext DECRYPT, BOTTOM UPciphertextsame XOR⊕ K10rotate rightInvShiftRowsinverse tableInvSubBytes same XOR⊕ Kr?InvMixColumnsrotate rightInvShiftRowsinverse tableInvSubBytes same XOR⊕ K0plaintext

? How do we undo MixColumns? Multiply each column by the inverse matrix, with rows 0e 0b 0d 09.

[+] AES in practice is fast and unbroken
$ openssl speed -evp aes-128-gcm -bytes 16384 # on this laptop AES-128-GCM 5387100.16k # about 5.4 GB per second, one core
QuestionAnswer today
How fast?Since about 2010, one CPU instruction runs a whole round.
Best attack on the math?The best attack takes 2^126 steps instead of 2^128, so AES is unbroken.
How is it really broken?Software AES that uses lookup tables leaks the key through cache timing (2005).
Quantum computers?Grover's algorithm needs about 2^64 steps for AES-128, so AES-256 keeps the margin.
[!] Wanted: still no GitHub handle from you
WANTED

for not sending us a GitHub handle and password. Last seen: not on the list.

★ Ananya Sanivarapu
★ Arthur Roshal
★ Austin Hearst
★ Donald Bi
★ Ethan Cearlock
★ Genna Gonzaga
★ Jack Bhalla
★ Jake Januzelli
★ Jaxson Robinson
★ Jorim Chua
★ Joshua Parra
★ Linfeng Li
★ Mark Hrytchak
★ Oscar O’Donnell
★ Sydney Kaufman
★ Yanni Pang
★ Zin Nas

No late days for this one. If you cannot share it with us, you may get a 0 in round 1. Send it today.

[+] Two channels: anonymous feedback, and Ed
QR code to the anonymous feedback poll on SlidoAnonymous feedback
too fast, too slow, confusing? say so, no name attached
QR code to the Ed discussion boardEd discussion
questions and answers, during and after class

app.sli.do · edstem.org/us/courses/107281/discussion

[+] Recap: the one-time pad
PLAINTEXT10111101 ⊕ KEY10001111 CIPHERTEXT00110010 Decrypt with the same key: C ⊕ K = P.
Two pictures encrypted with the same pad, XORed together: both show throughThe same pad used twice
  • Perfect secrecy. Without the key, every plaintext of this length is equally likely.
  • The key is as long as the message. It must be truly random, and both sides must already share it.
  • Never reuse it. C1 ⊕ C2 = P1 ⊕ P2, and the pictures show through.
  • No integrity. Flip one bit of C, and the same bit of P flips.
[+] Recap: DES, a Feistel cipher
LiRi Fround key Ki⊕ Li+1 = RiRi+1repeat 16 times, with keys K1 to K16
  • 64-bit block, 56-bit key, 16 rounds.
  • Decrypt with the same rounds, keys in reverse. F never has to be undone.
  • The S-boxes inside F are the only non-linear part.
  • It died of a short key. 2^56 keys: EFF Deep Crack took 56 hours in 1998.
INSIDE F R : 32 bits expand32 → 48 ⊕ Ki : 48 S1 S2 S3 S4 S5 S6 S7 S8 8 S-boxes: 6 bits in, 4 bits out each (the only non-linear step) 48 bits → 32 bits shufflepermute P F(R, Ki) : 32
[+] How the DES expand step turns 32 bits into 48 R = 32 BITS, SPLIT INTO EIGHT GROUPS BY POSITIONbits 1-4bits 5-8bits 9-12bits 13-16bits 17-20bits 21-24bits 25-28bits 29-32 TAKE THE GROUP AT POSITIONS 5-8 (EACH CELL: A BIT VALUE AT ITS POSITION)0pos 51pos 61pos 70pos 81pos 4borrow0pos 9borrow 1pos 40pos 51pos 61pos 70pos 80pos 9 6 bits out per group: positions 4,5,6,7,8,9. Positions 4 and 9 are borrowed, so 16 of the 32 bits are used twice.

The number is the bit’s position in R; the big digit is its value. Expansion copies bits — positions 4 and 9 land in two groups, so each feeds two S-boxes.

[+] Recap: the AES pipeline plaintext16 bytes⊕K0 ONE ROUND1SubBytesthe only non-linear step2ShiftRowsmoves bytes between columns3MixColumnsmixes the 4 bytes of a column4AddRoundKeythe only place the key entersrounds 1 to 9 ROUND 10SubBytesShiftRowsAddRoundKeyno MixColumnsciphertext
[+] Recap: where the round keys come from

Rule: each word is the word four back XOR the word just before it; every fourth word first goes through g.

K0w0 = 00010203w1 = 04050607w2 = 08090a0bw3 = 0c0d0e0f K1g⊕w4 = d6aa74fd ⊕⊕⊕w5 = d2af72faw6 = daa678f1w7 = d6ab76fe K2g⊕w8 = b692cf0b ⊕⊕⊕w9 = 643dbdf1w10 = be9bc500w11 = 6830b3fe ⋮K10w40 = 13111d7fw41 = e3944a17w42 = f307a78bw43 = 4d2b30c5

One 16-byte key becomes 11 round keys: 44 words, 176 bytes. K0 before the rounds, K1…K10 inside.

[?] Recap done. One problem is left. A REAL MESSAGE IS MANY 16-BYTE BLOCKS16 B16 B16 B16 B16 B16 B16 B16 B16 B…a 1 MB file = 65,536 blocks; a web page, a photo, an email — all far more than one block BUT AES ENCRYPTS EXACTLY ONE BLOCK16 bytesAES16 bytesOne block in, one block out.So how do we handle the rest?

? How do we encrypt a message longer than one block? That choice is the mode of operation — and it is where most real failures live.

[ 03 · modes of operation ]

Encrypting more
than one block

AES handles 16 bytes. A file has millions. How we chain the blocks together is where most real failures live.

[?] A block cipher encrypts 16 bytes. Now what? OUR MESSAGE: 21 BYTESSend␣$100␣to␣Bob,␣now AES TAKES EXACTLY 16 BYTES AT A TIMESend␣$100␣to␣Bob,␣nowblock 1: fullblock 2: only 5 bytes QUESTION 1: THE LAST BLOCK IS SHORT,␣now???????????5 bytes11 empty slotsAESneeds all 16What do we put in the 11 slots,so Bob can take them off again? QUESTION 2: THERE IS MORE THAN ONE BLOCKblock 1AESC1block 2AESC2……?1 MB = 65,536 blocks. Each on its own,or linked to the one before?
[?] Idea 1: just fill the gap with zeros IDEA 1: FILL THE EMPTY SLOTS WITH 00ATTACK␣AT␣DAWN000014 bytes+ two 00Bob strips the zeros at the end and gets ATTACK AT DAWN. It works? BUT A MESSAGE IS JUST BYTES, AND ITS LAST BYTE CAN BE 00 (A C STRING, A NUMBER, A FILE)ATTACK␣AT␣DAWN000015 bytes+ one 00the 15th byte is part of the message Bob gets the same 16 bytes both times. Strip one 00 or two? He cannot know.

? Zero padding cannot be undone. The padding has to tell Bob where the message ends. Have we seen a padding that does that?

[?] Hint: remember how MD5 filled its last block? MD5 HAD THE SAME PROBLEM: 64-BYTE BLOCKS, AND “hello” IS 5 BYTES68656c6c6f“hello” 59 empty bytes. What did MD5 put here? 8000 00 00 … 00  (50 bytes)2800000000000000a 1 bitzerosthe length: 40 bits, in 8 bytes 68656c6c6fthe other 59 bytes: padding, thrown awayBob reads the length: 0x28 = 40 bits = 5 bytes, so he keeps exactly the first 5.No guessing, even if the message ends in 00. (The 80 marks the end too; either one is enough.) Write the length down. But MD5 spends 9 bytes on it (80 + 8 bytes of length), and an AES block has 16.

? Can we write it down in one byte? Yes: count the padding bytes (1 to 16), not the message length.

[+] Padding: always fill the block, so it can be undone A BLOCK HOLDS 16 BYTES. OUR MESSAGE IS 14, SO 2 SLOTS ARE EMPTY.ATTACK␣AT␣DAWN02022 padding bytes, each = 02the message THE RULE (PKCS#7): IF n BYTES ARE MISSING, ADD n BYTES, EACH EQUAL TO n.ATTACK␣AT␣SIX03030313 bytes, so 3 padding bytes, each = 03Bob reads the last byte (03), checks that the last 3 bytes are all 03, and strips them.

? Now suppose the message is exactly 16 bytes, a whole block with nothing missing. What does the sender add? A whole extra block of 16 bytes, each equal to 10 (= 16). Padding is never skipped, so the receiver can always strip the last block without guessing.

␣␣␣␣␣␣␣␣1010101010101010 16-byte message + a full padding block
[?] What if we skip padding when the block is full? SUPPOSE WE SKIP PADDING WHEN THE BLOCK IS ALREADY FULLATTACK␣AT␣DAWN0202message B:16 bytes, no padB’s last two bytes happen to be 02 02: binary data, a counter, a version number. MESSAGE A: 14 BYTES, PADDED BY THE RULEATTACK␣AT␣DAWN0202message A:14 bytes + 02 02 The same 16 bytes. Bob reads 02, strips two bytes, and gets A. B lost its end. THE FIX: ALWAYS PAD. B IS FULL, SO IT GETS A WHOLE EXTRA BLOCK OF 10 (= 16)ATTACK␣AT␣DAWN020210101010101010101010101010101010B, 16 bytesa whole padding block: sixteen 10sBob reads 10 = 16, strips one whole block, and gets all of B. A still ends in 02 02. No clash.
[+] ECB, Electronic Code Book: each block alone P1EKC1P2EKC2P3EKC3P4EKC4Each block is encrypted on its own, with the same key: Cᵢ = Eₖ(Pᵢ)

Take a message that repeats every 16 bytes — the phrase attack at dawn!! three times — and encrypt it. -c16 prints one line per block.

$ printf 'attack at dawn!!attack at dawn!!attack at dawn!!' | openssl enc -aes-128-ecb -K 0001..0f | xxd -p -c16 4a7cf5c7bb2d48d42edb56b2d7538507 # block 1: "attack at dawn!!" 4a7cf5c7bb2d48d42edb56b2d7538507 # block 2: same phrase, same ciphertext 4a7cf5c7bb2d48d42edb56b2d7538507 # block 3: the repetition leaks straight through 954f64f2e4e86e9eee82d20216684899 # block 4: the padding block (48 bytes is a whole number of blocks)
[x] ECB shows the shape of the data
The Linux penguin, as a bitmapthe picture
The same bitmap encrypted with ECB: the outline of the penguin is still visibleAES-ECB of the pixels
An image with the words TOP SECRETa secret note
The same note encrypted with ECB: the words TOP SECRET are still readable through the noiseAES-ECB, still readable
  • Large areas of one color become many identical 16-byte blocks, so ECB gives them one repeated ciphertext pattern.
  • Wherever the pixels change, the pattern breaks, so edges survive: the penguin’s outline, and even the words TOP SECRET, are still there.
[x] ECB has no integrity: splice two tokens into one
An attacker
Mallory
Malloryuser=malloryrole=userAlice(admin)user=alicerole=admin forgeduser=malloryrole=admin↑ from Mallory↑ from Alice server decrypts → user=mallory is now ADMIN, and never notices
✓ She HAS both tokens off the wire. Alice is a known admin, so Alice’s 2nd block is role=admin.
✗ She LACKS the key. She reads and checks nothing — she only moves whole blocks.
✎ She FORGES her user block + Alice’s admin block.
Encryption is not integrity. The tokens stayed unreadable, but blocks are interchangeable, so she forges a valid one with no key. ECB, CBC and CTR all need a MAC.
[x] 2013: Adobe leaked 153M ECB-encrypted passwords

153M accounts, passwords in 3DES-ECB instead of hashed, and the password hints stored in the clear.

THREE USERS, ONE SHARED CIPHERTEXT BLOCK. THEIR HINTS:2fca...9e01“rhymes with sixty-five”2fca...9e01“1 through 6”2fca...9e01“the most common one” same blockthe hints saywhich one 123456 used by 1,911,938 accounts
xkcd 1286: the Adobe leak drawn as a crossword, where shared hints reveal shared passwordsxkcd 1286, “Encryptic”
[>] We know the whole pipeline. Time to break it. WHAT WE BUILT: MESSAGE TO CIPHERTEXTmessagepadPKCS#716-byte blockssplitAESkey Kciphertext NOW SWITCH SIDES. WHAT DOES MALLORY HAVE?HAS9f3a 0c…71be 5d…0d42 e8…c6a1 27…every ciphertext block, off the wireDOES NOT HAVEkey KWANTSthe messageget it back, with no key? FOUR CAPTURE-THE-FLAG GAMES. SAME GOAL EVERY TIME: RECOVER THE HIDDEN MESSAGECTF #1 · ECBnowCTF #2 · CBCafter CBCCTF #3 · CTRafter CTRCTF #4 · 2·AESthe finale

? Before we attack: what is Mallory actually allowed to do? Only watch ciphertexts, or can she make the server encrypt and decrypt things for her? That is the threat model, and it decides which attacks are even possible.

[+] How much can the attacker do? ALICE ENCRYPTS WITH A SECRET KEY. MALLORY NEVER GETS THE KEY. WHAT CAN SHE DO, AND WHAT IS SHE AFTER?weakstrong 1  Ciphertext only9f3a…71be…0d42…e.g. sniffing café Wi-Figoal: read the message, or find the key 2  Known plaintext%PDF…9f3a…+?0d42…e.g. file headers, boilerplategoal: find the key, then read everything 3  Chosen plaintext (CPA)p (she picks)serverE(p)e.g. a form or an APIgoal: read a message she did not choose 4  Chosen ciphertext (CCA)c (she picks)serverD(c)e.g. a login or a token checkgoal: read the one target ciphertext

The more the attacker can do, the harder security gets. A modern cipher must stay safe even in the strong models — and today’s four CTFs span the ladder: chosen-plaintext (ECB, CTR), chosen-ciphertext (CBC), and known-plaintext (2×AES).

[>] CTF #1: poke the ECB oracle from your browser

Threat model · chosen-plaintext (CPA) you choose the input, and the server encrypts input ‖ flag for you — exactly the CPA oracle from the threat-model ladder.

THE SERVER GLUES YOUR PREFIX IN FRONT OF THE HIDDEN FLAG, THEN AES-ECB ENCRYPTS IT ALLyour prefixyou pick‖the hidden flagyou never see itAES-ECBsecret key Kciphertext, as hexciphertext = AES-128-ECB( Key , PREFIX ‖ FLAG )

Try it yourself: open http://165.22.35.184:54321 and send two different prefixes. Watch the ciphertext:

...54321/oracle?prefix=41…41 (32 A’s)
85ced7ccdbc0aab01d972d6610b25bc1 # your A-block
85ced7ccdbc0aab01d972d6610b25bc1 # same → same!
dafca09b5531b721bcb829515f901aa2 # flag
7c3a873caafb2e21b639920eb7c5fbdb # flag
...54321/oracle?prefix=42…42 (32 B’s)
4493eb2b4fe8f1fd3373b51f3a16041d # your B-block
4493eb2b4fe8f1fd3373b51f3a16041d # same → same!
dafca09b5531b721bcb829515f901aa2 # SAME flag!
7c3a873caafb2e21b639920eb7c5fbdb # SAME flag!
[+] Pin one byte, guess it, then slide the window SEND 15 A’s. THE FIRST BLOCK IS NOW 15 KNOWN BYTES + 1 SECRET BYTE.AAAAAAAAAAAAAAA?↑ only this byte is unknown THE SERVER’S REAL BLOCK FOR 15 A’s IS YOUR TARGET. TRY EACH GUESS BYTE, COMPARE THE WHOLE BLOCK.targete24534a19cb8…the block the server returned15A + ‘a’10b7403c7712…✗ different block15A + ‘Q’e01e02f58841…✗ different block 15A + ‘S’e24534a19cb8…✓ identical to targetSame ciphertext block ⇒ same input ⇒ the hidden byte is ‘S’. At most 256 guesses. NEXT: SEND 14 A’s. THE KNOWN ‘S’ SHIFTS IN, THE NEXT SECRET BYTE IS ALONE. REPEAT FOR THE WHOLE FLAG.AAAAAAAAAAAAAAS?← guess it the same way → ‘E’, …
[+] CBC, Cipher Block Chaining: chain the blocks IVP1⊕EKC1P2⊕EKC2P3⊕EKC3random, publicsent with the ciphertextprevious ciphertextfeeds the next block Cᵢ = Eₖ(Pᵢ ⊕ Cᵢ₋₁), with C₀ = IV: XOR the previous ciphertext into the plaintext, then encrypt.
$ printf 'YELLOW SUBMARINEYELLOW SUBMARINE' | openssl enc -aes-128-cbc -nopad -K 0001..0f -iv 3a9f..540b | xxd -p -c16 60e35950a5e45dc362f7a3b6debf5eb8 # block 1 (the same 16 bytes, twice) 319ef88e83437a41aa7fade76001f230 # block 2: same input, different output
[+] CBC decryption undoes the chain ENCRYPT: Cᵢ = Eₖ(Pᵢ ⊕ Cᵢ₋₁), WITH C₀ = IV. P ON TOP, C BELOW.IVP1⊕EKC1P2⊕EKC2P3⊕EKC3 DECRYPT: Pᵢ = Dₖ(Cᵢ) ⊕ Cᵢ₋₁. SAME LAYOUT, THE ARROWS RUN UPWARD.IVP1⊕DKC1P2⊕DKC2P3⊕DKC3
[x] CBC has no integrity: flip a byte, edit the message RECALL DECRYPTION: THE PREVIOUS CIPHERTEXT IS XORED STRAIGHT ONTO THE PLAINTEXT. NO KEY IN THAT STEP.IVP1⊕DKC1P2⊕DKC2C1 landson P2P₂ = Dₖ(C₂) ⊕ C₁change a byte of C₁→ the same byte of P₂ changes by exactly the same amount(and P₁ turns to garbage) C1, THE FIRST CIPHERTEXT BLOCK85c954a0652f0d273783d1593e878398P2 = D(C2) ⊕ C1, so byte 8 of P2 depends on byte 8 of C1amount=$100.00;; FLIP BYTE 8 OF C1: 37 ⊕ (‘1’ ⊕ ‘9’) = 37 ⊕ 08 = 3f85c954a0652f0d273f83d1593e878398the same byte of P2 flips by the same 08: ‘1’ becomes ‘9’amount=$900.00;;

Block 1 turns to garbage, but the attacker chose the change in block 2. Encryption is not integrity.

[+] The IV must be unpredictable, not just unique 1. Eₖ IS DETERMINISTICXEKYXEKYsame Y, twice 2. THE IV IS THE ONLY RANDOMNESSP⊕IV₁EKC₁P⊕IV₂EKC₂C₁ ≠ C₂ 3. PREDICTABLE IV: MALLORY CANCELS ITsaw: IV = A, Cᵀguess: Mnext IV: BM ⊕ A ⊕ B⊕IV = BM ⊕ AEKC?← B cancels: Eₖ sees M ⊕ AC? = Cᵀ ⇔ the guess is right. One query, one guess checked. 4. RANDOM IV: NOTHING TO CANCELkey used once → IV = 0 is finekey reused → fresh random IV, sent alongcounter → encrypt it first (BitLocker)

Predictable IV = deterministic encryption in disguise.

[x] 2004: Diebold voting machines, CBC done wrong EVERY VOTE RECORD ON THE MEMORY CARD WAS ENCRYPTED LIKE THISIV = 0always zerorecord 1⊕DESC1record 2⊕DESC2key: “F2654hD4”, hardcodedin the source since 1998IV: NULL, so always zerono MAC: nothing checks the file→ three of the rules broken at once
[>] CTF #2: crack CBC with a padding oracle

Threat model · chosen-ciphertext (CCA) you choose ciphertexts and the server decrypts them, leaking one bit (valid padding?) — the strongest model, a CCA oracle.

HOW THE SERVER BUILDS THE TOKEN (YOU NEVER SEE THIS SIDE)secret messagepadCBC encryptkey K + a random IVtoken THE TOKEN YOU RECEIVE = IV, THEN THE CIPHERTEXT BLOCKSIVC1C2C3…16 bytes, in the clear16 bytes eachthe secret, encrypted — you cannot read it
http://165.22.35.184:54322/token
90b82a5b…9ad574a7   # the token: IV ‖ ciphertext
http://165.22.35.184:54322/verify?ct=<your hex>
valid / invalid  # the PKCS#7 padding only — one bit

Goal: recover the secret, with no key. That single yes/no padding bit is the whole lever. The next slides show how.

[+] The lever: we control what is XORed onto P Pthe plaintext⊕prevwe send this blockI = Dₖ(C)unknown, fixedDKneeds the keyCthe target blockP = I ⊕ prevWe pick prev, so we XOR anything onto P.Blind, no key.But I is unknown, so we cannot aim it yet.
[+] Read one byte: force the padding to 01 Pthe plaintext⊕prevwe send this blockI = Dₖ(C)unknown, fixedDKneeds the keyCthe target block?gFOCUS ON THE LAST BYTE (g = prev[15])P[15] = I[15] ⊕ gg = 00invalidg = 01invalid……g = 7avalidvalid ⇒ P[15] = 01 ⇒ I[15] = 7a ⊕ 01 = 7breal byte = I[15] ⊕ realprev[15] = 7b ⊕ 1e = 65 = “e”
[+] Peel the block, then the whole message Pthe plaintext⊕prevwe send this blockI = Dₖ(C)unknown, fixedDKneeds the keyCthe target block0202gSLIDE THE WINDOW, RIGHT TO LEFTbyte 15 known → now aim for padding 02 02set g[15] so P[15] = 02, then walk g[14]until valid → that pins I[14].peel 01, then 02 02, then 03 03 03 …until the whole block’s I is known.next block: its previous ciphertext blockplays the role of prev.the whole message falls out, block by block
[!] Three announcements
  • Feedback. The anonymous Slido is still open. Too fast, too slow, something did not land: scan the code and tell us.
  • Round 1 preparation is done: we are in the attack phase! We already have a lot of attacks submitted. We are validating your PRs and your attacks now, and the leaderboard will be up here soon.
  • Midterm 1: this Wednesday, same time as lecture. The problems are the ones from Homework 1 with the numbers changed, and only a subset of them.
QR code to the anonymous feedback poll on SlidoAnonymous feedback
no name attached
[+] Recap: PKCS#7 padding A: 14 BYTES, 2 MISSING → THE RULE ADDS 02 02ATTACK␣AT␣DAWN0202B: 16 BYTES OF REAL DATA, NOTHING MISSING. IT HAPPENS TO END IN 02 02ATTACK␣AT␣DAWN020210101010101010101010101010101010+ 0x10 × 16a whole extra block

? B is already full. Why add a whole block of 0x10 to it? Without it, A and B are the same 16 bytes on the wire. Bob reads 02, strips two bytes, and B silently loses real data. Always pad, so Bob always strips exactly one run.

[+] Recap: ECB, each block alone P1EKC1P2EKC2P3EKC3P4EKC4Each block is encrypted on its own, with the same key: Cᵢ = Eₖ(Pᵢ)
A pixel-art crewmate, one AES block per pixelplaintext
The same picture under AES-ECB: every colour is swapped for another, but the crewmate is fully visibleAES-ECB · still sus
  • Equal blocks in, equal blocks out. The shape leaks.
  • Blocks can be cut and pasted. No chaining, no integrity.
[+] Recap: CBC, chain the blocks behind a random IV IVP1⊕EKC1P2⊕EKC2P3⊕EKC3random, publicsent with the ciphertextprevious ciphertextfeeds the next block
A pixel-art crewmate, one AES block per pixelplaintext
The same picture under AES-CBC: uniform noiseAES-CBC · noise
  • IV: random, public, sent with C. Repeats vanish.
  • Serial encryption, padding, and bit-flips still land on P.
[+] CTR, Counter mode: a stream cipher from AES nonce ‖ 0EK⊕P1C1 nonce ‖ 1EK⊕P2C2 nonce ‖ 2EK⊕P3C3 Encrypt nonce ‖ counter to make a keystream, then XOR. Decryption is the same. No padding.
  • Fully parallel, no padding, and only needs E, never D.
  • Never reuse a (key, nonce) pair: same keystream, so C1 ⊕ C2 = P1 ⊕ P2.
[?] CTR on the wire: how does Bob get the nonce? ALICE THE INTERNET BOB pick a fresh nonce Nrandom, or a message counter EK(N‖0), EK(N‖1), EK(N‖2)keystream, needs K⊕P1 P2 P3C1 C2 C3 NC1C2C3in the clearciphertextEve sees N and Cwithout K, N is useless WHY NO PADDING: THE LAST BLOCK, BYTE BY BYTE (P3 = “dawn”, 4 BYTES)EK(N‖2)3f9a07c212 bytes thrown away⊕ P3dawn= C35bfb70acEK only ever sees N‖2,always a full 16 bytes.|C| = |P| = 36 bytes N travels in the clearread N off the packetno secret needed to find it EK(N‖0), EK(N‖1), EK(N‖2)keystream, needs K⊕P1 P2 P3C1 C2 C3

N is public, K is the secret. Never reuse a (K, N) pair.

[x] Nonce reused: the one-time pad, used twice SAME K, SAME N → SAME SN ‖ 0, 1, 2…EKkeystream S P1P2⊕⊕C1C2 EVE, ON THE WIRE⊕P1 ⊕ P2S cancelsno key needed
A crewmate silhouette, one byte per pixelP1
A space invader, one byte per pixelP2
C1: random-looking noiseC1 = P1 ⊕ S
C2: random-looking noiseC2 = P2 ⊕ S
C1 XOR C2: the crewmate on the left and the invader on the right, both visibleC1 ⊕ C2 = P1 ⊕ P2
[>] CTF #3: crack CTR

Threat model · chosen-plaintext (CPA) the server encrypts whatever you send.

YOUSERVER165.22.35.184:54323one key K, one nonce N, reused for every requestAES-128-CTRKS = EK(N ‖ 0), EK(N ‖ 1), …browser GET /tokensecret⊕KS=tokencfe1a9de…15572552token GET /encrypt?msg=<your hex>your bytes⊕KSthe SAME KS=replyyour bytes ⊕ KSreply

Goal: recover the secret, with no key.

[+] Why the keystream repeats, and how to steal it CTR MAKES ITS KEYSTREAM FROM (KEY, NONCE, COUNTER). THE MESSAGE NEVER ENTERS.nonce ‖ 0nonce ‖ 1counter blocksEKKS₀KS₁ …the keystream KSSame key + same nonce ⇒ the exact same KS. SO BOTH OF THESE USE ONE AND THE SAME KEYSTREAM:secret⊕KStoken← the server made thisyour bytes⊕KS/encrypt reply← the same KS again HINT: SEND msg = 0000…00 TO /encrypt00 00 00 … 00⊕KSreply = KS← 0 ⊕ KS = KS: the keystream itself, for free token⊕KSsecret← token ⊕ KS = secret. No key needed.
[>] CTF #4: two keys are not twice the security

Threat model · known-plaintext (KPA) you get known plaintext / ciphertext pairs.

THE SERVER ENCRYPTS TWICE, WITH TWO INDEPENDENT 20-BIT KEYS · AES KEY = 13 ZERO BYTES ‖ 3-BYTE v, v < 2²⁰PEk1k1: 20 bitsXmiddle valueEk2k2: 20 bitsCC = Ek2(Ek1(P)) YOU ARE GIVENtwo known pairs: (P, C) and (P2, C2)C*: the padded secret, ECB, block by blockBrute force on both keys: 2²⁰ × 2²⁰ = 2⁴⁰ guesses. Hopeless. But you do not have to.

Try it: http://165.22.35.184:54324/challenge returns both pairs and C*.

AES for you: http://165.22.35.184:54324/aes.py gives make_key(v), encrypt, decrypt.

Goal: recover both keys, then decrypt C*.

[?] Hint 1: the middle is reachable from both ends

💡 Hint 1

FROM THE LEFT: ONE KEY REACHES THE MIDDLEPEk12²⁰ guessesXX = Ek1(P) = Dk2(C) FROM THE RIGHT: ONE KEY REACHES THE SAME MIDDLECDk22²⁰ guesses

? Both keys at once: 2⁴⁰. One side at a time? 2²⁰ ≈ a million AES calls per side. A few minutes with the pure-Python aes.py; 2⁴⁰ would take about five years.

[?] Hint 2: trade memory for time

💡 Hint 2

1 · LEFT SIDE: EVERY k1, ONCEPEk1all 2²⁰ k1store X → k1A DICTIONARY: X → k13a9f…c1 → k1 = 0004eb07d…12 → k1 = 7b3a15c21…9e → k1 = 0f10c⋮2²⁰ entries, a few MB 2 · RIGHT SIDE: ONE k2 AT A TIMECDk2each k2is Dk2(C) in there?a hit = both keys

? A hit. What do you know? k1 from the entry, k2 from the loop. Check them on (P2, C2), then decrypt C*.

[+] Meet in the middle: 2⁴⁰ becomes 2²¹ 1 · BUILD: WALK EVERY k1 FROM THE LEFT ENDPEk1for all 2²⁰ k1X = Ek1(P)store X → k1TABLE: X → k13a9f…c1 → k1 = 0004eb07d…12 → k1 = 7b3a15c21…9e → k1 = 0f10c⋮2²⁰ rows, built oncememory for time 2 · MEET FROM THE RIGHT ENDCDk2for all 2²⁰ k2X′ = Dk2(C)X′ in table?lookupno: next k2yes: k1, k2 foundk1 = table[X′] 3 · CHECK ON (P2, C2), THEN DECRYPT C* BLOCK BY BLOCK AND STRIP THE PADDINGC*Dk2Dk1the secret2²⁰ + 2²⁰ ≈ 2²¹ steps,not 2⁴⁰. Two keys buy one bit.
[ 04 ]

When is a cipher
“secure”?

We have said “broken” and “secure” loosely. Now we pin down what a modern cipher must promise, and turn it into a precise game.

[?] What should “secure” even mean?

? A weather station encrypts one bit a day, 0 = rain, 1 = shine, with a strong cipher. Secure?

A DETERMINISTIC CIPHER: SAME WEATHER, SAME BYTES. ONLY TWO CIPHERTEXTS EVER APPEAR0 = rainEₖc₁ = 4f…1 = shineEₖc₂ = a9… MALLORY WATCHES A WEEK: TWO VALUES, REPEATING4f…Mona9…Tue4f…Wed4f…Thua9…Fri4f…Sata9…Sun one glanceTuesday: she looks out.Sunny, so a9… = shine …and 4f… must be rain:she reads the whole week

The bar: a ciphertext leaks nothing, not even whether two messages are equal. So encryption must be randomized.

[?] The game: which message was encrypted? CIPHERTEXT-ONLY: THE WEAKEST ATTACKER. SHE JUST SEES c.Malloryno key, efficientChallengerholds the key K 1. two messages of her choice: m₀ and m₁ b2. coin: b 3. one ciphertext: c = Eₖ(mb) 4. her guess: b′ she wins if b′ = b. guessing blindly wins half the time.

Secure means: nobody can do noticeably better than 1/2.

[+] Chosen plaintext: she also gets an encryption oracle CHOSEN-PLAINTEXT (CPA): A STRONGER ATTACKER, WITH AN ORACLEMalloryno key, efficientChallengerholds the key K “encrypt x for me” — any x, as often as she likesEₖ(x) ↓ then the same game as beforem₀ and m₁ bcoin: b c = Eₖ(mb) guess b′

That oracle is the chosen-plaintext power. Still stuck at 1/2? Then it is CPA-secure.

[x] Any deterministic cipher loses the game Malloryno key, efficientChallengerholds the key K 1. oracle: “encrypt m₀”c₀ = Eₖ(m₀) 2. challenge: m₀ and m₁bcoin: b c = Eₖ(mb) 3. if c = c₀ then b′ = 0, else b′ = 1 deterministic ⇒ Eₖ(m₀) is always the same bytes ⇒ c = c₀ exactly when b = 0

She wins every time. ECB is exactly this. Fix: a fresh IV or nonce, every time.

[+] Advantage: how far from a coin flip? HOW OFTEN DOES SHE WIN? MEASURE THE DISTANCE FROM A COIN FLIP01/21 a coin flip: secure always right: broken advantage Advantage = | Pr[ b′ = b ] − 1/2 | CPA-secure ⇔ the advantage is negligible for every efficient Mallory

“Negligible”: no better than 1/2 + 2−128, in practice.

[>] CTF #5: the final boss of symmetric encryption
BEAST

Not a toy this time. The bug you are about to exploit shipped in TLS 1.0 and sat in every browser for twelve years.

1999TLS 1.0 shipsCBC with a chained IV2002the warningRogaway, Möller: flaw noted2006the fixTLS 1.1: fresh IV per record2011BEASTcookies decrypted, live

Same bug, smaller scale: beat our server 20 times in a row.

[+] CTF #5: our encryption scheme MESSAGE 1: PAD INTO BLOCKS P1 P2, THEN AES-128-CBC UNDER A RANDOM IVP1P2IV₁random⊕EKC1⊕EKC2 reply 1 = hex stringIV₁C1C2 MESSAGE 2: THE SAME, EXCEPT ITS IV IS THE LAST BLOCK OF REPLY 1P1P2IV₂= C2⊕EKC1⊕EKC2reply 2 = hex stringIV₂C1C2the last block you received becomes the next IV

Real AES-CBC, fresh-looking bytes every time. But the next IV is public before it is used.

[>] CTF #5: the server, and how to talk to it

Threat model · chosen-plaintext (CPA) plain HTTP GET, all bytes as hex. GET /help prints the rules.

YOUR SCRIPTSERVER165.22.35.184:54325pick s=<random>on EVERY callone AES key · one IV chain per s= GET /encrypt?msg=<hex>&s=<name>reply (hex):IVC1C2the IV comes back GET /challenge?m0&m1&s=<name>reply (hex):IVC1C2b is hidden here GET /guess?b=0 (or 1)&s=<name>“correct, streak k/20” / “wrong, 0”20 → flag GET /sourcecpa_server.py, the whole fileno secrets

⚠ add &s=<name> to every call — the server keeps one IV chain per name, so without it you collide with other students. Pick a random name.

[?] Hint 1: the IV is no longer a surprise

💡 Hint 1

THE REPLY YOU JUST RECEIVED (hex)IVC1C2C3last block = your next IVyou hold it before you send anything the IV is the ONLY randomness in CBC — predict it, and the cipher is deterministic to you P1⊕IV (known)EKC1C1 = EK( P1 ⊕ IV ) — you choose both sides

? If you already know the IV, what has happened to the randomness that was supposed to protect each message? It is gone. A known IV means you control P1 ⊕ IV — set it to zero by sending P1 = IV, and encryption behaves like a deterministic one.

[?] Hint 2: cancel the IV, then compare

💡 Hint 2

STEP 1 · ONCE: SEND msg = IV, SO THE AES INPUT IS ZEROmsg = IV⊕⊕ IV = 0EKT = first block = EK(0)keep T for the whole game STEP 2 · EACH ROUND: CHALLENGE WITH m0 = IV, m1 = ANYTHING ELSEchallenge reply cblock 1= T ?yesguess b = 0noguess b = 1/guess, repeat 20×

? The challenge came back with first block equal to T. Which message did the server encrypt? m0, so b = 0. Otherwise b = 1. Right every round — advantage 1.

[/] CTF #5 solution: the program SETUP, ONCE/encrypt any bytesiv = last block of reply/encrypt msg = ivT = reply block 1 EACH ROUND (REPEAT UNTIL STREAK = 20)/challenge m0=iv, m1=…block1 = T ?yesb = 0nob = 1/guess biv = reply tailnext round, with the new iv

The whole fix for the real world: a fresh, unpredictable IV per message (random, or encrypt a counter) — which is why TLS 1.1 changed exactly this.

[!] Four announcements
  • Feedback, again. The anonymous Slido is still open. Scan the code and tell us what is not working.
  • Homework 1 and Midterm 1 are being graded. We will post the scores as soon as grading is done.
  • Important: 12 PRs did not pass our validation. We are cross-checking each one before anything is final. Check your email carefully this week.
  • We are building the leaderboard now. We have already received over 50 attacks!
QR code to the anonymous feedback poll on SlidoAnonymous feedback
no name attached
[+] Recap: AES in counter mode (CTR) nonce ‖ 0EK⊕P1C1 nonce ‖ 1EK⊕P2C2 nonce ‖ 2EK⊕P3C3 Encrypt nonce ‖ counter to make a keystream, then XOR. Decryption is the same. No padding.
  • Fully parallel, no padding, and only needs E, never D.
  • Never reuse a (key, nonce) pair: same keystream, so C1 ⊕ C2 = P1 ⊕ P2.
[+] Recap: the CPA game CHOSEN-PLAINTEXT (CPA): A STRONGER ATTACKER, WITH AN ORACLEMalloryno key, efficientChallengerholds the key K “encrypt x for me” — any x, as often as she likesEₖ(x) ↓ then the challengem₀ and m₁ bcoin: b c = Eₖ(mb) guess b′

That oracle is the chosen-plaintext power. Still stuck at 1/2? Then it is CPA-secure.

[?] Two goals, two tools — now put them together ENCRYPTION → CONFIDENTIALITYmsgEncryptciphertextEve sees it, learns nothing MAC (HASH-BASED) → INTEGRITYmsgMACtagany change breaks the tag

? Encryption hides the contents; a MAC catches tampering. A real channel needs both at once. How would you combine an encryption scheme and a MAC?

[?] Attempt 1: Encrypt-and-MAC

The obvious idea: encrypt the message, MAC the message, send both.

msgEncrypt(K1)ciphertext CMAC(K2, msg)tagthe tag is computed over the plaintext send: C ‖ tag

Looks right: C keeps the message secret, the tag proves integrity. So what could go wrong?

[x] Why it fails: the tag leaks equality Malloryno key, efficientChallengerholds K1, K2 1. oracle: “encrypt m₀”C₀ ‖ tag₀ = MAC(m₀) 2. challenge: m₀ and m₁bcoin: b C ‖ tag = MAC(mb) 3. if tag = tag₀ then b′ = 0, else b′ = 1 MAC is deterministic ⇒ MAC(m₀) is always the same ⇒ tag = tag₀ exactly when b = 0

Same plaintext → same tag. Mallory wins the game: Encrypt-and-MAC is not CPA-secure.

[?] Attempt 2: MAC-then-Encrypt

Hide the tag too: MAC the message, then encrypt the message and the tag together. Old SSL/TLS did exactly this.

SENDER: MAC FIRST, THEN ENCRYPT EVERYTHING msg MAC(K2) msg ‖ tag Encrypt(K1) C the tag is hidden,so attempt 1 dies RECEIVER: MUST DECRYPT BEFORE IT CAN CHECK ANYTHING C from Mallory Decrypt(K1) first strip padding check the tag runs on unverified bytes “bad padding”“bad MAC” Mallory flips one byte of C, resends, and reads which error comes back “bad MAC” means: the padding after my flip was valid 1 reply = 1 bit of plaintext ~256 tries per byte, then the whole message this was our CBC CTF #2
  • It happened to SSL/TLS: padding oracle (2002), Lucky Thirteen (2013, timing only), POODLE (2014). And it is not secure in general (Krawczyk, 2001).
QR code to Krawczyk 2001, The order of encryption and authentication for protecting communicationsKrawczyk, CRYPTO 2001
[/] Attempt 3, the fix: Encrypt-then-MAC ENCRYPT-THEN-MAC: MAC THE CIPHERTEXT, NOT THE MESSAGEmsgEncrypt(K1)ciphertext CMAC(K2, C)tag send C ‖ tagtag is over C:it leaks nothingverify tag first,then decrypt

MAC the ciphertext, not the message. Proven secure for any CPA-secure cipher and secure MAC. Better still, use one AEAD mode like AES-GCM.

QR code to Bellare and Namprempre 2000, Authenticated Encryption: Relations among notions and analysis of the generic composition paradigmBellare & Namprempre, ASIACRYPT 2000
[ 05 ]

Public-key
cryptography

Everything so far needed a shared secret key. But how do two strangers agree on one over an open network? This part removes that assumption.

[?] Locking without sharing a key first BOB MAKES A MATCHED PAIR — PUBLISHES ONE HALF, KEEPS THE OTHERKeyGenPKpublic: hand to anyoneSKsecret: Bob only ALICEBOBmessagelock · PKcopen wireunlock · SKmessage sees csees PKShe has PK and c — can lock,never unlock. No secret shared first.

The public key is an open padlock Bob gives away. Anyone can snap it shut; only his private key opens it.

[+] Use 1: send ZZ a private message

PK = ZZ’s public key (everyone has it) · SK = ZZ’s private key (ZZ only).

SETUP, ONCE: ZZ PUBLISHES THE PUBLIC KEY ZZ KeyGen PK SK post it on the course page every student has PK ZZ keeps SK and never sends it anywhere a studentEncrypt · PKcwireDecrypt · SKZZ reads
Can AES do this? Yes — but every student would need a key shared with ZZ in advance. 300 students, 300 secret keys to set up. Public key needs none.
[+] Use 2: ZZ posts a signed announcement

ZZ signs with the private key; every student checks with ZZ’s public key.

SETUP, ONCE: ZZ PUBLISHES THE PUBLIC KEY ZZ KeyGen PK SK post it on the course page every student has PK ZZ keeps SK and never sends it anywhere ZZSign · SKm, swireVerify · PK✓ really ZZ
Can AES do this? No — a MAC needs a shared key, so any student who can verify could also forge it, and could never prove to others it was ZZ. Signatures give public, non-repudiable proof.
[+] Use 3: a student and ZZ agree on a key

They never met, yet end up with the same secret k.

SETUP, ONCE: ZZ PUBLISHES THE PUBLIC KEY ZZ KeyGen PK SK post it on the course page every student has PK ZZ keeps SK and never sends it anywhere 1 · THE STUDENT PICKS A KEY AND SENDS IT LOCKED a student random k Encrypt · PK c open wire Decrypt · SK ZZ has k Eve sees c, never k 2 · BOTH SIDES NOW SHARE k: SWITCH TO FAST AES student · k AES-CTR(k): the real data ZZ · k
Can AES alone do this? No: it needs a secret already shared. Public key is 100–1000× slower, so it only sets up k. TLS, SSH and PGP all start this way.
[+] A public-key scheme, precisely THREE ALGORITHMS, AND HOW THEY CONNECTKeyGenPKeveryoneSKsecret MEncrypt(PK)Copen wireDecrypt(SK)M Correctness: Decrypt(SK, Encrypt(PK, M)) = M

Trapdoor: easy forward with PK, infeasible to invert without SK. Even given PK and C, Mallory must learn nothing.

RSA

Rivest · Shamir · Adleman, 1977 — the first public-key scheme you can actually run

It rests on just two facts of number theory.

Fact 1 · φ(pq) = (p−1)(q−1) Fact 2 · aφ(n) ≡ 1 (mod n)
[1] Fact 1: Euler’s totient φ(n)
φ(n) = how many numbers i in [1, n] share no factor with n, that is, gcd(n, i) = 1. Green = counted.
n = 71234567φ(7) = 67 is prime: everything but 7 n = 812345678φ(8) = 4only the odd ones n = 9123456789φ(9) = 6not 3, 6, 9 n = 1012345678910φ(10) = 4not even, not 5 n = 12123456789101112φ(12) = 4not 2s, not 3s

For a prime p, nothing below p shares a factor with it, so φ(p) = p − 1.

[?] Counting φ(n) is hopeless for RSA. Is there a shortcut?

An RSA modulus has about 600 digits, so we cannot count from 1 to n. But RSA picks n = p·q with two primes we know. Count a few small ones:

n= p·qφ(n), by counting
62·32
102·54
153·58
213·712
355·724

? So for n = p·q, what is φ(n)? φ(pq) = (p−1)(q−1). Check: 2 = 1·2, 4 = 1·4, 8 = 2·4, 12 = 2·6, 24 = 4·6.

[1] Fact 1, on one example: n = 15 = 3·5
Claim: for n = p·q (p, q prime), φ(n) = (p−1)(q−1). Here that predicts (3−1)(5−1) = 8. Green = coprime to 15.
1gcd=12gcd=13shares 34gcd=15shares 56shares 37gcd=18gcd=19shares 310shares 511gcd=112shares 313gcd=114gcd=1153 and 5

3, 6, 9, 12, 15: n/p = q = 55, 10, 15: n/q = p = 315 counted twice

φ(15) = 15 − 5 − 3 + 1 = 8: remove both kinds of multiples, then add back 15, which we removed twice. That is pq − q − p + 1 = (p−1)(q−1).

[1] Why φ(pq) = (p−1)(q−1)

Count what is not coprime, then subtract. A number fails only if it is a multiple of p or of q.

123×345×56×3789×310×51112×31314153 & 5 1. all numbers 1..pqpq = 152. − multiples of pq = 5 (3,6,9,12,15)3. − multiples of qp = 3 (5,10,15)4. + overlap counted twicepq itself = 15. φ(pq) = pq − p − q + 1= (p−1)(q−1)check: 15 − 3 − 5 + 1 = 8 = 2·4 ✓

Inclusion–exclusion on “multiple of p or q” → (p−1)(q−1).

[2] Fact 2: Euler’s theorem
If gcd(a, n) = 1, then aφ(n) ≡ 1 (mod n).
n = 10, φ(10) = 4, a = 3: WATCH 3ᵇ mod 103¹ = 3×33² = 9×33³ = 7×33⁴ = 1back to the start: cycle length 4 = φ(10) n = 14, φ(14) = 6, a = 3: WATCH 3ᵇ mod 143¹ = 3×33² = 9×33³ = 13×33⁴ = 11×33⁵ = 5×33⁶ = 1back to the start: cycle length 6 = φ(14) n = 22, φ(22) = 10, a = 7: WATCH 7ᵇ mod 227¹ = 7×77² = 5×77³ = 13×77⁴ = 3×77⁵ = 21×77⁶ = 15×77⁷ = 17×77⁸ = 9×77⁹ = 19×77¹⁰ = 1back to the start: cycle length 10 = φ(22)

Each one is back at 1 at the power φ(n). That is the engine that makes RSA return to the message. The condition matters: 2 shares a factor with 10, and 2, 4, 8, 6, 2, … never reaches 1.

[2] Why aφ(n) ≡ 1, step 1: ×a only shuffles coprimeto 10 1379 × 3,mod 10 1379 ×3 = 3×3 = 9×3 = 21 → 1×3 = 27 → 7

? Every number is hit exactly once: no two arrows land on the same target. Why can that never happen?

[2] Why no two arrows collide: suppose they do
SUPPOSE x ≠ y, both coprime to n, hit the same target:a·x ≡ a·y (mod n)n divides a·x − a·y = a·(x − y)move everything to one sideso n divides (x − y)a and n share no factor, so every factor of n must be inside (x − y)so x − y is a multiple of none of …, −2n, −n, 0, n, 2n, …x ≠ y, so x − y ≠ 0we assumed two different numbers1 ≤ x, y ≤ n, so −n < x − y < nthe gap between two numbers in [1, n] is at most n − 1✗ the only multiple of n in (−n, n) is 0. Contradiction.
WHAT WE ASSUMED x y ? MULTIPLES OF n −2n−n0n2n x − y must be in here not 0, since x ≠ y

Also, a·x is still coprime to n, so no arrow leaves the set.

[2] Step 2: multiply both rows, then simplify THE ARROWS 3·13·33·73·9 ≡≡≡≡ 3917 THE MAPPING RESULTS (BOTTOM ROW) each product, reduced mod 10,is its mapping result 3·1 · 3·3 · 3·7 · 3·9 ≡ 3 · 9 · 1 · 7 multiply both rows: same numbers, so same product (mod 10) 3⁴ · (1·3·7·9) ≡ 1·3·7·9 (mod 10) simplify: collect the four 3s on the left, reorder the right 1·3·7·9 = 189 ≡ 9, and 9·9 = 81 ≡ 1: multiply both sides by 9. 3⁴ ≡ 1 (mod 10)

Nothing was special about 10 and 3: for any a with gcd(a, n) = 1, shuffle, multiply, cancel gives aφ(n) ≡ 1.

[+] RSA: build the key pair p =61 q =53 two random primes secret multiply n = p·q3233 public factor n back? no fast method known φ(n) = (p−1)(q−1)60·52 = 3120 secret Fact 1: easy only if you know p and q pick e17 public e shares no factor with φ. Why? solve e·d ≡ 1 (mod φ)d = 2753 secret check: 17·2753 = 15·3120 + 1 public key(17, 3233) private key2753 then throw away p, q, φ

? Why must e share no factor with φ? Otherwise no d exists. e = 2: 2·d is even, never ≡ 1 (mod 3120).

[+] RSA symbols: what each letter means, and who may see it
PUBLIC – rides on the wire, anyone may hold it: n, e SECRET – never leaves the owner: p, q, φ, d
SymbolWhat it isRelationWho sees it
p, qthe two prime factorschosen at randomsecret
nthe modulusn = p·qpublic
φ(n)Euler totient of nφ(n) = (p−1)(q−1)secret
eencryption exponentshares no factor with φ(n)public
ddecryption exponente·d ≡ 1 (mod φ)secret
(e, n)public keyencrypt / verifypublic
(d, n)private keydecrypt / signsecret

Memory hook: the secret set is everything you need to build the key (p, q, φ) plus the trapdoor d; once d exists, p, q, φ are thrown away. Only n and e are ever published.

[+] RSA: encrypt and decrypt
p = 61,  q = 53     n = p·q = 3233     φ = (p−1)(q−1) = 3120
gcd(e, φ) = 1 ⇒ e = 17     e·d ≡ 1 (mod φ) ⇒ d = 2753
ANYONE, WITH THE PUBLIC KEY (e, n) = (17, 3233) message m65 c = me mod n = 6517 mod 3233 ciphertext c2790 sent over the open wire ONLY BOB, WITH THE PRIVATE KEY d = 2753 ciphertext c2790 m = cd mod n = 27902753 mod 3233 back to m65 ✓ cd = (me)d = me·d = m1 = m (mod n) why?
[+] Why decryption returns the message: me·d ≡ m IN SYMBOLSON OUR EXAMPLE 1 HOW WE CHOSE D e·d ≡ 1 (mod φ) ⇒ e·d = 1 + k·φ 17·2753 = 46801 = 1 + 15·3120 2 SPLIT THE EXPONENT me·d = m1 + k·φ = m · (mφ)k 6546801 = 65 · (653120)15 3 FACT 2: M SHARES NO FACTOR WITH N mφ ≡ 1 (mod n) 653120 ≡ 1 (mod 3233) 4 WHAT IS LEFT m · 1k = m 65 · 115 = 65 ✓
encrypt: c = me mod n
decrypt: m = cd mod n
Warning: if m shares a factor with n, the key is broken: anyone computes gcd(c, n) = p. m = 122 = 2·61 → c = 1830, gcd(1830, 3233) = 61.
[>] CTF #6: given p, q and c, recover the message m
Challenge. You intercepted the ciphertext c = 1647. The public key is (e, n) = (17, 2773). Because the modulus is small, it was already factored for you: n = p·q with p = 47, q = 59. Your goal: recover m = cd mod n.
WHAT TO DOYOUR NUMBERS 1 GET φ FROM THE PRIMES φ(n) = (p−1)(q−1) 46 · 58 = 2668 2 INVERT e MODULO φ (EXTENDED EUCLID) e·d ≡ 1 (mod φ) ⇒ d = e−1 mod φ d = 157 (17·157 = 2669 = 1 + 2668) 3 DECRYPT WITH d m = cd mod n 1647157 mod 2773 = 2024
[+] Why RSA is (believed) secure THE ATTACKER SEES e = 17, n = 3233, c = 2790. WHAT DOES SHE NEED TO GET m? to decryptm = cᵈ mod nneedsdto get dd = e⁻¹ mod φneedsφto get φφ = (p−1)(q−1)needsp, qto get p and qfactor n = 3233no fast method known Factoring a 2048-bit n is as hard as brute-forcing a 112-bit key.
[+] Signatures: public-key crypto, in reverse Bob signs with his private key Midterm moved to Friday. signature from SK posted student A✓ really Bob a stranger✓ really Bob grabs the post Mallory: no private key edits “...Monday”content changed ✗ rejectedhash ≠ se ENCRYPTION anyone, with the public key locks only Bob, private key reads it SIGNATURE only Bob, private key signs anyone, with the public key checks it: reversed
[+] RSA signatures: sign with d, verify with e SIGN: ONLY BOB, PRIVATE KEY d = 2753 messagem hashSHA-256 digesth = 123 s = hd mod n1232753 mod 3233 signatures = 2746 VERIFY: ANYONE, PUBLIC KEY e = 17 signatures = 2746 se mod n274617 mod 3233 recovered123 messagem hashSHA-256 fresh hash123 = they match✓ signed by Bob (hd)e = h, the same math as RSA encryption, with the keys swapped. Hash first: any length becomes one number below n. Never sign raw messages.
sign: s = hd mod n
verify: h = se mod n
$ openssl dgst -sha256 -sign priv.pem msg \ | openssl dgst -sha256 -verify pub.pem -signature - Verified OK
[>] CTF #7: forge a signature the server refuses to make
We have a server that will sign any message you send, except one target M. Your job: forge a valid signature on M yourself.

Five endpoints at 165.22.35.184:54327 (plain GET, decimal numbers):

/pubkeythe public key: n (1024-bit) and e
/targetM, the message you must forge a signature for
/sign?m=<int>its signature md mod n, for any m you choose. It refuses only m = M
/submit?sig=<int>the flag, if your signature is valid for M: sige mod n = M
/sourcethe full server source, if you want to read it
[+] Toolkit: big-integer math in Python

Python integers are arbitrary precision, so a 1024-bit value is just an int. Three built-ins plus a few GETs are the whole client:

# a 1024-bit number is just an int, no big-number library needed pow(m, d, n) # m^d mod n sign / decrypt pow(s, e, n) # s^e mod n verify: equals m for a valid signature pow(r, -1, n) # modular inverse (3.8+): the x with r*x = 1 (mod n) math.gcd(r, n) # 1 means r is invertible mod n import urllib.request def get(path): # GET one endpoint, return its text return urllib.request.urlopen(URL + path).read().decode() raw = get('/pubkey').split() # ['n=...', 'e=...'] n, e = int(raw[0][2:]), int(raw[1][2:]) M = int(get('/target')) # the message to forge sig = int(get(f'/sign?m={m}')) # a signature for any m you pick print(get(f'/submit?sig={S}')) # prints the flag if S is valid for M
[?] CTF #7 · hint 1: signatures multiply
sign(a) · sign(b) = ad·bd = (a·b)d = sign(a·b mod n)

Multiply two signatures and you get the signature of their product, with no key needed. So “the server won’t sign M” is not a wall: if you can reach M as a product of messages it will sign, you can build sign(M).

Toy check: sign(2) · sign(3) mod n is a valid signature of 6, because (2d·3d) = 6d.

[?] CTF #7 · hint 2: split M into two pieces it will sign

Pick a random x. Split M into M·x and x−1: since x·x−1 = 1, their product is M. Both are ≠ M, so the server signs them; multiply the two signatures (hint 1) to get sign(M).

M · x /sign (M·x)d x−1 /sign (x−1)d ×multiply Md = sign(M) ✓submit this Because (M·x)·x−1 = M, the product of the two signatures is sign(M).
x = random.randrange(2, n) # any random x, gcd(x, n) = 1 sa = int(get(f'/sign?m={(M * x) % n}')) # sign(M*x) (!= M, so allowed) sb = int(get(f'/sign?m={pow(x, -1, n)}')) # sign(x^-1) (!= M, so allowed) S = (sa * sb) % n # = (M*x)^d * (x^-1)^d = M^d get(f'/submit?sig={S}') # S^e == M -> the flag
[+] Diffie-Hellman: agree on a shared key over an open wire
Two strangers each mix a public value with their own secret, then swap the mixes. Both end up with the same shared key, yet an eavesdropper who sees only the swapped values cannot rebuild it. Like mixing paint: easy to combine, hard to separate.
QR code to the Computerphile Diffie-Hellman video
How it works, in 8 minutes

Computerphile: the paint-mixing picture, no math.

RSADiffie-Hellman
What it doesencrypt and signkey agreement only
Keysone reusable public/private pairfresh secret per session (ephemeral)
Forward secrecyno: one leaked key exposes old trafficyes: a new key every session
Main weaknesstrusting a public key is really Bob'sno authentication: sign the values, or Mallory sits in the middle
Both are slow, so every real system is hybrid: run RSA or DH once to agree on a key, then AES for all the data. TLS, SSH, and Signal all work this way.
[+] Takeaways
  • Each primitive solves exactly one problem. Hashes give integrity against noise, MACs against people, signatures against people to third parties.
  • Determinism is the recurring sin. ECB, textbook RSA, and encrypt-and-MAC all fail the same way.
  • Confidentiality is not integrity. The one-time pad, CBC and CTR all encrypt perfectly well and let an attacker edit the plaintext.
  • The mode and the randomness break before the cipher does. AES is fine. Your IV probably is not.
  • Public key fixes key distribution, not speed. RSA or Diffie-Hellman agree on a key over an open wire, then AES carries the data. RSA also signs; DH gives forward secrecy. What is left is authenticity: whose key is this?
$ ./crypto --next pki # we can sign. now: whose key is this, really?
[+] Acknowledgements

These lecture materials were provided by Suman Jana (Columbia University), and this deck is a re-typeset and extended version of his Security I slides.

Slides in the original material are borrowed from Vitaly Shmatikov.

Figures: ECB penguin [Wikipedia]; ECB vs CBC portrait [Bart Preneel]; Diebold analysis [Kohno, Stubblefield, Rubin, Wallach]; Kerckhoffs portrait [Wikimedia Commons, public domain]; one-time pad photo [CIA, public domain]; tape library [Linda Bartlett, public domain]; key icon [Twemoji, CC BY 4.0]; Debian advisory DSA-1571 [debian.org]; Clipper chip photo [Travis Goodspeed, CC BY 2.0]; “Random Number” [xkcd 221] and “Encryptic” [xkcd 1286, Randall Munroe, CC BY-NC 2.5]; ECB penguin [Wikimedia Commons]; OpenAI DNS incident report [OpenAI].

Errors introduced in the re-working are mine.

← all lectures