@@ -13,7 +13,7 @@ use crate::io::Write;
1313use crate :: ln:: msgs;
1414use crate :: ln:: msgs:: LightningError ;
1515use crate :: ln:: wire;
16- use crate :: sign:: { NodeSigner , Recipient } ;
16+ use crate :: sign:: { EntropySource , NodeSigner , RandomBytes , Recipient } ;
1717
1818use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
1919use bitcoin:: hashes:: { Hash , HashEngine } ;
@@ -106,8 +106,8 @@ enum NoiseState {
106106
107107pub struct PeerChannelEncryptor {
108108 their_node_id : Option < PublicKey > , // filled in for outbound, or inbound after noise_state is Finished
109-
110109 noise_state : NoiseState ,
110+ padding_entropy_source : RandomBytes ,
111111}
112112
113113impl PeerChannelEncryptor {
@@ -119,13 +119,20 @@ impl PeerChannelEncryptor {
119119 sha. input ( & their_node_id. serialize ( ) [ ..] ) ;
120120 let h = Sha256 :: from_engine ( sha) . to_byte_array ( ) ;
121121
122+ let mut padding_seed_engine = Sha256 :: engine ( ) ;
123+ padding_seed_engine. input ( b"LDK MESSAGE PADDING" ) ;
124+ padding_seed_engine. input ( & h) ;
125+ let padding_seed = Sha256 :: from_engine ( padding_seed_engine) . to_byte_array ( ) ;
126+ let padding_entropy_source = RandomBytes :: new ( padding_seed) ;
127+
122128 PeerChannelEncryptor {
123129 their_node_id : Some ( their_node_id) ,
124130 noise_state : NoiseState :: InProgress {
125131 state : NoiseStep :: PreActOne ,
126132 directional_state : DirectionalNoiseState :: Outbound { ie : ephemeral_key } ,
127133 bidirectional_state : BidirectionalNoiseState { h, ck : NOISE_CK } ,
128134 } ,
135+ padding_entropy_source,
129136 }
130137 }
131138
@@ -139,6 +146,12 @@ impl PeerChannelEncryptor {
139146 sha. input ( & our_node_id. serialize ( ) [ ..] ) ;
140147 let h = Sha256 :: from_engine ( sha) . to_byte_array ( ) ;
141148
149+ let mut padding_seed_engine = Sha256 :: engine ( ) ;
150+ padding_seed_engine. input ( b"LDK MESSAGE PADDING" ) ;
151+ padding_seed_engine. input ( & h) ;
152+ let padding_seed = Sha256 :: from_engine ( padding_seed_engine) . to_byte_array ( ) ;
153+ let padding_entropy_source = RandomBytes :: new ( padding_seed) ;
154+
142155 PeerChannelEncryptor {
143156 their_node_id : None ,
144157 noise_state : NoiseState :: InProgress {
@@ -150,6 +163,7 @@ impl PeerChannelEncryptor {
150163 } ,
151164 bidirectional_state : BidirectionalNoiseState { h, ck : NOISE_CK } ,
152165 } ,
166+ padding_entropy_source,
153167 }
154168 }
155169
@@ -599,7 +613,7 @@ impl PeerChannelEncryptor {
599613 while bytes_written < padding_len {
600614 // Write padding in 32-byte chunks if possible.
601615 const PAD_BYTES_LEN : usize = 32 ;
602- let pad_bytes = [ 42u8 ; PAD_BYTES_LEN ] ;
616+ let pad_bytes = self . padding_entropy_source . get_secure_random_bytes ( ) ;
603617 let bytes_to_write = ( padding_len - bytes_written) . min ( PAD_BYTES_LEN ) ;
604618 buffer
605619 . write_all ( & pad_bytes[ ..bytes_to_write] )
0 commit comments