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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
mod crypto_system;
mod dh_cache;
mod envelope;
mod guard;
mod receipt;
mod types;
#[cfg(any(test, feature = "test-util"))]
#[doc(hidden)]
pub mod tests_crypto;
pub use crypto_system::*;
use dh_cache::*;
pub(crate) use envelope::*;
pub use guard::*;
pub(crate) use receipt::*;
pub use types::*;
use super::*;
use core::convert::TryInto;
use hashlink::linked_hash_map::Entry;
use hashlink::LruCache;
impl_veilid_log_facility!("crypto");
cfg_if! {
if #[cfg(all(feature = "enable-crypto-none", feature = "enable-crypto-vld0"))] {
/// Crypto kinds in order of preference, best cryptosystem is the first one, worst is the last one
pub const VALID_CRYPTO_KINDS: [CryptoKind; 2] = [CRYPTO_KIND_VLD0, CRYPTO_KIND_NONE];
}
else if #[cfg(feature = "enable-crypto-none")] {
/// Crypto kinds in order of preference, best cryptosystem is the first one, worst is the last one
pub const VALID_CRYPTO_KINDS: [CryptoKind; 1] = [CRYPTO_KIND_NONE];
}
else if #[cfg(feature = "enable-crypto-vld0")] {
/// Crypto kinds in order of preference, best cryptosystem is the first one, worst is the last one
pub const VALID_CRYPTO_KINDS: [CryptoKind; 1] = [CRYPTO_KIND_VLD0];
}
// else if #[cfg(feature = "enable-crypto-vld1")] {
// /// Crypto kinds in order of preference, best cryptosystem is the first one, worst is the last one
// pub const VALID_CRYPTO_KINDS: [CryptoKind; 2] = [CRYPTO_KIND_VLD1, CRYPTO_KIND_VLD0];
// }
else {
compile_error!("No crypto kinds enabled, specify an enable-crypto- feature");
}
}
/// Number of cryptosystem signatures to keep on structures if many are present beyond the ones we consider valid
pub const MAX_CRYPTO_KINDS: usize = 3;
/// Return the best cryptosystem kind we support
pub(crate) fn best_crypto_kind() -> CryptoKind {
VALID_CRYPTO_KINDS[0]
}
struct CryptoInner {
dh_cache: DHCache,
dh_cache_misses: usize,
dh_cache_hits: usize,
dh_cache_lru: usize,
}
impl fmt::Debug for CryptoInner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CryptoInner")
//.field("dh_cache", &self.dh_cache)
.field("dh_cache_misses", &self.dh_cache_misses)
.field("dh_cache_hits", &self.dh_cache_hits)
.field("dh_cache_lru", &self.dh_cache_lru)
// .field("crypto_vld0", &self.crypto_vld0)
// .field("crypto_none", &self.crypto_none)
.finish()
}
}
/// Crypto factory implementation
#[must_use]
pub struct Crypto {
registry: VeilidComponentRegistry,
inner: Mutex<CryptoInner>,
#[cfg(feature = "enable-crypto-vld0")]
crypto_vld0: Arc<dyn CryptoSystem + Send + Sync>,
#[cfg(feature = "enable-crypto-none")]
crypto_none: Arc<dyn CryptoSystem + Send + Sync>,
}
impl_veilid_component!(Crypto);
impl fmt::Debug for Crypto {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Crypto")
//.field("registry", &self.registry)
.field("inner", &self.inner)
// .field("crypto_vld0", &self.crypto_vld0)
// .field("crypto_none", &self.crypto_none)
.finish()
}
}
impl Crypto {
fn new_inner() -> CryptoInner {
CryptoInner {
dh_cache: DHCache::new(DH_CACHE_SIZE),
dh_cache_misses: 0,
dh_cache_hits: 0,
dh_cache_lru: 0,
}
}
pub(crate) fn new(registry: VeilidComponentRegistry) -> Self {
Self {
registry: registry.clone(),
inner: Mutex::new(Self::new_inner()),
#[cfg(feature = "enable-crypto-vld0")]
crypto_vld0: Arc::new(vld0::CryptoSystemVLD0::new(registry.clone())),
#[cfg(feature = "enable-crypto-none")]
crypto_none: Arc::new(none::CryptoSystemNONE::new(registry.clone())),
}
}
fn log_facilities_impl(&self) -> VeilidComponentLogFacilities {
VeilidComponentLogFacilities::new().with_facility(
VeilidComponentLogFacility::try_new_with_tags("crypto", ["#common"]).unwrap(),
)
}
#[cfg_attr(
feature = "instrument",
instrument(level = "trace", target = "crypto", skip_all, err, fields(__VEILID_LOG_KEY = self.log_key()))
)]
#[allow(clippy::unused_async)]
async fn init_async(&self) -> EyreResult<()> {
// Nothing to initialize at this time
Ok(())
}
#[cfg_attr(
feature = "instrument",
instrument(level = "trace", target = "crypto", skip_all, err, fields(__VEILID_LOG_KEY = self.log_key()))
)]
#[allow(clippy::unused_async)]
async fn post_init_async(&self) -> EyreResult<()> {
Ok(())
}
#[allow(clippy::unused_async)]
async fn pre_terminate_async(&self) {}
#[expect(clippy::unused_async)]
async fn terminate_async(&self) {
// Nothing to terminate at this time
}
/// Factory method to get a specific crypto version
///
/// Returns a guard borrowing this `Crypto`; the cryptosystem stays reachable for the guard's
/// lifetime. Non-blocking (clones an `Arc`).
pub fn get(&self, kind: CryptoKind) -> Option<CryptoSystemGuard<'_>> {
match kind {
#[cfg(feature = "enable-crypto-vld0")]
CRYPTO_KIND_VLD0 => Some(CryptoSystemGuard::new(self.crypto_vld0.clone())),
#[cfg(feature = "enable-crypto-none")]
CRYPTO_KIND_NONE => Some(CryptoSystemGuard::new(self.crypto_none.clone())),
_ => None,
}
}
/// Factory method to get a specific crypto version for async use
///
/// Returns a guard borrowing this `Crypto`; the cryptosystem stays reachable for the guard's
/// lifetime. Non-blocking (clones an `Arc`).
pub fn get_async(&self, kind: CryptoKind) -> Option<AsyncCryptoSystemGuard<'_>> {
self.get(kind).map(|x| x.as_async())
}
// Factory method to get the best crypto version
pub(crate) fn best(&self) -> CryptoSystemGuard<'_> {
self.get(best_crypto_kind()).unwrap_or_log()
}
// Factory method to get the best crypto version for async use
pub(crate) fn best_async(&self) -> AsyncCryptoSystemGuard<'_> {
self.get_async(best_crypto_kind()).unwrap_or_log()
}
// Convenience validators
/// Validate a shared secret against the cryptosystem named by its kind. Fails if the kind is unsupported.
///
/// Errors `VeilidAPIError::Generic` if `secret`'s kind is unsupported or its length is wrong.
pub fn check_shared_secret(&self, secret: &SharedSecret) -> VeilidAPIResult<()> {
let Some(vcrypto) = self.get(secret.kind()) else {
apibail_generic!("unsupported crypto kind");
};
vcrypto.check_shared_secret(secret)
}
/// Validate a hash digest against the cryptosystem named by its kind. Fails if the kind is unsupported.
///
/// Errors `VeilidAPIError::Generic` if `hash`'s kind is unsupported or its length is wrong.
pub fn check_hash_digest(&self, hash: &HashDigest) -> VeilidAPIResult<()> {
let Some(vcrypto) = self.get(hash.kind()) else {
apibail_generic!("unsupported crypto kind");
};
vcrypto.check_hash_digest(hash)
}
/// Validate a public key against the cryptosystem named by its kind. Fails if the kind is unsupported.
///
/// Errors `VeilidAPIError::Generic` if `key`'s kind is unsupported or its length is wrong.
pub fn check_public_key(&self, key: &PublicKey) -> VeilidAPIResult<()> {
let Some(vcrypto) = self.get(key.kind()) else {
apibail_generic!("unsupported crypto kind");
};
vcrypto.check_public_key(key)
}
/// Validate a secret key against the cryptosystem named by its kind. Fails if the kind is unsupported.
///
/// Errors `VeilidAPIError::Generic` if `key`'s kind is unsupported or its length is wrong.
pub fn check_secret_key(&self, key: &SecretKey) -> VeilidAPIResult<()> {
let Some(vcrypto) = self.get(key.kind()) else {
apibail_generic!("unsupported crypto kind");
};
vcrypto.check_secret_key(key)
}
/// Validate a signature against the cryptosystem named by its kind. Fails if the kind is unsupported.
///
/// Errors `VeilidAPIError::Generic` if `signature`'s kind is unsupported or its length is wrong.
pub fn check_signature(&self, signature: &Signature) -> VeilidAPIResult<()> {
let Some(vcrypto) = self.get(signature.kind()) else {
apibail_generic!("unsupported crypto kind");
};
vcrypto.check_signature(signature)
}
/// Validate a keypair against the cryptosystem named by its kind. Fails if the kind is unsupported.
///
/// Errors `VeilidAPIError::Generic` if `key_pair`'s kind is unsupported, or if the pair or either
/// key has the wrong length.
pub fn check_keypair(&self, key_pair: &KeyPair) -> VeilidAPIResult<()> {
let Some(vcrypto) = self.get(key_pair.kind()) else {
apibail_generic!("unsupported crypto kind");
};
vcrypto.check_keypair(key_pair)
}
/// BareSignature set verification
/// Returns Some() the set of signature cryptokinds that validate and are supported
/// Returns None if any cryptokinds are supported and do not validate
///
/// Local CPU only; verifies each signature inline (no offload).
///
/// A supported signature that does not match returns `Ok(None)`, not an error. Errors
/// `VeilidAPIError::Generic` or `VeilidAPIError::ParseError` if a matching public key or
/// signature is malformed (propagated from the underlying verify).
pub fn verify_signatures(
&self,
public_keys: &[PublicKey],
data: &[u8],
signatures: &[Signature],
) -> VeilidAPIResult<Option<PublicKeyGroup>> {
let mut out = PublicKeyGroup::with_capacity(public_keys.len());
for signature in signatures {
for public_key in public_keys {
if public_key.kind() == signature.kind() {
if let Some(vcrypto) = self.get(signature.kind()) {
if !vcrypto.verify(public_key, data, signature)? {
return Ok(None);
}
out.add(public_key.clone());
}
}
}
}
Ok(Some(out))
}
/// BareSignature set generation
/// Generates the set of signatures that are supported
/// Any cryptokinds that are not supported are silently dropped
///
/// Local CPU only; signs inline for each keypair (no offload).
///
/// Errors `VeilidAPIError::Generic`, `VeilidAPIError::ParseError`, or `VeilidAPIError::Internal`
/// if a supported keypair is malformed (propagated from the underlying sign).
pub fn generate_signatures<F, R>(
&self,
data: &[u8],
key_pairs: &[KeyPair],
transform: F,
) -> VeilidAPIResult<Vec<R>>
where
F: Fn(&KeyPair, Signature) -> R,
{
let mut out = Vec::<R>::with_capacity(key_pairs.len());
for kp in key_pairs {
if let Some(vcrypto) = self.get(kp.kind()) {
let sig = vcrypto.sign(&kp.key(), &kp.secret(), data)?;
out.push(transform(kp, sig))
}
}
Ok(out)
}
/// Generate keypair
/// Does not require startup/init
///
/// Errors `VeilidAPIError::Generic` if `crypto_kind` is not a supported cryptosystem.
pub fn generate_keypair(crypto_kind: CryptoKind) -> VeilidAPIResult<KeyPair> {
#[cfg(feature = "enable-crypto-vld0")]
if crypto_kind == CRYPTO_KIND_VLD0 {
let kp = vld0_generate_keypair();
return Ok(kp);
}
#[cfg(feature = "enable-crypto-none")]
if crypto_kind == CRYPTO_KIND_NONE {
let kp = none_generate_keypair();
return Ok(kp);
}
Err(VeilidAPIError::generic("invalid crypto kind"))
}
// Internal utilities
fn cached_dh_internal<T: CryptoSystem>(
&self,
vcrypto: &T,
key: &PublicKey,
secret: &SecretKey,
) -> VeilidAPIResult<SharedSecret> {
vcrypto.check_public_key(key)?;
vcrypto.check_secret_key(secret)?;
let dh_cache_key = DHCacheKey {
key: key.clone(),
secret: secret.clone(),
};
{
let inner = &mut *self.inner.lock();
if let Some(value) = inner.dh_cache.get(&dh_cache_key) {
inner.dh_cache_hits += 1;
return Ok(value.shared_secret.clone());
}
}
let shared_secret = vcrypto.compute_dh(key, secret)?;
{
let inner = &mut *self.inner.lock();
let res = inner.dh_cache.entry_with_callback(dh_cache_key, |_, _| {
inner.dh_cache_lru += 1;
});
match res {
Entry::Occupied(_) => {
inner.dh_cache_hits += 1;
}
Entry::Vacant(e) => {
inner.dh_cache_misses += 1;
e.insert(DHCacheValue {
shared_secret: shared_secret.clone(),
});
}
}
}
Ok(shared_secret)
}
pub(crate) fn validate_crypto_kind(kind: CryptoKind) -> VeilidAPIResult<()> {
if !VALID_CRYPTO_KINDS.contains(&kind) {
apibail_generic!("invalid crypto kind");
}
Ok(())
}
pub(crate) fn debug_info_nodeinfo(&self) -> String {
let inner = self.inner.lock();
format!(
"Crypto Stats:\n DH Cache Hits/Misses/LRU: {} / {} / {}",
inner.dh_cache_hits, inner.dh_cache_misses, inner.dh_cache_lru
)
}
}