blob: e1b8df6637e534ecf19486eb3d41bdca5692f3c9 [file] [log] [blame]
Joel Galenson4be0c6d2020-07-07 13:20:14 -07001// Copyright 2018 Developers of the Rand project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Implementation for Windows
10use crate::{error::RTL_GEN_RANDOM_FAILED, Error};
11
12extern "system" {
13 #[link_name = "SystemFunction036"]
14 fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: u32) -> u8;
15}
16
17pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
18 // Prevent overflow of u32
19 for chunk in dest.chunks_mut(u32::max_value() as usize) {
20 let ret = unsafe { RtlGenRandom(chunk.as_mut_ptr(), chunk.len() as u32) };
21 if ret == 0 {
22 return Err(RTL_GEN_RANDOM_FAILED);
23 }
24 }
25 Ok(())
26}