#![allow(clippy::needless_return)]
use crate::AlgorithmError;
use crypto_core::Algorithm;
use zeroize::Zeroizing;
pub struct MlKem768Algo;
impl MlKem768Algo {
pub const ALG: Algorithm = Algorithm::MlKem768;
pub fn generate_keypair() -> Result<(Vec<u8>, Zeroizing<Vec<u8>>), AlgorithmError> {
#[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
{
return crate::algorithms::KeypairResultExt::into_algorithm_keypair(
crypto_ml_kem_768::generate_ml_kem_768_keypair(),
Self::ALG,
);
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
pub fn encapsulate(public_key: &[u8]) -> Result<(Zeroizing<Vec<u8>>, Vec<u8>), AlgorithmError> {
#[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
{
let (ct, ss) = crypto_ml_kem_768::ml_kem_768_encapsulate(public_key)
.map_err(AlgorithmError::from)?;
return Ok((ss, ct));
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
let _ = public_key;
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
pub fn decapsulate(
ciphertext: &[u8],
secret_key: &[u8],
) -> Result<Zeroizing<Vec<u8>>, AlgorithmError> {
#[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
{
return crypto_ml_kem_768::ml_kem_768_decapsulate(ciphertext, secret_key)
.map_err(AlgorithmError::from);
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
let _ = (ciphertext, secret_key);
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
}