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 = []
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.
aestests = [] # adds AES tests
tts = [] # adds text to speech plugin
pddbtest = []
default = [] # "debugprint"

View file

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

View file

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