Skip to content

Commit 0b4db7e

Browse files
committed
Time attack protection on hmac comparison
This fixes issue 2.7 of https://defuse.ca/audits/zerobin.htm, and thus (with commit a24212a) also issue 2.8.
1 parent 46c8f25 commit 0b4db7e

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

index.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ function trafic_limiter_canPass($ip)
3939
return true;
4040
}
4141

42+
// Constant time string comparison.
43+
// (Used to deter time attacks on hmac checking. See section 2.7 of https://defuse.ca/audits/zerobin.htm)
44+
function slow_equals($a, $b)
45+
{
46+
$diff = strlen($a) ^ strlen($b);
47+
for($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
48+
{
49+
$diff |= ord($a[$i]) ^ ord($b[$i]);
50+
}
51+
return $diff === 0;
52+
}
53+
54+
4255
/* Convert paste id to storage path.
4356
The idea is to creates subdirectories in order to limit the number of files per directory.
4457
(A high number of files in a single directory can slow things down.)
@@ -309,7 +322,7 @@ function processPasteDelete($pasteid,$deletetoken)
309322
}
310323
}
311324

312-
if ($deletetoken != hash_hmac('sha1', $pasteid , getServerSalt())) // Make sure token is valid.
325+
if (!slow_equals($deletetoken, hash_hmac('sha1', $pasteid , getServerSalt()))) // Make sure token is valid.
313326
{
314327
return array('','Wrong deletion token. Paste was not deleted.','');
315328
}

0 commit comments

Comments
 (0)