Skip to content

Commit 10dfa52

Browse files
committed
review: Remove Option around read_buffer in Decryptor
Clean up based on original review feedback. Option existed to work around borrow issues that were fixed with a small restructure.
1 parent 7b34a08 commit 10dfa52

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

fuzz/src/peer_crypt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ fn do_conduit_tests(generator: &mut FuzzGen, initiator_conduit: &mut Conduit, re
117117
// randomly choose sender of message
118118
let receiver_unencrypted_msg = if generator.generate_bool()? {
119119
let encrypted_msg = initiator_conduit.encrypt(sender_unencrypted_msg);
120-
if let Ok(msg) = responder_conduit.decrypt_single_message(Some(&encrypted_msg)) {
120+
if let Ok(msg) = responder_conduit.decrypt_single_message(&encrypted_msg) {
121121
msg
122122
} else {
123123
assert!(failures_expected);
124124
return Ok(());
125125
}
126126
} else {
127127
let encrypted_msg = responder_conduit.encrypt(sender_unencrypted_msg);
128-
if let Ok(msg) = initiator_conduit.decrypt_single_message(Some(&encrypted_msg)) {
128+
if let Ok(msg) = initiator_conduit.decrypt_single_message(&encrypted_msg) {
129129
msg
130130
} else {
131131
assert!(failures_expected);

lightning/src/ln/peers/conduit.rs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,15 @@ pub(super) struct Decryptor {
3232
receiving_nonce: u32,
3333

3434
pending_message_length: Option<usize>,
35-
read_buffer: Option<Vec<u8>>,
35+
read_buffer: Vec<u8>,
3636
poisoned: bool, // signal an error has occurred so None is returned on iteration after failure
3737
}
3838

3939
impl Iterator for Decryptor {
4040
type Item = Result<Option<Vec<u8>>, String>;
4141

4242
fn next(&mut self) -> Option<Self::Item> {
43-
if self.poisoned {
44-
return None;
45-
}
46-
47-
match self.decrypt_single_message(None) {
43+
match self.decrypt_single_message(&[]) {
4844
Ok(Some(result)) => {
4945
Some(Ok(Some(result)))
5046
},
@@ -72,7 +68,7 @@ impl Conduit {
7268
receiving_key,
7369
receiving_chaining_key: chaining_key,
7470
receiving_nonce: 0,
75-
read_buffer: None,
71+
read_buffer: Vec::new(),
7672
pending_message_length: None,
7773
poisoned: false
7874
}
@@ -92,8 +88,9 @@ impl Conduit {
9288
/// only the first message will be returned, and the rest stored in the internal buffer.
9389
/// If a message pending in the buffer still hasn't been decrypted, that message will be
9490
/// returned in lieu of anything new, even if new data is provided.
91+
/// After a failure, all calls will return Ok(None)
9592
#[cfg(any(test, feature = "fuzztarget"))]
96-
pub fn decrypt_single_message(&mut self, new_data: Option<&[u8]>) -> Result<Option<Vec<u8>>, String> {
93+
pub fn decrypt_single_message(&mut self, new_data: &[u8]) -> Result<Option<Vec<u8>>, String> {
9794
Ok(self.decryptor.decrypt_single_message(new_data)?)
9895
}
9996

@@ -135,44 +132,32 @@ impl Encryptor {
135132

136133
impl Decryptor {
137134
pub(super) fn read(&mut self, data: &[u8]) {
138-
let read_buffer = self.read_buffer.get_or_insert(Vec::new());
139-
read_buffer.extend_from_slice(data);
135+
self.read_buffer.extend(data);
140136
}
141137

142138
/// Decrypt a single message. If data containing more than one message has been received,
143139
/// only the first message will be returned, and the rest stored in the internal buffer.
144140
/// If a message pending in the buffer still hasn't been decrypted, that message will be
145141
/// returned in lieu of anything new, even if new data is provided.
146-
pub fn decrypt_single_message(&mut self, new_data: Option<&[u8]>) -> Result<Option<Vec<u8>>, String> {
147-
let mut read_buffer = if let Some(buffer) = self.read_buffer.take() {
148-
buffer
149-
} else {
150-
Vec::new()
151-
};
152-
153-
if let Some(data) = new_data {
154-
read_buffer.extend_from_slice(data);
142+
/// After a failure, all calls will return Ok(None)
143+
pub fn decrypt_single_message(&mut self, new_data: &[u8]) -> Result<Option<Vec<u8>>, String> {
144+
if self.poisoned {
145+
return Ok(None);
155146
}
156147

157-
let (current_message, offset) = self.decrypt(&read_buffer[..])?;
158-
read_buffer.drain(..offset); // drain the read buffer
159-
self.read_buffer = Some(read_buffer); // assign the new value to the built-in buffer
160-
Ok(current_message)
161-
}
148+
self.read(new_data);
162149

163-
fn decrypt(&mut self, buffer: &[u8]) -> Result<(Option<Vec<u8>>, usize), String> {
164150
let message_length = if let Some(length) = self.pending_message_length {
165151
// we have already decrypted the header
166152
length
167153
} else {
168-
if buffer.len() < TAGGED_MESSAGE_LENGTH_HEADER_SIZE {
154+
if self.read_buffer.len() < TAGGED_MESSAGE_LENGTH_HEADER_SIZE {
169155
// A message must be at least 18 bytes (2 for encrypted length, 16 for the tag)
170-
return Ok((None, 0));
156+
return Ok(None);
171157
}
172158

173-
let encrypted_length = &buffer[0..TAGGED_MESSAGE_LENGTH_HEADER_SIZE];
174159
let mut length_bytes = [0u8; MESSAGE_LENGTH_HEADER_SIZE];
175-
chacha::decrypt(&self.receiving_key, self.receiving_nonce as u64, &[0; 0], encrypted_length, &mut length_bytes)?;
160+
chacha::decrypt(&self.receiving_key, self.receiving_nonce as u64, &[0; 0], &self.read_buffer[..TAGGED_MESSAGE_LENGTH_HEADER_SIZE], &mut length_bytes)?;
176161

177162
self.increment_nonce();
178163

@@ -182,21 +167,22 @@ impl Decryptor {
182167

183168
let message_end_index = TAGGED_MESSAGE_LENGTH_HEADER_SIZE + message_length + chacha::TAG_SIZE;
184169

185-
if buffer.len() < message_end_index {
170+
if self.read_buffer.len() < message_end_index {
186171
self.pending_message_length = Some(message_length);
187-
return Ok((None, 0));
172+
return Ok(None);
188173
}
189174

190175
self.pending_message_length = None;
191176

192-
let encrypted_message = &buffer[TAGGED_MESSAGE_LENGTH_HEADER_SIZE..message_end_index];
193177
let mut message = vec![0u8; message_length];
194178

195-
chacha::decrypt(&self.receiving_key, self.receiving_nonce as u64, &[0; 0], encrypted_message, &mut message)?;
179+
chacha::decrypt(&self.receiving_key, self.receiving_nonce as u64, &[0; 0], &self.read_buffer[TAGGED_MESSAGE_LENGTH_HEADER_SIZE..message_end_index], &mut message)?;
196180

197181
self.increment_nonce();
198182

199-
Ok((Some(message), message_end_index))
183+
self.read_buffer.drain(..message_end_index);
184+
185+
Ok(Some(message))
200186
}
201187

202188
fn increment_nonce(&mut self) {
@@ -207,10 +193,7 @@ impl Decryptor {
207193
// infrastructure to properly encode it
208194
#[cfg(test)]
209195
pub fn read_buffer_length(&self) -> usize {
210-
match &self.read_buffer {
211-
&Some(ref vec) => { vec.len() }
212-
&None => 0
213-
}
196+
self.read_buffer.len()
214197
}
215198
}
216199

@@ -247,7 +230,7 @@ mod tests {
247230
let encrypted_message = connected_peer.encrypt(&message);
248231
assert_eq!(encrypted_message.len(), 2 + 16 + 16);
249232

250-
let decrypted_message = remote_peer.decrypt_single_message(Some(&encrypted_message)).unwrap().unwrap();
233+
let decrypted_message = remote_peer.decrypt_single_message(&encrypted_message).unwrap().unwrap();
251234
assert_eq!(decrypted_message, vec![]);
252235
}
253236

@@ -300,13 +283,13 @@ mod tests {
300283
let mut current_encrypted_message = encrypted_messages.remove(0);
301284
let next_encrypted_message = encrypted_messages.remove(0);
302285
current_encrypted_message.extend_from_slice(&next_encrypted_message);
303-
let decrypted_message = remote_peer.decrypt_single_message(Some(&current_encrypted_message)).unwrap().unwrap();
286+
let decrypted_message = remote_peer.decrypt_single_message(&current_encrypted_message).unwrap().unwrap();
304287
assert_eq!(decrypted_message, message);
305288
}
306289

307290
for _ in 0..501 {
308291
// decrypt messages directly from buffer without adding to it
309-
let decrypted_message = remote_peer.decrypt_single_message(None).unwrap().unwrap();
292+
let decrypted_message = remote_peer.decrypt_single_message(&[]).unwrap().unwrap();
310293
assert_eq!(decrypted_message, message);
311294
}
312295
}
@@ -318,7 +301,7 @@ mod tests {
318301
let encrypted = remote_peer.encrypt(&[1]);
319302

320303
connected_peer.decryptor.receiving_key = [0; 32];
321-
assert_eq!(connected_peer.decrypt_single_message(Some(&encrypted)), Err("invalid hmac".to_string()));
304+
assert_eq!(connected_peer.decrypt_single_message(&encrypted), Err("invalid hmac".to_string()));
322305
}
323306

324307
// Test next()::None

0 commit comments

Comments
 (0)