Course contentsShow
← Security
Lesson 222 of 3,0525. Cryptographic Hashing and MACsFree lesson

Encrypt-then-MAC vs MAC-then-Encrypt

Compare different MAC and encryption composition modes and understand why encrypt-then-MAC is preferred.

Encrypt-then-MAC vs MAC-then-Encrypt

What you'll learn: Why the order of encryption and MAC operations matters critically for security.

When combining encryption and MACs to achieve authenticated encryption, you have three choices for ordering:

  1. Encrypt-then-MAC (EtM): Encrypt plaintext first, then MAC the ciphertext
  2. MAC-then-Encrypt (MtE): MAC the plaintext first, then encrypt everything
  3. Encrypt-and-MAC (E&M): Separately encrypt plaintext and MAC plaintext

Why Order Matters

Think of it like sealing a letter in an envelope with a tamper-evident seal:

Encrypt-then-MAC (preferred): You seal the letter in the envelope, then apply a tamper-evident sticker on the outside. Anyone can check if the envelope was opened without touching the contents inside. If the sticker is broken, you reject it immediately—no need to open the envelope.

MAC-then-Encrypt: You write a signature on the letter, seal it in an envelope, then someone must open the envelope to verify the signature. This exposes you to attacks during decryption before authentication.

The Security Problem

With MAC-then-Encrypt and Encrypt-and-MAC, you must decrypt before verifying authenticity. This opens the door to padding oracle attacks, timing attacks, and other exploits that manipulate ciphertext and observe decryption behavior.

With Encrypt-then-MAC, you verify authenticity first. If the MAC fails, you reject the message immediately without ever decrypting it. This prevents entire classes of attacks.

Real-World Impact

TLS historically used MAC-then-Encrypt with CBC mode, leading to devastating attacks like BEAST and Lucky 13. Modern AEAD modes (AES-GCM, ChaCha20-Poly1305) all follow the Encrypt-then-MAC principle internally.

Key Takeaway: Always use Encrypt-then-MAC when composing encryption and authentication primitives manually—or better yet, use proven AEAD constructions that handle this correctly by design.