Skip to content

Commit

Permalink
update cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Dec 14, 2021
1 parent e048e5a commit 3286053
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 156 deletions.
34 changes: 8 additions & 26 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ members = [
"threefish",
]

[profile.dev]
opt-level = 2

[patch.crates-io]
cipher = { git = "https://github.com/RustCrypto/traits/", branch = "new_traits" }
block-buffer = { git = "https://github.com/RustCrypto/utils", branch = "pad_error" }
inout = { git = "https://github.com/RustCrypto/utils", branch = "pad_error" }
cipher = { git = "https://github.com/RustCrypto/traits/", branch = "cipher_v0.4" }
inout = { git = "https://github.com/RustCrypto/utils", branch = "add_inout" }
30 changes: 15 additions & 15 deletions aes/src/autodetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cipher::{
consts::{U16, U24, U32},
crypto_common::AlgorithmName,
generic_array::GenericArray,
inout::{InOut, InOutBuf, InSrc, InTmpOutBuf},
inout::{InOut, InCtrl, ChunkProc},
BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, KeySizeUser,
};
use core::fmt;
Expand Down Expand Up @@ -102,23 +102,23 @@ macro_rules! define_aes_impl {
}

#[inline]
fn encrypt_blocks_with_pre(
fn encrypt_blocks_with_gen<B: ChunkProc<Block>>(
&self,
blocks: InOutBuf<'_, Block>,
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
blocks: B,
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
body: impl FnMut(B, &mut [Block]),
) {
if self.token.get() {
unsafe {
self.inner
.intrinsics
.encrypt_blocks_with_pre(blocks, pre_fn, post_fn)
.encrypt_blocks_with_gen(blocks, gen_in, body)
}
} else {
unsafe {
self.inner
.soft
.encrypt_blocks_with_pre(blocks, pre_fn, post_fn)
.encrypt_blocks_with_gen(blocks, gen_in, body)
}
}
}
Expand All @@ -135,23 +135,23 @@ macro_rules! define_aes_impl {
}

#[inline]
fn decrypt_blocks_with_pre(
&self,
blocks: InOutBuf<'_, Block>,
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
) {
fn decrypt_blocks_with_gen<B: ChunkProc<Block>>(
&self,
blocks: B,
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
body: impl FnMut(B, &mut [Block]),
) {
if self.token.get() {
unsafe {
self.inner
.intrinsics
.decrypt_blocks_with_pre(blocks, pre_fn, post_fn)
.decrypt_blocks_with_gen(blocks, gen_in, body)
}
} else {
unsafe {
self.inner
.soft
.decrypt_blocks_with_pre(blocks, pre_fn, post_fn)
.decrypt_blocks_with_gen(blocks, gen_in, body)
}
}
}
Expand Down
14 changes: 2 additions & 12 deletions aes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,14 @@
//! // number of blocks processed in parallel depends in general
//! // on hardware capabilities
//! let mut blocks = [block; 100];
//! cipher.encrypt_blocks(
//! &mut blocks,
//! |chunk| {
//! // you can process encrypted chunk here, e.g. for MAC
//! },
//! );
//! cipher.encrypt_blocks(&mut blocks);
//!
//! for block in blocks.iter_mut() {
//! cipher.decrypt_block(block);
//! assert_eq!(block, &block_copy);
//! }
//!
//! cipher.decrypt_blocks(
//! &mut blocks,
//! |chunk| {
//! // you can process decrypted chunk here
//! },
//! );
//! cipher.decrypt_blocks(&mut blocks);
//!
//! for block in blocks.iter_mut() {
//! cipher.encrypt_block(block);
Expand Down
46 changes: 23 additions & 23 deletions aes/src/ni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use cipher::{
consts::{U16, U24, U32, U8},
crypto_common::AlgorithmName,
generic_array::{typenum::Unsigned, GenericArray},
inout::{InOut, InOutBuf, InSrc, InTmpOutBuf},
inout::{InOut, InCtrl, ChunkProc},
BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, KeySizeUser,
};
use core::fmt;
Expand Down Expand Up @@ -97,23 +97,23 @@ macro_rules! define_aes_impl {
}

#[inline]
fn encrypt_blocks_with_pre(
fn encrypt_blocks_with_gen<B: ChunkProc<Block>>(
&self,
blocks: InOutBuf<'_, Block>,
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
blocks: B,
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
body: impl FnMut(B, &mut [Block]),
) {
#[target_feature(enable = "aes")]
unsafe fn inner(
unsafe fn inner<B: ChunkProc<Block>>(
keys: &$module::RoundKeys,
blocks: InOutBuf<'_, Block>,
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
blocks: B,
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
body: impl FnMut(B, &mut [Block]),
) {
blocks.process_chunks::<U8, _, _, _, _, _>(
&keys,
pre_fn,
post_fn,
gen_in,
body,
|keys, chunk| $module::encrypt8(keys, chunk),
|keys, chunk| {
for block in chunk {
Expand All @@ -126,7 +126,7 @@ macro_rules! define_aes_impl {
// SAFETY: we enforce that this code is called only when
// required target features were properly checked.
unsafe {
inner(&self.encrypt_keys, blocks, pre_fn, post_fn);
inner(&self.encrypt_keys, blocks, gen_in, body);
}
}
}
Expand All @@ -142,23 +142,23 @@ macro_rules! define_aes_impl {
}

#[inline]
fn decrypt_blocks_with_pre(
fn decrypt_blocks_with_gen<B: ChunkProc<Block>>(
&self,
blocks: InOutBuf<'_, Block>,
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
blocks: B,
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
body: impl FnMut(B, &mut [Block]),
) {
#[target_feature(enable = "aes")]
unsafe fn inner(
unsafe fn inner<B: ChunkProc<Block>>(
keys: &$module::RoundKeys,
blocks: InOutBuf<'_, Block>,
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
blocks: B,
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
body: impl FnMut(B, &mut [Block]),
) {
blocks.process_chunks::<U8, _, _, _, _, _>(
&keys,
pre_fn,
post_fn,
gen_in,
body,
|keys, chunk| $module::decrypt8(keys, chunk),
|keys, chunk| {
for block in chunk {
Expand All @@ -171,7 +171,7 @@ macro_rules! define_aes_impl {
// SAFETY: we enforce that this code is called only when
// required target features were properly checked.
unsafe {
inner(&self.decrypt_keys, blocks, pre_fn, post_fn);
inner(&self.decrypt_keys, blocks, gen_in, body);
}
}
}
Expand Down
55 changes: 31 additions & 24 deletions aes/src/soft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use cipher::{
consts::{U16, U24, U32},
crypto_common::AlgorithmName,
generic_array::GenericArray,
inout::{InOut, InOutBuf, InSrc, InTmpOutBuf},
inout::{InOut, InCtrl, ChunkProc},
BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, KeySizeUser,
};
use core::fmt;
Expand Down Expand Up @@ -60,27 +60,30 @@ macro_rules! define_aes_impl {

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

fn encrypt_blocks_with_pre(
fn encrypt_blocks_with_gen<B: ChunkProc<Block>>(
&self,
blocks: InOutBuf<'_, Block>,
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
blocks: B,
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
body: impl FnMut(B, &mut [Block]),
) {
blocks.process_chunks::<FixsliceBlocks, _, _, _, _, _>(
&self.keys,
pre_fn,
post_fn,
|keys, chunk| *chunk.get_out() = $fixslice_encrypt(keys, chunk.get_in()),
|keys, chunk| {
gen_in,
body,
|keys, mut chunk| {
let res = $fixslice_encrypt(keys, chunk.reborrow().get_in());
*chunk.get_out() = res;
},
|keys, mut chunk| {
let n = chunk.len();
let mut blocks = BatchBlocks::default();
blocks[..n].copy_from_slice(chunk.get_in());
blocks[..n].copy_from_slice(chunk.reborrow().get_in());
let res = $fixslice_encrypt(keys, &blocks);
chunk.get_out().copy_from_slice(&res[..n]);
},
Expand All @@ -90,28 +93,32 @@ macro_rules! define_aes_impl {

impl BlockDecrypt for $name {
#[inline]
fn decrypt_block_inout(&self, block: InOut<'_, Block>) {
fn decrypt_block_inout(&self, mut block: InOut<'_, Block>) {
let mut blocks = BatchBlocks::default();
blocks[0] = *block.get_in();
*(block.get_out()) = $fixslice_decrypt(&self.keys, &blocks)[0];
blocks[0] = *block.reborrow().get_in();
let res = $fixslice_decrypt(&self.keys, &blocks);
*(block.get_out()) = res[0];
}

#[inline]
fn decrypt_blocks_with_pre(
fn decrypt_blocks_with_gen<B: ChunkProc<Block>>(
&self,
blocks: InOutBuf<'_, Block>,
pre_fn: impl FnMut(InTmpOutBuf<'_, Block>) -> InSrc,
post_fn: impl FnMut(InTmpOutBuf<'_, Block>),
blocks: B,
gen_in: impl FnMut(&mut [Block]) -> InCtrl,
body: impl FnMut(B, &mut [Block]),
) {
blocks.process_chunks::<FixsliceBlocks, _, _, _, _, _>(
&self.keys,
pre_fn,
post_fn,
|keys, chunk| *chunk.get_out() = $fixslice_decrypt(keys, chunk.get_in()),
|keys, chunk| {
gen_in,
body,
|keys, mut chunk| {
let res = $fixslice_decrypt(keys, chunk.reborrow().get_in());
*chunk.get_out() = res;
},
|keys, mut chunk| {
let n = chunk.len();
let mut blocks = BatchBlocks::default();
blocks[..n].copy_from_slice(chunk.get_in());
blocks[..n].copy_from_slice(chunk.reborrow().get_in());
let res = $fixslice_decrypt(keys, &blocks);
chunk.get_out().copy_from_slice(&res[..n]);
},
Expand Down
8 changes: 4 additions & 4 deletions des/src/des.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,16 @@ impl BlockSizeUser for Des {
impl BlockCipher for Des {}

impl BlockEncrypt for Des {
fn encrypt_block_inout(&self, block: InOut<'_, Block<Self>>) {
let mut data = u64::from_be_bytes(block.get_in().clone().into());
fn encrypt_block_inout(&self, mut block: InOut<'_, Block<Self>>) {
let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into());
data = self.encrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
}

impl BlockDecrypt for Des {
fn decrypt_block_inout(&self, block: InOut<'_, Block<Self>>) {
let mut data = u64::from_be_bytes(block.get_in().clone().into());
fn decrypt_block_inout(&self, mut block: InOut<'_, Block<Self>>) {
let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into());
data = self.decrypt(data);
block.get_out().copy_from_slice(&data.to_be_bytes());
}
Expand Down
Loading

0 comments on commit 3286053

Please sign in to comment.