@@ -38,19 +38,17 @@ use chain::keysinterface::{ChannelKeys, KeysInterface};
38
38
use util:: config:: UserConfig ;
39
39
use util:: { byte_utils, events} ;
40
40
use util:: ser:: { Readable , ReadableArgs , Writeable , Writer } ;
41
- use util:: chacha20:: ChaCha20 ;
41
+ use util:: chacha20:: { ChaCha20 , ChaChaReader } ;
42
42
use util:: logger:: Logger ;
43
43
use util:: errors:: APIError ;
44
44
45
45
use std:: { cmp, mem} ;
46
46
use std:: collections:: { HashMap , hash_map, HashSet } ;
47
- use std:: io:: Cursor ;
47
+ use std:: io:: { Cursor , Read } ;
48
48
use std:: sync:: { Arc , Mutex , MutexGuard , RwLock } ;
49
49
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
50
50
use std:: time:: Duration ;
51
51
52
- const SIXTY_FIVE_ZEROS : [ u8 ; 65 ] = [ 0 ; 65 ] ;
53
-
54
52
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
55
53
//
56
54
// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
@@ -883,20 +881,23 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
883
881
}
884
882
885
883
let mut chacha = ChaCha20 :: new ( & rho, & [ 0u8 ; 8 ] ) ;
884
+ let mut chacha_stream = ChaChaReader { chacha : & mut chacha, read : Cursor :: new ( & msg. onion_routing_packet . hop_data [ ..] ) } ;
886
885
let ( next_hop_data, next_hop_hmac) = {
887
- let mut decoded = [ 0 ; 65 ] ;
888
- chacha. process ( & msg. onion_routing_packet . hop_data [ 0 ..65 ] , & mut decoded) ;
889
- let mut hmac = [ 0 ; 32 ] ;
890
- hmac. copy_from_slice ( & decoded[ 33 ..] ) ;
891
- match msgs:: OnionHopData :: read ( & mut Cursor :: new ( & decoded[ ..33 ] ) ) {
886
+ match msgs:: OnionHopData :: read ( & mut chacha_stream) {
892
887
Err ( err) => {
893
888
let error_code = match err {
894
889
msgs:: DecodeError :: UnknownVersion => 0x4000 | 1 , // unknown realm byte
895
890
_ => 0x2000 | 2 , // Should never happen
896
891
} ;
897
892
return_err ! ( "Unable to decode our hop data" , error_code, & [ 0 ; 0 ] ) ;
898
893
} ,
899
- Ok ( msg) => ( msg, hmac)
894
+ Ok ( msg) => {
895
+ let mut hmac = [ 0 ; 32 ] ;
896
+ if let Err ( _) = chacha_stream. read_exact ( & mut hmac[ ..] ) {
897
+ return_err ! ( "Unable to decode our hop data" , 0x4000 | 1 , & [ 0 ; 0 ] ) ;
898
+ }
899
+ ( msg, hmac)
900
+ } ,
900
901
}
901
902
} ;
902
903
@@ -910,10 +911,11 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
910
911
// as-is (and were originally 0s).
911
912
// Of course reverse path calculation is still pretty easy given naive routing
912
913
// algorithms, but this fixes the most-obvious case.
913
- let mut new_packet_data = [ 0 ; 19 * 65 ] ;
914
- chacha. process ( & msg. onion_routing_packet . hop_data [ 65 ..] , & mut new_packet_data[ 0 ..19 * 65 ] ) ;
915
- assert_ne ! ( new_packet_data[ 0 ..65 ] , [ 0 ; 65 ] [ ..] ) ;
916
- assert_ne ! ( new_packet_data[ ..] , [ 0 ; 19 * 65 ] [ ..] ) ;
914
+ let mut next_bytes = [ 0 ; 32 ] ;
915
+ chacha_stream. read_exact ( & mut next_bytes) . unwrap ( ) ;
916
+ assert_ne ! ( next_bytes[ ..] , [ 0 ; 32 ] [ ..] ) ;
917
+ chacha_stream. read_exact ( & mut next_bytes) . unwrap ( ) ;
918
+ assert_ne ! ( next_bytes[ ..] , [ 0 ; 32 ] [ ..] ) ;
917
919
}
918
920
919
921
// OUR PAYMENT!
@@ -945,8 +947,8 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
945
947
} )
946
948
} else {
947
949
let mut new_packet_data = [ 0 ; 20 * 65 ] ;
948
- chacha . process ( & msg . onion_routing_packet . hop_data [ 65 .. ] , & mut new_packet_data[ 0 .. 19 * 65 ] ) ;
949
- chacha. process ( & SIXTY_FIVE_ZEROS [ .. ] , & mut new_packet_data[ 19 * 65 ..] ) ;
950
+ let read_pos = chacha_stream . read ( & mut new_packet_data) . unwrap ( ) ;
951
+ chacha_stream . chacha . process_inline ( & mut new_packet_data[ read_pos ..] ) ;
950
952
951
953
let mut new_pubkey = msg. onion_routing_packet . public_key . unwrap ( ) ;
952
954
0 commit comments