From 0687fe04319bfc91c6ca49b7d3d1759b73ab7c46 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 18 Dec 2025 18:42:50 +0200 Subject: [PATCH] fix: clippy warnings --- aes/src/sbox.rs | 48 +++++++++++++++++------------------ aes/tests/aes.rs | 10 ++++++-- des/src/block/secret_block.rs | 2 +- des/src/utils.rs | 2 +- des/tests/des.rs | 20 +++++++-------- 5 files changed, 43 insertions(+), 39 deletions(-) diff --git a/aes/src/sbox.rs b/aes/src/sbox.rs index 1e56b41..2775469 100644 --- a/aes/src/sbox.rs +++ b/aes/src/sbox.rs @@ -6,33 +6,31 @@ pub trait SboxLookup: Sized { } macro_rules! impl_sbox_lookup { - ($ty:ty, $bytes:expr) => { - impl SboxLookup for $ty { - fn sbox_lookup(self) -> Self { - (0..$bytes).fold(0, |acc, idx| { - let shift = ($bytes - 1 - idx) * 8; - let byte = ((self >> shift) & 0xFF) as u8; - let row = (byte >> 4) as usize; - let col = (byte & 0xF) as usize; - acc | Self::from(S_BOXES[row][col]) << shift - }) - } + ($($ty:ty),*) => { + $( + impl SboxLookup for $ty { + fn sbox_lookup(self) -> Self { + let mut bytes = self.to_le_bytes(); + for b in bytes.iter_mut() { + let row = (*b >> 4) as usize; + let col = (*b & 0x0F) as usize; + *b = S_BOXES[row][col]; + } + Self::from_le_bytes(bytes) + } - fn inv_sbox_lookup(self) -> Self { - (0..$bytes).fold(0, |acc, idx| { - let shift = ($bytes - 1 - idx) * 8; - let byte = ((self >> shift) & 0xFF) as u8; - let row = (byte >> 4) as usize; - let col = (byte & 0xF) as usize; - acc | Self::from(INV_S_BOXES[row][col]) << shift - }) + fn inv_sbox_lookup(self) -> Self { + let mut bytes = self.to_le_bytes(); + for b in bytes.iter_mut() { + let row = (*b >> 4) as usize; + let col = (*b & 0x0F) as usize; + *b = INV_S_BOXES[row][col]; + } + Self::from_le_bytes(bytes) + } } - } + )* }; } -impl_sbox_lookup!(u8, 1); -impl_sbox_lookup!(u16, 2); -impl_sbox_lookup!(u32, 4); -impl_sbox_lookup!(u64, 8); -impl_sbox_lookup!(u128, 16); +impl_sbox_lookup!(u8, u16, u32, u64, u128); diff --git a/aes/tests/aes.rs b/aes/tests/aes.rs index 899a505..ceb7254 100644 --- a/aes/tests/aes.rs +++ b/aes/tests/aes.rs @@ -519,7 +519,8 @@ fn encrypt_decrypt_roundtrip( let ciphertext = aes .encrypt(&plaintext.to_be_bytes()) .expect("Encryption failed"); - let ciphertext_u128 = u128::from_be_bytes(ciphertext.as_slice().try_into().unwrap()); + let ciphertext_u128 = + u128::from_be_bytes(ciphertext.as_slice().try_into().expect("ciphertext")); assert_eq!( ciphertext_u128, expected_ciphertext, @@ -528,7 +529,12 @@ fn encrypt_decrypt_roundtrip( // Decrypt let decrypted = aes.decrypt(&ciphertext).expect("Decryption failed"); - let decrypted_u128 = u128::from_be_bytes(decrypted.as_slice().try_into().unwrap()); + let decrypted_u128 = u128::from_be_bytes( + decrypted + .as_slice() + .try_into() + .expect("decrypted plaintext"), + ); assert_eq!( decrypted_u128, plaintext, diff --git a/des/src/block/secret_block.rs b/des/src/block/secret_block.rs index 804d976..5032c8b 100644 --- a/des/src/block/secret_block.rs +++ b/des/src/block/secret_block.rs @@ -15,7 +15,7 @@ macro_rules! secret_block { $vis:vis struct $name:ident ( $int:tt, $bits:expr, $mask:expr ); ) => { $(#[$meta])* - #[derive(Debug, Clone, Copy, PartialEq, Eq)] + #[derive(Debug, Clone, Copy, PartialEq, Eq, ::zeroize::Zeroize)] $vis struct $name($int); impl $name { diff --git a/des/src/utils.rs b/des/src/utils.rs index ab60209..4843812 100644 --- a/des/src/utils.rs +++ b/des/src/utils.rs @@ -23,7 +23,7 @@ pub fn permutate( .fold(0, |acc, (idx, &input_pos_1based)| { let input_bit_pos = input_bit_amount - u64::from(input_pos_1based); let bit_value = (input >> input_bit_pos) & 1; - let output_bit_pos = output_bit_amount - 1 - (idx as u64); + let output_bit_pos = (output_bit_amount - 1) - (idx as u64); acc | (bit_value << output_bit_pos) }) } diff --git a/des/tests/des.rs b/des/tests/des.rs index 420f2cc..2d96425 100644 --- a/des/tests/des.rs +++ b/des/tests/des.rs @@ -120,9 +120,9 @@ fn encrypt_decrypt_roundtrip( let dectrypted = assert_ok!(des.decrypt(&ciphertext)); let re_ciphertext = assert_ok!(des.encrypt(&dectrypted)); - let ciphertext_u64 = cipher_block_to_u64(ciphertext); - let decrypted_u64 = cipher_block_to_u64(dectrypted); - let re_ciphertext_u64 = cipher_block_to_u64(re_ciphertext); + let ciphertext_u64 = cipher_block_to_u64(&ciphertext); + let decrypted_u64 = cipher_block_to_u64(&dectrypted); + let re_ciphertext_u64 = cipher_block_to_u64(&re_ciphertext); assert_eq!( ciphertext_u64, expected_ciphertext, @@ -139,16 +139,16 @@ fn encrypt_decrypt_roundtrip( } #[rstest] -#[case(0x0101010101010101)] -#[case(0xFEFEFEFEFEFEFEFE)] -#[case(0xE001E001E001E001)] +#[case(0x0101_0101_0101_0101)] +#[case(0xFEFE_FEFE_FEFE_FEFE)] +#[case(0xE001_E001_E001_E001)] fn weak_keys(#[case] key: u64) { let des = Des::new(key); let plaintext = TEST_PLAINTEXT; let ciphertext = assert_ok!(des.encrypt(&plaintext.to_be_bytes())); let decrypted = assert_ok!(des.decrypt(&ciphertext)); - let decrypted_u64 = cipher_block_to_u64(decrypted); + let decrypted_u64 = cipher_block_to_u64(&decrypted); assert_eq!( decrypted_u64, plaintext, @@ -163,7 +163,7 @@ fn all_zero_paintext() { let plain = 0u64; let encrypted = assert_ok!(des.encrypt(&plain.to_be_bytes())); let decrypted = assert_ok!(des.decrypt(&encrypted)); - let decrypted_u64 = cipher_block_to_u64(decrypted); + let decrypted_u64 = cipher_block_to_u64(&decrypted); assert_eq!(decrypted_u64, plain, "All-zero plaintext failed"); } @@ -174,7 +174,7 @@ fn all_one_paintext() { let plain = u64::MAX; let encrypted = assert_ok!(des.encrypt(&plain.to_be_bytes())); let decrypted = assert_ok!(des.decrypt(&encrypted)); - let decrypted_u64 = cipher_block_to_u64(decrypted); + let decrypted_u64 = cipher_block_to_u64(&decrypted); assert_eq!(decrypted_u64, plain, "All-one plaintext failed"); } @@ -192,7 +192,7 @@ fn different_inputs() { ); } -fn cipher_block_to_u64(block: Output) -> u64 { +fn cipher_block_to_u64(block: &Output) -> u64 { let bytes = block.as_slice().try_into().expect("8 bytes"); u64::from_be_bytes(bytes) }