Skip to content

Commit 9ce5b19

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 ad79527 commit 9ce5b19

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
@@ -545,24 +545,6 @@ pub trait Verification {
545545
) -> Result<(), ()>;
546546
}
547547

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

628574
impl PaymentId {
@@ -5322,10 +5268,7 @@ where
53225268
},
53235269
};
53245270

5325-
let entropy = &*self.entropy_source;
5326-
53275271
let enqueue_held_htlc_available_res = self.flow.enqueue_held_htlc_available(
5328-
entropy,
53295272
invoice,
53305273
payment_id,
53315274
self.get_peers_for_blinded_path(),
@@ -11317,7 +11260,7 @@ where
1131711260

1131811261
let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?;
1131911262

11320-
self.flow.enqueue_invoice(entropy, invoice.clone(), refund, self.get_peers_for_blinded_path())?;
11263+
self.flow.enqueue_invoice(invoice.clone(), refund, self.get_peers_for_blinded_path())?;
1132111264

1132211265
Ok(invoice)
1132311266
},
@@ -13324,8 +13267,6 @@ where
1332413267
fn handle_message(
1332513268
&self, message: OffersMessage, context: Option<OffersContext>, responder: Option<Responder>,
1332613269
) -> Option<(OffersMessage, ResponseInstruction)> {
13327-
let expanded_key = &self.inbound_payment_key;
13328-
1332913270
macro_rules! handle_pay_invoice_res {
1333013271
($res: expr, $invoice: expr, $logger: expr) => {{
1333113272
let error = match $res {
@@ -13441,38 +13382,26 @@ where
1344113382
#[cfg(async_payments)]
1344213383
OffersMessage::StaticInvoice(invoice) => {
1344313384
let payment_id = match context {
13444-
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
13445-
if payment_id.verify_for_offer_payment(hmac, nonce, expanded_key).is_err() {
13446-
return None
13447-
}
13448-
payment_id
13449-
},
13385+
Some(OffersContext::OutboundPayment { payment_id, .. }) => payment_id,
1345013386
_ => return None
1345113387
};
1345213388
let res = self.initiate_async_payment(&invoice, payment_id);
1345313389
handle_pay_invoice_res!(res, invoice, self.logger);
1345413390
},
1345513391
OffersMessage::InvoiceError(invoice_error) => {
1345613392
let payment_hash = match context {
13457-
Some(OffersContext::InboundPayment { payment_hash, nonce, hmac }) => {
13458-
match payment_hash.verify_for_offer_payment(hmac, nonce, expanded_key) {
13459-
Ok(_) => Some(payment_hash),
13460-
Err(_) => None,
13461-
}
13462-
},
13393+
Some(OffersContext::InboundPayment { payment_hash }) => Some(payment_hash),
1346313394
_ => None,
1346413395
};
1346513396

1346613397
let logger = WithContext::from(&self.logger, None, None, payment_hash);
1346713398
log_trace!(logger, "Received invoice_error: {}", invoice_error);
1346813399

1346913400
match context {
13470-
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
13471-
if let Ok(()) = payment_id.verify_for_offer_payment(hmac, nonce, expanded_key) {
13472-
self.abandon_payment_with_reason(
13473-
payment_id, PaymentFailureReason::InvoiceRequestRejected,
13474-
);
13475-
}
13401+
Some(OffersContext::OutboundPayment { payment_id, .. }) => {
13402+
self.abandon_payment_with_reason(
13403+
payment_id, PaymentFailureReason::InvoiceRequestRejected,
13404+
);
1347613405
},
1347713406
_ => {},
1347813407
}
@@ -13621,15 +13550,18 @@ where
1362113550
fn handle_release_held_htlc(&self, _message: ReleaseHeldHtlc, _context: AsyncPaymentsContext) {
1362213551
#[cfg(async_payments)]
1362313552
{
13624-
if let Ok(payment_id) = self.flow.verify_outbound_async_payment_context(_context) {
13625-
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
13626-
log_trace!(
13627-
self.logger,
13628-
"Failed to release held HTLC with payment id {}: {:?}",
13629-
payment_id,
13630-
e
13631-
);
13632-
}
13553+
let payment_id = match _context {
13554+
AsyncPaymentsContext::OutboundPayment { payment_id } => payment_id,
13555+
_ => return,
13556+
};
13557+
13558+
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
13559+
log_trace!(
13560+
self.logger,
13561+
"Failed to release held HTLC with payment id {}: {:?}",
13562+
payment_id,
13563+
e
13564+
);
1363313565
}
1363413566
}
1363513567
}

0 commit comments

Comments
 (0)