Skip to main content

rtc_shared/replay_detector/
mod.rs

1mod fixed_big_int;
2#[cfg(test)]
3mod replay_detector_test;
4
5use fixed_big_int::*;
6
7// ReplayDetector is the interface of sequence replay detector.
8/// Tracks which sequence numbers have already been seen, so replayed packets can be dropped.
9///
10/// Both DTLS and SRTP require this: an attacker who captures a packet must not be able to
11/// have it accepted a second time.
12pub trait ReplayDetector: Send + Sync {
13    /// Returns `true` if `seq` has not been seen before and is inside the window.
14    ///
15    /// This only tests; call [`Self::accept`] afterwards to record the packet as received.
16    fn check(&mut self, seq: u64) -> bool;
17    /// Commits the sequence number from the preceding [`Self::check`] call as received.
18    ///
19    /// Split from `check` so a caller can validate a packet's authenticity first and only then
20    /// record it — a forged packet must not advance the window.
21    fn accept(&mut self);
22}
23
24/// A replay detector over a monotonically increasing sequence number that never wraps.
25///
26/// Handles the full 64-bit range, which is what DTLS needs. See
27/// [`WrappedSlidingWindowDetector`] for sequence numbers that do wrap.
28pub struct SlidingWindowDetector {
29    accepted: bool,
30    seq: u64,
31    latest_seq: u64,
32    max_seq: u64,
33    window_size: usize,
34    mask: FixedBigInt,
35}
36
37impl SlidingWindowDetector {
38    /// Creates a detector with a `window_size`-wide window over sequence numbers up to
39    /// `max_seq`.
40    ///
41    /// Does not allow wrapping: it handles monotonically increasing sequence numbers across
42    /// the full 64-bit range, which is what DTLS replay protection needs.
43    pub fn new(window_size: usize, max_seq: u64) -> Self {
44        SlidingWindowDetector {
45            accepted: false,
46            seq: 0,
47            latest_seq: 0,
48            max_seq,
49            window_size,
50            mask: FixedBigInt::new(window_size),
51        }
52    }
53}
54
55impl ReplayDetector for SlidingWindowDetector {
56    fn check(&mut self, seq: u64) -> bool {
57        self.accepted = false;
58
59        if seq > self.max_seq {
60            // Exceeded upper limit.
61            return false;
62        }
63
64        if seq <= self.latest_seq {
65            if self.latest_seq >= self.window_size as u64 + seq {
66                return false;
67            }
68            if self.mask.bit((self.latest_seq - seq) as usize) != 0 {
69                // The sequence number is duplicated.
70                return false;
71            }
72        }
73
74        self.accepted = true;
75        self.seq = seq;
76        true
77    }
78
79    fn accept(&mut self) {
80        if !self.accepted {
81            return;
82        }
83
84        if self.seq > self.latest_seq {
85            // Update the head of the window.
86            self.mask.lsh((self.seq - self.latest_seq) as usize);
87            self.latest_seq = self.seq;
88        }
89        let diff = (self.latest_seq - self.seq) % self.max_seq;
90        self.mask.set_bit(diff as usize);
91    }
92}
93
94/// A replay detector for a sequence number that wraps at a known maximum.
95///
96/// SRTP's 16-bit sequence numbers wrap, so the window has to interpret a large backwards
97/// jump as a rollover rather than a replay.
98pub struct WrappedSlidingWindowDetector {
99    accepted: bool,
100    seq: u64,
101    latest_seq: u64,
102    max_seq: u64,
103    window_size: usize,
104    mask: FixedBigInt,
105    init: bool,
106}
107
108impl WrappedSlidingWindowDetector {
109    /// Creates a detector with a `window_size`-wide window that allows the sequence number to
110    /// wrap at `max_seq`.
111    ///
112    /// Suitable for the short counters used by SRTP and SRTCP.
113    pub fn new(window_size: usize, max_seq: u64) -> Self {
114        WrappedSlidingWindowDetector {
115            accepted: false,
116            seq: 0,
117            latest_seq: 0,
118            max_seq,
119            window_size,
120            mask: FixedBigInt::new(window_size),
121            init: false,
122        }
123    }
124
125    /// Where the head of the window is, or will be if `seq` is the first to be accepted: one
126    /// before it.
127    fn head_before(&self, seq: u64) -> u64 {
128        if self.init {
129            self.latest_seq
130        } else if seq != 0 {
131            seq - 1
132        } else {
133            self.max_seq
134        }
135    }
136}
137
138impl ReplayDetector for WrappedSlidingWindowDetector {
139    fn check(&mut self, seq: u64) -> bool {
140        self.accepted = false;
141
142        if seq > self.max_seq {
143            // Exceeded upper limit.
144            return false;
145        }
146
147        // The window is only positioned by accept: seq is not authenticated yet.
148        let mut diff = self.head_before(seq) as i64 - seq as i64;
149        // Wrap the number.
150        if diff > self.max_seq as i64 / 2 {
151            diff -= (self.max_seq + 1) as i64;
152        } else if diff <= -(self.max_seq as i64 / 2) {
153            diff += (self.max_seq + 1) as i64;
154        }
155
156        if diff >= self.window_size as i64 {
157            // Too old.
158            return false;
159        }
160        if diff >= 0 && self.mask.bit(diff as usize) != 0 {
161            // The sequence number is duplicated.
162            return false;
163        }
164
165        self.accepted = true;
166        self.seq = seq;
167        true
168    }
169
170    fn accept(&mut self) {
171        if !self.accepted {
172            return;
173        }
174        self.latest_seq = self.head_before(self.seq);
175        self.init = true;
176
177        let mut diff = self.latest_seq as i64 - self.seq as i64;
178        // Wrap the number.
179        if diff > self.max_seq as i64 / 2 {
180            diff -= (self.max_seq + 1) as i64;
181        } else if diff <= -(self.max_seq as i64 / 2) {
182            diff += (self.max_seq + 1) as i64;
183        }
184
185        assert!(diff < self.window_size as i64);
186
187        if diff < 0 {
188            // Update the head of the window.
189            self.mask.lsh((-diff) as usize);
190            self.latest_seq = self.seq;
191            self.mask.set_bit(0);
192        } else {
193            self.mask.set_bit(diff as usize);
194        }
195    }
196}
197
198#[derive(Default)]
199/// A detector that accepts everything.
200///
201/// For contexts where replay protection is disabled or handled elsewhere.
202pub struct NoOpReplayDetector;
203
204impl ReplayDetector for NoOpReplayDetector {
205    fn check(&mut self, _: u64) -> bool {
206        true
207    }
208    fn accept(&mut self) {}
209}