Skip to main content
2 of 2
replaced http://security.stackexchange.com/ with https://security.stackexchange.com/

A good Message Authentication Code algorithm should be resistant to forgeries: imagine that the Bad Guy has access to a box which computes HMAC/SHA-1 over inputs that he chooses and provides; internally, the box uses a secret key. The goal of the attacker is to build a pair (m,h) where h is a correct MAC for message m, without using m as an input to the box. If you prefer, the attacker feeds N inputs to the box and tries to end with N+1 valid MAC values. Key recovery would be even worse (if the attacker recovers the key, then he can produce as many (m,h) pairs that he wishes to).

If the MAC output has length n bits, and the attacker can succeed by sending less than 2n-1 queries to the box and spending less than 2n-1 MAC invocations worth of CPU, then the MAC algorithm is declared "broken" (at least in an academic way). Currently, HMAC/SHA-1 is NOT broken (even academically), and since its output is n = 160 bits, and a CPU effort of 2159 is totally out of reach of mankind, we can say that nobody can produce forgeries on HMAC/SHA-1, let alone recover the key, given input/output pairs (even if the attacker gets to choose the input values in these pairs).

The above assumes that the key is itself taken from a set of possible keys of size at least 2n. If you use a key with less than n bits, then the attacker can just try random sequences of n bits until a match is found. That's the generic exhaustive search attack. If the key has length at least 80 bits or so, this is not feasible with today's technology (with a 128-bit key, you have a very substantial security margin). Then again, this assumes that the set of possible keys is really that large, i.e. that you used a cryptographically strong PRNG to generate the key -- see @D.W.'s answer for details.

Thomas Pornin
  • 328.6k
  • 60
  • 800
  • 969