liveBook · Manning

Crypto/subtle

As explained in Hacking Cryptography

crypto/subtle is the Go standard-library package providing cryptographic operations designed to reduce timing side channels.

crypto/subtle provides cryptographic operations designed to reduce timing side channels. Its ConstantTimeCompare function compares byte slices without stopping at the first mismatched byte, making runtime depend on slice length rather than contents. section 10.4.1

ConstantTimeCompare works by:

  • Returning 0 immediately when the slice lengths differ. section 10.4.1
  • XORing corresponding bytes and accumulating every difference instead of short-circuiting. section 10.4.1
  • Using ConstantTimeByteEq to determine whether the accumulated difference is zero. section 10.4.1

Why ordinary comparison leaks information

A memcmp-style loop exits at the first mismatch, so matching a longer prefix takes longer. An attacker can use these timing differences to infer where a comparison failed; ConstantTimeCompare processes every position instead. section 10.4.1

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)
}

Inspect the Go implementation, including its XOR accumulator and ConstantTimeByteEq helper. section 10.4.1

How Hacking Cryptography covers it

How deep it goes
Traces early-exit leakage through implementation details and explains XOR accumulation and constant-time byte equality.
Examples
Uses glibc memcmp behavior and Go ConstantTimeCompare code to contrast early termination with constant-time processing.
What it assumes
Assumes basic byte-slice and XOR knowledge while explaining timing leakage from ordinary comparisons.
What you can do after
Apply it via crypto/subtle.ConstantTimeCompare when comparing equal-length cryptographic byte sequences.
How it connects
Connects constant-time comparison with timing attacks, XOR, byte equality, and cryptographic implementation pitfalls.

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.