bring shellchat to AES 0.8 compatibility

Also note here that we have:

- Run all the AES test vectors and confirmed the pass for both
  hw and sw
- Updated the benchmark numbers for the new AES implementation
This commit is contained in:
bunnie 2022-05-30 03:09:05 +08:00
parent 684c92ca0f
commit c11d5fe52b
4 changed files with 57 additions and 44 deletions

View file

@ -89,6 +89,7 @@ utralib = {path = "../../utralib"}
debugprint = [] debugprint = []
spinortest = [] # for spinor testing. contra-indicated with PDDB, as it steals memory from the PDDB. spinortest = [] # for spinor testing. contra-indicated with PDDB, as it steals memory from the PDDB.
benchmarks = [] # adds the benchmark routines. Left off normally to free up code and memory space. benchmarks = [] # adds the benchmark routines. Left off normally to free up code and memory space.
aestests = [] # adds AES tests
tts = [] # adds text to speech plugin tts = [] # adds text to speech plugin
pddbtest = [] pddbtest = []
default = [] # "debugprint" default = [] # "debugprint"

View file

@ -114,9 +114,9 @@ use engine::*;
mod sha; mod sha;
#[cfg(feature="benchmarks")] #[cfg(feature="benchmarks")]
use sha::*; use sha::*;
#[cfg(feature="benchmarks")] #[cfg(feature="aestests")]
mod aes_cmd; mod aes_cmd;
#[cfg(feature="benchmarks")] #[cfg(feature="aestests")]
use aes_cmd::*; use aes_cmd::*;
//mod fcc; use fcc::*; //mod fcc; use fcc::*;
//mod pds; // dependency of the FCC file //mod pds; // dependency of the FCC file
@ -148,7 +148,7 @@ pub struct CmdEnv {
#[cfg(feature="benchmarks")] #[cfg(feature="benchmarks")]
sha_cmd: Sha, sha_cmd: Sha,
#[cfg(feature="benchmarks")] #[cfg(feature="aestests")]
aes_cmd: Aes, aes_cmd: Aes,
#[cfg(feature="benchmarks")] #[cfg(feature="benchmarks")]
engine_cmd: Engine, engine_cmd: Engine,
@ -171,7 +171,7 @@ impl CmdEnv {
//let fcc = Fcc::new(&mut common); //let fcc = Fcc::new(&mut common);
#[cfg(feature="benchmarks")] #[cfg(feature="benchmarks")]
let sha = Sha::new(&xns, &mut common); let sha = Sha::new(&xns, &mut common);
#[cfg(feature="benchmarks")] #[cfg(feature="aestests")]
let aes = Aes::new(&xns, &mut common); let aes = Aes::new(&xns, &mut common);
#[cfg(feature="benchmarks")] #[cfg(feature="benchmarks")]
let engine = Engine::new(&xns, &mut common); let engine = Engine::new(&xns, &mut common);
@ -218,7 +218,7 @@ impl CmdEnv {
#[cfg(feature="benchmarks")] #[cfg(feature="benchmarks")]
sha_cmd: sha, sha_cmd: sha,
#[cfg(feature="benchmarks")] #[cfg(feature="aestests")]
aes_cmd: aes, aes_cmd: aes,
#[cfg(feature="benchmarks")] #[cfg(feature="benchmarks")]
engine_cmd: engine, engine_cmd: engine,
@ -264,7 +264,7 @@ impl CmdEnv {
#[cfg(feature="benchmarks")] #[cfg(feature="benchmarks")]
&mut self.sha_cmd, &mut self.sha_cmd,
#[cfg(feature="benchmarks")] #[cfg(feature="aestests")]
&mut self.aes_cmd, &mut self.aes_cmd,
#[cfg(feature="benchmarks")] #[cfg(feature="benchmarks")]
&mut self.engine_cmd, &mut self.engine_cmd,

View file

@ -8,38 +8,36 @@ mod aes256tests;
macro_rules! block_cipher_test { macro_rules! block_cipher_test {
($name:ident, $test_name:expr, $test_case_name:ident, $cipher:ty) => { ($name:ident, $test_name:expr, $test_case_name:ident, $cipher:ty) => {
fn $name() -> String::<1024> { fn $name() -> String::<1024> {
use cipher::generic_array::{typenum::Unsigned, GenericArray}; use aes::cipher::{
use cipher::{ generic_array::GenericArray,
BlockCipher, BlockDecrypt, BlockEncrypt, NewBlockCipher, BlockDecryptMut, BlockEncryptMut, KeyInit,
consts::U16,
}; };
fn run_test(key: &[u8], pt: &[u8], ct: &[u8]) -> bool { fn run_test(key: &[u8], pt: &[u8], ct: &[u8]) -> (bool, GenericArray<u8, U16>) {
let state = <$cipher as NewBlockCipher>::new_from_slice(key).unwrap(); let mut state = <$cipher as KeyInit>::new_from_slice(key).unwrap();
let mut block = GenericArray::clone_from_slice(pt); let mut block = GenericArray::clone_from_slice(pt);
state.encrypt_block(&mut block); state.encrypt_block_mut(&mut block);
if ct != block.as_slice() { if ct != block.as_slice() {
return false; return (false, block);
} }
state.decrypt_block(&mut block); state.decrypt_block_mut(&mut block);
if pt != block.as_slice() { if pt != block.as_slice() {
return false; return (false, block);
} }
true (true, block)
} }
fn run_par_test(key: &[u8], pt: &[u8]) -> bool { fn run_par_test(key: &[u8], pt: &[u8]) -> bool {
type ParBlocks = <$cipher as BlockCipher>::ParBlocks; type Block = aes::cipher::Block<$cipher>;
type BlockSize = <$cipher as BlockCipher>::BlockSize;
type Block = GenericArray<u8, BlockSize>;
type ParBlock = GenericArray<Block, ParBlocks>;
let state = <$cipher as NewBlockCipher>::new_from_slice(key).unwrap(); let mut state = <$cipher as KeyInit>::new_from_slice(key).unwrap();
let block = Block::clone_from_slice(pt); let block = Block::clone_from_slice(pt);
let mut blocks1 = ParBlock::default(); let mut blocks1 = vec![block; 101];
for (i, b) in blocks1.iter_mut().enumerate() { for (i, b) in blocks1.iter_mut().enumerate() {
*b = block; *b = block;
b[0] = b[0].wrapping_add(i as u8); b[0] = b[0].wrapping_add(i as u8);
@ -48,9 +46,9 @@ macro_rules! block_cipher_test {
// check that `encrypt_blocks` and `encrypt_block` // check that `encrypt_blocks` and `encrypt_block`
// result in the same ciphertext // result in the same ciphertext
state.encrypt_blocks(&mut blocks1); state.encrypt_blocks_mut(&mut blocks1);
for b in blocks2.iter_mut() { for b in blocks2.iter_mut() {
state.encrypt_block(b); state.encrypt_block_mut(b);
} }
if blocks1 != blocks2 { if blocks1 != blocks2 {
return false; return false;
@ -58,9 +56,9 @@ macro_rules! block_cipher_test {
// check that `encrypt_blocks` and `encrypt_block` // check that `encrypt_blocks` and `encrypt_block`
// result in the same plaintext // result in the same plaintext
state.decrypt_blocks(&mut blocks1); state.decrypt_blocks_mut(&mut blocks1);
for b in blocks2.iter_mut() { for b in blocks2.iter_mut() {
state.decrypt_block(b); state.decrypt_block_mut(b);
} }
if blocks1 != blocks2 { if blocks1 != blocks2 {
return false; return false;
@ -71,29 +69,39 @@ macro_rules! block_cipher_test {
let mut ret = String::<1024>::new(); let mut ret = String::<1024>::new();
write!(ret, "test {} passed", $test_name).unwrap(); write!(ret, "test {} passed", $test_name).unwrap();
let pb = <$cipher as BlockCipher>::ParBlocks::to_usize(); for (i, test) in $test_case_name.iter().enumerate() {
let mut i = 0; let (pass, block) = run_test(&test.key, &test.pt, &test.ct);
for test in $test_case_name.iter() { if !pass {
if !run_test(&test.key, &test.pt, &test.ct) {
ret.clear(); ret.clear();
write!(ret, "Failed test #{}\nkey: {:?}\nplaintext: {:?}\nciphertext: {:?}\n", write!(ret,
i, test.key, test.pt, test.ct,).unwrap(); "\n\
Failed test №{}\n\
key:\t{:x?}\n\
plaintext:\t{:x?}\n\
ciphertext:\t{:x?}\n\
block:\t{:x?}\n",
i, test.key, test.pt, test.ct, block
).unwrap();
return ret; return ret;
} }
// test parallel blocks encryption/decryption // test parallel blocks encryption/decryption
if pb != 1 { if !run_par_test(&test.key, &test.pt) {
if !run_par_test(&test.key, &test.pt) { ret.clear();
write!(ret, "Failed parallel test #{}\nkey: {:?}\nplaintext: {:?}\nciphertext: {:?}\n", write!(ret,
i, test.key, test.pt, test.ct,).unwrap(); "\n\
return ret; Failed parallel test №{}\n\
} key:\t{:x?}\n\
plaintext:\t{:x?}\n\
ciphertext:\t{:x?}\n",
i, test.key, test.pt, test.ct,
).unwrap();
return ret;
} }
i += 1;
} }
// test if cipher can be cloned // test if cipher can be cloned
let key = Default::default(); let key = Default::default();
let _ = <$cipher as NewBlockCipher>::new(&key).clone(); let _ = <$cipher>::new(&key).clone();
ret ret
} }
@ -103,8 +111,8 @@ macro_rules! block_cipher_test {
use crate::{ShellCmdApi, CommonEnv}; use crate::{ShellCmdApi, CommonEnv};
use xous_ipc::String; use xous_ipc::String;
use aes::*; use aes::{Aes256, Aes128, Aes256Soft, Aes128Soft};
use cipher::{BlockDecrypt, BlockEncrypt, NewBlockCipher}; use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit};
use num_traits::*; use num_traits::*;
@ -126,7 +134,7 @@ pub(crate) enum BenchResult {
const TEST_ITERS: usize = 500; const TEST_ITERS: usize = 500;
const TEST_MAX_LEN: usize = 8192; const TEST_MAX_LEN: usize = 8192;
use cipher::generic_array::GenericArray; use aes::cipher::generic_array::GenericArray;
/* /*
hardware: -148mA @ 100% CPU usage, 77.74us/block enc+dec AES128 (500 iters, 8192 len) hardware: -148mA @ 100% CPU usage, 77.74us/block enc+dec AES128 (500 iters, 8192 len)
@ -142,6 +150,10 @@ software 158.48us/block enc+dec aes256 (500 iters, 8192 len)
with L2 cache on (64k): with L2 cache on (64k):
hardware 92.24us/block enc+dec aes256 (500 iters, 8192 len) hardware 92.24us/block enc+dec aes256 (500 iters, 8192 len)
software 154.05us/block enc+dec aes256 (500 iters, 8192 len) software 154.05us/block enc+dec aes256 (500 iters, 8192 len)
with L2 cache on (64k) and 0.8.2 fixedslice implementation:
hardware 95.8us/block enc+dec aes256 (500 iters, 8192 len)
software 131.66us/block enc+dec aes256 (500 iters, 8192 len)
*/ */
pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32); let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);

View file

@ -80,7 +80,7 @@ impl<'a> ShellCmdApi<'a> for Keys {
write!(ret, "Provisioning BBRAM").unwrap(); write!(ret, "Provisioning BBRAM").unwrap();
} }
"aes" => { "aes" => {
use aes::{BlockEncrypt, BlockDecrypt}; use root_keys::{BlockEncrypt, BlockDecrypt};
let mut pass = true; let mut pass = true;
let mut block = Block::clone_from_slice(&[0; 16]); let mut block = Block::clone_from_slice(&[0; 16]);
self.rootkeys.encrypt_block(&mut block); self.rootkeys.encrypt_block(&mut block);