diff options
author | Fabien Potencier <fabien.potencier@gmail.com> | 2014-08-31 05:48:56 +0200 |
---|---|---|
committer | Fabien Potencier <fabien.potencier@gmail.com> | 2014-08-31 05:48:56 +0200 |
commit | c5654d848b56630425d97042fd1e0b52f65933ad (patch) | |
tree | 733923dabfff76ce818cb1a4de095c7a7e02dc90 /Core | |
parent | 7d54a1a19687aed3f719640a78c5abdc88976445 (diff) | |
parent | a982db769f3a9e1d1c6f4f8740f69454231270b0 (diff) | |
download | symfony-security-c5654d848b56630425d97042fd1e0b52f65933ad.zip symfony-security-c5654d848b56630425d97042fd1e0b52f65933ad.tar.gz symfony-security-c5654d848b56630425d97042fd1e0b52f65933ad.tar.bz2 |
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
Diffstat (limited to 'Core')
-rw-r--r-- | Core/Util/StringUtils.php | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/Core/Util/StringUtils.php b/Core/Util/StringUtils.php index d47bd4b..eaeed84 100644 --- a/Core/Util/StringUtils.php +++ b/Core/Util/StringUtils.php @@ -35,23 +35,19 @@ class StringUtils */ public static function equals($knownString, $userInput) { - // Prevent issues if string length is 0 - $knownString .= chr(0); - $userInput .= chr(0); - $knownLen = strlen($knownString); $userLen = strlen($userInput); + // Extend know string to avoid uninitialized string offsets + $knownString .= $userInput; + // Set the result to the difference between the lengths $result = $knownLen - $userLen; // Note that we ALWAYS iterate over the user-supplied length // This is to prevent leaking length information for ($i = 0; $i < $userLen; $i++) { - // Using % here is a trick to prevent notices - // It's safe, since if the lengths are different - // $result is already non-0 - $result |= (ord($knownString[$i % $knownLen]) ^ ord($userInput[$i])); + $result |= (ord($knownString[$i]) ^ ord($userInput[$i])); } // They are only identical strings if $result is exactly 0... |