//! [`WolfSslProvider`] — the main provider struct implementing //! [`CryptoProvider`] for the wolfSSL / wolfCrypt library. //! //! Capabilities are live-probed on first construction. Benchmark results //! feed into the `efficiency_score` fields of [`ProviderCapabilities`]. use std::sync::OnceLock; use zeroize::Zeroizing; use ccc_crypto_core::{ algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, KemAlgorithm, MacAlgorithm}, capabilities::ProviderCapabilities, error::CryptoError, provider::{AeadProvider, CryptoProvider, HashProvider, KdfProvider, KemProvider, MacProvider}, types::{BenchmarkReport, KemEncapResult, KemKeyPair, SelfTestReport}, }; use crate::{aead, capabilities, hash, kdf, kem, mac, self_test}; // ────────────────────────────────────────────────────────────────────────────── // WolfSslProvider // ────────────────────────────────────────────────────────────────────────────── /// The wolfSSL / wolfCrypt crypto provider. /// /// Constructed by [`crate::init()`] and registered into the global /// [`ProviderRegistry`][ccc_crypto_core::registry::ProviderRegistry]. /// /// Capabilities and benchmark results are computed lazily on first access to /// avoid slowing down startup if the provider is never queried. pub struct WolfSslProvider { /// Lazy-initialised capabilities + benchmark results. caps: OnceLock, bench: OnceLock, } impl WolfSslProvider { /// Create a new wolfSSL provider. /// /// Does not run probes or benchmarks at construction time — those are /// deferred to the first call to [`capabilities()`] / [`benchmark()`]. pub fn new() -> Self { Self { caps: OnceLock::new(), bench: OnceLock::new(), } } /// Return the cached benchmark report, running the benchmark if needed. fn get_or_run_benchmark(&self) -> &BenchmarkReport { self.bench.get_or_init(|| { log::info!("[ccc-crypto-wolfssl] running throughput benchmark…"); let report = capabilities::run_benchmark(); log::info!( "[ccc-crypto-wolfssl] benchmark complete ({} algos)", report.results.len() ); report }) } } impl Default for WolfSslProvider { fn default() -> Self { Self::new() } } // ────────────────────────────────────────────────────────────────────────────── // Primitive trait implementations — thin delegation to module functions // ────────────────────────────────────────────────────────────────────────────── impl AeadProvider for WolfSslProvider { fn encrypt_aead( &self, algo: AeadAlgorithm, key: &[u8], nonce: &[u8], plaintext: &[u8], aad: &[u8], ) -> Result, CryptoError> { aead::encrypt(algo, key, nonce, plaintext, aad) } fn decrypt_aead( &self, algo: AeadAlgorithm, key: &[u8], nonce: &[u8], ciphertext_and_tag: &[u8], aad: &[u8], ) -> Result, CryptoError> { aead::decrypt(algo, key, nonce, ciphertext_and_tag, aad) } } impl KdfProvider for WolfSslProvider { fn derive_key( &self, algo: KdfAlgorithm, ikm: &[u8], salt: &[u8], info: &[u8], length: usize, ) -> Result>, CryptoError> { kdf::derive_key(algo, ikm, salt, info, length) } } impl MacProvider for WolfSslProvider { fn compute_mac( &self, algo: MacAlgorithm, key: &[u8], data: &[u8], ) -> Result, CryptoError> { mac::compute_mac(algo, key, data) } fn verify_mac( &self, algo: MacAlgorithm, key: &[u8], data: &[u8], mac_bytes: &[u8], ) -> Result { mac::verify_mac(algo, key, data, mac_bytes) } } impl HashProvider for WolfSslProvider { fn hash(&self, algo: HashAlgorithm, data: &[u8]) -> Result, CryptoError> { hash::hash(algo, data) } } /// `WolfSslProvider` also implements `KemProvider` but it is not part of the /// `CryptoProvider` supertrait. Bridge functions call it directly. impl KemProvider for WolfSslProvider { fn generate_keypair(&self, algo: KemAlgorithm) -> Result { kem::generate_keypair(algo) } fn encapsulate( &self, algo: KemAlgorithm, public_key: &[u8], ) -> Result { kem::encapsulate(algo, public_key) } fn decapsulate( &self, algo: KemAlgorithm, private_key: &[u8], ciphertext: &[u8], ) -> Result>, CryptoError> { kem::decapsulate(algo, private_key, ciphertext) } } // ────────────────────────────────────────────────────────────────────────────── // CryptoProvider umbrella impl // ────────────────────────────────────────────────────────────────────────────── impl CryptoProvider for WolfSslProvider { fn provider_name(&self) -> &'static str { "wolfssl" } /// `true` only for a `fips` build whose FIPS self-tests passed. fn fips_mode(&self) -> bool { crate::fips::fips_mode() } fn capabilities(&self) -> ProviderCapabilities { self.caps .get_or_init(|| { let bench = self.get_or_run_benchmark(); capabilities::probe_capabilities(Some(bench)) }) .clone() } fn self_test(&self) -> SelfTestReport { self_test::run() } fn benchmark(&self) -> BenchmarkReport { self.get_or_run_benchmark().clone() } }