Make all hash checking in programs constant-time
diff --git a/programs/hash/sha1sum.c b/programs/hash/sha1sum.c
index 92f8406..c20323d 100644
--- a/programs/hash/sha1sum.c
+++ b/programs/hash/sha1sum.c
@@ -81,6 +81,7 @@
int nb_tot1, nb_tot2;
unsigned char sum[20];
char buf[41], line[1024];
+ char diff;
if( ( f = fopen( filename, "rb" ) ) == NULL )
{
@@ -121,7 +122,12 @@
for( i = 0; i < 20; i++ )
sprintf( buf + i * 2, "%02x", sum[i] );
- if( memcmp( line, buf, 40 ) != 0 )
+ /* Use constant-time buffer comparison */
+ diff = 0;
+ for( i = 0; i < 40; i++ )
+ diff |= line[i] ^ buf[i];
+
+ if( diff != 0 )
{
nb_err2++;
fprintf( stderr, "wrong checksum: %s\n", line + 42 );