@@ -269,8 +269,9 @@ pub(super) fn verify<L: Deref>(payment_hash: PaymentHash, payment_data: &msgs::F
269
269
270
270
// Make sure to check the HMAC before doing the other checks below, to mitigate timing attacks.
271
271
let mut payment_preimage = None ;
272
+
272
273
match payment_type_res {
273
- Ok ( Method :: UserPaymentHash ) => {
274
+ Ok ( Method :: UserPaymentHash ) | Ok ( Method :: UserPaymentHashCustomFinalCltv ) => {
274
275
let mut hmac = HmacEngine :: < Sha256 > :: new ( & keys. user_pmt_hash_key ) ;
275
276
hmac. input ( & metadata_bytes[ ..] ) ;
276
277
hmac. input ( & payment_hash. 0 ) ;
@@ -279,7 +280,7 @@ pub(super) fn verify<L: Deref>(payment_hash: PaymentHash, payment_data: &msgs::F
279
280
return Err ( ( ) )
280
281
}
281
282
} ,
282
- Ok ( Method :: LdkPaymentHash ) => {
283
+ Ok ( Method :: LdkPaymentHash ) | Ok ( Method :: LdkPaymentHashCustomFinalCltv ) => {
283
284
match derive_ldk_payment_preimage ( payment_hash, & iv_bytes, & metadata_bytes, keys) {
284
285
Ok ( preimage) => payment_preimage = Some ( preimage) ,
285
286
Err ( bad_preimage_bytes) => {
@@ -288,40 +289,23 @@ pub(super) fn verify<L: Deref>(payment_hash: PaymentHash, payment_data: &msgs::F
288
289
}
289
290
}
290
291
} ,
291
- Ok ( Method :: UserPaymentHashCustomFinalCltv ) => {
292
- let mut hmac = HmacEngine :: < Sha256 > :: new ( & keys. user_pmt_hash_key ) ;
293
- hmac. input ( & metadata_bytes[ ..] ) ;
294
- hmac. input ( & payment_hash. 0 ) ;
295
- if !fixed_time_eq ( & iv_bytes, & Hmac :: from_engine ( hmac) . into_inner ( ) . split_at_mut ( IV_LEN ) . 0 ) {
296
- log_trace ! ( logger, "Failing HTLC with user-generated payment_hash {}: unexpected payment_secret" , log_bytes!( payment_hash. 0 ) ) ;
297
- return Err ( ( ) )
298
- }
292
+ Err ( unknown_bits) => {
293
+ log_trace ! ( logger, "Failing HTLC with payment hash {} due to unknown payment type {}" , log_bytes!( payment_hash. 0 ) , unknown_bits) ;
294
+ return Err ( ( ) ) ;
295
+ }
296
+ }
299
297
298
+ // Match again to check for custom `min_final_cltv_expiry_delta`.
299
+ match payment_type_res {
300
+ Ok ( Method :: UserPaymentHashCustomFinalCltv ) | Ok ( Method :: LdkPaymentHashCustomFinalCltv ) => {
300
301
min_final_cltv_expiry_delta = Some ( min_final_cltv_expiry_delta_from_metadata ( metadata_bytes) ) ;
301
302
// Zero out bits reserved for first 4 bits of `min_final_cltv_expiry_delta`, leaving one bit
302
303
// which is the most significant bit of `min_amt_msat`.
303
304
amt_msat_bytes[ 0 ] &= 0b0000_0001 ;
304
305
// Zero out bits reserved for the last 8 bits of `min_final_cltv_expiry_delta`.
305
306
expiry_bytes[ 0 ] &= 0 ;
306
- } ,
307
- Ok ( Method :: LdkPaymentHashCustomFinalCltv ) => {
308
- match derive_ldk_payment_preimage ( payment_hash, & iv_bytes, & metadata_bytes, keys) {
309
- Ok ( preimage) => payment_preimage = Some ( preimage) ,
310
- Err ( bad_preimage_bytes) => {
311
- log_trace ! ( logger, "Failing HTLC with payment_hash {} due to mismatching preimage {}" , log_bytes!( payment_hash. 0 ) , log_bytes!( bad_preimage_bytes) ) ;
312
- return Err ( ( ) )
313
- }
314
- }
315
- min_final_cltv_expiry_delta = Some ( min_final_cltv_expiry_delta_from_metadata ( metadata_bytes) ) ;
316
- // Zero out bits reserved for first 4 bits of `min_final_cltv_expiry_delta`.
317
- amt_msat_bytes[ 0 ] &= 0b1110_0001 ;
318
- // Zero out bits reserved for the last 8 bits of `min_final_cltv_expiry_delta`.
319
- expiry_bytes[ 0 ] &= 0 ;
320
- } ,
321
- Err ( unknown_bits) => {
322
- log_trace ! ( logger, "Failing HTLC with payment hash {} due to unknown payment type {}" , log_bytes!( payment_hash. 0 ) , unknown_bits) ;
323
- return Err ( ( ) ) ;
324
307
}
308
+ _ => { }
325
309
}
326
310
327
311
let min_amt_msat: u64 = u64:: from_be_bytes ( amt_msat_bytes. into ( ) ) ;
0 commit comments