Skip to content

Commit

Permalink
feat: securely zero out SecretKeys from memory (#1161)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahmed Sagdati <[email protected]>
  • Loading branch information
Br1ght0ne and segfault-magnet authored Oct 10, 2023
1 parent 91ffd8f commit 2625f90
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ thiserror = { version = "1.0.47", default-features = false }
tokio = { version = "1.31.0", default-features = false }
trybuild = "1.0.82"
which = { version = "4.4.0", default-features = false }
zeroize = "1.6.0"

# Dependencies from the `fuel-core` repository:
fuel-core = { version = "0.20.4", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions packages/fuels-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rand = { workspace = true, default-features = false }
tai64 = { workspace = true, features = ["serde"] }
thiserror = { workspace = true, default-features = false }
tokio = { workspace = true, features = ["full"] }
zeroize = { workspace = true, features = ["derive"] }

[dev-dependencies]
hex = { workspace = true, default-features = false, features = ["std"] }
Expand Down
13 changes: 9 additions & 4 deletions packages/fuels-accounts/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use fuels_core::{
};
use rand::{CryptoRng, Rng};
use thiserror::Error;
use zeroize::{Zeroize, ZeroizeOnDrop};

use crate::{
accounts_utils::{adjust_inputs, adjust_outputs, calculate_base_amount_with_fee},
Expand Down Expand Up @@ -71,8 +72,11 @@ pub struct Wallet {
/// A `WalletUnlocked` is equivalent to a [`Wallet`] whose private key is known and stored
/// alongside in-memory. Knowing the private key allows a `WalletUlocked` to sign operations, send
/// transactions, and more.
#[derive(Clone, Debug)]
///
/// `private_key` will be zeroed out on calling `lock()` or `drop`ping a `WalletUnlocked`.
#[derive(Clone, Debug, Zeroize, ZeroizeOnDrop)]
pub struct WalletUnlocked {
#[zeroize(skip)]
wallet: Wallet,
pub(crate) private_key: SecretKey,
}
Expand Down Expand Up @@ -118,9 +122,10 @@ impl ViewOnlyAccount for Wallet {
}

impl WalletUnlocked {
/// Lock the wallet by `drop`ping the private key from memory.
pub fn lock(self) -> Wallet {
self.wallet
/// Lock the wallet by securely `zeroize`-ing and `drop`ping the private key from memory.
pub fn lock(mut self) -> Wallet {
self.private_key.zeroize();
self.wallet.clone()
}

// NOTE: Rather than providing a `DerefMut` implementation, we wrap the `set_provider` method
Expand Down
1 change: 1 addition & 0 deletions packages/fuels-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ serde_json = { workspace = true, default-features = true }
sha2 = { workspace = true }
thiserror = { workspace = true, default-features = false }
uint = { version = "0.9.5", default-features = false }
zeroize = { workspace = true, features = ["derive"] }

[dev-dependencies]
fuels-macros = { workspace = true }
Expand Down
4 changes: 3 additions & 1 deletion packages/fuels-core/src/types/transaction_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use fuel_tx::{
};
use fuel_types::{bytes::padded_len_usize, Bytes32, ChainId, MemLayout, Salt};
use fuel_vm::{checked_transaction::EstimatePredicates, gas::GasCosts};
use zeroize::{Zeroize, ZeroizeOnDrop};

use super::{chain_info::ChainInfo, node_info::NodeInfo};
use crate::{
Expand Down Expand Up @@ -52,8 +53,9 @@ impl NetworkInfo {
}
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, Zeroize, ZeroizeOnDrop)]
struct UnresolvedSignatures {
#[zeroize(skip)]
addr_idx_offset_map: HashMap<Bech32Address, u8>,
secret_keys: Vec<SecretKey>,
}
Expand Down

0 comments on commit 2625f90

Please sign in to comment.