← All posts

Field notes / Security

Timing Attacks on Signature Validation

TLDR

If signature validation takes measurably different time for different inputs, you have a timing side channel. Attackers can use statistics over many requests to infer valid signature properties. Fix it by using constant-time comparison, normalizing work, and returning uniform responses.

Background

Timing attacks exploit small variations in how long a system takes to process inputs. In signature verification, non constant-time comparisons often exit early on the first mismatch. That makes shorter or partially correct inputs return faster or slower than others. Over thousands of requests, an attacker can average away noise and learn about valid signatures bit by bit.

What I observed

During a recent assessment, signature validation exhibited consistent timing differences tied to signature length. Across multiple endpoints, one class showed the largest delta at about 210 ms between short and very long incorrect signatures. Others varied from minimal to roughly 175 ms. Differences of this size are statistically significant when sampled at scale.

Why it matters

How to measure it safely

Only test systems you are authorized to test.

Shell quick check

# Send requests with different signature lengths and capture total time
# Substitute URL and headers for your target
for s in "WRONG" "WRONGLONGSIGNATURETHATDOESNOTMATCH"; do
  /usr/bin/time -f "%E total for $s" curl -sS -o /dev/null \
    -H "Authorization: Signature $s" \
    -H "Date: $(date -u +'%a, %d %b %Y %H:%M:%S GMT')" \
    https://example.test/api/resource
done

Python sampler with basic stats

import time, statistics, requests

URL = "https://example.test/api/resource"
HEAD = {"Date": "Thu, 01 Jan 1970 00:00:00 GMT"}

def measure(sig, n=50):
    dt = []
    for _ in range(n):
        h = HEAD.copy()
        h["Authorization"] = f"Signature {sig}"
        t0 = time.perf_counter()
        r = requests.get(URL, headers=h, timeout=10)
        t1 = time.perf_counter()
        dt.append((t1 - t0) * 1000.0)
    return dt

short = measure("WRONG", n=100)
long  = measure("WRONGLONGSIGNATURETHATDOESNOTMATCH", n=100)

print("short ms avg", statistics.mean(short), "stdev", statistics.stdev(short))
print("long  ms avg", statistics.mean(long),  "stdev", statistics.stdev(long))

Look for non overlapping distributions or a consistent mean difference after many samples. Network noise exists, so collect enough data and run from a stable vantage point.

Root causes

How to fix it

1) Use constant-time comparison

Always compare computed vs provided signatures with dedicated constant-time primitives. These consume the full input and do not short circuit on mismatch.

Go

import (
  "crypto/hmac"
  "crypto/sha256"
)

func verify(sigProvided, key, msg []byte) bool {
  mac := hmac.New(sha256.New, key)
  mac.Write(msg)
  expected := mac.Sum(nil)
  return hmac.Equal(expected, sigProvided) // constant time
}

Python

import hmac, hashlib

def verify(sig_provided: bytes, key: bytes, msg: bytes) -> bool:
    expected = hmac.new(key, msg, hashlib.sha256).digest()
    return hmac.compare_digest(expected, sig_provided)  # constant time

Node

const crypto = require("crypto");

function verify(sigProvided, key, msg) {
  const expected = crypto.createHmac("sha256", key).update(msg).digest();
  if (sigProvided.length !== expected.length) return false;
  return crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(sigProvided));
}

2) Normalize the work

3) Avoid variable error paths

4) Optional padding and jitter

You can add small fixed delay to failed validations to narrow timing spread. Treat this as a supplemental measure, not a substitute for constant-time comparison.

Verification checklist

Risk summary

References

← Back to Blog

← Back to the blog