//! Runtime capability probing and benchmarking for wolfSSL. //! //! [`probe_capabilities`] runs a one-byte encrypt/decrypt through each //! algorithm at startup and flips `available` based on whether it succeeds. //! //! [`run_benchmark`] runs 100 × 1 MB encryptions per AEAD algorithm to //! produce throughput scores. Results are stored in the returned //! [`BenchmarkReport`] and normalised to a 0–100 `efficiency_score`. use std::time::Instant; use ccc_crypto_core::{ algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, KemAlgorithm, MacAlgorithm}, capabilities::{AlgorithmCapability, ProviderCapabilities}, types::{AlgoBenchResult, BenchmarkReport}, }; // ────────────────────────────────────────────────────────────────────────────── // Capability probing // ────────────────────────────────────────────────────────────────────────────── /// Probe every algorithm by performing a minimal one-shot operation. /// /// Sets `available = true` only when the wolfCrypt call returns `0` (success). /// Sets `efficiency_score` / `reliability_score` from `benchmarks` results /// when provided, otherwise leaves them at 0 (to be populated later). pub fn probe_capabilities(benchmarks: Option<&BenchmarkReport>) -> ProviderCapabilities { let mut caps = ProviderCapabilities::empty("wolfssl"); // ── AEAD ───────────────────────────────────────────────────────────────── for algo in [ AeadAlgorithm::AesGcm256, AeadAlgorithm::ChaCha20Poly1305, AeadAlgorithm::XChaCha20Poly1305, ] { let nonce_len = algo.nonce_len(); let key = vec![0u8; algo.key_len()]; let nonce = vec![0u8; nonce_len]; let available = crate::aead::encrypt(algo, &key, &nonce, b"x", b"").is_ok(); let efficiency = benchmarks .and_then(|b| b.results.iter().find(|r| r.algo_id == algo as u32)) .map(|r| r.efficiency_score) .unwrap_or(0); caps = caps.with_aead( algo, AlgorithmCapability { available, deterministic_io: true, // wolfSSL output is deterministic for same key/nonce efficiency_score: efficiency, reliability_score: if available { 100 } else { 0 }, }, ); } // Ascon-128a is not in this wolfSSL build. for unavailable in [ AeadAlgorithm::Ascon128a, AeadAlgorithm::AesGcmSiv256, AeadAlgorithm::AsconAead128, ] { caps = caps.with_aead(unavailable, AlgorithmCapability::unavailable()); } // ── KDF ────────────────────────────────────────────────────────────────── for algo in [ KdfAlgorithm::Sha256, KdfAlgorithm::Sha384, KdfAlgorithm::Sha512, ] { let available = crate::kdf::derive_key(algo, b"key", b"salt", b"info", 32).is_ok(); caps = caps.with_kdf( algo, AlgorithmCapability { available, deterministic_io: true, efficiency_score: 80, // HKDF is fast; benchmark not run for KDF reliability_score: if available { 100 } else { 0 }, }, ); } for algo in [KdfAlgorithm::Blake2b512, KdfAlgorithm::Argon2id] { let salt: &[u8] = if algo == KdfAlgorithm::Argon2id { b"saltsalt" } else { b"" }; let available = crate::kdf::derive_key(algo, b"key", salt, b"", 32).is_ok(); caps = caps.with_kdf( algo, AlgorithmCapability { available, deterministic_io: true, efficiency_score: if algo == KdfAlgorithm::Argon2id { 10 } else { 70 }, reliability_score: if available { 100 } else { 0 }, }, ); } caps = caps.with_kdf(KdfAlgorithm::Kmac256, AlgorithmCapability::unavailable()); // ── MAC ────────────────────────────────────────────────────────────────── for algo in [ MacAlgorithm::HmacSha256, MacAlgorithm::HmacSha384, MacAlgorithm::HmacSha512, ] { let available = crate::mac::compute_mac(algo, b"key", b"data").is_ok(); caps = caps.with_mac( algo, AlgorithmCapability { available, deterministic_io: true, efficiency_score: 85, reliability_score: if available { 100 } else { 0 }, }, ); } { let available = crate::mac::compute_mac(MacAlgorithm::Blake2bMac, b"key-that-is-16-by", b"data") .is_ok(); caps = caps.with_mac( MacAlgorithm::Blake2bMac, AlgorithmCapability { available, deterministic_io: true, efficiency_score: 90, reliability_score: if available { 100 } else { 0 }, }, ); } caps = caps.with_mac(MacAlgorithm::Poly1305, AlgorithmCapability::unavailable()); // ── Hash ───────────────────────────────────────────────────────────────── for algo in [ HashAlgorithm::Sha256, HashAlgorithm::Sha384, HashAlgorithm::Sha512, HashAlgorithm::Blake2b512, HashAlgorithm::Sha3_256, HashAlgorithm::Sha3_512, ] { let available = crate::hash::hash(algo, b"probe").is_ok(); caps = caps.with_hash( algo, AlgorithmCapability { available, deterministic_io: true, efficiency_score: 90, reliability_score: if available { 100 } else { 0 }, }, ); } // ── KEM ────────────────────────────────────────────────────────────────── for algo in [KemAlgorithm::X25519, KemAlgorithm::X448] { let available = crate::kem::generate_keypair(algo).is_ok(); caps = caps.with_kem( algo, AlgorithmCapability { available, deterministic_io: false, // key generation is randomised efficiency_score: 80, reliability_score: if available { 100 } else { 0 }, }, ); } for algo in [ KemAlgorithm::MlKem768, KemAlgorithm::MlKem1024, KemAlgorithm::ClassicMcEliece460896, KemAlgorithm::ClassicMcEliece460896f, KemAlgorithm::Hqc192, KemAlgorithm::FrodoKem976Shake, ] { caps = caps.with_kem(algo, AlgorithmCapability::unavailable()); } caps } // ────────────────────────────────────────────────────────────────────────────── // Throughput benchmarking // ────────────────────────────────────────────────────────────────────────────── /// Run 100 × 1 MB AEAD encryptions for each available algorithm. /// /// Returns a [`BenchmarkReport`] with raw throughput values and normalised /// `efficiency_score` (0–100) relative to the fastest algorithm measured. pub fn run_benchmark() -> BenchmarkReport { const MB: usize = 1024 * 1024; const ITERS: u32 = 100; let plaintext = vec![0xABu8; MB]; let algos = [ AeadAlgorithm::AesGcm256, AeadAlgorithm::ChaCha20Poly1305, AeadAlgorithm::XChaCha20Poly1305, ]; let mut raw_results: Vec<(AeadAlgorithm, f64)> = Vec::new(); for algo in algos { let key = vec![0u8; algo.key_len()]; let nonce = vec![0u8; algo.nonce_len()]; // Probe first; skip unavailable algorithms. if crate::aead::encrypt(algo, &key, &nonce, b"x", b"").is_err() { continue; } let start = Instant::now(); for _ in 0..ITERS { let _ = crate::aead::encrypt(algo, &key, &nonce, &plaintext, b""); } let elapsed_secs = start.elapsed().as_secs_f64(); let throughput = (MB as f64 * ITERS as f64) / (1024.0 * 1024.0 * elapsed_secs); raw_results.push((algo, throughput)); } // Normalise to 0–100 relative to the fastest result. let max_throughput = raw_results .iter() .map(|(_, t)| *t) .fold(f64::NEG_INFINITY, f64::max); let results: Vec = raw_results .into_iter() .map(|(algo, throughput)| { let score = if max_throughput > 0.0 { ((throughput / max_throughput) * 100.0).round() as u8 } else { 0 }; AlgoBenchResult { algo_id: algo as u32, algo_name: algo.name().into(), throughput_mbps: throughput, efficiency_score: score, } }) .collect(); BenchmarkReport { provider_name: "wolfssl".into(), results, } }