Skip to content

Commit 612d964

Browse files
Aditya SharmaAditya 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 9e3d52a commit 612d964

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,7 @@ fn route_blinding_spec_test_vector() {
14661466
fn sign_invoice(
14671467
&self, _invoice: &RawBolt11Invoice, _recipient: Recipient,
14681468
) -> Result<RecoverableSignature, ()> { unreachable!() }
1469+
fn get_peer_storage_key(&self) -> [u8;32] { unreachable!() }
14691470
fn sign_bolt12_invoice_request(
14701471
&self, _invoice_request: &UnsignedInvoiceRequest,
14711472
) -> Result<schnorr::Signature, ()> { unreachable!() }

lightning/src/sign/mod.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,35 @@ pub trait NodeSigner {
835835
/// [phantom node payments]: PhantomKeysManager
836836
fn get_inbound_payment_key_material(&self) -> KeyMaterial;
837837

838+
/// Generates a 32-byte key used for peer storage encryption.
839+
///
840+
/// This function derives an encryption key for peer storage by using the HKDF
841+
/// (HMAC-based Key Derivation Function) with a specific label and the node
842+
/// secret key. The derived key is used for encrypting or decrypting peer storage
843+
/// data.
844+
///
845+
/// The process involves the following steps:
846+
/// 1. Retrieves the node secret key.
847+
/// 2. Uses the node secret key and the label `"Peer Storage Encryption Key"`
848+
/// to perform HKDF extraction and expansion.
849+
/// 3. Returns the first part of the derived key, which is a 32-byte array.
850+
///
851+
/// # Returns
852+
///
853+
/// Returns a 32-byte array that serves as the encryption key for peer storage.
854+
///
855+
/// # Panics
856+
///
857+
/// This function does not panic under normal circumstances, but failures in
858+
/// obtaining the node secret key or issues within the HKDF function may cause
859+
/// unexpected behavior.
860+
///
861+
/// # Notes
862+
///
863+
/// Ensure that the node secret key is securely managed, as it is crucial for
864+
/// the security of the derived encryption key.
865+
fn get_peer_storage_key(&self) -> [u8; 32];
866+
838867
/// Get node id based on the provided [`Recipient`].
839868
///
840869
/// This method must return the same value each time it is called with a given [`Recipient`]
@@ -2174,6 +2203,14 @@ impl NodeSigner for KeysManager {
21742203
self.inbound_payment_key.clone()
21752204
}
21762205

2206+
fn get_peer_storage_key(&self) -> [u8; 32] {
2207+
let (t1, _) = hkdf_extract_expand_twice(
2208+
b"Peer Storage Encryption Key",
2209+
&self.get_node_secret_key().secret_bytes(),
2210+
);
2211+
t1
2212+
}
2213+
21772214
fn sign_invoice(
21782215
&self, invoice: &RawBolt11Invoice, recipient: Recipient,
21792216
) -> Result<RecoverableSignature, ()> {
@@ -2352,6 +2389,14 @@ impl NodeSigner for PhantomKeysManager {
23522389
self.inbound_payment_key.clone()
23532390
}
23542391

2392+
fn get_peer_storage_key(&self) -> [u8; 32] {
2393+
let (t1, _) = hkdf_extract_expand_twice(
2394+
b"Peer Storage Encryption Key",
2395+
&self.get_node_secret_key().secret_bytes(),
2396+
);
2397+
t1
2398+
}
2399+
23552400
fn sign_invoice(
23562401
&self, invoice: &RawBolt11Invoice, recipient: Recipient,
23572402
) -> Result<RecoverableSignature, ()> {

lightning/src/util/test_utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,10 @@ impl NodeSigner for TestNodeSigner {
11951195
unreachable!()
11961196
}
11971197

1198+
fn get_peer_storage_key(&self) -> [u8;32] {
1199+
unreachable!()
1200+
}
1201+
11981202
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
11991203
let node_secret = match recipient {
12001204
Recipient::Node => Ok(&self.node_secret),
@@ -1271,6 +1275,10 @@ impl NodeSigner for TestKeysInterface {
12711275
self.backing.sign_invoice(invoice, recipient)
12721276
}
12731277

1278+
fn get_peer_storage_key(&self) -> [u8;32] {
1279+
self.backing.get_peer_storage_key()
1280+
}
1281+
12741282
fn sign_bolt12_invoice_request(
12751283
&self, invoice_request: &UnsignedInvoiceRequest
12761284
) -> Result<schnorr::Signature, ()> {

0 commit comments

Comments
 (0)