]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Stub out RNG when fuzzing
authorMatt Corallo <git@bluematt.me>
Mon, 19 Mar 2018 19:15:10 +0000 (15:15 -0400)
committerMatt Corallo <git@bluematt.me>
Fri, 23 Mar 2018 17:16:24 +0000 (13:16 -0400)
src/ln/channel.rs
src/ln/channelmanager.rs
src/ln/peer_channel_encryptor.rs
src/util/mod.rs
src/util/rng.rs [new file with mode: 0644]

index 2b8b085a8e4f84979697d9df26fc7515fce2e112..3c32cd39b5d289c6967105a1ce2f0b1e7980352b 100644 (file)
@@ -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!")
index 14e089249cdd37adc089a40b772abbe486301ce0..b492f3b3839823815f33a9aa1f31c6cccf491ac2 100644 (file)
@@ -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(..) {
index e22d8bd3574969cdee2ff1470a1207b5f4607480..a20e77a17f3a7989053de518236245a20df629fd 100644 (file)
@@ -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)
        }
index 0e92a2b27e4804bd7ddb14608c75fe1fddbad798..31f4f698ecdc4c5b7ce75953643a929166b4160a 100644 (file)
@@ -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 (file)
index 0000000..f0d4492
--- /dev/null
@@ -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::*;