Skip to content

Commit

Permalink
simon tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SymmetricChaos committed Sep 4, 2024
1 parent 5afb412 commit 368f16e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 52 deletions.
24 changes: 12 additions & 12 deletions ciphers/src/digital/block_ciphers/simon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ pub enum SimonVariant {
impl SimonVariant {
pub fn block_size(&self) -> u32 {
match self {
Self::Simon32_64 => 32,
Self::Simon64_96 => 64,
Self::Simon64_128 => 64,
Self::Simon128_128 => 128,
Self::Simon128_192 => 128,
Self::Simon128_256 => 128,
Self::Simon32_64 => 4,
Self::Simon64_96 => 8,
Self::Simon64_128 => 8,
Self::Simon128_128 => 16,
Self::Simon128_192 => 16,
Self::Simon128_256 => 16,
}
}

pub fn key_size(&self) -> u32 {
match self {
Self::Simon32_64 => 64,
Self::Simon64_96 => 96,
Self::Simon64_128 => 128,
Self::Simon128_128 => 128,
Self::Simon128_192 => 192,
Self::Simon128_256 => 256,
Self::Simon32_64 => 8,
Self::Simon64_96 => 12,
Self::Simon64_128 => 16,
Self::Simon128_128 => 16,
Self::Simon128_192 => 24,
Self::Simon128_256 => 32,
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions ciphers/src/digital/block_ciphers/simon/simon128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ macro_rules! simon128 {
subkeys[$key_words - i - 1] = key[i]
}

// println!("{:04x?}", subkeys);

for i in $key_words..$rounds as usize {
let mut t = subkeys[i - 1].rotate_right(3);
if $key_words == 4 {
Expand Down Expand Up @@ -149,11 +147,14 @@ crate::impl_cipher_for_block_cipher!(Simon128_192, 16);
simon128!(Simon128_256, 4, 72, 4);
crate::impl_cipher_for_block_cipher!(Simon128_256, 16);

// crate::test_block_cipher!(
// Simon128_96::default().with_key([0x19, 0x18, 0x11, 0x10, 0x09, 0x08, 0x01, 0x00]), test_32_128,
// [],
// [];
// Simon128_128::default().with_key([0x19, 0x18, 0x11, 0x10, 0x09, 0x08, 0x01, 0x00]), test_32_128,
// [],
// [];
// );
crate::test_block_cipher!(
Simon128_128::default().with_key([0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00]), test_128_128,
[0x63, 0x73, 0x65, 0x64, 0x20, 0x73, 0x72, 0x65, 0x6c, 0x6c, 0x65, 0x76, 0x61, 0x72, 0x74, 0x20],
[0x49, 0x68, 0x1b, 0x1e, 0x1e, 0x54, 0xfe, 0x3f, 0x65, 0xaa, 0x83, 0x2a, 0xf8, 0x4e, 0x0b, 0xbc];
Simon128_192::default().with_key([0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00]), test_128_196,
[0x20, 0x65, 0x72, 0x65, 0x68, 0x74, 0x20, 0x6e, 0x65, 0x68, 0x77, 0x20, 0x65, 0x62, 0x69, 0x72],
[0xc4, 0xac, 0x61, 0xef, 0xfc, 0xdc, 0x0d, 0x4f, 0x6c, 0x9c, 0x8d, 0x6e, 0x25, 0x97, 0xb8, 0x5b];
Simon128_256::default().with_key([0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00]), test_128_256,
[0x74, 0x20, 0x6e, 0x69, 0x20, 0x6d, 0x6f, 0x6f, 0x6d, 0x69, 0x73, 0x20, 0x61, 0x20, 0x73, 0x69],
[0x8d, 0x2b, 0x55, 0x79, 0xaf, 0xc8, 0xa3, 0xa0, 0x3b, 0xf7, 0x2a, 0x87, 0xef, 0xe7, 0xb8, 0x68];
);
13 changes: 6 additions & 7 deletions ciphers/src/digital/block_ciphers/simon/simon32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::digital::block_ciphers::block_cipher::{BCMode, BCPadding, BlockCipher
use super::select_z_bit;

const J: usize = 0;
const KEY_WORDS: usize = 4; // number of key words
const KEY_WORDS: usize = 4;
const ROUNDS: usize = 32;

pub struct Simon32_64 {
Expand Down Expand Up @@ -84,8 +84,6 @@ impl Simon32_64 {
subkeys[KEY_WORDS - i - 1] = key[i]
}

// println!("{:04x?}", subkeys);

for i in KEY_WORDS..ROUNDS as usize {
let mut t = subkeys[i - 1].rotate_right(3);
if KEY_WORDS == 4 {
Expand Down Expand Up @@ -126,13 +124,13 @@ impl BlockCipher<4> for Simon32_64 {
fill_u16s_be(&mut v, bytes);
let [mut x, mut y] = v;

for k in self.subkeys.into_iter().rev() {
let t = x;
for k in self.subkeys {
let t = y;
// L_i+1 = R_i
x = y;
y = x;

// R_i+1 = L_i xor f(R_i)
y = t ^ super::round!(y, k);
x = t ^ super::round!(x, k);
}

u16s_to_bytes_be(bytes, &[x, y]);
Expand All @@ -154,6 +152,7 @@ mod simon_tests {
[0x0100, 0x0908, 0x1110, 0x1918, 0x71C3, 0xB649, 0x56D4, 0xE070, 0xF15A, 0xC535],
&cipher.subkeys[0..10]
);
println!("{:04x?}", cipher.subkeys);
}
}

Expand Down
18 changes: 8 additions & 10 deletions ciphers/src/digital/block_ciphers/simon/simon64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ macro_rules! simon64 {
subkeys[$key_words - i - 1] = key[i]
}

// println!("{:04x?}", subkeys);

for i in $key_words..$rounds as usize {
let mut t = subkeys[i - 1].rotate_right(3);
if $key_words == 4 {
Expand Down Expand Up @@ -147,11 +145,11 @@ crate::impl_cipher_for_block_cipher!(Simon64_96, 8);
simon64!(Simon64_128, 4, 44, 3);
crate::impl_cipher_for_block_cipher!(Simon64_128, 8);

// crate::test_block_cipher!(
// Simon64_96::default().with_key([0x19, 0x18, 0x11, 0x10, 0x09, 0x08, 0x01, 0x00]), test_32_64,
// [],
// [];
// Simon64_128::default().with_key([0x19, 0x18, 0x11, 0x10, 0x09, 0x08, 0x01, 0x00]), test_32_64,
// [],
// [];
// );
crate::test_block_cipher!(
Simon64_96::default().with_key([0x13, 0x12, 0x11, 0x10, 0x0b, 0x0a, 0x09, 0x08, 0x03, 0x02, 0x01, 0x00]), test_64_96,
[0x6f, 0x72, 0x20, 0x67, 0x6e, 0x69, 0x6c, 0x63],
[0x5c, 0xa2, 0xe2, 0x7f, 0x11, 0x1a, 0x8f, 0xc8];
Simon64_128::default().with_key([0x1b, 0x1a, 0x19, 0x18, 0x13, 0x12, 0x11, 0x10, 0x0b, 0x0a, 0x09, 0x08, 0x03, 0x02, 0x01, 0x00]), test_64_128,
[0x65, 0x6b, 0x69, 0x6c, 0x20, 0x64, 0x6e, 0x75],
[0x44, 0xc8, 0xfc, 0x20, 0xb9, 0xdf, 0xa0, 0x7a];
);
24 changes: 12 additions & 12 deletions ciphers/src/digital/block_ciphers/speck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ pub enum SpeckVariant {
impl SpeckVariant {
pub fn block_size(&self) -> u32 {
match self {
Self::Speck32_64 => 32,
Self::Speck64_96 => 64,
Self::Speck64_128 => 64,
Self::Speck128_128 => 128,
Self::Speck128_192 => 128,
Self::Speck128_256 => 128,
Self::Speck32_64 => 4,
Self::Speck64_96 => 8,
Self::Speck64_128 => 8,
Self::Speck128_128 => 16,
Self::Speck128_192 => 16,
Self::Speck128_256 => 16,
}
}

pub fn key_size(&self) -> u32 {
match self {
Self::Speck32_64 => 64,
Self::Speck64_96 => 96,
Self::Speck64_128 => 128,
Self::Speck128_128 => 128,
Self::Speck128_192 => 192,
Self::Speck128_256 => 256,
Self::Speck32_64 => 8,
Self::Speck64_96 => 12,
Self::Speck64_128 => 16,
Self::Speck128_128 => 16,
Self::Speck128_192 => 20,
Self::Speck128_256 => 32,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ciphers/src/digital/block_ciphers/speck/speck128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ macro_rules! speck128 {
pub mode: BCMode,
pub padding: BCPadding,
pub iv: u128,
subkeys: [u64; $rounds],
pub subkeys: [u64; $rounds],
}

impl Default for $name {
Expand Down

0 comments on commit 368f16e

Please sign in to comment.