Skip to content

Commit 7e9e99f

Browse files
committed
Fixed issues
1 parent cce612e commit 7e9e99f

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

p2p/src/network/noise/p2p_network_noise_reducer.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use chacha20poly1305::{aead::generic_array::GenericArray, AeadInPlace, ChaCha20Poly1305, KeyInit};
2+
use crypto_bigint::consts::U12;
23

34
use self::p2p_network_noise_state::ResponderConsumeOutput;
45

@@ -9,6 +10,8 @@ use super::p2p_network_noise_state::{
910
P2pNetworkNoiseStateInner, P2pNetworkNoiseStateResponder, ResponderOutput,
1011
};
1112

13+
const MAX_CHUNK_SIZE: usize = u16::MAX as usize - 19;
14+
1215
impl P2pNetworkNoiseState {
1316
pub fn reducer(&mut self, action: redux::ActionWithMeta<&P2pNetworkNoiseAction>) {
1417
match action.action() {
@@ -68,6 +71,7 @@ impl P2pNetworkNoiseState {
6871
let mut offset = 0;
6972
loop {
7073
let buf = &self.buffer[offset..];
74+
// TODO: add bug_condition
7175
let len = buf
7276
.get(..2)
7377
.and_then(|buf| Some(u16::from_be_bytes(buf.try_into().ok()?)));
@@ -189,22 +193,22 @@ impl P2pNetworkNoiseState {
189193
..
190194
} => {
191195
let aead = ChaCha20Poly1305::new(&send_key.0.into());
192-
let chunk_max_size = u16::MAX as usize - 19;
193196
let mut chunks = vec![];
194197

195-
for data in data.chunks(chunk_max_size) {
196-
let mut chunk = Vec::with_capacity(18 + data.len());
197-
chunk.extend_from_slice(&((data.len() + 16) as u16).to_be_bytes());
198-
chunk.extend_from_slice(data);
198+
for data_chunk in data.chunks(MAX_CHUNK_SIZE) {
199+
let mut chunk = Vec::with_capacity(18 + data_chunk.len());
200+
chunk
201+
.extend_from_slice(&((data_chunk.len() + 16) as u16).to_be_bytes());
202+
chunk.extend_from_slice(data_chunk);
199203

200-
let mut nonce = GenericArray::default();
204+
let mut nonce: GenericArray<u8, U12> = GenericArray::default();
201205
nonce[4..].clone_from_slice(&send_nonce.to_le_bytes());
202206
*send_nonce += 1;
203207

204208
let tag = aead.encrypt_in_place_detached(
205209
&nonce,
206210
&[],
207-
&mut chunk[2..(2 + data.len())],
211+
&mut chunk[2..(2 + data_chunk.len())],
208212
);
209213

210214
let tag = match tag {

p2p/src/network/noise/p2p_network_noise_state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl NoiseState {
127127
let hkdf = Hkdf::<Sha256, Hmac<Sha256>>::new(Some(&self.chaining_key.0), &secret);
128128
secret.zeroize();
129129
let mut okm = [0; 64];
130+
// this will only panic if `okm.len() > chunk_len * 255` with chunk_len being 32
130131
hkdf.expand(&[], &mut okm)
131132
.expect("the length is constant and small");
132133
self.chaining_key.0.clone_from_slice(&okm[..32]);
@@ -173,6 +174,7 @@ impl NoiseState {
173174

174175
let hkdf = Hkdf::<Sha256, Hmac<Sha256>>::new(Some(&self.chaining_key.0), b"");
175176
let mut okm = [0; 64];
177+
// this will only panic if `okm.len() > chunk_len * 255` with chunk_len being 32
176178
hkdf.expand(&[], &mut okm)
177179
.expect("the length is constant and small");
178180
fst.clone_from_slice(&okm[..32]);

p2p/src/network/pubsub/p2p_network_pubsub_effects.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl P2pNetworkPubsubAction {
100100
P2pNetworkPubsubAction::Broadcast { message } => {
101101
// println!("(pubsub) {this} broadcast");
102102
let mut buffer = vec![0; 8];
103+
// TODO: add bug_condition
103104
if binprot::BinProtWrite::binprot_write(&message, &mut buffer).is_err() {
104105
return;
105106
}

0 commit comments

Comments
 (0)