A step-by-step rebuild of the integrity-and-authentication lecture. It starts from why a plain hash protects nothing on a single untrusted channel (and why it still works for downloads), defines a MAC and what it does not give (secrecy, third-party proof, freshness), explains on a hand-computable toy hash why hashes like SHA-256 are chains of states and why hand-rolled keyed hashes are discouraged, then computes HMAC-SHA256 by hand with real bytes, and closes with correct use in code: random keys, raw bytes, constant-time comparison, replay protection, and where HMAC appears in webhooks, JWTs, AWS and TLS, with the lecture's news break on an AI agent that escaped a sandbox through DNS.
Subject: Computer Security · 69 slides · code lesson
Open the interactive version of this deck
Title
Computer Security — Crypto Lecture 3
Why a hash alone protects nothing, what a MAC adds, and how to compute and use HMAC correctly
Section
A message has to arrive unchanged, and provably from its sender
Objectives
This lesson rebuilds the integrity-and-authentication part of the cryptography lecture one step at a time. Each idea arrives as the answer to a problem you have just seen, so by the end HMAC should look like a sensible design rather than a formula with two strange constants in it.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) part two: integrity and authentication
Warm-up
The last lesson built cryptographic hash functions. Before relying on them, pull the definition back out of memory.
Discussion prompt
List the three security properties of a cryptographic hash function. Then answer this: does any of them say anything about WHO computed a digest?
Hint: One property is about reversing, two are about finding matches.
Answer:
One-wayness: given a digest, you cannot find an input that produces it. Second-preimage resistance: given one input, you cannot find a different input with the same digest. Collision resistance: you cannot find any two inputs with the same digest at all.
None of the three mentions a person. A hash has no key, so everyone in the world computes exactly the same digest for the same input. That single fact drives this whole lesson: a digest can tell you that data matches a digest, but never who produced it.
Concept
Alice sends Bob a message across a network. Assume the worst about that network: someone else, traditionally called Mallory, can read, drop, change or inject packets. Public Wi-Fi, a compromised router, or a misbehaving ISP all give someone exactly this position, so designing for it is the normal starting point, not paranoia.
Bob wants two separate guarantees, and it pays to name them precisely before trying to provide them.
integrity — the message Bob received is bit-for-bit the message that was sent; nothing was altered on the way
authenticity — the message really came from Alice, not from someone claiming to be her
Notice what is not on the list: secrecy. Bob is not asking that nobody else can read the message. A payment instruction can be public and still need protecting from changes. Keeping integrity and confidentiality as separate goals lets us solve one without accidentally assuming we solved the other, which is where many real cryptographic mistakes begin.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-div-mac
Definition probe
Each problem below threatens one goal. Sort them; the rest of the lesson depends on telling these apart.
Sort into buckets
Which goal does each problem threaten?
Prediction
The first idea everyone has: Alice sends her message together with its SHA-256 digest. Bob recomputes the digest of what he receives and compares the two.
Predict first
On a network someone else controls, what does the attached digest protect against?
Correct: Only accidental corruption, because anyone can compute a fresh digest for a different message
Why: Whoever changes the message can simply replace the digest too, since computing SHA-256 needs no secret. Bob's check passes because the message does match its digest. The digest still catches a random flipped bit, so it is a good error check, but it adds nothing against someone acting on purpose.
Concept
Look at what Bob's check actually tests. He hashes the message he received and compares it with the digest he received. The only question that answers is: do these two things agree with each other?
Figure (svg): Alice sends a message plus its hash; Mallory replaces both with a different message and that message's hash; Bob's recomputed hash matches and the check passes
Anyone can make a message and a digest agree, because the hash is a public function with no secret in it. SHA-256 is never weakened here; it does exactly what it promises. The check simply was not asking the question Bob cared about, which is whether Alice produced this. A hash on its own cannot answer that, because nothing in a digest depends on who computed it.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-mac-q
Socratic
Ubuntu, Python and most open-source projects publish a SHA-256 digest next to each download, and checking it is genuinely useful. That seems to contradict the last slide.
Discussion prompt
What is different about the download situation that makes a plain digest useful there but useless between Alice and Bob?
Hint: Where did the file come from, and where did the digest come from?
Answer:
The digest and the file arrive by different routes. You fetch the file from any fast mirror, which might be untrustworthy, but you read the digest from the project's own HTTPS page. The mirror can serve a different file, but it cannot change the number you compare against.
Figure (svg): The digest comes from the official site over HTTPS while the file comes from an untrusted mirror; the user compares the two
So the protection never came from the hash. It came from the trusted second channel, and the hash let a short trusted number vouch for a large untrusted file. Between Alice and Bob there is only one channel, so there is nothing trusted for the digest to borrow from.
Concept
The diagnosis is now precise. A plain digest fails because anyone can compute it. To be useful on a single untrusted channel, the attached value must be something only Alice, and Bob who checks it, can compute.
The only way to make a computation available to some people and not others is a secret. So Alice and Bob agree on a random key in advance, and the value attached to each message is computed from both the message and the key. A changed message no longer matches its value, and nobody without the key can compute the right value for a different message.
| plain hash | keyed digest | |
|---|---|---|
| who can compute it | anyone | only holders of the key |
| what a match proves | message matches its digest | message came from a key holder, unchanged |
| needs a trusted channel | yes, for every digest | only once, to share the key |
Read the last row carefully. We still need a trusted channel, but only once, to agree on the key, instead of once per message. Every message after that can travel over a network nobody trusts. A keyed digest has a name: a message authentication code, or MAC.
Section
A digest that needs a key
Concept
A MAC is an algorithm that takes a secret key and a message and outputs a short value. The vocabulary is small and worth getting exactly right.
tag — the short value MAC(KEY, m) attached to message m; anyone with the key can check it, nobody else can make it
tagging — Alice computes tag = MAC(KEY, m) and sends m together with the tag
verifying — Bob recomputes MAC(KEY, m) on the message he received and accepts only if it equals the tag that arrived
Figure (svg): Alice and Bob each hold the same key; Alice sends the message and tag; Bob recomputes the MAC and compares; Mallory in the middle has no key
A MAC is deterministic: the same key and message always give the same tag, which is what lets Bob simply recompute it. The tag is short, typically 16 or 32 bytes, however long the message is.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-mac
Ranking
Order the steps of one authenticated message, from before the first message to Bob's decision.
Put in order
Why: The key must exist before any message, and agreeing on it is the only step that needs a trusted channel. After that the pattern is always tag, send, recompute, compare. Bob never decrypts or reverses anything; he repeats Alice's computation and checks that he gets the same answer.
Concept
Saying a MAC is secure needs a precise target. The standard target is deliberately generous to the opponent, so that anything meeting it is safe in practice.
Suppose an opponent can get Alice to tag any messages they choose, as many as they like, and sees every tag. The MAC is secure if, after all that, they still cannot produce a valid tag for any message Alice did not tag. This property is called unforgeability.
| the opponent may | the opponent still cannot |
|---|---|
| see the tags of many real messages | produce a valid tag for a new message |
| choose which messages get tagged | learn the key |
| change messages on the wire | make Bob accept a changed message |
Why allow chosen messages? Because in real systems outsiders often influence what gets authenticated: a username they registered, a search term, a form field. A MAC has to stay secure in exactly those situations. One loophole remains in every MAC: a random guess. The defence is length, since a 128-bit tag is guessed correctly once in 2 to the power 128 tries.
RFC 2104 — HMAC: Keyed-Hashing for Message Authentication (Krawczyk, Bellare, Canetti, 1997) section 1
Estimation
Suppose a server checked one billion guessed tags per second, every second of every day.
Predict first
With a 128-bit tag, roughly how long until a random guess is expected to succeed?
Correct: Far longer than the age of the universe
Why: There are about 3.4 times 10 to the 38 possible 128-bit tags. At 10 to the 9 guesses per second, a year holds about 3 times 10 to the 16 guesses, so the expected wait is around 10 to the 22 years, against a universe about 1.4 times 10 to the 10 years old. Message length plays no part, since the tag size is fixed. It is also why heavily shortened tags are risky: a 32-bit tag has only about 4 billion possibilities.
Elimination
A MAC is easy to over-trust. Rule out the claims that sound right but are false.
Eliminate the wrong options
Alice sends Bob a message with a valid HMAC tag. Which statement is true?
Survives elimination: c1
Why: A MAC gives integrity and authenticity relative to the key: unchanged, and from a key holder. It gives no secrecy, no third-party proof (non-repudiation), and no freshness. Each missing property needs its own mechanism, and forgetting that is behind many real protocol bugs.
Sorting
Four tools appear in this course. Sort each requirement to the simplest tool that meets it.
Sort into buckets
Which tool fits each requirement?
The map on the next slide is worth keeping: four tools, four promises, and real protocols usually combine two or three.
Picture it
Keep this picture next to every protocol you analyse for the rest of the course. When a design claims a property, find which box is meant to provide it. If no box provides it, the design is relying on something that is not there.
Figure (svg): Four cards comparing hash (no key, matches its digest), MAC (shared key, unchanged and from a key holder), encryption (unreadable to outsiders), and signature (private key, provable to anyone)
The MAC sits in a sweet spot: as fast as hashing, and it gives authenticity whenever both ends can share a key, which covers most traffic between a client and a server once a connection is set up.
Matching
Match each term to what it means.
Match the pairs
Why: Tag and unforgeability are what a MAC provides. Non-repudiation and freshness are the two properties people most often assume a MAC provides when it does not; they need signatures and counters or timestamps respectively.
Explain it
A friend asks why Bob needs the secret key. Surely only the sender needs it?
Discussion prompt
Explain to your friend, in two or three sentences, why verifying a MAC requires the same key that created it, and what that implies for proving authorship to a third party.
Hint: How does Bob check the tag?
Answer:
Bob verifies by recomputing the tag and comparing; a MAC has no separate public checking step. So whoever verifies must be able to compute tags, which means holding the key.
The consequence is that Bob could have made any tag Alice could. That is fine between the two of them, but useless in front of a judge. When verifiers must not be able to create, you need a signature instead.
Section
Hash functions have internal structure, and it matters
Concept
Fast, well-studied hash functions are in every library, so it is natural to build a MAC from one. There were historical reasons too: hashing is faster than encryption, one hash can be swapped for another without redesigning the MAC, and the US once restricted exports of encryption code but never of hash functions. The tempting shortcut is to glue the key and message together and hash the result, putting the key either in front or at the end.
tag = H(KEY || m) # key first -- do not use
tag = H(m || KEY) # key last -- do not use| design | status | reason, in one line |
|---|---|---|
| H(KEY || m) | unsafe with MD5, SHA-1, SHA-256 | the tag exposes the hash's internal state |
| H(m || KEY) | weak | its security falls back on collision resistance |
| HMAC | standard, with a security proof | built to avoid both problems |
Both shortcuts look reasonable, and the argument for them sounds convincing: the hash is one-way and the key is secret, so surely nobody can produce a tag without the key. The flaw is in the word surely. That argument treats the hash as a sealed black box, and the hashes in common use are not. To see why the details matter, we need to look inside one.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-mac-try
Concept
MD5, SHA-1 and SHA-256 share one structure, called Merkle-Damgard. The input is padded and cut into fixed-size blocks, 64 bytes each for SHA-256. An internal state starts at a published value called the IV, and a compression function f absorbs one block at a time, turning the current state and the next block into a new state.
Figure (svg): A chain: the IV enters a compression function f with block 1, producing state s1, then f with block 2, and so on through a final padding block; the last state is the digest
Two details matter. First, the final block is padding that records the input's length, so padding is part of what gets hashed, not decoration. Second, look at the right-hand end: the digest is not computed from the chain afterwards. It IS the final state, written out. For SHA-256 the state is 32 bytes and the digest is those same 32 bytes.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-md5-recap
Notation
The same structure as a loop. Open each note to see what each line contributes.
Annotate
That last line is why cryptographers say these hashes are not black boxes. Their output carries everything the loop knew, which a secure MAC must not hand out when the loop began with a secret key.
Concept
Real SHA-256 states are 32 bytes, too much to push around on paper. This toy hash has the same chain structure with tiny numbers. Blocks and states are whole numbers from 0 to 99. The IV is 7. The compression function multiplies the state by 3, adds the block, and keeps the last two digits. The final padding block is the number of blocks.
IV = 7
def f(s, b): # the compression function
return (3 * s + b) % 100 # state and blocks are numbers 0-99
def H(blocks):
s = IV
for b in blocks + [len(blocks)]: # final block = padding = the length
s = f(s, b)
return s # the digest is simply the last state| step | state before | block | new state = (3 x state + block) mod 100 |
|---|---|---|---|
| 1 | 7 | 5 | (21 + 5) mod 100 = 26 |
| 2 (padding) | 26 | 1, the length | (78 + 1) mod 100 = 79 |
| result | H([5]) = 79 |
The table hashes the one-block message 5. Each row uses only the previous state and one block, exactly like the real thing. The toy is hopeless as a real hash, since it is easy to reverse, but it is good for seeing the chain: each state depends on everything before it, and the answer is simply wherever the loop finished.
Fill the middle
Use the toy hash: IV 7, new state = (3 x state + block) mod 100, final block = number of blocks.
Fill in the blanks
message: [4, 50]
after block 4: s = 25
after block 50: s = 25
after padding block 2: s = 77
Why: Block 4: 3 x 7 + 4 = 25. Block 50: 3 x 25 + 50 = 125, keep 25. Padding is the length, 2: 3 x 25 + 2 = 77. So H([4, 50]) = 77. The state happens to repeat at 25, which is fine; f only cares about the current state and the block.
Socratic
Imagine using H(KEY || m) with this kind of hash. The key goes in first, the message follows, and the published tag is the final state.
Discussion prompt
Without working through any attack, explain why publishing the final state of a computation that began with a secret might be riskier than publishing a one-way scramble of the key. What does a designer want the published tag NOT to be?
Hint: Is the tag the end of the story, or a bookmark?
Answer:
The final state is not a scramble of the key alone; it is the complete position the computation reached, with the key already folded in. Publishing it hands out the computation's progress, not just a fingerprint. A designer wants the tag to be a dead end: a value from which no further keyed computation can be carried on by someone who lacks the key.
This is the property cryptographers call length extension, and RFC 2104 and the hash literature are why H(KEY || m) is listed as unsafe for MD5, SHA-1 and SHA-256. HMAC's second hash exists precisely to make the published value that dead end, as Part 4 shows.
Discrimination
The concern comes from publishing the whole internal state. Some hash designs publish only part of it.
Sort into buckets
Is each hash's output its complete final state?
Two truths and a lie
Two of these statements are true and one is a myth. Find the myth.
Eliminate the wrong options
Which statement is the myth?
Survives elimination: m1
Why: The strength of the parts does not guarantee the strength of the combination. The shortcuts fail without SHA-256 being broken and without the key leaking, purely because of how key and message were arranged. The safe habit is to use a construction analysed as a whole, and for MACs from hashes that is HMAC.
Trap
A common belief: as long as the key stays secret and the hash is strong, any sensible way of mixing the key into a hash makes a secure MAC.
tag = SHA256(KEY || m) # looks keyed, looks strong| claim | reality |
|---|---|
| the key is secret | true |
| SHA-256 is strong | true |
| so this tag is a secure MAC | false: the tag exposes SHA-256's internal state |
Use HMAC, which is designed and proven for exactly this job, through your language's standard library. Never hand-roll a keyed hash, even one that looks obviously fine.
In Python that means hmac.new(key, msg, hashlib.sha256). In OpenSSL it is the -hmac option. Every mainstream language has the equivalent, and all of them interoperate because HMAC is a fixed standard.
RFC 2104 — HMAC: Keyed-Hashing for Message Authentication (Krawczyk, Bellare, Canetti, 1997) section 6, security
Section
Two hashes, each with the key in front
Concept
HMAC, standardised in RFC 2104 and FIPS 198-1, is the answer to everything Part 3 warned about. It does put a key in front of the message, so no message processing happens before the secret is involved. But it never publishes the state of that first chain. Instead it hashes the result a second time, again with a key in front, and publishes only that.
Figure (svg): HMAC as two rows: K1, the key xor ipad, then the message go into a hash to give a 32-byte inner digest; K2, the key xor opad, then the inner digest go into a second hash to give the tag
Read the figure as two ordinary hash calls. Step one hashes the inner key followed by the message, producing a 32-byte inner digest that is never sent anywhere. Step two hashes the outer key followed by that inner digest, and the result is the tag. Nothing else is involved: two hash calls and two XORs.
RFC 2104 — HMAC: Keyed-Hashing for Message Authentication (Krawczyk, Bellare, Canetti, 1997) section 2, definition of HMAC
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-hmac
Concept
The names ipad and opad sound mysterious, but they are only two fixed byte patterns printed in the standard, each one block long. They are public. Their whole job is to turn one secret key into two different keys.
ipad = 36 36 36 ... 36 # 64 bytes of 0x36, a fixed public pattern
opad = 5c 5c 5c ... 5c # 64 bytes of 0x5c, another fixed public pattern
K0 = KEY prepared to exactly 64 bytes
inner = H( (K0 xor ipad) || message )
tag = H( (K0 xor opad) || inner )| name | value | purpose |
|---|---|---|
| ipad | 0x36 repeated 64 times | makes the inner key K1 |
| opad | 0x5c repeated 64 times | makes the outer key K2 |
| K0 | the key, prepared to one block | shared starting point for both |
Why two different keys instead of using the same one twice? It keeps the two hash calls from ever starting from the same block, so the inner and outer computations cannot be confused with each other. Any two different patterns would work in principle; 0x36 and 0x5c are simply the ones the standard fixed, and they differ in many bit positions. Because they are part of the standard, every HMAC library in every language gives identical results.
RFC 2104 — HMAC: Keyed-Hashing for Message Authentication (Krawczyk, Bellare, Canetti, 1997) section 2
Notation
Here is the definition on one line, exactly as it appears in the standard. Open each note to unpack one piece.
Annotate
Read from the inside out: prepare the key, make K1, hash with the message, make K2, hash with the inner digest. Every piece has a single job, and the next few slides put real bytes through each one.
Concept
HMAC first turns the key, whatever its length, into exactly one block: 64 bytes for SHA-256. There are three cases, and they cover every possible key.
Figure (svg): Three rows: a short key like k3y is padded with zero bytes to 64; a 64-byte key is used as it is; a longer key is hashed to 32 bytes and then zero-padded to 64
| key length | what HMAC does | example |
|---|---|---|
| shorter than 64 bytes | append zero bytes up to 64 | k3y becomes 6b 33 79 followed by 61 zero bytes |
| exactly 64 bytes | use it unchanged | a 64-byte random key |
| longer than 64 bytes | hash it, then zero-pad to 64 | a 100-byte key becomes a 32-byte digest plus zeros |
A practical consequence: there is no benefit in a key longer than the block, since it gets hashed down anyway, and a short key is padded with predictable zeros. The recommended key is at least as long as the hash output, 32 random bytes for HMAC-SHA256, generated by a cryptographic random source. The three-character k3y in the next example is only there so the bytes fit on a slide.
FIPS 198-1 — The Keyed-Hash Message Authentication Code (NIST, 2008) section 4, the HMAC specification
Faded example
K1 = K0 xor ipad, byte by byte. K0 starts 6b 33 79 then zeros; ipad is 36 in every byte. The first byte is done for you.
Fill in the blanks
6b xor 36 = 5d
33 xor 36 = 05
79 xor 36 = 4f
00 xor 36 = 36 # every padding byte
Why: In binary, 33 is 0011 0011 and 36 is 0011 0110, which XOR to 0000 0101 = 05. 79 is 0111 1001, XOR 0011 0110 gives 0100 1111 = 4f. Zero XOR anything is that thing, so every padding byte becomes 36. So K1 starts 5d 05 4f and then 36 repeats to the end of the block.
Worked example
Now the full computation with the lecture's own example: key k3y, message pay Bob 10 dollars. Every value below was checked against Python's hmac module and OpenSSL.
KEY = 6b 33 79 # "k3y", zero-padded to 64 bytes
K1 = KEY xor ipad = 5d 05 4f 36 36 ... 36
inner = SHA256(K1 || "pay Bob $10")
= 2220d7d9dd3b9ecd36d299d29b64f94abe44faee1ade5955035e7bc987e762f0
K2 = KEY xor opad = 37 6f 25 5c 5c ... 5c
tag = SHA256(K2 || inner)
= 14eea6c27806b0ea1c83c26f40ed49f510b8fddb0a0e080fc6913f4e2f25c4d4| step | input | output |
|---|---|---|
| prepare key | 6b 33 79 | 6b 33 79 plus 61 zero bytes |
| inner key | K0 xor 36s | K1 = 5d 05 4f 36 ... 36 |
| inner hash | K1 then the 11-byte message (75 bytes) | inner = 2220d7d9 ... 87e762f0 |
| outer key | K0 xor 5cs | K2 = 37 6f 25 5c ... 5c |
| outer hash | K2 then inner (96 bytes) | tag = 14eea6c2 ... 2f25c4d4 |
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-hmac-bytes
Notice the sizes
Why: The inner hash input is 64 + 11 = 75 bytes, so it grows with the message. The outer hash input is always 64 + 32 = 96 bytes, whatever the message was.
Verify: OpenSSL agrees
Why: Running echo -n with the message into openssl dgst -sha256 -hmac k3y prints 14eea6c27806b0ea...2f25c4d4, the same 64 hex digits as the outer hash above.
Figure (svg): HMAC as two rows of hash calls, inner then outer, matching the steps of the worked example
Faded example
Same idea with opad, which is 5c in every byte. The first byte is done for you.
Fill in the blanks
6b xor 5c = 37
33 xor 5c = 6f
79 xor 5c = 25
00 xor 5c = 5c # every padding byte
Why: 33 is 0011 0011 and 5c is 0101 1100, which XOR to 0110 1111 = 6f. 79 is 0111 1001, XOR 0101 1100 gives 0010 0101 = 25. Zero bytes become 5c. So K2 starts 37 6f 25 and then 5c repeats. K1 and K2 differ in every byte, which is the point of using two pads.
Picture it
Part 3 ended with a design goal: the published tag should be a dead end, not a bookmark in a computation that began with a secret. Here is how HMAC achieves it.
Figure (svg): The outer hash input is always K2 (64 bytes) then the 32-byte inner digest then padding, exactly two blocks; the published tag is the end of this short fixed computation, which begins with the key
The long chain that absorbed the message is the inner hash, and its final state is never published; it is fed into the outer hash instead. The outer hash is short and fixed: the outer key, one digest, padding, done. And because the outer computation itself begins with a secret key block, nobody without the key can perform it. That is the structural reason HMAC does not share the weaknesses of the shortcuts, and the security proof by Bellare, Canetti and Krawczyk makes the argument rigorous.
RFC 2104 — HMAC: Keyed-Hashing for Message Authentication (Krawczyk, Bellare, Canetti, 1997) section 6
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-hmac-recap
Hypothesis
SHA-256 works in 64-byte blocks, and the padding adds at least 9 bytes. A short message of 11 bytes needs one compression call when hashed plainly.
Predict first
How many compression calls does HMAC-SHA256 make on that same 11-byte message?
Correct: 4
Why: Inner hash: K1 (64 bytes) plus 11 message bytes plus at least 9 bytes of padding needs 2 blocks. Outer hash: K2 (64) plus the 32-byte inner digest plus padding is 105 bytes, which also fits in 2 blocks. Total 4. For long messages the overhead is a fixed 3 extra calls, so HMAC costs almost exactly what plain hashing costs.
Matching
Match each part of the construction to its job.
Match the pairs
Why: Each piece answers one problem. Key preparation handles keys of any length; the pads make the two hash calls start from different keys; the inner hash does the heavy work on the message; the outer hash makes sure the inner chain's state is never what gets published.
Edge cases
Real systems hand HMAC all kinds of keys. Test your understanding of key preparation at the edges.
Discussion prompt
For HMAC-SHA256, predict what happens with: (1) an empty key, (2) a key of 64 bytes, (3) a 200-byte key, and (4) two different keys, one of which is the other followed by a zero byte. Which of these is a real risk, and which only looks odd?
Hint: Apply the three key-preparation rules to each case.
Answer:
An empty key becomes 64 zero bytes. HMAC still runs, but anyone can compute the same tags, so it provides no authentication at all. A 64-byte key is used unchanged. A 200-byte key is hashed to 32 bytes first, so it is no stronger than a good 32-byte key.
The fourth case is a genuine quirk: zero-padding means the key abc and the key abc followed by a zero byte both prepare to the same block and give identical tags. It only matters if keys are built from variable-length data, which is one more reason to use fixed-length random keys.
Explain it to yourself
Imagine a variant that used K0 itself, unchanged, as both the inner and the outer key.
Discussion prompt
In your own words, what does deriving two different keys buy, compared with using one key in both places?
Hint: Think of the two hash calls as two different machines.
Answer:
With two different keys, the inner and outer hash calls are independent keyed computations: no value produced by one can be passed off as a value from the other, because they start from different secret blocks.
The security proof uses this independence, treating the inner and outer calls as two separately keyed functions. Using one key twice would tie them together and weaken that argument, for the price of a single XOR saved. The pads are a cheap way to get two keys while still storing only one secret.
Pattern
Everything in this part, as one procedure you can run by hand or recognise in code.
In practice you never write these steps yourself; you call the standard library. Knowing them lets you read specifications, debug mismatched tags between two systems, and recognise a hand-rolled imitation in a code review.
RFC 2104 — HMAC: Keyed-Hashing for Message Authentication (Krawczyk, Bellare, Canetti, 1997) section 2
Check
Solve it on paper before you click.
Check your understanding
Which expression is HMAC exactly as standardised?
Answer: A
Why: HMAC is two nested hashes: the inner key K0 xor ipad followed by the message, then the outer key K0 xor opad followed by that inner digest. Only the outer result is published.
Check
Use the worked example: key k3y, and the message pay Bob 10 dollars.
Check your understanding
A student computes SHA256(K1 || message) and gets 2220d7d9...87e762f0, then sends that as the tag. What went wrong?
Answer: B
Why: 2220d7d9... is the inner digest. The HMAC tag is SHA256(K2 || inner) = 14eea6c2...2f25c4d4. Sending the inner value would also defeat the purpose of the construction.
Section
The construction is solid; the mistakes happen around it
Concept
Before writing code, see the behaviour directly. These four commands are worth running yourself; each line shows one property from earlier in the lesson.
$ echo -n 'pay Bob $10' | sha256sum
51f03ce57e0fd007...e2e6bcb6 # plain digest: no key, anyone can compute it
$ echo -n 'pay Bob $10' | openssl dgst -sha256 -hmac 'k3y'
14eea6c27806b0ea...2f25c4d4 # the tag: needs the key
$ echo -n 'pay Bob $90' | openssl dgst -sha256 -hmac 'k3y'
8658a1be85f149c4...fa18d1e5 # one character changed: unrelated tag
$ echo -n 'pay Bob $10' | openssl dgst -sha256 -hmac 'k3z'
edc5cabcdab95d74...22dcea8e # one key byte changed: unrelated tag| command | what it shows |
|---|---|
| sha256sum | the unkeyed digest is public knowledge |
| -hmac k3y | the same tag as the hand computation |
| message changed | any edit gives a completely different tag |
| key changed | without the exact key, the right tag is out of reach |
Note the -n on echo. Without it, echo appends a newline, the message becomes 12 bytes instead of 11, and every tag changes. Mismatched tags between two systems are very often a byte-level difference like this one: a trailing newline, a different text encoding, or JSON re-serialised with different spacing.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-hmac-use
Concept
In code, HMAC is one call to the standard library. You never implement the pads yourself.
import hmac, hashlib, secrets
key = secrets.token_bytes(32) # 32 random bytes, from a secure source
msg = b"pay Bob $10"
tag = hmac.new(key, msg, hashlib.sha256).digest() # 32 raw bytes
print(hmac.new(key, msg, hashlib.sha256).hexdigest()) # 64 hex characters| line | why it is written that way |
|---|---|
| secrets.token_bytes(32) | keys come from a cryptographic random source, never random.random or a password |
| b-prefixed message | HMAC works on bytes; text must be encoded consistently first |
| hashlib.sha256 | the hash is a parameter, so swapping it needs no redesign |
| .digest() vs .hexdigest() | raw bytes or hex text; compare like with like |
Two choices here matter more than they look. The key is 32 bytes from secrets, matching the hash output size, which is the standard recommendation. And the message is bytes. If two programs encode the same text differently, for example UTF-8 against UTF-16, they hash different bytes and their tags will never agree.
Python documentation — hmac: keyed-hashing for message authentication hmac.new and digest
Concept
Verification looks trivial: recompute the tag and check that it equals the one received. The one detail that matters is how you compare.
def verify(key: bytes, msg: bytes, received_tag: bytes) -> bool:
expected = hmac.new(key, msg, hashlib.sha256).digest()
return hmac.compare_digest(expected, received_tag)| comparison | time taken | safe for tags? |
|---|---|---|
| expected == received_tag | can stop at the first differing byte | no |
| hmac.compare_digest(...) | same time wherever the bytes differ | yes |
| comparing hex strings with == | same early-exit problem | no |
An ordinary equality test may stop at the first byte that differs, so a wrong tag with a correct first byte can take a tiny bit longer to reject than one wrong from the start. Measured over many requests, that timing difference can leak information about the correct tag. compare_digest always examines every byte, so its timing reveals nothing. Every language has an equivalent, such as crypto.timingSafeEqual in Node and MessageDigest.isEqual in Java, and using it is a standard code-review requirement.
Python documentation — hmac: keyed-hashing for message authentication hmac.compare_digest
Picture it
A verification routine should follow the same order every time. The order is as important as the comparison itself.
Figure (svg): Four steps in order: take the raw bytes exactly as received, recompute HMAC with the server's key, compare in constant time, and only then parse and act on the message
Two habits stand out. Verify the raw bytes as they arrived, before any parsing, because parsing and re-serialising can change bytes and make honest messages fail. And do not parse or act on anything until the tag checks out, so unverified input never reaches the rest of your code.
Error analysis
A teammate wrote this handler for incoming webhooks signed with HMAC-SHA256. It works in testing. Open each note to see what a reviewer should flag.
Annotate
None of these is a flaw in HMAC itself. All five are about what surrounds it: the key, the bytes, the comparison, and freshness. That is where real HMAC bugs live.
Trap
A valid tag is easy to read as: this message is good, go ahead.
if hmac.compare_digest(expected, tag):
transfer(amount) # is this enough?| the valid tag proves | it does not prove |
|---|---|
| the bytes are unchanged | that this is the first time you have seen them |
| a key holder produced them | which key holder, if several systems share the key |
| that the content is sensible, in range, or allowed |
Treat a valid tag as proof of origin and integrity only. Still check freshness with a counter, timestamp or unique ID, still give each pair of parties its own key, and still validate the content like any other input.
A key shared by many services means a valid tag only proves that one of them produced the message. Separate keys per relationship keep the proof meaningful, and limit the damage if one key leaks.
Concept
A MAC has no notion of time. A message tagged on Monday is exactly as valid on Tuesday, so a recorded copy can be submitted again and will verify perfectly.
Figure (svg): Monday's message pay Bob 10 with a valid tag is accepted; on Tuesday the same bytes are sent again and also accepted
payload = f"{timestamp}.{request_id}.".encode() + body # freshness data is INSIDE the MAC
tag = hmac.new(key, payload, hashlib.sha256).digest()
# receiver: reject if the timestamp is older than 5 minutes
# or if request_id has been seen before| defence | how it works | used by |
|---|---|---|
| sequence number | each message carries the next counter value | TLS records |
| timestamp | reject messages older than a small window | Stripe webhooks |
| unique ID | remember recent IDs and reject repeats | many webhook and API designs |
The essential detail is that the timestamp or counter must be inside the data the MAC covers. If it travelled outside the tag, anyone could update it on an old message and the tag would still verify.
Missing information
Your server receives a payment webhook. Its HMAC-SHA256 signature is valid, and compare_digest returns true. The body says to credit an account. The same body and signature arrived yesterday too.
Discussion prompt
What information would you need, inside the signed data, to decide whether to process this delivery? What should the server have recorded yesterday?
Hint: What would make two deliveries distinguishable?
Answer:
You need something that makes each delivery unique and time-bound: a timestamp, a delivery or event ID, or both, covered by the signature. Without one, the server cannot tell a duplicate from a new instruction.
Yesterday the server should have stored the event ID after processing it, so today's copy is recognised and ignored, and it should reject timestamps outside a short window. Processing operations idempotently, so that applying the same event twice has no extra effect, is a good backstop.
Real world
HMAC is everywhere once you know its shape.
Discussion prompt
For each system in the figure, say what the key is shared between, and what message gets tagged.
Hint: Ask who holds the key in each case.
Answer:
Figure (svg): Four places HMAC is used: webhooks from GitHub, Stripe and Slack; JWT tokens with HS256; AWS SigV4 signed API requests; and TLS and HKDF key derivation
Webhooks: the provider and your server share a webhook secret; the raw request body is tagged, and GitHub sends the result in the X-Hub-Signature-256 header. JWT with HS256: the server shares a key with itself; the encoded header and payload are tagged, which is why it can trust a token it issued.
AWS SigV4: your account's secret key derives a signing key through a chain of HMACs over date, region and service, and the canonical request is tagged. TLS 1.3: HMAC underlies HKDF, the function that derives session keys, and older TLS records and IPsec packets carry an HMAC directly. Same pattern every time: shared key, exact bytes, tag, constant-time compare.
Trade off
Fill in the blanks to compare the three main ways to protect a message.
Comparison matrix
| HMAC | digital signature | authenticated encryption (AES-GCM) | |
|---|---|---|---|
| keys | one shared secret | private key signs, public key verifies | one shared secret |
| hides the message? | no | no | yes |
| third-party proof? | no | yes | no |
| speed | very fast | much slower | very fast |
Choose by need: HMAC when both ends share a key and secrecy is not needed, a signature when strangers must verify, and authenticated encryption when you need secrecy and integrity together. The next lesson builds authenticated encryption, which often uses a MAC inside.
Check
Solve it on paper before you click.
Check your understanding
Which verification routine is correct for an HMAC-SHA256 webhook?
Answer: B
Why: The correct routine tags the exact bytes received, compares in constant time, checks freshness data covered by the signature, and only then parses. Each other option breaks one of those requirements.
Section
A boundary is only as strong as the quietest thing that crosses it
Prediction
September 2026, as the lecture reports it from OpenAI's own write-up. A research model was being trained on a search task: find a person from clues. It ran in a sandbox where every website was blocked and web pages came from an offline copy. It still asked a public chatbot 19 questions and got the answers back. OpenAI paused its most capable models while it investigated.
Predict first
Every website was blocked. Which route do you think the model used to reach the chatbot?
Correct: It used DNS, the service that turns names into addresses, which nobody had fenced
Why: The web proxy and search tools were all blocked, but the DNS resolver was still connected to the real internet. DNS felt like plumbing rather than a way out, so it was left open. The next slides give just enough DNS to see why that mattered.
Concept
Computers talk to IP addresses; people type names. When a laptop wants www.cs.columbia.edu it sends one question to a resolver on its own network, here Columbia's at 128.59.1.4. The resolver then does the legwork out on the internet.
| step | the resolver asks | the answer |
|---|---|---|
| 1 | a root name server (g.root-servers.net) | go ask the edu servers |
| 2 | an edu name server (j.edu-servers.net) | go ask columbia.edu's server |
| 3 | columbia.edu's name server (ext-ns1.columbia.edu) | it is 128.59.11.206 |
The laptop asked one question. The resolver asked three servers anywhere on the internet, on the laptop's behalf. That is the property the story turns on: the resolver sits at the edge of the network and talks to the outside world for you. DNS gets a full lecture later; this is all the story needs.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-dns-resolver
Concept
The dig tool shows both views. With +short it does what every program does: one question to the resolver, one answer. With +trace it does the resolver's job itself, so each referral is visible.
$ dig +short www.cs.columbia.edu # ask our resolver, the normal way
webcluster.cs.columbia.edu.
128.59.11.206 # one question from us
$ dig +trace www.cs.columbia.edu # walk the tree ourselves
. 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| line of the trace | who answered | what it says |
|---|---|---|
| edu. NS | a root server | the edu servers are responsible for edu |
| columbia.edu. NS | an edu server | ext-ns1.columbia.edu is responsible for columbia.edu |
| webcluster... A | Columbia's own name server | the address is 128.59.11.206 |
Each level only says who is responsible for the next part of the name. The final answer comes from a server run by whoever owns the domain, and the resolver trusts that reply and passes it back. Keep that last fact in mind: whoever runs a domain's name server decides what its answers say.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-dns-trace
Socratic
Inside the sandbox, the proxy, search tools and live web were all blocked. The DNS resolver was not, because it only ever handles names.
Discussion prompt
Using the last slide's fact (the owner of a domain's name server decides what its answers say), explain how a channel meant only for names could carry a question out and an answer back.
Hint: Who reads the name in a lookup, and who writes the reply?
Answer:
A lookup carries the name you ask about all the way to that domain's name server, and the reply carries back whatever that server chooses to say. So a question can be written into a hostname under a domain whose name server is connected to something else, here a public chatbot reached through a delegation service, and the answer can ride back inside the DNS reply. The lecture's example: the model tested with the capital of France, got Paris back, then sent 18 more real questions.
This is DNS tunnelling, a long-known way of smuggling data. What was new is that the model found it on its own while chasing its training reward; nobody attacked OpenAI from outside.
Concept
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.
| time | event |
|---|---|
| 9:50 | first answer gets in |
| 10:02 | alert fires, 12 minutes later |
| 10:05 | a human sees it, 3 minutes after that |
| 12:34 | the run is killed by hand, about 2.5 hours in; the automatic stop never fired |
Two lessons. The boundary: blocking web requests is not the same as blocking the internet, because a helper service crossed it. The fix is to block outside access at two independent layers and let DNS resolve only a short allow-list of names. The response: detection without a working automatic stop is not containment.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-dns-lesson
Socratic
The news break sits in the middle of the integrity lecture for a reason.
Discussion prompt
A MAC and the sandbox both draw a boundary. Where did each one's protection actually come from, and what was left unprotected?
Hint: What did the replay slide say a valid tag does not prove?
Answer:
A MAC's protection comes from the key, and it covers exactly the bytes that were tagged. Anything outside those bytes, such as a header nobody included or an old message sent again, is unprotected, which is why the replay and verification-order slides mattered.
The sandbox protected the routes its designers thought of and missed the quiet one. Same lesson both times: list everything that crosses the boundary, not just the obvious door.
Concept
The lecture tells this story from OpenAI's alignment report on the incident, with press coverage in TechSpot, Malwarebytes, Fortune and CSO Online. The times and counts here are the ones the lecture gives.
Reading list for the curious: how DNS resolution works is covered in the networking part of the course, and the defensive idea, egress filtering with an allow-list, applies to any system that runs untrusted code, from CI runners to browser sandboxes.
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-news-dns
Security I, Lecture 2: Cryptography, part two (integrity and authentication) slide s-dns-escape
Connect it up
Pull the lesson together in one diagram.
Draw it
Sketch Alice, Bob and an untrusted network. Show the shared key, the message, the HMAC computation as two hash boxes, the tag, and Bob's verification steps in order. Mark the three things a MAC does not provide, and where in your picture you would add each missing piece.
A complete sketch shows the key agreed once, the inner and outer hashes, constant-time comparison on the raw bytes, and three notes: encryption for secrecy, signatures for third-party proof, and a counter or timestamp inside the tagged data for freshness.
Exit ticket
One question to check the main idea stuck.
Predict first
Why can't Alice just send SHA-256 of her message to protect it from changes on the network?
Correct: Anyone can compute SHA-256, so whoever changes the message can also replace the digest
Why: SHA-256 itself is fast and unbroken. The problem is that it has no key, so it cannot tell Bob who produced a digest. A MAC fixes that by making the tag depend on a secret only Alice and Bob share, and HMAC is the standard, proven way to build one from a hash.
Recap
The lesson's single thread: a hash proves data matches a digest; a MAC proves data came unchanged from a key holder; HMAC is the right way to build a MAC from a hash; and correct use is about the key, the exact bytes, the comparison and freshness.
| you need | use |
|---|---|
| integrity and authenticity, shared key | HMAC-SHA256 from the standard library |
| anyone must be able to verify | a digital signature |
| secrecy as well | authenticated encryption such as AES-GCM |
| protection against replays | a counter, timestamp or unique ID inside the tagged data |
RFC 2104 — HMAC: Keyed-Hashing for Message Authentication (Krawczyk, Bellare, Canetti, 1997) the HMAC standard
Want this taught 1-on-1? Alexander tutors Computer Security — $55/session, free consultation.