From 3286053d19291ff6893fde9acacbe2e0185f107b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Tue, 14 Dec 2021 21:33:10 +0300 Subject: [PATCH] update cipher --- Cargo.lock | 34 ++++++----------------- Cargo.toml | 8 +++--- aes/src/autodetect.rs | 30 ++++++++++----------- aes/src/lib.rs | 14 ++-------- aes/src/ni.rs | 46 +++++++++++++++---------------- aes/src/soft.rs | 55 +++++++++++++++++++++----------------- des/src/des.rs | 8 +++--- des/src/tdes.rs | 32 +++++++++++----------- kuznyechik/src/soft/mod.rs | 12 ++++----- kuznyechik/src/sse2/mod.rs | 40 +++++++++++++-------------- kuznyechik/tests/lib.rs | 4 +-- magma/src/lib.rs | 8 +++--- 12 files changed, 135 insertions(+), 156 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cefced58..fae1fd8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,26 +14,9 @@ dependencies = [ [[package]] name = "blobby" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc52553543ecb104069b0ff9e0fcc5c739ad16202935528a112d974e8f1a4ee8" - -[[package]] -name = "block-buffer" -version = "0.10.0" -source = "git+https://github.com/RustCrypto/utils?branch=pad_error#d8d00fab9279432c6453e685f9f389e5d1a9cdc3" -dependencies = [ - "block-padding", - "generic-array", -] - -[[package]] -name = "block-padding" -version = "0.3.0" -source = "git+https://github.com/RustCrypto/utils?branch=pad_error#d8d00fab9279432c6453e685f9f389e5d1a9cdc3" -dependencies = [ - "generic-array", -] +checksum = "847495c209977a90e8aad588b959d0ca9f5dc228096d29a6bd3defd53f35eaec" [[package]] name = "blowfish" @@ -79,10 +62,9 @@ dependencies = [ [[package]] name = "cipher" version = "0.4.0" -source = "git+https://github.com/RustCrypto/traits/?branch=new_traits#05238cad578031f87137398aae943d10dd7a0df7" +source = "git+https://github.com/RustCrypto/traits/?branch=cipher_v0.4#ddb2f329ccc8246fd24efb8a4452763c2465fc80" dependencies = [ "blobby", - "block-buffer", "crypto-common", "generic-array", "inout", @@ -99,8 +81,8 @@ dependencies = [ [[package]] name = "crypto-common" -version = "0.1.0" -source = "git+https://github.com/RustCrypto/traits/?branch=new_traits#05238cad578031f87137398aae943d10dd7a0df7" +version = "0.1.1" +source = "git+https://github.com/RustCrypto/traits/?branch=cipher_v0.4#ddb2f329ccc8246fd24efb8a4452763c2465fc80" dependencies = [ "generic-array", ] @@ -152,7 +134,7 @@ dependencies = [ [[package]] name = "inout" version = "0.1.0" -source = "git+https://github.com/RustCrypto/utils?branch=pad_error#d8d00fab9279432c6453e685f9f389e5d1a9cdc3" +source = "git+https://github.com/RustCrypto/utils?branch=add_inout#8ded17a8c85a37b79bc679d4aceb9015c1fc8a28" dependencies = [ "generic-array", ] @@ -167,9 +149,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.107" +version = "0.2.112" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbe5e23404da5b4f555ef85ebed98fb4083e55a00c317800bc2a50ede9f3d219" +checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" [[package]] name = "magma" diff --git a/Cargo.toml b/Cargo.toml index f178256f..d1d67298 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/aes/src/autodetect.rs b/aes/src/autodetect.rs index 5f380979..f3fc7882 100644 --- a/aes/src/autodetect.rs +++ b/aes/src/autodetect.rs @@ -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; @@ -102,23 +102,23 @@ macro_rules! define_aes_impl { } #[inline] - fn encrypt_blocks_with_pre( + fn encrypt_blocks_with_gen>( &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) } } } @@ -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>( + &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) } } } diff --git a/aes/src/lib.rs b/aes/src/lib.rs index 09b6410a..4d294f8b 100644 --- a/aes/src/lib.rs +++ b/aes/src/lib.rs @@ -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); diff --git a/aes/src/ni.rs b/aes/src/ni.rs index 006adc72..d871971e 100644 --- a/aes/src/ni.rs +++ b/aes/src/ni.rs @@ -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; @@ -97,23 +97,23 @@ macro_rules! define_aes_impl { } #[inline] - fn encrypt_blocks_with_pre( + fn encrypt_blocks_with_gen>( &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>( 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::( &keys, - pre_fn, - post_fn, + gen_in, + body, |keys, chunk| $module::encrypt8(keys, chunk), |keys, chunk| { for block in chunk { @@ -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); } } } @@ -142,23 +142,23 @@ macro_rules! define_aes_impl { } #[inline] - fn decrypt_blocks_with_pre( + fn decrypt_blocks_with_gen>( &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>( 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::( &keys, - pre_fn, - post_fn, + gen_in, + body, |keys, chunk| $module::decrypt8(keys, chunk), |keys, chunk| { for block in chunk { @@ -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); } } } diff --git a/aes/src/soft.rs b/aes/src/soft.rs index 043fea61..0b845169 100644 --- a/aes/src/soft.rs +++ b/aes/src/soft.rs @@ -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; @@ -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>( &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::( &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]); }, @@ -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>( &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::( &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]); }, diff --git a/des/src/des.rs b/des/src/des.rs index 76903faa..601923d1 100644 --- a/des/src/des.rs +++ b/des/src/des.rs @@ -203,16 +203,16 @@ impl BlockSizeUser for Des { impl BlockCipher for Des {} impl BlockEncrypt for Des { - fn encrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut data = u64::from_be_bytes(block.get_in().clone().into()); + fn encrypt_block_inout(&self, mut block: InOut<'_, Block>) { + 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>) { - let mut data = u64::from_be_bytes(block.get_in().clone().into()); + fn decrypt_block_inout(&self, mut block: InOut<'_, Block>) { + 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()); } diff --git a/des/src/tdes.rs b/des/src/tdes.rs index c8f38d70..745730b9 100644 --- a/des/src/tdes.rs +++ b/des/src/tdes.rs @@ -39,8 +39,8 @@ impl KeyInit for TdesEde3 { impl BlockCipher for TdesEde3 {} impl BlockEncrypt for TdesEde3 { - fn encrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut data = u64::from_be_bytes(block.get_in().clone().into()); + fn encrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into()); data = self.d1.encrypt(data); data = self.d2.decrypt(data); @@ -51,8 +51,8 @@ impl BlockEncrypt for TdesEde3 { } impl BlockDecrypt for TdesEde3 { - fn decrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut data = u64::from_be_bytes(block.get_in().clone().into()); + fn decrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into()); data = self.d3.decrypt(data); data = self.d2.encrypt(data); @@ -99,8 +99,8 @@ impl BlockSizeUser for TdesEee3 { impl BlockCipher for TdesEee3 {} impl BlockEncrypt for TdesEee3 { - fn encrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut data = u64::from_be_bytes(block.get_in().clone().into()); + fn encrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into()); data = self.d1.encrypt(data); data = self.d2.encrypt(data); @@ -111,8 +111,8 @@ impl BlockEncrypt for TdesEee3 { } impl BlockDecrypt for TdesEee3 { - fn decrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut data = u64::from_be_bytes(block.get_in().clone().into()); + fn decrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into()); data = self.d3.decrypt(data); data = self.d2.decrypt(data); @@ -156,8 +156,8 @@ impl BlockSizeUser for TdesEde2 { impl BlockCipher for TdesEde2 {} impl BlockEncrypt for TdesEde2 { - fn encrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut data = u64::from_be_bytes(block.get_in().clone().into()); + fn encrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into()); data = self.d1.encrypt(data); data = self.d2.decrypt(data); @@ -168,8 +168,8 @@ impl BlockEncrypt for TdesEde2 { } impl BlockDecrypt for TdesEde2 { - fn decrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut data = u64::from_be_bytes(block.get_in().clone().into()); + fn decrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into()); data = self.d1.decrypt(data); data = self.d2.encrypt(data); @@ -213,8 +213,8 @@ impl BlockSizeUser for TdesEee2 { impl BlockCipher for TdesEee2 {} impl BlockEncrypt for TdesEee2 { - fn encrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut data = u64::from_be_bytes(block.get_in().clone().into()); + fn encrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into()); data = self.d1.encrypt(data); data = self.d2.encrypt(data); @@ -225,8 +225,8 @@ impl BlockEncrypt for TdesEee2 { } impl BlockDecrypt for TdesEee2 { - fn decrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut data = u64::from_be_bytes(block.get_in().clone().into()); + fn decrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let mut data = u64::from_be_bytes(block.reborrow().get_in().clone().into()); data = self.d1.decrypt(data); data = self.d2.decrypt(data); diff --git a/kuznyechik/src/soft/mod.rs b/kuznyechik/src/soft/mod.rs index 2a7ee19f..b9d2a24d 100644 --- a/kuznyechik/src/soft/mod.rs +++ b/kuznyechik/src/soft/mod.rs @@ -112,24 +112,24 @@ impl KeyInit for Kuznyechik { impl BlockEncrypt for Kuznyechik { #[inline] - fn encrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut b = *block.get_in(); + fn encrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let mut b = *block.reborrow().get_in(); unroll9! { i, { lsx(&mut b, &self.keys[i]) ; } } x(&mut b, &self.keys[9]); - *block.get_out() = b; + *block.reborrow().get_out() = b; } } impl BlockDecrypt for Kuznyechik { #[inline] - fn decrypt_block_inout(&self, block: InOut<'_, Block>) { - let mut b = *block.get_in(); + fn decrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let mut b = *block.reborrow().get_in(); unroll9! { i, { lsx_inv(&mut b, &self.keys[9 - i]) ; } } x(&mut b, &self.keys[0]); - *block.get_out() = b; + *block.reborrow().get_out() = b; } } diff --git a/kuznyechik/src/sse2/mod.rs b/kuznyechik/src/sse2/mod.rs index b2e1428d..b3fd5c91 100644 --- a/kuznyechik/src/sse2/mod.rs +++ b/kuznyechik/src/sse2/mod.rs @@ -8,7 +8,7 @@ use crate::{ }; use cipher::{ generic_array::typenum::Unsigned, - inout::{InOut, InOutBuf, InSrc, InTmpOutBuf}, + inout::{InOut, InCtrl, ChunkProc}, BlockDecrypt, BlockEncrypt, KeyInit, }; use core::arch::x86_64::*; @@ -169,8 +169,8 @@ impl BlockEncrypt for Kuznyechik { fn encrypt_block_inout(&self, block: InOut<'_, Block>) { let k = self.enc_keys; unsafe { - let in_ptr = block.get_in() as *const Block as *const __m128i; - let mut b = _mm_loadu_si128(in_ptr); + let (in_ptr, out_ptr) = block.into_raw(); + let mut b = _mm_loadu_si128(in_ptr as *const __m128i); unroll9! { i, { @@ -179,22 +179,21 @@ impl BlockEncrypt for Kuznyechik { } }; b = _mm_xor_si128(b, k[9]); - let out_ptr = block.get_out() as *mut Block as *mut __m128i; - _mm_storeu_si128(out_ptr, b); + _mm_storeu_si128(out_ptr as *mut __m128i, b); } } #[inline] - fn encrypt_blocks_with_pre( + fn encrypt_blocks_with_gen>( &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::( self, - pre_fn, - post_fn, + gen_in, + body, |s, chunk| unsafe { let k = s.enc_keys; @@ -242,8 +241,8 @@ impl BlockDecrypt for Kuznyechik { let ek = self.enc_keys; let dk = self.dec_keys; unsafe { - let in_ptr = block.get_in() as *const Block as *const __m128i; - let mut b = _mm_loadu_si128(in_ptr); + let (in_ptr, out_ptr) = block.into_raw(); + let mut b = _mm_loadu_si128(in_ptr as *const __m128i); b = _mm_xor_si128(b, ek[9]); @@ -259,22 +258,21 @@ impl BlockDecrypt for Kuznyechik { b = sub_bytes(b, &P_INV); b = _mm_xor_si128(b, ek[0]); - let out_ptr = block.get_out() as *mut Block as *mut __m128i; - _mm_storeu_si128(out_ptr, b) + _mm_storeu_si128(out_ptr as *mut __m128i, b) } } #[inline] - fn decrypt_blocks_with_pre( + fn decrypt_blocks_with_gen>( &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::( self, - pre_fn, - post_fn, + gen_in, + body, |s, chunk| unsafe { let ek = s.enc_keys; let dk = s.dec_keys; diff --git a/kuznyechik/tests/lib.rs b/kuznyechik/tests/lib.rs index 26528493..9fc10f2e 100644 --- a/kuznyechik/tests/lib.rs +++ b/kuznyechik/tests/lib.rs @@ -34,14 +34,14 @@ fn kuznyechik() { let mut blocks2 = blocks.clone(); let blocks_cpy = blocks.clone(); - cipher.encrypt_blocks(&mut blocks, |_| {}); + cipher.encrypt_blocks(&mut blocks); assert!(blocks[..] != blocks_cpy[..]); for block in blocks2.iter_mut() { cipher.encrypt_block(block); } assert_eq!(blocks[..], blocks2[..]); - cipher.decrypt_blocks(&mut blocks, |_| {}); + cipher.decrypt_blocks(&mut blocks); assert_eq!(blocks[..], blocks_cpy[..]); for block in blocks2.iter_mut().rev() { cipher.decrypt_block(block); diff --git a/magma/src/lib.rs b/magma/src/lib.rs index 0b5559c9..b2841334 100644 --- a/magma/src/lib.rs +++ b/magma/src/lib.rs @@ -88,8 +88,8 @@ impl BlockCipher for Gost89 {} impl BlockEncrypt for Gost89 { #[inline] - fn encrypt_block_inout(&self, block: InOut<'_, Block>) { - let b = block.get_in(); + fn encrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let b = block.reborrow().get_in(); let mut v = (to_u32(&b[0..4]), to_u32(&b[4..8])); for _ in 0..3 { for i in 0..8 { @@ -107,8 +107,8 @@ impl BlockEncrypt for Gost89 { impl BlockDecrypt for Gost89 { #[inline] - fn decrypt_block_inout(&self, block: InOut<'_, Block>) { - let b = block.get_in(); + fn decrypt_block_inout(&self, mut block: InOut<'_, Block>) { + let b = block.reborrow().get_in(); let mut v = (to_u32(&b[0..4]), to_u32(&b[4..8])); for i in 0..8 {