diff --git a/README.md b/README.md index 65e774f..35d58c7 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,10 @@ Minimal ADNL implementation in Rust (client-server only, without p2p for now). S Run this example: `cargo run --example time` ```rust -use std::net::SocketAddrV4; use adnl::AdnlClient; use anyhow::{anyhow, Context, Result}; +use std::net::SocketAddrV4; + #[tokio::main] async fn main() -> Result<()> { @@ -28,10 +29,8 @@ async fn main() -> Result<()> { let ls_ip = "65.21.74.140"; let ls_port = 46427; // create AdnlClient - let mut client = AdnlClient::connect( - remote_public, - SocketAddrV4::new(ls_ip.parse()?, ls_port) - ).await?; + let mut client = + AdnlClient::connect(remote_public, SocketAddrV4::new(ls_ip.parse()?, ls_port)).await?; // already serialized TL with gettime query let mut query = hex::decode("7af98bb435263e6c95d6fecb497dfd0aa5f031e7d412986b5ce720496db512052e8f2d100cdf068c7904345aad16000000000000")?; @@ -50,5 +49,4 @@ async fn main() -> Result<()> { ); Ok(()) } - ``` diff --git a/examples/time.rs b/examples/time.rs index 04471dc..6a38d1b 100644 --- a/examples/time.rs +++ b/examples/time.rs @@ -1,6 +1,7 @@ -use std::net::SocketAddrV4; use adnl::AdnlClient; use anyhow::{anyhow, Context, Result}; +use std::net::SocketAddrV4; + #[tokio::main] async fn main() -> Result<()> { @@ -12,10 +13,8 @@ async fn main() -> Result<()> { let ls_ip = "65.21.74.140"; let ls_port = 46427; // create AdnlClient - let mut client = AdnlClient::connect( - remote_public, - SocketAddrV4::new(ls_ip.parse()?, ls_port) - ).await?; + let mut client = + AdnlClient::connect(remote_public, SocketAddrV4::new(ls_ip.parse()?, ls_port)).await?; // already serialized TL with gettime query let mut query = hex::decode("7af98bb435263e6c95d6fecb497dfd0aa5f031e7d412986b5ce720496db512052e8f2d100cdf068c7904345aad16000000000000")?; diff --git a/src/helper_types.rs b/src/helper_types.rs index 4328782..b025bce 100644 --- a/src/helper_types.rs +++ b/src/helper_types.rs @@ -1,6 +1,6 @@ +use sha2::{Digest, Sha256}; use std::io::Error; use std::net::AddrParseError; -use sha2::{Digest, Sha256}; use thiserror::Error; pub trait CryptoRandom: rand_core::RngCore + rand_core::CryptoRng {} @@ -12,7 +12,7 @@ pub trait AdnlPublicKey { let mut hasher = Sha256::new(); hasher.update([0xc6, 0xb4, 0x13, 0x48]); // type id - always ed25519 hasher.update(self.to_bytes()); - AdnlAddress(hasher.finalize().try_into().unwrap()) + AdnlAddress(hasher.finalize().into()) } fn to_bytes(&self) -> [u8; 32]; @@ -162,5 +162,5 @@ pub enum AdnlError { #[error("Incorrect ip address")] IncorrectAddr(AddrParseError), #[error(transparent)] - OtherError(#[from] Error) + OtherError(#[from] Error), } diff --git a/src/primitives/handshake.rs b/src/primitives/handshake.rs index b33b63a..7696d4c 100644 --- a/src/primitives/handshake.rs +++ b/src/primitives/handshake.rs @@ -44,7 +44,7 @@ impl AdnlHandshake

{ let mut raw_params = self.aes_params.to_bytes(); let mut hasher = Sha256::new(); hasher.update(raw_params); - let hash: [u8; 32] = hasher.finalize().try_into().unwrap(); + let hash: [u8; 32] = hasher.finalize().into(); let mut key = [0u8; 32]; key[..16].copy_from_slice(&self.secret.as_bytes()[..16]); diff --git a/src/primitives/receive.rs b/src/primitives/receive.rs index 6f71f5c..a4cf14f 100644 --- a/src/primitives/receive.rs +++ b/src/primitives/receive.rs @@ -34,7 +34,8 @@ impl AdnlReceiver { let mut length = [0u8; 4]; log::debug!("reading length"); transport - .read_exact(&mut length).await + .read_exact(&mut length) + .await .map_err(AdnlError::ReadError)?; self.aes.apply_keystream(&mut length); let length = u32::from_le_bytes(length); @@ -49,7 +50,8 @@ impl AdnlReceiver { let mut nonce = [0u8; 32]; log::debug!("reading nonce"); transport - .read_exact(&mut nonce).await + .read_exact(&mut nonce) + .await .map_err(AdnlError::ReadError)?; self.aes.apply_keystream(&mut nonce); hasher.update(nonce); @@ -65,12 +67,14 @@ impl AdnlReceiver { bytes_to_read ); transport - .read_exact(&mut buffer).await + .read_exact(&mut buffer) + .await .map_err(AdnlError::ReadError)?; self.aes.apply_keystream(&mut buffer); hasher.update(buffer); consumer - .write_all(&buffer).await + .write_all(&buffer) + .await .map_err(AdnlError::WriteError)?; bytes_to_read -= BUFFER; } @@ -80,12 +84,14 @@ impl AdnlReceiver { log::debug!("last chunk, {} bytes remaining", bytes_to_read); let buffer = &mut buffer[..bytes_to_read]; transport - .read_exact(buffer).await + .read_exact(buffer) + .await .map_err(AdnlError::ReadError)?; self.aes.apply_keystream(buffer); hasher.update(&buffer); consumer - .write_all(buffer).await + .write_all(buffer) + .await .map_err(AdnlError::WriteError)?; } } @@ -93,7 +99,8 @@ impl AdnlReceiver { let mut given_hash = [0u8; 32]; log::debug!("reading hash"); transport - .read_exact(&mut given_hash).await + .read_exact(&mut given_hash) + .await .map_err(AdnlError::ReadError)?; self.aes.apply_keystream(&mut given_hash); diff --git a/src/primitives/send.rs b/src/primitives/send.rs index d7ab951..41a7e93 100644 --- a/src/primitives/send.rs +++ b/src/primitives/send.rs @@ -39,7 +39,7 @@ impl AdnlSender { let mut hasher = Sha256::new(); hasher.update(*nonce); hasher.update(&*buffer); - let mut hash: [u8; 32] = hasher.finalize().try_into().unwrap(); + let mut hash: [u8; 32] = hasher.finalize().into(); // encrypt packet self.aes.apply_keystream(&mut length); @@ -49,16 +49,20 @@ impl AdnlSender { // write to transport transport - .write_all(&length).await + .write_all(&length) + .await .map_err(AdnlError::WriteError)?; transport - .write_all(nonce).await + .write_all(nonce) + .await .map_err(AdnlError::WriteError)?; transport - .write_all(buffer).await + .write_all(buffer) + .await .map_err(AdnlError::WriteError)?; transport - .write_all(&hash).await + .write_all(&hash) + .await .map_err(AdnlError::WriteError)?; transport.flush().await.map_err(AdnlError::WriteError)?; diff --git a/src/tests.rs b/src/tests.rs index a8500d7..9474b04 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -89,7 +89,9 @@ async fn test_send(aes_params: Vec, nonce: Vec, buffer: Vec, expecte let aes_params = AdnlAesParams::from(aes_params); let mut protocol_client = AdnlSender::new(&aes_params); let mut packet = Vec::::new(); - let _result = protocol_client.send(&mut packet, &mut nonce, &mut buffer).await; + let _result = protocol_client + .send(&mut packet, &mut nonce, &mut buffer) + .await; assert_eq!( packet.as_slice(), &expected_packet, diff --git a/src/wrappers/client.rs b/src/wrappers/client.rs index caedb0d..0e07382 100644 --- a/src/wrappers/client.rs +++ b/src/wrappers/client.rs @@ -1,5 +1,5 @@ use crate::{AdnlBuilder, AdnlError, AdnlHandshake, AdnlPublicKey, AdnlReceiver, AdnlSender}; -use tokio::io::{AsyncReadExt, AsyncWriteExt, empty}; +use tokio::io::{empty, AsyncReadExt, AsyncWriteExt}; use tokio::net::{TcpStream, ToSocketAddrs}; use x25519_dalek::StaticSecret; @@ -27,13 +27,13 @@ impl AdnlClient { // then perform handshake over our TcpStream let client = AdnlBuilder::with_random_aes_params(&mut rand::rngs::OsRng) .perform_ecdh(local_secret, ls_public) - .perform_handshake(transport).await?; + .perform_handshake(transport) + .await?; Ok(client) } } impl AdnlClient { - /// Send `handshake` over `transport` and check that handshake was successful pub async fn perform_handshake( mut transport: T, @@ -41,7 +41,8 @@ impl AdnlClient { ) -> Result { // send handshake transport - .write_all(&handshake.to_bytes()).await + .write_all(&handshake.to_bytes()) + .await .map_err(AdnlError::WriteError)?; // receive empty message to ensure that server knows our AES keys @@ -51,16 +52,21 @@ impl AdnlClient { transport, }; let mut empty = empty(); - client.receiver.receive::<_, _, 0>(&mut client.transport, &mut empty).await?; + client + .receiver + .receive::<_, _, 0>(&mut client.transport, &mut empty) + .await?; Ok(client) } - + /// Send `data` to another peer with random nonce pub async fn send( &mut self, data: &mut [u8], ) -> Result<(), AdnlError> { - self.sender.send(&mut self.transport, &mut rand::random(), data).await + self.sender + .send(&mut self.transport, &mut rand::random(), data) + .await } /// Send `data` to another peer. Random `nonce` must be provided to eliminate bit-flipping attacks. @@ -71,8 +77,7 @@ impl AdnlClient { ) -> Result<(), AdnlError> { self.sender.send(&mut self.transport, nonce, data).await } - - + /// Receive data from another peer into `consumer` which will process the data with /// a `BUFFER` size of 8192 bytes. pub async fn receive( @@ -80,7 +85,8 @@ impl AdnlClient { consumer: &mut C, ) -> Result<(), AdnlError> { self.receiver - .receive::<_, _, 8192>(&mut self.transport, consumer).await + .receive::<_, _, 8192>(&mut self.transport, consumer) + .await } /// Receive data from another peer into `consumer` which will process the data. Set `BUFFER` @@ -90,6 +96,7 @@ impl AdnlClient { consumer: &mut C, ) -> Result<(), AdnlError> { self.receiver - .receive::<_, _, BUFFER>(&mut self.transport, consumer).await + .receive::<_, _, BUFFER>(&mut self.transport, consumer) + .await } }