//! [`OqsProvider`]: the [`CryptoProvider`] implementation. //! //! Only KEMs are offered. Every AEAD, KDF, MAC and hash call fails with //! `UnsupportedAlgorithm`; without the `liboqs` feature every KEM call fails //! with `FeatureNotCompiled`. use std::sync::OnceLock; use zeroize::Zeroizing; use ccc_crypto_core::{ algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, KemAlgorithm, MacAlgorithm}, capabilities::{AlgorithmCapability, ProviderCapabilities}, error::CryptoError, provider::{AeadProvider, CryptoProvider, HashProvider, KdfProvider, KemProvider, MacProvider}, types::{BenchmarkReport, KemEncapResult, KemKeyPair, SelfTestReport}, }; use crate::PROVIDER_NAME; /// The liboqs provider. The self-test and capabilities are computed on /// first use and cached. #[derive(Default)] pub struct OqsProvider { caps: OnceLock, kats: OnceLock, } impl OqsProvider { /// Create a provider; no work is done until it is queried. pub fn new() -> Self { Self::default() } fn kats(&self) -> &SelfTestReport { self.kats.get_or_init(run_self_test) } } #[cfg(feature = "liboqs")] fn run_self_test() -> SelfTestReport { crate::self_test::run() } #[cfg(not(feature = "liboqs"))] fn run_self_test() -> SelfTestReport { SelfTestReport::finalise(PROVIDER_NAME, Vec::new()) } fn unsupported(what: &str) -> Result { Err(CryptoError::UnsupportedAlgorithm(format!( "the oqs provider offers KEMs only, not {what}" ))) } impl AeadProvider for OqsProvider { fn encrypt_aead( &self, algo: AeadAlgorithm, _key: &[u8], _nonce: &[u8], _plaintext: &[u8], _aad: &[u8], ) -> Result, CryptoError> { unsupported(algo.name()) } fn decrypt_aead( &self, algo: AeadAlgorithm, _key: &[u8], _nonce: &[u8], _ciphertext_and_tag: &[u8], _aad: &[u8], ) -> Result, CryptoError> { unsupported(algo.name()) } } impl KdfProvider for OqsProvider { fn derive_key( &self, algo: KdfAlgorithm, _ikm: &[u8], _salt: &[u8], _info: &[u8], _length: usize, ) -> Result>, CryptoError> { unsupported(algo.name()) } } impl MacProvider for OqsProvider { fn compute_mac( &self, algo: MacAlgorithm, _key: &[u8], _data: &[u8], ) -> Result, CryptoError> { unsupported(algo.name()) } fn verify_mac( &self, algo: MacAlgorithm, _key: &[u8], _data: &[u8], _mac: &[u8], ) -> Result { unsupported(algo.name()) } } impl HashProvider for OqsProvider { fn hash(&self, algo: HashAlgorithm, _data: &[u8]) -> Result, CryptoError> { unsupported(algo.name()) } } #[cfg(feature = "liboqs")] impl KemProvider for OqsProvider { fn generate_keypair(&self, algo: KemAlgorithm) -> Result { crate::kem::generate_keypair(algo) } fn encapsulate( &self, algo: KemAlgorithm, public_key: &[u8], ) -> Result { crate::kem::encapsulate(algo, public_key) } fn decapsulate( &self, algo: KemAlgorithm, private_key: &[u8], ciphertext: &[u8], ) -> Result>, CryptoError> { crate::kem::decapsulate(algo, private_key, ciphertext) } } #[cfg(not(feature = "liboqs"))] fn not_compiled(algo: KemAlgorithm) -> Result { Err(CryptoError::FeatureNotCompiled(format!( "{}: ccc-crypto-oqs was built without the `liboqs` feature", algo.name() ))) } #[cfg(not(feature = "liboqs"))] impl KemProvider for OqsProvider { fn generate_keypair(&self, algo: KemAlgorithm) -> Result { not_compiled(algo) } fn encapsulate( &self, algo: KemAlgorithm, _public_key: &[u8], ) -> Result { not_compiled(algo) } fn decapsulate( &self, algo: KemAlgorithm, _private_key: &[u8], _ciphertext: &[u8], ) -> Result>, CryptoError> { not_compiled(algo) } } /// KEMs this build offers. #[cfg(feature = "liboqs")] fn offered() -> &'static [KemAlgorithm] { &crate::kem::OFFERED } #[cfg(not(feature = "liboqs"))] fn offered() -> &'static [KemAlgorithm] { &[] } /// A KEM is available only when its KAT passed (fail closed, D-28). The /// KAT runs keygen, encapsulation and decapsulation through the same code /// as real calls, so no separate probe is needed. fn probe_capabilities(self_test: &SelfTestReport) -> ProviderCapabilities { let mut caps = ProviderCapabilities::empty(PROVIDER_NAME); for algo in KemAlgorithm::ALL { let mut kats = self_test .results .iter() .filter(|r| r.algo_id == algo as u32) .peekable(); let ok = offered().contains(&algo) && kats.peek().is_some() && kats.all(|r| r.passed); let cap = if ok { AlgorithmCapability { available: true, deterministic_io: false, efficiency_score: 50, reliability_score: 100, } } else { AlgorithmCapability::unavailable() }; caps = caps.with_kem(algo, cap); } caps } impl CryptoProvider for OqsProvider { fn provider_name(&self) -> &'static str { PROVIDER_NAME } /// liboqs is not a FIPS module. fn fips_mode(&self) -> bool { false } fn capabilities(&self) -> ProviderCapabilities { self.caps .get_or_init(|| probe_capabilities(self.kats())) .clone() } fn self_test(&self) -> SelfTestReport { self.kats().clone() } /// No AEAD throughput to measure. fn benchmark(&self) -> BenchmarkReport { BenchmarkReport { provider_name: PROVIDER_NAME.into(), results: Vec::new(), } } } #[cfg(test)] mod tests { use super::*; #[test] fn identity_and_non_kem_calls() { let p = OqsProvider::new(); assert_eq!(p.provider_name(), "oqs"); assert!(!p.fips_mode()); assert!(matches!( p.hash(HashAlgorithm::Sha256, b"x"), Err(CryptoError::UnsupportedAlgorithm(_)) )); assert!(p .encrypt_aead(AeadAlgorithm::AesGcm256, &[0; 32], &[0; 12], b"", b"") .is_err()); assert!(p.benchmark().results.is_empty()); } #[test] fn capabilities_match_the_build() { let p = OqsProvider::new(); assert!(p.self_test().all_passed); let caps = p.capabilities(); assert_eq!(caps.provider_name, "oqs"); for algo in KemAlgorithm::ALL { assert_eq!( caps.kem[&algo].available, offered().contains(&algo), "{}", algo.name() ); } assert!(caps.aead.is_empty()); } #[cfg(not(feature = "liboqs"))] #[test] fn stub_offers_nothing() { let p = OqsProvider::new(); assert!(matches!( p.generate_keypair(KemAlgorithm::Hqc192), Err(CryptoError::FeatureNotCompiled(_)) )); assert!(!p.capabilities().has_any_available()); } }