pub struct Session { /* private fields */ }Expand description
An established AEAD session. Seals outgoing frames under a monotonic counter and opens incoming frames out of order, rejecting replays.
Uses ring’s ChaCha20-Poly1305 keyed by the Noise Split() transport keys
(see Handshake::into_session) rather than snow’s own transport state.
Implementations§
Source§impl Session
impl Session
Sourcepub fn from_raw_keys(
k_send: &[u8; 32],
k_recv: &[u8; 32],
start_counter: u64,
stride: u64,
) -> Result<Self, CryptoError>
pub fn from_raw_keys( k_send: &[u8; 32], k_recv: &[u8; 32], start_counter: u64, stride: u64, ) -> Result<Self, CryptoError>
Construct a session directly from raw 32-byte send and receive keys, with an explicit start counter and stride increment (e.g. for worker sharding).
Sourcepub fn set_stride(&mut self, start_counter: u64, stride: u64)
pub fn set_stride(&mut self, start_counter: u64, stride: u64)
Reconfigure the send counter and stride increment for this session.
Sourcepub fn send_counter(&self) -> u64
pub fn send_counter(&self) -> u64
The current send counter.
Sourcepub fn seal(&mut self, plaintext: &[u8]) -> Result<Sealed, CryptoError>
pub fn seal(&mut self, plaintext: &[u8]) -> Result<Sealed, CryptoError>
Seal one inner frame, assigning it the next send counter.
Sourcepub fn open(
&mut self,
counter: u64,
ciphertext: &[u8],
) -> Result<Vec<u8>, CryptoError>
pub fn open( &mut self, counter: u64, ciphertext: &[u8], ) -> Result<Vec<u8>, CryptoError>
Open one inner frame received under explicit counter, enforcing replay protection.
The replay window is checked read-only before AEAD, then committed only after the frame authenticates (matching WireGuard, which advances its window post-decrypt). This ordering prevents a forged frame carrying an arbitrary counter from advancing the window and starving legitimate packets — an off-path DoS the mark-before-auth ordering was open to.
Sourcepub fn check_replay(&self, counter: u64) -> bool
pub fn check_replay(&self, counter: u64) -> bool
Check whether counter is valid within the anti-replay window without committing it.
Used for pre-decoding packet validation and DoS rejection.
Sourcepub fn seal_into(
&mut self,
plaintext: &[u8],
out: &mut Vec<u8>,
) -> Result<u64, CryptoError>
pub fn seal_into( &mut self, plaintext: &[u8], out: &mut Vec<u8>, ) -> Result<u64, CryptoError>
Seal into a caller-owned reusable buffer (no per-call allocation).
Sourcepub fn seal_with_counter(
&self,
counter: u64,
plaintext: &[u8],
) -> Result<Sealed, CryptoError>
pub fn seal_with_counter( &self, counter: u64, plaintext: &[u8], ) -> Result<Sealed, CryptoError>
Seal one inner frame under an explicit counter, without mutating internal counter. This allows multi-threaded workers sharing a single peer connection to seal concurrently using nonces dispensed from a ChunkedNonceDispenser.
Sourcepub fn seal_into_with_counter(
&self,
counter: u64,
plaintext: &[u8],
out: &mut Vec<u8>,
) -> Result<(), CryptoError>
pub fn seal_into_with_counter( &self, counter: u64, plaintext: &[u8], out: &mut Vec<u8>, ) -> Result<(), CryptoError>
Seal into a caller-owned reusable buffer under an explicit counter.
Sourcepub fn open_with_window(
&self,
counter: u64,
ciphertext: &[u8],
replay: &mut ReplayWindow,
) -> Result<Vec<u8>, CryptoError>
pub fn open_with_window( &self, counter: u64, ciphertext: &[u8], replay: &mut ReplayWindow, ) -> Result<Vec<u8>, CryptoError>
Open one inner frame received under explicit counter against a caller-provided replay window.
Sourcepub fn open_into_with_window(
&self,
counter: u64,
ciphertext: &[u8],
replay: &mut ReplayWindow,
out: &mut Vec<u8>,
) -> Result<(), CryptoError>
pub fn open_into_with_window( &self, counter: u64, ciphertext: &[u8], replay: &mut ReplayWindow, out: &mut Vec<u8>, ) -> Result<(), CryptoError>
Open into a caller-owned reusable buffer under explicit counter against a caller-provided replay window.
Sourcepub fn open_into(
&mut self,
counter: u64,
ciphertext: &[u8],
out: &mut Vec<u8>,
) -> Result<(), CryptoError>
pub fn open_into( &mut self, counter: u64, ciphertext: &[u8], out: &mut Vec<u8>, ) -> Result<(), CryptoError>
Open into a caller-owned reusable buffer (no per-call allocation).
Sourcepub fn seal_in_place(
&mut self,
buf: &mut [u8],
plaintext_len: usize,
) -> Result<u64, CryptoError>
pub fn seal_in_place( &mut self, buf: &mut [u8], plaintext_len: usize, ) -> Result<u64, CryptoError>
Seal plaintext in-place inside buf[..plaintext_len], writing the 16-byte Poly1305
authentication tag immediately following the ciphertext into buf[plaintext_len..plaintext_len + 16].
The buffer must have capacity of at least plaintext_len + 16 bytes.
Returns the explicit counter assigned to this frame.
Sourcepub fn open_in_place(
&mut self,
counter: u64,
buf: &mut [u8],
sealed_len: usize,
) -> Result<usize, CryptoError>
pub fn open_in_place( &mut self, counter: u64, buf: &mut [u8], sealed_len: usize, ) -> Result<usize, CryptoError>
Open and authenticate ciphertext in-place inside buf[..sealed_len], enforcing anti-replay.
Expects the 16-byte Poly1305 authentication tag at the end of the ciphertext:
buf[sealed_len - 16..sealed_len].
Returns the decrypted plaintext length (sealed_len - 16).