Skip to content

Commit 6179d29

Browse files
authored
Merge pull request #2 from yancyribbens/add-entropy-bounds-check
Add entropy bounds check
2 parents c4f7a37 + 57c79a3 commit 6179d29

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/lib.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ pub enum Error {
5454
BadWordCount(usize),
5555
/// Mnemonic contains an unknown word.
5656
UnknownWord(String),
57-
/// Entropy was not a multiple of 32 bits.
58-
/// Parameter is the number of bits in the entropy.
57+
/// Entropy was not a multiple of 32 bits or between 128-256n bits in length.
5958
BadEntropyBitCount(usize),
6059
/// The mnemonic has an invalid checksum.
6160
InvalidChecksum,
@@ -72,7 +71,7 @@ impl fmt::Display for Error {
7271
w, bitcoin_hashes::hex::ToHex::to_hex(w.as_bytes()),
7372
),
7473
Error::BadEntropyBitCount(c) => write!(f,
75-
"entropy was not a multiple of 32 bits: {} bits", c,
74+
"entropy was not between 128-256 bits or not a multiple of 32 bits: {} bits", c,
7675
),
7776
Error::InvalidChecksum => write!(f, "the mnemonic has an invalid checksum"),
7877
}
@@ -118,12 +117,16 @@ impl Mnemonic {
118117
}
119118

120119
/// Create a new [Mnemonic] in the specified language from the given entropy.
121-
/// Entropy must be a multiple of 32 bits (4 bytes).
120+
/// Entropy must be a multiple of 32 bits (4 bytes) and 128-256 bits in length.
122121
pub fn from_entropy_in(language: Language, entropy: &[u8]) -> Result<Mnemonic, Error> {
123122
if entropy.len() % 4 != 0 {
124123
return Err(Error::BadEntropyBitCount(entropy.len() * 8));
125124
}
126125

126+
if (entropy.len() * 8) < 128 || (entropy.len() * 8) > 256 {
127+
return Err(Error::BadEntropyBitCount(entropy.len() * 8));
128+
}
129+
127130
let check = sha256::Hash::hash(&entropy);
128131
let mut bits = vec![false; entropy.len() * 8 + entropy.len() / 4];
129132
for i in 0..entropy.len() {
@@ -150,7 +153,7 @@ impl Mnemonic {
150153
}
151154

152155
/// Create a new English [Mnemonic] in from the given entropy.
153-
/// Entropy must be a multiple of 32 bits (4 bytes).
156+
/// Entropy must be a multiple of 32 bits (4 bytes) and 128-256 bits in length.
154157
pub fn from_entropy(entropy: &[u8]) -> Result<Mnemonic, Error> {
155158
Mnemonic::from_entropy_in(Language::English, entropy)
156159
}
@@ -532,6 +535,27 @@ mod tests {
532535
);
533536
}
534537

538+
#[test]
539+
fn test_invalid_entropy() {
540+
//between 128 and 256 bits, but not divisible by 32
541+
assert_eq!(
542+
Mnemonic::from_entropy(&vec![b'x'; 17]),
543+
Err(Error::BadEntropyBitCount(136))
544+
);
545+
546+
//less than 128 bits
547+
assert_eq!(
548+
Mnemonic::from_entropy(&vec![b'x'; 4]),
549+
Err(Error::BadEntropyBitCount(32))
550+
);
551+
552+
//greater than 256 bits
553+
assert_eq!(
554+
Mnemonic::from_entropy(&vec![b'x'; 36]),
555+
Err(Error::BadEntropyBitCount(288))
556+
);
557+
}
558+
535559
#[cfg(feature = "japanese")]
536560
#[test]
537561
fn test_vectors_japanese() {

0 commit comments

Comments
 (0)