Skip to main content

NoiseSession

Struct NoiseSession 

Source
pub struct NoiseSession { /* private fields */ }
Expand description

Completed Noise session for transport encryption.

Provides bidirectional authenticated encryption with replay protection. The send counter is monotonically incremented; received counters are validated against a sliding window to prevent replay attacks.

Implementations§

Source§

impl NoiseSession

Source

pub fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>, NoiseError>

Encrypt a message for sending (using internal counter).

Returns the ciphertext. The current send counter should be included in the wire format before calling this method.

Source

pub fn current_send_counter(&self) -> u64

Get the current send counter (before incrementing).

Use this to get the counter to include in the wire format. The counter will be incremented when encrypt is called.

Source

pub fn decrypt(&mut self, ciphertext: &[u8]) -> Result<Vec<u8>, NoiseError>

Decrypt a received message (using internal counter).

This is for handshake-phase decryption. For transport phase with explicit counters, use decrypt_with_replay_check instead.

Source

pub fn check_replay(&self, counter: u64) -> Result<(), NoiseError>

Check if a counter passes the replay window.

Returns Ok(()) if the counter is acceptable, Err if it should be rejected. Call this before attempting decryption to avoid wasting CPU on replay attacks.

Source

pub fn decrypt_with_replay_check( &mut self, ciphertext: &[u8], counter: u64, ) -> Result<Vec<u8>, NoiseError>

Decrypt with explicit counter and replay protection.

This is the primary decryption method for transport phase. The counter comes from the wire format and is validated against the replay window before and after decryption.

On success, the counter is accepted into the replay window.

Source

pub fn encrypt_with_aad( &mut self, plaintext: &[u8], aad: &[u8], ) -> Result<Vec<u8>, NoiseError>

Encrypt a message with Additional Authenticated Data (AAD).

Returns the ciphertext. The current send counter should be included in the wire format before calling this method.

Source

pub fn decrypt_with_replay_check_and_aad( &mut self, ciphertext: &[u8], counter: u64, aad: &[u8], ) -> Result<Vec<u8>, NoiseError>

Decrypt with explicit counter, replay protection, and AAD.

This is the primary decryption method for the FMP transport phase with AAD binding. The AAD (typically the 16-byte outer header) must match what was used during encryption.

Source

pub fn decrypt_with_replay_check_and_aad_in_place( &mut self, buf: &mut [u8], counter: u64, aad: &[u8], ) -> Result<usize, NoiseError>

In-place variant of Self::decrypt_with_replay_check_and_aad.

On entry, buf holds ciphertext + 16-byte AEAD tag. On successful return, buf[..returned_len] holds the plaintext. The caller can then slice into buf without paying for an extra heap allocation + memcpy per packet — at multi-Gbps single-stream the by-value variant’s ciphertext.to_vec() alone is a measurable fraction of the rx_loop’s per-packet cost.

Source

pub fn highest_received_counter(&self) -> u64

Get the highest received counter.

Source

pub fn recv_cipher_clone(&self) -> Option<LessSafeKey>

Clone the recv-side AEAD instance, for off-task decrypt.

Returns None if the recv cipher has no key (transport phase has not begun). The cloned cipher pairs with decrypt_with_counter[_and_aad] on CipherState: a dispatcher can check_replay here, fan the AEAD work out to a worker holding the clone + counter + aad, then call accept_replay here once the worker reports success.

Source

pub fn recv_replay_snapshot_owned(&self) -> ReplayWindow

Snapshot the current replay-window state as an owned ReplayWindow value, for hand-off to a shard-owning decrypt worker.

The worker becomes the sole authority for replay protection on this session after this snapshot. The local self.replay_window is no longer the source of truth — it only matters for rare-slow-path uses (rekey, drain-window fallback). The worker keeps its copy in its own thread-local HashMap, so there’s no Mutex / no Arc / no sharing — direct &mut access on every packet.

(Previously this returned an Arc<Mutex<ReplayWindow>> for concurrent access; the data-plane shard restructure now hands the worker exclusive ownership instead.)

Source

pub fn send_cipher_clone(&self) -> Option<LessSafeKey>

Clone the send-side AEAD instance, for off-task encrypt.

Returns None if the send cipher has no key. Pairs with encrypt_with_counter[_and_aad] on CipherState. The caller must own counter sequencing — take_send_counter hands out monotonic counters under the session’s own &mut.

Source

pub fn take_send_counter(&mut self) -> Result<u64, NoiseError>

Reserve and return the next send counter, advancing the internal nonce. For pipelined encrypt paths that call encrypt_with_counter on a cloned cipher: the dispatcher pre-assigns the counter here (under the session’s &mut) and the worker performs the AEAD with no further mutation of session state.

Source

pub fn accept_replay(&mut self, counter: u64)

Accept a counter into the replay window after a successful out-of-task decrypt. Caller is responsible for verifying decrypt success first.

Source

pub fn reset_replay_window(&mut self)

Reset the replay window (use when rekeying).

Source

pub fn handshake_hash(&self) -> &[u8; 32]

Get the handshake hash for channel binding.

Source

pub fn remote_static(&self) -> &PublicKey

Get the remote peer’s static public key.

Source

pub fn remote_static_xonly(&self) -> XOnlyPublicKey

Get the remote peer’s x-only public key.

Source

pub fn role(&self) -> HandshakeRole

Get our role in the handshake.

Source

pub fn send_nonce(&self) -> u64

Get the send nonce (for debugging).

Source

pub fn recv_nonce(&self) -> u64

Get the receive nonce (for debugging).

Trait Implementations§

Source§

impl Debug for NoiseSession

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more