//! The per-session stream key handed to the host in `launch` / `resume` (`docs/PROTOCOL.md` 1.4, 7). use core::fmt; use crate::{RandomSource, Result}; /// Length of `rikey` in bytes (AES-128). pub const RIKEY_LEN: usize = 16; /// The stream key pair of one session: `rikey` (AES-128 key for control, audio and video) and `rikeyid` /// (feeds the audio IV). /// /// A fresh pair is generated for every launch and resume, because the GCM nonce counters restart per session. /// `Debug` never prints the key. #[derive(Clone, PartialEq, Eq)] pub struct SessionKeys { /// AES-128 key, sent hex-encoded as the `rikey` launch parameter. pub rikey: [u8; RIKEY_LEN], /// Key id, sent as the decimal `rikeyid` launch parameter. pub rikeyid: u32, } impl SessionKeys { /// Draws a fresh key and key id from `rng`. /// /// # Errors /// None today; the `Result` leaves room for a fallible source. pub fn generate(rng: &mut dyn RandomSource) -> Result { let mut rikey = [0u8; RIKEY_LEN]; rng.fill(&mut rikey); let mut id = [0u8; 4]; rng.fill(&mut id); Ok(Self { rikey, rikeyid: u32::from_le_bytes(id) }) } } impl fmt::Debug for SessionKeys { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("SessionKeys").field("rikey", &"").field("rikeyid", &self.rikeyid).finish() } }