Skip to content

Commit

Permalink
chore(all): apply cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
akostylev0 committed Oct 29, 2024
1 parent 4c7aa52 commit b7076ed
Show file tree
Hide file tree
Showing 59 changed files with 4,277 additions and 2,836 deletions.
49 changes: 34 additions & 15 deletions adnl-tcp/src/aes_ctr.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::ops::Mul;
use rand::Rng;
use aes::cipher::generic_array::GenericArray;
use aes::cipher::KeyIvInit;
use aes::cipher::StreamCipher;
use anyhow::{anyhow, bail};
use ed25519_dalek::hazmat::ExpandedSecretKey;
use ed25519_dalek::VerifyingKey;
use rand::Rng;
use sha2::{Digest, Sha256};
use std::ops::Mul;

pub type Aes256Ctr128 = ctr::Ctr128BE<aes::Aes256>;

pub struct AesCtr {
basis: [u8; 160]
basis: [u8; 160],
}

impl AesCtr {
Expand All @@ -22,8 +22,16 @@ impl AesCtr {
Self { basis }
}

pub fn from_encrypted(basis: &[u8; 160], checksum: &[u8; 32], expanded_secret_key: &ExpandedSecretKey, verifying_key: &VerifyingKey) -> anyhow::Result<Self> {
let x = verifying_key.to_montgomery().mul(expanded_secret_key.scalar).to_bytes();
pub fn from_encrypted(
basis: &[u8; 160],
checksum: &[u8; 32],
expanded_secret_key: &ExpandedSecretKey,
verifying_key: &VerifyingKey,
) -> anyhow::Result<Self> {
let x = verifying_key
.to_montgomery()
.mul(expanded_secret_key.scalar)
.to_bytes();

let mut cipher = Self::cipher(&x, checksum);
let mut basis_decrypted = [0u8; 160];
Expand All @@ -35,16 +43,25 @@ impl AesCtr {
bail!("wrong handshake checksum");
}

Ok(Self { basis: basis_decrypted })
Ok(Self {
basis: basis_decrypted,
})
}

pub fn into_bytes(self) -> [u8; 160] {
self.basis
}

pub fn encrypt(&self, expanded_secret_key: &ExpandedSecretKey, verifying_key: &VerifyingKey) -> ([u8; 160], [u8; 32]) {
pub fn encrypt(
&self,
expanded_secret_key: &ExpandedSecretKey,
verifying_key: &VerifyingKey,
) -> ([u8; 160], [u8; 32]) {
let checksum: [u8; 32] = Sha256::digest(self.basis).into();
let x = verifying_key.to_montgomery().mul(expanded_secret_key.scalar).to_bytes();
let x = verifying_key
.to_montgomery()
.mul(expanded_secret_key.scalar)
.to_bytes();

let mut cipher = Self::cipher(&x, &checksum);
let mut basis_encrypted = [0u8; 160];
Expand All @@ -57,16 +74,18 @@ impl AesCtr {

fn cipher(x: &[u8; 32], y: &[u8; 32]) -> Aes256Ctr128 {
let key = [
x[ 0], x[ 1], x[ 2], x[ 3], x[ 4], x[ 5], x[ 6], x[ 7],
x[ 8], x[ 9], x[10], x[11], x[12], x[13], x[14], x[15],
y[16], y[17], y[18], y[19], y[20], y[21], y[22], y[23],
y[24], y[25], y[26], y[27], y[28], y[29], y[30], y[31]
x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10], x[11], x[12], x[13],
x[14], x[15], y[16], y[17], y[18], y[19], y[20], y[21], y[22], y[23], y[24], y[25],
y[26], y[27], y[28], y[29], y[30], y[31],
];
let ctr = [
y[ 0], y[ 1], y[ 2], y[ 3], x[20], x[21], x[22], x[23],
x[24], x[25], x[26], x[27], x[28], x[29], x[30], x[31]
y[0], y[1], y[2], y[3], x[20], x[21], x[22], x[23], x[24], x[25], x[26], x[27], x[28],
x[29], x[30], x[31],
];

Aes256Ctr128::new(GenericArray::from_slice(&key), GenericArray::from_slice(&ctr))
Aes256Ctr128::new(
GenericArray::from_slice(&key),
GenericArray::from_slice(&ctr),
)
}
}
38 changes: 24 additions & 14 deletions adnl-tcp/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
use std::time::Duration;
use crate::aes_ctr::AesCtr;
use crate::codec::PacketCodec;
use crate::connection::Connection;
use crate::key::{Ed25519Key, Ed25519KeyId};
use anyhow::{anyhow, bail};
use ed25519_dalek::VerifyingKey;
use futures::StreamExt;
use tokio::net::{TcpStream, ToSocketAddrs};
use tokio_util::codec::Framed;
use std::time::Duration;
use tokio::io::AsyncWriteExt;
use tokio::net::{TcpStream, ToSocketAddrs};
use tokio::time::timeout;
use crate::aes_ctr::AesCtr;
use crate::codec::PacketCodec;
use crate::key::{Ed25519Key, Ed25519KeyId};
use crate::connection::Connection;
use tokio_util::codec::Framed;

pub type ServerKey = [u8; 32];

pub struct Client;

impl Client {
pub async fn connect<A: ToSocketAddrs>(addr: A, server_key: ServerKey) -> anyhow::Result<Connection> {
pub async fn connect<A: ToSocketAddrs>(
addr: A,
server_key: ServerKey,
) -> anyhow::Result<Connection> {
let mut stream = TcpStream::connect(addr).await?;

let aes_ctr = AesCtr::generate();
let server_public_key = VerifyingKey::from_bytes(&server_key)?;
let server_key_id = Ed25519KeyId::from_public_key_bytes(&server_key);
let client_key = Ed25519Key::generate();

let (basis, checksum) = aes_ctr.encrypt(client_key.expanded_secret_key(), &server_public_key);
let (basis, checksum) =
aes_ctr.encrypt(client_key.expanded_secret_key(), &server_public_key);

stream.write_all(server_key_id.as_slice()).await?;
stream.write_all(client_key.public_key().as_bytes()).await?;
Expand Down Expand Up @@ -52,12 +56,12 @@ impl Client {

#[cfg(test)]
mod tests {
use std::net::{Ipv4Addr, SocketAddrV4};
use super::*;
use crate::ping::{is_pong_packet, ping_packet};
use base64::Engine;
use futures::SinkExt;
use std::net::{Ipv4Addr, SocketAddrV4};
use tracing_test::traced_test;
use crate::ping::{is_pong_packet, ping_packet};
use super::*;

#[traced_test]
#[tokio::test]
Expand All @@ -82,7 +86,10 @@ mod tests {
let client = Client::connect(SocketAddrV4::new(ip, port), key).await;

assert!(client.is_err());
assert_eq!(client.err().unwrap().to_string(), "missed empty packet".to_string());
assert_eq!(
client.err().unwrap().to_string(),
"missed empty packet".to_string()
);

Ok(())
}
Expand All @@ -106,7 +113,10 @@ mod tests {
let ip: i32 = -2018147075;
let ip = Ipv4Addr::from(ip as u32);
let port = 46529;
let key: ServerKey = base64::engine::general_purpose::STANDARD.decode("jLO6yoooqUQqg4/1QXflpv2qGCoXmzZCR+bOsYJ2hxw=")?.as_slice().try_into()?;
let key: ServerKey = base64::engine::general_purpose::STANDARD
.decode("jLO6yoooqUQqg4/1QXflpv2qGCoXmzZCR+bOsYJ2hxw=")?
.as_slice()
.try_into()?;

tracing::info!("Connecting to {}:{} with key {:?}", ip, port, key);

Expand Down
109 changes: 80 additions & 29 deletions adnl-tcp/src/codec.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::aes_ctr::{Aes256Ctr128, AesCtr};
use crate::packet::Packet;
use aes::cipher::generic_array::GenericArray;
use anyhow::bail;
use ctr::cipher::StreamCipher;
use ctr::cipher::KeyIvInit;
use ctr::cipher::StreamCipher;
use sha2::{Digest, Sha256};
use tokio_util::bytes::{BufMut, BytesMut};
use tokio_util::codec::{Decoder, Encoder};
use crate::aes_ctr::{Aes256Ctr128, AesCtr};
use crate::packet::Packet;

pub struct PacketCodec {
cipher_recv: Aes256Ctr128,
cipher_send: Aes256Ctr128,
next_len: Option<usize>
next_len: Option<usize>,
}

impl Encoder<Packet> for PacketCodec {
Expand All @@ -28,7 +28,7 @@ impl Encoder<Packet> for PacketCodec {
dst.put(&packet.data[..]);
dst.put(&packet.checksum[..]);

self.cipher_send.apply_keystream(&mut dst[buf_size .. ]);
self.cipher_send.apply_keystream(&mut dst[buf_size..]);

Ok(())
}
Expand All @@ -40,7 +40,7 @@ impl Decoder for PacketCodec {

fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
let length = match self.next_len.take() {
Some(len) => { len }
Some(len) => len,
None => {
if src.len() < 4 {
return Ok(None);
Expand All @@ -49,7 +49,7 @@ impl Decoder for PacketCodec {
let mut length = src.split_to(4);
self.cipher_recv.apply_keystream(&mut length);

u32::from_le_bytes([ length[0], length[1], length[2], length[3] ]) as usize
u32::from_le_bytes([length[0], length[1], length[2], length[3]]) as usize
}
};

Expand All @@ -64,17 +64,17 @@ impl Decoder for PacketCodec {
return Ok(None);
}

self.cipher_recv.apply_keystream(&mut src[0 .. length]);
self.cipher_recv.apply_keystream(&mut src[0..length]);
let sha256: [u8; 32] = Sha256::digest(&src[..length - 32]).into();
if sha256 != src[length - 32..length] {
bail!("incorrect checksum for ADNL packet");
}

let data = src.split_to(length);
let packet = Packet {
nonce: data[0 .. 32].try_into()?,
data: data[32 .. length - 32].to_vec(),
checksum: data[length - 32 .. length].try_into()?
nonce: data[0..32].try_into()?,
data: data[32..length - 32].to_vec(),
checksum: data[length - 32..length].try_into()?,
};

src.reserve(68); // min size of packet is 68
Expand All @@ -97,28 +97,48 @@ impl PacketCodec {
}

fn from_bytes_as_client(bytes: &[u8; 160]) -> Self {
let cipher_recv = Aes256Ctr128::new(GenericArray::from_slice(&bytes[0..32]), GenericArray::from_slice(&bytes[64 .. 80]));
let cipher_send = Aes256Ctr128::new(GenericArray::from_slice(&bytes[32..64]), GenericArray::from_slice(&bytes[80 .. 96]));

Self { cipher_recv, cipher_send, next_len: None }
let cipher_recv = Aes256Ctr128::new(
GenericArray::from_slice(&bytes[0..32]),
GenericArray::from_slice(&bytes[64..80]),
);
let cipher_send = Aes256Ctr128::new(
GenericArray::from_slice(&bytes[32..64]),
GenericArray::from_slice(&bytes[80..96]),
);

Self {
cipher_recv,
cipher_send,
next_len: None,
}
}

fn from_bytes_as_server(bytes: &[u8; 160]) -> Self {
let cipher_recv = Aes256Ctr128::new(GenericArray::from_slice(&bytes[32..64]), GenericArray::from_slice(&bytes[80 .. 96]));
let cipher_send = Aes256Ctr128::new(GenericArray::from_slice(&bytes[0..32]), GenericArray::from_slice(&bytes[64 .. 80]));

Self { cipher_recv, cipher_send, next_len: None }
let cipher_recv = Aes256Ctr128::new(
GenericArray::from_slice(&bytes[32..64]),
GenericArray::from_slice(&bytes[80..96]),
);
let cipher_send = Aes256Ctr128::new(
GenericArray::from_slice(&bytes[0..32]),
GenericArray::from_slice(&bytes[64..80]),
);

Self {
cipher_recv,
cipher_send,
next_len: None,
}
}
}

#[cfg(test)]
mod tests {
use crate::codec::PacketCodec;
use crate::packet::Packet;
use anyhow::Result;
use tokio_util::bytes::{BufMut, BytesMut};
use tokio_util::codec::{Decoder, Encoder};
use tracing_test::traced_test;
use anyhow::Result;
use crate::codec::PacketCodec;
use crate::packet::Packet;

#[test]
#[traced_test]
Expand Down Expand Up @@ -155,10 +175,10 @@ mod tests {
let mut codec = given_codec_client();
let data = empty_packet_bytes();
let mut buf = BytesMut::with_capacity(68);
buf.put(&data[.. 4]);
buf.put(&data[..4]);
let _ = codec.decode(&mut buf)?;

buf.put(&data[4 ..]);
buf.put(&data[4..]);
let packet = codec.decode(&mut buf)?.unwrap();

assert_eq!(packet, empty_packet());
Expand All @@ -168,21 +188,52 @@ mod tests {

fn empty_packet() -> Packet {
Packet {
nonce: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32],
nonce: [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32,
],
data: vec![],
checksum: [174, 33, 108, 46, 245, 36, 122, 55, 130, 193, 53, 239, 162, 121, 163, 228, 205, 198, 16, 148, 39, 15, 93, 43, 229, 140, 98, 4, 183, 166, 18, 201],
checksum: [
174, 33, 108, 46, 245, 36, 122, 55, 130, 193, 53, 239, 162, 121, 163, 228, 205,
198, 16, 148, 39, 15, 93, 43, 229, 140, 98, 4, 183, 166, 18, 201,
],
}
}

fn empty_packet_bytes() -> Vec<u8> {
vec![236, 54, 122, 182, 214, 17, 172, 214, 162, 205, 134, 120, 170, 101, 249, 104, 117, 236, 29, 70, 212, 118, 65, 112, 186, 83, 233, 46, 171, 214, 143, 155, 205, 253, 189, 157, 156, 69, 198, 77, 204, 137, 167, 26, 135, 32, 12, 15, 136, 91, 210, 94, 248, 123, 235, 232, 40, 156, 113, 185, 39, 169, 111, 2, 228, 61, 170, 25]
vec![
236, 54, 122, 182, 214, 17, 172, 214, 162, 205, 134, 120, 170, 101, 249, 104, 117, 236,
29, 70, 212, 118, 65, 112, 186, 83, 233, 46, 171, 214, 143, 155, 205, 253, 189, 157,
156, 69, 198, 77, 204, 137, 167, 26, 135, 32, 12, 15, 136, 91, 210, 94, 248, 123, 235,
232, 40, 156, 113, 185, 39, 169, 111, 2, 228, 61, 170, 25,
]
}

fn given_codec_server() -> PacketCodec {
PacketCodec::from_bytes_as_server(&[222, 151, 216, 98, 74, 67, 129, 33, 184, 106, 25, 86, 84, 75, 215, 46, 214, 140, 214, 159, 44, 153, 85, 91, 8, 177, 232, 197, 31, 253, 81, 28, 55, 250, 129, 200, 76, 205, 84, 124, 48, 193, 118, 177, 24, 213, 203, 137, 43, 219, 17, 62, 142, 128, 20, 31, 38, 101, 25, 66, 46, 249, 238, 253, 134, 37, 18, 162, 54, 61, 178, 179, 163, 117, 192, 212, 187, 189, 39, 23, 33, 128, 216, 159, 35, 242, 226, 89, 186, 200, 80, 171, 2, 97, 147, 1, 151, 110, 92, 63, 166, 32, 9, 44, 113, 141, 133, 44, 167, 3, 182, 218, 158, 48, 117, 185, 242, 236, 184, 237, 66, 217, 247, 70, 191, 38, 170, 251, 127, 138, 50, 85, 4, 231, 49, 94, 218, 153, 125, 183, 134, 28, 148, 71, 245, 195, 239, 242, 99, 51, 178, 1, 128, 71, 93, 148, 68, 58, 16, 198])
PacketCodec::from_bytes_as_server(&[
222, 151, 216, 98, 74, 67, 129, 33, 184, 106, 25, 86, 84, 75, 215, 46, 214, 140, 214,
159, 44, 153, 85, 91, 8, 177, 232, 197, 31, 253, 81, 28, 55, 250, 129, 200, 76, 205,
84, 124, 48, 193, 118, 177, 24, 213, 203, 137, 43, 219, 17, 62, 142, 128, 20, 31, 38,
101, 25, 66, 46, 249, 238, 253, 134, 37, 18, 162, 54, 61, 178, 179, 163, 117, 192, 212,
187, 189, 39, 23, 33, 128, 216, 159, 35, 242, 226, 89, 186, 200, 80, 171, 2, 97, 147,
1, 151, 110, 92, 63, 166, 32, 9, 44, 113, 141, 133, 44, 167, 3, 182, 218, 158, 48, 117,
185, 242, 236, 184, 237, 66, 217, 247, 70, 191, 38, 170, 251, 127, 138, 50, 85, 4, 231,
49, 94, 218, 153, 125, 183, 134, 28, 148, 71, 245, 195, 239, 242, 99, 51, 178, 1, 128,
71, 93, 148, 68, 58, 16, 198,
])
}

fn given_codec_client() -> PacketCodec {
PacketCodec::from_bytes_as_client(&[222, 151, 216, 98, 74, 67, 129, 33, 184, 106, 25, 86, 84, 75, 215, 46, 214, 140, 214, 159, 44, 153, 85, 91, 8, 177, 232, 197, 31, 253, 81, 28, 55, 250, 129, 200, 76, 205, 84, 124, 48, 193, 118, 177, 24, 213, 203, 137, 43, 219, 17, 62, 142, 128, 20, 31, 38, 101, 25, 66, 46, 249, 238, 253, 134, 37, 18, 162, 54, 61, 178, 179, 163, 117, 192, 212, 187, 189, 39, 23, 33, 128, 216, 159, 35, 242, 226, 89, 186, 200, 80, 171, 2, 97, 147, 1, 151, 110, 92, 63, 166, 32, 9, 44, 113, 141, 133, 44, 167, 3, 182, 218, 158, 48, 117, 185, 242, 236, 184, 237, 66, 217, 247, 70, 191, 38, 170, 251, 127, 138, 50, 85, 4, 231, 49, 94, 218, 153, 125, 183, 134, 28, 148, 71, 245, 195, 239, 242, 99, 51, 178, 1, 128, 71, 93, 148, 68, 58, 16, 198])
PacketCodec::from_bytes_as_client(&[
222, 151, 216, 98, 74, 67, 129, 33, 184, 106, 25, 86, 84, 75, 215, 46, 214, 140, 214,
159, 44, 153, 85, 91, 8, 177, 232, 197, 31, 253, 81, 28, 55, 250, 129, 200, 76, 205,
84, 124, 48, 193, 118, 177, 24, 213, 203, 137, 43, 219, 17, 62, 142, 128, 20, 31, 38,
101, 25, 66, 46, 249, 238, 253, 134, 37, 18, 162, 54, 61, 178, 179, 163, 117, 192, 212,
187, 189, 39, 23, 33, 128, 216, 159, 35, 242, 226, 89, 186, 200, 80, 171, 2, 97, 147,
1, 151, 110, 92, 63, 166, 32, 9, 44, 113, 141, 133, 44, 167, 3, 182, 218, 158, 48, 117,
185, 242, 236, 184, 237, 66, 217, 247, 70, 191, 38, 170, 251, 127, 138, 50, 85, 4, 231,
49, 94, 218, 153, 125, 183, 134, 28, 148, 71, 245, 195, 239, 242, 99, 51, 178, 1, 128,
71, 93, 148, 68, 58, 16, 198,
])
}
}
Loading

0 comments on commit b7076ed

Please sign in to comment.