Skip to content

Commit 2429b63

Browse files
committed
refactor(crypto): use pure rust symmetric crypto
Later xsalsa20poly1305 can be replaced by chacha20poly1305 in most of the cases.
1 parent d6c79ca commit 2429b63

File tree

26 files changed

+260
-304
lines changed

26 files changed

+260
-304
lines changed

tox_binary_io/src/sodium.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use sodiumoxide::crypto::box_::{
99
NONCEBYTES
1010
};
1111

12-
use sodiumoxide::crypto::secretbox;
13-
1412
use super::FromBytes;
1513

1614

@@ -39,10 +37,6 @@ impl FromBytes for Nonce {
3937
named!(from_bytes<Nonce>, map_opt!(take!(NONCEBYTES), Nonce::from_slice));
4038
}
4139

42-
impl FromBytes for secretbox::Nonce {
43-
named!(from_bytes<secretbox::Nonce>, map_opt!(take!(secretbox::NONCEBYTES), secretbox::Nonce::from_slice));
44-
}
45-
4640
#[cfg(test)]
4741
mod tests {
4842
use super::*;

tox_core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ bitflags = "1.0"
3333
itertools = "0.10"
3434
rand = "0.8"
3535
sha2 = "0.9"
36+
xsalsa20poly1305 = "0.6"
3637

3738
[dependencies.tokio]
3839
version = "1.0"

tox_core/src/dht/codec.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ mod tests {
165165
use tox_packet::onion::*;
166166
use tox_crypto::*;
167167

168-
const ONION_RETURN_1_PAYLOAD_SIZE: usize = ONION_RETURN_1_SIZE - secretbox::NONCEBYTES;
169-
const ONION_RETURN_2_PAYLOAD_SIZE: usize = ONION_RETURN_2_SIZE - secretbox::NONCEBYTES;
170-
const ONION_RETURN_3_PAYLOAD_SIZE: usize = ONION_RETURN_3_SIZE - secretbox::NONCEBYTES;
168+
const ONION_RETURN_1_PAYLOAD_SIZE: usize = ONION_RETURN_1_SIZE - xsalsa20poly1305::NONCE_SIZE;
169+
const ONION_RETURN_2_PAYLOAD_SIZE: usize = ONION_RETURN_2_SIZE - xsalsa20poly1305::NONCE_SIZE;
170+
const ONION_RETURN_3_PAYLOAD_SIZE: usize = ONION_RETURN_3_SIZE - xsalsa20poly1305::NONCE_SIZE;
171171

172172
#[test]
173173
fn encode_decode() {
@@ -217,7 +217,7 @@ mod tests {
217217
temporary_pk: gen_keypair().0,
218218
payload: vec![42; 123],
219219
onion_return: OnionReturn {
220-
nonce: secretbox::gen_nonce(),
220+
nonce: [42; xsalsa20poly1305::NONCE_SIZE],
221221
payload: vec![42; ONION_RETURN_1_PAYLOAD_SIZE]
222222
}
223223
}),
@@ -226,7 +226,7 @@ mod tests {
226226
temporary_pk: gen_keypair().0,
227227
payload: vec![42; 123],
228228
onion_return: OnionReturn {
229-
nonce: secretbox::gen_nonce(),
229+
nonce: [42; xsalsa20poly1305::NONCE_SIZE],
230230
payload: vec![42; ONION_RETURN_2_PAYLOAD_SIZE]
231231
}
232232
}),
@@ -237,7 +237,7 @@ mod tests {
237237
payload: vec![42; 123]
238238
},
239239
onion_return: OnionReturn {
240-
nonce: secretbox::gen_nonce(),
240+
nonce: [42; xsalsa20poly1305::NONCE_SIZE],
241241
payload: vec![42; ONION_RETURN_3_PAYLOAD_SIZE]
242242
}
243243
}),
@@ -249,7 +249,7 @@ mod tests {
249249
payload: vec![42; 123]
250250
},
251251
onion_return: OnionReturn {
252-
nonce: secretbox::gen_nonce(),
252+
nonce: [42; xsalsa20poly1305::NONCE_SIZE],
253253
payload: vec![42; ONION_RETURN_3_PAYLOAD_SIZE]
254254
}
255255
}),
@@ -265,7 +265,7 @@ mod tests {
265265
}),
266266
Packet::OnionResponse3(OnionResponse3 {
267267
onion_return: OnionReturn {
268-
nonce: secretbox::gen_nonce(),
268+
nonce: [42; xsalsa20poly1305::NONCE_SIZE],
269269
payload: vec![42; ONION_RETURN_3_PAYLOAD_SIZE]
270270
},
271271
payload: InnerOnionResponse::OnionAnnounceResponse(OnionAnnounceResponse {
@@ -276,7 +276,7 @@ mod tests {
276276
}),
277277
Packet::OnionResponse2(OnionResponse2 {
278278
onion_return: OnionReturn {
279-
nonce: secretbox::gen_nonce(),
279+
nonce: [42; xsalsa20poly1305::NONCE_SIZE],
280280
payload: vec![42; ONION_RETURN_2_PAYLOAD_SIZE]
281281
},
282282
payload: InnerOnionResponse::OnionAnnounceResponse(OnionAnnounceResponse {
@@ -287,7 +287,7 @@ mod tests {
287287
}),
288288
Packet::OnionResponse1(OnionResponse1 {
289289
onion_return: OnionReturn {
290-
nonce: secretbox::gen_nonce(),
290+
nonce: [42; xsalsa20poly1305::NONCE_SIZE],
291291
payload: vec![42; ONION_RETURN_1_PAYLOAD_SIZE]
292292
},
293293
payload: InnerOnionResponse::OnionAnnounceResponse(OnionAnnounceResponse {

tox_core/src/dht/server/mod.rs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::net::SocketAddr;
1717
use std::sync::Arc;
1818
use std::time::{Duration, Instant};
1919
use std::{iter, mem};
20+
use xsalsa20poly1305::{XSalsa20Poly1305, aead::NewAead};
2021

2122
use crate::time::*;
2223
use 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();

tox_core/src/friend_connection/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ impl FriendConnections {
421421
#[cfg(test)]
422422
mod tests {
423423
use super::*;
424+
use rand::thread_rng;
424425

425426
use tox_packet::dht::{Packet as DhtPacket, *};
426427
use crate::dht::precomputed_cache::*;
@@ -944,11 +945,11 @@ mod tests {
944945
.map(Result::unwrap);
945946

946947
let precomputed_key = precompute(&friend_connections.real_pk, &friend_sk);
947-
let cookie = friend_connections.net_crypto.get_cookie(friend_pk, friend_dht_pk);
948+
let cookie = friend_connections.net_crypto.get_cookie(&mut thread_rng(), friend_pk, friend_dht_pk);
948949
let sent_nonce = gen_nonce();
949950
let (friend_session_pk, friend_session_sk) = gen_keypair();
950951
let our_cookie = EncryptedCookie {
951-
nonce: secretbox::gen_nonce(),
952+
nonce: [42; xsalsa20poly1305::NONCE_SIZE],
952953
payload: vec![42; 88]
953954
};
954955
let handshake_payload = CryptoHandshakePayload {

tox_core/src/net_crypto/crypto_connection.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::convert::Into;
44
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
55
use std::time::{Duration, Instant};
66
use rand::{thread_rng, Rng};
7+
use xsalsa20poly1305::XSalsa20Poly1305;
78

89
use super::packets_array::*;
910

@@ -373,13 +374,13 @@ impl CryptoConnection {
373374
received_nonce: Nonce,
374375
peer_session_pk: PublicKey,
375376
cookie: EncryptedCookie,
376-
symmetric_key: &secretbox::Key
377+
symmetric_key: &XSalsa20Poly1305,
377378
) -> CryptoConnection {
378379
let (session_pk, session_sk) = gen_keypair();
379380
let sent_nonce = gen_nonce();
380381

381382
let our_cookie = Cookie::new(peer_real_pk, peer_dht_pk);
382-
let our_encrypted_cookie = EncryptedCookie::new(symmetric_key, &our_cookie);
383+
let our_encrypted_cookie = EncryptedCookie::new(&mut thread_rng(), symmetric_key, &our_cookie);
383384
let handshake_payload = CryptoHandshakePayload {
384385
base_nonce: sent_nonce,
385386
session_pk,
@@ -736,7 +737,7 @@ mod tests {
736737

737738
let crypto_handshake = CryptoHandshake {
738739
cookie: EncryptedCookie {
739-
nonce: secretbox::gen_nonce(),
740+
nonce: [42; xsalsa20poly1305::NONCE_SIZE],
740741
payload: vec![42; 88]
741742
},
742743
nonce: gen_nonce(),
@@ -863,7 +864,7 @@ mod tests {
863864

864865
let crypto_handshake = CryptoHandshake {
865866
cookie: EncryptedCookie {
866-
nonce: secretbox::gen_nonce(),
867+
nonce: [42; xsalsa20poly1305::NONCE_SIZE],
867868
payload: vec![42; 88]
868869
},
869870
nonce: gen_nonce(),

0 commit comments

Comments
 (0)