aboutsummaryrefslogtreecommitdiff
path: root/src/rand.rs
blob: ce5f2df3244af213b0404283880f8455e663e037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Cryptographically secure randomness.

use std::io;

/// The maximum amount of entropy that can be read at a time.
pub const MAX_SIZE: usize = 256;

/// Fills the specified slice with cryptographically strong entropy.
pub fn fill(buf: &mut [u8]) {
    for chunk in buf.chunks_mut(MAX_SIZE) {
        getentropy(chunk).expect("chunk size less than max");
    }
}

/// Populates the specified slice with cryptographically strong entropy.
///
/// Returns an error if the buffer length is greater than [`MAX_SIZE`].
pub fn getentropy(buf: &mut [u8]) -> io::Result<()> {
    let result = unsafe { ffi::getentropy(buf.as_mut_ptr(), buf.len()) };
    if result >= 0 {
        Ok(())
    } else {
        Err(io::Error::last_os_error())
    }
}

mod ffi {
    use std::ffi::c_int;

    extern "C" {
        pub fn getentropy(buffer: *mut u8, len: usize) -> c_int;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn read_os_entropy() {
        let mut buf = [0u8; 32];
        assert!(getentropy(&mut buf[..16]).is_ok());
        assert!(getentropy(&mut buf).is_ok());
    }

    #[test]
    fn error_reading_more_than_256_bytes() {
        let mut buf = [0u8; MAX_SIZE + 1];
        assert!(getentropy(&mut buf[..MAX_SIZE]).is_ok());
        assert!(getentropy(&mut buf).is_err());
    }
}