Menu

The length is not the secret: why constant-time equals returns early
📰

The length is not the secret: why constant-time equals returns early

DEV Community: cpp·polycratia·10 days ago
#t0dzDFZW
#devtocpp#devto#dev#length#ctsafe
Long article (1828 words) — no AI summary yet.
Reading 0:00
15s threshold

A constant-time comparison is supposed to contain no branch that depends on its input. The span form of ctsafe::equals opens with exactly one: if the two sizes differ, it returns false before a single byte gets read. That looks like a hole in the discipline, and it isn't one. Constant-time code protects the contents of a buffer, so a comparison routine has to decide whether the length counts as contents. Call it public and you buy the one early return worth having, and you close an overread that is a far more realistic bug than the leak it admits.

What the timing channel actually carries

The bug the routine exists for is older than most of the code that still contains it:

if ( memcmp ( mac , expected , 16 ) == 0 ) { . . . } // CWE-208

Enter fullscreen mode

Exit fullscreen mode

memcmp returns as soon as two bytes differ. Someone who can time the call learns how many leading bytes they guessed correctly, and walks the tag out one byte at a time.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.