Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mnemonic and seed implement zeroize. #22

Merged
merged 4 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pbkdf2 = { version = "0.4.0", default-features = false }
rand = "0.7.3"
once_cell = { version = "1.4.0", features = [ "parking_lot" ] }
unicode-normalization = "0.1.13"
zeroize = { version = "1.1.1", features = ["zeroize_derive"] }

[dev-dependencies]
hex = "0.4.2"
6 changes: 4 additions & 2 deletions src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::ErrorKind;
use crate::util::{Bits, Bits11};
use failure::Error;
use rustc_hash::FxHashMap;
use zeroize::Zeroize;

pub struct WordMap {
inner: FxHashMap<&'static str, Bits11>,
Expand Down Expand Up @@ -32,7 +33,7 @@ impl WordList {
let count = self.inner[start..].iter()
.take_while(|word| word.starts_with(prefix))
.count();

&self.inner[start..start + count]
}
}
Expand Down Expand Up @@ -113,7 +114,8 @@ mod lazy {
///
/// [Mnemonic]: ./mnemonic/struct.Mnemonic.html
/// [Seed]: ./seed/struct.Seed.html
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Zeroize)]
#[zeroize(drop)]
pub enum Language {
English,
#[cfg(feature = "chinese-simplified")]
Expand Down
16 changes: 11 additions & 5 deletions src/mnemonic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::fmt;
use failure::Error;
use std::mem;
use unicode_normalization::UnicodeNormalization;
use zeroize::Zeroize;
use crate::crypto::{gen_random_bytes, sha256_first_byte};
use crate::error::ErrorKind;
use crate::language::Language;
Expand All @@ -22,6 +24,8 @@ use crate::util::{checksum, BitWriter, IterExt};
/// but beware that the entropy value is **not the same thing** as an HD wallet seed, and should
/// *never* be used that way.
///
/// [`Mnemonic`][Mnemonic] implements [`Zeroize`][Zeroize], so it's bytes will be zeroed when it's dropped.
///
/// [Mnemonic]: ./mnemonic/struct.Mnemonic.html
/// [Mnemonic::new()]: ./mnemonic/struct.Mnemonic.html#method.new
/// [Mnemonic::from_phrase()]: ./mnemonic/struct.Mnemonic.html#method.from_phrase
Expand All @@ -30,7 +34,8 @@ use crate::util::{checksum, BitWriter, IterExt};
/// [Seed::new()]: ./seed/struct.Seed.html#method.new
/// [Seed::as_bytes()]: ./seed/struct.Seed.html#method.as_bytes
///
#[derive(Clone)]
#[derive(Clone, Zeroize)]
#[zeroize(drop)]
pub struct Mnemonic {
phrase: String,
lang: Language,
Expand Down Expand Up @@ -220,10 +225,11 @@ impl Mnemonic {
}

/// Consume the `Mnemonic` and return the phrase as a `String`.
///
/// This operation doesn't perform any allocations.
pub fn into_phrase(self) -> String {
self.phrase
pub fn into_phrase(mut self) -> String {
// Create an empty string and swap values with the mnemonic's phrase.
// This allows `Mnemonic` to implement `Drop`, while still returning the phrase.
mem::replace(&mut self.phrase, String:new());
phrase
}

/// Get the original entropy value of the mnemonic phrase as a slice.
Expand Down
6 changes: 5 additions & 1 deletion src/seed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;
use unicode_normalization::UnicodeNormalization;
use zeroize::Zeroize;
use crate::crypto::pbkdf2;
use crate::mnemonic::Mnemonic;

Expand All @@ -13,11 +14,14 @@ use crate::mnemonic::Mnemonic;
/// HD wallet addresses using another crate (deriving HD wallet addresses is outside the scope of this
/// crate and the BIP39 standard).
///
/// [`Seed`][Seed] implements [`Zeroize`][Zeroize], so it's bytes will be zeroed when it's dropped.
///
/// [Mnemonic]: ./mnemonic/struct.Mnemonic.html
/// [Seed]: ./seed/struct.Seed.html
/// [Seed::as_bytes()]: ./seed/struct.Seed.html#method.as_bytes

#[derive(Clone)]
#[derive(Clone, Zeroize)]
#[zeroize(drop)]
pub struct Seed {
bytes: Vec<u8>,
}
Expand Down