Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f8d7aec
Got initial API added
SammyK Feb 13, 2015
26e4ed2
Got mostly working. I think Hex is broken. Need to add int.
SammyK Feb 13, 2015
3c5fcac
Updated to use zend_string but getting memory leaks. Doh!
SammyK Feb 20, 2015
e96b077
Got random_bytes() working again.
SammyK Feb 20, 2015
aa0ca69
Fix random_int() checking size of wrong var.
SammyK Feb 20, 2015
b32e0d0
Got random_int() seemingly working thanks to @ircmaxell
SammyK Feb 20, 2015
2c659ed
Make maximum argument to random_int() optional with default to INT_MAX.
SammyK Feb 20, 2015
a1e6229
Remove random_hex(). *sadface*
SammyK Feb 20, 2015
bbc9198
Detect presence of /dev/arandom
lt Feb 21, 2015
7a99db6
Tidy up `php_random_bytes` and add /dev/arandom
lt Feb 21, 2015
3d413ad
Ensure random_int() uses a uniform distribution
lt Feb 22, 2015
513d5c9
Allow full integer range from random_int()
lt Feb 24, 2015
77f99cc
Use arc4random where present
lt Feb 24, 2015
7ef5754
Merge pull request #1 from lt/rand-bytes
SammyK Feb 24, 2015
766ce0c
Add tests
SammyK Feb 24, 2015
99e36d6
Fix wording in error message. Add check for max value.
SammyK Feb 24, 2015
c6fc391
Fix return types on error. Avoid a warning on BSD systems.
SammyK Feb 25, 2015
ab02b7b
Add fd caching
lt Mar 4, 2015
fd0570b
Merge remote-tracking branch 'leigh/rand-bytes' into rand-bytes
SammyK Mar 13, 2015
7ae4917
Fixes based on PR feedback
lt Mar 27, 2015
a67e42f
Changes based on feedback
lt Apr 9, 2015
f8a6d38
Normalized the return value for errors & updated tests.
SammyK Apr 10, 2015
2990341
Fix merge conflicts
SammyK Apr 10, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Got random_int() seemingly working thanks to @ircmaxell
  • Loading branch information
SammyK committed Feb 20, 2015
commit b32e0d07cf863e1718109ed0968d5d2c6f82d790
60 changes: 26 additions & 34 deletions ext/standard/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>

#include "php.h"

#if PHP_WIN32
# include "win32/winutil.h"
#endif

// Big thanks to @ircmaxell for the help on this bit
union rand_long_buffer {
char buffer[8];
long number;
};

/*
// Copy/pasted from string.c
static char hexconvtab[] = "0123456789abcdef";
Expand Down Expand Up @@ -62,6 +69,7 @@ static int php_random_bytes(char *bytes, zend_long size)
}
n = (int)size;
#else
// @todo Need to cache the fd for random_int() call within loop
int fd;
size_t read_bytes = 0;

Expand Down Expand Up @@ -152,56 +160,40 @@ PHP_FUNCTION(random_hex)
}
/* }}} */

/* {{{ proto int random_int(int max)
/* {{{ proto int random_int(int maximum)
Return an arbitrary pseudo-random integer */
PHP_FUNCTION(random_int)
{
zend_long max;
zend_long maximum;
zend_long size;
zend_long number = 0;
zend_string *bytes;
size_t i;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &max) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maximum) == FAILURE) {
return;
}

if (max >= INT_MAX) {
php_error_docref(NULL, E_WARNING, "Cannot use max greater than %d", INT_MAX);
if (maximum <= 0 || maximum >= INT_MAX) {
php_error_docref(NULL, E_WARNING, "Cannot use maximum less than 1 or greater than %d", INT_MAX);
RETURN_FALSE;
}

size = sizeof(max);

bytes = zend_string_alloc(size, 0);
long range = (long) maximum; // @todo Support min?

if (php_random_bytes(bytes->val, size) == FAILURE) {
zend_string_release(bytes);
return;
}
// Big thanks to @ircmaxell for the help on this bit
union rand_long_buffer value;
long result;
int bits = (int) (log((double) range) / log(2.0)) + 1;
int bytes = MAX(ceil(bits / 8), 1);
long mask = (long) pow(2.0, (double) bits) - 1;

// @todo bin-to-int: I know this is wrong but don't know how to fix
for (i = 0; i < size; i++) {
unsigned char c = bytes->val[i++];
unsigned char d;

if (c >= '0' && c <= '9') {
d = c - '0';
} else if (c >= 'a' && c <= 'f') {
d = c - 'a' - 10;
} else if (c >= 'A' && c <= 'F') {
d = c - 'A' - 10;
} else {
continue;
do {
if (php_random_bytes(&value.buffer, 8) == FAILURE) {
return;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the random_int() function:

  • has a non-deterministic execution time (actually, it's quite random)
  • that doesn't have any limit
  • so the function isn't guaranteed to return at all, ever.

Naturally I understand that the likelihood of these characteristics causing problems in PHP applications is small but I think reviewers should be aware of it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let bw = sizeof(zend_ulong) * 8. ZEND_ULONG_MAX % umax will take its largest value for umax = 2 ** (bw - 1), in which case limit = 2 ** (bw - 1) - 1. This means that all results with top bit 1 will be discarded here. For every result the probability for a set top bit is 1/2. As such the worst case probability that this loop has not stopped after generating n random numbers is 1 / 2**n.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic That looks correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we just use the RAND_RANGE macro (or something similar) for this? It seems to me that fighting against the modulo problem is a wasted effort.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@datibbaw Because RAND_RANGE is highly biased. It's not an option for crypto-quality randomness. I'm not aware of algorithms for this that don't use a form of rejection sampling.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that fighting against the modulo problem is a wasted effort.

Get out! :)

In rand() or mt_rand() I completely agree, it would be wasted effort. In a function that is advertised as crypto-quality it's important.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that's fair enough; I shall read more into this, thanks :)

result = value.number & mask;
} while (result > maximum);

// Binary = base-2
number = number * 2 + d;
}

zend_string_release(bytes);

RETURN_LONG(number);
RETURN_LONG(result);
}
/* }}} */

Expand Down