@@ -315,8 +315,10 @@ impl EncryptedConnection {
315
315
let key_material_keccak = keccak ( & key_material) ;
316
316
( & mut key_material[ 32 ..64 ] ) . copy_from_slice ( key_material_keccak. as_bytes ( ) ) ;
317
317
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.
320
322
let encoder = AesCtr256 :: new ( & key_material[ 32 ..64 ] , & NULL_IV ) ?;
321
323
let decoder = AesCtr256 :: new ( & key_material[ 32 ..64 ] , & NULL_IV ) ?;
322
324
let key_material_keccak = keccak ( & key_material) ;
@@ -352,6 +354,7 @@ impl EncryptedConnection {
352
354
353
355
/// Send a packet
354
356
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 ;
355
358
let mut header = RlpStream :: new ( ) ;
356
359
let len = payload. len ( ) ;
357
360
if len > MAX_PAYLOAD_SIZE {
@@ -362,14 +365,13 @@ impl EncryptedConnection {
362
365
header. append_raw ( & [ 0xc2u8 , 0x80u8 , 0x80u8 ] , 1 ) ;
363
366
let padding = ( 16 - ( len % 16 ) ) % 16 ;
364
367
365
- let mut packet = vec ! [ 0u8 ; 32 + len + padding + 16 ] ;
368
+ let mut packet = vec ! [ 0u8 ; 16 + 16 + len + padding + 16 ] ;
366
369
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 ] ) ;
373
375
& mut packet[ 32 ..32 + len] . copy_from_slice ( payload) ;
374
376
self . encoder . encrypt ( & mut packet[ 32 ..32 + len] ) ?;
375
377
if padding != 0 {
0 commit comments