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
- CWE: 208 Observable Timing Discrepancy
- STRIDE: Information Disclosure
- Impact: Timing can help an attacker infer details about valid signatures or the comparison routine
- Effort: Requires many requests and statistical analysis but is feasible for skilled adversaries
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
- Early exit string or byte comparisons that return on first mismatch
- Per character loops where longer inputs incur more work
- Different code paths for different lengths or encodings
- Verbose error handling that varies by mismatch position
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
- Always parse and canonicalize inputs before comparison
- Perform the same hashing and allocation steps regardless of input length
- Return the same status code and generic error text for all failures
3) Avoid variable error paths
- Do not reveal which field failed
- Do not log timing relevant differences at high latency hot paths
- Throttle abusive clients and apply rate limits
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
- Replace non constant-time compares with constant-time primitives
- Ensure equal code paths for all failure modes
- Return uniform error messages and status codes
- Measure again with large sample sizes and confirm distributions overlap
- Add regression tests that assert constant-time compare is used
Risk summary
- Likelihood: Medium for skilled attackers with good vantage points
- Impact: Information disclosure that can aid larger attack chains
- Overall: Medium, worth fixing because the code change is small and durable
References
- CWE 208 Observable Timing Discrepancy
- Constant-time comparisons in standard crypto libraries