L24 · Authenticated Encryption & AEAD

CS 161, Lesson 24, in 50 slides. It explains why a MAC gives no confidentiality, in section 8.6, then combines encryption with a MAC and compares the two orders in section 8.7: encrypt-then-MAC against MAC-then-encrypt, with the padding-oracle break that the latter allows. It covers authenticated encryption with associated data in section 8.8, and AES-GCM together with its catastrophic failure under nonce reuse. It is anchored to textbook sections 8.6 to 8.8.

Subject: Computer Security · 86 slides · applied lesson

Open the interactive version of this deck · Homework for this lesson

What this lesson covers

The lesson, slide by slide

1. Both at Once: Authenticated Encryption

Title

CS 161 · Lesson 24 of 45

a MAC hides nothing · encrypt-then-MAC vs MAC-then-encrypt · AEAD & associated data · AES-GCM and the nonce-reuse cliff

2. By the end of this lesson you can…

Objectives

  1. Explain why a MAC gives integrity but no confidentiality, and build the counterexample F′(K,M) = F(K,M)‖M that stays a secure MAC yet leaks the whole message.
  2. Compare the two ways to combine an IND-CPA cipher with a MAC — encrypt-then-MAC vs MAC-then-encrypt — on ciphertext integrity and decrypt-before-verify.
  3. Describe the padding-oracle attack that broke a real MAC-then-encrypt TLS implementation, and state why encrypt-then-MAC with different keys is the design rule.
  4. Define AEAD and associated data — extra unencrypted-but-authenticated data — and say exactly what AEAD encrypts vs only authenticates.
  5. State why AES-GCM nonce reuse is catastrophic, losing BOTH confidentiality and integrity, and connect it back to L21 CTR IV reuse (the two-time pad).

3. What survived from L23 · Message Authentication Codes: EUF-CMA, AES-EMAC, HMAC?

Warm-up

Discussion prompt

Before we open L24 · Authenticated Encryption & AEAD: without looking back, what was the main idea of L23 · Message Authentication Codes: EUF-CMA, AES-EMAC, HMAC, and what could you do by the end of it that you could not do before?

Hint: One sentence for the idea, one for the skill. If the second one is blank, that is the part to revisit.

Answer:

CS 161, Lesson 23, in 51 slides. It explains what a MAC is and what it guarantees, in sections 8.1 and 8.2, then defines unforgeability through the EUF-CMA forgery game in section 8.3. It covers the AES-EMAC CBC-MAC construction and its two-key final step in section 8.4, and HMAC, built from NMAC with the ipad and opad key transform, in section 8.5 - including why the naive Hash(K||M) is forgeable. It is anchored to textbook sections 8.1 to 8.5.

4. Three questions this lesson answers

Concept

L23 built MACs — integrity and authenticity. But a MAC alone says nothing about secrecy. Today we combine secrecy and integrity into one safe primitive.

Does a MAC hide M?
§8.6 no — a MAC can leak the entire message
How to get both?
§8.7 encrypt-then-MAC (with two keys)
One primitive for both?
§8.8 AEAD — AES-GCM, associated data

5. Which is which: Three questions this lesson answers

Matching

Match the pairs

From Three questions this lesson answers — match each one to what it actually does. The descriptions have been shuffled.

  • c1. Does a MAC hide M?
  • c2. How to get both?
  • c3. One primitive for both?
  • b1. §8.6 no — a MAC can leak the entire message
  • b2. §8.7 encrypt-then-MAC (with two keys)
  • b3. §8.8 AEAD — AES-GCM, associated data

Why: Does a MAC hide M?, How to get both?, One primitive for both? are easy to tell apart while they are sitting next to their descriptions and much harder afterwards, which is what this checks.

6. A MAC Hides Nothing

Section

Part 1 · §8.6 MACs are not confidential

7. §8.6 A scenario: the message rides in the clear

Concept

In L23, Alice sent Bob a message M together with a tag T = MAC(K, M). Bob recomputes the tag and checks it matches — that proves M was not tampered with and really came from someone holding K.

But look at what actually went over the wire: M itself, in plaintext, alongside the tag. A MAC was never trying to hide M — it only certifies M. Integrity is not confidentiality.

8. Break it if you can: §8.6 A scenario: the message rides in the clear

Counterexample

Discussion prompt

In L23, Alice sent Bob a message M together with a tag T = MAC(K, M). Bob recomputes the tag and checks it matches — that proves M was not tampered with and really came from someone holding K.

That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.

Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.

9. §8.6 What a MAC does and does NOT promise

Concept

MAC (message authentication code) — A keyed function T = F(K, M) that gives integrity and authenticity: an attacker who does not know K cannot forge a valid (M, T) pair. It promises unforgeability — and nothing about secrecy.

Nothing in the definition of a secure MAC says F(K,M) hides M. The tag may leak partial — or total — information about the message and still be perfectly unforgeable.

There is also no general 'decrypting' a MAC: Alice and Bob both run the SAME algorithm F to verify. (Some MACs CAN be reversed with the key — e.g. a single-block AES-based MAC — but that is incidental, not the point.)

10. By analogy: §8.6 What a MAC does and does NOT promise

Analogy

Discussion prompt

Explain §8.6 What a MAC does and does NOT promise by analogy to something with no Computer Security in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.

Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.

Answer:

Nothing in the definition of a secure MAC says F(K,M) hides M. The tag may leak partial — or total — information about the message and still be perfectly unforgeable.

11. §8.6 'Unforgeable' and 'hidden' are different jobs

Intuition

Think of a MAC as a tamper-evident seal on a postcard. The seal proves nobody altered the message and that it came from the right sender — but the postcard text is still right there for anyone to read.

A seal that is impossible to forge tells you nothing about whether the contents are visible. Unforgeability (integrity) and invisibility (confidentiality) are orthogonal goals.

Ask yourself: if I could make the seal carry a perfect photocopy of the whole postcard and it were still unforgeable, would it be any less of a 'secure MAC'? (No — which is exactly the counterexample we build next.)

12. Teach it back: §8.6 'Unforgeable' and 'hidden' are different jobs

Explain it

Discussion prompt

Explain §8.6 'Unforgeable' and 'hidden' are different jobs to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.

Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.

Answer:

A seal that is impossible to forge tells you nothing about whether the contents are visible. Unforgeability (integrity) and invisibility (confidentiality) are orthogonal goals.

13. §8.6 The bar a counterexample must clear

Concept

To prove 'secure MAC' implies nothing about secrecy, we don't argue in words — we exhibit a single function that is provably a secure MAC and yet provably leaks the message.

One example that satisfies the MAC definition while broadcasting M settles it: the definition cannot possibly be promising confidentiality, or such a function couldn't exist. That function is F′, next.

14. §8.6 The counterexample: glue the message onto the tag

Concept

Take ANY secure MAC F and define a new function F′ that appends the plaintext to the tag:

\[ F'(K, M) = F(K, M) \,\Vert\, M \]

Here ‖ is concatenation. F′ outputs the original valid tag followed by the entire message. It obviously leaks M — yet, as we'll prove, it is still a perfectly secure MAC.

15. What has to happen first: §8.6 F′ is still unforgeable — but leaks everything

Ranking

Put in order

Put the moves of §8.6 F′ is still unforgeable — but leaks everything into the order they have to happen.

  1. Assume F is a secure MAC: no efficient attacker can forge a valid (M, F(K,M)) pair without K
  2. Suppose an attacker could forge F′ on a NEW message M: a valid F′(K, M) = F(K, M) ‖ M
  3. Strip off the M* suffix — the prefix is exactly F(K, M*), a forgery against F
  4. Note F′ leaks M in the clear: the suffix IS the entire plaintext
  5. Verify the point: F′ is a secure MAC (unforgeable) AND leaks the whole message

Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. This is our only hypothesis — F meets the standard MAC security definition (existential unforgeability under chosen-message attack).

16. §8.6 F′ is still unforgeable — but leaks everything

Worked example

Assume F is a secure MAC: no efficient attacker can forge a valid (M, F(K,M)) pair without K

Why: This is our only hypothesis — F meets the standard MAC security definition (existential unforgeability under chosen-message attack).

Suppose an attacker could forge F′ on a NEW message M: a valid F′(K, M) = F(K, M) ‖ M

Why: A forgery against F′ is a tag the attacker produces for a message it never queried.

Strip off the M* suffix — the prefix is exactly F(K, M*), a forgery against F

Why: F′ just appends M, so the first part of any valid F′ tag is a valid F tag. Forging F′ would forge F — contradicting our assumption.

\[ \text{forge } F' \;\Rightarrow\; \text{forge } F \quad(\text{impossible}) \]

Note F′ leaks M in the clear: the suffix IS the entire plaintext

Why: Anyone who sees the tag F′(K, M) reads M directly off the end — zero confidentiality.

Verify the point: F′ is a secure MAC (unforgeable) AND leaks the whole message

Why: §8.6: 'secure MAC' constrains forgery only. It says nothing about secrecy — a MAC can broadcast the plaintext and still pass.

17. Decode the notation: §8.6 F′ is still unforgeable — but leaks everything

Notation

Annotate

From §8.6 F′ is still unforgeable — but leaks everything — read this one piece at a time. What is each part doing?

On: \( \text{forge } F' \;\Rightarrow\; \text{forge } F \quad(\text{impossible}) \)

  • This is our only hypothesis — F meets the standard MAC security definition (existential unforgeability under chosen-message attack).
  • A forgery against F′ is a tag the attacker produces for a message it never queried.
  • F′ just appends M, so the first part of any valid F′ tag is a valid F tag. Forging F′ would forge F — contradicting our assumption.

18. Plan first: §8.6 A concrete F′: the tag broadcasts the message

Step zero

Discussion prompt

§8.6 A concrete F′: the tag broadcasts the message — before any calculation: what is the plan? Name the moves in order, in plain English, without doing the arithmetic.

Hint: It starts with: Let M = 1011 and suppose the underlying MAC gives F(K, M) = 0110

Answer:

  1. Let M = 1011 and suppose the underlying MAC gives F(K, M) = 0110
  2. Form the F′ output by concatenating the tag with the plaintext
  3. Read the plaintext straight off the suffix of the tag
  4. Verify both claims hold: still unforgeable (prefix is a real F tag), yet fully leaks M

19. §8.6 A concrete F′: the tag broadcasts the message

Worked example

Let M = 1011 and suppose the underlying MAC gives F(K, M) = 0110

Why: Any secure MAC F produces some fixed-length tag; the exact bits don't matter — what matters is what F′ does with them.

Form the F′ output by concatenating the tag with the plaintext

Why: F′ appends M to the right of the genuine tag — the ‖ operator just glues the two bit-strings together.

\[ F'(K, M) = 0110 \,\Vert\, 1011 = 0110\,1011 \]

Read the plaintext straight off the suffix of the tag

Why: Anyone seeing 01101011 takes the last 4 bits — 1011 — and recovers M with no key. The 'tag' literally carries the message.

Verify both claims hold: still unforgeable (prefix is a real F tag), yet fully leaks M

Why: §8.6: confidentiality and unforgeability are independent — this single output is the proof made concrete.

20. Draw the shape of it: §8.6 A concrete F′: the tag broadcasts the…

Blank canvas

Draw it

Draw what §8.6 A concrete F′: the tag broadcasts the message just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.

21. §8.6 The MACs we actually use don't leak — by luck, not by definition

Concept

In practice HMAC and block-cipher-based MACs (CBC-MAC, CMAC) happen NOT to reveal the message — their output looks random. But that is a property of those specific constructions, not something the MAC definition guarantees.

The lesson stands: never rely on a MAC for secrecy. If you need the message hidden, you must encrypt it. A tag is a certificate, not a lockbox.

22. Something is wrong here: 'if a message has a MAC, it's protected'

Anomaly

Predict first

A student writes this, and it looks reasonable:

A student: 'The message is MAC'd, so it's protected — an eavesdropper can't read it.'

It is wrong. Say what breaks — and say it before you turn the page.

Correct: Wrong — and this is THE recurring error of the unit.

A student: what does a MAC actually buy me?

Why: Wrong — and this is THE recurring error of the unit. A MAC gives integrity/authenticity only; the message often rides in plaintext, and even F′ shows a MAC can carry M itself. Integrity ≠ confidentiality.

23. Trap: 'if a message has a MAC, it's protected'

Trap

The trap

A student: 'The message is MAC'd, so it's protected — an eavesdropper can't read it.'

\[ M \,\Vert\, \text{MAC}(K,M) \;\stackrel{?}{\Rightarrow}\; M \text{ is hidden} \]

Equate 'has a MAC' with 'confidential'

Why: Wrong — and this is THE recurring error of the unit. A MAC gives integrity/authenticity only; the message often rides in plaintext, and even F′ shows a MAC can carry M itself. Integrity ≠ confidentiality.

The fix

A student: what does a MAC actually buy me?

\[ \text{MAC} \Rightarrow \text{integrity + authenticity, NOT secrecy} \]

Read a MAC as a tamper-evident seal, then ENCRYPT for secrecy

Why: §8.6: to hide M you must encrypt it; to detect tampering you add a MAC. They are separate goals — combine an IND-CPA cipher AND a MAC to get both.

24. Combining Encryption and a MAC

Section

Part 2 · §8.7 the two orders

25. §8.7 We usually want BOTH properties

Concept

Real protocols need confidentiality AND integrity at the same time: Eve must not be able to read the message, and Mallory must not be able to alter it undetected.

We have the parts: an IND-CPA encryption scheme Enc (L20–L21) and an unforgeable MAC (L23). The question is how to combine them — and the ORDER matters more than you'd expect.

26. §8.7 Encrypt-then-MAC: tag the ciphertext (the safe one)

Concept

Encrypt-then-MAC — Encrypt the message, then compute the MAC over the resulting CIPHERTEXT. Send the ciphertext and that tag. The MAC authenticates what actually travels on the wire.

\[ \text{send: } C = \text{Enc}_{K_1}(M), \quad T = \text{MAC}_{K_2}(C) \]

The receiver checks the tag on C first; only if it verifies does it bother to decrypt. Tampering is caught before decryption ever runs.

27. Take the definitions apart: MAC (message authenticat… vs Encrypt-then-MAC

Definition probe

Sort into buckets

Every line below is part of the definition of MAC (message authentication code) or of Encrypt-then-MAC — one or the other, never both. Put each where it belongs.

MAC (message authentication code)
A keyed function T = F(K, M) that gives integrity and authenticity; an attacker who does not know K cannot forge a valid (M, T) pair.
Encrypt-then-MAC
Encrypt the message, then compute the MAC over the resulting CIPHERTEXT.; Send the ciphertext and that tag.; The MAC authenticates what actually travels on the wire.
b1
A keyed function T = F(K, M) that gives integrity and authenticity: an attacker who does not know K cannot forge a valid (M, T) pair. It promises unforgeability — and nothing about secrecy.
b2
Encrypt the message, then compute the MAC over the resulting CIPHERTEXT. Send the ciphertext and that tag. The MAC authenticates what actually travels on the wire.

28. §8.7 Why tagging the ciphertext is the strong choice

Intuition

The MAC over C gives ciphertext integrity: any change to the bytes on the wire is detected by the tag check — without ever decrypting attacker-controlled input.

And nothing leaks: C already hides M (it's IND-CPA), and the tag is computed over C, so at worst the tag reveals something about the ciphertext — which already reveals nothing about the plaintext.

Ask yourself: where does the receiver's decryption code ever touch unverified attacker bytes? (It doesn't — the tag gate stops bad input at the door.)

29. Plan first: §8.7 Encrypt-then-MAC, step by step

Step zero

Discussion prompt

§8.7 Encrypt-then-MAC, step by step — before any calculation: what is the plan? Name the moves in order, in plain English, without doing the arithmetic.

Hint: It starts with: Sender: encrypt the plaintext under the encryption key K1

Answer:

  1. Sender: encrypt the plaintext under the encryption key K1
  2. Sender: compute the MAC over the CIPHERTEXT under the MAC key K2, and send (C, T)
  3. Receiver: recompute MAC_{K2}(C) and check it equals T — if not, REJECT without decrypting
  4. Verify: only on a passing tag does the receiver compute M = Dec_{K1}(C)

30. §8.7 Encrypt-then-MAC, step by step

Worked example

Sender: encrypt the plaintext under the encryption key K1

Why: First produce the ciphertext that will actually travel on the wire; nothing is authenticated yet.

\[ C = \text{Enc}_{K_1}(M) \]

Sender: compute the MAC over the CIPHERTEXT under the MAC key K2, and send (C, T)

Why: The tag covers C — the exact bytes on the wire — using an independent key K2.

\[ T = \text{MAC}_{K_2}(C); \quad \text{send } (C, T) \]

Receiver: recompute MAC_{K2}(C) and check it equals T — if not, REJECT without decrypting

Why: The tag gate runs first; tampered ciphertext is dropped before the decryption routine ever touches it.

Verify: only on a passing tag does the receiver compute M = Dec_{K1}(C)

Why: §8.7: verify-then-decrypt gives ciphertext integrity and keeps attacker bytes away from the decrypt routine.

31. Say it in words: §8.7 Encrypt-then-MAC, step by step

Translation

\( T = \text{MAC}_{K_2}(C); \quad \text{send } (C, T) \)

Draw it

Translate both ways. First write the expression above as a sentence with no symbols in it at all. Then cover it, and write your sentence back as notation. If the two versions disagree, the disagreement is the thing to fix.

32. §8.7 MAC-then-encrypt: tag the plaintext, then encrypt both

Concept

MAC-then-encrypt — Compute the MAC over the PLAINTEXT, append it to the message, and encrypt the whole thing. The tag is hidden inside the ciphertext and only revealed after decryption.

\[ \text{send: } C = \text{Enc}_{K_1}\big(M \,\Vert\, \text{MAC}_{K_2}(M)\big) \]

Now the receiver has no way to check the tag without first decrypting — and the bytes it decrypts are whatever the attacker put on the wire. That ordering is the seed of a real attack.

33. §8.7 The danger: decrypt-before-verify

Intuition

With MAC-then-encrypt there is no ciphertext integrity. To learn whether a message is authentic, the receiver must run decryption on raw, attacker-chosen bytes — then look inside for a valid tag.

Running your decryption routine on arbitrary input is dangerous: the decryption process itself can leak information through timing, error messages, or padding checks — a side channel.

Ask yourself: in encrypt-then-MAC, does the attacker ever get the decrypt routine to run on chosen bytes? (No — the tag check rejects them first. That's the whole difference.)

34. What has to happen first: §8.7 The padding-oracle attack on MAC-then-encrypt

Ranking

Put in order

Put the moves of §8.7 The padding-oracle attack on MAC-then-encrypt into the order they have to happen.

  1. Setup: a real TLS implementation used MAC-then-encrypt with CBC-mode block encryption
  2. Mallory submits a tampered ciphertext; the receiver decrypts it (no tag gate stops her)
  3. The receiver reacts differently depending on whether the PADDING was valid
  4. Mallory uses that one-bit oracle, ciphertext by ciphertext, to recover the plaintext
  5. Verify the conclusion: MAC-then-encrypt enabled a real decryption attack; encrypt-then-MAC would have blocked it

Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. CBC needs the plaintext padded to a whole number of blocks; the receiver must check that padding is well-formed AFTER decrypting — before it can find and verify the MAC.

35. §8.7 The padding-oracle attack on MAC-then-encrypt

Worked example

Setup: a real TLS implementation used MAC-then-encrypt with CBC-mode block encryption

Why: CBC needs the plaintext padded to a whole number of blocks; the receiver must check that padding is well-formed AFTER decrypting — before it can find and verify the MAC.

Mallory submits a tampered ciphertext; the receiver decrypts it (no tag gate stops her)

Why: Because the MAC is inside the ciphertext, the receiver has to decrypt the attacker's bytes first — exactly the decrypt-before-verify hazard.

The receiver reacts differently depending on whether the PADDING was valid

Why: A padding-error path and a MAC-error path took different times / returned different errors — a side channel leaking one bit: 'was the padding valid?'

Mallory uses that one-bit oracle, ciphertext by ciphertext, to recover the plaintext

Why: Classic padding-oracle math: tweak a byte, watch the padding signal, and peel off the plaintext one byte at a time — no key ever needed.

Verify the conclusion: MAC-then-encrypt enabled a real decryption attack; encrypt-then-MAC would have blocked it

Why: §8.7: tagging the ciphertext means the bad input is rejected before decryption — no padding oracle. This is why encrypt-then-MAC is the better order.

36. §8.7 The non-negotiable rule: use DIFFERENT keys

Concept

In BOTH constructions, the encryption key and the MAC key must be independent: K1 ≠ K2. Never feed one shared key to both the cipher and the MAC.

\[ K_1 \;(\text{encrypt}) \;\neq\; K_2 \;(\text{MAC}) \]

Reusing one key for both opens a whole class of attacks: the security proofs assume the two keys are independent, and interactions between a cipher and a MAC sharing a key can collapse both guarantees.

37. Predict the next row: §8.7 The two orders, side by side

Pattern

Predict first

The table runs: MAC computed over | the ciphertext C | the plaintext M · Ciphertext integrity? | yes | no · Decrypt attacker bytes before verify? | no — tag gate first | yes — must decrypt to find tag · Real-world break | none (the recommended order) | padding-oracle attack on a TLS impl

In §8.7 The two orders, side by side, given the rows so far: what is the next one — the row where Property is Two independent keys??

Correct: Two independent keys? | required (K1 ≠ K2) | required (K1 ≠ K2)

PropertyEncrypt-then-MACMAC-then-encrypt
MAC computed overthe ciphertext Cthe plaintext M
Ciphertext integrity?yesno
Decrypt attacker bytes before verify?no — tag gate firstyes — must decrypt to find tag
Real-world breaknone (the recommended order)padding-oracle attack on a TLS impl
Two independent keys?required (K1 ≠ K2)required (K1 ≠ K2)

Why: The relationship between the columns, not the individual numbers, is what generates the next row. We compare on ciphertext integrity, whether the receiver must decrypt before verifying, and what broke in the real world.

38. §8.7 The two orders, side by side

Worked example

Tabulate the two constructions against the properties that matter

Why: We compare on ciphertext integrity, whether the receiver must decrypt before verifying, and what broke in the real world.

PropertyEncrypt-then-MACMAC-then-encrypt
MAC computed overthe ciphertext Cthe plaintext M
Ciphertext integrity?yesno
Decrypt attacker bytes before verify?no — tag gate firstyes — must decrypt to find tag
Real-world breaknone (the recommended order)padding-oracle attack on a TLS impl
Two independent keys?required (K1 ≠ K2)required (K1 ≠ K2)

Verify the takeaway: encrypt-then-MAC dominates on every security row

Why: §8.7: tag the ciphertext, verify before decrypting, and use two keys. Encrypt-then-MAC is the construction to reach for.

39. Fill in: Encrypt-then-MAC for §8.7 The two orders, side by side

Comparison

Comparison matrix

From §8.7 The two orders, side by side: refill the Encrypt-then-MAC column from what you know. The rest of the table is as it appeared.

PropertyEncrypt-then-MACMAC-then-encrypt
MAC computed overthe ciphertext Cthe plaintext M
Ciphertext integrity?yesno
Decrypt attacker bytes before verify?no — tag gate firstyes — must decrypt to find tag
Real-world breaknone (the recommended order)padding-oracle attack on a TLS impl
Two independent keys?required (K1 ≠ K2)required (K1 ≠ K2)

40. Something is wrong here: 'MAC-then-encrypt is fine — everything is encrypted'

Anomaly

Predict first

A student writes this, and it looks reasonable:

A student: 'The MAC and the message are both inside the ciphertext, so it's all hidden — MAC-then-encrypt is perfectly safe.'

It is wrong. Say what breaks — and say it before you turn the page.

Correct: Hiding the tag does NOT give ciphertext integrity.

A student: which order avoids running decryption on attacker bytes?

Why: Hiding the tag does NOT give ciphertext integrity. The receiver must decrypt attacker-chosen bytes to even find the tag — and that decrypt-before-verify step enabled a real padding-oracle attack against TLS.

41. Trap: 'MAC-then-encrypt is fine — everything is encrypted'

Trap

The trap

A student: 'The MAC and the message are both inside the ciphertext, so it's all hidden — MAC-then-encrypt is perfectly safe.'

Assume 'all encrypted' implies 'integrity-protected'

Why: Wrong. Hiding the tag does NOT give ciphertext integrity. The receiver must decrypt attacker-chosen bytes to even find the tag — and that decrypt-before-verify step enabled a real padding-oracle attack against TLS.

The fix

A student: which order avoids running decryption on attacker bytes?

Use encrypt-then-MAC: verify the tag on the ciphertext FIRST, decrypt only if it passes

Why: §8.7: tagging the ciphertext gives ciphertext integrity — bad input is rejected before decryption, closing the padding-oracle side channel.

42. Something is wrong here: 'one key for both encryption and MAC is fine'

Anomaly

Predict first

A student writes this, and it looks reasonable:

A student: 'I'll save a key — use the same K for the cipher and the MAC. One secret is simpler to manage.'

It is wrong. Say what breaks — and say it before you turn the page.

Correct: The security proofs require independent keys; a cipher and a MAC sharing a key can interact in ways that void BOTH confidentiality and integrity.

A student: how many keys does a combined scheme need?

Why: The security proofs require independent keys; a cipher and a MAC sharing a key can interact in ways that void BOTH confidentiality and integrity. It opens a whole class of attacks.

43. Trap: 'one key for both encryption and MAC is fine'

Trap

The trap

A student: 'I'll save a key — use the same K for the cipher and the MAC. One secret is simpler to manage.'

\[ K_1 = K_2 = K \;\;(\text{share one key}) \]

Reuse a single key across the cipher and the MAC

Why: Wrong. The security proofs require independent keys; a cipher and a MAC sharing a key can interact in ways that void BOTH confidentiality and integrity. It opens a whole class of attacks.

The fix

A student: how many keys does a combined scheme need?

\[ K_1 \neq K_2 \;\;(\text{derive two independent keys}) \]

Use two independent keys — one for Enc, one for MAC (e.g. derived from a master key via a KDF)

Why: §8.7: independence is what the proofs assume. Different keys for encryption and authentication is a hard rule, not a suggestion.

44. AEAD & Associated Data

Section

Part 3 · §8.8 authenticated encryption

45. §8.8 One primitive that does both

Concept

Rather than hand-assembling Enc + MAC, modern systems use a single primitive — authenticated encryption — that provides confidentiality AND integrity/authenticity together, in one vetted block-cipher mode.

AEAD (Authenticated Encryption with Associated Data) — A scheme that encrypts a payload (confidentiality) and authenticates it (integrity), AND additionally authenticates extra 'associated data' that is sent in the clear but must not be tamperable.

46. §8.8 Why a single primitive beats hand-assembly

Intuition

You CAN build authenticated encryption yourself with encrypt-then-MAC — but every hand-assembled scheme is a chance to pick the wrong order, share a key, or mishandle the tag.

AEAD packages the safe construction once, reviewed by experts, behind one call: give it (key, nonce, payload, associated data) and it returns ciphertext + tag. Fewer knobs means fewer ways to cut yourself.

Ask yourself: which is more likely to be secure — a primitive vetted by the whole field, or a one-off you composed at 2am? (That's why the rule is 'use a vetted AEAD, never roll your own.')

47. §8.8 Associated data: authenticated but NOT encrypted

Concept

Associated data (AD) — Extra data sent UNENCRYPTED alongside the ciphertext, but still covered by the integrity tag — so it's readable in transit yet cannot be altered without detection.

Classic example: a plaintext routing header — 'From Alice to Bob' — that the network must read to deliver the packet, but which an attacker must not be able to forge or rewrite.

So AEAD authenticates (encrypted payload + associated data) while only ENCRYPTING the payload. The AD travels in the clear and stays integrity-protected.

48. Term to definition: L24 · Authenticated Encryption & AEAD

Matching

Match the pairs

Match each term to the definition this lesson gave it — not the one you would guess from the word.

  • t1. Encrypt-then-MAC
  • t2. MAC-then-encrypt
  • t3. Associated data (AD)
  • d1. Encrypt the message, then compute the MAC over the resulting CIPHERTEXT. Send the ciphertext and that tag. The MAC authenticates what actually travels on the wire.
  • d2. Compute the MAC over the PLAINTEXT, append it to the message, and encrypt the whole thing. The tag is hidden inside the ciphertext and only revealed after decryption.
  • d3. Extra data sent UNENCRYPTED alongside the ciphertext, but still covered by the integrity tag — so it's readable in transit yet cannot be altered without detection.

Why: These are the working definitions of Encrypt-then-MAC, MAC-then-encrypt, Associated data (AD) as L24 · Authenticated Encryption & AEAD uses them. Pairing them correctly is the test of whether you could state each one with the slide switched off.

49. §8.8 Why some data must stay readable

Intuition

Think of a sealed envelope inside a shipping box. The address label on the outside has to be readable so the courier can deliver it — you can't encrypt the address, or nobody could route the package.

But you still don't want a stranger swapping the label to redirect your package. AD is the address label: visible by necessity, tamper-evident by design.

Ask yourself: should the routing header be encrypted? (No — routers must read it. It must be authenticated, though, so it can't be silently rewritten.)

50. Plan first: §8.8 A packet: what's encrypted vs only authenticated

Step zero

Discussion prompt

§8.8 A packet: what's encrypted vs only authenticated — before any calculation: what is the plan? Name the moves in order, in plain English, without doing the arithmetic.

Hint: It starts with: Build a packet with a plaintext header and a secret body

Answer:

  1. Build a packet with a plaintext header and a secret body
  2. Feed both to AEAD: the body becomes ciphertext, the header stays plaintext, one tag covers both
  3. Verify: the header is readable in transit, but any tamper to header OR body fails the tag

51. §8.8 A packet: what's encrypted vs only authenticated

Worked example

Build a packet with a plaintext header and a secret body

Why: The header 'From Alice to Bob' is associated data the network reads; the body is the private message only Bob should see.

Feed both to AEAD: the body becomes ciphertext, the header stays plaintext, one tag covers both

Why: AEAD encrypts the payload and authenticates payload + AD together, producing ciphertext + a single authentication tag.

FieldRoleEncrypted?Authenticated?
'From Alice to Bob' headerassociated datanoyes
message bodypayloadyesyes
authentication tagintegrity checkn/acovers header + body

Verify: the header is readable in transit, but any tamper to header OR body fails the tag

Why: §8.8: AD gives you readable-yet-tamper-proof metadata; the body gets both secrecy and integrity. One primitive, two protections, plus authenticated headers.

52. Fill in: Encrypted? for §8.8 A packet: what's encrypted vs only…

Comparison

Comparison matrix

From §8.8 A packet: what's encrypted vs only authenticated: refill the Encrypted? column from what you know. The rest of the table is as it appeared.

FieldRoleEncrypted?Authenticated?
'From Alice to Bob' headerassociated datanoyes
message bodypayloadyesyes
authentication tagintegrity checkn/acovers header + body

53. Plan first: §8.8 Forging the header is caught by the tag

Step zero

Discussion prompt

§8.8 Forging the header is caught by the tag — before any calculation: what is the plan? Name the moves in order, in plain English, without doing the arithmetic.

Hint: It starts with: Mallory intercepts a packet: AD header 'From Alice to Bob' +…

Answer:

  1. Mallory intercepts a packet: AD header 'From Alice to Bob' + encrypted body + tag
  2. Mallory rewrites the header to 'From Alice to Mallory' but leaves the tag unchanged
  3. Bob recomputes the AEAD tag over (new header + body) and compares to the received tag
  4. Verify: the tags differ, so Bob REJECTS the forged packet

54. §8.8 Forging the header is caught by the tag

Worked example

Mallory intercepts a packet: AD header 'From Alice to Bob' + encrypted body + tag

Why: The header is readable because it is associated data — sent in the clear so the network can route it.

Mallory rewrites the header to 'From Alice to Mallory' but leaves the tag unchanged

Why: She can edit the plaintext header freely — but the original tag was computed over the OLD header plus the body.

Bob recomputes the AEAD tag over (new header + body) and compares to the received tag

Why: The tag authenticates payload + AD together, so changing the AD changes the value Bob computes.

\[ \text{tag}(\text{header}' \,\Vert\, \text{body}) \neq \text{tag}(\text{header} \,\Vert\, \text{body}) \]

Verify: the tags differ, so Bob REJECTS the forged packet

Why: §8.8: AD is readable but tamper-evident — Mallory could read and rewrite the header, but she cannot make a valid tag without the key.

55. Draw the shape of it: §8.8 Forging the header is caught by the tag

Blank canvas

Draw it

Draw what §8.8 Forging the header is caught by the tag just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.

56. §8.8 Powerful, but fragile

Concept

AEAD is the right default — but it is unforgiving. Because confidentiality and integrity flow from the SAME mechanism, a single misuse can lose both at once.

The most important misuse is reusing a nonce, which we'll see destroys an AEAD scheme completely. AEAD makes the easy path safe, but the misuse path is a cliff, not a slope.

57. Something is wrong here: 'associated data is encrypted'

Anomaly

Predict first

A student writes this, and it looks reasonable:

A student: 'AEAD authenticates the associated data, so the AD is encrypted and hidden too.'

It is wrong. Say what breaks — and say it before you turn the page.

Correct: Associated data is sent in the CLEAR — only the payload is encrypted.

A student: what protection does the AD field actually get?

Why: Associated data is sent in the CLEAR — only the payload is encrypted. AD is authenticated (tamper-evident) but fully readable. Putting a secret in the AD field leaks it.

58. Trap: 'associated data is encrypted'

Trap

The trap

A student: 'AEAD authenticates the associated data, so the AD is encrypted and hidden too.'

Assume 'authenticated' implies 'encrypted'

Why: Wrong. Associated data is sent in the CLEAR — only the payload is encrypted. AD is authenticated (tamper-evident) but fully readable. Putting a secret in the AD field leaks it.

The fix

A student: what protection does the AD field actually get?

Treat AD as plaintext-but-tamper-proof; put anything secret in the encrypted payload

Why: §8.8: AD = authenticated, NOT encrypted. It exists precisely for data that must be readable (headers, nonces, routing) yet unforgeable.

59. The Concrete AEAD: AES-GCM

Section

Part 4 · §8.8 AES-GCM

60. §8.8 AES-GCM: CTR mode with a built-in MAC

Concept

AES-GCM (Galois/Counter Mode) — The most widely used AEAD. At a high level it is a stream cipher operating like AES-CTR — XORing a keystream with the message — with a built-in MAC (GHASH) that is computed as a function of the CTR encryption.

So GCM is essentially AES-CTR for confidentiality + a polynomial MAC for integrity, fused into one mode that also authenticates associated data. One pass, both guarantees.

61. §8.8 Its security mirrors CTR's security

Intuition

Because the encryption part IS essentially CTR mode, GCM inherits CTR's strengths AND its single fatal weakness. CTR is IND-CPA — but only if every message uses a fresh nonce/IV.

Recall L21: CTR generates a keystream from (key, IV) and XORs it with the message — that's a one-time pad with a pseudorandom pad. Reuse the IV and you reuse the pad.

Ask yourself: from L21, what happens when CTR reuses an IV across two messages? (Same keystream ⇒ C ⊕ C′ = M ⊕ M′ — the two-time pad returns.)

62. Teach it back: §8.8 Its security mirrors CTR's security

Explain it

Discussion prompt

Explain §8.8 Its security mirrors CTR's security to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.

Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.

Answer:

Because the encryption part IS essentially CTR mode, GCM inherits CTR's strengths AND its single fatal weakness. CTR is IND-CPA — but only if every message uses a fresh nonce/IV.

63. What has to happen first: §8.8 Why GCM nonce reuse loses BOTH properties

Ranking

Put in order

Put the moves of §8.8 Why GCM nonce reuse loses BOTH properties into the order they have to happen.

  1. Reuse one nonce N with key K on two messages M and M′
  2. Confidentiality falls: identical keystream gives the two-time pad
  3. Integrity ALSO falls: GCM's MAC depends on the CTR encryption, so the repeated nonce exposes the MAC's secret values
  4. Verify: nonce reuse is catastrophic — it destroys confidentiality AND integrity at once

Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. GCM derives its keystream from (K, N) exactly like CTR; a repeated nonce means the SAME keystream encrypts both messages.

64. §8.8 Why GCM nonce reuse loses BOTH properties

Worked example

Reuse one nonce N with key K on two messages M and M′

Why: GCM derives its keystream from (K, N) exactly like CTR; a repeated nonce means the SAME keystream encrypts both messages.

Confidentiality falls: identical keystream gives the two-time pad

Why: C = M ⊕ keystream and C′ = M′ ⊕ keystream, so C ⊕ C′ = M ⊕ M′ — exactly the L21 / L19 key-reuse leak, no key needed.

\[ C \oplus C' = M \oplus M' \quad(\text{two-time pad, again}) \]

Integrity ALSO falls: GCM's MAC depends on the CTR encryption, so the repeated nonce exposes the MAC's secret values

Why: Because the built-in GHASH authentication is a function of the same nonce-derived encryption, two messages under one nonce let an attacker solve for the MAC's internal key and FORGE tags.

Verify: nonce reuse is catastrophic — it destroys confidentiality AND integrity at once

Why: §8.8: this is the AEAD fragility made concrete. A single repeated nonce loses both guarantees — far worse than CTR alone, which only lost confidentiality.

65. Decode the notation: §8.8 Why GCM nonce reuse loses BOTH properties

Notation

Annotate

From §8.8 Why GCM nonce reuse loses BOTH properties — read this one piece at a time. What is each part doing?

On: \( C \oplus C' = M \oplus M' \quad(\text{two-time pad, again}) \)

  • GCM derives its keystream from (K, N) exactly like CTR; a repeated nonce means the SAME keystream encrypts both messages.
  • C = M ⊕ keystream and C′ = M′ ⊕ keystream, so C ⊕ C′ = M ⊕ M′ — exactly the L21 / L19 key-reuse leak, no key needed.
  • Because the built-in GHASH authentication is a function of the same nonce-derived encryption, two messages under one nonce let an attacker solve for the MAC's internal key and FORGE tags.

66. §8.8 Why nonce reuse is WORSE than plain CTR reuse

Intuition

In L21, reusing a CTR IV cost you confidentiality — the two-time pad — but integrity wasn't on the table, because plain CTR never promised it.

GCM bundles integrity in, and ties its MAC to the same nonce-derived encryption. So the one mistake that breaks confidentiality ALSO unravels the MAC's secret — you lose the property you added GCM to get.

Ask yourself: with GCM, is a repeated nonce a 'lose one property' bug or a 'lose everything' bug? (Everything — which is why unique nonces are non-negotiable.)

67. By analogy: §8.8 Why nonce reuse is WORSE than plain CTR reuse

Analogy

Discussion prompt

Explain §8.8 Why nonce reuse is WORSE than plain CTR reuse by analogy to something with no Computer Security in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.

Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.

Answer:

In L21, reusing a CTR IV cost you confidentiality — the two-time pad — but integrity wasn't on the table, because plain CTR never promised it.

68. §8.8 Other AEAD modes, and two ⊕ supplements

Concept

Other AEAD modes exist — CCM, CWC, OCB — but they are out of scope here; AES-GCM is the one to know.

⊕ Beyond this textbook: ChaCha20-Poly1305 is a software-friendly AEAD (no AES hardware needed) used in TLS 1.3 and WireGuard, and AES-GCM-SIV is a nonce-misuse-resistant variant that degrades gracefully if a nonce repeats. Flagged as supplemental — not §8.8 material.

69. Break it if you can: §8.8 Other AEAD modes, and two ⊕ supplements

Counterexample

Discussion prompt

Other AEAD modes exist — CCM, CWC, OCB — but they are out of scope here; AES-GCM is the one to know.

That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.

Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.

70. Something is wrong here: 'AES-GCM tolerates nonce reuse'

Anomaly

Predict first

A student writes this, and it looks reasonable:

A student: 'GCM has a built-in MAC, so even if I repeat a nonce the integrity protects me — reuse is no big deal.'

It is wrong. Say what breaks — and say it before you turn the page.

Correct: Backwards — and catastrophically so.

A student: what is the one rule I must never break with AES-GCM?

Why: Backwards — and catastrophically so. Because the MAC is derived from the CTR encryption, reuse loses BOTH confidentiality (C ⊕ C′ = M ⊕ M′) AND integrity (the attacker can forge tags). The MAC makes reuse worse, not safer.

71. Trap: 'AES-GCM tolerates nonce reuse'

Trap

The trap

A student: 'GCM has a built-in MAC, so even if I repeat a nonce the integrity protects me — reuse is no big deal.'

Assume the built-in MAC makes nonce reuse survivable

Why: Backwards — and catastrophically so. Because the MAC is derived from the CTR encryption, reuse loses BOTH confidentiality (C ⊕ C′ = M ⊕ M′) AND integrity (the attacker can forge tags). The MAC makes reuse worse, not safer.

The fix

A student: what is the one rule I must never break with AES-GCM?

Never reuse a (key, nonce) pair — generate a fresh unique nonce for every message

Why: §8.8: GCM's security mirrors CTR's, so a repeated nonce is the two-time pad PLUS tag forgery. If you cannot guarantee unique nonces, use a nonce-misuse-resistant mode (AES-GCM-SIV).

72. Which of these survive contact with L24 · Authenticated Encryption & AEAD?

Two truths and a lie

Sort into buckets

Some of these hold up and some are the exact mistakes this lesson is built to prevent. Sort them.

Holds up
L23 built MACs — integrity and authenticity. But a MAC alone says nothing about secrecy. Today we combine secrecy and integrity into one safe primitive.; Nothing in the definition of a secure MAC says F(K,M) hides M. The tag may leak partial — or total — information about the message and still be perfectly unforgeable.; Take ANY secure MAC F and define a new function F′ that appends the plaintext to the tag:
Breaks
A student: 'The message is MAC'd, so it's protected — an eavesdropper can't read it.'; A student: 'The MAC and the message are both inside the ciphertext, so it's all hidden — MAC-then-encrypt is perfectly safe.'
sound
These are stated as this lesson states them — each one survives the edge cases L24 · Authenticated Encryption & AEAD puts it through.
flawed
Each of these is lifted from a trap in this deck: reasonable-sounding, and wrong in a way that only shows up once you rely on it.

73. The Decision Recap

Section

Part 5 · §8.6–8.8 choosing a primitive

74. §8.6–8.8 Goal → primitive: the master table

Concept

Pin down what you actually need, then pick the matching primitive. The wrong tool for the goal is how systems get broken.

Your goalUseNotes
Confidentiality onlyAES-CBC or AES-CTRIND-CPA; gives NO integrity
Integrity / authenticity onlyHMAC or AES-based MAC (e.g. CMAC)unforgeable; gives NO secrecy
Both, from partsencrypt-then-MACtag the ciphertext; two independent keys
Both, one primitiveAEAD — AES-GCM+ associated data; NEVER reuse a nonce

75. Fill in: Use for §8.6–8.8 Goal → primitive: the master table

Comparison

Comparison matrix

From §8.6–8.8 Goal → primitive: the master table: refill the Use column from what you know. The rest of the table is as it appeared.

Your goalUseNotes
Confidentiality onlyAES-CBC or AES-CTRIND-CPA; gives NO integrity
Integrity / authenticity onlyHMAC or AES-based MAC (e.g. CMAC)unforgeable; gives NO secrecy
Both, from partsencrypt-then-MACtag the ciphertext; two independent keys
Both, one primitiveAEAD — AES-GCM+ associated data; NEVER reuse a nonce

76. §8.6–8.8 The three rules to carry away

Concept

  1. Never roll your own — use a vetted, standard AEAD rather than hand-gluing a cipher and a MAC.
  2. Encrypt-then-MAC with two keys if you must compose by hand: tag the ciphertext, verify before decrypting, K1 ≠ K2.
  3. Never reuse a nonce under AEAD — a single repeat loses both confidentiality and integrity.

77. Without one step: The authenticated-encryption playbook

Constraint

Discussion prompt

Run The authenticated-encryption playbook with this step confiscated:

Two keys always: K1 ≠ K2 for encryption and MAC — independence is what the proofs require.

Is it still possible? If it is, say what takes its place and what it costs you. If it is not, say exactly what that step was providing that nothing else does.

Hint: A step you can drop for free was never load-bearing. If you cannot drop it, name the thing that goes wrong the moment it is gone.

Answer:

  1. A MAC ≠ secrecy (§8.6): F′(K,M) = F(K,M)‖M is a secure MAC that leaks all of M. Integrity is not confidentiality.
  2. Want both (§8.7): combine an IND-CPA cipher with an unforgeable MAC — order matters.
  3. Encrypt-then-MAC (the safe order): C = Enc_{K1}(M), T = MAC_{K2}(C). Ciphertext integrity; verify the tag BEFORE decrypting.
  4. MAC-then-encrypt (avoid): Enc_{K1}(M‖MAC_{K2}(M)). No ciphertext integrity ⇒ decrypt-before-verify ⇒ real padding-oracle break on TLS.
  5. Two keys always: K1 ≠ K2 for encryption and MAC — independence is what the proofs require.
  6. AEAD (§8.8): one primitive for both, plus associated data (authenticated but NOT encrypted — e.g. a routing header).
  7. AES-GCM: CTR-mode encryption + a built-in MAC; security mirrors CTR ⇒ nonce reuse is catastrophic (loses BOTH), the L21 two-time pad again.
  8. Rules: never roll your own; use a vetted AEAD; never reuse a nonce.

78. The authenticated-encryption playbook

Pattern

  1. A MAC ≠ secrecy (§8.6): F′(K,M) = F(K,M)‖M is a secure MAC that leaks all of M. Integrity is not confidentiality.
  2. Want both (§8.7): combine an IND-CPA cipher with an unforgeable MAC — order matters.
  3. Encrypt-then-MAC (the safe order): C = Enc_{K1}(M), T = MAC_{K2}(C). Ciphertext integrity; verify the tag BEFORE decrypting.
  4. MAC-then-encrypt (avoid): Enc_{K1}(M‖MAC_{K2}(M)). No ciphertext integrity ⇒ decrypt-before-verify ⇒ real padding-oracle break on TLS.
  5. Two keys always: K1 ≠ K2 for encryption and MAC — independence is what the proofs require.
  6. AEAD (§8.8): one primitive for both, plus associated data (authenticated but NOT encrypted — e.g. a routing header).
  7. AES-GCM: CTR-mode encryption + a built-in MAC; security mirrors CTR ⇒ nonce reuse is catastrophic (loses BOTH), the L21 two-time pad again.
  8. Rules: never roll your own; use a vetted AEAD; never reuse a nonce.

79. Where does it stop working: The authenticated-encryption playbook

Edge cases

Discussion prompt

The authenticated-encryption playbook works on the cases you have just seen. Push it to the edge: what is the most degenerate input it still handles — empty, zero, one item, everything equal — and what is the first case where it stops being true? Name the case, not just "it breaks".

Hint: Try the smallest legal input, then the largest, then the one where two things collide. Methods are specified at their edges; the middle takes care of itself.

Answer:

  1. A MAC ≠ secrecy (§8.6): F′(K,M) = F(K,M)‖M is a secure MAC that leaks all of M. Integrity is not confidentiality.
  2. Want both (§8.7): combine an IND-CPA cipher with an unforgeable MAC — order matters.
  3. Encrypt-then-MAC (the safe order): C = Enc_{K1}(M), T = MAC_{K2}(C). Ciphertext integrity; verify the tag BEFORE decrypting.
  4. MAC-then-encrypt (avoid): Enc_{K1}(M‖MAC_{K2}(M)). No ciphertext integrity ⇒ decrypt-before-verify ⇒ real padding-oracle break on TLS.
  5. Two keys always: K1 ≠ K2 for encryption and MAC — independence is what the proofs require.
  6. AEAD (§8.8): one primitive for both, plus associated data (authenticated but NOT encrypted — e.g. a routing header).
  7. AES-GCM: CTR-mode encryption + a built-in MAC; security mirrors CTR ⇒ nonce reuse is catastrophic (loses BOTH), the L21 two-time pad again.
  8. Rules: never roll your own; use a vetted AEAD; never reuse a nonce.

80. Rule out three: Checkpoint — order, AD, and nonces

Elimination

Eliminate the wrong options

Which design choice is the secure one, and why?

3 of these 4 are wrong. Strike them one at a time, and say what rules each one out before you strike the next. The survivor is the answer.

  • A. Encrypt-then-MAC with two independent keys — tag the ciphertext so the receiver verifies before decrypting, giving ciphertext integrity.
  • B. MAC-then-encrypt with two keys — since the tag is hidden inside the ciphertext, everything is encrypted and therefore safe.
  • C. AEAD (AES-GCM), but put the secret payload in the associated-data field so it's both authenticated and encrypted.
  • D. AES-GCM with one fixed nonce reused across messages — the built-in MAC keeps integrity intact even if the nonce repeats.

Survives elimination: A

Why: §8.7: encrypt-then-MAC computes the MAC over the ciphertext C, so the receiver checks the tag FIRST and only decrypts if it passes. That gives ciphertext integrity and never runs decryption on attacker-chosen bytes — exactly what blocks the padding-oracle class of attacks. The encryption and MAC keys must be independent (K1 ≠ K2).

81. Checkpoint — order, AD, and nonces

Check

You're designing a protocol that needs both confidentiality and integrity, and you must combine an IND-CPA cipher with a MAC by hand. Think it through before clicking.

Check your understanding

Which design choice is the secure one, and why?

  • A. Encrypt-then-MAC with two independent keys — tag the ciphertext so the receiver verifies before decrypting, giving ciphertext integrity. (correct)
  • B. MAC-then-encrypt with two keys — since the tag is hidden inside the ciphertext, everything is encrypted and therefore safe.
  • C. AEAD (AES-GCM), but put the secret payload in the associated-data field so it's both authenticated and encrypted.
  • D. AES-GCM with one fixed nonce reused across messages — the built-in MAC keeps integrity intact even if the nonce repeats.

Answer: A

Why: §8.7: encrypt-then-MAC computes the MAC over the ciphertext C, so the receiver checks the tag FIRST and only decrypts if it passes. That gives ciphertext integrity and never runs decryption on attacker-chosen bytes — exactly what blocks the padding-oracle class of attacks. The encryption and MAC keys must be independent (K1 ≠ K2).

Why B tempts people
MAC-then-encrypt hides the tag but gives NO ciphertext integrity: the receiver must decrypt attacker-controlled bytes before it can find and check the tag. That decrypt-before-verify step is exactly what enabled a real padding-oracle attack against a TLS implementation.
Why C tempts people
Associated data is authenticated but NOT encrypted — it is sent in the clear. Putting a secret in the AD field leaks it. Secrets go in the encrypted payload; AD is for readable-but-tamper-proof metadata like headers.
Why D tempts people
AES-GCM nonce reuse is catastrophic, not survivable. Because GCM's MAC is derived from the CTR encryption, a repeated nonce gives C ⊕ C′ = M ⊕ M′ (lost confidentiality) AND lets an attacker forge tags (lost integrity). The built-in MAC makes reuse worse, not safe.

82. Misconceptions to retire

Concept

83. Synthesis — AEAD is the whole symmetric unit, combined safely

Concept

84. Primary sources & where to read more

Concept

85. Connect it up: L24 · Authenticated Encryption & AEAD

Connect it up

Draw it

One page, no notation unless you need it: draw how these connect — A MAC Hides Nothing · Combining Encryption and a MAC · AEAD & Associated Data · The Concrete AEAD: AES-GCM · The Decision Recap. Put an arrow wherever one of them is what makes another possible, and label the arrow with why.

86. Recap — Lesson 24

Recap

You can now explain why a MAC gives no confidentiality (and build F′ to prove it), compare encrypt-then-MAC with MAC-then-encrypt and its padding-oracle break, define AEAD and associated data, and state why AES-GCM nonce reuse is catastrophic — the L21 two-time pad plus tag forgery.

Idea§The one-line version
MAC ≠ secrecy8.6F′(K,M)=F(K,M)‖M is a secure MAC that leaks all of M
Encrypt-then-MAC8.7C=Enc_{K1}(M), T=MAC_{K2}(C); verify before decrypt
MAC-then-encrypt8.7no ciphertext integrity ⇒ TLS padding-oracle break
Two keys8.7K1 ≠ K2 for encryption and MAC — always
AEAD + AD8.8both properties in one; AD authenticated, not encrypted
AES-GCM8.8CTR + built-in MAC; nonce reuse loses BOTH (catastrophic)
Rules8.6–8.8never roll your own; use AEAD; never reuse a nonce

Sources

  1. CS 161 Computer Security Textbook §8.6–8.8 — Wagner, Weaver, Kao, Shakir, Law & Ngai, UC Berkeley — MACs provide integrity but no confidentiality (§8.6), combining encryption and a MAC via encrypt-then-MAC and MAC-then-encrypt (§8.7), and authenticated encryption with associated data including AES-GCM (§8.8)
  2. Authenticated Encryption: Relations among Notions and Analysis of the Generic Composition Paradigm — M. Bellare & C. Namprempre, ASIACRYPT 2000 — proves encrypt-then-MAC is the generically secure composition (ciphertext integrity), while MAC-then-encrypt and encrypt-and-MAC are not generically secure
  3. The Galois/Counter Mode of Operation (GCM) — D. McGrew & J. Viega, 2004; standardized as NIST SP 800-38D — AES-GCM, a CTR-mode stream cipher with a built-in polynomial (GHASH) MAC; security mirrors CTR, so nonce reuse is catastrophic
  4. RFC 8439 — ChaCha20 and Poly1305 for IETF Protocols — Y. Nir & A. Langley, IETF (2018) — the ChaCha20-Poly1305 AEAD used in TLS 1.3 and WireGuard (⊕ supplemental, beyond the §8.8 textbook scope)

Want this taught 1-on-1? Alexander tutors Computer Security — $55/session, free consultation.

Book on Wyzant · Text (657) 465-8108