pub struct MlsCipher { /* private fields */ }Expand description
MLS cipher for encrypting and decrypting messages with ChaCha20-Poly1305.
The cipher uses a 32-byte key and derives per-message nonces from a base nonce and message counter. This provides authenticated encryption with additional data (AEAD).
Implementations§
Source§impl MlsCipher
impl MlsCipher
Sourcepub fn new(key: Vec<u8>, base_nonce: Vec<u8>) -> Self
pub fn new(key: Vec<u8>, base_nonce: Vec<u8>) -> Self
Creates a new MLS cipher with the given key and base nonce.
§Arguments
key- 32-byte encryption key for ChaCha20-Poly1305base_nonce- 12-byte base nonce (XORed with counter for each message)
§Security
The key should be securely derived from an MLS key schedule. The base nonce is combined with a counter to ensure unique nonces for each message.
Sourcepub fn encrypt(
&self,
plaintext: &[u8],
aad: &[u8],
counter: u64,
) -> Result<Vec<u8>>
pub fn encrypt( &self, plaintext: &[u8], aad: &[u8], counter: u64, ) -> Result<Vec<u8>>
Encrypts plaintext with authenticated encryption.
Uses ChaCha20-Poly1305 AEAD to encrypt the plaintext and authenticate both the ciphertext and additional authenticated data (AAD).
§Arguments
plaintext- Data to encryptaad- Additional authenticated data (not encrypted, but authenticated)counter- Message counter for nonce derivation
§Returns
Ciphertext with authentication tag appended (ciphertext.len() == plaintext.len() + 16).
§Errors
Returns MlsError::EncryptionError if encryption fails (e.g., invalid key length).
§Security
CRITICAL: Never reuse the same counter with the same key. Counter reuse completely breaks ChaCha20-Poly1305 security.
Sourcepub fn decrypt(
&self,
ciphertext: &[u8],
aad: &[u8],
counter: u64,
) -> Result<Vec<u8>>
pub fn decrypt( &self, ciphertext: &[u8], aad: &[u8], counter: u64, ) -> Result<Vec<u8>>
Decrypts ciphertext with authenticated decryption.
Uses ChaCha20-Poly1305 AEAD to decrypt and verify the authentication tag. Both the ciphertext and AAD are authenticated.
§Arguments
ciphertext- Encrypted data with authentication tag appendedaad- Additional authenticated data (must match encryption AAD)counter- Message counter for nonce derivation (must match encryption counter)
§Returns
Decrypted plaintext.
§Errors
MlsError::DecryptionError- Authentication tag verification failed or decryption failedMlsError::EncryptionError- Invalid key length
§Security
Authentication failure indicates either:
- Wrong key
- Tampered ciphertext
- Wrong AAD
- Wrong counter
Sourcepub fn base_nonce(&self) -> &[u8] ⓘ
pub fn base_nonce(&self) -> &[u8] ⓘ
Gets the base nonce.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MlsCipher
impl RefUnwindSafe for MlsCipher
impl Send for MlsCipher
impl Sync for MlsCipher
impl Unpin for MlsCipher
impl UnsafeUnpin for MlsCipher
impl UnwindSafe for MlsCipher
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