interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi / src / proto / rng.rs

65 lines · 1945 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! `Rng` protocol.
4
5use crate::proto::unsafe_protocol;
6use crate::{Result, Status, StatusExt};
7use core::ptr;
8use uefi_raw::protocol::rng::RngProtocol;
9
10pub use uefi_raw::protocol::rng::RngAlgorithmType;
11
12/// Random Number Generator [`Protocol`] (RNG).
13///
14/// [`Protocol`]: uefi::proto::Protocol
15#[derive(Debug)]
16#[repr(transparent)]
17#[unsafe_protocol(RngProtocol::GUID)]
18pub struct Rng(RngProtocol);
19
20impl Rng {
21 /// Returns information about the random number generation implementation.
22 pub fn get_info<'buf>(
23 &mut self,
24 algorithm_list: &'buf mut [RngAlgorithmType],
25 ) -> Result<&'buf [RngAlgorithmType], Option<usize>> {
26 let mut algorithm_list_size = size_of_val(algorithm_list);
27
28 // SAFETY: The memory is valid.
29 unsafe {
30 (self.0.get_info)(
31 &mut self.0,
32 &mut algorithm_list_size,
33 algorithm_list.as_mut_ptr(),
34 )
35 .to_result_with(
36 || {
37 let len = algorithm_list_size / size_of::<RngAlgorithmType>();
38 &algorithm_list[..len]
39 },
40 |status| {
41 if status == Status::BUFFER_TOO_SMALL {
42 Some(algorithm_list_size)
43 } else {
44 None
45 }
46 },
47 )
48 }
49 }
50
51 /// Returns the next set of random numbers
52 pub fn get_rng(&mut self, algorithm: Option<RngAlgorithmType>, buffer: &mut [u8]) -> Result {
53 let buffer_length = buffer.len();
54
55 let algo = match algorithm.as_ref() {
56 None => ptr::null(),
57 Some(algo) => ptr::from_ref(algo),
58 };
59
60 // SAFETY: The memory is valid.
61 unsafe {
62 (self.0.get_rng)(&mut self.0, algo, buffer_length, buffer.as_mut_ptr()).to_result()
63 }
64 }
65}