Skip to content

Commit 336fc02

Browse files
committed
Add another ExpandedKey derivation for Offers
To support transient signing pubkeys and payer ids for Offers, add another key derivation to ExpandedKey. Also useful for constructing metadata for stateless message authentication.
1 parent fd426a0 commit 336fc02

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

lightning/src/ln/inbound_payment.rs

Lines changed: 6 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_thrice;
22+
use crate::util::crypto::hkdf_extract_expand_4x;
2323
use crate::util::errors::APIError;
2424
use crate::util::logger::Logger;
2525

@@ -48,19 +48,22 @@ pub struct ExpandedKey {
4848
/// The key used to authenticate a user-provided payment hash and metadata as previously
4949
/// registered with LDK.
5050
user_pmt_hash_key: [u8; 32],
51+
/// The base key used to derive signing keys and authenticate messages for BOLT 12 Offers.
52+
offers_base_key: [u8; 32],
5153
}
5254

5355
impl ExpandedKey {
5456
/// Create a new [`ExpandedKey`] for generating an inbound payment hash and secret.
5557
///
5658
/// It is recommended to cache this value and not regenerate it for each new inbound payment.
5759
pub fn new(key_material: &KeyMaterial) -> ExpandedKey {
58-
let (metadata_key, ldk_pmt_hash_key, user_pmt_hash_key) =
59-
hkdf_extract_expand_thrice(b"LDK Inbound Payment Key Expansion", &key_material.0);
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);
6062
Self {
6163
metadata_key,
6264
ldk_pmt_hash_key,
6365
user_pmt_hash_key,
66+
offers_base_key,
6467
}
6568
}
6669
}

lightning/src/util/crypto.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,27 @@ macro_rules! hkdf_extract_expand {
2020
let (k1, k2, _) = hkdf_extract_expand!($salt, $ikm);
2121
(k1, k2)
2222
}};
23-
($salt: expr, $ikm: expr, 3) => {{
23+
($salt: expr, $ikm: expr, 4) => {{
2424
let (k1, k2, prk) = hkdf_extract_expand!($salt, $ikm);
2525

2626
let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
2727
hmac.input(&k2);
2828
hmac.input(&[3; 1]);
29-
(k1, k2, Hmac::from_engine(hmac).into_inner())
29+
let k3 = Hmac::from_engine(hmac).into_inner();
30+
31+
let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
32+
hmac.input(&k3);
33+
hmac.input(&[4; 1]);
34+
(k1, k2, k3, Hmac::from_engine(hmac).into_inner())
3035
}}
3136
}
3237

3338
pub fn hkdf_extract_expand_twice(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32]) {
3439
hkdf_extract_expand!(salt, ikm, 2)
3540
}
3641

37-
pub fn hkdf_extract_expand_thrice(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32], [u8; 32]) {
38-
hkdf_extract_expand!(salt, ikm, 3)
42+
pub fn hkdf_extract_expand_4x(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32], [u8; 32], [u8; 32]) {
43+
hkdf_extract_expand!(salt, ikm, 4)
3944
}
4045

4146
#[inline]

0 commit comments

Comments
 (0)