opyeb hmaclab

hmaclab · Safe comparison

Constant-time comparison: keeping time from leaking

Timing attacks, XOR accumulation, per-language functions

The comparison function matters more than it looks. In many languages a string comparison returns false at the first differing byte. That is fast, but it gives an attacker who measures response time a signal about how many bytes were correct.

How a timing attack works

The attacker sends a candidate signature and times the response. A wrong first byte ends the comparison immediately; a correct first byte makes it compare one more byte and take slightly longer. Repeated thousands of times and averaged, the difference reveals bytes one at a time. Network jitter makes this hard over the internet, but a local attacker with many attempts can make it practical.

How constant-time comparison works

The fix is simple. Take two equal-length arrays, XOR every byte and accumulate the result. Do not stop when the accumulator becomes non-zero. Check once at the end whether it is zero.

diff = len(a) ^ len(b) for i in range(n): diff |= a[i] ^ b[i] return diff == 0

Differing lengths mean false. Length is not secret, so comparing it first is fine; just make sure the implementation does not leave a timing signal that matters. hmaclab loops over the longer length and folds the length difference into the accumulator.

Functions by language

LanguageFunctionNote
Node.jscrypto.timingSafeEqual(a, b)Buffers must have equal length or it throws.
Pythonhmac.compare_digest(a, b)Pass the same type; do not mix bytes and str.
Gohmac.Equal(a, b)Returns false safely for different lengths.
JavaMessageDigest.isEqual(a, b)Check that your version compares in constant time.
Browser JSwrite the XOR loopNo built-in function exists.

Normalise before comparing

Mixed case in hexadecimal strings makes even a constant-time comparison fail. Lowercase both sides, strip the prefix and remove whitespace first. That normalisation does not touch the secret, so ordinary functions are fine there.

Constant-time comparison is one piece. Preserving the raw body, checking timestamps and blocking duplicate events must all be in place for real protection.

Questions

Is there no built-in constant-time comparison in JavaScript?

Browser and Node.js standard APIs have no string version. Node.js provides <code>crypto.timingSafeEqual</code> for buffers; in a browser you write the XOR loop.

May I return false immediately on a length mismatch?

Signature length is public, so that early return does not leak a secret. If you want the implementation simple, fold the length difference into the accumulator and compare to the end.

Is constant time enough on its own?

No. Raw body preservation, algorithm choice, timestamp checks and deduplication must also be correct. The comparison is one part of the chain.

See the byte-by-byte comparison on screen.

Open the tool

This guide follows the HMAC definition in RFC 2104 and the public webhook documentation of GitHub, Slack and Stripe. Every example value is synthetic.