summaryrefslogtreecommitdiffstats
path: root/src/noise/peer.rs
blob: 5b01d7541e389a2ef3edb0ce7d658e88c096a67f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use spin::Mutex;

use generic_array::typenum::U32;
use generic_array::GenericArray;

use x25519_dalek::PublicKey;
use x25519_dalek::SharedSecret;
use x25519_dalek::StaticSecret;

use super::device::Device;
use super::timestamp;
use super::types::*;

/* Represents the recomputation and state of a peer.
 *
 * This type is only for internal use and not exposed.
 */

pub struct Peer<T> {
    // external identifier
    pub(crate) identifier: T,

    // mutable state
    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 enum State {
    Reset,
    InitiationSent {
        sender: u32, // assigned sender id
        eph_sk: StaticSecret,
        hs: GenericArray<u8, U32>,
        ck: GenericArray<u8, U32>,
    },
}

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,
            },
        }
    }
}

impl<T> Peer<T>
where
    T: Copy,
{
    pub fn new(
        identifier: T,    // external identifier
        pk: PublicKey,    // public key of peer
        ss: SharedSecret, // precomputed DH(static, static)
    ) -> Self {
        Self {
            identifier: identifier,
            state: Mutex::new(State::Reset),
            timestamp: Mutex::new(None),
            pk: pk,
            ss: ss,
            psk: [0u8; 32],
        }
    }

    /// Return the state of the peer
    ///
    /// # Arguments
    pub fn get_state(&self) -> State {
        self.state.lock().clone()
    }

    /// Set the state of the peer unconditionally
    ///
    /// # Arguments
    ///
    pub fn set_state(&self, state_new: State) {
        *self.state.lock() = state_new;
    }

    /// Set the mutable state of the peer conditioned on the timestamp being newer
    ///
    /// # Arguments
    ///
    /// * st_new - The updated state of the peer
    /// * ts_new - The associated timestamp
    pub fn check_timestamp(
        &self,
        device: &Device<T>,
        timestamp_new: &timestamp::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(&timestamp_old, &timestamp_new) {
                    true
                } else {
                    false
                }
            }
        };

        if update {
            // release existing identifier
            match *state {
                State::InitiationSent { sender, .. } => device.release(sender),
                _ => (),
            }

            // reset state and update timestamp
            *state = State::Reset;
            *timestamp = Some(*timestamp_new);
            Ok(())
        } else {
            Err(HandshakeError::OldTimestamp)
        }
    }
}