Skip to content

Commit c8c7ecf

Browse files
committed
update cipher
1 parent 6002245 commit c8c7ecf

File tree

9 files changed

+331
-291
lines changed

9 files changed

+331
-291
lines changed

Cargo.lock

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

aes/src/autodetect.rs

Lines changed: 3 additions & 17 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},
9+
inout::InOutBuf,
1010
BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, KeySizeUser,
1111
};
1212
use core::fmt;
@@ -92,14 +92,7 @@ macro_rules! define_aes_impl {
9292
impl BlockCipher for $name {}
9393

9494
impl BlockEncrypt for $name {
95-
fn callback_encrypt(
96-
&self,
97-
f: impl FnOnce(
98-
&mut [Block],
99-
&dyn Fn(InOut<'_, Block>),
100-
&dyn Fn(InOutBuf<'_, Block>),
101-
),
102-
) {
95+
fn callback_encrypt(&self, f: impl FnOnce(&mut [Block], &dyn Fn(InOutBuf<'_, Block>))) {
10396
unsafe {
10497
if self.token.get() {
10598
self.inner.intrinsics.callback_encrypt(f);
@@ -111,14 +104,7 @@ macro_rules! define_aes_impl {
111104
}
112105

113106
impl BlockDecrypt for $name {
114-
fn callback_decrypt(
115-
&self,
116-
f: impl FnOnce(
117-
&mut [Block],
118-
&dyn Fn(InOut<'_, Block>),
119-
&dyn Fn(InOutBuf<'_, Block>),
120-
),
121-
) {
107+
fn callback_decrypt(&self, f: impl FnOnce(&mut [Block], &dyn Fn(InOutBuf<'_, Block>))) {
122108
unsafe {
123109
if self.token.get() {
124110
self.inner.intrinsics.callback_decrypt(f);

aes/src/ni.rs

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ use cipher::{
3838
consts::{U16, U24, U32, U8},
3939
crypto_common::AlgorithmName,
4040
generic_array::{typenum::Unsigned, GenericArray},
41-
inout::{InOut, InOutBuf},
41+
inout::InOutBuf,
4242
BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, KeySizeUser,
4343
};
4444
use core::{convert::TryInto, fmt};
4545

46+
const PAR_BLOCKS: usize = 8;
4647
type Block8 = GenericArray<Block, U8>;
4748

4849
macro_rules! define_aes_impl {
@@ -89,57 +90,43 @@ macro_rules! define_aes_impl {
8990
impl BlockCipher for $name {}
9091

9192
impl BlockEncrypt for $name {
92-
fn callback_encrypt(
93-
&self,
94-
f: impl FnOnce(
95-
&mut [Block],
96-
&dyn Fn(InOut<'_, Block>),
97-
&dyn Fn(InOutBuf<'_, Block>),
98-
),
99-
) {
93+
fn callback_encrypt(&self, f: impl FnOnce(&mut [Block], &dyn Fn(InOutBuf<'_, Block>))) {
10094
#[target_feature(enable = "aes")]
10195
unsafe fn inner(
10296
keys: &$module::RoundKeys,
103-
f: impl FnOnce(
104-
&mut [Block],
105-
&dyn Fn(InOut<'_, Block>),
106-
&dyn Fn(InOutBuf<'_, Block>),
107-
),
97+
f: impl FnOnce(&mut [Block], &dyn Fn(InOutBuf<'_, Block>)),
10898
) {
109-
f(
110-
&mut [Default::default(); 8],
111-
&|b| $module::encrypt1(keys, b),
112-
&|b| $module::encrypt8(keys, b.try_into().unwrap()),
113-
)
99+
f(&mut [Default::default(); PAR_BLOCKS], &|blocks| {
100+
if blocks.len() == PAR_BLOCKS {
101+
$module::encrypt8(keys, blocks.try_into().unwrap())
102+
} else {
103+
for block in blocks {
104+
$module::encrypt1(keys, block);
105+
}
106+
}
107+
})
114108
}
115109

116110
unsafe { inner(&self.encrypt_keys, f) }
117111
}
118112
}
119113

120114
impl BlockDecrypt for $name {
121-
fn callback_decrypt(
122-
&self,
123-
f: impl FnOnce(
124-
&mut [Block],
125-
&dyn Fn(InOut<'_, Block>),
126-
&dyn Fn(InOutBuf<'_, Block>),
127-
),
128-
) {
115+
fn callback_decrypt(&self, f: impl FnOnce(&mut [Block], &dyn Fn(InOutBuf<'_, Block>))) {
129116
#[target_feature(enable = "aes")]
130117
unsafe fn inner(
131118
keys: &$module::RoundKeys,
132-
f: impl FnOnce(
133-
&mut [Block],
134-
&dyn Fn(InOut<'_, Block>),
135-
&dyn Fn(InOutBuf<'_, Block>),
136-
),
119+
f: impl FnOnce(&mut [Block], &dyn Fn(InOutBuf<'_, Block>)),
137120
) {
138-
f(
139-
&mut [Default::default(); 8],
140-
&|b| $module::decrypt1(keys, b),
141-
&|b| $module::decrypt8(keys, b.try_into().unwrap()),
142-
)
121+
f(&mut [Default::default(); PAR_BLOCKS], &|blocks| {
122+
if blocks.len() == PAR_BLOCKS {
123+
$module::decrypt8(keys, blocks.try_into().unwrap())
124+
} else {
125+
for block in blocks {
126+
$module::decrypt1(keys, block);
127+
}
128+
}
129+
})
143130
}
144131

145132
unsafe { inner(&self.decrypt_keys, f) }
@@ -161,7 +148,5 @@ macro_rules! define_aes_impl {
161148
}
162149

163150
define_aes_impl!(Aes128, aes128, U16, "AES-128 block cipher instance");
164-
165151
define_aes_impl!(Aes192, aes192, U24, "AES-192 block cipher instance");
166-
167152
define_aes_impl!(Aes256, aes256, U32, "AES-256 block cipher instance");

aes/src/soft.rs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,52 +59,42 @@ macro_rules! define_aes_impl {
5959
impl BlockCipher for $name {}
6060

6161
impl BlockEncrypt for $name {
62-
fn callback_encrypt(
63-
&self,
64-
f: impl FnOnce(
65-
&mut [Block],
66-
&dyn Fn(InOut<'_, Block>),
67-
&dyn Fn(InOutBuf<'_, Block>),
68-
),
69-
) {
70-
f(
71-
&mut [Default::default(); FixsliceBlocks::USIZE],
72-
&|mut block| {
73-
let mut blocks = BatchBlocks::default();
74-
blocks[0] = *block.reborrow().get_in();
75-
*block.get_out() = $fixslice_encrypt(&self.keys, &blocks)[0];
76-
},
77-
&|blocks| {
62+
fn callback_encrypt(&self, f: impl FnOnce(&mut [Block], &dyn Fn(InOutBuf<'_, Block>))) {
63+
let mut tmp = [Default::default(); FixsliceBlocks::USIZE];
64+
f(&mut tmp, &|blocks| {
65+
if blocks.len() == FixsliceBlocks::USIZE {
7866
let mut blocks: InOut<'_, BatchBlocks> = blocks.try_into().unwrap();
7967
let res = $fixslice_encrypt(&self.keys, blocks.reborrow().get_in());
8068
*blocks.get_out() = res;
81-
},
82-
);
69+
} else {
70+
// TODO: do not process blocks one-by-one
71+
for mut block in blocks {
72+
let mut blocks = BatchBlocks::default();
73+
blocks[0] = *block.reborrow().get_in();
74+
*block.get_out() = $fixslice_encrypt(&self.keys, &blocks)[0];
75+
}
76+
}
77+
});
8378
}
8479
}
8580

8681
impl BlockDecrypt for $name {
87-
fn callback_decrypt(
88-
&self,
89-
f: impl FnOnce(
90-
&mut [Block],
91-
&dyn Fn(InOut<'_, Block>),
92-
&dyn Fn(InOutBuf<'_, Block>),
93-
),
94-
) {
95-
f(
96-
&mut [Default::default(); FixsliceBlocks::USIZE],
97-
&|mut block| {
98-
let mut blocks = BatchBlocks::default();
99-
blocks[0] = *block.reborrow().get_in();
100-
*block.get_out() = $fixslice_decrypt(&self.keys, &blocks)[0];
101-
},
102-
&|blocks| {
82+
fn callback_decrypt(&self, f: impl FnOnce(&mut [Block], &dyn Fn(InOutBuf<'_, Block>))) {
83+
let mut tmp = [Default::default(); FixsliceBlocks::USIZE];
84+
f(&mut tmp, &|blocks| {
85+
if blocks.len() == FixsliceBlocks::USIZE {
10386
let mut blocks: InOut<'_, BatchBlocks> = blocks.try_into().unwrap();
10487
let res = $fixslice_decrypt(&self.keys, blocks.reborrow().get_in());
10588
*blocks.get_out() = res;
106-
},
107-
);
89+
} else {
90+
// TODO: do not process blocks one-by-one
91+
for mut block in blocks {
92+
let mut blocks = BatchBlocks::default();
93+
blocks[0] = *block.reborrow().get_in();
94+
*block.get_out() = $fixslice_decrypt(&self.keys, &blocks)[0];
95+
}
96+
}
97+
});
10898
}
10999
}
110100

des/src/des.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#![allow(clippy::unreadable_literal)]
44

55
use cipher::{
6-
consts::U8, impl_simple_block_encdec, BlockCipher, BlockSizeUser, Key, KeyInit, KeySizeUser,
6+
consts::U8, inout::InOutBuf, Block, BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser,
7+
Key, KeyInit, KeySizeUser,
78
};
89
use core::fmt;
910

@@ -201,19 +202,35 @@ impl BlockSizeUser for Des {
201202

202203
impl BlockCipher for Des {}
203204

204-
impl_simple_block_encdec!(BlockEncrypt, Des, cipher, |mut block| {
205-
let b = block.reborrow().get_in();
206-
let mut data = u64::from_be_bytes((*b).into());
207-
data = cipher.encrypt(data);
208-
block.get_out().copy_from_slice(&data.to_be_bytes());
209-
});
210-
211-
impl_simple_block_encdec!(BlockDecrypt, Des, cipher, |mut block| {
212-
let b = block.reborrow().get_in();
213-
let mut data = u64::from_be_bytes((*b).into());
214-
data = cipher.decrypt(data);
215-
block.get_out().copy_from_slice(&data.to_be_bytes());
216-
});
205+
impl BlockEncrypt for Des {
206+
fn callback_encrypt(
207+
&self,
208+
f: impl FnOnce(&mut [Block<Self>], &dyn Fn(InOutBuf<'_, Block<Self>>)),
209+
) {
210+
f(&mut [Default::default(); 1], &|blocks| {
211+
for mut block in blocks {
212+
let b = block.reborrow().get_in().clone().into();
213+
let res = self.encrypt(u64::from_be_bytes(b));
214+
block.get_out().copy_from_slice(&res.to_be_bytes());
215+
}
216+
});
217+
}
218+
}
219+
220+
impl BlockDecrypt for Des {
221+
fn callback_decrypt(
222+
&self,
223+
f: impl FnOnce(&mut [Block<Self>], &dyn Fn(InOutBuf<'_, Block<Self>>)),
224+
) {
225+
f(&mut [Default::default(); 1], &|blocks| {
226+
for mut block in blocks {
227+
let b = block.reborrow().get_in().clone().into();
228+
let res = self.decrypt(u64::from_be_bytes(b));
229+
block.get_out().copy_from_slice(&res.to_be_bytes());
230+
}
231+
});
232+
}
233+
}
217234

218235
impl fmt::Debug for Des {
219236
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

0 commit comments

Comments
 (0)