feat: add aes/sea encrypt/decrypt in gcm and add test cases

This commit is contained in:
0xalivecow 2024-11-03 14:12:48 +01:00
parent 6bef350301
commit 6b2775cde1
No known key found for this signature in database
6 changed files with 373 additions and 5 deletions

View file

@ -8,7 +8,7 @@ use crate::utils::{
};
use tasks01::{
block2poly::block2poly,
gcm::gcm_encrypt,
gcm::{gcm_decrypt, gcm_encrypt},
gfmul::gfmul_task,
poly2block::poly2block,
sea128::sea128,
@ -68,6 +68,14 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
Ok(json)
}
"gcm_decrypt" => {
let (plaintext, valid) = gcm_decrypt(args)?;
let out_plain = BASE64_STANDARD.encode(&plaintext);
let json = json!({ "authentic" : valid, "plaintext" : out_plain});
Ok(json)
}
_ => Err(anyhow!(
"Fatal. No compatible action found. Json data was {:?}. Arguments were; {:?}",
testcase,
@ -201,4 +209,60 @@ mod tests {
Ok(())
}
#[test]
fn test_task_gcm_encrypt_sea_case() -> Result<()> {
let json = fs::read_to_string("test_json/gcm_encrypt_sea.json").unwrap();
let parsed = parse_json(json).unwrap();
let expected = json!({ "responses" : { "b856d760-023d-4b00-bad2-15d2b6da22fe" : {
"ciphertext": "0cI/Wg4R3URfrVFZ0hw/vg==",
"tag": "ysDdzOSnqLH0MQ+Mkb23gw==",
"L": "AAAAAAAAAEAAAAAAAAAAgA==",
"H": "xhFcAUT66qWIpYz+Ch5ujw=="
}}});
assert_eq!(
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
serde_json::to_value(expected).unwrap()
);
Ok(())
}
#[test]
fn test_task_gcm_decrypt_aes_case() -> Result<()> {
let json = fs::read_to_string("test_json/gcm_decrypt_aes.json").unwrap();
let parsed = parse_json(json).unwrap();
let expected = json!({ "responses" : { "b856d760-023d-4b00-bad2-15d2b6da22fe" : {
"plaintext": "RGFzIGlzdCBlaW4gVGVzdA==",
"authentic": true,
}}});
assert_eq!(
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
serde_json::to_value(expected).unwrap()
);
Ok(())
}
#[test]
fn test_task_gcm_decrypt_sea_case() -> Result<()> {
let json = fs::read_to_string("test_json/gcm_decrypt_sea.json").unwrap();
let parsed = parse_json(json).unwrap();
let expected = json!({ "responses" : { "b856d760-023d-4b00-bad2-15d2b6da22fe" : {
"plaintext": "RGFzIGlzdCBlaW4gVGVzdA==",
"authentic": true,
}}});
assert_eq!(
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
serde_json::to_value(expected).unwrap()
);
Ok(())
}
}

View file

@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use base64::prelude::*;
use serde_json::Value;
use crate::utils::ciphers::gcm_encrypt_aes;
use crate::utils::ciphers::{gcm_decrypt_aes, gcm_decrypt_sea, gcm_encrypt_aes, gcm_encrypt_sea};
pub fn gcm_encrypt(args: &Value) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)> {
let nonce_text: String = serde_json::from_value(args["nonce"].clone())?;
@ -21,6 +21,32 @@ pub fn gcm_encrypt(args: &Value) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)>
match alg_text.as_str() {
"aes128" => Ok(gcm_encrypt_aes(nonce, key, plaintext, ad)?),
"sea128" => Ok(gcm_encrypt_sea(nonce, key, plaintext, ad)?),
_ => Err(anyhow!("No compatible algorithm found")),
}
}
pub fn gcm_decrypt(args: &Value) -> Result<(Vec<u8>, bool)> {
let nonce_text: String = serde_json::from_value(args["nonce"].clone())?;
let nonce = BASE64_STANDARD.decode(nonce_text)?;
let key_text: String = serde_json::from_value(args["key"].clone())?;
let key = BASE64_STANDARD.decode(key_text)?;
let plaintext_text: String = serde_json::from_value(args["ciphertext"].clone())?;
let plaintext = BASE64_STANDARD.decode(plaintext_text)?;
let ad_text: String = serde_json::from_value(args["ad"].clone())?;
let ad = BASE64_STANDARD.decode(ad_text)?;
let tag_text: String = serde_json::from_value(args["tag"].clone())?;
let tag = BASE64_STANDARD.decode(tag_text)?;
let alg_text: String = serde_json::from_value(args["algorithm"].clone())?;
match alg_text.as_str() {
"aes128" => Ok(gcm_decrypt_aes(nonce, key, plaintext, ad, tag)?),
"sea128" => Ok(gcm_decrypt_sea(nonce, key, plaintext, ad, tag)?),
_ => Err(anyhow!("No compatible algorithm found")),
}
}