#![allow(clippy::needless_return)]
use crate::traits::SignatureAlgorithm;
use crate::AlgorithmError;
use crypto_core::Algorithm;
use zeroize::Zeroizing;
pub struct P256Algo;
impl SignatureAlgorithm for P256Algo {
const ALG: Algorithm = Algorithm::P256;
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_p256::generate_p256_keypair(),
Self::ALG,
);
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
fn sign(secret: &[u8], msg: &[u8]) -> Result<Vec<u8>, AlgorithmError> {
#[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
{
return crypto_p256::sign_p256_der_prehash(secret, msg).map_err(AlgorithmError::from);
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
let _ = (secret, msg);
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
fn verify(public: &[u8], msg: &[u8], sig: &[u8]) -> Result<(), AlgorithmError> {
#[cfg(any(feature = "native", all(feature = "wasm", target_arch = "wasm32")))]
{
return crypto_p256::verify_p256_der_prehash(sig, msg, public)
.map_err(|error| crate::algorithms::map_verify_error(Self::ALG, error));
}
#[cfg(not(any(feature = "native", all(feature = "wasm", target_arch = "wasm32"))))]
{
let _ = (public, msg, sig);
Err(AlgorithmError::UnsupportedAlgorithm(Self::ALG))
}
}
}
impl P256Algo {
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_p256::derive_p256_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))
}
}
}