Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hash() function hangs endlessly if using sha512 on strings >= 4GiB #12936

Closed
ohyeaah opened this issue Dec 11, 2023 · 1 comment
Closed

hash() function hangs endlessly if using sha512 on strings >= 4GiB #12936

ohyeaah opened this issue Dec 11, 2023 · 1 comment

Comments

@ohyeaah
Copy link

ohyeaah commented Dec 11, 2023

Description

The following script hangs endlessly.

<?php $t = time();
echo hash("sha512", str_repeat("x", 1024*1024*1024*4-1));
echo "\ncalculating hash took ".(time() - $t)."s\n";
$t = time();
echo hash("sha512", str_repeat("x", 1024*1024*1024*4));
echo "\ncalculating hash took ".(time() - $t)."s\n"; ?>

Resulted in this output:

f918f25f32fe81f274619743e661a4158f176384d74b46bce7c4305f160119ce727204db2910ff287535ca2e40c2a98f2d23b40c82afbcca750a532b9843e3bc
calculating hash took 14s

But I expected this output instead:

f918f25f32fe81f274619743e661a4158f176384d74b46bce7c4305f160119ce727204db2910ff287535ca2e40c2a98f2d23b40c82afbcca750a532b9843e3bc
calculating hash took 14s
...hash goes here...
calculating hash took 14s

PHP Version

PHP 8.2.10-2ubuntu1 (cli) (built: Sep 5 2023 14:37:47) (NTS)

Operating System

KUbuntu 23.10

@nielsdos
Copy link
Member

Looks like it's using unsigned int in some places... I'll check them all.

@nielsdos nielsdos self-assigned this Dec 11, 2023
nielsdos added a commit to nielsdos/php-src that referenced this issue Dec 11, 2023
…trings >= 4GiB

There's two problems:
- Some loops used `unsigned int` instead of `size_t`.
- The 2*N-bit addition that is emulated using 2 N bit numbers has a bug:
  it first truncated the number to 32/64 bit and only then shifted. This
  resulted in the wrong length info stored inside the resulting hash.
nielsdos added a commit to nielsdos/php-src that referenced this issue Dec 11, 2023
…trings >= 4GiB

There's two problems:
- Some loops used `unsigned int` instead of `size_t`.
- The 2*N-bit addition that is emulated using 2 N bit numbers has a bug:
  it first truncated the number to 32/64 bit and only then shifted. This
  resulted in the wrong length info stored inside the resulting hash.
nielsdos added a commit that referenced this issue Dec 12, 2023
* PHP-8.2:
  Fix GH-12936: hash() function hangs endlessly if using sha512 on strings >= 4GiB
nielsdos added a commit that referenced this issue Dec 12, 2023
* PHP-8.3:
  Fix GH-12936: hash() function hangs endlessly if using sha512 on strings >= 4GiB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants