use crate::aead::chacha20poly1305::{CHACHA20POLY1305_KEY_SIZE, CHACHA20POLY1305_TAG_SIZE};
use crate::error::{validate, Result};
use crate::types::nonce::XChaCha20Compatible;
use crate::types::Nonce;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use chacha20poly1305::aead::{Aead, KeyInit, Payload};
use chacha20poly1305::{XChaCha20Poly1305 as BackendXChaCha20Poly1305, XNonce};
use dcrypt_api::traits::AuthenticatedCipher;
use dcrypt_common::security::{SecretBuffer, SecureZeroingType};
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
pub const XCHACHA20POLY1305_NONCE_SIZE: usize = 24;
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct XChaCha20Poly1305 {
key: SecretBuffer<CHACHA20POLY1305_KEY_SIZE>,
}
impl XChaCha20Poly1305 {
pub fn new(key: &[u8; CHACHA20POLY1305_KEY_SIZE]) -> Self {
Self {
key: SecretBuffer::new(*key),
}
}
pub fn from_key(key: &[u8]) -> Result<Self> {
validate::length(
"XChaCha20Poly1305 key",
key.len(),
CHACHA20POLY1305_KEY_SIZE,
)?;
let mut key_bytes = Zeroizing::new([0u8; CHACHA20POLY1305_KEY_SIZE]);
key_bytes.copy_from_slice(&key[..CHACHA20POLY1305_KEY_SIZE]);
Ok(Self {
key: SecretBuffer::new(*key_bytes),
})
}
pub fn encrypt<const N: usize>(
&self,
nonce: &Nonce<N>,
plaintext: &[u8],
aad: Option<&[u8]>,
) -> Result<Vec<u8>>
where
Nonce<N>: XChaCha20Compatible,
{
validate::length(
"XChaCha20Poly1305 nonce",
nonce.as_ref().len(),
XCHACHA20POLY1305_NONCE_SIZE,
)?;
let cipher = BackendXChaCha20Poly1305::new_from_slice(self.key.as_ref())
.map_err(|_| crate::error::Error::param("key", "invalid XChaCha20 key"))?;
cipher
.encrypt(
XNonce::from_slice(nonce.as_ref()),
Payload {
msg: plaintext,
aad: aad.unwrap_or(&[]),
},
)
.map_err(|_| crate::error::Error::Processing {
operation: "XChaCha20Poly1305 encryption",
details: "message is too long",
})
}
pub fn decrypt<const N: usize>(
&self,
nonce: &Nonce<N>,
ciphertext: &[u8],
aad: Option<&[u8]>,
) -> Result<Vec<u8>>
where
Nonce<N>: XChaCha20Compatible,
{
validate::length(
"XChaCha20Poly1305 nonce",
nonce.as_ref().len(),
XCHACHA20POLY1305_NONCE_SIZE,
)?;
let cipher = BackendXChaCha20Poly1305::new_from_slice(self.key.as_ref())
.map_err(|_| crate::error::Error::param("key", "invalid XChaCha20 key"))?;
cipher
.decrypt(
XNonce::from_slice(nonce.as_ref()),
Payload {
msg: ciphertext,
aad: aad.unwrap_or(&[]),
},
)
.map_err(|_| crate::error::Error::Authentication {
algorithm: "XChaCha20Poly1305",
})
}
}
impl SecureZeroingType for XChaCha20Poly1305 {
fn zeroed() -> Self {
Self {
key: SecretBuffer::zeroed(),
}
}
fn secure_clone(&self) -> Self {
Self {
key: self.key.secure_clone(),
}
}
}
impl AuthenticatedCipher for XChaCha20Poly1305 {
const TAG_SIZE: usize = CHACHA20POLY1305_TAG_SIZE;
const ALGORITHM_ID: &'static str = "XChaCha20Poly1305";
}
#[cfg(test)]
mod tests;