Skip to content

Commit 54ded88

Browse files
authored
Add blanket trait impls for references (#210)
1 parent 8d39a09 commit 54ded88

File tree

8 files changed

+71
-45
lines changed

8 files changed

+71
-45
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

block-cipher/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "block-cipher"
33
description = "Traits for description of block ciphers"
4-
version = "0.7.1"
4+
version = "0.8.0"
55
authors = ["RustCrypto Developers"]
66
license = "MIT OR Apache-2.0"
77
readme = "README.md"
@@ -13,7 +13,7 @@ categories = ["cryptography", "no-std"]
1313

1414
[dependencies]
1515
generic-array = "0.14"
16-
blobby = { version = "0.2", optional = true }
16+
blobby = { version = "0.3", optional = true }
1717

1818
[features]
1919
std = []

block-cipher/src/dev.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,28 @@ macro_rules! new_test {
7373
let pb = <$cipher as BlockCipher>::ParBlocks::to_usize();
7474
let data = include_bytes!(concat!("data/", $test_name, ".blb"));
7575
for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() {
76-
let key = row[0];
77-
let plaintext = row[1];
78-
let ciphertext = row[2];
79-
if !run_test(key, plaintext, ciphertext) {
76+
let [key, pt, ct] = row.unwrap();
77+
if !run_test(key, pt, ct) {
8078
panic!(
8179
"\n\
8280
Failed test №{}\n\
8381
key:\t{:?}\n\
8482
plaintext:\t{:?}\n\
8583
ciphertext:\t{:?}\n",
86-
i, key, plaintext, ciphertext,
84+
i, key, pt, ct,
8785
);
8886
}
8987

9088
// test parallel blocks encryption/decryption
9189
if pb != 1 {
92-
if !run_par_test(key, plaintext) {
90+
if !run_par_test(key, pt) {
9391
panic!(
9492
"\n\
9593
Failed parallel test №{}\n\
9694
key:\t{:?}\n\
9795
plaintext:\t{:?}\n\
9896
ciphertext:\t{:?}\n",
99-
i, key, plaintext, ciphertext,
97+
i, key, pt, ct,
10098
);
10199
}
102100
}

block-cipher/src/lib.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,38 @@ pub trait BlockCipherMut {
118118
impl<Alg: BlockCipher> BlockCipherMut for Alg {
119119
type BlockSize = Alg::BlockSize;
120120

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

126-
/// Decrypt block in-place
126+
#[inline]
127127
fn decrypt_block(&mut self, block: &mut GenericArray<u8, Self::BlockSize>) {
128128
<Self as BlockCipher>::decrypt_block(self, block);
129129
}
130130
}
131+
132+
impl<Alg: BlockCipher> BlockCipher for &Alg {
133+
type BlockSize = Alg::BlockSize;
134+
type ParBlocks = Alg::ParBlocks;
135+
136+
#[inline]
137+
fn encrypt_block(&self, block: &mut Block<Self>) {
138+
Alg::encrypt_block(self, block);
139+
}
140+
141+
#[inline]
142+
fn decrypt_block(&self, block: &mut Block<Self>) {
143+
Alg::decrypt_block(self, block);
144+
}
145+
146+
#[inline]
147+
fn encrypt_blocks(&self, blocks: &mut ParBlocks<Self>) {
148+
Alg::encrypt_blocks(self, blocks);
149+
}
150+
151+
#[inline]
152+
fn decrypt_blocks(&self, blocks: &mut ParBlocks<Self>) {
153+
Alg::decrypt_blocks(self, blocks);
154+
}
155+
}

cryptography/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ edition = "2018"
1313

1414
[dependencies]
1515
aead = { version = "0.3", optional = true, path = "../aead" }
16-
block-cipher = { version = "0.7", optional = true, path = "../block-cipher" }
16+
block-cipher = { version = "0.8", optional = true, path = "../block-cipher" }
1717
digest = { version = "0.9", optional = true, path = "../digest" }
1818
mac = { version = "0.8", package = "crypto-mac", optional = true, path = "../crypto-mac" }
1919
signature = { version = "1.1.0", optional = true, default-features = false, path = "../signature" }
20-
stream-cipher = { version = "0.5", optional = true, path = "../stream-cipher" }
20+
stream-cipher = { version = "0.6", optional = true, path = "../stream-cipher" }
2121
universal-hash = { version = "0.4", optional = true, path = "../universal-hash" }
2222

2323
[package.metadata.docs.rs]

stream-cipher/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "stream-cipher"
33
description = "Stream cipher traits"
4-
version = "0.5.0"
4+
version = "0.6.0"
55
authors = ["RustCrypto Developers"]
66
license = "MIT OR Apache-2.0"
77
readme = "README.md"
@@ -13,10 +13,10 @@ categories = ["cryptography", "no-std"]
1313

1414
[dependencies]
1515
generic-array = "0.14"
16-
blobby = { version = "0.2", optional = true }
16+
blobby = { version = "0.3", optional = true }
1717

1818
[dependencies.block-cipher]
19-
version = "0.7"
19+
version = "0.8"
2020
optional = true
2121
path = "../block-cipher"
2222

stream-cipher/src/dev.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,22 @@ macro_rules! new_sync_test {
1515

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

2320
for chunk_n in 1..256 {
2421
let mut mode = <$cipher>::new_var(key, iv).unwrap();
25-
let mut pt = plaintext.to_vec();
22+
let mut pt = pt.to_vec();
2623
for chunk in pt.chunks_mut(chunk_n) {
2724
mode.apply_keystream(chunk);
2825
}
29-
if pt != &ciphertext[..] {
26+
if pt != &ct[..] {
3027
panic!(
3128
"Failed main test №{}, chunk size: {}\n\
32-
key:\t{:?}\n\
33-
iv:\t{:?}\n\
34-
plaintext:\t{:?}\n\
35-
ciphertext:\t{:?}\n",
36-
i, chunk_n, key, iv, plaintext, ciphertext,
29+
key:\t{:?}\n\
30+
iv:\t{:?}\n\
31+
plaintext:\t{:?}\n\
32+
ciphertext:\t{:?}\n",
33+
i, chunk_n, key, iv, pt, ct,
3734
);
3835
}
3936
}
@@ -57,26 +54,23 @@ macro_rules! new_seek_test {
5754

5855
let data = include_bytes!(concat!("data/", $test_name, ".blb"));
5956
for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() {
60-
let key = row[0];
61-
let iv = row[1];
62-
let plaintext = row[2];
63-
let ciphertext = row[3];
57+
let [key, iv, pt, ct] = row.unwrap();
6458

6559
let mut mode = <$cipher>::new_var(key, iv).unwrap();
66-
let pl = plaintext.len();
60+
let pl = pt.len();
6761
let n = if pl > MAX_SEEK { MAX_SEEK } else { pl };
6862
for seek_n in 0..n {
69-
let mut pt = plaintext[seek_n..].to_vec();
63+
let mut pt = pt[seek_n..].to_vec();
7064
mode.seek(seek_n as u64);
7165
mode.apply_keystream(&mut pt);
72-
if pt != &ciphertext[seek_n..] {
66+
if pt != &ct[seek_n..] {
7367
panic!(
7468
"Failed seek test №{}, seek pos: {}\n\
7569
key:\t{:?}\n\
7670
iv:\t{:?}\n\
7771
plaintext:\t{:?}\n\
7872
ciphertext:\t{:?}\n",
79-
i, seek_n, key, iv, plaintext, ciphertext,
73+
i, seek_n, key, iv, pt, ct,
8074
);
8175
}
8276
}
@@ -130,19 +124,16 @@ macro_rules! new_async_test {
130124
let data = include_bytes!(concat!("data/", $test_name, ".blb"));
131125

132126
for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() {
133-
let key = row[0];
134-
let iv = row[1];
135-
let plaintext = row[2];
136-
let ciphertext = row[3];
137-
if let Some(desc) = run_test(key, iv, plaintext, ciphertext) {
127+
let [key, iv, pt, ct] = row.unwrap();
128+
if let Some(desc) = run_test(key, iv, pt, ct) {
138129
panic!(
139130
"\n\
140131
Failed test №{}: {}\n\
141132
key:\t{:?}\n\
142133
iv:\t{:?}\n\
143134
plaintext:\t{:?}\n\
144135
ciphertext:\t{:?}\n",
145-
i, desc, key, iv, plaintext, ciphertext,
136+
i, desc, key, iv, pt, ct,
146137
);
147138
}
148139
}

stream-cipher/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ impl<C: SyncStreamCipher> StreamCipher for C {
125125
}
126126
}
127127

128+
impl<C: SyncStreamCipher> SyncStreamCipher for &mut C {
129+
#[inline]
130+
fn apply_keystream(&mut self, data: &mut [u8]) {
131+
C::apply_keystream(self, data);
132+
}
133+
134+
#[inline]
135+
fn try_apply_keystream(&mut self, data: &mut [u8]) -> Result<(), LoopError> {
136+
C::try_apply_keystream(self, data)
137+
}
138+
}
139+
128140
/// Trait for initializing a stream cipher from a block cipher
129141
#[cfg(feature = "block-cipher")]
130142
#[cfg_attr(docsrs, doc(cfg(feature = "block-cipher")))]

0 commit comments

Comments
 (0)