std: introduce a thread-local CSPRNG for general use

std.crypto.random

* cross platform, even freestanding
* can't fail. on initialization for some systems requires calling
  os.getrandom(), in which case there are rare but theoretically
  possible errors. The code panics in these cases, however the
  application may choose to override the default seed function and then
  handle the failure another way.
* thread-safe
* supports the full Random interface
* cryptographically secure
* no syscall required to initialize on Linux (AT_RANDOM)
* calls arc4random on systems that support it

`std.crypto.randomBytes` is removed in favor of `std.crypto.random.bytes`.

I moved some of the Random implementations into their own files in the
interest of organization.

stage2 no longer requires passing a RNG; instead it uses this API.

Closes #6704
This commit is contained in:
Andrew Kelley
2020-12-17 20:03:41 -07:00
parent ce65533985
commit 013efaf139
24 changed files with 730 additions and 629 deletions

View File

@@ -303,8 +303,7 @@ fn getCwdOrWasiPreopen() std.fs.Dir {
pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
var random_bytes: [TmpDir.random_bytes_count]u8 = undefined;
std.crypto.randomBytes(&random_bytes) catch
@panic("unable to make tmp dir for testing: unable to get random bytes");
std.crypto.random.bytes(&random_bytes);
var sub_path: [TmpDir.sub_path_len]u8 = undefined;
std.fs.base64_encoder.encode(&sub_path, &random_bytes);