Skip to content

Commit 4ed63ed

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 64ed321 commit 4ed63ed

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,28 @@ macro_rules! hkdf_extract_expand {
2727
hmac.input(&k2);
2828
hmac.input(&[3; 1]);
2929
(k1, k2, Hmac::from_engine(hmac).into_inner())
30+
}};
31+
($salt: expr, $ikm: expr, 4) => {{
32+
let (k1, k2, prk) = hkdf_extract_expand!($salt, $ikm);
33+
34+
let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
35+
hmac.input(&k2);
36+
hmac.input(&[3; 1]);
37+
let k3 = Hmac::from_engine(hmac).into_inner();
38+
39+
let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
40+
hmac.input(&k3);
41+
hmac.input(&[4; 1]);
42+
(k1, k2, k3, Hmac::from_engine(hmac).into_inner())
3043
}}
3144
}
3245

3346
pub fn hkdf_extract_expand_twice(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32]) {
3447
hkdf_extract_expand!(salt, ikm, 2)
3548
}
3649

37-
pub fn hkdf_extract_expand_thrice(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32], [u8; 32]) {
38-
hkdf_extract_expand!(salt, ikm, 3)
50+
pub fn hkdf_extract_expand_4x(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32], [u8; 32], [u8; 32]) {
51+
hkdf_extract_expand!(salt, ikm, 4)
3952
}
4053

4154
#[inline]

0 commit comments

Comments
 (0)