Skip to content

Commit b4d6f3f

Browse files
f de-DRY ChaChaPoly::encrypt_in_place and remove tag param
1 parent 00c19dd commit b4d6f3f

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

lightning/src/util/chacha20poly1305rfc.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,29 @@ mod real_chachapoly {
6161
}
6262

6363
pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
64+
assert!(input.len() == output.len());
65+
assert!(self.finished == false);
6466
self.cipher.process(input, output);
65-
self.encrypt_inner(input, Some(output), Some(out_tag));
66-
}
67-
68-
pub fn encrypt_in_place(&mut self, input_output: &mut [u8], out_tag: Option<&mut [u8]>) {
69-
self.cipher.process_in_place(input_output);
70-
self.encrypt_inner(input_output, None, out_tag);
67+
self.data_len += input.len();
68+
self.mac.input(output);
69+
ChaCha20Poly1305RFC::pad_mac_16(&mut self.mac, self.data_len);
70+
self.finished = true;
71+
self.mac.input(&self.aad_len.to_le_bytes());
72+
self.mac.input(&(self.data_len as u64).to_le_bytes());
73+
self.mac.raw_result(out_tag);
7174
}
7275

73-
// Encrypt in place, and fill in the tag if it's provided. If the tag is not provided, then
74-
// `finish_and_get_tag` may be called to check it later.
75-
fn encrypt_inner(&mut self, input: &[u8], output: Option<&mut [u8]>, out_tag: Option<&mut [u8]>) {
76+
// ChaCha20Poly1305-encrypt `input_output` in-place. To finish and calculate the tag, use
77+
// `finish_and_get_tag` below.
78+
pub fn encrypt_in_place(&mut self, input_output: &mut [u8]) {
7679
assert!(self.finished == false);
77-
if let Some(output) = output {
78-
assert!(input.len() == output.len());
79-
self.mac.input(output);
80-
} else {
81-
self.mac.input(input);
82-
}
83-
self.data_len += input.len();
84-
if let Some(tag) = out_tag {
85-
self.finish_and_get_tag(tag);
86-
}
80+
self.data_len += input_output.len();
81+
self.cipher.process_in_place(input_output);
82+
self.mac.input(input_output);
8783
}
8884

85+
// If we were previously encrypting with `encrypt_in_place`, this method can be used to finish
86+
// encrypting and calculate the tag.
8987
pub fn finish_and_get_tag(&mut self, out_tag: &mut [u8]) {
9088
ChaCha20Poly1305RFC::pad_mac_16(&mut self.mac, self.data_len);
9189
self.finished = true;
@@ -136,7 +134,7 @@ impl<'a, W: Writer> Writer for ChaChaPolyWriter<'a, W> {
136134
for i in 0..num_writes {
137135
let mut write_buffer = [0; 8192];
138136
let bytes_written = (&mut write_buffer[..]).write(&src[i * 8192..])?;
139-
self.chacha.encrypt_in_place(&mut write_buffer[..bytes_written], None);
137+
self.chacha.encrypt_in_place(&mut write_buffer[..bytes_written]);
140138
self.write.write_all(&write_buffer[..bytes_written])?;
141139
}
142140
Ok(())
@@ -204,7 +202,7 @@ mod fuzzy_chachapoly {
204202
self.finished = true;
205203
}
206204

207-
pub fn encrypt_in_place(&mut self, _input_output: &mut [u8], out_tag: Option<&mut [u8]>) {
205+
pub fn encrypt_in_place(&mut self, _input_output: &mut [u8]) {
208206
assert!(self.finished == false);
209207
if let Some(tag) = out_tag {
210208
tag.copy_from_slice(&self.tag);

0 commit comments

Comments
 (0)