Skip to content

Commit 41f6487

Browse files
committed
Parameterize failure packet length
Prepares for new test vectors that pad to 1024 bytes.
1 parent 385b87a commit 41f6487

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ use core::ops::Deref;
4141
#[allow(unused_imports)]
4242
use crate::prelude::*;
4343

44+
const DEFAULT_MIN_FAILURE_PACKET_LEN: usize = 256;
45+
4446
pub(crate) struct OnionKeys {
4547
#[cfg(test)]
4648
pub(crate) shared_secret: SharedSecret,
@@ -889,12 +891,15 @@ fn process_chacha(key: &[u8; 32], packet: &mut [u8]) {
889891
}
890892

891893
fn build_unencrypted_failure_packet(
892-
shared_secret: &[u8], failure_type: u16, failure_data: &[u8],
894+
shared_secret: &[u8], failure_type: u16, failure_data: &[u8], min_packet_len: usize,
893895
) -> OnionErrorPacket {
894896
assert_eq!(shared_secret.len(), 32);
895-
assert!(failure_data.len() <= 256 - 2);
896897

897-
let pad_len = 256 - 2 - failure_data.len();
898+
let pad_len = if failure_data.len() > min_packet_len - 2 {
899+
0
900+
} else {
901+
min_packet_len - 2 - failure_data.len()
902+
};
898903

899904
// Failure len is 2 bytes type plus the data.
900905
let failure_len = 2 + failure_data.len();
@@ -931,8 +936,12 @@ fn build_unencrypted_failure_packet(
931936
pub(super) fn build_failure_packet(
932937
shared_secret: &[u8], failure_type: u16, failure_data: &[u8],
933938
) -> OnionErrorPacket {
934-
let mut onion_error_packet =
935-
build_unencrypted_failure_packet(shared_secret, failure_type, failure_data);
939+
let mut onion_error_packet = build_unencrypted_failure_packet(
940+
shared_secret,
941+
failure_type,
942+
failure_data,
943+
DEFAULT_MIN_FAILURE_PACKET_LEN,
944+
);
936945

937946
crypt_failure_packet(shared_secret, &mut onion_error_packet);
938947

@@ -2453,6 +2462,7 @@ mod tests {
24532462
onion_keys[4].shared_secret.as_ref(),
24542463
0x2002,
24552464
&[0; 0],
2465+
DEFAULT_MIN_FAILURE_PACKET_LEN,
24562466
);
24572467
let hex = "4c2fc8bc08510334b6833ad9c3e79cd1b52ae59dfe5c2a4b23ead50f09f7ee0b0002200200fe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
24582468
assert_eq!(onion_error.data, <Vec<u8>>::from_hex(hex).unwrap());
@@ -2629,6 +2639,7 @@ mod tests {
26292639
outer_onion_keys[0].shared_secret.as_ref(),
26302640
error_code,
26312641
&[0; 0],
2642+
DEFAULT_MIN_FAILURE_PACKET_LEN,
26322643
);
26332644

26342645
crypt_failure_packet(
@@ -2648,6 +2659,7 @@ mod tests {
26482659
outer_onion_keys[1].shared_secret.as_ref(),
26492660
error_code,
26502661
&[0; 0],
2662+
DEFAULT_MIN_FAILURE_PACKET_LEN,
26512663
);
26522664

26532665
crypt_failure_packet(
@@ -2676,6 +2688,7 @@ mod tests {
26762688
trampoline_onion_keys[0].shared_secret.as_ref(),
26772689
error_code,
26782690
&[0; 0],
2691+
DEFAULT_MIN_FAILURE_PACKET_LEN,
26792692
);
26802693

26812694
crypt_failure_packet(
@@ -2709,6 +2722,7 @@ mod tests {
27092722
trampoline_onion_keys[1].shared_secret.as_ref(),
27102723
error_code,
27112724
&[0; 0],
2725+
DEFAULT_MIN_FAILURE_PACKET_LEN,
27122726
);
27132727

27142728
crypt_failure_packet(

0 commit comments

Comments
 (0)