diff --git a/services/aes-test/Cargo.toml b/services/aes-test/Cargo.toml index 2b590954d..18ec5f655 100644 --- a/services/aes-test/Cargo.toml +++ b/services/aes-test/Cargo.toml @@ -8,7 +8,9 @@ version = "0.1.0" [dependencies] log = "0.4" log-server = {path = "../log-server"} +hex-literal = "0.3" 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 be8d2b59d..b2c15ee2c 100644 --- a/services/aes-test/src/aes.rs +++ b/services/aes-test/src/aes.rs @@ -1,4 +1,4 @@ -const Te0: [u32; 256] = [ +const TE0: [u32; 256] = [ 0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554, 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a, 0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b, @@ -33,7 +33,7 @@ const Te0: [u32; 256] = [ 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a, ]; -const Te1: [u32; 256] = [ +const TE1: [u32; 256] = [ 0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5, 0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676, 0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d, 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0, @@ -67,7 +67,8 @@ const Te1: [u32; 256] = [ 0x8f038c8c, 0xf859a1a1, 0x80098989, 0x171a0d0d, 0xda65bfbf, 0x31d7e6e6, 0xc6844242, 0xb8d06868, 0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616, ]; -const Te2: [u32; 256] = [ + +const TE2: [u32; 256] = [ 0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5, 0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76, 0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d, 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0, @@ -101,7 +102,8 @@ const Te2: [u32; 256] = [ 0x8c8f038c, 0xa1f859a1, 0x89800989, 0x0d171a0d, 0xbfda65bf, 0xe631d7e6, 0x42c68442, 0x68b8d068, 0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16, ]; -const Te3: [u32; 256] = [ + +const TE3: [u32; 256] = [ 0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491, 0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec, 0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa, 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb, @@ -136,7 +138,7 @@ const Te3: [u32; 256] = [ 0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e, 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c, ]; -const Td0: [u32; 256] = [ +const TD0: [u32; 256] = [ 0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f, 0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6, @@ -170,7 +172,8 @@ const Td0: [u32; 256] = [ 0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541, 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742, ]; -const Td1: [u32; 256] = [ + +const TD1: [u32; 256] = [ 0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303, 0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3, 0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0, 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9, @@ -204,7 +207,8 @@ const Td1: [u32; 256] = [ 0x81caaff3, 0x3eb968c4, 0x2c382434, 0x5fc2a340, 0x72161dc3, 0x0cbce225, 0x8b283c49, 0x41ff0d95, 0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857, ]; -const Td2: [u32; 256] = [ + +const TD2: [u32; 256] = [ 0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3, 0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562, 0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe, 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3, @@ -238,7 +242,7 @@ const Td2: [u32; 256] = [ 0xf381caaf, 0xc43eb968, 0x342c3824, 0x405fc2a3, 0xc372161d, 0x250cbce2, 0x498b283c, 0x9541ff0d, 0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8, ]; -const Td3: [u32; 256] = [ +const TD3: [u32; 256] = [ 0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b, 0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5, 0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d, 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b, @@ -273,130 +277,529 @@ 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] = [ +const RCON: [u32; 10] = [ 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ ]; -const MAXNR: usize = 14; -pub struct Key { - key: [u32; 4 * (MAXNR + 1)], +#[derive(Clone, Copy, Debug)] +enum AesByte { + Byte0 = 0, + Byte1 = 1, + Byte2 = 2, + Byte3 = 3, +} + +#[derive(Debug)] +pub struct Aes { + // Why does this need to be so large? + key: [u32; 60], rounds: usize, } -pub fn -set_encrypt_key(user_key: [u8; 32], bits: usize, key: &mut Key) -> usize -{ - u32 *rk; - int i = 0; - u32 temp; +impl Default for Aes { + fn default() -> Self { + Aes { + key: [0u32; 60], + rounds: 0, + } + } +} - let rk = &mut key.key; +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; + fn vex_aes_enc_id_2(arg1: u32, arg2: u32) -> u32; + fn vex_aes_enc_id_3(arg1: u32, arg2: u32) -> u32; + } + match id { + 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) }, + } +} - key.rounds = match bits { - 128 => 10, - 192 => 12, - 256 => 14, - _ => return -2, +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; + 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 { + 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: 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; + fn vex_aes_dec_id_2(arg1: u32, arg2: u32) -> u32; + fn vex_aes_dec_id_3(arg1: u32, arg2: u32) -> u32; + } + match id { + 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: 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; + 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 { + 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) }, + } +} + +use core::convert::TryInto; + +fn get_u32_be(input: &[u8], offset: usize) -> u32 { + u32::from_be_bytes(input[offset..offset+4].try_into().unwrap()) +} + +fn get_u32_le(input: &[u8], offset: usize) -> u32 { + u32::from_le_bytes(input[offset..offset+4].try_into().unwrap()) +} + +// Const versions of these functions +// const fn get_u32_be(input: &[u8], offset: usize) -> u32 { +// let tmp = [ +// input[offset + 3], +// input[offset + 2], +// input[offset + 1], +// input[offset + 0], +// ]; +// u32::from_le_bytes(tmp) +// } + +// const fn get_u32_le(input: &[u8], offset: usize) -> u32 { +// let tmp = [ +// input[offset + 0], +// input[offset + 1], +// input[offset + 2], +// input[offset + 3], +// ]; +// u32::from_le_bytes(tmp) +// } + +fn set_u32(output: &mut [u8], offset: usize, value: u32) { + let tmp = value.to_le_bytes(); + output[offset + 0] = tmp[0]; + output[offset + 1] = tmp[1]; + output[offset + 2] = tmp[2]; + output[offset + 3] = tmp[3]; +} + +pub fn set_encrypt_key(user_key: &[u8], key: &mut Aes) -> Result<(), &'static str> { + set_encrypt_key_inner(user_key, key, true) +} + +fn set_encrypt_key_inner( + user_key: &[u8], + key: &mut Aes, + swap_final: bool, +) -> Result<(), &'static str> { + let rk = &mut key.key; + + key.rounds = match user_key.len() { + 16 => 10, + 24 => 12, + 32 => 14, + _ => Err("unrecognized keylength")?, }; - rk[0] = GETU32(user_key); - rk[1] = GETU32(user_key + 4); - rk[2] = GETU32(user_key + 8); - rk[3] = GETU32(user_key + 12); - if (bits == 128) { - while (1) { - temp = rk[3]; - rk[4] = rk[0] ^ - (Te2[(temp >> 16) & 0xff] & 0xff000000) ^ - (Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^ - (Te0[(temp) & 0xff] & 0x0000ff00) ^ - (Te1[(temp >> 24)] & 0x000000ff) ^ - rcon[i]; - rk[5] = rk[1] ^ rk[4]; - rk[6] = rk[2] ^ rk[5]; - rk[7] = rk[3] ^ rk[6]; - if (++i == 10) { - vexriscv_aes_swap_key(key->rd_key); - return 0; - } - rk += 4; - } - } - panic!("Unsupported number of bits"); - // rk[4] = GETU32(userKey + 16); - // rk[5] = GETU32(userKey + 20); - // if (bits == 192) { - // while (1) { - // temp = rk[5]; - // rk[6] = rk[ 0] ^ - // (Te2[(temp >> 16) & 0xff] & 0xff000000) ^ - // (Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^ - // (Te0[(temp) & 0xff] & 0x0000ff00) ^ - // (Te1[(temp >> 24)] & 0x000000ff) ^ - // rcon[i]; - // rk[7] = rk[1] ^ rk[6]; - // rk[8] = rk[2] ^ rk[7]; - // rk[9] = rk[3] ^ rk[8]; - // if (++i == 8) { - // vexriscv_aes_swap_key(key->rd_key); - // return 0; - // } - // rk[10] = rk[4] ^ rk[9]; - // rk[11] = rk[5] ^ rk[10]; - // rk += 6; - // } - // } - // rk[6] = GETU32(userKey + 24); - // rk[7] = GETU32(userKey + 28); - // if (bits == 256) { - // while (1) { - // temp = rk[7]; - // rk[8] = rk[0] ^ - // (Te2[(temp >> 16) & 0xff] & 0xff000000) ^ - // (Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^ - // (Te0[(temp) & 0xff] & 0x0000ff00) ^ - // (Te1[(temp >> 24)] & 0x000000ff) ^ - // rcon[i]; - // rk[9] = rk[1] ^ rk[8]; - // rk[10] = rk[2] ^ rk[9]; - // rk[11] = rk[3] ^ rk[10]; - // if (++i == 7) { - // vexriscv_aes_swap_key(key->rd_key); - // return 0; - // } - // temp = rk[11]; - // rk[12] = rk[4] ^ - // (Te2[(temp >> 24)] & 0xff000000) ^ - // (Te3[(temp >> 16) & 0xff] & 0x00ff0000) ^ - // (Te0[(temp >> 8) & 0xff] & 0x0000ff00) ^ - // (Te1[(temp) & 0xff] & 0x000000ff); - // rk[13] = rk[5] ^ rk[12]; - // rk[14] = rk[6] ^ rk[13]; - // rk[15] = rk[7] ^ rk[14]; + rk[0] = get_u32_be(user_key, 0); + rk[1] = get_u32_be(user_key, 4); + rk[2] = get_u32_be(user_key, 8); + rk[3] = get_u32_be(user_key, 12); + let mut rk_offset = 0; + if user_key.len() == 16 { + for rcon in &RCON { + let temp = rk[3 + rk_offset] as usize; + rk[4 + rk_offset] = rk[0 + rk_offset] + ^ (TE2[(temp >> 16) & 0xff] & 0xff000000) + ^ (TE3[(temp >> 8) & 0xff] & 0x00ff0000) + ^ (TE0[(temp) & 0xff] & 0x0000ff00) + ^ (TE1[(temp >> 24)] & 0x000000ff) + ^ rcon; + rk[5 + rk_offset] = rk[1 + rk_offset] ^ rk[4 + rk_offset]; + rk[6 + rk_offset] = rk[2 + rk_offset] ^ rk[5 + rk_offset]; + rk[7 + rk_offset] = rk[3 + rk_offset] ^ rk[6 + rk_offset]; + rk_offset += 4; + } + if swap_final { + for value in &mut key.key { + *value = value.swap_bytes(); + } + } + return Ok(()); + } - // rk += 8; - // } - // } - return 0; + rk[4] = get_u32_be(user_key, 16); + rk[5] = get_u32_be(user_key, 20); + if user_key.len() == 24 { + for rcon in &RCON { + let temp = rk[5 + rk_offset] as usize; + rk[6 + rk_offset] = rk[0 + rk_offset] + ^ (TE2[(temp >> 16) & 0xff] & 0xff000000) + ^ (TE3[(temp >> 8) & 0xff] & 0x00ff0000) + ^ (TE0[(temp) & 0xff] & 0x0000ff00) + ^ (TE1[(temp >> 24)] & 0x000000ff) + ^ rcon; + rk[7 + rk_offset] = rk[1 + rk_offset] ^ rk[6 + rk_offset]; + rk[8 + rk_offset] = rk[2 + rk_offset] ^ rk[7 + rk_offset]; + rk[9 + rk_offset] = rk[3 + rk_offset] ^ rk[8 + rk_offset]; + + // Stop midway through the 7th run + if *rcon == RCON[7] { + break; + } + + rk[10 + rk_offset] = rk[4 + rk_offset] ^ rk[9 + rk_offset]; + rk[11 + rk_offset] = rk[5 + rk_offset] ^ rk[10 + rk_offset]; + rk_offset += 6; + } + if swap_final { + for value in &mut key.key { + *value = value.swap_bytes(); + } + } + return Ok(()); + } + + rk[6] = get_u32_be(user_key, 24); + rk[7] = get_u32_be(user_key, 28); + if user_key.len() == 32 { + let mut rk_offset = 0; + for rcon in &RCON { + let temp = rk[7 + rk_offset] as usize; + rk[8 + rk_offset] = rk[0 + rk_offset] + ^ (TE2[(temp >> 16) & 0xff] & 0xff000000) + ^ (TE3[(temp >> 8) & 0xff] & 0x00ff0000) + ^ (TE0[(temp) & 0xff] & 0x0000ff00) + ^ (TE1[(temp >> 24)] & 0x000000ff) + ^ rcon; + rk[9 + rk_offset] = rk[1 + rk_offset] ^ rk[8 + rk_offset]; + rk[10 + rk_offset] = rk[2 + rk_offset] ^ rk[9 + rk_offset]; + rk[11 + rk_offset] = rk[3 + rk_offset] ^ rk[10 + rk_offset]; + + // Stop midway through the 6th run + if *rcon == RCON[6] { + break; + } + + let temp = rk[11 + rk_offset] as usize; + rk[12 + rk_offset] = rk[4 + rk_offset] + ^ (TE2[(temp >> 24)] & 0xff000000) + ^ (TE3[(temp >> 16) & 0xff] & 0x00ff0000) + ^ (TE0[(temp >> 8) & 0xff] & 0x0000ff00) + ^ (TE1[(temp) & 0xff] & 0x000000ff); + rk[13 + rk_offset] = rk[5 + rk_offset] ^ rk[12 + rk_offset]; + rk[14 + rk_offset] = rk[6 + rk_offset] ^ rk[13 + rk_offset]; + rk[15 + rk_offset] = rk[7 + rk_offset] ^ rk[14 + rk_offset]; + + rk_offset += 8; + } + if swap_final { + for value in &mut key.key { + *value = value.swap_bytes(); + } + } + return Ok(()); + } + unreachable!(); +} + +pub fn set_decrypt_key(user_key: &[u8], key: &mut Aes) -> Result<(), &'static str> { + set_encrypt_key_inner(user_key, key, false)?; + let rk = &mut key.key; + + /* invert the order of the round keys: */ + let mut i = 0; + let mut j = 4 * key.rounds; + while i < j { + let temp = rk[i]; + rk[i] = rk[j]; + rk[j] = temp; + + let temp = rk[i + 1]; + rk[i + 1] = rk[j + 1]; + rk[j + 1] = temp; + + let temp = rk[i + 2]; + rk[i + 2] = rk[j + 2]; + rk[j + 2] = temp; + + let temp = rk[i + 3]; + rk[i + 3] = rk[j + 3]; + rk[j + 3] = temp; + + i += 4; + j -= 4; + } + + /* apply the inverse MixColumn transform to all round keys but the first and the last: */ + let mut rk_offset = 4; + for _ in 1..key.rounds { + rk[0 + rk_offset] = TD0[TE1[(rk[0 + rk_offset] as usize >> 24)] as usize & 0xff] + ^ TD1[TE1[(rk[0 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] + ^ TD2[TE1[(rk[0 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] + ^ TD3[TE1[(rk[0 + rk_offset] as usize) & 0xff] as usize & 0xff]; + rk[1 + rk_offset] = TD0[TE1[(rk[1 + rk_offset] as usize >> 24)] as usize & 0xff] + ^ TD1[TE1[(rk[1 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] + ^ TD2[TE1[(rk[1 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] + ^ TD3[TE1[(rk[1 + rk_offset] as usize) & 0xff] as usize & 0xff]; + rk[2 + rk_offset] = TD0[TE1[(rk[2 + rk_offset] as usize >> 24)] as usize & 0xff] + ^ TD1[TE1[(rk[2 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] + ^ TD2[TE1[(rk[2 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] + ^ TD3[TE1[(rk[2 + rk_offset] as usize) & 0xff] as usize & 0xff]; + rk[3 + rk_offset] = TD0[TE1[(rk[3 + rk_offset] as usize >> 24)] as usize & 0xff] + ^ TD1[TE1[(rk[3 + rk_offset] as usize >> 16) & 0xff] as usize & 0xff] + ^ TD2[TE1[(rk[3 + rk_offset] as usize >> 8) & 0xff] as usize & 0xff] + ^ TD3[TE1[(rk[3 + rk_offset] as usize) & 0xff] as usize & 0xff]; + rk_offset += 4; + } + + for value in &mut key.key { + *value = value.swap_bytes(); + } + Ok(()) +} + +pub fn vexriscv_aes_encrypt(input: &[u8], output: &mut [u8], key: &Aes) { + let rk = key.key; + + // We do two rounds per loop + let mut round_count = key.rounds / 2; + + let mut s0 = get_u32_le(input, 0); + let mut s1 = get_u32_le(input, 4); + let mut s2 = get_u32_le(input, 8); + let mut s3 = get_u32_le(input, 12); + + let mut t0 = rk[0]; + let mut t1 = rk[1]; + let mut t2 = rk[2]; + let mut t3 = rk[3]; + + s0 ^= t0; + s1 ^= t1; + s2 ^= t2; + s3 ^= t3; + + let mut rk_offset = 0; + loop { + t0 = rk[rk_offset + 4]; + t1 = rk[rk_offset + 5]; + t2 = rk[rk_offset + 6]; + t3 = rk[rk_offset + 7]; + + 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, 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, 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, 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; + if round_count == 0 { + break; + } + + s0 = rk[rk_offset + 0]; + s1 = rk[rk_offset + 1]; + s2 = rk[rk_offset + 2]; + s3 = rk[rk_offset + 3]; + + 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, 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, 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, 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]; + s1 = rk[rk_offset + 1]; + s2 = rk[rk_offset + 2]; + s3 = rk[rk_offset + 3]; + + 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, 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, 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, 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); + set_u32(output, 8, s2); + set_u32(output, 12, s3); +} + +pub fn vexriscv_aes_decrypt(input: &[u8], output: &mut [u8], key: &Aes) { + let rk = key.key; + + // We do two rounds per loop + let mut round_count = key.rounds / 2; + + let mut s0 = get_u32_le(input, 0); + let mut s1 = get_u32_le(input, 4); + let mut s2 = get_u32_le(input, 8); + let mut s3 = get_u32_le(input, 12); + + let mut t0 = rk[0]; + let mut t1 = rk[1]; + let mut t2 = rk[2]; + let mut t3 = rk[3]; + + s0 ^= t0; + s1 ^= t1; + s2 ^= t2; + s3 ^= t3; + + let mut rk_offset = 0; + loop { + t0 = rk[rk_offset + 4]; + t1 = rk[rk_offset + 5]; + t2 = rk[rk_offset + 6]; + t3 = rk[rk_offset + 7]; + + 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, 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, 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, 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; + if round_count == 0 { + break; + } + + s0 = rk[rk_offset + 0]; + s1 = rk[rk_offset + 1]; + s2 = rk[rk_offset + 2]; + s3 = rk[rk_offset + 3]; + + 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, 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, 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, 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]; + s1 = rk[rk_offset + 1]; + s2 = rk[rk_offset + 2]; + s3 = rk[rk_offset + 3]; + + 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, 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, 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, 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); + set_u32(output, 8, s2); + set_u32(output, 12, s3); } 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 57dbf1b31..cc363bdd4 100644 --- a/services/aes-test/src/main.rs +++ b/services/aes-test/src/main.rs @@ -1,163 +1,67 @@ #![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_main)] -mod reference; -// mod aes; +mod aes; -#[derive(Clone, Copy, Debug)] -pub enum AesId { - AesId0 = 0, - AesId1 = 1, - AesId2 = 2, - AesId3 = 3, +#[cfg(feature = "low_level_tests")] +mod low_level_tests; + +use hex_literal::hex; + +enum AesKind { + ECB, } -pub 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) }, - } +struct AesTest<'a, 'b, 'c> { + kind: AesKind, + key: &'a [u8], + plaintext: &'b [u8], + ciphertext: &'c [u8], } -pub fn aes_enc_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_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_dec_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) }, - } -} - -pub 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) }, - } -} - -pub 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; +impl<'a, 'b, 'c> AesTest<'a, 'b, 'c> { + pub fn new_ecb(key: &'a [u8], plaintext: &'b [u8], ciphertext: &'c [u8]) -> Self { + AesTest { + kind: AesKind::ECB, + key, + plaintext, + ciphertext, } } - 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"); -} + pub fn test(&self) -> Result<(), &'static str> { + let mut output = [0u8; 16]; + let mut aes_key = Default::default(); -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!("Setting key"); + aes::set_encrypt_key(&self.key, &mut aes_key).unwrap(); + log::info!("Running encryption"); + aes::vexriscv_aes_encrypt(&self.plaintext, &mut output, &aes_key); + log::info!("Key: {:x?}", self.key); + log::info!("Plaintext: {:x?}", self.plaintext); + log::info!("Reference: {:x?}", self.ciphertext); + log::info!("Result: {:x?}", output); + if self.ciphertext.len() != output.len() { + Err("encrypt error: ciphertext and output lengths do not match")?; + } + if self.ciphertext != output { + Err("encrypt error: ciphertext and output values do not match")?; } - } - 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; + aes::set_decrypt_key(&self.key, &mut aes_key).unwrap(); + log::info!("Running decryption"); + aes::vexriscv_aes_decrypt(&self.ciphertext, &mut output, &aes_key); + log::info!("Plaintext: {:x?}", self.plaintext); + log::info!("Result: {:x?}", output); + if self.plaintext.len() != output.len() { + Err("decrypt error: plaintext and output lengths do not match")?; } + if self.plaintext != output { + Err("decrypt error: plaintext and output values do not match")?; + } + + Ok(()) } - log::info!("Pass"); } #[xous::xous_main] @@ -168,56 +72,115 @@ fn aes_test_main() -> ! { xous::current_pid().unwrap().get() ); - 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); + // let key: [u8; 32] = [ + // 0x70, 0x69, 0x19, 0xa0, 0x40, 0x61, 0x05, 0x17, 0xf7, 0xff, 0xf5, 0x27, 0x2b, 0x64, 0x04, + // 0x67, 0xc5, 0x06, 0x7a, 0x4b, 0xba, 0x57, 0x78, 0xad, 0x6c, 0xdd, 0xcb, 0xf4, 0x73, 0x03, + // 0x15, 0x64, + // ]; + // let plaintext: [u8; 16] = [ + // 0x0b, 0x25, 0xf6, 0x7a, 0x11, 0xec, 0x9d, 0xf5, 0x73, 0x05, 0xfb, 0xe9, 0x48, 0x8a, 0xd6, + // 0x1b, + // ]; + // let reference: [u8; 16] = [ + // 0xc4, 0xb8, 0x9f, 0x45, 0x4e, 0xd8, 0x55, 0xa8, 0xa8, 0x63, 0x0b, 0xc8, 0x14, 0x87, 0x7e, + // 0x94, + // ]; - 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); + let tests = [ + // NIST ECB-AES128 + AesTest::new_ecb( + &hex!("2b7e151628aed2a6abf7158809cf4f3c"), + &hex!("6bc1bee22e409f96e93d7e117393172a"), + &hex!("3ad77bb40d7a3660a89ecaf32466ef97"), + ), + AesTest::new_ecb( + &hex!("2b7e151628aed2a6abf7158809cf4f3c"), + &hex!("ae2d8a571e03ac9c9eb76fac45af8e51"), + &hex!("f5d3d58503b9699de785895a96fdbaaf"), + ), + AesTest::new_ecb( + &hex!("2b7e151628aed2a6abf7158809cf4f3c"), + &hex!("30c81c46a35ce411e5fbc1191a0a52ef"), + &hex!("43b1cd7f598ece23881b00e3ed030688"), + ), + AesTest::new_ecb( + &hex!("2b7e151628aed2a6abf7158809cf4f3c"), + &hex!("f69f2445df4f9b17ad2b417be66c3710"), + &hex!("7b0c785e27e8ad3f8223207104725dd4"), + ), - 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); + // NIST ECB-AES192 + AesTest::new_ecb( + &hex!("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"), + &hex!("6bc1bee22e409f96e93d7e117393172a"), + &hex!("bd334f1d6e45f25ff712a214571fa5cc"), + ), + AesTest::new_ecb( + &hex!("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"), + &hex!("ae2d8a571e03ac9c9eb76fac45af8e51"), + &hex!("974104846d0ad3ad7734ecb3ecee4eef"), + ), + AesTest::new_ecb( + &hex!("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"), + &hex!("30c81c46a35ce411e5fbc1191a0a52ef"), + &hex!("ef7afd2270e2e60adce0ba2face6444e"), + ), + AesTest::new_ecb( + &hex!("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"), + &hex!("f69f2445df4f9b17ad2b417be66c3710"), + &hex!("9a4b41ba738d6c72fb16691603c18e0e"), + ), - 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); + // NIST ECB-AES256 + AesTest::new_ecb( + &hex!("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"), + &hex!("6bc1bee22e409f96e93d7e117393172a"), + &hex!("f3eed1bdb5d2a03c064b5a7e3db181f8"), + ), + AesTest::new_ecb( + &hex!("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"), + &hex!("ae2d8a571e03ac9c9eb76fac45af8e51"), + &hex!("591ccb10d410ed26dc5ba74a31362870"), + ), + AesTest::new_ecb( + &hex!("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"), + &hex!("30c81c46a35ce411e5fbc1191a0a52ef"), + &hex!("b6ed21b99ca6f4f9f153e7b1beafed1d"), + ), + AesTest::new_ecb( + &hex!("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"), + &hex!("f69f2445df4f9b17ad2b417be66c3710"), + &hex!("23304b7a39f9f3ff067d8d8f9e24ecc7"), + ), - let key: [u8; 32] = [ - 0x70, 0x69, 0x19, 0xa0, 0x40, 0x61, 0x05, 0x17, 0xf7, 0xff, 0xf5, 0x27, 0x2b, 0x64, 0x04, - 0x67, 0xc5, 0x06, 0x7a, 0x4b, 0xba, 0x57, 0x78, 0xad, 0x6c, 0xdd, 0xcb, 0xf4, 0x73, 0x03, - 0x15, 0x64, - ]; - let plaintext: [u8; 16] = [ - 0x0b, 0x25, 0xf6, 0x7a, 0x11, 0xec, 0x9d, 0xf5, 0x73, 0x05, 0xfb, 0xe9, 0x48, 0x8a, 0xd6, - 0x1b, - ]; - let reference: [u8; 16] = [ - 0xc4, 0xb8, 0x9f, 0x45, 0x4e, 0xd8, 0x55, 0xa8, 0xa8, 0x63, 0x0b, 0xc8, 0x14, 0x87, 0x7e, - 0x94, + // From the VexRiscv AES test + AesTest::new_ecb( + &hex!("706919a040610517f7fff5272b640467c5067a4bba5778ad6cddcbf473031564"), + &hex!("0b25f67a11ec9df57305fbe9488ad61b"), + &hex!("c4b89f454ed855a8a8630bc814877e94"), + ), ]; + let mut failures = 0; + for test in &tests { + if let Err(e) = test.test() { + failures += 1; + log::error!("Failed on test: {}", e); + } + } + + log::error!("{} tests were run and {} errors were encountered", tests.len(), failures); + + #[cfg(feature = "low_level_tests")] + { + log::info!("Running additional tests"); + + low_level_tests::encrypt(); + low_level_tests::encrypt_last(); + low_level_tests::decrypt(); + low_level_tests::decrypt_last(); + } + loop { xous::wait_event(); }