@@ -38,21 +38,19 @@ use chain::keysinterface::{ChannelKeys, KeysInterface, InMemoryChannelKeys};
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
use std:: marker:: { Sync , Send } ;
52
52
use std:: ops:: Deref ;
53
53
54
- const SIXTY_FIVE_ZEROS : [ u8 ; 65 ] = [ 0 ; 65 ] ;
55
-
56
54
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
57
55
//
58
56
// 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
906
904
}
907
905
908
906
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 [ ..] ) } ;
909
908
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) {
915
910
Err ( err) => {
916
911
let error_code = match err {
917
912
msgs:: DecodeError :: UnknownVersion => 0x4000 | 1 , // unknown realm byte
918
913
_ => 0x2000 | 2 , // Should never happen
919
914
} ;
920
915
return_err ! ( "Unable to decode our hop data" , error_code, & [ 0 ; 0 ] ) ;
921
916
} ,
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
+ } ,
923
924
}
924
925
} ;
925
926
@@ -933,10 +934,11 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
933
934
// as-is (and were originally 0s).
934
935
// Of course reverse path calculation is still pretty easy given naive routing
935
936
// 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 ] [ ..] ) ;
940
942
}
941
943
942
944
// OUR PAYMENT!
@@ -968,8 +970,10 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
968
970
} )
969
971
} else {
970
972
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..] ) ;
973
977
974
978
let mut new_pubkey = msg. onion_routing_packet . public_key . unwrap ( ) ;
975
979
0 commit comments