Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit 0ae675b

Browse files
committed
Review feedback
1 parent f190a63 commit 0ae675b

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

util/network-devp2p/src/connection.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,10 @@ impl EncryptedConnection {
315315
let key_material_keccak = keccak(&key_material);
316316
(&mut key_material[32..64]).copy_from_slice(key_material_keccak.as_bytes());
317317

318-
// TODO: clarify this: ecdh::agree creates a **NEW** secret right? And AesCtr256 keeps an internal counter, right?
319-
// Using a 0 IV with CTR is fine as long as the same IV is never reused with the same key. This is not the case here.
318+
// Using a 0 IV with CTR is fine as long as the same IV is never reused with the same key.
319+
// This is the case here: ecdh creates a new secret which will be the symmetric key used
320+
// only for this session the 0 IV is only use once with this secret, so we are in the case
321+
// of same IV use for different key.
320322
let encoder = AesCtr256::new(&key_material[32..64], &NULL_IV)?;
321323
let decoder = AesCtr256::new(&key_material[32..64], &NULL_IV)?;
322324
let key_material_keccak = keccak(&key_material);
@@ -352,6 +354,7 @@ impl EncryptedConnection {
352354

353355
/// Send a packet
354356
pub fn send_packet<Message>(&mut self, io: &IoContext<Message>, payload: &[u8]) -> Result<(), Error> where Message: Send + Clone + Sync + 'static {
357+
const HEADER_LEN: usize = 16;
355358
let mut header = RlpStream::new();
356359
let len = payload.len();
357360
if len > MAX_PAYLOAD_SIZE {
@@ -362,14 +365,13 @@ impl EncryptedConnection {
362365
header.append_raw(&[0xc2u8, 0x80u8, 0x80u8], 1);
363366
let padding = (16 - (len % 16)) % 16;
364367

365-
let mut packet = vec![0u8; 32 + len + padding + 16];
368+
let mut packet = vec![0u8; 16 + 16 + len + padding + 16];
366369
let mut header = header.out();
367-
header.resize(16, 0u8);
368-
let header_len = header.len();
369-
&mut packet[..header_len].copy_from_slice(&mut header);
370-
self.encoder.encrypt(&mut packet[..header_len])?;
371-
EncryptedConnection::update_mac(&mut self.egress_mac, &self.mac_encoder_key, &packet[..header_len])?;
372-
self.egress_mac.clone().finalize(&mut packet[header_len..32]);
370+
header.resize(HEADER_LEN, 0u8);
371+
&mut packet[..HEADER_LEN].copy_from_slice(&mut header);
372+
self.encoder.encrypt(&mut packet[..HEADER_LEN])?;
373+
EncryptedConnection::update_mac(&mut self.egress_mac, &self.mac_encoder_key, &packet[..HEADER_LEN])?;
374+
self.egress_mac.clone().finalize(&mut packet[HEADER_LEN..32]);
373375
&mut packet[32..32 + len].copy_from_slice(payload);
374376
self.encoder.encrypt(&mut packet[32..32 + len])?;
375377
if padding != 0 {

0 commit comments

Comments
 (0)