void rust_win32_rand_release(HCRYPTPROV hProv) { win32_require (_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0)); }
void rust_win32_rand_acquire(HCRYPTPROV* phProv) { win32_require (_T("CryptAcquireContext"), // changes to the parameters here should be reflected in the docs of // std::rand::os::OSRng CryptAcquireContext(phProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT|CRYPT_SILENT)); }
void rng_gen_seed(uint8_t* dest, size_t size) { #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, size, (BYTE*) dest)); win32_require (_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0)); #else int fd = open("/dev/urandom", O_RDONLY); if (fd == -1) { fprintf(stderr, "error opening /dev/urandom: %s", strerror(errno)); abort(); } size_t amount = 0; do { ssize_t ret = read(fd, dest+amount, size-amount); if (ret < 0) { fprintf(stderr, "error reading /dev/urandom: %s", strerror(errno)); abort(); } else if (ret == 0) { fprintf(stderr, "somehow hit eof reading from /dev/urandom"); abort(); } amount += (size_t)ret; } while (amount < size); int ret = close(fd); if (ret != 0) { fprintf(stderr, "error closing /dev/urandom: %s", strerror(errno)); // FIXME #3697: Why does this fail sometimes? // abort(); } #endif }
void rust_win32_rand_gen(HCRYPTPROV hProv, DWORD dwLen, BYTE* pbBuffer) { win32_require (_T("CryptGenRandom"), CryptGenRandom(hProv, dwLen, pbBuffer)); }