Skip to content

Commit 91d0e1b

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 3b0e9a2 commit 91d0e1b

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;
@@ -368,12 +366,6 @@ pub enum OffersContext {
368366
/// [`Refund`]: crate::offers::refund::Refund
369367
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
370368
nonce: Nonce,
371-
372-
/// Authentication code for the [`PaymentId`], which should be checked when the context is
373-
/// used with an [`InvoiceError`].
374-
///
375-
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
376-
hmac: Option<Hmac<Sha256>>,
377369
},
378370
/// Context used by a [`BlindedMessagePath`] as a reply path for a [`Bolt12Invoice`].
379371
///
@@ -386,19 +378,6 @@ pub enum OffersContext {
386378
///
387379
/// [`Bolt12Invoice::payment_hash`]: crate::offers::invoice::Bolt12Invoice::payment_hash
388380
payment_hash: PaymentHash,
389-
390-
/// A nonce used for authenticating that a received [`InvoiceError`] is for a valid
391-
/// sent [`Bolt12Invoice`].
392-
///
393-
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
394-
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
395-
nonce: Nonce,
396-
397-
/// Authentication code for the [`PaymentHash`], which should be checked when the context is
398-
/// used to log the received [`InvoiceError`].
399-
///
400-
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
401-
hmac: Hmac<Sha256>,
402381
},
403382
}
404383

@@ -453,35 +432,12 @@ pub enum AsyncPaymentsContext {
453432
///
454433
/// [`Offer`]: crate::offers::offer::Offer
455434
payment_id: PaymentId,
456-
/// A nonce used for authenticating that a [`ReleaseHeldHtlc`] message is valid for a preceding
457-
/// [`HeldHtlcAvailable`] message.
458-
///
459-
/// [`ReleaseHeldHtlc`]: crate::onion_message::async_payments::ReleaseHeldHtlc
460-
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
461-
nonce: Nonce,
462-
/// Authentication code for the [`PaymentId`].
463-
///
464-
/// Prevents the recipient from being able to deanonymize us by creating a blinded path to us
465-
/// containing the expected [`PaymentId`].
466-
hmac: Hmac<Sha256>,
467435
},
468436
/// Context contained within the [`BlindedMessagePath`]s we put in static invoices, provided back
469437
/// to us in corresponding [`HeldHtlcAvailable`] messages.
470438
///
471439
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
472440
InboundPayment {
473-
/// A nonce used for authenticating that a [`HeldHtlcAvailable`] message is valid for a
474-
/// preceding static invoice.
475-
///
476-
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
477-
nonce: Nonce,
478-
/// Authentication code for the [`HeldHtlcAvailable`] message.
479-
///
480-
/// Prevents nodes from creating their own blinded path to us, sending a [`HeldHtlcAvailable`]
481-
/// message and trivially getting notified whenever we come online.
482-
///
483-
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
484-
hmac: Hmac<Sha256>,
485441
/// The time as duration since the Unix epoch at which this path expires and messages sent over
486442
/// it should be ignored. Without this, anyone with the path corresponding to this context is
487443
/// able to trivially ask if we're online forever.
@@ -496,31 +452,39 @@ impl_writeable_tlv_based_enum!(MessageContext,
496452
{3, DNSResolver} => (),
497453
);
498454

455+
// NOTE:
456+
// Several TLV fields (`nonce`, `hmac`, etc.) were removed in LDK v0.1.X
457+
// following the introduction of `ReceiveAuthKey`-based authentication for
458+
// inbound `BlindedMessagePath`s. These fields are now commented out and
459+
// their `type` values must not be reused unless support for LDK v0.1.X
460+
// and earlier is fully dropped.
461+
//
462+
// For context-specific removals, see the commented-out fields within each enum variant.
499463
impl_writeable_tlv_based_enum!(OffersContext,
500464
(0, InvoiceRequest) => {
501465
(0, nonce, required),
502466
},
503467
(1, OutboundPayment) => {
504468
(0, payment_id, required),
505469
(1, nonce, required),
506-
(2, hmac, option),
470+
// Removed: (2, hmac, option)
507471
},
508472
(2, InboundPayment) => {
509473
(0, payment_hash, required),
510-
(1, nonce, required),
511-
(2, hmac, required)
474+
// Removed: (1, nonce, required),
475+
// Removed: (2, hmac, required)
512476
},
513477
);
514478

515479
impl_writeable_tlv_based_enum!(AsyncPaymentsContext,
516480
(0, OutboundPayment) => {
517481
(0, payment_id, required),
518-
(2, nonce, required),
519-
(4, hmac, required),
482+
// Removed: (2, nonce, required),
483+
// Removed: (4, hmac, required),
520484
},
521485
(1, InboundPayment) => {
522-
(0, nonce, required),
523-
(2, hmac, required),
486+
// Removed: (0, nonce, required),
487+
// Removed: (2, hmac, required),
524488
(4, path_absolute_expiry, required),
525489
},
526490
(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 {
@@ -5302,10 +5248,7 @@ where
53025248
},
53035249
};
53045250

5305-
let entropy = &*self.entropy_source;
5306-
53075251
let enqueue_held_htlc_available_res = self.flow.enqueue_held_htlc_available(
5308-
entropy,
53095252
invoice,
53105253
payment_id,
53115254
self.get_peers_for_blinded_path(),
@@ -11297,7 +11240,7 @@ where
1129711240

1129811241
let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?;
1129911242

11300-
self.flow.enqueue_invoice(entropy, invoice.clone(), refund, self.get_peers_for_blinded_path())?;
11243+
self.flow.enqueue_invoice(invoice.clone(), refund, self.get_peers_for_blinded_path())?;
1130111244

1130211245
Ok(invoice)
1130311246
},
@@ -13276,8 +13219,6 @@ where
1327613219
fn handle_message(
1327713220
&self, message: OffersMessage, context: Option<OffersContext>, responder: Option<Responder>,
1327813221
) -> Option<(OffersMessage, ResponseInstruction)> {
13279-
let expanded_key = &self.inbound_payment_key;
13280-
1328113222
macro_rules! handle_pay_invoice_res {
1328213223
($res: expr, $invoice: expr, $logger: expr) => {{
1328313224
let error = match $res {
@@ -13383,38 +13324,26 @@ where
1338313324
#[cfg(async_payments)]
1338413325
OffersMessage::StaticInvoice(invoice) => {
1338513326
let payment_id = match context {
13386-
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
13387-
if payment_id.verify_for_offer_payment(hmac, nonce, expanded_key).is_err() {
13388-
return None
13389-
}
13390-
payment_id
13391-
},
13327+
Some(OffersContext::OutboundPayment { payment_id, .. }) => payment_id,
1339213328
_ => return None
1339313329
};
1339413330
let res = self.initiate_async_payment(&invoice, payment_id);
1339513331
handle_pay_invoice_res!(res, invoice, self.logger);
1339613332
},
1339713333
OffersMessage::InvoiceError(invoice_error) => {
1339813334
let payment_hash = match context {
13399-
Some(OffersContext::InboundPayment { payment_hash, nonce, hmac }) => {
13400-
match payment_hash.verify_for_offer_payment(hmac, nonce, expanded_key) {
13401-
Ok(_) => Some(payment_hash),
13402-
Err(_) => None,
13403-
}
13404-
},
13335+
Some(OffersContext::InboundPayment { payment_hash }) => Some(payment_hash),
1340513336
_ => None,
1340613337
};
1340713338

1340813339
let logger = WithContext::from(&self.logger, None, None, payment_hash);
1340913340
log_trace!(logger, "Received invoice_error: {}", invoice_error);
1341013341

1341113342
match context {
13412-
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
13413-
if let Ok(()) = payment_id.verify_for_offer_payment(hmac, nonce, expanded_key) {
13414-
self.abandon_payment_with_reason(
13415-
payment_id, PaymentFailureReason::InvoiceRequestRejected,
13416-
);
13417-
}
13343+
Some(OffersContext::OutboundPayment { payment_id, .. }) => {
13344+
self.abandon_payment_with_reason(
13345+
payment_id, PaymentFailureReason::InvoiceRequestRejected,
13346+
);
1341813347
},
1341913348
_ => {},
1342013349
}
@@ -13525,15 +13454,18 @@ where
1352513454
fn handle_release_held_htlc(&self, _message: ReleaseHeldHtlc, _context: AsyncPaymentsContext) {
1352613455
#[cfg(async_payments)]
1352713456
{
13528-
if let Ok(payment_id) = self.flow.verify_outbound_async_payment_context(_context) {
13529-
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
13530-
log_trace!(
13531-
self.logger,
13532-
"Failed to release held HTLC with payment id {}: {:?}",
13533-
payment_id,
13534-
e
13535-
);
13536-
}
13457+
let payment_id = match _context {
13458+
AsyncPaymentsContext::OutboundPayment { payment_id } => payment_id,
13459+
_ => return,
13460+
};
13461+
13462+
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
13463+
log_trace!(
13464+
self.logger,
13465+
"Failed to release held HTLC with payment id {}: {:?}",
13466+
payment_id,
13467+
e
13468+
);
1353713469
}
1353813470
}
1353913471
}

0 commit comments

Comments
 (0)