-
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
Closed
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 26e4ed2
Got mostly working. I think Hex is broken. Need to add int.
SammyK 3c5fcac
Updated to use zend_string but getting memory leaks. Doh!
SammyK e96b077
Got random_bytes() working again.
SammyK aa0ca69
Fix random_int() checking size of wrong var.
SammyK b32e0d0
Got random_int() seemingly working thanks to @ircmaxell
SammyK 2c659ed
Make maximum argument to random_int() optional with default to INT_MAX.
SammyK a1e6229
Remove random_hex(). *sadface*
SammyK bbc9198
Detect presence of /dev/arandom
lt 7a99db6
Tidy up `php_random_bytes` and add /dev/arandom
lt 3d413ad
Ensure random_int() uses a uniform distribution
lt 513d5c9
Allow full integer range from random_int()
lt 77f99cc
Use arc4random where present
lt 7ef5754
Merge pull request #1 from lt/rand-bytes
SammyK 766ce0c
Add tests
SammyK 99e36d6
Fix wording in error message. Add check for max value.
SammyK c6fc391
Fix return types on error. Avoid a warning on BSD systems.
SammyK ab02b7b
Add fd caching
lt fd0570b
Merge remote-tracking branch 'leigh/rand-bytes' into rand-bytes
SammyK 7ae4917
Fixes based on PR feedback
lt a67e42f
Changes based on feedback
lt f8a6d38
Normalized the return value for errors & updated tests.
SammyK 2990341
Fix merge conflicts
SammyK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Updated to use zend_string but getting memory leaks. Doh!
- Loading branch information
commit 3c5fcac453a0def6c754ab2a7f83b977e90a9b84
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,7 @@ static void php_bin_to_hex(char *old, const zend_long old_len, char *hex) | |
| } | ||
|
|
||
| // Copy/pasted from mcrypt.c | ||
| static int php_random_bytes(char *bytes, zend_long size) | ||
| static int php_random_bytes(zend_string *bytes, zend_long size) | ||
| { | ||
| int n = 0; | ||
|
|
||
|
|
@@ -90,7 +90,7 @@ Return an arbitrary length of pseudo-random bytes as binary string */ | |
| PHP_FUNCTION(random_bytes) | ||
| { | ||
| zend_long size; | ||
| char *bytes; | ||
| zend_string *bytes; | ||
|
|
||
| if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) { | ||
| return; | ||
|
|
@@ -101,16 +101,16 @@ PHP_FUNCTION(random_bytes) | |
| RETURN_FALSE; | ||
| } | ||
|
|
||
| bytes = ecalloc(size + 1, 1); | ||
| bytes = zend_string_alloc(size + 1, 0); | ||
|
|
||
| if (php_random_bytes(bytes, size) == FAILURE) { | ||
| efree(bytes); | ||
| zend_string_release(bytes); | ||
| return; | ||
|
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. Probably that should be
Contributor
Author
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. Nice catch! I'll fix. |
||
| } | ||
|
|
||
| RETVAL_STRINGL(bytes, size); | ||
|
|
||
| efree(bytes); | ||
| zend_string_release(bytes); | ||
| } | ||
| /* }}} */ | ||
|
|
||
|
|
@@ -119,8 +119,8 @@ Return an arbitrary length of pseudo-random bytes as hexadecimal string */ | |
| PHP_FUNCTION(random_hex) | ||
| { | ||
| zend_long size; | ||
| char *bytes; | ||
| char *hex; | ||
| zend_string *bytes; | ||
| zend_string *hex; | ||
|
|
||
| if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) { | ||
| return; | ||
|
|
@@ -132,21 +132,21 @@ PHP_FUNCTION(random_hex) | |
| } | ||
|
|
||
| // @todo should we half the size for hex? How for odd num of chars? | ||
| bytes = ecalloc(size + 1, 1); | ||
| bytes = zend_string_alloc(size + 1, 0); | ||
|
|
||
| if (php_random_bytes(bytes, size) == FAILURE) { | ||
| efree(bytes); | ||
| zend_string_release(bytes); | ||
| return; | ||
| } | ||
|
|
||
| int hex_size = size * 2; | ||
| hex = ecalloc(hex_size + 1, 1); | ||
| hex = zend_string_alloc(hex_size + 1, 0); | ||
| php_bin_to_hex(bytes, hex_size, hex); | ||
|
|
||
| RETVAL_STRINGL(hex, hex_size); | ||
|
|
||
| efree(bytes); | ||
| efree(hex); | ||
| zend_string_release(bytes); | ||
| zend_string_release(hex); | ||
| } | ||
| /* }}} */ | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Better would be
ssize_t.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 declaration should also be moved into the
whileloop, otherwise this will likely generate a warning on BSD systems with arc4random.