@@ -46,9 +46,9 @@ use crate::ln::script::ShutdownScript;
46
46
use crate :: prelude:: * ;
47
47
use core:: convert:: TryInto ;
48
48
use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
49
- use crate :: sync:: Mutex ;
50
49
use crate :: io:: { self , Error } ;
51
50
use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
51
+ use crate :: util:: atomic_counter:: AtomicCounter ;
52
52
use crate :: util:: chacha20:: ChaCha20 ;
53
53
use crate :: util:: invoice:: construct_invoice_preimage;
54
54
@@ -980,7 +980,8 @@ pub struct KeysManager {
980
980
channel_master_key : ExtendedPrivKey ,
981
981
channel_child_index : AtomicUsize ,
982
982
983
- chacha : Mutex < ChaCha20 > ,
983
+ rand_bytes_unique_start : [ u8 ; 32 ] ,
984
+ rand_bytes_index : AtomicCounter ,
984
985
985
986
seed : [ u8 ; 32 ] ,
986
987
starting_time_secs : u64 ,
@@ -1030,10 +1031,11 @@ impl KeysManager {
1030
1031
let mut inbound_pmt_key_bytes = [ 0 ; 32 ] ;
1031
1032
inbound_pmt_key_bytes. copy_from_slice ( & inbound_payment_key[ ..] ) ;
1032
1033
1033
- let mut nonce = [ 0u8 ; 12 ] ;
1034
- nonce[ ..8 ] . copy_from_slice ( & starting_time_secs. to_be_bytes ( ) ) ;
1035
- nonce[ 8 ..12 ] . copy_from_slice ( & starting_time_nanos. to_be_bytes ( ) ) ;
1036
- let chacha = Mutex :: new ( ChaCha20 :: new ( seed, & nonce) ) ;
1034
+ let mut rand_bytes_unique_start = Sha256 :: engine ( ) ;
1035
+ rand_bytes_unique_start. input ( & starting_time_secs. to_be_bytes ( ) ) ;
1036
+ rand_bytes_unique_start. input ( & starting_time_nanos. to_be_bytes ( ) ) ;
1037
+ rand_bytes_unique_start. input ( seed) ;
1038
+ let rand_bytes_unique_start = Sha256 :: from_engine ( rand_bytes_unique_start) . into_inner ( ) ;
1037
1039
1038
1040
let mut res = KeysManager {
1039
1041
secp_ctx,
@@ -1047,7 +1049,8 @@ impl KeysManager {
1047
1049
channel_master_key,
1048
1050
channel_child_index : AtomicUsize :: new ( 0 ) ,
1049
1051
1050
- chacha,
1052
+ rand_bytes_unique_start,
1053
+ rand_bytes_index : AtomicCounter :: new ( ) ,
1051
1054
1052
1055
seed : * seed,
1053
1056
starting_time_secs,
@@ -1244,11 +1247,10 @@ impl KeysManager {
1244
1247
1245
1248
impl EntropySource for KeysManager {
1246
1249
fn get_secure_random_bytes ( & self ) -> [ u8 ; 32 ] {
1247
- let mut chacha = self . chacha . lock ( ) . unwrap ( ) ;
1248
-
1249
- let mut random_bytes = [ 0u8 ; 32 ] ;
1250
- chacha. process_in_place ( & mut random_bytes) ;
1251
- random_bytes
1250
+ let index = self . rand_bytes_index . get_increment ( ) ;
1251
+ let mut nonce = [ 0u8 ; 16 ] ;
1252
+ nonce[ ..8 ] . copy_from_slice ( & index. to_be_bytes ( ) ) ;
1253
+ ChaCha20 :: get_single_block ( & self . rand_bytes_unique_start , & nonce)
1252
1254
}
1253
1255
}
1254
1256
0 commit comments