Initial MT and div improvements #30

Merged
0xalivecow merged 4 commits from dev into main 2024-11-29 20:14:43 +00:00
5 changed files with 9 additions and 72 deletions
Showing only changes of commit bf4c3ee4ca - Show all commits

View file

@ -18,3 +18,7 @@ replace-with = "vendored-sources"
[source.vendored-sources] [source.vendored-sources]
directory = "vendor" directory = "vendor"
[profile.profiling]
inherits = "release"
debug = true

View file

@ -119,10 +119,8 @@ pub fn gcm_encrypt_aes(
let mut counter: u32 = 1; let mut counter: u32 = 1;
nonce.append(counter.to_be_bytes().to_vec().as_mut()); nonce.append(counter.to_be_bytes().to_vec().as_mut());
//nonce.append(0u8.to_le_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)?; 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())?; 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.drain(12..);
nonce.append(counter.to_be_bytes().to_vec().as_mut()); nonce.append(counter.to_be_bytes().to_vec().as_mut());
eprintln!("{:001X?}", nonce);
let inter1 = aes_128_encrypt(&key, &nonce)?; let inter1 = aes_128_encrypt(&key, &nonce)?;
let mut inter2 = xor_bytes(&inter1, chunk.clone())?; 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())?, &ghash(auth_key_h.clone(), ad, ciphertext.clone(), l_field.clone())?,
auth_tag_xor, auth_tag_xor,
)?; )?;
eprintln!("aes auth tag: {:001X?}", &auth_tag);
Ok((ciphertext, auth_tag, l_field, auth_key_h)) Ok((ciphertext, auth_tag, l_field, auth_key_h))
} }
@ -168,7 +163,6 @@ pub fn gcm_decrypt_aes(
let mut counter: u32 = 1; let mut counter: u32 = 1;
nonce.append(counter.to_be_bytes().to_vec().as_mut()); nonce.append(counter.to_be_bytes().to_vec().as_mut());
//nonce.append(0u8.to_le_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)?; let auth_tag_xor = aes_128_encrypt(&key, &nonce)?;
@ -181,8 +175,6 @@ pub fn gcm_decrypt_aes(
nonce.drain(12..); nonce.drain(12..);
nonce.append(counter.to_be_bytes().to_vec().as_mut()); nonce.append(counter.to_be_bytes().to_vec().as_mut());
eprintln!("{:001X?}", nonce);
let inter1 = aes_128_encrypt(&key, &nonce)?; let inter1 = aes_128_encrypt(&key, &nonce)?;
let mut inter2 = xor_bytes(&inter1, chunk.clone())?; let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
@ -201,7 +193,6 @@ pub fn gcm_decrypt_aes(
)?; )?;
let valid = auth_tag == tag; let valid = auth_tag == tag;
eprintln!("aes auth tag: {:001X?}", auth_tag);
Ok((plaintext, valid)) Ok((plaintext, valid))
} }
@ -217,7 +208,6 @@ pub fn gcm_encrypt_sea(
let mut counter: u32 = 1; let mut counter: u32 = 1;
nonce.append(counter.to_be_bytes().to_vec().as_mut()); nonce.append(counter.to_be_bytes().to_vec().as_mut());
//nonce.append(0u8.to_le_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)?; let auth_tag_xor = sea_128_encrypt(&key, &nonce)?;
@ -230,8 +220,6 @@ pub fn gcm_encrypt_sea(
nonce.drain(12..); nonce.drain(12..);
nonce.append(counter.to_be_bytes().to_vec().as_mut()); nonce.append(counter.to_be_bytes().to_vec().as_mut());
eprintln!("{:001X?}", nonce);
let inter1 = sea_128_encrypt(&key, &nonce)?; let inter1 = sea_128_encrypt(&key, &nonce)?;
let mut inter2 = xor_bytes(&inter1, chunk.clone())?; let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
@ -264,7 +252,6 @@ pub fn gcm_decrypt_sea(
let mut counter: u32 = 1; let mut counter: u32 = 1;
nonce.append(counter.to_be_bytes().to_vec().as_mut()); nonce.append(counter.to_be_bytes().to_vec().as_mut());
//nonce.append(0u8.to_le_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)?; let auth_tag_xor = sea_128_encrypt(&key, &nonce)?;
@ -272,17 +259,11 @@ pub fn gcm_decrypt_sea(
let plaintext_chunks: Vec<Vec<u8>> = ciphertext.chunks(16).map(|x| x.to_vec()).collect(); let plaintext_chunks: Vec<Vec<u8>> = ciphertext.chunks(16).map(|x| x.to_vec()).collect();
eprintln!("{:?}", plaintext_chunks);
counter = 2; counter = 2;
for chunk in plaintext_chunks { for chunk in plaintext_chunks {
eprintln!("Inside loop");
nonce.drain(12..); nonce.drain(12..);
nonce.append(counter.to_be_bytes().to_vec().as_mut()); nonce.append(counter.to_be_bytes().to_vec().as_mut());
eprintln!("Nonce 2: {:001X?}", nonce);
let inter1 = sea_128_encrypt(&key, &nonce)?; let inter1 = sea_128_encrypt(&key, &nonce)?;
let mut inter2 = xor_bytes(&inter1, chunk.clone())?; let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
@ -295,15 +276,11 @@ pub fn gcm_decrypt_sea(
let mut c_len: Vec<u8> = ((plaintext.len() * 8) as u64).to_be_bytes().to_vec(); let mut c_len: Vec<u8> = ((plaintext.len() * 8) as u64).to_be_bytes().to_vec();
l_field.append(c_len.as_mut()); l_field.append(c_len.as_mut());
eprintln!("Ciphertext: {}", BASE64_STANDARD.encode(&ciphertext));
let auth_tag = xor_bytes( let auth_tag = xor_bytes(
&ghash(auth_key_h.clone(), ad, ciphertext.clone(), l_field.clone())?, &ghash(auth_key_h.clone(), ad, ciphertext.clone(), l_field.clone())?,
auth_tag_xor, auth_tag_xor,
)?; )?;
eprintln!("sea dec auth tag: {}", BASE64_STANDARD.encode(&auth_tag));
let valid = auth_tag == tag; let valid = auth_tag == tag;
Ok((plaintext, valid)) Ok((plaintext, valid))
@ -317,10 +294,6 @@ pub fn ghash(
) -> Result<Vec<u8>> { ) -> Result<Vec<u8>> {
let output: Vec<u8> = vec![0; 16]; let output: Vec<u8> = 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() { if ad.len() % 16 != 0 || ad.is_empty() {
ad.append(vec![0u8; 16 - (ad.len() % 16)].as_mut()); 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()); ciphertext.append(vec![0u8; 16 - (ciphertext.len() % 16)].as_mut());
} }
eprintln!("{:001X?}", ad);
eprintln!("{:001X?}", ciphertext);
let mut ad_chunks = ad.chunks(16); 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 inter1 = xor_bytes(&output, ad_chunks.next().unwrap().to_vec())?;
let mut inter_loop = gfmul(&inter1, &auth_key_h, "gcm")?; let mut inter_loop = gfmul(&inter1, &auth_key_h, "gcm")?;
eprintln!("Ad chunks after first next {:001X?}", ad_chunks);
for chunk in 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())?; let inter2 = xor_bytes(&inter_loop, chunk.to_vec())?;
inter_loop = gfmul(&inter2, &auth_key_h, "gcm")?; inter_loop = gfmul(&inter2, &auth_key_h, "gcm")?;
} }
@ -357,8 +322,6 @@ pub fn ghash(
let inter4 = xor_bytes(&inter_loop, l_field)?; let inter4 = xor_bytes(&inter_loop, l_field)?;
inter_loop = gfmul(&inter4, &auth_key_h, "gcm")?; inter_loop = gfmul(&inter4, &auth_key_h, "gcm")?;
eprintln!("GHASH auth tag: {:001X?}", inter_loop);
Ok(inter_loop) Ok(inter_loop)
} }

View file

@ -1,12 +1,9 @@
use num::{BigUint, FromPrimitive, One}; use num::{BigUint, FromPrimitive, One};
use rand::Rng; use rand::Rng;
use super::poly::{gcd, Polynomial}; use super::poly::{gcd, Polynomial};
pub fn edf(f: Polynomial, d: u32) -> Vec<Polynomial> { pub fn edf(f: Polynomial, d: u32) -> Vec<Polynomial> {
eprintln!("Starting edf");
let q = BigUint::pow(&BigUint::from_u8(2).unwrap(), 128); let q = BigUint::pow(&BigUint::from_u8(2).unwrap(), 128);
let n: u32 = (f.degree() as u32) / (d); let n: u32 = (f.degree() as u32) / (d);
let mut z: Vec<Polynomial> = vec![f.clone()]; let mut z: Vec<Polynomial> = vec![f.clone()];
@ -20,7 +17,6 @@ pub fn edf(f: Polynomial, d: u32) -> Vec<Polynomial> {
//eprintln!("h: {:02X?}", h); //eprintln!("h: {:02X?}", h);
let exponent = (q.pow(d) - BigUint::one()) / BigUint::from_u8(3).unwrap(); 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(); let g = h.bpow_mod(exponent, &f) + Polynomial::one();
//eprintln!("g before for {:0X?}", g); //eprintln!("g before for {:0X?}", g);
@ -32,9 +28,7 @@ pub fn edf(f: Polynomial, d: u32) -> Vec<Polynomial> {
//eprintln!("Inside if"); //eprintln!("Inside if");
let j = gcd(&z[i], &g); let j = gcd(&z[i], &g);
eprintln!("j: {:02X?}", j);
if j != one_cmp && j != z[i] { if j != one_cmp && j != z[i] {
eprintln!("Working on Z");
let intemediate = z[i].div(&j).0; let intemediate = z[i].div(&j).0;
z.remove(i); z.remove(i);
z.push(j.clone()); z.push(j.clone());

View file

@ -55,15 +55,12 @@ impl FieldElement {
let mut result: FieldElement = FieldElement::one(); let mut result: FieldElement = FieldElement::one();
if exponent == 1 { if exponent == 1 {
eprintln!("special case 1: {:02X?}", self.clone());
return self; return self;
} }
if exponent == 0 { if exponent == 0 {
let result = FieldElement::one(); let result = FieldElement::one();
eprintln!("Returned value is: {:02X?}", result);
return result; return result;
} }

View file

@ -70,8 +70,6 @@ impl Polynomial {
}) })
.collect(); .collect();
eprintln!("{:?}", c_array);
for coefficient in c_array { for coefficient in c_array {
polynomial.push(FieldElement::new( polynomial.push(FieldElement::new(
BASE64_STANDARD BASE64_STANDARD
@ -97,8 +95,6 @@ impl Polynomial {
)]); )]);
if exponent == 1 { if exponent == 1 {
eprintln!("special case 1: {:02X?}", self.clone());
return self; return self;
} }
@ -107,7 +103,6 @@ impl Polynomial {
polynomial_2_block(vec![0], "gcm").unwrap(), polynomial_2_block(vec![0], "gcm").unwrap(),
)]); )]);
eprintln!("Returned value is: {:02X?}", result);
return result; return result;
} }
@ -159,8 +154,6 @@ impl Polynomial {
)]); )]);
if exponent == BigUint::one() { if exponent == BigUint::one() {
eprintln!("special case 1: {:02X?}", self.clone().div(&modulus).1);
return self.div(&modulus).1; return self.div(&modulus).1;
} }
@ -169,7 +162,6 @@ impl Polynomial {
polynomial_2_block(vec![0], "gcm").unwrap(), polynomial_2_block(vec![0], "gcm").unwrap(),
)]); )]);
eprintln!("Returned value is: {:02X?}", result);
return result; return result;
} }
@ -213,8 +205,6 @@ impl Polynomial {
)]); )]);
if exponent == 1 { if exponent == 1 {
eprintln!("special case 1: {:02X?}", self.clone().div(&modulus).1);
return self.div(&modulus).1; return self.div(&modulus).1;
} }
@ -223,7 +213,6 @@ impl Polynomial {
polynomial_2_block(vec![0], "gcm").unwrap(), polynomial_2_block(vec![0], "gcm").unwrap(),
)]); )]);
eprintln!("Returned value is: {:02X?}", result);
return result; return result;
} }
@ -243,8 +232,6 @@ impl Polynomial {
exponent >>= 1; exponent >>= 1;
} }
eprintln!("result in powmod before reduction: {:02X?}", result);
while !result.polynomial.is_empty() while !result.polynomial.is_empty()
&& result && result
.polynomial .polynomial
@ -257,8 +244,6 @@ impl Polynomial {
result.polynomial.pop(); result.polynomial.pop();
} }
eprintln!("result in powmod after reduction: {:02X?}", result);
if result.is_empty() { if result.is_empty() {
result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]); result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
} }
@ -528,12 +513,6 @@ impl PartialOrd for Polynomial {
for (field_a, field_b) in for (field_a, field_b) in
self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) 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 match field_a
.reverse_bits() .reverse_bits()
.partial_cmp(&field_b.reverse_bits()) .partial_cmp(&field_b.reverse_bits())
@ -616,10 +595,10 @@ pub fn gfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<u
let mut red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec()); let mut red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec());
//red_poly_bytes.0.push(0x01); //red_poly_bytes.0.push(0x01);
let mut poly1: ByteArray = ByteArray(poly_a.to_owned()); let mut poly1: ByteArray = ByteArray(poly_a.to_vec());
//poly1.0.push(0x00); //poly1.0.push(0x00);
let mut poly2: ByteArray = ByteArray(poly_b.to_owned()); let mut poly2: ByteArray = ByteArray(poly_b.to_vec());
//poly2.0.push(0x00); //poly2.0.push(0x00);
if semantic == "gcm" { if semantic == "gcm" {
@ -663,9 +642,9 @@ pub fn bgfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<
0x87, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x01, 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(&reverse_bits_in_bytevec(poly_a.to_owned())); let mut poly1: BigUint = BigUint::from_le_bytes(poly_a);
let mut poly2: BigUint = BigUint::from_le_bytes(&reverse_bits_in_bytevec(poly_b.to_owned())); let mut poly2: BigUint = BigUint::from_le_bytes(poly_b);
/* /*
if semantic == "gcm" { if semantic == "gcm" {
@ -701,7 +680,7 @@ pub fn bgfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<
} }
*/ */
Ok(reverse_bits_in_bytevec(result.to_bytes_le())) Ok(result.to_bytes_le())
} }
pub fn convert_gcm_to_xex(gcm_poly: Vec<u8>) -> Result<Vec<u8>> { pub fn convert_gcm_to_xex(gcm_poly: Vec<u8>) -> Result<Vec<u8>> {