From f18661f6f6c51e67c4eac1443a50f4be72356386 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Mon, 8 May 2023 14:23:56 -0400 Subject: [PATCH] Fix onion messages of size BIG_PACKET_HOP_DATA_LEN 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 | 3 ++- lightning/src/onion_message/functional_tests.rs | 17 +++++++++++++++++ pending_changelog/big-om-error.txt | 4 ++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 pending_changelog/big-om-error.txt diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index ebcf83bd..b7759d26 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -324,7 +324,8 @@ fn construct_onion_packet_with_init_noise( 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::::new(&keys.mu); diff --git a/lightning/src/onion_message/functional_tests.rs b/lightning/src/onion_message/functional_tests.rs index e7cf4e87..98fcdb68 100644 --- a/lightning/src/onion_message/functional_tests.rs +++ b/lightning/src/onion_message/functional_tests.rs @@ -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 index 00000000..6f2ce899 --- /dev/null +++ b/pending_changelog/big-om-error.txt @@ -0,0 +1,4 @@ +## Bug Fixes + +* Fixed sending large onion messages, which previously would result in an HMAC error on the second + hop (#2277). -- 2.30.2