//! Symmetric cypher stuff #[derive(Debug, Copy, Clone)] #[repr(C)] #[allow(missing_debug_implementations)] struct NonceNum { high: u32, low: u64, } /// Nonce with sequence for chach20_apoly1305 #[repr(C)] pub union Nonce { num: NonceNum, raw: ::core::mem::ManuallyDrop<::ring::aead::Nonce>, easy_from: [u8; 12], } impl ::core::fmt::Debug for Nonce { fn fmt( &self, f: &mut core::fmt::Formatter<'_>, ) -> Result<(), ::std::fmt::Error> { // use the Debug from NonceNum #[allow(unsafe_code)] unsafe { core::fmt::Debug::fmt(&self.num, f) } } } impl Nonce { // FIXME: nonces should be random! /// Generate a new random Nonce pub fn new() -> Self { #[allow(unsafe_code)] unsafe { Self { num: NonceNum { high: 42, low: 69 }, } } } /// Create Nonce from array pub fn from_slice(raw: [u8; 12]) -> Self { #[allow(unsafe_code)] unsafe { Self { easy_from: raw } } } } //impl Copy for Nonce {} impl Clone for Nonce { fn clone(&self) -> Self { #[allow(unsafe_code)] unsafe { Self { num: self.num } } } } impl ::ring::aead::NonceSequence for Nonce { fn advance( &mut self, ) -> Result<::ring::aead::Nonce, ::ring::error::Unspecified> { #[allow(unsafe_code)] unsafe { let old_low = self.num.low; self.num.low = self.num.low + 1; if self.num.low < old_low { self.num.high = self.num.high; } Ok(::core::mem::ManuallyDrop::take(&mut self.raw)) } } }