Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] cipher: migrate to hybrid array #1357

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/crypto-common.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 All @@ -46,7 +46,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v3
Expand Down
53 changes: 28 additions & 25 deletions Cargo.lock

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

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

[patch.crates-io]
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" }
4 changes: 2 additions & 2 deletions cipher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ 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
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> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused how this worked before, but it seems necessary now, despite the bounds of ArrayLength<T> and ArraySize being otherwise identical (Unsigned):

error[E0277]: the trait bound `BS: Cmp<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>>` is not satisfied
   --> cipher/src/block.rs:601:51
    |
601 | impl<'inp, 'out, BS: ArraySize> BlockSizeUser for BlockCtx<'inp, 'out, BS> {
    |                                                   ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Cmp<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>>` is not implemented for `BS`
    |
    = note: required for `BS` to implement `IsLess<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>>`
    = note: required for `BS` to implement `crypto_common::sealed::BlockSizes`
    = note: required for `BS` to implement `BlockSizes`
note: required by a bound in `BlockCtx`
   --> cipher/src/block.rs:597:33
    |
597 | struct BlockCtx<'inp, 'out, BS: BlockSizes> {
    |                                 ^^^^^^^^^^ required by this bound in `BlockCtx`
help: consider further restricting this bound
    |
601 | impl<'inp, 'out, BS: ArraySize + crypto_common::typenum::Cmp<crypto_common::typenum::UInt<crypto_common::typenum::UInt<crypto_common::typenum::UInt<crypto_common::typenum::UInt<crypto_common::typenum::UInt<crypto_common::typenum::UInt<crypto_common::typenum::UInt<crypto_common::typenum::UInt<crypto_common::typenum::UInt<crypto_common::typenum::UTerm, crypto_common::typenum::B1>, crypto_common::typenum::B0>, crypto_common::typenum::B0>, crypto_common::typenum::B0>, crypto_common::typenum::B0>, crypto_common::typenum::B0>, crypto_common::typenum::B0>, crypto_common::typenum::B0>, crypto_common::typenum::B0>>> 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
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
34 changes: 17 additions & 17 deletions cipher/src/stream_core.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{ParBlocks, ParBlocksSizeUser, StreamCipherError};
use crypto_common::{
generic_array::{ArrayLength, GenericArray},
array::{Array, ArraySize},
typenum::Unsigned,
Block, BlockSizeUser,
Block, BlockSizeUser, BlockSizes,
};
use inout::{InOut, InOutBuf};

Expand Down Expand Up @@ -195,7 +195,7 @@ impl_counter! { u32 u64 u128 }
/// In case if `N` is less or equal to 1, buffer of arrays has length
/// of zero and tail is equal to `self`.
#[inline]
fn into_chunks<T, N: ArrayLength<T>>(buf: &mut [T]) -> (&mut [GenericArray<T, N>], &mut [T]) {
fn into_chunks<T, N: ArraySize>(buf: &mut [T]) -> (&mut [Array<T, N>], &mut [T]) {
use core::slice;
if N::USIZE <= 1 {
return (&mut [], buf);
Expand All @@ -205,32 +205,32 @@ fn into_chunks<T, N: ArrayLength<T>>(buf: &mut [T]) -> (&mut [GenericArray<T, N>
let tail_len = buf.len() - tail_pos;
unsafe {
let ptr = buf.as_mut_ptr();
let chunks = slice::from_raw_parts_mut(ptr as *mut GenericArray<T, N>, chunks_len);
let chunks = slice::from_raw_parts_mut(ptr as *mut Array<T, N>, chunks_len);
let tail = slice::from_raw_parts_mut(ptr.add(tail_pos), tail_len);
(chunks, tail)
}
}

struct WriteBlockCtx<'a, BS: ArrayLength<u8>> {
struct WriteBlockCtx<'a, BS: BlockSizes> {
block: &'a mut Block<Self>,
}
impl<'a, BS: ArrayLength<u8>> BlockSizeUser for WriteBlockCtx<'a, BS> {
impl<'a, BS: BlockSizes> BlockSizeUser for WriteBlockCtx<'a, BS> {
type BlockSize = BS;
}
impl<'a, BS: ArrayLength<u8>> StreamClosure for WriteBlockCtx<'a, BS> {
impl<'a, BS: BlockSizes> StreamClosure for WriteBlockCtx<'a, BS> {
#[inline(always)]
fn call<B: StreamBackend<BlockSize = BS>>(self, backend: &mut B) {
backend.gen_ks_block(self.block);
}
}

struct WriteBlocksCtx<'a, BS: ArrayLength<u8>> {
struct WriteBlocksCtx<'a, BS: BlockSizes> {
blocks: &'a mut [Block<Self>],
}
impl<'a, BS: ArrayLength<u8>> BlockSizeUser for WriteBlocksCtx<'a, BS> {
impl<'a, BS: BlockSizes> BlockSizeUser for WriteBlocksCtx<'a, BS> {
type BlockSize = BS;
}
impl<'a, BS: ArrayLength<u8>> StreamClosure for WriteBlocksCtx<'a, BS> {
impl<'a, BS: BlockSizes> StreamClosure for WriteBlocksCtx<'a, BS> {
#[inline(always)]
fn call<B: StreamBackend<BlockSize = BS>>(self, backend: &mut B) {
if B::ParBlocksSize::USIZE > 1 {
Expand All @@ -247,15 +247,15 @@ impl<'a, BS: ArrayLength<u8>> StreamClosure for WriteBlocksCtx<'a, BS> {
}
}

struct ApplyBlockCtx<'inp, 'out, BS: ArrayLength<u8>> {
struct ApplyBlockCtx<'inp, 'out, BS: BlockSizes> {
block: InOut<'inp, 'out, Block<Self>>,
}

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

impl<'inp, 'out, BS: ArrayLength<u8>> StreamClosure for ApplyBlockCtx<'inp, 'out, BS> {
impl<'inp, 'out, BS: BlockSizes> StreamClosure for ApplyBlockCtx<'inp, 'out, BS> {
#[inline(always)]
fn call<B: StreamBackend<BlockSize = BS>>(mut self, backend: &mut B) {
let mut t = Default::default();
Expand All @@ -264,15 +264,15 @@ impl<'inp, 'out, BS: ArrayLength<u8>> StreamClosure for ApplyBlockCtx<'inp, 'out
}
}

struct ApplyBlocksCtx<'inp, 'out, BS: ArrayLength<u8>> {
struct ApplyBlocksCtx<'inp, 'out, BS: BlockSizes> {
blocks: InOutBuf<'inp, 'out, Block<Self>>,
}

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

impl<'inp, 'out, BS: ArrayLength<u8>> StreamClosure for ApplyBlocksCtx<'inp, 'out, BS> {
impl<'inp, 'out, BS: BlockSizes> StreamClosure for ApplyBlocksCtx<'inp, 'out, BS> {
#[inline(always)]
#[allow(clippy::needless_range_loop)]
fn call<B: StreamBackend<BlockSize = BS>>(self, backend: &mut B) {
Expand All @@ -284,7 +284,7 @@ impl<'inp, 'out, BS: ArrayLength<u8>> StreamClosure for ApplyBlocksCtx<'inp, 'ou
chunk.xor_in2out(&tmp);
}
let n = tail.len();
let mut buf = GenericArray::<_, B::ParBlocksSize>::default();
let mut buf = Array::<_, B::ParBlocksSize>::default();
let ks = &mut buf[..n];
backend.gen_tail_blocks(ks);
for i in 0..n {
Expand Down
3 changes: 1 addition & 2 deletions crypto-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ keywords = ["crypto", "traits"]
categories = ["cryptography", "no-std"]

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

# optional dependencies
rand_core = { version = "0.6.4", optional = true }
Expand Down
6 changes: 3 additions & 3 deletions crypto-common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

[![crate][crate-image]][crate-link]
[![Docs][docs-image]][docs-link]
[![Build Status][build-image]][build-link]
![Apache2/MIT licensed][license-image]
![Rust Version][rustc-image]
[![Project Chat][chat-image]][chat-link]
[![Build Status][build-image]][build-link]

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

## 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 @@ -46,7 +46,7 @@ dual licensed as above, without any additional terms or conditions.
[docs-image]: https://docs.rs/crypto-common/badge.svg
[docs-link]: https://docs.rs/crypto-common/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.56+-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/260041-hashes
[build-image]: https://github.com/RustCrypto/traits/workflows/crypto-common/badge.svg?branch=master&event=push
Expand Down
Loading
Loading