@@ -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
@@ -922,20 +920,23 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
922
920
}
923
921
924
922
let mut chacha = ChaCha20 :: new ( & rho, & [ 0u8 ; 8 ] ) ;
923
+ let mut chacha_stream = ChaChaReader { chacha : & mut chacha, read : Cursor :: new ( & msg. onion_routing_packet . hop_data [ ..] ) } ;
925
924
let ( next_hop_data, next_hop_hmac) = {
926
- let mut decoded = [ 0 ; 65 ] ;
927
- chacha. process ( & msg. onion_routing_packet . hop_data [ 0 ..65 ] , & mut decoded) ;
928
- let mut hmac = [ 0 ; 32 ] ;
929
- hmac. copy_from_slice ( & decoded[ 33 ..] ) ;
930
- match msgs:: OnionHopData :: read ( & mut Cursor :: new ( & decoded[ ..33 ] ) ) {
925
+ match msgs:: OnionHopData :: read ( & mut chacha_stream) {
931
926
Err ( err) => {
932
927
let error_code = match err {
933
928
msgs:: DecodeError :: UnknownVersion => 0x4000 | 1 , // unknown realm byte
934
929
_ => 0x2000 | 2 , // Should never happen
935
930
} ;
936
931
return_err ! ( "Unable to decode our hop data" , error_code, & [ 0 ; 0 ] ) ;
937
932
} ,
938
- Ok ( msg) => ( msg, hmac)
933
+ Ok ( msg) => {
934
+ let mut hmac = [ 0 ; 32 ] ;
935
+ if let Err ( _) = chacha_stream. read_exact ( & mut hmac[ ..] ) {
936
+ return_err ! ( "Unable to decode our hop data" , 0x4000 | 1 , & [ 0 ; 0 ] ) ;
937
+ }
938
+ ( msg, hmac)
939
+ } ,
939
940
}
940
941
} ;
941
942
@@ -949,10 +950,11 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
949
950
// as-is (and were originally 0s).
950
951
// Of course reverse path calculation is still pretty easy given naive routing
951
952
// algorithms, but this fixes the most-obvious case.
952
- let mut new_packet_data = [ 0 ; 19 * 65 ] ;
953
- chacha. process ( & msg. onion_routing_packet . hop_data [ 65 ..] , & mut new_packet_data[ 0 ..19 * 65 ] ) ;
954
- assert_ne ! ( new_packet_data[ 0 ..65 ] , [ 0 ; 65 ] [ ..] ) ;
955
- assert_ne ! ( new_packet_data[ ..] , [ 0 ; 19 * 65 ] [ ..] ) ;
953
+ let mut next_bytes = [ 0 ; 32 ] ;
954
+ chacha_stream. read_exact ( & mut next_bytes) . unwrap ( ) ;
955
+ assert_ne ! ( next_bytes[ ..] , [ 0 ; 32 ] [ ..] ) ;
956
+ chacha_stream. read_exact ( & mut next_bytes) . unwrap ( ) ;
957
+ assert_ne ! ( next_bytes[ ..] , [ 0 ; 32 ] [ ..] ) ;
956
958
}
957
959
958
960
// OUR PAYMENT!
@@ -984,8 +986,8 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
984
986
} )
985
987
} else {
986
988
let mut new_packet_data = [ 0 ; 20 * 65 ] ;
987
- chacha . process ( & msg . onion_routing_packet . hop_data [ 65 .. ] , & mut new_packet_data[ 0 .. 19 * 65 ] ) ;
988
- chacha. process ( & SIXTY_FIVE_ZEROS [ .. ] , & mut new_packet_data[ 19 * 65 ..] ) ;
989
+ let read_pos = chacha_stream . read ( & mut new_packet_data) . unwrap ( ) ;
990
+ chacha_stream . chacha . process_inline ( & mut new_packet_data[ read_pos ..] ) ;
989
991
990
992
let mut new_pubkey = msg. onion_routing_packet . public_key . unwrap ( ) ;
991
993
0 commit comments