Table of Contents

PHP RFC: Easy User-land 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 between platforms and each come with their own set of problems

In addition users may attempt to generate their own streams of random bytes relying on rand() or mt_rand(), and this is something we absolutely want to avoid.

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 platform.

The initial proposal is to add two user-land functions that return the bytes as binary and integer. Arbitrary length strings of random bytes are important for salts, keys and initialisation vectors. Integers based on CS random are important for applications where unbiased results are critical (i.e. shuffling a Poker deck).

Signatures:

random_bytes(int length);
random_int(int min, int max);

Examples:

$randomStr = random_bytes($length = 16);
 
$randomInt = random_int($min = 0, $max = 127);

The sources of random used are as follows:

Backward Incompatible Changes

Any user-land code that defines a random_bytes() or random_int() function would generate a fatal error, however it is likely that these functions provide the same or similar functionality as desired.

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

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:

Patches and Tests

The current patch can be found here: https://github.com/php/php-src/pull/1119

Proposed Voting Choices

The voting choices are yes (in favor for accepting this RFC for PHP 7) or no (against it).

Vote

Vote starts on March 14th, and will end two weeks later, on March 28th.

This RFC requires a 2/3 majority.

Reliable user-land CSPRNG
Real name Yes No
aharvey (aharvey)  
auroraeosrose (auroraeosrose)  
bishop (bishop)  
crodas (crodas)  
Damien Tournoud (damz)  
derick (derick)  
dragoonis (dragoonis)  
eliw (eliw)  
galvao (galvao)  
gasolwu (gasolwu)  
hywan (hywan)  
ircmaxell (ircmaxell)  
jedibc (jedibc)  
jmikola (jmikola)  
jpauli (jpauli)  
jwage (jwage)  
kinncj (kinncj)  
klaussilveira (klaussilveira)  
lcobucci (lcobucci)  
leigh (leigh)  
leszek (leszek)  
lstrojny (lstrojny)  
mbeccati (mbeccati)  
mike (mike)  
nikic (nikic)  
omars (omars)  
pajoye (pajoye)  
pauloelr (pauloelr)  
ralphschindler (ralphschindler)  
ramsey (ramsey)  
rdlowrey (rdlowrey)  
rdohms (rdohms)  
rmf (rmf)  
salathe (salathe)  
stas (stas)  
stelianm (stelianm)  
svpernova09 (svpernova09)  
thekid (thekid)  
weierophinney (weierophinney)  
yohgaki (yohgaki)  
zeev (zeev)  
Final result: 41 0
This poll has been closed.

Changelog

Acknowledgements

Big thanks to Anthony Ferrara, Daniel Lowrey, E. Smith and all the kids in the PHP room for all the help with this one!