diff options
author | Isis Lovecruft <isis@torproject.org> | 2018-05-02 22:12:38 +0000 |
---|---|---|
committer | Isis Lovecruft <isis@torproject.org> | 2018-05-02 22:12:38 +0000 |
commit | 94dcd38a1430061a0bb630ad680a41a78262a55c (patch) | |
tree | bff61a3c57e151da07a0ff03e7510d72d0889dc5 /src/rust/external | |
parent | b5013e841cf1501014ab23e14a4083c105e0d092 (diff) | |
download | tor-94dcd38a1430061a0bb630ad680a41a78262a55c.tar.gz tor-94dcd38a1430061a0bb630ad680a41a78262a55c.zip |
rust: Expose crypto_rand() as an impl of rand_core::RngCore.
Diffstat (limited to 'src/rust/external')
-rw-r--r-- | src/rust/external/crypto_rand.rs | 30 |
1 files changed, 11 insertions, 19 deletions
diff --git a/src/rust/external/crypto_rand.rs b/src/rust/external/crypto_rand.rs index 19b9ab2816..af1ade0161 100644 --- a/src/rust/external/crypto_rand.rs +++ b/src/rust/external/crypto_rand.rs @@ -11,23 +11,18 @@ use std::time::Duration; -use libc::c_char; use libc::c_double; use libc::c_int; -use libc::c_uint; -use libc::c_void; use libc::size_t; use libc::time_t; use libc::uint8_t; -use libc::uint64_t; extern "C" { fn crypto_seed_rng() -> c_int; + fn crypto_rand(out: *mut uint8_t, out_len: size_t); fn crypto_strongest_rand(out: *mut uint8_t, out_len: size_t); fn crypto_rand_time_range(min: time_t, max: time_t) -> time_t; fn crypto_rand_double() -> c_double; - // fn crypto_random_hostname(min_rand_len: c_int, max_rand_len: c_int, - // prefix: *const c_char, suffix: *const c_char) -> *mut c_char; } /// Seed OpenSSL's random number generator with bytes from the operating @@ -48,7 +43,16 @@ pub fn c_tor_crypto_seed_rng() -> bool { } } -/// Fill the bytes of `dest` with strong random data. +/// Fill the bytes of `dest` with random data. +pub fn c_tor_crypto_rand(dest: &mut [u8]) { + unsafe { + crypto_rand(dest.as_mut_ptr(), dest.len() as size_t); + } +} + +/// Fill the bytes of `dest` with "strong" random data by hashing +/// together randomness obtained from OpenSSL's RNG and the operating +/// system. pub fn c_tor_crypto_strongest_rand(dest: &mut [u8]) { // We'll let the C side panic if the len is larger than // MAX_STRONGEST_RAND_SIZE, rather than potentially panicking here. A @@ -81,15 +85,3 @@ pub fn c_tor_crypto_rand_double() -> f64 { } } -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn test_layout_tor_weak_rng_t() { - assert_eq!(::std::mem::size_of::<tor_weak_rng_t>(), 0usize, - concat!("Size of: ", stringify!(tor_weak_rng_t))); - assert_eq!(::std::mem::align_of::<tor_weak_rng_t>(), 1usize, - concat!("Alignment of ", stringify!(tor_weak_rng_t))); - } -} |