Skip to content

Commit

Permalink
refactor(p2p): remove garbage (#4889)
Browse files Browse the repository at this point in the history
Signed-off-by: Shanin Roman <[email protected]>
  • Loading branch information
Erigara authored Jul 24, 2024
1 parent 2dec013 commit 14e50e4
Showing 1 changed file with 0 additions and 37 deletions.
37 changes: 0 additions & 37 deletions p2p/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use bytes::{Buf, BufMut, BytesMut};
use iroha_data_model::prelude::PeerId;
use message::*;
use parity_scale_codec::{DecodeAll, Encode};
use rand::{Rng, RngCore};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{
Expand Down Expand Up @@ -478,14 +477,12 @@ mod state {
let key_exchange = K::new();
let (kx_local_pk, kx_local_sk) = key_exchange.keypair(KeyGenOption::Random);
let write_half = &mut connection.write;
garbage::write(write_half).await?;
write_half
.write_all(K::encode_public_key(&kx_local_pk))
.await?;
// Read server hello with node's public key
let read_half = &mut connection.read;
let kx_remote_pk = {
garbage::read(read_half).await?;
// Then we have servers public key
let mut key = vec![0_u8; 32];
let _ = read_half.read_exact(&mut key).await?;
Expand Down Expand Up @@ -526,14 +523,12 @@ mod state {
let kx_local_pk_raw = K::encode_public_key(&kx_local_pk);
let read_half = &mut connection.read;
let kx_remote_pk = {
garbage::read(read_half).await?;
// And then we have clients public key
let mut key = vec![0_u8; 32];
let _ = read_half.read_exact(&mut key).await?;
K::decode_public_key(key).map_err(iroha_crypto::error::Error::from)?
};
let write_half = &mut connection.write;
garbage::write(write_half).await?;
write_half.write_all(kx_local_pk_raw).await?;
let shared_key = key_exchange.compute_shared_secret(&kx_local_sk, &kx_remote_pk);
let cryptographer = Cryptographer::new(&shared_key);
Expand Down Expand Up @@ -839,35 +834,3 @@ impl Connection {
Connection { id, read, write }
}
}

mod garbage {
//! Module with functions to read and write garbage.
// TODO: why do we need this?

use super::*;

/// Generate random garbage bytes and writes then to the stream.
pub(super) async fn write(stream: &mut OwnedWriteHalf) -> Result<(), Error> {
let size;
// Additional byte for the length of the garbage to send everything in one go
let mut garbage = [0u8; MAX_HANDSHAKE_LENGTH as usize + 1];
{
let rng = &mut rand::thread_rng();
size = rng.gen_range(64..=MAX_HANDSHAKE_LENGTH);
garbage[0] = size;
rng.fill_bytes(&mut garbage[1..=(size as usize)]);
}
iroha_logger::trace!(size, "Writing garbage");
stream.write_all(&garbage[..=(size as usize)]).await?;
Ok(())
}

/// Read and discards random garbage bytes from the stream.
pub(super) async fn read(stream: &mut OwnedReadHalf) -> Result<(), Error> {
let size = stream.read_u8().await? as usize;
iroha_logger::trace!(size, "Reading garbage");
let mut garbage = [0u8; MAX_HANDSHAKE_LENGTH as usize];
let _ = stream.read_exact(&mut garbage[..size]).await?;
Ok(())
}
}

0 comments on commit 14e50e4

Please sign in to comment.