Merge pull request #1413 from ViktorTigerstrom/2022-04-default-to-bolt4-tlv-onions
[rust-lightning] / lightning / src / ln / peer_channel_encryptor.rs
index 29fa84505fcefc55a255b842f05fd5eca84bed65..277b0faf6a0d4e81f488a40b440de9d09940caad 100644 (file)
@@ -7,10 +7,11 @@
 // You may not use this file except in accordance with one or both of these
 // licenses.
 
-use prelude::*;
+use crate::prelude::*;
 
-use ln::msgs::LightningError;
-use ln::msgs;
+use crate::ln::msgs::LightningError;
+use crate::ln::msgs;
+use crate::ln::wire;
 
 use bitcoin::hashes::{Hash, HashEngine};
 use bitcoin::hashes::sha256::Hash as Sha256;
@@ -20,8 +21,9 @@ use bitcoin::secp256k1::{PublicKey,SecretKey};
 use bitcoin::secp256k1::ecdh::SharedSecret;
 use bitcoin::secp256k1;
 
-use util::chacha20poly1305rfc::ChaCha20Poly1305RFC;
-use util::crypto::hkdf_extract_expand_twice;
+use crate::util::chacha20poly1305rfc::ChaCha20Poly1305RFC;
+use crate::util::crypto::hkdf_extract_expand_twice;
+use crate::util::ser::VecWriter;
 use bitcoin::hashes::hex::ToHex;
 
 /// Maximum Lightning message data length according to
@@ -142,6 +144,19 @@ impl PeerChannelEncryptor {
                res[plaintext.len()..].copy_from_slice(&tag);
        }
 
+       #[inline]
+       /// Encrypts the message in res[offset..] in-place and pushes a 16-byte tag onto the end of
+       /// res.
+       fn encrypt_in_place_with_ad(res: &mut Vec<u8>, offset: usize, n: u64, key: &[u8; 32], h: &[u8]) {
+               let mut nonce = [0; 12];
+               nonce[4..].copy_from_slice(&n.to_le_bytes()[..]);
+
+               let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce, h);
+               let mut tag = [0; 16];
+               chacha.encrypt_full_message_in_place(&mut res[offset..], &mut tag);
+               res.extend_from_slice(&tag);
+       }
+
        #[inline]
        fn decrypt_with_ad(res: &mut[u8], n: u64, key: &[u8; 32], h: &[u8], cyphertext: &[u8]) -> Result<(), LightningError> {
                let mut nonce = [0; 12];
@@ -372,9 +387,9 @@ impl PeerChannelEncryptor {
                Ok(self.their_node_id.unwrap().clone())
        }
 
-       /// Encrypts the given message, returning the encrypted version
+       /// Encrypts the given pre-serialized message, returning the encrypted version.
        /// panics if msg.len() > 65535 or Noise handshake has not finished.
-       pub fn encrypt_message(&mut self, msg: &[u8]) -> Vec<u8> {
+       pub fn encrypt_buffer(&mut self, msg: &[u8]) -> Vec<u8> {
                if msg.len() > LN_MAX_MSG_LEN {
                        panic!("Attempted to encrypt message longer than 65535 bytes!");
                }
@@ -403,6 +418,42 @@ impl PeerChannelEncryptor {
                res
        }
 
+       /// Encrypts the given message, returning the encrypted version.
+       /// panics if the length of `message`, once encoded, is greater than 65535 or if the Noise
+       /// handshake has not finished.
+       pub fn encrypt_message<M: wire::Type>(&mut self, message: &M) -> Vec<u8> {
+               // Allocate a buffer with 2KB, fitting most common messages. Reserve the first 16+2 bytes
+               // for the 2-byte message type prefix and its MAC.
+               let mut res = VecWriter(Vec::with_capacity(2048));
+               res.0.resize(16 + 2, 0);
+               wire::write(message, &mut res).expect("In-memory messages must never fail to serialize");
+
+               let msg_len = res.0.len() - 16 - 2;
+               if msg_len > LN_MAX_MSG_LEN {
+                       panic!("Attempted to encrypt message longer than 65535 bytes!");
+               }
+
+               match self.noise_state {
+                       NoiseState::Finished { ref mut sk, ref mut sn, ref mut sck, rk: _, rn: _, rck: _ } => {
+                               if *sn >= 1000 {
+                                       let (new_sck, new_sk) = hkdf_extract_expand_twice(sck, sk);
+                                       *sck = new_sck;
+                                       *sk = new_sk;
+                                       *sn = 0;
+                               }
+
+                               Self::encrypt_with_ad(&mut res.0[0..16+2], *sn, sk, &[0; 0], &(msg_len as u16).to_be_bytes());
+                               *sn += 1;
+
+                               Self::encrypt_in_place_with_ad(&mut res.0, 16+2, *sn, sk, &[0; 0]);
+                               *sn += 1;
+                       },
+                       _ => panic!("Tried to encrypt a message prior to noise handshake completion"),
+               }
+
+               res.0
+       }
+
        /// Decrypts a message length header from the remote peer.
        /// panics if noise handshake has not yet finished or msg.len() != 18
        pub fn decrypt_length_header(&mut self, msg: &[u8]) -> Result<u16, LightningError> {
@@ -476,7 +527,7 @@ mod tests {
 
        use hex;
 
-       use ln::peer_channel_encryptor::{PeerChannelEncryptor,NoiseState};
+       use crate::ln::peer_channel_encryptor::{PeerChannelEncryptor,NoiseState};
 
        fn get_outbound_peer_for_initiator_test_vectors() -> PeerChannelEncryptor {
                let their_node_id = PublicKey::from_slice(&hex::decode("028d7500dd4c12685d1f568b4c2b5048e8534b873319f3a8daa612b469132ec7f7").unwrap()[..]).unwrap();
@@ -682,7 +733,7 @@ mod tests {
 
                for i in 0..1005 {
                        let msg = [0x68, 0x65, 0x6c, 0x6c, 0x6f];
-                       let res = outbound_peer.encrypt_message(&msg);
+                       let res = outbound_peer.encrypt_buffer(&msg);
                        assert_eq!(res.len(), 5 + 2*16 + 2);
 
                        let len_header = res[0..2+16].to_vec();
@@ -716,7 +767,7 @@ mod tests {
        fn max_message_len_encryption() {
                let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
                let msg = [4u8; LN_MAX_MSG_LEN + 1];
-               outbound_peer.encrypt_message(&msg);
+               outbound_peer.encrypt_buffer(&msg);
        }
 
        #[test]