Skip to content

Commit

Permalink
ChaCha20: Use ArraySplitMap to construct Iv.
Browse files Browse the repository at this point in the history
This is a step towards eliminating the `unsafe` in `chunks_fixed()`.
  • Loading branch information
briansmith committed Oct 12, 2023
1 parent 0aa8388 commit 5b81ff2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/aead/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ pub struct Iv([u32; 4]);

impl Iv {
fn assume_unique_for_key(value: [u8; 16]) -> Self {
let value: &[[u8; 4]; 4] = value.chunks_fixed();
Self(value.map(u32::from_le_bytes))
Self(value.array_split_map(u32::from_le_bytes))
}

fn into_counter_for_single_block_less_safe(self) -> Counter {
Expand Down
13 changes: 13 additions & 0 deletions src/polyfill/array_split_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ impl<I, O> ArraySplitMap<I, O, 4, 3> for [I; 12] {
]
}
}

impl<I, O> ArraySplitMap<I, O, 4, 4> for [I; 16] {
#[inline]
fn array_split_map(self, f: impl Fn([I; 4]) -> O) -> [O; 4] {
let [a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3, d0, d1, d2, d3] = self;
[
f([a0, a1, a2, a3]),
f([b0, b1, b2, b3]),
f([c0, c1, c2, c3]),
f([d0, d1, d2, d3]),
]
}
}
1 change: 0 additions & 1 deletion src/polyfill/chunks_fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ macro_rules! define_chunks_fixed {
}

// Sorted by the first value, then the second value.
define_chunks_fixed!(16, 4);
define_chunks_fixed!(16, 8);
define_chunks_fixed!(32, 4);
define_chunks_fixed!(64, 4);
Expand Down

0 comments on commit 5b81ff2

Please sign in to comment.