@@ -63,13 +63,10 @@ use aes::{
63
63
} ;
64
64
use byte_string:: ByteStr ;
65
65
use bytes:: { Buf , BufMut , BytesMut } ;
66
- use log:: { error , trace, warn } ;
66
+ use log:: trace;
67
67
use lru_time_cache:: LruCache ;
68
- use once_cell:: sync:: Lazy ;
69
- use spin:: Mutex as SpinMutex ;
70
68
71
69
use crate :: {
72
- config:: ReplayAttackPolicy ,
73
70
context:: Context ,
74
71
crypto:: {
75
72
v2:: udp:: { ChaCha20Poly1305Cipher , UdpCipher } ,
@@ -159,57 +156,6 @@ fn get_cipher(method: CipherKind, key: &[u8], session_id: u64) -> Rc<UdpCipher>
159
156
} )
160
157
}
161
158
162
- fn check_and_record_nonce ( method : CipherKind , key : & [ u8 ] , session_id : u64 , nonce : & [ u8 ] ) -> bool {
163
- static REPLAY_FILTER_RECORDER : Lazy < SpinMutex < LruCache < CipherKey , LruCache < Vec < u8 > , ( ) > > > > = Lazy :: new ( || {
164
- SpinMutex :: new ( LruCache :: with_expiry_duration_and_capacity (
165
- CIPHER_CACHE_DURATION ,
166
- CIPHER_CACHE_LIMIT ,
167
- ) )
168
- } ) ;
169
-
170
- let cache_key = CipherKey {
171
- method,
172
- // The key is stored in ServerConfig structure, so the address of it won't change.
173
- key : key. as_ptr ( ) as usize ,
174
- session_id,
175
- } ;
176
-
177
- const REPLAY_DETECT_NONCE_EXPIRE_DURATION : Duration = Duration :: from_secs ( SERVER_PACKET_TIMESTAMP_MAX_DIFF ) ;
178
-
179
- let mut session_map = REPLAY_FILTER_RECORDER . lock ( ) ;
180
-
181
- let session_nonce_map = session_map
182
- . entry ( cache_key)
183
- . or_insert_with ( || LruCache :: with_expiry_duration ( REPLAY_DETECT_NONCE_EXPIRE_DURATION ) ) ;
184
-
185
- if session_nonce_map. get ( nonce) . is_some ( ) {
186
- return true ;
187
- }
188
-
189
- session_nonce_map. insert ( nonce. to_vec ( ) , ( ) ) ;
190
- false
191
- }
192
-
193
- #[ inline]
194
- fn check_nonce_replay ( context : & Context , method : CipherKind , key : & [ u8 ] , session_id : u64 , nonce : & [ u8 ] ) -> bool {
195
- match context. replay_attack_policy ( ) {
196
- ReplayAttackPolicy :: Ignore => false ,
197
- ReplayAttackPolicy :: Detect => {
198
- if check_and_record_nonce ( method, key, session_id, nonce) {
199
- warn ! ( "detected repeated nonce salt {:?}" , ByteStr :: new( nonce) ) ;
200
- }
201
- false
202
- }
203
- ReplayAttackPolicy :: Reject => {
204
- let replayed = check_and_record_nonce ( method, key, session_id, nonce) ;
205
- if replayed {
206
- error ! ( "detected repeated nonce salt {:?}" , ByteStr :: new( nonce) ) ;
207
- }
208
- replayed
209
- }
210
- }
211
- }
212
-
213
159
fn encrypt_message ( _context : & Context , method : CipherKind , key : & [ u8 ] , packet : & mut BytesMut , session_id : u64 ) {
214
160
unsafe {
215
161
packet. advance_mut ( method. tag_len ( ) ) ;
@@ -255,7 +201,7 @@ fn encrypt_message(_context: &Context, method: CipherKind, key: &[u8], packet: &
255
201
}
256
202
}
257
203
258
- fn decrypt_message ( context : & Context , method : CipherKind , key : & [ u8 ] , packet : & mut [ u8 ] ) -> bool {
204
+ fn decrypt_message ( _context : & Context , method : CipherKind , key : & [ u8 ] , packet : & mut [ u8 ] ) -> bool {
259
205
match method {
260
206
CipherKind :: AEAD2022_BLAKE3_CHACHA20_POLY1305 => {
261
207
// ChaCha20-Poly1305 uses PSK as key, prepended nonce in packet
@@ -272,11 +218,6 @@ fn decrypt_message(context: &Context, method: CipherKind, key: &[u8], packet: &m
272
218
u64:: from_be ( session_id_slice[ 0 ] )
273
219
} ;
274
220
275
- if check_nonce_replay ( context, method, key, session_id, nonce) {
276
- error ! ( "detected replayed nonce: {:?}" , ByteStr :: new( nonce) ) ;
277
- return false ;
278
- }
279
-
280
221
let cipher = get_cipher ( method, key, session_id) ;
281
222
282
223
if !cipher. decrypt_packet ( nonce, message) {
@@ -316,14 +257,7 @@ fn decrypt_message(context: &Context, method: CipherKind, key: &[u8], packet: &m
316
257
317
258
let nonce = & packet_header[ 4 ..16 ] ;
318
259
319
- let cipher = {
320
- if check_nonce_replay ( context, method, key, session_id, nonce) {
321
- error ! ( "detected replayed nonce: {:?}" , ByteStr :: new( nonce) ) ;
322
- return false ;
323
- }
324
-
325
- get_cipher ( method, key, session_id)
326
- } ;
260
+ let cipher = get_cipher ( method, key, session_id) ;
327
261
328
262
if !cipher. decrypt_packet ( nonce, message) {
329
263
return false ;
0 commit comments