Skip to content

Commit 45f88ee

Browse files
Add new inbound payment key for spontaneous payments
This key will be used in upcoming commits for encrypting metadata bytes for spontaneous payments' payment secrets, to be included in the blinded paths of static invoices for async payments. We need a new type of payment secret for these payments because they don't have an a prior known payment hash, see the next commit.
1 parent 02316d2 commit 45f88ee

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lightning/src/crypto/utils.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ macro_rules! hkdf_extract_expand {
2424
let (k1, k2, _) = hkdf_extract_expand!($salt, $ikm);
2525
(k1, k2)
2626
}};
27-
($salt: expr, $ikm: expr, 5) => {{
27+
($salt: expr, $ikm: expr, 6) => {{
2828
let (k1, k2, prk) = hkdf_extract_expand!($salt, $ikm);
2929

3030
let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
@@ -42,18 +42,23 @@ macro_rules! hkdf_extract_expand {
4242
hmac.input(&[5; 1]);
4343
let k5 = Hmac::from_engine(hmac).to_byte_array();
4444

45-
(k1, k2, k3, k4, k5)
45+
let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
46+
hmac.input(&k5);
47+
hmac.input(&[6; 1]);
48+
let k6 = Hmac::from_engine(hmac).to_byte_array();
49+
50+
(k1, k2, k3, k4, k5, k6)
4651
}};
4752
}
4853

4954
pub fn hkdf_extract_expand_twice(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32]) {
5055
hkdf_extract_expand!(salt, ikm, 2)
5156
}
5257

53-
pub fn hkdf_extract_expand_5x(
58+
pub fn hkdf_extract_expand_6x(
5459
salt: &[u8], ikm: &[u8],
55-
) -> ([u8; 32], [u8; 32], [u8; 32], [u8; 32], [u8; 32]) {
56-
hkdf_extract_expand!(salt, ikm, 5)
60+
) -> ([u8; 32], [u8; 32], [u8; 32], [u8; 32], [u8; 32], [u8; 32]) {
61+
hkdf_extract_expand!(salt, ikm, 6)
5762
}
5863

5964
#[inline]

lightning/src/ln/inbound_payment.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use bitcoin::hashes::hmac::{Hmac, HmacEngine};
1515
use bitcoin::hashes::sha256::Hash as Sha256;
1616

1717
use crate::crypto::chacha20::ChaCha20;
18-
use crate::crypto::utils::hkdf_extract_expand_5x;
18+
use crate::crypto::utils::hkdf_extract_expand_6x;
1919
use crate::ln::msgs;
2020
use crate::ln::msgs::MAX_VALUE_MSAT;
2121
use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
@@ -55,6 +55,9 @@ pub struct ExpandedKey {
5555
offers_base_key: [u8; 32],
5656
/// The key used to encrypt message metadata for BOLT 12 Offers.
5757
offers_encryption_key: [u8; 32],
58+
/// The key used to authenticate spontaneous payments' metadata as previously registered with LDK
59+
/// for inclusion in a blinded path.
60+
spontaneous_pmt_key: [u8; 32],
5861
}
5962

6063
impl ExpandedKey {
@@ -68,13 +71,15 @@ impl ExpandedKey {
6871
user_pmt_hash_key,
6972
offers_base_key,
7073
offers_encryption_key,
71-
) = hkdf_extract_expand_5x(b"LDK Inbound Payment Key Expansion", &key_material.0);
74+
spontaneous_pmt_key,
75+
) = hkdf_extract_expand_6x(b"LDK Inbound Payment Key Expansion", &key_material.0);
7276
Self {
7377
metadata_key,
7478
ldk_pmt_hash_key,
7579
user_pmt_hash_key,
7680
offers_base_key,
7781
offers_encryption_key,
82+
spontaneous_pmt_key,
7883
}
7984
}
8085

0 commit comments

Comments
 (0)