PHP: openssl_random_pseudo_bytes() 和加密安全与极端随机性

PHP: openssl_random_pseudo_bytes() and Crypto Security vs Extreme Randomness

对 PHP 中的 openssl_random_pseudo_bytes() 进行了一些研究,我注意到在 PHP 的源代码中 openssl_random_pseudo_bytes() 函数的实现中. OpenSSL 的 RAND_pseudo_bytes 函数用于生成 return 值,这与 OpenSSL 中也可用的 RAND_bytes 相对.

OpenSSL对这两个函数的说明如下:

RAND_pseudo_bytes() puts num pseudo-random bytes into buf. Pseudo-random byte sequences generated by RAND_pseudo_bytes() will be unique if they are of sufficient length, but are not necessarily unpredictable. They can be used for non-cryptographic purposes and for certain purposes in cryptographic protocols, but usually not for key generation etc.

RAND_bytes() puts num cryptographically strong pseudo-random bytes into buf. An error occurs if the PRNG has not been seeded with enough randomness to ensure an unpredictable byte sequence.

我想我的问题是为什么没有使用 RAND_bytes 或者为什么 PHP 中没有 openssl_rand_bytes() 函数,如果是的话,根据 OpenSSL,更随机。

只是好奇。这是速度问题吗?不够可靠?还是 PRNG 是问题所在(即:当伪函数在大多数情况下都能正常工作时难以实现)?

谢谢

这个选择可能是基于实用性而不是加密货币的可靠性。如果使用 RAND_bytes(),该函数可能会由于可用的随机性不足而失败。 PHP 代码的作者无疑是想避免 PHP 函数失败。

我注意到 openssl_random_pseudo_bytes() 函数确实有一个可选的 crypto_strong 参数,根据 OpenSSL 的观点,它可以让调用者知道返回的字节是否真的具有很强的加密强度。

顺便说一句,可以使用外部引擎配置 OpenSSL,其中一些引擎(例如 CHIL)对 RAND_pseudo_bytes()RAND_bytes() 使用基于硬件的随机源,如果是的话你需要。

此外,在 Windows 上,PHP 代码正在使用 CryptGenRandom