#[cfg(feature = "default-resolver")]
mod default;
#[cfg(feature = "libsodium-resolver")]
mod libsodium;
#[cfg(feature = "ring-resolver")]
mod ring;
#[cfg(feature = "hfs")]
use crate::params::KemChoice;
#[cfg(feature = "hfs")]
use crate::types::Kem;
use crate::{
params::{CipherChoice, DHChoice, HashChoice},
types::{Cipher, Dh, Hash, Random},
};
#[cfg(feature = "default-resolver")]
pub use self::default::DefaultResolver;
#[cfg(feature = "libsodium-resolver")]
pub use self::libsodium::SodiumResolver;
#[cfg(feature = "ring-resolver")]
pub use self::ring::RingResolver;
pub type BoxedCryptoResolver = Box<dyn CryptoResolver + Send>;
pub trait CryptoResolver {
fn resolve_rng(&self) -> Option<Box<dyn Random>>;
fn resolve_dh(&self, choice: &DHChoice) -> Option<Box<dyn Dh>>;
fn resolve_hash(&self, choice: &HashChoice) -> Option<Box<dyn Hash>>;
fn resolve_cipher(&self, choice: &CipherChoice) -> Option<Box<dyn Cipher>>;
#[cfg(feature = "hfs")]
fn resolve_kem(&self, _choice: &KemChoice) -> Option<Box<dyn Kem>> {
None
}
}
pub struct FallbackResolver {
preferred: BoxedCryptoResolver,
fallback: BoxedCryptoResolver,
}
impl FallbackResolver {
pub fn new(preferred: BoxedCryptoResolver, fallback: BoxedCryptoResolver) -> Self {
Self { preferred, fallback }
}
}
impl CryptoResolver for FallbackResolver {
fn resolve_rng(&self) -> Option<Box<dyn Random>> {
self.preferred.resolve_rng().or_else(|| self.fallback.resolve_rng())
}
fn resolve_dh(&self, choice: &DHChoice) -> Option<Box<dyn Dh>> {
self.preferred.resolve_dh(choice).or_else(|| self.fallback.resolve_dh(choice))
}
fn resolve_hash(&self, choice: &HashChoice) -> Option<Box<dyn Hash>> {
self.preferred.resolve_hash(choice).or_else(|| self.fallback.resolve_hash(choice))
}
fn resolve_cipher(&self, choice: &CipherChoice) -> Option<Box<dyn Cipher>> {
self.preferred.resolve_cipher(choice).or_else(|| self.fallback.resolve_cipher(choice))
}
#[cfg(feature = "hfs")]
fn resolve_kem(&self, choice: &KemChoice) -> Option<Box<dyn Kem>> {
self.preferred.resolve_kem(choice).or_else(|| self.fallback.resolve_kem(choice))
}
}