-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rand: use simple PRNG owned by Interface, sockets access it through C…
…ontext.
- Loading branch information
Showing
10 changed files
with
114 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,26 @@ | ||
#![allow(unsafe_code)] | ||
#![allow(unused)] | ||
|
||
#[cfg(not(any(test, feature = "std", feature = "rand-custom-impl")))] | ||
compile_error!("None of the Cargo features `std` or `rand-custom-impl` is enabled. smoltcp needs a `rand` implementation to work. If your target supports `std`, enable the `std` feature to use the OS's RNG. Otherwise, you must enable the `rand-custom-impl` Cargo feature, and supply your own custom implementation using the `smoltcp::rand_custom_impl!()` macro"); | ||
|
||
pub fn rand_u32() -> u32 { | ||
let mut val = [0; 4]; | ||
rand_bytes(&mut val); | ||
u32::from_ne_bytes(val) | ||
#[derive(Debug)] | ||
pub(crate) struct Rand { | ||
state: u64, | ||
} | ||
|
||
/// Fill `buf` with random bytes. | ||
pub fn rand_bytes(buf: &mut [u8]) { | ||
extern "Rust" { | ||
fn _smoltcp_rand(buf: &mut [u8]); | ||
impl Rand { | ||
pub(crate) const fn new(seed: u64) -> Self { | ||
Self { state: seed } | ||
} | ||
|
||
unsafe { _smoltcp_rand(buf) } | ||
} | ||
pub(crate) fn rand_u32(&mut self) -> u32 { | ||
// sPCG32 from https://www.pcg-random.org/paper.html | ||
// see also https://nullprogram.com/blog/2017/09/21/ | ||
const M: u64 = 0xbb2efcec3c39611d; | ||
const A: u64 = 0x7590ef39; | ||
|
||
/// Methods required for a custom rand implementation. | ||
/// | ||
/// This trait is not intended to be used directly, just to supply a custom rand implementation to smoltcp. | ||
#[cfg(feature = "rand-custom-impl")] | ||
pub trait Rand { | ||
/// Fill `buf` with random bytes. | ||
fn rand_bytes(buf: &mut [u8]); | ||
} | ||
|
||
/// Set the custom rand implementation. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// struct Rand; | ||
/// smoltcp::rand_custom_impl!(Rand); | ||
/// impl smoltcp::Rand for Rand { | ||
/// fn rand_bytes(buf: &mut [u8]) { | ||
/// // TODO | ||
/// } | ||
/// } | ||
/// | ||
#[macro_export] | ||
#[cfg(feature = "rand-custom-impl")] | ||
macro_rules! rand_custom_impl { | ||
($t: ty) => { | ||
#[no_mangle] | ||
fn _smoltcp_rand(buf: &mut [u8]) { | ||
<$t as $crate::Rand>::rand_bytes(buf) | ||
} | ||
}; | ||
} | ||
let s = self.state * M + A; | ||
self.state = s; | ||
|
||
#[cfg(all(feature = "std", not(feature = "rand-custom-impl"), not(test)))] | ||
#[no_mangle] | ||
fn _smoltcp_rand(buf: &mut [u8]) { | ||
use rand_core::RngCore; | ||
|
||
rand_core::OsRng.fill_bytes(buf) | ||
} | ||
|
||
#[cfg(test)] | ||
#[no_mangle] | ||
fn _smoltcp_rand(buf: &mut [u8]) { | ||
panic!("Rand should not be used when testing"); | ||
let shift = 29 - (s >> 61); | ||
(s >> shift) as u32 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.