Using the Windows cryptography API, how do I compare two byte arrays for equality in constant time?
Edit: The length of the secret is fixed and is public knowledge.
Using the Windows cryptography API, how do I compare two byte arrays for equality in constant time?
Edit: The length of the secret is fixed and is public knowledge.
Timing safe comparison requires knowing which array is coming from the user (which determines the time it will take), and which array is your secret (which you don't want to give away the secret of how long it is)
//Code released into public domain. No attribution required.
Boolean TimingSafeArrayCompare(Byte[] safe, Byte[] user)
{
/*
A timing safe array comparison.
To prevent leaking length information,
it is important that user input is always used as the second parameter.
safe: The internal (safe) value to be checked
user: The user submitted (unsafe) value
Returns True if the two arrays are identical.
*/
int safeLen = safe.Length;
int userLen = user.Length;
// Set the result to the difference between the lengths.
// This means that arrays of different length will already cause nDiff to be non-zero
int nDiff = safeLen - userLen;
// Note that we ALWAYS iterate over the user-supplied length
// This is to prevent leaking length information
for (i = 0 to userLen-1)
{
//Using mod here is a trick to prevent leaking.
//It's safe, since if the lengths are different, nDiff will already be non-zero
nDiff = nDiff | ( User[i] xor Safe[i mod safeLen] );
}
// They are only identical strings if nDiff is exactly zero
return (nDiff == 0);
}
It's an ingenious technique, that i first saw here.