From: Matt Corallo Date: Tue, 20 Mar 2018 00:34:27 +0000 (-0400) Subject: Stub out Sha256 calls when fuzzing X-Git-Tag: v0.0.12~417^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=ab56b81acd9da3a697dfc37fc4cb43cd8fbb1a2c;hp=d0a3d0f728ea8560e7df0d35db116e153b70ddb8;p=rust-lightning Stub out Sha256 calls when fuzzing --- diff --git a/src/ln/chan_utils.rs b/src/ln/chan_utils.rs index dd5515c8..eb85c0e1 100644 --- a/src/ln/chan_utils.rs +++ b/src/ln/chan_utils.rs @@ -7,9 +7,10 @@ use secp256k1::Secp256k1; use secp256k1; use crypto::digest::Digest; -use crypto::sha2::Sha256; use crypto::ripemd160::Ripemd160; +use util::sha2::Sha256; + // Various functions for key derivation and transaction creation for use within channels. Primarily // used in Channel and ChannelMonitor. diff --git a/src/ln/channel.rs b/src/ln/channel.rs index 3c32cd39..6c6dacc8 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -13,7 +13,6 @@ use secp256k1; use crypto::digest::Digest; use crypto::hkdf::{hkdf_extract,hkdf_expand}; -use crypto::sha2::Sha256; use ln::msgs; use ln::msgs::{HandleError, MsgEncodable}; @@ -23,6 +22,7 @@ use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment}; use ln::chan_utils; use chain::chaininterface::{FeeEstimator,ConfirmationTarget}; use util::{transaction_utils,rng}; +use util::sha2::Sha256; use std::default::Default; use std::cmp; diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index b492f3b3..3d70b1d3 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -18,11 +18,11 @@ use ln::router::Route; use ln::msgs; use ln::msgs::{HandleError,ChannelMessageHandler,MsgEncodable,MsgDecodable}; use util::{byte_utils, events, internal_traits, rng}; +use util::sha2::Sha256; use crypto::mac::{Mac,MacResult}; use crypto::hmac::Hmac; use crypto::digest::Digest; -use crypto::sha2::Sha256; use crypto::symmetriccipher::SynchronousStreamCipher; use crypto::chacha20::ChaCha20; diff --git a/src/ln/channelmonitor.rs b/src/ln/channelmonitor.rs index d5529af9..5ac9a6b0 100644 --- a/src/ln/channelmonitor.rs +++ b/src/ln/channelmonitor.rs @@ -4,7 +4,6 @@ use bitcoin::blockdata::script::Script; use bitcoin::util::hash::Sha256dHash; use bitcoin::util::bip143; -use crypto::sha2::Sha256; use crypto::digest::Digest; use secp256k1::{Secp256k1,Message,Signature}; @@ -14,6 +13,7 @@ use ln::msgs::HandleError; use ln::chan_utils; use ln::chan_utils::HTLCOutputInCommitment; use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface}; +use util::sha2::Sha256; use std::collections::HashMap; use std::sync::{Arc,Mutex}; diff --git a/src/ln/peer_channel_encryptor.rs b/src/ln/peer_channel_encryptor.rs index a20e77a1..67b0a14c 100644 --- a/src/ln/peer_channel_encryptor.rs +++ b/src/ln/peer_channel_encryptor.rs @@ -7,12 +7,12 @@ use secp256k1::ecdh::SharedSecret; use crypto::digest::Digest; use crypto::hkdf::{hkdf_extract,hkdf_expand}; -use crypto::sha2::Sha256; use crypto::aead::{AeadEncryptor, AeadDecryptor}; use util::chacha20poly1305rfc::ChaCha20Poly1305RFC; use util::{byte_utils,rng}; +use util::sha2::Sha256; // 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]; diff --git a/src/util/mod.rs b/src/util/mod.rs index 31f4f698..b7578bce 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -5,6 +5,7 @@ pub(crate) mod byte_utils; pub(crate) mod chacha20poly1305rfc; pub(crate) mod internal_traits; pub(crate) mod rng; +pub(crate) mod sha2; #[cfg(test)] pub(crate) mod test_utils; diff --git a/src/util/sha2.rs b/src/util/sha2.rs new file mode 100644 index 00000000..31616f50 --- /dev/null +++ b/src/util/sha2.rs @@ -0,0 +1,37 @@ +#[cfg(not(feature = "fuzztarget"))] +pub use crypto::sha2::Sha256; + +#[cfg(feature = "fuzztarget")] +mod fuzzy_sha { + use crypto::digest::Digest; + use crypto::sha2; + + #[derive(Clone, Copy)] + pub struct Sha256 { + state: sha2::Sha256, + } + + impl Sha256 { + pub fn new() -> Sha256 { + Sha256 { + state: sha2::Sha256::new(), + } + } + } + + impl Digest for Sha256 { + fn result(&mut self, data: &mut [u8]) { + self.state.result(data); + for i in 1..32 { + data[i] = 0; + } + } + + fn input(&mut self, data: &[u8]) { self.state.input(data); } + fn reset(&mut self) { self.state.reset(); } + fn output_bits(&self) -> usize { self.state.output_bits() } + fn block_size(&self) -> usize { self.state.block_size() } + } +} +#[cfg(feature = "fuzztarget")] +pub use self::fuzzy_sha::Sha256;