aboutsummaryrefslogtreecommitdiffstats
path: root/src/noise/messages.rs
blob: dca49b95a3aa8179cbfef64010e591d541a4ecf3 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#[cfg(test)]
use hex;

#[cfg(test)]
use std::fmt;

use byteorder::LittleEndian;
use zerocopy::byteorder::U32;
use zerocopy::{AsBytes, ByteSlice, FromBytes, LayoutVerified};

use super::types::*;

const SIZE_TAG: usize = 16;
const SIZE_X25519_POINT: usize = 32;
const SIZE_TIMESTAMP: usize = 12;

pub const TYPE_INITIATION: u8 = 1;
pub const TYPE_RESPONSE: u8 = 2;

#[repr(C)]
#[derive(Copy, Clone, FromBytes, AsBytes)]
pub struct Initiation {
    f_type: U32<LittleEndian>,
    pub f_sender: U32<LittleEndian>,
    pub f_ephemeral: [u8; SIZE_X25519_POINT],
    pub f_static: [u8; SIZE_X25519_POINT],
    pub f_static_tag: [u8; SIZE_TAG],
    pub f_timestamp: [u8; SIZE_TIMESTAMP],
    pub f_timestamp_tag: [u8; SIZE_TAG],
}

impl Default for Initiation {
    fn default() -> Self {
        Self {
            f_type: <U32<LittleEndian>>::new(TYPE_INITIATION as u32),

            f_sender: <U32<LittleEndian>>::ZERO,
            f_ephemeral: [0u8; SIZE_X25519_POINT],
            f_static: [0u8; SIZE_X25519_POINT],
            f_static_tag: [0u8; SIZE_TAG],
            f_timestamp: [0u8; SIZE_TIMESTAMP],
            f_timestamp_tag: [0u8; SIZE_TAG],
        }
    }
}

impl Initiation {
    pub fn parse<B: ByteSlice>(bytes: B) -> Result<LayoutVerified<B, Self>, HandshakeError> {
        let msg: LayoutVerified<B, Self> =
            LayoutVerified::new(bytes).ok_or(HandshakeError::InvalidMessageFormat)?;

        if msg.f_type.get() != (TYPE_INITIATION as u32) {
            return Err(HandshakeError::InvalidMessageFormat);
        }

        Ok(msg)
    }
}

#[cfg(test)]
impl fmt::Debug for Initiation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f,
            "MessageInitiation {{ type = {}, sender = {}, ephemeral = {}, static = {}|{}, timestamp = {}|{} }}",
            self.f_type.get(),
            self.f_sender.get(),
            hex::encode(self.f_ephemeral),
            hex::encode(self.f_static),
            hex::encode(self.f_static_tag),
            hex::encode(self.f_timestamp),
            hex::encode(self.f_timestamp_tag)
        )
    }
}

#[cfg(test)]
impl PartialEq for Initiation {
    fn eq(&self, other: &Self) -> bool {
        self.f_type.get() == other.f_type.get()
            && self.f_sender.get() == other.f_sender.get()
            && self.f_ephemeral[..] == other.f_ephemeral[..]
            && self.f_static[..] == other.f_static[..]
            && self.f_static_tag[..] == other.f_static_tag[..]
            && self.f_timestamp[..] == other.f_timestamp
            && self.f_timestamp_tag[..] == other.f_timestamp_tag
    }
}

#[cfg(test)]
impl Eq for Initiation {}

#[repr(C)]
#[derive(Copy, Clone, FromBytes, AsBytes)]
pub struct Response {
    f_type: U32<LittleEndian>,
    pub f_sender: U32<LittleEndian>,
    pub f_receiver: U32<LittleEndian>,
    pub f_ephemeral: [u8; SIZE_X25519_POINT],
    pub f_empty_tag: [u8; SIZE_TAG],
}

impl Response {
    pub fn parse<B: ByteSlice>(bytes: B) -> Result<LayoutVerified<B, Self>, HandshakeError> {
        let msg: LayoutVerified<B, Self> =
            LayoutVerified::new(bytes).ok_or(HandshakeError::InvalidMessageFormat)?;

        if msg.f_type.get() != (TYPE_RESPONSE as u32) {
            return Err(HandshakeError::InvalidMessageFormat);
        }

        Ok(msg)
    }
}

impl Default for Response {
    fn default() -> Self {
        Self {
            f_type: <U32<LittleEndian>>::new(TYPE_RESPONSE as u32),
            f_sender: <U32<LittleEndian>>::ZERO,
            f_receiver: <U32<LittleEndian>>::ZERO,
            f_ephemeral: [0u8; SIZE_X25519_POINT],
            f_empty_tag: [0u8; SIZE_TAG],
        }
    }
}

#[cfg(test)]
impl fmt::Debug for Response {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f,
            "MessageResponse {{ type = {}, sender = {}, receiver = {}, ephemeral = {}, empty = |{}  }}",
            self.f_type,
            self.f_sender,
            self.f_receiver,
            hex::encode(self.f_ephemeral),
            hex::encode(self.f_empty_tag)
        )
    }
}

#[cfg(test)]
impl PartialEq for Response {
    fn eq(&self, other: &Self) -> bool {
        self.f_type == other.f_type
            && self.f_sender == other.f_sender
            && self.f_receiver == other.f_receiver
            && self.f_ephemeral == other.f_ephemeral
            && self.f_empty_tag == other.f_empty_tag
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn message_response_identity() {
        let mut msg: Response = Default::default();

        msg.f_sender.set(146252);
        msg.f_receiver.set(554442);
        msg.f_ephemeral = [
            0xc1, 0x66, 0x0a, 0x0c, 0xdc, 0x0f, 0x6c, 0x51, 0x0f, 0xc2, 0xcc, 0x51, 0x52, 0x0c,
            0xde, 0x1e, 0xf7, 0xf1, 0xca, 0x90, 0x86, 0x72, 0xad, 0x67, 0xea, 0x89, 0x45, 0x44,
            0x13, 0x56, 0x52, 0x1f,
        ];
        msg.f_empty_tag = [
            0x60, 0x0e, 0x1e, 0x95, 0x41, 0x6b, 0x52, 0x05, 0xa2, 0x09, 0xe1, 0xbf, 0x40, 0x05,
            0x2f, 0xde,
        ];

        let buf: Vec<u8> = msg.as_bytes().to_vec();
        let msg_p = Response::parse(&buf[..]).unwrap();
        assert_eq!(msg, *msg_p.into_ref());
    }

    #[test]
    fn message_initiate_identity() {
        let mut msg: Initiation = Default::default();

        msg.f_sender.set(575757);
        msg.f_ephemeral = [
            0xc1, 0x66, 0x0a, 0x0c, 0xdc, 0x0f, 0x6c, 0x51, 0x0f, 0xc2, 0xcc, 0x51, 0x52, 0x0c,
            0xde, 0x1e, 0xf7, 0xf1, 0xca, 0x90, 0x86, 0x72, 0xad, 0x67, 0xea, 0x89, 0x45, 0x44,
            0x13, 0x56, 0x52, 0x1f,
        ];
        msg.f_static = [
            0xdc, 0x33, 0x90, 0x15, 0x8f, 0x82, 0x3e, 0x06, 0x44, 0xa0, 0xde, 0x4c, 0x15, 0x6c,
            0x5d, 0xa4, 0x65, 0x99, 0xf6, 0x6c, 0xa1, 0x14, 0x77, 0xf9, 0xeb, 0x6a, 0xec, 0xc3,
            0x3c, 0xda, 0x47, 0xe1,
        ];
        msg.f_static_tag = [
            0x45, 0xac, 0x8d, 0x43, 0xea, 0x1b, 0x2f, 0x02, 0x45, 0x5d, 0x86, 0x37, 0xee, 0x83,
            0x6b, 0x42,
        ];
        msg.f_timestamp = [
            0x4f, 0x1c, 0x60, 0xec, 0x0e, 0xf6, 0x36, 0xf0, 0x78, 0x28, 0x57, 0x42,
        ];
        msg.f_timestamp_tag = [
            0x60, 0x0e, 0x1e, 0x95, 0x41, 0x6b, 0x52, 0x05, 0xa2, 0x09, 0xe1, 0xbf, 0x40, 0x05,
            0x2f, 0xde,
        ];

        let buf: Vec<u8> = msg.as_bytes().to_vec();
        let msg_p = Initiation::parse(&buf[..]).unwrap();
        assert_eq!(msg, *msg_p.into_ref());
    }
}