33use ln:: peers:: { chacha, hkdf} ;
44use util:: byte_utils;
55
6- /// Returned after a successful handshake to encrypt and decrypt communication with peer nodes
6+ /// Returned after a successful handshake to encrypt and decrypt communication with peer nodes.
7+ /// It should not normally be manually instantiated.
78/// Automatically handles key rotation.
89/// For decryption, it is recommended to call `decrypt_message_stream` for automatic buffering.
910pub struct Conduit {
@@ -25,14 +26,14 @@ impl Conduit {
2526 let length = buffer. len ( ) as u16 ;
2627 let length_bytes = byte_utils:: be16_to_array ( length) ;
2728
28- let encrypted_length = chacha:: encrypt ( & self . sending_key , self . sending_nonce as u64 , & [ 0 ; 0 ] , & length_bytes) ;
29+ let mut ciphertext = vec ! [ 0u8 ; 18 + length as usize + 16 ] ;
30+
31+ ciphertext[ 0 ..18 ] . copy_from_slice ( & chacha:: encrypt ( & self . sending_key , self . sending_nonce as u64 , & [ 0 ; 0 ] , & length_bytes) ) ;
2932 self . increment_sending_nonce ( ) ;
3033
31- let encrypted_message = chacha:: encrypt ( & self . sending_key , self . sending_nonce as u64 , & [ 0 ; 0 ] , buffer) ;
34+ ciphertext [ 18 .. ] . copy_from_slice ( & chacha:: encrypt ( & self . sending_key , self . sending_nonce as u64 , & [ 0 ; 0 ] , buffer) ) ;
3235 self . increment_sending_nonce ( ) ;
3336
34- let mut ciphertext = encrypted_length;
35- ciphertext. extend_from_slice ( & encrypted_message) ;
3637 ciphertext
3738 }
3839
0 commit comments