Skip to content

Commit 4f6f9ad

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 8f7c3b3 commit 4f6f9ad

File tree

4 files changed

+34
-331
lines changed

4 files changed

+34
-331
lines changed

lightning/src/blinded_path/message.rs

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

3533
use core::mem;
3634
use core::ops::Deref;
@@ -367,12 +365,6 @@ pub enum OffersContext {
367365
/// [`Refund`]: crate::offers::refund::Refund
368366
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
369367
nonce: Nonce,
370-
371-
/// Authentication code for the [`PaymentId`], which should be checked when the context is
372-
/// used with an [`InvoiceError`].
373-
///
374-
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
375-
hmac: Option<Hmac<Sha256>>,
376368
},
377369
/// Context used by a [`BlindedMessagePath`] as a reply path for a [`Bolt12Invoice`].
378370
///
@@ -385,19 +377,6 @@ pub enum OffersContext {
385377
///
386378
/// [`Bolt12Invoice::payment_hash`]: crate::offers::invoice::Bolt12Invoice::payment_hash
387379
payment_hash: PaymentHash,
388-
389-
/// A nonce used for authenticating that a received [`InvoiceError`] is for a valid
390-
/// sent [`Bolt12Invoice`].
391-
///
392-
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
393-
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
394-
nonce: Nonce,
395-
396-
/// Authentication code for the [`PaymentHash`], which should be checked when the context is
397-
/// used to log the received [`InvoiceError`].
398-
///
399-
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
400-
hmac: Hmac<Sha256>,
401380
},
402381
}
403382

@@ -418,35 +397,12 @@ pub enum AsyncPaymentsContext {
418397
///
419398
/// [`Offer`]: crate::offers::offer::Offer
420399
payment_id: PaymentId,
421-
/// A nonce used for authenticating that a [`ReleaseHeldHtlc`] message is valid for a preceding
422-
/// [`HeldHtlcAvailable`] message.
423-
///
424-
/// [`ReleaseHeldHtlc`]: crate::onion_message::async_payments::ReleaseHeldHtlc
425-
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
426-
nonce: Nonce,
427-
/// Authentication code for the [`PaymentId`].
428-
///
429-
/// Prevents the recipient from being able to deanonymize us by creating a blinded path to us
430-
/// containing the expected [`PaymentId`].
431-
hmac: Hmac<Sha256>,
432400
},
433401
/// Context contained within the [`BlindedMessagePath`]s we put in static invoices, provided back
434402
/// to us in corresponding [`HeldHtlcAvailable`] messages.
435403
///
436404
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
437405
InboundPayment {
438-
/// A nonce used for authenticating that a [`HeldHtlcAvailable`] message is valid for a
439-
/// preceding static invoice.
440-
///
441-
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
442-
nonce: Nonce,
443-
/// Authentication code for the [`HeldHtlcAvailable`] message.
444-
///
445-
/// Prevents nodes from creating their own blinded path to us, sending a [`HeldHtlcAvailable`]
446-
/// message and trivially getting notified whenever we come online.
447-
///
448-
/// [`HeldHtlcAvailable`]: crate::onion_message::async_payments::HeldHtlcAvailable
449-
hmac: Hmac<Sha256>,
450406
/// The time as duration since the Unix epoch at which this path expires and messages sent over
451407
/// it should be ignored. Without this, anyone with the path corresponding to this context is
452408
/// able to trivially ask if we're online forever.
@@ -468,24 +424,17 @@ impl_writeable_tlv_based_enum!(OffersContext,
468424
(1, OutboundPayment) => {
469425
(0, payment_id, required),
470426
(1, nonce, required),
471-
(2, hmac, option),
472427
},
473428
(2, InboundPayment) => {
474429
(0, payment_hash, required),
475-
(1, nonce, required),
476-
(2, hmac, required)
477430
},
478431
);
479432

480433
impl_writeable_tlv_based_enum!(AsyncPaymentsContext,
481434
(0, OutboundPayment) => {
482435
(0, payment_id, required),
483-
(2, nonce, required),
484-
(4, hmac, required),
485436
},
486437
(1, InboundPayment) => {
487-
(0, nonce, required),
488-
(2, hmac, required),
489438
(4, path_absolute_expiry, required),
490439
},
491440
);

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -543,24 +543,6 @@ pub trait Verification {
543543
) -> Result<(), ()>;
544544
}
545545

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

626572
impl PaymentId {
@@ -5276,10 +5222,7 @@ where
52765222
},
52775223
};
52785224

5279-
let entropy = &*self.entropy_source;
5280-
52815225
let enqueue_held_htlc_available_res = self.flow.enqueue_held_htlc_available(
5282-
entropy,
52835226
invoice,
52845227
payment_id,
52855228
self.get_peers_for_blinded_path(),
@@ -11228,7 +11171,7 @@ where
1122811171

1122911172
let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?;
1123011173

11231-
self.flow.enqueue_invoice(entropy, invoice.clone(), refund, self.get_peers_for_blinded_path())?;
11174+
self.flow.enqueue_invoice(invoice.clone(), refund, self.get_peers_for_blinded_path())?;
1123211175

1123311176
Ok(invoice)
1123411177
},
@@ -13199,8 +13142,6 @@ where
1319913142
fn handle_message(
1320013143
&self, message: OffersMessage, context: Option<OffersContext>, responder: Option<Responder>,
1320113144
) -> Option<(OffersMessage, ResponseInstruction)> {
13202-
let expanded_key = &self.inbound_payment_key;
13203-
1320413145
macro_rules! handle_pay_invoice_res {
1320513146
($res: expr, $invoice: expr, $logger: expr) => {{
1320613147
let error = match $res {
@@ -13306,38 +13247,26 @@ where
1330613247
#[cfg(async_payments)]
1330713248
OffersMessage::StaticInvoice(invoice) => {
1330813249
let payment_id = match context {
13309-
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
13310-
if payment_id.verify_for_offer_payment(hmac, nonce, expanded_key).is_err() {
13311-
return None
13312-
}
13313-
payment_id
13314-
},
13250+
Some(OffersContext::OutboundPayment { payment_id, .. }) => payment_id,
1331513251
_ => return None
1331613252
};
1331713253
let res = self.initiate_async_payment(&invoice, payment_id);
1331813254
handle_pay_invoice_res!(res, invoice, self.logger);
1331913255
},
1332013256
OffersMessage::InvoiceError(invoice_error) => {
1332113257
let payment_hash = match context {
13322-
Some(OffersContext::InboundPayment { payment_hash, nonce, hmac }) => {
13323-
match payment_hash.verify_for_offer_payment(hmac, nonce, expanded_key) {
13324-
Ok(_) => Some(payment_hash),
13325-
Err(_) => None,
13326-
}
13327-
},
13258+
Some(OffersContext::InboundPayment { payment_hash }) => Some(payment_hash),
1332813259
_ => None,
1332913260
};
1333013261

1333113262
let logger = WithContext::from(&self.logger, None, None, payment_hash);
1333213263
log_trace!(logger, "Received invoice_error: {}", invoice_error);
1333313264

1333413265
match context {
13335-
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
13336-
if let Ok(()) = payment_id.verify_for_offer_payment(hmac, nonce, expanded_key) {
13337-
self.abandon_payment_with_reason(
13338-
payment_id, PaymentFailureReason::InvoiceRequestRejected,
13339-
);
13340-
}
13266+
Some(OffersContext::OutboundPayment { payment_id, .. }) => {
13267+
self.abandon_payment_with_reason(
13268+
payment_id, PaymentFailureReason::InvoiceRequestRejected,
13269+
);
1334113270
},
1334213271
_ => {},
1334313272
}
@@ -13390,15 +13319,18 @@ where
1339013319
fn handle_release_held_htlc(&self, _message: ReleaseHeldHtlc, _context: AsyncPaymentsContext) {
1339113320
#[cfg(async_payments)]
1339213321
{
13393-
if let Ok(payment_id) = self.flow.verify_outbound_async_payment_context(_context) {
13394-
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
13395-
log_trace!(
13396-
self.logger,
13397-
"Failed to release held HTLC with payment id {}: {:?}",
13398-
payment_id,
13399-
e
13400-
);
13401-
}
13322+
let payment_id = match _context {
13323+
AsyncPaymentsContext::OutboundPayment { payment_id } => payment_id,
13324+
_ => return,
13325+
};
13326+
13327+
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
13328+
log_trace!(
13329+
self.logger,
13330+
"Failed to release held HTLC with payment id {}: {:?}",
13331+
payment_id,
13332+
e
13333+
);
1340213334
}
1340313335
}
1340413336
}

0 commit comments

Comments
 (0)