From: Matt Corallo Date: Mon, 19 Mar 2018 19:15:10 +0000 (-0400) Subject: Stub out RNG when fuzzing X-Git-Tag: v0.0.12~417^2~5 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=d18e54bd90e949267a025260cf09e10c3804875f;p=rust-lightning Stub out RNG when fuzzing --- diff --git a/src/ln/channel.rs b/src/ln/channel.rs index 2b8b085a8..3c32cd39b 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -22,9 +22,7 @@ use ln::channelmanager::PendingForwardHTLCInfo; use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment}; use ln::chan_utils; use chain::chaininterface::{FeeEstimator,ConfirmationTarget}; -use util::transaction_utils; - -use rand::{thread_rng,Rng}; +use util::{transaction_utils,rng}; use std::default::Default; use std::cmp; @@ -274,12 +272,11 @@ impl Channel { panic!("funding value > 2^24"); } - let mut rng = thread_rng(); let feerate = fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Normal); let background_feerate = fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Background); let mut key_seed = [0u8; 32]; - rng.fill_bytes(&mut key_seed); + rng::fill_bytes(&mut key_seed); let chan_keys = match ChannelKeys::new_from_seed(&key_seed) { Ok(key) => key, Err(_) => panic!("RNG is busted!") @@ -296,7 +293,7 @@ impl Channel { Channel { user_id: user_id, - channel_id: Uint256([rng.gen(), rng.gen(), rng.gen(), rng.gen()]), + channel_id: rng::rand_uint256(), channel_state: ChannelState::OurInitSent as u32, channel_outbound: true, secp_ctx: secp_ctx, @@ -392,9 +389,8 @@ impl Channel { let background_feerate = fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Background); - let mut rng = thread_rng(); let mut key_seed = [0u8; 32]; - rng.fill_bytes(&mut key_seed); + rng::fill_bytes(&mut key_seed); let chan_keys = match ChannelKeys::new_from_seed(&key_seed) { Ok(key) => key, Err(_) => panic!("RNG is busted!") diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index 14e089249..b492f3b38 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -17,11 +17,7 @@ use ln::channelmonitor::ManyChannelMonitor; use ln::router::Route; use ln::msgs; use ln::msgs::{HandleError,ChannelMessageHandler,MsgEncodable,MsgDecodable}; -use util::byte_utils; -use util::events; -use util::internal_traits; - -use rand::{thread_rng,Rng}; +use util::{byte_utils, events, internal_traits, rng}; use crypto::mac::{Mac,MacResult}; use crypto::hmac::Hmac; @@ -468,10 +464,9 @@ impl ChannelManager { } } - let mut rng = thread_rng(); let session_priv = secp_call!(SecretKey::from_slice(&self.secp_ctx, &{ let mut session_key = [0; 32]; - rng.fill_bytes(&mut session_key); + rng::fill_bytes(&mut session_key); session_key })); @@ -1319,8 +1314,7 @@ impl ChannelMessageHandler for ChannelManager { }; if channel_state.forward_htlcs.is_empty() { - let mut rng = thread_rng(); - forward_event = Some(Instant::now() + Duration::from_millis(((rng.next_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64)); + forward_event = Some(Instant::now() + Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64)); channel_state.next_forward = forward_event.unwrap(); } for forward_info in forwarding_infos.drain(..) { diff --git a/src/ln/peer_channel_encryptor.rs b/src/ln/peer_channel_encryptor.rs index e22d8bd35..a20e77a17 100644 --- a/src/ln/peer_channel_encryptor.rs +++ b/src/ln/peer_channel_encryptor.rs @@ -5,8 +5,6 @@ use secp256k1::Secp256k1; use secp256k1::key::{PublicKey,SecretKey}; use secp256k1::ecdh::SharedSecret; -use rand::{thread_rng,Rng}; - use crypto::digest::Digest; use crypto::hkdf::{hkdf_extract,hkdf_expand}; use crypto::sha2::Sha256; @@ -14,7 +12,7 @@ use crypto::sha2::Sha256; use crypto::aead::{AeadEncryptor, AeadDecryptor}; use util::chacha20poly1305rfc::ChaCha20Poly1305RFC; -use util::byte_utils; +use util::{byte_utils,rng}; // Sha256("Noise_XK_secp256k1_ChaChaPoly_SHA256") const NOISE_CK: [u8; 32] = [0x26, 0x40, 0xf5, 0x2e, 0xeb, 0xcd, 0x9e, 0x88, 0x29, 0x58, 0x95, 0x1c, 0x79, 0x42, 0x50, 0xee, 0xdb, 0x28, 0x00, 0x2c, 0x05, 0xd7, 0xdc, 0x2e, 0xa0, 0xf1, 0x95, 0x40, 0x60, 0x42, 0xca, 0xf1]; @@ -75,9 +73,8 @@ pub struct PeerChannelEncryptor { impl PeerChannelEncryptor { pub fn new_outbound(their_node_id: PublicKey) -> PeerChannelEncryptor { - let mut rng = thread_rng(); let mut key = [0u8; 32]; - rng.fill_bytes(&mut key); + rng::fill_bytes(&mut key); let secp_ctx = Secp256k1::new(); let sec_key = SecretKey::from_slice(&secp_ctx, &key).unwrap(); //TODO: nicer rng-is-bad error message @@ -275,9 +272,8 @@ impl PeerChannelEncryptor { pub fn process_act_one_with_key(&mut self, act_one: &[u8], our_node_secret: &SecretKey) -> Result<[u8; 50], HandleError> { assert_eq!(act_one.len(), 50); - let mut rng = thread_rng(); let mut key = [0u8; 32]; - rng.fill_bytes(&mut key); + rng::fill_bytes(&mut key); let our_ephemeral_key = SecretKey::from_slice(&self.secp_ctx, &key).unwrap(); //TODO: nicer rng-is-bad error message self.process_act_one_with_ephemeral_key(act_one, our_node_secret, our_ephemeral_key) } diff --git a/src/util/mod.rs b/src/util/mod.rs index 0e92a2b27..31f4f698e 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -4,6 +4,7 @@ pub mod events; pub(crate) mod byte_utils; pub(crate) mod chacha20poly1305rfc; pub(crate) mod internal_traits; +pub(crate) mod rng; #[cfg(test)] pub(crate) mod test_utils; diff --git a/src/util/rng.rs b/src/util/rng.rs new file mode 100644 index 000000000..f0d449252 --- /dev/null +++ b/src/util/rng.rs @@ -0,0 +1,43 @@ +#[cfg(not(feature = "fuzztarget"))] +mod real_rng { + use rand::{thread_rng,Rng}; + use bitcoin::util::uint::Uint256; + + pub fn fill_bytes(data: &mut [u8]) { + let mut rng = thread_rng(); + rng.fill_bytes(data); + } + + pub fn rand_uint256() -> Uint256 { + let mut rng = thread_rng(); + Uint256([rng.gen(), rng.gen(), rng.gen(), rng.gen()]) + } + + pub fn rand_f32() -> f32 { + let mut rng = thread_rng(); + rng.next_f32() + } +} +#[cfg(not(feature = "fuzztarget"))] +pub use self::real_rng::*; + +#[cfg(feature = "fuzztarget")] +mod fuzzy_rng { + use bitcoin::util::uint::Uint256; + + pub fn fill_bytes(data: &mut [u8]) { + for i in 0..data.len() { + data[i] = 0x42; + } + } + + pub fn rand_uint256() -> Uint256 { + Uint256([0xdeadbeef, 0x1badcafe, 0xbadbeef, 0xdeadcafe]) + } + + pub fn rand_f32() -> f32 { + 0.42 + } +} +#[cfg(feature = "fuzztarget")] +pub use self::fuzzy_rng::*;