chore: Merge newest dev to get most recent changes
This commit is contained in:
commit
a6571e43af
1769 changed files with 398681 additions and 20 deletions
|
|
@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod block2poly;
|
||||
pub mod gfmul;
|
||||
pub mod poly2block;
|
||||
pub mod sea128;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::utils::poly::{self, coefficient_to_binary};
|
||||
use crate::utils::poly::{self};
|
||||
use base64::prelude::*;
|
||||
use serde_json::Value;
|
||||
|
||||
|
|
|
|||
61
src/tasks/tasks01/sea128.rs
Normal file
61
src/tasks/tasks01/sea128.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
20
src/test_json/sea128.json
Normal file
20
src/test_json/sea128.json
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"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=="
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
59
src/utils/ciphers.rs
Normal file
59
src/utils/ciphers.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use anyhow::Result;
|
||||
use openssl::symm::{Cipher, Crypter, Mode};
|
||||
|
||||
use super::math::xor_bytes;
|
||||
|
||||
pub fn aes_128_encrypt(key: &Vec<u8>, input: &Vec<u8>) -> Result<Vec<u8>> {
|
||||
let mut encrypter = Crypter::new(Cipher::aes_128_ecb(), Mode::Encrypt, &key, None)?;
|
||||
encrypter.pad(false);
|
||||
|
||||
let mut ciphertext = [0; 32].to_vec();
|
||||
|
||||
let mut count = encrypter.update(input, &mut ciphertext)?;
|
||||
count += encrypter.finalize(&mut ciphertext)?;
|
||||
ciphertext.truncate(count);
|
||||
|
||||
//eprintln!("{:?}", &ciphertext[..]);
|
||||
|
||||
Ok(ciphertext)
|
||||
}
|
||||
|
||||
pub fn aes_128_decrypt(key: &Vec<u8>, input: &Vec<u8>) -> Result<Vec<u8>> {
|
||||
let mut decrypter = Crypter::new(Cipher::aes_128_ecb(), Mode::Decrypt, key, None)?;
|
||||
decrypter.pad(false);
|
||||
|
||||
let mut plaintext = [0; 32].to_vec();
|
||||
|
||||
let mut count = decrypter.update(input, &mut plaintext)?;
|
||||
count += decrypter.finalize(&mut plaintext)?;
|
||||
plaintext.truncate(count);
|
||||
|
||||
let mut bytes: [u8; 16] = [0u8; 16];
|
||||
bytes.copy_from_slice(&plaintext);
|
||||
let number: u128 = <u128>::from_be_bytes(bytes);
|
||||
|
||||
Ok(plaintext)
|
||||
}
|
||||
|
||||
pub fn sea_128_encrypt(key: &Vec<u8>, input: &Vec<u8>) -> Result<Vec<u8>> {
|
||||
let xor_val: u128 = 0xc0ffeec0ffeec0ffeec0ffeec0ffee11;
|
||||
let sea128_out = xor_bytes(
|
||||
&aes_128_encrypt(key, input)?,
|
||||
xor_val.to_be_bytes().to_vec(),
|
||||
)?;
|
||||
Ok(sea128_out)
|
||||
}
|
||||
|
||||
pub fn sea_128_decrypt(key: &Vec<u8>, input: &Vec<u8>) -> Result<Vec<u8>> {
|
||||
let xor_val: u128 = 0xc0ffeec0ffeec0ffeec0ffeec0ffee11;
|
||||
|
||||
let intermediate = xor_bytes(input, xor_val.to_be_bytes().to_vec())?;
|
||||
Ok(aes_128_decrypt(&key, &intermediate)?)
|
||||
}
|
||||
|
||||
/*
|
||||
* let mut bytes: [u8; 16] = [0u8; 16];
|
||||
bytes.copy_from_slice(&ciphertext);
|
||||
let number: u128 = <u128>::from_be_bytes(bytes);
|
||||
|
||||
* */
|
||||
9
src/utils/math.rs
Normal file
9
src/utils/math.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use anyhow::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;
|
||||
}
|
||||
|
||||
Ok(vec2)
|
||||
}
|
||||
|
|
@ -1,2 +1,4 @@
|
|||
pub mod ciphers;
|
||||
pub mod math;
|
||||
pub mod parse;
|
||||
pub mod poly;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use anyhow::Result;
|
||||
use base64::prelude::*;
|
||||
use serde_json::Value;
|
||||
use std::{fmt::format, str::FromStr, u128, u8};
|
||||
|
||||
use std::{str::FromStr, u128, u8};
|
||||
|
||||
pub fn get_alpha_rep(num: u128) -> String {
|
||||
let powers: Vec<u8> = get_coefficients(num);
|
||||
|
|
@ -21,8 +21,7 @@ pub fn get_alpha_rep(num: u128) -> String {
|
|||
alpha_rep
|
||||
}
|
||||
|
||||
pub fn block_2_number(string: String) -> Result<u128> {
|
||||
//let string: String = serde_json::from_value(val["block"].clone())?;
|
||||
pub fn b64_2_num(string: &String) -> Result<u128> {
|
||||
let decoded: Vec<u8> = BASE64_STANDARD.decode(string)?;
|
||||
|
||||
let mut bytes: [u8; 16] = [0u8; 16];
|
||||
|
|
@ -67,6 +66,8 @@ pub fn coefficient_to_binary(coefficients: Vec<u8>) -> u128 {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::utils::poly::b64_2_num;
|
||||
use anyhow::Result;
|
||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||
use super::*;
|
||||
|
||||
|
|
@ -101,4 +102,17 @@ mod tests {
|
|||
"ARIAAAAAAAAAAAAAAAAAgA=="
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_b64_2_num() -> Result<()> {
|
||||
let b64_payload: String = String::from_str("juMqbhnlBwAAAAAAAAAAAA==")?;
|
||||
assert_eq!(
|
||||
b64_2_num(&b64_payload)?,
|
||||
2222222222222222,
|
||||
"Error: Value was: {}",
|
||||
b64_2_num(&b64_payload)?
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue