Fix comparison test in hash_equal, release 1.0.1.
The result in the loop was being reset each iteration; only the last element in the byte array was being checked for equality.
This commit is contained in:
@@ -55,7 +55,7 @@ hash_equal(const uint8_t *a, const uint8_t *b)
|
||||
EMSHA_CHECK(b != NULL, false);
|
||||
|
||||
for (uint32_t i = 0; i < SHA256_HASH_SIZE; i++) {
|
||||
res = a[i] ^ b[i];
|
||||
res += a[i] ^ b[i];
|
||||
}
|
||||
|
||||
return res == 0;
|
||||
|
||||
@@ -106,6 +106,29 @@ hash_equal_test(void)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// This catches the bug in the initial version where the code was
|
||||
// res = a[i] ^ b[i];
|
||||
// instead of
|
||||
// res += a[i] ^ b[i];
|
||||
for (uint32_t i = 0; i < emsha::SHA256_HASH_SIZE; i++) {
|
||||
a[i] = static_cast<uint8_t>(i);
|
||||
b[i] = static_cast<uint8_t>(i+1);
|
||||
}
|
||||
|
||||
b[emsha::SHA256_HASH_SIZE - 1]--;
|
||||
if (emsha::hash_equal(a, b)) {
|
||||
string s;
|
||||
cerr << "FAILED: hash_equal\n";
|
||||
cerr << "\tREGRESSION: hash_equal should not have succeeded comparing a and b.\n";
|
||||
dump_hexstring(s, a, emsha::SHA256_HASH_SIZE);
|
||||
cerr << "\ta <- " << s << std::endl;
|
||||
dump_hexstring(s, b, emsha::SHA256_HASH_SIZE);
|
||||
cerr << "\tb <- " << s << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
cout << "PASSED: hash_equal\n";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user