pub struct ChaCha20Poly1305Cipher { /* private fields */ }Expand description
ChaCha20-Poly1305 AEAD cipher for quantum-resistant symmetric encryption
This struct provides authenticated encryption with associated data using
the ChaCha20 stream cipher for encryption and Poly1305 for authentication.
ChaCha20-Poly1305 is considered quantum-resistant because:
- It relies on symmetric cryptography rather than mathematical problems
ChaCha20uses a 256-bit key with a large keyspace (2^256)- Quantum attacks on symmetric ciphers require approximately 2^(n/2) operations
- This means ~2^128 operations for
ChaCha20, which is still computationally infeasible
Implementations§
Source§impl ChaCha20Poly1305Cipher
impl ChaCha20Poly1305Cipher
Sourcepub fn new(key: &SymmetricKey) -> ChaCha20Poly1305Cipher
pub fn new(key: &SymmetricKey) -> ChaCha20Poly1305Cipher
Sourcepub fn encrypt(
&self,
plaintext: &[u8],
associated_data: Option<&[u8]>,
) -> Result<(Vec<u8>, [u8; 12]), SymmetricError>
pub fn encrypt( &self, plaintext: &[u8], associated_data: Option<&[u8]>, ) -> Result<(Vec<u8>, [u8; 12]), SymmetricError>
Encrypt plaintext data
This method encrypts the provided plaintext and returns the ciphertext along with the nonce used for encryption. The nonce is randomly generated for each encryption operation.
§Arguments
plaintext- The data to encryptassociated_data- Optional associated data that is authenticated but not encrypted
§Returns
A tuple containing the ciphertext and the nonce used for encryption
§Errors
Returns SymmetricError::EncryptionFailed if encryption fails
§Example
use saorsa_pqc::symmetric::{ChaCha20Poly1305Cipher, SymmetricKey};
let key = SymmetricKey::generate();
let cipher = ChaCha20Poly1305Cipher::new(&key);
let plaintext = b"Secret message";
let (ciphertext, nonce) = cipher.encrypt(plaintext, None)?;Sourcepub fn decrypt(
&self,
ciphertext: &[u8],
nonce: &[u8; 12],
associated_data: Option<&[u8]>,
) -> Result<Vec<u8>, SymmetricError>
pub fn decrypt( &self, ciphertext: &[u8], nonce: &[u8; 12], associated_data: Option<&[u8]>, ) -> Result<Vec<u8>, SymmetricError>
Decrypt ciphertext data
This method decrypts the provided ciphertext using the given nonce.
§Arguments
ciphertext- The encrypted data to decryptnonce- The nonce that was used during encryptionassociated_data- Optional associated data that was authenticated during encryption
§Returns
The decrypted plaintext
§Errors
Returns SymmetricError::DecryptionFailed if decryption or authentication fails
§Example
use saorsa_pqc::symmetric::{ChaCha20Poly1305Cipher, SymmetricKey};
let key = SymmetricKey::generate();
let cipher = ChaCha20Poly1305Cipher::new(&key);
let plaintext = b"Secret message";
let (ciphertext, nonce) = cipher.encrypt(plaintext, None)?;
let decrypted = cipher.decrypt(&ciphertext, &nonce, None)?;
assert_eq!(plaintext, &decrypted[..]);