Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit c5654d8

Browse files
committed
minor #11574 [Security] Made optimization on constant-time algorithm removing modulus operator (yosmanyga)
This PR was merged into the 2.3 branch. Discussion ---------- [Security] Made optimization on constant-time algorithm removing modulus operator | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This fix improves the constant-time algorithm used to compare strings, as it removes the `%` operator inside the loop. Commits ------- 000bd0d Made optimization deprecating modulus operator
2 parents 7d54a1a + a982db7 commit c5654d8

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

Core/Util/StringUtils.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,19 @@ private function __construct() {}
3535
*/
3636
public static function equals($knownString, $userInput)
3737
{
38-
// Prevent issues if string length is 0
39-
$knownString .= chr(0);
40-
$userInput .= chr(0);
41-
4238
$knownLen = strlen($knownString);
4339
$userLen = strlen($userInput);
4440

41+
// Extend know string to avoid uninitialized string offsets
42+
$knownString .= $userInput;
43+
4544
// Set the result to the difference between the lengths
4645
$result = $knownLen - $userLen;
4746

4847
// Note that we ALWAYS iterate over the user-supplied length
4948
// This is to prevent leaking length information
5049
for ($i = 0; $i < $userLen; $i++) {
51-
// Using % here is a trick to prevent notices
52-
// It's safe, since if the lengths are different
53-
// $result is already non-0
54-
$result |= (ord($knownString[$i % $knownLen]) ^ ord($userInput[$i]));
50+
$result |= (ord($knownString[$i]) ^ ord($userInput[$i]));
5551
}
5652

5753
// They are only identical strings if $result is exactly 0...

0 commit comments

Comments
 (0)