Fix onion messages of size BIG_PACKET_HOP_DATA_LEN
authorValentine Wallace <vwallace@protonmail.com>
Mon, 8 May 2023 18:23:56 +0000 (14:23 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Mon, 8 May 2023 19:02:42 +0000 (15:02 -0400)
This was previously broken and would result in an invalid HMAC error, because
we had a hardcoded assumption that OM hop data would always be of size 1300.

lightning/src/ln/onion_utils.rs
lightning/src/onion_message/functional_tests.rs
pending_changelog/big-om-error.txt [new file with mode: 0644]

index ebcf83bd90dc2d0db68c5300e50cf2f6b06dd718..b7759d26f5c55eaee02900c3d4ebcc3c655f3af0 100644 (file)
@@ -324,7 +324,8 @@ fn construct_onion_packet_with_init_noise<HD: Writeable, P: Packet>(
                chacha.process_in_place(packet_data);
 
                if i == 0 {
-                       packet_data[ONION_DATA_LEN - filler.len()..ONION_DATA_LEN].copy_from_slice(&filler[..]);
+                       let onion_data_len = packet_data.len();
+                       packet_data[onion_data_len - filler.len()..onion_data_len].copy_from_slice(&filler[..]);
                }
 
                let mut hmac = HmacEngine::<Sha256>::new(&keys.mu);
index e7cf4e87d9f952703c1273c00de14800ea6e6f2e..98fcdb680ee03abd1f2c8f3730c0b740bb75977f 100644 (file)
@@ -284,3 +284,20 @@ fn peer_buffer_full() {
        let err = nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), OnionMessageContents::Custom(test_msg), None).unwrap_err();
        assert_eq!(err, SendError::BufferFull);
 }
+
+#[test]
+fn many_hops() {
+       // Check we can send over a route with many hops. This will exercise our logic for onion messages
+       // of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
+       let num_nodes: usize = 25;
+       let nodes = create_nodes(num_nodes as u8);
+       let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
+
+       let mut intermediates = vec![];
+       for i in 1..(num_nodes-1) {
+               intermediates.push(nodes[i].get_node_pk());
+       }
+
+       nodes[0].messenger.send_onion_message(&intermediates, Destination::Node(nodes[num_nodes-1].get_node_pk()), test_msg, None).unwrap();
+       pass_along_path(&nodes);
+}
diff --git a/pending_changelog/big-om-error.txt b/pending_changelog/big-om-error.txt
new file mode 100644 (file)
index 0000000..6f2ce89
--- /dev/null
@@ -0,0 +1,4 @@
+## Bug Fixes
+
+* Fixed sending large onion messages, which previously would result in an HMAC error on the second
+       hop (#2277).