FAQ - Help and questions

How do I compare tokens and MACs without leaking timing information?

#security#crypto#subtlepublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Ordinary equality stops at the first byte that differs, so the time it takes can tell an attacker how much of a guess was right. Compare secrets with the subtle crate's ct_eq, and verify MACs with the verify_slice method of the hmac crate, which does a constant-time check for you.

Checked with rustc 1.93.0, subtle 2.6.1, hmac 0.12.1, sha2 0.10.9, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

== on byte slices returns as soon as it finds a difference. In principle, an attacker who can measure response times precisely enough could learn a token one byte at a time. This is hard to exploit over a network and easy to avoid, so use a comparison whose running time does not depend on where the difference is.

Cargo.tomltoml
[dependencies]
subtle = "2.6"
hmac = "0.12"
sha2 = "0.10"
src/main.rsrust
use hmac::{Hmac, Mac};
use sha2::Sha256;
use subtle::ConstantTimeEq;

fn main() {
    // 1. Comparing two secrets of the same length without stopping at the first difference.
    let stored = b"0123456789abcdef";
    let supplied = b"0123456789abcdeX";
    println!("equal: {}", bool::from(stored.ct_eq(supplied)));
    println!("equal: {}", bool::from(stored.ct_eq(stored)));

    // 2. Checking a MAC: `verify_slice` compares in constant time for you.
    let key = b"a demo key, not for real use";
    let mut mac = Hmac::<Sha256>::new_from_slice(key).unwrap();
    mac.update(b"user=42");
    let tag = mac.finalize().into_bytes();

    let mut check = Hmac::<Sha256>::new_from_slice(key).unwrap();
    check.update(b"user=42");
    println!("tag ok: {}", check.verify_slice(&tag).is_ok());

    let mut forged = Hmac::<Sha256>::new_from_slice(key).unwrap();
    forged.update(b"user=1");
    println!("other message ok: {}", forged.verify_slice(&tag).is_ok());
}
text
equal: false
equal: true
tag ok: true
other message ok: false

ct_eq returns a Choice, not a bool; convert with bool::from at the very end. For a MAC, verify_slice already compares in constant time. Slices of different length compare as not equal straight away: the length is not treated as secret.

How to do it#

  1. Compare equal-length secrets (tokens, MAC tags, API keys) with ct_eq.
  2. For HMAC tags, call verify_slice; do not compute the tag and compare with ==.
  3. For passwords, use the verify function of your hashing crate.
  4. Keep the Choice as a Choice as long as you can, and convert once.

Common mistakes#

  • Branching on secret data before the comparison (an early return at the first different byte in your own loop).
  • Believing constant-time comparison fixes weak tokens. A guessable token is guessable at any speed.

Does this matter when I look a session up by its hash?#

A database lookup by a hash of the token reveals nothing useful about the token itself, because an attacker cannot choose inputs that share a prefix with the stored value. That is one reason to store only hashes of session IDs.

When is a plain comparison fine?#

For values that are not secret: public IDs, version strings, file names. Use constant-time comparison when an attacker could learn something from the result of comparing a secret.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet