diff --git a/Cargo.lock b/Cargo.lock index 9ce84a21..b90903e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2590,9 +2590,9 @@ dependencies = [ [[package]] name = "paste" -version = "1.0.12" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "percent-encoding" @@ -3723,6 +3723,7 @@ dependencies = [ "lazy_static", "mod_exp", "num 0.4.3", + "paste", "primal", "rand", "regex", diff --git a/ciphers/src/digital/block_ciphers/khufu_khafre/khafre.rs b/ciphers/src/digital/block_ciphers/khufu_khafre/khafre.rs index 2d1be7c8..75821ce3 100644 --- a/ciphers/src/digital/block_ciphers/khufu_khafre/khafre.rs +++ b/ciphers/src/digital/block_ciphers/khufu_khafre/khafre.rs @@ -38,14 +38,14 @@ impl Khafre { impl BlockCipher<8> for Khafre { fn encrypt_block(&self, bytes: &mut [u8]) { let mut v = [0u32; 2]; - utils::byte_formatting::fill_u32s_be(&mut v, bytes); + utils::byte_formatting::fill_u32s_be(&mut v, &bytes); for key in self.round_keys {} utils::byte_formatting::u32s_to_bytes_be(bytes, &v); } fn decrypt_block(&self, bytes: &mut [u8]) { let mut v = [0u32; 2]; - utils::byte_formatting::fill_u32s_be(&mut v, bytes); + utils::byte_formatting::fill_u32s_be(&mut v, &bytes); for key in self.round_keys {} utils::byte_formatting::u32s_to_bytes_be(bytes, &v); } diff --git a/ciphers/src/digital/block_ciphers/khufu_khafre/khufu.rs b/ciphers/src/digital/block_ciphers/khufu_khafre/khufu.rs index 33967681..530dda8b 100644 --- a/ciphers/src/digital/block_ciphers/khufu_khafre/khufu.rs +++ b/ciphers/src/digital/block_ciphers/khufu_khafre/khufu.rs @@ -38,7 +38,7 @@ impl Khufu { impl BlockCipher<8> for Khufu { fn encrypt_block(&self, bytes: &mut [u8]) { let mut v = [0u32; 2]; - utils::byte_formatting::fill_u32s_be(&mut v, bytes); + utils::byte_formatting::fill_u32s_be(&mut v, &bytes); for key in self.round_keys {} utils::byte_formatting::u32s_to_bytes_be(bytes, &v); @@ -46,7 +46,7 @@ impl BlockCipher<8> for Khufu { fn decrypt_block(&self, bytes: &mut [u8]) { let mut v = [0u32; 2]; - utils::byte_formatting::fill_u32s_be(&mut v, bytes); + utils::byte_formatting::fill_u32s_be(&mut v, &bytes); for key in self.round_keys {} utils::byte_formatting::u32s_to_bytes_be(bytes, &v); diff --git a/ciphers/src/digital/block_ciphers/speck/speck32.rs b/ciphers/src/digital/block_ciphers/speck/speck32.rs index 99711fb4..effb7b83 100644 --- a/ciphers/src/digital/block_ciphers/speck/speck32.rs +++ b/ciphers/src/digital/block_ciphers/speck/speck32.rs @@ -68,7 +68,7 @@ impl BlockCipher<4> for Speck32_64 { fn encrypt_block(&self, bytes: &mut [u8]) { // Make mutable variables from the working vector let mut v = [0u16; 2]; - fill_u16s_be(&mut v, bytes); + fill_u16s_be(&mut v, &bytes); let [mut x, mut y] = v; for k in self.subkeys { @@ -81,7 +81,7 @@ impl BlockCipher<4> for Speck32_64 { fn decrypt_block(&self, bytes: &mut [u8]) { // Make mutable variables from the working vector let mut v = [0u16; 2]; - fill_u16s_be(&mut v, bytes); + fill_u16s_be(&mut v, &bytes); let [mut x, mut y] = v; for k in self.subkeys.into_iter().rev() { diff --git a/utils/cargo.toml b/utils/cargo.toml index ebabd62f..d15ed24d 100644 --- a/utils/cargo.toml +++ b/utils/cargo.toml @@ -22,4 +22,5 @@ rfd = "0.11.3" mod_exp = "1.0.1" base64 = "0.21.7" strsim = "0.11.0" -strum = { version = "0.26", features = ["derive"] } \ No newline at end of file +strum = { version = "0.26", features = ["derive"] } +paste = "1.0.15" \ No newline at end of file diff --git a/utils/src/byte_formatting.rs b/utils/src/byte_formatting.rs index 89d2f21b..0ac8195e 100644 --- a/utils/src/byte_formatting.rs +++ b/utils/src/byte_formatting.rs @@ -87,11 +87,12 @@ impl std::fmt::Display for ByteFormatError { #[derive(Copy, Clone, Debug, PartialEq, Eq, EnumIter, Display)] pub enum ByteFormat { #[strum(to_string = "Hexadecimal")] + #[strum(default)] Hex, - #[strum(to_string = "Text (UTF-8)")] - Utf8, Base64, Binary, + #[strum(to_string = "Text (UTF-8)")] + Utf8, } impl ByteFormat { @@ -418,126 +419,87 @@ mod bit_function_tests { } } -macro_rules! filler_and_maker { - ($n1: ident, $n2: ident, $n3: ident, $n4: ident, $n5: ident, $n6: ident, $t: ty, $w: literal) => { - pub fn $n1(target: &mut [$t], bytes: &[u8]) { - for (elem, chunk) in target.iter_mut().zip_eq(bytes.chunks_exact($w)) { - *elem = <$t>::from_be_bytes(chunk.try_into().unwrap()); - } - } - - pub fn $n2(bytes: &[u8]) -> [$t; N] { - let mut out = [0; N]; - for (elem, chunk) in out.iter_mut().zip_eq(bytes.chunks_exact($w)) { - *elem = <$t>::from_be_bytes(chunk.try_into().unwrap()); - } - out - } - - pub fn $n3(target: &mut [u8], words: &[$t]) { - for (chunk, word) in target.chunks_exact_mut($w).zip_eq(words) { - chunk.copy_from_slice(&word.to_be_bytes()); - } - } - - pub fn $n4(target: &mut [$t], bytes: &[u8]) { - for (elem, chunk) in target.iter_mut().zip_eq(bytes.chunks_exact($w)) { - *elem = <$t>::from_le_bytes(chunk.try_into().unwrap()); - } - } - - pub fn $n5(bytes: &[u8]) -> [$t; N] { - let mut out = [0; N]; - for (elem, chunk) in out.iter_mut().zip_eq(bytes.chunks_exact($w)) { - *elem = <$t>::from_le_bytes(chunk.try_into().unwrap()); - } - out - } - - pub fn $n6(target: &mut [u8], words: &[$t]) { - for (chunk, word) in target.chunks_exact_mut($w).zip_eq(words) { - chunk.copy_from_slice(&word.to_le_bytes()); - } +use paste::paste; + +macro_rules! fillers_and_makers { + ($t: ty, $w: literal) => { + paste! { + /// Use bytes to fill the target with the type. Panics if target cannot be not exactly filled. Big-endian. + pub fn []>(target: &mut [$t], bytes: T) { + for (elem, chunk) in target.iter_mut().zip_eq(bytes.as_ref().chunks_exact($w)) { + *elem = <$t>::from_be_bytes(chunk.try_into().unwrap()); + } + } + + /// Use bytes to make an array filled with the type. Panics if the array cannot be exactly filled. Big-endian. + pub fn [](bytes: &[u8]) -> [$t; N] { + let mut out = [0; N]; + for (elem, chunk) in out.iter_mut().zip_eq(bytes.chunks_exact($w)) { + *elem = <$t>::from_be_bytes(chunk.try_into().unwrap()); + } + out + } + + /// Take a slice of the type and filled the target with bytes. Panics if the target cannot be exactly filled. Big-endian. + pub fn [<$t s_to_bytes_be>]>(target: &mut [u8], words: T) { + for (chunk, word) in target.chunks_exact_mut($w).zip_eq(words.as_ref()) { + chunk.copy_from_slice(&word.to_be_bytes()); + } + } + + /// Use bytes to fill the target with the type. Panics if target cannot be not exactly filled. Little-endian. + pub fn []>(target: &mut [$t], bytes: T) { + for (elem, chunk) in target.iter_mut().zip_eq(bytes.as_ref().chunks_exact($w)) { + *elem = <$t>::from_le_bytes(chunk.try_into().unwrap()); + } + } + + /// Use bytes to make an array filled with the type. Panics if the array cannot be exactly filled. Little-endian. + pub fn [](bytes: &[u8]) -> [$t; N] { + let mut out = [0; N]; + for (elem, chunk) in out.iter_mut().zip_eq(bytes.chunks_exact($w)) { + *elem = <$t>::from_le_bytes(chunk.try_into().unwrap()); + } + out + } + + /// Take a slice of the type and filled the target with bytes. Panics if the target cannot be exactly filled. Little-endian. + pub fn [<$t s_to_bytes_le>]>(target: &mut [u8], words: T) { + for (chunk, word) in target.chunks_exact_mut($w).zip_eq(words.as_ref()) { + chunk.copy_from_slice(&word.to_le_bytes()); + } + } } }; } -filler_and_maker!( - fill_u16s_be, - make_u16s_be, - u16s_to_bytes_be, - fill_u16s_le, - make_u16s_le, - u16s_to_bytes_le, - u16, - 2 -); - -filler_and_maker!( - fill_u32s_be, - make_u32s_be, - u32s_to_bytes_be, - fill_u32s_le, - make_u32s_le, - u32s_to_bytes_le, - u32, - 4 -); - -filler_and_maker!( - fill_i32s_be, - make_i32s_be, - i32s_to_bytes_be, - fill_i32s_le, - make_i32s_le, - i32s_to_bytes_le, - i32, - 4 -); - -filler_and_maker!( - fill_u64s_be, - make_u64s_be, - u64s_to_bytes_be, - fill_u64s_le, - make_u64s_le, - u64s_to_bytes_le, - u64, - 8 -); - -filler_and_maker!( - fill_i64s_be, - make_i64s_be, - i64s_to_bytes_be, - fill_i64s_le, - make_i64s_le, - i64s_to_bytes_le, - i64, - 8 -); - -filler_and_maker!( - fill_u128s_be, - make_u128s_be, - u128s_to_bytes_be, - fill_u128s_le, - make_u128s_le, - u128s_to_bytes_le, - u128, - 16 -); +// Creates these functions +// fill_TYPEs_be() +// make_TYPEs_be() +// TYPESs_to_bytes_be() +// fill_TYPEs_le() +// make_TYPEs_le() +// TYPESs_to_bytes_le() + +fillers_and_makers!(u16, 2); +fillers_and_makers!(i16, 2); +fillers_and_makers!(u32, 4); +fillers_and_makers!(i32, 4); +fillers_and_makers!(u64, 8); +fillers_and_makers!(i64, 8); +fillers_and_makers!(u128, 16); +fillers_and_makers!(i128, 16); /// If target is longer it is only partially overwritten. If target is shorter the extra source is ignored. -pub fn overwrite_bytes(target: &mut [u8], source: &[u8]) { - for (t, s) in target.iter_mut().zip(source.iter()) { +pub fn overwrite_bytes>(target: &mut [u8], source: T) { + for (t, s) in target.iter_mut().zip(source.as_ref().iter()) { *t = *s } } /// If target is longer it is only partially XORed. If target is shorter the extra source is ignored. -pub fn xor_into_bytes(target: &mut [u8], source: &[u8]) { - for (t, s) in target.iter_mut().zip(source.iter()) { +pub fn xor_into_bytes>(target: &mut [u8], source: T) { + for (t, s) in target.iter_mut().zip(source.as_ref().iter()) { *t ^= *s } }