fix: clippy warnings

This commit is contained in:
Kristofers Solo 2025-12-18 18:42:50 +02:00
parent 9869036bdf
commit 0687fe0431
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
5 changed files with 43 additions and 39 deletions

View File

@ -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);

View File

@ -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,

View File

@ -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 {

View File

@ -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)
})
}

View File

@ -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)
}