refactor: Externalise AES and SEA functions
This commit is contained in:
parent
3a777cab00
commit
7c94e5d8fb
4 changed files with 76 additions and 33 deletions
|
|
@ -1,9 +1,11 @@
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
use openssl::symm::{Cipher, Crypter, Mode};
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::utils::poly::b64_2_num;
|
use crate::utils::{
|
||||||
|
ciphers::{sea_128_decrypt, sea_128_encrypt},
|
||||||
|
poly::b64_2_num,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn sea128(args: &Value) -> Result<String> {
|
pub fn sea128(args: &Value) -> Result<String> {
|
||||||
let key_string: String = serde_json::from_value(args["key"].clone())?;
|
let key_string: String = serde_json::from_value(args["key"].clone())?;
|
||||||
|
|
@ -19,42 +21,13 @@ pub fn sea128(args: &Value) -> Result<String> {
|
||||||
match mode.as_str() {
|
match mode.as_str() {
|
||||||
"encrypt" => {
|
"encrypt" => {
|
||||||
//eprintln!("{:?}", plaintexts);
|
//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 output = BASE64_STANDARD.encode(sea_128_encrypt(&key, &input)?);
|
||||||
|
|
||||||
let mut count = encrypter.update(&input, &mut ciphertext)?;
|
|
||||||
count += encrypter.finalize(&mut ciphertext)?;
|
|
||||||
ciphertext.truncate(count);
|
|
||||||
|
|
||||||
//eprintln!("{:?}", &ciphertext[..]);
|
|
||||||
|
|
||||||
let mut bytes: [u8; 16] = [0u8; 16];
|
|
||||||
bytes.copy_from_slice(&ciphertext);
|
|
||||||
let number: u128 = <u128>::from_be_bytes(bytes);
|
|
||||||
|
|
||||||
let output = BASE64_STANDARD.encode((number ^ xor_val).to_be_bytes());
|
|
||||||
|
|
||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
"decrypt" => {
|
"decrypt" => {
|
||||||
let mut decrypter = Crypter::new(Cipher::aes_128_ecb(), Mode::Decrypt, &key, None)?;
|
let output = BASE64_STANDARD.encode(sea_128_decrypt(&key, &input)?);
|
||||||
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)
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
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 parse;
|
||||||
pub mod poly;
|
pub mod poly;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue