Skip to content

Commit

Permalink
[WIP] cipher+digest: migrate to hybrid array; MSRV 1.65
Browse files Browse the repository at this point in the history
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 idiosyncrasies.
  • Loading branch information
tarcieri committed Oct 8, 2023
1 parent 9a65db3 commit fb64380
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 121 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cipher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand Down Expand Up @@ -57,7 +57,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v3
Expand Down
15 changes: 7 additions & 8 deletions .github/workflows/digest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
rust:
- 1.57.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -34,21 +34,20 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
- run: rm ../Cargo.toml
- run: cargo build --target ${{ matrix.target }}

minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
with:
working-directory: ${{ github.workflow }}
# TODO(tarcieri): re-enable after next `crypto-common` release
#minimal-versions:
# uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
# with:
# working-directory: ${{ github.workflow }}

test:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.57.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v3
Expand Down
67 changes: 30 additions & 37 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ exclude = [
"crypto",
"elliptic-curve"
]

[patch.crates-io]
block-buffer = { git = "https://github.com/RustCrypto/utils.git", branch = "migrate-to-hybrid-array" }
crypto-common = { path = "crypto-common" }
hybrid-array = { git = "https://github.com/RustCrypto/utils.git", branch = "migrate-to-hybrid-array" }
inout = { git = "https://github.com/RustCrypto/utils.git", branch = "migrate-to-hybrid-array" }
8 changes: 4 additions & 4 deletions cipher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[package]
name = "cipher"
description = "Traits for describing block ciphers and stream ciphers"
version = "0.4.4"
version = "0.5.0-pre"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"
rust-version = "1.56"
rust-version = "1.65"
documentation = "https://docs.rs/cipher"
repository = "https://github.com/RustCrypto/traits"
keywords = ["crypto", "block-cipher", "stream-cipher", "trait"]
categories = ["cryptography", "no-std"]

[dependencies]
crypto-common = "0.1.6"
inout = "0.1"
crypto-common = "=0.2.0-pre"
inout = "=0.2.0-pre"

# optional dependencies
blobby = { version = "0.3", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions cipher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ implementations which use these traits.

## Minimum Supported Rust Version

Rust **1.56** or higher.
Rust **1.65** or higher.

Minimum supported Rust version can be changed in the future, but it will be
done with a minor version bump.
Expand Down Expand Up @@ -48,7 +48,7 @@ dual licensed as above, without any additional terms or conditions.
[docs-image]: https://docs.rs/cipher/badge.svg
[docs-link]: https://docs.rs/cipher/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.41+-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.65+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260050-traits
[build-image]: https://github.com/RustCrypto/traits/workflows/cipher/badge.svg?branch=master&event=push
Expand Down
15 changes: 8 additions & 7 deletions cipher/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
use crate::{ParBlocks, ParBlocksSizeUser};
#[cfg(all(feature = "block-padding", feature = "alloc"))]
use alloc::{vec, vec::Vec};
use crypto_common::BlockSizes;
#[cfg(feature = "block-padding")]
use inout::{
block_padding::{Padding, UnpadError},
InOutBufReserved, PadError,
};
use inout::{InOut, InOutBuf, NotEqualError};

pub use crypto_common::{generic_array::ArrayLength, typenum::Unsigned, Block, BlockSizeUser};
pub use crypto_common::{array::ArraySize, typenum::Unsigned, Block, BlockSizeUser};

/// Marker trait for block ciphers.
pub trait BlockCipher: BlockSizeUser {}
Expand Down Expand Up @@ -593,31 +594,31 @@ impl<Alg: BlockDecrypt> BlockDecrypt for &Alg {
}

/// Closure used in methods which operate over separate blocks.
struct BlockCtx<'inp, 'out, BS: ArrayLength<u8>> {
struct BlockCtx<'inp, 'out, BS: BlockSizes> {
block: InOut<'inp, 'out, Block<Self>>,
}

impl<'inp, 'out, BS: ArrayLength<u8>> BlockSizeUser for BlockCtx<'inp, 'out, BS> {
impl<'inp, 'out, BS: BlockSizes> BlockSizeUser for BlockCtx<'inp, 'out, BS> {
type BlockSize = BS;
}

impl<'inp, 'out, BS: ArrayLength<u8>> BlockClosure for BlockCtx<'inp, 'out, BS> {
impl<'inp, 'out, BS: BlockSizes> BlockClosure for BlockCtx<'inp, 'out, BS> {
#[inline(always)]
fn call<B: BlockBackend<BlockSize = BS>>(self, backend: &mut B) {
backend.proc_block(self.block);
}
}

/// Closure used in methods which operate over slice of blocks.
struct BlocksCtx<'inp, 'out, BS: ArrayLength<u8>> {
struct BlocksCtx<'inp, 'out, BS: BlockSizes> {
blocks: InOutBuf<'inp, 'out, Block<Self>>,
}

impl<'inp, 'out, BS: ArrayLength<u8>> BlockSizeUser for BlocksCtx<'inp, 'out, BS> {
impl<'inp, 'out, BS: BlockSizes> BlockSizeUser for BlocksCtx<'inp, 'out, BS> {
type BlockSize = BS;
}

impl<'inp, 'out, BS: ArrayLength<u8>> BlockClosure for BlocksCtx<'inp, 'out, BS> {
impl<'inp, 'out, BS: BlockSizes> BlockClosure for BlocksCtx<'inp, 'out, BS> {
#[inline(always)]
fn call<B: BlockBackend<BlockSize = BS>>(self, backend: &mut B) {
if B::ParBlocksSize::USIZE > 1 {
Expand Down
14 changes: 7 additions & 7 deletions cipher/src/dev/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ macro_rules! block_cipher_test {
#[test]
fn $name() {
use cipher::{
blobby::Blob3Iterator, generic_array::GenericArray, typenum::Unsigned,
BlockDecryptMut, BlockEncryptMut, BlockSizeUser, KeyInit,
array::Array, blobby::Blob3Iterator, typenum::Unsigned, BlockDecryptMut,
BlockEncryptMut, BlockSizeUser, KeyInit,
};

fn run_test(key: &[u8], pt: &[u8], ct: &[u8]) -> bool {
let mut state = <$cipher as KeyInit>::new_from_slice(key).unwrap();

let mut block = GenericArray::clone_from_slice(pt);
let mut block = Array::clone_from_slice(pt);
state.encrypt_block_mut(&mut block);
if ct != block.as_slice() {
return false;
Expand Down Expand Up @@ -105,8 +105,8 @@ macro_rules! block_mode_enc_test {
#[test]
fn $name() {
use cipher::{
blobby::Blob4Iterator, generic_array::GenericArray, inout::InOutBuf,
typenum::Unsigned, BlockEncryptMut, BlockSizeUser, KeyIvInit,
array::Array, blobby::Blob4Iterator, inout::InOutBuf, typenum::Unsigned,
BlockEncryptMut, BlockSizeUser, KeyIvInit,
};

fn run_test(key: &[u8], iv: &[u8], pt: &[u8], ct: &[u8]) -> bool {
Expand Down Expand Up @@ -164,8 +164,8 @@ macro_rules! block_mode_dec_test {
#[test]
fn $name() {
use cipher::{
blobby::Blob4Iterator, generic_array::GenericArray, inout::InOutBuf,
typenum::Unsigned, BlockDecryptMut, BlockSizeUser, KeyIvInit,
array::Array, blobby::Blob4Iterator, inout::InOutBuf, typenum::Unsigned,
BlockDecryptMut, BlockSizeUser, KeyIvInit,
};

fn run_test(key: &[u8], iv: &[u8], pt: &[u8], ct: &[u8]) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions cipher/src/dev/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ macro_rules! stream_cipher_test {
($name:ident, $test_name:expr, $cipher:ty $(,)?) => {
#[test]
fn $name() {
use cipher::generic_array::GenericArray;
use cipher::array::Array;
use cipher::{blobby::Blob4Iterator, KeyIvInit, StreamCipher};

let data = include_bytes!(concat!("data/", $test_name, ".blb"));
Expand Down Expand Up @@ -43,7 +43,7 @@ macro_rules! stream_cipher_seek_test {
($name:ident, $cipher:ty) => {
#[test]
fn $name() {
use cipher::generic_array::GenericArray;
use cipher::array::Array;
use cipher::{KeyIvInit, StreamCipher, StreamCipherSeek};

fn get_cipher() -> $cipher {
Expand Down
2 changes: 1 addition & 1 deletion cipher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod stream_wrapper;

pub use crate::{block::*, errors::*, stream::*, stream_core::*, stream_wrapper::*};
pub use crypto_common::{
generic_array,
array,
typenum::{self, consts},
AlgorithmName, Block, InnerIvInit, InvalidLength, Iv, IvSizeUser, Key, KeyInit, KeyIvInit,
KeySizeUser, ParBlocks, ParBlocksSizeUser,
Expand Down
Loading

0 comments on commit fb64380

Please sign in to comment.