Skip to content

Commit 3aa2a1e

Browse files
committed
Introduce test for Padding
Add test to verify blinded message and payment path padding.
1 parent fc13f92 commit 3aa2a1e

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

lightning/src/blinded_path/utils.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,19 @@ impl<T:Writeable> Writeable for BlindedPathWithPadding<T> {
229229

230230
self.tlvs.write(writer)
231231
}
232-
}
232+
}
233+
234+
#[cfg(test)]
235+
/// Checks if all the packets in the blinded path are properly padded.
236+
pub fn is_padded(hops: &[BlindedHop], padding_round_off: usize) -> bool {
237+
let first_hop = hops.first().expect("BlindedPath must have at least one hop");
238+
let first_payload_size = first_hop.encrypted_payload.len();
239+
240+
// The unencrypted payload data is padded before getting encrypted.
241+
// Assuming the first payload is padded properly, get the extra data length.
242+
let extra_length = first_payload_size % padding_round_off;
243+
hops.iter().all(|hop| {
244+
// Check that every packet is padded to the round off length subtracting the extra length.
245+
(hop.encrypted_payload.len() - extra_length) % padding_round_off == 0
246+
})
247+
}

lightning/src/ln/blinded_payment_tests.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey, schnorr};
1212
use bitcoin::secp256k1::ecdh::SharedSecret;
1313
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
1414
use crate::blinded_path;
15-
use crate::blinded_path::payment::{BlindedPaymentPath, Bolt12RefundContext, PaymentForwardNode, ForwardTlvs, PaymentConstraints, PaymentContext, PaymentRelay, UnauthenticatedReceiveTlvs};
15+
use crate::blinded_path::payment::{BlindedPaymentPath, Bolt12RefundContext, ForwardTlvs, PaymentConstraints, PaymentContext, PaymentForwardNode, PaymentRelay, UnauthenticatedReceiveTlvs, PAYMENT_PADDING_ROUND_OFF};
16+
use crate::blinded_path::utils::is_padded;
1617
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PaymentFailureReason};
1718
use crate::ln::types::ChannelId;
1819
use crate::types::payment::{PaymentHash, PaymentSecret};
@@ -1514,6 +1515,42 @@ fn fails_receive_tlvs_authentication() {
15141515
);
15151516
}
15161517

1518+
#[test]
1519+
fn blinded_payment_path_padding() {
1520+
// Make sure that for a blinded payment path, all encrypted payloads are padded to equal lengths.
1521+
let chanmon_cfgs = create_chanmon_cfgs(5);
1522+
let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
1523+
let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
1524+
let mut nodes = create_network(5, &node_cfgs, &node_chanmgrs);
1525+
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
1526+
create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
1527+
let chan_upd_2_3 = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0).0.contents;
1528+
let chan_upd_3_4 = create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 1_000_000, 0).0.contents;
1529+
1530+
// Get all our nodes onto the same height so payments don't fail for CLTV violations.
1531+
connect_blocks(&nodes[0], nodes[4].best_block_info().1 - nodes[0].best_block_info().1);
1532+
connect_blocks(&nodes[1], nodes[4].best_block_info().1 - nodes[1].best_block_info().1);
1533+
connect_blocks(&nodes[2], nodes[4].best_block_info().1 - nodes[2].best_block_info().1);
1534+
assert_eq!(nodes[4].best_block_info().1, nodes[3].best_block_info().1);
1535+
1536+
let amt_msat = 5000;
1537+
let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[4], Some(amt_msat), None);
1538+
1539+
let blinded_path = blinded_payment_path(payment_secret, 1, 1_0000_0000,
1540+
nodes.iter().skip(2).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_2_3, &chan_upd_3_4],
1541+
&chanmon_cfgs[4].keys_manager
1542+
);
1543+
1544+
assert!(is_padded(&blinded_path.blinded_hops(), PAYMENT_PADDING_ROUND_OFF));
1545+
1546+
let route_params = RouteParameters::from_payment_params_and_value(PaymentParameters::blinded(vec![blinded_path]), amt_msat);
1547+
1548+
nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
1549+
check_added_monitors(&nodes[0], 1);
1550+
pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2], &nodes[3], &nodes[4]]], amt_msat, payment_hash, payment_secret);
1551+
claim_payment(&nodes[0], &[&nodes[1], &nodes[2], &nodes[3], &nodes[4]], payment_preimage);
1552+
}
1553+
15171554
fn secret_from_hex(hex: &str) -> SecretKey {
15181555
SecretKey::from_slice(&<Vec<u8>>::from_hex(hex).unwrap()).unwrap()
15191556
}

lightning/src/onion_message/functional_tests.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
//! Onion message testing and test utilities live here.
1111
1212
use crate::blinded_path::EmptyNodeIdLookUp;
13-
use crate::blinded_path::message::{AsyncPaymentsContext, BlindedMessagePath, DNSResolverContext, MessageForwardNode, MessageContext, OffersContext};
13+
use crate::blinded_path::message::{AsyncPaymentsContext, BlindedMessagePath, DNSResolverContext, MessageContext, MessageForwardNode, OffersContext, MESSAGE_PADDING_ROUND_OFF};
14+
use crate::blinded_path::utils::is_padded;
1415
use crate::events::{Event, EventsProvider};
1516
use crate::types::features::{ChannelFeatures, InitFeatures};
1617
use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
@@ -562,6 +563,51 @@ fn too_big_packet_error() {
562563
assert_eq!(err, SendError::TooBigPacket);
563564
}
564565

566+
#[test]
567+
fn test_blinded_path_padding_for_full_length_path() {
568+
// Check that for a full blinded path, all encrypted payload are padded to rounded-off length.
569+
let nodes = create_nodes(4);
570+
let test_msg = TestCustomMessage::Pong;
571+
572+
let secp_ctx = Secp256k1::new();
573+
let intermediate_nodes = [
574+
MessageForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
575+
MessageForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
576+
];
577+
// Update the context to create a larger final receive TLVs, ensuring that
578+
// the hop sizes vary before padding.
579+
let context = MessageContext::Custom(vec![0u8; 42]);
580+
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[3].node_id, context, &*nodes[3].entropy_source, &secp_ctx).unwrap();
581+
582+
assert!(is_padded(&blinded_path.blinded_hops(), MESSAGE_PADDING_ROUND_OFF));
583+
584+
let destination = Destination::BlindedPath(blinded_path);
585+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
586+
587+
nodes[0].messenger.send_onion_message(test_msg, instructions).unwrap();
588+
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Pong);
589+
pass_along_path(&nodes);
590+
}
591+
592+
#[test]
593+
fn test_blinded_path_no_padding_for_compact_path() {
594+
// Check that for a compact blinded path, no padding is applied.
595+
let nodes = create_nodes(4);
596+
let secp_ctx = Secp256k1::new();
597+
598+
// Include some short_channel_id, so that MessageRouter uses this to create compact blinded paths.
599+
let intermediate_nodes = [
600+
MessageForwardNode { node_id: nodes[1].node_id, short_channel_id: Some(24) },
601+
MessageForwardNode { node_id: nodes[2].node_id, short_channel_id: Some(25) },
602+
];
603+
// Update the context to create a larger final receive TLVs, ensuring that
604+
// the hop sizes vary before padding.
605+
let context = MessageContext::Custom(vec![0u8; 42]);
606+
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[3].node_id, context, &*nodes[3].entropy_source, &secp_ctx).unwrap();
607+
608+
assert!(!is_padded(&blinded_path.blinded_hops(), MESSAGE_PADDING_ROUND_OFF));
609+
}
610+
565611
#[test]
566612
fn we_are_intro_node() {
567613
// If we are sending straight to a blinded path and we are the introduction node, we need to

0 commit comments

Comments
 (0)