pub struct CipherState { /* private fields */ }Expand description
Symmetric cipher state for post-handshake encryption.
AEAD is ring’s ChaCha20-Poly1305 (BoringSSL backend), which dispatches
to NEON on aarch64 and AVX-512/AVX2 on x86_64. The cipher field caches
a constructed LessSafeKey so we don’t re-derive it per packet.
LessSafeKey itself isn’t Clone, so CipherState’s Clone impl
rebuilds it from the retained 32-byte key on demand — for the
off-task-decrypt path see cipher_clone.
Implementations§
Source§impl CipherState
impl CipherState
Sourcepub fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>, NoiseError>
pub fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>, NoiseError>
Encrypt plaintext, returning ciphertext with appended tag.
Sourcepub fn decrypt(&mut self, ciphertext: &[u8]) -> Result<Vec<u8>, NoiseError>
pub fn decrypt(&mut self, ciphertext: &[u8]) -> Result<Vec<u8>, NoiseError>
Decrypt ciphertext (with appended tag), returning plaintext.
Uses the internal nonce counter. For transport phase with explicit
counters from the wire format, use decrypt_with_counter instead.
Sourcepub fn decrypt_with_counter(
&self,
ciphertext: &[u8],
counter: u64,
) -> Result<Vec<u8>, NoiseError>
pub fn decrypt_with_counter( &self, ciphertext: &[u8], counter: u64, ) -> Result<Vec<u8>, NoiseError>
Decrypt with an explicit counter value (for transport phase).
This is used when the counter comes from the wire format rather than an internal counter. The counter must be validated by a replay window before calling this method.
Sourcepub fn encrypt_with_aad(
&mut self,
plaintext: &[u8],
aad: &[u8],
) -> Result<Vec<u8>, NoiseError>
pub fn encrypt_with_aad( &mut self, plaintext: &[u8], aad: &[u8], ) -> Result<Vec<u8>, NoiseError>
Encrypt plaintext with Additional Authenticated Data (AAD).
The AAD is authenticated but not encrypted. Used for the FMP established frame format where the 16-byte outer header is bound to the AEAD tag.
Sourcepub fn encrypt_with_counter(
&self,
plaintext: &[u8],
counter: u64,
) -> Result<Vec<u8>, NoiseError>
pub fn encrypt_with_counter( &self, plaintext: &[u8], counter: u64, ) -> Result<Vec<u8>, NoiseError>
Encrypt plaintext with an explicit counter (no AAD).
Symmetric to decrypt_with_counter: takes &self and a caller-
supplied counter rather than mutating the internal nonce. Intended
for pipelined encrypt paths where a dispatcher pre-assigns counters
and fans the AEAD work out across worker threads. Callers are
responsible for ensuring counter uniqueness — typically by holding
the cipher behind a lock or queue that hands out counters in order.
Sourcepub fn encrypt_with_counter_and_aad(
&self,
plaintext: &[u8],
counter: u64,
aad: &[u8],
) -> Result<Vec<u8>, NoiseError>
pub fn encrypt_with_counter_and_aad( &self, plaintext: &[u8], counter: u64, aad: &[u8], ) -> Result<Vec<u8>, NoiseError>
Encrypt plaintext with an explicit counter and AAD.
Symmetric to decrypt_with_counter_and_aad: takes &self and a
caller-supplied counter rather than mutating the internal nonce.
Same uniqueness contract as encrypt_with_counter.
Sourcepub fn cipher_clone(&self) -> Option<LessSafeKey>
pub fn cipher_clone(&self) -> Option<LessSafeKey>
Construct an independent keyed AEAD pinned to this cipher’s key.
Returns None for an empty (un-keyed) state. The returned key is
freshly built from the retained 32-byte key material — ring’s
LessSafeKey doesn’t implement Clone deliberately, but for
ChaCha20-Poly1305 the construction is essentially a key copy plus
a constant-time check, so this is cheap. Combined with
decrypt_with_counter[_and_aad] (which already takes &self),
this lets a dispatcher offload the AEAD rounds to a worker pool
while the main task keeps the replay window and counter
assignment sequential.
Sourcepub fn decrypt_with_counter_and_aad(
&self,
ciphertext: &[u8],
counter: u64,
aad: &[u8],
) -> Result<Vec<u8>, NoiseError>
pub fn decrypt_with_counter_and_aad( &self, ciphertext: &[u8], counter: u64, aad: &[u8], ) -> Result<Vec<u8>, NoiseError>
Decrypt with an explicit counter and AAD (for transport phase).
Combines explicit counter (from wire format) with AAD verification. The AAD must match exactly what was used during encryption or the AEAD tag verification will fail.
Sourcepub fn decrypt_with_counter_and_aad_in_place(
&self,
buf: &mut [u8],
counter: u64,
aad: &[u8],
) -> Result<usize, NoiseError>
pub fn decrypt_with_counter_and_aad_in_place( &self, buf: &mut [u8], counter: u64, aad: &[u8], ) -> Result<usize, NoiseError>
In-place variant of Self::decrypt_with_counter_and_aad.
On entry, buf holds ciphertext + 16-byte AEAD tag. On
successful return, buf[..returned_len] holds the plaintext.
Saves one heap alloc + memcpy per packet versus the by-value
variant — at multi-Gbps that’s a real chunk of the rx_loop’s
per-packet cost.
If the cipher has no key (handshake-not-yet-complete fallback),
buf is treated as already-plaintext and the full length is
returned unchanged.
Trait Implementations§
Source§impl Clone for CipherState
impl Clone for CipherState
Auto Trait Implementations§
impl Freeze for CipherState
impl RefUnwindSafe for CipherState
impl Send for CipherState
impl Sync for CipherState
impl Unpin for CipherState
impl UnsafeUnpin for CipherState
impl UnwindSafe for CipherState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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