MACs based on Hash Functions

Explore the fascinating world of Message Authentication Codes built on cryptographic hash functions. Learn, interact, and master the concepts that secure our digital communications.

What are MACs?

A Message Authentication Code (MAC) is a cryptographic checksum that provides both data integrity and authentication. It ensures that a message hasn't been tampered with and verifies the identity of the sender.

Message

Original data to be authenticated

Secret Key

Shared between sender & receiver

MAC Algorithm

Combines message + key

MAC Tag

Authentication code

MACs are essential for secure communication protocols like TLS, IPSec, and many others. They provide assurance that the message came from someone who knows the secret key and that the message hasn't been modified in transit.

Hash-based MAC Algorithms

HMAC (Hash-based Message Authentication Code)

The most widely used hash-based MAC, defined in RFC 2104. HMAC can work with any cryptographic hash function and provides strong security guarantees.

HMAC(K, m) = H((K ⊕ opad) || H((K ⊕ ipad) || m))

Where:
- K = secret key
- m = message
- H = hash function (SHA-256, SHA-512, etc.)
- opad = outer padding (0x5c repeated)
- ipad = inner padding (0x36 repeated)
- || = concatenation

NMAC (Nested MAC)

The predecessor to HMAC, using nested hash function calls. Less practical but theoretically important.

NMAC(K1, K2, m) = H(K1 || H(K2 || m))

Where K1 and K2 are derived from the original key K

KMAC (Keccak Message Authentication Code)

Based on the SHA-3 family, providing variable-length output and enhanced security features.

KMAC(K, X, L, S) = KECCAK[c](encode_string(K) || encode_string(X) || right_encode(L) || encode_string(S), L)

Interactive HMAC Demo

Try generating HMAC values with different inputs to see how the algorithm works in practice.

Click "Generate HMAC" to see the result...

Security Properties

Property Description Importance
Unforgeability Cannot create valid MAC without the secret key Critical
Collision Resistance Hard to find two messages with same MAC High
Key Recovery Resistance MAC values don't reveal the secret key Critical
Deterministic Same input always produces same output Essential
Efficient Fast computation for practical use Medium

Applications & Use Cases

🔐 Network Security

TLS/SSL protocols use HMAC for message authentication in secure web communications, ensuring data integrity during transmission.

🔑 API Authentication

REST APIs use HMAC signatures to verify request authenticity, preventing unauthorized access and tampering.

💾 Data Storage

File systems and databases use MACs to detect unauthorized modifications to stored data.

🔄 Password Storage

HMAC is used in password-based key derivation functions (PBKDF2) for secure password hashing.