Skip to content

Commit

Permalink
AEAD: Remove all use of ring::endian and u64 from Block.
Browse files Browse the repository at this point in the history
In particular, eliminate the use of `ArrayEncoding::as_byte_array` as
we work towards removing that function because it uses `unsafe`.

Where `Block` is used in parameters to C/assembly code, I verified that
the C/assembly code uses `uint8_t *` as the function type (meaning
`uint8_t[16]`) in the BoringSSL headers.
  • Loading branch information
briansmith committed Oct 11, 2023
1 parent 797a6ee commit facdfc7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
13 changes: 7 additions & 6 deletions src/aead/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use crate::{endian::*, polyfill::ChunksFixed};
use core::ops::{BitXor, BitXorAssign};

// TODO: Alignment?
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct Block([BigEndian<u64>; 2]);
pub struct Block([u8; 16]);

pub const BLOCK_LEN: usize = 16;

impl Block {
#[inline]
pub fn zero() -> Self {
Self([Encoding::ZERO; 2])
Self([0; 16])
}

#[inline]
Expand All @@ -45,6 +45,8 @@ impl Block {
impl BitXorAssign for Block {
#[inline]
fn bitxor_assign(&mut self, a: Self) {
// Relies heavily on optimizer to optimize this into word- or vector-
// level XOR.
for (r, a) in self.0.iter_mut().zip(a.0.iter()) {
*r ^= *a;
}
Expand All @@ -65,14 +67,13 @@ impl BitXor for Block {
impl From<&'_ [u8; BLOCK_LEN]> for Block {
#[inline]
fn from(bytes: &[u8; BLOCK_LEN]) -> Self {
let bytes: &[[u8; BLOCK_LEN / 2]; 2] = bytes.chunks_fixed();
Self(bytes.map(Into::into))
Self(*bytes)
}
}

impl AsRef<[u8; BLOCK_LEN]> for Block {
#[inline]
fn as_ref(&self) -> &[u8; BLOCK_LEN] {
self.0.as_byte_array()
&self.0
}
}
10 changes: 0 additions & 10 deletions src/endian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ macro_rules! define_endian {
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct $endian<T>(T);

impl<T> core::ops::BitXorAssign for $endian<T>
where
T: core::ops::BitXorAssign,
{
#[inline(always)]
fn bitxor_assign(&mut self, a: Self) {
self.0 ^= a.0;
}
}
};
}

Expand Down

0 comments on commit facdfc7

Please sign in to comment.