Skip to content

Commit 02f1674

Browse files
committed
Remove get_node_secret from NodeSigner
Secrets should not be exposed in-memory at the interface level as it would be impossible to implement it against a hardware security module/secure element.
1 parent 98417a1 commit 02f1674

20 files changed

+571
-334
lines changed

fuzz/src/chanmon_consistency.rs

+28-26
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use bitcoin::network::constants::Network;
2929

3030
use bitcoin::hashes::Hash as TraitImport;
3131
use bitcoin::hashes::sha256::Hash as Sha256;
32+
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
3233
use bitcoin::hash_types::{BlockHash, WPubkeyHash};
3334

3435
use lightning::chain;
@@ -54,10 +55,9 @@ use lightning::routing::router::{InFlightHtlcs, Route, RouteHop, RouteParameters
5455
use crate::utils::test_logger::{self, Output};
5556
use crate::utils::test_persister::TestPersister;
5657

57-
use bitcoin::secp256k1::{PublicKey, SecretKey, Scalar};
58+
use bitcoin::secp256k1::{Message, PublicKey, SecretKey, Scalar, Secp256k1};
5859
use bitcoin::secp256k1::ecdh::SharedSecret;
59-
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
60-
use bitcoin::secp256k1::Secp256k1;
60+
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
6161

6262
use std::mem;
6363
use std::cmp::{self, Ordering};
@@ -174,45 +174,47 @@ impl chain::Watch<EnforcingSigner> for TestChainMonitor {
174174
}
175175

176176
struct KeyProvider {
177-
node_id: u8,
177+
node_secret: SecretKey,
178178
rand_bytes_id: atomic::AtomicU32,
179179
enforcement_states: Mutex<HashMap<[u8;32], Arc<Mutex<EnforcementState>>>>,
180180
}
181181

182182
impl EntropySource for KeyProvider {
183183
fn get_secure_random_bytes(&self) -> [u8; 32] {
184184
let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed);
185-
let mut res = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, self.node_id];
185+
let mut res = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, self.node_secret[31]];
186186
res[30-4..30].copy_from_slice(&id.to_le_bytes());
187187
res
188188
}
189189
}
190190

191191
impl NodeSigner for KeyProvider {
192-
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> {
193-
Ok(SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, self.node_id]).unwrap())
194-
}
195-
196-
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
192+
fn get_node_id(&self, _recipient: Recipient) -> Result<PublicKey, ()> {
197193
let secp_ctx = Secp256k1::signing_only();
198-
Ok(PublicKey::from_secret_key(&secp_ctx, &self.get_node_secret(recipient)?))
194+
Ok(PublicKey::from_secret_key(&secp_ctx, &self.node_secret))
199195
}
200196

201-
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
202-
let mut node_secret = self.get_node_secret(recipient)?;
197+
fn ecdh(&self, _recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
198+
let mut node_secret = self.node_secret.clone();
203199
if let Some(tweak) = tweak {
204-
node_secret = node_secret.mul_tweak(tweak).unwrap();
200+
node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
205201
}
206202
Ok(SharedSecret::new(other_key, &node_secret))
207203
}
208204

209205
fn get_inbound_payment_key_material(&self) -> KeyMaterial {
210-
KeyMaterial([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, self.node_id])
206+
KeyMaterial([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, self.node_secret[31]])
211207
}
212208

213209
fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> {
214210
unreachable!()
215211
}
212+
213+
fn sign_gossip_message(&self, msg: lightning::ln::msgs::UnsignedGossipMessage) -> Result<Signature, ()> {
214+
let msg_hash = Message::from_slice(&Sha256dHash::hash(&msg.encode()[..])[..]).map_err(|_| ())?;
215+
let secp_ctx = Secp256k1::signing_only();
216+
Ok(secp_ctx.sign_ecdsa(&msg_hash, &self.node_secret))
217+
}
216218
}
217219

218220
impl SignerProvider for KeyProvider {
@@ -228,13 +230,12 @@ impl SignerProvider for KeyProvider {
228230
let id = channel_keys_id[0];
229231
let keys = InMemorySigner::new(
230232
&secp_ctx,
231-
self.get_node_secret(Recipient::Node).unwrap(),
232-
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_id]).unwrap(),
233-
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_id]).unwrap(),
234-
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
235-
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
236-
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
237-
[id, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
233+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_secret[31]]).unwrap(),
234+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_secret[31]]).unwrap(),
235+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_secret[31]]).unwrap(),
236+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_secret[31]]).unwrap(),
237+
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_secret[31]]).unwrap(),
238+
[id, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_secret[31]],
238239
channel_value_satoshis,
239240
channel_keys_id,
240241
);
@@ -245,7 +246,7 @@ impl SignerProvider for KeyProvider {
245246
fn read_chan_signer(&self, buffer: &[u8]) -> Result<Self::Signer, DecodeError> {
246247
let mut reader = std::io::Cursor::new(buffer);
247248

248-
let inner: InMemorySigner = ReadableArgs::read(&mut reader, self.get_node_secret(Recipient::Node).unwrap())?;
249+
let inner: InMemorySigner = Readable::read(&mut reader)?;
249250
let state = self.make_enforcement_state_cell(inner.commitment_seed);
250251

251252
Ok(EnforcingSigner {
@@ -257,14 +258,14 @@ impl SignerProvider for KeyProvider {
257258

258259
fn get_destination_script(&self) -> Script {
259260
let secp_ctx = Secp256k1::signing_only();
260-
let channel_monitor_claim_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, self.node_id]).unwrap();
261+
let channel_monitor_claim_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, self.node_secret[31]]).unwrap();
261262
let our_channel_monitor_claim_key_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &channel_monitor_claim_key).serialize());
262263
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script()
263264
}
264265

265266
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript {
266267
let secp_ctx = Secp256k1::signing_only();
267-
let secret_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, self.node_id]).unwrap();
268+
let secret_key = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, self.node_secret[31]]).unwrap();
268269
let pubkey_hash = WPubkeyHash::hash(&PublicKey::from_secret_key(&secp_ctx, &secret_key).serialize());
269270
ShutdownScript::new_p2wpkh(&pubkey_hash)
270271
}
@@ -402,7 +403,8 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
402403
macro_rules! make_node {
403404
($node_id: expr, $fee_estimator: expr) => { {
404405
let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new($node_id.to_string(), out.clone()));
405-
let keys_manager = Arc::new(KeyProvider { node_id: $node_id, rand_bytes_id: atomic::AtomicU32::new(0), enforcement_states: Mutex::new(HashMap::new()) });
406+
let node_secret = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, $node_id]).unwrap();
407+
let keys_manager = Arc::new(KeyProvider { node_secret, rand_bytes_id: atomic::AtomicU32::new(0), enforcement_states: Mutex::new(HashMap::new()) });
406408
let monitor = Arc::new(TestChainMonitor::new(broadcast.clone(), logger.clone(), $fee_estimator.clone(),
407409
Arc::new(TestPersister {
408410
update_ret: Mutex::new(ChannelMonitorUpdateStatus::Completed)

fuzz/src/full_stack.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use bitcoin::network::constants::Network;
2626
use bitcoin::hashes::Hash as TraitImport;
2727
use bitcoin::hashes::HashEngine as TraitImportEngine;
2828
use bitcoin::hashes::sha256::Hash as Sha256;
29+
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
2930
use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
3031

3132
use lightning::chain;
@@ -47,14 +48,14 @@ use lightning::util::errors::APIError;
4748
use lightning::util::events::Event;
4849
use lightning::util::enforcing_trait_impls::{EnforcingSigner, EnforcementState};
4950
use lightning::util::logger::Logger;
50-
use lightning::util::ser::ReadableArgs;
51+
use lightning::util::ser::{Readable, Writeable};
5152

5253
use crate::utils::test_logger;
5354
use crate::utils::test_persister::TestPersister;
5455

55-
use bitcoin::secp256k1::{PublicKey, SecretKey, Scalar};
56+
use bitcoin::secp256k1::{Message, PublicKey, SecretKey, Scalar};
5657
use bitcoin::secp256k1::ecdh::SharedSecret;
57-
use bitcoin::secp256k1::ecdsa::RecoverableSignature;
58+
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
5859
use bitcoin::secp256k1::Secp256k1;
5960

6061
use std::cell::RefCell;
@@ -183,7 +184,7 @@ impl<'a> std::hash::Hash for Peer<'a> {
183184
type ChannelMan<'a> = ChannelManager<
184185
Arc<chainmonitor::ChainMonitor<EnforcingSigner, Arc<dyn chain::Filter>, Arc<TestBroadcaster>, Arc<FuzzEstimator>, Arc<dyn Logger>, Arc<TestPersister>>>,
185186
Arc<TestBroadcaster>, Arc<KeyProvider>, Arc<KeyProvider>, Arc<KeyProvider>, Arc<FuzzEstimator>, &'a FuzzRouter, Arc<dyn Logger>>;
186-
type PeerMan<'a> = PeerManager<Peer<'a>, Arc<ChannelMan<'a>>, Arc<P2PGossipSync<Arc<NetworkGraph<Arc<dyn Logger>>>, Arc<dyn chain::Access>, Arc<dyn Logger>>>, IgnoringMessageHandler, Arc<dyn Logger>, IgnoringMessageHandler>;
187+
type PeerMan<'a> = PeerManager<Peer<'a>, Arc<ChannelMan<'a>>, Arc<P2PGossipSync<Arc<NetworkGraph<Arc<dyn Logger>>>, Arc<dyn chain::Access>, Arc<dyn Logger>>>, IgnoringMessageHandler, Arc<dyn Logger>, IgnoringMessageHandler, Arc<KeyProvider>>;
187188

188189
struct MoneyLossDetector<'a> {
189190
manager: Arc<ChannelMan<'a>>,
@@ -293,19 +294,15 @@ impl EntropySource for KeyProvider {
293294
}
294295

295296
impl NodeSigner for KeyProvider {
296-
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> {
297-
Ok(self.node_secret.clone())
298-
}
299-
300-
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
297+
fn get_node_id(&self, _recipient: Recipient) -> Result<PublicKey, ()> {
301298
let secp_ctx = Secp256k1::signing_only();
302-
Ok(PublicKey::from_secret_key(&secp_ctx, &self.get_node_secret(recipient)?))
299+
Ok(PublicKey::from_secret_key(&secp_ctx, &self.node_secret))
303300
}
304301

305-
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
306-
let mut node_secret = self.get_node_secret(recipient)?;
302+
fn ecdh(&self, _recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
303+
let mut node_secret = self.node_secret.clone();
307304
if let Some(tweak) = tweak {
308-
node_secret = node_secret.mul_tweak(tweak).unwrap();
305+
node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
309306
}
310307
Ok(SharedSecret::new(other_key, &node_secret))
311308
}
@@ -317,6 +314,12 @@ impl NodeSigner for KeyProvider {
317314
fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> {
318315
unreachable!()
319316
}
317+
318+
fn sign_gossip_message(&self, msg: lightning::ln::msgs::UnsignedGossipMessage) -> Result<Signature, ()> {
319+
let msg_hash = Message::from_slice(&Sha256dHash::hash(&msg.encode()[..])[..]).map_err(|_| ())?;
320+
let secp_ctx = Secp256k1::signing_only();
321+
Ok(secp_ctx.sign_ecdsa(&msg_hash, &self.node_secret))
322+
}
320323
}
321324

322325
impl SignerProvider for KeyProvider {
@@ -335,7 +338,6 @@ impl SignerProvider for KeyProvider {
335338
EnforcingSigner::new_with_revoked(if inbound {
336339
InMemorySigner::new(
337340
&secp_ctx,
338-
self.node_secret.clone(),
339341
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ctr]).unwrap(),
340342
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, ctr]).unwrap(),
341343
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, ctr]).unwrap(),
@@ -348,7 +350,6 @@ impl SignerProvider for KeyProvider {
348350
} else {
349351
InMemorySigner::new(
350352
&secp_ctx,
351-
self.node_secret.clone(),
352353
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, ctr]).unwrap(),
353354
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, ctr]).unwrap(),
354355
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, ctr]).unwrap(),
@@ -362,7 +363,7 @@ impl SignerProvider for KeyProvider {
362363
}
363364

364365
fn read_chan_signer(&self, mut data: &[u8]) -> Result<EnforcingSigner, DecodeError> {
365-
let inner: InMemorySigner = ReadableArgs::read(&mut data, self.node_secret.clone())?;
366+
let inner: InMemorySigner = Readable::read(&mut data)?;
366367
let state = Arc::new(Mutex::new(EnforcementState::new()));
367368

368369
Ok(EnforcingSigner::new_with_revoked(
@@ -446,7 +447,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
446447
// keys subsequently generated in this test. Rather than regenerating all the messages manually,
447448
// it's easier to just increment the counter here so the keys don't change.
448449
keys_manager.counter.fetch_sub(3, Ordering::AcqRel);
449-
let our_id = PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret(Recipient::Node).unwrap());
450+
let our_id = &keys_manager.get_node_id(Recipient::Node).unwrap();
450451
let network_graph = Arc::new(NetworkGraph::new(genesis_block(network).block_hash(), Arc::clone(&logger)));
451452
let gossip_sync = Arc::new(P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)));
452453
let scorer = FixedPenaltyScorer::with_penalty(0);
@@ -456,7 +457,7 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
456457
chan_handler: channelmanager.clone(),
457458
route_handler: gossip_sync.clone(),
458459
onion_message_handler: IgnoringMessageHandler {},
459-
}, our_network_key, 0, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0], Arc::clone(&logger), IgnoringMessageHandler{}));
460+
}, 0, &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0], Arc::clone(&logger), IgnoringMessageHandler{}, keys_manager.clone()));
460461

461462
let mut should_forward = false;
462463
let mut payments_received: Vec<PaymentHash> = Vec::new();

fuzz/src/onion_message.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,13 @@ impl EntropySource for KeyProvider {
100100
}
101101

102102
impl NodeSigner for KeyProvider {
103-
fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> {
104-
Ok(self.node_secret.clone())
105-
}
106-
107-
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
103+
fn get_node_id(&self, _recipient: Recipient) -> Result<PublicKey, ()> {
108104
let secp_ctx = Secp256k1::signing_only();
109-
Ok(PublicKey::from_secret_key(&secp_ctx, &self.get_node_secret(recipient)?))
105+
Ok(PublicKey::from_secret_key(&secp_ctx, &self.node_secret))
110106
}
111107

112-
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
113-
let mut node_secret = self.get_node_secret(recipient)?;
108+
fn ecdh(&self, _recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
109+
let mut node_secret = self.node_secret.clone();
114110
if let Some(tweak) = tweak {
115111
node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
116112
}
@@ -122,6 +118,10 @@ impl NodeSigner for KeyProvider {
122118
fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> {
123119
unreachable!()
124120
}
121+
122+
fn sign_gossip_message(&self, _msg: lightning::ln::msgs::UnsignedGossipMessage) -> Result<bitcoin::secp256k1::ecdsa::Signature, ()> {
123+
unreachable!()
124+
}
125125
}
126126

127127
impl SignerProvider for KeyProvider {

fuzz/src/peer_crypt.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use lightning::ln::peer_channel_encryptor::PeerChannelEncryptor;
1111

1212
use bitcoin::secp256k1::{Secp256k1, PublicKey, SecretKey};
1313

14-
use crate::utils::test_logger;
14+
use crate::utils::{test_logger, test_node_signer};
1515

1616
#[inline]
1717
fn slice_to_be16(v: &[u8]) -> u16 {
@@ -41,6 +41,7 @@ pub fn do_test(data: &[u8]) {
4141
Ok(key) => key,
4242
Err(_) => return,
4343
};
44+
let node_signer = test_node_signer::TestNodeSigner::new(our_network_key);
4445
let ephemeral_key = match SecretKey::from_slice(get_slice!(32)) {
4546
Ok(key) => key,
4647
Err(_) => return,
@@ -53,15 +54,15 @@ pub fn do_test(data: &[u8]) {
5354
};
5455
let mut crypter = PeerChannelEncryptor::new_outbound(their_pubkey, ephemeral_key);
5556
crypter.get_act_one(&secp_ctx);
56-
match crypter.process_act_two(get_slice!(50), &our_network_key, &secp_ctx) {
57+
match crypter.process_act_two(get_slice!(50), &&node_signer) {
5758
Ok(_) => {},
5859
Err(_) => return,
5960
}
6061
assert!(crypter.is_ready_for_encryption());
6162
crypter
6263
} else {
63-
let mut crypter = PeerChannelEncryptor::new_inbound(&our_network_key, &secp_ctx);
64-
match crypter.process_act_one_with_keys(get_slice!(50), &our_network_key, ephemeral_key, &secp_ctx) {
64+
let mut crypter = PeerChannelEncryptor::new_inbound(&&node_signer);
65+
match crypter.process_act_one_with_keys(get_slice!(50), &&node_signer, ephemeral_key, &secp_ctx) {
6566
Ok(_) => {},
6667
Err(_) => return,
6768
}

fuzz/src/utils/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99

1010
pub mod test_logger;
1111
pub mod test_persister;
12+
pub mod test_node_signer;

fuzz/src/utils/test_node_signer.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
use bitcoin::secp256k1::{PublicKey, SecretKey, Secp256k1};
11+
use bitcoin::secp256k1::ecdh::SharedSecret;
12+
13+
use lightning::chain::keysinterface::{NodeSigner, Recipient};
14+
15+
pub struct TestNodeSigner {
16+
node_secret: SecretKey,
17+
}
18+
19+
impl TestNodeSigner {
20+
pub fn new(node_secret: SecretKey) -> Self {
21+
Self { node_secret }
22+
}
23+
}
24+
25+
impl NodeSigner for TestNodeSigner {
26+
fn get_inbound_payment_key_material(&self) -> lightning::chain::keysinterface::KeyMaterial {
27+
unreachable!()
28+
}
29+
30+
fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
31+
let node_secret = match recipient {
32+
Recipient::Node => Ok(&self.node_secret),
33+
Recipient::PhantomNode => Err(())
34+
}?;
35+
Ok(PublicKey::from_secret_key(&Secp256k1::new(), node_secret))
36+
}
37+
38+
fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&bitcoin::secp256k1::Scalar>) -> Result<SharedSecret, ()> {
39+
let mut node_secret = match recipient {
40+
Recipient::Node => Ok(self.node_secret.clone()),
41+
Recipient::PhantomNode => Err(())
42+
}?;
43+
if let Some(tweak) = tweak {
44+
node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
45+
}
46+
Ok(SharedSecret::new(other_key, &node_secret))
47+
}
48+
49+
fn sign_invoice(&self, _: &[u8], _: &[bitcoin::bech32::u5], _: Recipient) -> Result<bitcoin::secp256k1::ecdsa::RecoverableSignature, ()> {
50+
unreachable!()
51+
}
52+
53+
fn sign_gossip_message(&self, _: lightning::ln::msgs::UnsignedGossipMessage) -> Result<bitcoin::secp256k1::ecdsa::Signature, ()> {
54+
unreachable!()
55+
}
56+
}

0 commit comments

Comments
 (0)