aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/sys/unix/rand.rs
diff options
context:
space:
mode:
authorValentin <[email protected]>2018-06-15 18:57:24 +0200
committerFenrirWolf <[email protected]>2018-06-15 10:57:24 -0600
commitf2a90174bb36b9ad528e863ab34c02ebce002b02 (patch)
tree959e8d67883d3a89e179b3549b1f30d28e51a87c /ctr-std/src/sys/unix/rand.rs
parentMerge pull request #68 from linouxis9/master (diff)
downloadarchived-ctru-rs-f2a90174bb36b9ad528e863ab34c02ebce002b02.tar.xz
archived-ctru-rs-f2a90174bb36b9ad528e863ab34c02ebce002b02.zip
Update for latest nightly 2018-06-09 (#70)
* Update for latest nightly 2018-06-09 * We now have a proper horizon os and sys modules in libstd
Diffstat (limited to 'ctr-std/src/sys/unix/rand.rs')
-rw-r--r--ctr-std/src/sys/unix/rand.rs200
1 files changed, 187 insertions, 13 deletions
diff --git a/ctr-std/src/sys/unix/rand.rs b/ctr-std/src/sys/unix/rand.rs
index 4500c9d..caa1894 100644
--- a/ctr-std/src/sys/unix/rand.rs
+++ b/ctr-std/src/sys/unix/rand.rs
@@ -21,23 +21,197 @@ pub fn hashmap_random_keys() -> (u64, u64) {
return v
}
+#[cfg(all(unix,
+ not(target_os = "ios"),
+ not(target_os = "openbsd"),
+ not(target_os = "freebsd"),
+ not(target_os = "fuchsia")))]
mod imp {
- use libctru;
+ use fs::File;
+ use io::Read;
+ use libc;
+ use sys::os::errno;
+
+ #[cfg(any(target_os = "linux", target_os = "android"))]
+ fn getrandom(buf: &mut [u8]) -> libc::c_long {
+ unsafe {
+ libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr(), buf.len(), libc::GRND_NONBLOCK)
+ }
+ }
+
+ #[cfg(not(any(target_os = "linux", target_os = "android")))]
+ fn getrandom(_buf: &mut [u8]) -> libc::c_long { -1 }
+
+ fn getrandom_fill_bytes(v: &mut [u8]) -> bool {
+ let mut read = 0;
+ while read < v.len() {
+ let result = getrandom(&mut v[read..]);
+ if result == -1 {
+ let err = errno() as libc::c_int;
+ if err == libc::EINTR {
+ continue;
+ } else if err == libc::EAGAIN {
+ return false
+ } else {
+ panic!("unexpected getrandom error: {}", err);
+ }
+ } else {
+ read += result as usize;
+ }
+ }
+
+ return true
+ }
+
+ #[cfg(any(target_os = "linux", target_os = "android"))]
+ fn is_getrandom_available() -> bool {
+ use io;
+ use sync::atomic::{AtomicBool, Ordering};
+ use sync::Once;
+
+ static CHECKER: Once = Once::new();
+ static AVAILABLE: AtomicBool = AtomicBool::new(false);
+
+ CHECKER.call_once(|| {
+ let mut buf: [u8; 0] = [];
+ let result = getrandom(&mut buf);
+ let available = if result == -1 {
+ let err = io::Error::last_os_error().raw_os_error();
+ err != Some(libc::ENOSYS)
+ } else {
+ true
+ };
+ AVAILABLE.store(available, Ordering::Relaxed);
+ });
+
+ AVAILABLE.load(Ordering::Relaxed)
+ }
+
+ #[cfg(not(any(target_os = "linux", target_os = "android")))]
+ fn is_getrandom_available() -> bool { false }
+
+ pub fn fill_bytes(v: &mut [u8]) {
+ // getrandom_fill_bytes here can fail if getrandom() returns EAGAIN,
+ // meaning it would have blocked because the non-blocking pool (urandom)
+ // has not initialized in the kernel yet due to a lack of entropy the
+ // fallback we do here is to avoid blocking applications which could
+ // depend on this call without ever knowing they do and don't have a
+ // work around. The PRNG of /dev/urandom will still be used but not
+ // over a completely full entropy pool
+ if is_getrandom_available() && getrandom_fill_bytes(v) {
+ return
+ }
+
+ let mut file = File::open("/dev/urandom")
+ .expect("failed to open /dev/urandom");
+ file.read_exact(v).expect("failed to read /dev/urandom");
+ }
+}
+
+#[cfg(target_os = "openbsd")]
+mod imp {
+ use libc;
+ use sys::os::errno;
+
+ pub fn fill_bytes(v: &mut [u8]) {
+ // getentropy(2) permits a maximum buffer size of 256 bytes
+ for s in v.chunks_mut(256) {
+ let ret = unsafe {
+ libc::getentropy(s.as_mut_ptr() as *mut libc::c_void, s.len())
+ };
+ if ret == -1 {
+ panic!("unexpected getentropy error: {}", errno());
+ }
+ }
+ }
+}
+
+#[cfg(target_os = "ios")]
+mod imp {
+ use io;
+ use libc::{c_int, size_t};
+ use ptr;
+
+ enum SecRandom {}
+
+ #[allow(non_upper_case_globals)]
+ const kSecRandomDefault: *const SecRandom = ptr::null();
+
+ extern {
+ fn SecRandomCopyBytes(rnd: *const SecRandom,
+ count: size_t,
+ bytes: *mut u8) -> c_int;
+ }
+
+ pub fn fill_bytes(v: &mut [u8]) {
+ let ret = unsafe {
+ SecRandomCopyBytes(kSecRandomDefault,
+ v.len(),
+ v.as_mut_ptr())
+ };
+ if ret == -1 {
+ panic!("couldn't generate random bytes: {}",
+ io::Error::last_os_error());
+ }
+ }
+}
+
+#[cfg(target_os = "freebsd")]
+mod imp {
+ use libc;
+ use ptr;
pub fn fill_bytes(v: &mut [u8]) {
+ let mib = [libc::CTL_KERN, libc::KERN_ARND];
+ // kern.arandom permits a maximum buffer size of 256 bytes
+ for s in v.chunks_mut(256) {
+ let mut s_len = s.len();
+ let ret = unsafe {
+ libc::sysctl(mib.as_ptr(), mib.len() as libc::c_uint,
+ s.as_mut_ptr() as *mut _, &mut s_len,
+ ptr::null(), 0)
+ };
+ if ret == -1 || s_len != s.len() {
+ panic!("kern.arandom sysctl failed! (returned {}, s.len() {}, oldlenp {})",
+ ret, s.len(), s_len);
+ }
+ }
+ }
+}
+
+#[cfg(target_os = "fuchsia")]
+mod imp {
+ #[link(name = "zircon")]
+ extern {
+ fn zx_cprng_draw(buffer: *mut u8, len: usize, actual: *mut usize) -> i32;
+ }
+
+ fn getrandom(buf: &mut [u8]) -> Result<usize, i32> {
unsafe {
- // Initializing and de-initializing the sslC subsystem every time
- // we initialize a hashmap is pretty dumb, but I can't think of a
- // better method at the moment.
- //
- // lazy_static won't work because
- // destructors (for closing the subsystem on exit) won't run.
- //
- // Perhaps overriding __appInit() and __appExit() will work,
- // but that's an experiment for another time.
- libctru::sslcInit(0);
- libctru::sslcGenerateRandomData(v.as_ptr() as _, v.len() as u32);
- libctru::sslcExit();
+ let mut actual = 0;
+ let status = zx_cprng_draw(buf.as_mut_ptr(), buf.len(), &mut actual);
+ if status == 0 {
+ Ok(actual)
+ } else {
+ Err(status)
+ }
+ }
+ }
+
+ pub fn fill_bytes(v: &mut [u8]) {
+ let mut buf = v;
+ while !buf.is_empty() {
+ let ret = getrandom(buf);
+ match ret {
+ Err(err) => {
+ panic!("kernel zx_cprng_draw call failed! (returned {}, buf.len() {})",
+ err, buf.len())
+ }
+ Ok(actual) => {
+ let move_buf = buf;
+ buf = &mut move_buf[(actual as usize)..];
+ }
+ }
}
}
}