Message authentication codes
Message Authentication Codes (MACs): symmetric-key primitives that provide message integrity and authentication, including PRF-based MACs, CBC-MAC, HMAC and CMAC, their security definitions (EUF-CMA), constructions, formulas and worked exam-style examples.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Message Authentication Codes (MACs) are used by engineers whenever two systems that share a secret key need to ensure a message was not changed and that it came from the other party. You will see MACs in protocols (e.g. IPsec, TLS record layer, application APIs) where fast symmetric integrity checks are needed and where digital signatures (public-key) are too slow or provide extra properties (non-repudiation) not required here.
Key ideas
Start from first principles and define every term.
Message Authentication Code (MAC): A MAC is a triple of algorithms (KeyGen, Tag, Verify) using a shared secret key k. For any message m:
- Tag generation: the sender computes t = Tag(k, m).
- Verification: the receiver checks Verify(k, m, t) which outputs accept or reject. If accept, receiver believes m came from someone who knows k and that m was not altered.
Properties required:
- Correctness: For all keys k generated by KeyGen and all messages m, Verify(k, m, Tag(k, m)) = accept.
- Existential Unforgeability under Chosen-Message Attack (EUF-CMA): No efficient adversary, given oracle access to Tag(k, ·) for messages of its choice, can produce a new valid (m*, t*) where m* was not queried to the oracle, except with negligible probability. Define the security experiment precisely:
- KeyGen generates k.
- Adversary A has oracle access to Tag(k, ·) and may query messages m1, m2, ... adaptively.
- Eventually A outputs a pair (m*, t*).
- A wins if Verify(k, m*, t*) = accept and m* not equal to any queried mi. For randomized MACs, a stronger notion (SUF-CMA) forbids producing a new tag for a previously queried message; most textbook MACs are deterministic so EUF-CMA and SUF-CMA coincide. The advantage Adv_MAC(A) = Pr[A wins], over random k and A's coins, should be negligible for a secure MAC.
Relation to PRFs (Pseudo-Random Functions): A common method to build a MAC is to use a secure Pseudo-Random Function (PRF) F_k: {0,1}^n -> {0,1}^t and define Tag(k,m) = F_k(m). Informally, if F_k is indistinguishable from a random function, forging a tag is as hard as breaking the PRF. The usual theorem stated in courses:
- Theorem (sketch): If F is a secure PRF then the MAC defined by Tag(k,m)=F_k(m) is EUF-CMA secure (with reductions). Assumptions: messages are single fixed-length blocks equal to PRF input size; for variable length you need domain separation or a PRF-based combiner. A quantitative bound: after q MAC queries, the forgery probability against a random function is about q/2^t for t-bit tags; the PRF reduction adds the PRF distinguishing advantage.
Common constructions and ideas:
CBC-MAC: Build a MAC from a block cipher E_k of block size n. To MAC a fixed-length message split into n-bit blocks m1,...,mℓ:
- Set C0 = 0^n (an all-zero block).
- For i = 1..ℓ compute Ci = E_k(C_{i-1} XOR m_i).
- Tag = C_ℓ. Correctness: deterministic. Security: secure only for fixed-length messages. If variable lengths are allowed without care, CBC-MAC is insecure (classic forgery attacks). Fixes: use length-prepend or use EMAC/CBC-MAC with final encryption with a separate key, or use CMAC. Also, do not use a random IV: CBC-MAC requires C0 = 0^n; using a variable or random IV enables forgeries. Diagram to draw for exam: draw a horizontal chain of boxes for m1, m2, ..., mℓ. Between mi and next box show XOR with previous Ci-1. Each XOR output goes into an encryption box labeled E_k. Label C0 = 0^n. Final output labelled Tag.
CMAC: A standardized secure variant of CBC-MAC (NIST SP 800-38B). CMAC uses a single key but modifies the last block with subkeys derived from the block cipher to handle variable-length messages securely.
HMAC: A MAC built from an iterated cryptographic hash function H (like SHA-256). Define B = block size in bytes of the hash (e.g. 64 for SHA-256). Let opad = byte 0x5c repeated B times, ipad = byte 0x36 repeated B times. HMAC is:
- If |K| > B then K := H(K); if |K| < B pad K with zeros to length B. Then
- Tag = H((K ⊕ opad) || H((K ⊕ ipad) || m)). HMAC resists length-extension attacks that naive Hash(K || m) suffers from and is proven (under reasonable assumptions about H behaving like a PRF on keyed input) to be secure for standard use. Tags may be safely truncated (e.g., to 128 bits); security degrades roughly proportionally to 2^{-t} for t-bit tags, factoring in the number of queries. Another important MAC family uses almost-universal hashing (e.g., Poly1305), commonly deployed inside AEAD schemes.
Comparison with digital signatures: A MAC is symmetric (shared secret) and does not provide non-repudiation. A signature is asymmetric and public-verifiable.
Formulas
t = Tag(k, m) — Tag generation; k: secret key, m: message, t: tag (no SI units)
Verify(k, m, t) ∈ {accept, reject} — verification function; returns accept if tag correct.
CBC-MAC: C0 = 0^n — initial chaining value (n-bit zero block)
CBC-MAC: Ci = E_k(C_{i-1} XOR m_i) for i = 1..ℓ — block-by-block operation; E_k is block cipher with n-bit block, m_i is ith n-bit block
CBC-MAC tag: t = C_ℓ — final chaining value is tag
HMAC: t = H((K ⊕ opad) || H((K ⊕ ipad) || m)) — opad, ipad are B-byte constants (0x5c and 0x36 repeated); K is block-sized key
Security experiment EUF-CMA advantage: Adv_MAC(A) = Pr[Verify(k, m*, t*) = accept and m* not queried] — should be negligible for secure MAC
Notes: E_k, H, F_k have no SI units. Apply formulas when key and message sizes match the primitive assumptions (e.g., CBC-MAC requires block-aligned inputs or a padding rule and/or fixed-length messages).
Worked examples
Example 1 — CBC-MAC (toy numerical block cipher) Problem: Use CBC-MAC with a toy block cipher E_k(x) = (x + k) mod 16 (block size n=4 bits). Key k = 3 (decimal). Message has two blocks m1 = 5, m2 = 9 (decimal). Compute tag and show verification. Steps:
- C0 = 0 (4 bits = 0 decimal).
- Compute C1 = E_k(C0 XOR m1). C0 XOR m1 = 0 XOR 5 = 5. E_k(5) = (5 + 3) mod 16 = 8. So C1 = 8.
- Compute C2 = E_k(C1 XOR m2). C1 XOR m2 = 8 XOR 9 = 1 (since 1000 XOR 1001 = 0001). E_k(1) = (1 + 3) mod 16 = 4. So C2 = 4.
- Tag = C2 = 4. Final answer: Tag = 4 (decimal, 4-bit value). Verification (receiver) repeats steps 1–3 and checks final equals received tag. If equal, accept.
Example 2 — HMAC with a toy hash Problem: Define a toy hash H that maps a byte string to a single byte equal to the sum of bytes modulo 256. Use block size B = 8 bytes. Key K = [1,2,3] (3 bytes), message m = [10,20] (2 bytes). Compute HMAC(K,m) using ipad=0x36, opad=0x5c (in decimal ipad=54, opad=92) repeated to B. Steps:
- Pad K to B bytes: K_padded = [1,2,3,0,0,0,0,0].
- Compute K_ipad by XOR each byte with 54: K_ipad = [1⊕54, 2⊕54, 3⊕54, 0⊕54, ...] = [55, 52, 53, 54, 54, 54, 54, 54].
- Compute inner = H(K_ipad || m). Concatenate bytes: [55,52,53,54,54,54,54,54,10,20]. Sum = 55+52+53+54+54+54+54+54+10+20 = 460. 460 mod 256 = 460 - 256 = 204. So inner = 204.
- Compute K_opad = K_padded XOR 92: [1⊕92, 2⊕92, 3⊕92, 0⊕92, ...] = [93, 94, 95, 92,92,92,92,92].
- Compute outer = H(K_opad || inner). Bytes: [93,94,95,92,92,92,92,92, 204]. Sum = 93+94+95+92+92+92+92+92+204 = 946. 946 mod 256 = 946 - 3*256 = 946 - 768 = 178.
- Tag = 178. Final answer: HMAC(K,m) = 178 (1 byte). Notes: This is a toy example to show XORs and nesting; real HMAC uses SHA-256 producing 32-byte tags.
Example 3 (theory) — short model answer Question: Explain why naive Tag = H(K || m) using an iterated hash H like MD-SHA is unsafe; how HMAC fixes this? Model answer: Iterated hashes are vulnerable to length-extension: given H(K || m) and the length of K || m, an attacker can compute H(K || m || m') without knowing K by using the internal state as if continuing the compression function. HMAC avoids this by hashing K ⊕ ipad inner then hashing the result with K ⊕ opad outside; the key does not appear only at one end so length-extension on the inner digest does not produce a valid final tag without K.
Common mistakes
- Applying CBC-MAC to variable-length messages without a secure variant (e.g., forgetting to prepend length or use CMAC/EMAC).
- Confusing MAC tag length with security level: a 32-bit tag may be forgeable by brute force in about 2^32 attempts; tag length must be chosen to meet required security and protocol limits.
- Not specifying or checking exact padding/bit-length assumptions when writing CBC-MAC or HMAC computations in exams.
For your exam
Types of questions you will get:
- Definitions: define MAC, EUF-CMA, PRF, HMAC, CBC-MAC.
- Short notes: explain CBC-MAC and why fixed-length restriction matters; explain HMAC and its resistance to length-extension.
- Derivations and proofs: show a reduction that a PRF implies EUF-CMA security for the PRF-based MAC (outline is acceptable in exams: build a PRF distinguisher from a MAC forger).
- Numericals and step traces: compute CBC-MAC on toy block cipher; show HMAC inner/outer computations on small inputs.
- Diagrams: draw CBC-MAC chaining as described above.
What a full-marks answer includes:
- Precise definitions (bold terms in notes above), correct assumptions (message length, block sizes), and a clear description of algorithms (KeyGen/Tag/Verify).
- For derivations: state the security experiment, what event constitutes forgery, and the high-level reduction steps.
- For constructions: show step-by-step algorithm (including byte padding and xor with ipad/opad for HMAC), and discuss known limitations/fixes (e.g., CMAC for variable-length CBC-MAC).
Quick check
- What does MAC stand for and what are its two main security goals?
- Why is Hash(K || m) with a Merkle–Damgård hash function risky?
- In CBC-MAC, what is the initial chaining value C0 for the simple construction students first learn?
Answers: 1) Message Authentication Code — integrity and authentication (of origin). 2) Because of length-extension attacks: attacker can extend message and compute new hash without K. 3) C0 = 0^n (an all-zero block).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?