From 90d61a655ed60e432b40a78cfa8107d090a50fc8 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Tue, 3 Dec 2024 23:15:42 +0100 Subject: [PATCH 1/9] fix: Fix length field implementation in gcm_crack Length field was calculated after padding --- src/tasks/tasks01/gcm_crack.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/tasks/tasks01/gcm_crack.rs b/src/tasks/tasks01/gcm_crack.rs index 5dc2236..05d8886 100644 --- a/src/tasks/tasks01/gcm_crack.rs +++ b/src/tasks/tasks01/gcm_crack.rs @@ -35,9 +35,12 @@ struct Message { fn parse_message(val: &Value) -> Result<(Message, Polynomial)> { let ciphertext_text: String = serde_json::from_value(val["ciphertext"].clone())?; let mut ciphertext_bytes: Vec = BASE64_STANDARD.decode(ciphertext_text)?; + let mut c_len: Vec = ((ciphertext_bytes.len() * 8) as u64).to_be_bytes().to_vec(); + if ciphertext_bytes.len() % 16 != 0 { ciphertext_bytes.append(vec![0u8; 16 - (ciphertext_bytes.len() % 16)].as_mut()); } + let ciphertext_chunks: Vec = ciphertext_bytes .chunks(16) .into_iter() @@ -61,7 +64,6 @@ fn parse_message(val: &Value) -> Result<(Message, Polynomial)> { let tag_bytes: Vec = BASE64_STANDARD.decode(tag_text)?; let tag_field: FieldElement = FieldElement::new(tag_bytes.clone()); - let mut c_len: Vec = ((ciphertext_bytes.len() * 8) as u64).to_be_bytes().to_vec(); l_field.append(c_len.as_mut()); // Combine all data From 7a0d1219f90a2d083da8931c9fcf0977abbba1a3 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Thu, 5 Dec 2024 15:57:18 +0100 Subject: [PATCH 2/9] fix: Fix GCM crack output --- src/tasks/mod.rs | 2 +- src/tasks/tasks01/gcm_crack.rs | 62 +++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index aeca1c7..d48952d 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -179,7 +179,7 @@ pub fn task_deploy(testcase: &Testcase) -> Result { } "gcm_crack" => { let result = gcm_crack(args)?; - let json = json!({"factors" : result}); + let json = json!(result); Ok(json) } diff --git a/src/tasks/tasks01/gcm_crack.rs b/src/tasks/tasks01/gcm_crack.rs index 05d8886..cce3df6 100644 --- a/src/tasks/tasks01/gcm_crack.rs +++ b/src/tasks/tasks01/gcm_crack.rs @@ -54,6 +54,7 @@ fn parse_message(val: &Value) -> Result<(Message, Polynomial)> { if ad_bytes.len() % 16 != 0 || ad_bytes.is_empty() { ad_bytes.append(vec![0u8; 16 - (ad_bytes.len() % 16)].as_mut()); } + let ad_chunks: Vec = ad_bytes .chunks(16) .into_iter() @@ -128,7 +129,6 @@ pub fn gcm_crack(args: &Value) -> Result { for candidate in combine_edf { if candidate.degree() == 1 { h_candidate = candidate.extract_component(0); - eprintln!("H candidate: {:02X?}", h_candidate.to_b64()); let m1_ghash = ghash( reverse_bits_in_bytevec(h_candidate.to_vec()), m1_data.ad.clone(), @@ -170,6 +170,7 @@ pub fn gcm_crack(args: &Value) -> Result { ); if m3_auth_tag.is_empty() { + assert!(false); eprintln!("No valid candidate found"); } @@ -195,3 +196,62 @@ pub fn gcm_crack(args: &Value) -> Result { mask: BASE64_STANDARD.encode(eky0), }) } + +#[cfg(test)] +mod tests { + + use anyhow::Result; + + use rand::Rng; + + use serde_json::json; + use utils::ciphers::{aes_128_encrypt, gcm_encrypt_aes}; + // Note this useful idiom: importing names from outer (for mod tests) scope. + use super::*; + + #[test] + fn test_random() -> Result<()> { + let key = vec![1, 1, 1, 1]; + let nonce = BASE64_STANDARD.decode("4gF+BtR3ku/PUQci")?; + let ad = vec![0]; + + let input: Vec = Vec::with_capacity(rand::thread_rng().gen_range(0..=60)); + let plain1 = gcm_encrypt_aes(nonce.clone(), key.clone(), input, ad.clone())?; + let input: Vec = Vec::with_capacity(rand::thread_rng().gen_range(0..=60)); + let plain2 = gcm_encrypt_aes(nonce.clone(), key.clone(), input, ad.clone())?; + let input: Vec = Vec::with_capacity(rand::thread_rng().gen_range(0..=60)); + let plain3 = gcm_encrypt_aes(nonce.clone(), key.clone(), input, ad.clone())?; + + let crack_input = json!({ + "testcases": { + "gcm_crack46": { + "action": "gcm_crack", + "arguments": { + "nonce": "4gF+BtR3ku/PUQci", + "m1": { + "ciphertext": BASE64_STANDARD.encode(plain1.0), + "associated_data": "", + "tag": BASE64_STANDARD.encode(plain1.1) + }, + "m2": { + "ciphertext": BASE64_STANDARD.encode(plain2.0), + "associated_data": "", + "tag": BASE64_STANDARD.encode(plain2.1) + }, + "m3": { + "ciphertext": BASE64_STANDARD.encode(plain3.0), + "associated_data": "", + "tag": BASE64_STANDARD.encode(plain3.1) + }, + "forgery": { + "ciphertext": "AXe/ZQ==", + "associated_data": "" + } + } + } + } + }); + + todo!(); + } +} From 6d1b735a0ba0aefcb51d3f2df2074cc9d5c158b5 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Thu, 5 Dec 2024 16:37:54 +0100 Subject: [PATCH 3/9] refactor: remove unneded prints and enable mt --- src/tasks/mod.rs | 2 +- src/tasks/tasks01/gcm_crack.rs | 80 ---------------------------------- 2 files changed, 1 insertion(+), 81 deletions(-) diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index d48952d..8a8b782 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -241,7 +241,7 @@ pub fn task_distribute_st(testcases: &Testcases) -> Result { pub fn task_distribute(testcases: &Testcases) -> Result { let cpus = num_cpus::get(); - if cpus > 1000000 { + if cpus > 1 { task_distribute_mt(testcases) } else { task_distribute_st(testcases) diff --git a/src/tasks/tasks01/gcm_crack.rs b/src/tasks/tasks01/gcm_crack.rs index cce3df6..89788c7 100644 --- a/src/tasks/tasks01/gcm_crack.rs +++ b/src/tasks/tasks01/gcm_crack.rs @@ -98,13 +98,8 @@ pub fn gcm_crack(args: &Value) -> Result { let (m3_data, _) = parse_message(&args["m3"])?; - eprintln!("m1 poly: {:?}", m1_h_poly.clone().to_c_array()); - eprintln!("m2 poly: {:?}", m2_h_poly.clone().to_c_array()); - let combine_poly = m1_h_poly + m2_h_poly; - eprintln!("combine poly: {:?}", combine_poly.clone().to_c_array()); - let combine_sff = sff(combine_poly.monic()); let mut combine_ddf: Vec<(Polynomial, u128)> = vec![]; @@ -112,8 +107,6 @@ pub fn gcm_crack(args: &Value) -> Result { combine_ddf.extend(ddf(factor)); } - eprintln!("combine_ddf: {:?}", combine_ddf); - let mut combine_edf: Vec = vec![]; for (factor, degree) in combine_ddf { if degree == 1 { @@ -121,8 +114,6 @@ pub fn gcm_crack(args: &Value) -> Result { } } - eprintln!("combine_edf: {:?}", combine_edf); - let mut m3_auth_tag: Vec = vec![]; let mut h_candidate: FieldElement = FieldElement::zero(); let mut eky0: Vec = vec![]; @@ -155,8 +146,6 @@ pub fn gcm_crack(args: &Value) -> Result { ); if m3_auth_tag == m3_data.tag { - eprintln!("Candidate valid"); - eprintln!("{:02X?}", m3_auth_tag); break; } else { eprintln!("H candidate not valid"); @@ -164,16 +153,6 @@ pub fn gcm_crack(args: &Value) -> Result { } } - eprintln!( - "M3 Authentication TAG {:02X?}", - BASE64_STANDARD.encode(&m3_auth_tag) - ); - - if m3_auth_tag.is_empty() { - assert!(false); - eprintln!("No valid candidate found"); - } - let (forgery_data, _) = parse_message(&args["forgery"])?; let forgery_ghash = ghash( @@ -196,62 +175,3 @@ pub fn gcm_crack(args: &Value) -> Result { mask: BASE64_STANDARD.encode(eky0), }) } - -#[cfg(test)] -mod tests { - - use anyhow::Result; - - use rand::Rng; - - use serde_json::json; - use utils::ciphers::{aes_128_encrypt, gcm_encrypt_aes}; - // Note this useful idiom: importing names from outer (for mod tests) scope. - use super::*; - - #[test] - fn test_random() -> Result<()> { - let key = vec![1, 1, 1, 1]; - let nonce = BASE64_STANDARD.decode("4gF+BtR3ku/PUQci")?; - let ad = vec![0]; - - let input: Vec = Vec::with_capacity(rand::thread_rng().gen_range(0..=60)); - let plain1 = gcm_encrypt_aes(nonce.clone(), key.clone(), input, ad.clone())?; - let input: Vec = Vec::with_capacity(rand::thread_rng().gen_range(0..=60)); - let plain2 = gcm_encrypt_aes(nonce.clone(), key.clone(), input, ad.clone())?; - let input: Vec = Vec::with_capacity(rand::thread_rng().gen_range(0..=60)); - let plain3 = gcm_encrypt_aes(nonce.clone(), key.clone(), input, ad.clone())?; - - let crack_input = json!({ - "testcases": { - "gcm_crack46": { - "action": "gcm_crack", - "arguments": { - "nonce": "4gF+BtR3ku/PUQci", - "m1": { - "ciphertext": BASE64_STANDARD.encode(plain1.0), - "associated_data": "", - "tag": BASE64_STANDARD.encode(plain1.1) - }, - "m2": { - "ciphertext": BASE64_STANDARD.encode(plain2.0), - "associated_data": "", - "tag": BASE64_STANDARD.encode(plain2.1) - }, - "m3": { - "ciphertext": BASE64_STANDARD.encode(plain3.0), - "associated_data": "", - "tag": BASE64_STANDARD.encode(plain3.1) - }, - "forgery": { - "ciphertext": "AXe/ZQ==", - "associated_data": "" - } - } - } - } - }); - - todo!(); - } -} From 0da047110f94996cbb76fc30e2cdeeaf0beb4bac Mon Sep 17 00:00:00 2001 From: Alivecow Date: Thu, 5 Dec 2024 17:48:31 +0100 Subject: [PATCH 4/9] feat: enable tcp no delay option --- src/tasks/tasks01/pad_oracle.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tasks/tasks01/pad_oracle.rs b/src/tasks/tasks01/pad_oracle.rs index 4286e37..6ea7d3e 100644 --- a/src/tasks/tasks01/pad_oracle.rs +++ b/src/tasks/tasks01/pad_oracle.rs @@ -29,6 +29,7 @@ pub fn padding_oracle(args: &Value) -> Result> { for chunk in &cipher_chunks { let mut stream = TcpStream::connect(format!("{}:{}", hostname, port))?; + stream.set_nodelay(true).expect("Error on no delay"); stream.set_nonblocking(false)?; // Track value sent to server From 2f0e265ed69adc1a9a8f684eca5dc22f366f2942 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Thu, 5 Dec 2024 18:12:26 +0100 Subject: [PATCH 5/9] refactor: Change vector init in padding oracle --- src/tasks/tasks01/pad_oracle.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tasks/tasks01/pad_oracle.rs b/src/tasks/tasks01/pad_oracle.rs index 6ea7d3e..60356b4 100644 --- a/src/tasks/tasks01/pad_oracle.rs +++ b/src/tasks/tasks01/pad_oracle.rs @@ -57,7 +57,8 @@ pub fn padding_oracle(args: &Value) -> Result> { // Generate attack blocks // TODO: Collect all and send in one - let mut payload: Vec = l_msg.to_vec(); + let mut payload: Vec = Vec::with_capacity(2 + 16 * 265); + payload.extend(l_msg.to_vec()); for j in 0..q_block_count { // Next byte //eprintln!("Sending attack block: {:02X?}", attack_counter); From b24c70342900eb3f994dcc3179925bb96506c3a8 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Sun, 22 Dec 2024 18:13:23 +0100 Subject: [PATCH 6/9] refactor: Apply general cargo recommendations --- src/tasks/tasks01/gcm_crack.rs | 5 +---- src/utils/ciphers.rs | 1 - src/utils/field.rs | 2 -- src/utils/poly.rs | 5 ++--- 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/tasks/tasks01/gcm_crack.rs b/src/tasks/tasks01/gcm_crack.rs index 89788c7..09ccb2c 100644 --- a/src/tasks/tasks01/gcm_crack.rs +++ b/src/tasks/tasks01/gcm_crack.rs @@ -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, diff --git a/src/utils/ciphers.rs b/src/utils/ciphers.rs index 82af34b..f147c5a 100644 --- a/src/utils/ciphers.rs +++ b/src/utils/ciphers.rs @@ -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; diff --git a/src/utils/field.rs b/src/utils/field.rs index 13029cd..b5a65dc 100644 --- a/src/utils/field.rs +++ b/src/utils/field.rs @@ -8,9 +8,7 @@ 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, diff --git a/src/utils/poly.rs b/src/utils/poly.rs index 61109f8..361108e 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -2,7 +2,7 @@ use crate::utils::field::ByteArray; use base64::prelude::*; use num::traits::{FromBytes, ToBytes}; -use num::{BigInt, BigUint, One, Zero}; +use num::{BigUint, One, Zero}; use std::{str::FromStr, u128, u8, usize}; @@ -15,7 +15,6 @@ 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 { @@ -574,7 +573,7 @@ pub fn sort_polynomial_array(mut polys: Vec) -> Result, poly_b: &Vec, semantic: &str) -> Result> { - let mut red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec()); + let red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec()); //red_poly_bytes.0.push(0x01); let mut poly1: ByteArray = ByteArray(poly_a.to_vec()); From c9c26b39712112381b9237eeb7839f7d74812439 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Mon, 23 Dec 2024 10:27:23 +0100 Subject: [PATCH 7/9] refactor: remove commented code --- src/tasks/tasks01/block2poly.rs | 1 - src/tasks/tasks01/pad_oracle.rs | 37 -------------------------- src/tasks/tasks01/sea128.rs | 5 ---- src/utils/ciphers.rs | 9 ------- src/utils/field.rs | 23 ---------------- src/utils/poly.rs | 47 ++------------------------------- 6 files changed, 2 insertions(+), 120 deletions(-) diff --git a/src/tasks/tasks01/block2poly.rs b/src/tasks/tasks01/block2poly.rs index 3e636eb..3377bf6 100644 --- a/src/tasks/tasks01/block2poly.rs +++ b/src/tasks/tasks01/block2poly.rs @@ -19,7 +19,6 @@ pub fn block2poly(val: &Value) -> Result> { #[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::*; diff --git a/src/tasks/tasks01/pad_oracle.rs b/src/tasks/tasks01/pad_oracle.rs index 60356b4..067a55f 100644 --- a/src/tasks/tasks01/pad_oracle.rs +++ b/src/tasks/tasks01/pad_oracle.rs @@ -40,7 +40,6 @@ pub fn padding_oracle(args: &Value) -> Result> { 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,10 +49,6 @@ pub fn padding_oracle(args: &Value) -> Result> { // 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 @@ -61,14 +56,9 @@ pub fn padding_oracle(args: &Value) -> Result> { payload.extend(l_msg.to_vec()); 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> { // 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> { 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> { 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> { 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> { stream.flush()?; // Write plaintext - //eprintln!("{:02X?}", plaintext); } chunk_counter += 1; stream.flush()?; - // break; drop(stream); - //eprintln!("Time rest of calc: {:?}", start.elapsed()); } plaintext.reverse(); diff --git a/src/tasks/tasks01/sea128.rs b/src/tasks/tasks01/sea128.rs index 6d76909..2a6a7e2 100644 --- a/src/tasks/tasks01/sea128.rs +++ b/src/tasks/tasks01/sea128.rs @@ -6,18 +6,13 @@ use crate::utils::ciphers::{sea_128_decrypt, sea_128_encrypt}; pub fn sea128(args: &Value) -> Result { 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) diff --git a/src/utils/ciphers.rs b/src/utils/ciphers.rs index f147c5a..0b558af 100644 --- a/src/utils/ciphers.rs +++ b/src/utils/ciphers.rs @@ -161,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)?; @@ -250,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)?; @@ -324,13 +322,6 @@ pub fn ghash( Ok(inter_loop) } -/* -* let mut bytes: [u8; 16] = [0u8; 16]; - bytes.copy_from_slice(&ciphertext); - let number: u128 = ::from_be_bytes(bytes); - -* */ - #[cfg(test)] mod tests { use super::*; diff --git a/src/utils/field.rs b/src/utils/field.rs index b5a65dc..0a06662 100644 --- a/src/utils/field.rs +++ b/src/utils/field.rs @@ -8,7 +8,6 @@ use std::{ use anyhow::{anyhow, Ok, Result}; - use super::{ math::{reverse_bits_in_bytevec, xor_bytes}, poly::gfmul, @@ -41,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) -> Self { Self { field_element: reverse_bits_in_bytevec(field_element), @@ -80,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 } @@ -111,10 +91,8 @@ impl FieldElement { let mut inverser = INVERSER_START; let mut inverse: Vec = 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(); } @@ -122,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) } diff --git a/src/utils/poly.rs b/src/utils/poly.rs index 361108e..90ef5e0 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -1,14 +1,14 @@ use crate::utils::field::ByteArray; use base64::prelude::*; -use num::traits::{FromBytes, ToBytes}; +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}; @@ -107,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 @@ -140,8 +131,6 @@ impl Polynomial { result.polynomial.pop(); } - //eprintln!("result in powmod after reduction: {:02X?}", result); - if result.is_empty() { result = Polynomial::zero(); } @@ -166,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; } @@ -215,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,13 +551,10 @@ pub const RED_POLY: u128 = 0x87000000_00000000_00000000_00000000; pub fn gfmul(poly_a: &Vec, poly_b: &Vec, semantic: &str) -> Result> { let red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec()); - //red_poly_bytes.0.push(0x01); 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,7 +592,6 @@ pub fn gfmul(poly_a: &Vec, poly_b: &Vec, semantic: &str) -> Result, poly_b: &Vec, semantic: &str) -> Result> { - //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, ]); @@ -627,13 +600,6 @@ pub fn bgfmul(poly_a: &Vec, poly_b: &Vec, semantic: &str) -> Result, poly_b: &Vec, semantic: &str) -> Result> 1; } - /* - if semantic == "gcm" { - result.reverse_bits_in_bytevec(); - } - */ - Ok(result.to_bytes_le()) } @@ -676,8 +636,6 @@ pub fn convert_gcm_to_xex(gcm_poly: Vec) -> Result> { pub fn get_alpha_rep(num: u128) -> String { let powers: Vec = get_coefficients(num); - //println!("{:?}", powers); - let mut alpha_rep = String::new(); if powers.len() == 1 { @@ -704,7 +662,6 @@ pub fn b64_2_num(string: &String) -> Result { pub fn get_coefficients(num: u128) -> Vec { let mut powers: Vec = vec![]; for shift in 0..128 { - //println!("{:?}", ((num >> shift) & 1)); if ((num >> shift) & 1) == 1 { powers.push(shift); } From 848ad15bb8ed531ab246777f76635528b90d2612 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Mon, 23 Dec 2024 10:29:12 +0100 Subject: [PATCH 8/9] refactor: Remove unneded imports --- src/tasks/tasks01/gfmul.rs | 1 - src/tasks/tasks01/pad_oracle.rs | 3 +-- src/tasks/tasks01/poly2block.rs | 2 +- src/utils/dff.rs | 2 +- src/utils/field.rs | 1 - src/utils/poly.rs | 2 +- src/utils/sff.rs | 2 +- 7 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/tasks/tasks01/gfmul.rs b/src/tasks/tasks01/gfmul.rs index 70ab788..8bca55f 100644 --- a/src/tasks/tasks01/gfmul.rs +++ b/src/tasks/tasks01/gfmul.rs @@ -21,7 +21,6 @@ pub fn gfmul_task(args: &Value) -> Result> { #[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::*; diff --git a/src/tasks/tasks01/pad_oracle.rs b/src/tasks/tasks01/pad_oracle.rs index 067a55f..234babf 100644 --- a/src/tasks/tasks01/pad_oracle.rs +++ b/src/tasks/tasks01/pad_oracle.rs @@ -54,7 +54,7 @@ pub fn padding_oracle(args: &Value) -> Result> { // TODO: Collect all and send in one let mut payload: Vec = 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 payload.extend(&attack_counter); attack_counter[i as usize] += 1; @@ -140,7 +140,6 @@ pub fn padding_oracle(args: &Value) -> Result> { #[cfg(test)] mod tests { use super::*; - use serde_json::json; #[test] fn test_connection() -> Result<()> { diff --git a/src/tasks/tasks01/poly2block.rs b/src/tasks/tasks01/poly2block.rs index 6d5ad95..7557981 100644 --- a/src/tasks/tasks01/poly2block.rs +++ b/src/tasks/tasks01/poly2block.rs @@ -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; diff --git a/src/utils/dff.rs b/src/utils/dff.rs index 4cd4b39..21dde69 100644 --- a/src/utils/dff.rs +++ b/src/utils/dff.rs @@ -75,7 +75,7 @@ mod tests { } println!("Result: {:?}", result); - let bit_indices: Vec = vec![0]; + let _bit_indices: Vec = vec![0]; assert!(false) } } diff --git a/src/utils/field.rs b/src/utils/field.rs index 0a06662..739b466 100644 --- a/src/utils/field.rs +++ b/src/utils/field.rs @@ -337,7 +337,6 @@ impl ByteArray { #[cfg(test)] mod tests { use super::*; - use serde_json::json; #[test] fn test_byte_array_shift1() { diff --git a/src/utils/poly.rs b/src/utils/poly.rs index 90ef5e0..f2e7d33 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -788,7 +788,7 @@ mod tests { #[test] fn coeff_to_binary() { let coefficients: Vec = 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()), diff --git a/src/utils/sff.rs b/src/utils/sff.rs index e7a154a..de2bef6 100644 --- a/src/utils/sff.rs +++ b/src/utils/sff.rs @@ -86,7 +86,7 @@ mod tests { } println!("{:?}", result); - let bit_indices: Vec = vec![0]; + let _bit_indices: Vec = vec![0]; assert!(false) } } From 0a573d84da8d0984f4ea5d14a04f27c0f7fb14e9 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Mon, 23 Dec 2024 10:30:21 +0100 Subject: [PATCH 9/9] refactor: Remove unused function --- src/utils/poly.rs | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/src/utils/poly.rs b/src/utils/poly.rs index f2e7d33..6a61853 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -591,39 +591,6 @@ pub fn gfmul(poly_a: &Vec, poly_b: &Vec, semantic: &str) -> Result, poly_b: &Vec, semantic: &str) -> Result> { - 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); - - 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; - } - - Ok(result.to_bytes_le()) -} - pub fn convert_gcm_to_xex(gcm_poly: Vec) -> Result> { let xex_poly = gcm_poly .into_iter()