Poly2Block; Block2Poly; SEA128 tasks working #2
3 changed files with 102 additions and 22 deletions
|
|
@ -8,6 +8,7 @@ use crate::utils::parse::{Responses, Testcase, Testcases};
|
|||
use tasks01::{
|
||||
block2poly::block2poly,
|
||||
poly2block::{self, poly2block},
|
||||
sea128::sea128,
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
|
@ -36,6 +37,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")),
|
||||
}
|
||||
}
|
||||
|
|
@ -86,4 +92,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,4 +1,4 @@
|
|||
use anyhow::Result;
|
||||
use anyhow::{anyhow, Result};
|
||||
use base64::prelude::*;
|
||||
use openssl::symm::{Cipher, Crypter, Mode};
|
||||
use serde_json::Value;
|
||||
|
|
@ -9,25 +9,26 @@ 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 plaintexts_string: String = serde_json::from_value(args["input"].clone())?;
|
||||
//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 plaintexts = BASE64_STANDARD.decode(plaintexts_string)?;
|
||||
eprintln!("{:?}", plaintexts);
|
||||
|
||||
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 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(&plaintexts, &mut ciphertext)?;
|
||||
let mut count = encrypter.update(&input, &mut ciphertext)?;
|
||||
count += encrypter.finalize(&mut ciphertext)?;
|
||||
ciphertext.truncate(count);
|
||||
|
||||
eprintln!("{:?}", &ciphertext[..]);
|
||||
//eprintln!("{:?}", &ciphertext[..]);
|
||||
|
||||
let mut bytes: [u8; 16] = [0u8; 16];
|
||||
bytes.copy_from_slice(&ciphertext);
|
||||
|
|
@ -37,6 +38,29 @@ pub fn sea128(args: &Value) -> Result<String> {
|
|||
|
||||
Ok(output)
|
||||
}
|
||||
"decrypt" => {
|
||||
let mut decrypter = Crypter::new(Cipher::aes_128_ecb(), Mode::Decrypt, &key, None)?;
|
||||
decrypter.pad(false);
|
||||
|
||||
let mut bytes: [u8; 16] = [0u8; 16];
|
||||
bytes.copy_from_slice(&input);
|
||||
let input_num: u128 = <u128>::from_be_bytes(bytes);
|
||||
|
||||
let input_afer_xor = (input_num ^ xor_val).to_be_bytes();
|
||||
|
||||
let mut plaintext = [0; 32].to_vec();
|
||||
|
||||
let mut count = decrypter.update(&input_afer_xor, &mut plaintext)?;
|
||||
count += decrypter.finalize(&mut plaintext)?;
|
||||
plaintext.truncate(count);
|
||||
|
||||
let output = BASE64_STANDARD.encode(plaintext);
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
_ => Err(anyhow!("Failure. no valid mode detected")),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
|
@ -48,12 +72,20 @@ mod tests {
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_sea128() -> Result<()> {
|
||||
let args =
|
||||
json!({"key" : "istDASeincoolerKEYrofg==", "input" : "yv66vvrO263eyviIiDNEVQ=="});
|
||||
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=="
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue