-
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
Got random_int() seemingly working thanks to @ircmaxell
- Loading branch information
commit b32e0d07cf863e1718109ed0968d5d2c6f82d790
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
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.
So the
random_int()function: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.
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.
Let
bw = sizeof(zend_ulong) * 8.ZEND_ULONG_MAX % umaxwill take its largest value forumax = 2 ** (bw - 1), in which caselimit = 2 ** (bw - 1) - 1. This means that allresults 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 generatingnrandom numbers is1 / 2**n.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.
@nikic That looks correct.
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.
Why can't we just use the
RAND_RANGEmacro (or something similar) for this? It seems to me that fighting against the modulo problem is a wasted effort.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.
@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.
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.
Get out! :)
In
rand()ormt_rand()I completely agree, it would be wasted effort. In a function that is advertised as crypto-quality it's important.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.
Well, that's fair enough; I shall read more into this, thanks :)