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
Why anyone wants a fingerprint of a file. Three everyday uses, so the rest has a point.
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.
The three things we need from it, one-way, second-preimage resistant, collision resistant, each with the attack it stops.
Why finding a collision is much cheaper than it looks. The birthday paradox, computed step by step for this room.
Where hashes are used, in detail, and which property each use needs.
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?
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 in2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824# 64 hex digits = 256 bits$echo -n "hellp" | sha256sum# one letter changedfdd7585e08c4e2afd71dcabdb4636c89d557a3f42db9e2040c8bbd1708aa4ce7# 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.
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
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
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?
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
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.
? 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
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 resistanceGiven an input x, it is hard to find a different input x' with H(x') = H(x).
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.
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.
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.00000person 2 must avoid the 1 day already taken 364/365 = 0.99726person 3 must avoid the 2 days already taken 363/365 = 0.99452person 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 factorsrunning 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.
People
Shared birthday
10
12%
23
51%
30
71%
50
97%
80
99.99%
[+] Recap: the birthday paradox for any K and T
T people, each birthday drawn at random from K days. First we count outcomes.
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
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)/2so 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 tinyP(no repetition) ≤ e^(-x) ≈ 1 - T(T-1)/2K # with x = T(T-1)/2KP(some repetition) ≥ 1 - e^(-x) ≈ T(T-1)/2K# grows like T^2: it counts pairs
T
exact
bound e^(-T(T-1)/2K)
1 - T(T-1)/2K
10
0.883
0.884
0.877
23
0.493
0.500
0.307
40
0.109
0.118
-1.14
80
0.00009
0.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 resistanceIt should be hard to find x ≠ x' with h(x) = h(x'). We now ask how hard, using the birthday formula.
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 Twe 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) · √Kwith 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.
target p
T needed
for K = 365
1%
0.14 √K
3 people
50%
1.18 √K
23 people
99%
3.03 √K
58 people
[+] Recap: the square root on real digest sizes
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.
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).
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 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
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.
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
Hash
Encryption
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.
Example
SHA-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
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.
[+] 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.
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.
[+] Application 3: a sealed bid
An auction where everyone bids at once, and nobody can see the others’ bids or change their own afterwards.
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.
“Use a hash” is not a design. Say which property you are relying on.
[+] Real hash functions, and where they stand
Function
Year
Digest
Status today
MD5
1992
128 bits
Broken. A laptop finds a collision in seconds. Never use it for security.
SHA-1
1995
160 bits
Broken. First real collision in 2017, chosen-prefix collisions in 2020. Deprecated everywhere.
SHA-256, SHA-512
2001
256, 512 bits
Fine. The SHA-2 family. Used in TLS, Git, Bitcoin, package managers.
SHA-3
2015
224 to 512 bits
Fine. 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?
state = 00000000 # a fixed start valuestate = state + 6c6c6568 ("hell" as a 32-bit number) = 6c6c6568 # chunk 1, mod 2^32state = state + 6f77206f ("o wo") = dbe385d7 # chunk 2state = 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
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
$echo -n "hello" | md5sum5d41402abc4b2a76b9719d911017c592# 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]
"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
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
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
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
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
Round
The mix of B, C, D
The 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
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
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
SHA-1
Designed
1991, Ron Rivest
1995, NSA, published by NIST
Shape
pad, split into 512-bit blocks, chain
the same, carrying 160 bits along
One block
64 operations, 4 rounds of 16
80 steps, 4 rounds of 20
The mix
F, G, H, I: one per round
choose, parity, majority, parity: one per round
Constants
64 values K[i], one per operation
4 values, one per round
Rotations
s[i] from a table, four per round
fixed: A by 5 and B by 30, every step
Message words
16 words, reused once per round
16 words stretched to 80, one per step
[+] SHA-1 against MD5, in numbers
MD5
SHA-1
Digest
128 bits, 32 hex digits
160 bits, 40 hex digits
Birthday bound
2^64 hashes
2^80 hashes
Speed, one RTX 4090
about 164 billion hashes per second
about 51 billion per second
First real collision
2004, Wang and colleagues
2017, Google and CWI
Collisions today
seconds on a laptop
months on a few gaming GPUs, under $10,000
Second preimage
no practical attack; best is 2^123
no practical attack
Retired
2008 warning; 2011 RFC says do not use
2011 NIST deprecation; 2017 browsers reject it
Where you still meet it
file checksums, old password databases
Git object names; SHA-256 is the newer option
Same design, same fate, thirteen years apart.
[+] How strong is SHA-1?
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.
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
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 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
What a MAC gives usIntegrity 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?
? 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
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 bytesK1 = KEY ⊕ ipad = 5d 05 4f 36 36 ... 36 # 6b⊕36=5d, 33⊕36=05, 79⊕36=4f, 00⊕36=36inner = SHA256( K1 || "pay Bob $10" )
= 2220d7d9dd3b9ecd36d299d29b64f94abe44faee1ade5955035e7bc987e762f0K2 = KEY ⊕ opad = 37 6f 25 5c 5c ... 5c # 6b⊕5c=37, 33⊕5c=6f, 79⊕5c=25, 00⊕5c=5ctag = 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' | sha256sum51f03ce57e0fd007...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.
OpenAI'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[+] 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[+] The fence had a hole, and the alarm did not stop it
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
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
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[?] The threat model: a secret over an open network
? 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, 1883An encryption scheme should be secure even if the enemy knows everything about it except the key. Do not rely on “security by obscurity.”
What leaks
What we must do
The key
Make a new key. It takes minutes.
A secret algorithm
Replace 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.
Jean-Guillaume-Hubert-Victor-François-Alexandre-Auguste Kerckhoffs von Nieuwenhof, 1835–1903. Public domain
[!] Weak randomness broke Debian's keys in 2008
In 2006, a debugging tool warned that OpenSSL read uninitialized memory, and a Debian maintainer deleted the line that caused the warning.
That line fed randomness into the key generator, so the only input left was the process ID, a number below 32,768.
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.
In May 2008, Luciano Bello found the bug, and attackers simply generated all 32,768 keys and tried each one.
[+] 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
OBKRUOXOGHULBSOLIFBBWFLRVQQPRNGKSSOTWTQSJQSSEKZZWA
TJKLUDIAWINFBNYPVTTMZFPKW
GDKZXTJCDIGKUHUAUEKCAR
# Sanborn's cluesFLRV = EAST
QQPRNGKSS = NORTHEAST
NYPVTT = BERLIN
MZFPK = CLOCK
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
Properties we will use
x ⊕ 0 = x
XOR with zero changes nothing.
x ⊕ x = 0
XOR with itself gives zero.
x ⊕ y = y ⊕ x
The order does not matter.
(x ⊕ y) ⊕ z = x ⊕ (y ⊕ z)
The grouping does not matter.
(p ⊕ k) ⊕ k = p
XOR 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
Shannon, 1949A 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.
the proof
[?] The one-time pad is perfect. What is the catch?
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?
The CIA's own page about this pad and how agents used it.
[?] Limitation 1: What if we reuse the key?
Is P1 ⊕ P2 useful without the key?
[?] Limitation 1: What if two pictures share a key?
P1
P2
C1 = P1 ⊕ K
C2 = 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 ⊕ 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.
The data: 365 GB of video that Alice wants to send secretly.
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
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
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 -p69c4e0d86a7b0430d8cdb78070b4c55a# the block above74db6c596f02c433989fb6c9cd317f15# 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?
A one-to-one table is called a permutation. How many are there?
[+] DES scrambles half the block, XORs it in, and swaps
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
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.
[+] How expand turns 32 bits into 48
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
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 DESMost attacks on DES target the S-boxes. IBM designed them against differential attacks in 1974, 16 years before the public found the attack.
Coppersmith, 1994
[x] DES died because its 56-bit key is too short
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:
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
DES
AES
Standard since
1977
2001, after an open competition with 15 candidates
Designed by
IBM, with changes by the NSA
Daemen and Rijmen, as Rijndael
Block size bits encrypted at once
64 bits
128 bits
Key
56 bits
128, 192 or 256 bits
Rounds
16
10, 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[+] The plaintext becomes a grid, then meets K0
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.
[+] Step 1: SubBytes looks up every byte
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.
[+] Step 2: ShiftRows slides row r left by r
The idea: ShiftRows slides each row of the grid to the left, and row r moves by exactly r places.
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 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.
[+] Step 3: MixColumns, one new byte at a time
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.
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 idea: AddRoundKey XORs every byte of the grid with the byte in the same place of this round's key.
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 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.
[+] A word is the one above XOR the one to its left
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 idea: g takes one word and returns a scrambled word. It reuses the S-box from SubBytes.
[+] Each round key gets a different Rcon
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.
[?] Why the key schedule needs g
? 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.
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.
[+] 10,000 random flips: the count is a coin flip[?] What if we remove one step of AES?
? 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
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.
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
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 Fare the only non-linear part.
It died of a short key.2^56 keys: EFF Deep Crack took 56 hours in 1998.
INSIDE F
[+] How the DES expand step turns 32 bits into 48
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[+] 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.
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.
? 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?[?] Idea 1: just fill the gap with zeros
? 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?
? 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
? 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.
[?] What if we skip padding when the block is full?[+] ECB, Electronic Code Book: each block alone
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 -c164a7cf5c7bb2d48d42edb56b2d7538507# block 1: "attack at dawn!!"4a7cf5c7bb2d48d42edb56b2d7538507# block 2: same phrase, same ciphertext4a7cf5c7bb2d48d42edb56b2d7538507# 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 picture
AES-ECB of the pixels
a secret note
AES-ECB, still readable
Large areas of one colorbecome 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
Mallory
✓ 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.
153M accounts, passwords in 3DES-ECB instead of hashed, and the password hints stored in the clear.
xkcd 1286, “Encryptic”
[>] We know the whole pipeline. Time to break it.
? 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?
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.
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[+] CBC, Cipher Block Chaining: chain the blocks
$printf 'YELLOW SUBMARINEYELLOW SUBMARINE' | openssl enc -aes-128-cbc -nopad -K 0001..0f -iv 3a9f..540b | xxd -p -c1660e35950a5e45dc362f7a3b6debf5eb8# block 1 (the same 16 bytes, twice)319ef88e83437a41aa7fade76001f230# block 2: same input, different output
[+] CBC decryption undoes the chain[x] CBC has no integrity: flip a byte, edit the message
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
Predictable IV = deterministic encryption in disguise.
[x] 2004: Diebold voting machines, CBC done wrong[>] 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.
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[+] Read one byte: force the padding to 01[+] Peel the block, then the whole message[!] 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.
Anonymous feedback no name attached
[+] Recap: PKCS#7 padding
? 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
plaintext
AES-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
plaintext
AES-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
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?
N is public, K is the secret. Never reuse a (K, N) pair.
[x] Nonce reused: the one-time pad, used twice
P1
P2
C1 = P1 ⊕ S
C2 = P2 ⊕ S
C1 ⊕ C2 = P1 ⊕ P2
[>] CTF #3: crack CTR
Threat model · chosen-plaintext (CPA) the server encrypts whatever you send.
Goal: recover the secret, with no key.
[+] Why the keystream repeats, and how to steal it[>] CTF #4: two keys are not twice the security
Threat model · known-plaintext (KPA) you get known plaintext / ciphertext pairs.
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
? 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
? 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²¹
[ 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?
The bar: a ciphertext leaks nothing, not even whether two messages are equal. So encryption must be randomized.
[?] The game: which message was encrypted?
Secure means: nobody can do noticeably better than 1/2.
[+] Chosen plaintext: she also gets an encryption oracle
That oracle is the chosen-plaintext power. Still stuck at 1/2? Then it is CPA-secure.
[x] Any deterministic cipher loses the game
She wins every time. ECB is exactly this. Fix: a fresh IV or nonce, every time.
[+] Advantage: how far from a coin flip?
“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.
Same bug, smaller scale: beat our server 20 times in a row.
[+] CTF #5: our encryption scheme
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.
⚠ 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
? 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
? 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
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!
Anonymous feedback no name attached
[+] Recap: AES in counter mode (CTR)
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
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 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.
Looks right: C keeps the message secret, the tag proves integrity. So what could go wrong?
[x] Why it fails: the tag leaks equality
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.
It happened to SSL/TLS:padding oracle (2002), Lucky Thirteen (2013, timing only), POODLE (2014). And it is not secure in general (Krawczyk, 2001).
Krawczyk, CRYPTO 2001
[/] Attempt 3, the fix: Encrypt-then-MAC
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.
Bellare & 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
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).
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.
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.
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
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
φ(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.
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).
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
? 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
Also, a·x is still coprime to n, so no arrow leaves the set.
[2] Step 2: multiply both rows, then simplify
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
? 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, eSECRET – never leaves the owner: p, q, φ, d
Symbol
What it is
Relation
Who sees it
p, q
the two prime factors
chosen at random
secret
n
the modulus
n = p·q
public
φ(n)
Euler totient of n
φ(n) = (p−1)(q−1)
secret
e
encryption exponent
shares no factor with φ(n)
public
d
decryption exponent
e·d ≡ 1 (mod φ)
secret
(e, n)
public key
encrypt / verify
public
(d, n)
private key
decrypt / sign
secret
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 = 53n = p·q = 3233φ = (p−1)(q−1) = 3120
gcd(e, φ) = 1 ⇒ e = 17e·d ≡ 1 (mod φ) ⇒ d = 2753
[+] Why decryption returns the message: me·d ≡ m
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.
[+] Why RSA is (believed) secure[+] Signatures: public-key crypto, in reverse[+] RSA signatures: sign with d, verify with e
[>] 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):
/pubkey
the public key: n (1024-bit) and e
/target
M, the message you must forge a signature for
/sign?m=<int>
its signature md mod n, for anym you choose. It refuses only m = M
/submit?sig=<int>
the flag, if your signature is valid for M: sige mod n = M
/source
the 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 nimport urllib.request
defget(path): # GET one endpoint, return its textreturn 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
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).
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.
no 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].