From bf4c3ee4ca71c928e978aafaca2083cdaa7badf7 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Fri, 29 Nov 2024 19:02:37 +0100 Subject: [PATCH] refactor: Remove unneded prints and change gfmul --- Cargo.toml | 4 ++++ src/utils/ciphers.rs | 37 ------------------------------------- src/utils/edf.rs | 6 ------ src/utils/field.rs | 3 --- src/utils/poly.rs | 31 +++++-------------------------- 5 files changed, 9 insertions(+), 72 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 28fc780..9cf5fa2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,7 @@ replace-with = "vendored-sources" [source.vendored-sources] directory = "vendor" + +[profile.profiling] +inherits = "release" +debug = true diff --git a/src/utils/ciphers.rs b/src/utils/ciphers.rs index 35181a5..82af34b 100644 --- a/src/utils/ciphers.rs +++ b/src/utils/ciphers.rs @@ -119,10 +119,8 @@ pub fn gcm_encrypt_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()); - eprintln!("{:001X?}", nonce); let auth_tag_xor = aes_128_encrypt(&key, &nonce)?; - eprintln!("Y0 {:001X?}", auth_tag_xor); let auth_key_h = aes_128_encrypt(&key, &0u128.to_be_bytes().to_vec())?; @@ -133,8 +131,6 @@ pub fn gcm_encrypt_aes( nonce.drain(12..); nonce.append(counter.to_be_bytes().to_vec().as_mut()); - eprintln!("{:001X?}", nonce); - let inter1 = aes_128_encrypt(&key, &nonce)?; let mut inter2 = xor_bytes(&inter1, chunk.clone())?; @@ -151,7 +147,6 @@ pub fn gcm_encrypt_aes( &ghash(auth_key_h.clone(), ad, ciphertext.clone(), l_field.clone())?, auth_tag_xor, )?; - eprintln!("aes auth tag: {:001X?}", &auth_tag); Ok((ciphertext, auth_tag, l_field, auth_key_h)) } @@ -168,7 +163,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()); - eprintln!("{:001X?}", nonce); let auth_tag_xor = aes_128_encrypt(&key, &nonce)?; @@ -181,8 +175,6 @@ pub fn gcm_decrypt_aes( nonce.drain(12..); nonce.append(counter.to_be_bytes().to_vec().as_mut()); - eprintln!("{:001X?}", nonce); - let inter1 = aes_128_encrypt(&key, &nonce)?; let mut inter2 = xor_bytes(&inter1, chunk.clone())?; @@ -201,7 +193,6 @@ pub fn gcm_decrypt_aes( )?; let valid = auth_tag == tag; - eprintln!("aes auth tag: {:001X?}", auth_tag); Ok((plaintext, valid)) } @@ -217,7 +208,6 @@ pub fn gcm_encrypt_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()); - eprintln!("{:001X?}", nonce); let auth_tag_xor = sea_128_encrypt(&key, &nonce)?; @@ -230,8 +220,6 @@ pub fn gcm_encrypt_sea( nonce.drain(12..); nonce.append(counter.to_be_bytes().to_vec().as_mut()); - eprintln!("{:001X?}", nonce); - let inter1 = sea_128_encrypt(&key, &nonce)?; let mut inter2 = xor_bytes(&inter1, chunk.clone())?; @@ -264,7 +252,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()); - eprintln!("Nonce 1: {:001X?}", nonce); let auth_tag_xor = sea_128_encrypt(&key, &nonce)?; @@ -272,17 +259,11 @@ pub fn gcm_decrypt_sea( let plaintext_chunks: Vec> = ciphertext.chunks(16).map(|x| x.to_vec()).collect(); - eprintln!("{:?}", plaintext_chunks); - counter = 2; for chunk in plaintext_chunks { - eprintln!("Inside loop"); - nonce.drain(12..); nonce.append(counter.to_be_bytes().to_vec().as_mut()); - eprintln!("Nonce 2: {:001X?}", nonce); - let inter1 = sea_128_encrypt(&key, &nonce)?; let mut inter2 = xor_bytes(&inter1, chunk.clone())?; @@ -295,15 +276,11 @@ pub fn gcm_decrypt_sea( let mut c_len: Vec = ((plaintext.len() * 8) as u64).to_be_bytes().to_vec(); l_field.append(c_len.as_mut()); - eprintln!("Ciphertext: {}", BASE64_STANDARD.encode(&ciphertext)); - let auth_tag = xor_bytes( &ghash(auth_key_h.clone(), ad, ciphertext.clone(), l_field.clone())?, auth_tag_xor, )?; - eprintln!("sea dec auth tag: {}", BASE64_STANDARD.encode(&auth_tag)); - let valid = auth_tag == tag; Ok((plaintext, valid)) @@ -317,10 +294,6 @@ pub fn ghash( ) -> Result> { let output: Vec = vec![0; 16]; - eprintln!("{:?}", ad.len() as u8); - eprintln!("{:?}", (ad.len() % 16) as u8); - eprintln!("{:001X?}", ad); - if ad.len() % 16 != 0 || ad.is_empty() { ad.append(vec![0u8; 16 - (ad.len() % 16)].as_mut()); } @@ -329,20 +302,12 @@ pub fn ghash( ciphertext.append(vec![0u8; 16 - (ciphertext.len() % 16)].as_mut()); } - eprintln!("{:001X?}", ad); - eprintln!("{:001X?}", ciphertext); - let mut ad_chunks = ad.chunks(16); - eprintln!("Ad chunks before first next {:001X?}", ad_chunks); - let inter1 = xor_bytes(&output, ad_chunks.next().unwrap().to_vec())?; let mut inter_loop = gfmul(&inter1, &auth_key_h, "gcm")?; - eprintln!("Ad chunks after first next {:001X?}", ad_chunks); for chunk in ad_chunks { - eprintln!("Inside ad chunk loop"); - eprintln!("Ad chunk in loop {:001X?}", chunk); let inter2 = xor_bytes(&inter_loop, chunk.to_vec())?; inter_loop = gfmul(&inter2, &auth_key_h, "gcm")?; } @@ -357,8 +322,6 @@ pub fn ghash( let inter4 = xor_bytes(&inter_loop, l_field)?; inter_loop = gfmul(&inter4, &auth_key_h, "gcm")?; - eprintln!("GHASH auth tag: {:001X?}", inter_loop); - Ok(inter_loop) } diff --git a/src/utils/edf.rs b/src/utils/edf.rs index 80f552c..72f687d 100644 --- a/src/utils/edf.rs +++ b/src/utils/edf.rs @@ -1,12 +1,9 @@ use num::{BigUint, FromPrimitive, One}; use rand::Rng; - use super::poly::{gcd, Polynomial}; pub fn edf(f: Polynomial, d: u32) -> Vec { - eprintln!("Starting edf"); - let q = BigUint::pow(&BigUint::from_u8(2).unwrap(), 128); let n: u32 = (f.degree() as u32) / (d); let mut z: Vec = vec![f.clone()]; @@ -20,7 +17,6 @@ pub fn edf(f: Polynomial, d: u32) -> Vec { //eprintln!("h: {:02X?}", h); let exponent = (q.pow(d) - BigUint::one()) / BigUint::from_u8(3).unwrap(); - eprintln!("q before for {:0X?}", exponent); let g = h.bpow_mod(exponent, &f) + Polynomial::one(); //eprintln!("g before for {:0X?}", g); @@ -32,9 +28,7 @@ pub fn edf(f: Polynomial, d: u32) -> Vec { //eprintln!("Inside if"); let j = gcd(&z[i], &g); - eprintln!("j: {:02X?}", j); if j != one_cmp && j != z[i] { - eprintln!("Working on Z"); let intemediate = z[i].div(&j).0; z.remove(i); z.push(j.clone()); diff --git a/src/utils/field.rs b/src/utils/field.rs index 9b9876b..4876271 100644 --- a/src/utils/field.rs +++ b/src/utils/field.rs @@ -55,15 +55,12 @@ impl FieldElement { let mut result: FieldElement = FieldElement::one(); if exponent == 1 { - eprintln!("special case 1: {:02X?}", self.clone()); - return self; } if exponent == 0 { let result = FieldElement::one(); - eprintln!("Returned value is: {:02X?}", result); return result; } diff --git a/src/utils/poly.rs b/src/utils/poly.rs index 68edb82..3816dac 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -70,8 +70,6 @@ impl Polynomial { }) .collect(); - eprintln!("{:?}", c_array); - for coefficient in c_array { polynomial.push(FieldElement::new( BASE64_STANDARD @@ -97,8 +95,6 @@ impl Polynomial { )]); if exponent == 1 { - eprintln!("special case 1: {:02X?}", self.clone()); - return self; } @@ -107,7 +103,6 @@ impl Polynomial { polynomial_2_block(vec![0], "gcm").unwrap(), )]); - eprintln!("Returned value is: {:02X?}", result); return result; } @@ -159,8 +154,6 @@ impl Polynomial { )]); if exponent == BigUint::one() { - eprintln!("special case 1: {:02X?}", self.clone().div(&modulus).1); - return self.div(&modulus).1; } @@ -169,7 +162,6 @@ impl Polynomial { polynomial_2_block(vec![0], "gcm").unwrap(), )]); - eprintln!("Returned value is: {:02X?}", result); return result; } @@ -213,8 +205,6 @@ impl Polynomial { )]); if exponent == 1 { - eprintln!("special case 1: {:02X?}", self.clone().div(&modulus).1); - return self.div(&modulus).1; } @@ -223,7 +213,6 @@ impl Polynomial { polynomial_2_block(vec![0], "gcm").unwrap(), )]); - eprintln!("Returned value is: {:02X?}", result); return result; } @@ -243,8 +232,6 @@ impl Polynomial { exponent >>= 1; } - eprintln!("result in powmod before reduction: {:02X?}", result); - while !result.polynomial.is_empty() && result .polynomial @@ -257,8 +244,6 @@ impl Polynomial { result.polynomial.pop(); } - eprintln!("result in powmod after reduction: {:02X?}", result); - if result.is_empty() { result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]); } @@ -528,12 +513,6 @@ impl PartialOrd for Polynomial { for (field_a, field_b) in self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) { - eprintln!( - "Poly partord: {:02X?} {:02X?} ", - self.clone().to_c_array(), - other.clone().to_c_array() - ); - match field_a .reverse_bits() .partial_cmp(&field_b.reverse_bits()) @@ -616,10 +595,10 @@ pub fn gfmul(poly_a: &Vec, poly_b: &Vec, semantic: &str) -> Result, poly_b: &Vec, semantic: &str) -> Result, poly_b: &Vec, semantic: &str) -> Result) -> Result> {