Skip to content

Commit 3286053

Browse files
committed
update cipher
1 parent e048e5a commit 3286053

File tree

12 files changed

+135
-156
lines changed

12 files changed

+135
-156
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ members = [
1414
"threefish",
1515
]
1616

17+
[profile.dev]
18+
opt-level = 2
19+
1720
[patch.crates-io]
18-
cipher = { git = "https://github.com/RustCrypto/traits/", branch = "new_traits" }
19-
block-buffer = { git = "https://github.com/RustCrypto/utils", branch = "pad_error" }
20-
inout = { git = "https://github.com/RustCrypto/utils", branch = "pad_error" }
21+
cipher = { git = "https://github.com/RustCrypto/traits/", branch = "cipher_v0.4" }
22+
inout = { git = "https://github.com/RustCrypto/utils", branch = "add_inout" }

aes/src/autodetect.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cipher::{
66
consts::{U16, U24, U32},
77
crypto_common::AlgorithmName,
88
generic_array::GenericArray,
9-
inout::{InOut, InOutBuf, InSrc, InTmpOutBuf},
9+
inout::{InOut, InCtrl, ChunkProc},
1010
BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, KeySizeUser,
1111
};
1212
use core::fmt;
@@ -102,23 +102,23 @@ macro_rules! define_aes_impl {
102102
}
103103

104104
#[inline]
105-
fn encrypt_blocks_with_pre(
105+
fn encrypt_blocks_with_gen<B: ChunkProc<Block>>(
106106
&self,
107-
blocks: InOutBuf<'_, Block>,
108-
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
109-
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
107+
blocks: B,
108+
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
109+
body: impl FnMut(B, &mut [Block]),
110110
) {
111111
if self.token.get() {
112112
unsafe {
113113
self.inner
114114
.intrinsics
115-
.encrypt_blocks_with_pre(blocks, pre_fn, post_fn)
115+
.encrypt_blocks_with_gen(blocks, gen_in, body)
116116
}
117117
} else {
118118
unsafe {
119119
self.inner
120120
.soft
121-
.encrypt_blocks_with_pre(blocks, pre_fn, post_fn)
121+
.encrypt_blocks_with_gen(blocks, gen_in, body)
122122
}
123123
}
124124
}
@@ -135,23 +135,23 @@ macro_rules! define_aes_impl {
135135
}
136136

137137
#[inline]
138-
fn decrypt_blocks_with_pre(
139-
&self,
140-
blocks: InOutBuf<'_, Block>,
141-
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
142-
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
143-
) {
138+
fn decrypt_blocks_with_gen<B: ChunkProc<Block>>(
139+
&self,
140+
blocks: B,
141+
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
142+
body: impl FnMut(B, &mut [Block]),
143+
) {
144144
if self.token.get() {
145145
unsafe {
146146
self.inner
147147
.intrinsics
148-
.decrypt_blocks_with_pre(blocks, pre_fn, post_fn)
148+
.decrypt_blocks_with_gen(blocks, gen_in, body)
149149
}
150150
} else {
151151
unsafe {
152152
self.inner
153153
.soft
154-
.decrypt_blocks_with_pre(blocks, pre_fn, post_fn)
154+
.decrypt_blocks_with_gen(blocks, gen_in, body)
155155
}
156156
}
157157
}

aes/src/lib.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,14 @@
6969
//! // number of blocks processed in parallel depends in general
7070
//! // on hardware capabilities
7171
//! let mut blocks = [block; 100];
72-
//! cipher.encrypt_blocks(
73-
//! &mut blocks,
74-
//! |chunk| {
75-
//! // you can process encrypted chunk here, e.g. for MAC
76-
//! },
77-
//! );
72+
//! cipher.encrypt_blocks(&mut blocks);
7873
//!
7974
//! for block in blocks.iter_mut() {
8075
//! cipher.decrypt_block(block);
8176
//! assert_eq!(block, &block_copy);
8277
//! }
8378
//!
84-
//! cipher.decrypt_blocks(
85-
//! &mut blocks,
86-
//! |chunk| {
87-
//! // you can process decrypted chunk here
88-
//! },
89-
//! );
79+
//! cipher.decrypt_blocks(&mut blocks);
9080
//!
9181
//! for block in blocks.iter_mut() {
9282
//! cipher.encrypt_block(block);

aes/src/ni.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use cipher::{
3838
consts::{U16, U24, U32, U8},
3939
crypto_common::AlgorithmName,
4040
generic_array::{typenum::Unsigned, GenericArray},
41-
inout::{InOut, InOutBuf, InSrc, InTmpOutBuf},
41+
inout::{InOut, InCtrl, ChunkProc},
4242
BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, KeySizeUser,
4343
};
4444
use core::fmt;
@@ -97,23 +97,23 @@ macro_rules! define_aes_impl {
9797
}
9898

9999
#[inline]
100-
fn encrypt_blocks_with_pre(
100+
fn encrypt_blocks_with_gen<B: ChunkProc<Block>>(
101101
&self,
102-
blocks: InOutBuf<'_, Block>,
103-
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
104-
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
102+
blocks: B,
103+
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
104+
body: impl FnMut(B, &mut [Block]),
105105
) {
106106
#[target_feature(enable = "aes")]
107-
unsafe fn inner(
107+
unsafe fn inner<B: ChunkProc<Block>>(
108108
keys: &$module::RoundKeys,
109-
blocks: InOutBuf<'_, Block>,
110-
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
111-
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
109+
blocks: B,
110+
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
111+
body: impl FnMut(B, &mut [Block]),
112112
) {
113113
blocks.process_chunks::<U8, _, _, _, _, _>(
114114
&keys,
115-
pre_fn,
116-
post_fn,
115+
gen_in,
116+
body,
117117
|keys, chunk| $module::encrypt8(keys, chunk),
118118
|keys, chunk| {
119119
for block in chunk {
@@ -126,7 +126,7 @@ macro_rules! define_aes_impl {
126126
// SAFETY: we enforce that this code is called only when
127127
// required target features were properly checked.
128128
unsafe {
129-
inner(&self.encrypt_keys, blocks, pre_fn, post_fn);
129+
inner(&self.encrypt_keys, blocks, gen_in, body);
130130
}
131131
}
132132
}
@@ -142,23 +142,23 @@ macro_rules! define_aes_impl {
142142
}
143143

144144
#[inline]
145-
fn decrypt_blocks_with_pre(
145+
fn decrypt_blocks_with_gen<B: ChunkProc<Block>>(
146146
&self,
147-
blocks: InOutBuf<'_, Block>,
148-
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
149-
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
147+
blocks: B,
148+
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
149+
body: impl FnMut(B, &mut [Block]),
150150
) {
151151
#[target_feature(enable = "aes")]
152-
unsafe fn inner(
152+
unsafe fn inner<B: ChunkProc<Block>>(
153153
keys: &$module::RoundKeys,
154-
blocks: InOutBuf<'_, Block>,
155-
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
156-
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
154+
blocks: B,
155+
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
156+
body: impl FnMut(B, &mut [Block]),
157157
) {
158158
blocks.process_chunks::<U8, _, _, _, _, _>(
159159
&keys,
160-
pre_fn,
161-
post_fn,
160+
gen_in,
161+
body,
162162
|keys, chunk| $module::decrypt8(keys, chunk),
163163
|keys, chunk| {
164164
for block in chunk {
@@ -171,7 +171,7 @@ macro_rules! define_aes_impl {
171171
// SAFETY: we enforce that this code is called only when
172172
// required target features were properly checked.
173173
unsafe {
174-
inner(&self.decrypt_keys, blocks, pre_fn, post_fn);
174+
inner(&self.decrypt_keys, blocks, gen_in, body);
175175
}
176176
}
177177
}

aes/src/soft.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use cipher::{
1717
consts::{U16, U24, U32},
1818
crypto_common::AlgorithmName,
1919
generic_array::GenericArray,
20-
inout::{InOut, InOutBuf, InSrc, InTmpOutBuf},
20+
inout::{InOut, InCtrl, ChunkProc},
2121
BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, KeySizeUser,
2222
};
2323
use core::fmt;
@@ -60,27 +60,30 @@ macro_rules! define_aes_impl {
6060

6161
impl BlockEncrypt for $name {
6262
#[inline]
63-
fn encrypt_block_inout(&self, block: InOut<'_, Block>) {
63+
fn encrypt_block_inout(&self, mut block: InOut<'_, Block>) {
6464
let mut blocks = BatchBlocks::default();
65-
blocks[0] = *block.get_in();
65+
blocks[0] = *block.reborrow().get_in();
6666
*(block.get_out()) = $fixslice_encrypt(&self.keys, &blocks)[0];
6767
}
6868

69-
fn encrypt_blocks_with_pre(
69+
fn encrypt_blocks_with_gen<B: ChunkProc<Block>>(
7070
&self,
71-
blocks: InOutBuf<'_, Block>,
72-
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
73-
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
71+
blocks: B,
72+
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
73+
body: impl FnMut(B, &mut [Block]),
7474
) {
7575
blocks.process_chunks::<FixsliceBlocks, _, _, _, _, _>(
7676
&self.keys,
77-
pre_fn,
78-
post_fn,
79-
|keys, chunk| *chunk.get_out() = $fixslice_encrypt(keys, chunk.get_in()),
80-
|keys, chunk| {
77+
gen_in,
78+
body,
79+
|keys, mut chunk| {
80+
let res = $fixslice_encrypt(keys, chunk.reborrow().get_in());
81+
*chunk.get_out() = res;
82+
},
83+
|keys, mut chunk| {
8184
let n = chunk.len();
8285
let mut blocks = BatchBlocks::default();
83-
blocks[..n].copy_from_slice(chunk.get_in());
86+
blocks[..n].copy_from_slice(chunk.reborrow().get_in());
8487
let res = $fixslice_encrypt(keys, &blocks);
8588
chunk.get_out().copy_from_slice(&res[..n]);
8689
},
@@ -90,28 +93,32 @@ macro_rules! define_aes_impl {
9093

9194
impl BlockDecrypt for $name {
9295
#[inline]
93-
fn decrypt_block_inout(&self, block: InOut<'_, Block>) {
96+
fn decrypt_block_inout(&self, mut block: InOut<'_, Block>) {
9497
let mut blocks = BatchBlocks::default();
95-
blocks[0] = *block.get_in();
96-
*(block.get_out()) = $fixslice_decrypt(&self.keys, &blocks)[0];
98+
blocks[0] = *block.reborrow().get_in();
99+
let res = $fixslice_decrypt(&self.keys, &blocks);
100+
*(block.get_out()) = res[0];
97101
}
98102

99103
#[inline]
100-
fn decrypt_blocks_with_pre(
104+
fn decrypt_blocks_with_gen<B: ChunkProc<Block>>(
101105
&self,
102-
blocks: InOutBuf<'_, Block>,
103-
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
104-
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
106+
blocks: B,
107+
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
108+
body: impl FnMut(B, &mut [Block]),
105109
) {
106110
blocks.process_chunks::<FixsliceBlocks, _, _, _, _, _>(
107111
&self.keys,
108-
pre_fn,
109-
post_fn,
110-
|keys, chunk| *chunk.get_out() = $fixslice_decrypt(keys, chunk.get_in()),
111-
|keys, chunk| {
112+
gen_in,
113+
body,
114+
|keys, mut chunk| {
115+
let res = $fixslice_decrypt(keys, chunk.reborrow().get_in());
116+
*chunk.get_out() = res;
117+
},
118+
|keys, mut chunk| {
112119
let n = chunk.len();
113120
let mut blocks = BatchBlocks::default();
114-
blocks[..n].copy_from_slice(chunk.get_in());
121+
blocks[..n].copy_from_slice(chunk.reborrow().get_in());
115122
let res = $fixslice_decrypt(keys, &blocks);
116123
chunk.get_out().copy_from_slice(&res[..n]);
117124
},

des/src/des.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,16 @@ impl BlockSizeUser for Des {
203203
impl BlockCipher for Des {}
204204

205205
impl BlockEncrypt for Des {
206-
fn encrypt_block_inout(&self, block: InOut<'_, Block<Self>>) {
207-
let mut data = u64::from_be_bytes(block.get_in().clone().into());
206+
fn encrypt_block_inout(&self, mut block: InOut<'_, Block<Self>>) {
207+
let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into());
208208
data = self.encrypt(data);
209209
block.get_out().copy_from_slice(&data.to_be_bytes());
210210
}
211211
}
212212

213213
impl BlockDecrypt for Des {
214-
fn decrypt_block_inout(&self, block: InOut<'_, Block<Self>>) {
215-
let mut data = u64::from_be_bytes(block.get_in().clone().into());
214+
fn decrypt_block_inout(&self, mut block: InOut<'_, Block<Self>>) {
215+
let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into());
216216
data = self.decrypt(data);
217217
block.get_out().copy_from_slice(&data.to_be_bytes());
218218
}

0 commit comments

Comments
 (0)