3
3
use ln:: peers:: { chacha, hkdf} ;
4
4
use util:: byte_utils;
5
5
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.
7
8
/// Automatically handles key rotation.
8
9
/// For decryption, it is recommended to call `decrypt_message_stream` for automatic buffering.
9
10
pub struct Conduit {
@@ -25,14 +26,14 @@ impl Conduit {
25
26
let length = buffer. len ( ) as u16 ;
26
27
let length_bytes = byte_utils:: be16_to_array ( length) ;
27
28
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) ) ;
29
32
self . increment_sending_nonce ( ) ;
30
33
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) ) ;
32
35
self . increment_sending_nonce ( ) ;
33
36
34
- let mut ciphertext = encrypted_length;
35
- ciphertext. extend_from_slice ( & encrypted_message) ;
36
37
ciphertext
37
38
}
38
39
0 commit comments