Skip to content
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

Add compatibility for PHP 5.6's hash_equals function #61

Closed
Closed
Show file tree
Hide file tree
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
61 changes: 56 additions & 5 deletions lib/password.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,67 @@ function password_verify($password, $hash) {
return false;
}
$ret = crypt($password, $hash);
if (!is_string($ret) || PasswordCompat\binary\_strlen($ret) != PasswordCompat\binary\_strlen($hash) || PasswordCompat\binary\_strlen($ret) <= 13) {
if (!is_string($hash) || !is_string($ret) || PasswordCompat\binary\_strlen($ret) <= 13) {
return false;
}

$status = 0;
for ($i = 0; $i < PasswordCompat\binary\_strlen($ret); $i++) {
$status |= (ord($ret[$i]) ^ ord($hash[$i]));
return hash_equals($hash, $ret);
}

}

if (!function_exists('hash_equals')) {

/**
* Timing attack safe string comparison
*
* @param string $known_string The string of known length to compare against
* @param string $user_string The user-supplied hash
*
* @return boolean or null
*/
function hash_equals($known_string, $user_string) {
$argc = func_num_args();
$eq = true;
$result = 0;

if ($argc < 2) {
trigger_error("hash_equals() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
return null;
}

if (!is_string($known_string)) {
trigger_error("hash_equals(): Expected known_string to be a string, " . gettype($known_string) . " given", E_USER_WARNING);
$eq = false;
}

if (!is_string($user_string)) {
trigger_error("hash_equals(): Expected user_string to be a string, " . gettype($user_string) . " given", E_USER_WARNING);
$eq = false;
}

if (!is_string($known_string) || !is_string($user_string)) {
$eq = false;
}

$known_len = PasswordCompat\binary\_strlen($known_string);
$user_len = PasswordCompat\binary\_strlen($user_string);

if ($known_len != $user_len) {
$eq = false;
}

Choose a reason for hiding this comment

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

I'm not sure the following block of code is equivalent to the xor + or loop e.g.

for($i = 0; $i < $user_len && $i < $known_len; ++$i)
{
    $diff |= ord($known_string[$i]) ^ ord($user_string[$i]);
}
return $diff === 0;

However, it appears to still resolve the side-channel. Just worth examining before merging, I suspect. 😄

Copy link
Author

Choose a reason for hiding this comment

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

I originally left out the ord diff as it is not UTF-8 safe and I wanted to stay future compatible. But it doesn't matter as bcrypt, scrypt, PBKDF2, and SHA2x all generate ASCII safe hashes. Updated.

for ($i = 0; $i < $known_len; $i++) {
if ($known_len > $i && $user_len > $i) {
$result |= ord($known_string[$i]) ^ ord($user_string[$i]);
}
}

if ($result !== 0) {
$eq = false;
}

return $status === 0;
return $eq;
}
}

Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
testSuiteLoaderFile="vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php"
strict="false"
verbose="false">
<testsuites>
Expand Down
55 changes: 55 additions & 0 deletions test/Unit/HashEquals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

class HashEquals extends PHPUnit_Framework_TestCase {

public function testFuncExists() {
$this->assertTrue(function_exists('hash_equals'));
}
public function testExamples() {
$expected = crypt('12345', '$2a$07$usesomesillystringforsalt$');
$correct = crypt('12345', '$2a$07$usesomesillystringforsalt$');
$incorrect = crypt('apple', '$2a$07$usesomesillystringforsalt$');

$this->assertTrue(hash_equals($expected, $correct));
$this->assertFalse(hash_equals($expected, $incorrect));
}

public function testProvider() {
return array(
array("same", "same", true),
array("not1same", "not2same", false),
array("short", "longer", false),
array("longer", "short", false),
array("", "notempty", false),
array("notempty", "", false),
array("", "", true),
);
}

public function warningProvider() {
return array(
array(123, "NaN", true),
array("NaN", 123, true),
array(123, 123, true),
array(null, "", true),
array(null, 123, true),
array(null, null, true),
);
}

/**
* @dataProvider testProvider
*/
public function testHashEquals($knownString, $userString, $result) {
$this->assertSame($result, hash_equals($knownString, $userString));
}

/**
* @expectedException PHPUnit_Framework_Error_Warning
* @dataProvider warningProvider
*/
public function testHashEqualsWarning($knownString, $userString, $result) {
$this->assertSame($result, hash_equals($knownString, $userString));
}

}