Skip to content

Commit

Permalink
Bump cipher crate to v0.3.0-pre.3
Browse files Browse the repository at this point in the history
Implements the changes from:

RustCrypto/traits#435

Unfortunately there's a circular dependency with `aes`, which now pulls
in `ctr`, so the tests which depend on AES are failing.

The plan is to land these changes first, then update the `block-ciphers`
repository, then circle back and update the `aes` crate dependencies
used in this repo.
  • Loading branch information
tarcieri committed Dec 30, 2020
1 parent 527303e commit 46a9dda
Show file tree
Hide file tree
Showing 32 changed files with 137 additions and 176 deletions.
31 changes: 7 additions & 24 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -12,3 +12,4 @@ members = [

[patch.crates-io]
aes = { git = "https://github.com/RustCrypto/block-ciphers.git" }
cipher = { path = "../traits/cipher" }
2 changes: 1 addition & 1 deletion benches/src/chacha20.rs
Expand Up @@ -3,7 +3,7 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Through
use criterion_cycles_per_byte::CyclesPerByte;

use chacha20::{
cipher::{NewStreamCipher, SyncStreamCipher},
cipher::{NewCipher, StreamCipher},
ChaCha20,
};

Expand Down
6 changes: 3 additions & 3 deletions cfb-mode/Cargo.toml
Expand Up @@ -12,9 +12,9 @@ readme = "README.md"
edition = "2018"

[dependencies]
cipher = "0.3.0-pre"
cipher = "0.3.0-pre.3"

[dev-dependencies]
aes = "0.7.0-pre"
cipher = { version = "0.3.0-pre", features = ["dev"] }
#aes = "0.7.0-pre"
cipher = { version = "0.3.0-pre.3", features = ["dev"] }
hex-literal = "0.2"
7 changes: 3 additions & 4 deletions cfb-mode/src/lib.rs
Expand Up @@ -10,7 +10,7 @@
//! ```
//! use aes::Aes128;
//! use cfb_mode::Cfb;
//! use cfb_mode::cipher::{NewStreamCipher, StreamCipher};
//! use cfb_mode::cipher::{NewCipher, StreamCipher};
//! use hex_literal::hex;
//!
//! type AesCfb = Cfb<Aes128>;
Expand Down Expand Up @@ -53,9 +53,8 @@
pub use cipher;

use cipher::{
block::{BlockCipher, BlockEncrypt, NewBlockCipher, ParBlocks},
generic_array::{typenum::Unsigned, GenericArray},
stream::{FromBlockCipher, StreamCipher},
AsyncStreamCipher, BlockCipher, BlockEncrypt, FromBlockCipher, NewBlockCipher, ParBlocks,
};
use core::slice;

Expand All @@ -80,7 +79,7 @@ where
}
}

impl<C: BlockCipher + BlockEncrypt> StreamCipher for Cfb<C> {
impl<C: BlockCipher + BlockEncrypt> AsyncStreamCipher for Cfb<C> {
fn encrypt(&mut self, mut data: &mut [u8]) {
let bs = C::BlockSize::USIZE;
let n = data.len();
Expand Down
6 changes: 3 additions & 3 deletions cfb8/Cargo.toml
Expand Up @@ -12,9 +12,9 @@ readme = "README.md"
edition = "2018"

[dependencies]
cipher = "=0.3.0-pre"
cipher = "=0.3.0-pre.3"

[dev-dependencies]
aes = "=0.7.0-pre"
cipher = { version = "=0.3.0-pre", features = ["dev"] }
#aes = "=0.7.0-pre"
cipher = { version = "=0.3.0-pre.3", features = ["dev"] }
hex-literal = "0.2"
9 changes: 4 additions & 5 deletions cfb8/src/lib.rs
Expand Up @@ -10,7 +10,7 @@
//! ```
//! use aes::Aes128;
//! use cfb8::Cfb8;
//! use cfb8::cipher::{NewStreamCipher, StreamCipher};
//! use cfb8::cipher::{NewCipher, AsyncStreamCipher};
//! use hex_literal::hex;
//!
//! type AesCfb8 = Cfb8<Aes128>;
Expand Down Expand Up @@ -54,9 +54,8 @@
pub use cipher;

use cipher::{
block::{BlockCipher, BlockEncrypt, NewBlockCipher},
generic_array::GenericArray,
stream::{FromBlockCipher, Nonce, StreamCipher},
generic_array::GenericArray, AsyncStreamCipher, BlockCipher, BlockEncrypt, FromBlockCipher,
NewBlockCipher, Nonce,
};

/// CFB self-synchronizing stream cipher instance.
Expand All @@ -80,7 +79,7 @@ where
}
}

impl<C: BlockCipher + BlockEncrypt> StreamCipher for Cfb8<C> {
impl<C: BlockCipher + BlockEncrypt> AsyncStreamCipher for Cfb8<C> {
fn encrypt(&mut self, data: &mut [u8]) {
let mut iv = self.iv.clone();
let n = iv.len();
Expand Down
4 changes: 2 additions & 2 deletions chacha20/Cargo.toml
Expand Up @@ -18,15 +18,15 @@ edition = "2018"

[dependencies]
cfg-if = "1"
cipher = { version = "=0.3.0-pre", optional = true }
cipher = { version = "=0.3.0-pre.3", optional = true }
rand_core = { version = "0.5", optional = true, default-features = false }
zeroize = { version = "1", optional = true, default-features = false }

[target.'cfg(any(target_arch = "x86_64", target_arch = "x86"))'.dependencies]
cpuid-bool = "0.2"

[dev-dependencies]
cipher = { version = "=0.3.0-pre", features = ["dev"] }
cipher = { version = "=0.3.0-pre.3", features = ["dev"] }
hex-literal = "0.2"

[features]
Expand Down
15 changes: 7 additions & 8 deletions chacha20/src/chacha.rs
Expand Up @@ -11,9 +11,8 @@ use crate::{
};
use cipher::{
consts::{U12, U32},
stream::{
LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek,
},
errors::{LoopError, OverflowError},
NewCipher, SeekNum, StreamCipher, StreamCipherSeek,
};
use core::{
convert::TryInto,
Expand All @@ -37,12 +36,12 @@ pub type ChaCha20 = ChaCha<R20>;
/// Implemented as an alias for [`GenericArray`].
///
/// (NOTE: all variants of [`ChaCha20`] including `XChaCha20` use the same key type)
pub type Key = cipher::stream::Key<ChaCha20>;
pub type Key = cipher::CipherKey<ChaCha20>;

/// Nonce type (96-bits/12-bytes)
///
/// Implemented as an alias for [`GenericArray`].
pub type Nonce = cipher::stream::Nonce<ChaCha20>;
pub type Nonce = cipher::Nonce<ChaCha20>;

/// Internal buffer
type Buffer = [u8; BUFFER_SIZE];
Expand Down Expand Up @@ -78,7 +77,7 @@ pub struct ChaCha<R: Rounds> {
counter_offset: u64,
}

impl<R: Rounds> NewStreamCipher for ChaCha<R> {
impl<R: Rounds> NewCipher for ChaCha<R> {
/// Key size in bytes
type KeySize = U32;

Expand Down Expand Up @@ -106,7 +105,7 @@ impl<R: Rounds> NewStreamCipher for ChaCha<R> {
}
}

impl<R: Rounds> SyncStreamCipher for ChaCha<R> {
impl<R: Rounds> StreamCipher for ChaCha<R> {
fn try_apply_keystream(&mut self, mut data: &mut [u8]) -> Result<(), LoopError> {
self.check_data_len(data)?;
let pos = self.buffer_pos as usize;
Expand Down Expand Up @@ -147,7 +146,7 @@ impl<R: Rounds> SyncStreamCipher for ChaCha<R> {
}
}

impl<R: Rounds> SyncStreamCipherSeek for ChaCha<R> {
impl<R: Rounds> StreamCipherSeek for ChaCha<R> {
fn try_current_pos<T: SeekNum>(&self) -> Result<T, OverflowError> {
// quick and dirty fix, until ctr-like parallel block processing will be added
let (counter, pos) = if self.buffer_pos < BLOCK_SIZE as u8 {
Expand Down
13 changes: 6 additions & 7 deletions chacha20/src/legacy.rs
Expand Up @@ -3,22 +3,21 @@
use crate::chacha::{ChaCha20, Key};
use cipher::{
consts::{U32, U8},
stream::{
LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek,
},
errors::{LoopError, OverflowError},
NewCipher, SeekNum, StreamCipher, StreamCipherSeek,
};

/// Size of the nonce for the legacy ChaCha20 stream cipher
#[cfg_attr(docsrs, doc(cfg(feature = "legacy")))]
pub type LegacyNonce = cipher::stream::Nonce<ChaCha20Legacy>;
pub type LegacyNonce = cipher::Nonce<ChaCha20Legacy>;

/// The ChaCha20 stream cipher (legacy "djb" construction with 64-bit nonce).
///
/// The `legacy` Cargo feature must be enabled to use this.
#[cfg_attr(docsrs, doc(cfg(feature = "legacy")))]
pub struct ChaCha20Legacy(ChaCha20);

impl NewStreamCipher for ChaCha20Legacy {
impl NewCipher for ChaCha20Legacy {
/// Key size in bytes
type KeySize = U32;

Expand All @@ -32,13 +31,13 @@ impl NewStreamCipher for ChaCha20Legacy {
}
}

impl SyncStreamCipher for ChaCha20Legacy {
impl StreamCipher for ChaCha20Legacy {
fn try_apply_keystream(&mut self, data: &mut [u8]) -> Result<(), LoopError> {
self.0.try_apply_keystream(data)
}
}

impl SyncStreamCipherSeek for ChaCha20Legacy {
impl StreamCipherSeek for ChaCha20Legacy {
fn try_current_pos<T: SeekNum>(&self) -> Result<T, OverflowError> {
self.0.try_current_pos()
}
Expand Down
2 changes: 1 addition & 1 deletion chacha20/src/lib.rs
Expand Up @@ -42,7 +42,7 @@
//!
//! ```
//! use chacha20::{ChaCha20, Key, Nonce};
//! use chacha20::cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek};
//! use chacha20::cipher::{NewCipher, StreamCipher, StreamCipherSeek};
//!
//! let mut data = [1, 2, 3, 4, 5, 6, 7];
//!
Expand Down
13 changes: 6 additions & 7 deletions chacha20/src/xchacha.rs
Expand Up @@ -7,16 +7,15 @@ use crate::{
};
use cipher::{
consts::{U16, U24, U32},
errors::{LoopError, OverflowError},
generic_array::GenericArray,
stream::{
LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek,
},
NewCipher, SeekNum, StreamCipher, StreamCipherSeek,
};
use core::convert::TryInto;

/// EXtended ChaCha20 nonce (192-bits/24-bytes)
#[cfg_attr(docsrs, doc(cfg(feature = "xchacha20")))]
pub type XNonce = cipher::stream::Nonce<XChaCha20>;
pub type XNonce = cipher::Nonce<XChaCha20>;

/// XChaCha20 is a ChaCha20 variant with an extended 192-bit (24-byte) nonce.
///
Expand All @@ -39,7 +38,7 @@ pub type XNonce = cipher::stream::Nonce<XChaCha20>;
#[cfg_attr(docsrs, doc(cfg(feature = "xchacha20")))]
pub struct XChaCha20(ChaCha20);

impl NewStreamCipher for XChaCha20 {
impl NewCipher for XChaCha20 {
/// Key size in bytes
type KeySize = U32;

Expand All @@ -56,13 +55,13 @@ impl NewStreamCipher for XChaCha20 {
}
}

impl SyncStreamCipher for XChaCha20 {
impl StreamCipher for XChaCha20 {
fn try_apply_keystream(&mut self, data: &mut [u8]) -> Result<(), LoopError> {
self.0.try_apply_keystream(data)
}
}

impl SyncStreamCipherSeek for XChaCha20 {
impl StreamCipherSeek for XChaCha20 {
fn try_current_pos<T: SeekNum>(&self) -> Result<T, OverflowError> {
self.0.try_current_pos()
}
Expand Down

0 comments on commit 46a9dda

Please sign in to comment.