Prevent timing attacks 开 cryptographic 操作
Timing attacks exploit 执行时间 variations 到 leak secrets. Learn 常量-时间 comparison, 均匀过程, 和 blinding techniques.
- 1
timing 攻击 measures 如何长 操作 takes 到 infer 信息关于 secret. 为示例, 字符串 comparison 该 returns early 开第一 mismatch leaks 如何许多 characters 匹配. Defense: 所有操作开 secret 数据必须 take 时间 independent 的数据值.
- 2 哈希对比器 →
比较 hashes in constant time. This tool performs a byte-by-byte comparison that always processes all bytes, regardless of where the first mismatch occurs. Never use === to compare hashes or HMACs — it short-circuits and leaks timing information.
- 3 HMAC 生成器 →
生成 an HMAC for authentication. When verifying, use a constant-time comparison function (crypto.timingSafeEqual in Node.js). The comparison must touch every byte regardless of match position.
- 4
为 RSA, use blinding: multiply 消息通过 random 值 r^e 之前签名, 然后 divide 结果通过 r. 此 randomizes computation 时间和 prevents timing-based 键恢复. 现代 crypto libraries (OpenSSL, Web Crypto) 此内部 — 总是 use 库函数, 从不 implement crypto 从 scratch.
常见问题
- 如何 timing 攻击工作?
- attacker sends 许多 requests 和 measures 响应时间. 如果第一字节的 secret matches, comparison takes slightly longer. 通过 trying 所有 values 为每个字节和 observing timing, attacker recovers secret one 字节在 时间. 此有 是 demonstrated 遍历网络 (内 ~15ms 精确率).
- 是 === 安全为 comparing non-secret 数据?
- 是. Timing attacks 仅 matter 当 compared 值是 secret (passwords, hashes, tokens). 为 non-sensitive comparisons (e.g., checking 如果用户 role 是 "admin"), === 是 fine. 规则: use 常量-时间 comparison 仅为 secrets.