Skip to content

Commit 423c192

Browse files
narfbgnikic
authored andcommitted
Use hash_ops->is_crypto in hash_init()
1 parent d6ef39e commit 423c192

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

ext/hash/hash.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ PHP_FUNCTION(hash_init)
350350
RETURN_FALSE;
351351
}
352352

353+
if (options & PHP_HASH_HMAC && !ops->is_crypto) {
354+
php_error_docref(NULL, E_WARNING, "HMAC requested with a non-cryptographic hashing algorithm: %s", algo);
355+
RETURN_FALSE;
356+
}
357+
353358
if (options & PHP_HASH_HMAC &&
354359
key_len <= 0) {
355360
/* Note: a zero length key is no key at all */

ext/hash/tests/bug52240.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Bug #52240 (hash_copy() does not copy the HMAC key, causes wrong results and PHP
55
--FILE--
66
<?php
77

8-
$h = hash_init('crc32b', HASH_HMAC, '123456' );
8+
$h = hash_init('md5', HASH_HMAC, '123456');
99
$h2 = hash_copy($h);
1010
var_dump(hash_final($h));
1111
$h3 = hash_copy($h2);
@@ -14,6 +14,6 @@ var_dump(hash_final($h3));
1414

1515
?>
1616
--EXPECT--
17-
string(8) "278af264"
18-
string(8) "278af264"
19-
string(8) "278af264"
17+
string(32) "cab1380ea86d8acc9aa62390a58406aa"
18+
string(32) "cab1380ea86d8acc9aa62390a58406aa"
19+
string(32) "cab1380ea86d8acc9aa62390a58406aa"

ext/hash/tests/hash_init_error.phpt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
hash_init() function - errors test
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('hash')) die('skip hash extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
echo "*** Testing hash_init(): error conditions ***\n";
10+
11+
echo "-- Testing hash_init() function with no parameters --\n";
12+
var_dump(hash_init());
13+
14+
echo "-- Testing hash_init() function with unknown algorithms --\n";
15+
var_dump(hash_init('dummy'));
16+
17+
echo "-- Testing hash_init() function with HASH_HMAC and non-cryptographic algorithms --\n";
18+
var_dump(hash_init('crc32', HASH_HMAC));
19+
20+
echo "-- Testing hash_init() function with HASH_HMAC and no key --\n";
21+
var_dump(hash_init('md5', HASH_HMAC));
22+
var_dump(hash_init('md5', HASH_HMAC, null));
23+
?>
24+
--EXPECTF--
25+
*** Testing hash_init(): error conditions ***
26+
-- Testing hash_init() function with no parameters --
27+
28+
Warning: hash_init() expects at least 1 parameter, 0 given in %s on line %d
29+
NULL
30+
-- Testing hash_init() function with unknown algorithms --
31+
32+
Warning: hash_init(): Unknown hashing algorithm: dummy in %s on line %d
33+
bool(false)
34+
-- Testing hash_init() function with HASH_HMAC and non-cryptographic algorithms --
35+
36+
Warning: hash_init(): HMAC requested with a non-cryptographic hashing algorithm: crc32 in %s on line %d
37+
bool(false)
38+
-- Testing hash_init() function with HASH_HMAC and no key --
39+
40+
Warning: hash_init(): HMAC requested without a key %s on line %d
41+
bool(false)
42+
43+
Warning: hash_init(): HMAC requested without a key %s on line %d
44+
bool(false)

0 commit comments

Comments
 (0)