Dev #4

Merged
0xalivecow merged 4 commits from dev into main 2024-10-30 17:02:16 +00:00
5 changed files with 225 additions and 76 deletions

View file

@ -5,7 +5,7 @@ use std::collections::HashMap;
use crate::utils::parse::{Responses, Testcase, Testcases}; use crate::utils::parse::{Responses, Testcase, Testcases};
use tasks01::{ use tasks01::{
block2poly::block2poly, block2poly::block2poly,
gfmul::gfmul, gfmul::gfmul_task,
poly2block::poly2block, poly2block::poly2block,
sea128::sea128, sea128::sea128,
xex::{self, fde_xex}, xex::{self, fde_xex},
@ -43,13 +43,7 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
Ok(json) Ok(json)
} }
"gfmul" => { "gfmul" => {
let poly1_text: String = serde_json::from_value(args["a"].clone())?; let result = BASE64_STANDARD.encode(gfmul_task(args)?);
let poly_a = BASE64_STANDARD.decode(poly1_text)?;
let poly2_text: String = serde_json::from_value(args["b"].clone())?;
let poly_b = BASE64_STANDARD.decode(poly2_text)?;
let result = BASE64_STANDARD.encode(gfmul(poly_a, poly_b)?);
let json = json!({"product" : result}); let json = json!({"product" : result});
Ok(json) Ok(json)
} }

View file

@ -1,49 +1,24 @@
use anyhow::Result;
use base64::prelude::*;
//use num_bigint::{BigUint, ToBigUint};
use serde_json::Value;
use crate::utils::{ use crate::utils::{
math::ByteArray, math::ByteArray,
poly::{b64_2_num, coefficient_to_binary}, poly::{b64_2_num, coefficient_to_binary, gfmul},
}; };
pub const RED_POLY: u128 = 0x87000000_00000000_00000000_00000000; use anyhow::Result;
use base64::prelude::*;
use serde_json::Value;
pub fn gfmul(poly_a: Vec<u8>, poly_b: Vec<u8>) -> Result<Vec<u8>> { pub fn gfmul_task(args: &Value) -> Result<Vec<u8>> {
let mut red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec()); let poly1_text: String = serde_json::from_value(args["a"].clone())?;
red_poly_bytes.0.push(0x01); let poly_a = BASE64_STANDARD.decode(poly1_text)?;
let mut poly1: ByteArray = ByteArray(poly_a); let poly2_text: String = serde_json::from_value(args["b"].clone())?;
poly1.0.push(0x00); let poly_b = BASE64_STANDARD.decode(poly2_text)?;
let mut poly2: ByteArray = ByteArray(poly_b); let semantic: String = serde_json::from_value(args["semantic"].clone())?;
poly2.0.push(0x00);
let mut result: ByteArray = ByteArray(vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); let result = gfmul(poly_a, poly_b, &semantic)?;
if poly2.LSB_is_one() { Ok(result)
result.xor_byte_arrays(&poly1);
poly2.right_shift();
} else {
poly2.right_shift();
}
while !poly2.is_empty() {
if poly2.LSB_is_one() {
poly1.left_shift();
poly1.xor_byte_arrays(&red_poly_bytes);
result.xor_byte_arrays(&poly1);
} else {
poly1.left_shift();
poly1.xor_byte_arrays(&red_poly_bytes);
}
poly2.right_shift();
}
result.0.remove(16);
Ok(result.0)
} }
#[cfg(test)] #[cfg(test)]
@ -64,7 +39,67 @@ mod tests {
let poly2_text: String = serde_json::from_value(args["b"].clone())?; let poly2_text: String = serde_json::from_value(args["b"].clone())?;
let poly_b = BASE64_STANDARD.decode(poly2_text)?; let poly_b = BASE64_STANDARD.decode(poly2_text)?;
let result = BASE64_STANDARD.encode(gfmul(poly_a, poly_b)?); let result = BASE64_STANDARD.encode(gfmul(poly_a, poly_b, "xex")?);
assert_eq!(
result, "hSQAAAAAAAAAAAAAAAAAAA==",
"Failure. Calulated result was: {}",
result
);
Ok(())
}
#[test]
fn gfmul_task02() -> Result<()> {
let args: Value = json!({"a": "AwEAAAAAAAAAAAAAAAAAgA==", "b": "gBAAAAAAAAAAAAAAAAAAAA=="});
let poly1_text: String = serde_json::from_value(args["a"].clone())?;
let poly_a = BASE64_STANDARD.decode(poly1_text)?;
let poly2_text: String = serde_json::from_value(args["b"].clone())?;
let poly_b = BASE64_STANDARD.decode(poly2_text)?;
let result = BASE64_STANDARD.encode(gfmul(poly_a, poly_b, "xex")?);
assert_eq!(
result, "QKgUAAAAAAAAAAAAAAAAAA==",
"Failure. Calulated result was: {}",
result
);
Ok(())
}
#[test]
fn gfmul_task03() -> Result<()> {
let args: Value = json!({"a": "AwEAAAAAAAAAAAAAAAAAgA==", "b": "oBAAAAAAAAAAAAAAAAAAAA=="});
let poly1_text: String = serde_json::from_value(args["a"].clone())?;
let poly_a = BASE64_STANDARD.decode(poly1_text)?;
let poly2_text: String = serde_json::from_value(args["b"].clone())?;
let poly_b = BASE64_STANDARD.decode(poly2_text)?;
let result = BASE64_STANDARD.encode(gfmul(poly_a, poly_b, "xex")?);
assert_eq!(
result, "UIAUAAAAAAAAAAAAAAAAAA==",
"Failure. Calulated result was: {}",
result
);
Ok(())
}
#[test]
fn gfmul_task04() -> Result<()> {
let args: Value = json!({"a": "ARIAAAAAAAAAAAAAAAAAgA==", "b": "AgAAAAAAAAAAAAAAAAAAAA=="});
let poly1_text: String = serde_json::from_value(args["a"].clone())?;
let poly_a = BASE64_STANDARD.decode(poly1_text)?;
let poly2_text: String = serde_json::from_value(args["b"].clone())?;
let poly_b = BASE64_STANDARD.decode(poly2_text)?;
let result = BASE64_STANDARD.encode(gfmul(poly_a, poly_b, "xex")?);
assert_eq!( assert_eq!(
result, "hSQAAAAAAAAAAAAAAAAAAA==", result, "hSQAAAAAAAAAAAAAAAAAAA==",

View file

@ -83,7 +83,7 @@ pub fn xex_encrypt(mut key: Vec<u8>, tweak: &Vec<u8>, input: &Vec<u8>) -> Result
let cypher_block_intermediate = sea_128_encrypt(&key, &plaintext_intermediate)?; let cypher_block_intermediate = sea_128_encrypt(&key, &plaintext_intermediate)?;
let mut cypher_block = xor_bytes(&tweak_block.0, cypher_block_intermediate)?; let mut cypher_block = xor_bytes(&tweak_block.0, cypher_block_intermediate)?;
output.append(cypher_block.as_mut()); output.append(cypher_block.as_mut());
tweak_block.left_shift_reduce(); tweak_block.left_shift_reduce("xex");
} }
Ok(output) Ok(output)
@ -116,7 +116,7 @@ pub fn xex_decrypt(mut key: Vec<u8>, tweak: &Vec<u8>, input: &Vec<u8>) -> Result
let plaintext_block_intermediate = sea_128_decrypt(&key, &cyphertext_intermediate)?; let plaintext_block_intermediate = sea_128_decrypt(&key, &cyphertext_intermediate)?;
let mut cypher_block = xor_bytes(&tweak_block.0, plaintext_block_intermediate)?; let mut cypher_block = xor_bytes(&tweak_block.0, plaintext_block_intermediate)?;
output.append(cypher_block.as_mut()); output.append(cypher_block.as_mut());
tweak_block.left_shift_reduce(); tweak_block.left_shift_reduce("xex");
} }
Ok(output) Ok(output)
@ -167,4 +167,17 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn test_xex_encrypt_empty_case() -> Result<()> {
let key = BASE64_STANDARD.decode("B1ygNO/CyRYIUYhTSgoUysX5Y/wWLi4UiWaVeloUWs0=")?;
let tweak = BASE64_STANDARD.decode("6VXORr+YYHrd2nVe0OlA+Q==")?;
let input = BASE64_STANDARD.decode("")?;
let output = BASE64_STANDARD.encode(xex_encrypt(key, &tweak, &input)?);
assert_eq!(output, "");
Ok(())
}
} }

View file

@ -1,7 +1,7 @@
use anyhow::{Ok, Result}; use anyhow::{anyhow, Ok, Result};
use base64::Engine; use base64::Engine;
use crate::tasks::tasks01::gfmul::gfmul; use super::poly::gfmul;
pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> { pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> {
for (byte1, byte2) in vec1.iter().zip(vec2.iter_mut()) { for (byte1, byte2) in vec1.iter().zip(vec2.iter_mut()) {
@ -15,31 +15,70 @@ pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> {
pub struct ByteArray(pub Vec<u8>); pub struct ByteArray(pub Vec<u8>);
impl ByteArray { impl ByteArray {
pub fn left_shift(&mut self) -> u8 { pub fn left_shift(&mut self, semantic: &str) -> Result<u8> {
let mut carry = 0u8; match semantic {
for byte in self.0.iter_mut() { "xex" => {
let new_carry = *byte >> 7; let mut carry = 0u8;
*byte = (*byte << 1) | carry; for byte in self.0.iter_mut() {
carry = new_carry; let new_carry = *byte >> 7;
*byte = (*byte << 1) | carry;
carry = new_carry;
}
Ok(carry)
}
"gcm" => {
let mut carry = 0u8;
for byte in self.0.iter_mut() {
let new_carry = *byte & 1;
*byte = (*byte >> 1) | (carry << 7);
carry = new_carry;
}
Ok(carry)
}
_ => Err(anyhow!("Failure in lsh. No compatible action found")),
} }
carry
} }
pub fn left_shift_reduce(&mut self) { pub fn left_shift_reduce(&mut self, semantic: &str) {
let alpha_poly: Vec<u8> = base64::prelude::BASE64_STANDARD match semantic {
.decode("AgAAAAAAAAAAAAAAAAAAAA==") "xex" => {
.expect("Decode failed"); let alpha_poly: Vec<u8> = base64::prelude::BASE64_STANDARD
self.0 = gfmul(self.0.clone(), alpha_poly).unwrap(); .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) -> u8 { pub fn right_shift(&mut self, semantic: &str) -> Result<u8> {
let mut carry = 0u8; match semantic {
for byte in self.0.iter_mut().rev() { "xex" => {
let new_carry = *byte & 1; let mut carry = 0u8;
*byte = (*byte >> 1) | (carry << 7); for byte in self.0.iter_mut().rev() {
carry = new_carry; let new_carry = *byte & 1;
*byte = (*byte >> 1) | (carry << 7);
carry = new_carry;
}
Ok(carry)
}
"gcm" => {
let mut carry = 0u8;
for byte in self.0.iter_mut().rev() {
let new_carry = *byte & 1;
*byte = (*byte << 1) | carry;
carry = new_carry;
}
Ok(carry)
}
_ => Err(anyhow!("Failure in rsh. No valid semantic found")),
} }
carry
} }
pub fn xor_byte_arrays(&mut self, vec2: &ByteArray) { pub fn xor_byte_arrays(&mut self, vec2: &ByteArray) {
@ -53,6 +92,10 @@ impl ByteArray {
(self.0.first().unwrap() & 1) == 1 (self.0.first().unwrap() & 1) == 1
} }
pub fn msb_is_one(&self) -> bool {
(self.0.last().unwrap() & 1) == 1
}
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
for i in self.0.iter() { for i in self.0.iter() {
if *i != 0 { if *i != 0 {
@ -72,7 +115,7 @@ mod tests {
fn test_byte_array_shift1() { fn test_byte_array_shift1() {
let mut byte_array: ByteArray = ByteArray(vec![0x00, 0x01]); let mut byte_array: ByteArray = ByteArray(vec![0x00, 0x01]);
let shifted_array: ByteArray = ByteArray(vec![0x00, 0x02]); let shifted_array: ByteArray = ByteArray(vec![0x00, 0x02]);
byte_array.left_shift(); byte_array.left_shift("xex");
assert_eq!(byte_array.0, shifted_array.0); assert_eq!(byte_array.0, shifted_array.0);
} }
@ -81,7 +124,7 @@ mod tests {
fn test_byte_array_shift2() { fn test_byte_array_shift2() {
let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]); let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]);
let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x01]); let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x01]);
byte_array.left_shift(); byte_array.left_shift("xex");
assert_eq!( assert_eq!(
byte_array.0, shifted_array.0, byte_array.0, shifted_array.0,
@ -90,11 +133,37 @@ mod tests {
); );
} }
#[test]
fn test_byte_array_shift1_gcm() {
let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]);
let shifted_array: ByteArray = ByteArray(vec![0x7F, 0x80]);
byte_array.left_shift("gcm");
assert_eq!(
byte_array.0, shifted_array.0,
"Failure: Shifted array was: {:02X?}",
byte_array.0
);
}
#[test]
fn test_byte_array_shift1_right_gcm() {
let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]);
let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x00]);
byte_array.right_shift("gcm");
assert_eq!(
byte_array.0, shifted_array.0,
"Failure: Shifted array was: {:02X?}",
byte_array.0
);
}
#[test] #[test]
fn test_byte_array_shift_right() { fn test_byte_array_shift_right() {
let mut byte_array: ByteArray = ByteArray(vec![0x02]); let mut byte_array: ByteArray = ByteArray(vec![0x02]);
let shifted_array: ByteArray = ByteArray(vec![0x01]); let shifted_array: ByteArray = ByteArray(vec![0x01]);
byte_array.right_shift(); byte_array.right_shift("xex");
assert_eq!( assert_eq!(
byte_array.0, shifted_array.0, byte_array.0, shifted_array.0,
@ -105,13 +174,13 @@ mod tests {
#[test] #[test]
fn test_lsb_one() { fn test_lsb_one() {
let mut byte_array: ByteArray = ByteArray(vec![0x00, 0xFF]); let byte_array: ByteArray = ByteArray(vec![0x00, 0xFF]);
assert!(!byte_array.LSB_is_one()); assert!(!byte_array.LSB_is_one());
let mut byte_array2: ByteArray = ByteArray(vec![0x02, 0xFF]); let byte_array2: ByteArray = ByteArray(vec![0x02, 0xFF]);
assert!(!byte_array2.LSB_is_one()); assert!(!byte_array2.LSB_is_one());
let mut byte_array3: ByteArray = ByteArray(vec![0xFF, 0x00]); let byte_array3: ByteArray = ByteArray(vec![0xFF, 0x00]);
assert!(byte_array3.LSB_is_one()); assert!(byte_array3.LSB_is_one());
} }

View file

@ -1,7 +1,45 @@
use crate::utils::math::ByteArray;
use anyhow::Result; use anyhow::Result;
use base64::prelude::*; use base64::prelude::*;
use serde_json::Value;
use std::{str::FromStr, u128, u8, usize}; use std::{str::FromStr, u128, u8, usize};
pub const RED_POLY: u128 = 0x87000000_00000000_00000000_00000000;
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);
let mut poly1: ByteArray = ByteArray(poly_a);
poly1.0.push(0x00);
let mut poly2: ByteArray = ByteArray(poly_b);
poly2.0.push(0x00);
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() {
result.xor_byte_arrays(&poly1);
}
poly2.right_shift(semantic)?;
while !poly2.is_empty() {
poly1.left_shift(semantic)?;
if poly1.msb_is_one() {
poly1.xor_byte_arrays(&red_poly_bytes);
}
if poly2.LSB_is_one() {
result.xor_byte_arrays(&poly1);
}
poly2.right_shift(semantic)?;
}
result.0.remove(16);
Ok(result.0)
}
pub fn get_alpha_rep(num: u128) -> String { pub fn get_alpha_rep(num: u128) -> String {
let powers: Vec<u8> = get_coefficients(num); let powers: Vec<u8> = get_coefficients(num);