WIP: feat: working on gfmul gcm

This commit is contained in:
0xalivecow 2024-11-02 16:12:51 +01:00
parent 1ce30e1cea
commit 1e51015a14
No known key found for this signature in database
5 changed files with 86 additions and 42 deletions

View file

@ -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};

View file

@ -22,6 +22,7 @@ pub fn gfmul_task(args: &Value) -> Result<Vec<u8>> {
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(())
}
}

View file

@ -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<u8> = base64::prelude::BASE64_STANDARD
.decode("AgAAAAAAAAAAAAAAAAAAAA==")
.expect("Decode failed");
self.0 = gfmul(self.0.clone(), alpha_poly, "xex").unwrap();
}
"gcm" => {
let alpha_poly: Vec<u8> = 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<u8> {
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<u8> = base64::prelude::BASE64_STANDARD
.decode("AgAAAAAAAAAAAAAAAAAAAA==")
.expect("Decode failed");
self.0 = gfmul(self.0.clone(), alpha_poly, "xex").unwrap();
}
"gcm" => {
let alpha_poly: Vec<u8> = 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,13 +96,16 @@ 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() {
let mut byte_array: ByteArray = ByteArray(vec![0x00, 0x01]);
@ -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]

View file

@ -1,6 +1,5 @@
use anyhow::{Ok, Result};
pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> {
for (byte1, byte2) in vec1.iter().zip(vec2.iter_mut()) {
*byte2 ^= byte1;
@ -8,3 +7,9 @@ pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> {
Ok(vec2)
}
pub fn reverse_bits_in_bytevec(mut vec: Vec<u8>) -> Vec<u8> {
vec = vec.iter_mut().map(|byte| byte.reverse_bits()).collect();
vec
}

View file

@ -9,27 +9,44 @@ pub fn gfmul(poly_a: Vec<u8>, poly_b: Vec<u8>, semantic: &str) -> Result<Vec<u8>
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<u8>, semantic: &str) -> Result<Vec<u8>> {
let mut output: Vec<u8> = 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<u8>, semantic: &str) -> Result<Vec<u8>> {
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);
}
}
}