Skip to content

Commit 42b7d21

Browse files
committed
Use os_rng for seed/mnemonic generation
The previously-used `thread_rng` should be fine, but `os_rng` is guaranteed to block until there is sufficient entropy available (e.g., after startup), which might slightly improve security here.
1 parent 423dbd8 commit 42b7d21

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/io/utils.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ use lightning::util::persist::{
3535
};
3636
use lightning::util::ser::{Readable, ReadableArgs, Writeable};
3737
use lightning_types::string::PrintableString;
38-
use rand::{rng, RngCore};
38+
use rand::rngs::OsRng;
39+
use rand::TryRngCore;
3940

4041
use super::*;
4142
use crate::chain::ChainSource;
@@ -63,7 +64,7 @@ pub const EXTERNAL_PATHFINDING_SCORES_CACHE_KEY: &str = "external_pathfinding_sc
6364
pub fn generate_entropy_mnemonic() -> Mnemonic {
6465
// bip39::Mnemonic supports 256 bit entropy max
6566
let mut entropy = [0; 32];
66-
rng().fill_bytes(&mut entropy);
67+
OsRng.try_fill_bytes(&mut entropy).expect("Failed to generate entropy");
6768
Mnemonic::from_entropy(&entropy).unwrap()
6869
}
6970

@@ -96,7 +97,10 @@ where
9697
Ok(key)
9798
} else {
9899
let mut key = [0; WALLET_KEYS_SEED_LEN];
99-
rng().fill_bytes(&mut key);
100+
OsRng.try_fill_bytes(&mut key).map_err(|e| {
101+
log_error!(logger, "Failed to generate entropy: {}", e);
102+
std::io::Error::new(std::io::ErrorKind::Other, "Failed to generate seed bytes")
103+
})?;
100104

101105
if let Some(parent_dir) = Path::new(&keys_seed_path).parent() {
102106
fs::create_dir_all(parent_dir).map_err(|e| {

0 commit comments

Comments
 (0)