Skip to content

Commit f307ab7

Browse files
committed
Remove error side effect from decrypt_onion_error_packet
This function still decrypted the message in-place even when it returned an error. This is confusing for the caller.
1 parent 447148d commit f307ab7

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -931,15 +931,11 @@ pub(crate) struct DecodedOnionFailure {
931931
pub(crate) onion_error_data: Option<Vec<u8>>,
932932
}
933933

934-
/// Note that we always decrypt `packet` in-place here even if the deserialization into
935-
/// [`msgs::DecodedOnionErrorPacket`] ultimately fails.
936-
fn decrypt_onion_error_packet(
937-
packet: &mut Vec<u8>, shared_secret: SharedSecret,
938-
) -> Result<msgs::DecodedOnionErrorPacket, msgs::DecodeError> {
934+
/// Decrypt the error packet in-place.
935+
fn decrypt_onion_error_packet(packet: &mut Vec<u8>, shared_secret: SharedSecret) {
939936
let ammag = gen_ammag_from_shared_secret(shared_secret.as_ref());
940937
let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
941938
chacha.process_in_place(packet);
942-
msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(packet))
943939
}
944940

945941
/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
@@ -1021,9 +1017,11 @@ where
10211017
{
10221018
// Actually parse the onion error data in tests so we can check that blinded hops fail
10231019
// back correctly.
1024-
let err_packet =
1025-
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret)
1026-
.unwrap();
1020+
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
1021+
let err_packet = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(
1022+
&encrypted_packet,
1023+
))
1024+
.unwrap();
10271025
error_code_ret = Some(u16::from_be_bytes(
10281026
err_packet.failuremsg.get(0..2).unwrap().try_into().unwrap(),
10291027
));
@@ -1044,10 +1042,12 @@ where
10441042
let amt_to_forward = htlc_msat - route_hop.fee_msat;
10451043
htlc_msat = amt_to_forward;
10461044

1047-
let err_packet = match decrypt_onion_error_packet(&mut encrypted_packet, shared_secret) {
1048-
Ok(p) => p,
1049-
Err(_) => return,
1050-
};
1045+
decrypt_onion_error_packet(&mut encrypted_packet, shared_secret);
1046+
let err_packet =
1047+
match msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&encrypted_packet)) {
1048+
Ok(p) => p,
1049+
Err(_) => return,
1050+
};
10511051
let um = gen_um_from_shared_secret(shared_secret.as_ref());
10521052
let mut hmac = HmacEngine::<Sha256>::new(&um);
10531053
hmac.input(&err_packet.encode()[32..]);

0 commit comments

Comments
 (0)