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
/* Copyright (c) Fortanix, Inc.
 *
 * Licensed under the GNU General Public License, version 2 <LICENSE-GPL or
 * https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version
 * 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your
 * option. This file may not be copied, modified, or distributed except
 * according to those terms. */

pub mod ctr_drbg;
pub mod hmac_drbg;
#[cfg(feature = "std")]
pub mod os_entropy;
#[cfg(feature = "rdrand")]
mod rdrand;

#[doc(inline)]
pub use self::ctr_drbg::CtrDrbg;
#[doc(inline)]
pub use self::hmac_drbg::HmacDrbg;
#[cfg(feature = "std")]
#[doc(inline)]
pub use self::os_entropy::OsEntropy;
#[cfg(feature = "rdrand")]
pub use self::rdrand::{Entropy as Rdseed, Nrbg as Rdrand};

use error::IntoResult;
use mbedtls_sys::types::raw_types::{c_int, c_uchar};
use mbedtls_sys::types::size_t;

callback!(EntropyCallback:Sync(data: *mut c_uchar, len: size_t) -> c_int);
callback!(RngCallback:Sync(data: *mut c_uchar, len: size_t) -> c_int);

pub trait Random: RngCallback {
    fn random(&mut self, data: &mut [u8]) -> ::Result<()> {
        try!(unsafe { Self::call(self.data_ptr(), data.as_mut_ptr(), data.len()) }.into_result());
        Ok(())
    }
}

impl<'r, F: RngCallback> Random for F {}