@@ -137,7 +137,7 @@ static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_
137137 }
138138 if (isfilename ) {
139139 if (CHECK_NULL_PATH (data , data_len )) {
140- zend_argument_value_error (1 , "must be a valid path" );
140+ zend_argument_type_error (1 , "must be a valid path" );
141141 RETURN_THROWS ();
142142 }
143143 stream = php_stream_open_wrapper_ex (data , "rb" , REPORT_ERRORS , NULL , FG (default_context ));
@@ -254,18 +254,14 @@ static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename,
254254 }
255255
256256 ops = php_hash_fetch_ops (algo );
257- if (!ops ) {
258- zend_throw_error (NULL , "Unknown hashing algorithm: %s" , ZSTR_VAL (algo ));
259- RETURN_THROWS ();
260- }
261- else if (!ops -> is_crypto ) {
262- zend_throw_error (NULL , "Non-cryptographic hashing algorithm: %s" , ZSTR_VAL (algo ));
257+ if (!ops || !ops -> is_crypto ) {
258+ zend_argument_value_error (1 , "must be a valid cryptographic hashing algorithm" );
263259 RETURN_THROWS ();
264260 }
265261
266262 if (isfilename ) {
267263 if (CHECK_NULL_PATH (data , data_len )) {
268- zend_throw_error ( NULL , "Invalid path" );
264+ zend_argument_type_error ( 2 , "must be a valid path" );
269265 RETURN_THROWS ();
270266 }
271267 stream = php_stream_open_wrapper_ex (data , "rb" , REPORT_ERRORS , NULL , FG (default_context ));
@@ -361,18 +357,18 @@ PHP_FUNCTION(hash_init)
361357
362358 ops = php_hash_fetch_ops (algo );
363359 if (!ops ) {
364- zend_throw_error ( NULL , "Unknown hashing algorithm: %s" , ZSTR_VAL ( algo ) );
360+ zend_argument_value_error ( 1 , "must be a valid hashing algorithm" );
365361 RETURN_THROWS ();
366362 }
367363
368364 if (options & PHP_HASH_HMAC ) {
369365 if (!ops -> is_crypto ) {
370- zend_throw_error ( NULL , "HMAC requested with a non- cryptographic hashing algorithm: %s" , ZSTR_VAL ( algo ) );
366+ zend_argument_value_error ( 1 , "must be a cryptographic hashing algorithm if HMAC is requested" );
371367 RETURN_THROWS ();
372368 }
373369 if (!key || (ZSTR_LEN (key ) == 0 )) {
374370 /* Note: a zero length key is no key at all */
375- zend_throw_error ( NULL , "HMAC requested without a key " );
371+ zend_argument_value_error ( 3 , "cannot be empty when HMAC is requested " );
376372 RETURN_THROWS ();
377373 }
378374 }
@@ -649,28 +645,23 @@ PHP_FUNCTION(hash_hkdf)
649645 }
650646
651647 ops = php_hash_fetch_ops (algo );
652- if (!ops ) {
653- zend_throw_error (NULL , "Unknown hashing algorithm: %s" , ZSTR_VAL (algo ));
654- RETURN_THROWS ();
655- }
656-
657- if (!ops -> is_crypto ) {
658- zend_throw_error (NULL , "Non-cryptographic hashing algorithm: %s" , ZSTR_VAL (algo ));
648+ if (!ops || !ops -> is_crypto ) {
649+ zend_argument_value_error (1 , "must be a valid cryptographic hashing algorithm" );
659650 RETURN_THROWS ();
660651 }
661652
662653 if (ZSTR_LEN (ikm ) == 0 ) {
663- zend_throw_error ( NULL , "Input keying material cannot be empty" );
654+ zend_argument_value_error ( 2 , "cannot be empty" );
664655 RETURN_THROWS ();
665656 }
666657
667658 if (length < 0 ) {
668- zend_throw_error ( NULL , "Length must be greater than or equal to 0: " ZEND_LONG_FMT , length );
659+ zend_argument_value_error ( 3 , "must be greater than or equal to 0" );
669660 RETURN_THROWS ();
670661 } else if (length == 0 ) {
671662 length = ops -> digest_size ;
672663 } else if (length > (zend_long ) (ops -> digest_size * 255 )) {
673- zend_throw_error ( NULL , "Length must be less than or equal to %zd: " ZEND_LONG_FMT , ops -> digest_size * 255 , length );
664+ zend_argument_value_error ( 3 , "must be less than or equal to %zd" , ops -> digest_size * 255 );
674665 RETURN_THROWS ();
675666 }
676667
@@ -749,27 +740,23 @@ PHP_FUNCTION(hash_pbkdf2)
749740 }
750741
751742 ops = php_hash_fetch_ops (algo );
752- if (!ops ) {
753- zend_throw_error ( NULL , "Unknown hashing algorithm: %s" , ZSTR_VAL ( algo ) );
743+ if (!ops || ! ops -> is_crypto ) {
744+ zend_argument_value_error ( 1 , "must be a valid cryptographic hashing algorithm" );
754745 RETURN_THROWS ();
755746 }
756- else if (!ops -> is_crypto ) {
757- zend_throw_error (NULL , "Non-cryptographic hashing algorithm: %s" , ZSTR_VAL (algo ));
747+
748+ if (salt_len > INT_MAX - 4 ) {
749+ zend_argument_value_error (3 , "must be less than or equal to INT_MAX - 4 bytes" );
758750 RETURN_THROWS ();
759751 }
760752
761753 if (iterations <= 0 ) {
762- zend_throw_error ( NULL , "Iterations must be a positive integer: " ZEND_LONG_FMT , iterations );
754+ zend_argument_value_error ( 4 , "must be greater than 0" );
763755 RETURN_THROWS ();
764756 }
765757
766758 if (length < 0 ) {
767- zend_throw_error (NULL , "Length must be greater than or equal to 0: " ZEND_LONG_FMT , length );
768- RETURN_THROWS ();
769- }
770-
771- if (salt_len > INT_MAX - 4 ) {
772- zend_throw_error (NULL , "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied" , salt_len );
759+ zend_argument_value_error (5 , "must be greater than or equal to 0" );
773760 RETURN_THROWS ();
774761 }
775762
@@ -875,12 +862,12 @@ PHP_FUNCTION(hash_equals)
875862
876863 /* We only allow comparing string to prevent unexpected results. */
877864 if (Z_TYPE_P (known_zval ) != IS_STRING ) {
878- zend_type_error ( "Expected known_string to be a string, %s given" , zend_zval_type_name (known_zval ));
865+ zend_argument_type_error ( 1 , "must be of type string, %s given" , zend_zval_type_name (known_zval ));
879866 RETURN_THROWS ();
880867 }
881868
882869 if (Z_TYPE_P (user_zval ) != IS_STRING ) {
883- zend_type_error ( "Expected user_string to be a string, %s given" , zend_zval_type_name (user_zval ));
870+ zend_argument_type_error ( 2 , "must be of type string, %s given" , zend_zval_type_name (user_zval ));
884871 RETURN_THROWS ();
885872 }
886873
0 commit comments