Skip to content

Commit

Permalink
big improvements to fillers and makers
Browse files Browse the repository at this point in the history
  • Loading branch information
SymmetricChaos committed Nov 17, 2024
1 parent 72a1893 commit a778b21
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 120 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ciphers/src/digital/block_ciphers/khufu_khafre/khafre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions ciphers/src/digital/block_ciphers/khufu_khafre/khufu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ 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);
}

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);
Expand Down
4 changes: 2 additions & 2 deletions ciphers/src/digital/block_ciphers/speck/speck32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion utils/cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
strum = { version = "0.26", features = ["derive"] }
paste = "1.0.15"
184 changes: 73 additions & 111 deletions utils/src/byte_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<const N: usize>(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<const N: usize>(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 [<fill_ $t s_be>]<T: AsRef<[u8]>>(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 [<make_ $t s_be>]<const N: usize>(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>]<T: AsRef<[$t]>>(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 [<fill_ $t s_le>]<T: AsRef<[u8]>>(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 [<make_ $t s_le>]<const N: usize>(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>]<T: AsRef<[$t]>>(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<T: AsRef<[u8]>>(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<T: AsRef<[u8]>>(target: &mut [u8], source: T) {
for (t, s) in target.iter_mut().zip(source.as_ref().iter()) {
*t ^= *s
}
}

0 comments on commit a778b21

Please sign in to comment.