Skip to content

Commit cdb2221

Browse files
committed
make decrypt_in_place inout aware
1 parent ee8698a commit cdb2221

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

deoxys/src/modes.rs

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ where
203203
let mut computed_tag = Tag::default();
204204
let mut checksum = Checksum::default();
205205
let mut tweak = Tweak::default();
206+
let buffer: InOutBuf<'_, '_, u8> = buffer.into();
207+
let buffer_len = buffer.len();
206208

207209
// Associated Data
208210
<Self as DeoxysModeInternal<B>>::compute_ad_tag(
@@ -224,61 +226,68 @@ where
224226
if !buffer.is_empty() {
225227
tweak[0] = (tweak[0] & 0xf) | TWEAK_M;
226228

227-
for (index, data) in buffer.chunks_mut(16).enumerate() {
229+
let (data_blocks, tail) = buffer.into_chunks();
230+
let data_blocks_len = data_blocks.len();
231+
232+
for (index, mut data) in data_blocks.into_iter().enumerate() {
228233
// Copy block number
229234
let tmp = tweak[8] & 0xf0;
230235
tweak[8..].copy_from_slice(&(index as u64).to_be_bytes());
231236
tweak[8] = (tweak[8] & 0xf) | tmp;
232237

233-
if data.len() == 16 {
234-
let data: &mut Block = data.try_into().unwrap();
235-
B::decrypt_in_place(data.into(), tweak.as_ref(), subkeys);
238+
B::decrypt_in_place(data.reborrow(), tweak.as_ref(), subkeys);
236239

237-
for (c, d) in checksum.iter_mut().zip(data.iter()) {
238-
*c ^= d;
239-
}
240-
} else {
241-
// Last block checksum
242-
tweak[0] = (tweak[0] & 0xf) | TWEAK_M_LAST;
240+
for (c, d) in checksum.iter_mut().zip(data.get_out().iter()) {
241+
*c ^= d;
242+
}
243+
}
243244

244-
let mut block = Block::default();
245-
B::encrypt_in_place((&mut block).into(), tweak.as_ref(), subkeys);
245+
let mut data = tail;
246+
let index = data_blocks_len;
247+
if !data.is_empty() {
248+
// Copy block number
249+
let tmp = tweak[8] & 0xf0;
250+
tweak[8..].copy_from_slice(&(index as u64).to_be_bytes());
251+
tweak[8] = (tweak[8] & 0xf) | tmp;
246252

247-
for (d, b) in data.iter_mut().zip(block.iter()) {
248-
*d ^= b;
249-
}
253+
// Last block checksum
254+
tweak[0] = (tweak[0] & 0xf) | TWEAK_M_LAST;
255+
256+
let mut block = Block::default();
257+
B::encrypt_in_place((&mut block).into(), tweak.as_ref(), subkeys);
250258

251-
block.fill(0);
259+
data.xor_in2out((block[..data.len()]).into());
252260

253-
block[0..data.len()].copy_from_slice(data);
254-
block[data.len()] = 0x80;
261+
block.fill(0);
255262

256-
for (c, d) in checksum.iter_mut().zip(block.iter()) {
257-
*c ^= d;
258-
}
263+
block[0..data.len()].copy_from_slice(data.get_in());
264+
block[data.len()] = 0x80;
259265

260-
// Tag computing.
261-
tweak[0] = (tweak[0] & 0xf) | TWEAK_CHKSUM;
266+
for (c, d) in checksum.iter_mut().zip(block.iter()) {
267+
*c ^= d;
268+
}
262269

263-
let tmp = tweak[8] & 0xf0;
264-
tweak[8..].copy_from_slice(&((index + 1) as u64).to_be_bytes());
265-
tweak[8] = (tweak[8] & 0xf) | tmp;
270+
// Tag computing.
271+
tweak[0] = (tweak[0] & 0xf) | TWEAK_CHKSUM;
266272

267-
B::encrypt_in_place((&mut checksum).into(), tweak.as_ref(), subkeys);
273+
let tmp = tweak[8] & 0xf0;
274+
tweak[8..].copy_from_slice(&((index + 1) as u64).to_be_bytes());
275+
tweak[8] = (tweak[8] & 0xf) | tmp;
268276

269-
for (t, c) in computed_tag.iter_mut().zip(checksum.iter()) {
270-
*t ^= c;
271-
}
277+
B::encrypt_in_place((&mut checksum).into(), tweak.as_ref(), subkeys);
278+
279+
for (t, c) in computed_tag.iter_mut().zip(checksum.iter()) {
280+
*t ^= c;
272281
}
273282
}
274283
}
275284

276-
if buffer.len() % 16 == 0 {
285+
if buffer_len % 16 == 0 {
277286
// Tag computing without last block
278287
tweak[0] = (tweak[0] & 0xf) | TWEAK_TAG;
279288

280289
let tmp = tweak[8] & 0xf0;
281-
tweak[8..].copy_from_slice(&((buffer.len() / 16) as u64).to_be_bytes());
290+
tweak[8..].copy_from_slice(&((buffer_len / 16) as u64).to_be_bytes());
282291
tweak[8] = (tweak[8] & 0xf) | tmp;
283292

284293
B::encrypt_in_place((&mut checksum).into(), tweak.as_ref(), subkeys);

0 commit comments

Comments
 (0)