The original HMAC algorithm is as follows:
H(K XOR opad || H(K XOR ipad || text))
What if text
would replaced with H(text)
? Will it weaken the HMAC algorithm?
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It only takes a minute to sign up.
Sign up to join this communityWhat if
text
would replaced withH(text)
? Will it weaken the HMAC algorithm?
Yes. It makes collision attacks on the hash function apply to the MAC, which isn't normally the case with HMAC. You can find a pair $(m, m')$ that hash to the same value, get the MAC for one of them and move it to the other.
That means that your modified HMAC construction would be insecure with a hash function $H$ that isn't collision resistant, such as MD5. Even though normal HMAC-MD5 is still believed to be secure. In fact, any hash function with a small enough output size to lack collision resistance would be insecure to use in this modified HMAC.
If you use a collision resistant hash function, such as one of the SHA-2 hash functions (notably SHA-256 and SHA-512) or SHA-3 then the modified HMAC should still be secure.
Will it weaken the HMAC algorithm?
No, assuming that $H$ is a collision resistant hash function (which is a stronger constraint than what HMAC puts on the hash function).
The security property that a MAC (such as HMAC) has is that "given a large set of $message, tag=HMAC(K, message)$ pairs (where $K$ is an unknown key), where the attacker can choose the messages, the attacker cannot construct a new message, tag pair that validates".
This security property doesn't become any easier for the attacker if he is constrained to pick messages that correspond to images of H. Now, if the attacker could find a collision $H(M_1) = H(M_2)$, he could ask for the $HMAC(K, H(M_1))$, and then he would know $HMAC(K, H(M_2))$. However, if we assume collision resistance, this is infeasible, and so the attacker is limited to trying to break the underlying HMAC algorithm.