From 368f16e840538dc993da2588c093ebbca7785a99 Mon Sep 17 00:00:00 2001 From: SymmetricChaos <42520289+SymmetricChaos@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:06:11 -0400 Subject: [PATCH] simon tests --- .../src/digital/block_ciphers/simon/mod.rs | 24 +++++++++---------- .../digital/block_ciphers/simon/simon128.rs | 21 ++++++++-------- .../digital/block_ciphers/simon/simon32.rs | 13 +++++----- .../digital/block_ciphers/simon/simon64.rs | 18 +++++++------- .../src/digital/block_ciphers/speck/mod.rs | 24 +++++++++---------- .../digital/block_ciphers/speck/speck128.rs | 2 +- 6 files changed, 50 insertions(+), 52 deletions(-) diff --git a/ciphers/src/digital/block_ciphers/simon/mod.rs b/ciphers/src/digital/block_ciphers/simon/mod.rs index e8cdd9d3..9cdb082b 100644 --- a/ciphers/src/digital/block_ciphers/simon/mod.rs +++ b/ciphers/src/digital/block_ciphers/simon/mod.rs @@ -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, } } } diff --git a/ciphers/src/digital/block_ciphers/simon/simon128.rs b/ciphers/src/digital/block_ciphers/simon/simon128.rs index 33c37710..e739f796 100644 --- a/ciphers/src/digital/block_ciphers/simon/simon128.rs +++ b/ciphers/src/digital/block_ciphers/simon/simon128.rs @@ -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 { @@ -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]; +); diff --git a/ciphers/src/digital/block_ciphers/simon/simon32.rs b/ciphers/src/digital/block_ciphers/simon/simon32.rs index a6db099d..3c05cf29 100644 --- a/ciphers/src/digital/block_ciphers/simon/simon32.rs +++ b/ciphers/src/digital/block_ciphers/simon/simon32.rs @@ -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 { @@ -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 { @@ -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]); @@ -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); } } diff --git a/ciphers/src/digital/block_ciphers/simon/simon64.rs b/ciphers/src/digital/block_ciphers/simon/simon64.rs index 0dde10bf..f374c5f6 100644 --- a/ciphers/src/digital/block_ciphers/simon/simon64.rs +++ b/ciphers/src/digital/block_ciphers/simon/simon64.rs @@ -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 { @@ -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]; +); diff --git a/ciphers/src/digital/block_ciphers/speck/mod.rs b/ciphers/src/digital/block_ciphers/speck/mod.rs index bfae4288..5b565987 100644 --- a/ciphers/src/digital/block_ciphers/speck/mod.rs +++ b/ciphers/src/digital/block_ciphers/speck/mod.rs @@ -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, } } } diff --git a/ciphers/src/digital/block_ciphers/speck/speck128.rs b/ciphers/src/digital/block_ciphers/speck/speck128.rs index ca90cdda..a8f740b4 100644 --- a/ciphers/src/digital/block_ciphers/speck/speck128.rs +++ b/ciphers/src/digital/block_ciphers/speck/speck128.rs @@ -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 {