Skip to content

Commit ad25a0d

Browse files
committed
Stop using rng in peer_channel_encryptor to generate ephemeral keys
This removes the bulk of our reliance on the rand crate in non-test envs, paving a way towards a syscall-less rust-lightning and WASM. Since this is a breaking change for full_stack_target (and several fuzz targets), go ahead and make other changes to make things more distinct.
1 parent 1bb3ea2 commit ad25a0d

6 files changed

+121
-114
lines changed

fuzz/fuzz_targets/chanmon_fail_consistency.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use lightning::ln::channelmonitor::{ChannelMonitorUpdateErr, HTLCUpdate};
3737
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage};
3838
use lightning::ln::router::{Route, RouteHop};
3939
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, HandleError, UpdateAddHTLC};
40-
use lightning::util::{reset_rng_state, fill_bytes, events};
40+
use lightning::util::{reset_rng_state, events};
4141
use lightning::util::logger::Logger;
4242
use lightning::util::config::UserConfig;
4343
use lightning::util::events::{EventsProvider, MessageSendEventsProvider};
@@ -52,6 +52,7 @@ use secp256k1::Secp256k1;
5252
use std::cmp::Ordering;
5353
use std::collections::HashSet;
5454
use std::sync::{Arc,Mutex};
55+
use std::sync::atomic;
5556
use std::io::Cursor;
5657

5758
struct FuzzEstimator {}
@@ -91,6 +92,8 @@ impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
9192

9293
struct KeyProvider {
9394
node_id: u8,
95+
session_id: atomic::AtomicU8,
96+
channel_id: atomic::AtomicU8,
9497
}
9598
impl KeysInterface for KeyProvider {
9699
fn get_node_secret(&self) -> SecretKey {
@@ -121,15 +124,13 @@ impl KeysInterface for KeyProvider {
121124
}
122125

123126
fn get_session_key(&self) -> SecretKey {
124-
let mut session_key = [0; 32];
125-
fill_bytes(&mut session_key);
126-
SecretKey::from_slice(&session_key).unwrap()
127+
let id = self.session_id.fetch_add(1, atomic::Ordering::Relaxed);
128+
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, id, 10, self.node_id]).unwrap()
127129
}
128130

129131
fn get_channel_id(&self) -> [u8; 32] {
130-
let mut channel_id = [0; 32];
131-
fill_bytes(&mut channel_id);
132-
channel_id
132+
let id = self.channel_id.fetch_add(1, atomic::Ordering::Relaxed);
133+
[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, id, 11, self.node_id]
133134
}
134135
}
135136

@@ -146,7 +147,7 @@ pub fn do_test(data: &[u8]) {
146147
let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
147148
let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone()));
148149

149-
let keys_manager = Arc::new(KeyProvider { node_id: $node_id });
150+
let keys_manager = Arc::new(KeyProvider { node_id: $node_id, session_id: atomic::AtomicU8::new(0), channel_id: atomic::AtomicU8::new(0) });
150151
let mut config = UserConfig::new();
151152
config.channel_options.fee_proportional_millionths = 0;
152153
config.channel_options.announced_channel = true;

0 commit comments

Comments
 (0)