2121//! of the PASE protocol for establishing secure sessions using a shared passcode.
2222
2323use core:: num:: NonZeroU8 ;
24- use core :: ops :: Add ;
25- use core :: time :: Duration ;
24+
25+ use embassy_time :: { Duration , Instant } ;
2626
2727use crate :: dm:: clusters:: adm_comm:: { self } ;
2828use crate :: dm:: endpoints:: ROOT_ENDPOINT_ID ;
@@ -34,7 +34,6 @@ use crate::sc::pase::spake2p::{
3434use crate :: sc:: SessionParameters ;
3535use crate :: tlv:: { FromTLV , OctetStr , ToTLV } ;
3636use crate :: transport:: exchange:: { Exchange , ExchangeId } ;
37- use crate :: utils:: epoch:: Epoch ;
3837use crate :: utils:: init:: { init, Init } ;
3938use crate :: utils:: maybe:: Maybe ;
4039use crate :: MatterService ;
@@ -95,7 +94,7 @@ pub struct CommWindow {
9594 /// The opener info
9695 opener : Option < CommWindowOpener > ,
9796 /// The window expiry instant
98- window_expiry : Duration ,
97+ window_expiry : Instant ,
9998 /// Number of failed PAKE handshake attempts within this window.
10099 /// Per Matter Core spec section 11.18.6.1.5, the window SHALL be
101100 /// revoked after 20 unsuccessful handshakes.
@@ -118,7 +117,7 @@ impl CommWindow {
118117 salt : Spake2pVerifierSaltRef < ' a > ,
119118 discriminator : u16 ,
120119 opener : Option < CommWindowOpener > ,
121- window_expiry : Duration ,
120+ window_expiry : Instant ,
122121 ) -> impl Init < Self > + ' a {
123122 init ! ( Self {
124123 mdns_id,
@@ -146,7 +145,7 @@ impl CommWindow {
146145 count : u32 ,
147146 discriminator : u16 ,
148147 opener : Option < CommWindowOpener > ,
149- window_expiry : Duration ,
148+ window_expiry : Instant ,
150149 ) -> impl Init < Self > + ' a {
151150 init ! ( Self {
152151 mdns_id,
@@ -189,34 +188,23 @@ pub struct Pase {
189188 /// The (one and only) PASE session timeout tracker
190189 /// If there is no active PASE session, this is `None`
191190 pub ( crate ) session_timeout : Option < SessionEstTimeout > ,
192- /// The epoch function
193- pub ( crate ) epoch : Epoch ,
194191}
195192
196193impl Pase {
197194 /// Create a new PASE state
198- ///
199- /// # Arguments
200- /// - `epoch` - The epoch function
201195 #[ inline( always) ]
202- pub const fn new ( epoch : Epoch ) -> Self {
196+ pub const fn new ( ) -> Self {
203197 Self {
204198 comm_window : Maybe :: none ( ) ,
205199 session_timeout : None ,
206- epoch,
207200 }
208201 }
209202
210203 /// Return an in-place initializer for the PASE manager
211- ///
212- /// # Arguments
213- /// - `epoch` - The epoch function
214- /// - `rand` - The random number generator
215- pub fn init ( epoch : Epoch ) -> impl Init < Self > {
204+ pub fn init ( ) -> impl Init < Self > {
216205 init ! ( Self {
217206 comm_window <- Maybe :: init_none( ) ,
218207 session_timeout: None ,
219- epoch,
220208 } )
221209 }
222210
@@ -232,7 +220,7 @@ impl Pase {
232220 let expired = self
233221 . comm_window
234222 . as_opt_ref ( )
235- . map ( |comm_window| ( self . epoch ) ( ) > comm_window. window_expiry )
223+ . map ( |comm_window| Instant :: now ( ) > comm_window. window_expiry )
236224 . unwrap_or ( false ) ;
237225
238226 if expired {
@@ -285,7 +273,7 @@ impl Pase {
285273 Err ( ErrorCode :: InvalidCommand ) ?;
286274 }
287275
288- let window_expiry = ( self . epoch ) ( ) . add ( Duration :: from_secs ( timeout_secs as _ ) ) ;
276+ let window_expiry = Instant :: now ( ) . saturating_add ( Duration :: from_secs ( timeout_secs as _ ) ) ;
289277
290278 self . comm_window
291279 . reinit ( Maybe :: init_some ( CommWindow :: init_with_pw (
@@ -342,7 +330,7 @@ impl Pase {
342330 Err ( ErrorCode :: InvalidCommand ) ?;
343331 }
344332
345- let window_expiry = ( self . epoch ) ( ) . add ( Duration :: from_secs ( timeout_secs as _ ) ) ;
333+ let window_expiry = Instant :: now ( ) . saturating_add ( Duration :: from_secs ( timeout_secs as _ ) ) ;
346334
347335 self . comm_window . reinit ( Maybe :: init_some ( CommWindow :: init (
348336 mdns_id,
@@ -433,6 +421,12 @@ impl Pase {
433421 }
434422}
435423
424+ impl Default for Pase {
425+ fn default ( ) -> Self {
426+ Self :: new ( )
427+ }
428+ }
429+
436430/// The timeout tracker for a PASE session establishment
437431const PASE_SESSION_EST_TIMEOUT_SECS : Duration = Duration :: from_secs ( 60 ) ;
438432
@@ -442,30 +436,23 @@ pub(crate) const SPAKE2_SESSION_KEYS_INFO: &[u8] = b"SessionKeys";
442436/// The PASE session establishment timeout tracker
443437pub ( crate ) struct SessionEstTimeout {
444438 /// The session expiry instant
445- session_est_expiry : Duration ,
439+ session_est_expiry : Instant ,
446440 /// The exchange identifier
447441 pub ( crate ) exch_id : ExchangeId ,
448442}
449443
450444impl SessionEstTimeout {
451- /// Create a new session establishment timeout tracker
452- ///
453- /// # Arguments
454- /// - `exchange` - The exchange
455- /// - `epoch` - The epoch function
456- pub ( crate ) fn new ( exchange : & Exchange , epoch : Epoch ) -> Self {
445+ /// Create a new session establishment timeout tracker.
446+ pub ( crate ) fn new ( exchange : & Exchange ) -> Self {
457447 Self {
458- session_est_expiry : epoch ( ) . add ( PASE_SESSION_EST_TIMEOUT_SECS ) ,
448+ session_est_expiry : Instant :: now ( ) . saturating_add ( PASE_SESSION_EST_TIMEOUT_SECS ) ,
459449 exch_id : exchange. id ( ) ,
460450 }
461451 }
462452
463- /// Check if the session establishment has expired
464- ///
465- /// # Arguments
466- /// - `epoch` - The current epoch
467- pub ( crate ) fn is_sess_expired ( & self , epoch : Epoch ) -> bool {
468- epoch ( ) > self . session_est_expiry
453+ /// Check if the session establishment has expired.
454+ pub ( crate ) fn is_sess_expired ( & self ) -> bool {
455+ Instant :: now ( ) > self . session_est_expiry
469456 }
470457}
471458
0 commit comments