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
use crate::{crypto::OneRttKey, path::MaxMtu};
pub struct Key<K> {
key: K,
encrypted_packets: u64,
decrypted_packets: u64,
confidentiality_limit: u64,
}
#[derive(Copy, Clone, Debug)]
#[non_exhaustive]
pub struct Limits {
pub key_update_window: u64,
pub sealer_optimization_threshold: u64,
pub opener_optimization_threshold: u64,
pub max_mtu: MaxMtu,
}
impl Default for Limits {
fn default() -> Self {
Self {
key_update_window: KEY_UPDATE_WINDOW,
sealer_optimization_threshold: 100,
opener_optimization_threshold: 100,
max_mtu: MaxMtu::default(),
}
}
}
const KEY_UPDATE_WINDOW: u64 = 10_000;
impl<K: OneRttKey> Key<K> {
pub fn new(key: K) -> Self {
Key {
confidentiality_limit: key.aead_confidentiality_limit(),
key,
encrypted_packets: 0,
decrypted_packets: 0,
}
}
#[inline]
pub fn expired(&self) -> bool {
self.encrypted_packets >= self.confidentiality_limit
}
#[inline]
pub fn needs_update(&self, limits: &Limits) -> bool {
self.encrypted_packets
> (self
.confidentiality_limit
.saturating_sub(limits.key_update_window))
}
pub fn derive_next_key(&self) -> K {
self.key.derive_next_key()
}
#[inline]
pub fn encrypted_packets(&self) -> u64 {
self.encrypted_packets
}
#[inline]
pub fn on_packet_encryption(&mut self, limits: &Limits) {
self.encrypted_packets += 1;
if self.encrypted_packets == limits.sealer_optimization_threshold {
self.key.update_sealer_pmtu(limits.max_mtu.into());
}
}
#[inline]
pub fn on_packet_decryption(&mut self, limits: &Limits) {
self.decrypted_packets += 1;
if self.decrypted_packets == limits.opener_optimization_threshold {
self.key.update_opener_pmtu(limits.max_mtu.into());
}
}
#[inline]
pub fn key(&self) -> &K {
&self.key
}
}