1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#[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))
}
}