summaryrefslogtreecommitdiff
path: root/nrf-softdevice/src/random.rs
blob: c28ea64c43da6fcaf9b58624930f2203b99190df (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
use crate::{raw, RawError, Softdevice};

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum RandomError {
    BufferTooBig,
    NotEnoughEntropy,
    Raw(RawError),
}

impl From<RawError> for RandomError {
    fn from(err: RawError) -> Self {
        Self::Raw(err)
    }
}

/// Get cryptographically-securerandom bytes.
pub fn random_bytes(_sd: &Softdevice, buf: &mut [u8]) -> Result<(), RandomError> {
    if buf.len() > u8::MAX as usize {
        return Err(RandomError::BufferTooBig);
    }

    let ret = unsafe { raw::sd_rand_application_vector_get(buf[..].as_mut_ptr(), buf.len() as u8) };
    match RawError::convert(ret) {
        Ok(()) => Ok(()),
        Err(RawError::SocRandNotEnoughValues) => Err(RandomError::NotEnoughEntropy),
        Err(e) => Err(e.into()),
    }
}