@@ -877,10 +877,7 @@ fn crypt_failure_packet(shared_secret: &[u8], packet: &mut OnionErrorPacket) {
877
877
chacha. process_in_place ( & mut packet. data ) ;
878
878
879
879
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) ;
884
881
}
885
882
}
886
883
@@ -942,10 +939,7 @@ fn update_attribution_data(
942
939
let attribution_data =
943
940
onion_error_packet. attribution_data . get_or_insert ( AttributionData :: new ( ) ) ;
944
941
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) ;
949
943
}
950
944
951
945
pub ( super ) fn build_failure_packet (
@@ -2636,6 +2630,14 @@ impl_writeable!(AttributionData, {
2636
2630
} ) ;
2637
2631
2638
2632
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
+
2639
2641
/// Adds the current node's HMACs for all possible positions to this packet.
2640
2642
pub ( crate ) fn add_hmacs ( & mut self , shared_secret : & [ u8 ] , message : & [ u8 ] ) {
2641
2643
let um: [ u8 ; 32 ] = gen_um_from_shared_secret ( & shared_secret) ;
@@ -2685,7 +2687,7 @@ impl AttributionData {
2685
2687
2686
2688
/// Verifies the attribution data of a failure packet for the given position in the path. If the HMAC checks out, the
2687
2689
/// 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 > {
2689
2691
// Calculate the expected HMAC.
2690
2692
let um = gen_um_from_shared_secret ( shared_secret) ;
2691
2693
let mut hmac = HmacEngine :: < Sha256 > :: new ( & um) ;
@@ -2770,6 +2772,12 @@ impl AttributionData {
2770
2772
fn get_hold_time_bytes ( & self , idx : usize ) -> & [ u8 ] {
2771
2773
& self . hold_times [ idx * HOLD_TIME_LEN ..( idx + 1 ) * HOLD_TIME_LEN ]
2772
2774
}
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
+ }
2773
2781
}
2774
2782
2775
2783
/// Updates the attribution data for an intermediate node.
0 commit comments