commit 7ef01525176a4f029ed2a499f96fbb503cddba1b Author: Skye Date: Thu Jan 5 19:13:49 2023 +0900 l;kdsfak;jlfdjsl;fdssa diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afecdba --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +/Cargo.lock +.direnv \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..00c7e20 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "uwurandom-cffi" +version = "0.1.0" +edition = "2021" + +[lib] +name = "uwurandom_cffi" +# crate-type = ["cdylib"] # Creates dynamic lib +crate-type = ["staticlib"] # Creates static lib + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[profile.dev] +panic = "abort" + +[profile.release] +panic = "abort" +lto = "fat" +strip = "symbols" + +[dependencies] +rand_xoshiro = "0.6.0" +uwurandom-rs = { git = "https://git.skye.vg/me/uwurandom-rs.git" } diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..7b9e7dc --- /dev/null +++ b/flake.lock @@ -0,0 +1,94 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1672525397, + "narHash": "sha256-WASDnyxHKWVrEe0dIzkpH+jzKlCKAk0husv0f/9pyxg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "8ba56d7c0d7490680f2d51ba46a141eca7c46afa", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1665296151, + "narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "14ccaaedd95a488dd7ae142757884d8e125b3363", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1672712534, + "narHash": "sha256-8S0DdMPcbITnlOu0uA81mTo3hgX84wK8S9wS34HEFY4=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "69fb7bf0a8c40e6c4c197fa1816773774c8ac59f", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..4ea6e8a --- /dev/null +++ b/flake.nix @@ -0,0 +1,25 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + rust-overlay.url = "github:oxalica/rust-overlay"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + overlays = [ (import rust-overlay) ]; + pkgs = import nixpkgs { inherit system overlays; }; + in + with pkgs; { + devShells.default = mkShell { + buildInputs = [ + (rust-bin.selectLatestNightlyWith (toolchain: + toolchain.default.override { + extensions = [ "rust-src" ]; + targets = [ "thumbv6m-none-eabi" ]; + })) + ]; + }; + }); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ffd8b20 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,51 @@ +#![no_std] + +use core::panic::PanicInfo; + +#[panic_handler] +fn panic(_panic: &PanicInfo<'_>) -> ! { + loop {} +} + +use rand_xoshiro::rand_core::SeedableRng; + +pub struct FfiUwurandom { + machine: uwurandom_rs::StateMachine, + rng: rand_xoshiro::Xoroshiro128StarStar, +} + +#[no_mangle] +pub extern "C" fn uwurandom_size() -> usize { + core::mem::size_of::() +} + +#[no_mangle] +pub extern "C" fn uwurandom_align() -> usize { + core::mem::align_of::() +} + +/// # Safety +/// +/// `buf` should point to a valid region of memory with the minimum size of `uwurandom_size` and align of `uwurandom_align`. +#[no_mangle] +pub unsafe extern "C" fn create_uwurandom(seed: &[u8; 16], buf: *mut core::ffi::c_void) { + let mut rng = rand_xoshiro::Xoroshiro128StarStar::from_seed(*seed); + core::ptr::write( + buf as *mut _, + FfiUwurandom { + machine: uwurandom_rs::StateMachine::new(&mut rng), + rng, + }, + ) +} + +/// # Safety +/// +/// `uwurandom` should point to memory initialized by `create_uwurandom`. +#[no_mangle] +pub unsafe extern "C" fn step_uwurandom(uwurandom: *mut core::ffi::c_void) -> core::ffi::c_char { + let uwurandom = &mut *(uwurandom as *mut FfiUwurandom); + let (machine, generated) = uwurandom.machine.generate(&mut uwurandom.rng); + uwurandom.machine = machine; + generated as _ +} diff --git a/uwurandom_cffi.h b/uwurandom_cffi.h new file mode 100644 index 0000000..fa9b593 --- /dev/null +++ b/uwurandom_cffi.h @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +uintptr_t uwurandom_size(void); + +uintptr_t uwurandom_align(void); + +/** + * # Safety + * + * `buf` should point to a valid region of memory with the minimum size of `uwurandom_size` and align of `uwurandom_align`. + */ +void create_uwurandom(const uint8_t (*seed)[16], + void *buf); + +/** + * # Safety + * + * `uwurandom` should point to memory initialized by `create_uwurandom`. + */ +char step_uwurandom(void *uwurandom);