From 6520a36e14c603cdadb8d836240e8a1b46e37dd1 Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Sat, 27 Apr 2024 18:52:43 +0200 Subject: [PATCH] find round key test --- src/aes.rs | 19 +++++++- src/main.rs | 131 ---------------------------------------------------- 2 files changed, 18 insertions(+), 132 deletions(-) diff --git a/src/aes.rs b/src/aes.rs index 3f34cbf..4229a0f 100644 --- a/src/aes.rs +++ b/src/aes.rs @@ -523,7 +523,6 @@ impl Aes { } } found_key - } } @@ -620,4 +619,22 @@ mod tests { ]; assert_eq!(cleartext, expected_cleartext); } + #[test] + fn findroundkey_test() { + let key: [u8; 16] = [ + 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, + 0x4f, 0x3c, + ]; + let nturn = 4; // doesn't matter for this test + let aescipher = Aes::new(&key, &nturn); + let expected: [u8; 16] = aescipher.expanded_key[16..20] // 5th key (1st is the pre_whitenning, 2nd, 3rd and 4th the 3 previous round key) + .iter() + .flat_map(|subarray| subarray.iter()) + .copied() + .collect::>() + .try_into() + .unwrap(); + let found_key: [u8; 16] = Aes::findroundkey(&key); + assert_eq!(found_key, expected); + } } diff --git a/src/main.rs b/src/main.rs index dc557ab..bdcc1df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,135 +15,4 @@ fn main() { // a8525b7f // b671253b // db0bad00 - - // let aescipher = Aes::new(&key, &4); - // let expanded_key = Aes::key_schedule(&key); - // for i in 0..10 { - // println!("Block {}", i + 1); - // for j in 0..4 { - // let index = i * 4 + j; - // print!(" "); - // for &byte in &expanded_key[index] { - // print!("{:02x}", byte); - // } - // println!(); - // } - // } - // let cleartext: [u8; 16] = [ - // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - // 0x00, - // ]; - // let ciphertext: [u8; 16] = Aes::aes_reduced(&key, &cleartext); - // - // let mut ciphertexts = Aes::aes_reduced_gen_texts(&key, &0x00); - // let mut S = 0x00; - // - // - // - // let mut matrix: [[u8; 4]; 4] = [[0; 4]; 4]; - // let mut array: [u8; 16] = [0; 16]; - // - // for j in 0..16 { - // for i in 0..=255 { - // S = 0x00; - // for ciphertext in ciphertexts { - // for i in 0..4 { - // for j in 0..4 { - // matrix[j][i] = ciphertext[j * 4 + i]; - // } - // } - // // aes::inverse_shift_rows(&mut matrix); - // for i in 0..4 { - // for j in 0..4 { - // array[j * 4 + i] = matrix[j][i]; - // } - // } - // S ^= aes::substitute(array[j]^ i as u8, false); - // } - // if S == 0x00 { - // println!("found: {:02x}", i); - // } - // } - // println!(); - // } - // println!("part 2"); - // ciphertexts = Aes::aes_reduced_gen_texts(&key, &0xff); - // - // let mut matrix: [[u8; 4]; 4] = [[0; 4]; 4]; - // let mut array: [u8; 16] = [0; 16]; - // - // for j in 0..16 { - // for i in 0..=255 { - // S = 0x00; - // for ciphertext in ciphertexts { - // for k in 0..4 { - // for m in 0..4 { - // matrix[k][m] = ciphertext[k * 4 + m]; - // } - // } - // // aes::inverse_shift_rows(&mut matrix); - // for i in 0..4 { - // for j in 0..4 { - // array[j * 4 + i] = matrix[j][i]; - // } - // } - // S ^= aes::substitute(array[j]^ i as u8, false); - // } - // if S == 0x00 { - // println!("found: {:02x}", i); - // } - // } - // println!(); - // } - // println!("part 3"); - // ciphertexts = Aes::aes_reduced_gen_texts(&key, &0xff); - // - // let mut matrix: [[u8; 4]; 4] = [[0; 4]; 4]; - // let mut array: [u8; 16] = [0; 16]; - // - // let mut ciphertext2: [[u8;16]; 256] = [[0; 16]; 256]; - // S = 0x00; - // for i in 0..256 { - // ciphertext2[i] = aescipher.decrypt_block_reduced_1_step(&ciphertexts[i]); - // // for &byte in &aescipher.decrypt_block(&ciphertexts[i]) { - // // print!("{:02x}", byte); - // // } - // // println!(); - // - // } - // for ciphertext in ciphertext2 { - // S ^= ciphertext[0]; - // } - // if S == 0x00 { - // println!("should be: {:02x}", S); - // } - // println!(); - - // 0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c, WHITENING 0;4 - // 0xa0fafe17, 0x88542cb1, 0x23a33939, 0x2a6c7605, ROUND 1 4;8 - // 0xf2c295f2, 0x7a96b943, 0x5935807a, 0x7359f67f, ROUND 2 8;12 - // 0x3d80477d, 0x4716fe3e, 0x1e237e44, 0x6d7a883b, ROUND 3 12;16 - // 0xef44a541, 0xa8525b7f, 0xb671253b, 0xdb0bad00, ROUND 4 16;20 - // 0xd4d1c6f8, 0x7c839d87, 0xcaf2b8bc, 0x11f915bc, - // 0x6d88a37a, 0x110b3efd, 0xdbf98641, 0xca0093fd, - // 0x4e54f70e, 0x5f5fc9f3, 0x84a64fb2, 0x4ea6dc4f, - // 0xead27321, 0xb58dbad2, 0x312bf560, 0x7f8d292f, - // 0xac7766f3, 0x19fadc21, 0x28d12941, 0x575c006e, - // 0xd014f9a8, 0xc9ee2589, 0xe13f0cc8, 0xb6630ca6, - - // for ciphertext in ciphertexts { - // S = S ^ aes::substitute(ciphertext[0] ^ 0x4b, false); - // for elem in ciphertext { - // print!("{:02x}", elem); - // } - // println!(); - // } - // println!("should be: {:02x}", S); - - // ef44a541 - // a8525b7f - // b671253b - // db0bad00 - - }