Skip to content

Commit 5e34bc4

Browse files
committed
Add an option to in-place decrypt with ChaCha20Poly1305
In the next commit we'll use this to avoid an allocation when deserializing messages from the wire.
1 parent e4c6b70 commit 5e34bc4

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lightning/src/util/chacha20poly1305rfc.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,24 @@ mod real_chachapoly {
122122
}
123123
}
124124

125-
// Decrypt in place, without checking the tag. Use `finish_and_check_tag` to check it
126-
// later when decryption finishes.
127-
//
128-
// Should never be `pub` because the public API should always enforce tag checking.
125+
pub fn check_decrypt_in_place(&mut self, input_output: &mut [u8], tag: &[u8]) -> Result<(), ()> {
126+
self.decrypt_in_place(input_output);
127+
if self.finish_and_check_tag(tag) { Ok(()) } else { Err(()) }
128+
}
129+
130+
/// Decrypt in place, without checking the tag. Use `finish_and_check_tag` to check it
131+
/// later when decryption finishes.
132+
///
133+
/// Should never be `pub` because the public API should always enforce tag checking.
129134
pub(super) fn decrypt_in_place(&mut self, input_output: &mut [u8]) {
130135
debug_assert!(self.finished == false);
131136
self.mac.input(input_output);
132137
self.data_len += input_output.len();
133138
self.cipher.process_in_place(input_output);
134139
}
135140

136-
// If we were previously decrypting with `decrypt_in_place`, this method must be used to finish
137-
// decrypting and check the tag. Returns whether or not the tag is valid.
141+
/// If we were previously decrypting with `just_decrypt_in_place`, this method must be used
142+
/// to check the tag. Returns whether or not the tag is valid.
138143
pub(super) fn finish_and_check_tag(&mut self, tag: &[u8]) -> bool {
139144
debug_assert!(self.finished == false);
140145
self.finished = true;
@@ -313,6 +318,11 @@ mod fuzzy_chachapoly {
313318
true
314319
}
315320

321+
pub fn check_decrypt_in_place(&mut self, input_output: &mut [u8], tag: &[u8]) -> Result<(), ()> {
322+
self.decrypt_in_place(input_output);
323+
if self.finish_and_check_tag(tag) { Ok(()) } else { Err(()) }
324+
}
325+
316326
pub(super) fn decrypt_in_place(&mut self, _input: &mut [u8]) {
317327
assert!(self.finished == false);
318328
}

0 commit comments

Comments
 (0)