Skip to content

Commit c914636

Browse files
committed
sm4: upgrade cipher to 0.5.0
1 parent 829b443 commit c914636

File tree

3 files changed

+49
-59
lines changed

3 files changed

+49
-59
lines changed

sm4/src/armv8/autodetect/linux.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#![allow(unsafe_code)]
22

33
use cipher::{
4-
consts::U16, AlgorithmName, BlockCipher, BlockClosure, BlockDecrypt, BlockEncrypt,
5-
BlockSizeUser, Key, KeyInit, KeySizeUser,
4+
AlgorithmName, Block, BlockCipherDecBackend, BlockCipherDecClosure, BlockCipherDecrypt,
5+
BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, InOut, Key,
6+
KeyInit, KeySizeUser, ParBlocks, ParBlocksSizeUser, consts::U16,
67
};
78
use core::{fmt, mem::ManuallyDrop};
89

@@ -67,10 +68,8 @@ impl BlockSizeUser for Sm4 {
6768
type BlockSize = U16;
6869
}
6970

70-
impl BlockCipher for Sm4 {}
71-
72-
impl BlockEncrypt for Sm4 {
73-
fn encrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
71+
impl BlockCipherEncrypt for Sm4 {
72+
fn encrypt_with_backend(&self, f: impl BlockCipherEncClosure<BlockSize = U16>) {
7473
unsafe {
7574
if self.token.get() {
7675
self.cipher.sm4.encrypt_with_backend(f);
@@ -81,8 +80,8 @@ impl BlockEncrypt for Sm4 {
8180
}
8281
}
8382

84-
impl BlockDecrypt for Sm4 {
85-
fn decrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
83+
impl BlockCipherDecrypt for Sm4 {
84+
fn decrypt_with_backend(&self, f: impl BlockCipherDecClosure<BlockSize = U16>) {
8685
unsafe {
8786
if self.token.get() {
8887
self.cipher.sm4.decrypt_with_backend(f);

sm4/src/armv8/neon.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
#[cfg(feature = "zeroize")]
88
use cipher::zeroize::{Zeroize, ZeroizeOnDrop};
99
use cipher::{
10-
consts::{U16, U4},
11-
generic_array::GenericArray,
12-
inout::InOut,
13-
AlgorithmName, Block, BlockCipher, BlockDecrypt, BlockSizeUser, Key, KeyInit, KeySizeUser,
14-
ParBlocks, ParBlocksSizeUser,
10+
AlgorithmName, Block, BlockCipherDecBackend, BlockCipherDecClosure, BlockCipherDecrypt,
11+
BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, InOut, Key,
12+
KeyInit, KeySizeUser, ParBlocks, ParBlocksSizeUser,
13+
consts::{U4, U16},
1514
};
1615
use cipher::{BlockBackend, BlockEncrypt};
1716
use core::{arch::aarch64::*, fmt};
1817

1918
use crate::consts::SBOX;
2019

21-
type ParBlocks4<T> = GenericArray<Block<T>, U4>;
22-
2320
#[inline]
2421
#[target_feature(enable = "neon")]
2522
unsafe fn sbox_table_lookup(
@@ -45,8 +42,8 @@ unsafe fn sbox_table_lookup(
4542

4643
#[inline]
4744
#[target_feature(enable = "neon")]
48-
pub(super) unsafe fn sm4_process4<T: BlockSizeUser>(
49-
blocks: InOut<'_, '_, ParBlocks4<T>>,
45+
pub(super) unsafe fn sm4_process4<T: ParBlocksSizeUser>(
46+
blocks: InOut<'_, '_, ParBlocks<T>>,
5047
rk: &[u32; 32],
5148
encrypt: bool,
5249
) {
@@ -161,8 +158,6 @@ pub struct Sm4 {
161158
rk: [u32; 32],
162159
}
163160

164-
impl BlockCipher for Sm4 {}
165-
166161
impl KeySizeUser for Sm4 {
167162
type KeySize = U16;
168163
}
@@ -203,8 +198,9 @@ impl BlockSizeUser for Sm4 {
203198
type BlockSize = U16;
204199
}
205200

206-
impl BlockEncrypt for Sm4 {
207-
fn encrypt_with_backend(&self, f: impl cipher::BlockClosure<BlockSize = Self::BlockSize>) {
201+
impl BlockCipherEncrypt for Sm4 {
202+
#[inline]
203+
fn encrypt_with_backend(&self, f: impl BlockCipherEncClosure<BlockSize = Self::BlockSize>) {
208204
f.call(&mut Sm4Enc(self))
209205
}
210206
}
@@ -219,20 +215,20 @@ impl<'a> ParBlocksSizeUser for Sm4Enc<'a> {
219215
type ParBlocksSize = U4;
220216
}
221217

222-
impl<'a> BlockBackend for Sm4Enc<'a> {
218+
impl<'a> BlockCipherEncBackend for Sm4Enc<'a> {
223219
#[inline(always)]
224-
fn proc_block(&mut self, block: InOut<'_, '_, Block<Self>>) {
220+
fn encrypt_block(&self, block: InOut<'_, '_, Block<Self>>) {
225221
crate::soft::sm4_encrypt::<Self>(block, &self.0.rk);
226222
}
227223

228224
#[inline(always)]
229-
fn proc_par_blocks(&mut self, blocks: InOut<'_, '_, ParBlocks<Self>>) {
225+
fn encrypt_par_blocks(&self, blocks: InOut<'_, '_, ParBlocks<Self>>) {
230226
unsafe { sm4_process4::<Self>(blocks, &self.0.rk, true) }
231227
}
232228
}
233229

234-
impl BlockDecrypt for Sm4 {
235-
fn decrypt_with_backend(&self, f: impl cipher::BlockClosure<BlockSize = Self::BlockSize>) {
230+
impl BlockCipherDecrypt for Sm4 {
231+
fn decrypt_with_backend(&self, f: impl BlockCipherDecClosure<BlockSize = Self::BlockSize>) {
236232
f.call(&mut Sm4Dec(self))
237233
}
238234
}
@@ -247,14 +243,14 @@ impl<'a> ParBlocksSizeUser for Sm4Dec<'a> {
247243
type ParBlocksSize = U4;
248244
}
249245

250-
impl<'a> BlockBackend for Sm4Dec<'a> {
246+
impl<'a> BlockCipherDecBackend for Sm4Dec<'a> {
251247
#[inline(always)]
252-
fn proc_block(&mut self, block: InOut<'_, '_, Block<Self>>) {
248+
fn decrypt_block(&mut self, block: InOut<'_, '_, Block<Self>>) {
253249
crate::soft::sm4_decrypt::<Self>(block, &self.0.rk);
254250
}
255251

256252
#[inline(always)]
257-
fn proc_par_blocks(&mut self, blocks: InOut<'_, '_, ParBlocks<Self>>) {
253+
fn decrypt_par_blocks(&mut self, blocks: InOut<'_, '_, ParBlocks<Self>>) {
258254
unsafe { sm4_process4::<Self>(blocks, &self.0.rk, false) }
259255
}
260256
}

sm4/src/armv8/sm4e.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
#[cfg(feature = "zeroize")]
88
use cipher::zeroize::ZeroizeOnDrop;
99
use cipher::{
10-
consts::{U16, U4, U8},
11-
generic_array::GenericArray,
12-
inout::{InOut, InOutBuf},
13-
AlgorithmName, Block, BlockCipher, BlockDecrypt, BlockSizeUser, Key, KeyInit, KeySizeUser,
14-
ParBlocks, ParBlocksSizeUser, Unsigned,
10+
AlgorithmName, Block, BlockCipherDecBackend, BlockCipherDecClosure, BlockCipherDecrypt,
11+
BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, InOut, Key,
12+
KeyInit, KeySizeUser, ParBlocks, ParBlocksSizeUser,
13+
consts::{U4, U8, U16},
1514
};
1615
use cipher::{BlockBackend, BlockEncrypt};
1716
use core::{arch::aarch64::*, fmt};
@@ -92,13 +91,10 @@ macro_rules! sm4_e {
9291
}
9392
}
9493

95-
type ParBlocks4<T> = GenericArray<Block<T>, U4>;
96-
type ParBlocks8<T> = GenericArray<Block<T>, U8>;
97-
9894
#[inline]
9995
#[target_feature(enable = "sm4")]
100-
pub(super) unsafe fn sm4_encrypt4<T: BlockSizeUser>(
101-
blocks: InOut<'_, '_, ParBlocks4<T>>,
96+
pub(super) unsafe fn sm4_encrypt4<T: ParBlocksSizeUser>(
97+
blocks: InOut<'_, '_, ParBlocks<T>>,
10298
rk: &[uint32x4_t; 8],
10399
) {
104100
let (in_ptr, out_ptr) = blocks.into_raw();
@@ -127,8 +123,8 @@ pub(super) unsafe fn sm4_encrypt4<T: BlockSizeUser>(
127123

128124
#[inline]
129125
#[target_feature(enable = "sm4")]
130-
pub(super) unsafe fn sm4_encrypt8<T: BlockSizeUser>(
131-
blocks: InOut<'_, '_, ParBlocks8<T>>,
126+
pub(super) unsafe fn sm4_encrypt8<T: ParBlocksSizeUser>(
127+
blocks: InOut<'_, '_, ParBlocks<T>>,
132128
rk: &[uint32x4_t; 8],
133129
) {
134130
let (in_ptr, out_ptr) = blocks.into_raw();
@@ -165,7 +161,7 @@ pub(super) unsafe fn sm4_encrypt8<T: BlockSizeUser>(
165161

166162
#[inline]
167163
#[target_feature(enable = "sm4")]
168-
pub(super) unsafe fn sm4_encrypt1<T: BlockSizeUser>(
164+
pub(super) unsafe fn sm4_encrypt1<T: BlocksSizeUser>(
169165
block: InOut<'_, '_, Block<T>>,
170166
rk: &[uint32x4_t; 8],
171167
) {
@@ -189,8 +185,8 @@ pub(super) unsafe fn sm4_encrypt1<T: BlockSizeUser>(
189185

190186
#[inline]
191187
#[target_feature(enable = "sm4")]
192-
pub(super) unsafe fn sm4_decrypt4<T: BlockSizeUser>(
193-
blocks: InOut<'_, '_, ParBlocks4<T>>,
188+
pub(super) unsafe fn sm4_decrypt4<T: ParBlocksSizeUser>(
189+
blocks: InOut<'_, '_, ParBlocks<T>>,
194190
rk: &[uint32x4_t; 8],
195191
) {
196192
let (in_ptr, out_ptr) = blocks.into_raw();
@@ -220,7 +216,7 @@ pub(super) unsafe fn sm4_decrypt4<T: BlockSizeUser>(
220216
#[inline]
221217
#[target_feature(enable = "sm4")]
222218
pub(super) unsafe fn sm4_decrypt8<T: BlockSizeUser>(
223-
blocks: InOut<'_, '_, ParBlocks8<T>>,
219+
blocks: InOut<'_, '_, ParBlocks<T>>,
224220
rk: &[uint32x4_t; 8],
225221
) {
226222
let (in_ptr, out_ptr) = blocks.into_raw();
@@ -286,8 +282,6 @@ pub struct Sm4 {
286282
drk: [uint32x4_t; 8],
287283
}
288284

289-
impl BlockCipher for Sm4 {}
290-
291285
impl KeySizeUser for Sm4 {
292286
type KeySize = U16;
293287
}
@@ -359,8 +353,9 @@ impl BlockSizeUser for Sm4 {
359353
type BlockSize = U16;
360354
}
361355

362-
impl BlockEncrypt for Sm4 {
363-
fn encrypt_with_backend(&self, f: impl cipher::BlockClosure<BlockSize = Self::BlockSize>) {
356+
impl BlockCipherEncrypt for Sm4 {
357+
#[inline]
358+
fn encrypt_with_backend(&self, f: impl BlockCipherEncClosure<BlockSize = Self::BlockSize>) {
364359
f.call(&mut Sm4Enc(self))
365360
}
366361
}
@@ -375,14 +370,14 @@ impl<'a> ParBlocksSizeUser for Sm4Enc<'a> {
375370
type ParBlocksSize = U8;
376371
}
377372

378-
impl<'a> BlockBackend for Sm4Enc<'a> {
373+
impl<'a> BlockCipherEncBackend for Sm4Enc<'a> {
379374
#[inline(always)]
380-
fn proc_block(&mut self, block: InOut<'_, '_, Block<Self>>) {
375+
fn encrypt_block(&self, block: InOut<'_, '_, Block<Self>>) {
381376
unsafe { sm4_encrypt1::<Self>(block, &self.0.erk) }
382377
}
383378

384379
#[inline(always)]
385-
fn proc_tail_blocks(&mut self, blocks: InOutBuf<'_, '_, Block<Self>>) {
380+
fn encrypt_tail_blocks(&self, blocks: InOutBuf<'_, '_, Block<Self>>) {
386381
assert!(blocks.len() < Self::ParBlocksSize::USIZE);
387382

388383
let (chunks, tail) = blocks.into_chunks::<U4>();
@@ -396,13 +391,13 @@ impl<'a> BlockBackend for Sm4Enc<'a> {
396391
}
397392

398393
#[inline(always)]
399-
fn proc_par_blocks(&mut self, blocks: InOut<'_, '_, ParBlocks<Self>>) {
394+
fn encrypt_par_blocks(&mut self, blocks: InOut<'_, '_, ParBlocks<Self>>) {
400395
unsafe { sm4_encrypt8::<Self>(blocks, &self.0.erk) }
401396
}
402397
}
403398

404-
impl BlockDecrypt for Sm4 {
405-
fn decrypt_with_backend(&self, f: impl cipher::BlockClosure<BlockSize = Self::BlockSize>) {
399+
impl BlockCipherDecrypt for Sm4 {
400+
fn decrypt_with_backend(&self, f: impl BlockCipherDecClosure<BlockSize = Self::BlockSize>) {
406401
f.call(&mut Sm4Dec(self))
407402
}
408403
}
@@ -417,14 +412,14 @@ impl<'a> ParBlocksSizeUser for Sm4Dec<'a> {
417412
type ParBlocksSize = U8;
418413
}
419414

420-
impl<'a> BlockBackend for Sm4Dec<'a> {
415+
impl<'a> BlockCipherDecBackend for Sm4Dec<'a> {
421416
#[inline(always)]
422-
fn proc_block(&mut self, block: InOut<'_, '_, Block<Self>>) {
417+
fn decrypt_block(&self, block: InOut<'_, '_, Block<Self>>) {
423418
unsafe { sm4_decrypt1::<Self>(block, &self.0.drk) }
424419
}
425420

426421
#[inline(always)]
427-
fn proc_tail_blocks(&mut self, blocks: InOutBuf<'_, '_, Block<Self>>) {
422+
fn decrypt_tail_blocks(&self, blocks: InOutBuf<'_, '_, Block<Self>>) {
428423
assert!(blocks.len() < Self::ParBlocksSize::USIZE);
429424

430425
let (chunks, tail) = blocks.into_chunks::<U4>();
@@ -438,7 +433,7 @@ impl<'a> BlockBackend for Sm4Dec<'a> {
438433
}
439434

440435
#[inline(always)]
441-
fn proc_par_blocks(&mut self, blocks: InOut<'_, '_, ParBlocks<Self>>) {
436+
fn decrypt_par_blocks(&mut self, blocks: InOut<'_, '_, ParBlocks<Self>>) {
442437
unsafe { sm4_decrypt8::<Self>(blocks, &self.0.drk) }
443438
}
444439
}

0 commit comments

Comments
 (0)