MAC and HMAC
Slide deck 16. MACs, their requirements, HMAC design, formula and security.
MAC / HMAC - Message Authentication Code / Hash Bashed Message Authentication CodesPractical Networking · why a hash alone is not enough, then MAC and HMAC, 7 min
MAC & HMAC (with Animations)Cyber Security Entertainment · MAC and HMAC with animations, 6 min
Message Authentication Codes: CMAC and HMACCryptography · CMAC and HMAC constructions, 10 min
105 keyed-Hash Message Authentication Code HMAC: Get a Gut Level UnderstandingBrent Bilger · HMAC inner and outer hash, ipad and opad, 7 minMessage 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:
- 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 and sends with . Destination B computes with the same and compares the two values.
| Property | MAC | Plain hash |
|---|---|---|
| Secret key | Needed | Not needed |
| Authentication of origin | Yes | No |
| Non-repudiation | No | No (a signature is necessary) |
| Output size | Fixed small block | Fixed 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 . 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:
It condenses a variable-length message , with a secret key , 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:
- If an attacker knows a message and its MAC, it is infeasible to find another message with the same MAC.
- MACs must be uniformly distributed. For an -bit MAC, two random messages have the same MAC with probability .
- 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 , known only to the sender and the receiver.
- A hash function , such as SHA-256, SHA-512 or MD5.
Then it does two hashing operations with two special padding constants:
| Constant | Value | Role |
|---|---|---|
| ipad | repeated | Inner pad, used in the inner hash |
| opad | repeated | Outer 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
| Symbol | Meaning |
|---|---|
| Cryptographic hash function (for example, SHA-256) | |
| Secret key (padded or truncated to the block size) | |
| Message | |
| Concatenation | |
| XOR operation |
ProcessSearch on YouTube
- Prepare the key.
- If the key is longer than the block size, hash it.
- If is shorter, pad it with zeros to the block size (for example, 64 bytes for SHA-256).
- Inner hash. Compute .
- Outer hash. Compute .
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 and the message "Hello":
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 , has this weakness. HMAC prevents attackers from appending extra data.
alone does not give authentication. An attacker who changes can also calculate the new . 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
- Use hash functions without modifications.
- Allow easy replacement of the embedded hash function.
- Preserve the original performance of the hash function, without significant degradation.
- Use and handle keys in a simple way.
- Have a well understood cryptographic analysis of the strength of the authentication mechanism.
To remember the pads: ipad is ("in", the smaller number), and opad is ("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: . 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.
. is the hash function. is the secret key, padded or truncated to the block size. is the message, is concatenation and 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: . Outer hash: .
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: . 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 , a hash function , ipad () and opad ().
- . 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.