summaryrefslogtreecommitdiffstats
path: root/vendor/security-framework/src/random.rs
blob: 7bd7f614124807da1dd96e2064b6d66bfb83e779 (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
//! Randomness support.

use security_framework_sys::random::{SecRandomCopyBytes, SecRandomRef, kSecRandomDefault};
use std::io;

/// A source of random data.
pub struct SecRandom(SecRandomRef);

unsafe impl Sync for SecRandom {}
unsafe impl Send for SecRandom {}

impl Default for SecRandom {
    #[inline(always)]
    fn default() -> Self {
        unsafe { Self(kSecRandomDefault) }
    }
}

impl SecRandom {
    /// Fills the buffer with cryptographically secure random bytes.
    pub fn copy_bytes(&self, buf: &mut [u8]) -> io::Result<()> {
        if unsafe { SecRandomCopyBytes(self.0, buf.len(), buf.as_mut_ptr().cast()) } == 0 {
            Ok(())
        } else {
            Err(io::Error::last_os_error())
        }
    }
}

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

    #[test]
    fn basic() {
        let mut buf = [0; 10];
        SecRandom::default().copy_bytes(&mut buf).unwrap();
    }
}