SyntaxPilot
Courses/Cybersecurity Fundamentals/Lesson 12

Cybersecurity Fundamentals · Lesson 12 of 90 · 12 min

Integrity and authenticated encryption: MAC, HMAC, AEAD

integrity vs confidentialitymessage authentication code (MAC)HMACAEADAES-GCMChaCha20-Poly1305associated dataauthentication tagnonce reuse failureencrypt-then-MACpadding oracle (concept)bit-flipping (concept)use vetted libraries (libsodium, Tink)

These are the notes for Cybersecurity Fundamentals (defensive), episode 12. Last time you built real AES and ChaCha20 encryption, and the man in the middle couldn't read a thing. Today he doesn't need to read it: he changes Riya's encrypted payment from 500 to 900 rupees without the key. The question is: how do you know a message hasn't been changed? You'll invent each idea from a real-life problem: integrity vs confidentiality and the bit-flipping attack, why a plain hash checksum fails, the MAC (message authentication code) and HMAC, constant-time comparison, encrypt-then-MAC and the padding oracle (as a concept only), AEAD (authenticated encryption with associated data) with AES-GCM, authentication tags, associated data, ChaCha20-Poly1305, nonce reuse, and why you should use vetted libraries like libsodium and Tink. Every term is explained the first time it appears, and the Glossary at the end defines them all again.

Riya, Aman, Ravi and the payments are made up. Every code sample runs as-is on your own machine with Python 3.13 and the cryptography package (version 50.0.1); outputs are real, copied from actual runs. Demos that use a random key print different values on every run (the tags and random bytes below will not match yours, but the ACCEPTED/REJECTED results and errors will). In tracebacks, long file paths are shortened to just the file name.

The problems you'll solve

Problem Idea it teaches
Changing a message you can't read Bit-flipping attack, integrity vs confidentiality
Add a checksum Why a plain hash (SHA-256) isn't enough
A checksum with a key MAC, HMAC, constant-time comparison
Encryption and a MAC: which order? Encrypt-then-MAC, the padding oracle (concept)
One tool for both AEAD, AES-GCM, authentication tag, InvalidTag, associated data, ChaCha20-Poly1305
One repeated nonce Nonce reuse, vetted libraries (libsodium, Tink)

The one-line lesson of the whole episode: secret is not the same as safe.

Problem 1: changing what you can't read

Riya sends her bank an encrypted instruction: PAY RAVI RS 500. She uses AES in CTR mode with a perfect random key. CTR mode (counter mode) turns AES into a stream cipher: AES generates a long stream of random-looking bytes called the keystream, and the ciphertext is simply plaintext XOR keystream. (XOR, written ^ in Python, compares two bits: the result is 1 if they differ and 0 if they're the same. XOR-ing with the same value twice gives back the original.)

The man in the middle can't read the message. But he knows the format of these payment messages, so he knows the amount sits at bytes 12 to 14. And because of XOR:

Flip a bit in the ciphertext, and the same bit flips in the plaintext.

It's like a sealed envelope that someone edits through the paper, without ever opening it.

Step What happens
Riya encrypts PAY RAVI RS 500 Unreadable to everyone without the key
Attacker flips three bytes He knows where the amount sits
Bank decrypts Reads RS 900, and pays it. No error at all

Demo 1: a real bit-flipping attack on AES-CTR

import os
from cryptography.hazmat.primitives.ciphers import (
    Cipher, algorithms, modes)

key, nonce = os.urandom(16), os.urandom(16)   # attacker: neither
aes = Cipher(algorithms.AES(key), modes.CTR(nonce))
ct = bytearray(aes.encryptor().update(b"PAY RAVI RS 500"))

# attacker knows the format: "500" sits at bytes 12-14
for i, (old, new) in enumerate(zip(b"500", b"900")):
    ct[12 + i] ^= old ^ new

print(aes.decryptor().update(bytes(ct)))

Output:

b'PAY RAVI RS 900'

Real AES-CTR, a random key and a random nonce (a "number used once": a value that must be different for every message encrypted with the same key). The attacker never sees either. He only XORs three ciphertext bytes with "5" ^ "9", "0" ^ "0" and "0" ^ "0". Why it works: the ciphertext byte is P ^ K (plaintext XOR keystream). XOR it with P ^ P' and you get P' ^ K, which decrypts to the new plaintext P'. The key never appears.

This is a bit-flipping attack: changing ciphertext so the decrypted plaintext changes in a controlled way. No key, no error, no warning.

Why encryption didn't help: encryption gives confidentiality (nobody can read the data). It does not give integrity (the guarantee that data hasn't been changed). These are two different promises, the C and the I of the CIA triad from episode 2 (Confidentiality, Integrity, Availability), and we need both.

Problem 2: a checksum isn't enough

The obvious fix: attach a checksum, a small value computed from the message that changes if the message changes. Riya attaches the SHA-256 hash of the message. A hash is a function that turns any data into a fixed-size fingerprint; SHA-256 is a widely used hash with a 256-bit (32-byte) output, which you met in episode 10.

But SHA-256 is public and has no key. The attacker changes the message to 900 and simply computes a new SHA-256 for it. Anyone can.

Demo 2: the hash checksum that accepts a fake

import hashlib

def send(msg):                       # message + checksum
    return msg, hashlib.sha256(msg).hexdigest()

def receive(msg, check):
    ok = hashlib.sha256(msg).hexdigest() == check
    return "ACCEPTED" if ok else "REJECTED"

msg, check = send(b"PAY RAVI RS 500")
# attacker changes the message AND recomputes the hash
fake = b"PAY RAVI RS 900"
print(receive(fake, hashlib.sha256(fake).hexdigest()))

Output:

ACCEPTED

Why it fails: a plain hash catches accidents, like a corrupted download. It can't stop an attacker, because the attacker can make one too. We need a checksum that only Riya and the bank can make.

Problem 3: MAC and HMAC

Old letters solved this with a wax seal pressed by a family ring.

Old letters In cryptography Meaning
A family seal ring (only the family has it) Secret key Shared by sender and receiver only
Wax seal on the letter (pressed from letter + ring) MAC tag Computed from the message and the key
Check the seal on arrival (a forger can't press one) Verify Recompute the tag and compare

A MAC (message authentication code) is a short tag computed from the message and a secret key. Change the message, and the tag no longer matches. Without the key, you can't make a valid tag. Making a valid tag without the key is called a forgery, and a good MAC makes forgery practically impossible.

The standard way to build a MAC from a hash function is HMAC (hash-based MAC), defined in RFC 2104 in February 1997 by H. Krawczyk, M. Bellare and R. Canetti. It mixes the key into the hash twice, in a careful way:

HMAC(K, text) = H( K xor opad,  H( K xor ipad, text ) )
ipad = the byte 0x36 repeated,  opad = the byte 0x5C repeated

ipad and opad ("inner padding" and "outer padding") are two fixed constants XOR-ed with the key so that the inner and outer hashes use different versions of the key. RFC 2104 recommends a key at least as long as the hash output (32 bytes for SHA-256). You never need to do this by hand: Python's built-in hmac module does it (Practice 6 shows it's the same thing).

Demo 3: HMAC with Python's hmac module

import hmac, hashlib, secrets

key = secrets.token_bytes(32)        # only Riya and Aman

def tag(msg):
    return hmac.new(key, msg, hashlib.sha256).hexdigest()

def receive(msg, t):
    ok = hmac.compare_digest(tag(msg), t)    # constant time
    return "ACCEPTED" if ok else "REJECTED"

t = tag(b"PAY RAVI RS 500")
print("tag:", t[:32], "...")
print("real msg:", receive(b"PAY RAVI RS 500", t))
print("changed: ", receive(b"PAY RAVI RS 900", t))
# no key: the attacker can only guess a tag
guess = secrets.token_hex(32)
print("guessed: ", receive(b"PAY RAVI RS 900", guess))

Output (the tag is different every run, because the key is random):

tag: 1eea47d7182f2f52babd73c6c07bf37c ...
real msg: ACCEPTED
changed:  REJECTED
guessed:  REJECTED

The real message is accepted. The changed message is rejected, because its tag doesn't match. And the attacker, who has no key, can only guess a 256-bit tag: rejected. MAC = hash + key.

Why compare_digest and not ==

A normal comparison (==) stops at the first different character. So how long it takes leaks how much of a guess was right: a guess with the first 5 characters right takes slightly longer to reject than one with the first character wrong. An attacker who measures those tiny time differences could build a valid tag one character at a time. Using time measurements to learn secrets is a timing attack.

hmac.compare_digest(a, b) takes the same time whether the strings differ at the first character or the last. That's a constant-time comparison. Rule: always compare tags in constant time. You'll meet timing attacks again in episode 27.

Problem 4: encrypt-then-MAC

Now we have two tools: encryption for secrecy and a MAC for integrity. Which order?

plaintext  ->  encrypt (key 1)  ->  MAC over the ciphertext (key 2)
Order What the receiver must do Verdict
Encrypt-then-MAC Check the tag first; decrypt only if it's valid Safe: tampered data is thrown away before any decryption code touches it
MAC-then-encrypt Decrypt first, then check the MAC that was inside Risky: decryption code runs on attacker-controlled data

In encrypt-then-MAC you encrypt first, then compute the MAC over the ciphertext, using a separate key.

The padding oracle (concept only)

Some modes, like CBC, work on fixed-size blocks, so the message is filled out to a whole block with padding bytes, which the receiver checks and removes after decrypting. With MAC-then-encrypt, the receiver has to decrypt and check that padding before it can check the MAC.

In 2002, Serge Vaudenay showed what can go wrong ("Security Flaws Induced by CBC Padding: Applications to SSL, IPSEC, WTLS...", Eurocrypt 2002). Suppose a server replies differently for "bad padding" than for other errors (say "bad MAC"). Then an attacker can send huge numbers of slightly changed ciphertexts and watch which reply comes back. Each reply is a tiny yes/no clue about the plaintext, and with enough of them the attacker can slowly decrypt a message, without the key.

In cryptography, an oracle is anything that answers questions for an attacker. Here the server itself became a padding oracle: it answers "was the padding of this ciphertext correct, yes or no?"

The defensive lesson:

  • Check integrity before touching the data. With encrypt-then-MAC, a changed ciphertext fails the tag check and is rejected before decryption, so there's no padding check to leak.
  • Give the same error for every kind of failure.
  • Better still: don't combine the tools yourself (next problem).

Problem 5: one tool for both (AEAD and AES-GCM)

Gluing encryption and a MAC together yourself is easy to get wrong: wrong order, the same key for both, forgetting to cover something. So modern cryptography gives you one tool that does both. Think of a tamper-evident courier bag:

A courier bag AEAD Promise
Opaque bag (you can't see inside) Encryption Confidentiality
Tamper-evident seal (tears if anyone opens it) Authentication tag Integrity, usually 16 bytes
Address label outside (readable, but glued under the seal) Associated data Not secret, still protected

An authentication tag is the short value (usually 16 bytes) that an AEAD attaches to the ciphertext to prove nothing was changed. AEAD = authenticated encryption with associated data: one tool, both promises.

The most widely used AEAD is AES-GCM: AES in Galois/Counter Mode (GCM), a NIST standard (NIST SP 800-38D, November 2007) that defines GCM as authenticated encryption with associated data. Inside, it's CTR mode plus a fast built-in MAC. The same standard also defines GMAC, a MAC-only variant (a tag with no encryption).

Demo 4: AES-GCM encrypt and decrypt

import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

key = AESGCM.generate_key(bit_length=128)
gcm = AESGCM(key)
nonce = os.urandom(12)               # 96 bits, never reuse

ct = gcm.encrypt(nonce, b"PAY RAVI RS 500", None)
print("15 bytes in ->", len(ct), "bytes out (16-byte tag)")
print(gcm.decrypt(nonce, ct, None))

Output:

15 bytes in -> 31 bytes out (16-byte tag)
b'PAY RAVI RS 500'

A 128-bit key and a 12-byte (96-bit) nonce, the standard size for GCM. Fifteen bytes in, thirty-one out: the ciphertext (same length as the plaintext, because it's CTR inside) plus a 16-byte authentication tag. The third argument, None, is the associated data (none yet).

Demo 5: the same bit-flip attack, against AES-GCM

import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

gcm = AESGCM(AESGCM.generate_key(bit_length=128))
nonce = os.urandom(12)
ct = bytearray(gcm.encrypt(nonce, b"PAY RAVI RS 500", None))

ct[12] ^= ord("5") ^ ord("9")        # the same bit-flip attack
print(gcm.decrypt(nonce, bytes(ct), None))

Output:

Traceback (most recent call last):
  File "tamper.py", line 9, in <module>
    print(gcm.decrypt(nonce, bytes(ct), None))
          ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
cryptography.exceptions.InvalidTag

InvalidTag. decrypt refuses to return anything at all: the tag check failed, so the tampered data never reaches the bank's code. Not even a partial plaintext.

cryptography.exceptions.InvalidTag is an exception: Python's way of signalling an error that stops the normal flow of the program unless you catch it with try / except. Here the exception is not a bug, it's the protection working. In real code, you catch it and reject the message (see Practice 1 and the Mini project).

Demo 6: associated data (readable, but locked)

A message header, to=aman;msg=7. The bank's servers need to read it to route the message, so it isn't encrypted. But it must not change. We pass it as the third argument.

import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

gcm = AESGCM(AESGCM.generate_key(bit_length=128))
nonce = os.urandom(12)
header = b"to=aman;msg=7"            # not secret, must not change

ct = gcm.encrypt(nonce, b"PAY RAVI RS 500", header)
print("right header:", gcm.decrypt(nonce, ct, header))
print("new header:", gcm.decrypt(nonce, ct, b"to=eve;msg=7"))

Output:

right header: b'PAY RAVI RS 500'
Traceback (most recent call last):
  File "ad.py", line 10, in <module>
    print("new header:", gcm.decrypt(nonce, ct, b"to=eve;msg=7"))
                         ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cryptography.exceptions.InvalidTag

With the right header, it decrypts. Change the header to Eve, and: InvalidTag. The header was never secret, yet it's protected by the same tag. That's associated data (AD): data that is authenticated but not encrypted. Note that the header is not stored inside ct: the receiver must already have it (it travels next to the ciphertext, in the clear) and pass the same bytes to decrypt.

Demo 7: ChaCha20-Poly1305, the software-friendly twin

The other modern AEAD is ChaCha20-Poly1305: ChaCha20 (the stream cipher from last episode) for encryption, plus Poly1305, a fast one-time MAC by the same designer. It's what many phones without AES hardware use. It's standardised as AEAD_CHACHA20_POLY1305 in RFC 8439 (June 2018), with a 128-bit (16-byte) tag. Same API as AES-GCM.

import os
from cryptography.hazmat.primitives.ciphers.aead import (
    ChaCha20Poly1305)

key = ChaCha20Poly1305.generate_key()     # 256-bit key
box = ChaCha20Poly1305(key)
nonce = os.urandom(12)

ct = box.encrypt(nonce, b"meet at the chai stall", b"to=aman")
print(len(ct), "bytes = 22 message + 16 tag")
print(box.decrypt(nonce, ct, b"to=aman"))

Output:

38 bytes = 22 message + 16 tag
b'meet at the chai stall'

Thirty-eight bytes: 22 of message, 16 of tag. Decrypts with the right header (b"to=aman" as associated data). Two modern AEADs, AES-GCM and ChaCha20-Poly1305: use one of them and you get secrecy and integrity in one call.

Problem 6: nonce reuse

A developer thinks: "the nonce is just a number, right?" and hardcodes it: nonce = bytes(12) (twelve zero bytes, every time).

Step What happens
nonce = bytes(12) Always zero: a shortcut
Two messages, same key + same nonce GCM is CTR inside, so: same keystream
Messages leak, tags can be forged The seal stops working too: catastrophic

Same key and same nonce make the same keystream, exactly like the reused pad of episode 10. XOR the two ciphertexts and the keystream cancels out, leaving plaintext1 XOR plaintext2. Guess one message and the other falls out.

And it's worse than before: with repeated nonces, an attacker can also work out the secret used to make GCM tags, and then forge valid tags on messages of his choice. RFC 8439 says the same about ChaCha20-Poly1305: a repeated nonce repeats the keystream and the one-time Poly1305 key, and it calls nonce uniqueness "the most important security consideration".

Demo 8: AES-GCM with a repeated nonce (the bug)

from cryptography.hazmat.primitives.ciphers.aead import AESGCM

gcm = AESGCM(AESGCM.generate_key(bit_length=128))
nonce = bytes(12)                    # BUG: same nonce, twice

c1 = gcm.encrypt(nonce, b"ATTACK AT DAWN", None)
c2 = gcm.encrypt(nonce, b"RETREAT AT TEN", None)
leak = bytes(a ^ b for a, b in zip(c1, c2))
print(bytes(a ^ b for a, b in zip(leak, b"ATTACK AT DAWN")))

Output:

b'RETREAT AT TEN'

The eavesdropper XORs the two ciphertexts and guesses the first message, and the second one appears. Even an AEAD, used with a repeated nonce, leaks. (This demo only shows the leak on your own toy data; we don't write forgery code.)

The fix: use a fresh random 12-byte nonce from os.urandom(12) for every message, or a counter that never repeats, and rotate the key (switch to a new one) long before you could run out. Nonce once. Always.

Use vetted libraries: don't assemble crypto yourself

Count the traps from the last three episodes: ECB mode, fixed IVs, reused nonces, encryption without a MAC, and comparing tags with ==. Each one breaks real AES, even though AES itself is fine.

So in real projects, use a vetted library: one that experts have reviewed and tested for years, with safe defaults.

Library What it is
libsodium A popular, easy-to-use crypto library; in its own docs, "a portable, cross-compilable, installable, and packageable fork of NaCl"
NaCl The original crypto library that libsodium was forked (copied and continued) from
Tink (Google) Introduced on the Google Security Blog on 30 Aug 2018; its APIs aim to be "secure, easy to use correctly, and hard(er) to misuse". For example, it doesn't let you pass your own nonce, so you can't reuse one

The rule: pick an AEAD, and let the library handle the details.

The whole chain

Step Problem You invented
1 500 became 900 without a key Bit-flipping; integrity is not confidentiality
2 Add a hash The attacker recomputes it: a hash has no key
3 A hash with a key MAC, HMAC, constant-time compare
4 Which order? Encrypt-then-MAC; the padding oracle
5 One tool for both AEAD, AES-GCM, tag, associated data, ChaCha20-Poly1305
6 nonce = 0 Leaks and forgeries, so: vetted libraries
? What makes a hash function safe in the first place? Next episode

You just invented authenticated encryption.

Common mistakes (and what they print)

Mistake What you see Why it's wrong Fix
AES-CTR / CBC with no MAC b'PAY RAVI RS 900' Encryption is not integrity Use an AEAD (AES-GCM)
sha256(msg) as a checksum ACCEPTED (fake message) Anyone can compute a hash HMAC with a secret key
gcm.decrypt(...) on tampered data, not caught cryptography.exceptions.InvalidTag (program crashes) The check worked; you just didn't handle it Catch InvalidTag, reject the message
nonce = bytes(12) every time b'RETREAT AT TEN' Same keystream twice os.urandom(12) per message
Comparing tags with == No error, just a timing leak Stops at the first different character hmac.compare_digest(a, b)
compare_digest on a str and bytes TypeError: a bytes-like object is required, not 'str' Both sides must be the same type Compare hex with hex, or bytes with bytes
Passing a str as the message TypeError: Cannot convert "<class 'str'>" instance to a buffer. / Did you mean to pass a bytestring instead? AEADs work on bytes "text".encode() or b"..."
Using a password as the key: AESGCM(b"mypassword") ValueError: AESGCM key must be 128, 192, or 256 bits. A key is random bytes of a fixed size AESGCM.generate_key(bit_length=128)
A 4-byte nonce ValueError: Nonce must be between 8 and 128 bytes Too short Use 12 bytes for GCM
Naming your script hmac.py ImportError: cannot import name 'compare_digest' from 'hmac' (consider renaming ...) Your file hides Python's hmac module Rename it, e.g. hmac_demo.py

Mini project: a tiny secure-message helper

A small defensive helper for your own machine: seal() encrypts a message with AES-GCM, using a fresh os.urandom(12) nonce every time and prepending the nonce to the ciphertext (the nonce isn't secret, it just must never repeat). A routing header goes in as associated data. open_msg() splits the nonce back off and rejects anything tampered by catching InvalidTag.

import os
from cryptography.exceptions import InvalidTag
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

KEY = AESGCM.generate_key(bit_length=128)   # shared by Riya and the bank

def seal(text, header):
    nonce = os.urandom(12)                  # fresh nonce, every message
    ct = AESGCM(KEY).encrypt(nonce, text.encode(), header)
    return nonce + ct                       # nonce travels in front

def open_msg(packet, header):
    nonce, ct = packet[:12], packet[12:]
    try:
        return AESGCM(KEY).decrypt(nonce, ct, header).decode()
    except InvalidTag:
        return "REJECTED: message was changed"

header = b"to=bank;msg=7"
packet = seal("PAY RAVI RS 500", header)
print("packet:", len(packet), "bytes = 12 nonce + 15 text + 16 tag")
print("normal:  ", open_msg(packet, header))

flipped = bytearray(packet)
flipped[12 + 12] ^= ord("5") ^ ord("9")     # same 500 -> 900 trick
print("flipped: ", open_msg(bytes(flipped), header))
print("new hdr: ", open_msg(packet, b"to=eve;msg=7"))

again = seal("PAY RAVI RS 500", header)
print("same text, same packet?", again == packet)

Output:

packet: 43 bytes = 12 nonce + 15 text + 16 tag
normal:   PAY RAVI RS 500
flipped:  REJECTED: message was changed
new hdr:  REJECTED: message was changed
same text, same packet? False

What to notice:

  • flipped[12 + 12]: the first 12 bytes are now the nonce, so the "5" sits at position 24 of the packet.
  • Both the bit flip and the changed header are rejected, and the program keeps running instead of crashing.
  • Sealing the same text twice gives a different packet, because the nonce is fresh each time. An eavesdropper can't even tell two identical payments apart.
  • The error message is the same for every failure, so it gives an attacker no extra clues (the padding-oracle lesson).
  • In a real app the key would be stored safely and shared through a proper key exchange, not generated at the top of the script.

Practice

1. Homework: catch InvalidTag. Wrap gcm.decrypt from Demo 5 in try / except so the tampered message is rejected without crashing.

Solution
import os
from cryptography.exceptions import InvalidTag
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

gcm = AESGCM(AESGCM.generate_key(bit_length=128))
nonce = os.urandom(12)
ct = bytearray(gcm.encrypt(nonce, b"PAY RAVI RS 500", None))
ct[12] ^= ord("5") ^ ord("9")

try:
    print(gcm.decrypt(nonce, bytes(ct), None))
except InvalidTag:
    print("REJECTED: tag check failed, message ignored")

Output:

REJECTED: tag check failed, message ignored

Import InvalidTag from cryptography.exceptions. Catch only that exception, not every error, so real bugs still show up.

2. Homework: change "RAVI" to "EVE!" in the bit-flip demo (Demo 1).

Solution

RAVI sits at bytes 4 to 7 (P=0, A=1, Y=2, space=3). Both names are 4 bytes long, so the format stays the same.

import os
from cryptography.hazmat.primitives.ciphers import (
    Cipher, algorithms, modes)

key, nonce = os.urandom(16), os.urandom(16)
aes = Cipher(algorithms.AES(key), modes.CTR(nonce))
ct = bytearray(aes.encryptor().update(b"PAY RAVI RS 500"))

# "RAVI" sits at bytes 4-7
for i, (old, new) in enumerate(zip(b"RAVI", b"EVE!")):
    ct[4 + i] ^= old ^ new

print(aes.decryptor().update(bytes(ct)))

Output:

b'PAY EVE! RS 500'

The payee changed, still without the key. With AES-GCM the same edit would raise InvalidTag.

3. Homework: verify an HMAC with a wrong key.

Solution
import hmac, hashlib, secrets

key = secrets.token_bytes(32)        # the real shared key
wrong = secrets.token_bytes(32)      # someone else's key

msg = b"PAY RAVI RS 500"
t = hmac.new(key, msg, hashlib.sha256).hexdigest()

def verify(k, msg, t):
    mine = hmac.new(k, msg, hashlib.sha256).hexdigest()
    return "ACCEPTED" if hmac.compare_digest(mine, t) else "REJECTED"

print("right key:", verify(key, msg, t))
print("wrong key:", verify(wrong, msg, t))

Output:

right key: ACCEPTED
wrong key: REJECTED

The message is unchanged, but the tag depends on the key too. That's exactly why an attacker without the key can't make a valid tag.

4. Homework: why check the tag first? Explain in two or three sentences.

Solution

If you check the tag first (encrypt-then-MAC, or an AEAD), any changed ciphertext is rejected before decryption code runs, so the attacker never gets to feed crafted data into the decrypt-and-unpad step. If you decrypt first (MAC-then-encrypt), the server's behaviour on attacker-made data, like "bad padding" vs "bad MAC", can become an oracle that slowly leaks the plaintext. Check integrity before touching the data.

5. Fix the nonce bug. Change Demo 8 to use fresh random nonces and run the same XOR trick.

Solution
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

gcm = AESGCM(AESGCM.generate_key(bit_length=128))
n1, n2 = os.urandom(12), os.urandom(12)     # FIX: fresh nonces

c1 = gcm.encrypt(n1, b"ATTACK AT DAWN", None)
c2 = gcm.encrypt(n2, b"RETREAT AT TEN", None)
leak = bytes(a ^ b for a, b in zip(c1, c2))
print(bytes(a ^ b for a, b in zip(leak, b"ATTACK AT DAWN")))

Output (random bytes, different every run):

b'\x10\xcc\x06J\xd3hC"\xa7\xa16Y\xe8h'

Different nonces give different keystreams, so they no longer cancel out and the trick prints garbage.

6. HMAC by hand. Using RFC 2104's formula (ipad 0x36, opad 0x5C, key padded with zeros to SHA-256's 64-byte block), compute an HMAC yourself and check it matches the hmac module.

Solution
import hmac, hashlib

key = bytes(range(32)).ljust(64, b"\0")   # pad key to SHA-256's 64-byte block
msg = b"PAY RAVI RS 500"

ipad = bytes(k ^ 0x36 for k in key)
opad = bytes(k ^ 0x5C for k in key)
inner = hashlib.sha256(ipad + msg).digest()
by_hand = hashlib.sha256(opad + inner).hexdigest()

library = hmac.new(bytes(range(32)), msg, hashlib.sha256).hexdigest()
print(by_hand[:32], "...")
print("same as hmac module:", by_hand == library)

Output:

919f2c24a4aca93511dc60b67487e5bd ...
same as hmac module: True

This is only to understand the formula. In real code, always use hmac.new (and, for comparing tags, hmac.compare_digest).

7. Which tool? (a) Detect accidental corruption of a public download. (b) Prove an API request came from someone holding a shared key, where the request itself isn't secret. (c) Send a secret message that must also not be changed, with a routing header that must stay readable.

Solution

(a) A plain hash (SHA-256) is fine for accidents (but not against an attacker who can also replace the hash). (b) HMAC, compared with compare_digest. (c) An AEAD (AES-GCM or ChaCha20-Poly1305), with the header as associated data and a fresh nonce per message.

Quick quiz

An attacker can't read an AES-CTR message. So is it safe from changes?

A. Yes, encryption stops all changes B. No: add SHA-256(message), that's enough C. No: add an HMAC, or use AES-GCM D. Yes, if the key is 256 bits

Answer

C. Encryption gives confidentiality, not integrity, whatever the key size. A plain hash can be recomputed by anyone. Only a keyed tag, from HMAC or an AEAD like AES-GCM, detects changes.

Interview questions

  1. What's the difference between confidentiality and integrity? Confidentiality means only authorised people can read the data; integrity means nobody can change it without being detected. Encryption alone gives only confidentiality.
  2. What is a bit-flipping attack, and why does it work on CTR mode? Changing ciphertext bits so the plaintext changes in a controlled way. In CTR (and other stream modes) ciphertext = plaintext XOR keystream, so flipping a ciphertext bit flips the same plaintext bit.
  3. Why isn't a SHA-256 checksum enough to protect a message? A hash has no key: an attacker who changes the message just recomputes the hash. It only catches accidental corruption.
  4. What is a MAC, and how is HMAC built? A short tag computed from a message and a secret key; without the key you can't make a valid one. HMAC (RFC 2104) is H(K xor opad, H(K xor ipad, message)), with ipad 0x36 and opad 0x5C.
  5. Why use hmac.compare_digest instead of ==? == stops at the first difference, so its timing leaks how much of a guessed tag was correct (a timing attack). compare_digest takes constant time.
  6. Encrypt-then-MAC or MAC-then-encrypt? Why? Encrypt-then-MAC: the receiver verifies the tag over the ciphertext before decrypting, so tampered data never reaches decryption code. MAC-then-encrypt forces decryption first, which enabled padding-oracle attacks (Vaudenay, 2002).
  7. What is AEAD, and what is associated data? Authenticated encryption with associated data: one primitive giving confidentiality and integrity, like AES-GCM or ChaCha20-Poly1305. Associated data is authenticated but not encrypted, such as a routing header.
  8. What happens if you reuse a nonce with AES-GCM? The keystream repeats, so XOR-ing ciphertexts leaks plaintext, and an attacker can also recover the tag-making secret and forge tags. Use a fresh 12-byte nonce (or a never-repeating counter) per message, or a library like Tink that manages nonces for you.

Glossary

Term Meaning in plain words Example
Confidentiality Only the right people can read the data AES hides PAY RAVI RS 500
Integrity Data can't be changed without it being detected A changed payment is rejected
CIA triad The three security goals: Confidentiality, Integrity, Availability (episode 2) Encryption covers C, a MAC covers I
Bit-flipping attack Changing ciphertext so the decrypted plaintext changes in a controlled way 500 becomes 900 without the key
XOR Bit operation: 1 if the two bits differ, 0 if they're the same (^ in Python) ord("5") ^ ord("9")
CTR mode Counter mode: AES makes a keystream that is XOR-ed with the plaintext modes.CTR(nonce)
Keystream The stream of random-looking bytes a stream cipher XORs with the data Same key + nonce = same keystream
Nonce "Number used once": a value that must differ for every message under one key os.urandom(12)
Nonce reuse Using the same nonce twice with the same key; breaks secrecy and, in GCM, the tags too nonce = bytes(12)
Checksum A small value computed from data that changes when the data changes A hash attached to a download
Hash A function that turns any data into a fixed-size fingerprint; it has no key hashlib.sha256(msg)
SHA-256 A widely used hash function with a 256-bit (32-byte) output 64 hex characters
MAC (message authentication code) A short tag made from a message and a secret key An HMAC-SHA256 tag
Tag The short value a MAC or AEAD produces to prove the data is unchanged 16 bytes for AES-GCM
Forgery Making a valid tag without knowing the key What a good MAC prevents
HMAC Hash-based MAC (RFC 2104): mixes the key into a hash twice hmac.new(key, msg, hashlib.sha256)
ipad / opad HMAC's inner and outer padding constants (bytes 0x36 and 0x5C) XOR-ed with the key k ^ 0x36
Timing attack Learning a secret by measuring how long operations take Guessing a tag one character at a time
Constant-time comparison Comparing values in the same time no matter where they differ hmac.compare_digest
hmac.compare_digest Python's constant-time comparison function for tags hmac.compare_digest(a, b)
Encrypt-then-MAC Encrypt first, then MAC the ciphertext; receiver checks the tag before decrypting The safe order
MAC-then-encrypt MAC the plaintext, then encrypt both; receiver must decrypt first The risky order
Padding Extra bytes that fill a message up to a whole block (in block modes like CBC) Checked after decryption
Oracle Anything that answers yes/no questions for an attacker A server's error messages
Padding oracle A server whose replies reveal whether decrypted padding was valid, which can leak the plaintext "bad padding" vs "bad MAC" (Vaudenay, 2002)
AEAD Authenticated encryption with associated data: secrecy and integrity in one tool AES-GCM, ChaCha20-Poly1305
Authentication tag The tag an AEAD attaches to the ciphertext The last 16 bytes of ct
Associated data (AD) Data that is authenticated but not encrypted b"to=aman;msg=7" header
AES-GCM AES in Galois/Counter Mode: CTR encryption plus a fast built-in MAC AESGCM(key)
GCM / Galois Counter Mode The NIST mode (SP 800-38D, 2007) that turns a block cipher into an AEAD 12-byte nonce, 16-byte tag
GMAC The MAC-only variant of GCM: a tag with no encryption Authenticating public data
ChaCha20-Poly1305 AEAD combining the ChaCha20 cipher with the Poly1305 MAC (RFC 8439) ChaCha20Poly1305(key)
Poly1305 A fast one-time MAC, used with ChaCha20 Makes the 16-byte tag
InvalidTag The exception the cryptography library raises when a tag check fails Tampered ciphertext or header
Exception Python's way of signalling an error; stops the program unless caught with try / except except InvalidTag:
Vetted library A crypto library reviewed and tested by experts for years, with safe defaults libsodium, Tink
libsodium A popular, easy-to-use crypto library, a fork of NaCl Used from many languages
NaCl The original easy-to-use crypto library that libsodium forked libsodium's ancestor
Tink Google's crypto library designed to be hard(er) to misuse; it picks nonces for you Introduced Aug 2018

Summary

  • Encryption hides data but doesn't stop changes. In CTR mode, flipping a ciphertext bit flips the same plaintext bit: a bit-flipping attack. Confidentiality is not integrity.
  • A plain hash checksum fails against attackers: anyone can recompute it.
  • A MAC (like HMAC) is a hash with a key: only key holders can make a valid tag. Compare tags with hmac.compare_digest (constant time).
  • If you must combine tools, encrypt-then-MAC and check the tag first; decrypting first can turn a server into a padding oracle.
  • Better: use an AEAD, AES-GCM or ChaCha20-Poly1305. Tampering raises InvalidTag: catch it and reject the message. Put non-secret headers in associated data.
  • Never reuse a nonce: use os.urandom(12) per message (or a never-repeating counter) and rotate keys.
  • Don't assemble crypto yourself: use vetted libraries like libsodium and Tink.

Next episode: hash functions: one-way functions, collisions, the birthday paradox, why MD5 and SHA-1 broke, SHA-2, SHA-3 and Merkle trees.

Sources: RFC 2104, "HMAC: Keyed-Hashing for Message Authentication" (Feb 1997, H. Krawczyk, M. Bellare, R. Canetti); NIST SP 800-38D (Nov 2007), GCM and GMAC; RFC 8439 (June 2018), ChaCha20 and Poly1305 for IETF Protocols; Serge Vaudenay, "Security Flaws Induced by CBC Padding: Applications to SSL, IPSEC, WTLS...", Eurocrypt 2002; Google Security Blog, "Introducing Tink" (30 Aug 2018); libsodium documentation. Code outputs from Python 3.13 with cryptography 50.0.1. Fetched 29 Oct 2026.