l;kdsfak;jlfdjsl;fdssa

This commit is contained in:
Skye 2023-01-05 19:13:49 +09:00
commit 7ef0152517
Signed by: me
GPG key ID: 0104BC05F41B77B8
7 changed files with 220 additions and 0 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
/Cargo.lock
.direnv

23
Cargo.toml Normal file
View file

@ -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" }

94
flake.lock Normal file
View file

@ -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
}

25
flake.nix Normal file
View file

@ -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" ];
}))
];
};
});
}

51
src/lib.rs Normal file
View file

@ -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::<FfiUwurandom>()
}
#[no_mangle]
pub extern "C" fn uwurandom_align() -> usize {
core::mem::align_of::<FfiUwurandom>()
}
/// # 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 _
}

23
uwurandom_cffi.h Normal file
View file

@ -0,0 +1,23 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
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);