CSEC3616Cybersecurity Engineering

    Message authentication codes and HMAC

    Why a plain hash cannot authenticate a sender, what a MAC adds, and HMAC's exact construction, structure and the reasoning behind why HMAC-MD5 is still safe to use.

    • Explain why hashing alone, and message encryption alone, both fail to provide message authentication.
    • State the two requirements a MAC function must satisfy.
    • Reproduce the HMAC formula and the seven-step algorithm that computes it, including the ipad and opad constants.
    • Explain why HMAC-MD5 remains safe to use even though MD5 itself is broken.

    18 min read

    Intuition

    A bare hash fails to authenticate a sender because computing one needs no secret: an attacker can substitute a message and recompute a matching hash just as easily as the real sender could. Fixing this means tying the hash to something the attacker does not have. A Message Authentication Code (MAC) does exactly that, by mixing a shared secret key into the computation. It is sometimes called a keyed hash function for this reason.

    Mechanism

    Message authentication needs a function that generates an authenticator, a value used to authenticate a message, which a higher-level protocol then checks. Three classes of function can generate one:

    Hash function alone. Insufficient on its own, an unprotected hash is exactly the person-in-the-middle weakness from the previous topic.

    Symmetric message encryption. Encrypting with a shared key lets the receiver infer the message came from someone who knows the key, but an attacker can still flip bits of the ciphertext without knowing that key. If the plaintext happens to be an arbitrary bit sequence, the receiver has no way to tell the resulting decrypted garbage from a genuine message.

    Public-key encryption. Plain public-key encryption gives confidentiality but not authentication: anyone can encrypt a forged message with the recipient’s public key and substitute it undetected. Encrypting with the sender’s private key instead gives authentication (only the sender could have produced it) but drops confidentiality, since anyone with the sender’s public key can decrypt it. Combining both, sign with the sender’s private key, then encrypt with the receiver’s public key, gives both properties, at the cost of running the public-key algorithm four times per message instead of two.

    Mechanism

    The MAC itself. A fixed-size tag, generated from the message and a secret key shared by both parties, and appended to the message. Also called a cryptographic checksum.

    Formula

    MAC

    MAC=C(K,M)MAC = C(K, M)
    MM
    the input message
    KK
    the secret key, shared between sender and receiver
    CC
    the MAC function

    Sent alongside the original message, unlike encryption, which replaces it.

    Mechanism

    The receiver recomputes the MAC on the received message with the shared key and compares it to the one that was sent. Assuming the key is known only to sender and receiver, a match assures the receiver of three things: the message was not altered (an attacker without the key cannot produce a matching MAC for a modified message), the message’s origin (only a key holder could have generated a valid MAC), and, if a sequence number is included, that it was not tampered with either.

    A MAC by itself provides none of this alongside confidentiality. Confidentiality is layered on separately, by encrypting either before or after the MAC computation.

    Mechanism

    A function used as a MAC must satisfy two requirements. First, it must have the resistance properties of a cryptographic hash function. Second, given a message mm and its MAC tt, it must be computationally infeasible to predict a correct MAC for a different message mmm' \neq m, even when other (m,t)(m, t) pairs are known. A cryptographic hash function satisfies the first requirement on its own; mixing in a secret satisfies the second, provided the resistance properties still hold once the secret is added.

    Mechanism

    HMAC is the most widely used way to build a MAC from a hash function: published as RFC 2104, mandatory for IPsec, used in SSL, and standardised by NIST as FIPS 198.

    Formula

    HMAC

    HMAC(K,M)=H[(Kopad)H[(Kipad)M]]HMAC(K, M) = H\big[(K \oplus \text{opad}) \Vert H[(K \oplus \text{ipad}) \Vert M]\big]
    HH
    any cryptographic hash function, e.g. MD5, SHA-1, SHA-2
    KK
    the shared secret key
    MM
    the input message
    ipad,opad\text{ipad}, \text{opad}
    publicly known constant bit strings, required for HMAC's security proof to hold

    ipad = 0x36 repeated for the block length; opad = 0x5c repeated for the block length.

    Mechanism

    In the notation the lecture uses for the structure: bb is the number of bits in one block, LL the number of blocks in MM, K+K^+ is KK padded with zeros on the left to reach bb bits, YiY_i is the ii-th block of MM for 0iL10 \leq i \leq L-1, and nn is the length of the hash code HH produces.

    Worked example

    AnswerThe HMAC algorithm runs H twice: once over (K+ xor ipad) concatenated with M, once over (K+ xor opad) concatenated with that result.

    1. Append zeros to the left of KK to build a bb-bit string K+K^+. For example, if KK is 160 bits and b=512b = 512, this needs 512160=352512 - 160 = 352 zero bits, equivalently 44 bytes, appended.
    2. XOR K+K^+ with ipad\text{ipad} to produce the bb-bit block SiS_i.
    3. Append MM to SiS_i.
    4. Apply HH to the stream from step 3.
    5. XOR K+K^+ with opad\text{opad} to produce the bb-bit block SoS_o.
    6. Append the hash result from step 4 to SoS_o.
    7. Apply HH to the stream from step 6 and output the result. This is HMAC(K,M)HMAC(K, M).

    Pitfall

    The notes describe the padding in the worked example above as “44 zeroes” for a 160-bit key against a 512-bit block. Read literally as bits, that is wrong: 160+44=204160 + 44 = 204, nowhere near the required 512 bits. The figure only works out if read as 44 bytes, which is 44×8=35244 \times 8 = 352 bits, exactly the gap between 160 and 512. When the notes’ padding counts look inconsistent with the stated block size, check whether bits or bytes are meant before assuming an error.

    Mechanism

    Security of HMAC. The security of any hash-based MAC depends on the strength of the underlying hash. What makes HMAC specifically appealing is that its designers proved an exact relationship between the strength of the embedded hash function and the strength of HMAC built on top of it.

    Worked example

    AnswerYes, HMAC-MD5 is still safe to use, because attacking HMAC needs online observation under an unknown key, not offline computation.

    1. MD5 has a 128-bit output. By the birthday bound, roughly 2642^{64} attempts give a 50% chance of a collision, feasible with today’s technology.
    2. Attacking MD5 directly can be done entirely offline: the attacker knows the algorithm and the default IV, so they can compute the hash of any message they choose without needing to observe anything.
    3. Attacking HMAC is different. The attacker does not know the secret key KK, so they cannot generate (m,t)(m, t) pairs offline. They must instead observe a sequence of messages generated by HMAC under the same unknown key.
    4. For a 128-bit hash, this requires observing about 2642^{64} blocks, or 2722^{72} bits, generated under one unchanging key.
    5. On a 1-Gbps link, observing that much traffic under a single unchanged key would take roughly 150,000 years, making the attack infeasible with today’s technology.
    6. Answer: yes, HMAC-MD5 remains safe to use. If raw speed matters and a choice is available, the slides note HMAC-MD5 can be preferred over HMAC-SHA-1 purely for that reason, not because MD5’s own weakness threatens HMAC.

    Exam detail

    Keep the two attack models distinct: attacking a hash function directly is an offline search over any input the attacker chooses, since the algorithm and its IV are public. Attacking a MAC built from that same hash function is an online search that requires observing real traffic under a secret, unknown key. That difference in threat model, not any special strength added by HMAC’s construction, is why “MD5 is broken” does not automatically mean “HMAC-MD5 is broken.”

    Recall

    Why can a broken hash function like MD5 still be used safely inside HMAC, when it cannot be trusted on its own?

    Because the two are attacked differently. MD5 alone can be attacked entirely offline, since anyone can compute its hash for any chosen message. HMAC adds a secret key the attacker does not know, forcing them to observe real message-and-tag pairs generated under that key instead of computing their own, which for MD5’s 128-bit output pushes the practical cost up to roughly 150,000 years of continuous observation on a 1-Gbps link.