Debian Codegraph @

debian/rust-vendor/getrandom/src/backends/getentropy.rs

raw
1 //! Implementation using getentropy(2)
2 //!
3 //! When porting to a new target, ensure that its implementation follows the
4 //! POSIX conventions from
5 //! <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getentropy.html>.
6 //!
7 //! Available since:
8 //! - macOS 10.12
9 //! - OpenBSD 5.6
10 //! - Emscripten 2.0.5
11 //! - vita newlib since Dec 2021
12 //!
13 //! For these targets, we use getentropy(2) because getrandom(2) doesn't exist.
14 use crate::Error;
15 use core::{ffi::c_void, mem::MaybeUninit};
16
17 pub use crate::util::{inner_u32, inner_u64};
18
19 #[path = "../utils/get_errno.rs"]
20 mod utils;
21
22 #[inline]
23 pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
24 // https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/limits.h.html
25 // says `GETENTROPY_MAX` is at least 256.
26 const GETENTROPY_MAX: usize = 256;
27
28 for chunk in dest.chunks_mut(GETENTROPY_MAX) {
29 let ret = unsafe { libc::getentropy(chunk.as_mut_ptr().cast::<c_void>(), chunk.len()) };
30 match ret {
31 0 => continue,
32 -1 => return Err(Error::from_errno(utils::get_errno())),
33 _ => return Err(Error::UNEXPECTED),
34 }
35 }
36 Ok(())
37 }