Skip to content

Commit 1bc7884

Browse files
Aditya Sharmaadi2011
Aditya Sharma
authored andcommitted
Add method to derive Peer Storage encryption key
Add get_peer_storage_key method to derive a 32-byte encryption key for securing Peer Storage. This method utilizes HKDF with the node's secret key as input and a fixed info string to generate the encryption key. - Add 'get_peer_storage_key' to NodeSigner. - Implement 'get_peer_storage_key' for KeysManager & PhantomKeysManager.
1 parent 047bd61 commit 1bc7884

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ impl NodeSigner for KeyProvider {
336336
unreachable!()
337337
}
338338

339+
fn get_peer_storage_key(&self) -> [u8; 32] {
340+
unreachable!()
341+
}
342+
339343
fn sign_bolt12_invoice(
340344
&self, _invoice: &UnsignedBolt12Invoice,
341345
) -> Result<schnorr::Signature, ()> {

fuzz/src/full_stack.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ impl NodeSigner for KeyProvider {
420420
let secp_ctx = Secp256k1::signing_only();
421421
Ok(secp_ctx.sign_ecdsa(&msg_hash, &self.node_secret))
422422
}
423+
424+
fn get_peer_storage_key(&self) -> [u8; 32] {
425+
unreachable!()
426+
}
423427
}
424428

425429
impl SignerProvider for KeyProvider {

fuzz/src/onion_message.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ impl NodeSigner for KeyProvider {
246246
) -> Result<bitcoin::secp256k1::ecdsa::Signature, ()> {
247247
unreachable!()
248248
}
249+
250+
fn get_peer_storage_key(&self) -> [u8; 32] {
251+
unreachable!()
252+
}
249253
}
250254

251255
impl SignerProvider for KeyProvider {

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,7 @@ fn route_blinding_spec_test_vector() {
15501550
fn sign_invoice(
15511551
&self, _invoice: &RawBolt11Invoice, _recipient: Recipient,
15521552
) -> Result<RecoverableSignature, ()> { unreachable!() }
1553+
fn get_peer_storage_key(&self) -> [u8;32] { unreachable!() }
15531554
fn sign_bolt12_invoice(
15541555
&self, _invoice: &UnsignedBolt12Invoice,
15551556
) -> Result<schnorr::Signature, ()> { unreachable!() }

lightning/src/sign/mod.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,35 @@ pub trait NodeSigner {
856856
/// [phantom node payments]: PhantomKeysManager
857857
fn get_inbound_payment_key(&self) -> ExpandedKey;
858858

859+
/// Generates a 32-byte key used for peer storage encryption.
860+
///
861+
/// This function derives an encryption key for peer storage by using the HKDF
862+
/// (HMAC-based Key Derivation Function) with a specific label and the node
863+
/// secret key. The derived key is used for encrypting or decrypting peer storage
864+
/// data.
865+
///
866+
/// The process involves the following steps:
867+
/// 1. Retrieves the node secret key.
868+
/// 2. Uses the node secret key and the label `"Peer Storage Encryption Key"`
869+
/// to perform HKDF extraction and expansion.
870+
/// 3. Returns the first part of the derived key, which is a 32-byte array.
871+
///
872+
/// # Returns
873+
///
874+
/// Returns a 32-byte array that serves as the encryption key for peer storage.
875+
///
876+
/// # Panics
877+
///
878+
/// This function does not panic under normal circumstances, but failures in
879+
/// obtaining the node secret key or issues within the HKDF function may cause
880+
/// unexpected behavior.
881+
///
882+
/// # Notes
883+
///
884+
/// Ensure that the node secret key is securely managed, as it is crucial for
885+
/// the security of the derived encryption key.
886+
fn get_peer_storage_key(&self) -> [u8; 32];
887+
859888
/// Get node id based on the provided [`Recipient`].
860889
///
861890
/// This method must return the same value each time it is called with a given [`Recipient`]
@@ -2201,6 +2230,14 @@ impl NodeSigner for KeysManager {
22012230
self.inbound_payment_key.clone()
22022231
}
22032232

2233+
fn get_peer_storage_key(&self) -> [u8; 32] {
2234+
let (t1, _) = hkdf_extract_expand_twice(
2235+
b"Peer Storage Encryption Key",
2236+
&self.get_node_secret_key().secret_bytes(),
2237+
);
2238+
t1
2239+
}
2240+
22042241
fn sign_invoice(
22052242
&self, invoice: &RawBolt11Invoice, recipient: Recipient,
22062243
) -> Result<RecoverableSignature, ()> {
@@ -2370,6 +2407,14 @@ impl NodeSigner for PhantomKeysManager {
23702407
self.inbound_payment_key.clone()
23712408
}
23722409

2410+
fn get_peer_storage_key(&self) -> [u8; 32] {
2411+
let (t1, _) = hkdf_extract_expand_twice(
2412+
b"Peer Storage Encryption Key",
2413+
&self.get_node_secret_key().secret_bytes(),
2414+
);
2415+
t1
2416+
}
2417+
23732418
fn sign_invoice(
23742419
&self, invoice: &RawBolt11Invoice, recipient: Recipient,
23752420
) -> Result<RecoverableSignature, ()> {

lightning/src/util/test_utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,10 @@ impl NodeSigner for TestNodeSigner {
14601460
unreachable!()
14611461
}
14621462

1463+
fn get_peer_storage_key(&self) -> [u8; 32] {
1464+
unreachable!()
1465+
}
1466+
14631467
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
14641468
let node_secret = match recipient {
14651469
Recipient::Node => Ok(&self.node_secret),
@@ -1538,6 +1542,10 @@ impl NodeSigner for TestKeysInterface {
15381542
self.backing.sign_invoice(invoice, recipient)
15391543
}
15401544

1545+
fn get_peer_storage_key(&self) -> [u8; 32] {
1546+
self.backing.get_peer_storage_key()
1547+
}
1548+
15411549
fn sign_bolt12_invoice(
15421550
&self, invoice: &UnsignedBolt12Invoice,
15431551
) -> Result<schnorr::Signature, ()> {

0 commit comments

Comments
 (0)