Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion host/src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ use crate::{wasi_random, WasiCtx};

#[async_trait::async_trait]
impl wasi_random::WasiRandom for WasiCtx {
async fn getrandom(&mut self, len: u32) -> anyhow::Result<Vec<u8>> {
async fn get_random_bytes(&mut self, len: u32) -> anyhow::Result<Vec<u8>> {
Ok((&mut self.random)
.sample_iter(Standard)
.take(len as usize)
.collect())
}

async fn get_random_u64(&mut self) -> anyhow::Result<u64> {
Ok((&mut self.random).sample(Standard))
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ pub unsafe extern "C" fn random_get(buf: *mut u8, buf_len: Size) -> Errno {
state.register_buffer(buf, buf_len);

assert_eq!(buf_len as u32 as Size, buf_len);
let result = wasi_random::getrandom(buf_len as u32);
let result = wasi_random::get_random_bytes(buf_len as u32);
assert_eq!(result.as_ptr(), buf);

// The returned buffer's memory was allocated in `buf`, so don't separately
Expand Down
11 changes: 10 additions & 1 deletion wit/wasi.wit
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,16 @@ interface wasi-random {
///
/// Deterministic environments must omit this function, rather than
/// implementing it with deterministic data.
getrandom: func(len: u32) -> list<u8>
get-random-bytes: func(len: u32) -> list<u8>

/// Return a random `u64` value.
///
/// This function must produce data from an adaquately seeded CSPRNG, so it
/// must not block, and the returned data is always unpredictable.
///
/// Deterministic environments must omit this function, rather than
/// implementing it with deterministic data.
get-random-u64: func() -> u64

/// A value containing 128 random bits.
///
Expand Down