Skip to content

Commit ee104cf

Browse files
marandallkrakjoe
authored andcommitted
Warnings become errors for hash_hkdf
1 parent 960da6d commit ee104cf

File tree

3 files changed

+62
-63
lines changed

3 files changed

+62
-63
lines changed

ext/hash/hash.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -639,28 +639,28 @@ PHP_FUNCTION(hash_hkdf)
639639

640640
ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
641641
if (!ops) {
642-
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
643-
RETURN_FALSE;
642+
zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
643+
return;
644644
}
645645

646646
if (!ops->is_crypto) {
647-
php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
648-
RETURN_FALSE;
647+
zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
648+
return;
649649
}
650650

651651
if (ZSTR_LEN(ikm) == 0) {
652-
php_error_docref(NULL, E_WARNING, "Input keying material cannot be empty");
653-
RETURN_FALSE;
652+
zend_throw_error(NULL, "Input keying material cannot be empty");
653+
return;
654654
}
655655

656656
if (length < 0) {
657-
php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
658-
RETURN_FALSE;
657+
zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
658+
return;
659659
} else if (length == 0) {
660660
length = ops->digest_size;
661661
} else if (length > (zend_long) (ops->digest_size * 255)) {
662-
php_error_docref(NULL, E_WARNING, "Length must be less than or equal to %zd: " ZEND_LONG_FMT, ops->digest_size * 255, length);
663-
RETURN_FALSE;
662+
zend_throw_error(NULL, "Length must be less than or equal to %zd: " ZEND_LONG_FMT, ops->digest_size * 255, length);
663+
return;
664664
}
665665

666666
context = emalloc(ops->context_size);

ext/hash/tests/hash_hkdf_edges.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ echo 'Length < digestSize: ', bin2hex(hash_hkdf('md5', $ikm, 7)), "\n";
1616
echo 'Length % digestSize != 0: ', bin2hex(hash_hkdf('md5', $ikm, 17)), "\n";
1717
echo 'Algo name case-sensitivity: ', (bin2hex(hash_hkdf('Md5', $ikm, 7)) === '98b16391063ece' ? 'true' : 'false'), "\n";
1818
echo "Non-crypto algo name case-sensitivity:\n";
19-
var_dump(hash_hkdf('jOaAt', $ikm));
19+
20+
try {
21+
var_dump(hash_hkdf('jOaAt', $ikm));
22+
}
23+
catch (\Error $e) {
24+
echo '[Error] ' . $e->getMessage() . "\n";
25+
}
2026

2127
?>
2228
--EXPECTF--
@@ -25,6 +31,4 @@ Length < digestSize: 98b16391063ece
2531
Length % digestSize != 0: 98b16391063ecee006a3ca8ee5776b1e5f
2632
Algo name case-sensitivity: true
2733
Non-crypto algo name case-sensitivity:
28-
29-
Warning: hash_hkdf(): Non-cryptographic hashing algorithm: jOaAt in %s on line %d
30-
bool(false)
34+
[Error] Non-cryptographic hashing algorithm: jOaAt

ext/hash/tests/hash_hkdf_error.phpt

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,71 @@ Hash: hash_hkdf() function: error conditions
33
--FILE--
44
<?php
55

6+
error_reporting(E_ALL);
7+
68
/* Prototype : string hkdf ( string $algo , string $ikm [, int $length , string $info = '' , string $salt = '' ] )
79
* Description: HMAC-based Key Derivation Function
810
* Source code: ext/hash/hash.c
911
*/
1012

13+
function trycatch_dump(...$tests) {
14+
foreach ($tests as $test) {
15+
try {
16+
var_dump($test());
17+
}
18+
catch (\Error $e) {
19+
echo '[' . get_class($e) . '] ' . $e->getMessage() . "\n";
20+
}
21+
}
22+
}
23+
1124
$ikm = 'input key material';
1225

1326
echo "*** Testing hash_hkdf(): error conditions ***\n";
1427

1528
echo "\n-- Testing hash_hkdf() function with invalid hash algorithm --\n";
16-
var_dump(hash_hkdf('foo', $ikm));
29+
trycatch_dump(
30+
fn() => hash_hkdf('foo', $ikm)
31+
);
1732

1833
echo "\n-- Testing hash_hkdf() function with non-cryptographic hash algorithm --\n";
19-
var_dump(hash_hkdf('adler32', $ikm));
20-
var_dump(hash_hkdf('crc32', $ikm));
21-
var_dump(hash_hkdf('crc32b', $ikm));
22-
var_dump(hash_hkdf('fnv132', $ikm));
23-
var_dump(hash_hkdf('fnv1a32', $ikm));
24-
var_dump(hash_hkdf('fnv164', $ikm));
25-
var_dump(hash_hkdf('fnv1a64', $ikm));
26-
var_dump(hash_hkdf('joaat', $ikm));
34+
trycatch_dump(
35+
fn() => hash_hkdf('adler32', $ikm),
36+
fn() => hash_hkdf('crc32', $ikm),
37+
fn() => hash_hkdf('crc32b', $ikm),
38+
fn() => hash_hkdf('fnv132', $ikm),
39+
fn() => hash_hkdf('fnv1a32', $ikm),
40+
fn() => hash_hkdf('fnv164', $ikm),
41+
fn() => hash_hkdf('fnv1a64', $ikm),
42+
fn() => hash_hkdf('joaat', $ikm)
43+
);
2744

2845
echo "\n-- Testing hash_hkdf() function with invalid parameters --\n";
29-
var_dump(hash_hkdf('sha1', ''));
30-
var_dump(hash_hkdf('sha1', $ikm, -1));
31-
var_dump(hash_hkdf('sha1', $ikm, 20 * 255 + 1)); // Length can't be more than 255 times the hash digest size
46+
trycatch_dump(
47+
fn() => hash_hkdf('sha1', ''),
48+
fn() => hash_hkdf('sha1', $ikm, -1),
49+
fn() => hash_hkdf('sha1', $ikm, 20 * 255 + 1) // Length can't be more than 255 times the hash digest size
50+
)
3251
?>
3352
===Done===
34-
--EXPECTF--
53+
--EXPECT--
3554
*** Testing hash_hkdf(): error conditions ***
3655

3756
-- Testing hash_hkdf() function with invalid hash algorithm --
38-
39-
Warning: hash_hkdf(): Unknown hashing algorithm: foo in %s on line %d
40-
bool(false)
57+
[Error] Unknown hashing algorithm: foo
4158

4259
-- Testing hash_hkdf() function with non-cryptographic hash algorithm --
43-
44-
Warning: hash_hkdf(): Non-cryptographic hashing algorithm: adler32 in %s on line %d
45-
bool(false)
46-
47-
Warning: hash_hkdf(): Non-cryptographic hashing algorithm: crc32 in %s on line %d
48-
bool(false)
49-
50-
Warning: hash_hkdf(): Non-cryptographic hashing algorithm: crc32b in %s on line %d
51-
bool(false)
52-
53-
Warning: hash_hkdf(): Non-cryptographic hashing algorithm: fnv132 in %s on line %d
54-
bool(false)
55-
56-
Warning: hash_hkdf(): Non-cryptographic hashing algorithm: fnv1a32 in %s on line %d
57-
bool(false)
58-
59-
Warning: hash_hkdf(): Non-cryptographic hashing algorithm: fnv164 in %s on line %d
60-
bool(false)
61-
62-
Warning: hash_hkdf(): Non-cryptographic hashing algorithm: fnv1a64 in %s on line %d
63-
bool(false)
64-
65-
Warning: hash_hkdf(): Non-cryptographic hashing algorithm: joaat in %s on line %d
66-
bool(false)
60+
[Error] Non-cryptographic hashing algorithm: adler32
61+
[Error] Non-cryptographic hashing algorithm: crc32
62+
[Error] Non-cryptographic hashing algorithm: crc32b
63+
[Error] Non-cryptographic hashing algorithm: fnv132
64+
[Error] Non-cryptographic hashing algorithm: fnv1a32
65+
[Error] Non-cryptographic hashing algorithm: fnv164
66+
[Error] Non-cryptographic hashing algorithm: fnv1a64
67+
[Error] Non-cryptographic hashing algorithm: joaat
6768

6869
-- Testing hash_hkdf() function with invalid parameters --
69-
70-
Warning: hash_hkdf(): Input keying material cannot be empty in %s on line %d
71-
bool(false)
72-
73-
Warning: hash_hkdf(): Length must be greater than or equal to 0: -1 in %s on line %d
74-
bool(false)
75-
76-
Warning: hash_hkdf(): Length must be less than or equal to 5100: 5101 in %s on line %d
77-
bool(false)
70+
[Error] Input keying material cannot be empty
71+
[Error] Length must be greater than or equal to 0: -1
72+
[Error] Length must be less than or equal to 5100: 5101
7873
===Done===

0 commit comments

Comments
 (0)