@@ -972,26 +972,26 @@ impl ChannelManager {
972
972
}
973
973
974
974
fn decode_update_add_htlc_onion ( & self , msg : & msgs:: UpdateAddHTLC ) -> ( PendingHTLCStatus , MutexGuard < ChannelHolder > ) {
975
- macro_rules! get_onion_hash {
976
- ( ) => {
975
+ macro_rules! return_malformed_err {
976
+ ( $msg : expr , $err_code : expr ) => {
977
977
{
978
+ log_info!( self , "Failed to accept/forward incoming HTLC: {}" , $msg) ;
979
+ let mut sha256_of_onion = [ 0 ; 32 ] ;
978
980
let mut sha = Sha256 :: new( ) ;
979
981
sha. input( & msg. onion_routing_packet. hop_data) ;
980
- let mut onion_hash = [ 0 ; 32 ] ;
981
- sha. result( & mut onion_hash) ;
982
- onion_hash
982
+ sha. result( & mut sha256_of_onion) ;
983
+ return ( PendingHTLCStatus :: Fail ( HTLCFailureMsg :: Malformed ( msgs:: UpdateFailMalformedHTLC {
984
+ channel_id: msg. channel_id,
985
+ htlc_id: msg. htlc_id,
986
+ sha256_of_onion,
987
+ failure_code: $err_code,
988
+ } ) ) , self . channel_state. lock( ) . unwrap( ) ) ;
983
989
}
984
990
}
985
991
}
986
992
987
993
if let Err ( _) = msg. onion_routing_packet . public_key {
988
- log_info ! ( self , "Failed to accept/forward incoming HTLC with invalid ephemeral pubkey" ) ;
989
- return ( PendingHTLCStatus :: Fail ( HTLCFailureMsg :: Malformed ( msgs:: UpdateFailMalformedHTLC {
990
- channel_id : msg. channel_id ,
991
- htlc_id : msg. htlc_id ,
992
- sha256_of_onion : get_onion_hash ! ( ) ,
993
- failure_code : 0x8000 | 0x4000 | 6 ,
994
- } ) ) , self . channel_state . lock ( ) . unwrap ( ) ) ;
994
+ return_malformed_err ! ( "invalid ephemeral pubkey" , 0x8000 | 0x4000 | 6 ) ;
995
995
}
996
996
997
997
let shared_secret = {
@@ -1001,6 +1001,23 @@ impl ChannelManager {
1001
1001
} ;
1002
1002
let ( rho, mu) = ChannelManager :: gen_rho_mu_from_shared_secret ( & shared_secret) ;
1003
1003
1004
+ if msg. onion_routing_packet . version != 0 {
1005
+ //TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
1006
+ //sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
1007
+ //the hash doesn't really serve any purpuse - in the case of hashing all data, the
1008
+ //receiving node would have to brute force to figure out which version was put in the
1009
+ //packet by the node that send us the message, in the case of hashing the hop_data, the
1010
+ //node knows the HMAC matched, so they already know what is there...
1011
+ return_malformed_err ! ( "Unknown onion packet version" , 0x8000 | 0x4000 | 4 ) ;
1012
+ }
1013
+
1014
+ let mut hmac = Hmac :: new ( Sha256 :: new ( ) , & mu) ;
1015
+ hmac. input ( & msg. onion_routing_packet . hop_data ) ;
1016
+ hmac. input ( & msg. payment_hash . 0 [ ..] ) ;
1017
+ if hmac. result ( ) != MacResult :: new ( & msg. onion_routing_packet . hmac ) {
1018
+ return_malformed_err ! ( "HMAC Check failed" , 0x8000 | 0x4000 | 5 ) ;
1019
+ }
1020
+
1004
1021
let mut channel_state = None ;
1005
1022
macro_rules! return_err {
1006
1023
( $msg: expr, $err_code: expr, $data: expr) => {
@@ -1018,23 +1035,6 @@ impl ChannelManager {
1018
1035
}
1019
1036
}
1020
1037
1021
- if msg. onion_routing_packet . version != 0 {
1022
- //TODO: Spec doesn't indicate if we should only hash hop_data here (and in other
1023
- //sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
1024
- //the hash doesn't really serve any purpuse - in the case of hashing all data, the
1025
- //receiving node would have to brute force to figure out which version was put in the
1026
- //packet by the node that send us the message, in the case of hashing the hop_data, the
1027
- //node knows the HMAC matched, so they already know what is there...
1028
- return_err ! ( "Unknown onion packet version" , 0x8000 | 0x4000 | 4 , & get_onion_hash!( ) ) ;
1029
- }
1030
-
1031
- let mut hmac = Hmac :: new ( Sha256 :: new ( ) , & mu) ;
1032
- hmac. input ( & msg. onion_routing_packet . hop_data ) ;
1033
- hmac. input ( & msg. payment_hash . 0 [ ..] ) ;
1034
- if hmac. result ( ) != MacResult :: new ( & msg. onion_routing_packet . hmac ) {
1035
- return_err ! ( "HMAC Check failed" , 0x8000 | 0x4000 | 5 , & get_onion_hash!( ) ) ;
1036
- }
1037
-
1038
1038
let mut chacha = ChaCha20 :: new ( & rho, & [ 0u8 ; 8 ] ) ;
1039
1039
let next_hop_data = {
1040
1040
let mut decoded = [ 0 ; 65 ] ;
@@ -1092,21 +1092,16 @@ impl ChannelManager {
1092
1092
sha. input ( & shared_secret) ;
1093
1093
let mut res = [ 0u8 ; 32 ] ;
1094
1094
sha. result ( & mut res) ;
1095
- match SecretKey :: from_slice ( & self . secp_ctx , & res) {
1096
- Err ( _) => {
1097
- return_err ! ( "Blinding factor is an invalid private key" , 0x8000 | 0x4000 | 6 , & get_onion_hash!( ) ) ;
1098
- } ,
1099
- Ok ( key) => key
1100
- }
1095
+ SecretKey :: from_slice ( & self . secp_ctx , & res) . expect ( "SHA-256 is broken?" )
1101
1096
} ;
1102
1097
1103
- if let Err ( _ ) = new_pubkey. mul_assign ( & self . secp_ctx , & blinding_factor) {
1104
- return_err ! ( "New blinding factor is an invalid private key" , 0x8000 | 0x4000 | 6 , & get_onion_hash! ( ) ) ;
1105
- }
1098
+ let public_key = if let Err ( e ) = new_pubkey. mul_assign ( & self . secp_ctx , & blinding_factor) {
1099
+ Err ( e )
1100
+ } else { Ok ( new_pubkey ) } ;
1106
1101
1107
1102
let outgoing_packet = msgs:: OnionPacket {
1108
1103
version : 0 ,
1109
- public_key : Ok ( new_pubkey ) ,
1104
+ public_key,
1110
1105
hop_data : new_packet_data,
1111
1106
hmac : next_hop_data. hmac . clone ( ) ,
1112
1107
} ;
0 commit comments