Skip to content

Commit d3f14e7

Browse files
committed
Use chacha in get_secure_random_bytes()
1 parent 153b048 commit d3f14e7

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use bitcoin::util::sighash;
2121

2222
use bitcoin::bech32::u5;
2323
use bitcoin::hashes::{Hash, HashEngine};
24-
use bitcoin::hashes::sha256::HashEngine as Sha256State;
2524
use bitcoin::hashes::sha256::Hash as Sha256;
2625
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
2726
use bitcoin::hash_types::WPubkeyHash;
@@ -47,8 +46,10 @@ use crate::ln::script::ShutdownScript;
4746
use crate::prelude::*;
4847
use core::convert::TryInto;
4948
use core::sync::atomic::{AtomicUsize, Ordering};
49+
use std::sync::Mutex;
5050
use crate::io::{self, Error};
5151
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
52+
use crate::util::chacha20::ChaCha20;
5253
use crate::util::invoice::construct_invoice_preimage;
5354

5455
/// Used as initial key material, to be expanded into multiple secret keys (but not to be used
@@ -967,9 +968,7 @@ pub struct KeysManager {
967968
channel_master_key: ExtendedPrivKey,
968969
channel_child_index: AtomicUsize,
969970

970-
rand_bytes_master_key: ExtendedPrivKey,
971-
rand_bytes_child_index: AtomicUsize,
972-
rand_bytes_unique_start: Sha256State,
971+
chacha: Mutex<ChaCha20>,
973972

974973
seed: [u8; 32],
975974
starting_time_secs: u64,
@@ -1015,15 +1014,14 @@ impl KeysManager {
10151014
Err(_) => panic!("Your RNG is busted"),
10161015
};
10171016
let channel_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(3).unwrap()).expect("Your RNG is busted");
1018-
let rand_bytes_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(4).unwrap()).expect("Your RNG is busted");
10191017
let inbound_payment_key: SecretKey = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(5).unwrap()).expect("Your RNG is busted").private_key;
10201018
let mut inbound_pmt_key_bytes = [0; 32];
10211019
inbound_pmt_key_bytes.copy_from_slice(&inbound_payment_key[..]);
10221020

1023-
let mut rand_bytes_unique_start = Sha256::engine();
1024-
rand_bytes_unique_start.input(&starting_time_secs.to_be_bytes());
1025-
rand_bytes_unique_start.input(&starting_time_nanos.to_be_bytes());
1026-
rand_bytes_unique_start.input(seed);
1021+
let mut nonce = Vec::new();
1022+
nonce.append(&mut starting_time_secs.to_be_bytes().to_vec());
1023+
nonce.append(&mut starting_time_nanos.to_be_bytes().to_vec());
1024+
let chacha = Mutex::new(ChaCha20::new(seed, &nonce));
10271025

10281026
let mut res = KeysManager {
10291027
secp_ctx,
@@ -1037,9 +1035,7 @@ impl KeysManager {
10371035
channel_master_key,
10381036
channel_child_index: AtomicUsize::new(0),
10391037

1040-
rand_bytes_master_key,
1041-
rand_bytes_child_index: AtomicUsize::new(0),
1042-
rand_bytes_unique_start,
1038+
chacha,
10431039

10441040
seed: *seed,
10451041
starting_time_secs,
@@ -1236,14 +1232,11 @@ impl KeysManager {
12361232

12371233
impl EntropySource for KeysManager {
12381234
fn get_secure_random_bytes(&self) -> [u8; 32] {
1239-
let mut sha = self.rand_bytes_unique_start.clone();
1235+
let mut chacha = self.chacha.lock().unwrap();
12401236

1241-
let child_ix = self.rand_bytes_child_index.fetch_add(1, Ordering::AcqRel);
1242-
let child_privkey = self.rand_bytes_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted");
1243-
sha.input(&child_privkey.private_key[..]);
1244-
1245-
sha.input(b"Unique Secure Random Bytes Salt");
1246-
Sha256::from_engine(sha).into_inner()
1237+
let mut random_bytes = [0; 32];
1238+
chacha.process_in_place(&mut random_bytes);
1239+
random_bytes
12471240
}
12481241
}
12491242

0 commit comments

Comments
 (0)