From af60db2c480fbd9b0f9fb7ba8a0ddea3bdad62bb Mon Sep 17 00:00:00 2001 From: Able Date: Mon, 5 Dec 2022 03:34:40 -0600 Subject: [PATCH] Add a random lib and driver --- Cargo.lock | 15 ++ Cargo.toml | 6 +- drivers/randomness_handler/.cargo/config.toml | 2 + drivers/randomness_handler/Cargo.toml | 10 ++ drivers/randomness_handler/src/main.rs | 12 ++ libraries/rand/Cargo.toml | 9 ++ libraries/rand/src/lib.rs | 140 ++++++++++++++++++ 7 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 drivers/randomness_handler/.cargo/config.toml create mode 100644 drivers/randomness_handler/Cargo.toml create mode 100644 drivers/randomness_handler/src/main.rs create mode 100644 libraries/rand/Cargo.toml create mode 100644 libraries/rand/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index de202d4..ba65ca0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -186,6 +186,21 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.1.0" +dependencies = [ + "versioning", +] + +[[package]] +name = "randomness_handler" +version = "0.1.0" +dependencies = [ + "rand", + "versioning", +] + [[package]] name = "ron" version = "0.7.1" diff --git a/Cargo.toml b/Cargo.toml index b227d42..8fb0b3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "drivers/basic_driver", + "drivers/randomness_handler", "drivers/graphics/derelict_microarchitecture", "drivers/graphics/ground", "drivers/graphics/novideo", @@ -10,11 +11,12 @@ members = [ "libraries/able_graphics_library", "libraries/clparse", "libraries/locale-maxima", + "libraries/rand", + "libraries/table", "libraries/tar", + "libraries/time", "libraries/trash_manifest", "libraries/versioning", - "libraries/table", - "libraries/time", "programs/delete", "programs/list", diff --git a/drivers/randomness_handler/.cargo/config.toml b/drivers/randomness_handler/.cargo/config.toml new file mode 100644 index 0000000..f4e8c00 --- /dev/null +++ b/drivers/randomness_handler/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "wasm32-unknown-unknown" diff --git a/drivers/randomness_handler/Cargo.toml b/drivers/randomness_handler/Cargo.toml new file mode 100644 index 0000000..1189c8c --- /dev/null +++ b/drivers/randomness_handler/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "randomness_handler" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +versioning = { path = "../../libraries/versioning" } +rand = { path = "../../libraries/rand" } diff --git a/drivers/randomness_handler/src/main.rs b/drivers/randomness_handler/src/main.rs new file mode 100644 index 0000000..964141f --- /dev/null +++ b/drivers/randomness_handler/src/main.rs @@ -0,0 +1,12 @@ +#![no_std] +#![no_main] +use rand::Csprng; +use versioning::Version; +pub const VERSION: Version = Version::new(0, 1, 0); + +#[no_mangle] +fn start() { + let mut inner_rng = Csprng::new(); + inner_rng.mix_in_data(&[8, 9, 10, 23, 2, 3]); + let _ran_u8 = inner_rng.get_random_u8(); +} diff --git a/libraries/rand/Cargo.toml b/libraries/rand/Cargo.toml new file mode 100644 index 0000000..775a604 --- /dev/null +++ b/libraries/rand/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rand" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +versioning = { path = "../versioning" } diff --git a/libraries/rand/src/lib.rs b/libraries/rand/src/lib.rs new file mode 100644 index 0000000..4d79371 --- /dev/null +++ b/libraries/rand/src/lib.rs @@ -0,0 +1,140 @@ +#![no_std] + +use versioning::Version; +pub const VERSION: Version = Version::new(0, 1, 0); + +pub struct Csprng { + state: [u8; 256], + dirty: bool, +} + +impl Csprng { + pub fn new() -> Self { + // Initialize the state with a seed. + let mut state = [0; 256]; + for i in 0..256 { + state[i] = 0; + } + + Self { + state, + dirty: false, + } + } + + pub fn mix_in_data(&mut self, data: &[u8]) { + self.dirty = true; + + // TODO: Mix the data into the state using a secure hashing function. + + // Mix the data into the state using a simple hash function. + let mut result = [0; 256]; + for (i, byte) in data.iter().enumerate() { + result[i % 256] ^= byte; + } + + // XOR the result of the hashing function with the current state. + for i in 0..256 { + self.state[i] ^= result[i]; + } + } +} + +impl Csprng { + pub fn get_random_u8(&mut self) -> Result { + self.dirty = true; + let random_bytes = self.state[0]; + Ok(random_bytes) + } + + pub fn get_random_u16(&mut self) -> Result { + self.dirty = true; + + let random_byte_1 = self.state[0]; + let random_byte_2 = self.state[1]; + + let random_number = u16::from_le_bytes([random_byte_1, random_byte_2]); + Ok(random_number) + } + + pub fn get_random_u32(&mut self) -> Result { + self.dirty = true; + + // Generate a random number using the current state of the CSPRNG. + let random_byte_1 = self.state[0]; + let random_byte_2 = self.state[1]; + let random_byte_3 = self.state[2]; + let random_byte_4 = self.state[3]; + + let random_number = + u32::from_le_bytes([random_byte_1, random_byte_2, random_byte_3, random_byte_4]); + Ok(random_number) + } + + pub fn get_random_u64(&mut self) -> Result { + self.dirty = true; + + // Generate a random number using the current state of the CSPRNG. + let random_byte_1 = self.state[0]; + let random_byte_2 = self.state[1]; + let random_byte_3 = self.state[2]; + let random_byte_4 = self.state[3]; + let random_byte_5 = self.state[4]; + let random_byte_6 = self.state[5]; + let random_byte_7 = self.state[6]; + let random_byte_8 = self.state[7]; + + let random_number = u64::from_le_bytes([ + random_byte_1, + random_byte_2, + random_byte_3, + random_byte_4, + random_byte_5, + random_byte_6, + random_byte_7, + random_byte_8, + ]); + Ok(random_number) + } + + pub fn get_random_u128(&mut self) -> Result { + let random_byte_1 = self.state[0]; + let random_byte_2 = self.state[1]; + let random_byte_3 = self.state[2]; + let random_byte_4 = self.state[3]; + let random_byte_5 = self.state[4]; + let random_byte_6 = self.state[5]; + let random_byte_7 = self.state[6]; + let random_byte_8 = self.state[8]; + let random_byte_9 = self.state[9]; + let random_byte_10 = self.state[10]; + let random_byte_11 = self.state[11]; + let random_byte_12 = self.state[12]; + let random_byte_15 = self.state[13]; + let random_byte_16 = self.state[14]; + let random_byte_17 = self.state[15]; + + let random_number = u128::from_le_bytes([ + random_byte_1, + random_byte_2, + random_byte_3, + random_byte_4, + random_byte_5, + random_byte_6, + random_byte_7, + random_byte_8, + random_byte_8, + random_byte_9, + random_byte_10, + random_byte_11, + random_byte_12, + random_byte_15, + random_byte_16, + random_byte_17, + ]); + + Ok(random_number) + } +} + +pub enum RandError {}