Skip to content

Commit f83368c

Browse files
committed
Fix ZPP for mhash()
Closes GH-5985
1 parent 96c7d42 commit f83368c

File tree

3 files changed

+65
-41
lines changed

3 files changed

+65
-41
lines changed

ext/hash/hash.c

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -345,23 +345,14 @@ PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long ma
345345

346346
/* Userspace */
347347

348-
static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
349-
{
350-
zend_string *digest, *algo;
351-
char *data;
352-
size_t data_len;
353-
zend_bool raw_output = raw_output_default;
348+
static void php_hash_do_hash(
349+
zval *return_value, zend_string *algo, char *data, size_t data_len, zend_bool raw_output, bool isfilename
350+
) /* {{{ */ {
351+
zend_string *digest;
354352
const php_hash_ops *ops;
355353
void *context;
356354
php_stream *stream = NULL;
357355

358-
ZEND_PARSE_PARAMETERS_START(2, 3)
359-
Z_PARAM_STR(algo)
360-
Z_PARAM_STRING(data, data_len)
361-
Z_PARAM_OPTIONAL
362-
Z_PARAM_BOOL(raw_output)
363-
ZEND_PARSE_PARAMETERS_END();
364-
365356
ops = php_hash_fetch_ops(algo);
366357
if (!ops) {
367358
zend_argument_value_error(1, "must be a valid hashing algorithm");
@@ -420,15 +411,39 @@ static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_
420411
Returns lowercase hexits by default */
421412
PHP_FUNCTION(hash)
422413
{
423-
php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
414+
zend_string *algo;
415+
char *data;
416+
size_t data_len;
417+
zend_bool raw_output = 0;
418+
419+
ZEND_PARSE_PARAMETERS_START(2, 3)
420+
Z_PARAM_STR(algo)
421+
Z_PARAM_STRING(data, data_len)
422+
Z_PARAM_OPTIONAL
423+
Z_PARAM_BOOL(raw_output)
424+
ZEND_PARSE_PARAMETERS_END();
425+
426+
php_hash_do_hash(return_value, algo, data, data_len, raw_output, 0);
424427
}
425428
/* }}} */
426429

427430
/* {{{ Generate a hash of a given file
428431
Returns lowercase hexits by default */
429432
PHP_FUNCTION(hash_file)
430433
{
431-
php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
434+
zend_string *algo;
435+
char *data;
436+
size_t data_len;
437+
zend_bool raw_output = 0;
438+
439+
ZEND_PARSE_PARAMETERS_START(2, 3)
440+
Z_PARAM_STR(algo)
441+
Z_PARAM_STRING(data, data_len)
442+
Z_PARAM_OPTIONAL
443+
Z_PARAM_BOOL(raw_output)
444+
ZEND_PARSE_PARAMETERS_END();
445+
446+
php_hash_do_hash(return_value, algo, data, data_len, raw_output, 1);
432447
}
433448
/* }}} */
434449

@@ -467,22 +482,15 @@ static inline void php_hash_hmac_round(unsigned char *final, const php_hash_ops
467482
ops->hash_final(final, context);
468483
}
469484

470-
static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
471-
{
472-
zend_string *digest, *algo;
473-
char *data, *key;
485+
static void php_hash_do_hash_hmac(
486+
zval *return_value, zend_string *algo, char *data, size_t data_len, char *key, size_t key_len, zend_bool raw_output, bool isfilename
487+
) /* {{{ */ {
488+
zend_string *digest;
474489
unsigned char *K;
475-
size_t data_len, key_len;
476-
zend_bool raw_output = raw_output_default;
477490
const php_hash_ops *ops;
478491
void *context;
479492
php_stream *stream = NULL;
480493

481-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len,
482-
&key, &key_len, &raw_output) == FAILURE) {
483-
RETURN_THROWS();
484-
}
485-
486494
ops = php_hash_fetch_ops(algo);
487495
if (!ops || !ops->is_crypto) {
488496
zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
@@ -556,15 +564,33 @@ static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename,
556564
Returns lowercase hexits by default */
557565
PHP_FUNCTION(hash_hmac)
558566
{
559-
php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
567+
zend_string *algo;
568+
char *data, *key;
569+
size_t data_len, key_len;
570+
zend_bool raw_output = 0;
571+
572+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
573+
RETURN_THROWS();
574+
}
575+
576+
php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, raw_output, 0);
560577
}
561578
/* }}} */
562579

563580
/* {{{ Generate a hash of a given file with a key using HMAC
564581
Returns lowercase hexits by default */
565582
PHP_FUNCTION(hash_hmac_file)
566583
{
567-
php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
584+
zend_string *algo;
585+
char *data, *key;
586+
size_t data_len, key_len;
587+
zend_bool raw_output = 0;
588+
589+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
590+
RETURN_THROWS();
591+
}
592+
593+
php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, raw_output, 1);
568594
}
569595
/* }}} */
570596

@@ -1163,29 +1189,27 @@ static void mhash_init(INIT_FUNC_ARGS)
11631189
/* {{{ Hash data with hash */
11641190
PHP_FUNCTION(mhash)
11651191
{
1166-
zval *z_algorithm;
11671192
zend_long algorithm;
1193+
zend_string *algo = NULL;
1194+
char *data, *key = NULL;
1195+
size_t data_len, key_len = 0;
11681196

1169-
if (zend_parse_parameters(1, "z", &z_algorithm) == FAILURE) {
1197+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!", &algorithm, &data, &data_len, &key, &key_len) == FAILURE) {
11701198
RETURN_THROWS();
11711199
}
11721200

1173-
algorithm = zval_get_long(z_algorithm);
1174-
11751201
/* need to convert the first parameter from int constant to string algorithm name */
11761202
if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
11771203
struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
11781204
if (algorithm_lookup.hash_name) {
1179-
ZVAL_STRING(z_algorithm, algorithm_lookup.hash_name);
1205+
algo = zend_string_init(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name), 1);
11801206
}
11811207
}
11821208

1183-
if (ZEND_NUM_ARGS() == 3) {
1184-
php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
1185-
} else if (ZEND_NUM_ARGS() == 2) {
1186-
php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
1209+
if (key) {
1210+
php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, 1, 0);
11871211
} else {
1188-
WRONG_PARAM_COUNT;
1212+
php_hash_do_hash(return_value, algo, data, data_len, 1, 0);
11891213
}
11901214
}
11911215
/* }}} */

ext/hash/hash.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function mhash_keygen_s2k(int $hash, string $input_password, string $salt, int $
4343

4444
function mhash_count(): int {}
4545

46-
function mhash(int $hash, string $data, string $key = UNKNOWN): string|false {}
46+
function mhash(int $hash, string $data, ?string $key = null): string|false {}
4747
#endif
4848

4949
final class HashContext

ext/hash/hash_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 6da0ff3370cecc919fccf7c6791828a81b44156d */
2+
* Stub hash: 9280e94fe6bb8b86d0cc6f939996c6c0b0bb9241 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
55
ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0)
@@ -111,7 +111,7 @@ ZEND_END_ARG_INFO()
111111
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mhash, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
112112
ZEND_ARG_TYPE_INFO(0, hash, IS_LONG, 0)
113113
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
114-
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
114+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, key, IS_STRING, 1, "null")
115115
ZEND_END_ARG_INFO()
116116
#endif
117117

0 commit comments

Comments
 (0)