Skip to content

Commit

Permalink
confirmed shabal test vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
SymmetricChaos committed Nov 26, 2024
1 parent 9a146d1 commit db28ff2
Showing 1 changed file with 14 additions and 60 deletions.
74 changes: 14 additions & 60 deletions hashers/src/shabal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::traits::ClassicHasher;
use utils::{
byte_formatting::{make_u32s_be, make_u32s_le, ByteFormat},
padding::{bit_padding, md_strengthening_64_be, md_strengthening_64_le},
byte_formatting::{make_u32s_le, ByteFormat},
padding::bit_padding,
};

// https://www.cs.rit.edu/~ark/20090927/Round2Candidates/Shabal.pdf
Expand Down Expand Up @@ -85,26 +85,8 @@ const O1: usize = 13;
const O2: usize = 9;
const O3: usize = 6;

// fn single_permutation(m: &[u32; 16], a: &mut [u32; 12], b: &mut [u32; 16], c: &[u32; 16]) {
// for i in 0..16 {
// let p = i + (16 * j);
// let t0 = a[p % R];
// let t1 = a[((i + 15) + (16 * j)) % R].rotate_left(15).wrapping_mul(5);
// let t2 = c[(24 - i) % 16];
// a[p % R] = (t0 ^ t1 ^ t2).wrapping_mul(3)
// ^ b[(i + O1) % 16]
// ^ (b[(i + O2) % 16] & !b[(i + O3) % 16])
// ^ m[i];
// b[i] = b[i].rotate_left(1) ^ !a[p % R];
// }
// }

fn keyed_permutation(m: &[u32; 16], a: &mut [u32; 12], b: &mut [u32; 16], c: &[u32; 16]) {
b.iter_mut().for_each(|b| *b = b.rotate_left(17));
// println!("rotate B");
// println!("{:08x?}", a);
// println!("{:08x?}", b);
// println!("{:08x?}", c);

for j in 0..P {
for i in 0..16 {
Expand All @@ -118,10 +100,6 @@ fn keyed_permutation(m: &[u32; 16], a: &mut [u32; 12], b: &mut [u32; 16], c: &[u
^ m[i];
b[i] = b[i].rotate_left(1) ^ !a[p % R];
}
// println!("permutation j = {}", j);
// println!("{:08x?}", a);
// println!("{:08x?}", b);
// println!("{:08x?}", c);
}

for j in 0..36 {
Expand Down Expand Up @@ -164,34 +142,20 @@ impl ClassicHasher for Shabal256 {
let mut b = IV256B;
let mut c = IV256C;

// let mut a = [0_u32; 12];
// let mut b = [0_u32; 16];
// let mut c = [0_u32; 16];

let mut ctr: u64 = 0xffffffffffffffff;
let mut ctr: u64 = 0;

for block in input.chunks_exact(64) {
ctr = ctr.wrapping_add(1);
let m = make_u32s_le::<16>(block);
// println!("M");
// println!("{:08x?}", m);

// Insert the message into b by addition
for i in 0..16 {
b[i] = b[i].wrapping_add(m[i]);
}
println!("add M to B");
println!("{:08x?}", a);
println!("{:08x?}", b);
println!("{:08x?}", c);

// XOR the counter in A[0] and A[1]
a[0] ^= ctr as u32;
a[1] ^= (ctr >> 32) as u32;
println!("xor Counter W into A");
println!("{:08x?}", a);
println!("{:08x?}", b);
println!("{:08x?}", c);

// Apply the keyed permutation
keyed_permutation(&m, &mut a, &mut b, &c);
Expand All @@ -200,26 +164,15 @@ impl ClassicHasher for Shabal256 {
for i in 0..16 {
c[i] = c[i].wrapping_sub(m[i]);
}
println!("subtract M from C");
println!("{:08x?}", a);
println!("{:08x?}", b);
println!("{:08x?}", c);

// Swap B and C
std::mem::swap(&mut b, &mut c);
println!("Swap B and C");
println!("{:08x?}", a);
println!("{:08x?}", b);
println!("{:08x?}", c);
}

println!("\n\nStart Finalization\n\n");
// Finalization rounds
let final_block = make_u32s_le::<16>(input.chunks_exact(64).last().unwrap());
for _ in 0..3 {
let m = final_block;
println!("M");
println!("{:08x?}", m);

for i in 0..16 {
b[i] = b[i].wrapping_add(m[i]);
Expand All @@ -235,11 +188,6 @@ impl ClassicHasher for Shabal256 {
}

std::mem::swap(&mut b, &mut c);

println!("Swap B and C");
println!("{:08x?}", a);
println!("{:08x?}", b);
println!("{:08x?}", c);
}

let mut out = Vec::with_capacity(32);
Expand All @@ -253,13 +201,19 @@ impl ClassicHasher for Shabal256 {
}

crate::basic_hash_tests!(
test1,
Shabal256::default().input(ByteFormat::Hex),
"000100000101000002010000030100000401000005010000060100000701000008010000090100000a0100000b0100000c0100000d0100000e0100000f010000100100001101000012010000130100001401000015010000160100001701000018010000190100001a0100001b0100001c0100001d0100001e0100001f01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"da8f08c02a67ba9a56bdd0798e48ae0714215e093b5b850649a37718993f54a2";
//
// test_a_zeroed_ivs,
// Shabal256::default().input(ByteFormat::Hex),
// "000100000101000002010000030100000401000005010000060100000701000008010000090100000a0100000b0100000c0100000d0100000e0100000f010000100100001101000012010000130100001401000015010000160100001701000018010000190100001a0100001b0100001c0100001d0100001e0100001f01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
// "da8f08c02a67ba9a56bdd0798e48ae0714215e093b5b850649a37718993f54a2";

test2,
test_a,
Shabal256::default().input(ByteFormat::Hex),
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"da8f08c02a67ba9a56bdd0798e48ae0714215e093b5b850649a37718993f54a2";

test_b,
Shabal256::default().input(ByteFormat::Hex),
"6162636465666768696A6B6C6D6E6F707172737475767778797A2D303132333435363738392D4142434445464748494A4B4C4D4E4F505152535455565758595A2D303132333435363738392D6162636465666768696A6B6C6D6E6F707172737475767778797A",
"b49f34bf51864c30533cc46cc2542bdec2f96fd06f5c539aff6ead5883f7327a";
);

0 comments on commit db28ff2

Please sign in to comment.