Bug->Doc #81473 [Nab->ReO]: hash_pbkdf2 truncate in hex

[email protected]
Newsgroups php.doc.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=81473&edit=1

 ID:                 81473
 Updated by:         [email protected]
 Reported by:        php at wfuchs dot de
 Summary:            hash_pbkdf2 truncate in hex
-Status:             Not a bug
+Status:             Re-Opened
-Type:               Bug
+Type:               Documentation Problem
 Package:            hash related
 Operating System:   Linux
 PHP Version:        8.0.11
-Assigned To:        cmb
+Assigned To:        
 Block user comment: N
 Private report:     N

 New Comment:

How about we add a second example to the page showing how $binary and $length affect the return value?


Previous Comments:
------------------------------------------------------------------------
[2021-09-25 15:43:43] [email protected]

> In my opinion this is a security related bug
It is always a security bug to call hashing functions without understanding how they should be used.

hash_pbkdf2 is a key derivation function. Its purpose is to generate a key that will presumably be fed into something else of a cryptographic nature. That "something else" might want binary bytes or it might want a hexit string. $length and $binary control hash_pbkdf2's output so a developer doesn't have to do further work with substr/bin2hex/hex2bin to be able to use the returned value as needed.

If you want a hexit string of length 128 (ie. 512 bits or two SHA256 hash blocks) then pass $binary=false and $length=128. Which is what the documentation says.

------------------------------------------------------------------------
[2021-09-25 12:25:58] php at wfuchs dot de

I am addressing the part you posted:
"if binary is false this corresponds to twice the byte-length".
In my opinion this is a security related bug that weakens the cryptography and makes the implementation incompatible to other languages:
256 different possibilities can be represented in one byte. That is 256^64 in binary form. Hexits are only the numbers 0-9 and the letters A-F which makes a maximum of 16 possible hexits. Therefore there are only 16^64 which is much less.
If you use 2 hexits per byte you get 16^2 which is 256 again.

------------------------------------------------------------------------
[2021-09-24 19:54:40] [email protected]

> The length in hex should be 128 and not 64 right?

No. The $length parameter dictates the length of the returned string. If you pass 64, the length of the returned string will be 64.

------------------------------------------------------------------------
[2021-09-24 19:14:37] php at wfuchs dot de

The behavior the function describes 

"if binary is false this corresponds to twice the byte-length of the derived key (as every byte of the key is returned as two hexits)"

The length in hex should be 128 and not 64 right?

------------------------------------------------------------------------
[2021-09-24 10:15:51] [email protected]

This behaves as advertized[1]:

| length
|
| The length of the output string. If binary is true this
| corresponds to the byte-length of the derived key, if binary is
| false this corresponds to twice the byte-length of the derived key
| (as every byte of the key is returned as two hexits).

[1] <https://www.php.net/hash_pbkdf2>

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=81473


--
Edit this bug report at https://bugs.php.net/bug.php?id=81473&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.