liveBook · Manning

Constant-time comparison

As explained in Hacking Cryptography

A constant-time comparison compares values without execution time depending on the position or content of mismatches, aside from permitted length handling.

A constant-time comparison prevents execution time from revealing where two values first differ. A short-circuit comparison can let an attacker guess a secret byte by byte by measuring server response times. section 10.4.1

The early-exit failure

A comparison that stops at the first mismatch runs longer when more leading bytes match. The glibc memcmp implementation loops until a differing byte appears, so its runtime reflects the number of successfully matched bytes. An attacker can use repeated measurements to refine guesses. section 10.4.1

The defensive pattern checks equal lengths, scans every byte, accumulates x[i] ^ y[i] with bitwise OR, and tests whether the accumulated value is zero. Runtime then depends on slice length rather than contents. The OR accumulator preserves any discovered difference; addition or XOR could incorrectly cancel differences or introduce overflow concerns. section 10.4.1 section 10.7

Listing 10.2 Constant-time comparisons in the crypto/subtle Go package
func ConstantTimeCompare(x, y []byte) int {
  if len(x) != len(y) {
    return 0
  }

  var v byte

  for i := 0; i < len(x); i++ {
    v |= x[i] ^ y[i]
  }

  return ConstantTimeByteEq(v, 0)
}

// ...

func ConstantTimeByteEq(x, y uint8) int {
  return int((uint32(x^y) - 1) >> 31)
}

The Go crypto/subtle implementation makes the constant-time structure concrete: length handling precedes a full XOR-and-OR scan, followed by an equality test. section 10.4.1

Apply these defensive rules:

  • Do not use ordinary bytes.Compare or memcmp for secrets when content-dependent timing matters. section 10.4.1
  • Use an implementation that scans all equal-length inputs and treats length handling explicitly. section 10.4.1

How Hacking Cryptography covers it

How deep it goes
Explains short-circuit leakage and a Go implementation, including accumulator behavior, but does not develop timing-measurement methodology.
Examples
Uses decimal-guessing intuition, glibc memcmp, and Go's crypto/subtle ConstantTimeCompare.
What it assumes
Assumes familiarity with byte arrays and basic bitwise operations, while explaining short-circuit timing leakage directly.
What you can do after
Apply Go's crypto/subtle.ConstantTimeCompare or an equivalent full-scan comparison for equal-length secret values.
How it connects
Connects timing attacks, memcmp, XOR, bitwise OR, and cryptographic implementations.

Also explained in

Compare how 2 books explain constant-time comparison →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.