From a77e8e32be06ad9c46a1c542a91f22249ef3f9a0 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 11:08:40 +0800 Subject: [PATCH 01/10] aes-test: get initial tests passing Signed-off-by: Sean Cross --- services/aes-test/src/aes.rs | 434 ++++++++++++++++++++++++++-------- services/aes-test/src/main.rs | 115 +++++---- 2 files changed, 401 insertions(+), 148 deletions(-) diff --git a/services/aes-test/src/aes.rs b/services/aes-test/src/aes.rs index be8d2b59d..02873bc88 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,7 +277,7 @@ const Td3: [u32; 256] = [ 0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0, ]; -const Td4: [u8; 256] = [ +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, @@ -292,111 +296,335 @@ const Td4: [u8; 256] = [ 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 AesId { + AesId0 = 0, + AesId1 = 1, + AesId2 = 2, + AesId3 = 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: 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) }, + } +} - key.rounds = match bits { - 128 => 10, - 192 => 12, - 256 => 14, - _ => return -2, +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) }, + } +} + +// use core::convert::TryInto; + +fn get_u32(input: &[u8], offset: usize) -> u32 { + // let tmp = [ + // input[offset + 0], + // input[offset + 1], + // input[offset + 2], + // input[offset + 3], + // ]; + let tmp = [ + input[offset + 3], + input[offset + 2], + input[offset + 1], + input[offset + 0], + ]; + u32::from_le_bytes(tmp) +} + +fn get_u32_swapped(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> { + 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(user_key, 0); + rk[1] = get_u32(user_key, 4); + rk[2] = get_u32(user_key, 8); + rk[3] = get_u32(user_key, 12); + let mut rk_offset = 0; + if user_key.len() == 16 { + for i in 0..10 { + 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[i]; + 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; + } + for value in &mut key.key { + *value = value.swap_bytes(); + } + return Ok(()); + } + rk[4] = get_u32(user_key, 16); + rk[5] = get_u32(user_key, 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] = get_u32(user_key, 24); + rk[7] = get_u32(user_key, 28); + if user_key.len() == 32 { + let mut rk_offset = 0; + for i in 0.. { + 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[i]; + 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]; - // rk += 8; - // } - // } - return 0; + if i == 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; + } + for value in &mut key.key { + *value = value.swap_bytes(); + } + return Ok(()); + } + unreachable!(); +} + +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_swapped(input, 0); + let mut s1 = get_u32_swapped(input, 4); + let mut s2 = get_u32_swapped(input, 8); + let mut s3 = get_u32_swapped(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, 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, 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, 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, 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); + + 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, 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, 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, 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, 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 = 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, 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, 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, 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, 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); + + 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/main.rs b/services/aes-test/src/main.rs index 57dbf1b31..f29cdd95f 100644 --- a/services/aes-test/src/main.rs +++ b/services/aes-test/src/main.rs @@ -1,18 +1,18 @@ #![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_main)] +mod aes; mod reference; -// mod aes; #[derive(Clone, Copy, Debug)] -pub enum AesId { +enum AesId { AesId0 = 0, AesId1 = 1, AesId2 = 2, AesId3 = 3, } -pub fn aes_enc_round(arg1: u32, arg2: u32, id: AesId) -> u32 { +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; @@ -27,22 +27,22 @@ pub fn aes_enc_round(arg1: u32, arg2: u32, id: AesId) -> u32 { } } -pub fn aes_enc_round_last(arg1: u32, arg2: u32, id: AesId) -> u32 { +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_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::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) }, } } -pub fn aes_dec_round(arg1: u32, arg2: u32, id: AesId) -> u32 { +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; @@ -57,7 +57,7 @@ pub fn aes_dec_round(arg1: u32, arg2: u32, id: AesId) -> u32 { } } -pub fn aes_dec_round_last(arg1: u32, arg2: u32, id: AesId) -> u32 { +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; @@ -110,7 +110,7 @@ fn encrypt_test_last(id: AesId) { dut, reference ); - return; + // return; } } log::info!("Pass"); @@ -168,42 +168,6 @@ 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); - - 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); - 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, @@ -218,6 +182,67 @@ fn aes_test_main() -> ! { 0x94, ]; + let key = [ + 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, + 0x3C, + ]; + let plaintext = [ + 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, + 0x2A, + ]; + let reference = [ + 0x3A, 0xD7, 0x7B, 0xB4, 0x0D, 0x7A, 0x36, 0x60, 0xA8, 0x9E, 0xCA, 0xF3, 0x24, 0x66, 0xEF, + 0x97, + ]; + + let mut output = [0u8; 16]; + let mut aes_key = Default::default(); + log::info!("Setting key"); + aes::set_encrypt_key(&key, &mut aes_key).unwrap(); + log::info!("Running encryption"); + aes::vexriscv_aes_encrypt(&plaintext, &mut output, &aes_key); + log::info!("Plaintext: {:?}", plaintext); + log::info!("Key: {:?}", key); + log::info!("Reference: {:?}", reference); + log::info!("Result: {:?}", output); + + // 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); + + // 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(); } From 7a675cdbc4fc4285bfc2f820822ca5d36e05b7d1 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 12:15:24 +0800 Subject: [PATCH 02/10] aes-test: get decryption working Signed-off-by: Sean Cross --- services/aes-test/src/aes.rs | 227 ++++++++++++++++++++++++++++++---- services/aes-test/src/main.rs | 30 +++-- 2 files changed, 221 insertions(+), 36 deletions(-) diff --git a/services/aes-test/src/aes.rs b/services/aes-test/src/aes.rs index 02873bc88..0df33ff41 100644 --- a/services/aes-test/src/aes.rs +++ b/services/aes-test/src/aes.rs @@ -423,6 +423,14 @@ fn set_u32(output: &mut [u8], offset: usize, value: u32) { } 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() { @@ -451,8 +459,10 @@ pub fn set_encrypt_key(user_key: &[u8], key: &mut Aes) -> Result<(), &'static st rk[7 + rk_offset] = rk[3 + rk_offset] ^ rk[6 + rk_offset]; rk_offset += 4; } - for value in &mut key.key { - *value = value.swap_bytes(); + if swap_final { + for value in &mut key.key { + *value = value.swap_bytes(); + } } return Ok(()); } @@ -484,40 +494,98 @@ pub fn set_encrypt_key(user_key: &[u8], key: &mut Aes) -> Result<(), &'static st if user_key.len() == 32 { let mut rk_offset = 0; for i in 0.. { - 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[i]; - 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]; + 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[i]; + 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]; if i == 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[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; - } - for value in &mut key.key { - *value = value.swap_bytes(); + 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; @@ -628,3 +696,114 @@ pub fn vexriscv_aes_encrypt(input: &[u8], output: &mut [u8], key: &Aes) { 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_swapped(input, 0); + let mut s1 = get_u32_swapped(input, 4); + let mut s2 = get_u32_swapped(input, 8); + let mut s3 = get_u32_swapped(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, 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, 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, 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, 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); + + 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, 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, 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, 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, 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 = 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, 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, 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, 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, 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); + + 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/main.rs b/services/aes-test/src/main.rs index f29cdd95f..7718d72b4 100644 --- a/services/aes-test/src/main.rs +++ b/services/aes-test/src/main.rs @@ -182,18 +182,18 @@ fn aes_test_main() -> ! { 0x94, ]; - let key = [ - 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, - 0x3C, - ]; - let plaintext = [ - 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, - 0x2A, - ]; - let reference = [ - 0x3A, 0xD7, 0x7B, 0xB4, 0x0D, 0x7A, 0x36, 0x60, 0xA8, 0x9E, 0xCA, 0xF3, 0x24, 0x66, 0xEF, - 0x97, - ]; + // let key = [ + // 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, + // 0x3C, + // ]; + // let plaintext = [ + // 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, + // 0x2A, + // ]; + // let reference = [ + // 0x3A, 0xD7, 0x7B, 0xB4, 0x0D, 0x7A, 0x36, 0x60, 0xA8, 0x9E, 0xCA, 0xF3, 0x24, 0x66, 0xEF, + // 0x97, + // ]; let mut output = [0u8; 16]; let mut aes_key = Default::default(); @@ -206,6 +206,12 @@ fn aes_test_main() -> ! { log::info!("Reference: {:?}", reference); log::info!("Result: {:?}", output); + aes::set_decrypt_key(&key, &mut aes_key).unwrap(); + log::info!("Running decryption"); + aes::vexriscv_aes_decrypt(&reference, &mut output, &aes_key); + log::info!("Plaintext: {:?}", plaintext); + log::info!("Result: {:?}", output); + // log::info!("Running additional tests"); // log::info!("Testing Encrypt AesId 0"); From 41e9a70977418a50f90c3b16e6b9b63a1fcac8a9 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 12:22:27 +0800 Subject: [PATCH 03/10] 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(); } From 668a9c758d4d11dc5a24dda0603ad6ad0c096382 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 13:44:28 +0800 Subject: [PATCH 04/10] aes-test: implement AES-192 Signed-off-by: Sean Cross --- services/aes-test/src/aes.rs | 48 ++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/services/aes-test/src/aes.rs b/services/aes-test/src/aes.rs index 8f580f716..4a03a12f0 100644 --- a/services/aes-test/src/aes.rs +++ b/services/aes-test/src/aes.rs @@ -449,27 +449,33 @@ fn set_encrypt_key_inner( } rk[4] = get_u32(user_key, 16); rk[5] = get_u32(user_key, 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; - // } - // } + if user_key.len() == 24 { + let mut rk_offset = 0; + for i in 0.. { + 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[i]; + 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]; + if i == 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(user_key, 24); rk[7] = get_u32(user_key, 28); if user_key.len() == 32 { From a137ac384857ccd2a318a03928d82a1d900ac848 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 13:44:41 +0800 Subject: [PATCH 05/10] aes-test: implement looping tests Add looping tests for AES-128, -192, and -256. Signed-off-by: Sean Cross --- services/aes-test/Cargo.toml | 1 + services/aes-test/src/main.rs | 189 +++++++++++++++++++++++++++------- 2 files changed, 152 insertions(+), 38 deletions(-) diff --git a/services/aes-test/Cargo.toml b/services/aes-test/Cargo.toml index 91b8eaad3..18ec5f655 100644 --- a/services/aes-test/Cargo.toml +++ b/services/aes-test/Cargo.toml @@ -8,6 +8,7 @@ version = "0.1.0" [dependencies] log = "0.4" log-server = {path = "../log-server"} +hex-literal = "0.3" xous = {path = "../../xous-rs"} [features] diff --git a/services/aes-test/src/main.rs b/services/aes-test/src/main.rs index f87fc4962..e854d5497 100644 --- a/services/aes-test/src/main.rs +++ b/services/aes-test/src/main.rs @@ -6,6 +6,64 @@ mod aes; #[cfg(feature = "low_level_tests")] mod low_level_tests; +use hex_literal::hex; + +enum AesKind { + ECB, +} + +struct AesTest<'a, 'b, 'c> { + kind: AesKind, + key: &'a [u8], + plaintext: &'b [u8], + ciphertext: &'c [u8], +} + +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, + } + } + + pub fn test(&self) -> Result<(), &'static str> { + let mut output = [0u8; 16]; + let mut aes_key = Default::default(); + + 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!("Plaintext: {:?}", self.plaintext); + log::info!("Key: {:?}", self.key); + log::info!("Reference: {:?}", self.ciphertext); + log::info!("Result: {:?}", 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")?; + } + + 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: {:?}", self.plaintext); + log::info!("Result: {:?}", 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(()) + } +} + #[xous::xous_main] fn aes_test_main() -> ! { log_server::init_wait().unwrap(); @@ -14,49 +72,104 @@ fn aes_test_main() -> ! { xous::current_pid().unwrap().get() ); - 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, - ]; - - // let key = [ - // 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, - // 0x3C, + // 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 = [ - // 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, - // 0x2A, + // let plaintext: [u8; 16] = [ + // 0x0b, 0x25, 0xf6, 0x7a, 0x11, 0xec, 0x9d, 0xf5, 0x73, 0x05, 0xfb, 0xe9, 0x48, 0x8a, 0xd6, + // 0x1b, // ]; - // let reference = [ - // 0x3A, 0xD7, 0x7B, 0xB4, 0x0D, 0x7A, 0x36, 0x60, 0xA8, 0x9E, 0xCA, 0xF3, 0x24, 0x66, 0xEF, - // 0x97, + // let reference: [u8; 16] = [ + // 0xc4, 0xb8, 0x9f, 0x45, 0x4e, 0xd8, 0x55, 0xa8, 0xa8, 0x63, 0x0b, 0xc8, 0x14, 0x87, 0x7e, + // 0x94, // ]; - let mut output = [0u8; 16]; - let mut aes_key = Default::default(); - log::info!("Setting key"); - aes::set_encrypt_key(&key, &mut aes_key).unwrap(); - log::info!("Running encryption"); - aes::vexriscv_aes_encrypt(&plaintext, &mut output, &aes_key); - log::info!("Plaintext: {:?}", plaintext); - log::info!("Key: {:?}", key); - log::info!("Reference: {:?}", reference); - log::info!("Result: {:?}", output); + 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!("30c81c46a35ce411e5fbc1191a0a52ef"), + &hex!("ae2d8a571e03ac9c9eb76fac45af8e51"), + &hex!("43b1cd7f598ece23881b00e3ed030688"), + ), + AesTest::new_ecb( + &hex!("f69f2445df4f9b17ad2b417be66c3710"), + &hex!("ae2d8a571e03ac9c9eb76fac45af8e51"), + &hex!("7b0c785e27e8ad3f8223207104725dd4"), + ), - aes::set_decrypt_key(&key, &mut aes_key).unwrap(); - log::info!("Running decryption"); - aes::vexriscv_aes_decrypt(&reference, &mut output, &aes_key); - log::info!("Plaintext: {:?}", plaintext); - log::info!("Result: {:?}", output); + // 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"), + ), + + // 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"), + ), + + // 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")] { From e6816ce98217bf2cb81ecc81cc1ce589165c2232 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 13:52:03 +0800 Subject: [PATCH 06/10] aes-test: fix AES-128 test vectors Some values got swapped around. Signed-off-by: Sean Cross --- services/aes-test/src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/services/aes-test/src/main.rs b/services/aes-test/src/main.rs index e854d5497..c5b70ba2d 100644 --- a/services/aes-test/src/main.rs +++ b/services/aes-test/src/main.rs @@ -37,10 +37,10 @@ impl<'a, 'b, 'c> AesTest<'a, 'b, 'c> { 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!("Plaintext: {:?}", self.plaintext); - log::info!("Key: {:?}", self.key); - log::info!("Reference: {:?}", self.ciphertext); - log::info!("Result: {:?}", output); + 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")?; } @@ -99,13 +99,13 @@ fn aes_test_main() -> ! { &hex!("f5d3d58503b9699de785895a96fdbaaf"), ), AesTest::new_ecb( + &hex!("2b7e151628aed2a6abf7158809cf4f3c"), &hex!("30c81c46a35ce411e5fbc1191a0a52ef"), - &hex!("ae2d8a571e03ac9c9eb76fac45af8e51"), &hex!("43b1cd7f598ece23881b00e3ed030688"), ), AesTest::new_ecb( + &hex!("2b7e151628aed2a6abf7158809cf4f3c"), &hex!("f69f2445df4f9b17ad2b417be66c3710"), - &hex!("ae2d8a571e03ac9c9eb76fac45af8e51"), &hex!("7b0c785e27e8ad3f8223207104725dd4"), ), From 27c4444d7c9173547ece45f9a35cbee78d1a7beb Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 13:52:16 +0800 Subject: [PATCH 07/10] aes-test: more pruning of C-isms Signed-off-by: Sean Cross --- services/aes-test/src/aes.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/services/aes-test/src/aes.rs b/services/aes-test/src/aes.rs index 4a03a12f0..ec5b28a0e 100644 --- a/services/aes-test/src/aes.rs +++ b/services/aes-test/src/aes.rs @@ -367,7 +367,7 @@ fn aes_dec_round_last(arg1: u32, arg2: u32, id: AesByte) -> u32 { } } -// use core::convert::TryInto; +use core::convert::TryInto; fn get_u32(input: &[u8], offset: usize) -> u32 { // let tmp = [ @@ -376,23 +376,17 @@ fn get_u32(input: &[u8], offset: usize) -> u32 { // input[offset + 2], // input[offset + 3], // ]; - let tmp = [ - input[offset + 3], - input[offset + 2], - input[offset + 1], - input[offset + 0], - ]; - u32::from_le_bytes(tmp) + // let tmp = [ + // input[offset + 3], + // input[offset + 2], + // input[offset + 1], + // input[offset + 0], + // ]; + u32::from_be_bytes(input[offset..offset+4].try_into().unwrap()) } fn get_u32_swapped(input: &[u8], offset: usize) -> u32 { - let tmp = [ - input[offset + 0], - input[offset + 1], - input[offset + 2], - input[offset + 3], - ]; - u32::from_le_bytes(tmp) + u32::from_le_bytes(input[offset..offset+4].try_into().unwrap()) } fn set_u32(output: &mut [u8], offset: usize, value: u32) { From ee36638b5ea38a4d41ca7d8d6078806c9c1ffd86 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 13:57:19 +0800 Subject: [PATCH 08/10] aes-test: rename get_u32_* to get_u32_[bl]e These functions are better described using this nomenclature. Signed-off-by: Sean Cross --- services/aes-test/src/aes.rs | 48 ++++++++++++++---------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/services/aes-test/src/aes.rs b/services/aes-test/src/aes.rs index ec5b28a0e..3603fbdc9 100644 --- a/services/aes-test/src/aes.rs +++ b/services/aes-test/src/aes.rs @@ -369,23 +369,11 @@ fn aes_dec_round_last(arg1: u32, arg2: u32, id: AesByte) -> u32 { use core::convert::TryInto; -fn get_u32(input: &[u8], offset: usize) -> u32 { - // let tmp = [ - // input[offset + 0], - // input[offset + 1], - // input[offset + 2], - // input[offset + 3], - // ]; - // let tmp = [ - // input[offset + 3], - // input[offset + 2], - // input[offset + 1], - // input[offset + 0], - // ]; +fn get_u32_be(input: &[u8], offset: usize) -> u32 { u32::from_be_bytes(input[offset..offset+4].try_into().unwrap()) } -fn get_u32_swapped(input: &[u8], offset: usize) -> u32 { +fn get_u32_le(input: &[u8], offset: usize) -> u32 { u32::from_le_bytes(input[offset..offset+4].try_into().unwrap()) } @@ -415,10 +403,10 @@ fn set_encrypt_key_inner( _ => Err("unrecognized keylength")?, }; - rk[0] = get_u32(user_key, 0); - rk[1] = get_u32(user_key, 4); - rk[2] = get_u32(user_key, 8); - rk[3] = get_u32(user_key, 12); + 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 i in 0..10 { @@ -441,8 +429,8 @@ fn set_encrypt_key_inner( } return Ok(()); } - rk[4] = get_u32(user_key, 16); - rk[5] = get_u32(user_key, 20); + rk[4] = get_u32_be(user_key, 16); + rk[5] = get_u32_be(user_key, 20); if user_key.len() == 24 { let mut rk_offset = 0; for i in 0.. { @@ -470,8 +458,8 @@ fn set_encrypt_key_inner( } return Ok(()); } - rk[6] = get_u32(user_key, 24); - rk[7] = get_u32(user_key, 28); + 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 i in 0.. { @@ -573,10 +561,10 @@ pub fn vexriscv_aes_encrypt(input: &[u8], output: &mut [u8], key: &Aes) { // We do two rounds per loop let mut round_count = key.rounds / 2; - let mut s0 = get_u32_swapped(input, 0); - let mut s1 = get_u32_swapped(input, 4); - let mut s2 = get_u32_swapped(input, 8); - let mut s3 = get_u32_swapped(input, 12); + 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]; @@ -684,10 +672,10 @@ pub fn vexriscv_aes_decrypt(input: &[u8], output: &mut [u8], key: &Aes) { // We do two rounds per loop let mut round_count = key.rounds / 2; - let mut s0 = get_u32_swapped(input, 0); - let mut s1 = get_u32_swapped(input, 4); - let mut s2 = get_u32_swapped(input, 8); - let mut s3 = get_u32_swapped(input, 12); + 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]; From c365fad9623483f46175868201e390dc4fa440a9 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 14:28:20 +0800 Subject: [PATCH 09/10] services: start removing some C array -isms Signed-off-by: Sean Cross --- services/aes-test/src/aes.rs | 24 +++++++++++++++--------- services/aes-test/src/main.rs | 4 ++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/services/aes-test/src/aes.rs b/services/aes-test/src/aes.rs index 3603fbdc9..e4e516740 100644 --- a/services/aes-test/src/aes.rs +++ b/services/aes-test/src/aes.rs @@ -409,14 +409,14 @@ fn set_encrypt_key_inner( rk[3] = get_u32_be(user_key, 12); let mut rk_offset = 0; if user_key.len() == 16 { - for i in 0..10 { + 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[i]; + ^ 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]; @@ -429,24 +429,27 @@ fn set_encrypt_key_inner( } return Ok(()); } + rk[4] = get_u32_be(user_key, 16); rk[5] = get_u32_be(user_key, 20); if user_key.len() == 24 { - let mut rk_offset = 0; - for i in 0.. { + 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[i]; + ^ 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]; - if i == 7 { + + // Stop midway through the 6th 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; @@ -458,25 +461,28 @@ fn set_encrypt_key_inner( } 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 i in 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[i]; + ^ 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]; - if i == 6 { + // 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) diff --git a/services/aes-test/src/main.rs b/services/aes-test/src/main.rs index c5b70ba2d..cc363bdd4 100644 --- a/services/aes-test/src/main.rs +++ b/services/aes-test/src/main.rs @@ -51,8 +51,8 @@ impl<'a, 'b, 'c> AesTest<'a, 'b, 'c> { 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: {:?}", self.plaintext); - log::info!("Result: {:?}", output); + 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")?; } From f69bbb2085820015c7ce6edba20e190820db5552 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 2 Jun 2021 14:36:07 +0800 Subject: [PATCH 10/10] aes-test: add `const` versions of some functions These may compile to faster code, but that hasn't been determined. Signed-off-by: Sean Cross --- services/aes-test/src/aes.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/services/aes-test/src/aes.rs b/services/aes-test/src/aes.rs index e4e516740..b2c15ee2c 100644 --- a/services/aes-test/src/aes.rs +++ b/services/aes-test/src/aes.rs @@ -377,6 +377,27 @@ 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]; @@ -445,7 +466,7 @@ fn set_encrypt_key_inner( 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 6th run + // Stop midway through the 7th run if *rcon == RCON[7] { break; }