mirror of
https://github.com/rosenpass/rosenpass.git
synced 2026-02-28 14:33:37 -08:00
chore: Move timing related thing out of protocol.rs
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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::*;
|
||||
|
||||
@@ -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
|
||||
|
||||
46
rosenpass/src/protocol/timing.rs
Normal file
46
rosenpass/src/protocol/timing.rs
Normal file
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user