mirror of
https://github.com/kristoferssolo/cipher-workshop.git
synced 2025-12-20 11:04:38 +00:00
fix: clippy warnings
This commit is contained in:
parent
9869036bdf
commit
0687fe0431
@ -6,33 +6,31 @@ pub trait SboxLookup: Sized {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_sbox_lookup {
|
macro_rules! impl_sbox_lookup {
|
||||||
($ty:ty, $bytes:expr) => {
|
($($ty:ty),*) => {
|
||||||
impl SboxLookup for $ty {
|
$(
|
||||||
fn sbox_lookup(self) -> Self {
|
impl SboxLookup for $ty {
|
||||||
(0..$bytes).fold(0, |acc, idx| {
|
fn sbox_lookup(self) -> Self {
|
||||||
let shift = ($bytes - 1 - idx) * 8;
|
let mut bytes = self.to_le_bytes();
|
||||||
let byte = ((self >> shift) & 0xFF) as u8;
|
for b in bytes.iter_mut() {
|
||||||
let row = (byte >> 4) as usize;
|
let row = (*b >> 4) as usize;
|
||||||
let col = (byte & 0xF) as usize;
|
let col = (*b & 0x0F) as usize;
|
||||||
acc | Self::from(S_BOXES[row][col]) << shift
|
*b = S_BOXES[row][col];
|
||||||
})
|
}
|
||||||
}
|
Self::from_le_bytes(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
fn inv_sbox_lookup(self) -> Self {
|
fn inv_sbox_lookup(self) -> Self {
|
||||||
(0..$bytes).fold(0, |acc, idx| {
|
let mut bytes = self.to_le_bytes();
|
||||||
let shift = ($bytes - 1 - idx) * 8;
|
for b in bytes.iter_mut() {
|
||||||
let byte = ((self >> shift) & 0xFF) as u8;
|
let row = (*b >> 4) as usize;
|
||||||
let row = (byte >> 4) as usize;
|
let col = (*b & 0x0F) as usize;
|
||||||
let col = (byte & 0xF) as usize;
|
*b = INV_S_BOXES[row][col];
|
||||||
acc | Self::from(INV_S_BOXES[row][col]) << shift
|
}
|
||||||
})
|
Self::from_le_bytes(bytes)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
)*
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_sbox_lookup!(u8, 1);
|
impl_sbox_lookup!(u8, u16, u32, u64, u128);
|
||||||
impl_sbox_lookup!(u16, 2);
|
|
||||||
impl_sbox_lookup!(u32, 4);
|
|
||||||
impl_sbox_lookup!(u64, 8);
|
|
||||||
impl_sbox_lookup!(u128, 16);
|
|
||||||
|
|||||||
@ -519,7 +519,8 @@ fn encrypt_decrypt_roundtrip(
|
|||||||
let ciphertext = aes
|
let ciphertext = aes
|
||||||
.encrypt(&plaintext.to_be_bytes())
|
.encrypt(&plaintext.to_be_bytes())
|
||||||
.expect("Encryption failed");
|
.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!(
|
assert_eq!(
|
||||||
ciphertext_u128, expected_ciphertext,
|
ciphertext_u128, expected_ciphertext,
|
||||||
@ -528,7 +529,12 @@ fn encrypt_decrypt_roundtrip(
|
|||||||
|
|
||||||
// Decrypt
|
// Decrypt
|
||||||
let decrypted = aes.decrypt(&ciphertext).expect("Decryption failed");
|
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!(
|
assert_eq!(
|
||||||
decrypted_u128, plaintext,
|
decrypted_u128, plaintext,
|
||||||
|
|||||||
@ -15,7 +15,7 @@ macro_rules! secret_block {
|
|||||||
$vis:vis struct $name:ident ( $int:tt, $bits:expr, $mask:expr );
|
$vis:vis struct $name:ident ( $int:tt, $bits:expr, $mask:expr );
|
||||||
) => {
|
) => {
|
||||||
$(#[$meta])*
|
$(#[$meta])*
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, ::zeroize::Zeroize)]
|
||||||
$vis struct $name($int);
|
$vis struct $name($int);
|
||||||
|
|
||||||
impl $name {
|
impl $name {
|
||||||
|
|||||||
@ -23,7 +23,7 @@ pub fn permutate(
|
|||||||
.fold(0, |acc, (idx, &input_pos_1based)| {
|
.fold(0, |acc, (idx, &input_pos_1based)| {
|
||||||
let input_bit_pos = input_bit_amount - u64::from(input_pos_1based);
|
let input_bit_pos = input_bit_amount - u64::from(input_pos_1based);
|
||||||
let bit_value = (input >> input_bit_pos) & 1;
|
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)
|
acc | (bit_value << output_bit_pos)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -120,9 +120,9 @@ fn encrypt_decrypt_roundtrip(
|
|||||||
let dectrypted = assert_ok!(des.decrypt(&ciphertext));
|
let dectrypted = assert_ok!(des.decrypt(&ciphertext));
|
||||||
let re_ciphertext = assert_ok!(des.encrypt(&dectrypted));
|
let re_ciphertext = assert_ok!(des.encrypt(&dectrypted));
|
||||||
|
|
||||||
let ciphertext_u64 = cipher_block_to_u64(ciphertext);
|
let ciphertext_u64 = cipher_block_to_u64(&ciphertext);
|
||||||
let decrypted_u64 = cipher_block_to_u64(dectrypted);
|
let decrypted_u64 = cipher_block_to_u64(&dectrypted);
|
||||||
let re_ciphertext_u64 = cipher_block_to_u64(re_ciphertext);
|
let re_ciphertext_u64 = cipher_block_to_u64(&re_ciphertext);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ciphertext_u64, expected_ciphertext,
|
ciphertext_u64, expected_ciphertext,
|
||||||
@ -139,16 +139,16 @@ fn encrypt_decrypt_roundtrip(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
#[case(0x0101010101010101)]
|
#[case(0x0101_0101_0101_0101)]
|
||||||
#[case(0xFEFEFEFEFEFEFEFE)]
|
#[case(0xFEFE_FEFE_FEFE_FEFE)]
|
||||||
#[case(0xE001E001E001E001)]
|
#[case(0xE001_E001_E001_E001)]
|
||||||
fn weak_keys(#[case] key: u64) {
|
fn weak_keys(#[case] key: u64) {
|
||||||
let des = Des::new(key);
|
let des = Des::new(key);
|
||||||
let plaintext = TEST_PLAINTEXT;
|
let plaintext = TEST_PLAINTEXT;
|
||||||
let ciphertext = assert_ok!(des.encrypt(&plaintext.to_be_bytes()));
|
let ciphertext = assert_ok!(des.encrypt(&plaintext.to_be_bytes()));
|
||||||
let decrypted = assert_ok!(des.decrypt(&ciphertext));
|
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!(
|
assert_eq!(
|
||||||
decrypted_u64, plaintext,
|
decrypted_u64, plaintext,
|
||||||
@ -163,7 +163,7 @@ fn all_zero_paintext() {
|
|||||||
let plain = 0u64;
|
let plain = 0u64;
|
||||||
let encrypted = assert_ok!(des.encrypt(&plain.to_be_bytes()));
|
let encrypted = assert_ok!(des.encrypt(&plain.to_be_bytes()));
|
||||||
let decrypted = assert_ok!(des.decrypt(&encrypted));
|
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");
|
assert_eq!(decrypted_u64, plain, "All-zero plaintext failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +174,7 @@ fn all_one_paintext() {
|
|||||||
let plain = u64::MAX;
|
let plain = u64::MAX;
|
||||||
let encrypted = assert_ok!(des.encrypt(&plain.to_be_bytes()));
|
let encrypted = assert_ok!(des.encrypt(&plain.to_be_bytes()));
|
||||||
let decrypted = assert_ok!(des.decrypt(&encrypted));
|
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");
|
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");
|
let bytes = block.as_slice().try_into().expect("8 bytes");
|
||||||
u64::from_be_bytes(bytes)
|
u64::from_be_bytes(bytes)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user