Make all hash checking in programs constant-time
diff --git a/programs/hash/sha2sum.c b/programs/hash/sha2sum.c
index 2f3acf8..c3f1a0d 100644
--- a/programs/hash/sha2sum.c
+++ b/programs/hash/sha2sum.c
@@ -77,6 +77,7 @@
int nb_tot1, nb_tot2;
unsigned char sum[32];
char buf[65], line[1024];
+ char diff;
if( ( f = fopen( filename, "rb" ) ) == NULL )
{
@@ -117,7 +118,12 @@
for( i = 0; i < 32; i++ )
sprintf( buf + i * 2, "%02x", sum[i] );
- if( memcmp( line, buf, 64 ) != 0 )
+ /* Use constant-time buffer comparison */
+ diff = 0;
+ for( i = 0; i < 64; i++ )
+ diff |= line[i] ^ buf[i];
+
+ if( diff != 0 )
{
nb_err2++;
fprintf( stderr, "wrong checksum: %s\n", line + 66 );