Skip to content

Commit

Permalink
Errorfy hash_pbkdf2
Browse files Browse the repository at this point in the history
  • Loading branch information
marandall authored and krakjoe committed Aug 29, 2019
1 parent ee104cf commit e18bac9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
20 changes: 10 additions & 10 deletions ext/hash/hash.c
Expand Up @@ -739,27 +739,27 @@ PHP_FUNCTION(hash_pbkdf2)

ops = php_hash_fetch_ops(algo, algo_len);
if (!ops) {
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
RETURN_FALSE;
zend_throw_error(NULL, "Unknown hashing algorithm: %s", algo);
return;
}
else if (!ops->is_crypto) {
php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo);
RETURN_FALSE;
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", algo);
return;
}

if (iterations <= 0) {
php_error_docref(NULL, E_WARNING, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
RETURN_FALSE;
zend_throw_error(NULL, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
return;
}

if (length < 0) {
php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
RETURN_FALSE;
zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
return;
}

if (salt_len > INT_MAX - 4) {
php_error_docref(NULL, E_WARNING, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
RETURN_FALSE;
zend_throw_error(NULL, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
return;
}

context = emalloc(ops->context_size);
Expand Down
59 changes: 38 additions & 21 deletions ext/hash/tests/hash_pbkdf2_error.phpt
Expand Up @@ -13,43 +13,60 @@ $password = 'password';
$salt = 'salt';

echo "\n-- Testing hash_pbkdf2() function with invalid hash algorithm --\n";
var_dump(hash_pbkdf2('foo', $password, $salt, 1));
try {
var_dump(hash_pbkdf2('foo', $password, $salt, 1));
}
catch (\Error $e) {
echo $e->getMessage() . "\n";
}


echo "\n-- Testing hash_pbkdf2() function with non-cryptographic hash algorithm --\n";
var_dump(hash_pbkdf2('crc32', $password, $salt, 1));
try {
var_dump(hash_pbkdf2('crc32', $password, $salt, 1));
}
catch (\Error $e) {
echo $e->getMessage() . "\n";
}

echo "\n-- Testing hash_pbkdf2() function with invalid iterations --\n";
var_dump(hash_pbkdf2('md5', $password, $salt, 0));
var_dump(hash_pbkdf2('md5', $password, $salt, -1));
try {
var_dump(hash_pbkdf2('md5', $password, $salt, 0));
}
catch (\Error $e) {
echo $e->getMessage() . "\n";
}

try {
var_dump(hash_pbkdf2('md5', $password, $salt, -1));
}
catch (\Error $e) {
echo $e->getMessage() . "\n";
}

echo "\n-- Testing hash_pbkdf2() function with invalid length --\n";
var_dump(hash_pbkdf2('md5', $password, $salt, 1, -1));
try {
var_dump(hash_pbkdf2('md5', $password, $salt, 1, -1));
}
catch (\Error $e) {
echo $e->getMessage() . "\n";
}

?>
===Done===
--EXPECTF--
--EXPECT--
*** Testing hash_pbkdf2() : error conditions ***

-- Testing hash_pbkdf2() function with invalid hash algorithm --

Warning: hash_pbkdf2(): Unknown hashing algorithm: foo in %s on line %d
bool(false)
Unknown hashing algorithm: foo

-- Testing hash_pbkdf2() function with non-cryptographic hash algorithm --

Warning: hash_pbkdf2(): Non-cryptographic hashing algorithm: crc32 in %s on line %d
bool(false)
Non-cryptographic hashing algorithm: crc32

-- Testing hash_pbkdf2() function with invalid iterations --

Warning: hash_pbkdf2(): Iterations must be a positive integer: 0 in %s on line %d
bool(false)

Warning: hash_pbkdf2(): Iterations must be a positive integer: -1 in %s on line %d
bool(false)
Iterations must be a positive integer: 0
Iterations must be a positive integer: -1

-- Testing hash_pbkdf2() function with invalid length --

Warning: hash_pbkdf2(): Length must be greater than or equal to 0: -1 in %s on line %d
bool(false)
Length must be greater than or equal to 0: -1
===Done===

0 comments on commit e18bac9

Please sign in to comment.