#![allow(clippy::needless_return)]
use crate::AlgorithmError;
use crypto_core::Algorithm;
use zeroize::Zeroizing;
pub struct X25519Algo;
impl X25519Algo {
pub const ALG: Algorithm = Algorithm::X25519;
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_x25519::generate_x25519_keypair(),
Self::ALG,
);
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
pub fn derive_shared_secret(
secret_key: &[u8],
public_key: &[u8],
) -> Result<Zeroizing<Vec<u8>>, AlgorithmError> {
#[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
{
return crypto_x25519::derive_x25519_shared_secret(secret_key, public_key)
.map_err(AlgorithmError::from);
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
let _ = (secret_key, public_key);
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
}