Skip to content

Commit

Permalink
Merge b9dd459 into 44858ce
Browse files Browse the repository at this point in the history
  • Loading branch information
kurnevsky committed Aug 15, 2022
2 parents 44858ce + b9dd459 commit c153b7f
Show file tree
Hide file tree
Showing 55 changed files with 204 additions and 210 deletions.
4 changes: 2 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ rand = "0.8"
anyhow = "1.0"

[dev-dependencies.tokio]
version = "1.12"
version = "1.20"
default-features = false
features = ["macros", "test-util", "net", "rt", "rt-multi-thread", "sync", "time"]

[dev-dependencies.tokio-util]
version = "0.6"
version = "0.7"
features = ["codec", "net"]

[[example]]
Expand Down
2 changes: 0 additions & 2 deletions examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ async fn main() -> Result<(), Error> {
net_crypto.set_tcp_sink(net_crypto_tcp_tx).await;

let friend_connections = FriendConnections::new(
real_sk,
real_pk,
dht_server.clone(),
tcp_connections.clone(),
onion_client.clone(),
Expand Down
4 changes: 2 additions & 2 deletions tox_binary_io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ license = "GPL-3.0+"
edition = "2021"

[dependencies]
nom = "7.0"
nom = "7.1"
cookie-factory = "0.3"
crypto_box = { version = "0.7", optional = true }
crypto_box = { version = "0.8", optional = true }

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion tox_binary_io/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ mod tests {
let bytes = [42; KEY_SIZE];
let (_rest, sk) = SecretKey::from_bytes(&bytes).unwrap();

assert_eq!(&sk.to_bytes()[..], &bytes as &[u8]);
assert_eq!(sk.as_bytes(), &bytes as &[u8]);
}
}
16 changes: 8 additions & 8 deletions tox_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ tox_binary_io = { version = "0.1.1", path = "../tox_binary_io" }
tox_crypto = { version = "0.1.1", path = "../tox_crypto" }
tox_packet = { version = "0.1.1", path = "../tox_packet" }

bytes = "1.0"
bytes = "1.2"
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
log = "0.4"
nom = "7.0"
nom = "7.1"
cookie-factory = "0.3"
get_if_addrs = "0.5"
thiserror = "1.0"
lru = "0.6"
lru = "0.7"
bitflags = "1.3"
itertools = "0.10"
rand = "0.8"
sha2 = "0.9"
xsalsa20poly1305 = "0.8"
crypto_box = "0.7"
xsalsa20poly1305 = "0.9"
crypto_box = "0.8"

[dependencies.tokio]
version = "1.12"
version = "1.20"
default-features = false
features = ["net", "sync", "time"]

[dependencies.tokio-util]
version = "0.6"
version = "0.7"
features = ["codec", "net"]

[dev-dependencies.tokio]
version = "1.0"
version = "1.20"
default-features = false
features = ["macros", "test-util", "net", "rt", "rt-multi-thread", "sync", "time"]
2 changes: 1 addition & 1 deletion tox_core/src/dht/kbucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ where
if self.is_full() {
debug!(target: "Kbucket",
"No free space left in the kbucket, the last (bad) node removed.");
let eviction_index = Node::eviction_index(&self.nodes).unwrap_or_else(|| self.nodes.len() - 1);
let eviction_index = Node::eviction_index(&self.nodes).unwrap_or(self.nodes.len() - 1);
self.nodes.remove(eviction_index);
let index = index - if eviction_index < index { 1 } else { 0 };
self.nodes.insert(index, new_node.into());
Expand Down
10 changes: 6 additions & 4 deletions tox_core/src/dht/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::{iter, mem};
use xsalsa20poly1305::{XSalsa20Poly1305, aead::NewAead};
use xsalsa20poly1305::{XSalsa20Poly1305, KeyInit};

use crate::time::*;
use tox_crypto::*;
Expand Down Expand Up @@ -71,14 +71,16 @@ pub const PRECOMPUTED_LRU_CACHE_SIZE: usize = KBUCKET_DEFAULT_SIZE as usize * KB
/// How often DHT main loop should be called.
const MAIN_LOOP_INTERVAL: u64 = 1;

type MotdCallback = dyn Fn(&Server) -> Vec<u8> + Send + Sync;

/// Struct that contains necessary data for `BootstrapInfo` packet.
#[derive(Clone)]
struct ServerBootstrapInfo {
/// Version of tox core which will be sent with `BootstrapInfo` packet.
version: u32,
/// Callback to get the message of the day which will be sent with
/// `BootstrapInfo` packet.
motd_cb: Arc<dyn Fn(&Server) -> Vec<u8> + Send + Sync>,
motd_cb: Arc<MotdCallback>,
}

/// DHT server state.
Expand Down Expand Up @@ -1358,7 +1360,7 @@ impl Server {
}

/// Set toxcore version and message of the day callback.
pub fn set_bootstrap_info(&mut self, version: u32, motd_cb: Box<dyn Fn(&Server) -> Vec<u8> + Send + Sync>) {
pub fn set_bootstrap_info(&mut self, version: u32, motd_cb: Box<MotdCallback>) {
self.bootstrap_info = Some(ServerBootstrapInfo {
version,
motd_cb: motd_cb.into(),
Expand Down Expand Up @@ -2456,7 +2458,7 @@ mod tests {

// send onion data request

let nonce = crypto_box::generate_nonce(&mut rng).into();
let nonce = SalsaBox::generate_nonce(&mut rng).into();
let temporary_pk = SecretKey::generate(&mut rng).public_key();
let payload = vec![42; 123];
let inner = InnerOnionDataRequest {
Expand Down
28 changes: 9 additions & 19 deletions tox_core/src/friend_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ impl Friend {
/// Friend connections module that handles friends and their connections.
#[derive(Clone)]
pub struct FriendConnections {
/// Our long term `SecretKey`.
real_sk: SecretKey,
/// Our long term `PublicKey`.
real_pk: PublicKey,
/// List of friends we want to be connected to.
friends: Arc<RwLock<HashMap<PublicKey, Friend>>>,
/// Sink to send a connection status when it becomes connected or
Expand All @@ -112,16 +108,12 @@ pub struct FriendConnections {
impl FriendConnections {
/// Create new `FriendConnections`.
pub fn new(
real_sk: SecretKey,
real_pk: PublicKey,
dht: DhtServer,
tcp_connections: TcpConnections,
onion_client: OnionClient,
net_crypto: NetCrypto,
) -> Self {
FriendConnections {
real_sk,
real_pk,
friends: Arc::new(RwLock::new(HashMap::new())),
connection_status_tx: Arc::new(RwLock::new(None)),
dht,
Expand Down Expand Up @@ -450,13 +442,11 @@ mod tests {
lossy_tx,
dht_pk,
dht_sk,
real_pk: real_pk.clone(),
real_sk: real_sk.clone(),
real_pk,
real_sk,
precomputed_keys,
});
let friend_connections = FriendConnections::new(
real_sk,
real_pk,
dht,
tcp_connections,
onion_client,
Expand Down Expand Up @@ -494,7 +484,7 @@ mod tests {
let _ = friend_connections.tcp_connections.add_connection(relay_pk, friend_dht_pk.clone());

let session_precomputed_key = SalsaBox::new(&SecretKey::generate(&mut rng).public_key(), &SecretKey::generate(&mut rng));
let sent_nonce = crypto_box::generate_nonce(&mut rng).into();
let sent_nonce = SalsaBox::generate_nonce(&mut rng).into();
friend_connections.net_crypto.add_established_connection(
SecretKey::generate(&mut rng).public_key(),
friend_pk.clone(),
Expand Down Expand Up @@ -547,7 +537,7 @@ mod tests {
let _ = friend_connections.tcp_connections.add_connection(relay_pk, friend_dht_pk.clone());

let session_precomputed_key = SalsaBox::new(&SecretKey::generate(&mut rng).public_key(), &SecretKey::generate(&mut rng));
let sent_nonce = crypto_box::generate_nonce(&mut rng).into();
let sent_nonce = SalsaBox::generate_nonce(&mut rng).into();
friend_connections.net_crypto.add_established_connection(
SecretKey::generate(&mut rng).public_key(),
friend_pk.clone(),
Expand Down Expand Up @@ -703,7 +693,7 @@ mod tests {
friend_connections.friends.write().await.insert(friend_pk.clone(), friend);

let session_precomputed_key = SalsaBox::new(&SecretKey::generate(&mut rng).public_key(), &SecretKey::generate(&mut rng));
let sent_nonce = crypto_box::generate_nonce(&mut rng).into();
let sent_nonce = SalsaBox::generate_nonce(&mut rng).into();
friend_connections.net_crypto.add_established_connection(
SecretKey::generate(&mut rng).public_key(),
friend_pk.clone(),
Expand Down Expand Up @@ -750,7 +740,7 @@ mod tests {
friend_connections.friends.write().await.insert(friend_pk.clone(), friend);

let session_precomputed_key = SalsaBox::new(&SecretKey::generate(&mut rng).public_key(), &SecretKey::generate(&mut rng));
let sent_nonce = crypto_box::generate_nonce(&mut rng).into();
let sent_nonce = SalsaBox::generate_nonce(&mut rng).into();
friend_connections.net_crypto.add_established_connection(
SecretKey::generate(&mut rng).public_key(),
friend_pk.clone(),
Expand Down Expand Up @@ -800,7 +790,7 @@ mod tests {
friend_connections.friends.write().await.insert(friend_pk.clone(), friend);

let session_precomputed_key = SalsaBox::new(&SecretKey::generate(&mut rng).public_key(), &SecretKey::generate(&mut rng));
let sent_nonce = crypto_box::generate_nonce(&mut rng).into();
let sent_nonce = SalsaBox::generate_nonce(&mut rng).into();
friend_connections.net_crypto.add_established_connection(
SecretKey::generate(&mut rng).public_key(),
friend_pk.clone(),
Expand Down Expand Up @@ -961,9 +951,9 @@ mod tests {
let run_future = friend_connections.run()
.map(Result::unwrap);

let precomputed_key = SalsaBox::new(&friend_connections.real_pk, &friend_sk);
let precomputed_key = SalsaBox::new(friend_connections.net_crypto.real_pk(), &friend_sk);
let cookie = friend_connections.net_crypto.get_cookie(&mut rng, friend_pk.clone(), friend_dht_pk);
let sent_nonce = crypto_box::generate_nonce(&mut rng).into();
let sent_nonce = SalsaBox::generate_nonce(&mut rng).into();
let friend_session_sk = SecretKey::generate(&mut rng);
let friend_session_pk = friend_session_sk.public_key();
let our_cookie = EncryptedCookie {
Expand Down
6 changes: 3 additions & 3 deletions tox_core/src/net_crypto/crypto_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::convert::Into;
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use std::time::{Duration, Instant};
use crypto_box::SalsaBox;
use crypto_box::{SalsaBox, aead::AeadCore};
use rand::{thread_rng, Rng};
use xsalsa20poly1305::XSalsa20Poly1305;

Expand Down Expand Up @@ -228,7 +228,7 @@ impl RecvPacket {
}

/// UDP address of a connection with the time when last UDP packet was received
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConnectionAddr<T: Into<SocketAddr> + Copy> {
/// Address to send UDP packets directly to the peer
pub addr: T,
Expand Down Expand Up @@ -382,7 +382,7 @@ impl CryptoConnection {
let mut rng = thread_rng();
let session_sk = SecretKey::generate(&mut rng);
let session_pk = session_sk.public_key();
let sent_nonce = crypto_box::generate_nonce(&mut rng);
let sent_nonce = SalsaBox::generate_nonce(&mut rng);

let our_cookie = Cookie::new(peer_real_pk.clone(), peer_dht_pk.clone());
let our_encrypted_cookie = EncryptedCookie::new(&mut rng, symmetric_key, &our_cookie);
Expand Down

0 comments on commit c153b7f

Please sign in to comment.