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 2e78c4b commit 1491f8b
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 148 deletions.
35 changes: 8 additions & 27 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io]
crypto-common = { git = "https://github.com/RustCrypto/traits", branch = "crypto-common/hybrid-array" }
hybrid-array = { path = "hybrid-array" }
1 change: 0 additions & 1 deletion block-buffer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ readme = "README.md"

[dependencies]
crypto-common = "0.2.0-pre"
generic-array = "0.14"

[dev-dependencies]
hex-literal = "0.3.3"
23 changes: 10 additions & 13 deletions block-buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
)]
#![warn(missing_docs, rust_2018_idioms)]

pub use generic_array;
pub use crypto_common::{array, Block};

use core::{fmt, ops::Add, slice};
pub use crypto_common::Block;
use crypto_common::{BlockSizeUser, BlockSizes};
use generic_array::{
use array::{
typenum::{Add1, B1},
ArrayLength, GenericArray,
Array, ArraySize,
};
use core::{fmt, ops::Add, slice};
use crypto_common::{BlockSizeUser, BlockSizes};

mod read;
mod sealed;

pub use read::ReadBuffer;

/// Block with additional one byte
type BlockP1<BlockSize> = GenericArray<u8, Add1<BlockSize>>;
type BlockP1<BlockSize> = Array<u8, Add1<BlockSize>>;

/// Trait for buffer kinds.
pub trait BufferKind: sealed::Sealed {}
Expand Down Expand Up @@ -304,7 +303,7 @@ impl<BS: BlockSizes> BlockBuffer<BS, Lazy> {
pub fn serialize(&self) -> BlockP1<BS>
where
BS: Add<B1>,
Add1<BS>: ArrayLength<u8>,
Add1<BS>: ArraySize,
{
let mut res = BlockP1::<BS>::default();
res[0] = self.pos;
Expand All @@ -318,7 +317,7 @@ impl<BS: BlockSizes> BlockBuffer<BS, Lazy> {
pub fn deserialize(buffer: &BlockP1<BS>) -> Result<Self, Error>
where
BS: Add<B1>,
Add1<BS>: ArrayLength<u8>,
Add1<BS>: ArraySize,
{
let pos = buffer[0];
if !<Lazy as sealed::Sealed>::invariant(pos as usize, BS::USIZE) {
Expand All @@ -327,9 +326,7 @@ impl<BS: BlockSizes> BlockBuffer<BS, Lazy> {
if buffer[1..][pos as usize..].iter().any(|&b| b != 0) {
return Err(Error);
}
Ok(Self {
buffer: GenericArray::clone_from_slice(&buffer[1..]),
pos,
})
let buffer = Array::clone_from_slice(&buffer[1..]);
Ok(Self { buffer, pos })
}
}
12 changes: 6 additions & 6 deletions block-buffer/src/sealed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::array::{Array, ArraySize};
use core::slice;
use generic_array::{ArrayLength, GenericArray};

/// Sealed trait for buffer kinds.
pub trait Sealed {
Expand All @@ -14,7 +14,7 @@ pub trait Sealed {
fn invariant(pos: usize, block_size: usize) -> bool;

/// Split input data into slice of blocks and tail.
fn split_blocks<N: ArrayLength<u8>>(data: &[u8]) -> (&[GenericArray<u8, N>], &[u8]);
fn split_blocks<N: ArraySize>(data: &[u8]) -> (&[Array<u8, N>], &[u8]);
}

impl Sealed for super::Eager {
Expand All @@ -35,14 +35,14 @@ impl Sealed for super::Eager {
}

#[inline(always)]
fn split_blocks<N: ArrayLength<u8>>(data: &[u8]) -> (&[GenericArray<u8, N>], &[u8]) {
fn split_blocks<N: ArraySize>(data: &[u8]) -> (&[Array<u8, N>], &[u8]) {
let nb = data.len() / N::USIZE;
let blocks_len = nb * N::USIZE;
let tail_len = data.len() - blocks_len;
// SAFETY: we guarantee that created slices do not point
// outside of `data`
unsafe {
let blocks_ptr = data.as_ptr() as *const GenericArray<u8, N>;
let blocks_ptr = data.as_ptr() as *const Array<u8, N>;
let tail_ptr = data.as_ptr().add(blocks_len);
(
slice::from_raw_parts(blocks_ptr, nb),
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Sealed for super::Lazy {
}

#[inline(always)]
fn split_blocks<N: ArrayLength<u8>>(data: &[u8]) -> (&[GenericArray<u8, N>], &[u8]) {
fn split_blocks<N: ArraySize>(data: &[u8]) -> (&[Array<u8, N>], &[u8]) {
if data.is_empty() {
return (&[], &[]);
}
Expand All @@ -84,7 +84,7 @@ impl Sealed for super::Lazy {
// SAFETY: we guarantee that created slices do not point
// outside of `data`
unsafe {
let blocks_ptr = data.as_ptr() as *const GenericArray<u8, N>;
let blocks_ptr = data.as_ptr() as *const Array<u8, N>;
let tail_ptr = data.as_ptr().add(blocks_len);
(
slice::from_raw_parts(blocks_ptr, nb),
Expand Down
73 changes: 37 additions & 36 deletions block-buffer/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use block_buffer::{
generic_array::{
array::{
typenum::{U10, U16, U24, U4, U8},
GenericArray,
Array,
},
Block, EagerBuffer, LazyBuffer, ReadBuffer,
};
Expand Down Expand Up @@ -203,17 +203,17 @@ fn test_eager_serialize() {
assert_eq!(buf3.serialize(), ser3);

// Invalid position
let buf = GenericArray::from_slice(&[0, 0, 0, 4]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[0, 0, 0, 10]);
assert!(Buf::deserialize(buf).is_err());
let buf = Array([0, 0, 0, 4]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([0, 0, 0, 10]);
assert!(Buf::deserialize(&buf).is_err());
// "Garbage" bytes are not zeroized
let buf = GenericArray::from_slice(&[1, 0, 0, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[0, 1, 0, 1]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[0, 0, 1, 2]);
assert!(Buf::deserialize(buf).is_err());
let buf = Array([1, 0, 0, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([0, 1, 0, 1]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([0, 0, 1, 2]);
assert!(Buf::deserialize(&buf).is_err());
}

#[test]
Expand All @@ -223,6 +223,7 @@ fn test_lazy_serialize() {
let mut buf1 = Buf::default();
let ser0 = buf1.serialize();
assert_eq!(&ser0[..], &[0, 0, 0, 0, 0]);
Buf::deserialize(&ser0).unwrap();
assert_eq!(Buf::deserialize(&ser0).unwrap().serialize(), ser0);

buf1.digest_blocks(&[41, 42], |_| {});
Expand Down Expand Up @@ -262,19 +263,19 @@ fn test_lazy_serialize() {
assert_eq!(buf3.serialize(), ser4);

// Invalid position
let buf = GenericArray::from_slice(&[10, 0, 0, 0, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[5, 0, 0, 0, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = Array([10, 0, 0, 0, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([5, 0, 0, 0, 0]);
assert!(Buf::deserialize(&buf).is_err());
// "Garbage" bytes are not zeroized
let buf = GenericArray::from_slice(&[0, 1, 0, 0, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[1, 0, 1, 0, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[2, 0, 0, 1, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[3, 0, 0, 0, 1]);
assert!(Buf::deserialize(buf).is_err());
let buf = Array([0, 1, 0, 0, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([1, 0, 1, 0, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([2, 0, 0, 1, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([3, 0, 0, 0, 1]);
assert!(Buf::deserialize(&buf).is_err());
}

#[test]
Expand Down Expand Up @@ -332,17 +333,17 @@ fn test_read_serialize() {
assert_eq!(&buf3.serialize()[..], &[1, 55, 56, 57]);

// Invalid position
let buf = GenericArray::from_slice(&[0, 0, 0, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[5, 0, 0, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[10, 0, 0, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = Array([0, 0, 0, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([5, 0, 0, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([10, 0, 0, 0]);
assert!(Buf::deserialize(&buf).is_err());
// "Garbage" bytes are not zeroized
let buf = GenericArray::from_slice(&[2, 1, 0, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[3, 0, 1, 0]);
assert!(Buf::deserialize(buf).is_err());
let buf = GenericArray::from_slice(&[4, 0, 0, 1]);
assert!(Buf::deserialize(buf).is_err());
let buf = Array([2, 1, 0, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([3, 0, 1, 0]);
assert!(Buf::deserialize(&buf).is_err());
let buf = Array([4, 0, 0, 1]);
assert!(Buf::deserialize(&buf).is_err());
}
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]
generic-array = "0.14"
hybrid-array = "=0.2.0-pre.3"

[features]
std = []
Expand Down
Loading

0 comments on commit 1491f8b

Please sign in to comment.