This is an old revision of the document!
PHP RFC: Easy User-land CSPRNG
- Version: 0.1
- Date: 2015-02-20
- Author: Sammy Kaye Powers, me@sammyk.me
- Status: Draft
- First Published at: http://wiki.php.net/rfc/easy_userland_csprng
Introduction
This RFC proposes adding a user-land API for an easy to use and reliable CSPRNG in PHP.
The Problem
By default PHP does not provide an easy mechanism for accessing cryptographically strong random numbers in user-land. Users have a few options like openssl_random_pseudo_bytes()
, mcrypt_create_iv()
or directly opening /dev/*random
devices to obtain high quality pseudo-random bytes, but unfortunately system support for these functions and extensions varies.
The mcrypt_create_iv()
function is provided by the MCrypt lib which is solid, but unmaintained. Since it is built into PHP as an extension, it might not be enabled in certain environments (like most versions of PHP on Mac OS X). The longer this lib goes unmaintained, the more likely it is to have a security hole discovered that goes unfixed. And on top of all that, there is a bounty on MCrypt's head!
The openssl_random_pseudo_bytes()
function is provided by the OpenSSL lib which is being actively maintained but is hugely bloated and we've seen several major security issues pop up requiring the most-up-to-date version of the lib to stay secure. Moreover, in certain configurations, openssl_random_pseudo_bytes()
will return bytes that are not cryptographically secure adding more required knowledge in user-land to ensure secure bytes.
Currently the most reliable way to grab pseudo-random bytes across systems is by using either of the libs mentioned above or falling back to a stream of bytes from /dev/urandom
which is OS-specific and can fail when the open_basedir
ini setting is set. This requires user-land apps to write potentially 100's of lines of code to simply generate pseudo-random bytes and there are several caveats that will not generate cryptographically secure bytes. And in some cases no reliable method can be found at all.
See the Facebook PHP SDK's implementation of a CSPRNG in PHP to understand how much code is needed in user-land to simply generate cryptographically secure pseudo-random bytes.
Proposal
There should be a user-land API to easily return an arbitrary length of cryptographically secure pseudo-random bytes directly and work on any supported server configuration or OS.
The initial proposal is to add two user-land functions that return the bytes as binary and integer.
$randomStr = random_bytes($bytes = 16); $randomInt = random_int($min = -PHP_INT_MAX, $max = PHP_INT_MAX);
The sources of random used are as follows:
- On windows
CryptGenRandom
is used exclusively arc4random_buf()
is used if present for fd-less random/dev/arandom
is used where available/dev/urandom
is used where none of the above is available
Backward Incompatible Changes
There would be no BC breaks.
Proposed PHP Version(s)
PHP 7
RFC Impact
To SAPIs
This RFC should not impact the SAPI's.
To Existing Extensions
No existing extensions are affected.
To Opcache
Opcache is unaffected.
New Constants
There would be no new constants.
php.ini Defaults
There would be no new php.ini defaults.
Open Issues
- Nothing yet
Unaffected PHP Functionality
This change does not affect any of the existing rand()
or mt_rand()
functionality.
Future Scope
The concepts from the RFC could be used to:
- Deprecate
mcrypt_create_iv()
- Improve
session_id
randomness generation - Detect LibreSSL portable for arc4random() on Linux
- Improve fd-less random for chroot environments with our own arc4random and the linux
getrandom
syscall
Patches and Tests
The current WIP patch can be found here: https://github.com/SammyK/php-src/compare/rand-bytes#diff-f1067207e863d8fa568e63446920e7fcR182
References
None so far.
Rejected Features
None so far.
Changelog
- 0.1: Mmmm drafty - Leigh
- 0.0: Initial draft - need Leigh's input
Acknowledgements
Big thanks to Anthony Ferrara, Daniel Lowrey, Leigh, E. Smith and all the kids in the PHP room for all the help with this one!