fix: wrong value in S8 box

This commit is contained in:
Kristofers Solo 2025-10-17 19:41:46 +03:00
parent 6175305641
commit 2d59f4fb70
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
5 changed files with 19 additions and 12 deletions

View File

@ -22,18 +22,18 @@ impl Block64 {
Self(u64::from_be_bytes(bytes))
}
#[inline]
#[must_use]
pub const fn to_le_bytes(self) -> [u8; 8] {
self.0.to_le_bytes()
}
#[inline]
#[must_use]
pub const fn to_be_bytes(self) -> [u8; 8] {
self.0.to_be_bytes()
}
#[inline]
#[must_use]
pub const fn to_le_bytes(self) -> [u8; 8] {
self.0.to_le_bytes()
}
#[inline]
#[must_use]
pub fn split_lr(self) -> LR {
@ -41,6 +41,12 @@ impl Block64 {
}
}
impl From<[u8; 8]> for Block64 {
fn from(bytes: [u8; 8]) -> Self {
Self::from_be_bytes(bytes)
}
}
impl From<u64> for Block64 {
fn from(value: u64) -> Self {
Self::new(value)
@ -65,12 +71,12 @@ impl From<&Block64> for LR {
impl From<Block64> for Vec<u8> {
fn from(value: Block64) -> Self {
value.0.to_le_bytes().to_vec()
value.0.to_be_bytes().to_vec()
}
}
impl From<&Block64> for Vec<u8> {
fn from(value: &Block64) -> Self {
value.0.to_le_bytes().to_vec()
value.0.to_be_bytes().to_vec()
}
}

View File

@ -95,7 +95,7 @@ pub const S_BOXES: [[[u8; 16]; 4]; 8] = [
],
// S8
[
[13, 2, 8, 4, 6, 15, 11, 1, 19, 9, 3, 14, 5, 0, 12, 7],
[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
[1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
[7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
[2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11],

View File

@ -58,6 +58,7 @@ where
for subkey in subkeys {
feistel(&mut lr, subkey);
}
lr.swap();
lr.into()
}

View File

@ -44,7 +44,7 @@ impl AsRef<[u8]> for Key {
impl From<u64> for Key {
fn from(key: u64) -> Self {
Self(key.to_le_bytes())
Self(key.to_be_bytes())
}
}

View File

@ -116,7 +116,7 @@ fn encrypt_decrypt_roundtrip(
) {
let des = assert_ok!(Des::new(key), "Valid DES key");
let ciphertext = assert_ok!(des.encrypt(&plaintext.to_le_bytes()));
let ciphertext = assert_ok!(des.encrypt(&plaintext.to_be_bytes()));
let dectrypted = assert_ok!(des.decrypt(&ciphertext));
let re_ciphertext = assert_ok!(des.encrypt(&dectrypted));
@ -126,7 +126,7 @@ fn encrypt_decrypt_roundtrip(
assert_eq!(
ciphertext_u64, expected_ciphertext,
"Encyption failed. Expected 0x{expected_ciphertext:016X}, got 0x{decrypted_u64:016X}"
"Encyption failed. Expected 0x{expected_ciphertext:016X}, got 0x{ciphertext_u64:016X}"
);
assert_eq!(
decrypted_u64, plaintext,