fuchsia_cprng/
lib.rs
1#![no_std]
11
12pub fn cprng_draw(buffer: &mut [u8]) {
18 unsafe { zx_cprng_draw(buffer.as_mut_ptr(), buffer.len()) };
19}
20
21extern "C" {
23 fn zx_cprng_draw(buffer: *mut u8, length: usize);
24}
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29
30 #[test]
31 fn cprng() {
32 let mut buffer = [0; 20];
33 cprng_draw(&mut buffer);
34 let mut first_zero = 0;
35 let mut last_zero = 0;
36 for _ in 0..30 {
37 let mut buffer = [0; 20];
38 cprng_draw(&mut buffer);
39 if buffer[0] == 0 {
40 first_zero += 1;
41 }
42 if buffer[19] == 0 {
43 last_zero += 1;
44 }
45 }
46 assert_ne!(first_zero, 30);
47 assert_ne!(last_zero, 30);
48 }
49
50 #[test]
51 fn cprng_large() {
52 let mut buffer = [0; 1024];
53 cprng_draw(&mut buffer);
54
55 for mut s in buffer.chunks_mut(256) {
56 cprng_draw(&mut s);
57 }
58 }
59}