From 56b62a319bdbf7680daa6592ec13601c24d8e40a Mon Sep 17 00:00:00 2001 From: eupn <36292692+eupn@users.noreply.github.com> Date: Fri, 31 May 2019 04:16:53 +0700 Subject: [PATCH] Fill BlindingFactor with zeros on Drop (#2847) * Implement simple zeroing of BlindingFactor in Drop * rustfmt * Make Debug implementation for BlindingFactor empty * Implement BlindingFactor zeroing unit test * mnemonic.rs: fix deprecated warning in test_bip39_random test * Use zeroize crate to clear BlindingFactor * Fix comment and implement dummy Debug trait for BlindingFactor * Fix formatter --- Cargo.lock | 22 ++++++++++++++++++++++ core/src/core/block.rs | 10 ++++++---- core/src/core/transaction.rs | 2 +- core/src/libtx/build.rs | 2 +- keychain/Cargo.toml | 1 + keychain/src/mnemonic.rs | 3 ++- keychain/src/types.rs | 36 ++++++++++++++++++++++++++++++++++-- pool/src/pool.rs | 2 +- 8 files changed, 68 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 750dc159..6b47fc0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,6 +798,7 @@ dependencies = [ "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "zeroize 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2631,6 +2632,25 @@ name = "zeroize" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "zeroize" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "zeroize_derive 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "zeroize_derive" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.31 (registry+https://github.com/rust-lang/crates.io-index)", + "synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "zip" version = "0.4.2" @@ -2916,4 +2936,6 @@ dependencies = [ "checksum yaml-rust 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e66366e18dc58b46801afbf2ca7661a9f59cc8c5962c29892b6039b4f86fa992" "checksum yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "65923dd1784f44da1d2c3dbbc5e822045628c590ba72123e1c73d3c230c4434d" "checksum zeroize 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8ddfeb6eee2fb3b262ef6e0898a52b7563bb8e0d5955a313b3cf2f808246ea14" +"checksum zeroize 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b60a6c572b91d8ecb0a460950d84fe5b40699edd07d65f73789b31237afc8f66" +"checksum zeroize_derive 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9dac4b660d969bff9c3fe1847a891cacaa8b21dd5f2aae6e0a3e0975aea96431" "checksum zip 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "36b9e08fb518a65cf7e08a1e482573eb87a2f4f8c6619316612a3c1f162fe822" diff --git a/core/src/core/block.rs b/core/src/core/block.rs index edd41d5e..21e562ef 100644 --- a/core/src/core/block.rs +++ b/core/src/core/block.rs @@ -384,7 +384,7 @@ impl BlockHeader { /// Total kernel offset for the chain state up to and including this block. pub fn total_kernel_offset(&self) -> BlindingFactor { - self.total_kernel_offset + self.total_kernel_offset.clone() } } @@ -560,8 +560,10 @@ impl Block { .with_kernel(reward_kern); // Now add the kernel offset of the previous block for a total - let total_kernel_offset = - committed::sum_kernel_offsets(vec![agg_tx.offset, prev.total_kernel_offset], vec![])?; + let total_kernel_offset = committed::sum_kernel_offsets( + vec![agg_tx.offset.clone(), prev.total_kernel_offset.clone()], + vec![], + )?; let now = Utc::now().timestamp(); let timestamp = DateTime::::from_utc(NaiveDateTime::from_timestamp(now, 0), Utc); @@ -698,7 +700,7 @@ impl Block { // verify.body.outputs and kernel sums let (_utxo_sum, kernel_sum) = self.verify_kernel_sums( self.header.overage(), - self.block_kernel_offset(*prev_kernel_offset)?, + self.block_kernel_offset(prev_kernel_offset.clone())?, )?; Ok(kernel_sum) diff --git a/core/src/core/transaction.rs b/core/src/core/transaction.rs index 8c50cdf8..c1084667 100644 --- a/core/src/core/transaction.rs +++ b/core/src/core/transaction.rs @@ -957,7 +957,7 @@ impl Transaction { ) -> Result<(), Error> { self.body.validate(weighting, verifier)?; self.body.verify_features()?; - self.verify_kernel_sums(self.overage(), self.offset)?; + self.verify_kernel_sums(self.overage(), self.offset.clone())?; Ok(()) } diff --git a/core/src/libtx/build.rs b/core/src/libtx/build.rs index 0930623f..ef2d67c6 100644 --- a/core/src/libtx/build.rs +++ b/core/src/libtx/build.rs @@ -158,7 +158,7 @@ where { Box::new( move |_build, (tx, kern, sum)| -> (Transaction, TxKernel, BlindSum) { - (tx.with_offset(offset), kern, sum) + (tx.with_offset(offset.clone()), kern, sum) }, ) } diff --git a/keychain/Cargo.toml b/keychain/Cargo.toml index f9fbc6fa..8e651ad6 100644 --- a/keychain/Cargo.toml +++ b/keychain/Cargo.toml @@ -19,6 +19,7 @@ serde_derive = "1" serde_json = "1" uuid = { version = "0.6", features = ["serde", "v4"] } lazy_static = "1" +zeroize = "0.8.0" digest = "0.7" hmac = "0.6" diff --git a/keychain/src/mnemonic.rs b/keychain/src/mnemonic.rs index 798af953..47a31bac 100644 --- a/keychain/src/mnemonic.rs +++ b/keychain/src/mnemonic.rs @@ -324,10 +324,11 @@ mod tests { #[test] fn test_bip39_random() { + use rand::seq::SliceRandom; let sizes: [usize; 5] = [16, 20, 24, 28, 32]; let mut rng = thread_rng(); - let size = *rng.choose(&sizes).unwrap(); + let size = *sizes.choose(&mut rng).unwrap(); let mut entropy: Vec = Vec::with_capacity(size); for _ in 0..size { diff --git a/keychain/src/types.rs b/keychain/src/types.rs index 9229d678..aa67e08b 100644 --- a/keychain/src/types.rs +++ b/keychain/src/types.rs @@ -32,6 +32,7 @@ use crate::util::secp::key::{PublicKey, SecretKey}; use crate::util::secp::pedersen::Commitment; use crate::util::secp::{self, Message, Secp256k1, Signature}; use crate::util::static_secp_instance; +use zeroize::Zeroize; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; @@ -227,12 +228,13 @@ impl fmt::Display for Identifier { } } -#[derive(Clone, Copy, PartialEq, Serialize, Deserialize)] +#[derive(Default, Clone, PartialEq, Serialize, Deserialize, Zeroize)] pub struct BlindingFactor([u8; SECRET_KEY_SIZE]); +// Dummy `Debug` implementation that prevents secret leakage. impl fmt::Debug for BlindingFactor { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.to_hex()) + write!(f, "BlindingFactor(