mirror of
https://github.com/kristoferssolo/cipher-workshop.git
synced 2026-02-04 06:42:11 +00:00
fix: clippy warnings
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user