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

Add blanket trait impls for references #210

Merged
merged 6 commits into from
Jul 10, 2020
Merged
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
8 changes: 4 additions & 4 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions block-cipher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "block-cipher"
description = "Traits for description of block ciphers"
version = "0.7.1"
version = "0.8.0"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -13,7 +13,7 @@ categories = ["cryptography", "no-std"]

[dependencies]
generic-array = "0.14"
blobby = { version = "0.2", optional = true }
blobby = { version = "0.3", optional = true }

[features]
std = []
Expand Down
12 changes: 5 additions & 7 deletions block-cipher/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,28 @@ macro_rules! new_test {
let pb = <$cipher as BlockCipher>::ParBlocks::to_usize();
let data = include_bytes!(concat!("data/", $test_name, ".blb"));
for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() {
let key = row[0];
let plaintext = row[1];
let ciphertext = row[2];
if !run_test(key, plaintext, ciphertext) {
let [key, pt, ct] = row.unwrap();
if !run_test(key, pt, ct) {
panic!(
"\n\
Failed test №{}\n\
key:\t{:?}\n\
plaintext:\t{:?}\n\
ciphertext:\t{:?}\n",
i, key, plaintext, ciphertext,
i, key, pt, ct,
);
}

// test parallel blocks encryption/decryption
if pb != 1 {
if !run_par_test(key, plaintext) {
if !run_par_test(key, pt) {
panic!(
"\n\
Failed parallel test №{}\n\
key:\t{:?}\n\
plaintext:\t{:?}\n\
ciphertext:\t{:?}\n",
i, key, plaintext, ciphertext,
i, key, pt, ct,
);
}
}
Expand Down
29 changes: 27 additions & 2 deletions block-cipher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,38 @@ pub trait BlockCipherMut {
impl<Alg: BlockCipher> BlockCipherMut for Alg {
type BlockSize = Alg::BlockSize;

/// Encrypt block in-place
#[inline]
fn encrypt_block(&mut self, block: &mut GenericArray<u8, Self::BlockSize>) {
<Self as BlockCipher>::encrypt_block(self, block);
}

/// Decrypt block in-place
#[inline]
fn decrypt_block(&mut self, block: &mut GenericArray<u8, Self::BlockSize>) {
<Self as BlockCipher>::decrypt_block(self, block);
}
}

impl<Alg: BlockCipher> BlockCipher for &Alg {
type BlockSize = Alg::BlockSize;
type ParBlocks = Alg::ParBlocks;

#[inline]
fn encrypt_block(&self, block: &mut Block<Self>) {
Alg::encrypt_block(self, block);
}

#[inline]
fn decrypt_block(&self, block: &mut Block<Self>) {
Alg::decrypt_block(self, block);
}

#[inline]
fn encrypt_blocks(&self, blocks: &mut ParBlocks<Self>) {
Alg::encrypt_blocks(self, blocks);
}

#[inline]
fn decrypt_blocks(&self, blocks: &mut ParBlocks<Self>) {
Alg::decrypt_blocks(self, blocks);
}
}
4 changes: 2 additions & 2 deletions cryptography/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ edition = "2018"

[dependencies]
aead = { version = "0.3", optional = true, path = "../aead" }
block-cipher = { version = "0.7", optional = true, path = "../block-cipher" }
block-cipher = { version = "0.8", optional = true, path = "../block-cipher" }
digest = { version = "0.9", optional = true, path = "../digest" }
mac = { version = "0.8", package = "crypto-mac", optional = true, path = "../crypto-mac" }
signature = { version = "1.1.0", optional = true, default-features = false, path = "../signature" }
stream-cipher = { version = "0.5", optional = true, path = "../stream-cipher" }
stream-cipher = { version = "0.6", optional = true, path = "../stream-cipher" }
universal-hash = { version = "0.4", optional = true, path = "../universal-hash" }

[package.metadata.docs.rs]
Expand Down
6 changes: 3 additions & 3 deletions stream-cipher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "stream-cipher"
description = "Stream cipher traits"
version = "0.5.0"
version = "0.6.0"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -13,10 +13,10 @@ categories = ["cryptography", "no-std"]

[dependencies]
generic-array = "0.14"
blobby = { version = "0.2", optional = true }
blobby = { version = "0.3", optional = true }

[dependencies.block-cipher]
version = "0.7"
version = "0.8"
optional = true
path = "../block-cipher"

Expand Down
41 changes: 16 additions & 25 deletions stream-cipher/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,22 @@ macro_rules! new_sync_test {

let data = include_bytes!(concat!("data/", $test_name, ".blb"));
for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() {
let key = row[0];
let iv = row[1];
let plaintext = row[2];
let ciphertext = row[3];
let [key, iv, pt, ct] = row.unwrap();

for chunk_n in 1..256 {
let mut mode = <$cipher>::new_var(key, iv).unwrap();
let mut pt = plaintext.to_vec();
let mut pt = pt.to_vec();
for chunk in pt.chunks_mut(chunk_n) {
mode.apply_keystream(chunk);
}
if pt != &ciphertext[..] {
if pt != &ct[..] {
panic!(
"Failed main test №{}, chunk size: {}\n\
key:\t{:?}\n\
iv:\t{:?}\n\
plaintext:\t{:?}\n\
ciphertext:\t{:?}\n",
i, chunk_n, key, iv, plaintext, ciphertext,
key:\t{:?}\n\
iv:\t{:?}\n\
plaintext:\t{:?}\n\
ciphertext:\t{:?}\n",
i, chunk_n, key, iv, pt, ct,
);
}
}
Expand All @@ -57,26 +54,23 @@ macro_rules! new_seek_test {

let data = include_bytes!(concat!("data/", $test_name, ".blb"));
for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() {
let key = row[0];
let iv = row[1];
let plaintext = row[2];
let ciphertext = row[3];
let [key, iv, pt, ct] = row.unwrap();

let mut mode = <$cipher>::new_var(key, iv).unwrap();
let pl = plaintext.len();
let pl = pt.len();
let n = if pl > MAX_SEEK { MAX_SEEK } else { pl };
for seek_n in 0..n {
let mut pt = plaintext[seek_n..].to_vec();
let mut pt = pt[seek_n..].to_vec();
mode.seek(seek_n as u64);
mode.apply_keystream(&mut pt);
if pt != &ciphertext[seek_n..] {
if pt != &ct[seek_n..] {
panic!(
"Failed seek test №{}, seek pos: {}\n\
key:\t{:?}\n\
iv:\t{:?}\n\
plaintext:\t{:?}\n\
ciphertext:\t{:?}\n",
i, seek_n, key, iv, plaintext, ciphertext,
i, seek_n, key, iv, pt, ct,
);
}
}
Expand Down Expand Up @@ -130,19 +124,16 @@ macro_rules! new_async_test {
let data = include_bytes!(concat!("data/", $test_name, ".blb"));

for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() {
let key = row[0];
let iv = row[1];
let plaintext = row[2];
let ciphertext = row[3];
if let Some(desc) = run_test(key, iv, plaintext, ciphertext) {
let [key, iv, pt, ct] = row.unwrap();
if let Some(desc) = run_test(key, iv, pt, ct) {
panic!(
"\n\
Failed test №{}: {}\n\
key:\t{:?}\n\
iv:\t{:?}\n\
plaintext:\t{:?}\n\
ciphertext:\t{:?}\n",
i, desc, key, iv, plaintext, ciphertext,
i, desc, key, iv, pt, ct,
);
}
}
Expand Down
12 changes: 12 additions & 0 deletions stream-cipher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ impl<C: SyncStreamCipher> StreamCipher for C {
}
}

impl<C: SyncStreamCipher> SyncStreamCipher for &mut C {
#[inline]
fn apply_keystream(&mut self, data: &mut [u8]) {
C::apply_keystream(self, data);
}

#[inline]
fn try_apply_keystream(&mut self, data: &mut [u8]) -> Result<(), LoopError> {
C::try_apply_keystream(self, data)
}
}

/// Trait for initializing a stream cipher from a block cipher
#[cfg(feature = "block-cipher")]
#[cfg_attr(docsrs, doc(cfg(feature = "block-cipher")))]
Expand Down