From 8ed7c97a5dc374b16c92bc1231c154f4bf64365e Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Sat, 5 Feb 2022 15:51:28 +0100 Subject: [PATCH] Simple functions added to Ps and HashMap example --- ctru-rs/examples/hashmaps.rs | 39 +++++++++++++++++++++++++ ctru-rs/src/services/ps.rs | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 ctru-rs/examples/hashmaps.rs diff --git a/ctru-rs/examples/hashmaps.rs b/ctru-rs/examples/hashmaps.rs new file mode 100644 index 0000000..d0aab20 --- /dev/null +++ b/ctru-rs/examples/hashmaps.rs @@ -0,0 +1,39 @@ +use ctru::console::Console; +use ctru::gfx::Gfx; +use ctru::services::apt::Apt; +use ctru::services::hid::{Hid, KeyPad}; +use ctru::services::ps::Ps; + +fn main() { + // Initialize services + ctru::init(); + let apt = Apt::init().unwrap(); + let hid = Hid::init().unwrap(); + let gfx = Gfx::default(); + let _console = Console::init(gfx.top_screen.borrow_mut()); + + // HashMaps generate hashes thanks to the 3DS' criptografically secure generator. + // Sadly, this generator is only active when activating the `Ps` service. + // To do this, we have to make sure the `Ps` service handle is alive for the whole + // run time (or at least, when `HashMaps` are used). + // Not having a living `Ps` instance when using `HashMap`s results in a panic + let _ps = Ps::init().unwrap(); + + let mut map = std::collections::HashMap::new(); + map.insert("A Key!", 102); + map.insert("Another key?", 543); + map.remove("A Key!"); + + println!("{:#?}", map); + + while apt.main_loop() { + gfx.flush_buffers(); + gfx.swap_buffers(); + gfx.wait_for_vblank(); + + hid.scan_input(); + if hid.keys_down().contains(KeyPad::KEY_START) { + break; + } + } +} diff --git a/ctru-rs/src/services/ps.rs b/ctru-rs/src/services/ps.rs index 5e9adfa..5b1ca27 100644 --- a/ctru-rs/src/services/ps.rs +++ b/ctru-rs/src/services/ps.rs @@ -8,6 +8,30 @@ #[non_exhaustive] pub struct Ps; +#[repr(u32)] +pub enum AESAlgorithm { + CbcEnc, + CbcDec, + CtrEnc, + CtrDec, + CcmEnc, + CcmDec, +} + +#[repr(u32)] +pub enum AESKeyType { + Keyslot0D, + Keyslot2D, + Keyslot31, + Keyslot38, + Keyslot32, + Keyslot39Dlp, + Keyslot2E, + KeyslotInvalid, + Keyslot36, + Keyslot39Nfc, +} + impl Ps { /// Initialize the PS module. pub fn init() -> crate::Result { @@ -18,6 +42,38 @@ impl Ps { Ok(Self) } } + + pub fn local_friend_code_seed(&self) -> crate::Result { + let mut seed: u64 = 0; + + let r = unsafe { ctru_sys::PS_GetLocalFriendCodeSeed(&mut seed) }; + if r < 0 { + Err(r.into()) + } else { + Ok(seed) + } + } + + pub fn device_id(&self) -> crate::Result { + let mut id: u32 = 0; + + let r = unsafe { ctru_sys::PS_GetDeviceId(&mut id) }; + if r < 0 { + Err(r.into()) + } else { + Ok(id) + } + } + + pub fn generate_random_bytes(&self, out: &mut [u8]) -> crate::Result<()> { + let r = + unsafe { ctru_sys::PS_GenerateRandomBytes(out as *mut _ as *mut _, out.len() as u32) }; + if r < 0 { + Err(r.into()) + } else { + Ok(()) + } + } } impl Drop for Ps {