From 0e1efd5815e8d4ef7c10452366226dd278896587 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Fri, 16 Oct 2020 09:19:16 -0700 Subject: [PATCH] Replace `block-cipher`/`stream-cipher` with `cipher` crate This PR replaces all previous usages of the `block-cipher` and `stream-cipher` crates with the new unified `cipher` crate. See also: https://github.com/RustCrypto/traits/pull/337 --- Cargo.lock | 104 ++++++++-------------- README.md | 7 +- aes-ctr/Cargo.toml | 10 +-- aes-ctr/benches/aes128_ctr.rs | 2 +- aes-ctr/benches/aes192_ctr.rs | 2 +- aes-ctr/benches/aes256_ctr.rs | 2 +- aes-ctr/src/lib.rs | 12 +-- aes-ctr/tests/lib.rs | 9 +- benches/Cargo.toml | 2 +- benches/src/chacha20.rs | 4 +- cfb-mode/Cargo.toml | 8 +- cfb-mode/benches/cfb-mode.rs | 2 +- cfb-mode/src/lib.rs | 16 ++-- cfb-mode/tests/lib.rs | 7 +- cfb8/Cargo.toml | 8 +- cfb8/benches/cfb8.rs | 2 +- cfb8/src/lib.rs | 14 +-- cfb8/tests/lib.rs | 7 +- chacha20/Cargo.toml | 10 +-- chacha20/benches/chacha12.rs | 4 +- chacha20/benches/chacha20.rs | 4 +- chacha20/benches/chacha8.rs | 4 +- chacha20/src/block/avx2.rs | 2 +- chacha20/src/block/soft.rs | 2 +- chacha20/src/block/sse2.rs | 2 +- chacha20/src/{cipher.rs => chacha.rs} | 33 +++---- chacha20/src/legacy.rs | 6 +- chacha20/src/lib.rs | 38 ++++---- chacha20/src/{xchacha20.rs => xchacha.rs} | 12 +-- chacha20/tests/lib.rs | 17 ++-- ctr/Cargo.toml | 6 +- ctr/benches/aes128.rs | 2 +- ctr/src/ctr128.rs | 18 ++-- ctr/src/ctr32.rs | 16 ++-- ctr/src/lib.rs | 8 +- ctr/tests/ctr128/mod.rs | 8 +- ctr/tests/ctr32/big_endian.rs | 2 +- ctr/tests/ctr32/little_endian.rs | 2 +- hc-256/Cargo.toml | 4 +- hc-256/src/lib.rs | 8 +- hc-256/tests/lib.rs | 2 +- ofb/Cargo.toml | 8 +- ofb/benches/cfb-mode.rs | 2 +- ofb/src/lib.rs | 17 ++-- ofb/tests/lib.rs | 2 +- salsa20/Cargo.toml | 6 +- salsa20/benches/salsa20.rs | 2 +- salsa20/src/lib.rs | 32 +++---- salsa20/src/{cipher.rs => salsa.rs} | 18 ++-- salsa20/src/{xsalsa20.rs => xsalsa.rs} | 10 ++- salsa20/tests/lib.rs | 8 +- 51 files changed, 252 insertions(+), 281 deletions(-) rename chacha20/src/{cipher.rs => chacha.rs} (92%) rename chacha20/src/{xchacha20.rs => xchacha.rs} (95%) rename salsa20/src/{cipher.rs => salsa.rs} (87%) rename salsa20/src/{xsalsa20.rs => xsalsa.rs} (95%) diff --git a/Cargo.lock b/Cargo.lock index c0c43370..34341560 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,72 +2,52 @@ # It is not intended for manual editing. [[package]] name = "aes" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd2bc6d3f370b5666245ff421e231cba4353df936e26986d2918e61a8fd6aef6" +checksum = "884391ef1066acaa41e766ba8f596341b96e93ce34f9a43e7d24bf0a0eaf0561" dependencies = [ "aes-soft", - "aesni 0.8.0", - "block-cipher", + "aesni", + "cipher", ] [[package]] name = "aes-ctr" -version = "0.5.0" +version = "0.6.0-pre" dependencies = [ "aes-soft", - "aesni 0.9.0", + "aesni", + "cipher", "ctr", - "stream-cipher 0.8.0-pre", ] [[package]] name = "aes-soft" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63dd91889c49327ad7ef3b500fd1109dbd3c509a03db0d4a9ce413b79f575cb6" +checksum = "8c5ab1fa47ce0ddf44cbd65b1b4e626e3c03b06fd42005603acdc6fa13526296" dependencies = [ - "block-cipher", "byteorder", + "cipher", "opaque-debug", ] [[package]] name = "aesni" -version = "0.8.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a6fe808308bb07d393e2ea47780043ec47683fcf19cf5efc8ca51c50cc8c68a" +checksum = "ea2e11f5e94c2f7d386164cc2aa1f97823fed6f259e486940a71c174dd01b0ce" dependencies = [ - "block-cipher", + "cipher", "opaque-debug", ] -[[package]] -name = "aesni" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6a4d655ae633a96d0acaf0fd7e76aafb8ca5732739bba37aac6f882c8fce656" -dependencies = [ - "block-cipher", - "opaque-debug", - "stream-cipher 0.7.1", -] - [[package]] name = "blobby" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc52553543ecb104069b0ff9e0fcc5c739ad16202935528a112d974e8f1a4ee8" -[[package]] -name = "block-cipher" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f337a3e6da609650eb74e02bc9fac7b735049f7623ab12f2e4c719316fcc7e80" -dependencies = [ - "generic-array", -] - [[package]] name = "byteorder" version = "1.3.4" @@ -76,39 +56,49 @@ checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" [[package]] name = "cfb-mode" -version = "0.5.0" +version = "0.6.0-pre" dependencies = [ "aes", + "cipher", "hex-literal", - "stream-cipher 0.7.1", ] [[package]] name = "cfb8" -version = "0.5.0" +version = "0.6.0-pre" dependencies = [ "aes", + "cipher", "hex-literal", - "stream-cipher 0.7.1", ] [[package]] name = "chacha20" -version = "0.5.0" +version = "0.6.0-pre" dependencies = [ + "cipher", "hex-literal", "rand_core", - "stream-cipher 0.7.1", "zeroize", ] +[[package]] +name = "cipher" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1f7954ae5588102b35257639b1c36a2e7425cc6540fcdb4de19dcb91055d659" +dependencies = [ + "blobby", + "generic-array", +] + [[package]] name = "ctr" version = "0.6.0-pre" dependencies = [ "aes", + "cipher", "hex-literal", - "stream-cipher 0.8.0-pre", ] [[package]] @@ -123,9 +113,9 @@ dependencies = [ [[package]] name = "hc-256" -version = "0.2.0" +version = "0.3.0-pre" dependencies = [ - "stream-cipher 0.7.1", + "cipher", "zeroize", ] @@ -150,11 +140,11 @@ dependencies = [ [[package]] name = "ofb" -version = "0.3.0" +version = "0.4.0-pre" dependencies = [ "aes", + "cipher", "hex-literal", - "stream-cipher 0.7.1", ] [[package]] @@ -177,34 +167,12 @@ checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" [[package]] name = "salsa20" -version = "0.6.0" +version = "0.7.0-pre" dependencies = [ - "stream-cipher 0.7.1", + "cipher", "zeroize", ] -[[package]] -name = "stream-cipher" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c80e15f898d8d8f25db24c253ea615cc14acf418ff307822995814e7d42cfa89" -dependencies = [ - "blobby", - "block-cipher", - "generic-array", -] - -[[package]] -name = "stream-cipher" -version = "0.8.0-pre" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81c22786b7392254461cf5f358c143035828bf5d95850d4adc483faf065f5b9e" -dependencies = [ - "blobby", - "block-cipher", - "generic-array", -] - [[package]] name = "typenum" version = "1.12.0" diff --git a/README.md b/README.md index fce9988a..96bfe30f 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,7 @@ done with a minor version bump. ## Usage -Crates functionality is expressed in terms of traits defined in the -[`stream-cipher`][2] crate. +Crates functionality is expressed in terms of traits defined in the [`cipher`][2] crate. Let's use AES-128-OFB to demonstrate usage of synchronous stream cipher: @@ -44,7 +43,7 @@ use aes::Aes128; use ofb::Ofb; // import relevant traits -use ofb::stream_cipher::{NewStreamCipher, SyncStreamCipher}; +use ofb::cipher::{NewStreamCipher, SyncStreamCipher}; // OFB mode implementation is generic over block ciphers // we will create a type alias for convenience @@ -98,7 +97,7 @@ dual licensed as above, without any additional terms or conditions. [//]: # (footnotes) [1]: https://en.wikipedia.org/wiki/Stream_cipher -[2]: https://docs.rs/stream-cipher +[2]: https://docs.rs/cipher [//]: # (crates) diff --git a/aes-ctr/Cargo.toml b/aes-ctr/Cargo.toml index 4184e3e2..1a0523a4 100644 --- a/aes-ctr/Cargo.toml +++ b/aes-ctr/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aes-ctr" -version = "0.5.0" +version = "0.6.0-pre" description = "AES-CTR stream ciphers" authors = ["RustCrypto Developers"] license = "MIT OR Apache-2.0" @@ -12,14 +12,14 @@ readme = "README.md" edition = "2018" [dependencies] -stream-cipher = "0.8.0-pre" +cipher = "0.2" [target.'cfg(not(all(target_feature = "aes", target_feature = "sse2", target_feature = "ssse3", any(target_arch = "x86_64", target_arch = "x86"))))'.dependencies] ctr = { version = "0.6.0-pre", path = "../ctr" } -aes-soft = "0.5" +aes-soft = "0.6" [target.'cfg(all(target_feature = "aes", target_feature = "sse2", target_feature = "ssse3", any(target_arch = "x86_64", target_arch = "x86")))'.dependencies] -aesni = "0.9" +aesni = "0.10" [dev-dependencies] -stream-cipher = { version = "0.8.0-pre", features = ["dev"] } +cipher = { version = "0.2", features = ["dev"] } diff --git a/aes-ctr/benches/aes128_ctr.rs b/aes-ctr/benches/aes128_ctr.rs index 17c2db8e..4c533d3e 100644 --- a/aes-ctr/benches/aes128_ctr.rs +++ b/aes-ctr/benches/aes128_ctr.rs @@ -1,3 +1,3 @@ #![feature(test)] -stream_cipher::bench_sync!(aes_ctr::Aes128Ctr); +cipher::bench_sync!(aes_ctr::Aes128Ctr); diff --git a/aes-ctr/benches/aes192_ctr.rs b/aes-ctr/benches/aes192_ctr.rs index bb20ac89..b4454d92 100644 --- a/aes-ctr/benches/aes192_ctr.rs +++ b/aes-ctr/benches/aes192_ctr.rs @@ -1,3 +1,3 @@ #![feature(test)] -stream_cipher::bench_sync!(aes_ctr::Aes192Ctr); +cipher::bench_sync!(aes_ctr::Aes192Ctr); diff --git a/aes-ctr/benches/aes256_ctr.rs b/aes-ctr/benches/aes256_ctr.rs index 17c2db8e..4c533d3e 100644 --- a/aes-ctr/benches/aes256_ctr.rs +++ b/aes-ctr/benches/aes256_ctr.rs @@ -1,3 +1,3 @@ #![feature(test)] -stream_cipher::bench_sync!(aes_ctr::Aes128Ctr); +cipher::bench_sync!(aes_ctr::Aes128Ctr); diff --git a/aes-ctr/src/lib.rs b/aes-ctr/src/lib.rs index f112b331..4c362f8d 100644 --- a/aes-ctr/src/lib.rs +++ b/aes-ctr/src/lib.rs @@ -1,7 +1,7 @@ //! AES-CTR ciphers implementation. //! //! Cipher functionality is accessed using traits from re-exported -//! [`stream-cipher`](https://docs.rs/stream-cipher) crate. +//! [`cipher`](https://docs.rs/cipher) crate. //! //! This crate will select appropriate implementation at compile time depending //! on target architecture and enabled target features. For the best performance @@ -16,9 +16,11 @@ //! # Usage example //! ``` //! use aes_ctr::Aes128Ctr; -//! use aes_ctr::stream_cipher::generic_array::GenericArray; -//! use aes_ctr::stream_cipher::{ -//! NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek +//! use aes_ctr::cipher::{ +//! generic_array::GenericArray, +//! stream::{ +//! NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek +//! } //! }; //! //! let mut data = [1, 2, 3, 4, 5, 6, 7]; @@ -45,7 +47,7 @@ #![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")] #![warn(missing_docs, rust_2018_idioms)] -pub use stream_cipher; +pub use cipher; #[cfg(not(all( target_feature = "aes", diff --git a/aes-ctr/tests/lib.rs b/aes-ctr/tests/lib.rs index 90097d8e..67a67796 100644 --- a/aes-ctr/tests/lib.rs +++ b/aes-ctr/tests/lib.rs @@ -1,9 +1,8 @@ #![no_std] use aes_ctr::{Aes128Ctr, Aes256Ctr}; -use stream_cipher::{new_seek_test, new_sync_test}; -new_sync_test!(aes128_ctr_core, Aes128Ctr, "aes128-ctr"); -new_sync_test!(aes256_ctr_core, Aes256Ctr, "aes256-ctr"); -new_seek_test!(aes128_ctr_seek, Aes128Ctr); -new_seek_test!(aes256_ctr_seek, Aes256Ctr); +cipher::new_sync_test!(aes128_ctr_core, Aes128Ctr, "aes128-ctr"); +cipher::new_sync_test!(aes256_ctr_core, Aes256Ctr, "aes256-ctr"); +cipher::new_seek_test!(aes128_ctr_seek, Aes128Ctr); +cipher::new_seek_test!(aes256_ctr_seek, Aes256Ctr); diff --git a/benches/Cargo.toml b/benches/Cargo.toml index 88cd7a3a..d316eee3 100644 --- a/benches/Cargo.toml +++ b/benches/Cargo.toml @@ -12,7 +12,7 @@ publish = false [dev-dependencies] criterion = "0.3" criterion-cycles-per-byte = "0.1" -chacha20 = { path = "../chacha20/", features = ["stream-cipher"] } +chacha20 = { path = "../chacha20/", features = ["cipher"] } [[bench]] name = "chacha20" diff --git a/benches/src/chacha20.rs b/benches/src/chacha20.rs index 1b2bd1e4..60257eef 100644 --- a/benches/src/chacha20.rs +++ b/benches/src/chacha20.rs @@ -3,7 +3,7 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Through use criterion_cycles_per_byte::CyclesPerByte; use chacha20::{ - stream_cipher::{NewStreamCipher, SyncStreamCipher}, + cipher::{NewStreamCipher, SyncStreamCipher}, ChaCha20, }; @@ -34,4 +34,4 @@ criterion_group!( config = Criterion::default().with_measurement(CyclesPerByte); targets = bench ); -criterion_main!(benches); \ No newline at end of file +criterion_main!(benches); diff --git a/cfb-mode/Cargo.toml b/cfb-mode/Cargo.toml index 4dec6d1f..e9e185ce 100644 --- a/cfb-mode/Cargo.toml +++ b/cfb-mode/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cfb-mode" -version = "0.5.0" +version = "0.6.0-pre" description = "Generic Cipher Feedback (CFB) mode implementation." authors = ["RustCrypto Developers"] license = "MIT OR Apache-2.0" @@ -12,9 +12,9 @@ readme = "README.md" edition = "2018" [dependencies] -stream-cipher = { version = "0.7", features = ["block-cipher"] } +cipher = "0.2" [dev-dependencies] -aes = "0.5" -stream-cipher = { version = "0.7", features = ["dev"] } +aes = "0.6" +cipher = { version = "0.2", features = ["dev"] } hex-literal = "0.2" diff --git a/cfb-mode/benches/cfb-mode.rs b/cfb-mode/benches/cfb-mode.rs index fc7c1d50..0835430e 100644 --- a/cfb-mode/benches/cfb-mode.rs +++ b/cfb-mode/benches/cfb-mode.rs @@ -1,3 +1,3 @@ #![feature(test)] -stream_cipher::bench_async!(cfb_mode::Cfb); +cipher::bench_async!(cfb_mode::Cfb); diff --git a/cfb-mode/src/lib.rs b/cfb-mode/src/lib.rs index 8798ff76..1d3a2d10 100644 --- a/cfb-mode/src/lib.rs +++ b/cfb-mode/src/lib.rs @@ -10,7 +10,7 @@ //! ``` //! use aes::Aes128; //! use cfb_mode::Cfb; -//! use cfb_mode::stream_cipher::{NewStreamCipher, StreamCipher}; +//! use cfb_mode::cipher::{NewStreamCipher, StreamCipher}; //! use hex_literal::hex; //! //! type AesCfb = Cfb; @@ -50,13 +50,14 @@ )] #![warn(missing_docs, rust_2018_idioms)] -pub use stream_cipher; +pub use cipher; +use cipher::{ + block::{BlockCipher, NewBlockCipher, ParBlocks}, + generic_array::{typenum::Unsigned, GenericArray}, + stream::{FromBlockCipher, StreamCipher}, +}; use core::slice; -use stream_cipher::block_cipher::{BlockCipher, NewBlockCipher}; -use stream_cipher::generic_array::typenum::Unsigned; -use stream_cipher::generic_array::GenericArray; -use stream_cipher::{FromBlockCipher, StreamCipher}; /// CFB self-synchronizing stream cipher instance. pub struct Cfb { @@ -65,9 +66,6 @@ pub struct Cfb { pos: usize, } -type Block = GenericArray::BlockSize>; -type ParBlocks = GenericArray, ::ParBlocks>; - impl FromBlockCipher for Cfb where C: BlockCipher + NewBlockCipher, diff --git a/cfb-mode/tests/lib.rs b/cfb-mode/tests/lib.rs index 90aa4ba5..c63538e0 100644 --- a/cfb-mode/tests/lib.rs +++ b/cfb-mode/tests/lib.rs @@ -1,8 +1,7 @@ use cfb_mode::Cfb; -use stream_cipher::new_async_test; // tests vectors are from: // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf -new_async_test!(cfb_aes128, "aes128", Cfb); -new_async_test!(cfb_aes192, "aes192", Cfb); -new_async_test!(cfb_aes256, "aes256", Cfb); +cipher::new_async_test!(cfb_aes128, "aes128", Cfb); +cipher::new_async_test!(cfb_aes192, "aes192", Cfb); +cipher::new_async_test!(cfb_aes256, "aes256", Cfb); diff --git a/cfb8/Cargo.toml b/cfb8/Cargo.toml index ca237c68..a76ae36f 100644 --- a/cfb8/Cargo.toml +++ b/cfb8/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cfb8" -version = "0.5.0" +version = "0.6.0-pre" description = "Generic 8-bit Cipher Feedback (CFB8) mode implementation." authors = ["RustCrypto Developers"] license = "MIT OR Apache-2.0" @@ -12,9 +12,9 @@ readme = "README.md" edition = "2018" [dependencies] -stream-cipher = { version = "0.7", features = ["block-cipher"] } +cipher = "0.2" [dev-dependencies] -aes = "0.5" -stream-cipher = { version = "0.7", features = ["dev"] } +aes = "0.6" +cipher = { version = "0.2", features = ["dev"] } hex-literal = "0.2" diff --git a/cfb8/benches/cfb8.rs b/cfb8/benches/cfb8.rs index 093a9827..71e805e4 100644 --- a/cfb8/benches/cfb8.rs +++ b/cfb8/benches/cfb8.rs @@ -1,3 +1,3 @@ #![feature(test)] -stream_cipher::bench_async!(cfb8::Cfb8); +cipher::bench_async!(cfb8::Cfb8); diff --git a/cfb8/src/lib.rs b/cfb8/src/lib.rs index 8af1ab6b..7eec148c 100644 --- a/cfb8/src/lib.rs +++ b/cfb8/src/lib.rs @@ -10,7 +10,7 @@ //! ``` //! use aes::Aes128; //! use cfb8::Cfb8; -//! use cfb8::stream_cipher::{NewStreamCipher, StreamCipher}; +//! use cfb8::cipher::{NewStreamCipher, StreamCipher}; //! use hex_literal::hex; //! //! type AesCfb8 = Cfb8; @@ -51,11 +51,13 @@ #![deny(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] -pub use stream_cipher; +pub use cipher; -use stream_cipher::block_cipher::{BlockCipher, NewBlockCipher}; -use stream_cipher::generic_array::GenericArray; -use stream_cipher::{FromBlockCipher, StreamCipher}; +use cipher::{ + block::{BlockCipher, NewBlockCipher}, + generic_array::GenericArray, + stream::{FromBlockCipher, StreamCipher, Nonce}, +}; /// CFB self-synchronizing stream cipher instance. pub struct Cfb8 { @@ -70,7 +72,7 @@ where type BlockCipher = C; type NonceSize = C::BlockSize; - fn from_block_cipher(cipher: C, iv: &GenericArray) -> Self { + fn from_block_cipher(cipher: C, iv: &Nonce) -> Self { Self { cipher, iv: iv.clone(), diff --git a/cfb8/tests/lib.rs b/cfb8/tests/lib.rs index 05de030d..86d4ce45 100644 --- a/cfb8/tests/lib.rs +++ b/cfb8/tests/lib.rs @@ -1,8 +1,7 @@ use cfb8::Cfb8; -use stream_cipher::new_async_test; // tests vectors are from: // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf -new_async_test!(cfb8_aes128, "aes128", Cfb8); -new_async_test!(cfb8_aes192, "aes192", Cfb8); -new_async_test!(cfb8_aes256, "aes256", Cfb8); +cipher::new_async_test!(cfb8_aes128, "aes128", Cfb8); +cipher::new_async_test!(cfb8_aes192, "aes192", Cfb8); +cipher::new_async_test!(cfb8_aes256, "aes256", Cfb8); diff --git a/chacha20/Cargo.toml b/chacha20/Cargo.toml index e48e3b41..0aad505f 100644 --- a/chacha20/Cargo.toml +++ b/chacha20/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "chacha20" -version = "0.5.0" +version = "0.6.0-pre" authors = ["RustCrypto Developers"] license = "Apache-2.0 OR MIT" description = """ @@ -17,18 +17,18 @@ readme = "README.md" edition = "2018" [dependencies] +cipher = { version = "0.2", optional = true } rand_core = { version = "0.5", optional = true, default-features = false } -stream-cipher = { version = "0.7", optional = true } zeroize = { version = "1", optional = true, default-features = false } [dev-dependencies] -stream-cipher = { version = "0.7", features = ["dev"] } +cipher = { version = "0.2", features = ["dev"] } hex-literal = "0.2" [features] default = ["xchacha20"] -legacy = ["stream-cipher"] -xchacha20 = ["stream-cipher"] +legacy = ["cipher"] +xchacha20 = ["cipher"] rng = ["rand_core"] [package.metadata.docs.rs] diff --git a/chacha20/benches/chacha12.rs b/chacha20/benches/chacha12.rs index f5389f6a..d1c6835d 100644 --- a/chacha20/benches/chacha12.rs +++ b/chacha20/benches/chacha12.rs @@ -1,4 +1,4 @@ -#![cfg(feature = "stream-cipher")] +#![cfg(feature = "cipher")] #![feature(test)] -stream_cipher::bench_sync!(chacha20::ChaCha12); +cipher::bench_sync!(chacha20::ChaCha12); diff --git a/chacha20/benches/chacha20.rs b/chacha20/benches/chacha20.rs index 57da8f3b..94fbc846 100644 --- a/chacha20/benches/chacha20.rs +++ b/chacha20/benches/chacha20.rs @@ -1,4 +1,4 @@ -#![cfg(feature = "stream-cipher")] +#![cfg(feature = "cipher")] #![feature(test)] -stream_cipher::bench_sync!(chacha20::ChaCha20); +cipher::bench_sync!(chacha20::ChaCha20); diff --git a/chacha20/benches/chacha8.rs b/chacha20/benches/chacha8.rs index 74193ead..66ff4cc7 100644 --- a/chacha20/benches/chacha8.rs +++ b/chacha20/benches/chacha8.rs @@ -1,4 +1,4 @@ -#![cfg(feature = "stream-cipher")] +#![cfg(feature = "cipher")] #![feature(test)] -stream_cipher::bench_sync!(chacha20::ChaCha8); +cipher::bench_sync!(chacha20::ChaCha8); diff --git a/chacha20/src/block/avx2.rs b/chacha20/src/block/avx2.rs index 4ece41b7..57a4bf4d 100644 --- a/chacha20/src/block/avx2.rs +++ b/chacha20/src/block/avx2.rs @@ -61,7 +61,7 @@ impl Block { } #[inline] - #[cfg(feature = "stream-cipher")] + #[cfg(feature = "cipher")] #[allow(clippy::cast_ptr_alignment)] // loadu/storeu support unaligned loads/stores pub(crate) fn apply_keystream(&self, counter: u64, output: &mut [u8]) { debug_assert_eq!(output.len(), BUFFER_SIZE); diff --git a/chacha20/src/block/soft.rs b/chacha20/src/block/soft.rs index 70bc4954..fcb1fc0b 100644 --- a/chacha20/src/block/soft.rs +++ b/chacha20/src/block/soft.rs @@ -68,7 +68,7 @@ impl Block { /// Apply generated keystream to the output buffer #[inline] - #[cfg(feature = "stream-cipher")] + #[cfg(feature = "cipher")] pub(crate) fn apply_keystream(&mut self, counter: u64, output: &mut [u8]) { debug_assert_eq!(output.len(), BUFFER_SIZE); self.counter_setup(counter); diff --git a/chacha20/src/block/sse2.rs b/chacha20/src/block/sse2.rs index 78af12eb..e0d24780 100644 --- a/chacha20/src/block/sse2.rs +++ b/chacha20/src/block/sse2.rs @@ -58,7 +58,7 @@ impl Block { } #[inline] - #[cfg(feature = "stream-cipher")] + #[cfg(feature = "cipher")] #[allow(clippy::cast_ptr_alignment)] // loadu/storeu support unaligned loads/stores pub(crate) fn apply_keystream(&self, counter: u64, output: &mut [u8]) { debug_assert_eq!(output.len(), BUFFER_SIZE); diff --git a/chacha20/src/cipher.rs b/chacha20/src/chacha.rs similarity index 92% rename from chacha20/src/cipher.rs rename to chacha20/src/chacha.rs index 9ce8fab5..ed10a05b 100644 --- a/chacha20/src/cipher.rs +++ b/chacha20/src/chacha.rs @@ -9,35 +9,38 @@ use crate::{ rounds::{Rounds, R12, R20, R8}, BLOCK_SIZE, MAX_BLOCKS, }; +use cipher::stream::{ + consts::{U12, U32}, + LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, +}; use core::{ convert::TryInto, fmt::{self, Debug}, }; -use stream_cipher::{ - consts::{U12, U32}, - LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, -}; + +#[cfg(docsrs)] +use cipher::generic_array::GenericArray; /// ChaCha8 stream cipher (reduced-round variant of ChaCha20 with 8 rounds) -pub type ChaCha8 = Cipher; +pub type ChaCha8 = ChaCha; /// ChaCha12 stream cipher (reduced-round variant of ChaCha20 with 12 rounds) -pub type ChaCha12 = Cipher; +pub type ChaCha12 = ChaCha; /// ChaCha20 stream cipher (RFC 8439 version with 96-bit nonce) -pub type ChaCha20 = Cipher; +pub type ChaCha20 = ChaCha; /// ChaCha20 key type (256-bits/32-bytes) /// /// Implemented as an alias for [`GenericArray`]. /// /// (NOTE: all variants of [`ChaCha20`] including `XChaCha20` use the same key type) -pub type Key = stream_cipher::Key; +pub type Key = cipher::stream::Key; /// Nonce type (96-bits/12-bytes) /// /// Implemented as an alias for [`GenericArray`]. -pub type Nonce = stream_cipher::Nonce; +pub type Nonce = cipher::stream::Nonce; /// Internal buffer type Buffer = [u8; BUFFER_SIZE]; @@ -54,7 +57,7 @@ const COUNTER_INCR: u64 = (BUFFER_SIZE as u64) / (BLOCK_SIZE as u64); /// a specific number of rounds. /// /// Generally [`ChaCha20`] is preferred. -pub struct Cipher { +pub struct ChaCha { /// ChaCha20 block function initialized with a key and IV block: Block, @@ -73,7 +76,7 @@ pub struct Cipher { counter_offset: u64, } -impl NewStreamCipher for Cipher { +impl NewStreamCipher for ChaCha { /// Key size in bytes type KeySize = U32; @@ -101,7 +104,7 @@ impl NewStreamCipher for Cipher { } } -impl SyncStreamCipher for Cipher { +impl SyncStreamCipher for ChaCha { fn try_apply_keystream(&mut self, mut data: &mut [u8]) -> Result<(), LoopError> { self.check_data_len(data)?; let pos = self.buffer_pos as usize; @@ -142,7 +145,7 @@ impl SyncStreamCipher for Cipher { } } -impl SyncStreamCipherSeek for Cipher { +impl SyncStreamCipherSeek for ChaCha { fn try_current_pos(&self) -> Result { // quick and dirty fix, until ctr-like parallel block processing will be added let (counter, pos) = if self.buffer_pos < BLOCK_SIZE as u8 { @@ -167,7 +170,7 @@ impl SyncStreamCipherSeek for Cipher { } } -impl Cipher { +impl ChaCha { /// Check data length fn check_data_len(&self, data: &[u8]) -> Result<(), LoopError> { let leftover_bytes = BUFFER_SIZE - self.buffer_pos as usize; @@ -192,7 +195,7 @@ impl Cipher { } } -impl Debug for Cipher { +impl Debug for ChaCha { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { write!(f, "Cipher {{ .. }}") } diff --git a/chacha20/src/legacy.rs b/chacha20/src/legacy.rs index 91c3f2be..3a7b773f 100644 --- a/chacha20/src/legacy.rs +++ b/chacha20/src/legacy.rs @@ -1,14 +1,14 @@ //! Legacy version of ChaCha20 with a 64-bit nonce -use crate::cipher::{ChaCha20, Key}; -use stream_cipher::{ +use crate::chacha::{ChaCha20, Key}; +use cipher::stream::{ consts::{U32, U8}, LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, }; /// Size of the nonce for the legacy ChaCha20 stream cipher #[cfg_attr(docsrs, doc(cfg(feature = "legacy")))] -pub type LegacyNonce = stream_cipher::Nonce; +pub type LegacyNonce = cipher::stream::Nonce; /// The ChaCha20 stream cipher (legacy "djb" construction with 64-bit nonce). /// diff --git a/chacha20/src/lib.rs b/chacha20/src/lib.rs index 4a9ad154..af034cf6 100644 --- a/chacha20/src/lib.rs +++ b/chacha20/src/lib.rs @@ -6,21 +6,24 @@ //! with no cost to performance. //! //! Cipher functionality is accessed using traits from re-exported -//! [`stream-cipher`](https://docs.rs/stream-cipher) crate. +//! [`cipher`](https://docs.rs/cipher) crate. //! -//! This crate contains three variants of ChaCha20: +//! This crate contains the following variants of the ChaCha20 core algorithm: //! -//! - `ChaCha20`: standard IETF variant with 96-bit nonce -//! - `ChaCha20Legacy`: (gated under the `legacy` feature) "djb" variant with 64-bit nonce -//! - `ChaCha8` / `ChaCha12`: reduced round variants of ChaCha20 -//! - `XChaCha20`: (gated under the `xchacha20` feature) 192-bit extended nonce variant +//! - [`ChaCha20`]: standard IETF variant with 96-bit nonce +//! - [`ChaCha20Legacy`]: (gated under the `legacy` feature) "djb" variant with 64-bit nonce +//! - [`ChaCha8`] / [`ChaCha12`]: reduced round variants of ChaCha20 +//! - [`XChaCha20`]: (gated under the `xchacha20` feature) 192-bit extended nonce variant //! -//! # Security Warning +//! # ⚠️ Security Warning: [Hazmat!] //! //! This crate does not ensure ciphertexts are authentic, which can lead to //! serious vulnerabilities if used incorrectly! //! -//! USE AT YOUR OWN RISK! +//! If in doubt, use the [`chacha20poly1305`](https://docs.rs/chacha20poly1305) +//! crate instead, which provides an authenticated mode on top of ChaCha20. +//! +//! **USE AT YOUR OWN RISK!** //! //! # Diagram //! @@ -39,7 +42,7 @@ //! //! ``` //! use chacha20::{ChaCha20, Key, Nonce}; -//! use chacha20::stream_cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek}; +//! use chacha20::cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek}; //! //! let mut data = [1, 2, 3, 4, 5, 6, 7]; //! @@ -61,6 +64,7 @@ //! //! [RFC 8439]: https://tools.ietf.org/html/rfc8439 //! [Salsa20]: https://docs.rs/salsa20 +//! [Hazmat!]: https://github.com/RustCrypto/meta/blob/master/HAZMAT.md #![no_std] #![doc( @@ -71,21 +75,21 @@ #![warn(missing_docs, rust_2018_idioms, trivial_casts, unused_qualifications)] mod block; -#[cfg(feature = "stream-cipher")] -pub(crate) mod cipher; +#[cfg(feature = "cipher")] +mod chacha; #[cfg(feature = "legacy")] mod legacy; #[cfg(feature = "rng")] mod rng; mod rounds; #[cfg(feature = "xchacha20")] -mod xchacha20; +mod xchacha; -#[cfg(feature = "stream-cipher")] -pub use stream_cipher; +#[cfg(feature = "cipher")] +pub use cipher; -#[cfg(feature = "stream-cipher")] -pub use self::cipher::{ChaCha12, ChaCha20, ChaCha8, Cipher, Key, Nonce}; +#[cfg(feature = "cipher")] +pub use self::chacha::{ChaCha, ChaCha12, ChaCha20, ChaCha8, Key, Nonce}; #[cfg(feature = "legacy")] pub use self::legacy::{ChaCha20Legacy, LegacyNonce}; @@ -96,7 +100,7 @@ pub use rng::{ }; #[cfg(feature = "xchacha20")] -pub use self::xchacha20::{XChaCha20, XNonce}; +pub use self::xchacha::{XChaCha20, XNonce}; /// Size of a ChaCha20 block in bytes pub const BLOCK_SIZE: usize = 64; diff --git a/chacha20/src/xchacha20.rs b/chacha20/src/xchacha.rs similarity index 95% rename from chacha20/src/xchacha20.rs rename to chacha20/src/xchacha.rs index 37bcc4c9..412a9800 100644 --- a/chacha20/src/xchacha20.rs +++ b/chacha20/src/xchacha.rs @@ -2,19 +2,21 @@ use crate::{ block::soft::quarter_round, - cipher::{ChaCha20, Key}, + chacha::{ChaCha20, Key}, CONSTANTS, }; -use core::convert::TryInto; -use stream_cipher::{ +use cipher::{ consts::{U16, U24, U32}, generic_array::GenericArray, - LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, + stream::{ + LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, + }, }; +use core::convert::TryInto; /// EXtended ChaCha20 nonce (192-bits/24-bytes) #[cfg_attr(docsrs, doc(cfg(feature = "xchacha20")))] -pub type XNonce = stream_cipher::Nonce; +pub type XNonce = cipher::stream::Nonce; /// XChaCha20 is a ChaCha20 variant with an extended 192-bit (24-byte) nonce. /// diff --git a/chacha20/tests/lib.rs b/chacha20/tests/lib.rs index 093feb98..68f53186 100644 --- a/chacha20/tests/lib.rs +++ b/chacha20/tests/lib.rs @@ -1,21 +1,19 @@ //! Tests for ChaCha20 (IETF and "djb" versions) as well as XChaCha20 use chacha20::ChaCha20; -use stream_cipher::{new_seek_test, new_sync_test}; // IETF version of ChaCha20 (96-bit nonce) -new_sync_test!(chacha20_core, ChaCha20, "chacha20"); -new_seek_test!(chacha20_seek, ChaCha20); +cipher::new_sync_test!(chacha20_core, ChaCha20, "chacha20"); +cipher::new_seek_test!(chacha20_seek, ChaCha20); #[cfg(feature = "xchacha20")] #[rustfmt::skip] mod xchacha20 { use chacha20::{Key, XChaCha20, XNonce}; - use stream_cipher::{NewStreamCipher, StreamCipher}; + use cipher::stream::{NewStreamCipher, StreamCipher}; use hex_literal::hex; - use stream_cipher::new_seek_test; - new_seek_test!(xchacha20_seek, XChaCha20); + cipher::new_seek_test!(xchacha20_seek, XChaCha20); // // XChaCha20 test vectors from: @@ -101,12 +99,11 @@ mod xchacha20 { #[rustfmt::skip] mod legacy { use chacha20::{ChaCha20Legacy, Key, LegacyNonce}; - use stream_cipher::{new_seek_test, new_sync_test}; - use stream_cipher::{NewStreamCipher, StreamCipher, SyncStreamCipherSeek}; + use cipher::stream::{NewStreamCipher, StreamCipher, SyncStreamCipherSeek}; use hex_literal::hex; - new_sync_test!(chacha20_legacy_core, ChaCha20Legacy, "chacha20-legacy"); - new_seek_test!(chacha20_legacy_seek, ChaCha20Legacy); + cipher::new_sync_test!(chacha20_legacy_core, ChaCha20Legacy, "chacha20-legacy"); + cipher::new_seek_test!(chacha20_legacy_seek, ChaCha20Legacy); const KEY_LONG: [u8; 32] = hex!(" 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 diff --git a/ctr/Cargo.toml b/ctr/Cargo.toml index f67648e3..87d2ce84 100644 --- a/ctr/Cargo.toml +++ b/ctr/Cargo.toml @@ -12,9 +12,9 @@ readme = "README.md" edition = "2018" [dependencies] -stream-cipher = { version = "0.8.0-pre", features = ["block-cipher"] } +cipher = "0.2" [dev-dependencies] -aes = "0.5" +aes = "0.6" hex-literal = "0.2" -stream-cipher = { version = "0.8.0-pre", features = ["block-cipher", "dev"] } +cipher = { version = "0.2", features = ["dev"] } diff --git a/ctr/benches/aes128.rs b/ctr/benches/aes128.rs index 2bef5fcd..5f2bfeb0 100644 --- a/ctr/benches/aes128.rs +++ b/ctr/benches/aes128.rs @@ -1,3 +1,3 @@ #![feature(test)] -stream_cipher::bench_sync!(ctr::Ctr128); +cipher::bench_sync!(ctr::Ctr128); diff --git a/ctr/src/ctr128.rs b/ctr/src/ctr128.rs index 0fc4b967..f55f1473 100644 --- a/ctr/src/ctr128.rs +++ b/ctr/src/ctr128.rs @@ -1,14 +1,16 @@ //! Generic implementation of CTR mode for block cipher with 128-bit block size. -use core::{convert::TryInto, fmt, mem}; -use stream_cipher::{ - block_cipher::{BlockCipher, NewBlockCipher}, +use cipher::{ + block::{BlockCipher, NewBlockCipher, Block, ParBlocks}, generic_array::{ typenum::{Unsigned, U16}, ArrayLength, GenericArray, }, - FromBlockCipher, LoopError, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, + stream::{ + FromBlockCipher, LoopError, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, + }, }; +use core::{convert::TryInto, fmt, mem}; #[inline(always)] fn xor(buf: &mut [u8], key: &[u8]) { @@ -18,8 +20,6 @@ fn xor(buf: &mut [u8], key: &[u8]) { } } -type Block = GenericArray::BlockSize>; -type Blocks = GenericArray, ::ParBlocks>; type Nonce = GenericArray; /// CTR mode of operation for 128-bit block ciphers @@ -141,10 +141,10 @@ where C::ParBlocks: ArrayLength>, { #[inline(always)] - fn generate_par_blocks(&self, counter: u64) -> Blocks { + fn generate_par_blocks(&self, counter: u64) -> ParBlocks { let mut block = self.nonce; block[1] = block[1].wrapping_add(counter); - let mut blocks: Blocks = unsafe { mem::zeroed() }; + let mut blocks: ParBlocks = unsafe { mem::zeroed() }; for b in blocks.iter_mut() { let block_be = conv_be(block); *b = unsafe { mem::transmute_copy(&block_be) }; @@ -195,7 +195,7 @@ fn conv_be(val: [u64; 2]) -> [u64; 2] { } #[inline(always)] -fn to_slice(blocks: &Blocks) -> &[u8] { +fn to_slice(blocks: &ParBlocks) -> &[u8] { let blocks_len = C::BlockSize::to_usize() * C::ParBlocks::to_usize(); unsafe { core::slice::from_raw_parts(blocks.as_ptr() as *const u8, blocks_len) } } diff --git a/ctr/src/ctr32.rs b/ctr/src/ctr32.rs index d92ae576..959b8d20 100644 --- a/ctr/src/ctr32.rs +++ b/ctr/src/ctr32.rs @@ -1,16 +1,12 @@ //! Generic implementation of CTR mode with a 32-bit counter //! (big or little endian), generic over block ciphers. -use core::{convert::TryInto, marker::PhantomData, mem}; -use stream_cipher::{ - block_cipher::{Block, BlockCipher}, +use cipher::{ + block::{Block, BlockCipher, ParBlocks}, generic_array::{typenum::Unsigned, ArrayLength, GenericArray}, - FromBlockCipher, LoopError, SyncStreamCipher, + stream::{FromBlockCipher, LoopError, SyncStreamCipher}, }; - -/// Internal buffer for a given block cipher -type BlockBuffer = GenericArray, ::ParBlocks>; - +use core::{convert::TryInto, marker::PhantomData, mem}; /// CTR mode with a 32-bit big endian counter. /// /// Used by e.g. AES-GCM. @@ -72,7 +68,7 @@ where } } -/// Implement `stream-cipher` traits for the given `Ctr32*` type +/// Implement stream cipher traits for the given `Ctr32*` type macro_rules! impl_ctr32 { ($ctr32:tt) => { impl SyncStreamCipher for $ctr32 @@ -130,7 +126,7 @@ where cipher: B, /// Keystream buffer - buffer: BlockBuffer, + buffer: ParBlocks, /// Current CTR value counter_block: Block, diff --git a/ctr/src/lib.rs b/ctr/src/lib.rs index 3845b300..46b09346 100644 --- a/ctr/src/lib.rs +++ b/ctr/src/lib.rs @@ -1,7 +1,7 @@ //! Generic implementations of CTR mode for block ciphers. //! //! Mode functionality is accessed using traits from re-exported -//! [`stream-cipher`](https://docs.rs/stream-cipher) crate. +//! [`cipher`](https://docs.rs/cipher) crate. //! //! # Security Warning //! This crate does not ensure ciphertexts are authentic! Thus ciphertext integrity @@ -9,8 +9,8 @@ //! //! # `Ctr128` Usage Example //! ``` -//! use ctr::stream_cipher::generic_array::GenericArray; -//! use ctr::stream_cipher::{ +//! use ctr::cipher::generic_array::GenericArray; +//! use ctr::cipher::{ //! NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek //! }; //! @@ -49,4 +49,4 @@ pub use crate::{ ctr128::Ctr128, ctr32::{Ctr32BE, Ctr32LE}, }; -pub use stream_cipher; +pub use cipher; diff --git a/ctr/tests/ctr128/mod.rs b/ctr/tests/ctr128/mod.rs index 16de6f71..f1b037e9 100644 --- a/ctr/tests/ctr128/mod.rs +++ b/ctr/tests/ctr128/mod.rs @@ -1,7 +1,7 @@ type Aes128Ctr = ctr::Ctr128; type Aes256Ctr = ctr::Ctr128; -stream_cipher::new_sync_test!(aes128_ctr_core, Aes128Ctr, "aes128-ctr"); -stream_cipher::new_sync_test!(aes256_ctr_core, Aes256Ctr, "aes256-ctr"); -stream_cipher::new_seek_test!(aes128_ctr_seek, Aes128Ctr); -stream_cipher::new_seek_test!(aes256_ctr_seek, Aes256Ctr); +cipher::new_sync_test!(aes128_ctr_core, Aes128Ctr, "aes128-ctr"); +cipher::new_sync_test!(aes256_ctr_core, Aes256Ctr, "aes256-ctr"); +cipher::new_seek_test!(aes128_ctr_seek, Aes128Ctr); +cipher::new_seek_test!(aes256_ctr_seek, Aes256Ctr); diff --git a/ctr/tests/ctr32/big_endian.rs b/ctr/tests/ctr32/big_endian.rs index d2ef989c..e2608ace 100644 --- a/ctr/tests/ctr32/big_endian.rs +++ b/ctr/tests/ctr32/big_endian.rs @@ -1,7 +1,7 @@ //! Counter Mode with a 32-bit big endian counter +use cipher::stream::{NewStreamCipher, SyncStreamCipher}; use hex_literal::hex; -use stream_cipher::{NewStreamCipher, SyncStreamCipher}; type Aes128Ctr = ctr::Ctr32BE; diff --git a/ctr/tests/ctr32/little_endian.rs b/ctr/tests/ctr32/little_endian.rs index fac93821..0689839a 100644 --- a/ctr/tests/ctr32/little_endian.rs +++ b/ctr/tests/ctr32/little_endian.rs @@ -1,7 +1,7 @@ //! Counter Mode with a 32-bit little endian counter +use cipher::stream::{NewStreamCipher, SyncStreamCipher}; use hex_literal::hex; -use stream_cipher::{NewStreamCipher, SyncStreamCipher}; type Aes128Ctr = ctr::Ctr32LE; diff --git a/hc-256/Cargo.toml b/hc-256/Cargo.toml index 307fc739..ccf81782 100644 --- a/hc-256/Cargo.toml +++ b/hc-256/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hc-256" -version = "0.2.0" +version = "0.3.0-pre" authors = ["Eric McCorkle "] license = "MIT OR Apache-2.0" description = "HC-256 Stream Cipher" @@ -11,5 +11,5 @@ readme = "README.md" edition = "2018" [dependencies] -stream-cipher = "0.7" +cipher = "0.2" zeroize = { version = "1", optional = true } diff --git a/hc-256/src/lib.rs b/hc-256/src/lib.rs index af47a480..767ac171 100644 --- a/hc-256/src/lib.rs +++ b/hc-256/src/lib.rs @@ -8,11 +8,11 @@ #![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] -pub use stream_cipher; +pub use cipher; -use stream_cipher::consts::U32; -use stream_cipher::generic_array::GenericArray; -use stream_cipher::{NewStreamCipher, StreamCipher}; +use cipher::stream::consts::U32; +use cipher::stream::generic_array::GenericArray; +use cipher::stream::{NewStreamCipher, StreamCipher}; #[cfg(cargo_feature = "zeroize")] use std::ops::Drop; diff --git a/hc-256/tests/lib.rs b/hc-256/tests/lib.rs index db789379..415b7e05 100644 --- a/hc-256/tests/lib.rs +++ b/hc-256/tests/lib.rs @@ -1,5 +1,5 @@ +use cipher::stream::{generic_array::GenericArray, NewStreamCipher, StreamCipher}; use hc_256::Hc256; -use stream_cipher::{generic_array::GenericArray, NewStreamCipher, StreamCipher}; #[cfg(test)] const KEY_BYTES: usize = 256 / 8; diff --git a/ofb/Cargo.toml b/ofb/Cargo.toml index e81aec21..ededd0a2 100644 --- a/ofb/Cargo.toml +++ b/ofb/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ofb" -version = "0.3.0" +version = "0.4.0-pre" authors = ["RustCrypto Developers"] license = "MIT OR Apache-2.0" description = "Generic Output Feedback (OFB) mode implementation." @@ -12,9 +12,9 @@ readme = "README.md" edition = "2018" [dependencies] -stream-cipher = { version = "0.7", features = ["block-cipher"] } +cipher = "0.2" [dev-dependencies] -aes = "0.5" -stream-cipher = { version = "0.7", features = ["dev"] } +aes = "0.6" +cipher = { version = "0.2", features = ["dev"] } hex-literal = "0.2" diff --git a/ofb/benches/cfb-mode.rs b/ofb/benches/cfb-mode.rs index c7d753a3..22b9c30f 100644 --- a/ofb/benches/cfb-mode.rs +++ b/ofb/benches/cfb-mode.rs @@ -1,3 +1,3 @@ #![feature(test)] -stream_cipher::bench_sync!(ofb::Ofb); +cipher::bench_sync!(ofb::Ofb); diff --git a/ofb/src/lib.rs b/ofb/src/lib.rs index 846d91df..93471805 100644 --- a/ofb/src/lib.rs +++ b/ofb/src/lib.rs @@ -11,7 +11,7 @@ //! use aes::Aes128; //! use hex_literal::hex; //! use ofb::Ofb; -//! use ofb::stream_cipher::{NewStreamCipher, SyncStreamCipher}; +//! use ofb::cipher::{NewStreamCipher, SyncStreamCipher}; //! //! type AesOfb = Ofb; //! @@ -53,14 +53,13 @@ #![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms)] -pub use stream_cipher; +pub use cipher; -use stream_cipher::block_cipher::{BlockCipher, NewBlockCipher}; -use stream_cipher::generic_array::typenum::Unsigned; -use stream_cipher::generic_array::GenericArray; -use stream_cipher::{FromBlockCipher, LoopError, SyncStreamCipher}; - -type Block = GenericArray::BlockSize>; +use cipher::{ + block::{Block, BlockCipher, NewBlockCipher}, + generic_array::typenum::Unsigned, + stream::{FromBlockCipher, LoopError, Nonce, SyncStreamCipher}, +}; /// OFB self-synchronizing stream cipher instance. pub struct Ofb { @@ -76,7 +75,7 @@ where type BlockCipher = C; type NonceSize = C::BlockSize; - fn from_block_cipher(cipher: C, iv: &GenericArray) -> Self { + fn from_block_cipher(cipher: C, iv: &Nonce) -> Self { let mut block = iv.clone(); cipher.encrypt_block(&mut block); Self { diff --git a/ofb/tests/lib.rs b/ofb/tests/lib.rs index 1c6823f0..85de6f1f 100644 --- a/ofb/tests/lib.rs +++ b/ofb/tests/lib.rs @@ -1 +1 @@ -stream_cipher::new_sync_test!(ofb_aes128, ofb::Ofb, "aes128"); +cipher::new_sync_test!(ofb_aes128, ofb::Ofb, "aes128"); diff --git a/salsa20/Cargo.toml b/salsa20/Cargo.toml index abdf6026..9f7fcb35 100644 --- a/salsa20/Cargo.toml +++ b/salsa20/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "salsa20" -version = "0.6.0" +version = "0.7.0-pre" authors = ["RustCrypto Developers"] license = "MIT OR Apache-2.0" description = "Salsa20 Stream Cipher" @@ -11,11 +11,11 @@ readme = "README.md" edition = "2018" [dependencies] -stream-cipher = "0.7" +cipher = "0.2" zeroize = { version = "1", optional = true } [dev-dependencies] -stream-cipher = { version = "0.7", features = ["dev"] } +cipher = { version = "0.2", features = ["dev"] } [features] default = ["xsalsa20"] diff --git a/salsa20/benches/salsa20.rs b/salsa20/benches/salsa20.rs index 7e642a45..503a152d 100755 --- a/salsa20/benches/salsa20.rs +++ b/salsa20/benches/salsa20.rs @@ -1,3 +1,3 @@ #![feature(test)] -stream_cipher::bench_sync!(salsa20::Salsa20); +cipher::bench_sync!(salsa20::Salsa20); diff --git a/salsa20/src/lib.rs b/salsa20/src/lib.rs index 3ed676db..2d6d4e9c 100644 --- a/salsa20/src/lib.rs +++ b/salsa20/src/lib.rs @@ -1,7 +1,7 @@ //! The Salsa20 stream cipher. //! //! Cipher functionality is accessed using traits from re-exported -//! [`stream-cipher`](https://docs.rs/stream-cipher) crate. +//! [`cipher`](https://docs.rs/cipher) crate. //! //! # Security Warning //! @@ -27,7 +27,7 @@ //! //! ``` //! use salsa20::{Salsa20, Key, Nonce}; -//! use salsa20::stream_cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek}; +//! use salsa20::cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek}; //! //! let mut data = [1, 2, 3, 4, 5, 6, 7]; //! @@ -56,30 +56,32 @@ #![deny(unsafe_code)] #![warn(missing_docs, rust_2018_idioms, trivial_casts, unused_qualifications)] -pub use stream_cipher; +pub use cipher; mod block; -mod cipher; mod rounds; +mod salsa; #[cfg(feature = "xsalsa20")] -mod xsalsa20; +mod xsalsa; #[cfg(feature = "xsalsa20")] -pub use self::xsalsa20::{XNonce, XSalsa20}; +pub use self::xsalsa::{XNonce, XSalsa20}; #[cfg(feature = "hsalsa20")] -pub use self::xsalsa20::hsalsa20; +pub use self::xsalsa::hsalsa20; use crate::{ block::Block, - cipher::Cipher, rounds::{Rounds, R12, R20, R8}, + salsa::SalsaCore, }; -use core::convert::TryInto; -use stream_cipher::{ +use cipher::{ consts::{U32, U8}, - LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, + stream::{ + LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, + }, }; +use core::convert::TryInto; /// Size of a Salsa20 block in bytes pub const BLOCK_SIZE: usize = 64; @@ -113,19 +115,19 @@ pub type Salsa20 = Salsa; /// Implemented as an alias for [`GenericArray`]. /// /// (NOTE: all three round variants use the same key size) -pub type Key = stream_cipher::Key; +pub type Key = cipher::stream::Key; /// Nonce type. /// /// Implemented as an alias for [`GenericArray`]. -pub type Nonce = stream_cipher::Nonce; +pub type Nonce = cipher::stream::Nonce; /// The Salsa20 family of stream ciphers /// (implemented generically over a number of rounds). /// /// We recommend you use the [`Salsa20`] (a.k.a. Salsa20/20) variant. #[derive(Debug)] -pub struct Salsa(Cipher); +pub struct Salsa(SalsaCore); impl NewStreamCipher for Salsa { /// Key size in bytes @@ -136,7 +138,7 @@ impl NewStreamCipher for Salsa { fn new(key: &Key, nonce: &Nonce) -> Self { let block = Block::new(key.as_slice().try_into().unwrap(), (*nonce).into()); - Salsa(Cipher::new(block)) + Salsa(SalsaCore::new(block)) } } diff --git a/salsa20/src/cipher.rs b/salsa20/src/salsa.rs similarity index 87% rename from salsa20/src/cipher.rs rename to salsa20/src/salsa.rs index f48ff2ab..72dfadd0 100644 --- a/salsa20/src/cipher.rs +++ b/salsa20/src/salsa.rs @@ -5,15 +5,15 @@ // TODO(tarcieri): figure out how to unify this with the `ctr` crate (see #95) use crate::{block::Block, rounds::Rounds, BLOCK_SIZE}; +use cipher::stream::{LoopError, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek}; use core::fmt; -use stream_cipher::{LoopError, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek}; /// Internal buffer type Buffer = [u8; BLOCK_SIZE]; -/// ChaCha20 as a counter mode stream cipher -pub(crate) struct Cipher { - /// ChaCha20 block function initialized with a key and IV +/// The Salsa20 core function +pub(crate) struct SalsaCore { + /// Salsa block function initialized with a key and IV block: Block, /// Buffer containing previous block function output @@ -26,7 +26,7 @@ pub(crate) struct Cipher { counter: u64, } -impl Cipher { +impl SalsaCore { /// Create new CTR mode cipher from the given block and starting counter pub fn new(block: Block) -> Self { Self { @@ -38,7 +38,7 @@ impl Cipher { } } -impl SyncStreamCipher for Cipher { +impl SyncStreamCipher for SalsaCore { fn try_apply_keystream(&mut self, mut data: &mut [u8]) -> Result<(), LoopError> { self.check_data_len(data)?; let pos = self.buffer_pos as usize; @@ -78,7 +78,7 @@ impl SyncStreamCipher for Cipher { } } -impl SyncStreamCipherSeek for Cipher { +impl SyncStreamCipherSeek for SalsaCore { fn try_current_pos(&self) -> Result { T::from_block_byte(self.counter, self.buffer_pos, BLOCK_SIZE as u8) } @@ -94,7 +94,7 @@ impl SyncStreamCipherSeek for Cipher { } } -impl Cipher { +impl SalsaCore { fn check_data_len(&self, data: &[u8]) -> Result<(), LoopError> { let leftover_bytes = BLOCK_SIZE - self.buffer_pos as usize; if data.len() < leftover_bytes { @@ -108,7 +108,7 @@ impl Cipher { } } -impl fmt::Debug for Cipher { +impl fmt::Debug for SalsaCore { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { write!(f, "Cipher {{ .. }}") } diff --git a/salsa20/src/xsalsa20.rs b/salsa20/src/xsalsa.rs similarity index 95% rename from salsa20/src/xsalsa20.rs rename to salsa20/src/xsalsa.rs index 4d26c842..3f1fd0be 100644 --- a/salsa20/src/xsalsa20.rs +++ b/salsa20/src/xsalsa.rs @@ -1,16 +1,18 @@ //! XSalsa20 is an extended nonce variant of Salsa20 use crate::{block::quarter_round, Key, Salsa20, CONSTANTS, IV_SIZE}; -use core::convert::TryInto; -use stream_cipher::{ +use cipher::{ consts::{U16, U24, U32}, generic_array::GenericArray, - LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, + stream::{ + LoopError, NewStreamCipher, OverflowError, SeekNum, SyncStreamCipher, SyncStreamCipherSeek, + }, }; +use core::convert::TryInto; /// EXtended Salsa20 nonce (192-bit/24-byte) #[cfg_attr(docsrs, doc(cfg(feature = "xsalsa20")))] -pub type XNonce = stream_cipher::Nonce; +pub type XNonce = cipher::stream::Nonce; /// XSalsa20 is a Salsa20 variant with an extended 192-bit (24-byte) nonce. /// diff --git a/salsa20/tests/lib.rs b/salsa20/tests/lib.rs index 2a7a94cb..ed9f8f92 100644 --- a/salsa20/tests/lib.rs +++ b/salsa20/tests/lib.rs @@ -1,14 +1,14 @@ //! Salsa20 tests +use cipher::stream::generic_array::GenericArray; +use cipher::stream::{NewStreamCipher, StreamCipher, SyncStreamCipherSeek}; use salsa20::Salsa20; #[cfg(feature = "xsalsa20")] use salsa20::XSalsa20; -use stream_cipher::generic_array::GenericArray; -use stream_cipher::{new_seek_test, NewStreamCipher, StreamCipher, SyncStreamCipherSeek}; -new_seek_test!(salsa20_seek, Salsa20); +cipher::new_seek_test!(salsa20_seek, Salsa20); #[cfg(feature = "xsalsa20")] -new_seek_test!(xsalsa20_seek, XSalsa20); +cipher::new_seek_test!(xsalsa20_seek, XSalsa20); #[cfg(test)] const KEY_BYTES: usize = 32;