use alloc::vec::Vec;
use crate::CryptoError;
pub trait Aead: Send + Sync {
#[must_use]
fn name(&self) -> &'static str;
#[must_use]
fn key_len(&self) -> usize;
#[must_use]
fn nonce_len(&self) -> usize;
#[must_use]
fn tag_len(&self) -> usize;
#[must_use = "result must be checked"]
fn seal(
&self,
key: &[u8],
nonce: &[u8],
aad: &[u8],
pt: &[u8],
ct_out: &mut [u8],
) -> Result<usize, CryptoError>;
#[must_use = "result must be checked"]
fn open(
&self,
key: &[u8],
nonce: &[u8],
aad: &[u8],
ct: &[u8],
pt_out: &mut [u8],
) -> Result<usize, CryptoError>;
#[must_use = "result must be checked"]
fn seal_to_vec(
&self,
key: &[u8],
nonce: &[u8],
aad: &[u8],
plaintext: &[u8],
) -> Result<Vec<u8>, CryptoError> {
let mut out = alloc::vec![0u8; plaintext.len() + self.tag_len()];
self.seal(key, nonce, aad, plaintext, &mut out)?;
Ok(out)
}
#[must_use = "result must be checked"]
fn open_to_vec(
&self,
key: &[u8],
nonce: &[u8],
aad: &[u8],
ciphertext: &[u8],
) -> Result<Vec<u8>, CryptoError> {
let tag_len = self.tag_len();
if ciphertext.len() < tag_len {
return Err(CryptoError::BufferTooSmall);
}
let mut out = alloc::vec![0u8; ciphertext.len() - tag_len];
self.open(key, nonce, aad, ciphertext, &mut out)?;
Ok(out)
}
}
pub trait StreamingAead: Sized + Send {
#[must_use = "result must be checked"]
fn init(key: &[u8], nonce: &[u8], aad: &[u8]) -> Result<Self, CryptoError>;
#[must_use = "result must be checked"]
fn encrypt_update(&mut self, chunk: &[u8], out: &mut [u8]) -> Result<usize, CryptoError>;
#[must_use = "result must be checked"]
fn encrypt_finalize(self, out: &mut [u8]) -> Result<[u8; 16], CryptoError>;
#[must_use = "result must be checked"]
fn decrypt_update(&mut self, chunk: &[u8], out: &mut [u8]) -> Result<usize, CryptoError>;
#[must_use = "result must be checked"]
fn decrypt_finalize(self, expected_tag: &[u8]) -> Result<(), CryptoError>;
fn reset(&mut self);
}