136 lines
5.0 KiB
Rust
136 lines
5.0 KiB
Rust
//! Capability structs describing what each provider can do.
|
||
//!
|
||
//! [`ProviderCapabilities`] is populated by a provider at startup via a live
|
||
//! probe call (not hard-coded). The populated map is then exposed to Dart
|
||
//! via the `ccc_provider_capabilities()` bridge function, replacing the former
|
||
//! compile-time stub in `ccc_provider_spec.dart`.
|
||
|
||
use std::collections::HashMap;
|
||
|
||
use serde::{Deserialize, Serialize};
|
||
|
||
use crate::algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, KemAlgorithm, MacAlgorithm};
|
||
|
||
// ──────────────────────────────────────────────────────────────────────────────
|
||
|
||
/// Runtime capability descriptor for a single algorithm within a provider.
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct AlgorithmCapability {
|
||
/// Whether the algorithm is usable on this platform / build.
|
||
///
|
||
/// Determined by a live probe call at startup, not by compile-time flags.
|
||
pub available: bool,
|
||
|
||
/// Whether the algorithm produces byte-identical output for the same
|
||
/// key/nonce/plaintext across providers.
|
||
///
|
||
/// Must be `true` for any algorithm used in the shared cipher chain.
|
||
/// Cross-provider conformance tests verify this at build time.
|
||
pub deterministic_io: bool,
|
||
|
||
/// Throughput score normalised to 0–100 (0 = slowest, 100 = fastest).
|
||
///
|
||
/// Populated after `WolfSslProvider::benchmark()` runs at app start.
|
||
/// Stored so Dart can rank providers when multiple are available.
|
||
pub efficiency_score: u8,
|
||
|
||
/// Reliability score normalised to 0–100.
|
||
///
|
||
/// Derived from the self-test pass rate: `(passed / total) * 100`.
|
||
pub reliability_score: u8,
|
||
}
|
||
|
||
impl AlgorithmCapability {
|
||
/// Create a placeholder capability that marks an algorithm as unavailable.
|
||
///
|
||
/// Used while the provider is initialising or when a probe fails.
|
||
pub fn unavailable() -> Self {
|
||
Self {
|
||
available: false,
|
||
deterministic_io: false,
|
||
efficiency_score: 0,
|
||
reliability_score: 0,
|
||
}
|
||
}
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────────────────────────
|
||
|
||
/// The complete runtime capability matrix for one crypto provider.
|
||
///
|
||
/// Returned by [`CryptoProvider::capabilities()`] and forwarded to Dart as
|
||
/// `CapabilitiesDto`.
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ProviderCapabilities {
|
||
/// The provider's canonical name (matches `CccCryptoProvider` enum in Dart).
|
||
pub provider_name: String,
|
||
|
||
/// AEAD algorithm capabilities, keyed by [`AeadAlgorithm`].
|
||
pub aead: HashMap<AeadAlgorithm, AlgorithmCapability>,
|
||
|
||
/// KDF algorithm capabilities, keyed by [`KdfAlgorithm`].
|
||
pub kdf: HashMap<KdfAlgorithm, AlgorithmCapability>,
|
||
|
||
/// MAC algorithm capabilities, keyed by [`MacAlgorithm`].
|
||
pub mac: HashMap<MacAlgorithm, AlgorithmCapability>,
|
||
|
||
/// Hash algorithm capabilities, keyed by [`HashAlgorithm`].
|
||
pub hash: HashMap<HashAlgorithm, AlgorithmCapability>,
|
||
|
||
/// KEM algorithm capabilities, keyed by [`KemAlgorithm`].
|
||
pub kem: HashMap<KemAlgorithm, AlgorithmCapability>,
|
||
}
|
||
|
||
impl ProviderCapabilities {
|
||
/// Create an empty capability set for the given provider.
|
||
pub fn empty(provider_name: impl Into<String>) -> Self {
|
||
Self {
|
||
provider_name: provider_name.into(),
|
||
aead: HashMap::new(),
|
||
kdf: HashMap::new(),
|
||
mac: HashMap::new(),
|
||
hash: HashMap::new(),
|
||
kem: HashMap::new(),
|
||
}
|
||
}
|
||
|
||
/// Register an AEAD capability entry.
|
||
pub fn with_aead(mut self, algo: AeadAlgorithm, cap: AlgorithmCapability) -> Self {
|
||
self.aead.insert(algo, cap);
|
||
self
|
||
}
|
||
|
||
/// Register a KDF capability entry.
|
||
pub fn with_kdf(mut self, algo: KdfAlgorithm, cap: AlgorithmCapability) -> Self {
|
||
self.kdf.insert(algo, cap);
|
||
self
|
||
}
|
||
|
||
/// Register a MAC capability entry.
|
||
pub fn with_mac(mut self, algo: MacAlgorithm, cap: AlgorithmCapability) -> Self {
|
||
self.mac.insert(algo, cap);
|
||
self
|
||
}
|
||
|
||
/// Register a Hash capability entry.
|
||
pub fn with_hash(mut self, algo: HashAlgorithm, cap: AlgorithmCapability) -> Self {
|
||
self.hash.insert(algo, cap);
|
||
self
|
||
}
|
||
|
||
/// Register a KEM capability entry.
|
||
pub fn with_kem(mut self, algo: KemAlgorithm, cap: AlgorithmCapability) -> Self {
|
||
self.kem.insert(algo, cap);
|
||
self
|
||
}
|
||
|
||
/// Return `true` if any algorithm in any category is available.
|
||
pub fn has_any_available(&self) -> bool {
|
||
self.aead.values().any(|c| c.available)
|
||
|| self.kdf.values().any(|c| c.available)
|
||
|| self.mac.values().any(|c| c.available)
|
||
|| self.hash.values().any(|c| c.available)
|
||
|| self.kem.values().any(|c| c.available)
|
||
}
|
||
}
|