From 41e9a70977418a50f90c3b16e6b9b63a1fcac8a9 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 12:22:27 +0800 Subject: [PATCH] aes-test: move low_level_tests to their own mod The low-level tests are useful in validating that the hardware works, however they generally are not useful. Split them into their own module. Signed-off-by: Sean Cross --- services/aes-test/Cargo.toml | 1 + services/aes-test/src/aes.rs | 261 ++++++++---------- services/aes-test/src/low_level_tests.rs | 201 ++++++++++++++ .../src/{ => low_level_tests}/reference.rs | 0 services/aes-test/src/main.rs | 201 +------------- 5 files changed, 333 insertions(+), 331 deletions(-) create mode 100644 services/aes-test/src/low_level_tests.rs rename services/aes-test/src/{ => low_level_tests}/reference.rs (100%) diff --git a/services/aes-test/Cargo.toml b/services/aes-test/Cargo.toml index 2b590954d..91b8eaad3 100644 --- a/services/aes-test/Cargo.toml +++ b/services/aes-test/Cargo.toml @@ -12,3 +12,4 @@ xous = {path = "../../xous-rs"} [features] default = [] +low_level_tests = [] diff --git a/services/aes-test/src/aes.rs b/services/aes-test/src/aes.rs index 0df33ff41..8f580f716 100644 --- a/services/aes-test/src/aes.rs +++ b/services/aes-test/src/aes.rs @@ -277,25 +277,6 @@ const TD3: [u32; 256] = [ 0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0, ]; -const TD4: [u8; 256] = [ - 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, - 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, - 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, - 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, - 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, - 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, - 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, - 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, - 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, - 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, - 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, - 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, - 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, - 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, - 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, - 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d, -]; - const RCON: [u32; 10] = [ 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1B000000, @@ -303,11 +284,11 @@ const RCON: [u32; 10] = [ ]; #[derive(Clone, Copy, Debug)] -enum AesId { - AesId0 = 0, - AesId1 = 1, - AesId2 = 2, - AesId3 = 3, +enum AesByte { + Byte0 = 0, + Byte1 = 1, + Byte2 = 2, + Byte3 = 3, } #[derive(Debug)] @@ -326,7 +307,7 @@ impl Default for Aes { } } -fn aes_enc_round(arg1: u32, arg2: u32, id: AesId) -> u32 { +fn aes_enc_round(arg1: u32, arg2: u32, id: AesByte) -> u32 { extern "C" { fn vex_aes_enc_id_0(arg1: u32, arg2: u32) -> u32; fn vex_aes_enc_id_1(arg1: u32, arg2: u32) -> u32; @@ -334,14 +315,14 @@ fn aes_enc_round(arg1: u32, arg2: u32, id: AesId) -> u32 { fn vex_aes_enc_id_3(arg1: u32, arg2: u32) -> u32; } match id { - AesId::AesId0 => unsafe { vex_aes_enc_id_0(arg1, arg2) }, - AesId::AesId1 => unsafe { vex_aes_enc_id_1(arg1, arg2) }, - AesId::AesId2 => unsafe { vex_aes_enc_id_2(arg1, arg2) }, - AesId::AesId3 => unsafe { vex_aes_enc_id_3(arg1, arg2) }, + AesByte::Byte0 => unsafe { vex_aes_enc_id_0(arg1, arg2) }, + AesByte::Byte1 => unsafe { vex_aes_enc_id_1(arg1, arg2) }, + AesByte::Byte2 => unsafe { vex_aes_enc_id_2(arg1, arg2) }, + AesByte::Byte3 => unsafe { vex_aes_enc_id_3(arg1, arg2) }, } } -fn aes_enc_round_last(arg1: u32, arg2: u32, id: AesId) -> u32 { +fn aes_enc_round_last(arg1: u32, arg2: u32, id: AesByte) -> u32 { extern "C" { fn vex_aes_enc_id_last_0(arg1: u32, arg2: u32) -> u32; fn vex_aes_enc_id_last_1(arg1: u32, arg2: u32) -> u32; @@ -349,14 +330,14 @@ fn aes_enc_round_last(arg1: u32, arg2: u32, id: AesId) -> u32 { fn vex_aes_enc_id_last_3(arg1: u32, arg2: u32) -> u32; } match id { - AesId::AesId0 => unsafe { vex_aes_enc_id_last_0(arg1, arg2) }, - AesId::AesId1 => unsafe { vex_aes_enc_id_last_1(arg1, arg2) }, - AesId::AesId2 => unsafe { vex_aes_enc_id_last_2(arg1, arg2) }, - AesId::AesId3 => unsafe { vex_aes_enc_id_last_3(arg1, arg2) }, + AesByte::Byte0 => unsafe { vex_aes_enc_id_last_0(arg1, arg2) }, + AesByte::Byte1 => unsafe { vex_aes_enc_id_last_1(arg1, arg2) }, + AesByte::Byte2 => unsafe { vex_aes_enc_id_last_2(arg1, arg2) }, + AesByte::Byte3 => unsafe { vex_aes_enc_id_last_3(arg1, arg2) }, } } -fn aes_dec_round(arg1: u32, arg2: u32, id: AesId) -> u32 { +fn aes_dec_round(arg1: u32, arg2: u32, id: AesByte) -> u32 { extern "C" { fn vex_aes_dec_id_0(arg1: u32, arg2: u32) -> u32; fn vex_aes_dec_id_1(arg1: u32, arg2: u32) -> u32; @@ -364,14 +345,14 @@ fn aes_dec_round(arg1: u32, arg2: u32, id: AesId) -> u32 { fn vex_aes_dec_id_3(arg1: u32, arg2: u32) -> u32; } match id { - AesId::AesId0 => unsafe { vex_aes_dec_id_0(arg1, arg2) }, - AesId::AesId1 => unsafe { vex_aes_dec_id_1(arg1, arg2) }, - AesId::AesId2 => unsafe { vex_aes_dec_id_2(arg1, arg2) }, - AesId::AesId3 => unsafe { vex_aes_dec_id_3(arg1, arg2) }, + AesByte::Byte0 => unsafe { vex_aes_dec_id_0(arg1, arg2) }, + AesByte::Byte1 => unsafe { vex_aes_dec_id_1(arg1, arg2) }, + AesByte::Byte2 => unsafe { vex_aes_dec_id_2(arg1, arg2) }, + AesByte::Byte3 => unsafe { vex_aes_dec_id_3(arg1, arg2) }, } } -fn aes_dec_round_last(arg1: u32, arg2: u32, id: AesId) -> u32 { +fn aes_dec_round_last(arg1: u32, arg2: u32, id: AesByte) -> u32 { extern "C" { fn vex_aes_dec_id_last_0(arg1: u32, arg2: u32) -> u32; fn vex_aes_dec_id_last_1(arg1: u32, arg2: u32) -> u32; @@ -379,10 +360,10 @@ fn aes_dec_round_last(arg1: u32, arg2: u32, id: AesId) -> u32 { fn vex_aes_dec_id_last_3(arg1: u32, arg2: u32) -> u32; } match id { - AesId::AesId0 => unsafe { vex_aes_dec_id_last_0(arg1, arg2) }, - AesId::AesId1 => unsafe { vex_aes_dec_id_last_1(arg1, arg2) }, - AesId::AesId2 => unsafe { vex_aes_dec_id_last_2(arg1, arg2) }, - AesId::AesId3 => unsafe { vex_aes_dec_id_last_3(arg1, arg2) }, + AesByte::Byte0 => unsafe { vex_aes_dec_id_last_0(arg1, arg2) }, + AesByte::Byte1 => unsafe { vex_aes_dec_id_last_1(arg1, arg2) }, + AesByte::Byte2 => unsafe { vex_aes_dec_id_last_2(arg1, arg2) }, + AesByte::Byte3 => unsafe { vex_aes_dec_id_last_3(arg1, arg2) }, } } @@ -614,25 +595,25 @@ pub fn vexriscv_aes_encrypt(input: &[u8], output: &mut [u8], key: &Aes) { t2 = rk[rk_offset + 6]; t3 = rk[rk_offset + 7]; - t0 = aes_enc_round(t0, s0, AesId::AesId0); - t1 = aes_enc_round(t1, s1, AesId::AesId0); - t2 = aes_enc_round(t2, s2, AesId::AesId0); - t3 = aes_enc_round(t3, s3, AesId::AesId0); + t0 = aes_enc_round(t0, s0, AesByte::Byte0); + t1 = aes_enc_round(t1, s1, AesByte::Byte0); + t2 = aes_enc_round(t2, s2, AesByte::Byte0); + t3 = aes_enc_round(t3, s3, AesByte::Byte0); - t0 = aes_enc_round(t0, s1, AesId::AesId1); - t1 = aes_enc_round(t1, s2, AesId::AesId1); - t2 = aes_enc_round(t2, s3, AesId::AesId1); - t3 = aes_enc_round(t3, s0, AesId::AesId1); + t0 = aes_enc_round(t0, s1, AesByte::Byte1); + t1 = aes_enc_round(t1, s2, AesByte::Byte1); + t2 = aes_enc_round(t2, s3, AesByte::Byte1); + t3 = aes_enc_round(t3, s0, AesByte::Byte1); - t0 = aes_enc_round(t0, s2, AesId::AesId2); - t1 = aes_enc_round(t1, s3, AesId::AesId2); - t2 = aes_enc_round(t2, s0, AesId::AesId2); - t3 = aes_enc_round(t3, s1, AesId::AesId2); + t0 = aes_enc_round(t0, s2, AesByte::Byte2); + t1 = aes_enc_round(t1, s3, AesByte::Byte2); + t2 = aes_enc_round(t2, s0, AesByte::Byte2); + t3 = aes_enc_round(t3, s1, AesByte::Byte2); - t0 = aes_enc_round(t0, s3, AesId::AesId3); - t1 = aes_enc_round(t1, s0, AesId::AesId3); - t2 = aes_enc_round(t2, s1, AesId::AesId3); - t3 = aes_enc_round(t3, s2, AesId::AesId3); + t0 = aes_enc_round(t0, s3, AesByte::Byte3); + t1 = aes_enc_round(t1, s0, AesByte::Byte3); + t2 = aes_enc_round(t2, s1, AesByte::Byte3); + t3 = aes_enc_round(t3, s2, AesByte::Byte3); rk_offset += 8; round_count -= 1; @@ -645,25 +626,25 @@ pub fn vexriscv_aes_encrypt(input: &[u8], output: &mut [u8], key: &Aes) { s2 = rk[rk_offset + 2]; s3 = rk[rk_offset + 3]; - s0 = aes_enc_round(s0, t0, AesId::AesId0); - s1 = aes_enc_round(s1, t1, AesId::AesId0); - s2 = aes_enc_round(s2, t2, AesId::AesId0); - s3 = aes_enc_round(s3, t3, AesId::AesId0); + s0 = aes_enc_round(s0, t0, AesByte::Byte0); + s1 = aes_enc_round(s1, t1, AesByte::Byte0); + s2 = aes_enc_round(s2, t2, AesByte::Byte0); + s3 = aes_enc_round(s3, t3, AesByte::Byte0); - s0 = aes_enc_round(s0, t1, AesId::AesId1); - s1 = aes_enc_round(s1, t2, AesId::AesId1); - s2 = aes_enc_round(s2, t3, AesId::AesId1); - s3 = aes_enc_round(s3, t0, AesId::AesId1); + s0 = aes_enc_round(s0, t1, AesByte::Byte1); + s1 = aes_enc_round(s1, t2, AesByte::Byte1); + s2 = aes_enc_round(s2, t3, AesByte::Byte1); + s3 = aes_enc_round(s3, t0, AesByte::Byte1); - s0 = aes_enc_round(s0, t2, AesId::AesId2); - s1 = aes_enc_round(s1, t3, AesId::AesId2); - s2 = aes_enc_round(s2, t0, AesId::AesId2); - s3 = aes_enc_round(s3, t1, AesId::AesId2); + s0 = aes_enc_round(s0, t2, AesByte::Byte2); + s1 = aes_enc_round(s1, t3, AesByte::Byte2); + s2 = aes_enc_round(s2, t0, AesByte::Byte2); + s3 = aes_enc_round(s3, t1, AesByte::Byte2); - s0 = aes_enc_round(s0, t3, AesId::AesId3); - s1 = aes_enc_round(s1, t0, AesId::AesId3); - s2 = aes_enc_round(s2, t1, AesId::AesId3); - s3 = aes_enc_round(s3, t2, AesId::AesId3); + s0 = aes_enc_round(s0, t3, AesByte::Byte3); + s1 = aes_enc_round(s1, t0, AesByte::Byte3); + s2 = aes_enc_round(s2, t1, AesByte::Byte3); + s3 = aes_enc_round(s3, t2, AesByte::Byte3); } s0 = rk[rk_offset + 0]; @@ -671,25 +652,25 @@ pub fn vexriscv_aes_encrypt(input: &[u8], output: &mut [u8], key: &Aes) { s2 = rk[rk_offset + 2]; s3 = rk[rk_offset + 3]; - s0 = aes_enc_round_last(s0, t0, AesId::AesId0); - s1 = aes_enc_round_last(s1, t1, AesId::AesId0); - s2 = aes_enc_round_last(s2, t2, AesId::AesId0); - s3 = aes_enc_round_last(s3, t3, AesId::AesId0); + s0 = aes_enc_round_last(s0, t0, AesByte::Byte0); + s1 = aes_enc_round_last(s1, t1, AesByte::Byte0); + s2 = aes_enc_round_last(s2, t2, AesByte::Byte0); + s3 = aes_enc_round_last(s3, t3, AesByte::Byte0); - s0 = aes_enc_round_last(s0, t1, AesId::AesId1); - s1 = aes_enc_round_last(s1, t2, AesId::AesId1); - s2 = aes_enc_round_last(s2, t3, AesId::AesId1); - s3 = aes_enc_round_last(s3, t0, AesId::AesId1); + s0 = aes_enc_round_last(s0, t1, AesByte::Byte1); + s1 = aes_enc_round_last(s1, t2, AesByte::Byte1); + s2 = aes_enc_round_last(s2, t3, AesByte::Byte1); + s3 = aes_enc_round_last(s3, t0, AesByte::Byte1); - s0 = aes_enc_round_last(s0, t2, AesId::AesId2); - s1 = aes_enc_round_last(s1, t3, AesId::AesId2); - s2 = aes_enc_round_last(s2, t0, AesId::AesId2); - s3 = aes_enc_round_last(s3, t1, AesId::AesId2); + s0 = aes_enc_round_last(s0, t2, AesByte::Byte2); + s1 = aes_enc_round_last(s1, t3, AesByte::Byte2); + s2 = aes_enc_round_last(s2, t0, AesByte::Byte2); + s3 = aes_enc_round_last(s3, t1, AesByte::Byte2); - s0 = aes_enc_round_last(s0, t3, AesId::AesId3); - s1 = aes_enc_round_last(s1, t0, AesId::AesId3); - s2 = aes_enc_round_last(s2, t1, AesId::AesId3); - s3 = aes_enc_round_last(s3, t2, AesId::AesId3); + s0 = aes_enc_round_last(s0, t3, AesByte::Byte3); + s1 = aes_enc_round_last(s1, t0, AesByte::Byte3); + s2 = aes_enc_round_last(s2, t1, AesByte::Byte3); + s3 = aes_enc_round_last(s3, t2, AesByte::Byte3); set_u32(output, 0, s0); set_u32(output, 4, s1); @@ -725,25 +706,25 @@ pub fn vexriscv_aes_decrypt(input: &[u8], output: &mut [u8], key: &Aes) { t2 = rk[rk_offset + 6]; t3 = rk[rk_offset + 7]; - t0 = aes_dec_round(t0, s0, AesId::AesId0); - t1 = aes_dec_round(t1, s1, AesId::AesId0); - t2 = aes_dec_round(t2, s2, AesId::AesId0); - t3 = aes_dec_round(t3, s3, AesId::AesId0); + t0 = aes_dec_round(t0, s0, AesByte::Byte0); + t1 = aes_dec_round(t1, s1, AesByte::Byte0); + t2 = aes_dec_round(t2, s2, AesByte::Byte0); + t3 = aes_dec_round(t3, s3, AesByte::Byte0); - t0 = aes_dec_round(t0, s3, AesId::AesId1); - t1 = aes_dec_round(t1, s0, AesId::AesId1); - t2 = aes_dec_round(t2, s1, AesId::AesId1); - t3 = aes_dec_round(t3, s2, AesId::AesId1); + t0 = aes_dec_round(t0, s3, AesByte::Byte1); + t1 = aes_dec_round(t1, s0, AesByte::Byte1); + t2 = aes_dec_round(t2, s1, AesByte::Byte1); + t3 = aes_dec_round(t3, s2, AesByte::Byte1); - t0 = aes_dec_round(t0, s2, AesId::AesId2); - t1 = aes_dec_round(t1, s3, AesId::AesId2); - t2 = aes_dec_round(t2, s0, AesId::AesId2); - t3 = aes_dec_round(t3, s1, AesId::AesId2); + t0 = aes_dec_round(t0, s2, AesByte::Byte2); + t1 = aes_dec_round(t1, s3, AesByte::Byte2); + t2 = aes_dec_round(t2, s0, AesByte::Byte2); + t3 = aes_dec_round(t3, s1, AesByte::Byte2); - t0 = aes_dec_round(t0, s1, AesId::AesId3); - t1 = aes_dec_round(t1, s2, AesId::AesId3); - t2 = aes_dec_round(t2, s3, AesId::AesId3); - t3 = aes_dec_round(t3, s0, AesId::AesId3); + t0 = aes_dec_round(t0, s1, AesByte::Byte3); + t1 = aes_dec_round(t1, s2, AesByte::Byte3); + t2 = aes_dec_round(t2, s3, AesByte::Byte3); + t3 = aes_dec_round(t3, s0, AesByte::Byte3); rk_offset += 8; round_count -= 1; @@ -756,25 +737,25 @@ pub fn vexriscv_aes_decrypt(input: &[u8], output: &mut [u8], key: &Aes) { s2 = rk[rk_offset + 2]; s3 = rk[rk_offset + 3]; - s0 = aes_dec_round(s0, t0, AesId::AesId0); - s1 = aes_dec_round(s1, t1, AesId::AesId0); - s2 = aes_dec_round(s2, t2, AesId::AesId0); - s3 = aes_dec_round(s3, t3, AesId::AesId0); + s0 = aes_dec_round(s0, t0, AesByte::Byte0); + s1 = aes_dec_round(s1, t1, AesByte::Byte0); + s2 = aes_dec_round(s2, t2, AesByte::Byte0); + s3 = aes_dec_round(s3, t3, AesByte::Byte0); - s0 = aes_dec_round(s0, t3, AesId::AesId1); - s1 = aes_dec_round(s1, t0, AesId::AesId1); - s2 = aes_dec_round(s2, t1, AesId::AesId1); - s3 = aes_dec_round(s3, t2, AesId::AesId1); + s0 = aes_dec_round(s0, t3, AesByte::Byte1); + s1 = aes_dec_round(s1, t0, AesByte::Byte1); + s2 = aes_dec_round(s2, t1, AesByte::Byte1); + s3 = aes_dec_round(s3, t2, AesByte::Byte1); - s0 = aes_dec_round(s0, t2, AesId::AesId2); - s1 = aes_dec_round(s1, t3, AesId::AesId2); - s2 = aes_dec_round(s2, t0, AesId::AesId2); - s3 = aes_dec_round(s3, t1, AesId::AesId2); + s0 = aes_dec_round(s0, t2, AesByte::Byte2); + s1 = aes_dec_round(s1, t3, AesByte::Byte2); + s2 = aes_dec_round(s2, t0, AesByte::Byte2); + s3 = aes_dec_round(s3, t1, AesByte::Byte2); - s0 = aes_dec_round(s0, t1, AesId::AesId3); - s1 = aes_dec_round(s1, t2, AesId::AesId3); - s2 = aes_dec_round(s2, t3, AesId::AesId3); - s3 = aes_dec_round(s3, t0, AesId::AesId3); + s0 = aes_dec_round(s0, t1, AesByte::Byte3); + s1 = aes_dec_round(s1, t2, AesByte::Byte3); + s2 = aes_dec_round(s2, t3, AesByte::Byte3); + s3 = aes_dec_round(s3, t0, AesByte::Byte3); } s0 = rk[rk_offset + 0]; @@ -782,25 +763,25 @@ pub fn vexriscv_aes_decrypt(input: &[u8], output: &mut [u8], key: &Aes) { s2 = rk[rk_offset + 2]; s3 = rk[rk_offset + 3]; - s0 = aes_dec_round_last(s0, t0, AesId::AesId0); - s1 = aes_dec_round_last(s1, t1, AesId::AesId0); - s2 = aes_dec_round_last(s2, t2, AesId::AesId0); - s3 = aes_dec_round_last(s3, t3, AesId::AesId0); + s0 = aes_dec_round_last(s0, t0, AesByte::Byte0); + s1 = aes_dec_round_last(s1, t1, AesByte::Byte0); + s2 = aes_dec_round_last(s2, t2, AesByte::Byte0); + s3 = aes_dec_round_last(s3, t3, AesByte::Byte0); - s0 = aes_dec_round_last(s0, t3, AesId::AesId1); - s1 = aes_dec_round_last(s1, t0, AesId::AesId1); - s2 = aes_dec_round_last(s2, t1, AesId::AesId1); - s3 = aes_dec_round_last(s3, t2, AesId::AesId1); + s0 = aes_dec_round_last(s0, t3, AesByte::Byte1); + s1 = aes_dec_round_last(s1, t0, AesByte::Byte1); + s2 = aes_dec_round_last(s2, t1, AesByte::Byte1); + s3 = aes_dec_round_last(s3, t2, AesByte::Byte1); - s0 = aes_dec_round_last(s0, t2, AesId::AesId2); - s1 = aes_dec_round_last(s1, t3, AesId::AesId2); - s2 = aes_dec_round_last(s2, t0, AesId::AesId2); - s3 = aes_dec_round_last(s3, t1, AesId::AesId2); + s0 = aes_dec_round_last(s0, t2, AesByte::Byte2); + s1 = aes_dec_round_last(s1, t3, AesByte::Byte2); + s2 = aes_dec_round_last(s2, t0, AesByte::Byte2); + s3 = aes_dec_round_last(s3, t1, AesByte::Byte2); - s0 = aes_dec_round_last(s0, t1, AesId::AesId3); - s1 = aes_dec_round_last(s1, t2, AesId::AesId3); - s2 = aes_dec_round_last(s2, t3, AesId::AesId3); - s3 = aes_dec_round_last(s3, t0, AesId::AesId3); + s0 = aes_dec_round_last(s0, t1, AesByte::Byte3); + s1 = aes_dec_round_last(s1, t2, AesByte::Byte3); + s2 = aes_dec_round_last(s2, t3, AesByte::Byte3); + s3 = aes_dec_round_last(s3, t0, AesByte::Byte3); set_u32(output, 0, s0); set_u32(output, 4, s1); diff --git a/services/aes-test/src/low_level_tests.rs b/services/aes-test/src/low_level_tests.rs new file mode 100644 index 000000000..7b511666b --- /dev/null +++ b/services/aes-test/src/low_level_tests.rs @@ -0,0 +1,201 @@ +mod reference; + +#[derive(Clone, Copy, Debug)] +enum AesId { + AesId0 = 0, + AesId1 = 1, + AesId2 = 2, + AesId3 = 3, +} + +fn aes_enc_round(arg1: u32, arg2: u32, id: AesId) -> u32 { + extern "C" { + fn vex_aes_enc_id_0(arg1: u32, arg2: u32) -> u32; + fn vex_aes_enc_id_1(arg1: u32, arg2: u32) -> u32; + fn vex_aes_enc_id_2(arg1: u32, arg2: u32) -> u32; + fn vex_aes_enc_id_3(arg1: u32, arg2: u32) -> u32; + } + match id { + AesId::AesId0 => unsafe { vex_aes_enc_id_0(arg1, arg2) }, + AesId::AesId1 => unsafe { vex_aes_enc_id_1(arg1, arg2) }, + AesId::AesId2 => unsafe { vex_aes_enc_id_2(arg1, arg2) }, + AesId::AesId3 => unsafe { vex_aes_enc_id_3(arg1, arg2) }, + } +} + +fn aes_enc_round_last(arg1: u32, arg2: u32, id: AesId) -> u32 { + extern "C" { + fn vex_aes_enc_id_last_0(arg1: u32, arg2: u32) -> u32; + fn vex_aes_enc_id_last_1(arg1: u32, arg2: u32) -> u32; + fn vex_aes_enc_id_last_2(arg1: u32, arg2: u32) -> u32; + fn vex_aes_enc_id_last_3(arg1: u32, arg2: u32) -> u32; + } + match id { + AesId::AesId0 => unsafe { vex_aes_enc_id_last_0(arg1, arg2) }, + AesId::AesId1 => unsafe { vex_aes_enc_id_last_1(arg1, arg2) }, + AesId::AesId2 => unsafe { vex_aes_enc_id_last_2(arg1, arg2) }, + AesId::AesId3 => unsafe { vex_aes_enc_id_last_3(arg1, arg2) }, + } +} + +fn aes_dec_round(arg1: u32, arg2: u32, id: AesId) -> u32 { + extern "C" { + fn vex_aes_dec_id_0(arg1: u32, arg2: u32) -> u32; + fn vex_aes_dec_id_1(arg1: u32, arg2: u32) -> u32; + fn vex_aes_dec_id_2(arg1: u32, arg2: u32) -> u32; + fn vex_aes_dec_id_3(arg1: u32, arg2: u32) -> u32; + } + match id { + AesId::AesId0 => unsafe { vex_aes_dec_id_0(arg1, arg2) }, + AesId::AesId1 => unsafe { vex_aes_dec_id_1(arg1, arg2) }, + AesId::AesId2 => unsafe { vex_aes_dec_id_2(arg1, arg2) }, + AesId::AesId3 => unsafe { vex_aes_dec_id_3(arg1, arg2) }, + } +} + +fn aes_dec_round_last(arg1: u32, arg2: u32, id: AesId) -> u32 { + extern "C" { + fn vex_aes_dec_id_last_0(arg1: u32, arg2: u32) -> u32; + fn vex_aes_dec_id_last_1(arg1: u32, arg2: u32) -> u32; + fn vex_aes_dec_id_last_2(arg1: u32, arg2: u32) -> u32; + fn vex_aes_dec_id_last_3(arg1: u32, arg2: u32) -> u32; + } + match id { + AesId::AesId0 => unsafe { vex_aes_dec_id_last_0(arg1, arg2) }, + AesId::AesId1 => unsafe { vex_aes_dec_id_last_1(arg1, arg2) }, + AesId::AesId2 => unsafe { vex_aes_dec_id_last_2(arg1, arg2) }, + AesId::AesId3 => unsafe { vex_aes_dec_id_last_3(arg1, arg2) }, + } +} + +fn encrypt_test(id: AesId) { + log::info!("Testing Encrypt, ID {:?}", id); + let mut dut: u32 = 0; + let mut reference: u32 = 0; + for (idx, byte) in reference::ENCRYPT_REF[id as usize].iter().enumerate() { + let tmp = idx << ((id as usize) * 8); + dut = aes_enc_round(dut, tmp as u32, id); + reference ^= byte; + if dut != reference { + log::error!( + "Encrypt BUG at index {}, {:?}: dut {:08x} != {:08x} ref\n", + idx, + id, + dut, + reference + ); + return; + } + } + log::info!("Pass"); +} + +fn encrypt_test_last(id: AesId) { + log::info!("Testing Encrypt Last, ID {:?}", id); + let mut dut: u32 = 0; + let mut reference: u32 = 0; + for (idx, byte) in reference::ENCRYPT_LAST_REF[id as usize].iter().enumerate() { + let tmp = idx << ((id as usize) * 8); + dut = aes_enc_round_last(dut, tmp as u32, id); + reference ^= byte; + if dut != reference { + log::error!( + "Encrypt Last BUG at index {}, {:?}: dut {:08x} != {:08x} ref\n", + idx, + id, + dut, + reference + ); + // return; + } + } + log::info!("Pass"); +} + +fn decrypt_test(id: AesId) { + log::info!("Testing Decrypt, ID {:?}", id); + let mut dut: u32 = 0; + let mut reference: u32 = 0; + for (idx, byte) in reference::DECRYPT_REF[id as usize].iter().enumerate() { + let tmp = idx << ((id as usize) * 8); + dut = aes_dec_round(dut, tmp as u32, id); + reference ^= byte; + if dut != reference { + log::error!( + "Decrypt BUG at index {}, {:?}: dut {:08x} != {:08x} ref\n", + idx, + id, + dut, + reference + ); + return; + } + } + log::info!("Pass"); +} + +fn decrypt_test_last(id: AesId) { + log::info!("Testing Decrypt Last, ID {:?}", id); + let mut dut: u32 = 0; + let mut reference: u32 = 0; + for (idx, byte) in reference::DECRYPT_LAST_REF[id as usize].iter().enumerate() { + let tmp = idx << ((id as usize) * 8); + dut = aes_dec_round_last(dut, tmp as u32, id); + reference ^= byte; + if dut != reference { + log::error!( + "Decrypt Last BUG at index {}, {:?}: dut {:08x} != {:08x} ref\n", + idx, + id, + dut, + reference + ); + return; + } + } + log::info!("Pass"); +} + +pub fn encrypt() { + log::info!("Testing Encrypt AesId 0"); + encrypt_test(AesId::AesId0); + log::info!("Testing Encrypt AesId 1"); + encrypt_test(AesId::AesId1); + log::info!("Testing Encrypt AesId 2"); + encrypt_test(AesId::AesId2); + log::info!("Testing Encrypt AesId 3"); + encrypt_test(AesId::AesId3); +} + +pub fn encrypt_last() { + log::info!("Testing Encrypt Last AesId 0"); + encrypt_test_last(AesId::AesId0); + log::info!("Testing Encrypt Last AesId 1"); + encrypt_test_last(AesId::AesId1); + log::info!("Testing Encrypt Last AesId 2"); + encrypt_test_last(AesId::AesId2); + log::info!("Testing Encrypt Last AesId 3"); + encrypt_test_last(AesId::AesId3); +} + +pub fn decrypt() { + log::info!("Testing Decrypt AesId 0"); + decrypt_test(AesId::AesId0); + log::info!("Testing Decrypt AesId 1"); + decrypt_test(AesId::AesId1); + log::info!("Testing Decrypt AesId 2"); + decrypt_test(AesId::AesId2); + log::info!("Testing Decrypt AesId 3"); + decrypt_test(AesId::AesId3); +} + +pub fn decrypt_last() { + log::info!("Testing Decrypt Last AesId 0"); + decrypt_test_last(AesId::AesId0); + log::info!("Testing Decrypt Last AesId 1"); + decrypt_test_last(AesId::AesId1); + log::info!("Testing Decrypt Last AesId 2"); + decrypt_test_last(AesId::AesId2); + log::info!("Testing Decrypt Last AesId 3"); + decrypt_test_last(AesId::AesId3); +} diff --git a/services/aes-test/src/reference.rs b/services/aes-test/src/low_level_tests/reference.rs similarity index 100% rename from services/aes-test/src/reference.rs rename to services/aes-test/src/low_level_tests/reference.rs diff --git a/services/aes-test/src/main.rs b/services/aes-test/src/main.rs index 7718d72b4..f87fc4962 100644 --- a/services/aes-test/src/main.rs +++ b/services/aes-test/src/main.rs @@ -2,163 +2,9 @@ #![cfg_attr(target_os = "none", no_main)] mod aes; -mod reference; -#[derive(Clone, Copy, Debug)] -enum AesId { - AesId0 = 0, - AesId1 = 1, - AesId2 = 2, - AesId3 = 3, -} - -fn aes_enc_round(arg1: u32, arg2: u32, id: AesId) -> u32 { - extern "C" { - fn vex_aes_enc_id_0(arg1: u32, arg2: u32) -> u32; - fn vex_aes_enc_id_1(arg1: u32, arg2: u32) -> u32; - fn vex_aes_enc_id_2(arg1: u32, arg2: u32) -> u32; - fn vex_aes_enc_id_3(arg1: u32, arg2: u32) -> u32; - } - match id { - AesId::AesId0 => unsafe { vex_aes_enc_id_0(arg1, arg2) }, - AesId::AesId1 => unsafe { vex_aes_enc_id_1(arg1, arg2) }, - AesId::AesId2 => unsafe { vex_aes_enc_id_2(arg1, arg2) }, - AesId::AesId3 => unsafe { vex_aes_enc_id_3(arg1, arg2) }, - } -} - -fn aes_enc_round_last(arg1: u32, arg2: u32, id: AesId) -> u32 { - extern "C" { - fn vex_aes_enc_id_last_0(arg1: u32, arg2: u32) -> u32; - fn vex_aes_enc_id_last_1(arg1: u32, arg2: u32) -> u32; - fn vex_aes_enc_id_last_2(arg1: u32, arg2: u32) -> u32; - fn vex_aes_enc_id_last_3(arg1: u32, arg2: u32) -> u32; - } - match id { - AesId::AesId0 => unsafe { vex_aes_enc_id_last_0(arg1, arg2) }, - AesId::AesId1 => unsafe { vex_aes_enc_id_last_1(arg1, arg2) }, - AesId::AesId2 => unsafe { vex_aes_enc_id_last_2(arg1, arg2) }, - AesId::AesId3 => unsafe { vex_aes_enc_id_last_3(arg1, arg2) }, - } -} - -fn aes_dec_round(arg1: u32, arg2: u32, id: AesId) -> u32 { - extern "C" { - fn vex_aes_dec_id_0(arg1: u32, arg2: u32) -> u32; - fn vex_aes_dec_id_1(arg1: u32, arg2: u32) -> u32; - fn vex_aes_dec_id_2(arg1: u32, arg2: u32) -> u32; - fn vex_aes_dec_id_3(arg1: u32, arg2: u32) -> u32; - } - match id { - AesId::AesId0 => unsafe { vex_aes_dec_id_0(arg1, arg2) }, - AesId::AesId1 => unsafe { vex_aes_dec_id_1(arg1, arg2) }, - AesId::AesId2 => unsafe { vex_aes_dec_id_2(arg1, arg2) }, - AesId::AesId3 => unsafe { vex_aes_dec_id_3(arg1, arg2) }, - } -} - -fn aes_dec_round_last(arg1: u32, arg2: u32, id: AesId) -> u32 { - extern "C" { - fn vex_aes_dec_id_last_0(arg1: u32, arg2: u32) -> u32; - fn vex_aes_dec_id_last_1(arg1: u32, arg2: u32) -> u32; - fn vex_aes_dec_id_last_2(arg1: u32, arg2: u32) -> u32; - fn vex_aes_dec_id_last_3(arg1: u32, arg2: u32) -> u32; - } - match id { - AesId::AesId0 => unsafe { vex_aes_dec_id_last_0(arg1, arg2) }, - AesId::AesId1 => unsafe { vex_aes_dec_id_last_1(arg1, arg2) }, - AesId::AesId2 => unsafe { vex_aes_dec_id_last_2(arg1, arg2) }, - AesId::AesId3 => unsafe { vex_aes_dec_id_last_3(arg1, arg2) }, - } -} - -fn encrypt_test(id: AesId) { - log::info!("Testing Encrypt, ID {:?}", id); - let mut dut: u32 = 0; - let mut reference: u32 = 0; - for (idx, byte) in reference::ENCRYPT_REF[id as usize].iter().enumerate() { - let tmp = idx << ((id as usize) * 8); - dut = aes_enc_round(dut, tmp as u32, id); - reference ^= byte; - if dut != reference { - log::error!( - "Encrypt BUG at index {}, {:?}: dut {:08x} != {:08x} ref\n", - idx, - id, - dut, - reference - ); - return; - } - } - log::info!("Pass"); -} - -fn encrypt_test_last(id: AesId) { - log::info!("Testing Encrypt Last, ID {:?}", id); - let mut dut: u32 = 0; - let mut reference: u32 = 0; - for (idx, byte) in reference::ENCRYPT_LAST_REF[id as usize].iter().enumerate() { - let tmp = idx << ((id as usize) * 8); - dut = aes_enc_round_last(dut, tmp as u32, id); - reference ^= byte; - if dut != reference { - log::error!( - "Encrypt Last BUG at index {}, {:?}: dut {:08x} != {:08x} ref\n", - idx, - id, - dut, - reference - ); - // return; - } - } - log::info!("Pass"); -} - -fn decrypt_test(id: AesId) { - log::info!("Testing Decrypt, ID {:?}", id); - let mut dut: u32 = 0; - let mut reference: u32 = 0; - for (idx, byte) in reference::DECRYPT_REF[id as usize].iter().enumerate() { - let tmp = idx << ((id as usize) * 8); - dut = aes_dec_round(dut, tmp as u32, id); - reference ^= byte; - if dut != reference { - log::error!( - "Decrypt BUG at index {}, {:?}: dut {:08x} != {:08x} ref\n", - idx, - id, - dut, - reference - ); - return; - } - } - log::info!("Pass"); -} - -fn decrypt_test_last(id: AesId) { - log::info!("Testing Decrypt Last, ID {:?}", id); - let mut dut: u32 = 0; - let mut reference: u32 = 0; - for (idx, byte) in reference::DECRYPT_LAST_REF[id as usize].iter().enumerate() { - let tmp = idx << ((id as usize) * 8); - dut = aes_dec_round_last(dut, tmp as u32, id); - reference ^= byte; - if dut != reference { - log::error!( - "Decrypt Last BUG at index {}, {:?}: dut {:08x} != {:08x} ref\n", - idx, - id, - dut, - reference - ); - return; - } - } - log::info!("Pass"); -} +#[cfg(feature = "low_level_tests")] +mod low_level_tests; #[xous::xous_main] fn aes_test_main() -> ! { @@ -212,43 +58,16 @@ fn aes_test_main() -> ! { log::info!("Plaintext: {:?}", plaintext); log::info!("Result: {:?}", output); - // log::info!("Running additional tests"); + #[cfg(feature = "low_level_tests")] + { + log::info!("Running additional tests"); - // log::info!("Testing Encrypt AesId 0"); - // encrypt_test(AesId::AesId0); - // log::info!("Testing Encrypt AesId 1"); - // encrypt_test(AesId::AesId1); - // log::info!("Testing Encrypt AesId 2"); - // encrypt_test(AesId::AesId2); - // log::info!("Testing Encrypt AesId 3"); - // encrypt_test(AesId::AesId3); + low_level_tests::encrypt(); + low_level_tests::encrypt_last(); + low_level_tests::decrypt(); + low_level_tests::decrypt_last(); + } - // log::info!("Testing Encrypt Last AesId 0"); - // encrypt_test_last(AesId::AesId0); - // log::info!("Testing Encrypt Last AesId 1"); - // encrypt_test_last(AesId::AesId1); - // log::info!("Testing Encrypt Last AesId 2"); - // encrypt_test_last(AesId::AesId2); - // log::info!("Testing Encrypt Last AesId 3"); - // encrypt_test_last(AesId::AesId3); - - // log::info!("Testing Decrypt AesId 0"); - // decrypt_test(AesId::AesId0); - // log::info!("Testing Decrypt AesId 1"); - // decrypt_test(AesId::AesId1); - // log::info!("Testing Decrypt AesId 2"); - // decrypt_test(AesId::AesId2); - // log::info!("Testing Decrypt AesId 3"); - // decrypt_test(AesId::AesId3); - - // log::info!("Testing Decrypt Last AesId 0"); - // decrypt_test_last(AesId::AesId0); - // log::info!("Testing Decrypt Last AesId 1"); - // decrypt_test_last(AesId::AesId1); - // log::info!("Testing Decrypt Last AesId 2"); - // decrypt_test_last(AesId::AesId2); - // log::info!("Testing Decrypt Last AesId 3"); - // decrypt_test_last(AesId::AesId3); loop { xous::wait_event(); }