cybersecurity · intermediate · ~15 min
Avoid timing side-channels in comparisons.
Compare two byte arrays without letting the running time reveal where they first differ — the defence against timing side-channels on secrets.
Implement int ct_equal(const unsigned char *a, const unsigned char *b, int n) that returns 1 if the n bytes are equal, else 0, with no early return.
a, b: two byte arrays of length n the grader passes.n: the number of bytes to compare.Returns int: 1 if all n bytes match, else 0.
ct_equal({1,2,3}, {1,2,3}, 3) -> 1
ct_equal({1,2,3}, {1,9,3}, 3) -> 0
diff |= a[i] ^ b[i]) and test once at the end so the time is independent of where the bytes differ.Two byte arrays a and b and their common length n.
An int: 1 if all n bytes are equal, else 0.
No early return — accumulate differences and test once at the end.
int ct_equal(const unsigned char *a, const unsigned char *b, int n) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.