Skip to content

Commit fb5ece9

Browse files
committed
Small refactors in onion_utils
1 parent 4a6a2b6 commit fb5ece9

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -877,10 +877,7 @@ fn crypt_failure_packet(shared_secret: &[u8], packet: &mut OnionErrorPacket) {
877877
chacha.process_in_place(&mut packet.data);
878878

879879
if let Some(ref mut attribution_data) = packet.attribution_data {
880-
let ammagext = gen_ammagext_from_shared_secret(&shared_secret);
881-
let mut chacha = ChaCha20::new(&ammagext, &[0u8; 8]);
882-
chacha.process_in_place(&mut attribution_data.hold_times);
883-
chacha.process_in_place(&mut attribution_data.hmacs);
880+
attribution_data.crypt(shared_secret);
884881
}
885882
}
886883

@@ -942,10 +939,7 @@ fn update_attribution_data(
942939
let attribution_data =
943940
onion_error_packet.attribution_data.get_or_insert(AttributionData::new());
944941

945-
let hold_time_bytes: [u8; 4] = hold_time.to_be_bytes();
946-
attribution_data.hold_times[..HOLD_TIME_LEN].copy_from_slice(&hold_time_bytes);
947-
948-
attribution_data.add_hmacs(shared_secret, &onion_error_packet.data);
942+
attribution_data.update(&onion_error_packet.data, shared_secret, hold_time);
949943
}
950944

951945
pub(super) fn build_failure_packet(
@@ -2636,6 +2630,14 @@ impl_writeable!(AttributionData, {
26362630
});
26372631

26382632
impl AttributionData {
2633+
/// Encrypts or decrypts the attribution data using the provided shared secret.
2634+
pub(crate) fn crypt(&mut self, shared_secret: &[u8]) {
2635+
let ammagext = gen_ammagext_from_shared_secret(&shared_secret);
2636+
let mut chacha = ChaCha20::new(&ammagext, &[0u8; 8]);
2637+
chacha.process_in_place(&mut self.hold_times);
2638+
chacha.process_in_place(&mut self.hmacs);
2639+
}
2640+
26392641
/// Adds the current node's HMACs for all possible positions to this packet.
26402642
pub(crate) fn add_hmacs(&mut self, shared_secret: &[u8], message: &[u8]) {
26412643
let um: [u8; 32] = gen_um_from_shared_secret(&shared_secret);
@@ -2685,7 +2687,7 @@ impl AttributionData {
26852687

26862688
/// Verifies the attribution data of a failure packet for the given position in the path. If the HMAC checks out, the
26872689
/// reported hold time is returned. If the HMAC does not match, None is returned.
2688-
fn verify(&self, message: &Vec<u8>, shared_secret: &[u8], position: usize) -> Option<u32> {
2690+
fn verify(&self, message: &[u8], shared_secret: &[u8], position: usize) -> Option<u32> {
26892691
// Calculate the expected HMAC.
26902692
let um = gen_um_from_shared_secret(shared_secret);
26912693
let mut hmac = HmacEngine::<Sha256>::new(&um);
@@ -2770,6 +2772,12 @@ impl AttributionData {
27702772
fn get_hold_time_bytes(&self, idx: usize) -> &[u8] {
27712773
&self.hold_times[idx * HOLD_TIME_LEN..(idx + 1) * HOLD_TIME_LEN]
27722774
}
2775+
2776+
fn update(&mut self, message: &[u8], shared_secret: &[u8], hold_time: u32) {
2777+
let hold_time_bytes: [u8; 4] = hold_time.to_be_bytes();
2778+
self.hold_times[..HOLD_TIME_LEN].copy_from_slice(&hold_time_bytes);
2779+
self.add_hmacs(shared_secret, message);
2780+
}
27732781
}
27742782

27752783
/// Updates the attribution data for an intermediate node.

0 commit comments

Comments
 (0)