Skip to content

Commit c896c0b

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

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

lightning/src/chain/keysinterface.rs

+10-14
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;
@@ -49,6 +48,7 @@ use core::convert::TryInto;
4948
use core::sync::atomic::{AtomicUsize, Ordering};
5049
use crate::io::{self, Error};
5150
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
51+
use crate::util::chacha20::ChaCha20;
5252
use crate::util::invoice::construct_invoice_preimage;
5353

5454
/// Used as initial key material, to be expanded into multiple secret keys (but not to be used
@@ -967,9 +967,7 @@ pub struct KeysManager {
967967
channel_master_key: ExtendedPrivKey,
968968
channel_child_index: AtomicUsize,
969969

970-
rand_bytes_master_key: ExtendedPrivKey,
971-
rand_bytes_child_index: AtomicUsize,
972-
rand_bytes_unique_start: Sha256State,
970+
rand_bytes_index: AtomicUsize,
973971

974972
seed: [u8; 32],
975973
starting_time_secs: u64,
@@ -1015,7 +1013,6 @@ impl KeysManager {
10151013
Err(_) => panic!("Your RNG is busted"),
10161014
};
10171015
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");
10191016
let inbound_payment_key: SecretKey = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(5).unwrap()).expect("Your RNG is busted").private_key;
10201017
let mut inbound_pmt_key_bytes = [0; 32];
10211018
inbound_pmt_key_bytes.copy_from_slice(&inbound_payment_key[..]);
@@ -1037,9 +1034,7 @@ impl KeysManager {
10371034
channel_master_key,
10381035
channel_child_index: AtomicUsize::new(0),
10391036

1040-
rand_bytes_master_key,
1041-
rand_bytes_child_index: AtomicUsize::new(0),
1042-
rand_bytes_unique_start,
1037+
rand_bytes_index: AtomicUsize::new(0),
10431038

10441039
seed: *seed,
10451040
starting_time_secs,
@@ -1236,14 +1231,15 @@ impl KeysManager {
12361231

12371232
impl EntropySource for KeysManager {
12381233
fn get_secure_random_bytes(&self) -> [u8; 32] {
1239-
let mut sha = self.rand_bytes_unique_start.clone();
1234+
let index = self.rand_bytes_index.fetch_add(1, Ordering::AcqRel);
12401235

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[..]);
1236+
let nanos_plus_index = self.starting_time_nanos as u64 + index as u64;
1237+
let nonce: [u8; 16] = [self.starting_time_secs.to_be_bytes(), nanos_plus_index.to_be_bytes()]
1238+
.concat()
1239+
.try_into()
1240+
.unwrap();
12441241

1245-
sha.input(b"Unique Secure Random Bytes Salt");
1246-
Sha256::from_engine(sha).into_inner()
1242+
ChaCha20::get_single_block(&self.seed, &nonce)
12471243
}
12481244
}
12491245

0 commit comments

Comments
 (0)