Skip to content

Commit 4fafae0

Browse files
committed
Add an encryption key to ExpandedKey for Offers
Metadata such as the PaymentId should be encrypted when included in an InvoiceRequest or a Refund, as it is user data and is exposed to the payment recipient. Add an encryption key to ExpandedKey for this purpose instead of reusing offers_base_key.
1 parent 971cb20 commit 4fafae0

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lightning/src/ln/inbound_payment.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
1919
use crate::ln::msgs;
2020
use crate::ln::msgs::MAX_VALUE_MSAT;
2121
use crate::util::chacha20::ChaCha20;
22-
use crate::util::crypto::hkdf_extract_expand_4x;
22+
use crate::util::crypto::hkdf_extract_expand_5x;
2323
use crate::util::errors::APIError;
2424
use crate::util::logger::Logger;
2525

@@ -50,20 +50,28 @@ pub struct ExpandedKey {
5050
user_pmt_hash_key: [u8; 32],
5151
/// The base key used to derive signing keys and authenticate messages for BOLT 12 Offers.
5252
offers_base_key: [u8; 32],
53+
/// The key used to encrypt message metadata for BOLT 12 Offers.
54+
offers_encryption_key: [u8; 32],
5355
}
5456

5557
impl ExpandedKey {
5658
/// Create a new [`ExpandedKey`] for generating an inbound payment hash and secret.
5759
///
5860
/// It is recommended to cache this value and not regenerate it for each new inbound payment.
5961
pub fn new(key_material: &KeyMaterial) -> ExpandedKey {
60-
let (metadata_key, ldk_pmt_hash_key, user_pmt_hash_key, offers_base_key) =
61-
hkdf_extract_expand_4x(b"LDK Inbound Payment Key Expansion", &key_material.0);
62+
let (
63+
metadata_key,
64+
ldk_pmt_hash_key,
65+
user_pmt_hash_key,
66+
offers_base_key,
67+
offers_encryption_key,
68+
) = hkdf_extract_expand_5x(b"LDK Inbound Payment Key Expansion", &key_material.0);
6269
Self {
6370
metadata_key,
6471
ldk_pmt_hash_key,
6572
user_pmt_hash_key,
6673
offers_base_key,
74+
offers_encryption_key,
6775
}
6876
}
6977

lightning/src/util/crypto.rs

Lines changed: 11 additions & 4 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, 4) => {{
27+
($salt: expr, $ikm: expr, 5) => {{
2828
let (k1, k2, prk) = hkdf_extract_expand!($salt, $ikm);
2929

3030
let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
@@ -35,16 +35,23 @@ macro_rules! hkdf_extract_expand {
3535
let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
3636
hmac.input(&k3);
3737
hmac.input(&[4; 1]);
38-
(k1, k2, k3, Hmac::from_engine(hmac).into_inner())
38+
let k4 = Hmac::from_engine(hmac).into_inner();
39+
40+
let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
41+
hmac.input(&k4);
42+
hmac.input(&[5; 1]);
43+
let k5 = Hmac::from_engine(hmac).into_inner();
44+
45+
(k1, k2, k3, k4, k5)
3946
}}
4047
}
4148

4249
pub fn hkdf_extract_expand_twice(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32]) {
4350
hkdf_extract_expand!(salt, ikm, 2)
4451
}
4552

46-
pub fn hkdf_extract_expand_4x(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32], [u8; 32], [u8; 32]) {
47-
hkdf_extract_expand!(salt, ikm, 4)
53+
pub fn hkdf_extract_expand_5x(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32], [u8; 32], [u8; 32], [u8; 32]) {
54+
hkdf_extract_expand!(salt, ikm, 5)
4855
}
4956

5057
#[inline]

0 commit comments

Comments
 (0)