Skip to content

Commit

Permalink
[WIP] Migrate to hybrid-array; MSRV 1.65
Browse files Browse the repository at this point in the history
Builds on RustCrypto/traits#1319.

Migrates the following crates away from using `generic-array` to using
`hybrid-array` instead:

- `block-buffer`
- `block-padding`
- `dbl`
- `inout`
  • Loading branch information
tarcieri committed Sep 5, 2023
1 parent 1f995e9 commit 28eeaf7
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 71 deletions.
29 changes: 6 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions block-padding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "block-padding"
version = "0.3.3"
version = "0.4.0-pre"
description = "Padding and unpadding of messages divided into blocks."
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
Expand All @@ -12,7 +12,7 @@ keywords = ["padding", "pkcs7", "ansix923", "iso7816"]
categories = ["cryptography", "no-std"]

[dependencies]
hybrid-array = "=0.2.0-pre.2"
hybrid-array = "=0.2.0-pre.3"

[features]
std = []
Expand Down
6 changes: 4 additions & 2 deletions dbl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[package]
name = "dbl"
version = "0.3.2"
version = "0.4.0-pre"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
description = "Double operation in Galois Field (GF)"
documentation = "https://docs.rs/dbl"
repository = "https://github.com/RustCrypto/utils"
keywords = ["crypto", "dbl", "gf", "galois"]
edition = "2021"
rust-version = "1.65"

[dependencies]
generic-array = "0.14"
hybrid-array = "=0.2.0-pre.3"
12 changes: 5 additions & 7 deletions dbl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
)]
#![forbid(unsafe_code)]

extern crate generic_array;

use generic_array::typenum::{U16, U32, U8};
use generic_array::GenericArray;
use hybrid_array::typenum::{U16, U32, U8};
use hybrid_array::Array;

use core::convert::TryInto;

Expand Down Expand Up @@ -39,7 +37,7 @@ pub trait Dbl {
fn inv_dbl(self) -> Self;
}

impl Dbl for GenericArray<u8, U8> {
impl Dbl for Array<u8, U8> {
#[inline]
fn dbl(self) -> Self {
let mut val = u64::from_be_bytes(self.into());
Expand All @@ -63,7 +61,7 @@ impl Dbl for GenericArray<u8, U8> {
}
}

impl Dbl for GenericArray<u8, U16> {
impl Dbl for Array<u8, U16> {
#[inline]
fn dbl(self) -> Self {
let mut val = [
Expand Down Expand Up @@ -108,7 +106,7 @@ impl Dbl for GenericArray<u8, U16> {
}
}

impl Dbl for GenericArray<u8, U32> {
impl Dbl for Array<u8, U32> {
#[inline]
fn dbl(self) -> Self {
let mut val = [
Expand Down
6 changes: 6 additions & 0 deletions hybrid-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,12 @@ macro_rules! impl_array_size {
type ArrayType<T> = [T; $len];
}

impl<T> From<Array<T, typenum::$ty>> for [T; $len] {
fn from(arr: Array<T, typenum::$ty>) -> [T; $len] {
arr.0
}
}

impl<T> IntoArray<T> for [T; $len] {
type Size = typenum::$ty;

Expand Down
6 changes: 3 additions & 3 deletions inout/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "inout"
version = "0.1.3"
version = "0.2.0-pre"
description = "Custom reference types for code generic over in-place and buffer-to-buffer modes of operation."
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
Expand All @@ -11,8 +11,8 @@ repository = "https://github.com/RustCrypto/utils"
keywords = ["custom-reference"]

[dependencies]
generic-array = "0.14"
block-padding = { version = "0.3", path = "../block-padding", optional = true }
block-padding = { version = "0.4.0-pre", path = "../block-padding", optional = true }
hybrid-array = "=0.2.0-pre.3"

[features]
std = ["block-padding/std"]
Expand Down
20 changes: 10 additions & 10 deletions inout/src/inout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::InOutBuf;
use core::{marker::PhantomData, ptr};
use generic_array::{ArrayLength, GenericArray};
use hybrid_array::{Array, ArraySize};

/// Custom pointer type which contains one immutable (input) and one mutable
/// (output) pointer, which are either equal or non-overlapping.
Expand Down Expand Up @@ -98,7 +98,7 @@ impl<'inp, 'out, T> From<(&'inp T, &'out mut T)> for InOut<'inp, 'out, T> {
}
}

impl<'inp, 'out, T, N: ArrayLength<T>> InOut<'inp, 'out, GenericArray<T, N>> {
impl<'inp, 'out, T, N: ArraySize> InOut<'inp, 'out, Array<T, N>> {
/// Returns `InOut` for the given position.
///
/// # Panics
Expand Down Expand Up @@ -127,18 +127,18 @@ impl<'inp, 'out, T, N: ArrayLength<T>> InOut<'inp, 'out, GenericArray<T, N>> {
}
}

impl<'inp, 'out, N: ArrayLength<u8>> InOut<'inp, 'out, GenericArray<u8, N>> {
impl<'inp, 'out, N: ArraySize> InOut<'inp, 'out, Array<u8, N>> {
/// XOR `data` with values behind the input slice and write
/// result to the output slice.
///
/// # Panics
/// If `data` length is not equal to the buffer length.
#[inline(always)]
#[allow(clippy::needless_range_loop)]
pub fn xor_in2out(&mut self, data: &GenericArray<u8, N>) {
pub fn xor_in2out(&mut self, data: &Array<u8, N>) {
unsafe {
let input = ptr::read(self.in_ptr);
let mut temp = GenericArray::<u8, N>::default();
let mut temp = Array::<u8, N>::default();
for i in 0..N::USIZE {
temp[i] = input[i] ^ data[i];
}
Expand All @@ -147,10 +147,10 @@ impl<'inp, 'out, N: ArrayLength<u8>> InOut<'inp, 'out, GenericArray<u8, N>> {
}
}

impl<'inp, 'out, N, M> InOut<'inp, 'out, GenericArray<GenericArray<u8, N>, M>>
impl<'inp, 'out, N, M> InOut<'inp, 'out, Array<Array<u8, N>, M>>
where
N: ArrayLength<u8>,
M: ArrayLength<GenericArray<u8, N>>,
N: ArraySize,
M: ArraySize,
{
/// XOR `data` with values behind the input slice and write
/// result to the output slice.
Expand All @@ -159,10 +159,10 @@ where
/// If `data` length is not equal to the buffer length.
#[inline(always)]
#[allow(clippy::needless_range_loop)]
pub fn xor_in2out(&mut self, data: &GenericArray<GenericArray<u8, N>, M>) {
pub fn xor_in2out(&mut self, data: &Array<Array<u8, N>, M>) {
unsafe {
let input = ptr::read(self.in_ptr);
let mut temp = GenericArray::<GenericArray<u8, N>, M>::default();
let mut temp = Array::<Array<u8, N>, M>::default();
for i in 0..M::USIZE {
for j in 0..N::USIZE {
temp[i][j] = input[i][j] ^ data[i][j];
Expand Down
19 changes: 8 additions & 11 deletions inout/src/inout_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
InOut,
};
use core::{marker::PhantomData, slice};
use generic_array::{ArrayLength, GenericArray};
use hybrid_array::{Array, ArraySize};

/// Custom slice type which references one immutable (input) slice and one
/// mutable (output) slice of equal length. Input and output slices are
Expand Down Expand Up @@ -209,19 +209,16 @@ impl<'inp, 'out, T> InOutBuf<'inp, 'out, T> {

/// Partition buffer into 2 parts: buffer of arrays and tail.
#[inline(always)]
pub fn into_chunks<N: ArrayLength<T>>(
pub fn into_chunks<N: ArraySize>(
self,
) -> (
InOutBuf<'inp, 'out, GenericArray<T, N>>,
InOutBuf<'inp, 'out, T>,
) {
) -> (InOutBuf<'inp, 'out, Array<T, N>>, InOutBuf<'inp, 'out, T>) {
let chunks = self.len() / N::USIZE;
let tail_pos = N::USIZE * chunks;
let tail_len = self.len() - tail_pos;
unsafe {
let chunks = InOutBuf {
in_ptr: self.in_ptr as *const GenericArray<T, N>,
out_ptr: self.out_ptr as *mut GenericArray<T, N>,
in_ptr: self.in_ptr as *const Array<T, N>,
out_ptr: self.out_ptr as *mut Array<T, N>,
len: chunks,
_pd: PhantomData,
};
Expand Down Expand Up @@ -256,14 +253,14 @@ impl<'inp, 'out> InOutBuf<'inp, 'out, u8> {
}
}

impl<'inp, 'out, T, N> TryInto<InOut<'inp, 'out, GenericArray<T, N>>> for InOutBuf<'inp, 'out, T>
impl<'inp, 'out, T, N> TryInto<InOut<'inp, 'out, Array<T, N>>> for InOutBuf<'inp, 'out, T>
where
N: ArrayLength<T>,
N: ArraySize,
{
type Error = IntoArrayError;

#[inline(always)]
fn try_into(self) -> Result<InOut<'inp, 'out, GenericArray<T, N>>, Self::Error> {
fn try_into(self) -> Result<InOut<'inp, 'out, Array<T, N>>, Self::Error> {
if self.len() == N::USIZE {
Ok(InOut {
in_ptr: self.in_ptr as *const _,
Expand Down
26 changes: 13 additions & 13 deletions inout/src/reserved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{InOut, InOutBuf};
#[cfg(feature = "block-padding")]
use block_padding::{PadType, Padding};
#[cfg(feature = "block-padding")]
use generic_array::{ArrayLength, GenericArray};
use hybrid_array::{Array, ArraySize};

/// Custom slice type which references one immutable (input) slice and one
/// mutable (output) slice. Input and output slices are either the same or
Expand Down Expand Up @@ -134,19 +134,19 @@ impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> {
pub fn into_padded_blocks<P, BS>(self) -> Result<PaddedInOutBuf<'inp, 'out, BS>, PadError>
where
P: Padding<BS>,
BS: ArrayLength<u8>,
BS: ArraySize,
{
let bs = BS::USIZE;
let blocks_len = self.in_len / bs;
let tail_len = self.in_len - bs * blocks_len;
let blocks = unsafe {
InOutBuf::from_raw(
self.in_ptr as *const GenericArray<u8, BS>,
self.out_ptr as *mut GenericArray<u8, BS>,
self.in_ptr as *const Array<u8, BS>,
self.out_ptr as *mut Array<u8, BS>,
blocks_len,
)
};
let mut tail_in = GenericArray::<u8, BS>::default();
let mut tail_in = Array::<u8, BS>::default();
let tail_out = match P::TYPE {
PadType::NoPadding | PadType::Ambiguous if tail_len == 0 => None,
PadType::NoPadding => return Err(PadError),
Expand All @@ -167,7 +167,7 @@ impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> {
tail_in.as_mut_ptr(),
tail_len,
);
&mut *(self.out_ptr.add(blen) as *mut GenericArray<u8, BS>)
&mut *(self.out_ptr.add(blen) as *mut Array<u8, BS>)
};
P::pad(&mut tail_in, tail_len);
Some(out_block)
Expand All @@ -184,17 +184,17 @@ impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> {
/// Variant of [`InOutBuf`] with optional padded tail block.
#[cfg(feature = "block-padding")]
#[cfg_attr(docsrs, doc(cfg(feature = "block-padding")))]
pub struct PaddedInOutBuf<'inp, 'out, BS: ArrayLength<u8>> {
blocks: InOutBuf<'inp, 'out, GenericArray<u8, BS>>,
tail_in: GenericArray<u8, BS>,
tail_out: Option<&'out mut GenericArray<u8, BS>>,
pub struct PaddedInOutBuf<'inp, 'out, BS: ArraySize> {
blocks: InOutBuf<'inp, 'out, Array<u8, BS>>,
tail_in: Array<u8, BS>,
tail_out: Option<&'out mut Array<u8, BS>>,
}

#[cfg(feature = "block-padding")]
impl<'inp, 'out, BS: ArrayLength<u8>> PaddedInOutBuf<'inp, 'out, BS> {
impl<'inp, 'out, BS: ArraySize> PaddedInOutBuf<'inp, 'out, BS> {
/// Get full blocks.
#[inline(always)]
pub fn get_blocks<'a>(&'a mut self) -> InOutBuf<'a, 'a, GenericArray<u8, BS>> {
pub fn get_blocks<'a>(&'a mut self) -> InOutBuf<'a, 'a, Array<u8, BS>> {
self.blocks.reborrow()
}

Expand All @@ -203,7 +203,7 @@ impl<'inp, 'out, BS: ArrayLength<u8>> PaddedInOutBuf<'inp, 'out, BS> {
/// For paddings with `P::TYPE = PadType::Reversible` it always returns `Some`.
#[inline(always)]
#[allow(clippy::needless_option_as_deref)]
pub fn get_tail_block<'a>(&'a mut self) -> Option<InOut<'a, 'a, GenericArray<u8, BS>>> {
pub fn get_tail_block<'a>(&'a mut self) -> Option<InOut<'a, 'a, Array<u8, BS>>> {
match self.tail_out.as_deref_mut() {
Some(out_block) => Some((&self.tail_in, out_block).into()),
None => None,
Expand Down

0 comments on commit 28eeaf7

Please sign in to comment.