Skip to content

Commit 5052f4d

Browse files
committed
Randomize padding bytes
While it shouldn't really make any difference for the Noise protocol, we here avoid taking any chances w.r.t. known plaintext attacks and opt to randomize the padding data.
1 parent cf70faf commit 5052f4d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lightning/src/ln/peer_channel_encryptor.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::io::Write;
1313
use crate::ln::msgs;
1414
use crate::ln::msgs::LightningError;
1515
use crate::ln::wire;
16-
use crate::sign::{NodeSigner, Recipient};
16+
use crate::sign::{EntropySource, NodeSigner, RandomBytes, Recipient};
1717

1818
use bitcoin::hashes::sha256::Hash as Sha256;
1919
use bitcoin::hashes::{Hash, HashEngine};
@@ -106,8 +106,8 @@ enum NoiseState {
106106

107107
pub 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

113113
impl 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

Comments
 (0)