@@ -38,21 +38,19 @@ use chain::keysinterface::{ChannelKeys, KeysInterface, InMemoryChannelKeys};
3838use util:: config:: UserConfig ;
3939use util:: { byte_utils, events} ;
4040use util:: ser:: { Readable , ReadableArgs , Writeable , Writer } ;
41- use util:: chacha20:: ChaCha20 ;
41+ use util:: chacha20:: { ChaCha20 , ChaChaReader } ;
4242use util:: logger:: Logger ;
4343use util:: errors:: APIError ;
4444
4545use std:: { cmp, mem} ;
4646use std:: collections:: { HashMap , hash_map, HashSet } ;
47- use std:: io:: Cursor ;
47+ use std:: io:: { Cursor , Read } ;
4848use std:: sync:: { Arc , Mutex , MutexGuard , RwLock } ;
4949use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
5050use std:: time:: Duration ;
5151use std:: marker:: { Sync , Send } ;
5252use std:: ops:: Deref ;
5353
54- const SIXTY_FIVE_ZEROS : [ u8 ; 65 ] = [ 0 ; 65 ] ;
55-
5654// We hold various information about HTLC relay in the HTLC objects in Channel itself:
5755//
5856// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
@@ -906,20 +904,23 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
906904 }
907905
908906 let mut chacha = ChaCha20 :: new ( & rho, & [ 0u8 ; 8 ] ) ;
907+ let mut chacha_stream = ChaChaReader { chacha : & mut chacha, read : Cursor :: new ( & msg. onion_routing_packet . hop_data [ ..] ) } ;
909908 let ( next_hop_data, next_hop_hmac) = {
910- let mut decoded = [ 0 ; 65 ] ;
911- chacha. process ( & msg. onion_routing_packet . hop_data [ 0 ..65 ] , & mut decoded) ;
912- let mut hmac = [ 0 ; 32 ] ;
913- hmac. copy_from_slice ( & decoded[ 33 ..] ) ;
914- match msgs:: OnionHopData :: read ( & mut Cursor :: new ( & decoded[ ..33 ] ) ) {
909+ match msgs:: OnionHopData :: read ( & mut chacha_stream) {
915910 Err ( err) => {
916911 let error_code = match err {
917912 msgs:: DecodeError :: UnknownVersion => 0x4000 | 1 , // unknown realm byte
918913 _ => 0x2000 | 2 , // Should never happen
919914 } ;
920915 return_err ! ( "Unable to decode our hop data" , error_code, & [ 0 ; 0 ] ) ;
921916 } ,
922- Ok ( msg) => ( msg, hmac)
917+ Ok ( msg) => {
918+ let mut hmac = [ 0 ; 32 ] ;
919+ if let Err ( _) = chacha_stream. read_exact ( & mut hmac[ ..] ) {
920+ return_err ! ( "Unable to decode hop data" , 0x4000 | 1 , & [ 0 ; 0 ] ) ;
921+ }
922+ ( msg, hmac)
923+ } ,
923924 }
924925 } ;
925926
@@ -933,10 +934,11 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
933934 // as-is (and were originally 0s).
934935 // Of course reverse path calculation is still pretty easy given naive routing
935936 // algorithms, but this fixes the most-obvious case.
936- let mut new_packet_data = [ 0 ; 19 * 65 ] ;
937- chacha. process ( & msg. onion_routing_packet . hop_data [ 65 ..] , & mut new_packet_data[ 0 ..19 * 65 ] ) ;
938- assert_ne ! ( new_packet_data[ 0 ..65 ] , [ 0 ; 65 ] [ ..] ) ;
939- assert_ne ! ( new_packet_data[ ..] , [ 0 ; 19 * 65 ] [ ..] ) ;
937+ let mut next_bytes = [ 0 ; 32 ] ;
938+ chacha_stream. read_exact ( & mut next_bytes) . unwrap ( ) ;
939+ assert_ne ! ( next_bytes[ ..] , [ 0 ; 32 ] [ ..] ) ;
940+ chacha_stream. read_exact ( & mut next_bytes) . unwrap ( ) ;
941+ assert_ne ! ( next_bytes[ ..] , [ 0 ; 32 ] [ ..] ) ;
940942 }
941943
942944 // OUR PAYMENT!
@@ -968,8 +970,10 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
968970 } )
969971 } else {
970972 let mut new_packet_data = [ 0 ; 20 * 65 ] ;
971- chacha. process ( & msg. onion_routing_packet . hop_data [ 65 ..] , & mut new_packet_data[ 0 ..19 * 65 ] ) ;
972- chacha. process ( & SIXTY_FIVE_ZEROS [ ..] , & mut new_packet_data[ 19 * 65 ..] ) ;
973+ let read_pos = chacha_stream. read ( & mut new_packet_data) . unwrap ( ) ;
974+ // Once we've emptied the set of bytes our peer gave us, encrypt 0 bytes until we
975+ // fill the onion hop data we'll forward to our next-hop peer.
976+ chacha_stream. chacha . process_inline ( & mut new_packet_data[ read_pos..] ) ;
973977
974978 let mut new_pubkey = msg. onion_routing_packet . public_key . unwrap ( ) ;
975979
0 commit comments