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
impl SessionCipher
Sourcepub fn new(key: &[u8; 32], nonce_prefix: [u8; 8]) -> Self
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.
Sourcepub fn encrypt(&self, plaintext: &[u8]) -> Result<EncryptedFrame, SessionError>
pub fn encrypt(&self, plaintext: &[u8]) -> Result<EncryptedFrame, SessionError>
Encrypt plaintext, returning the nonce and ciphertext.
Sourcepub fn decrypt(&self, frame: &EncryptedFrame) -> Result<Vec<u8>, SessionError>
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.
Sourcepub fn encrypt_in_place_detached(
&self,
payload: &mut [u8],
) -> Result<([u8; 12], [u8; 16]), SessionError>
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.
Sourcepub fn decrypt_in_place_detached(
&self,
nonce_bytes: &[u8; 12],
payload: &mut [u8],
tag_bytes: &[u8; 16],
) -> Result<(), SessionError>
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.