From 09cf72f87920c216e0413f9e41cb985122aace12 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=20=5BArtyom=20Pavlov=5D?= Date: Mon, 29 Jul 2024 15:57:56 +0300 Subject: [PATCH 1/3] Fix new Clippy lints --- aes/src/lib.rs | 2 +- aes/src/ni/aes192.rs | 27 +++++++++++++++------------ des/src/des.rs | 4 ++-- gift/benches/gift128enc.rs | 2 +- rc2/src/lib.rs | 16 ++++++++-------- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/aes/src/lib.rs b/aes/src/lib.rs index 0f8bab50..4f5c08b5 100644 --- a/aes/src/lib.rs +++ b/aes/src/lib.rs @@ -101,7 +101,7 @@ //! //! - `aes_force_soft`: force software implementation. //! - `aes_compact`: reduce code size at the cost of slower performance -//! (affects only software backend). +//! (affects only software backend). //! //! It can be enabled using `RUSTFLAGS` environmental variable //! (e.g. `RUSTFLAGS="--cfg aes_compact"`) or by modifying `.cargo/config`. diff --git a/aes/src/ni/aes192.rs b/aes/src/ni/aes192.rs index eee1f211..631af6f8 100644 --- a/aes/src/ni/aes192.rs +++ b/aes/src/ni/aes192.rs @@ -115,10 +115,13 @@ macro_rules! expand_round { }}; } -macro_rules! shuffle { - ($a:expr, $b:expr, $imm:expr) => { - mem::transmute::<_, __m128i>(_mm_shuffle_pd(mem::transmute($a), mem::transmute($b), $imm)) - }; +#[inline(always)] +unsafe fn shuffle(a: __m128i, b: __m128i, i: usize) -> __m128i { + unsafe { + let a: [u64; 2] = mem::transmute(a); + let b: [u64; 2] = mem::transmute(b); + mem::transmute([a[i], b[0]]) + } } #[inline] @@ -144,15 +147,15 @@ pub(super) unsafe fn expand_key(key: &[u8; 24]) -> RoundKeys { keys[0] = k0; let (k1_2, k2r) = expand_round!(k0, k1l, 0x01); - keys[1] = shuffle!(k1l, k1_2, 0); - keys[2] = shuffle!(k1_2, k2r, 1); + keys[1] = shuffle(k1l, k1_2, 0); + keys[2] = shuffle(k1_2, k2r, 1); let (k3, k4l) = expand_round!(k1_2, k2r, 0x02); keys[3] = k3; let (k4_5, k5r) = expand_round!(k3, k4l, 0x04); - let k4 = shuffle!(k4l, k4_5, 0); - let k5 = shuffle!(k4_5, k5r, 1); + let k4 = shuffle(k4l, k4_5, 0); + let k5 = shuffle(k4_5, k5r, 1); keys[4] = k4; keys[5] = k5; @@ -160,15 +163,15 @@ pub(super) unsafe fn expand_key(key: &[u8; 24]) -> RoundKeys { keys[6] = k6; let (k7_8, k8r) = expand_round!(k6, k7l, 0x10); - keys[7] = shuffle!(k7l, k7_8, 0); - keys[8] = shuffle!(k7_8, k8r, 1); + keys[7] = shuffle(k7l, k7_8, 0); + keys[8] = shuffle(k7_8, k8r, 1); let (k9, k10l) = expand_round!(k7_8, k8r, 0x20); keys[9] = k9; let (k10_11, k11r) = expand_round!(k9, k10l, 0x40); - keys[10] = shuffle!(k10l, k10_11, 0); - keys[11] = shuffle!(k10_11, k11r, 1); + keys[10] = shuffle(k10l, k10_11, 0); + keys[11] = shuffle(k10_11, k11r, 1); let (k12, _) = expand_round!(k10_11, k11r, 0x80); keys[12] = k12; diff --git a/des/src/des.rs b/des/src/des.rs index 0f6c628b..6a748f5d 100644 --- a/des/src/des.rs +++ b/des/src/des.rs @@ -173,7 +173,7 @@ impl Des { for key in &self.keys { data = round(data, *key); } - fp((data << 32) | (data >> 32)) + fp(data.rotate_right(32)) } pub(crate) fn decrypt(&self, mut data: u64) -> u64 { @@ -181,7 +181,7 @@ impl Des { for key in self.keys.iter().rev() { data = round(data, *key); } - fp((data << 32) | (data >> 32)) + fp(data.rotate_right(32)) } } diff --git a/gift/benches/gift128enc.rs b/gift/benches/gift128enc.rs index 9df2f03b..eabfb492 100644 --- a/gift/benches/gift128enc.rs +++ b/gift/benches/gift128enc.rs @@ -1,6 +1,6 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; use criterion_cycles_per_byte::CyclesPerByte; -use gift_cipher::cipher::{BlockEncrypt, KeyInit}; +use gift_cipher::cipher::{BlockCipherEncrypt, KeyInit}; use gift_cipher::Gift128; const KB: usize = 1024; diff --git a/rc2/src/lib.rs b/rc2/src/lib.rs index 21652e50..e41884d9 100644 --- a/rc2/src/lib.rs +++ b/rc2/src/lib.rs @@ -83,28 +83,28 @@ impl Rc2 { .wrapping_add(r[3] & r[2]) .wrapping_add(!r[3] & r[1]); *j += 1; - r[0] = (r[0] << 1) | (r[0] >> 15); + r[0] = r[0].rotate_left(1); r[1] = r[1] .wrapping_add(self.keys[*j]) .wrapping_add(r[0] & r[3]) .wrapping_add(!r[0] & r[2]); *j += 1; - r[1] = (r[1] << 2) | (r[1] >> 14); + r[1] = r[1].rotate_left(2); r[2] = r[2] .wrapping_add(self.keys[*j]) .wrapping_add(r[1] & r[0]) .wrapping_add(!r[1] & r[3]); *j += 1; - r[2] = (r[2] << 3) | (r[2] >> 13); + r[2] = r[2].rotate_left(3); r[3] = r[3] .wrapping_add(self.keys[*j]) .wrapping_add(r[2] & r[1]) .wrapping_add(!r[2] & r[0]); *j += 1; - r[3] = (r[3] << 5) | (r[3] >> 11); + r[3] = r[3].rotate_left(5); } fn mash(&self, r: &mut [u16; 4]) { @@ -115,28 +115,28 @@ impl Rc2 { } fn reverse_mix(&self, r: &mut [u16; 4], j: &mut usize) { - r[3] = (r[3] << 11) | (r[3] >> 5); + r[3] = r[3].rotate_right(5); r[3] = r[3] .wrapping_sub(self.keys[*j]) .wrapping_sub(r[2] & r[1]) .wrapping_sub(!r[2] & r[0]); *j -= 1; - r[2] = (r[2] << 13) | (r[2] >> 3); + r[2] = r[2].rotate_right(3); r[2] = r[2] .wrapping_sub(self.keys[*j]) .wrapping_sub(r[1] & r[0]) .wrapping_sub(!r[1] & r[3]); *j -= 1; - r[1] = (r[1] << 14) | (r[1] >> 2); + r[1] = r[1].rotate_right(2); r[1] = r[1] .wrapping_sub(self.keys[*j]) .wrapping_sub(r[0] & r[3]) .wrapping_sub(!r[0] & r[2]); *j -= 1; - r[0] = (r[0] << 15) | (r[0] >> 1); + r[0] = r[0].rotate_right(1); r[0] = r[0] .wrapping_sub(self.keys[*j]) .wrapping_sub(r[3] & r[2]) From adedb6f2503619a040a73b04679bfee243ba430d 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=20=5BArtyom=20Pavlov=5D?= Date: Mon, 29 Jul 2024 15:59:06 +0300 Subject: [PATCH 2/3] remove redundant unsafe --- aes/src/ni/aes192.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/aes/src/ni/aes192.rs b/aes/src/ni/aes192.rs index 631af6f8..74a4e374 100644 --- a/aes/src/ni/aes192.rs +++ b/aes/src/ni/aes192.rs @@ -117,11 +117,9 @@ macro_rules! expand_round { #[inline(always)] unsafe fn shuffle(a: __m128i, b: __m128i, i: usize) -> __m128i { - unsafe { - let a: [u64; 2] = mem::transmute(a); - let b: [u64; 2] = mem::transmute(b); - mem::transmute([a[i], b[0]]) - } + let a: [u64; 2] = mem::transmute(a); + let b: [u64; 2] = mem::transmute(b); + mem::transmute([a[i], b[0]]) } #[inline] From 6db40e72b8f6644e94ca2957f81af8cfbfe4a886 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=20=5BArtyom=20Pavlov=5D?= Date: Mon, 29 Jul 2024 16:03:27 +0300 Subject: [PATCH 3/3] Bump Clippy version --- .github/workflows/workspace.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index 816bb875..78d52564 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -17,7 +17,7 @@ jobs: - uses: RustCrypto/actions/cargo-cache@master - uses: dtolnay/rust-toolchain@master with: - toolchain: 1.71.0 + toolchain: 1.80.0 components: clippy - run: cargo clippy --all --exclude aes --all-features -- -D warnings