Merge branch 'dev' into feat_xex

This commit is contained in:
0xalivecow 2024-10-27 17:25:27 +01:00
commit f6fe75b987
No known key found for this signature in database
1777 changed files with 421 additions and 398492 deletions

View file

@ -12,7 +12,7 @@ fn main() -> Result<()> {
let json = fs::read_to_string(path_to_workload).unwrap();
let workload = kauma::utils::parse::parse_json(json)?;
let response = kauma::tasks::task_distrubute(&workload);
let response = kauma::tasks::task_distrubute(&workload)?;
println!("{}", serde_json::to_string(&response)?);
Ok(())

View file

@ -1,11 +1,7 @@
use std::collections::HashMap;
use crate::utils::parse::{Responses, Testcase, Testcases};
use tasks01::{
block2poly::block2poly,
poly2block::{poly2block},
sea128::sea128,
};
use tasks01::{block2poly::block2poly, gfmul::gfmul, poly2block::poly2block, sea128::sea128};
use anyhow::{anyhow, Result};
use serde_json::{json, Value};
@ -29,6 +25,7 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
}
"block2poly" => {
let result: Vec<u8> = block2poly(args)?;
//TODO: Sort Coefficients
let json = json!({"coefficients" : result});
Ok(json)
}
@ -37,20 +34,29 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
let json = json!({"output" : result});
Ok(json)
}
_ => Err(anyhow!("Fatal. No compatible action found")),
"gfmul" => {
let result = gfmul(args)?;
let json = json!({"product" : result});
Ok(json)
}
_ => Err(anyhow!(
"Fatal. No compatible action found. Json data was {:?}. Arguments were; {:?}",
testcase,
args
)),
}
}
pub fn task_distrubute(testcases: &Testcases) -> Responses {
pub fn task_distrubute(testcases: &Testcases) -> Result<Responses> {
let mut responses: HashMap<String, Value> = HashMap::new();
for (id, testcase) in &testcases.testcases {
responses.insert(id.to_owned(), task_deploy(testcase).unwrap());
}
Responses {
Ok(Responses {
responses: responses,
}
})
}
#[cfg(test)]
@ -76,20 +82,22 @@ mod tests {
}
#[test]
fn test_task_distribution() {
fn test_task_distribution() -> Result<()> {
let json = fs::read_to_string("src/test_json/poly2block_example.json").unwrap();
let parsed = parse_json(json).unwrap();
let expected = json!({ "responses": { "b856d760-023d-4b00-bad2-15d2b6da22fe": {"block": "ARIAAAAAAAAAAAAAAAAAgA=="}}});
assert_eq!(
serde_json::to_value(task_distrubute(&parsed)).unwrap(),
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
serde_json::to_value(expected).unwrap()
);
Ok(())
}
#[test]
fn test_task_sea128_task_full() {
fn test_task_sea128_task_full() -> Result<()> {
let json = fs::read_to_string("src/test_json/sea128.json").unwrap();
let parsed = parse_json(json).unwrap();
@ -105,8 +113,25 @@ mod tests {
});
assert_eq!(
serde_json::to_value(task_distrubute(&parsed)).unwrap(),
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
serde_json::to_value(expected).unwrap()
);
Ok(())
}
#[test]
fn test_task_gfmul_full() -> Result<()> {
let json = fs::read_to_string("src/test_json/gfmul_test.json").unwrap();
let parsed = parse_json(json).unwrap();
let expected = json!({ "responses": { "b856d760-023d-4b00-bad2-15d2b6da22fe": {"product": "hSQAAAAAAAAAAAAAAAAAAA=="}}});
assert_eq!(
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
serde_json::to_value(expected).unwrap()
);
Ok(())
}
}

View file

@ -1,4 +1,3 @@
use crate::utils::poly::{b64_2_num, get_coefficients};
use anyhow::Result;
use serde_json::Value;
@ -7,6 +6,7 @@ pub fn block2poly(val: &Value) -> Result<Vec<u8>> {
// Convert JSON data in to a u128
// TODO: Transfer decoding into own function?
let string: String = serde_json::from_value(val["block"].clone())?;
let number = b64_2_num(&string)?;
let coefficients: Vec<u8> = get_coefficients(number);

View file

@ -0,0 +1,72 @@
use anyhow::Result;
use base64::prelude::*;
//use num_bigint::{BigUint, ToBigUint};
use serde_json::Value;
use crate::utils::{
math::ByteArray,
poly::{b64_2_num, coefficient_to_binary},
};
pub const RED_POLY: u128 = 0x87000000_00000000_00000000_00000000;
pub fn gfmul(args: &Value) -> Result<String> {
let mut red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec());
red_poly_bytes.0.push(0x01);
let poly1_text: String = serde_json::from_value(args["a"].clone())?;
let mut poly1: ByteArray = ByteArray(BASE64_STANDARD.decode(poly1_text)?);
poly1.0.push(0x00);
let poly2_text: String = serde_json::from_value(args["b"].clone())?;
let mut poly2: ByteArray = ByteArray(BASE64_STANDARD.decode(poly2_text)?);
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();
} 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);
let mut bytes: [u8; 16] = [0u8; 16];
bytes.copy_from_slice(&result.0);
Ok(BASE64_STANDARD.encode(bytes))
}
#[cfg(test)]
mod tests {
use serde_json::json;
use std::str::FromStr;
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
#[test]
fn gfmul_task01() -> Result<()> {
let args: Value = json!({"a": "ARIAAAAAAAAAAAAAAAAAgA==", "b": "AgAAAAAAAAAAAAAAAAAAAA=="});
let result = gfmul(&args)?;
assert_eq!(
result, "hSQAAAAAAAAAAAAAAAAAAA==",
"Failure. Calulated result was: {}",
result
);
Ok(())
}
}

View file

@ -1,3 +1,4 @@
pub mod block2poly;
pub mod gfmul;
pub mod poly2block;
pub mod sea128;

View file

@ -1,23 +0,0 @@
{
"testcases": {
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
"action": "block2poly",
"arguments": {
"semantic": "xex",
"block": "ARIAAAAAAAAAAAAAAAAAgA=="
}
},
"254eaee7-05fd-4e0d-8292-9b658a852245": {
"action": "poly2block",
"arguments": {
"semantic": "xex",
"coefficients": [
12,
127,
9,
0
]
}
}
}
}

View file

@ -1,10 +0,0 @@
{
"testcases": {
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
"action": "block2poly",
"arguments": {
"semantic": "xex",
"block": "ARIAAAAAAAAAAAAAAAAAgA=="
}
}
}

View file

@ -1,25 +0,0 @@
{
"testcases": {
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
"action": "add_numbers",
"arguments": {
"number1": 123,
"number2": 234
}
},
"254eaee7-05fd-4e0d-8292-9b658a852245": {
"action": "add_numbers",
"arguments": {
"number1": 333,
"number2": 444
}
},
"affbf4fc-4d2a-41e3-afe0-a79e1d174781": {
"action": "subtract_numbers",
"arguments": {
"number1": 999,
"number2": 121212
}
}
}
}

View file

@ -1,16 +0,0 @@
{
"testcases": {
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
"action": "poly2block",
"arguments": {
"semantic": "xex",
"coefficients": [
12,
127,
9,
0
]
}
}
}
}

View file

@ -1,20 +0,0 @@
{
"testcases": {
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
"action": "sea128",
"arguments": {
"mode": "encrypt",
"key": "istDASeincoolerKEYrofg==",
"input": "yv66vvrO263eyviIiDNEVQ=="
}
},
"254eaee7-05fd-4e0d-8292-9b658a852245": {
"action": "sea128",
"arguments": {
"mode": "decrypt",
"key": "istDASeincoolerKEYrofg==",
"input": "D5FDo3iVBoBN9gVi9/MSKQ=="
}
}
}
}

View file

@ -1,4 +1,4 @@
use anyhow::Result;
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()) {
@ -7,3 +7,121 @@ pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> {
Ok(vec2)
}
#[derive(Debug)]
pub struct ByteArray(pub Vec<u8>);
impl ByteArray {
pub fn left_shift(&mut self) -> u8 {
let mut carry = 0u8;
for byte in self.0.iter_mut() {
let new_carry = *byte >> 7;
*byte = (*byte << 1) | carry;
carry = new_carry;
}
carry
}
pub fn right_shift(&mut self) -> u8 {
let mut carry = 0u8;
for byte in self.0.iter_mut().rev() {
let new_carry = *byte & 1;
*byte = (*byte >> 1) | (carry << 7);
carry = new_carry;
}
carry
}
pub fn xor_byte_arrays(&mut self, vec2: &ByteArray) {
self.0
.iter_mut()
.zip(vec2.0.iter())
.for_each(|(x1, x2)| *x1 ^= *x2);
}
pub fn LSB_is_one(&self) -> bool {
(self.0.first().unwrap() & 1) == 1
}
pub fn is_empty(&self) -> bool {
for i in self.0.iter() {
if *i != 0 {
return false;
}
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_byte_array_shift1() {
let mut byte_array: ByteArray = ByteArray(vec![0x00, 0x01]);
let shifted_array: ByteArray = ByteArray(vec![0x00, 0x02]);
byte_array.left_shift();
assert_eq!(byte_array.0, shifted_array.0);
}
#[test]
fn test_byte_array_shift2() {
let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]);
let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x01]);
byte_array.left_shift();
assert_eq!(
byte_array.0, shifted_array.0,
"Failure: Shifted array was: {:?}",
byte_array.0
);
}
#[test]
fn test_byte_array_shift_right() {
let mut byte_array: ByteArray = ByteArray(vec![0x02]);
let shifted_array: ByteArray = ByteArray(vec![0x01]);
byte_array.right_shift();
assert_eq!(
byte_array.0, shifted_array.0,
"Failure: Shifted array was: {:?}",
byte_array.0
);
}
#[test]
fn test_lsb_one() {
let mut byte_array: ByteArray = ByteArray(vec![0x00, 0xFF]);
assert!(!byte_array.LSB_is_one());
let mut byte_array2: ByteArray = ByteArray(vec![0x02, 0xFF]);
assert!(!byte_array2.LSB_is_one());
let mut byte_array3: ByteArray = ByteArray(vec![0xFF, 0x00]);
assert!(byte_array3.LSB_is_one());
}
#[test]
fn test_byte_xor() {
let mut byte_array: ByteArray = ByteArray(vec![0x25, 0x25]);
let byte_array2: ByteArray = ByteArray(vec![0x55, 0x55]);
byte_array.xor_byte_arrays(&byte_array2);
assert_eq!(byte_array.0, vec![0x70, 0x70]);
}
#[test]
fn test_byte_xor2() {
let mut byte_array: ByteArray = ByteArray(vec![0x00, 0x00]);
let byte_array2: ByteArray = ByteArray(vec![0x55, 0x55]);
byte_array.xor_byte_arrays(&byte_array2);
assert_eq!(byte_array.0, vec![0x55, 0x55]);
}
}

View file

@ -1,6 +1,7 @@
use anyhow::Result;
use base64::prelude::*;
use std::{str::FromStr, u128, u8};
use std::{str::FromStr, u128, u8, usize};
pub fn get_alpha_rep(num: u128) -> String {
let powers: Vec<u8> = get_coefficients(num);
@ -35,7 +36,6 @@ pub fn get_coefficients(num: u128) -> Vec<u8> {
for shift in 0..128 {
//println!("{:?}", ((num >> shift) & 1));
if ((num >> shift) & 1) == 1 {
dbg!("Shift success");
powers.push(shift);
}
}
@ -54,6 +54,16 @@ pub fn get_bit_indices_from_byte(byte: u8) -> Vec<u8> {
coefficients
}
pub fn coefficients_to_byte_arr_xex(coeffs: Vec<u8>) -> Vec<u8> {
let mut byte_array: Vec<u8> = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for coeff in coeffs {
let block_num = coeff / 8;
byte_array[usize::from(block_num)] |= (1 << (coeff % 7));
}
byte_array
}
pub fn coefficient_to_binary(coefficients: Vec<u8>) -> u128 {
let mut binary_number: u128 = 0;
for coeff in coefficients {
@ -70,6 +80,26 @@ mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
/*
* TODO: Consider removing
#[test]
fn coefficients_to_byte_arr_xex_test1() {
let coefficients: Vec<u8> = vec![0];
let byte_array = vec![
01, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
];
assert_eq!(coefficients_to_byte_arr_xex(coefficients), byte_array)
}
#[test]
fn coefficients_to_byte_arr_xex_test2() {
let coefficients: Vec<u8> = vec![127, 12, 9, 0];
let byte_array = vec![
01, 12, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 80,
];
assert_eq!(coefficients_to_byte_arr_xex(coefficients), byte_array)
}
*/
#[test]
fn byte_indices_0x01() {
let byte: u8 = 0x01;