Skip to content

Commit 9a65db3

Browse files
committed
[WIP] crypto-common: migrate to hybrid-array; MSRV 1.65
Replaces `generic-array` with `hybrid-array`, which is built on a combination of `typenum` and const generics, providing a degree of interoperability between the two systems. `hybrid-array` is designed to be a largely drop-in replacement, and the number of changes required to switch are relatively minimal aside from some idiosyncracies.
1 parent 38c0ff9 commit 9a65db3

5 files changed

Lines changed: 39 additions & 44 deletions

File tree

.github/workflows/crypto-common.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
strategy:
2323
matrix:
2424
rust:
25-
- 1.56.0 # MSRV
25+
- 1.65.0 # MSRV
2626
- stable
2727
target:
2828
- thumbv7em-none-eabi
@@ -46,7 +46,7 @@ jobs:
4646
strategy:
4747
matrix:
4848
rust:
49-
- 1.56.0 # MSRV
49+
- 1.65.0 # MSRV
5050
- stable
5151
steps:
5252
- uses: actions/checkout@v3

Cargo.lock

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

crypto-common/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ keywords = ["crypto", "traits"]
1313
categories = ["cryptography", "no-std"]
1414

1515
[dependencies]
16-
generic-array = { version = "0.14.6", features = ["more_lengths"] }
17-
typenum = "1.14" # earlier versions of typenum don't satisfy the 'static bound on U* types
16+
hybrid-array = "=0.2.0-pre.4"
1817

1918
# optional dependencies
2019
rand_core = { version = "0.6.4", optional = true }

crypto-common/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
[![crate][crate-image]][crate-link]
44
[![Docs][docs-image]][docs-link]
5+
[![Build Status][build-image]][build-link]
56
![Apache2/MIT licensed][license-image]
67
![Rust Version][rustc-image]
78
[![Project Chat][chat-image]][chat-link]
8-
[![Build Status][build-image]][build-link]
99

1010
Common traits used by cryptographic algorithms. Users should generally use
1111
higher-level trait crates instead of this one.
@@ -14,7 +14,7 @@ higher-level trait crates instead of this one.
1414

1515
## Minimum Supported Rust Version
1616

17-
Rust **1.56** or higher.
17+
Rust **1.65** or higher.
1818

1919
Minimum supported Rust version can be changed in the future, but it will be
2020
done with a minor version bump.
@@ -46,7 +46,7 @@ dual licensed as above, without any additional terms or conditions.
4646
[docs-image]: https://docs.rs/crypto-common/badge.svg
4747
[docs-link]: https://docs.rs/crypto-common/
4848
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
49-
[rustc-image]: https://img.shields.io/badge/rustc-1.56+-blue.svg
49+
[rustc-image]: https://img.shields.io/badge/rustc-1.65+-blue.svg
5050
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
5151
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260041-hashes
5252
[build-image]: https://github.com/RustCrypto/traits/workflows/crypto-common/badge.svg?branch=master&event=push

crypto-common/src/lib.rs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ extern crate std;
1515
#[cfg(feature = "rand_core")]
1616
pub use rand_core;
1717

18-
pub use generic_array;
19-
pub use generic_array::typenum;
18+
pub use hybrid_array as array;
19+
pub use hybrid_array::typenum;
2020

2121
use core::fmt;
22-
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
22+
use hybrid_array::{typenum::Unsigned, Array, ArraySize, ByteArray};
2323
#[cfg(feature = "rand_core")]
2424
use rand_core::CryptoRngCore;
2525

2626
/// Block on which [`BlockSizeUser`] implementors operate.
27-
pub type Block<B> = GenericArray<u8, <B as BlockSizeUser>::BlockSize>;
27+
pub type Block<B> = ByteArray<<B as BlockSizeUser>::BlockSize>;
2828

2929
/// Parallel blocks on which [`ParBlocksSizeUser`] implementors operate.
30-
pub type ParBlocks<T> = GenericArray<Block<T>, <T as ParBlocksSizeUser>::ParBlocksSize>;
30+
pub type ParBlocks<T> = Array<Block<T>, <T as ParBlocksSizeUser>::ParBlocksSize>;
3131

3232
/// Output array of [`OutputSizeUser`] implementors.
33-
pub type Output<T> = GenericArray<u8, <T as OutputSizeUser>::OutputSize>;
33+
pub type Output<T> = ByteArray<<T as OutputSizeUser>::OutputSize>;
3434

3535
/// Key used by [`KeySizeUser`] implementors.
36-
pub type Key<B> = GenericArray<u8, <B as KeySizeUser>::KeySize>;
36+
pub type Key<B> = ByteArray<<B as KeySizeUser>::KeySize>;
3737

3838
/// Initialization vector (nonce) used by [`IvSizeUser`] implementors.
39-
pub type Iv<B> = GenericArray<u8, <B as IvSizeUser>::IvSize>;
39+
pub type Iv<B> = ByteArray<<B as IvSizeUser>::IvSize>;
4040

4141
/// Types which process data in blocks.
4242
pub trait BlockSizeUser {
@@ -59,12 +59,12 @@ impl<T: BlockSizeUser> BlockSizeUser for &mut T {
5959
}
6060

6161
/// Trait implemented for supported block sizes, i.e. for types from `U1` to `U255`.
62-
pub trait BlockSizes: ArrayLength<u8> + sealed::BlockSizes + 'static {}
62+
pub trait BlockSizes: ArraySize + sealed::BlockSizes + 'static {}
6363

64-
impl<T: ArrayLength<u8> + sealed::BlockSizes> BlockSizes for T {}
64+
impl<T: ArraySize + sealed::BlockSizes> BlockSizes for T {}
6565

6666
mod sealed {
67-
use generic_array::typenum::{Gr, IsGreater, IsLess, Le, NonZero, Unsigned, U1, U256};
67+
use crate::typenum::{Gr, IsGreater, IsLess, Le, NonZero, Unsigned, U1, U256};
6868

6969
pub trait BlockSizes {}
7070

@@ -80,13 +80,13 @@ mod sealed {
8080
/// Types which can process blocks in parallel.
8181
pub trait ParBlocksSizeUser: BlockSizeUser {
8282
/// Number of blocks which can be processed in parallel.
83-
type ParBlocksSize: ArrayLength<Block<Self>>;
83+
type ParBlocksSize: ArraySize;
8484
}
8585

8686
/// Types which return data with the given size.
8787
pub trait OutputSizeUser {
8888
/// Size of the output in bytes.
89-
type OutputSize: ArrayLength<u8> + 'static;
89+
type OutputSize: ArraySize + 'static;
9090

9191
/// Return output size in bytes.
9292
#[inline(always)]
@@ -100,7 +100,7 @@ pub trait OutputSizeUser {
100100
/// Generally it's used indirectly via [`KeyInit`] or [`KeyIvInit`].
101101
pub trait KeySizeUser {
102102
/// Key size in bytes.
103-
type KeySize: ArrayLength<u8> + 'static;
103+
type KeySize: ArraySize + 'static;
104104

105105
/// Return key size in bytes.
106106
#[inline(always)]
@@ -114,7 +114,7 @@ pub trait KeySizeUser {
114114
/// Generally it's used indirectly via [`KeyIvInit`] or [`InnerIvInit`].
115115
pub trait IvSizeUser {
116116
/// Initialization vector size in bytes.
117-
type IvSize: ArrayLength<u8> + 'static;
117+
type IvSize: ArraySize + 'static;
118118

119119
/// Return IV size in bytes.
120120
#[inline(always)]
@@ -151,11 +151,9 @@ pub trait KeyInit: KeySizeUser + Sized {
151151
/// Create new value from variable size key.
152152
#[inline]
153153
fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> {
154-
if key.len() != Self::KeySize::to_usize() {
155-
Err(InvalidLength)
156-
} else {
157-
Ok(Self::new(Key::<Self>::from_slice(key)))
158-
}
154+
<&Key<Self>>::try_from(key)
155+
.map(Self::new)
156+
.map_err(|_| InvalidLength)
159157
}
160158

161159
/// Generate random key using the provided [`CryptoRngCore`].
@@ -177,16 +175,9 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
177175
/// Create new value from variable length key and nonce.
178176
#[inline]
179177
fn new_from_slices(key: &[u8], iv: &[u8]) -> Result<Self, InvalidLength> {
180-
let key_len = Self::KeySize::USIZE;
181-
let iv_len = Self::IvSize::USIZE;
182-
if key.len() != key_len || iv.len() != iv_len {
183-
Err(InvalidLength)
184-
} else {
185-
Ok(Self::new(
186-
Key::<Self>::from_slice(key),
187-
Iv::<Self>::from_slice(iv),
188-
))
189-
}
178+
let key = <&Key<Self>>::try_from(key).map_err(|_| InvalidLength)?;
179+
let iv = <&Iv<Self>>::try_from(iv).map_err(|_| InvalidLength)?;
180+
Ok(Self::new(key, iv))
190181
}
191182

192183
/// Generate random key using the provided [`CryptoRngCore`].
@@ -237,11 +228,8 @@ pub trait InnerIvInit: InnerUser + IvSizeUser + Sized {
237228
/// Initialize value using `inner` and `iv` slice.
238229
#[inline]
239230
fn inner_iv_slice_init(inner: Self::Inner, iv: &[u8]) -> Result<Self, InvalidLength> {
240-
if iv.len() != Self::IvSize::to_usize() {
241-
Err(InvalidLength)
242-
} else {
243-
Ok(Self::inner_iv_init(inner, Iv::<Self>::from_slice(iv)))
244-
}
231+
let iv = <&Iv<Self>>::try_from(iv).map_err(|_| InvalidLength)?;
232+
Ok(Self::inner_iv_init(inner, iv))
245233
}
246234

247235
/// Generate random IV using the provided [`CryptoRngCore`].

0 commit comments

Comments
 (0)