@@ -63,13 +63,10 @@ use aes::{
6363} ;
6464use byte_string:: ByteStr ;
6565use bytes:: { Buf , BufMut , BytesMut } ;
66- use log:: { error , trace, warn } ;
66+ use log:: trace;
6767use lru_time_cache:: LruCache ;
68- use once_cell:: sync:: Lazy ;
69- use spin:: Mutex as SpinMutex ;
7068
7169use crate :: {
72- config:: ReplayAttackPolicy ,
7370 context:: Context ,
7471 crypto:: {
7572 v2:: udp:: { ChaCha20Poly1305Cipher , UdpCipher } ,
@@ -159,57 +156,6 @@ fn get_cipher(method: CipherKind, key: &[u8], session_id: u64) -> Rc<UdpCipher>
159156 } )
160157}
161158
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-
213159fn encrypt_message ( _context : & Context , method : CipherKind , key : & [ u8 ] , packet : & mut BytesMut , session_id : u64 ) {
214160 unsafe {
215161 packet. advance_mut ( method. tag_len ( ) ) ;
@@ -255,7 +201,7 @@ fn encrypt_message(_context: &Context, method: CipherKind, key: &[u8], packet: &
255201 }
256202}
257203
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 {
259205 match method {
260206 CipherKind :: AEAD2022_BLAKE3_CHACHA20_POLY1305 => {
261207 // 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
272218 u64:: from_be ( session_id_slice[ 0 ] )
273219 } ;
274220
275- if check_nonce_replay ( context, method, key, session_id, nonce) {
276- error ! ( "detected replayed nonce: {:?}" , ByteStr :: new( nonce) ) ;
277- return false ;
278- }
279-
280221 let cipher = get_cipher ( method, key, session_id) ;
281222
282223 if !cipher. decrypt_packet ( nonce, message) {
@@ -316,14 +257,7 @@ fn decrypt_message(context: &Context, method: CipherKind, key: &[u8], packet: &m
316257
317258 let nonce = & packet_header[ 4 ..16 ] ;
318259
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) ;
327261
328262 if !cipher. decrypt_packet ( nonce, message) {
329263 return false ;
0 commit comments