@@ -36,47 +36,46 @@ union rand_long_buffer {
36
36
};
37
37
38
38
// Copy/pasted from mcrypt.c
39
- static int php_random_bytes (char * bytes , zend_long size )
39
+ static int php_random_bytes (void * bytes , size_t size )
40
40
{
41
41
int n = 0 ;
42
42
43
43
#if PHP_WIN32
44
- /* random/urandom equivalent on Windows */
45
- BYTE * win_bytes = (BYTE * ) bytes ;
46
- if (php_win32_get_random_bytes (win_bytes , (size_t ) size ) == FAILURE ) {
44
+ /* Defer to CryptGenRandom on Windows */
45
+ if (php_win32_get_random_bytes (bytes , size ) == FAILURE ) {
47
46
php_error_docref (NULL , E_WARNING , "Could not gather sufficient random data" );
48
47
return FAILURE ;
49
48
}
50
- n = (int )size ;
51
49
#else
52
- // @todo Need to cache the fd for random_int() call within loop
53
- int fd ;
50
+ int fd = -1 ;
54
51
size_t read_bytes = 0 ;
55
-
52
+ #if HAVE_DEV_ARANDOM
53
+ fd = open ("/dev/arandom" , O_RDONLY );
54
+ #else
55
+ #if HAVE_DEV_URANDOM
56
56
fd = open ("/dev/urandom" , O_RDONLY );
57
+ #endif
58
+ #endif
57
59
if (fd < 0 ) {
58
60
php_error_docref (NULL , E_WARNING , "Cannot open source device" );
59
61
return FAILURE ;
60
62
}
63
+
61
64
while (read_bytes < size ) {
62
65
n = read (fd , bytes + read_bytes , size - read_bytes );
63
66
if (n < 0 ) {
64
67
break ;
65
68
}
66
69
read_bytes += n ;
67
70
}
68
- n = read_bytes ;
69
71
70
72
close (fd );
71
- if (n < size ) {
73
+ if (read_bytes < size ) {
72
74
php_error_docref (NULL , E_WARNING , "Could not gather sufficient random data" );
73
75
return FAILURE ;
74
76
}
75
77
#endif
76
78
77
- // @todo - Do we need to do this?
78
- bytes [size ] = '\0' ;
79
-
80
79
return SUCCESS ;
81
80
}
82
81
@@ -103,6 +102,8 @@ PHP_FUNCTION(random_bytes)
103
102
return ;
104
103
}
105
104
105
+ bytes -> val [size ] = '\0' ;
106
+
106
107
RETURN_STR (bytes );
107
108
}
108
109
/* }}} */
0 commit comments