From 9d3a42c535a08db8ac1e7ce28ed5a53c454bda94 Mon Sep 17 00:00:00 2001 From: Aaron Kaiser Date: Sun, 16 Mar 2025 13:42:12 +0100 Subject: [PATCH] feat: add getrandom implementation --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/lib.rs | 9 +++++++++ 3 files changed, 43 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 1abbba1..700d395 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,6 +69,18 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi", + "windows-targets", +] + [[package]] name = "glob" version = "0.3.2" @@ -129,6 +141,8 @@ name = "mlkem-native-rs" version = "0.1.0" dependencies = [ "bindgen", + "getrandom", + "libc", "make-cmd", ] @@ -228,6 +242,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "windows-targets" version = "0.52.6" @@ -291,3 +314,12 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] diff --git a/Cargo.toml b/Cargo.toml index 3c9e9db..ed03ba2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,8 @@ version = "0.1.0" edition = "2021" [dependencies] +getrandom = "0.3.1" +libc = "0.2.171" [build-dependencies] bindgen = "0.71.1" diff --git a/src/lib.rs b/src/lib.rs index a38a13a..5a29265 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,13 @@ #![allow(non_camel_case_types)] #![allow(non_snake_case)] +use std::slice; + include!(concat!(env!("OUT_DIR"), "/bindings.rs")); + +#[no_mangle] +pub unsafe extern "C" fn randombytes(buf: *mut u8, len: libc::size_t) -> libc::c_int { + let buf = slice::from_raw_parts_mut(buf, len); + getrandom::fill(buf).expect("RNG Failed"); + 0 +}