Skip to main content

agent_phone/
frame.rs

1//! Post-handshake transport frame cipher.
2
3use crate::error::{Error, Result};
4use crate::noise::HandshakeResult;
5
6pub const MAX_PLAINTEXT: usize = 65519;
7
8pub struct FrameCipher {
9    pub transport: HandshakeResult,
10}
11
12impl FrameCipher {
13    pub fn new(transport: HandshakeResult) -> Self {
14        Self { transport }
15    }
16
17    pub fn seal(&mut self, plaintext: &[u8]) -> Result<Vec<u8>> {
18        if plaintext.len() > MAX_PLAINTEXT {
19            return Err(Error::PlaintextTooLarge {
20                got: plaintext.len(),
21                max: MAX_PLAINTEXT,
22            });
23        }
24        self.transport.send(plaintext)
25    }
26
27    pub fn open(&mut self, ciphertext: &[u8]) -> Result<Vec<u8>> {
28        self.transport.recv(ciphertext)
29    }
30}