Merge refactoring changes #34
11 changed files with 10 additions and 171 deletions
|
|
@ -19,7 +19,6 @@ pub fn block2poly(val: &Value) -> Result<Vec<u8>> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
use std::str::FromStr;
|
||||
|
||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||
use super::*;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,10 @@
|
|||
use std::{env::args, fs::canonicalize, slice::Chunks};
|
||||
|
||||
use anyhow::{Ok, Result};
|
||||
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||
use openssl::derive;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{map, Value};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::utils::{
|
||||
self,
|
||||
ciphers::ghash,
|
||||
dff::ddf,
|
||||
edf::edf,
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ pub fn gfmul_task(args: &Value) -> Result<Vec<u8>> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
use std::str::FromStr;
|
||||
|
||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||
use super::*;
|
||||
|
|
|
|||
|
|
@ -40,7 +40,6 @@ pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
|||
let q_block_count: u16 = 256;
|
||||
|
||||
//Send the first ciphertext chunk
|
||||
//eprintln!("Sending Ciphertext chunk: {:002X?}", chunk);
|
||||
stream.flush()?;
|
||||
stream.write_all(&chunk)?;
|
||||
stream.flush()?;
|
||||
|
|
@ -50,25 +49,16 @@ pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
|||
// FIXME: Assignment is redundant for now
|
||||
// TODO: Goal is to maybe add speed increase in the future
|
||||
let l_msg: [u8; 2] = q_block_count.to_le_bytes();
|
||||
//eprintln!("Sending l_msg: {:02X?}", l_msg);
|
||||
//stream.write_all(&l_msg)?;
|
||||
//stream.flush()?;
|
||||
//eprintln!("L_msg sent");
|
||||
|
||||
// Generate attack blocks
|
||||
// TODO: Collect all and send in one
|
||||
let mut payload: Vec<u8> = Vec::with_capacity(2 + 16 * 265);
|
||||
payload.extend(l_msg.to_vec());
|
||||
for j in 0..q_block_count {
|
||||
for _j in 0..q_block_count {
|
||||
// Next byte
|
||||
//eprintln!("Sending attack block: {:02X?}", attack_counter);
|
||||
|
||||
//thread::sleep(Duration::from_millis(1000));
|
||||
payload.extend(&attack_counter);
|
||||
//eprintln!("I in q builder {}", i);
|
||||
attack_counter[i as usize] += 1;
|
||||
}
|
||||
//eprintln!("Time for qblocks: {:?}", start.elapsed());
|
||||
|
||||
stream.write_all(&payload)?;
|
||||
stream.flush()?;
|
||||
|
|
@ -76,7 +66,6 @@ pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
|||
// Read server response
|
||||
let mut server_q_resp = [0u8; 256];
|
||||
stream.read_exact(&mut server_q_resp)?;
|
||||
//eprintln!("{:02X?}", buf);
|
||||
|
||||
// extract valid position
|
||||
let valid_val = server_q_resp
|
||||
|
|
@ -86,7 +75,6 @@ pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
|||
if valid_val == 0x00 {
|
||||
eprintln!("No valid found in main loop");
|
||||
}
|
||||
//eprintln!("Valid value found: {:02X?}", valid_val);
|
||||
// Craft next attack vector padding; 0x01, 0x02, ...
|
||||
attack_counter[i as usize] = valid_val;
|
||||
|
||||
|
|
@ -100,15 +88,10 @@ pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
|||
l_msg_check.extend(check_q_block.as_slice());
|
||||
|
||||
stream.write_all(&l_msg_check)?;
|
||||
//stream.write_all(&check_q_block)?;
|
||||
let mut buf = [0u8; 0x01];
|
||||
stream.read(&mut buf)?;
|
||||
//eprintln!("I = {}", i);
|
||||
//eprintln!("Buffer from pad check: {:02X?}", buf);
|
||||
if buf == [0x01] {
|
||||
//eprintln!("Valid padding");
|
||||
} else {
|
||||
//eprintln!("Invalid padding");
|
||||
// Search for second hit
|
||||
let valid_val = 255
|
||||
- server_q_resp
|
||||
|
|
@ -119,38 +102,21 @@ pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
|||
if valid_val == 0x00 {
|
||||
eprintln!("No valid found");
|
||||
}
|
||||
//eprintln!("Valid value found: {:02X?}", valid_val);
|
||||
// Craft next attack vector padding; 0x01, 0x02, ...
|
||||
attack_counter[i as usize] = valid_val;
|
||||
}
|
||||
}
|
||||
|
||||
if chunk_counter + 1 < cipher_chunks.len() {
|
||||
//eprintln!("XOR Next Ciph block");
|
||||
plaintext.push(
|
||||
cipher_chunks[chunk_counter + 1][i]
|
||||
^ (attack_counter[i as usize] ^ (15 - i as u8 + 1)),
|
||||
);
|
||||
} else {
|
||||
//seprintln!("XOR IV");
|
||||
|
||||
plaintext.push(iv[i] ^ (attack_counter[i as usize] ^ (15 - i as u8 + 1)));
|
||||
}
|
||||
//eprintln!("Attack counter after set: {:02X?}", attack_counter);
|
||||
let range = i;
|
||||
for pos in range..=15 {
|
||||
//eprintln!("i is: {:02X?}", i);
|
||||
//eprintln!("i + 1 is: {:02X?}", ((16 - i) as u8).to_le());
|
||||
/*
|
||||
eprintln!(
|
||||
"attack_counter[pos as usize]: {:02X?}",
|
||||
attack_counter[pos as usize]
|
||||
);
|
||||
eprintln!(
|
||||
"attack_counter[pos as usize] ^ 0x02 {:02X?}",
|
||||
attack_counter[pos as usize] ^ (15 - i as u8 + 1)
|
||||
);
|
||||
*/
|
||||
let intermediate = attack_counter[pos as usize] ^ (15 - i as u8 + 1);
|
||||
|
||||
attack_counter[pos as usize] = intermediate ^ ((15 - i as u8 + 1) + 1);
|
||||
|
|
@ -159,13 +125,10 @@ pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
|||
stream.flush()?;
|
||||
|
||||
// Write plaintext
|
||||
//eprintln!("{:02X?}", plaintext);
|
||||
}
|
||||
chunk_counter += 1;
|
||||
stream.flush()?;
|
||||
// break;
|
||||
drop(stream);
|
||||
//eprintln!("Time rest of calc: {:?}", start.elapsed());
|
||||
}
|
||||
|
||||
plaintext.reverse();
|
||||
|
|
@ -177,7 +140,6 @@ pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn test_connection() -> Result<()> {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::utils::poly::{polynomial_2_block};
|
||||
use crate::utils::poly::polynomial_2_block;
|
||||
use anyhow::{Ok, Result};
|
||||
use serde_json::Value;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,18 +6,13 @@ 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 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)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
use crate::utils::{field::ByteArray, poly::gfmul};
|
||||
use anyhow::Result;
|
||||
use base64::prelude::*;
|
||||
use openssl::symm::{Cipher, Crypter, Mode};
|
||||
|
||||
use super::math::xor_bytes;
|
||||
|
|
@ -162,7 +161,6 @@ pub fn gcm_decrypt_aes(
|
|||
|
||||
let mut counter: u32 = 1;
|
||||
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
||||
//nonce.append(0u8.to_le_bytes().to_vec().as_mut());
|
||||
|
||||
let auth_tag_xor = aes_128_encrypt(&key, &nonce)?;
|
||||
|
||||
|
|
@ -251,7 +249,6 @@ pub fn gcm_decrypt_sea(
|
|||
|
||||
let mut counter: u32 = 1;
|
||||
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
||||
//nonce.append(0u8.to_le_bytes().to_vec().as_mut());
|
||||
|
||||
let auth_tag_xor = sea_128_encrypt(&key, &nonce)?;
|
||||
|
||||
|
|
@ -325,13 +322,6 @@ pub fn ghash(
|
|||
Ok(inter_loop)
|
||||
}
|
||||
|
||||
/*
|
||||
* let mut bytes: [u8; 16] = [0u8; 16];
|
||||
bytes.copy_from_slice(&ciphertext);
|
||||
let number: u128 = <u128>::from_be_bytes(bytes);
|
||||
|
||||
* */
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ mod tests {
|
|||
}
|
||||
|
||||
println!("Result: {:?}", result);
|
||||
let bit_indices: Vec<u8> = vec![0];
|
||||
let _bit_indices: Vec<u8> = vec![0];
|
||||
assert!(false)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,9 +8,6 @@ use std::{
|
|||
|
||||
use anyhow::{anyhow, Ok, Result};
|
||||
|
||||
use crate::utils::poly::bgfmul;
|
||||
|
||||
use super::poly::polynomial_2_block;
|
||||
use super::{
|
||||
math::{reverse_bits_in_bytevec, xor_bytes},
|
||||
poly::gfmul,
|
||||
|
|
@ -43,14 +40,6 @@ impl FieldElement {
|
|||
self.field_element.clone()
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn padd(&mut self) {
|
||||
if self.field_element.len() % 16 != 0 || ad.is_empty() {
|
||||
ad.append(vec![0u8; 16 - (ad.len() % 16)].as_mut());
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn new(field_element: Vec<u8>) -> Self {
|
||||
Self {
|
||||
field_element: reverse_bits_in_bytevec(field_element),
|
||||
|
|
@ -82,29 +71,18 @@ impl FieldElement {
|
|||
return result;
|
||||
}
|
||||
|
||||
//eprintln!("Initial result: {:?}", result);
|
||||
while exponent > 0 {
|
||||
//eprintln!("Current exponent: {:02X}", exponent);
|
||||
if exponent & 1 == 1 {
|
||||
let temp = &self * &result;
|
||||
//eprintln!("Mult");
|
||||
//eprintln!("After mod: {:?}", temp);
|
||||
|
||||
result = temp
|
||||
}
|
||||
let temp_square = &self * &self;
|
||||
// eprintln!("Square");
|
||||
|
||||
// eprintln!("After squaring: {:?}", temp_square);
|
||||
self = temp_square;
|
||||
//eprintln!("After mod: {:?}", self);
|
||||
exponent >>= 1;
|
||||
}
|
||||
|
||||
// eprintln!("result in powmod before reduction: {:02X?}", result);
|
||||
|
||||
// eprintln!("result in powmod after reduction: {:02X?}", result);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
|
|
@ -113,10 +91,8 @@ impl FieldElement {
|
|||
|
||||
let mut inverser = INVERSER_START;
|
||||
let mut inverse: Vec<u8> = vec![0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
||||
//eprintln!("Inverse start {:02X?}", inverse);
|
||||
|
||||
while inverser > 0 {
|
||||
//eprintln!("{:02X}", inverser);
|
||||
if inverser & 1 == 1 {
|
||||
inverse = gfmul(&self.field_element, &inverse, "xex").unwrap();
|
||||
}
|
||||
|
|
@ -124,7 +100,6 @@ impl FieldElement {
|
|||
self.field_element = gfmul(&self.field_element, &self.field_element, "xex")
|
||||
.expect("Error in sqrmul sqr");
|
||||
}
|
||||
//eprintln!("Inverse rhs {:?}", inverse);
|
||||
FieldElement::new_no_convert(inverse)
|
||||
}
|
||||
|
||||
|
|
@ -362,7 +337,6 @@ impl ByteArray {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn test_byte_array_shift1() {
|
||||
|
|
|
|||
|
|
@ -1,21 +1,20 @@
|
|||
use crate::utils::field::ByteArray;
|
||||
use base64::prelude::*;
|
||||
|
||||
use num::traits::{FromBytes, ToBytes};
|
||||
use num::{BigInt, BigUint, One, Zero};
|
||||
use num::traits::FromBytes;
|
||||
use num::{BigUint, One, Zero};
|
||||
|
||||
use std::{str::FromStr, u128, u8, usize};
|
||||
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
ops::{Add, Div, Mul},
|
||||
ops::{Add, Mul},
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, Ok, Result};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::field::FieldElement;
|
||||
use super::math::reverse_bits_in_bytevec;
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Polynomial {
|
||||
|
|
@ -108,27 +107,18 @@ impl Polynomial {
|
|||
return result;
|
||||
}
|
||||
|
||||
//eprintln!("Initial result: {:?}", result);
|
||||
while exponent > 0 {
|
||||
//eprintln!("Current exponent: {:02X}", exponent);
|
||||
if exponent & 1 == 1 {
|
||||
let temp = &self * &result;
|
||||
//eprintln!("Mult");
|
||||
//eprintln!("After mod: {:?}", temp);
|
||||
|
||||
result = temp
|
||||
}
|
||||
let temp_square = &self * &self;
|
||||
//eprintln!("Square");
|
||||
|
||||
//eprintln!("After squaring: {:?}", temp_square);
|
||||
self = temp_square;
|
||||
//eprintln!("After mod: {:?}", self);
|
||||
exponent >>= 1;
|
||||
}
|
||||
|
||||
//eprintln!("result in powmod before reduction: {:02X?}", result);
|
||||
|
||||
while !result.polynomial.is_empty()
|
||||
&& result
|
||||
.polynomial
|
||||
|
|
@ -141,8 +131,6 @@ impl Polynomial {
|
|||
result.polynomial.pop();
|
||||
}
|
||||
|
||||
//eprintln!("result in powmod after reduction: {:02X?}", result);
|
||||
|
||||
if result.is_empty() {
|
||||
result = Polynomial::zero();
|
||||
}
|
||||
|
|
@ -167,19 +155,13 @@ impl Polynomial {
|
|||
return result;
|
||||
}
|
||||
|
||||
//eprintln!("Initial result: {:?}", result);
|
||||
while &exponent > &BigUint::zero() {
|
||||
//eprintln!("Current exponent: {:02X}", exponent);
|
||||
if &exponent & BigUint::one() == BigUint::one() {
|
||||
let temp = &self * &result;
|
||||
//eprintln!("After multiplication: {:?}", temp);
|
||||
result = temp.div(&modulus).1;
|
||||
//eprintln!("After mod: {:?}", result);
|
||||
}
|
||||
let temp_square = &self * &self;
|
||||
//eprintln!("After squaring: {:?}", temp_square);
|
||||
self = temp_square.div(&modulus).1;
|
||||
//eprintln!("After mod: {:?}", self);
|
||||
exponent >>= 1;
|
||||
}
|
||||
|
||||
|
|
@ -216,19 +198,13 @@ impl Polynomial {
|
|||
return result;
|
||||
}
|
||||
|
||||
//eprintln!("Initial result: {:?}", result);
|
||||
while exponent > 0 {
|
||||
//eprintln!("Current exponent: {:02X}", exponent);
|
||||
if exponent & 1 == 1 {
|
||||
let temp = &self * &result;
|
||||
//eprintln!("After multiplication: {:?}", temp);
|
||||
result = temp.div(&modulus).1;
|
||||
//eprintln!("After mod: {:?}", result);
|
||||
}
|
||||
let temp_square = &self * &self;
|
||||
//eprintln!("After squaring: {:?}", temp_square);
|
||||
self = temp_square.div(&modulus).1;
|
||||
//eprintln!("After mod: {:?}", self);
|
||||
exponent >>= 1;
|
||||
}
|
||||
|
||||
|
|
@ -574,14 +550,11 @@ pub fn sort_polynomial_array(mut polys: Vec<Polynomial>) -> Result<Vec<Polynomia
|
|||
pub const RED_POLY: u128 = 0x87000000_00000000_00000000_00000000;
|
||||
|
||||
pub fn gfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<u8>> {
|
||||
let mut red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec());
|
||||
//red_poly_bytes.0.push(0x01);
|
||||
let red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec());
|
||||
|
||||
let mut poly1: ByteArray = ByteArray(poly_a.to_vec());
|
||||
//poly1.0.push(0x00);
|
||||
|
||||
let mut poly2: ByteArray = ByteArray(poly_b.to_vec());
|
||||
//poly2.0.push(0x00);
|
||||
|
||||
if semantic == "gcm" {
|
||||
poly1.reverse_bits_in_bytevec();
|
||||
|
|
@ -618,53 +591,6 @@ pub fn gfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<u
|
|||
Ok(result.0)
|
||||
}
|
||||
|
||||
pub fn bgfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<u8>> {
|
||||
//TODO: Implement gfmul with bigint
|
||||
let red_poly_bytes: BigUint = BigUint::from_slice(&[
|
||||
0x87, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x01,
|
||||
]);
|
||||
|
||||
let mut poly1: BigUint = BigUint::from_le_bytes(poly_a);
|
||||
|
||||
let mut poly2: BigUint = BigUint::from_le_bytes(poly_b);
|
||||
|
||||
/*
|
||||
if semantic == "gcm" {
|
||||
poly1.re;
|
||||
poly2.reverse_bits_in_bytevec();
|
||||
}
|
||||
*/
|
||||
|
||||
let mut result: BigUint = BigUint::zero();
|
||||
|
||||
if (&poly2 & (BigUint::one() << 127)) == BigUint::one() {
|
||||
result = &result ^ &poly1;
|
||||
}
|
||||
poly2 = &poly2 >> 1;
|
||||
|
||||
while &poly2 != &BigUint::zero() {
|
||||
poly1 = &poly1 << 1;
|
||||
|
||||
if (&poly1 & (BigUint::one() << 127)) == BigUint::one() {
|
||||
poly1 = &poly1 ^ &red_poly_bytes;
|
||||
}
|
||||
|
||||
if &poly2 & BigUint::one() == BigUint::one() {
|
||||
result = &result ^ &poly1;
|
||||
}
|
||||
|
||||
poly2 = &poly2 >> 1;
|
||||
}
|
||||
|
||||
/*
|
||||
if semantic == "gcm" {
|
||||
result.reverse_bits_in_bytevec();
|
||||
}
|
||||
*/
|
||||
|
||||
Ok(result.to_bytes_le())
|
||||
}
|
||||
|
||||
pub fn convert_gcm_to_xex(gcm_poly: Vec<u8>) -> Result<Vec<u8>> {
|
||||
let xex_poly = gcm_poly
|
||||
.into_iter()
|
||||
|
|
@ -677,8 +603,6 @@ pub fn convert_gcm_to_xex(gcm_poly: Vec<u8>) -> Result<Vec<u8>> {
|
|||
pub fn get_alpha_rep(num: u128) -> String {
|
||||
let powers: Vec<u8> = get_coefficients(num);
|
||||
|
||||
//println!("{:?}", powers);
|
||||
|
||||
let mut alpha_rep = String::new();
|
||||
|
||||
if powers.len() == 1 {
|
||||
|
|
@ -705,7 +629,6 @@ pub fn b64_2_num(string: &String) -> Result<u128> {
|
|||
pub fn get_coefficients(num: u128) -> Vec<u8> {
|
||||
let mut powers: Vec<u8> = vec![];
|
||||
for shift in 0..128 {
|
||||
//println!("{:?}", ((num >> shift) & 1));
|
||||
if ((num >> shift) & 1) == 1 {
|
||||
powers.push(shift);
|
||||
}
|
||||
|
|
@ -832,7 +755,7 @@ mod tests {
|
|||
#[test]
|
||||
fn coeff_to_binary() {
|
||||
let coefficients: Vec<u8> = vec![12, 127, 9, 0];
|
||||
let b64: &str = "ARIAAAAAAAAAAAAAAAAAgA==";
|
||||
let _b64: &str = "ARIAAAAAAAAAAAAAAAAAgA==";
|
||||
let calculated_num: u128 = coefficient_to_binary(coefficients);
|
||||
assert_eq!(
|
||||
BASE64_STANDARD.encode(calculated_num.to_ne_bytes()),
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ mod tests {
|
|||
}
|
||||
|
||||
println!("{:?}", result);
|
||||
let bit_indices: Vec<u8> = vec![0];
|
||||
let _bit_indices: Vec<u8> = vec![0];
|
||||
assert!(false)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue