Skip to content

Commit db180e2

Browse files
committed
Small refactors in onion_utils
1 parent c03c728 commit db180e2

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
@@ -880,10 +880,7 @@ fn crypt_failure_packet(shared_secret: &[u8], packet: &mut OnionErrorPacket) {
880880
chacha.process_in_place(&mut packet.data);
881881

882882
if let Some(ref mut attribution_data) = packet.attribution_data {
883-
let ammagext = gen_ammagext_from_shared_secret(&shared_secret);
884-
let mut chacha = ChaCha20::new(&ammagext, &[0u8; 8]);
885-
chacha.process_in_place(&mut attribution_data.hold_times);
886-
chacha.process_in_place(&mut attribution_data.hmacs);
883+
attribution_data.crypt(shared_secret);
887884
}
888885
}
889886

@@ -945,10 +942,7 @@ fn update_attribution_data(
945942
let attribution_data =
946943
onion_error_packet.attribution_data.get_or_insert(AttributionData::new());
947944

948-
let hold_time_bytes: [u8; 4] = hold_time.to_be_bytes();
949-
attribution_data.hold_times[..HOLD_TIME_LEN].copy_from_slice(&hold_time_bytes);
950-
951-
attribution_data.add_hmacs(shared_secret, &onion_error_packet.data);
945+
attribution_data.update(&onion_error_packet.data, shared_secret, hold_time);
952946
}
953947

954948
pub(super) fn build_failure_packet(
@@ -2657,6 +2651,14 @@ impl_writeable!(AttributionData, {
26572651
});
26582652

26592653
impl AttributionData {
2654+
/// Encrypts or decrypts the attribution data using the provided shared secret.
2655+
pub(crate) fn crypt(&mut self, shared_secret: &[u8]) {
2656+
let ammagext = gen_ammagext_from_shared_secret(&shared_secret);
2657+
let mut chacha = ChaCha20::new(&ammagext, &[0u8; 8]);
2658+
chacha.process_in_place(&mut self.hold_times);
2659+
chacha.process_in_place(&mut self.hmacs);
2660+
}
2661+
26602662
/// Adds the current node's HMACs for all possible positions to this packet.
26612663
pub(crate) fn add_hmacs(&mut self, shared_secret: &[u8], message: &[u8]) {
26622664
let um: [u8; 32] = gen_um_from_shared_secret(&shared_secret);
@@ -2706,7 +2708,7 @@ impl AttributionData {
27062708

27072709
/// Verifies the attribution data of a failure packet for the given position in the path. If the HMAC checks out, the
27082710
/// reported hold time is returned. If the HMAC does not match, None is returned.
2709-
fn verify(&self, message: &Vec<u8>, shared_secret: &[u8], position: usize) -> Option<u32> {
2711+
fn verify(&self, message: &[u8], shared_secret: &[u8], position: usize) -> Option<u32> {
27102712
// Calculate the expected HMAC.
27112713
let um = gen_um_from_shared_secret(shared_secret);
27122714
let mut hmac = HmacEngine::<Sha256>::new(&um);
@@ -2791,6 +2793,12 @@ impl AttributionData {
27912793
fn get_hold_time_bytes(&self, idx: usize) -> &[u8] {
27922794
&self.hold_times[idx * HOLD_TIME_LEN..(idx + 1) * HOLD_TIME_LEN]
27932795
}
2796+
2797+
fn update(&mut self, message: &[u8], shared_secret: &[u8], hold_time: u32) {
2798+
let hold_time_bytes: [u8; 4] = hold_time.to_be_bytes();
2799+
self.hold_times[..HOLD_TIME_LEN].copy_from_slice(&hold_time_bytes);
2800+
self.add_hmacs(shared_secret, message);
2801+
}
27942802
}
27952803

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

0 commit comments

Comments
 (0)