#[cfg(feature = "alloc")]
use alloc::{
string::ToString,
vec::Vec,
};
use crate::api::{
Algorithm,
SignatureOperations,
};
use crate::error::Result;
use crate::security::SecurityValidator;
use crate::traits::{
SigKeypair,
SigPublicKey,
SigSecretKey,
};
#[cfg(feature = "alloc")]
#[derive(Clone)]
pub struct LibQSignatureProvider {
security_validator: SecurityValidator,
}
#[cfg(feature = "alloc")]
impl LibQSignatureProvider {
pub fn new() -> Result<Self> {
Ok(Self {
security_validator: SecurityValidator::new()?,
})
}
}
#[cfg(feature = "alloc")]
impl SignatureOperations for LibQSignatureProvider {
fn generate_keypair(
&self,
algorithm: Algorithm,
randomness: Option<&[u8]>,
) -> Result<SigKeypair> {
self.security_validator
.validate_algorithm_category(algorithm, crate::api::AlgorithmCategory::Signature)?;
if let Some(rng) = randomness {
self.security_validator.validate_randomness(rng)?;
}
match algorithm {
Algorithm::MlDsa44 | Algorithm::MlDsa65 | Algorithm::MlDsa87 => {
Err(crate::error::Error::NotImplemented {
feature: "ML-DSA implementations are provided by the main lib-q crate"
.to_string(),
})
}
Algorithm::FnDsa | Algorithm::FnDsa512 | Algorithm::FnDsa1024 => {
Err(crate::error::Error::NotImplemented {
feature: "FN-DSA implementations are provided by the main lib-q crate"
.to_string(),
})
}
Algorithm::SlhDsaSha256128fRobust |
Algorithm::SlhDsaSha256192fRobust |
Algorithm::SlhDsaSha256256fRobust |
Algorithm::SlhDsaShake256128fRobust |
Algorithm::SlhDsaShake256192fRobust |
Algorithm::SlhDsaShake256256fRobust => Err(crate::error::Error::NotImplemented {
feature: "SLH-DSA implementations are provided by the main lib-q crate".to_string(),
}),
_ => Err(crate::error::Error::InvalidAlgorithm {
algorithm: "Algorithm not supported for signature operations",
}),
}
}
fn sign(
&self,
algorithm: Algorithm,
secret_key: &SigSecretKey,
message: &[u8],
randomness: Option<&[u8]>,
) -> Result<Vec<u8>> {
self.security_validator
.validate_algorithm_category(algorithm, crate::api::AlgorithmCategory::Signature)?;
self.security_validator
.validate_secret_key(algorithm, secret_key.as_bytes())?;
self.security_validator
.validate_signature_message(message)?;
if let Some(rng) = randomness {
self.security_validator.validate_randomness(rng)?;
}
match algorithm {
Algorithm::MlDsa44 | Algorithm::MlDsa65 | Algorithm::MlDsa87 => {
Err(crate::error::Error::NotImplemented {
feature: "ML-DSA implementations are provided by the main lib-q crate"
.to_string(),
})
}
Algorithm::FnDsa | Algorithm::FnDsa512 | Algorithm::FnDsa1024 => {
Err(crate::error::Error::NotImplemented {
feature: "FN-DSA implementations are provided by the main lib-q crate"
.to_string(),
})
}
Algorithm::SlhDsaSha256128fRobust |
Algorithm::SlhDsaSha256192fRobust |
Algorithm::SlhDsaSha256256fRobust |
Algorithm::SlhDsaShake256128fRobust |
Algorithm::SlhDsaShake256192fRobust |
Algorithm::SlhDsaShake256256fRobust => Err(crate::error::Error::NotImplemented {
feature: "SLH-DSA implementations are provided by the main lib-q crate".to_string(),
}),
_ => Err(crate::error::Error::InvalidAlgorithm {
algorithm: "Algorithm not supported for signature operations",
}),
}
}
fn verify(
&self,
algorithm: Algorithm,
public_key: &SigPublicKey,
message: &[u8],
signature: &[u8],
) -> Result<bool> {
self.security_validator
.validate_algorithm_category(algorithm, crate::api::AlgorithmCategory::Signature)?;
self.security_validator
.validate_public_key(algorithm, public_key.as_bytes())?;
self.security_validator
.validate_signature_message(message)?;
self.security_validator
.validate_signature(algorithm, signature)?;
match algorithm {
Algorithm::MlDsa44 | Algorithm::MlDsa65 | Algorithm::MlDsa87 => {
Err(crate::error::Error::NotImplemented {
feature: "ML-DSA implementations are provided by the main lib-q crate"
.to_string(),
})
}
Algorithm::FnDsa | Algorithm::FnDsa512 | Algorithm::FnDsa1024 => {
Err(crate::error::Error::NotImplemented {
feature: "FN-DSA implementations are provided by the main lib-q crate"
.to_string(),
})
}
Algorithm::SlhDsaSha256128fRobust |
Algorithm::SlhDsaSha256192fRobust |
Algorithm::SlhDsaSha256256fRobust |
Algorithm::SlhDsaShake256128fRobust |
Algorithm::SlhDsaShake256192fRobust |
Algorithm::SlhDsaShake256256fRobust => Err(crate::error::Error::NotImplemented {
feature: "SLH-DSA implementations are provided by the main lib-q crate".to_string(),
}),
_ => Err(crate::error::Error::InvalidAlgorithm {
algorithm: "Algorithm not supported for signature operations",
}),
}
}
}
#[cfg(test)]
#[cfg(feature = "alloc")]
mod tests {
use super::*;
#[test]
fn test_signature_provider_creation() {
let provider = LibQSignatureProvider::new();
assert!(
provider.is_ok(),
"LibQSignatureProvider should be created successfully"
);
}
#[test]
fn test_signature_provider_unsupported_algorithm() {
let provider = LibQSignatureProvider::new().unwrap();
let result = provider.generate_keypair(Algorithm::Sha3_256, None);
assert!(
result.is_err(),
"Should return error for unsupported algorithm"
);
if let Err(crate::error::Error::InvalidAlgorithm { .. }) = result {
} else {
panic!("Expected InvalidAlgorithm error");
}
}
#[test]
fn test_signature_provider_feature_flag_handling() {
let provider = LibQSignatureProvider::new().unwrap();
let result = provider.generate_keypair(Algorithm::MlDsa65, None);
assert!(
result.is_err(),
"Should return error when feature flag is not enabled"
);
if let Err(crate::error::Error::NotImplemented { feature }) = result {
assert!(
feature.contains("ML-DSA implementations are provided by the main lib-q crate"),
"Error should mention that implementations are provided by main lib-q crate"
);
} else {
panic!("Expected NotImplemented error");
}
}
}