From fe0e7d7639d46e2502a9bd699353ab01069d1362 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sun, 30 Jan 2022 18:11:54 -0500 Subject: [PATCH] Add getrandom impl using ctru_sys bindings --- Cargo.toml | 1 + src/lib.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index dc453fc..76e70da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ license = "MIT/Apache 2.0" edition = "2018" [dependencies] +ctru-sys = { git = "https://github.com/Meziu/ctru-rs.git" } libc = "0.2.116" diff --git a/src/lib.rs b/src/lib.rs index 7783976..0e7db60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,3 +67,19 @@ unsafe extern "C" fn clock_gettime( retval } + +#[no_mangle] +unsafe extern "C" fn getrandom( + buf: *mut libc::c_void, + buflen: libc::size_t, + _flags: libc::c_uint, +) -> libc::ssize_t { + let ret = ctru_sys::psInit(); + if ret != 0 { + return ret.try_into().unwrap(); + } + + ctru_sys::PS_GenerateRandomBytes(buf, buflen.try_into().unwrap()) + .try_into() + .unwrap() +}