Skip to content

Commit e2ee325

Browse files
committed
HMAC Construction and Verification for PaymentHash
When a InvoiceError is received for a sent BOLT12Invoice, the corresponding PaymentHash is to be logged. Introduce hmac construction and verification function for PaymentHash for this purpose.
1 parent 5e62df7 commit e2ee325

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lightning/src/offers/signer.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use bitcoin::hashes::cmp::fixed_time_eq;
1414
use bitcoin::hashes::hmac::{Hmac, HmacEngine};
1515
use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{Keypair, PublicKey, Secp256k1, SecretKey, self};
17+
use types::payment::PaymentHash;
1718
use core::fmt;
1819
use crate::ln::channelmanager::PaymentId;
1920
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
@@ -39,6 +40,9 @@ const WITH_ENCRYPTED_PAYMENT_ID_HMAC_INPUT: &[u8; 16] = &[4; 16];
3940
// HMAC input for a `PaymentId`. The HMAC is used in `OffersContext::OutboundPayment`.
4041
const PAYMENT_ID_HMAC_INPUT: &[u8; 16] = &[5; 16];
4142

43+
// HMAC input for a `PaymentHash`. The HMAC is used in `OffersContext::InboundPayment`.
44+
const PAYMENT_HASH_HMAC_INPUT: &[u8; 16] = &[6; 16];
45+
4246
/// Message metadata which possibly is derived from [`MetadataMaterial`] such that it can be
4347
/// verified.
4448
#[derive(Clone)]
@@ -413,3 +417,22 @@ pub(crate) fn verify_payment_id(
413417
) -> Result<(), ()> {
414418
if hmac_for_payment_id(payment_id, nonce, expanded_key) == hmac { Ok(()) } else { Err(()) }
415419
}
420+
421+
pub(crate) fn hmac_for_payment_hash(
422+
payment_hash: PaymentHash, nonce: Nonce, expanded_key: &ExpandedKey,
423+
) -> Hmac<Sha256> {
424+
const IV_BYTES: &[u8; IV_LEN] = b"LDK Payment Hash";
425+
let mut hmac = expanded_key.hmac_for_offer();
426+
hmac.input(IV_BYTES);
427+
hmac.input(&nonce.0);
428+
hmac.input(PAYMENT_HASH_HMAC_INPUT);
429+
hmac.input(&payment_hash.0);
430+
431+
Hmac::from_engine(hmac)
432+
}
433+
434+
pub(crate) fn verify_payment_hash(
435+
payment_hash: PaymentHash, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &ExpandedKey,
436+
) -> Result<(), ()> {
437+
if hmac_for_payment_hash(payment_hash, nonce, expanded_key) == hmac { Ok(()) } else { Err(()) }
438+
}

0 commit comments

Comments
 (0)