-
Notifications
You must be signed in to change notification settings - Fork 8k
[WIP] Easy User-land CSPRNG #1119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
f8d7aec
26e4ed2
3c5fcac
e96b077
aa0ca69
b32e0d0
2c659ed
a1e6229
bbc9198
7a99db6
3d413ad
513d5c9
77f99cc
7ef5754
766ce0c
99e36d6
c6fc391
ab02b7b
fd0570b
7ae4917
a67e42f
f8a6d38
2990341
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,19 @@ | |
|
|
||
| PHP_FUNCTION(random_bytes); | ||
| PHP_FUNCTION(random_int); | ||
|
|
||
| PHP_MINIT_FUNCTION(lcg); | ||
|
|
||
| ZEND_BEGIN_MODULE_GLOBALS(random) | ||
| int fd; | ||
| ZEND_END_MODULE_GLOBALS(random) | ||
|
|
||
| #ifdef ZTS | ||
| # define RANDOM_G(v) TSRMG(random_globals_id, zend_random_globals *, v); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there should be no semicolon at the end
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also ZEND_TSRMG should be used to avoid abundant function calls. But even without it I guess the TS build is broken as there's no code allocating/deallocating globals. You can lookup how it is done in some other submodule, fe browser cap. I'd also like to ask - shouldn't the secure device actually be reopened on each request rather than once on the start? Maybe it were better to introduce the request handlers, if it should. Or even not putting it into globals, but reopening every time it's required - then it's guaranteed to be valid every time it's used. |
||
| #else | ||
| # define RANDOM_G(v) random_globals.v | ||
| #endif | ||
|
|
||
| #endif | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,11 +24,23 @@ | |
| #include <math.h> | ||
|
|
||
| #include "php.h" | ||
| #include "php_random.h" | ||
|
|
||
| #if PHP_WIN32 | ||
| # include "win32/winutil.h" | ||
| #endif | ||
|
|
||
| ZEND_DECLARE_MODULE_GLOBALS(random); | ||
|
|
||
| /* {{{ */ | ||
| PHP_MINIT_FUNCTION(random) | ||
| { | ||
| RANDOM_G(fd) = -1; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this descriptor should be closed somewhen |
||
| return SUCCESS; | ||
| } | ||
| /* }}} */ | ||
|
|
||
| /* {{{ */ | ||
| static int php_random_bytes(void *bytes, size_t size) | ||
| { | ||
| int n = 0; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better would be
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The declaration should also be moved into the |
||
|
|
@@ -43,18 +55,23 @@ static int php_random_bytes(void *bytes, size_t size) | |
| #if HAVE_DECL_ARC4RANDOM_BUF | ||
| arc4random_buf(bytes, size); | ||
| #else | ||
| int fd = -1; | ||
| int fd = RANDOM_G(fd); | ||
| size_t read_bytes = 0; | ||
|
|
||
| if (fd < 0) { | ||
| #if HAVE_DEV_ARANDOM | ||
| fd = open("/dev/arandom", O_RDONLY); | ||
| fd = open("/dev/arandom", O_RDONLY); | ||
| #else | ||
| #if HAVE_DEV_URANDOM | ||
| fd = open("/dev/urandom", O_RDONLY); | ||
| fd = open("/dev/urandom", O_RDONLY); | ||
| #endif // URANDOM | ||
| #endif // ARANDOM | ||
| if (fd < 0) { | ||
| php_error_docref(NULL, E_WARNING, "Cannot open source device"); | ||
| return FAILURE; | ||
| if (fd < 0) { | ||
| php_error_docref(NULL, E_WARNING, "Cannot open source device"); | ||
| return FAILURE; | ||
| } | ||
|
|
||
| RANDOM_G(fd) = fd; | ||
| } | ||
|
|
||
| while (read_bytes < size) { | ||
|
|
@@ -65,7 +82,6 @@ static int php_random_bytes(void *bytes, size_t size) | |
| read_bytes += n; | ||
| } | ||
|
|
||
| close(fd); | ||
| if (read_bytes < size) { | ||
| php_error_docref(NULL, E_WARNING, "Could not gather sufficient random data"); | ||
| return FAILURE; | ||
|
|
@@ -75,6 +91,7 @@ static int php_random_bytes(void *bytes, size_t size) | |
|
|
||
| return SUCCESS; | ||
| } | ||
| /* }}} */ | ||
|
|
||
| /* {{{ proto string random_bytes(int length) | ||
| Return an arbitrary length of pseudo-random bytes as binary string */ | ||
|
|
@@ -139,7 +156,7 @@ PHP_FUNCTION(random_int) | |
| umax++; | ||
|
|
||
| // Powers of two are not biased | ||
| if (umax & ~umax != umax) { | ||
| if ((umax & ~umax) != umax) { | ||
| // Ceiling under which ZEND_LONG_MAX % max == 0 | ||
| limit = ZEND_ULONG_MAX - (ZEND_ULONG_MAX % umax) - 1; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The prototype to this needs to be somewhere in the header, otherwise there'll be visibility issue.