Skip to content

Commit 6e7ee07

Browse files
authored
cipher: stream cipher improvements (#1388)
1 parent 2b4f648 commit 6e7ee07

File tree

3 files changed

+137
-168
lines changed

3 files changed

+137
-168
lines changed

cipher/src/stream.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,21 @@ macro_rules! impl_seek_num {
202202
{$($t:ty )*} => {
203203
$(
204204
impl SeekNum for $t {
205-
fn from_block_byte<T: Counter>(block: T, byte: u8, bs: u8) -> Result<Self, OverflowError> {
206-
debug_assert!(byte < bs);
207-
let mut block: Self = block.try_into().map_err(|_| OverflowError)?;
208-
if byte != 0 {
209-
block -= 1;
210-
}
211-
let pos = block.checked_mul(bs as Self).ok_or(OverflowError)? + (byte as Self);
212-
Ok(pos)
205+
fn from_block_byte<T: Counter>(block: T, byte: u8, block_size: u8) -> Result<Self, OverflowError> {
206+
debug_assert!(byte != 0);
207+
let rem = block_size.checked_sub(byte).ok_or(OverflowError)?;
208+
let block: Self = block.try_into().map_err(|_| OverflowError)?;
209+
block
210+
.checked_mul(block_size.into())
211+
.and_then(|v| v.checked_sub(rem.into()))
212+
.ok_or(OverflowError)
213213
}
214214

215-
fn into_block_byte<T: Counter>(self, bs: u8) -> Result<(T, u8), OverflowError> {
216-
let bs = bs as Self;
217-
let byte = self % bs;
218-
let block = T::try_from(self/bs).map_err(|_| OverflowError)?;
219-
Ok((block, byte as u8))
215+
fn into_block_byte<T: Counter>(self, block_size: u8) -> Result<(T, u8), OverflowError> {
216+
let bs: Self = block_size.into();
217+
let byte = (self % bs) as u8;
218+
let block = T::try_from(self / bs).map_err(|_| OverflowError)?;
219+
Ok((block, byte))
220220
}
221221
}
222222
)*

cipher/src/stream_core.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{ParBlocks, ParBlocksSizeUser, StreamCipherError};
22
use crypto_common::{
3-
array::{Array, ArraySize},
3+
array::{slice_as_chunks_mut, Array},
44
typenum::Unsigned,
55
Block, BlockSizeUser, BlockSizes,
66
};
@@ -190,27 +190,6 @@ macro_rules! impl_counter {
190190

191191
impl_counter! { u32 u64 u128 }
192192

193-
/// Partition buffer into 2 parts: buffer of arrays and tail.
194-
///
195-
/// In case if `N` is less or equal to 1, buffer of arrays has length
196-
/// of zero and tail is equal to `self`.
197-
#[inline]
198-
fn into_chunks<T, N: ArraySize>(buf: &mut [T]) -> (&mut [Array<T, N>], &mut [T]) {
199-
use core::slice;
200-
if N::USIZE <= 1 {
201-
return (&mut [], buf);
202-
}
203-
let chunks_len = buf.len() / N::USIZE;
204-
let tail_pos = N::USIZE * chunks_len;
205-
let tail_len = buf.len() - tail_pos;
206-
unsafe {
207-
let ptr = buf.as_mut_ptr();
208-
let chunks = slice::from_raw_parts_mut(ptr as *mut Array<T, N>, chunks_len);
209-
let tail = slice::from_raw_parts_mut(ptr.add(tail_pos), tail_len);
210-
(chunks, tail)
211-
}
212-
}
213-
214193
struct WriteBlockCtx<'a, BS: BlockSizes> {
215194
block: &'a mut Block<Self>,
216195
}
@@ -234,7 +213,7 @@ impl<'a, BS: BlockSizes> StreamClosure for WriteBlocksCtx<'a, BS> {
234213
#[inline(always)]
235214
fn call<B: StreamBackend<BlockSize = BS>>(self, backend: &mut B) {
236215
if B::ParBlocksSize::USIZE > 1 {
237-
let (chunks, tail) = into_chunks::<_, B::ParBlocksSize>(self.blocks);
216+
let (chunks, tail) = slice_as_chunks_mut(self.blocks);
238217
for chunk in chunks {
239218
backend.gen_par_ks_blocks(chunk);
240219
}

0 commit comments

Comments
 (0)