Skip to main content

SessionCipher

Struct SessionCipher 

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

Symmetric cipher for encrypting/decrypting frames within a session.

Uses AES-256-GCM with an incrementing nonce counter to prevent reuse. The nonce is constructed as: 8-byte random session prefix || 4-byte counter.

Replay protection: decrypt tracks the highest accepted counter and rejects any frame whose counter is ≤ the last accepted value.

Implementations§

Source§

impl SessionCipher

Source

pub fn new(key: &[u8; 32], nonce_prefix: [u8; 8]) -> Self

Create a new SessionCipher from a 32-byte key and 8-byte nonce prefix.

Source

pub fn encrypt(&self, plaintext: &[u8]) -> Result<EncryptedFrame, SessionError>

Encrypt plaintext, returning the nonce and ciphertext.

Source

pub fn decrypt(&self, frame: &EncryptedFrame) -> Result<Vec<u8>, SessionError>

Decrypt an encrypted frame.

Rejects replayed frames: the counter embedded in frame.nonce[8..12] must be strictly greater than the last accepted counter.

Source

pub fn encrypt_in_place_detached( &self, payload: &mut [u8], ) -> Result<([u8; 12], [u8; 16]), SessionError>

Encrypts plaintext in-place, returning the generated nonce and tag. Returns an error if the nonce counter is exhausted or encryption fails.

Source

pub fn decrypt_in_place_detached( &self, nonce_bytes: &[u8; 12], payload: &mut [u8], tag_bytes: &[u8; 16], ) -> Result<(), SessionError>

Decrypts a frame in-place, avoiding an allocation. Returns an error if the nonce is replayed, or if decryption fails.

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 = 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