Saltar al contenido ForoHackers

Why comparing secrets with == leaks them, in twenty lines

CriptografíaSeguridad del hardwareProgramación segura
#1207 · sp00ky_s1d3ch4nn3l · · Denunciar

A plain string comparison stops at the first different byte. So the time it takes depends on how many leading bytes were correct. That is a side channel: information leaking through something other than the output.

def check(token, expected):
    return token == expected      # time depends on the matching prefix

Over a noisy network the difference is nanoseconds, but with enough samples and statistics it can be measured, and in setups where the attacker is close (same machine, same cloud host) it gets easier. The defence is old and simple: take the same time for every input of the same length.

import hmac

def check(token: bytes, expected: bytes) -> bool:
    return hmac.compare_digest(token, expected)

And the idea in C, for the curious:

int ct_equal(const unsigned char *a, const unsigned char *b, size_t n) {
    unsigned char diff = 0;
    for (size_t i = 0; i < n; i++)
        diff |= a[i] ^ b[i];       /* never exits early */
    return diff == 0;
}

Caveats: the length still leaks unless it is fixed, and you should use the vetted function from your crypto library instead of your own loop, because a compiler is free to be clever.

#2133 · entropy_eater · · Denunciar

A neat belt-and-braces trick: compare MACs of both values under a fresh random key instead of the values themselves. Then even if the comparison does exit early, the position of the first difference tells the attacker nothing about the real secret, because they cannot predict the MAC. And, as always, do not roll your own: use the library function.

#2134 · undefined_behaviour_enjoyer · · Denunciar

About "a compiler is free to be clever": yes. The C abstract machine knows nothing about time, so the optimizer is allowed to turn your branch-free loop back into an early exit if it proves the result is the same. People have been bitten. volatile does not give you a guarantee either. That is why the library implementations are carefully written, checked in the generated code, and tested.

#2135 · null_terminated_hopes · · Denunciar

Great, a world where == is the bug. I will add it to the pile next to "strlen in the loop" and "it worked on my machine".

#2136 · bitflip_betty · · Denunciar

Hardware makes it worse and better: caches, branch predictors, and even memory timing leak similar things. The principle is the same one you wrote: if the time depends on the secret, the time is an output. Sadly my cosmic rays do not care about my constant-time code.

#2137 · sp00ky_s1d3ch4nn3l · · Denunciar

Fair. Note for beginners: this is not a reason to panic about every == in your code base. It matters when one side is an attacker-controlled guess and the other is a secret. Passwords should be hashed with a slow, salted function anyway, so there is nothing to compare directly.

Inicia sesión o únete para responder.