Skip to content

Commit 12c9fcd

Browse files
committed
Make Poly1305::result return, rather than copy to a slice
`Poly1305::raw_result` copies the output into a slice, for some reason allowing any length sice. This isn't a great API, so while we're here we change it to return the 16-byte tag instead.
1 parent e797180 commit 12c9fcd

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

lightning/src/crypto/chacha20poly1305rfc.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ mod real_chachapoly {
6767
self.finished = true;
6868
self.mac.input(&self.aad_len.to_le_bytes());
6969
self.mac.input(&(self.data_len as u64).to_le_bytes());
70-
self.mac.raw_result(out_tag);
70+
out_tag.copy_from_slice(&self.mac.result());
7171
}
7272

7373
pub fn encrypt_full_message_in_place(
@@ -94,7 +94,7 @@ mod real_chachapoly {
9494
self.finished = true;
9595
self.mac.input(&self.aad_len.to_le_bytes());
9696
self.mac.input(&(self.data_len as u64).to_le_bytes());
97-
self.mac.raw_result(out_tag);
97+
out_tag.copy_from_slice(&self.mac.result());
9898
}
9999

100100
/// Decrypt the `input`, checking the given `tag` prior to writing the decrypted contents
@@ -115,8 +115,7 @@ mod real_chachapoly {
115115
self.mac.input(&self.aad_len.to_le_bytes());
116116
self.mac.input(&(self.data_len as u64).to_le_bytes());
117117

118-
let mut calc_tag = [0u8; 16];
119-
self.mac.raw_result(&mut calc_tag);
118+
let calc_tag = self.mac.result();
120119
if fixed_time_eq(&calc_tag, tag) {
121120
self.cipher.process(input, output);
122121
Ok(())
@@ -156,8 +155,7 @@ mod real_chachapoly {
156155
self.mac.input(&self.aad_len.to_le_bytes());
157156
self.mac.input(&(self.data_len as u64).to_le_bytes());
158157

159-
let mut calc_tag = [0u8; 16];
160-
self.mac.raw_result(&mut calc_tag);
158+
let calc_tag = self.mac.result();
161159
if fixed_time_eq(&calc_tag, tag) {
162160
true
163161
} else {

lightning/src/crypto/poly1305.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,16 @@ impl Poly1305 {
252252
self.leftover = m.len();
253253
}
254254

255-
pub fn raw_result(&mut self, output: &mut [u8]) {
256-
assert!(output.len() >= 16);
255+
pub fn result(&mut self) -> [u8; 16] {
257256
if !self.finalized {
258257
self.finish();
259258
}
259+
let mut output = [0; 16];
260260
output[0..4].copy_from_slice(&self.h[0].to_le_bytes());
261261
output[4..8].copy_from_slice(&self.h[1].to_le_bytes());
262262
output[8..12].copy_from_slice(&self.h[2].to_le_bytes());
263263
output[12..16].copy_from_slice(&self.h[3].to_le_bytes());
264+
output
264265
}
265266
}
266267

@@ -270,10 +271,10 @@ mod test {
270271

271272
use super::Poly1305;
272273

273-
fn poly1305(key: &[u8], msg: &[u8], mac: &mut [u8]) {
274+
fn poly1305(key: &[u8], msg: &[u8], mac: &mut [u8; 16]) {
274275
let mut poly = Poly1305::new(key);
275276
poly.input(msg);
276-
poly.raw_result(mac);
277+
*mac = poly.result();
277278
}
278279

279280
#[test]
@@ -318,7 +319,7 @@ mod test {
318319
poly.input(&msg[128..129]);
319320
poly.input(&msg[129..130]);
320321
poly.input(&msg[130..131]);
321-
poly.raw_result(&mut mac);
322+
let mac = poly.result();
322323
assert_eq!(&mac[..], &expected[..]);
323324
}
324325

@@ -363,7 +364,7 @@ mod test {
363364
poly1305(&key[..], &msg[0..i], &mut mac);
364365
tpoly.input(&mac);
365366
}
366-
tpoly.raw_result(&mut mac);
367+
let mac = tpoly.result();
367368
assert_eq!(&mac[..], &total_mac[..]);
368369
}
369370

0 commit comments

Comments
 (0)