chore: Merge newest dev to get most recent changes

This commit is contained in:
0xalivecow 2024-10-23 10:54:50 +02:00
commit a6571e43af
No known key found for this signature in database
1769 changed files with 398681 additions and 20 deletions

View file

@ -1,17 +1,13 @@
use std::{
collections::HashMap,
fmt::format,
io::{self, Error, ErrorKind},
};
use std::collections::HashMap;
use crate::utils::parse::{Responses, Testcase, Testcases};
use tasks01::{
block2poly::block2poly,
poly2block::{self, poly2block},
poly2block::{poly2block},
sea128::sea128,
};
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
mod tasks01;
@ -37,6 +33,11 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
let json = json!({"coefficients" : result});
Ok(json)
}
"sea128" => {
let result = sea128(args)?;
let json = json!({"output" : result});
Ok(json)
}
_ => Err(anyhow!("Fatal. No compatible action found")),
}
}
@ -87,4 +88,26 @@ mod tests {
serde_json::to_value(expected).unwrap()
);
}
#[test]
fn test_task_sea128_task_full() {
let json = fs::read_to_string("src/test_json/sea128.json").unwrap();
let parsed = parse_json(json).unwrap();
let expected = json!({
"responses": {
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
"output": "D5FDo3iVBoBN9gVi9/MSKQ=="
},
"254eaee7-05fd-4e0d-8292-9b658a852245": {
"output": "yv66vvrO263eyviIiDNEVQ=="
}
}
});
assert_eq!(
serde_json::to_value(task_distrubute(&parsed)).unwrap(),
serde_json::to_value(expected).unwrap()
);
}
}

View file

@ -1,15 +1,13 @@
use std::{str::Bytes, string};
use crate::utils::poly::{self, block_2_number, get_coefficients};
use crate::utils::poly::{b64_2_num, get_coefficients};
use anyhow::Result;
use base64::prelude::*;
use serde_json::Value;
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: u128 = block_2_number(string)?;
let number = b64_2_num(&string)?;
let coefficients: Vec<u8> = get_coefficients(number);

View file

@ -2,7 +2,7 @@ use anyhow::Result;
use base64::prelude::*;
use serde_json::Value;
use crate::utils::poly::{block_2_number, coefficient_to_binary};
use crate::utils::poly::{b64_2_num, coefficient_to_binary};
pub fn gfmul(args: &Value) -> Result<String> {
eprintln!("{args}");
@ -11,8 +11,8 @@ pub fn gfmul(args: &Value) -> Result<String> {
let red_poly_num: u128 = coefficient_to_binary(reduction_polynomial_coeffs);
//eprintln!("{:?}", serde_json::from_value(args["a"].clone())?);
let mut poly1: u128 = block_2_number(serde_json::from_value(args["a"].clone())?)?;
let poly2: u128 = block_2_number(serde_json::from_value(args["b"].clone())?)?;
let mut poly1: u128 = b64_2_num(&serde_json::from_value(args["a"].clone())?)?;
let poly2: u128 = b64_2_num(&serde_json::from_value(args["b"].clone())?)?;
eprintln!("poly1 is: {}", poly1);
eprintln!("poly2 is: {}", poly2);
/* Begin of magic algorithm

View file

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

View file

@ -1,4 +1,4 @@
use crate::utils::poly::{self, coefficient_to_binary};
use crate::utils::poly::{self};
use base64::prelude::*;
use serde_json::Value;

View file

@ -0,0 +1,61 @@
use anyhow::{anyhow, Result};
use base64::prelude::*;
use serde_json::Value;
use crate::utils::ciphers::{sea_128_decrypt, sea_128_encrypt};
pub fn sea128(args: &Value) -> Result<String> {
let key_string: String = serde_json::from_value(args["key"].clone())?;
//let key: &[u8] = b64_2_num(key_string)?.to_ne_bytes();
let key = BASE64_STANDARD.decode(key_string)?;
//eprintln!("{:?}", key);
let input_string: String = serde_json::from_value(args["input"].clone())?;
//let plaintexts: &[u8] = &b64_2_num(plaintexts_string)?.to_ne_bytes();
let input = BASE64_STANDARD.decode(input_string)?;
let xor_val: u128 = 0xc0ffeec0ffeec0ffeec0ffeec0ffee11;
let mode: String = serde_json::from_value(args["mode"].clone())?;
match mode.as_str() {
"encrypt" => {
//eprintln!("{:?}", plaintexts);
let output = BASE64_STANDARD.encode(sea_128_encrypt(&key, &input)?);
Ok(output)
}
"decrypt" => {
let output = BASE64_STANDARD.encode(sea_128_decrypt(&key, &input)?);
Ok(output)
}
_ => Err(anyhow!("Failure. no valid mode detected")),
}
}
#[cfg(test)]
mod tests {
use std::fs;
use anyhow::Result;
use serde_json::json;
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
#[test]
fn test_sea128_encrypt() -> Result<()> {
let args = json!({"mode" : "encrypt", "key" : "istDASeincoolerKEYrofg==", "input" : "yv66vvrO263eyviIiDNEVQ=="});
assert_eq!(sea128(&args)?, "D5FDo3iVBoBN9gVi9/MSKQ==");
Ok(())
}
#[test]
fn test_sea128_decrypt() -> Result<()> {
let args = json!({"mode" : "decrypt", "key" : "istDASeincoolerKEYrofg==", "input" : "D5FDo3iVBoBN9gVi9/MSKQ=="});
assert_eq!(sea128(&args)?, "yv66vvrO263eyviIiDNEVQ==");
Ok(())
}
}