Skip to content

[Security] Made optimization on constant-time algorithm removing modulus operator #11574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/Symfony/Component/Security/Core/Util/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,19 @@ private function __construct() {}
*/
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

$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...
Expand Down