-
-
Notifications
You must be signed in to change notification settings - Fork 336
Add cryptographically secure random bytes source #2812
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
base: master
Are you sure you want to change the base?
Conversation
|
you can test this with import std::io;
import std::crypto::random;
fn void main()
{
char [64] key;
random::crypto_secure_random(&key)!!;
io::printn(key);
} |
|
fopen, fread, fclose is not a good way to get a random. Have you taken a look at my example in #2808, syscall is much faster. 👎 |
|
It's much more portable as it also works on the BSDs and Darwin and the syscall number of getrandom() depends on the architecture. |
|
idk why it just closed the PR but I've added the getrandom() call for Linux/x86_64 |
|
It makes sense to add OS/Architecture specific cases to use the syscall but the file method should remain as a fallback. |
|
The current implementations are: Linux/Android x86_64/Aarch64: syscall |
|
I'm also currently installing Windows 10 in a VM to test the Win32 implementation. |
|
The stdlib has a random interface that can be used through functions like Lines 165 to 174 in 396263f
Maybe this could be added under https://github.com/c3lang/c3c/tree/master/lib/std/math/random as a type that implements this interface? Though I don't know if this form of random would be able to fulfil the expectations of the Random interface (specifically setting the random seed) so it's up to you if you want to do that.
|
|
No the idea is to use this to derive cryptographically strong keys. I originally started this since I wanted to implement RSA for the stdlib but then I realized there is no way to generate those random primes safely. |
|
I wonder if we should add an option to use |
|
Okay from my tests, the Win32 version works on Windows 10 and under Wine. |
|
@laura240406 Pull requests aren't meant for ongoing experimentation. Please keep PRs focused on a specific requested change, or a complete implementation. For discussion or exploration, let's use issues, discussions or discord chat instead. |
|
okay but this PR as it currently stands should be done it has syscall implementations for the main platforms of Linux/Android (x86_64/x86/Aarch64) and the library implementation of Win32 and the fallback of /dev/urandom a later PR could add *BSD specific getrandom() implementations and x86 RDRAND implementations but that's out of scope rn |
|
I've also tested it on OpenBSD (uses /dev/urandom) and it works |
| alias RtlGenRandom = fn CInt(void *, ulong); | ||
| RtlGenRandom rtlgenrandom @if(env::WIN32) @local; | ||
|
|
||
| fn void crypto_init_win32_random() @if(env::WIN32) @init |
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.
This should probably not be @init, since that will make ALL win32 apps do this. Instead, the correct way is to run this once. You can use OnceFlag.call_once in std::thread to ensure it's done once across all threads.
|
Can you add a test for this to the unit tests, and then add a note in the releasenotes as well please? |
My proposal implementation for #2808. The WIN32 version is untested as it seems to be impossible for me to obtain the Windows SDK as it's buried in layers of MSIs that Wine can't deal with.