diff options
Diffstat (limited to 'src/peer.rs')
-rw-r--r-- | src/peer.rs | 101 |
1 files changed, 52 insertions, 49 deletions
diff --git a/src/peer.rs b/src/peer.rs index dd7d303..e4dff8b 100644 --- a/src/peer.rs +++ b/src/peer.rs @@ -4,12 +4,12 @@ use generic_array::typenum::U32; use generic_array::GenericArray; use x25519_dalek::PublicKey; -use x25519_dalek::StaticSecret; use x25519_dalek::SharedSecret; +use x25519_dalek::StaticSecret; -use crate::types::*; -use crate::timestamp; use crate::device::Device; +use crate::timestamp; +use crate::types::*; /* Represents the recomputation and state of a peer. * @@ -18,28 +18,28 @@ use crate::device::Device; pub struct Peer<T> { // external identifier - pub(crate) identifier : T, + pub(crate) identifier: T, // internal identifier - pub(crate) idx : usize, + pub(crate) idx: usize, // mutable state - state : Mutex<State>, - timestamp : Mutex<Option<timestamp::TAI64N>>, + state: Mutex<State>, + timestamp: Mutex<Option<timestamp::TAI64N>>, // constant state - pub(crate) pk : PublicKey, // public key of peer - pub(crate) ss : SharedSecret, // precomputed DH(static, static) - pub(crate) psk : Psk // psk of peer + pub(crate) pk: PublicKey, // public key of peer + pub(crate) ss: SharedSecret, // precomputed DH(static, static) + pub(crate) psk: Psk, // psk of peer } pub enum State { Reset, - InitiationSent{ - sender : u32, // assigned sender id - eph_sk : StaticSecret, - hs : GenericArray<u8, U32>, - ck : GenericArray<u8, U32> + InitiationSent { + sender: u32, // assigned sender id + eph_sk: StaticSecret, + hs: GenericArray<u8, U32>, + ck: GenericArray<u8, U32>, }, } @@ -47,32 +47,39 @@ impl Clone for State { fn clone(&self) -> State { match self { State::Reset => State::Reset, - State::InitiationSent{sender, eph_sk, hs, ck} => - State::InitiationSent{ - sender : *sender, - eph_sk : StaticSecret::from(eph_sk.to_bytes()), - hs : *hs, - ck : *ck - } + State::InitiationSent { + sender, + eph_sk, + hs, + ck, + } => State::InitiationSent { + sender: *sender, + eph_sk: StaticSecret::from(eph_sk.to_bytes()), + hs: *hs, + ck: *ck, + }, } } } -impl <T>Peer<T> where T : Copy { +impl<T> Peer<T> +where + T: Copy, +{ pub fn new( - idx : usize, - identifier : T, // external identifier - pk : PublicKey, // public key of peer - ss : SharedSecret // precomputed DH(static, static) + idx: usize, + identifier: T, // external identifier + pk: PublicKey, // public key of peer + ss: SharedSecret, // precomputed DH(static, static) ) -> Self { Self { - idx : idx, - identifier : identifier, - state : Mutex::new(State::Reset), - timestamp : Mutex::new(None), - pk : pk, - ss : ss, - psk : [0u8; 32] + idx: idx, + identifier: identifier, + state: Mutex::new(State::Reset), + timestamp: Mutex::new(None), + pk: pk, + ss: ss, + psk: [0u8; 32], } } @@ -87,10 +94,7 @@ impl <T>Peer<T> where T : Copy { /// /// # Arguments /// - pub fn set_state( - &self, - state_new : State - ) { + pub fn set_state(&self, state_new: State) { *self.state.lock() = state_new; } @@ -102,29 +106,28 @@ impl <T>Peer<T> where T : Copy { /// * ts_new - The associated timestamp pub fn check_timestamp( &self, - device : &Device<T>, - timestamp_new : ×tamp::TAI64N + device: &Device<T>, + timestamp_new: ×tamp::TAI64N, ) -> Result<(), HandshakeError> { - let mut state = self.state.lock(); let mut timestamp = self.timestamp.lock(); let update = match *timestamp { None => true, - Some(timestamp_old) => if timestamp::compare(×tamp_old, ×tamp_new) { - true - } else { - false + Some(timestamp_old) => { + if timestamp::compare(×tamp_old, ×tamp_new) { + true + } else { + false + } } }; if update { // release existing identifier match *state { - State::InitiationSent{sender, ..} => { - device.release(sender) - }, - _ => () + State::InitiationSent { sender, .. } => device.release(sender), + _ => (), } // reset state and update timestamp |