71 lines
2.6 KiB
Rust
71 lines
2.6 KiB
Rust
//! Fixed Ed25519 verification; no algorithm negotiation or trust-key discovery.
|
|
use anyhow::Result;
|
|
use ed25519_dalek::{Signature, VerifyingKey};
|
|
|
|
pub trait Ed25519Verifier {
|
|
fn validate_key(&self, public_key: &[u8]) -> Result<bool>;
|
|
fn verify(&self, public_key: &[u8], message: &[u8], signature: &[u8]) -> Result<bool>;
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
pub struct DalekVerifier;
|
|
|
|
impl Ed25519Verifier for DalekVerifier {
|
|
fn validate_key(&self, public_key: &[u8]) -> Result<bool> {
|
|
let Ok(raw): Result<&[u8; 32], _> = public_key.try_into() else {
|
|
return Ok(false);
|
|
};
|
|
Ok(VerifyingKey::from_bytes(raw).is_ok_and(|key| !key.is_weak()))
|
|
}
|
|
|
|
fn verify(&self, public_key: &[u8], message: &[u8], signature: &[u8]) -> Result<bool> {
|
|
let Ok(raw): Result<&[u8; 32], _> = public_key.try_into() else {
|
|
return Ok(false);
|
|
};
|
|
let Ok(key) = VerifyingKey::from_bytes(raw) else {
|
|
return Ok(false);
|
|
};
|
|
let Ok(signature) = Signature::from_slice(signature) else {
|
|
return Ok(false);
|
|
};
|
|
// Reject weak points and scalar/point malleability. This is raw Ed25519,
|
|
// never Ed25519ph: the exact stored bytes are the signed message.
|
|
Ok(key.verify_strict(message, &signature).is_ok())
|
|
}
|
|
}
|
|
|
|
/// Existing Linux verifier semantics, retained explicitly during extraction.
|
|
#[cfg(all(unix, feature = "openssl-verifier"))]
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
pub struct OpenSslVerifier;
|
|
|
|
#[cfg(all(unix, feature = "openssl-verifier"))]
|
|
impl Ed25519Verifier for OpenSslVerifier {
|
|
fn validate_key(&self, public_key: &[u8]) -> Result<bool> {
|
|
if public_key.len() != 32 {
|
|
return Ok(false);
|
|
}
|
|
openssl::pkey::PKey::public_key_from_raw_bytes(public_key, openssl::pkey::Id::ED25519)?;
|
|
Ok(true)
|
|
}
|
|
|
|
fn verify(&self, public_key: &[u8], message: &[u8], signature: &[u8]) -> Result<bool> {
|
|
use openssl::{
|
|
pkey::{Id, PKey},
|
|
sign::Verifier,
|
|
};
|
|
if public_key.len() != 32 || signature.len() != 64 {
|
|
return Ok(false);
|
|
}
|
|
let key = PKey::public_key_from_raw_bytes(public_key, Id::ED25519)?;
|
|
Ok(Verifier::new_without_digest(&key)?.verify_oneshot(signature, message)?)
|
|
}
|
|
}
|
|
|
|
/// Preserve the existing Linux OpenSSL profile when selected by its adapter;
|
|
/// Windows never depends on OpenSSL, including workspace feature unification.
|
|
#[cfg(all(unix, feature = "openssl-verifier"))]
|
|
pub type DefaultVerifier = OpenSslVerifier;
|
|
#[cfg(not(all(unix, feature = "openssl-verifier")))]
|
|
pub type DefaultVerifier = DalekVerifier;
|