← back to cryptography

MACs and Authentication

Wikipedia · wpMessage authentication code · CC BY-SA 4.0

A message authentication code (MAC) is a short tag computed from a message and a secret key. The receiver recomputes the tag and checks it matches. If it does, the message was not tampered with and came from someone who knows the key. A hash alone is not enough: anyone can compute H(m). A MAC requires the key.

Sender message key MAC message tag Receiver verify key OK Both sides need the same key. Tag proves integrity and authenticity.

HMAC construction

HMAC combines a hash function H with a key K using two passes: HMAC(K, m) = H((K XOR opad) || H((K XOR ipad) || m)). The inner and outer padding constants (ipad = 0x36, opad = 0x5C) ensure the two hash applications behave differently. This construction is provably secure if the underlying hash is a pseudorandom function.

Scheme

Encrypt-then-MAC vs MAC-then-encrypt

There are three ways to combine encryption and authentication. Encrypt-then-MAC (encrypt the plaintext, then MAC the ciphertext) is the recommended approach. The receiver checks the MAC first, rejecting tampered messages before decrypting. MAC-then-encrypt (MAC the plaintext, then encrypt both) is weaker because the receiver must decrypt before verifying integrity, which opens the door to padding oracle attacks.

Scheme
Neighbors

Foundations (Wikipedia)