@@ -46,7 +46,6 @@ use crate::ln::script::ShutdownScript;
4646use crate :: prelude:: * ;
4747use core:: convert:: TryInto ;
4848use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
49- use crate :: sync:: Mutex ;
5049use crate :: io:: { self , Error } ;
5150use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
5251use crate :: util:: chacha20:: ChaCha20 ;
@@ -980,7 +979,8 @@ pub struct KeysManager {
980979 channel_master_key : ExtendedPrivKey ,
981980 channel_child_index : AtomicUsize ,
982981
983- chacha : Mutex < ChaCha20 > ,
982+ rand_bytes_unique_start : [ u8 ; 32 ] ,
983+ rand_bytes_index : AtomicUsize ,
984984
985985 seed : [ u8 ; 32 ] ,
986986 starting_time_secs : u64 ,
@@ -1030,10 +1030,9 @@ impl KeysManager {
10301030 let mut inbound_pmt_key_bytes = [ 0 ; 32 ] ;
10311031 inbound_pmt_key_bytes. copy_from_slice ( & inbound_payment_key[ ..] ) ;
10321032
1033- let mut nonce = [ 0u8 ; 12 ] ;
1034- nonce[ ..8 ] . clone_from_slice ( & starting_time_secs. to_be_bytes ( ) ) ;
1035- nonce[ 8 ..12 ] . clone_from_slice ( & starting_time_nanos. to_be_bytes ( ) ) ;
1036- let chacha = Mutex :: new ( ChaCha20 :: new ( seed, & nonce) ) ;
1033+ let mut rand_bytes_unique_start = [ 0u8 ; 32 ] ;
1034+ rand_bytes_unique_start[ ..8 ] . clone_from_slice ( & starting_time_secs. to_be_bytes ( ) ) ;
1035+ rand_bytes_unique_start[ 8 ..12 ] . clone_from_slice ( & starting_time_nanos. to_be_bytes ( ) ) ;
10371036
10381037 let mut res = KeysManager {
10391038 secp_ctx,
@@ -1047,7 +1046,8 @@ impl KeysManager {
10471046 channel_master_key,
10481047 channel_child_index : AtomicUsize :: new ( 0 ) ,
10491048
1050- chacha,
1049+ rand_bytes_unique_start,
1050+ rand_bytes_index : AtomicUsize :: new ( 0 ) ,
10511051
10521052 seed : * seed,
10531053 starting_time_secs,
@@ -1244,11 +1244,10 @@ impl KeysManager {
12441244
12451245impl EntropySource for KeysManager {
12461246 fn get_secure_random_bytes ( & self ) -> [ u8 ; 32 ] {
1247- let mut chacha = self . chacha . lock ( ) . unwrap ( ) ;
1248-
1249- let mut random_bytes = [ 0 ; 32 ] ;
1250- chacha. process_in_place ( & mut random_bytes) ;
1251- random_bytes
1247+ let index = self . rand_bytes_index . fetch_add ( 1 , Ordering :: AcqRel ) ;
1248+ let mut nonce = [ 0u8 ; 16 ] ;
1249+ nonce[ ..8 ] . clone_from_slice ( & index. to_be_bytes ( ) ) ;
1250+ ChaCha20 :: get_single_block ( & self . rand_bytes_unique_start , & nonce)
12521251 }
12531252}
12541253
0 commit comments