use crate::AlgorithmError;
use crypto_core::Algorithm;
pub(crate) fn validate_compressed_public_key(
algorithm: Algorithm,
public_key: &[u8],
) -> Result<(), AlgorithmError> {
match algorithm {
Algorithm::P256 => {
#[cfg(all(feature = "p256", any(feature = "native", feature = "wasm")))]
{
crypto_p256::decompress_public_key(public_key)
.map(|_| ())
.map_err(|_| AlgorithmError::InvalidKey(algorithm))
}
#[cfg(not(all(feature = "p256", any(feature = "native", feature = "wasm"))))]
{
let _ = public_key;
Err(AlgorithmError::UnsupportedAlgorithm(algorithm))
}
}
Algorithm::P384 => {
#[cfg(all(feature = "p384", any(feature = "native", feature = "wasm")))]
{
crypto_p384::decompress_public_key(public_key)
.map(|_| ())
.map_err(|_| AlgorithmError::InvalidKey(algorithm))
}
#[cfg(not(all(feature = "p384", any(feature = "native", feature = "wasm"))))]
{
let _ = public_key;
Err(AlgorithmError::UnsupportedAlgorithm(algorithm))
}
}
Algorithm::P521 => {
#[cfg(all(feature = "p521", any(feature = "native", feature = "wasm")))]
{
crypto_p521::decompress_public_key(public_key)
.map(|_| ())
.map_err(|_| AlgorithmError::InvalidKey(algorithm))
}
#[cfg(not(all(feature = "p521", any(feature = "native", feature = "wasm"))))]
{
let _ = public_key;
Err(AlgorithmError::UnsupportedAlgorithm(algorithm))
}
}
Algorithm::Secp256k1 => {
#[cfg(all(feature = "secp256k1", any(feature = "native", feature = "wasm")))]
{
crypto_secp256k1::decompress_public_key(public_key)
.map(|_| ())
.map_err(|_| AlgorithmError::InvalidKey(algorithm))
}
#[cfg(not(all(feature = "secp256k1", any(feature = "native", feature = "wasm"))))]
{
let _ = public_key;
Err(AlgorithmError::UnsupportedAlgorithm(algorithm))
}
}
_ => Ok(()),
}
}