Skip to content

Commit

Permalink
extended debugging macro idea
Browse files Browse the repository at this point in the history
  • Loading branch information
SymmetricChaos committed Sep 22, 2024
1 parent f800690 commit ca21f52
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
18 changes: 9 additions & 9 deletions ciphers/src/digital/block_ciphers/fealnx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::block_cipher::{BCMode, BCPadding, BlockCipher};
use utils::byte_formatting::{fill_u32s_be, u32s_to_bytes_be, ByteFormat};

const DEBUG: bool = false;
macro_rules! debug {
macro_rules! debug_state {
($s:literal, $v:ident) => {
if DEBUG {
print!($s);
Expand Down Expand Up @@ -102,13 +102,13 @@ impl BlockCipher<8> for FealNx {
fn encrypt_block(&self, bytes: &mut [u8]) {
let mut v = [0u32; 2];
fill_u32s_be(&mut v, bytes);
debug!("e input:", v);
debug_state!("e input:", v);

// Preprocessing stage
v[0] ^= (self.subkeys[N] as u32) << 16 | self.subkeys[N + 1] as u32;
v[1] ^= (self.subkeys[N + 2] as u32) << 16 | self.subkeys[N + 3] as u32;
v[1] ^= v[0];
debug!("e pre:", v);
debug_state!("e pre:", v);

// Feistel network
for subkey in self.subkeys.into_iter().take(32) {
Expand All @@ -118,30 +118,30 @@ impl BlockCipher<8> for FealNx {

// R_i+1 = L_i xor f(R_i)
v[1] = t ^ f(v[1], subkey);
debug!("e med:", v);
debug_state!("e med:", v);
}

// Postprocessing
v.swap(0, 1);
v[1] ^= v[0];
v[0] ^= (self.subkeys[N + 4] as u32) << 16 | self.subkeys[N + 5] as u32;
v[1] ^= (self.subkeys[N + 6] as u32) << 16 | self.subkeys[N + 7] as u32;
debug!("e post:", v);
debug_state!("e post:", v);

u32s_to_bytes_be(bytes, &v);
}

fn decrypt_block(&self, bytes: &mut [u8]) {
let mut v = [0u32; 2];
fill_u32s_be(&mut v, bytes);
debug!("d input:", v);
debug_state!("d input:", v);

// Preprocessing stage
v[0] ^= (self.subkeys[N + 4] as u32) << 16 | self.subkeys[N + 5] as u32;
v[1] ^= (self.subkeys[N + 6] as u32) << 16 | self.subkeys[N + 7] as u32;
v[1] ^= v[0];
v.swap(0, 1);
debug!("d pre:", v);
debug_state!("d pre:", v);

// Feistel network
for subkey in self.subkeys.into_iter().take(32).rev() {
Expand All @@ -151,14 +151,14 @@ impl BlockCipher<8> for FealNx {

// R_i+1 = L_i xor f(R_i)
v[0] = t ^ f(v[0], subkey);
debug!("d med:", v);
debug_state!("d med:", v);
}

// Postprocessing
v[1] ^= v[0];
v[0] ^= (self.subkeys[N] as u32) << 16 | self.subkeys[N + 1] as u32;
v[1] ^= (self.subkeys[N + 2] as u32) << 16 | self.subkeys[N + 3] as u32;
debug!("d post:", v);
debug_state!("d post:", v);

u32s_to_bytes_be(bytes, &v);
}
Expand Down
1 change: 0 additions & 1 deletion ciphers/src/digital/block_ciphers/gost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,4 @@ crate::test_block_cipher!(
Gost::default().with_sboxes(TEST_SBOX), test_1,
[0, 0, 0, 0, 0, 0, 0, 0],
[0x0e, 0xca, 0x1a, 0x54, 0x4d, 0x33, 0x07, 0x0b];

);
34 changes: 17 additions & 17 deletions ciphers/src/digital/stream_ciphers/chacha/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
use itertools::Itertools;

pub mod chacha;
pub mod chacha20poly1305;
pub mod chacha_ietf;
pub mod xchacha;
pub mod xchacha_ietf;

const DEBUG: bool = false;
macro_rules! debug_state {
($s:literal, $v:ident) => {
if DEBUG {
print!($s);
println!("\n{}", $v);
}
};
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChaChaState([u32; 16]);
Expand All @@ -26,11 +36,9 @@ impl std::ops::IndexMut<usize> for ChaChaState {

impl std::fmt::Display for ChaChaState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut out = String::with_capacity(148);
let mut out = String::with_capacity(144);
for line in self.0.chunks_exact(4) {
for word in line {
out.push_str(&format!("{:08x?} ", word))
}
out.push_str(&line.iter().map(|word| format!("{:08x?}", word)).join(" "));
out.push('\n')
}
writeln!(f, "{}", out)
Expand All @@ -39,13 +47,9 @@ impl std::fmt::Display for ChaChaState {

impl ChaChaState {
pub fn new(state: [u32; 16]) -> Self {
if DEBUG {
let s = Self(state);
println!("initial:\n{s}");
s
} else {
Self(state)
}
let s = Self(state);
debug_state!("initial", s);
s
}

pub fn quarter_round(&mut self, a: usize, b: usize, c: usize, d: usize) {
Expand All @@ -71,19 +75,15 @@ impl ChaChaState {
self.quarter_round(1, 5, 9, 13);
self.quarter_round(2, 6, 10, 14);
self.quarter_round(3, 7, 11, 15);
if DEBUG {
println!("column:\n{self}")
}
debug_state!("column", self);
}

pub fn diag_round(&mut self) {
self.quarter_round(0, 5, 10, 15);
self.quarter_round(1, 6, 11, 12);
self.quarter_round(2, 7, 8, 13);
self.quarter_round(3, 4, 9, 14);
if DEBUG {
println!("diagon:\n{self}")
}
debug_state!("diagonal", self);
}

pub fn double_round(&mut self) {
Expand Down

0 comments on commit ca21f52

Please sign in to comment.