more test

This commit is contained in:
Sam Hadow 2024-04-26 17:03:03 +02:00
parent 5a910a3a07
commit 7de7e08429

View File

@ -160,7 +160,7 @@ fn xor(bits1: &[u8; 4], bits2: &[u8; 4]) -> [u8; 4] {
result result
} }
fn substitute_bytes(bytes: &[u8; 4]) -> [u8; 4] { fn substitute_word(bytes: &[u8; 4]) -> [u8; 4] {
let mut result = [0u8; 4]; let mut result = [0u8; 4];
for i in 0..4 { for i in 0..4 {
result[i] = substitute(bytes[i], true); result[i] = substitute(bytes[i], true);
@ -194,7 +194,7 @@ fn substitute(byte: u8, encryption: bool) -> u8 {
} }
} }
fn shift_bytes(bytes: &[u8; 4]) -> [u8; 4] { fn shift_word(bytes: &[u8; 4]) -> [u8; 4] {
let mut result = [0u8; 4]; let mut result = [0u8; 4];
for i in 0..4 { for i in 0..4 {
result[i] = bytes[(i + 1) % 4]; result[i] = bytes[(i + 1) % 4];
@ -268,7 +268,7 @@ impl Aes {
expanded_key[i] = xor( expanded_key[i] = xor(
&xor( &xor(
&expanded_key[i - n], &expanded_key[i - n],
&substitute_bytes(&shift_bytes(&expanded_key[i - 1])), &substitute_word(&shift_word(&expanded_key[i - 1])),
), ),
&rcon, &rcon,
); );
@ -285,6 +285,29 @@ impl Aes {
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn shift_word_test() {
let test: u32 = 0x09cf4f3c;
let test_bytes: [u8; 4] = test.to_be_bytes();
let expected: u32 = 0xcf4f3c09;
let expected_bytes: [u8; 4] = expected.to_be_bytes();
assert_eq!(shift_word(&test_bytes), expected_bytes);
}
#[test]
fn substitute_word_test() {
let test: u32 = 0xcf4f3c09;
let test_bytes: [u8; 4] = test.to_be_bytes();
let expected: u32 = 0x8a84eb01;
let expected_bytes: [u8; 4] = expected.to_be_bytes();
assert_eq!(substitute_word(&test_bytes), expected_bytes);
}
#[test]
fn xor_test() {
let w1: [u8; 4] = [0x00, 0x11, 0x10, 0x10];
let w2: [u8; 4] = [0x00, 0x11, 0x01, 0x10];
let expected: [u8; 4] = [0x00, 0x00, 0x11, 0x00];
assert_eq!(xor(&w1, &w2), expected);
}
#[test]
fn key_schedule_test() { fn key_schedule_test() {
let key: [u8; 16] = [ let key: [u8; 16] = [
0x2b, 0x7e, 0x15, 0x16, 0x2b, 0x7e, 0x15, 0x16,