CSS411 Cryptography

MAC and HMAC

Slide deck 16. MACs, their requirements, HMAC design, formula and security.

11 min read

Message authentication codesSearch on YouTube

DefinitionSearch on YouTube

A MAC (message authentication code) is a small fixed-sized block of data. An algorithm generates it from the message and a secret key:

MAC=C(K,M)\text{MAC} = C(K, M)

  • The MAC depends on both the message and the secret key.
  • It is like encryption, but it need not be reversible.
  • The sender appends the MAC to the message as a "signature".
  • The receiver does the same computation on the message and checks that the result matches the MAC.
  • This gives assurance that the message is unaltered and comes from the sender.

The slide figure shows this flow. Source A computes C(K,M)C(K, M) and sends MM with MAC(K,M)\text{MAC}(K, M). Destination B computes C(K,M)C(K, M) with the same KK and compares the two values.

PropertyMACPlain hash
Secret keyNeededNot needed
Authentication of originYesNo
Non-repudiationNoNo (a signature is necessary)
Output sizeFixed small blockFixed digest

MAC with encryptionSearch on YouTube

The MAC gives authentication. You can also use encryption for secrecy.

  • Generally, use separate keys for the MAC and for the encryption.
  • You can compute the MAC before or after the encryption.
  • The slide says that "before" is generally regarded as better, "but see Generic Composition".

The Generic Composition analysis shows a different result. It shows that "MAC after encryption" (encrypt-then-MAC) is the order that is always secure. The receiver then checks the MAC before decryption.

Why use a MACSearch on YouTube

  • Sometimes only authentication is needed.
  • Sometimes authentication must persist longer than the encryption, for example in archival use.

A MAC is not a digital signature. It does not give non-repudiation. The sender and the receiver share the key KK. Thus a third party cannot tell which of the two made a given MAC.

MAC propertiesSearch on YouTube

Cryptographic checksumSearch on YouTube

A MAC is a cryptographic checksum:

MAC=CK(M)\text{MAC} = C_K(M)

It condenses a variable-length message MM, with a secret key KK, to a fixed-sized authenticator.

  • It is a many-to-one function. Potentially many messages have the same MAC.
  • But it must be very difficult to find these messages.

A MAC is like a wax seal that only two people can make. The receiver can check that the seal is correct. But a judge cannot tell which of the two people made it.

RequirementsSearch on YouTube

The MAC must satisfy these requirements, because of the types of attacks:

  1. If an attacker knows a message and its MAC, it is infeasible to find another message with the same MAC.
  2. MACs must be uniformly distributed. For an nn-bit MAC, two random messages have the same MAC with probability 2−n2^{-n}.
  3. The MAC must depend equally on all bits of the message.

Three MAC requirements: N-U-E. No second message with the same MAC, Uniform distribution, Equal dependence on all bits.

HMACSearch on YouTube

ComponentsSearch on YouTube

HMAC (hash-based message authentication code) is a MAC that uses a cryptographic hash function, like SHA-256 or SHA-512, with a secret key. It verifies both the integrity and the authenticity of a message.

HMAC combines:

  • A secret key KK, known only to the sender and the receiver.
  • A hash function HH, such as SHA-256, SHA-512 or MD5.

Then it does two hashing operations with two special padding constants:

ConstantValueRole
ipad0x360x36 repeatedInner pad, used in the inner hash
opad0x5C0x5C repeatedOuter pad, used in the outer hash

Each pad is repeated to the hash block size, so that it can be XORed with the padded key.

Formula and notationSearch on YouTube

HMAC(K,M)=H((K⊕opad) ∥ H((K⊕ipad) ∥ M))\text{HMAC}(K, M) = H\left( (K \oplus \text{opad}) \,\Vert\, H\left( (K \oplus \text{ipad}) \,\Vert\, M \right) \right)

SymbolMeaning
HHCryptographic hash function (for example, SHA-256)
KKSecret key (padded or truncated to the block size)
MMMessage
∥\VertConcatenation
⊕\oplusXOR operation

ProcessSearch on YouTube

  1. Prepare the key.
    • If the key KK is longer than the block size, hash it.
    • If KK is shorter, pad it with zeros to the block size (for example, 64 bytes for SHA-256).
  2. Inner hash. Compute InnerHash=H((K⊕ipad) ∥ M)\text{InnerHash} = H((K \oplus \text{ipad}) \,\Vert\, M).
  3. Outer hash. Compute HMAC=H((K⊕opad) ∥ InnerHash)\text{HMAC} = H((K \oplus \text{opad}) \,\Vert\, \text{InnerHash}).
HMAC(K, M) = H((K⁺ ⊕ opad) ‖ H((K⁺ ⊕ ipad) ‖ M))K⁺ ⊕ ipadipad = 0x36…‖ Mappend messageHInner hashK⁺ ⊕ opadopad = 0x5C…‖ inner hashHHMACK⁺: the key paddedto the block sizeThe receiver computes the same value and compares.
HMAC: the inner hash covers the key XOR ipad and the message. The outer hash covers the key XOR opad and the inner hash.

Read the formula from the inside out. The inner hash uses ipad and the message. The outer hash uses opad and the inner hash.

The sender and the receiver both compute this value. If the results match, the message is verified.

ExampleSearch on YouTube

The slide example uses hash function SHA-512, key KK and the message "Hello":

HMAC(K,“Hello”)=SHA512((K⊕opad) ∥ SHA512((K⊕ipad) ∥ “Hello”))\text{HMAC}(K, \text{``Hello''}) = \text{SHA512}\left( (K \oplus \text{opad}) \,\Vert\, \text{SHA512}\left( (K \oplus \text{ipad}) \,\Vert\, \text{``Hello''} \right) \right)

The same steps in code form:

inner = sha512((K xor ipad) || "Hello")
tag   = sha512((K xor opad) || inner)

SHA-512 has a block size of 1024 bits (128 bytes), so the key is padded to 128 bytes.

HMAC in practiceSearch on YouTube

SecuritySearch on YouTube

  • Integrity: if one bit of the message changes, the HMAC output changes very much.
  • Authentication: only a person who knows the secret key can make a valid HMAC.
  • Resistance to length extension attacks: plain hash-based verification, such as SHA256(M)\text{SHA256}(M), has this weakness. HMAC prevents attackers from appending extra data.

H(M)H(M) alone does not give authentication. An attacker who changes MM can also calculate the new H(M)H(M). The attacker cannot calculate a valid HMAC without the secret key.

Use casesSearch on YouTube

  • TLS / HTTPS: verify data packets between the client and the server.
  • IPSec / VPNs: message integrity.
  • JWT (JSON Web Tokens): HMAC-SHA256 signs the tokens.
  • APIs: authentication headers verify the requests.

Design objectivesSearch on YouTube

  1. Use hash functions without modifications.
  2. Allow easy replacement of the embedded hash function.
  3. Preserve the original performance of the hash function, without significant degradation.
  4. Use and handle keys in a simple way.
  5. Have a well understood cryptographic analysis of the strength of the authentication mechanism.

To remember the pads: ipad is 0x360x36 ("in", the smaller number), and opad is 0x5C0x5C ("out"). Both are repeated to the hash block size.

Quick self-testSearch on YouTube

Define a MAC in one line, with its formula.

A MAC is a small fixed-sized block of data, generated from the message and a secret key, and appended to the message: MAC=C(K,M)\text{MAC} = C(K, M). The receiver computes it again and compares.

What assurance does the receiver get from a matching MAC?

The message is unaltered and comes from the sender. This is true because only the two parties know the secret key.

Why is a MAC not a digital signature?

The two parties share the secret key. A third party cannot tell which party made a given MAC. Thus a MAC does not give non-repudiation.

Must you compute the MAC before or after encryption?

The slide says that "before" is generally regarded as better, but refers to Generic Composition. That analysis shows that encrypt-then-MAC is always secure. In both cases, use separate keys for the MAC and the encryption.

When is a MAC useful without encryption?

When only authentication is necessary. Also when authentication must persist longer than the encryption, for example in archival use.

Give the three requirements for a MAC.

If an attacker knows a message and its MAC, it is infeasible to find another message with the same MAC. MACs are uniformly distributed. The MAC depends equally on all bits of the message.

Write the full HMAC formula and define each symbol.

HMAC(K,M)=H((K⊕opad) ∥ H((K⊕ipad) ∥ M))\text{HMAC}(K, M) = H((K \oplus \text{opad}) \,\Vert\, H((K \oplus \text{ipad}) \,\Vert\, M)). HH is the hash function. KK is the secret key, padded or truncated to the block size. MM is the message, ∥\Vert is concatenation and ⊕\oplus is XOR.

Give the three steps of the HMAC process.

Prepare the key: hash it if it is longer than the block size, or pad it with zeros if it is shorter. Inner hash: H((K⊕ipad) ∥ M)H((K \oplus \text{ipad}) \,\Vert\, M). Outer hash: H((K⊕opad) ∥ InnerHash)H((K \oplus \text{opad}) \,\Vert\, \text{InnerHash}).

Give the security properties and two use cases of HMAC.

Integrity: one changed message bit changes the output very much. Authentication: only key holders can make a valid HMAC. Resistance to length extension attacks. Use cases: TLS/HTTPS, IPSec/VPNs, JWT HMAC-SHA256 and API authentication headers.

Give the five HMAC design objectives.

Use hash functions without modifications. Allow easy replacement of the embedded hash function. Preserve the original performance of the hash function. Use and handle keys in a simple way. Have a well understood cryptographic analysis of the strength of the mechanism.

  • A MAC is a small fixed-sized block made from the message and a secret key: MAC=C(K,M)\text{MAC} = C(K, M). It shows that the message is unaltered and comes from the sender. It does not give non-repudiation.
  • Use separate keys for the MAC and for encryption. The slide prefers MAC before encryption, but Generic Composition favours encrypt-then-MAC.
  • MAC requirements: no second message with the same MAC, uniform distribution, equal dependence on all bits.
  • HMAC uses a secret key KK, a hash function HH, ipad (0x360x36) and opad (0x5C0x5C).
  • HMAC(K,M)=H((K⊕opad) ∥ H((K⊕ipad) ∥ M))\text{HMAC}(K, M) = H((K \oplus \text{opad}) \,\Vert\, H((K \oplus \text{ipad}) \,\Vert\, M)). Prepare the key, do the inner hash, then do the outer hash.
  • HMAC gives integrity, authentication and resistance to length extension. It is used in TLS, IPSec, JWT and APIs.