@@ -17,6 +17,7 @@ use std::net::SocketAddr;
1717use std:: sync:: Arc ;
1818use std:: time:: { Duration , Instant } ;
1919use std:: { iter, mem} ;
20+ use xsalsa20poly1305:: { XSalsa20Poly1305 , aead:: NewAead } ;
2021
2122use crate :: time:: * ;
2223use tox_crypto:: * ;
@@ -121,7 +122,7 @@ pub struct Server {
121122 /// Close nodes list which contains nodes close to own DHT `PublicKey`.
122123 pub close_nodes : Arc < RwLock < ForcedKtree > > ,
123124 /// Symmetric key used for onion return encryption.
124- onion_symmetric_key : Arc < RwLock < secretbox :: Key > > ,
125+ onion_symmetric_key : Arc < RwLock < XSalsa20Poly1305 > > ,
125126 /// Onion announce struct to handle `OnionAnnounce` and `OnionData` packets.
126127 onion_announce : Arc < RwLock < OnionAnnounce > > ,
127128 /// `PublicKey`s of fake friends. They serve two purposes:
@@ -191,14 +192,15 @@ impl Server {
191192
192193 let precomputed_keys = PrecomputedCache :: new ( sk. clone ( ) , PRECOMPUTED_LRU_CACHE_SIZE ) ;
193194
195+ let onion_symmetric_key = XSalsa20Poly1305 :: new ( & rng. gen :: < [ u8 ; xsalsa20poly1305:: KEY_SIZE ] > ( ) . into ( ) ) ;
194196 Server {
195197 sk,
196198 pk,
197199 tx,
198200 friend_saddr_sink : Default :: default ( ) ,
199201 request_queue : Arc :: new ( RwLock :: new ( RequestQueue :: new ( PING_TIMEOUT ) ) ) ,
200202 close_nodes : Arc :: new ( RwLock :: new ( ForcedKtree :: new ( & pk) ) ) ,
201- onion_symmetric_key : Arc :: new ( RwLock :: new ( secretbox :: gen_key ( ) ) ) ,
203+ onion_symmetric_key : Arc :: new ( RwLock :: new ( onion_symmetric_key ) ) ,
202204 onion_announce : Arc :: new ( RwLock :: new ( OnionAnnounce :: new ( pk) ) ) ,
203205 fake_friends_keys,
204206 friends : Arc :: new ( RwLock :: new ( friends) ) ,
@@ -1270,7 +1272,8 @@ impl Server {
12701272
12711273 /// Refresh onion symmetric key to enforce onion paths expiration.
12721274 async fn refresh_onion_key ( & self ) {
1273- * self . onion_symmetric_key . write ( ) . await = secretbox:: gen_key ( ) ;
1275+ * self . onion_symmetric_key . write ( ) . await =
1276+ XSalsa20Poly1305 :: new ( & thread_rng ( ) . gen :: < [ u8 ; xsalsa20poly1305:: KEY_SIZE ] > ( ) . into ( ) ) ;
12741277 }
12751278
12761279 /// Handle `OnionRequest` from TCP relay and send `OnionRequest1` packet
@@ -1373,9 +1376,9 @@ mod tests {
13731376
13741377 use std:: net:: SocketAddr ;
13751378
1376- const ONION_RETURN_1_PAYLOAD_SIZE : usize = ONION_RETURN_1_SIZE - secretbox :: NONCEBYTES ;
1377- const ONION_RETURN_2_PAYLOAD_SIZE : usize = ONION_RETURN_2_SIZE - secretbox :: NONCEBYTES ;
1378- const ONION_RETURN_3_PAYLOAD_SIZE : usize = ONION_RETURN_3_SIZE - secretbox :: NONCEBYTES ;
1379+ const ONION_RETURN_1_PAYLOAD_SIZE : usize = ONION_RETURN_1_SIZE - xsalsa20poly1305 :: NONCE_SIZE ;
1380+ const ONION_RETURN_2_PAYLOAD_SIZE : usize = ONION_RETURN_2_SIZE - xsalsa20poly1305 :: NONCE_SIZE ;
1381+ const ONION_RETURN_3_PAYLOAD_SIZE : usize = ONION_RETURN_3_SIZE - xsalsa20poly1305 :: NONCE_SIZE ;
13791382
13801383 impl Server {
13811384 pub async fn has_friend ( & self , pk : & PublicKey ) -> bool {
@@ -2174,7 +2177,7 @@ mod tests {
21742177 inner : inner. clone ( )
21752178 } ;
21762179 let onion_return = OnionReturn {
2177- nonce : secretbox :: gen_nonce ( ) ,
2180+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
21782181 payload : vec ! [ 42 ; ONION_RETURN_1_PAYLOAD_SIZE ]
21792182 } ;
21802183 let packet = OnionRequest1 :: new ( & precomp, & bob_pk, & payload, onion_return) ;
@@ -2206,7 +2209,7 @@ mod tests {
22062209 temporary_pk : gen_keypair ( ) . 0 ,
22072210 payload : vec ! [ 42 ; 123 ] , // not encrypted with dht pk
22082211 onion_return : OnionReturn {
2209- nonce : secretbox :: gen_nonce ( ) ,
2212+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
22102213 payload : vec ! [ 42 ; ONION_RETURN_1_PAYLOAD_SIZE ]
22112214 }
22122215 } ;
@@ -2236,7 +2239,7 @@ mod tests {
22362239 inner : InnerOnionRequest :: InnerOnionAnnounceRequest ( inner. clone ( ) )
22372240 } ;
22382241 let onion_return = OnionReturn {
2239- nonce : secretbox :: gen_nonce ( ) ,
2242+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
22402243 payload : vec ! [ 42 ; ONION_RETURN_2_PAYLOAD_SIZE ]
22412244 } ;
22422245 let packet = OnionRequest2 :: new ( & precomp, & bob_pk, & payload, onion_return) ;
@@ -2278,7 +2281,7 @@ mod tests {
22782281 inner : InnerOnionRequest :: InnerOnionDataRequest ( inner. clone ( ) )
22792282 } ;
22802283 let onion_return = OnionReturn {
2281- nonce : secretbox :: gen_nonce ( ) ,
2284+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
22822285 payload : vec ! [ 42 ; ONION_RETURN_2_PAYLOAD_SIZE ]
22832286 } ;
22842287 let packet = OnionRequest2 :: new ( & precomp, & bob_pk, & payload, onion_return) ;
@@ -2309,7 +2312,7 @@ mod tests {
23092312 temporary_pk : gen_keypair ( ) . 0 ,
23102313 payload : vec ! [ 42 ; 123 ] , // not encrypted with dht pk
23112314 onion_return : OnionReturn {
2312- nonce : secretbox :: gen_nonce ( ) ,
2315+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
23132316 payload : vec ! [ 42 ; ONION_RETURN_2_PAYLOAD_SIZE ]
23142317 }
23152318 } ;
@@ -2333,7 +2336,7 @@ mod tests {
23332336 } ;
23342337 let inner = InnerOnionAnnounceRequest :: new ( & precomp, & bob_pk, & payload) ;
23352338 let onion_return = OnionReturn {
2336- nonce : secretbox :: gen_nonce ( ) ,
2339+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
23372340 payload : vec ! [ 42 ; ONION_RETURN_3_PAYLOAD_SIZE ]
23382341 } ;
23392342 let packet = OnionAnnounceRequest {
@@ -2371,7 +2374,7 @@ mod tests {
23712374 payload : vec ! [ 42 ; 123 ]
23722375 } ;
23732376 let onion_return = OnionReturn {
2374- nonce : secretbox :: gen_nonce ( ) ,
2377+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
23752378 payload : vec ! [ 42 ; ONION_RETURN_3_PAYLOAD_SIZE ]
23762379 } ;
23772380 let packet = OnionAnnounceRequest {
@@ -2399,7 +2402,7 @@ mod tests {
23992402 } ;
24002403 let inner = InnerOnionAnnounceRequest :: new ( & precomp, & bob_pk, & payload) ;
24012404 let onion_return = OnionReturn {
2402- nonce : secretbox :: gen_nonce ( ) ,
2405+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
24032406 payload : vec ! [ 42 ; ONION_RETURN_3_PAYLOAD_SIZE ]
24042407 } ;
24052408 let packet = OnionAnnounceRequest {
@@ -2478,7 +2481,7 @@ mod tests {
24782481 port : 12345
24792482 } ;
24802483 let next_onion_return = OnionReturn {
2481- nonce : secretbox :: gen_nonce ( ) ,
2484+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
24822485 payload : vec ! [ 42 ; ONION_RETURN_2_PAYLOAD_SIZE ]
24832486 } ;
24842487 let onion_return = OnionReturn :: new ( & onion_symmetric_key, & ip_port, Some ( & next_onion_return) ) ;
@@ -2510,7 +2513,7 @@ mod tests {
25102513 let ( alice, _precomp, _bob_pk, _bob_sk, rx, _addr) = create_node ( ) ;
25112514
25122515 let onion_return = OnionReturn {
2513- nonce : secretbox :: gen_nonce ( ) ,
2516+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
25142517 payload : vec ! [ 42 ; ONION_RETURN_3_PAYLOAD_SIZE ] // not encrypted with onion_symmetric_key
25152518 } ;
25162519 let payload = InnerOnionResponse :: OnionAnnounceResponse ( OnionAnnounceResponse {
@@ -2571,7 +2574,7 @@ mod tests {
25712574 port : 12345
25722575 } ;
25732576 let next_onion_return = OnionReturn {
2574- nonce : secretbox :: gen_nonce ( ) ,
2577+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
25752578 payload : vec ! [ 42 ; ONION_RETURN_1_PAYLOAD_SIZE ]
25762579 } ;
25772580 let onion_return = OnionReturn :: new ( & onion_symmetric_key, & ip_port, Some ( & next_onion_return) ) ;
@@ -2603,7 +2606,7 @@ mod tests {
26032606 let ( alice, _precomp, _bob_pk, _bob_sk, rx, _addr) = create_node ( ) ;
26042607
26052608 let onion_return = OnionReturn {
2606- nonce : secretbox :: gen_nonce ( ) ,
2609+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
26072610 payload : vec ! [ 42 ; ONION_RETURN_2_PAYLOAD_SIZE ] // not encrypted with onion_symmetric_key
26082611 } ;
26092612 let payload = InnerOnionResponse :: OnionAnnounceResponse ( OnionAnnounceResponse {
@@ -2785,7 +2788,7 @@ mod tests {
27852788 let ( alice, _precomp, _bob_pk, _bob_sk, rx, _addr) = create_node ( ) ;
27862789
27872790 let onion_return = OnionReturn {
2788- nonce : secretbox :: gen_nonce ( ) ,
2791+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
27892792 payload : vec ! [ 42 ; ONION_RETURN_1_PAYLOAD_SIZE ] // not encrypted with onion_symmetric_key
27902793 } ;
27912794 let payload = InnerOnionResponse :: OnionAnnounceResponse ( OnionAnnounceResponse {
@@ -2818,7 +2821,7 @@ mod tests {
28182821 port : 12345
28192822 } ;
28202823 let next_onion_return = OnionReturn {
2821- nonce : secretbox :: gen_nonce ( ) ,
2824+ nonce : [ 42 ; xsalsa20poly1305 :: NONCE_SIZE ] ,
28222825 payload : vec ! [ 42 ; ONION_RETURN_1_PAYLOAD_SIZE ]
28232826 } ;
28242827 let onion_return = OnionReturn :: new ( & onion_symmetric_key, & ip_port, Some ( & next_onion_return) ) ;
@@ -2931,19 +2934,6 @@ mod tests {
29312934 assert ! ( rx. collect:: <Vec <_>>( ) . await . is_empty( ) ) ;
29322935 }
29332936
2934- #[ tokio:: test]
2935- async fn refresh_onion_key ( ) {
2936- let ( alice, _precomp, _bob_pk, _bob_sk, _rx, _addr) = create_node ( ) ;
2937-
2938- let onion_symmetric_key_1 = alice. onion_symmetric_key . read ( ) . await . clone ( ) ;
2939-
2940- alice. refresh_onion_key ( ) . await ;
2941-
2942- let onion_symmetric_key_2 = alice. onion_symmetric_key . read ( ) . await . clone ( ) ;
2943-
2944- assert_ne ! ( onion_symmetric_key_1, onion_symmetric_key_2)
2945- }
2946-
29472937 #[ tokio:: test]
29482938 async fn handle_tcp_onion_request ( ) {
29492939 let ( alice, _precomp, _bob_pk, _bob_sk, rx, addr) = create_node ( ) ;
0 commit comments