cybersecurity · intermediate · ~15 min · safe pentest lab
Avoid early-exit branches when comparing secret material.
Compare two secret tokens without leaking, through timing, how many leading bytes matched.
Implement int ct_token_match(const char *a, const char *b) that compares two NUL-terminated tokens in constant time with respect to their content. Return 1 if they are equal, 0 otherwise.
a, b: NUL-terminated strings the grader passes (e.g. an HMAC, hash, or API token and a candidate).Returns int: 1 if the two strings are exactly equal, 0 otherwise.
ct_token_match("abc123", "abc123") -> 1
ct_token_match("abc123", "abc124") -> 0
ct_token_match("abc", "abcd") -> 0 (different length)
ct_token_match("", "") -> 1
acc |= a[i] ^ b[i]) and test once at the end.Two NUL-terminated tokens a and b.
An int: 1 if the tokens are equal, 0 otherwise.
Constant-time compare — no early exit on the first differing byte.
#include <stddef.h>
int ct_token_match(const char *a, const char *b) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.