From cb8bb93df5a26514eda8bf8b3c7c3d713a01106b Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Mon, 2 Nov 2020 20:00:28 +0100 Subject: [PATCH 01/12] hash: Support custom algo parameters The concrete need on this change is to support passing an initial seed to the murmur hash. Passing a custom seed is important in terms of randomizing the hash function. The suggested implementation adds a HashTable parameter to all the init callbacks. Further on, an array with custom arguments is accepted from `hash` or `hash_init` from the user land. Currently several things like `hash_hkdf` are not touched, as they don't need passing custom args. Some convenience macros have been added to the SHA/MD families of functions, so the consuming code doesn't have to be changed widely. Special note on what has happened to `ext/hash/tests/hash_serialize_003.phpt` - the test gathers serialization strings produced on different platforms. As the implementation requires adding a new property to the hash context object, it changes the serialization string. However, it doesn't seem practical to repeat all the serialization strings again. The test is changed so it tests that the string can be unserialized on the same platform it was produced on at the runtime. Another way to implement this is to add another type of the init that would accept a HT with arguments. However, that would still require touching all the context structs in all the algos. That would also increase the size of those structs. As an init function is called just once, the way of modifying the existing init callback has been seen preferrable. Signed-off-by: Anatol Belski --- ext/hash/hash.c | 77 ++++++--- ext/hash/hash.stub.php | 10 +- ext/hash/hash_adler32.c | 2 +- ext/hash/hash_arginfo.h | 7 +- ext/hash/hash_crc32.c | 2 +- ext/hash/hash_fnv.c | 4 +- ext/hash/hash_gost.c | 6 +- ext/hash/hash_haval.c | 2 +- ext/hash/hash_joaat.c | 2 +- ext/hash/hash_md.c | 12 +- ext/hash/hash_murmur.c | 51 +++++- ext/hash/hash_ripemd.c | 8 +- ext/hash/hash_sha.c | 38 ++-- ext/hash/hash_sha3.c | 4 +- ext/hash/hash_snefru.c | 2 +- ext/hash/hash_tiger.c | 4 +- ext/hash/hash_whirlpool.c | 2 +- ext/hash/php_hash.h | 4 +- ext/hash/php_hash_adler32.h | 2 +- ext/hash/php_hash_crc32.h | 2 +- ext/hash/php_hash_fnv.h | 4 +- ext/hash/php_hash_gost.h | 2 +- ext/hash/php_hash_haval.h | 2 +- ext/hash/php_hash_joaat.h | 2 +- ext/hash/php_hash_md.h | 6 +- ext/hash/php_hash_murmur.h | 6 +- ext/hash/php_hash_ripemd.h | 8 +- ext/hash/php_hash_sha.h | 18 +- ext/hash/php_hash_sha3.h | 8 +- ext/hash/php_hash_snefru.h | 2 +- ext/hash/php_hash_tiger.h | 4 +- ext/hash/php_hash_whirlpool.h | 2 +- ext/hash/tests/hash_serialize_003.phpt | 230 +------------------------ ext/hash/tests/murmurhash3_seed.phpt | 52 ++++++ ext/standard/md5.c | 2 +- ext/standard/md5.h | 3 +- ext/standard/sha1.c | 2 +- ext/standard/sha1.h | 3 +- 38 files changed, 253 insertions(+), 344 deletions(-) create mode 100644 ext/hash/tests/murmurhash3_seed.phpt diff --git a/ext/hash/hash.c b/ext/hash/hash.c index f7b851f5a49fe..4738d9e84ab9b 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -349,7 +349,7 @@ PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long ma /* Userspace */ static void php_hash_do_hash( - zval *return_value, zend_string *algo, char *data, size_t data_len, zend_bool raw_output, bool isfilename + zval *return_value, zend_string *algo, char *data, size_t data_len, zend_bool raw_output, bool isfilename, HashTable *args ) /* {{{ */ { zend_string *digest; const php_hash_ops *ops; @@ -374,7 +374,7 @@ static void php_hash_do_hash( } context = php_hash_alloc_context(ops); - ops->hash_init(context); + ops->hash_init(context, args); if (isfilename) { char buf[1024]; @@ -418,15 +418,17 @@ PHP_FUNCTION(hash) char *data; size_t data_len; zend_bool raw_output = 0; + HashTable *args = NULL; - ZEND_PARSE_PARAMETERS_START(2, 3) + ZEND_PARSE_PARAMETERS_START(2, 4) Z_PARAM_STR(algo) Z_PARAM_STRING(data, data_len) Z_PARAM_OPTIONAL Z_PARAM_BOOL(raw_output) + Z_PARAM_ARRAY_HT_OR_NULL(args) ZEND_PARSE_PARAMETERS_END(); - php_hash_do_hash(return_value, algo, data, data_len, raw_output, 0); + php_hash_do_hash(return_value, algo, data, data_len, raw_output, 0, args); } /* }}} */ @@ -438,15 +440,17 @@ PHP_FUNCTION(hash_file) char *data; size_t data_len; zend_bool raw_output = 0; + HashTable *args = NULL; ZEND_PARSE_PARAMETERS_START(2, 3) Z_PARAM_STR(algo) Z_PARAM_STRING(data, data_len) Z_PARAM_OPTIONAL Z_PARAM_BOOL(raw_output) + Z_PARAM_ARRAY_HT_OR_NULL(args) ZEND_PARSE_PARAMETERS_END(); - php_hash_do_hash(return_value, algo, data, data_len, raw_output, 1); + php_hash_do_hash(return_value, algo, data, data_len, raw_output, 1, args); } /* }}} */ @@ -468,7 +472,7 @@ static inline void php_hash_hmac_prep_key(unsigned char *K, const php_hash_ops * memset(K, 0, ops->block_size); if (key_len > ops->block_size) { /* Reduce the key first */ - ops->hash_init(context); + ops->hash_init(context, NULL); ops->hash_update(context, key, key_len); ops->hash_final(K, context); } else { @@ -479,7 +483,7 @@ static inline void php_hash_hmac_prep_key(unsigned char *K, const php_hash_ops * } static inline void php_hash_hmac_round(unsigned char *final, const php_hash_ops *ops, void *context, const unsigned char *key, const unsigned char *data, const zend_long data_size) { - ops->hash_init(context); + ops->hash_init(context, NULL); ops->hash_update(context, key, ops->block_size); ops->hash_update(context, data, data_size); ops->hash_final(final, context); @@ -522,7 +526,7 @@ static void php_hash_do_hash_hmac( if (isfilename) { char buf[1024]; ssize_t n; - ops->hash_init(context); + ops->hash_init(context, NULL); ops->hash_update(context, K, ops->block_size); while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) { ops->hash_update(context, (unsigned char *) buf, n); @@ -605,8 +609,9 @@ PHP_FUNCTION(hash_init) void *context; const php_hash_ops *ops; php_hashcontext_object *hash; + HashTable *args = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lS", &algo, &options, &key) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lSh!", &algo, &options, &key, &args) == FAILURE) { RETURN_THROWS(); } @@ -632,12 +637,13 @@ PHP_FUNCTION(hash_init) hash = php_hashcontext_from_object(Z_OBJ_P(return_value)); context = php_hash_alloc_context(ops); - ops->hash_init(context); + ops->hash_init(context, args); hash->ops = ops; hash->context = context; hash->options = options; hash->key = NULL; + hash->args = args ? zend_array_dup(args) : NULL; if (options & PHP_HASH_HMAC) { char *K = emalloc(ops->block_size); @@ -650,7 +656,7 @@ PHP_FUNCTION(hash_init) ops->hash_update(context, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key)); ops->hash_final((unsigned char *) K, context); /* Make the context ready to start over */ - ops->hash_init(context); + ops->hash_init(context, args); } else { memcpy(K, ZSTR_VAL(key), ZSTR_LEN(key)); } @@ -792,7 +798,7 @@ PHP_FUNCTION(hash_final) } /* Feed this result into the outer hash */ - hash->ops->hash_init(hash->context); + hash->ops->hash_init(hash->context, hash->args); hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size); hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(digest), hash->ops->digest_size); hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context); @@ -915,7 +921,7 @@ PHP_FUNCTION(hash_hkdf) context = php_hash_alloc_context(ops); // Extract - ops->hash_init(context); + ops->hash_init(context, NULL); K = emalloc(ops->block_size); php_hash_hmac_prep_key(K, ops, context, (unsigned char *) (salt ? ZSTR_VAL(salt) : ""), salt ? ZSTR_LEN(salt) : 0); @@ -935,7 +941,7 @@ PHP_FUNCTION(hash_hkdf) c[0] = (i & 0xFF); php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size); - ops->hash_init(context); + ops->hash_init(context, NULL); ops->hash_update(context, K, ops->block_size); if (i > 1) { @@ -980,8 +986,9 @@ PHP_FUNCTION(hash_pbkdf2) zend_bool raw_output = 0; const php_hash_ops *ops; void *context; + HashTable *args; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lb", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lbh!", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output, &args) == FAILURE) { RETURN_THROWS(); } @@ -1007,7 +1014,7 @@ PHP_FUNCTION(hash_pbkdf2) } context = php_hash_alloc_context(ops); - ops->hash_init(context); + ops->hash_init(context, args); K1 = emalloc(ops->block_size); K2 = emalloc(ops->block_size); @@ -1196,8 +1203,9 @@ PHP_FUNCTION(mhash) zend_string *algo = NULL; char *data, *key = NULL; size_t data_len, key_len = 0; + HashTable *args; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!", &algorithm, &data, &data_len, &key, &key_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!h!", &algorithm, &data, &data_len, &key, &key_len, &args) == FAILURE) { RETURN_THROWS(); } @@ -1212,7 +1220,7 @@ PHP_FUNCTION(mhash) if (key) { php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, 1, 0); } else { - php_hash_do_hash(return_value, algo, data, data_len, 1, 0); + php_hash_do_hash(return_value, algo, data, data_len, 1, 0, args); } if (algo) { @@ -1282,8 +1290,9 @@ PHP_FUNCTION(mhash_keygen_s2k) char *password, *salt; size_t password_len, salt_len; char padded_salt[SALT_SIZE]; + HashTable *args; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl|h!", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes, &args) == FAILURE) { RETURN_THROWS(); } @@ -1319,13 +1328,13 @@ PHP_FUNCTION(mhash_keygen_s2k) } context = php_hash_alloc_context(ops); - ops->hash_init(context); + ops->hash_init(context, args); key = ecalloc(1, times * block_size); digest = emalloc(ops->digest_size + 1); for (i = 0; i < times; i++) { - ops->hash_init(context); + ops->hash_init(context, args); for (j=0;jhash_update(context, &null, 1); @@ -1378,6 +1387,11 @@ static void php_hashcontext_dtor(zend_object *obj) { efree(hash->key); hash->key = NULL; } + + if (hash->args) { + zend_array_destroy(hash->args); + hash->args = NULL; + } } /* }}} */ @@ -1392,7 +1406,7 @@ static zend_object *php_hashcontext_clone(zend_object *zobj) { newobj->ops = oldobj->ops; newobj->options = oldobj->options; newobj->context = php_hash_alloc_context(newobj->ops); - newobj->ops->hash_init(newobj->context); + newobj->ops->hash_init(newobj->context, oldobj->args); if (SUCCESS != newobj->ops->hash_copy(newobj->ops, oldobj->context, newobj->context)) { efree(newobj->context); @@ -1468,6 +1482,14 @@ PHP_METHOD(HashContext, __serialize) Z_TRY_ADDREF(tmp); zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); + if (hash->args) { + ZVAL_ARR(&tmp, hash->args); + Z_TRY_ADDREF(tmp); + } else { + ZVAL_NULL(&tmp); + } + zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); + return; serialize_failure: @@ -1482,7 +1504,7 @@ PHP_METHOD(HashContext, __unserialize) zval *object = ZEND_THIS; php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object)); HashTable *data; - zval *algo_zv, *magic_zv, *options_zv, *hash_zv, *members_zv; + zval *algo_zv, *magic_zv, *options_zv, *hash_zv, *members_zv, *args_zv; zend_long magic, options; int unserialize_result; const php_hash_ops *ops; @@ -1501,6 +1523,7 @@ PHP_METHOD(HashContext, __unserialize) hash_zv = zend_hash_index_find(data, 2); magic_zv = zend_hash_index_find(data, 3); members_zv = zend_hash_index_find(data, 4); + args_zv = zend_hash_index_find(data, 5); if (!algo_zv || Z_TYPE_P(algo_zv) != IS_STRING || !magic_zv || Z_TYPE_P(magic_zv) != IS_LONG @@ -1529,8 +1552,14 @@ PHP_METHOD(HashContext, __unserialize) hash->ops = ops; hash->context = php_hash_alloc_context(ops); - ops->hash_init(hash->context); hash->options = options; + if (args_zv && IS_ARRAY == Z_TYPE_P(args_zv)) { + ops->hash_init(hash->context, Z_ARRVAL_P(args_zv)); + hash->args = zend_array_dup(Z_ARRVAL_P(args_zv)); + } else { + ops->hash_init(hash->context, NULL); + hash->args = NULL; + } unserialize_result = ops->hash_unserialize(hash, magic, hash_zv); if (unserialize_result != SUCCESS) { diff --git a/ext/hash/hash.stub.php b/ext/hash/hash.stub.php index c0d4cbca7c3d5..85d21f01ae9e1 100644 --- a/ext/hash/hash.stub.php +++ b/ext/hash/hash.stub.php @@ -2,15 +2,15 @@ /** @generate-function-entries */ -function hash(string $algo, string $data, bool $binary = false): string|false {} +function hash(string $algo, string $data, bool $binary = false, array $args = null): string|false {} -function hash_file(string $algo, string $filename, bool $binary = false): string|false {} +function hash_file(string $algo, string $filename, bool $binary = false, array $args = null): string|false {} function hash_hmac(string $algo, string $data, string $key, bool $binary = false): string|false {} function hash_hmac_file(string $algo, string $data, string $key, bool $binary = false): string|false {} -function hash_init(string $algo, int $flags = 0, string $key = ""): HashContext {} +function hash_init(string $algo, int $flags = 0, string $key = "", array $args = null): HashContext {} function hash_update(HashContext $context, string $data): bool {} @@ -39,11 +39,11 @@ function mhash_get_block_size(int $algo): int|false {} function mhash_get_hash_name(int $algo): string|false {} -function mhash_keygen_s2k(int $algo, string $password, string $salt, int $length): string|false {} +function mhash_keygen_s2k(int $algo, string $password, string $salt, int $length, array $args = null): string|false {} function mhash_count(): int {} -function mhash(int $algo, string $data, ?string $key = null): string|false {} +function mhash(int $algo, string $data, ?string $key = null, array $args = null): string|false {} #endif final class HashContext diff --git a/ext/hash/hash_adler32.c b/ext/hash/hash_adler32.c index d45012f8e6d51..5acd621b2d3f4 100644 --- a/ext/hash/hash_adler32.c +++ b/ext/hash/hash_adler32.c @@ -18,7 +18,7 @@ #include "php_hash.h" #include "php_hash_adler32.h" -PHP_HASH_API void PHP_ADLER32Init(PHP_ADLER32_CTX *context) +PHP_HASH_API void PHP_ADLER32Init(PHP_ADLER32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->state = 1; } diff --git a/ext/hash/hash_arginfo.h b/ext/hash/hash_arginfo.h index db043da97b477..8cd6b9c7479a7 100644 --- a/ext/hash/hash_arginfo.h +++ b/ext/hash/hash_arginfo.h @@ -1,16 +1,18 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 9352e0ac98e2ac53dc15d5024f9ef0c8092c4e9c */ + * Stub hash: d93291f14a495dc68a307af8c246469e79ba3979 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash, 0, 2, MAY_BE_STRING|MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, binary, _IS_BOOL, 0, "false") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "null") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash_file, 0, 2, MAY_BE_STRING|MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, binary, _IS_BOOL, 0, "false") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "null") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash_hmac, 0, 3, MAY_BE_STRING|MAY_BE_FALSE) @@ -26,6 +28,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_hash_init, 0, 1, HashContext, 0) ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "0") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, key, IS_STRING, 0, "\"\"") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "null") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_hash_update, 0, 2, _IS_BOOL, 0) @@ -99,6 +102,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mhash_keygen_s2k, 0, 4, MAY_BE_S ZEND_ARG_TYPE_INFO(0, password, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, salt, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, length, IS_LONG, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "null") ZEND_END_ARG_INFO() #endif @@ -112,6 +116,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mhash, 0, 2, MAY_BE_STRING|MAY_B ZEND_ARG_TYPE_INFO(0, algo, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, key, IS_STRING, 1, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "null") ZEND_END_ARG_INFO() #endif diff --git a/ext/hash/hash_crc32.c b/ext/hash/hash_crc32.c index ade32a3b35c1c..d77cdde013fbd 100644 --- a/ext/hash/hash_crc32.c +++ b/ext/hash/hash_crc32.c @@ -20,7 +20,7 @@ #include "php_hash_crc32_tables.h" #include "ext/standard/crc32_x86.h" -PHP_HASH_API void PHP_CRC32Init(PHP_CRC32_CTX *context) +PHP_HASH_API void PHP_CRC32Init(PHP_CRC32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->state = ~0; } diff --git a/ext/hash/hash_fnv.c b/ext/hash/hash_fnv.c index 2ee81b9c8601b..3a48d404841a3 100644 --- a/ext/hash/hash_fnv.c +++ b/ext/hash/hash_fnv.c @@ -83,7 +83,7 @@ const php_hash_ops php_hash_fnv1a64_ops = { /* {{{ PHP_FNV132Init * 32-bit FNV-1 hash initialisation */ -PHP_HASH_API void PHP_FNV132Init(PHP_FNV132_CTX *context) +PHP_HASH_API void PHP_FNV132Init(PHP_FNV132_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->state = PHP_FNV1_32_INIT; } @@ -118,7 +118,7 @@ PHP_HASH_API void PHP_FNV132Final(unsigned char digest[4], PHP_FNV132_CTX * cont /* {{{ PHP_FNV164Init * 64-bit FNV-1 hash initialisation */ -PHP_HASH_API void PHP_FNV164Init(PHP_FNV164_CTX *context) +PHP_HASH_API void PHP_FNV164Init(PHP_FNV164_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->state = PHP_FNV1_64_INIT; } diff --git a/ext/hash/hash_gost.c b/ext/hash/hash_gost.c index 46ea032c32e30..d1f4a31765a9a 100644 --- a/ext/hash/hash_gost.c +++ b/ext/hash/hash_gost.c @@ -235,15 +235,15 @@ static inline void GostTransform(PHP_GOST_CTX *context, const unsigned char inpu Gost(context, data); } -PHP_HASH_API void PHP_GOSTInit(PHP_GOST_CTX *context) +PHP_HASH_API void PHP_GOSTInit(PHP_GOST_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { memset(context, 0, sizeof(*context)); context->tables = &tables_test; } -PHP_HASH_API void PHP_GOSTInitCrypto(PHP_GOST_CTX *context) +PHP_HASH_API void PHP_GOSTInitCrypto(PHP_GOST_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { - PHP_GOSTInit(context); + PHP_GOSTInit(context, NULL); context->tables = &tables_crypto; } diff --git a/ext/hash/hash_haval.c b/ext/hash/hash_haval.c index 84ff242bd90d5..9b117db8d9b01 100644 --- a/ext/hash/hash_haval.c +++ b/ext/hash/hash_haval.c @@ -253,7 +253,7 @@ const php_hash_ops php_hash_##p##haval##b##_ops = { \ php_hash_unserialize, \ PHP_HAVAL_SPEC, \ ((b) / 8), 128, sizeof(PHP_HAVAL_CTX), 1 }; \ -PHP_HASH_API void PHP_##p##HAVAL##b##Init(PHP_HAVAL_CTX *context) \ +PHP_HASH_API void PHP_##p##HAVAL##b##Init(PHP_HAVAL_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) \ { int i; context->count[0] = context->count[1] = 0; \ for(i = 0; i < 8; i++) context->state[i] = D0[i]; \ context->passes = p; context->output = b; \ diff --git a/ext/hash/hash_joaat.c b/ext/hash/hash_joaat.c index 5d5a2e53b9b39..41fde52f30474 100644 --- a/ext/hash/hash_joaat.c +++ b/ext/hash/hash_joaat.c @@ -36,7 +36,7 @@ const php_hash_ops php_hash_joaat_ops = { 0 }; -PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context) +PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->state = 0; } diff --git a/ext/hash/hash_md.c b/ext/hash/hash_md.c index 94fafbbf798a8..a34936d625304 100644 --- a/ext/hash/hash_md.c +++ b/ext/hash/hash_md.c @@ -19,7 +19,7 @@ const php_hash_ops php_hash_md5_ops = { "md5", - (php_hash_init_func_t) PHP_MD5Init, + (php_hash_init_func_t) PHP_MD5InitArgs, (php_hash_update_func_t) PHP_MD5Update, (php_hash_final_func_t) PHP_MD5Final, php_hash_copy, @@ -34,7 +34,7 @@ const php_hash_ops php_hash_md5_ops = { const php_hash_ops php_hash_md4_ops = { "md4", - (php_hash_init_func_t) PHP_MD4Init, + (php_hash_init_func_t) PHP_MD4InitArgs, (php_hash_update_func_t) PHP_MD4Update, (php_hash_final_func_t) PHP_MD4Final, php_hash_copy, @@ -51,7 +51,7 @@ static int php_md2_unserialize(php_hashcontext_object *hash, zend_long magic, co const php_hash_ops php_hash_md2_ops = { "md2", - (php_hash_init_func_t) PHP_MD2Init, + (php_hash_init_func_t) PHP_MD2InitArgs, (php_hash_update_func_t) PHP_MD2Update, (php_hash_final_func_t) PHP_MD2Final, php_hash_copy, @@ -182,10 +182,10 @@ static void MD4Transform(uint32_t state[4], const unsigned char block[64]) state[3] += d; } -/* {{{ PHP_MD4Init +/* {{{ PHP_MD4InitArgs * MD4 initialization. Begins an MD4 operation, writing a new context. */ -PHP_HASH_API void PHP_MD4Init(PHP_MD4_CTX * context) +PHP_HASH_API void PHP_MD4InitArgs(PHP_MD4_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. @@ -287,7 +287,7 @@ static const unsigned char MD2_S[256] = { 242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 }; -PHP_HASH_API void PHP_MD2Init(PHP_MD2_CTX *context) +PHP_HASH_API void PHP_MD2InitArgs(PHP_MD2_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { memset(context, 0, sizeof(PHP_MD2_CTX)); } diff --git a/ext/hash/hash_murmur.c b/ext/hash/hash_murmur.c index ab01b4f19fce6..44b5ad3cbd72c 100644 --- a/ext/hash/hash_murmur.c +++ b/ext/hash/hash_murmur.c @@ -36,9 +36,20 @@ const php_hash_ops php_hash_murmur3a_ops = { 0 }; -PHP_HASH_API void PHP_MURMUR3AInit(PHP_MURMUR3A_CTX *ctx) +PHP_HASH_API void PHP_MURMUR3AInit(PHP_MURMUR3A_CTX *ctx, HashTable *args) { - ctx->h = 0; + if (args) { + zval *seed; + /* This might be a bit too restrictive, but thinking that a seed might be set + once and for all, it should be done a clean way. */ + if ((NULL != (seed = zend_hash_str_find(args, "seed", sizeof("seed") - 1))) && IS_LONG == Z_TYPE_P(seed)) { + ctx->h = (uint32_t)Z_LVAL_P(seed); + } else { + ctx->h = 0; + } + } else { + ctx->h = 0; + } ctx->carry = 0; ctx->len = 0; } @@ -82,9 +93,24 @@ const php_hash_ops php_hash_murmur3c_ops = { 0 }; -PHP_HASH_API void PHP_MURMUR3CInit(PHP_MURMUR3C_CTX *ctx) +PHP_HASH_API void PHP_MURMUR3CInit(PHP_MURMUR3C_CTX *ctx, HashTable *args) { - memset(&ctx->h, 0, sizeof ctx->h); + if (args) { + zval *seed; + /* This might be a bit too restrictive, but thinking that a seed might be set + once and for all, it should be done a clean way. */ + if ((NULL != (seed = zend_hash_str_find(args, "seed", sizeof("seed") - 1))) && IS_LONG == Z_TYPE_P(seed)) { + uint32_t _seed = (uint32_t)Z_LVAL_P(seed); + ctx->h[0] = _seed; + ctx->h[1] = _seed; + ctx->h[2] = _seed; + ctx->h[3] = _seed; + } else { + memset(&ctx->h, 0, sizeof ctx->h); + } + } else { + memset(&ctx->h, 0, sizeof ctx->h); + } memset(&ctx->carry, 0, sizeof ctx->carry); ctx->len = 0; } @@ -141,9 +167,22 @@ const php_hash_ops php_hash_murmur3f_ops = { 0 }; -PHP_HASH_API void PHP_MURMUR3FInit(PHP_MURMUR3F_CTX *ctx) +PHP_HASH_API void PHP_MURMUR3FInit(PHP_MURMUR3F_CTX *ctx, HashTable *args) { - memset(&ctx->h, 0, sizeof ctx->h); + if (args) { + zval *seed; + /* This might be a bit too restrictive, but thinking that a seed might be set + once and for all, it should be done a clean way. */ + if ((NULL != (seed = zend_hash_str_find(args, "seed", sizeof("seed") - 1))) && IS_LONG == Z_TYPE_P(seed)) { + uint64_t _seed = (uint64_t)Z_LVAL_P(seed); + ctx->h[0] = _seed; + ctx->h[1] = _seed; + } else { + memset(&ctx->h, 0, sizeof ctx->h); + } + } else { + memset(&ctx->h, 0, sizeof ctx->h); + } memset(&ctx->carry, 0, sizeof ctx->carry); ctx->len = 0; } diff --git a/ext/hash/hash_ripemd.c b/ext/hash/hash_ripemd.c index db1d1dc02b9e2..58c40b06a78cc 100644 --- a/ext/hash/hash_ripemd.c +++ b/ext/hash/hash_ripemd.c @@ -84,7 +84,7 @@ const php_hash_ops php_hash_ripemd320_ops = { /* {{{ PHP_RIPEMD128Init * ripemd128 initialization. Begins a ripemd128 operation, writing a new context. */ -PHP_HASH_API void PHP_RIPEMD128Init(PHP_RIPEMD128_CTX * context) +PHP_HASH_API void PHP_RIPEMD128Init(PHP_RIPEMD128_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. @@ -99,7 +99,7 @@ PHP_HASH_API void PHP_RIPEMD128Init(PHP_RIPEMD128_CTX * context) /* {{{ PHP_RIPEMD256Init * ripemd256 initialization. Begins a ripemd256 operation, writing a new context. */ -PHP_HASH_API void PHP_RIPEMD256Init(PHP_RIPEMD256_CTX * context) +PHP_HASH_API void PHP_RIPEMD256Init(PHP_RIPEMD256_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. @@ -118,7 +118,7 @@ PHP_HASH_API void PHP_RIPEMD256Init(PHP_RIPEMD256_CTX * context) /* {{{ PHP_RIPEMD160Init * ripemd160 initialization. Begins a ripemd160 operation, writing a new context. */ -PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX * context) +PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. @@ -134,7 +134,7 @@ PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX * context) /* {{{ PHP_RIPEMD320Init * ripemd320 initialization. Begins a ripemd320 operation, writing a new context. */ -PHP_HASH_API void PHP_RIPEMD320Init(PHP_RIPEMD320_CTX * context) +PHP_HASH_API void PHP_RIPEMD320Init(PHP_RIPEMD320_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. diff --git a/ext/hash/hash_sha.c b/ext/hash/hash_sha.c index 4ea5b768d703c..acc23a21bf096 100644 --- a/ext/hash/hash_sha.c +++ b/ext/hash/hash_sha.c @@ -64,7 +64,7 @@ static void SHADecode32(uint32_t *output, const unsigned char *input, unsigned i const php_hash_ops php_hash_sha1_ops = { "sha1", - (php_hash_init_func_t) PHP_SHA1Init, + (php_hash_init_func_t) PHP_SHA1InitArgs, (php_hash_update_func_t) PHP_SHA1Update, (php_hash_final_func_t) PHP_SHA1Final, php_hash_copy, @@ -81,7 +81,7 @@ const php_hash_ops php_hash_sha1_ops = { const php_hash_ops php_hash_sha256_ops = { "sha256", - (php_hash_init_func_t) PHP_SHA256Init, + (php_hash_init_func_t) PHP_SHA256InitArgs, (php_hash_update_func_t) PHP_SHA256Update, (php_hash_final_func_t) PHP_SHA256Final, php_hash_copy, @@ -96,7 +96,7 @@ const php_hash_ops php_hash_sha256_ops = { const php_hash_ops php_hash_sha224_ops = { "sha224", - (php_hash_init_func_t) PHP_SHA224Init, + (php_hash_init_func_t) PHP_SHA224InitArgs, (php_hash_update_func_t) PHP_SHA224Update, (php_hash_final_func_t) PHP_SHA224Final, php_hash_copy, @@ -136,10 +136,10 @@ static const uint32_t SHA256_K[64] = { 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; -/* {{{ PHP_SHA256Init +/* {{{ PHP_SHA256InitArgs * SHA256 initialization. Begins an SHA256 operation, writing a new context. */ -PHP_HASH_API void PHP_SHA256Init(PHP_SHA256_CTX * context) +PHP_HASH_API void PHP_SHA256InitArgs(PHP_SHA256_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. @@ -196,10 +196,10 @@ static void SHA256Transform(uint32_t state[8], const unsigned char block[64]) } /* }}} */ -/* {{{ PHP_SHA224Init +/* {{{ PHP_SHA224InitArgs * SHA224 initialization. Begins an SHA224 operation, writing a new context. */ -PHP_HASH_API void PHP_SHA224Init(PHP_SHA224_CTX * context) +PHP_HASH_API void PHP_SHA224InitArgs(PHP_SHA224_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. @@ -445,10 +445,10 @@ static void SHADecode64(uint64_t *output, const unsigned char *input, unsigned i } /* }}} */ -/* {{{ PHP_SHA384Init +/* {{{ PHP_SHA384InitArgs * SHA384 initialization. Begins an SHA384 operation, writing a new context. */ -PHP_HASH_API void PHP_SHA384Init(PHP_SHA384_CTX * context) +PHP_HASH_API void PHP_SHA384InitArgs(PHP_SHA384_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. @@ -591,7 +591,7 @@ PHP_HASH_API void PHP_SHA384Final(unsigned char digest[48], PHP_SHA384_CTX * con const php_hash_ops php_hash_sha384_ops = { "sha384", - (php_hash_init_func_t) PHP_SHA384Init, + (php_hash_init_func_t) PHP_SHA384InitArgs, (php_hash_update_func_t) PHP_SHA384Update, (php_hash_final_func_t) PHP_SHA384Final, php_hash_copy, @@ -604,10 +604,10 @@ const php_hash_ops php_hash_sha384_ops = { 1 }; -/* {{{ PHP_SHA512Init +/* {{{ PHP_SHA512InitArgs * SHA512 initialization. Begins an SHA512 operation, writing a new context. */ -PHP_HASH_API void PHP_SHA512Init(PHP_SHA512_CTX * context) +PHP_HASH_API void PHP_SHA512InitArgs(PHP_SHA512_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. @@ -623,10 +623,10 @@ PHP_HASH_API void PHP_SHA512Init(PHP_SHA512_CTX * context) } /* }}} */ -/* {{{ PHP_SHA512_256Init +/* {{{ PHP_SHA512_256InitArgs * SHA512/245 initialization. Identical algorithm to SHA512, using alternate initval and truncation */ -PHP_HASH_API void PHP_SHA512_256Init(PHP_SHA512_CTX * context) +PHP_HASH_API void PHP_SHA512_256InitArgs(PHP_SHA512_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; @@ -641,10 +641,10 @@ PHP_HASH_API void PHP_SHA512_256Init(PHP_SHA512_CTX * context) } /* }}} */ -/* {{{ PHP_SHA512_224Init +/* {{{ PHP_SHA512_224InitArgs * SHA512/224 initialization. Identical algorithm to SHA512, using alternate initval and truncation */ -PHP_HASH_API void PHP_SHA512_224Init(PHP_SHA512_CTX * context) +PHP_HASH_API void PHP_SHA512_224InitArgs(PHP_SHA512_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; @@ -768,7 +768,7 @@ PHP_HASH_API void PHP_SHA512_224Final(unsigned char digest[28], PHP_SHA512_CTX * const php_hash_ops php_hash_sha512_ops = { "sha512", - (php_hash_init_func_t) PHP_SHA512Init, + (php_hash_init_func_t) PHP_SHA512InitArgs, (php_hash_update_func_t) PHP_SHA512Update, (php_hash_final_func_t) PHP_SHA512Final, php_hash_copy, @@ -783,7 +783,7 @@ const php_hash_ops php_hash_sha512_ops = { const php_hash_ops php_hash_sha512_256_ops = { "sha512/256", - (php_hash_init_func_t) PHP_SHA512_256Init, + (php_hash_init_func_t) PHP_SHA512_256InitArgs, (php_hash_update_func_t) PHP_SHA512_256Update, (php_hash_final_func_t) PHP_SHA512_256Final, php_hash_copy, @@ -798,7 +798,7 @@ const php_hash_ops php_hash_sha512_256_ops = { const php_hash_ops php_hash_sha512_224_ops = { "sha512/224", - (php_hash_init_func_t) PHP_SHA512_224Init, + (php_hash_init_func_t) PHP_SHA512_224InitArgs, (php_hash_update_func_t) PHP_SHA512_224Update, (php_hash_final_func_t) PHP_SHA512_224Final, php_hash_copy, diff --git a/ext/hash/hash_sha3.c b/ext/hash/hash_sha3.c index 52bd495f9df60..123e599d04c31 100644 --- a/ext/hash/hash_sha3.c +++ b/ext/hash/hash_sha3.c @@ -220,7 +220,7 @@ static int php_sha3_unserialize(php_hashcontext_object *hash, // ========================================================================== #define DECLARE_SHA3_OPS(bits) \ -void PHP_SHA3##bits##Init(PHP_SHA3_##bits##_CTX* ctx) { \ +void PHP_SHA3##bits##Init(PHP_SHA3_##bits##_CTX* ctx, ZEND_ATTRIBUTE_UNUSED HashTable *args) { \ PHP_SHA3_Init(ctx, bits); \ } \ void PHP_SHA3##bits##Update(PHP_SHA3_##bits##_CTX* ctx, \ @@ -315,7 +315,7 @@ static int php_keccak_unserialize(php_hashcontext_object *hash, zend_long magic, // ========================================================================== #define DECLARE_SHA3_OPS(bits) \ -void PHP_SHA3##bits##Init(PHP_SHA3_##bits##_CTX* ctx) { \ +void PHP_SHA3##bits##Init(PHP_SHA3_##bits##_CTX* ctx, ZEND_ATTRIBUTE_UNUSED HashTable *args) { \ ZEND_ASSERT(sizeof(Keccak_HashInstance) <= sizeof(PHP_SHA3_##bits##_CTX)); \ Keccak_HashInitialize_SHA3_##bits((Keccak_HashInstance *)ctx); \ } \ diff --git a/ext/hash/hash_snefru.c b/ext/hash/hash_snefru.c index 292bfef2cb8da..66f9300b11506 100644 --- a/ext/hash/hash_snefru.c +++ b/ext/hash/hash_snefru.c @@ -128,7 +128,7 @@ static inline void SnefruTransform(PHP_SNEFRU_CTX *context, const unsigned char ZEND_SECURE_ZERO(&context->state[8], sizeof(uint32_t) * 8); } -PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *context) +PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { memset(context, 0, sizeof(*context)); } diff --git a/ext/hash/hash_tiger.c b/ext/hash/hash_tiger.c index 8e0f365dc49eb..b360dd6adec31 100644 --- a/ext/hash/hash_tiger.c +++ b/ext/hash/hash_tiger.c @@ -174,7 +174,7 @@ static inline void TigerDigest(unsigned char *digest_str, unsigned int digest_le } } -PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context) +PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { memset(context, 0, sizeof(*context)); context->state[0] = L64(0x0123456789ABCDEF); @@ -182,7 +182,7 @@ PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context) context->state[2] = L64(0xF096A5B4C3B2E187); } -PHP_HASH_API void PHP_4TIGERInit(PHP_TIGER_CTX *context) +PHP_HASH_API void PHP_4TIGERInit(PHP_TIGER_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { memset(context, 0, sizeof(*context)); context->passes = 1; diff --git a/ext/hash/hash_whirlpool.c b/ext/hash/hash_whirlpool.c index 8d5cbb77370a8..2a64e864c1cc4 100644 --- a/ext/hash/hash_whirlpool.c +++ b/ext/hash/hash_whirlpool.c @@ -263,7 +263,7 @@ static void WhirlpoolTransform(PHP_WHIRLPOOL_CTX *context) ZEND_SECURE_ZERO(state, sizeof(state)); } -PHP_HASH_API void PHP_WHIRLPOOLInit(PHP_WHIRLPOOL_CTX *context) +PHP_HASH_API void PHP_WHIRLPOOLInit(PHP_WHIRLPOOL_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { memset(context, 0, sizeof(*context)); } diff --git a/ext/hash/php_hash.h b/ext/hash/php_hash.h index 69d1330072daf..88eaff713fc8a 100644 --- a/ext/hash/php_hash.h +++ b/ext/hash/php_hash.h @@ -31,7 +31,7 @@ typedef struct _php_hashcontext_object php_hashcontext_object; -typedef void (*php_hash_init_func_t)(void *context); +typedef void (*php_hash_init_func_t)(void *context, HashTable *args); typedef void (*php_hash_update_func_t)(void *context, const unsigned char *buf, size_t count); typedef void (*php_hash_final_func_t)(unsigned char *digest, void *context); typedef int (*php_hash_copy_func_t)(const void *ops, void *orig_context, void *dest_context); @@ -61,6 +61,8 @@ struct _php_hashcontext_object { zend_long options; unsigned char *key; + HashTable *args; + zend_object std; }; diff --git a/ext/hash/php_hash_adler32.h b/ext/hash/php_hash_adler32.h index 049f16b28e956..db2db38114012 100644 --- a/ext/hash/php_hash_adler32.h +++ b/ext/hash/php_hash_adler32.h @@ -24,7 +24,7 @@ typedef struct { } PHP_ADLER32_CTX; #define PHP_ADLER32_SPEC "l." -PHP_HASH_API void PHP_ADLER32Init(PHP_ADLER32_CTX *context); +PHP_HASH_API void PHP_ADLER32Init(PHP_ADLER32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args); PHP_HASH_API void PHP_ADLER32Update(PHP_ADLER32_CTX *context, const unsigned char *input, size_t len); PHP_HASH_API void PHP_ADLER32Final(unsigned char digest[4], PHP_ADLER32_CTX *context); PHP_HASH_API int PHP_ADLER32Copy(const php_hash_ops *ops, PHP_ADLER32_CTX *orig_context, PHP_ADLER32_CTX *copy_context); diff --git a/ext/hash/php_hash_crc32.h b/ext/hash/php_hash_crc32.h index 4c1b0fedc9159..1003e4b39bc07 100644 --- a/ext/hash/php_hash_crc32.h +++ b/ext/hash/php_hash_crc32.h @@ -24,7 +24,7 @@ typedef struct { } PHP_CRC32_CTX; #define PHP_CRC32_SPEC "l." -PHP_HASH_API void PHP_CRC32Init(PHP_CRC32_CTX *context); +PHP_HASH_API void PHP_CRC32Init(PHP_CRC32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args); PHP_HASH_API void PHP_CRC32Update(PHP_CRC32_CTX *context, const unsigned char *input, size_t len); PHP_HASH_API void PHP_CRC32BUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len); PHP_HASH_API void PHP_CRC32CUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len); diff --git a/ext/hash/php_hash_fnv.h b/ext/hash/php_hash_fnv.h index 6728b2e90205e..f4dacb223e894 100644 --- a/ext/hash/php_hash_fnv.h +++ b/ext/hash/php_hash_fnv.h @@ -52,12 +52,12 @@ typedef struct { #define PHP_FNV164_SPEC "q." -PHP_HASH_API void PHP_FNV132Init(PHP_FNV132_CTX *context); +PHP_HASH_API void PHP_FNV132Init(PHP_FNV132_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args); PHP_HASH_API void PHP_FNV132Update(PHP_FNV132_CTX *context, const unsigned char *input, size_t inputLen); PHP_HASH_API void PHP_FNV1a32Update(PHP_FNV132_CTX *context, const unsigned char *input, size_t inputLen); PHP_HASH_API void PHP_FNV132Final(unsigned char digest[16], PHP_FNV132_CTX * context); -PHP_HASH_API void PHP_FNV164Init(PHP_FNV164_CTX *context); +PHP_HASH_API void PHP_FNV164Init(PHP_FNV164_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args); PHP_HASH_API void PHP_FNV164Update(PHP_FNV164_CTX *context, const unsigned char *input, size_t inputLen); PHP_HASH_API void PHP_FNV1a64Update(PHP_FNV164_CTX *context, const unsigned char *input, size_t inputLen); PHP_HASH_API void PHP_FNV164Final(unsigned char digest[16], PHP_FNV164_CTX * context); diff --git a/ext/hash/php_hash_gost.h b/ext/hash/php_hash_gost.h index eb9441faa66a4..850b089506810 100644 --- a/ext/hash/php_hash_gost.h +++ b/ext/hash/php_hash_gost.h @@ -29,7 +29,7 @@ typedef struct { } PHP_GOST_CTX; #define PHP_GOST_SPEC "l16l2bb32" -PHP_HASH_API void PHP_GOSTInit(PHP_GOST_CTX *); +PHP_HASH_API void PHP_GOSTInit(PHP_GOST_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *args); PHP_HASH_API void PHP_GOSTUpdate(PHP_GOST_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_GOSTFinal(unsigned char[64], PHP_GOST_CTX *); diff --git a/ext/hash/php_hash_haval.h b/ext/hash/php_hash_haval.h index 43802b41834fa..2ae0af6bf8ea2 100644 --- a/ext/hash/php_hash_haval.h +++ b/ext/hash/php_hash_haval.h @@ -30,7 +30,7 @@ typedef struct { } PHP_HAVAL_CTX; #define PHP_HAVAL_SPEC "l8l2b128" -#define PHP_HASH_HAVAL_INIT_DECL(p,b) PHP_HASH_API void PHP_##p##HAVAL##b##Init(PHP_HAVAL_CTX *); \ +#define PHP_HASH_HAVAL_INIT_DECL(p,b) PHP_HASH_API void PHP_##p##HAVAL##b##Init(PHP_HAVAL_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); \ PHP_HASH_API void PHP_HAVAL##b##Final(unsigned char*, PHP_HAVAL_CTX *); PHP_HASH_API void PHP_HAVALUpdate(PHP_HAVAL_CTX *, const unsigned char *, size_t); diff --git a/ext/hash/php_hash_joaat.h b/ext/hash/php_hash_joaat.h index b0df8a67b38ae..113983c2a4254 100644 --- a/ext/hash/php_hash_joaat.h +++ b/ext/hash/php_hash_joaat.h @@ -22,7 +22,7 @@ typedef struct { } PHP_JOAAT_CTX; #define PHP_JOAAT_SPEC "l." -PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context); +PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args); PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *input, size_t inputLen); PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[16], PHP_JOAAT_CTX * context); diff --git a/ext/hash/php_hash_md.h b/ext/hash/php_hash_md.h index 2a677fe54d662..ce155b3e1f295 100644 --- a/ext/hash/php_hash_md.h +++ b/ext/hash/php_hash_md.h @@ -28,7 +28,8 @@ typedef struct { } PHP_MD4_CTX; #define PHP_MD4_SPEC "l4l2b64." -PHP_HASH_API void PHP_MD4Init(PHP_MD4_CTX *); +#define PHP_MD4Init(ctx) PHP_MD4InitArgs(ctx, NULL) +PHP_HASH_API void PHP_MD4InitArgs(PHP_MD4_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_MD4Update(PHP_MD4_CTX *context, const unsigned char *, size_t); PHP_HASH_API void PHP_MD4Final(unsigned char[16], PHP_MD4_CTX *); @@ -41,7 +42,8 @@ typedef struct { } PHP_MD2_CTX; #define PHP_MD2_SPEC "b48b16b16b." -PHP_HASH_API void PHP_MD2Init(PHP_MD2_CTX *context); +#define PHP_MD2Init(ctx) PHP_MD2InitArgs(ctx, NULL) +PHP_HASH_API void PHP_MD2InitArgs(PHP_MD2_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args); PHP_HASH_API void PHP_MD2Update(PHP_MD2_CTX *context, const unsigned char *, size_t); PHP_HASH_API void PHP_MD2Final(unsigned char[16], PHP_MD2_CTX *); diff --git a/ext/hash/php_hash_murmur.h b/ext/hash/php_hash_murmur.h index 100a8d1fa32f1..598912141b5c4 100644 --- a/ext/hash/php_hash_murmur.h +++ b/ext/hash/php_hash_murmur.h @@ -24,7 +24,7 @@ typedef struct { } PHP_MURMUR3A_CTX; #define PHP_MURMUR3A_SPEC "lll" -PHP_HASH_API void PHP_MURMUR3AInit(PHP_MURMUR3A_CTX *ctx); +PHP_HASH_API void PHP_MURMUR3AInit(PHP_MURMUR3A_CTX *ctx, HashTable *args); PHP_HASH_API void PHP_MURMUR3AUpdate(PHP_MURMUR3A_CTX *ctx, const unsigned char *in, size_t len); PHP_HASH_API void PHP_MURMUR3AFinal(unsigned char digest[4], PHP_MURMUR3A_CTX *ctx); PHP_HASH_API int PHP_MURMUR3ACopy(const php_hash_ops *ops, PHP_MURMUR3A_CTX *orig_context, PHP_MURMUR3A_CTX *copy_context); @@ -36,7 +36,7 @@ typedef struct { } PHP_MURMUR3C_CTX; #define PHP_MURMUR3C_SPEC "lllllllll" -PHP_HASH_API void PHP_MURMUR3CInit(PHP_MURMUR3C_CTX *ctx); +PHP_HASH_API void PHP_MURMUR3CInit(PHP_MURMUR3C_CTX *ctx, HashTable *args); PHP_HASH_API void PHP_MURMUR3CUpdate(PHP_MURMUR3C_CTX *ctx, const unsigned char *in, size_t len); PHP_HASH_API void PHP_MURMUR3CFinal(unsigned char digest[16], PHP_MURMUR3C_CTX *ctx); PHP_HASH_API int PHP_MURMUR3CCopy(const php_hash_ops *ops, PHP_MURMUR3C_CTX *orig_context, PHP_MURMUR3C_CTX *copy_context); @@ -48,7 +48,7 @@ typedef struct { } PHP_MURMUR3F_CTX; #define PHP_MURMUR3F_SPEC "qqqql" -PHP_HASH_API void PHP_MURMUR3FInit(PHP_MURMUR3F_CTX *ctx); +PHP_HASH_API void PHP_MURMUR3FInit(PHP_MURMUR3F_CTX *ctx, HashTable *args); PHP_HASH_API void PHP_MURMUR3FUpdate(PHP_MURMUR3F_CTX *ctx, const unsigned char *in, size_t len); PHP_HASH_API void PHP_MURMUR3FFinal(unsigned char digest[16], PHP_MURMUR3F_CTX *ctx); PHP_HASH_API int PHP_MURMUR3FCopy(const php_hash_ops *ops, PHP_MURMUR3F_CTX *orig_context, PHP_MURMUR3F_CTX *copy_context); diff --git a/ext/hash/php_hash_ripemd.h b/ext/hash/php_hash_ripemd.h index dd9913b85b72a..55a430a4f2bdc 100644 --- a/ext/hash/php_hash_ripemd.h +++ b/ext/hash/php_hash_ripemd.h @@ -47,19 +47,19 @@ typedef struct { } PHP_RIPEMD320_CTX; #define PHP_RIPEMD320_SPEC "l10l2b64." -PHP_HASH_API void PHP_RIPEMD128Init(PHP_RIPEMD128_CTX *); +PHP_HASH_API void PHP_RIPEMD128Init(PHP_RIPEMD128_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_RIPEMD128Update(PHP_RIPEMD128_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_RIPEMD128Final(unsigned char[16], PHP_RIPEMD128_CTX *); -PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX *); +PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_RIPEMD160Update(PHP_RIPEMD160_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_RIPEMD160Final(unsigned char[20], PHP_RIPEMD160_CTX *); -PHP_HASH_API void PHP_RIPEMD256Init(PHP_RIPEMD256_CTX *); +PHP_HASH_API void PHP_RIPEMD256Init(PHP_RIPEMD256_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_RIPEMD256Update(PHP_RIPEMD256_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_RIPEMD256Final(unsigned char[32], PHP_RIPEMD256_CTX *); -PHP_HASH_API void PHP_RIPEMD320Init(PHP_RIPEMD320_CTX *); +PHP_HASH_API void PHP_RIPEMD320Init(PHP_RIPEMD320_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_RIPEMD320Update(PHP_RIPEMD320_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_RIPEMD320Final(unsigned char[40], PHP_RIPEMD320_CTX *); diff --git a/ext/hash/php_hash_sha.h b/ext/hash/php_hash_sha.h index 16da9273636a7..4240a6228e147 100644 --- a/ext/hash/php_hash_sha.h +++ b/ext/hash/php_hash_sha.h @@ -29,7 +29,8 @@ typedef struct { } PHP_SHA224_CTX; #define PHP_SHA224_SPEC "l8l2b64." -PHP_HASH_API void PHP_SHA224Init(PHP_SHA224_CTX *); +#define PHP_SHA224Init(ctx) PHP_SHA224InitArgs(ctx, NULL) +PHP_HASH_API void PHP_SHA224InitArgs(PHP_SHA224_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_SHA224Update(PHP_SHA224_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_SHA224Final(unsigned char[28], PHP_SHA224_CTX *); @@ -41,7 +42,8 @@ typedef struct { } PHP_SHA256_CTX; #define PHP_SHA256_SPEC "l8l2b64." -PHP_HASH_API void PHP_SHA256Init(PHP_SHA256_CTX *); +#define PHP_SHA256Init(ctx) PHP_SHA256InitArgs(ctx, NULL) +PHP_HASH_API void PHP_SHA256InitArgs(PHP_SHA256_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_SHA256Update(PHP_SHA256_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_SHA256Final(unsigned char[32], PHP_SHA256_CTX *); @@ -53,7 +55,8 @@ typedef struct { } PHP_SHA384_CTX; #define PHP_SHA384_SPEC "q8q2b128." -PHP_HASH_API void PHP_SHA384Init(PHP_SHA384_CTX *); +#define PHP_SHA384Init(ctx) PHP_SHA384InitArgs(ctx, NULL) +PHP_HASH_API void PHP_SHA384InitArgs(PHP_SHA384_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_SHA384Update(PHP_SHA384_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_SHA384Final(unsigned char[48], PHP_SHA384_CTX *); @@ -65,15 +68,18 @@ typedef struct { } PHP_SHA512_CTX; #define PHP_SHA512_SPEC "q8q2b128." -PHP_HASH_API void PHP_SHA512Init(PHP_SHA512_CTX *); +#define PHP_SHA512Init(ctx) PHP_SHA512InitArgs(ctx, NULL) +PHP_HASH_API void PHP_SHA512InitArgs(PHP_SHA512_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_SHA512Update(PHP_SHA512_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_SHA512Final(unsigned char[64], PHP_SHA512_CTX *); -PHP_HASH_API void PHP_SHA512_256Init(PHP_SHA512_CTX *); +#define PHP_SHA512_256Init(ctx) PHP_SHA512_256InitArgs(ctx, NULL) +PHP_HASH_API void PHP_SHA512_256InitArgs(PHP_SHA512_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); #define PHP_SHA512_256Update PHP_SHA512Update PHP_HASH_API void PHP_SHA512_256Final(unsigned char[32], PHP_SHA512_CTX *); -PHP_HASH_API void PHP_SHA512_224Init(PHP_SHA512_CTX *); +#define PHP_SHA512_224Init(ctx) PHP_SHA512_224InitArgs(ctx, NULL) +PHP_HASH_API void PHP_SHA512_224InitArgs(PHP_SHA512_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); #define PHP_SHA512_224Update PHP_SHA512Update PHP_HASH_API void PHP_SHA512_224Final(unsigned char[28], PHP_SHA512_CTX *); diff --git a/ext/hash/php_hash_sha3.h b/ext/hash/php_hash_sha3.h index f75191f339fc6..b8bd310ccef66 100644 --- a/ext/hash/php_hash_sha3.h +++ b/ext/hash/php_hash_sha3.h @@ -36,19 +36,19 @@ typedef PHP_SHA3_CTX PHP_SHA3_256_CTX; typedef PHP_SHA3_CTX PHP_SHA3_384_CTX; typedef PHP_SHA3_CTX PHP_SHA3_512_CTX; -PHP_HASH_API void PHP_SHA3224Init(PHP_SHA3_224_CTX*); +PHP_HASH_API void PHP_SHA3224Init(PHP_SHA3_224_CTX*, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_SHA3224Update(PHP_SHA3_224_CTX*, const unsigned char*, size_t); PHP_HASH_API void PHP_SAH3224Final(unsigned char[32], PHP_SHA3_224_CTX*); -PHP_HASH_API void PHP_SHA3256Init(PHP_SHA3_256_CTX*); +PHP_HASH_API void PHP_SHA3256Init(PHP_SHA3_256_CTX*, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_SHA3256Update(PHP_SHA3_256_CTX*, const unsigned char*, size_t); PHP_HASH_API void PHP_SAH3256Final(unsigned char[32], PHP_SHA3_256_CTX*); -PHP_HASH_API void PHP_SHA3384Init(PHP_SHA3_384_CTX*); +PHP_HASH_API void PHP_SHA3384Init(PHP_SHA3_384_CTX*, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_SHA3384Update(PHP_SHA3_384_CTX*, const unsigned char*, size_t); PHP_HASH_API void PHP_SAH3384Final(unsigned char[32], PHP_SHA3_384_CTX*); -PHP_HASH_API void PHP_SHA3512Init(PHP_SHA3_512_CTX*); +PHP_HASH_API void PHP_SHA3512Init(PHP_SHA3_512_CTX*, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_SHA3512Update(PHP_SHA3_512_CTX*, const unsigned char*, size_t); PHP_HASH_API void PHP_SAH3512Final(unsigned char[32], PHP_SHA3_512_CTX*); diff --git a/ext/hash/php_hash_snefru.h b/ext/hash/php_hash_snefru.h index 0f339e93090ab..c533b576a909f 100644 --- a/ext/hash/php_hash_snefru.h +++ b/ext/hash/php_hash_snefru.h @@ -32,7 +32,7 @@ typedef struct { } PHP_SNEFRU_CTX; #define PHP_SNEFRU_SPEC "l16l2bb32" -PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *); +PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_SNEFRUUpdate(PHP_SNEFRU_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_SNEFRUFinal(unsigned char[32], PHP_SNEFRU_CTX *); diff --git a/ext/hash/php_hash_tiger.h b/ext/hash/php_hash_tiger.h index d30276ddeaa29..6854c3588715f 100644 --- a/ext/hash/php_hash_tiger.h +++ b/ext/hash/php_hash_tiger.h @@ -27,8 +27,8 @@ typedef struct { } PHP_TIGER_CTX; #define PHP_TIGER_SPEC "q3qb64l" -PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context); -PHP_HASH_API void PHP_4TIGERInit(PHP_TIGER_CTX *context); +PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args); +PHP_HASH_API void PHP_4TIGERInit(PHP_TIGER_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args); PHP_HASH_API void PHP_TIGERUpdate(PHP_TIGER_CTX *context, const unsigned char *input, size_t len); PHP_HASH_API void PHP_TIGER128Final(unsigned char digest[16], PHP_TIGER_CTX *context); PHP_HASH_API void PHP_TIGER160Final(unsigned char digest[20], PHP_TIGER_CTX *context); diff --git a/ext/hash/php_hash_whirlpool.h b/ext/hash/php_hash_whirlpool.h index fbd5948a4817b..376d75a94009f 100644 --- a/ext/hash/php_hash_whirlpool.h +++ b/ext/hash/php_hash_whirlpool.h @@ -29,7 +29,7 @@ typedef struct { } PHP_WHIRLPOOL_CTX; #define PHP_WHIRLPOOL_SPEC "q8b32iib64." -PHP_HASH_API void PHP_WHIRLPOOLInit(PHP_WHIRLPOOL_CTX *); +PHP_HASH_API void PHP_WHIRLPOOLInit(PHP_WHIRLPOOL_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHP_HASH_API void PHP_WHIRLPOOLUpdate(PHP_WHIRLPOOL_CTX *, const unsigned char *, size_t); PHP_HASH_API void PHP_WHIRLPOOLFinal(unsigned char[64], PHP_WHIRLPOOL_CTX *); diff --git a/ext/hash/tests/hash_serialize_003.phpt b/ext/hash/tests/hash_serialize_003.phpt index a687c2aeffbdf..49bf0b58d6c03 100644 --- a/ext/hash/tests/hash_serialize_003.phpt +++ b/ext/hash/tests/hash_serialize_003.phpt @@ -3,230 +3,6 @@ Hash: serialization formats --FILE-- 42]); +hash_update($ctx, "Two"); +hash_update($ctx, " hashes"); +hash_update($ctx, " meet"); +hash_update($ctx, " in"); +hash_update($ctx, " a"); +hash_update($ctx, " bar."); +$h0 = hash_final($ctx); +echo $h0, "\n"; + +$h0 = hash("murmur3f", "Two hashes meet in a bar.", args: ["seed" => 42]); +echo $h0, "\n"; + +$ctx = hash_init("murmur3c", args: ["seed" => 106]); +hash_update($ctx, "Two"); +hash_update($ctx, " hashes"); +hash_update($ctx, " meet"); +hash_update($ctx, " in"); +hash_update($ctx, " a"); +hash_update($ctx, " bar."); +$h0 = hash_final($ctx); +echo $h0, "\n"; + +$h0 = hash("murmur3c", "Two hashes meet in a bar.", args: ["seed" => 106]); +echo $h0, "\n"; + +$ctx = hash_init("murmur3a", args: ["seed" => 2345]); +hash_update($ctx, "Two"); +hash_update($ctx, " hashes"); +hash_update($ctx, " meet"); +hash_update($ctx, " in"); +hash_update($ctx, " a"); +hash_update($ctx, " bar."); +$h0 = hash_final($ctx); +echo $h0, "\n"; + +$h0 = hash("murmur3a", "Two hashes meet in a bar.", args: ["seed" => 2345]); +echo $h0, "\n"; + +?> +--EXPECT-- +95855f9be0db784a5c37e878c4a4dcee +95855f9be0db784a5c37e878c4a4dcee +f64c9eb40287fa686575163893e283b2 +f64c9eb40287fa686575163893e283b2 +7f7ec59b +7f7ec59b diff --git a/ext/standard/md5.c b/ext/standard/md5.c index ec07ae2ed35ad..83d43c497694e 100644 --- a/ext/standard/md5.c +++ b/ext/standard/md5.c @@ -290,7 +290,7 @@ static const void *body(PHP_MD5_CTX *ctx, const void *data, size_t size) return ptr; } -PHPAPI void PHP_MD5Init(PHP_MD5_CTX *ctx) +PHPAPI void PHP_MD5InitArgs(PHP_MD5_CTX *ctx, ZEND_ATTRIBUTE_UNUSED HashTable *args) { ctx->a = 0x67452301; ctx->b = 0xefcdab89; diff --git a/ext/standard/md5.h b/ext/standard/md5.h index ac60d7fca4cda..09bcff1cf1e72 100644 --- a/ext/standard/md5.h +++ b/ext/standard/md5.h @@ -42,7 +42,8 @@ typedef struct { } PHP_MD5_CTX; #define PHP_MD5_SPEC "llllllb64l16." -PHPAPI void PHP_MD5Init(PHP_MD5_CTX *ctx); +#define PHP_MD5Init(ctx) PHP_MD5InitArgs(ctx, NULL) +PHPAPI void PHP_MD5InitArgs(PHP_MD5_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args); PHPAPI void PHP_MD5Update(PHP_MD5_CTX *ctx, const void *data, size_t size); PHPAPI void PHP_MD5Final(unsigned char *result, PHP_MD5_CTX *ctx); diff --git a/ext/standard/sha1.c b/ext/standard/sha1.c index 58bd91385c2c7..f5668b9283c8a 100644 --- a/ext/standard/sha1.c +++ b/ext/standard/sha1.c @@ -152,7 +152,7 @@ static const unsigned char PADDING[64] = /* {{{ PHP_SHA1Init * SHA1 initialization. Begins an SHA1 operation, writing a new context. */ -PHPAPI void PHP_SHA1Init(PHP_SHA1_CTX * context) +PHPAPI void PHP_SHA1InitArgs(PHP_SHA1_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. diff --git a/ext/standard/sha1.h b/ext/standard/sha1.h index ef98ecc29cc89..3ae3ec219ed2d 100644 --- a/ext/standard/sha1.h +++ b/ext/standard/sha1.h @@ -27,7 +27,8 @@ typedef struct { } PHP_SHA1_CTX; #define PHP_SHA1_SPEC "l5l2b64." -PHPAPI void PHP_SHA1Init(PHP_SHA1_CTX *); +#define PHP_SHA1Init(ctx) PHP_SHA1InitArgs(ctx, NULL) +PHPAPI void PHP_SHA1InitArgs(PHP_SHA1_CTX *, ZEND_ATTRIBUTE_UNUSED HashTable *); PHPAPI void PHP_SHA1Update(PHP_SHA1_CTX *, const unsigned char *, size_t); PHPAPI void PHP_SHA1Final(unsigned char[20], PHP_SHA1_CTX *); PHPAPI void make_sha1_digest(char *sha1str, const unsigned char *digest); From a0b85d643977404ffde0719868868c931bf62fdd Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Fri, 27 Nov 2020 17:21:50 +0100 Subject: [PATCH 02/12] hash: Switch to [] by default for the args parameter Signed-off-by: Anatol Belski --- ext/hash/hash.c | 20 ++++++++++++-------- ext/hash/hash.stub.php | 10 +++++----- ext/hash/hash_arginfo.h | 12 ++++++------ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 4738d9e84ab9b..b642a5180538b 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -425,7 +425,7 @@ PHP_FUNCTION(hash) Z_PARAM_STRING(data, data_len) Z_PARAM_OPTIONAL Z_PARAM_BOOL(raw_output) - Z_PARAM_ARRAY_HT_OR_NULL(args) + Z_PARAM_ARRAY_HT(args) ZEND_PARSE_PARAMETERS_END(); php_hash_do_hash(return_value, algo, data, data_len, raw_output, 0, args); @@ -447,7 +447,7 @@ PHP_FUNCTION(hash_file) Z_PARAM_STRING(data, data_len) Z_PARAM_OPTIONAL Z_PARAM_BOOL(raw_output) - Z_PARAM_ARRAY_HT_OR_NULL(args) + Z_PARAM_ARRAY_HT(args) ZEND_PARSE_PARAMETERS_END(); php_hash_do_hash(return_value, algo, data, data_len, raw_output, 1, args); @@ -611,7 +611,7 @@ PHP_FUNCTION(hash_init) php_hashcontext_object *hash; HashTable *args = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lSh!", &algo, &options, &key, &args) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lSh", &algo, &options, &key, &args) == FAILURE) { RETURN_THROWS(); } @@ -643,7 +643,7 @@ PHP_FUNCTION(hash_init) hash->context = context; hash->options = options; hash->key = NULL; - hash->args = args ? zend_array_dup(args) : NULL; + hash->args = args && zend_hash_num_elements(args) > 0 ? zend_array_dup(args) : NULL; if (options & PHP_HASH_HMAC) { char *K = emalloc(ops->block_size); @@ -1205,7 +1205,7 @@ PHP_FUNCTION(mhash) size_t data_len, key_len = 0; HashTable *args; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!h!", &algorithm, &data, &data_len, &key, &key_len, &args) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!h", &algorithm, &data, &data_len, &key, &key_len, &args) == FAILURE) { RETURN_THROWS(); } @@ -1292,7 +1292,7 @@ PHP_FUNCTION(mhash_keygen_s2k) char padded_salt[SALT_SIZE]; HashTable *args; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl|h!", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes, &args) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl|h", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes, &args) == FAILURE) { RETURN_THROWS(); } @@ -1389,6 +1389,9 @@ static void php_hashcontext_dtor(zend_object *obj) { } if (hash->args) { + if (GC_REFCOUNT(hash->args) > 1) { + GC_DELREF(hash->args); + } zend_array_destroy(hash->args); hash->args = NULL; } @@ -1553,9 +1556,10 @@ PHP_METHOD(HashContext, __unserialize) hash->ops = ops; hash->context = php_hash_alloc_context(ops); hash->options = options; - if (args_zv && IS_ARRAY == Z_TYPE_P(args_zv)) { + if (args_zv && IS_ARRAY == Z_TYPE_P(args_zv) && zend_hash_num_elements(Z_ARRVAL_P(args_zv)) > 0) { ops->hash_init(hash->context, Z_ARRVAL_P(args_zv)); - hash->args = zend_array_dup(Z_ARRVAL_P(args_zv)); + hash->args = Z_ARRVAL_P(args_zv); + GC_ADDREF(hash->args); } else { ops->hash_init(hash->context, NULL); hash->args = NULL; diff --git a/ext/hash/hash.stub.php b/ext/hash/hash.stub.php index 85d21f01ae9e1..b816cf2b79259 100644 --- a/ext/hash/hash.stub.php +++ b/ext/hash/hash.stub.php @@ -2,15 +2,15 @@ /** @generate-function-entries */ -function hash(string $algo, string $data, bool $binary = false, array $args = null): string|false {} +function hash(string $algo, string $data, bool $binary = false, array $args = []): string|false {} -function hash_file(string $algo, string $filename, bool $binary = false, array $args = null): string|false {} +function hash_file(string $algo, string $filename, bool $binary = false, array $args = []): string|false {} function hash_hmac(string $algo, string $data, string $key, bool $binary = false): string|false {} function hash_hmac_file(string $algo, string $data, string $key, bool $binary = false): string|false {} -function hash_init(string $algo, int $flags = 0, string $key = "", array $args = null): HashContext {} +function hash_init(string $algo, int $flags = 0, string $key = "", array $args = []): HashContext {} function hash_update(HashContext $context, string $data): bool {} @@ -39,11 +39,11 @@ function mhash_get_block_size(int $algo): int|false {} function mhash_get_hash_name(int $algo): string|false {} -function mhash_keygen_s2k(int $algo, string $password, string $salt, int $length, array $args = null): string|false {} +function mhash_keygen_s2k(int $algo, string $password, string $salt, int $length, array $args = []): string|false {} function mhash_count(): int {} -function mhash(int $algo, string $data, ?string $key = null, array $args = null): string|false {} +function mhash(int $algo, string $data, ?string $key = null, array $args = []): string|false {} #endif final class HashContext diff --git a/ext/hash/hash_arginfo.h b/ext/hash/hash_arginfo.h index 8cd6b9c7479a7..09e596d7c7d3b 100644 --- a/ext/hash/hash_arginfo.h +++ b/ext/hash/hash_arginfo.h @@ -1,18 +1,18 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: d93291f14a495dc68a307af8c246469e79ba3979 */ + * Stub hash: 260f19c3cc3cf2c9899d8f90d02388ea1351e7ab */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash, 0, 2, MAY_BE_STRING|MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, binary, _IS_BOOL, 0, "false") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash_file, 0, 2, MAY_BE_STRING|MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, binary, _IS_BOOL, 0, "false") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash_hmac, 0, 3, MAY_BE_STRING|MAY_BE_FALSE) @@ -28,7 +28,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_hash_init, 0, 1, HashContext, 0) ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "0") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, key, IS_STRING, 0, "\"\"") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_hash_update, 0, 2, _IS_BOOL, 0) @@ -102,7 +102,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mhash_keygen_s2k, 0, 4, MAY_BE_S ZEND_ARG_TYPE_INFO(0, password, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, salt, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, length, IS_LONG, 0) - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() #endif @@ -116,7 +116,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mhash, 0, 2, MAY_BE_STRING|MAY_B ZEND_ARG_TYPE_INFO(0, algo, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, key, IS_STRING, 1, "null") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() #endif From e220fd9ac10abf4fc585cbb1cdf02739cb3cd0ce Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Fri, 27 Nov 2020 17:25:45 +0100 Subject: [PATCH 03/12] hash: The args array is always mutable Signed-off-by: Anatol Belski --- ext/hash/hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/hash/hash.c b/ext/hash/hash.c index b642a5180538b..c02969188ac89 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -1487,7 +1487,7 @@ PHP_METHOD(HashContext, __serialize) if (hash->args) { ZVAL_ARR(&tmp, hash->args); - Z_TRY_ADDREF(tmp); + Z_ADDREF(tmp); } else { ZVAL_NULL(&tmp); } From f1dc8e19d9bad63e0d3badb676a33e80600de700 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Mon, 30 Nov 2020 21:00:54 +0100 Subject: [PATCH 04/12] hash: Address PR comments - Deref passed seed arg - Don't dup args array Signed-off-by: Anatol Belski --- ext/hash/hash.c | 16 +++++++++------- ext/hash/hash_murmur.c | 12 ++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/ext/hash/hash.c b/ext/hash/hash.c index c02969188ac89..94f5d4fdafa23 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -643,7 +643,12 @@ PHP_FUNCTION(hash_init) hash->context = context; hash->options = options; hash->key = NULL; - hash->args = args && zend_hash_num_elements(args) > 0 ? zend_array_dup(args) : NULL; + if (args && zend_hash_num_elements(args)) { + hash->args = args; + GC_TRY_ADDREF(hash->args); + } else { + hash->args = NULL; + } if (options & PHP_HASH_HMAC) { char *K = emalloc(ops->block_size); @@ -1389,10 +1394,7 @@ static void php_hashcontext_dtor(zend_object *obj) { } if (hash->args) { - if (GC_REFCOUNT(hash->args) > 1) { - GC_DELREF(hash->args); - } - zend_array_destroy(hash->args); + zend_array_release(hash->args); hash->args = NULL; } } @@ -1487,7 +1489,7 @@ PHP_METHOD(HashContext, __serialize) if (hash->args) { ZVAL_ARR(&tmp, hash->args); - Z_ADDREF(tmp); + Z_TRY_ADDREF(tmp); } else { ZVAL_NULL(&tmp); } @@ -1559,7 +1561,7 @@ PHP_METHOD(HashContext, __unserialize) if (args_zv && IS_ARRAY == Z_TYPE_P(args_zv) && zend_hash_num_elements(Z_ARRVAL_P(args_zv)) > 0) { ops->hash_init(hash->context, Z_ARRVAL_P(args_zv)); hash->args = Z_ARRVAL_P(args_zv); - GC_ADDREF(hash->args); + GC_TRY_ADDREF(hash->args); } else { ops->hash_init(hash->context, NULL); hash->args = NULL; diff --git a/ext/hash/hash_murmur.c b/ext/hash/hash_murmur.c index 44b5ad3cbd72c..466231054c669 100644 --- a/ext/hash/hash_murmur.c +++ b/ext/hash/hash_murmur.c @@ -39,10 +39,10 @@ const php_hash_ops php_hash_murmur3a_ops = { PHP_HASH_API void PHP_MURMUR3AInit(PHP_MURMUR3A_CTX *ctx, HashTable *args) { if (args) { - zval *seed; + zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1); /* This might be a bit too restrictive, but thinking that a seed might be set once and for all, it should be done a clean way. */ - if ((NULL != (seed = zend_hash_str_find(args, "seed", sizeof("seed") - 1))) && IS_LONG == Z_TYPE_P(seed)) { + if (seed && IS_LONG == Z_TYPE_P(seed)) { ctx->h = (uint32_t)Z_LVAL_P(seed); } else { ctx->h = 0; @@ -96,10 +96,10 @@ const php_hash_ops php_hash_murmur3c_ops = { PHP_HASH_API void PHP_MURMUR3CInit(PHP_MURMUR3C_CTX *ctx, HashTable *args) { if (args) { - zval *seed; + zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1); /* This might be a bit too restrictive, but thinking that a seed might be set once and for all, it should be done a clean way. */ - if ((NULL != (seed = zend_hash_str_find(args, "seed", sizeof("seed") - 1))) && IS_LONG == Z_TYPE_P(seed)) { + if (seed && IS_LONG == Z_TYPE_P(seed)) { uint32_t _seed = (uint32_t)Z_LVAL_P(seed); ctx->h[0] = _seed; ctx->h[1] = _seed; @@ -170,10 +170,10 @@ const php_hash_ops php_hash_murmur3f_ops = { PHP_HASH_API void PHP_MURMUR3FInit(PHP_MURMUR3F_CTX *ctx, HashTable *args) { if (args) { - zval *seed; + zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1); /* This might be a bit too restrictive, but thinking that a seed might be set once and for all, it should be done a clean way. */ - if ((NULL != (seed = zend_hash_str_find(args, "seed", sizeof("seed") - 1))) && IS_LONG == Z_TYPE_P(seed)) { + if (seed && IS_LONG == Z_TYPE_P(seed)) { uint64_t _seed = (uint64_t)Z_LVAL_P(seed); ctx->h[0] = _seed; ctx->h[1] = _seed; From 48868168f3faea33bf7f7ac38d852b33cff8edf9 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Tue, 1 Dec 2020 19:43:28 +0100 Subject: [PATCH 05/12] hash: Revert change to ext/hash/tests/hash_serialize_003.phpt Signed-off-by: Anatol Belski --- ext/hash/tests/hash_serialize_003.phpt | 230 ++++++++++++++++++++++++- 1 file changed, 229 insertions(+), 1 deletion(-) diff --git a/ext/hash/tests/hash_serialize_003.phpt b/ext/hash/tests/hash_serialize_003.phpt index 49bf0b58d6c03..a687c2aeffbdf 100644 --- a/ext/hash/tests/hash_serialize_003.phpt +++ b/ext/hash/tests/hash_serialize_003.phpt @@ -3,6 +3,230 @@ Hash: serialization formats --FILE-- Date: Tue, 1 Dec 2020 20:13:31 +0100 Subject: [PATCH 06/12] hash: Don't carry passed initialization args through Signed-off-by: Anatol Belski --- ext/hash/hash.c | 32 +++----------------------------- ext/hash/php_hash.h | 2 -- 2 files changed, 3 insertions(+), 31 deletions(-) diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 94f5d4fdafa23..f5180a0bab0cd 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -643,12 +643,6 @@ PHP_FUNCTION(hash_init) hash->context = context; hash->options = options; hash->key = NULL; - if (args && zend_hash_num_elements(args)) { - hash->args = args; - GC_TRY_ADDREF(hash->args); - } else { - hash->args = NULL; - } if (options & PHP_HASH_HMAC) { char *K = emalloc(ops->block_size); @@ -803,7 +797,7 @@ PHP_FUNCTION(hash_final) } /* Feed this result into the outer hash */ - hash->ops->hash_init(hash->context, hash->args); + hash->ops->hash_init(hash->context, NULL); hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size); hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(digest), hash->ops->digest_size); hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context); @@ -1392,11 +1386,6 @@ static void php_hashcontext_dtor(zend_object *obj) { efree(hash->key); hash->key = NULL; } - - if (hash->args) { - zend_array_release(hash->args); - hash->args = NULL; - } } /* }}} */ @@ -1411,7 +1400,7 @@ static zend_object *php_hashcontext_clone(zend_object *zobj) { newobj->ops = oldobj->ops; newobj->options = oldobj->options; newobj->context = php_hash_alloc_context(newobj->ops); - newobj->ops->hash_init(newobj->context, oldobj->args); + newobj->ops->hash_init(newobj->context, NULL); if (SUCCESS != newobj->ops->hash_copy(newobj->ops, oldobj->context, newobj->context)) { efree(newobj->context); @@ -1487,14 +1476,6 @@ PHP_METHOD(HashContext, __serialize) Z_TRY_ADDREF(tmp); zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); - if (hash->args) { - ZVAL_ARR(&tmp, hash->args); - Z_TRY_ADDREF(tmp); - } else { - ZVAL_NULL(&tmp); - } - zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp); - return; serialize_failure: @@ -1558,14 +1539,7 @@ PHP_METHOD(HashContext, __unserialize) hash->ops = ops; hash->context = php_hash_alloc_context(ops); hash->options = options; - if (args_zv && IS_ARRAY == Z_TYPE_P(args_zv) && zend_hash_num_elements(Z_ARRVAL_P(args_zv)) > 0) { - ops->hash_init(hash->context, Z_ARRVAL_P(args_zv)); - hash->args = Z_ARRVAL_P(args_zv); - GC_TRY_ADDREF(hash->args); - } else { - ops->hash_init(hash->context, NULL); - hash->args = NULL; - } + ops->hash_init(hash->context, NULL); unserialize_result = ops->hash_unserialize(hash, magic, hash_zv); if (unserialize_result != SUCCESS) { diff --git a/ext/hash/php_hash.h b/ext/hash/php_hash.h index 88eaff713fc8a..7f57fe567d0f9 100644 --- a/ext/hash/php_hash.h +++ b/ext/hash/php_hash.h @@ -61,8 +61,6 @@ struct _php_hashcontext_object { zend_long options; unsigned char *key; - HashTable *args; - zend_object std; }; From 1271bffb4d03bbbedf68a5e303dcfa7b91884c37 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Tue, 1 Dec 2020 20:35:10 +0100 Subject: [PATCH 07/12] hash: Remove missed remains args member Signed-off-by: Anatol Belski --- ext/hash/hash.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ext/hash/hash.c b/ext/hash/hash.c index f5180a0bab0cd..04c4a55ad7d9c 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -1490,7 +1490,7 @@ PHP_METHOD(HashContext, __unserialize) zval *object = ZEND_THIS; php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object)); HashTable *data; - zval *algo_zv, *magic_zv, *options_zv, *hash_zv, *members_zv, *args_zv; + zval *algo_zv, *magic_zv, *options_zv, *hash_zv, *members_zv; zend_long magic, options; int unserialize_result; const php_hash_ops *ops; @@ -1509,7 +1509,6 @@ PHP_METHOD(HashContext, __unserialize) hash_zv = zend_hash_index_find(data, 2); magic_zv = zend_hash_index_find(data, 3); members_zv = zend_hash_index_find(data, 4); - args_zv = zend_hash_index_find(data, 5); if (!algo_zv || Z_TYPE_P(algo_zv) != IS_STRING || !magic_zv || Z_TYPE_P(magic_zv) != IS_LONG From 9c8d6efc5a31be438377516d44a6ba126f6b0519 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Sat, 5 Dec 2020 20:28:14 +0100 Subject: [PATCH 08/12] hash: Revert args addition to mhash variants As discussed, that's legacy and should not be touched. Signed-off-by: Anatol Belski --- ext/hash/hash.c | 12 +++++------- ext/hash/hash.stub.php | 4 ++-- ext/hash/hash_arginfo.h | 4 +--- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 04c4a55ad7d9c..1a5a0a5afb23a 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -1202,9 +1202,8 @@ PHP_FUNCTION(mhash) zend_string *algo = NULL; char *data, *key = NULL; size_t data_len, key_len = 0; - HashTable *args; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!h", &algorithm, &data, &data_len, &key, &key_len, &args) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s", &algorithm, &data, &data_len, &key, &key_len) == FAILURE) { RETURN_THROWS(); } @@ -1219,7 +1218,7 @@ PHP_FUNCTION(mhash) if (key) { php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, 1, 0); } else { - php_hash_do_hash(return_value, algo, data, data_len, 1, 0, args); + php_hash_do_hash(return_value, algo, data, data_len, 1, 0, NULL); } if (algo) { @@ -1289,9 +1288,8 @@ PHP_FUNCTION(mhash_keygen_s2k) char *password, *salt; size_t password_len, salt_len; char padded_salt[SALT_SIZE]; - HashTable *args; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl|h", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes, &args) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) { RETURN_THROWS(); } @@ -1327,13 +1325,13 @@ PHP_FUNCTION(mhash_keygen_s2k) } context = php_hash_alloc_context(ops); - ops->hash_init(context, args); + ops->hash_init(context, NULL); key = ecalloc(1, times * block_size); digest = emalloc(ops->digest_size + 1); for (i = 0; i < times; i++) { - ops->hash_init(context, args); + ops->hash_init(context, NULL); for (j=0;jhash_update(context, &null, 1); diff --git a/ext/hash/hash.stub.php b/ext/hash/hash.stub.php index b816cf2b79259..29eb9d44949a5 100644 --- a/ext/hash/hash.stub.php +++ b/ext/hash/hash.stub.php @@ -39,11 +39,11 @@ function mhash_get_block_size(int $algo): int|false {} function mhash_get_hash_name(int $algo): string|false {} -function mhash_keygen_s2k(int $algo, string $password, string $salt, int $length, array $args = []): string|false {} +function mhash_keygen_s2k(int $algo, string $password, string $salt, int $length): string|false {} function mhash_count(): int {} -function mhash(int $algo, string $data, ?string $key = null, array $args = []): string|false {} +function mhash(int $algo, string $data, ?string $key = null): string|false {} #endif final class HashContext diff --git a/ext/hash/hash_arginfo.h b/ext/hash/hash_arginfo.h index 09e596d7c7d3b..d5e7dc107a7db 100644 --- a/ext/hash/hash_arginfo.h +++ b/ext/hash/hash_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 260f19c3cc3cf2c9899d8f90d02388ea1351e7ab */ + * Stub hash: 26da55a281b13eb8aca4c104c94c80e83f69aaf8 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash, 0, 2, MAY_BE_STRING|MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0) @@ -102,7 +102,6 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mhash_keygen_s2k, 0, 4, MAY_BE_S ZEND_ARG_TYPE_INFO(0, password, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, salt, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, length, IS_LONG, 0) - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() #endif @@ -116,7 +115,6 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mhash, 0, 2, MAY_BE_STRING|MAY_B ZEND_ARG_TYPE_INFO(0, algo, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, key, IS_STRING, 1, "null") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() #endif From 1655a7b745769d18804ab6c8f0beea66ce5ce9e5 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Sat, 5 Dec 2020 20:31:17 +0100 Subject: [PATCH 09/12] hash: Rename $args -> $options Signed-off-by: Anatol Belski --- ext/hash/hash.stub.php | 6 +++--- ext/hash/hash_arginfo.h | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ext/hash/hash.stub.php b/ext/hash/hash.stub.php index 29eb9d44949a5..7eb3925c353b4 100644 --- a/ext/hash/hash.stub.php +++ b/ext/hash/hash.stub.php @@ -2,15 +2,15 @@ /** @generate-function-entries */ -function hash(string $algo, string $data, bool $binary = false, array $args = []): string|false {} +function hash(string $algo, string $data, bool $binary = false, array $options = []): string|false {} -function hash_file(string $algo, string $filename, bool $binary = false, array $args = []): string|false {} +function hash_file(string $algo, string $filename, bool $binary = false, array $options = []): string|false {} function hash_hmac(string $algo, string $data, string $key, bool $binary = false): string|false {} function hash_hmac_file(string $algo, string $data, string $key, bool $binary = false): string|false {} -function hash_init(string $algo, int $flags = 0, string $key = "", array $args = []): HashContext {} +function hash_init(string $algo, int $flags = 0, string $key = "", array $options = []): HashContext {} function hash_update(HashContext $context, string $data): bool {} diff --git a/ext/hash/hash_arginfo.h b/ext/hash/hash_arginfo.h index d5e7dc107a7db..1d654b546ee1f 100644 --- a/ext/hash/hash_arginfo.h +++ b/ext/hash/hash_arginfo.h @@ -1,18 +1,18 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 26da55a281b13eb8aca4c104c94c80e83f69aaf8 */ + * Stub hash: e8466049fca2eae179adbc19bb67e71f6486ec4e */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash, 0, 2, MAY_BE_STRING|MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, binary, _IS_BOOL, 0, "false") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "[]") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash_file, 0, 2, MAY_BE_STRING|MAY_BE_FALSE) ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, binary, _IS_BOOL, 0, "false") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "[]") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_hash_hmac, 0, 3, MAY_BE_STRING|MAY_BE_FALSE) @@ -28,7 +28,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_hash_init, 0, 1, HashContext, 0) ZEND_ARG_TYPE_INFO(0, algo, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "0") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, key, IS_STRING, 0, "\"\"") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, args, IS_ARRAY, 0, "[]") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_hash_update, 0, 2, _IS_BOOL, 0) From 24410e074d1af6a98040bab3d5f119cc4248e669 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Sat, 5 Dec 2020 20:57:33 +0100 Subject: [PATCH 10/12] hash: Fix options test Signed-off-by: Anatol Belski --- ext/hash/tests/murmurhash3_seed.phpt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/hash/tests/murmurhash3_seed.phpt b/ext/hash/tests/murmurhash3_seed.phpt index 26aa0dd0ec534..68cc10a71935a 100644 --- a/ext/hash/tests/murmurhash3_seed.phpt +++ b/ext/hash/tests/murmurhash3_seed.phpt @@ -3,7 +3,7 @@ Hash: MurmurHash3 seed --FILE-- 42]); +$ctx = hash_init("murmur3f", options: ["seed" => 42]); hash_update($ctx, "Two"); hash_update($ctx, " hashes"); hash_update($ctx, " meet"); @@ -13,10 +13,10 @@ hash_update($ctx, " bar."); $h0 = hash_final($ctx); echo $h0, "\n"; -$h0 = hash("murmur3f", "Two hashes meet in a bar.", args: ["seed" => 42]); +$h0 = hash("murmur3f", "Two hashes meet in a bar.", options: ["seed" => 42]); echo $h0, "\n"; -$ctx = hash_init("murmur3c", args: ["seed" => 106]); +$ctx = hash_init("murmur3c", options: ["seed" => 106]); hash_update($ctx, "Two"); hash_update($ctx, " hashes"); hash_update($ctx, " meet"); @@ -26,10 +26,10 @@ hash_update($ctx, " bar."); $h0 = hash_final($ctx); echo $h0, "\n"; -$h0 = hash("murmur3c", "Two hashes meet in a bar.", args: ["seed" => 106]); +$h0 = hash("murmur3c", "Two hashes meet in a bar.", options: ["seed" => 106]); echo $h0, "\n"; -$ctx = hash_init("murmur3a", args: ["seed" => 2345]); +$ctx = hash_init("murmur3a", options: ["seed" => 2345]); hash_update($ctx, "Two"); hash_update($ctx, " hashes"); hash_update($ctx, " meet"); @@ -39,7 +39,7 @@ hash_update($ctx, " bar."); $h0 = hash_final($ctx); echo $h0, "\n"; -$h0 = hash("murmur3a", "Two hashes meet in a bar.", args: ["seed" => 2345]); +$h0 = hash("murmur3a", "Two hashes meet in a bar.", options: ["seed" => 2345]); echo $h0, "\n"; ?> From bb658d1db02e9c9546370df121c31ee0bf46cfa1 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Wed, 9 Dec 2020 13:28:34 +0100 Subject: [PATCH 11/12] Update ext/hash/hash.c Co-authored-by: Nikita Popov --- ext/hash/hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 1a5a0a5afb23a..61e7da80b1a79 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -987,7 +987,7 @@ PHP_FUNCTION(hash_pbkdf2) void *context; HashTable *args; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lbh!", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output, &args) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lbh", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output, &args) == FAILURE) { RETURN_THROWS(); } From 530c7d3b266bfb14e82b93d18fb3e27de8050d21 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Wed, 9 Dec 2020 13:29:28 +0100 Subject: [PATCH 12/12] Update ext/hash/hash.c Co-authored-by: Nikita Popov --- ext/hash/hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 61e7da80b1a79..a337ef0489342 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -1203,7 +1203,7 @@ PHP_FUNCTION(mhash) char *data, *key = NULL; size_t data_len, key_len = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s", &algorithm, &data, &data_len, &key, &key_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!", &algorithm, &data, &data_len, &key, &key_len) == FAILURE) { RETURN_THROWS(); }