diff options
| author | Roy Frostig <[email protected]> | 2010-07-25 21:45:09 -0700 |
|---|---|---|
| committer | Roy Frostig <[email protected]> | 2010-07-25 21:45:09 -0700 |
| commit | 5b6e714d05a0c96ade618256a67a9cb7f337f196 (patch) | |
| tree | 45406973ea3c0ffb1a9e070588f4ca5fc8c7744c /src/rt/rust_util.h | |
| parent | Don't write to NULL after calling C natives returning void. (diff) | |
| download | rust-5b6e714d05a0c96ade618256a67a9cb7f337f196.tar.xz rust-5b6e714d05a0c96ade618256a67a9cb7f337f196.zip | |
Expose an RNG (the one used by our runtime) to Rust via std.
Diffstat (limited to 'src/rt/rust_util.h')
| -rw-r--r-- | src/rt/rust_util.h | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/rt/rust_util.h b/src/rt/rust_util.h index 62ac7de2..95010f17 100644 --- a/src/rt/rust_util.h +++ b/src/rt/rust_util.h @@ -117,6 +117,38 @@ next_power_of_two(size_t s) return tmp + 1; } +// Initialization helper for ISAAC RNG + +static inline void +isaac_init(rust_dom *dom, randctx *rctx) +{ + memset(rctx, 0, sizeof(randctx)); + +#ifdef __WIN32__ + { + HCRYPTPROV hProv; + win32_require + (_T("CryptAcquireContext"), + CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT|CRYPT_SILENT)); + win32_require + (_T("CryptGenRandom"), + CryptGenRandom(hProv, sizeof(rctx->randrsl), + (BYTE*)(&rctx->randrsl))); + win32_require + (_T("CryptReleaseContext"), + CryptReleaseContext(hProv, 0)); + } +#else + int fd = open("/dev/urandom", O_RDONLY); + I(dom, fd > 0); + I(dom, read(fd, (void*) &rctx->randrsl, sizeof(rctx->randrsl)) + == sizeof(rctx->randrsl)); + I(dom, close(fd) == 0); +#endif + randinit(rctx, 1); +} + // Vectors (rust-user-code level). struct |