Skip to content

Commit e06727b

Browse files
committed
Cleanup: Remove redundant (hmac, nonce) from codebase
Now that we have introduced an alternate mechanism for authentication in the codebase, we can safely remove the now redundant (hmac, nonce) fields from the MessageContext's while maintaining the security of the onion messages.
1 parent ae19424 commit e06727b

File tree

4 files changed

+64
-330
lines changed

4 files changed

+64
-330
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ use crate::sign::{EntropySource, NodeSigner, ReceiveAuthKey, Recipient};
3030
use crate::types::payment::PaymentHash;
3131
use crate::util::scid_utils;
3232
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, Writeable, Writer};
33-
use bitcoin::hashes::hmac::Hmac;
34-
use bitcoin::hashes::sha256::Hash as Sha256;
3533

3634
use core::mem;
3735
use core::ops::Deref;
@@ -406,12 +404,6 @@ pub enum OffersContext {
406404
/// [`Refund`]: crate::offers::refund::Refund
407405
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
408406
nonce: Nonce,
409-
410-
/// Authentication code for the [`PaymentId`], which should be checked when the context is
411-
/// used with an [`InvoiceError`].
412-
///
413-
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
414-
hmac: Option<Hmac<Sha256>>,
415407
},
416408
/// Context used by a [`BlindedMessagePath`] as a reply path for a [`Bolt12Invoice`].
417409
///
@@ -424,19 +416,6 @@ pub enum OffersContext {
424416
///
425417
/// [`Bolt12Invoice::payment_hash`]: crate::offers::invoice::Bolt12Invoice::payment_hash
426418
payment_hash: PaymentHash,
427-
428-
/// A nonce used for authenticating that a received [`InvoiceError`] is for a valid
429-
/// sent [`Bolt12Invoice`].
430-
///
431-
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
432-
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
433-
nonce: Nonce,
434-
435-
/// Authentication code for the [`PaymentHash`], which should be checked when the context is
436-
/// used to log the received [`InvoiceError`].
437-
///
438-
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
439-
hmac: Hmac<Sha256>,
440419
},
441420
}
442421

@@ -544,35 +523,12 @@ pub enum AsyncPaymentsContext {
544523
///
545524
/// [`Offer`]: crate::offers::offer::Offer
546525
payment_id: PaymentId,
547-
/// A nonce used for authenticating that a [`ReleaseHeldHtlc`] message is valid for a preceding
548-
/// [`HeldHtlcAvailable`] message.
549-
///
550-
/// [`ReleaseHeldHtlc`]: crate::onion_message::async_payments::ReleaseHeldHtlc
551-
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
552-
nonce: Nonce,
553-
/// Authentication code for the [`PaymentId`].
554-
///
555-
/// Prevents the recipient from being able to deanonymize us by creating a blinded path to us
556-
/// containing the expected [`PaymentId`].
557-
hmac: Hmac<Sha256>,
558526
},
559527
/// Context contained within the [`BlindedMessagePath`]s we put in static invoices, provided back
560528
/// to us in corresponding [`HeldHtlcAvailable`] messages.
561529
///
562530
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
563531
InboundPayment {
564-
/// A nonce used for authenticating that a [`HeldHtlcAvailable`] message is valid for a
565-
/// preceding static invoice.
566-
///
567-
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
568-
nonce: Nonce,
569-
/// Authentication code for the [`HeldHtlcAvailable`] message.
570-
///
571-
/// Prevents nodes from creating their own blinded path to us, sending a [`HeldHtlcAvailable`]
572-
/// message and trivially getting notified whenever we come online.
573-
///
574-
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
575-
hmac: Hmac<Sha256>,
576532
/// The time as duration since the Unix epoch at which this path expires and messages sent over
577533
/// it should be ignored. Without this, anyone with the path corresponding to this context is
578534
/// able to trivially ask if we're online forever.
@@ -587,19 +543,27 @@ impl_writeable_tlv_based_enum!(MessageContext,
587543
{3, DNSResolver} => (),
588544
);
589545

546+
// NOTE:
547+
// Several TLV fields (`nonce`, `hmac`, etc.) were removed in LDK v0.1.X
548+
// following the introduction of `ReceiveAuthKey`-based authentication for
549+
// inbound `BlindedMessagePath`s. These fields are now commented out and
550+
// their `type` values must not be reused unless support for LDK v0.1.X
551+
// and earlier is fully dropped.
552+
//
553+
// For context-specific removals, see the commented-out fields within each enum variant.
590554
impl_writeable_tlv_based_enum!(OffersContext,
591555
(0, InvoiceRequest) => {
592556
(0, nonce, required),
593557
},
594558
(1, OutboundPayment) => {
595559
(0, payment_id, required),
596560
(1, nonce, required),
597-
(2, hmac, option),
561+
// Removed: (2, hmac, option)
598562
},
599563
(2, InboundPayment) => {
600564
(0, payment_hash, required),
601-
(1, nonce, required),
602-
(2, hmac, required)
565+
// Removed: (1, nonce, required),
566+
// Removed: (2, hmac, required)
603567
},
604568
(3, StaticInvoiceRequested) => {
605569
(0, recipient_id, required),
@@ -611,12 +575,12 @@ impl_writeable_tlv_based_enum!(OffersContext,
611575
impl_writeable_tlv_based_enum!(AsyncPaymentsContext,
612576
(0, OutboundPayment) => {
613577
(0, payment_id, required),
614-
(2, nonce, required),
615-
(4, hmac, required),
578+
// Removed: (2, nonce, required),
579+
// Removed: (4, hmac, required),
616580
},
617581
(1, InboundPayment) => {
618-
(0, nonce, required),
619-
(2, hmac, required),
582+
// Removed: (0, nonce, required),
583+
// Removed: (2, hmac, required),
620584
(4, path_absolute_expiry, required),
621585
},
622586
(2, OfferPaths) => {

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -547,24 +547,6 @@ pub trait Verification {
547547
) -> Result<(), ()>;
548548
}
549549

550-
impl Verification for PaymentHash {
551-
/// Constructs an HMAC to include in [`OffersContext::InboundPayment`] for the payment hash
552-
/// along with the given [`Nonce`].
553-
fn hmac_for_offer_payment(
554-
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
555-
) -> Hmac<Sha256> {
556-
signer::hmac_for_payment_hash(*self, nonce, expanded_key)
557-
}
558-
559-
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
560-
/// [`OffersContext::InboundPayment`].
561-
fn verify_for_offer_payment(
562-
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
563-
) -> Result<(), ()> {
564-
signer::verify_payment_hash(*self, hmac, nonce, expanded_key)
565-
}
566-
}
567-
568550
impl Verification for UnauthenticatedReceiveTlvs {
569551
fn hmac_for_offer_payment(
570552
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
@@ -589,42 +571,6 @@ pub struct PaymentId(pub [u8; Self::LENGTH]);
589571
impl PaymentId {
590572
/// Number of bytes in the id.
591573
pub const LENGTH: usize = 32;
592-
593-
/// Constructs an HMAC to include in [`AsyncPaymentsContext::OutboundPayment`] for the payment id
594-
/// along with the given [`Nonce`].
595-
#[cfg(async_payments)]
596-
pub fn hmac_for_async_payment(
597-
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
598-
) -> Hmac<Sha256> {
599-
signer::hmac_for_async_payment_id(*self, nonce, expanded_key)
600-
}
601-
602-
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
603-
/// [`AsyncPaymentsContext::OutboundPayment`].
604-
#[cfg(async_payments)]
605-
pub fn verify_for_async_payment(
606-
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
607-
) -> Result<(), ()> {
608-
signer::verify_async_payment_id(*self, hmac, nonce, expanded_key)
609-
}
610-
}
611-
612-
impl Verification for PaymentId {
613-
/// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
614-
/// along with the given [`Nonce`].
615-
fn hmac_for_offer_payment(
616-
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
617-
) -> Hmac<Sha256> {
618-
signer::hmac_for_offer_payment_id(*self, nonce, expanded_key)
619-
}
620-
621-
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
622-
/// [`OffersContext::OutboundPayment`].
623-
fn verify_for_offer_payment(
624-
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
625-
) -> Result<(), ()> {
626-
signer::verify_offer_payment_id(*self, hmac, nonce, expanded_key)
627-
}
628574
}
629575

630576
impl PaymentId {
@@ -5291,10 +5237,7 @@ where
52915237
},
52925238
};
52935239

5294-
let entropy = &*self.entropy_source;
5295-
52965240
let enqueue_held_htlc_available_res = self.flow.enqueue_held_htlc_available(
5297-
entropy,
52985241
invoice,
52995242
payment_id,
53005243
self.get_peers_for_blinded_path(),
@@ -11824,7 +11767,7 @@ where
1182411767

1182511768
let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?;
1182611769

11827-
self.flow.enqueue_invoice(entropy, invoice.clone(), refund, self.get_peers_for_blinded_path())?;
11770+
self.flow.enqueue_invoice(invoice.clone(), refund, self.get_peers_for_blinded_path())?;
1182811771

1182911772
Ok(invoice)
1183011773
},
@@ -13825,8 +13768,6 @@ where
1382513768
fn handle_message(
1382613769
&self, message: OffersMessage, context: Option<OffersContext>, responder: Option<Responder>,
1382713770
) -> Option<(OffersMessage, ResponseInstruction)> {
13828-
let expanded_key = &self.inbound_payment_key;
13829-
1383013771
macro_rules! handle_pay_invoice_res {
1383113772
($res: expr, $invoice: expr, $logger: expr) => {{
1383213773
let error = match $res {
@@ -13942,38 +13883,26 @@ where
1394213883
#[cfg(async_payments)]
1394313884
OffersMessage::StaticInvoice(invoice) => {
1394413885
let payment_id = match context {
13945-
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
13946-
if payment_id.verify_for_offer_payment(hmac, nonce, expanded_key).is_err() {
13947-
return None
13948-
}
13949-
payment_id
13950-
},
13886+
Some(OffersContext::OutboundPayment { payment_id, .. }) => payment_id,
1395113887
_ => return None
1395213888
};
1395313889
let res = self.initiate_async_payment(&invoice, payment_id);
1395413890
handle_pay_invoice_res!(res, invoice, self.logger);
1395513891
},
1395613892
OffersMessage::InvoiceError(invoice_error) => {
1395713893
let payment_hash = match context {
13958-
Some(OffersContext::InboundPayment { payment_hash, nonce, hmac }) => {
13959-
match payment_hash.verify_for_offer_payment(hmac, nonce, expanded_key) {
13960-
Ok(_) => Some(payment_hash),
13961-
Err(_) => None,
13962-
}
13963-
},
13894+
Some(OffersContext::InboundPayment { payment_hash }) => Some(payment_hash),
1396413895
_ => None,
1396513896
};
1396613897

1396713898
let logger = WithContext::from(&self.logger, None, None, payment_hash);
1396813899
log_trace!(logger, "Received invoice_error: {}", invoice_error);
1396913900

1397013901
match context {
13971-
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
13972-
if let Ok(()) = payment_id.verify_for_offer_payment(hmac, nonce, expanded_key) {
13973-
self.abandon_payment_with_reason(
13974-
payment_id, PaymentFailureReason::InvoiceRequestRejected,
13975-
);
13976-
}
13902+
Some(OffersContext::OutboundPayment { payment_id, .. }) => {
13903+
self.abandon_payment_with_reason(
13904+
payment_id, PaymentFailureReason::InvoiceRequestRejected,
13905+
);
1397713906
},
1397813907
_ => {},
1397913908
}
@@ -14122,15 +14051,18 @@ where
1412214051
fn handle_release_held_htlc(&self, _message: ReleaseHeldHtlc, _context: AsyncPaymentsContext) {
1412314052
#[cfg(async_payments)]
1412414053
{
14125-
if let Ok(payment_id) = self.flow.verify_outbound_async_payment_context(_context) {
14126-
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
14127-
log_trace!(
14128-
self.logger,
14129-
"Failed to release held HTLC with payment id {}: {:?}",
14130-
payment_id,
14131-
e
14132-
);
14133-
}
14054+
let payment_id = match _context {
14055+
AsyncPaymentsContext::OutboundPayment { payment_id } => payment_id,
14056+
_ => return,
14057+
};
14058+
14059+
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
14060+
log_trace!(
14061+
self.logger,
14062+
"Failed to release held HTLC with payment id {}: {:?}",
14063+
payment_id,
14064+
e
14065+
);
1413414066
}
1413514067
}
1413614068
}

0 commit comments

Comments
 (0)