Skip to content

Commit 28eeaf7

Browse files
committed
[WIP] Migrate to hybrid-array; MSRV 1.65
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`
1 parent 1f995e9 commit 28eeaf7

9 files changed

Lines changed: 57 additions & 71 deletions

File tree

Cargo.lock

Lines changed: 6 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

block-padding/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "block-padding"
3-
version = "0.3.3"
3+
version = "0.4.0-pre"
44
description = "Padding and unpadding of messages divided into blocks."
55
authors = ["RustCrypto Developers"]
66
license = "MIT OR Apache-2.0"
@@ -12,7 +12,7 @@ keywords = ["padding", "pkcs7", "ansix923", "iso7816"]
1212
categories = ["cryptography", "no-std"]
1313

1414
[dependencies]
15-
hybrid-array = "=0.2.0-pre.2"
15+
hybrid-array = "=0.2.0-pre.3"
1616

1717
[features]
1818
std = []

dbl/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
[package]
22
name = "dbl"
3-
version = "0.3.2"
3+
version = "0.4.0-pre"
44
authors = ["RustCrypto Developers"]
55
license = "MIT OR Apache-2.0"
66
description = "Double operation in Galois Field (GF)"
77
documentation = "https://docs.rs/dbl"
88
repository = "https://github.com/RustCrypto/utils"
99
keywords = ["crypto", "dbl", "gf", "galois"]
10+
edition = "2021"
11+
rust-version = "1.65"
1012

1113
[dependencies]
12-
generic-array = "0.14"
14+
hybrid-array = "=0.2.0-pre.3"

dbl/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
)]
77
#![forbid(unsafe_code)]
88

9-
extern crate generic_array;
10-
11-
use generic_array::typenum::{U16, U32, U8};
12-
use generic_array::GenericArray;
9+
use hybrid_array::typenum::{U16, U32, U8};
10+
use hybrid_array::Array;
1311

1412
use core::convert::TryInto;
1513

@@ -39,7 +37,7 @@ pub trait Dbl {
3937
fn inv_dbl(self) -> Self;
4038
}
4139

42-
impl Dbl for GenericArray<u8, U8> {
40+
impl Dbl for Array<u8, U8> {
4341
#[inline]
4442
fn dbl(self) -> Self {
4543
let mut val = u64::from_be_bytes(self.into());
@@ -63,7 +61,7 @@ impl Dbl for GenericArray<u8, U8> {
6361
}
6462
}
6563

66-
impl Dbl for GenericArray<u8, U16> {
64+
impl Dbl for Array<u8, U16> {
6765
#[inline]
6866
fn dbl(self) -> Self {
6967
let mut val = [
@@ -108,7 +106,7 @@ impl Dbl for GenericArray<u8, U16> {
108106
}
109107
}
110108

111-
impl Dbl for GenericArray<u8, U32> {
109+
impl Dbl for Array<u8, U32> {
112110
#[inline]
113111
fn dbl(self) -> Self {
114112
let mut val = [

hybrid-array/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,12 @@ macro_rules! impl_array_size {
556556
type ArrayType<T> = [T; $len];
557557
}
558558

559+
impl<T> From<Array<T, typenum::$ty>> for [T; $len] {
560+
fn from(arr: Array<T, typenum::$ty>) -> [T; $len] {
561+
arr.0
562+
}
563+
}
564+
559565
impl<T> IntoArray<T> for [T; $len] {
560566
type Size = typenum::$ty;
561567

inout/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "inout"
3-
version = "0.1.3"
3+
version = "0.2.0-pre"
44
description = "Custom reference types for code generic over in-place and buffer-to-buffer modes of operation."
55
authors = ["RustCrypto Developers"]
66
license = "MIT OR Apache-2.0"
@@ -11,8 +11,8 @@ repository = "https://github.com/RustCrypto/utils"
1111
keywords = ["custom-reference"]
1212

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

1717
[features]
1818
std = ["block-padding/std"]

inout/src/inout.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::InOutBuf;
22
use core::{marker::PhantomData, ptr};
3-
use generic_array::{ArrayLength, GenericArray};
3+
use hybrid_array::{Array, ArraySize};
44

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

101-
impl<'inp, 'out, T, N: ArrayLength<T>> InOut<'inp, 'out, GenericArray<T, N>> {
101+
impl<'inp, 'out, T, N: ArraySize> InOut<'inp, 'out, Array<T, N>> {
102102
/// Returns `InOut` for the given position.
103103
///
104104
/// # Panics
@@ -127,18 +127,18 @@ impl<'inp, 'out, T, N: ArrayLength<T>> InOut<'inp, 'out, GenericArray<T, N>> {
127127
}
128128
}
129129

130-
impl<'inp, 'out, N: ArrayLength<u8>> InOut<'inp, 'out, GenericArray<u8, N>> {
130+
impl<'inp, 'out, N: ArraySize> InOut<'inp, 'out, Array<u8, N>> {
131131
/// XOR `data` with values behind the input slice and write
132132
/// result to the output slice.
133133
///
134134
/// # Panics
135135
/// If `data` length is not equal to the buffer length.
136136
#[inline(always)]
137137
#[allow(clippy::needless_range_loop)]
138-
pub fn xor_in2out(&mut self, data: &GenericArray<u8, N>) {
138+
pub fn xor_in2out(&mut self, data: &Array<u8, N>) {
139139
unsafe {
140140
let input = ptr::read(self.in_ptr);
141-
let mut temp = GenericArray::<u8, N>::default();
141+
let mut temp = Array::<u8, N>::default();
142142
for i in 0..N::USIZE {
143143
temp[i] = input[i] ^ data[i];
144144
}
@@ -147,10 +147,10 @@ impl<'inp, 'out, N: ArrayLength<u8>> InOut<'inp, 'out, GenericArray<u8, N>> {
147147
}
148148
}
149149

150-
impl<'inp, 'out, N, M> InOut<'inp, 'out, GenericArray<GenericArray<u8, N>, M>>
150+
impl<'inp, 'out, N, M> InOut<'inp, 'out, Array<Array<u8, N>, M>>
151151
where
152-
N: ArrayLength<u8>,
153-
M: ArrayLength<GenericArray<u8, N>>,
152+
N: ArraySize,
153+
M: ArraySize,
154154
{
155155
/// XOR `data` with values behind the input slice and write
156156
/// result to the output slice.
@@ -159,10 +159,10 @@ where
159159
/// If `data` length is not equal to the buffer length.
160160
#[inline(always)]
161161
#[allow(clippy::needless_range_loop)]
162-
pub fn xor_in2out(&mut self, data: &GenericArray<GenericArray<u8, N>, M>) {
162+
pub fn xor_in2out(&mut self, data: &Array<Array<u8, N>, M>) {
163163
unsafe {
164164
let input = ptr::read(self.in_ptr);
165-
let mut temp = GenericArray::<GenericArray<u8, N>, M>::default();
165+
let mut temp = Array::<Array<u8, N>, M>::default();
166166
for i in 0..M::USIZE {
167167
for j in 0..N::USIZE {
168168
temp[i][j] = input[i][j] ^ data[i][j];

inout/src/inout_buf.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
InOut,
44
};
55
use core::{marker::PhantomData, slice};
6-
use generic_array::{ArrayLength, GenericArray};
6+
use hybrid_array::{Array, ArraySize};
77

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

210210
/// Partition buffer into 2 parts: buffer of arrays and tail.
211211
#[inline(always)]
212-
pub fn into_chunks<N: ArrayLength<T>>(
212+
pub fn into_chunks<N: ArraySize>(
213213
self,
214-
) -> (
215-
InOutBuf<'inp, 'out, GenericArray<T, N>>,
216-
InOutBuf<'inp, 'out, T>,
217-
) {
214+
) -> (InOutBuf<'inp, 'out, Array<T, N>>, InOutBuf<'inp, 'out, T>) {
218215
let chunks = self.len() / N::USIZE;
219216
let tail_pos = N::USIZE * chunks;
220217
let tail_len = self.len() - tail_pos;
221218
unsafe {
222219
let chunks = InOutBuf {
223-
in_ptr: self.in_ptr as *const GenericArray<T, N>,
224-
out_ptr: self.out_ptr as *mut GenericArray<T, N>,
220+
in_ptr: self.in_ptr as *const Array<T, N>,
221+
out_ptr: self.out_ptr as *mut Array<T, N>,
225222
len: chunks,
226223
_pd: PhantomData,
227224
};
@@ -256,14 +253,14 @@ impl<'inp, 'out> InOutBuf<'inp, 'out, u8> {
256253
}
257254
}
258255

259-
impl<'inp, 'out, T, N> TryInto<InOut<'inp, 'out, GenericArray<T, N>>> for InOutBuf<'inp, 'out, T>
256+
impl<'inp, 'out, T, N> TryInto<InOut<'inp, 'out, Array<T, N>>> for InOutBuf<'inp, 'out, T>
260257
where
261-
N: ArrayLength<T>,
258+
N: ArraySize,
262259
{
263260
type Error = IntoArrayError;
264261

265262
#[inline(always)]
266-
fn try_into(self) -> Result<InOut<'inp, 'out, GenericArray<T, N>>, Self::Error> {
263+
fn try_into(self) -> Result<InOut<'inp, 'out, Array<T, N>>, Self::Error> {
267264
if self.len() == N::USIZE {
268265
Ok(InOut {
269266
in_ptr: self.in_ptr as *const _,

inout/src/reserved.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{InOut, InOutBuf};
88
#[cfg(feature = "block-padding")]
99
use block_padding::{PadType, Padding};
1010
#[cfg(feature = "block-padding")]
11-
use generic_array::{ArrayLength, GenericArray};
11+
use hybrid_array::{Array, ArraySize};
1212

1313
/// Custom slice type which references one immutable (input) slice and one
1414
/// mutable (output) slice. Input and output slices are either the same or
@@ -134,19 +134,19 @@ impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> {
134134
pub fn into_padded_blocks<P, BS>(self) -> Result<PaddedInOutBuf<'inp, 'out, BS>, PadError>
135135
where
136136
P: Padding<BS>,
137-
BS: ArrayLength<u8>,
137+
BS: ArraySize,
138138
{
139139
let bs = BS::USIZE;
140140
let blocks_len = self.in_len / bs;
141141
let tail_len = self.in_len - bs * blocks_len;
142142
let blocks = unsafe {
143143
InOutBuf::from_raw(
144-
self.in_ptr as *const GenericArray<u8, BS>,
145-
self.out_ptr as *mut GenericArray<u8, BS>,
144+
self.in_ptr as *const Array<u8, BS>,
145+
self.out_ptr as *mut Array<u8, BS>,
146146
blocks_len,
147147
)
148148
};
149-
let mut tail_in = GenericArray::<u8, BS>::default();
149+
let mut tail_in = Array::<u8, BS>::default();
150150
let tail_out = match P::TYPE {
151151
PadType::NoPadding | PadType::Ambiguous if tail_len == 0 => None,
152152
PadType::NoPadding => return Err(PadError),
@@ -167,7 +167,7 @@ impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> {
167167
tail_in.as_mut_ptr(),
168168
tail_len,
169169
);
170-
&mut *(self.out_ptr.add(blen) as *mut GenericArray<u8, BS>)
170+
&mut *(self.out_ptr.add(blen) as *mut Array<u8, BS>)
171171
};
172172
P::pad(&mut tail_in, tail_len);
173173
Some(out_block)
@@ -184,17 +184,17 @@ impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> {
184184
/// Variant of [`InOutBuf`] with optional padded tail block.
185185
#[cfg(feature = "block-padding")]
186186
#[cfg_attr(docsrs, doc(cfg(feature = "block-padding")))]
187-
pub struct PaddedInOutBuf<'inp, 'out, BS: ArrayLength<u8>> {
188-
blocks: InOutBuf<'inp, 'out, GenericArray<u8, BS>>,
189-
tail_in: GenericArray<u8, BS>,
190-
tail_out: Option<&'out mut GenericArray<u8, BS>>,
187+
pub struct PaddedInOutBuf<'inp, 'out, BS: ArraySize> {
188+
blocks: InOutBuf<'inp, 'out, Array<u8, BS>>,
189+
tail_in: Array<u8, BS>,
190+
tail_out: Option<&'out mut Array<u8, BS>>,
191191
}
192192

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

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

0 commit comments

Comments
 (0)