Skip to content
Closed
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
42 changes: 8 additions & 34 deletions hermit-abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ pub mod errno;
pub mod tcplistener;
pub mod tcpstream;

use core::mem::MaybeUninit;

use core::ffi::{c_int, c_void};

/// A thread handle type
Expand Down Expand Up @@ -453,16 +451,6 @@ extern "C" {
#[link_name = "sys_network_init"]
pub fn network_init() -> i32;

/// The function computes a sequence of pseudo-random integers
/// in the range of 0 to RAND_MAX
#[link_name = "sys_rand"]
pub fn rand() -> u32;

/// The function sets its argument as the seed for a new sequence
/// of pseudo-random numbers to be returned by `rand`
#[link_name = "sys_srand"]
pub fn srand(seed: u32);

/// Add current task to the queue of blocked tasks. After calling `block_current_task`,
/// call `yield_now` to switch to another task.
#[link_name = "sys_block_current_task"]
Expand Down Expand Up @@ -497,6 +485,14 @@ extern "C" {
#[link_name = "sys_read"]
pub fn read(fd: i32, buf: *mut u8, len: usize) -> isize;

/// Fill `len` bytes in `buf` with cryptographically secure random data.
///
/// Returns either the number of bytes written to buf (a positive value) or
/// * `-EINVAL` if `flags` contains unknown flags.
/// * `-ENOSYS` if the system does not support random data generation.
#[link_name = "sys_read_entropy"]
pub fn read_entropy(buf: *mut u8, len: usize, flags: u32) -> isize;

/// receive() a message from a socket
#[link_name = "sys_recv"]
pub fn recv(socket: i32, buf: *mut u8, len: usize, flags: i32) -> isize;
Expand Down Expand Up @@ -599,32 +595,10 @@ extern "C" {
res: *mut *mut addrinfo,
) -> i32;

fn sys_secure_rand32(value: *mut u32) -> i32;
fn sys_secure_rand64(value: *mut u64) -> i32;
fn sys_get_priority() -> u8;
fn sys_set_priority(tid: Tid, prio: u8);
}

/// Create a cryptographicly secure 32bit random number with the support of
/// the underlying hardware. If the required hardware isn't available,
/// the function returns `None`.
#[inline(always)]
pub unsafe fn secure_rand32() -> Option<u32> {
let mut rand = MaybeUninit::uninit();
let res = sys_secure_rand32(rand.as_mut_ptr());
(res == 0).then(|| rand.assume_init())
}

/// Create a cryptographicly secure 64bit random number with the support of
/// the underlying hardware. If the required hardware isn't available,
/// the function returns `None`.
#[inline(always)]
pub unsafe fn secure_rand64() -> Option<u64> {
let mut rand = MaybeUninit::uninit();
let res = sys_secure_rand64(rand.as_mut_ptr());
(res == 0).then(|| rand.assume_init())
}

/// Determine the priority of the current thread
#[inline(always)]
pub unsafe fn get_priority() -> Priority {
Expand Down