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
extern crate rustc_serialize;
extern crate arrayvec;
use error::NoiseError;
use handshakestate::{CipherStates, HandshakeState};
pub struct TransportState {
pub cipherstates: CipherStates,
initiator: bool,
}
impl TransportState {
pub fn new(cipherstates: CipherStates, initiator: bool) -> Self {
TransportState {
cipherstates: cipherstates,
initiator: initiator,
}
}
pub fn write_transport_message(&mut self,
payload: &[u8],
message: &mut [u8]) -> Result<usize, NoiseError> {
let cipher = if self.initiator { &mut self.cipherstates.0 } else { &mut self.cipherstates.1 };
Ok(cipher.encrypt(payload, message))
}
pub fn read_transport_message(&mut self,
payload: &[u8],
message: &mut [u8]) -> Result<usize, NoiseError> {
let cipher = if self.initiator { &mut self.cipherstates.1 } else { &mut self.cipherstates.0 };
cipher.decrypt(payload, message).map_err(|_| NoiseError::DecryptError)
}
}