Skip to main content

Session

Struct Session 

Source
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

Source

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).

Source

pub fn set_stride(&mut self, start_counter: u64, stride: u64)

Reconfigure the send counter and stride increment for this session.

Source

pub fn stride(&self) -> u64

The current nonce stride increment.

Source

pub fn send_counter(&self) -> u64

The current send counter.

Source

pub fn seal(&mut self, plaintext: &[u8]) -> Result<Sealed, CryptoError>

Seal one inner frame, assigning it the next send counter.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.