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