From da642186f2356939852ae73b92b0e7d841327d4f Mon Sep 17 00:00:00 2001 From: Karolin Varner Date: Sun, 1 Jun 2025 14:21:48 +0200 Subject: [PATCH] chore: Move timing related thing out of protocol.rs --- rosenpass/src/app_server.rs | 4 +- rosenpass/src/protocol/mod.rs | 6 ++- rosenpass/src/protocol/protocol.rs | 66 ++++++------------------------ rosenpass/src/protocol/timing.rs | 46 +++++++++++++++++++++ rosenpass/tests/poll_example.rs | 6 ++- 5 files changed, 68 insertions(+), 60 deletions(-) create mode 100644 rosenpass/src/protocol/timing.rs diff --git a/rosenpass/src/app_server.rs b/rosenpass/src/app_server.rs index 58fc8ed..55162ad 100644 --- a/rosenpass/src/app_server.rs +++ b/rosenpass/src/app_server.rs @@ -47,7 +47,7 @@ use crate::protocol::BuildCryptoServer; use crate::protocol::HostIdentification; use crate::{ config::Verbosity, - protocol::{CryptoServer, MsgBuf, PeerPtr, SPk, SSk, SymKey, Timing}, + protocol::{timing::Timing, CryptoServer, MsgBuf, PeerPtr, SPk, SSk, SymKey}, }; use rosenpass_util::attempt; use rosenpass_util::b64::B64Display; @@ -1337,7 +1337,7 @@ impl AppServer { break A::SendRetransmission(AppPeerPtr(no)) } Some(C::Sleep(timeout)) => timeout, // No event from crypto-server, do IO - None => crate::protocol::UNENDING, // Crypto server is uninitialized, do IO + None => crate::protocol::timing::UNENDING, // Crypto server is uninitialized, do IO }; // Perform IO (look for a message) diff --git a/rosenpass/src/protocol/mod.rs b/rosenpass/src/protocol/mod.rs index f9ea0f3..9d8608f 100644 --- a/rosenpass/src/protocol/mod.rs +++ b/rosenpass/src/protocol/mod.rs @@ -76,8 +76,10 @@ //! ``` mod build_crypto_server; +pub use build_crypto_server::*; + +pub mod timing; + #[allow(clippy::module_inception)] mod protocol; - -pub use build_crypto_server::*; pub use protocol::*; diff --git a/rosenpass/src/protocol/protocol.rs b/rosenpass/src/protocol/protocol.rs index 4fce927..f82e6d0 100644 --- a/rosenpass/src/protocol/protocol.rs +++ b/rosenpass/src/protocol/protocol.rs @@ -36,26 +36,10 @@ use rosenpass_util::mem::DiscardResultExt; use rosenpass_util::{cat, mem::cpy_min, time::Timebase}; use zerocopy::{AsBytes, FromBytes, Ref}; +use super::timing::{has_happened, Timing, BCE, UNENDING}; + // CONSTANTS & SETTINGS ////////////////////////// -/// A type for time, e.g. for backoff before re-tries -pub type Timing = f64; - -/// Magic time stamp to indicate some object is ancient; "Before Common Era" -/// -/// This is for instance used as a magic time stamp indicating age when some -/// cryptographic object certainly needs to be refreshed. -/// -/// Using this instead of Timing::MIN or Timing::INFINITY to avoid floating -/// point math weirdness. -pub const BCE: Timing = -3600.0 * 24.0 * 356.0 * 10_000.0; - -/// Magic time stamp to indicate that some process is not time-limited -/// -/// Actually it's eight hours; This is intentional to avoid weirdness -/// regarding unexpectedly large numbers in system APIs as this is < i16::MAX -pub const UNENDING: Timing = 3600.0 * 8.0; - /// Time after which the responder attempts to rekey the session /// /// From the wireguard paper: rekey every two minutes, @@ -122,31 +106,6 @@ pub const EVENT_GRACE: Timing = 0.0025; // UTILITY FUNCTIONS ///////////////////////////// -/// An even `ev` has happened relative to a point in time `now` -/// if the `ev` does not lie in the future relative to now. -/// -/// An event lies in the future relative to `now` if -/// does not lie in the past or present. -/// -/// An event `ev` lies in the past if `ev < now`. It lies in the -/// present if the absolute difference between `ev` and `now` is -/// smaller than [EVENT_GRACE]. -/// -/// Think of this as `ev <= now` for with [EVENT_GRACE] applied. -/// -/// # Examples -/// -/// ``` -/// use rosenpass::protocol::{has_happened, EVENT_GRACE}; -/// assert!(has_happened(EVENT_GRACE * -1.0, 0.0)); -/// assert!(has_happened(0.0, 0.0)); -/// assert!(has_happened(EVENT_GRACE * 0.999, 0.0)); -/// assert!(!has_happened(EVENT_GRACE * 1.001, 0.0)); -/// ``` -pub fn has_happened(ev: Timing, now: Timing) -> bool { - (ev - now) < EVENT_GRACE -} - // DATA STRUCTURES & BASIC TRAITS & ACCESSORS //// /// Static public key @@ -274,7 +233,7 @@ pub struct CryptoServer { /// /// ``` /// use rosenpass_util::time::Timebase; -/// use rosenpass::protocol::{BCE, SymKey, CookieStore}; +/// use rosenpass::protocol::{timing::BCE, SymKey, CookieStore}; /// /// rosenpass_secret_memory::secret_policy_try_use_memfd_secrets(); /// @@ -1928,7 +1887,7 @@ impl Mortal for KnownInitConfResponsePtr { /// # Examples /// /// ``` -/// use rosenpass::protocol::{Timing, Mortal, MortalExt, Lifecycle, CryptoServer, ProtocolVersion}; +/// use rosenpass::protocol::{timing::Timing, Mortal, MortalExt, Lifecycle, CryptoServer, ProtocolVersion}; /// use rosenpass::protocol::testutils::{ServerForTesting, time_travel_forward}; /// /// rosenpass_secret_memory::secret_policy_try_use_memfd_secrets(); @@ -2536,7 +2495,7 @@ impl Wait { /// # Examples /// /// ``` - /// use rosenpass::protocol::{Wait, UNENDING}; + /// use rosenpass::protocol::{Wait, timing::UNENDING}; /// /// assert_eq!(Wait::hibernate().0, UNENDING); /// ``` @@ -2550,7 +2509,7 @@ impl Wait { /// # Examples /// /// ``` - /// use rosenpass::protocol::{Wait, UNENDING}; + /// use rosenpass::protocol::{Wait, timing::UNENDING}; /// /// assert_eq!(Wait::immediate_unless(false).0, 0.0); /// assert_eq!(Wait::immediate_unless(true).0, UNENDING); @@ -2568,7 +2527,7 @@ impl Wait { /// # Examples /// /// ``` - /// use rosenpass::protocol::{Wait, UNENDING}; + /// use rosenpass::protocol::{Wait, timing::UNENDING}; /// /// assert_eq!(Wait::or_hibernate(None).0, UNENDING); /// assert_eq!(Wait::or_hibernate(Some(20.0)).0, 20.0); @@ -2585,7 +2544,7 @@ impl Wait { /// # Examples /// /// ``` - /// use rosenpass::protocol::{Wait, UNENDING}; + /// use rosenpass::protocol::{Wait, timing::UNENDING}; /// /// assert_eq!(Wait::or_immediate(None).0, 0.0); /// assert_eq!(Wait::or_immediate(Some(20.0)).0, 20.0); @@ -2602,7 +2561,7 @@ impl Wait { /// # Examples /// /// ``` - /// use rosenpass::protocol::{Wait, UNENDING}; + /// use rosenpass::protocol::{Wait, timing::UNENDING}; /// /// /// assert_eq!(Wait(20.0).and(30.0).0, 30.0); @@ -2674,7 +2633,7 @@ impl PollResult { /// # Examples /// /// ``` - /// use rosenpass::protocol::{PollResult, UNENDING}; + /// use rosenpass::protocol::{PollResult, timing::UNENDING}; /// /// assert!(matches!(PollResult::hibernate(), PollResult::Sleep(UNENDING))); /// ``` @@ -2688,7 +2647,7 @@ impl PollResult { /// # Examples /// /// ``` - /// use rosenpass::protocol::{PollResult, PeerPtr, UNENDING}; + /// use rosenpass::protocol::{PollResult, PeerPtr, timing::UNENDING}; /// /// let p = PeerPtr(0); /// @@ -2943,7 +2902,7 @@ impl PollResult { /// # Examples /// /// ``` -/// use rosenpass::protocol::{begin_poll, PollResult, UNENDING}; +/// use rosenpass::protocol::{begin_poll, PollResult, timing::UNENDING}; /// /// assert!(matches!(begin_poll(), PollResult::Sleep(UNENDING))); /// ``` @@ -4763,7 +4722,6 @@ mod test { poll(&mut b)?; check_retransmission(&mut b, &ic1, &ic1_broken, &rc1)?; } - // We can even validate that the data is coming out of the cache by changing the cache // to use our broken messages. It does not matter that these messages are cryptographically // broken since we insert them manually into the cache diff --git a/rosenpass/src/protocol/timing.rs b/rosenpass/src/protocol/timing.rs new file mode 100644 index 0000000..221d794 --- /dev/null +++ b/rosenpass/src/protocol/timing.rs @@ -0,0 +1,46 @@ +//! Time-keeping related utilities for the Rosenpass protocol + +use super::EVENT_GRACE; + +/// A type for time, e.g. for backoff before re-tries +pub type Timing = f64; + +/// Magic time stamp to indicate some object is ancient; "Before Common Era" +/// +/// This is for instance used as a magic time stamp indicating age when some +/// cryptographic object certainly needs to be refreshed. +/// +/// Using this instead of Timing::MIN or Timing::INFINITY to avoid floating +/// point math weirdness. +pub const BCE: Timing = -3600.0 * 24.0 * 356.0 * 10_000.0; + +/// Magic time stamp to indicate that some process is not time-limited +/// +/// Actually it's eight hours; This is intentional to avoid weirdness +/// regarding unexpectedly large numbers in system APIs as this is < i16::MAX +pub const UNENDING: Timing = 3600.0 * 8.0; + +/// An even `ev` has happened relative to a point in time `now` +/// if the `ev` does not lie in the future relative to now. +/// +/// An event lies in the future relative to `now` if +/// does not lie in the past or present. +/// +/// An event `ev` lies in the past if `ev < now`. It lies in the +/// present if the absolute difference between `ev` and `now` is +/// smaller than [EVENT_GRACE]. +/// +/// Think of this as `ev <= now` for with [EVENT_GRACE] applied. +/// +/// # Examples +/// +/// ``` +/// use rosenpass::protocol::{timing::has_happened, EVENT_GRACE}; +/// assert!(has_happened(EVENT_GRACE * -1.0, 0.0)); +/// assert!(has_happened(0.0, 0.0)); +/// assert!(has_happened(EVENT_GRACE * 0.999, 0.0)); +/// assert!(!has_happened(EVENT_GRACE * 1.001, 0.0)); +/// ``` +pub fn has_happened(ev: Timing, now: Timing) -> bool { + (ev - now) < EVENT_GRACE +} diff --git a/rosenpass/tests/poll_example.rs b/rosenpass/tests/poll_example.rs index 9fb8bbf..cabded0 100644 --- a/rosenpass/tests/poll_example.rs +++ b/rosenpass/tests/poll_example.rs @@ -10,8 +10,10 @@ use rosenpass_ciphers::StaticKem; use rosenpass_util::result::OkExt; use rosenpass::protocol::{ - testutils::time_travel_forward, CryptoServer, HostIdentification, MsgBuf, PeerPtr, PollResult, - ProtocolVersion, SPk, SSk, SymKey, Timing, UNENDING, + testutils::time_travel_forward, + timing::{Timing, UNENDING}, + CryptoServer, HostIdentification, MsgBuf, PeerPtr, PollResult, ProtocolVersion, SPk, SSk, + SymKey, }; // TODO: Most of the utility functions in here should probably be moved to