MAC-then-Encrypt and Encrypt-and-MAC Pitfalls
What you'll learn: Why the MAC-then-Encrypt and Encrypt-and-MAC constructions expose you to dangerous attacks, unlike the secure Encrypt-then-MAC approach.
The Problem with Alternative Orderings
You've already learned that Encrypt-then-MAC is the gold standard: encrypt your data first, then MAC the ciphertext. But what happens if you do it differently?
MAC-then-Encrypt computes the MAC over plaintext, then encrypts both the plaintext and MAC together. Encrypt-and-MAC encrypts the plaintext while separately MACing the plaintext (producing two independent outputs).
Both approaches sound reasonable—after all, you're still using both encryption and authentication. But they share a fatal flaw: the MAC is verified after decryption.
Why This Order Matters
When you MAC-then-Encrypt, an attacker can send manipulated ciphertexts. Your system must:
- Decrypt the ciphertext (exposing it to padding operations)
- Verify the MAC
If decryption fails due to bad padding before MAC verification, your system might reveal whether padding was valid through timing differences or error messages. This creates a padding oracle—the exact vulnerability you learned about in CBC padding oracle attacks.
Think of it like opening a suspicious package before checking if it's addressed to you. You've already exposed yourself to potential danger before verifying authenticity.
With Encrypt-and-MAC, the MAC doesn't cover the ciphertext at all, so attackers can manipulate ciphertext bits while keeping the plaintext MAC valid—breaking integrity guarantees.
Real Consequences
These aren't theoretical concerns. SSL/TLS originally used MAC-then-Encrypt with CBC mode, leading to decades of padding oracle vulnerabilities (BEAST, Lucky13, POODLE). The timing differences between "bad padding" and "bad MAC" were exploitable even across networks.
Key Takeaway: Only Encrypt-then-MAC is secure by design—it verifies authenticity before any decryption happens, preventing padding oracles and timing attacks. MAC-then-Encrypt and Encrypt-and-MAC expose decryption operations to untrusted input, creating exploitable side channels.