summaryrefslogtreecommitdiff
path: root/ext/hash/hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/hash/hash.c')
-rw-r--r--ext/hash/hash.c250
1 files changed, 56 insertions, 194 deletions
diff --git a/ext/hash/hash.c b/ext/hash/hash.c
index 856ae0e11b..6b869affde 100644
--- a/ext/hash/hash.c
+++ b/ext/hash/hash.c
@@ -1,7 +1,5 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 7 |
- +----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
@@ -29,6 +27,8 @@
#include "zend_interfaces.h"
#include "zend_exceptions.h"
+#include "hash_arginfo.h"
+
HashTable php_hash_hashtable;
zend_class_entry *php_hashcontext_ce;
static zend_object_handlers php_hashcontext_handlers;
@@ -252,18 +252,18 @@ static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename,
ops = php_hash_fetch_ops(algo, algo_len);
if (!ops) {
- php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
- RETURN_FALSE;
+ zend_throw_error(NULL, "Unknown hashing algorithm: %s", algo);
+ return;
}
else if (!ops->is_crypto) {
- php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo);
- RETURN_FALSE;
+ zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", algo);
+ return;
}
if (isfilename) {
if (CHECK_NULL_PATH(data, data_len)) {
- php_error_docref(NULL, E_WARNING, "Invalid path");
- RETURN_FALSE;
+ zend_throw_error(NULL, "Invalid path");
+ return;
}
stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
if (!stream) {
@@ -358,19 +358,19 @@ PHP_FUNCTION(hash_init)
ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
if (!ops) {
- php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
- RETURN_FALSE;
+ zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
+ return;
}
if (options & PHP_HASH_HMAC) {
if (!ops->is_crypto) {
- php_error_docref(NULL, E_WARNING, "HMAC requested with a non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
- RETURN_FALSE;
+ zend_throw_error(NULL, "HMAC requested with a non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
+ return;
}
if (!key || (ZSTR_LEN(key) == 0)) {
/* Note: a zero length key is no key at all */
- php_error_docref(NULL, E_WARNING, "HMAC requested without a key");
- RETURN_FALSE;
+ zend_throw_error(NULL, "HMAC requested without a key");
+ return;
}
}
@@ -414,8 +414,8 @@ PHP_FUNCTION(hash_init)
#define PHP_HASHCONTEXT_VERIFY(func, hash) { \
if (!hash->context) { \
- php_error(E_WARNING, "%s(): supplied resource is not a valid Hash Context resource", func); \
- RETURN_NULL(); \
+ zend_throw_error(NULL, "%s(): supplied resource is not a valid Hash Context resource", func); \
+ return; \
} \
}
@@ -581,11 +581,13 @@ PHP_FUNCTION(hash_copy)
return;
}
- RETVAL_OBJ(Z_OBJ_HANDLER_P(zhash, clone_obj)(zhash));
+ RETVAL_OBJ(Z_OBJ_HANDLER_P(zhash, clone_obj)(Z_OBJ_P(zhash)));
if (php_hashcontext_from_object(Z_OBJ_P(return_value))->context == NULL) {
zval_ptr_dtor(return_value);
- RETURN_FALSE;
+
+ zend_throw_error(NULL, "Cannot copy hash");
+ return;
}
}
/* }}} */
@@ -596,6 +598,10 @@ PHP_FUNCTION(hash_algos)
{
zend_string *str;
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+
array_init(return_value);
ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
add_next_index_str(return_value, zend_string_copy(str));
@@ -610,6 +616,10 @@ PHP_FUNCTION(hash_hmac_algos)
zend_string *str;
const php_hash_ops *ops;
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+
array_init(return_value);
ZEND_HASH_FOREACH_STR_KEY_PTR(&php_hash_hashtable, str, ops) {
if (ops->is_crypto) {
@@ -637,28 +647,28 @@ PHP_FUNCTION(hash_hkdf)
ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
if (!ops) {
- php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
- RETURN_FALSE;
+ zend_throw_error(NULL, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
+ return;
}
if (!ops->is_crypto) {
- php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
- RETURN_FALSE;
+ zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
+ return;
}
if (ZSTR_LEN(ikm) == 0) {
- php_error_docref(NULL, E_WARNING, "Input keying material cannot be empty");
- RETURN_FALSE;
+ zend_throw_error(NULL, "Input keying material cannot be empty");
+ return;
}
if (length < 0) {
- php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
- RETURN_FALSE;
+ zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
+ return;
} else if (length == 0) {
length = ops->digest_size;
} else if (length > (zend_long) (ops->digest_size * 255)) {
- php_error_docref(NULL, E_WARNING, "Length must be less than or equal to %zd: " ZEND_LONG_FMT, ops->digest_size * 255, length);
- RETURN_FALSE;
+ zend_throw_error(NULL, "Length must be less than or equal to %zd: " ZEND_LONG_FMT, ops->digest_size * 255, length);
+ return;
}
context = emalloc(ops->context_size);
@@ -737,27 +747,27 @@ PHP_FUNCTION(hash_pbkdf2)
ops = php_hash_fetch_ops(algo, algo_len);
if (!ops) {
- php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
- RETURN_FALSE;
+ zend_throw_error(NULL, "Unknown hashing algorithm: %s", algo);
+ return;
}
else if (!ops->is_crypto) {
- php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo);
- RETURN_FALSE;
+ zend_throw_error(NULL, "Non-cryptographic hashing algorithm: %s", algo);
+ return;
}
if (iterations <= 0) {
- php_error_docref(NULL, E_WARNING, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
- RETURN_FALSE;
+ zend_throw_error(NULL, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
+ return;
}
if (length < 0) {
- php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
- RETURN_FALSE;
+ zend_throw_error(NULL, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
+ return;
}
if (salt_len > INT_MAX - 4) {
- php_error_docref(NULL, E_WARNING, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
- RETURN_FALSE;
+ zend_throw_error(NULL, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
+ return;
}
context = emalloc(ops->context_size);
@@ -862,13 +872,13 @@ PHP_FUNCTION(hash_equals)
/* We only allow comparing string to prevent unexpected results. */
if (Z_TYPE_P(known_zval) != IS_STRING) {
- php_error_docref(NULL, E_WARNING, "Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
- RETURN_FALSE;
+ zend_type_error("Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
+ return;
}
if (Z_TYPE_P(user_zval) != IS_STRING) {
- php_error_docref(NULL, E_WARNING, "Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
- RETURN_FALSE;
+ zend_type_error("Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
+ return;
}
if (Z_STRLEN_P(known_zval) != Z_STRLEN_P(user_zval)) {
@@ -1149,12 +1159,12 @@ static void php_hashcontext_dtor(zend_object *obj) {
/* }}} */
/* {{{ php_hashcontext_clone */
-static zend_object *php_hashcontext_clone(zval *pzv) {
- php_hashcontext_object *oldobj = php_hashcontext_from_object(Z_OBJ_P(pzv));
- zend_object *znew = php_hashcontext_create(Z_OBJCE_P(pzv));
+static zend_object *php_hashcontext_clone(zend_object *zobj) {
+ php_hashcontext_object *oldobj = php_hashcontext_from_object(zobj);
+ zend_object *znew = php_hashcontext_create(zobj->ce);
php_hashcontext_object *newobj = php_hashcontext_from_object(znew);
- zend_objects_clone_members(znew, Z_OBJ_P(pzv));
+ zend_objects_clone_members(znew, zobj);
newobj->ops = oldobj->ops;
newobj->options = oldobj->options;
@@ -1302,143 +1312,6 @@ PHP_MINFO_FUNCTION(hash)
}
/* }}} */
-/* {{{ arginfo */
-#ifdef PHP_HASH_MD5_NOT_IN_CORE
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5, 0, 0, 1)
- ZEND_ARG_INFO(0, str)
- ZEND_ARG_INFO(0, raw_output)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5_file, 0, 0, 1)
- ZEND_ARG_INFO(0, filename)
- ZEND_ARG_INFO(0, raw_output)
-ZEND_END_ARG_INFO()
-#endif
-
-#ifdef PHP_HASH_SHA1_NOT_IN_CORE
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1, 0, 0, 1)
- ZEND_ARG_INFO(0, str)
- ZEND_ARG_INFO(0, raw_output)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1_file, 0, 0, 1)
- ZEND_ARG_INFO(0, filename)
- ZEND_ARG_INFO(0, raw_output)
-ZEND_END_ARG_INFO()
-#endif
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash, 0, 0, 2)
- ZEND_ARG_INFO(0, algo)
- ZEND_ARG_INFO(0, data)
- ZEND_ARG_INFO(0, raw_output)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_file, 0, 0, 2)
- ZEND_ARG_INFO(0, algo)
- ZEND_ARG_INFO(0, filename)
- ZEND_ARG_INFO(0, raw_output)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac, 0, 0, 3)
- ZEND_ARG_INFO(0, algo)
- ZEND_ARG_INFO(0, data)
- ZEND_ARG_INFO(0, key)
- ZEND_ARG_INFO(0, raw_output)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac_file, 0, 0, 3)
- ZEND_ARG_INFO(0, algo)
- ZEND_ARG_INFO(0, filename)
- ZEND_ARG_INFO(0, key)
- ZEND_ARG_INFO(0, raw_output)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_init, 0, 0, 1)
- ZEND_ARG_INFO(0, algo)
- ZEND_ARG_INFO(0, options)
- ZEND_ARG_INFO(0, key)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO(arginfo_hash_update, 0)
- ZEND_ARG_INFO(0, context)
- ZEND_ARG_INFO(0, data)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_stream, 0, 0, 2)
- ZEND_ARG_INFO(0, context)
- ZEND_ARG_INFO(0, handle)
- ZEND_ARG_INFO(0, length)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_file, 0, 0, 2)
- ZEND_ARG_INFO(0, context)
- ZEND_ARG_INFO(0, filename)
- ZEND_ARG_INFO(0, stream_context)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_final, 0, 0, 1)
- ZEND_ARG_INFO(0, context)
- ZEND_ARG_INFO(0, raw_output)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO(arginfo_hash_copy, 0)
- ZEND_ARG_INFO(0, context)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO(arginfo_hash_algos, 0)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_pbkdf2, 0, 0, 4)
- ZEND_ARG_INFO(0, algo)
- ZEND_ARG_INFO(0, password)
- ZEND_ARG_INFO(0, salt)
- ZEND_ARG_INFO(0, iterations)
- ZEND_ARG_INFO(0, length)
- ZEND_ARG_INFO(0, raw_output)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO(arginfo_hash_equals, 0)
- ZEND_ARG_INFO(0, known_string)
- ZEND_ARG_INFO(0, user_string)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hkdf, 0, 0, 2)
- ZEND_ARG_INFO(0, ikm)
- ZEND_ARG_INFO(0, algo)
- ZEND_ARG_INFO(0, length)
- ZEND_ARG_INFO(0, string)
- ZEND_ARG_INFO(0, salt)
-ZEND_END_ARG_INFO()
-
-/* BC Land */
-#ifdef PHP_MHASH_BC
-ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_block_size, 0)
- ZEND_ARG_INFO(0, hash)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_hash_name, 0)
- ZEND_ARG_INFO(0, hash)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO(arginfo_mhash_keygen_s2k, 0)
- ZEND_ARG_INFO(0, hash)
- ZEND_ARG_INFO(0, input_password)
- ZEND_ARG_INFO(0, salt)
- ZEND_ARG_INFO(0, bytes)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO(arginfo_mhash_count, 0)
-ZEND_END_ARG_INFO()
-
-ZEND_BEGIN_ARG_INFO_EX(arginfo_mhash, 0, 0, 2)
- ZEND_ARG_INFO(0, hash)
- ZEND_ARG_INFO(0, data)
- ZEND_ARG_INFO(0, key)
-ZEND_END_ARG_INFO()
-#endif
-
-/* }}} */
-
/* {{{ hash_functions[]
*/
static const zend_function_entry hash_functions[] = {
@@ -1461,17 +1334,6 @@ static const zend_function_entry hash_functions[] = {
PHP_FE(hash_equals, arginfo_hash_equals)
PHP_FE(hash_hkdf, arginfo_hash_hkdf)
- /* BC Land */
-#ifdef PHP_HASH_MD5_NOT_IN_CORE
- PHP_NAMED_FE(md5, php_if_md5, arginfo_hash_md5)
- PHP_NAMED_FE(md5_file, php_if_md5_file, arginfo_hash_md5_file)
-#endif /* PHP_HASH_MD5_NOT_IN_CORE */
-
-#ifdef PHP_HASH_SHA1_NOT_IN_CORE
- PHP_NAMED_FE(sha1, php_if_sha1, arginfo_hash_sha1)
- PHP_NAMED_FE(sha1_file, php_if_sha1_file, arginfo_hash_sha1_file)
-#endif /* PHP_HASH_SHA1_NOT_IN_CORE */
-
#ifdef PHP_MHASH_BC
PHP_FE(mhash_keygen_s2k, arginfo_mhash_keygen_s2k)
PHP_FE(mhash_get_block_size, arginfo_mhash_get_block_size)