@@ -880,10 +880,7 @@ fn crypt_failure_packet(shared_secret: &[u8], packet: &mut OnionErrorPacket) {
880
880
chacha. process_in_place ( & mut packet. data ) ;
881
881
882
882
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) ;
887
884
}
888
885
}
889
886
@@ -945,10 +942,7 @@ fn update_attribution_data(
945
942
let attribution_data =
946
943
onion_error_packet. attribution_data . get_or_insert ( AttributionData :: new ( ) ) ;
947
944
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) ;
952
946
}
953
947
954
948
pub ( super ) fn build_failure_packet (
@@ -2657,6 +2651,14 @@ impl_writeable!(AttributionData, {
2657
2651
} ) ;
2658
2652
2659
2653
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
+
2660
2662
/// Adds the current node's HMACs for all possible positions to this packet.
2661
2663
pub ( crate ) fn add_hmacs ( & mut self , shared_secret : & [ u8 ] , message : & [ u8 ] ) {
2662
2664
let um: [ u8 ; 32 ] = gen_um_from_shared_secret ( & shared_secret) ;
@@ -2706,7 +2708,7 @@ impl AttributionData {
2706
2708
2707
2709
/// Verifies the attribution data of a failure packet for the given position in the path. If the HMAC checks out, the
2708
2710
/// 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 > {
2710
2712
// Calculate the expected HMAC.
2711
2713
let um = gen_um_from_shared_secret ( shared_secret) ;
2712
2714
let mut hmac = HmacEngine :: < Sha256 > :: new ( & um) ;
@@ -2791,6 +2793,12 @@ impl AttributionData {
2791
2793
fn get_hold_time_bytes ( & self , idx : usize ) -> & [ u8 ] {
2792
2794
& self . hold_times [ idx * HOLD_TIME_LEN ..( idx + 1 ) * HOLD_TIME_LEN ]
2793
2795
}
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
+ }
2794
2802
}
2795
2803
2796
2804
/// Updates the attribution data for an intermediate node.
0 commit comments