From 1e51015a1416d9caaa030e236abc3d971de58e1c Mon Sep 17 00:00:00 2001 From: 0xalivecow Date: Sat, 2 Nov 2024 16:12:51 +0100 Subject: [PATCH] WIP: feat: working on gfmul gcm --- src/tasks/mod.rs | 6 +---- src/tasks/tasks01/gfmul.rs | 25 ++++++++++++++++++- src/utils/field.rs | 51 ++++++++++++++++++++------------------ src/utils/math.rs | 7 +++++- src/utils/poly.rs | 39 +++++++++++++++++++++-------- 5 files changed, 86 insertions(+), 42 deletions(-) diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index f0adb70..f053cfc 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -4,11 +4,7 @@ use std::collections::HashMap; use crate::utils::parse::{Responses, Testcase, Testcases}; use tasks01::{ - block2poly::block2poly, - gfmul::gfmul_task, - poly2block::poly2block, - sea128::sea128, - xex::{fde_xex}, + block2poly::block2poly, gfmul::gfmul_task, poly2block::poly2block, sea128::sea128, xex::fde_xex, }; use anyhow::{anyhow, Result}; diff --git a/src/tasks/tasks01/gfmul.rs b/src/tasks/tasks01/gfmul.rs index b398b6e..95d0a4c 100644 --- a/src/tasks/tasks01/gfmul.rs +++ b/src/tasks/tasks01/gfmul.rs @@ -21,7 +21,8 @@ pub fn gfmul_task(args: &Value) -> Result> { #[cfg(test)] mod tests { use serde_json::json; - + + use crate::utils::math::reverse_bits_in_bytevec; // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; @@ -105,4 +106,26 @@ mod tests { ); Ok(()) } + + #[test] + fn gfmul_task_gcm01() -> Result<()> { + let args: Value = json!({"a": "ARIAAAAAAAAAAAAAAAAAgA==", "b": "AgAAAAAAAAAAAAAAAAAAAA=="}); + + let poly1_text: String = serde_json::from_value(args["a"].clone())?; + let poly_a = reverse_bits_in_bytevec(BASE64_STANDARD.decode(poly1_text)?); + + let poly2_text: String = serde_json::from_value(args["b"].clone())?; + let poly_b = reverse_bits_in_bytevec(BASE64_STANDARD.decode(poly2_text)?); + let result = BASE64_STANDARD.encode(gfmul(poly_a, poly_b, "gcm")?); + + assert_eq!( + result, + BASE64_STANDARD.encode(reverse_bits_in_bytevec( + BASE64_STANDARD.decode("hSQAAAAAAAAAAAAAAAAAAA==")? + )), + "Failure. Calulated result was: {}", + result + ); + Ok(()) + } } diff --git a/src/utils/field.rs b/src/utils/field.rs index f2c80b4..2670b2d 100644 --- a/src/utils/field.rs +++ b/src/utils/field.rs @@ -22,7 +22,7 @@ impl ByteArray { let mut carry = 0u8; for byte in self.0.iter_mut() { let new_carry = *byte & 1; - *byte = (*byte >> 1) | (carry << 7); + *byte = (*byte >> 1) | carry << 7; carry = new_carry; } Ok(carry) @@ -31,24 +31,6 @@ impl ByteArray { } } - pub fn left_shift_reduce(&mut self, semantic: &str) { - match semantic { - "xex" => { - let alpha_poly: Vec = base64::prelude::BASE64_STANDARD - .decode("AgAAAAAAAAAAAAAAAAAAAA==") - .expect("Decode failed"); - self.0 = gfmul(self.0.clone(), alpha_poly, "xex").unwrap(); - } - "gcm" => { - let alpha_poly: Vec = base64::prelude::BASE64_STANDARD - .decode("AgAAAAAAAAAAAAAAAAAAAA==") - .expect("Decode failed"); - self.0 = gfmul(self.0.clone(), alpha_poly, "gcm").unwrap(); - } - _ => {} - } - } - pub fn right_shift(&mut self, semantic: &str) -> Result { match semantic { "xex" => { @@ -73,6 +55,24 @@ impl ByteArray { } } + pub fn left_shift_reduce(&mut self, semantic: &str) { + match semantic { + "xex" => { + let alpha_poly: Vec = base64::prelude::BASE64_STANDARD + .decode("AgAAAAAAAAAAAAAAAAAAAA==") + .expect("Decode failed"); + self.0 = gfmul(self.0.clone(), alpha_poly, "xex").unwrap(); + } + "gcm" => { + let alpha_poly: Vec = base64::prelude::BASE64_STANDARD + .decode("AgAAAAAAAAAAAAAAAAAAAA==") + .expect("Decode failed"); + self.0 = gfmul(self.0.clone(), alpha_poly, "gcm").unwrap(); + } + _ => {} + } + } + pub fn xor_byte_arrays(&mut self, vec2: &ByteArray) { self.0 .iter_mut() @@ -80,7 +80,7 @@ impl ByteArray { .for_each(|(x1, x2)| *x1 ^= *x2); } - pub fn LSB_is_one(&self) -> bool { + pub fn lsb_is_one(&self) -> bool { (self.0.first().unwrap() & 1) == 1 } @@ -96,12 +96,15 @@ impl ByteArray { } true } + + pub fn reverse_bits_in_bytevec(&mut self) { + self.0 = self.0.iter_mut().map(|byte| byte.reverse_bits()).collect(); + } } #[cfg(test)] mod tests { use super::*; - #[test] fn test_byte_array_shift1() { @@ -167,13 +170,13 @@ mod tests { #[test] fn test_lsb_one() { let byte_array: ByteArray = ByteArray(vec![0x00, 0xFF]); - assert!(!byte_array.LSB_is_one()); + assert!(!byte_array.lsb_is_one()); let byte_array2: ByteArray = ByteArray(vec![0x02, 0xFF]); - assert!(!byte_array2.LSB_is_one()); + assert!(!byte_array2.lsb_is_one()); let byte_array3: ByteArray = ByteArray(vec![0xFF, 0x00]); - assert!(byte_array3.LSB_is_one()); + assert!(byte_array3.lsb_is_one()); } #[test] diff --git a/src/utils/math.rs b/src/utils/math.rs index fa82819..df3fe7c 100644 --- a/src/utils/math.rs +++ b/src/utils/math.rs @@ -1,6 +1,5 @@ use anyhow::{Ok, Result}; - pub fn xor_bytes(vec1: &Vec, mut vec2: Vec) -> Result> { for (byte1, byte2) in vec1.iter().zip(vec2.iter_mut()) { *byte2 ^= byte1; @@ -8,3 +7,9 @@ pub fn xor_bytes(vec1: &Vec, mut vec2: Vec) -> Result> { Ok(vec2) } + +pub fn reverse_bits_in_bytevec(mut vec: Vec) -> Vec { + vec = vec.iter_mut().map(|byte| byte.reverse_bits()).collect(); + + vec +} diff --git a/src/utils/poly.rs b/src/utils/poly.rs index 4b984ea..8f525bf 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -9,27 +9,44 @@ pub fn gfmul(poly_a: Vec, poly_b: Vec, semantic: &str) -> Result let mut red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec()); red_poly_bytes.0.push(0x01); + red_poly_bytes.reverse_bits_in_bytevec(); + let mut poly1: ByteArray = ByteArray(poly_a); poly1.0.push(0x00); let mut poly2: ByteArray = ByteArray(poly_b); poly2.0.push(0x00); + eprintln!( + "poly1 is: {:001X?} \n poly2 is: {:001X?} \n gen poly is: {:001X?} \n", + poly1, poly2, red_poly_bytes + ); + let mut result: ByteArray = ByteArray(vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); - if poly2.LSB_is_one() { + if poly2.msb_is_one() { result.xor_byte_arrays(&poly1); } poly2.right_shift(semantic)?; + eprintln!( + "poly1 is: {:001X?} \n poly2 is: {:001X?} \n gen poly is: {:001X?} \n result is: {:001X?} \n ", + poly1, poly2, red_poly_bytes, result + ); + while !poly2.is_empty() { + eprintln!( + "poly1 is: {:001X?} \n poly2 is: {:001X?} \n gen poly is: {:001X?} \n result is: {:001X?} \n ", + poly1, poly2, red_poly_bytes, result + ); poly1.left_shift(semantic)?; - if poly1.msb_is_one() { + if poly1.lsb_is_one() { poly1.xor_byte_arrays(&red_poly_bytes); } - if poly2.LSB_is_one() { + if poly2.msb_is_one() { + eprintln!("poly write to result"); result.xor_byte_arrays(&poly1); } @@ -108,10 +125,10 @@ pub fn block_2_polynomial(block: Vec, semantic: &str) -> Result> { let mut output: Vec = vec![]; match semantic { "xex" => { - for i in 0u8..=15 { - for j in 0u8..=7 { - if (block[i as usize] >> j) & 1 == 1 { - output.push(8 * i + j); + for byte in 0u8..=15 { + for bit in 0u8..=7 { + if (block[byte as usize] >> bit) & 1 == 1 { + output.push(8 * byte + bit); } } } @@ -119,10 +136,10 @@ pub fn block_2_polynomial(block: Vec, semantic: &str) -> Result> { Ok(output) } "gcm" => { - for i in 0u8..=15 { - for j in 0u8..=7 { - if (block[i as usize] >> j) & 1 == 1 { - output.push(8 * i + 7 - j); + for byte in 0u8..=15 { + for bit in 0u8..=7 { + if (block[byte as usize] >> bit) & 1 == 1 { + output.push(8 * byte + 7 - bit); } } }