add performance counter to measure AES runtimes

this simple test does a walking 1's across AES256 key space to
see if there are gross bit-dependent execution timing problems
This commit is contained in:
bunnie 2022-09-11 07:46:41 +08:00
parent 5e15f798c8
commit beed2ceab8
3 changed files with 103 additions and 1 deletions

View file

@ -118,4 +118,5 @@ dbg-ecupdate = [] # for issuing manual commands to initiate an EC update (vs UX)
test-rekey = []
nettest = [] # batch network tests
tls = ["rustls", "webpki-roots", "ring", "tungstenite", "url"]
default = [] # "debugprint"
perfcounter = []
default = ["perfcounter"] # "debugprint"

View file

@ -1,6 +1,8 @@
use xous::{MessageEnvelope};
use xous_ipc::String;
use core::fmt::Write;
#[cfg(feature="perfcounter")]
use utralib::generated::*;
use std::collections::HashMap;
/////////////////////////// Common items to all commands
@ -48,6 +50,11 @@ pub struct CommonEnv {
netmgr: net::NetManager,
xns: xous_names::XousNames,
boot_instant: std::time::Instant,
/// make this communal so any number of commands can trigger or reset the performance counter, and/or perform logging
#[cfg(feature="perfcounter")]
perf_csr: CSR<u32>,
#[cfg(feature="perfcounter")]
event_csr: CSR::<u32>,
}
impl CommonEnv {
pub fn register_handler(&mut self, verb: String::<256>) -> u32 {
@ -161,6 +168,23 @@ pub struct CmdEnv {
impl CmdEnv {
pub fn new(xns: &xous_names::XousNames) -> CmdEnv {
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
#[cfg(feature="perfcounter")]
let perf_csr = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::perfcounter::HW_PERFCOUNTER_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map perfcounter CSR range");
#[cfg(feature="perfcounter")]
let event1_csr = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::event_source1::HW_EVENT_SOURCE1_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map event1 CSR range");
let common = CommonEnv {
llio: llio::Llio::new(&xns),
com: com::Com::new(&xns).expect("could't connect to COM"),
@ -171,6 +195,10 @@ impl CmdEnv {
xns: xous_names::XousNames::new().unwrap(),
netmgr: net::NetManager::new(),
boot_instant: std::time::Instant::now(),
#[cfg(feature="perfcounter")]
perf_csr: CSR::new(perf_csr.as_mut_ptr() as *mut u32),
#[cfg(feature="perfcounter")]
event_csr: CSR::new(event1_csr.as_mut_ptr() as *mut u32),
};
//let fcc = Fcc::new(&mut common);
#[cfg(feature="benchmarks")]

View file

@ -22,6 +22,8 @@ use gam::DecodePng;
use std::convert::TryInto;
#[cfg(feature="tls")]
use tungstenite::{WebSocket, stream::MaybeTlsStream};
#[cfg(feature="perfcounter")]
use utralib::generated::*;
pub struct NetCmd {
callback_id: Option<u32>,
@ -498,6 +500,77 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
log::info!("{}", std::str::from_utf8(&plaintext).unwrap_or("utf-error"));
log::set_max_level(log::LevelFilter::Info);
}
#[cfg(feature="perfcounter")]
"ctime" => {
log::info!("constant time tests");
env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// stop the counter if it would rollover
env.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT0, 0xFFFF_FFFF);
env.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT1, 0);
// configure the system
env.perf_csr.wo(utra::perfcounter::CONFIG,
env.perf_csr.ms(utra::perfcounter::CONFIG_PRESCALER, 0)
| env.perf_csr.ms(utra::perfcounter::CONFIG_SATURATE, 1)
| env.perf_csr.ms(utra::perfcounter::CONFIG_EVENT_WIDTH_MINUS_ONE, 31)
);
// this starts the performance counter
log::info!("== restart ==");
env.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1);
use aes::Aes256;
use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit};
use aes::cipher::generic_array::GenericArray;
let mut key_array: [u8; 32];
let mut data_array: [u8; 16];
const MAX_ENTRIES: u32 = 4096;
let mut entries: u32 = 0; // track the number of entries we've used up
for databit in 0..128 {
data_array = [0; 16];
data_array[databit / 8] = 1 << databit % 8;
for keybit in 0..256 {
key_array = [0; 32];
key_array[keybit / 8] = 1 << keybit % 8;
let cipher_hw = Aes256::new(&key_array.into());
let mut block = GenericArray::clone_from_slice(&mut data_array);
// demarcate the encryption operation performance counter events
entries += 1;
env.event_csr.wfo(utra::event_source1::PERFEVENT_CODE,
((databit as u32) << 8) | keybit as u32);
cipher_hw.encrypt_block(&mut block);
env.event_csr.wfo(utra::event_source1::PERFEVENT_CODE,
0x100_0000 | ((databit as u32) << 8) | keybit as u32);
entries += 1;
if entries >= MAX_ENTRIES {
// stop the counter
env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// read out the events and print them
while env.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 {
let code = env.perf_csr.r(utra::perfcounter::EVENT_RAW0);
let time = env.perf_csr.r(utra::perfcounter::EVENT_RAW1);
let index = env.perf_csr.r(utra::perfcounter::EVENT_INDEX);
log::info!("{}:{}:{}", index, time, code)
}
// restart the counter
log::info!("== restart ==");
entries = 0;
env.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1);
}
}
}
// stop the counter
env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// read out the events and print them
while env.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 {
let code = env.perf_csr.r(utra::perfcounter::EVENT_RAW0);
let time = env.perf_csr.r(utra::perfcounter::EVENT_RAW1);
let index = env.perf_csr.r(utra::perfcounter::EVENT_INDEX);
log::info!("{}:{}:{}", index, time, code)
}
}
#[cfg(any(target_os = "none", target_os = "xous"))]
"ping" => {
if let Some(name) = tokens.next() {