aboutsummaryrefslogtreecommitdiffstats
path: root/src/anti_replay.rs
blob: e71285dc6dfd683f36b3da4a1a7c51eb839aaa64 (plain) (blame)
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
/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2017-2018 WireGuard LLC. All Rights Reserved.
 */

use failure::Error;

// This is RFC 6479.

// Power of 2.
const BITMAP_BITLEN: u64 = 2048;

const SIZE_OF_INTEGER: u64 = 32;
const BITMAP_LEN: usize = (BITMAP_BITLEN / SIZE_OF_INTEGER) as usize;
const BITMAP_INDEX_MASK: u64 = BITMAP_LEN as u64 - 1;
// REDUNDANT_BIT_SHIFTS = log2(SIZE_OF_INTEGER).
const REDUNDANT_BIT_SHIFTS: u64 = 5;
const BITMAP_LOC_MASK: u64 = SIZE_OF_INTEGER - 1;
/// Size of anti-replay window.
pub const WINDOW_SIZE: u64 = BITMAP_BITLEN - SIZE_OF_INTEGER;

pub struct AntiReplay {
    bitmap: [u32; BITMAP_LEN],
    last: u64,
}

impl Default for AntiReplay {
    fn default() -> Self {
        AntiReplay::new()
    }
}

impl AntiReplay {
    pub fn new() -> Self {
        AntiReplay {
            last: 0,
            bitmap: [0; BITMAP_LEN],
        }
    }

    /// Returns true if check is passed, i.e., not a replay or too old.
    ///
    /// Unlike RFC 6479, zero is allowed.
    fn check(&self, seq: u64) -> bool {
        // Larger is always good.
        if seq > self.last {
            return true;
        }

        if self.last - seq > WINDOW_SIZE {
            return false;
        }

        let bit_location = seq & BITMAP_LOC_MASK;
        let index = (seq >> REDUNDANT_BIT_SHIFTS) & BITMAP_INDEX_MASK;

        self.bitmap[index as usize] & (1 << bit_location) == 0
    }

    /// Should only be called if check returns true.
    fn update_store(&mut self, seq: u64) {
        debug_assert!(self.check(seq));

        let index = seq >> REDUNDANT_BIT_SHIFTS;

        if seq > self.last {
            let index_cur = self.last >> REDUNDANT_BIT_SHIFTS;
            let diff = index - index_cur;

            if diff >= BITMAP_LEN as u64 {
                self.bitmap = [0; BITMAP_LEN];
            } else {
                for i in 0..diff {
                    let real_index = (index_cur + i + 1) & BITMAP_INDEX_MASK;
                    self.bitmap[real_index as usize] = 0;
                }
            }

            self.last = seq;
        }

        let index = index & BITMAP_INDEX_MASK;
        let bit_location = seq & BITMAP_LOC_MASK;
        self.bitmap[index as usize] |= 1 << bit_location;
    }

    pub fn update(&mut self, seq: u64) -> Result<(), Error> {
        if self.check(seq) {
            self.update_store(seq);
            Ok(())
        } else {
            bail!("replayed nonce")
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn anti_replay() {
        let mut ar = AntiReplay::new();

        for i in 0..20000 {
            ar.update(i).unwrap();
        }

        for i in (0..20000).rev() {
            assert!(!ar.check(i));
        }

        ar.update(65536).unwrap();
        for i in (65536 - WINDOW_SIZE)..65535 {
            ar.update(i).unwrap();
        }
        for i in (65536 - 10 * WINDOW_SIZE)..65535 {
            assert!(!ar.check(i));
        }

        ar.update(66000).unwrap();
        for i in 65537..66000 {
            ar.update(i).unwrap();
        }
        for i in 65537..66000 {
            assert!(ar.update(i).is_err());
        }

        // Test max u64.
        let next = u64::max_value();
        ar.update(next).unwrap();
        assert!(!ar.check(next));
        for i in (next - WINDOW_SIZE)..next {
            ar.update(i).unwrap();
        }
        for i in (next - 20 * WINDOW_SIZE)..next {
            assert!(!ar.check(i));
        }
    }

    #[bench]
    fn bench_anti_replay_sequential(b: &mut ::test::Bencher) {
        let mut ar = AntiReplay::new();
        let mut seq = 0;

        b.iter(|| {
            ar.update(seq).unwrap();
            seq += 1;
        });
    }

    #[bench]
    fn bench_anti_replay_old(b: &mut ::test::Bencher) {
        let mut ar = AntiReplay::new();
        ar.update(12345).unwrap();
        ar.update(11234).unwrap();

        b.iter(|| {
            assert!(ar.update(11234).is_err());
        });
    }

    #[bench]
    fn bench_anti_replay_large_skip(b: &mut ::test::Bencher) {
        let mut ar = AntiReplay::new();
        let mut seq = 0;

        b.iter(|| {
            ar.update(seq).unwrap();
            seq += 30000;
        });
    }
}