libsodium verifies that the HMAC is correct with the following code:
return crypto_verify_32(h, correct) | (-(h == correct)) | sodium_memcmp(correct, h, 32);
where crypto_verify_32
is
for (i = 0; i < n; i++) {
d |= x[i] ^ y[i];
}
return (1 & ((d - 1) >> 8)) - 1;
and sodium_memcmp
is
for (i = 0U; i < len; i++) {
d |= b1[i] ^ b2[i];
}
return (1 & ((d - 1) >> 8)) - 1;
Why not using a simple memcmp
?