From 0166bc3d4a7ff2dd4d4de44eee4192b8ae39581e Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 29 Jul 2022 23:28:02 +0800 Subject: [PATCH 01/11] remove some more trash scripts that shouldn't be in the main repo these are all helper scripts specific to my system that aren't really useful to anyone. also made it generally the case that scripts in the root directory are ignored. "generally useful" tools are put in the `tools` directory. stuff in the root directory is just notes and bits for me to chew on. --- .gitignore | 1 + buildpush.ps1 | 33 ----------- buildpush.sh | 148 -------------------------------------------------- start-gdb.sh | 11 ---- wb-gdb.sh | 6 -- 5 files changed, 1 insertion(+), 198 deletions(-) delete mode 100644 buildpush.ps1 delete mode 100755 buildpush.sh delete mode 100755 start-gdb.sh delete mode 100755 wb-gdb.sh diff --git a/.gitignore b/.gitignore index 435edab08..ad238af08 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ buildpush.ps1 # local-only routines used to start builds /*.ps1 /*.bat +/*.sh # notes to myself /*.txt diff --git a/buildpush.ps1 b/buildpush.ps1 deleted file mode 100644 index 267f25b1e..000000000 --- a/buildpush.ps1 +++ /dev/null @@ -1,33 +0,0 @@ -# a crappy starter script to help with builds under Windows environments. ymmv. - -# set RUSTFLAGS=--remap-path-prefix=F:\largework\rust-win\code\xous-core\=build -# $env:RUSTFLAGS="--remap-path-prefix=$(Get-Location)=build" - -cargo xtask app-image vault -# cargo xtask ditherpunk-image vault -# cargo xtask ffi-test -# cargo xtask minimal precursors/soc.svd - -CertUtil -hashfile precursors/bbram-test1.nky MD5 -CertUtil -hashfile precursors/soc_csr.bin MD5 - -CertUtil -hashfile target/riscv32imac-unknown-xous-elf/release/loader.bin MD5 -CertUtil -hashfile target/riscv32imac-unknown-xous-elf/release/xous.img MD5 -scp -i c:/users/bunnie/.ssh/id_pi target/riscv32imac-unknown-xous-elf/release/xous.img target/riscv32imac-unknown-xous-elf/release/loader.bin precursors/soc_csr.bin pi@10.0.245.135:code/precursors/ - -# scp -i c:/users/bunnie/.ssh/id_ed25519 target/riscv32imac-unknown-xous-elf/release/xous.img target/riscv32imac-unknown-xous-elf/release/loader.bin precursors/soc_csr.bin bunnie@192.168.137.161:code/precursors/ - -# scp -i c:/users/bunnie/.ssh/id_ed25519 target/riscv32imac-unknown-xous-elf/release/xous.img target/riscv32imac-unknown-xous-elf/release/loader.bin precursors/soc_csr.bin bunnie@10.0.245.9:/mnt/c/Users/bunnie/ - -# CertUtil -hashfile target/riscv32imac-unknown-none-elf/release/loader.bin MD5 -# CertUtil -hashfile target/riscv32imac-unknown-none-elf/release/xous.img MD5 -# scp -i c:/users/bunnie/.ssh/id_pi target/riscv32imac-unknown-none-elf/release/xous.img target/riscv32imac-unknown-none-elf/release/loader.bin precursors/soc_csr.bin precursors/bbram-test1.nky pi@10.0.245.90:code/precursors/ - -# wishbone-tool --load-name target/riscv32imac-unknown-none-elf/release/xous.img --load-address 0x980000 --load-flash - -$file = Get-Item target/riscv32imac-unknown-xous-elf/release/xous.img -$SizeinBytes = $file.Length -if($SizeinBytes -gt 6815743) { - $Overage = $SizeinBytes - 6815743 - "Warning: xous.img is getting bloated. Bloat over ideal target: $Overage bytes." -} \ No newline at end of file diff --git a/buildpush.sh b/buildpush.sh deleted file mode 100755 index 8b1504559..000000000 --- a/buildpush.sh +++ /dev/null @@ -1,148 +0,0 @@ -#!/usr/bin/env bash - -set -e # exit on any error - -UPDATE_FPGA=1 -UPDATE_KERNEL=1 -UPDATE_LOADER=1 -USE_USB=1 -FPGA_IMAGE=precursors/soc_csr.bin -KERNEL_IMAGE=target/riscv32imac-unknown-xous-elf/release/xous.img -LOADER_IMAGE=target/riscv32imac-unknown-xous-elf/release/loader.bin -CSR_CSV= -USE_IDENTITY=0 -USE_NIGHTLY= -IMAGE=hw-image -SOC_SVD=precursors/soc.svd - -POSITIONAL=() -while [[ $# -gt 0 ]] -do - key="$1" - case $key in - -k|--kernel-skip) - UPDATE_KERNEL=0 - shift - ;; - -f|--fpga-skip) - UPDATE_FPGA=0 - shift - ;; - -l|--loader-skip) - UPDATE_LOADER=0 - shift - ;; - -c|--copy-to) - DEST_HOST=$2 - USE_USB=0 - shift - shift - ;; - -i|--identity) - USE_IDENTITY=1 - IDENTITY=$2 - shift - shift - ;; - --current-csr) - CSR_CSV="--csr-csv ../betrusted-soc/build/csr.csv" - shift - ;; - -n|--nightly) - USE_NIGHTLY=+nightly - shift - ;; - -m|--minimal) - IMAGE=minimal - shift - ;; - -t|--trngtest) - IMAGE=trng-test - shift - ;; - -r|--rotest) - IMAGE=ro-test - shift - ;; - -a|--avtest) - IMAGE=av-test - shift - ;; - --image) - IMAGE=$2 - shift - shift - ;; - -h|--help) - echo "$0 provisions betrusted. --kernel-skip skips the kernel, --fpga-skip skips the FPGA. --current-csr indicates to use the CSR for the new FPGA image to do the update (normally you want to use the one corresponding to the older, currently installed version)." - echo "Alternatively, using --copy-to copies the files to a remote host and skips provisioning." - echo " --identity supplies an identity file to the scp command." - echo " Copying assumes betrusted-scripts repo is cloned on ssh-target at ~/code/betrused-scripts/" - exit 0 - ;; - *) - POSITIONAL+=("$1") - shift - ;; - esac -done - -set -- "${POSITIONAL[@]}" - -DESTDIR=code/precursors - -# this didn't work. Timestamping is actually kinda broken, because -# you end up capturing just the time that you managed to trigger a -# full rebuild, and not just an incremental rebuild. :-/ -#touch services/log-server/src/main.rs # bump the build time in the log server - -cargo $USE_NIGHTLY xtask $IMAGE $SOC_SVD - -if [ $? -ne 0 ] -then - echo "build failed, aborting!" - exit 1 -fi - -# case of no private key specified -if [ $USE_USB -eq 1 ] -then - if [ $UPDATE_LOADER -eq 1 ] - then - echo "Burning loader" - cd tools && ./usb_update.py -l - fi - if [ $UPDATE_KERNEL -eq 1 ] - then - echo "Burning kernel" - cd tools && ./usb_update.py -k - fi - if [ $UPDATE_FPGA -eq 1 ] - then - echo "Burning FPGA image" - cd tools && ./usb_update.py -s - echo "*** Select 'Install gateware update' from the main menu to apply the update with your root keys ***" - fi -else - - if [ -n "$OVERRIDE_SVD" ] - then - FPGA_IMAGE=precursors/soc_csr.bin - fi - if [ -e "$FPGA_IMAGE" ] - then - md5sum $FPGA_IMAGE - fi - md5sum $KERNEL_IMAGE - md5sum $LOADER_IMAGE - - if [ $USE_IDENTITY -eq 1 ] - then - # there is a private key - echo "Copying to $DEST_HOST:$DESTDIR/ with public key $IDENTITY" - scp -i $IDENTITY $KERNEL_IMAGE $FPGA_IMAGE $LOADER_IMAGE $DEST_HOST:$DESTDIR/ - else - echo "Copying to $DEST_HOST:$DESTDIR/ without public key" - scp $KERNEL_IMAGE $FPGA_IMAGE $LOADER_IMAGE $DEST_HOST:$DESTDIR/ - fi -fi diff --git a/start-gdb.sh b/start-gdb.sh deleted file mode 100755 index 592d7ded2..000000000 --- a/start-gdb.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -if [ -z "$1" ] -then - echo "Usage: $0 target" - echo "Missing target argument." - exit 0 -fi - -riscv64-unknown-elf-gdb -ex 'set riscv use-compressed-breakpoints off' -ex 'file kernel/target/riscv32imac-unknown-none-elf/release/kernel' -ex "tar rem $1:3333" -#riscv64-unknown-elf-gdb -ex 'set riscv use-compressed-breakpoints off' -ex 'file target/riscv32imac-unknown-none-elf/release/fcc-agent' -ex "tar rem $1:3333" diff --git a/wb-gdb.sh b/wb-gdb.sh deleted file mode 100755 index 883edec48..000000000 --- a/wb-gdb.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh - -CSR_CSV=../betrusted-soc/build/csr.csv - -#wishbone-tool -s gdb -s terminal --bind-addr 0.0.0.0 --csr-csv=$CSR_CSV --debug-offset=0xefff0000 -wishbone-tool -s gdb --bind-addr 0.0.0.0 --csr-csv=$CSR_CSV --debug-offset=0xefff0000 From 7d62ca85b8357d96aed97a62f66b4dcef5f19be2 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Aug 2022 01:32:19 +0800 Subject: [PATCH 02/11] some preliminary schwags at making flamegraphs turns out, they're pretty useless. I mean, for debugging actual running code. it tells us basically what we know already which is 99.99% of the time is spent waiting on inter-thread messaging in hosted mode. --- Cargo.lock | 1 + services/pddb/Cargo.toml | 6 +++- services/pddb/src/main.rs | 5 +++ services/pddb/src/profiling.rs | 66 ++++++++++++++++++++++++++++++++++ xtask/src/main.rs | 27 ++++++++++++++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 services/pddb/src/profiling.rs diff --git a/Cargo.lock b/Cargo.lock index f6127a681..a2e4235eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2364,6 +2364,7 @@ dependencies = [ "cipher 0.4.3", "digest 0.9.0", "gam", + "hex 0.4.3", "hkdf", "llio", "locales", diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 7c3034e5f..e2ee5e5fd 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -52,6 +52,8 @@ utralib = { path = "../../utralib"} rand = "0.7.3" rand_chacha = "0.3.1" +hex = { version = "0.4.3", default-features = false, features = ["alloc"], optional = true } + [features] # when selected, physical disk addresses are set to 64 bits, otherwise, they are 32 bits. # 32 bit addressing is recommended for Precursor, as its disk is only 128MiB and it has limited RAM for bookkeeping. @@ -76,4 +78,6 @@ migration1 = [] # hazardous debug flag decorates any debug paths that might accidentally leak key material hazardous-debug = [] test-rekey = [] -default = ["mbbb"] +# routines to help with performance profiling of the PDDB in hosted mode +pddb-flamegraph = ["hex"] +default = ["mbbb", "pddb-flamegraph"] diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index d6852bcdf..71cbfbeb9 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -381,6 +381,9 @@ mod tests; #[allow(unused_imports)] use tests::*; +#[cfg(feature="pddb-flamegraph")] +mod profiling; + use num_traits::*; use xous::{send_message, Message, msg_blocking_scalar_unpack}; use xous_ipc::Buffer; @@ -663,6 +666,8 @@ fn wrapped_main() -> ! { latest_cache = basis_cache.cache_size(); log::info!("PDDB post-mount caching stats: {} heap, {} cache", latest_heap, latest_cache); scrub_run.store(true, Ordering::SeqCst); + #[cfg(feature="pddb-flamegraph")] + profiling::do_query_work(); } } }), diff --git a/services/pddb/src/profiling.rs b/services/pddb/src/profiling.rs new file mode 100644 index 000000000..5363fdb99 --- /dev/null +++ b/services/pddb/src/profiling.rs @@ -0,0 +1,66 @@ +use std::thread; +#[allow(unused_imports)] +use std::io::{Error, ErrorKind, Write, Read}; + +const PROFILING_DICT: &'static str = "pdict"; +const NUMKEYS: usize = 200; +const PROFILE_ITERS: usize = 200; + +fn gen_guid(trng: &mut trng::Trng) -> String { + let mut guid = [0u8; 16]; + trng.fill_bytes(&mut guid); + hex::encode(guid) +} + +/// A test that repeatedly queries a dictionary with many entries +pub fn do_query_work() { + let _ = thread::spawn(|| { + let xns = xous_names::XousNames::new().unwrap(); + let pddb = pddb::Pddb::new(); + let mut trng = trng::Trng::new(&xns).unwrap(); + + let key_count = match pddb.list_keys(PROFILING_DICT, None) { + Ok(kl) => kl.len(), + Err(_e) => { + 0 + } + }; + + // populate the dictionary if we're short the number of keys we want to have + log::info!("test setup"); + if key_count < NUMKEYS { + for _ in 0..NUMKEYS - key_count { + let guid = gen_guid(&mut trng); + match pddb.get( + PROFILING_DICT, + &guid, + None, + true, + true, + Some(256), + None:: + ) { + Ok(mut key) => { + let mut rdata = [0u8; 192]; + trng.fill_bytes(&mut rdata); + key.write_all(&rdata).ok(); + } + Err(e) => log::error!("couldn't create test key {:?}", e), + } + } + } + log::info!("starting main loop"); + let modals = modals::Modals::new(&xns).unwrap(); + modals.show_notification("press any key to start profiling run.", None).ok(); + + modals.dynamic_notification(Some("running"), None).ok(); + for i in 0..PROFILE_ITERS { + let klist = pddb.list_keys(PROFILING_DICT, None).unwrap(); + log::info!("iter {}, klist: {}", i, klist.len()); + } + modals.dynamic_notification_close().ok(); + modals.show_notification("done", None).ok(); + + log::info!("quitting"); + }); +} \ No newline at end of file diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 4427dba12..b1bf38407 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -382,6 +382,33 @@ fn main() -> Result<(), Box> { Some(&["--features", "renode-bypass"]), )?; } + Some("pddb-flamegraph") => { + let mut args = env::args(); + args.nth(1); + let mut pkgs = pddb_dev_pkgs.to_vec(); + // add pkgs required for functional testing. Quite a bit. :-/ + pkgs.push("llio"); + pkgs.push("root-keys"); + pkgs.push("jtag"); + pkgs.push("com"); + pkgs.push("gam"); + pkgs.push("net"); + pkgs.push("dns"); + pkgs.push("graphics-server"); + pkgs.push("modals"); + pkgs.push("keyboard"); + pkgs.push("ime-frontend"); + pkgs.push("ime-plugin-shell"); + pkgs.push("status"); + pkgs.push("usb-device-xous"); + generate_app_menus(&vec![]); + run(false, &pkgs, + Some(&[ + "--features", "pddbtest", + "--features", "pddb-flamegraph", + ]), false)? + + } Some("renode-aes-test") => { generate_app_menus(&Vec::::new()); renode_image(false, &aestest_pkgs, &[], None, None)? From 5be62e372df892437f44ab462b3123e3b547ac46 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Aug 2022 15:44:16 +0800 Subject: [PATCH 03/11] chore: move AES-GCM-SIV off of branch to 0.11.1 official this patch moves us off the branch we had to hop on for cipher API compatibility to the one that's officially published by RustCrypto. This closes #168. --- Cargo.lock | 24 +++++++++-------- services/pddb/Cargo.toml | 4 +-- services/pddb/src/backend/basis.rs | 12 +++++---- services/pddb/src/backend/dictionary.rs | 19 +++++++------ services/pddb/src/backend/hw.rs | 36 ++++++++++++------------- services/root-keys/Cargo.toml | 4 +-- services/root-keys/src/backups.rs | 11 ++++---- 7 files changed, 56 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f6127a681..9716f9fb0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,10 +16,11 @@ checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" [[package]] name = "aead" -version = "0.4.3" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b613b8e1e3cf911a086f53f03bf286f52fd7a7258e4fa606f0ef220d39d8877" +checksum = "ae06cea71059b6b79d879afcdd237a33ac61afc052fdd605815e6f3916254abf" dependencies = [ + "crypto-common", "generic-array", ] @@ -36,8 +37,9 @@ dependencies = [ [[package]] name = "aes-gcm-siv" -version = "0.11.0-pre" -source = "git+https://github.com/RustCrypto/AEADs.git?branch=master#479ae5f8f37d265847da80c4e713597c2c4813ac" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae0784134ba9375416d469ec31e7c5f9fa94405049cf08c5ce5b4698be673e0d" dependencies = [ "aead", "aes", @@ -714,9 +716,9 @@ dependencies = [ [[package]] name = "crypto-common" -version = "0.1.3" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", "typenum", @@ -2479,9 +2481,9 @@ dependencies = [ [[package]] name = "polyval" -version = "0.5.3" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8419d2b623c7c0896ff2d5d96e2cb4ede590fed28fcc34934f4c33c036e620a1" +checksum = "7ef234e08c11dfcb2e56f79fd70f6f2eb7f025c0ce2333e82f4f0518ecad30c6" dependencies = [ "cfg-if 1.0.0", "cpufeatures", @@ -3780,11 +3782,11 @@ checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04" [[package]] name = "universal-hash" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f214e8f697e925001e66ec2c6e37a4ef93f0f78c2eed7814394e10c62025b05" +checksum = "7d3160b73c9a19f7e2939a2fdad446c57c1bbbbf4d919d3213ff1267a580d8b5" dependencies = [ - "generic-array", + "crypto-common", "subtle", ] diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 7c3034e5f..846d27c3d 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -24,8 +24,8 @@ aes = {path="../aes"} root-keys = {path="../root-keys"} cipher = "0.4.2" bitfield = "0.13.2" -# aes-gcm-siv = {version = "0.11.0-pre", default-features = false, features = ["alloc"]} -aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"} +aes-gcm-siv = {version = "0.11.1", default-features = false, features = ["alloc", "aes"]} +# aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"} llio = {path="../llio"} subtle = {version = "2.4.1", default-features = false} tts-frontend = {path="../tts"} diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 0a82e976a..747296bc6 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -136,10 +136,12 @@ use super::*; use core::ops::{Deref, DerefMut}; use core::mem::size_of; use std::convert::TryInto; -use aes_gcm_siv::{AesGcmSiv, Key}; -use aes_gcm_siv::aead::NewAead; +use aes_gcm_siv::{ + aead::KeyInit, + Aes256GcmSiv, +}; use aes::Aes256; -use aes::cipher::{KeyInit, generic_array::GenericArray}; +use aes::cipher::generic_array::GenericArray; use std::iter::IntoIterator; use std::collections::{BinaryHeap, HashMap, HashSet}; use std::io::{Result, Error, ErrorKind}; @@ -1090,7 +1092,7 @@ pub(crate) struct BasisCacheEntry { /// expressed as a number that needs to be multiplied by DICT_VSIZE to arrive at a virtual address pub free_dict_offset: Option, /// the cipher for the basis - pub cipher: AesGcmSiv::, + pub cipher: Aes256GcmSiv, /// derived cipher for encrypting PTEs -- cache it, so we can save the time cost of constructing the cipher key schedule pub cipher_ecb: Aes256, /// raw AES page table key -- needed because we have to do a low-level PT scan to generate FSCB, and sometimes the key comes from @@ -1121,7 +1123,7 @@ impl BasisCacheEntry { /// discover the location of the `large_alloc_ptr`. pub(crate) fn mount(hw: &mut PddbOs, name: &str, key: &BasisKeys, lazy: bool, policy: BasisRetentionPolicy) -> Option { if let Some(basis_map) = hw.pt_scan_key(&key.pt, &key.data, name) { - let cipher = AesGcmSiv::::new(Key::from_slice(&key.data)); + let cipher = Aes256GcmSiv::new(&key.data.into()); let aad = hw.data_aad(name); // get the first page, where the basis root is guaranteed to be if let Some(root_page) = basis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) { diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 434bb9b04..0858d2c46 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -4,8 +4,7 @@ use super::*; use std::num::NonZeroU32; use core::ops::{Deref, DerefMut}; use core::mem::size_of; -use aes_gcm_siv::AesGcmSiv; -use aes::Aes256; +use aes_gcm_siv::Aes256GcmSiv; use std::collections::{HashMap, BinaryHeap, HashSet}; use std::io::{Result, Error, ErrorKind}; use bitfield::bitfield; @@ -89,7 +88,7 @@ impl DictCacheEntry { /// Requires a descriptor for the hardware, and our virtual to physical page mapping. /// Does not overwrite existing cache entries, if they already exist -- only loads in ones that are missing. /// Todo: condense routines in common with ensure_key_entry() to make it easier to maintain. - pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &AesGcmSiv::) -> VirtAddr { + pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv) -> VirtAddr { let mut try_entry = 1; let mut key_count = 0; let mut alloc_top = VirtAddr::new(LARGE_POOL_START).unwrap(); @@ -197,7 +196,7 @@ impl DictCacheEntry { } /// merges the list of keys in this dict cache entry into a merge_list. /// The `merge_list` is used because keys are presented as a union across all open basis. - pub(crate) fn key_list(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &AesGcmSiv::, merge_list: &mut HashSet) { + pub(crate) fn key_list(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, merge_list: &mut HashSet) { // ensure that the key cache is filled if self.keys.len() < self.key_count as usize { self.fill(hw, v2p_map, cipher); @@ -213,7 +212,7 @@ impl DictCacheEntry { /// This shares a lot of code with the fill() routine -- we should condense the common routines /// to make this easier to maintain. Returns false if the disk was searched and no key was found; true /// if cache is hot or key was found on search. - pub(crate) fn ensure_key_entry(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &AesGcmSiv::, + pub(crate) fn ensure_key_entry(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv, name_str: &str) -> bool { // only fill if the key isn't in the cache. if !self.keys.contains_key(name_str) { @@ -285,7 +284,7 @@ impl DictCacheEntry { } } } - fn try_fill_small_key(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &AesGcmSiv::, + fn try_fill_small_key(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, data_cache: &mut PlaintextCache, key_name: &str) { if let Some(kcache) = self.keys.get_mut(key_name) { if let Some(pool_index) = small_storage_index_from_key(&kcache, self.index) { @@ -368,7 +367,7 @@ impl DictCacheEntry { /// specific dictionary in a named basis. In all of these cases, the Basis resolver will have had to find the /// correct DictCacheEntry and issue the `key_update` to it; for multiple updates, then multiple calls to /// multiple DictCacheEntry are required. - pub fn key_update(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &AesGcmSiv::, + pub fn key_update(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv, name: &str, data: &[u8], offset: usize, alloc_hint:Option, truncate: bool, large_alloc_ptr: PageAlignedVa) -> Result { self.age = self.age.saturating_add(1); self.clean = false; @@ -702,7 +701,7 @@ impl DictCacheEntry { /// Used to remove a key from the dictionary. If you call it with a non-existent key, /// the routine has no effect, and does not report an error. Small keys are not immediately /// overwritten in paranoid mode, but large keys are. - pub fn key_remove(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &AesGcmSiv::, + pub fn key_remove(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv, name_str: &str, paranoid: bool) { if paranoid { // large records are paranoid-erased, by default, because of the pool-reuse problem. @@ -815,7 +814,7 @@ impl DictCacheEntry { /// /// Observation: given the dictionary index and the small key pool index, we know exactly /// the virtual address of the data pool. - pub(crate) fn sync_small_pool(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &AesGcmSiv::) -> bool { + pub(crate) fn sync_small_pool(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv) -> bool { for (index, entry) in self.small_pool.iter_mut().enumerate() { if !entry.clean { let pool_vaddr = VirtAddr::new(small_storage_base_vaddr_from_indices(self.index, index)).unwrap(); @@ -1219,7 +1218,7 @@ pub(crate) struct PlaintextCache { pub(crate) tag: Option, } impl PlaintextCache { - pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &AesGcmSiv::, aad: &[u8], + pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, aad: &[u8], req_vaddr: VirtAddr ) { if let Some(pp) = v2p_map.get(&req_vaddr) { diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 3381dc1cd..2d0e2d6e1 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -1,8 +1,8 @@ use std::convert::TryInto; use crate::*; -use aes_gcm_siv::{AesGcmSiv, Nonce, Key, Tag}; -use aes_gcm_siv::aead::{Aead, NewAead, Payload}; +use aes_gcm_siv::{Nonce, Tag, Aes256GcmSiv}; +use aes_gcm_siv::aead::{Aead, Payload}; use aes::{Aes256, Block, BLOCK_SIZE}; use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit, generic_array::GenericArray}; use root_keys::api::AesRootkeyType; @@ -97,7 +97,7 @@ pub(crate) struct BasisKeys { struct MigrationCiphers { pt_ecb: Aes256, - data_gcm_siv: AesGcmSiv, + data_gcm_siv: Aes256GcmSiv, data_key: [u8; 32], aad_incoming: Vec::, aad_local: Vec::, @@ -497,7 +497,7 @@ impl PddbOs { pp.set_space_state(SpaceState::Used); // handle conflicting journal versions here if let Some(prev_page) = map.get(&pte.vaddr()) { - let cipher = AesGcmSiv::::new(Key::from_slice(data_key)); + let cipher = Aes256GcmSiv::new(data_key.into()); let aad = self.data_aad(basis_name); if pte.vaddr() != VirtAddr::new(VPAGE_SIZE as u64).unwrap() { // handle case that there is a conflict in data areas @@ -761,8 +761,7 @@ impl PddbOs { fn fast_space_write(&mut self, fs: &FastSpace) { self.syskey_ensure(); if let Some(system_basis_key) = &self.system_basis_key { - let key = Key::from_slice(&system_basis_key.data); - let cipher = AesGcmSiv::::new(key); + let cipher = Aes256GcmSiv::new(&system_basis_key.data.into()); let nonce_array = self.entropy.borrow_mut().get_nonce(); let nonce = Nonce::from_slice(&nonce_array); let fs_ser: &[u8] = fs.deref(); @@ -974,8 +973,7 @@ impl PddbOs { msg: &fscb_buf, aad: &aad, }; - let key = Key::from_slice(&system_key.data); - let cipher = AesGcmSiv::::new(key); + let cipher = Aes256GcmSiv::new(&system_key.data.into()); match cipher.decrypt(Nonce::from_slice(&fscb_slice[page_start..page_start + size_of::()]), payload) { Ok(msg) => { log::info!("decrypted: {}, FastSpace size: {}", msg.len(), size_of::()); @@ -1333,7 +1331,7 @@ impl PddbOs { /// returns a decrypted page that still includes the journal number at the very beginning /// We don't clip it off because it would require re-allocating a vector, and it's cheaper (although less elegant) to later /// just index past it. - pub(crate) fn data_decrypt_page(&self, cipher: &AesGcmSiv::, aad: &[u8], page: &PhysPage) -> Option> { + pub(crate) fn data_decrypt_page(&self, cipher: &Aes256GcmSiv, aad: &[u8], page: &PhysPage) -> Option> { let ct_slice = &self.pddb_mr.as_slice()[ self.data_phys_base.as_usize() + page.page_number() as usize * PAGE_SIZE .. self.data_phys_base.as_usize() + (page.page_number() as usize + 1) * PAGE_SIZE]; @@ -1408,7 +1406,7 @@ impl PddbOs { log::debug!("found kcom_nonce of {:x?}", nonce_comm); let (kenc, kcom) = self.kcom_func(key.try_into().unwrap(), &nonce_comm); - let cipher = AesGcmSiv::::new(Key::from_slice(&kenc)); + let cipher = Aes256GcmSiv::new(&kenc.into()); // Attempt decryption. This is None on failure let plaintext = cipher.decrypt( @@ -1428,7 +1426,7 @@ impl PddbOs { } /// `data` includes the journal entry on top. The data passed in must be exactly one vpage plus the journal entry - pub(crate) fn data_encrypt_and_patch_page(&self, cipher: &AesGcmSiv::, aad: &[u8], data: &mut [u8], pp: &PhysPage) { + pub(crate) fn data_encrypt_and_patch_page(&self, cipher: &Aes256GcmSiv, aad: &[u8], data: &mut [u8], pp: &PhysPage) { assert!(data.len() == VPAGE_SIZE + size_of::(), "did not get a page-sized region to patch"); let j = JournalType::from_le_bytes(data[..size_of::()].try_into().unwrap()).saturating_add(1); for (&src, dst) in j.to_le_bytes().iter().zip(data[..size_of::()].iter_mut()) { *dst = src; } @@ -1459,7 +1457,7 @@ impl PddbOs { self.trng_slice(&mut kcom_nonce); // generates the encryption and commit keys let (kenc, kcom) = self.kcom_func(key.try_into().unwrap(), &kcom_nonce); - let cipher = AesGcmSiv::::new(Key::from_slice(&kenc)); + let cipher = Aes256GcmSiv::new(&kenc.into()); let ciphertext = cipher.encrypt( &nonce, Payload { @@ -1982,7 +1980,7 @@ impl PddbOs { log::warn!("pt_scan for basis {} failed, data may be lost", name); } let pt_ecb = Aes256::new(&GenericArray::from_slice(&basis_keys.pt)); - let data_gcm_siv = AesGcmSiv::::new(Key::from_slice(&basis_keys.data)); + let data_gcm_siv = Aes256GcmSiv::new(&basis_keys.data.into()); self.dna_mode = DnaMode::Normal; let aad_local = self.data_aad(&name); self.dna_mode = DnaMode::Migration; @@ -2579,10 +2577,10 @@ impl PddbOs { aad_v2: &Vec::, key_v1: &[u8; AES_KEYSIZE], // needed to decrypt page with commit cipher_pt_v1: &Aes256, - cipher_data_v1: &AesGcmSiv::, + cipher_data_v1: &Aes256GcmSiv, key_data_v2: &[u8; AES_KEYSIZE], // this is needed because we have to do a key commitment cipher_pt_v2: &Aes256, - cipher_data_v2: &AesGcmSiv::, + cipher_data_v2: &Aes256GcmSiv, used_pages: &mut BinaryHeap::>, ) -> bool { let blank = [0xffu8; aes::BLOCK_SIZE]; @@ -2742,7 +2740,7 @@ impl PddbOs { // build the ECB cipher for page table entries, and data cipher for data pages self.cipher_ecb = Some(Aes256::new(GenericArray::from_slice(&system_basis_key_pt))); let cipher_v2 = Aes256::new(GenericArray::from_slice(&system_basis_key_pt)); // a second copy for patching the page table later in this routine interior mutability blah blah work around oops - let data_cipher_v2 = AesGcmSiv::::new(Key::from_slice(&system_basis_key)); + let data_cipher_v2 = Aes256GcmSiv::new(Key::from_slice(&system_basis_key)); let aad_v2 = self.data_aad(PDDB_DEFAULT_SYSTEM_BASIS); // now wrap the key for storage let wrapped_key = self.rootkeys.wrap_key(&system_basis_key).expect("Internal error wrapping our encryption key"); @@ -2772,7 +2770,7 @@ impl PddbOs { // now we have a copy of the AES key necessary to re-encrypt all the basis // build the data cipher for v1 pages - let data_cipher_v1 = AesGcmSiv::::new(Key::from_slice(&system_key_v1)); + let data_cipher_v1 = Aes256GcmSiv::new(Key::from_slice(&system_key_v1)); let aad_v1 = data_aad_v1(&self, PDDB_DEFAULT_SYSTEM_BASIS); // track used pages so we can create the FSCB at the end @@ -2829,10 +2827,10 @@ impl PddbOs { let basis_aad_v1 = data_aad_v1(&self, bname.first().as_str()); let basis_aad_v2 = self.data_aad(bname.first().as_str()); let basis_pt_cipher_v1 = Aes256::new(GenericArray::from_slice(&basis_key_v1)); - let basis_data_cipher_v1 = AesGcmSiv::::new(Key::from_slice(&basis_key_v1)); + let basis_data_cipher_v1 = Aes256GcmSiv::new(Key::from_slice(&basis_key_v1)); let basis_keys = self.basis_derive_key(bname.first().as_str(), pw.as_str().unwrap_or("UTF8 error")); let basis_pt_cipher_2 = Aes256::new(GenericArray::from_slice(&basis_keys.pt)); - let basis_data_cipher_2 = AesGcmSiv::::new(Key::from_slice(&basis_keys.data)); + let basis_data_cipher_2 = Aes256GcmSiv::new(Key::from_slice(&basis_keys.data)); // perform the migration if self.migration_v1_to_v2_inner( &basis_aad_v1, &basis_aad_v2, diff --git a/services/root-keys/Cargo.toml b/services/root-keys/Cargo.toml index 7324d6894..182c89909 100644 --- a/services/root-keys/Cargo.toml +++ b/services/root-keys/Cargo.toml @@ -47,8 +47,8 @@ jtag = {path = "../jtag"} tts-frontend = {path="../tts"} # backups -# aes-gcm-siv = {version = "0.11.0-pre", default-features = false, features = ["alloc"]} -aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"} +aes-gcm-siv = {version = "0.11.1", default-features = false, features = ["alloc", "aes"]} +#aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"} subtle = {version = "2.4.1", default-features = false} usb-device-xous = {path="../usb-device-xous"} keyboard = {path="../keyboard"} diff --git a/services/root-keys/src/backups.rs b/services/root-keys/src/backups.rs index 1dee30633..5786e22aa 100644 --- a/services/root-keys/src/backups.rs +++ b/services/root-keys/src/backups.rs @@ -1,9 +1,10 @@ use zeroize::Zeroize; use core::mem::size_of; use core::ops::{Deref, DerefMut}; -use aes_gcm_siv::{AesGcmSiv, Nonce, Key, Tag}; -use aes_gcm_siv::aead::{Aead, NewAead, Payload}; -use aes::Aes256; +use aes_gcm_siv::{ + aead::{KeyInit, Aead, Payload}, + Aes256GcmSiv, Tag, Nonce +}; use subtle::ConstantTimeEq; use crate::{BackupHeader, BackupOp}; @@ -144,7 +145,7 @@ pub(crate) fn create_backup( trng.fill_bytes(&mut kcom_nonce); let (kenc, kcom) = kcom_func(&key.0, &kcom_nonce); - let cipher = AesGcmSiv::::new(Key::from_slice(&kenc.0)); + let cipher = Aes256GcmSiv::new(&kenc.0.into()); // the backup data let mut backup_data_pt = BackupDataPt::default(); @@ -186,7 +187,7 @@ pub(crate) fn restore_backup( backup: &BackupDataCt ) -> Option { let (kenc, kcom) = kcom_func(&key.0, &backup.commit_nonce); - let cipher = AesGcmSiv::::new(Key::from_slice(&kenc.0)); + let cipher = Aes256GcmSiv::new(&kenc.0.into()); // Attempt decryption. This is None on failure let plaintext = cipher.decrypt( From 513fe519a73c16aa9b5596b763df69f55e75d42b Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Aug 2022 16:12:38 +0800 Subject: [PATCH 04/11] profiling should not be enabled, somehow that slipped through --- services/pddb/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index d0ef3826b..f22760eda 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -80,4 +80,4 @@ hazardous-debug = [] test-rekey = [] # routines to help with performance profiling of the PDDB in hosted mode pddb-flamegraph = ["hex"] -default = ["mbbb", "pddb-flamegraph"] +default = ["mbbb"] From 13b366345c441f94e49ef841f12dc88354876dff Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Aug 2022 23:12:26 +0800 Subject: [PATCH 05/11] clean up/optimize the key listing routine provides some marginal improvement on the vault UX --- services/pddb/src/api.rs | 17 +++++- services/pddb/src/backend/basis.rs | 6 +- services/pddb/src/backend/dictionary.rs | 4 +- services/pddb/src/lib.rs | 58 ++++++++++++++------ services/pddb/src/main.rs | 73 +++++++++++++------------ 5 files changed, 99 insertions(+), 59 deletions(-) diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 752b4d152..fbb211581 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -125,7 +125,7 @@ pub(crate) enum Opcode { // routines to list available resources KeyCountInDict = 11, - GetKeyNameAtIndex = 12, + // GetKeyNameAtIndex = 12, // superceded by ListKeyV2 DictCountInBasis = 13, GetDictNameAtIndex = 14, @@ -207,6 +207,9 @@ pub(crate) enum Opcode { /// Flush the SpaceUpdate log, restoring deniability FlushSpaceUpdate = 44, + /// Optimized key listing + ListKeyV2 = 45, + /// This key type could not be decoded InvalidOpcode = u32::MAX as _, } @@ -289,9 +292,19 @@ pub struct PddbKeyRequest { pub result: PddbRequestCode, } +pub(crate) const MAX_PDDBKLISTLEN: usize = 4064; +/// A structure for requesting a token to access a particular key/value pair +#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub (crate) struct PddbKeyList { + pub token: [u32; 4], + pub data: [u8; MAX_PDDBKLISTLEN], + pub retcode: PddbRetcode, + pub end: bool, +} + /// Return codes for Read/Write API calls to the main server #[repr(u8)] -#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub(crate) enum PddbRetcode { Uninit = 0, Ok = 1, diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 747296bc6..ca14c9c5a 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -143,7 +143,7 @@ use aes_gcm_siv::{ use aes::Aes256; use aes::cipher::generic_array::GenericArray; use std::iter::IntoIterator; -use std::collections::{BinaryHeap, HashMap, HashSet}; +use std::collections::{BinaryHeap, HashMap, HashSet, BTreeSet}; use std::io::{Result, Error, ErrorKind}; use std::cmp::Reverse; use std::cmp::Ordering; @@ -381,8 +381,8 @@ impl BasisCache { } dict_set } - pub(crate) fn key_list(&mut self, hw: &mut PddbOs, dict: &str, basis_name: Option<&str>) -> Result> { - let mut merge_list = HashSet::::new(); + pub(crate) fn key_list(&mut self, hw: &mut PddbOs, dict: &str, basis_name: Option<&str>) -> Result> { + let mut merge_list = BTreeSet::::new(); let mut found_dict = false; if basis_name.is_some() { if let Some(basis_index) = self.select_basis(basis_name) { diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 0858d2c46..daf704aa5 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -5,7 +5,7 @@ use std::num::NonZeroU32; use core::ops::{Deref, DerefMut}; use core::mem::size_of; use aes_gcm_siv::Aes256GcmSiv; -use std::collections::{HashMap, BinaryHeap, HashSet}; +use std::collections::{HashMap, BinaryHeap, BTreeSet}; use std::io::{Result, Error, ErrorKind}; use bitfield::bitfield; use std::cmp::{Ordering, Reverse}; @@ -196,7 +196,7 @@ impl DictCacheEntry { } /// merges the list of keys in this dict cache entry into a merge_list. /// The `merge_list` is used because keys are presented as a union across all open basis. - pub(crate) fn key_list(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, merge_list: &mut HashSet) { + pub(crate) fn key_list(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, merge_list: &mut BTreeSet) { // ensure that the key cache is filled if self.keys.len() < self.key_count as usize { self.fill(hw, v2p_map, cipher); diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index a8a56941f..e18c269f3 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -524,34 +524,56 @@ impl Pddb { .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; let response = buf.to_original::().unwrap(); - let count = match response.code { - PddbRequestCode::NoErr => response.index, + match response.code { + PddbRequestCode::NoErr => (), PddbRequestCode::NotFound => return Err(Error::new(ErrorKind::NotFound, "dictionary not found")), _ => return Err(Error::new(ErrorKind::Other, "Internal error")), }; - // very non-optimal, slow way of doing this, but let's just get it working first and optimize later. - // it's absolutely important that you access every entry, and the highest index last, because - // that is how the server knows you've finished with the list-out. + // v2 key listing packs the key list into a larger [u8] field that should cut down on the number of messages + // required to list a large dictionary by about a factor of 50. let mut key_list = Vec::::new(); - for index in 0..count { - let request = PddbDictRequest { - basis_specified: basis_name.is_some(), - basis: xous_ipc::String::::from_str(&bname), - dict: xous_ipc::String::::from_str(dict_name), - key: xous_ipc::String::::new(), - index, - code: PddbRequestCode::Uninit, + // just make sure we didn't screw up the sizing of this record + assert!(core::mem::size_of::() < 4096); + loop { + let request = PddbKeyList { token, + end: false, + retcode: PddbRetcode::Uninit, + data: [0u8; MAX_PDDBKLISTLEN] }; let mut buf = Buffer::into_buf(request) .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; - buf.lend_mut(self.conn, Opcode::GetKeyNameAtIndex.to_u32().unwrap()) + buf.lend_mut(self.conn, Opcode::ListKeyV2.to_u32().unwrap()) .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; - let response = buf.to_original::().unwrap(); - match response.code { - PddbRequestCode::NoErr => key_list.push(String::from(response.key.as_str().expect("utf-8 parse error in key name"))), - _ => return Err(Error::new(ErrorKind::Other, "Internal error")), + let response = buf.as_flat::().unwrap(); + match response.retcode { + ArchivedPddbRetcode::Ok => (), + ArchivedPddbRetcode::AccessDenied => return Err(Error::new(ErrorKind::PermissionDenied, "KeyList facility locked by another process, try again later")), + _ => return Err(Error::new(ErrorKind::Other, "Internal Error")), } + // the [u8] data is structured as a packed list of u8-len + u8 data slice. The max length of + // a PDDB key name is guaranteed to be shorter than a u8. If the length field is 0, then this + // particular response has no more data in it to read. + let mut index = 0; + while response.data[index] != 0 && index < MAX_PDDBKLISTLEN { + let strlen = response.data[index] as usize; + index += 1; + if strlen + index >= MAX_PDDBKLISTLEN { + log::error!("Logic error in key list, index would be out of bounds. Aborting"); + break; + } + if strlen == 0 { // case of an empty dictionary + break; + } + let key = String::from(std::str::from_utf8(&response.data[index..index+strlen]).unwrap_or("UTF8 error")); + log::trace!("Returned {}", key); + key_list.push(key); + index += strlen; + } + if response.end { + break; + } + } Ok(key_list) } diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 71cbfbeb9..49eebf9e0 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -390,7 +390,7 @@ use xous_ipc::Buffer; use core::cell::RefCell; use std::rc::Rc; use std::thread; -use std::collections::HashMap; +use std::collections::{HashMap, BTreeSet}; use std::io::ErrorKind; use core::fmt::Write; use std::sync::atomic::{AtomicBool, Ordering}; @@ -559,7 +559,7 @@ fn wrapped_main() -> ! { } }); // main server loop - let mut key_list = Vec::::new(); // storage for key lists + let mut key_list: Option::> = None; // storage for key lists let mut key_token: Option<[u32; 4]> = None; let mut dict_list = Vec::::new(); // storage for dict lists let mut dict_token: Option<[u32; 4]> = None; @@ -1158,7 +1158,6 @@ fn wrapped_main() -> ! { continue; } key_token = Some(req.token); - key_list.clear(); let bname = if req.basis_specified { Some(req.basis.as_str().unwrap()) } else { @@ -1166,58 +1165,64 @@ fn wrapped_main() -> ! { }; let dict = req.dict.as_str().expect("dict utf-8 decode error"); log::debug!("counting keys in dict {} basis {:?}", dict, bname); - match basis_cache.key_list(&mut pddb_os, dict, bname) { + key_list = match basis_cache.key_list(&mut pddb_os, dict, bname) { Ok(list) => { log::debug!("count: {}", list.len()); - if list.len() > 0 { - req.index = list.len() as u32; - for key in list { - log::debug!("key list: {}", key); - key_list.push(key); - } - } else { - log::debug!("count is 0, resetting state"); - // no keys to list, so reset the state - key_token = None; - key_list.clear(); - } req.code = PddbRequestCode::NoErr; + Some(list) } Err(e) => { - key_token = None; - key_list.clear(); match e.kind() { std::io::ErrorKind::NotFound => req.code = PddbRequestCode::NotFound, _ => req.code = PddbRequestCode::InternalError, } + None } - } + }; buffer.replace(req).unwrap(); } - Opcode::GetKeyNameAtIndex => { + Opcode::ListKeyV2 => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut req = buffer.to_original::().unwrap(); + let mut req = buffer.to_original::().unwrap(); if let Some(token) = key_token { if req.token != token { - req.code = PddbRequestCode::AccessDenied; + req.retcode = PddbRetcode::AccessDenied; } else { - if req.index >= key_list.len() as u32 { - req.code = PddbRequestCode::InternalError; - } else { - req.key = xous_ipc::String::::from_str(&key_list[req.index as usize]); - req.code = PddbRequestCode::NoErr; - log::debug!("fetching key at index {}: {}", req.index, req.key); - // the last index requested must be the highest one! - if req.index == key_list.len() as u32 - 1 { - log::debug!("last key, resetting state"); - key_token = None; - key_list.clear(); + if let Some(klist) = &mut key_list { + let mut index = 0; + loop { + let keyname = + if let Some(keyname) = klist.iter().next() { + keyname.to_string() + } else { + key_token = None; + req.end = true; + req.retcode = PddbRetcode::Ok; + break; + }; + if keyname.len() + 1 + index < MAX_PDDBKLISTLEN { + assert!(keyname.len() < u8::MAX as usize); // this should always be true due to other limits in the PDDB + req.data[index] = keyname.len() as u8; + index += 1; + req.data[index..index + keyname.len()].copy_from_slice(keyname.as_bytes()); + index += keyname.len(); + klist.remove(&keyname); + } else { + // don't remove the item, and indicate there is more to come + req.end = false; + req.retcode = PddbRetcode::Ok; + break; + } } + } else { + key_token = None; + req.end = true; + req.retcode = PddbRetcode::Ok; } } } else { log::debug!("multiple concurrent requests detected, returning error"); - req.code = PddbRequestCode::AccessDenied; + req.retcode = PddbRetcode::AccessDenied; } buffer.replace(req).unwrap(); } From 0d61cbb838b072fac296c668e4eb98ea4465de8e Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 2 Aug 2022 01:37:45 +0800 Subject: [PATCH 06/11] improve debug output --- services/pddb/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 49eebf9e0..387b67990 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -1301,7 +1301,7 @@ fn wrapped_main() -> ! { if let Some(rec) = token_dict.get(&token) { for basis in basis_cache.access_list().iter() { let temp = if let Some (name) = &rec.basis {Some(name)} else {Some(basis)}; - log::debug!("read (spec: {:?}){:?} {}", rec.basis, temp, rec.key); + log::debug!("read (spec: {:?}){:?} {} len {} pos {}", rec.basis, temp, rec.key, pbuf.len, pbuf.position); match basis_cache.key_read(&mut pddb_os, &rec.dict, &rec.key, &mut pbuf.data[..pbuf.len as usize], Some(pbuf.position as usize), From 3d992cedf4128b1653f74b8dc3039236cca62967 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 2 Aug 2022 01:38:07 +0800 Subject: [PATCH 07/11] fix an import error + reduce log noise on a normal path --- services/pddb/src/backend/hw.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 2d0e2d6e1..07e8688ba 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -1883,7 +1883,7 @@ impl PddbOs { pub(crate) fn pddb_generate_used_map(&self, cache: &Vec::) -> Option>> { if let Some(all_keys) = self.pddb_get_all_keys(&cache) { let mut page_heap = BinaryHeap::new(); - let mut page_check = HashSet::new(); // this is just for sanity checking because you can't query a heap + let mut page_check = std::collections::HashSet::new(); // this is just for sanity checking because you can't query a heap for (basis_keys, name) in all_keys { // scan the disclosed bases if let Some(map) = self.pt_scan_key(&basis_keys.pt, &basis_keys.data, &name) { @@ -1907,7 +1907,7 @@ impl PddbOs { for pp in self.fspace_cache.iter() { if pp.space_state() == SpaceState::Used || pp.space_state() == SpaceState::MaybeUsed { if !page_check.insert(pp.page_number()) { - log::info!("FSCB and page table both record this used page: {} (this is normal)", pp.page_number()); + log::debug!("FSCB and page table both record this used page: {} (this is normal)", pp.page_number()); } else { page_heap.push(Reverse(pp.page_number())); log::info!("FSCB contained {}, but not yet committed to disk; added to page_heap", pp.page_number()); From e32ea1e32fb21410440cf2e9493abf516f4acc9f Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 2 Aug 2022 01:38:46 +0800 Subject: [PATCH 08/11] change read buffer api from read_to_end() to read_exact() read_to_end() "guesses" the total amount of data to read by doing roughly 5 calls per couple hundred bytes: it starts by guessing it's 32 bytes long, then another 32, then 64, then 128, and then a final call to make sure there's nothing more. The read() call is somewhat expensive in that it involves initializing and sending page-sized memory chunks, and every time it also descends the entire basis hierarchy, recomputing the overlay. Probably, this can be improved with some sort of server-side caching of read results -- if a file is read, it seems very likely that the next few bytes will be read and a smarter caching implementation could smooth over some of the expense. However, a much easier fix for now is to just change the read call to read exactly the length of the file being presented. Since we already have the key handle, we can get the attributes and then do a single read of the exact amount. Doing this improves the speed of the UX substantially, about 40% faster for the same operation (at least). --- apps/vault/src/actions.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 1765ddae7..0540912a2 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -736,6 +736,7 @@ impl ActionManager { il.clear(); match self.mode_cache { VaultMode::Password => { + let start = self.tt.elapsed_ms(); let keylist = match self.pddb.borrow().list_keys(VAULT_PASSWORD_DICT, None) { Ok(keylist) => keylist, Err(e) => { @@ -751,6 +752,9 @@ impl ActionManager { Vec::new() } }; + log::info!("listing took {} ms", self.tt.elapsed_ms() - start); + let start = self.tt.elapsed_ms(); + let klen = keylist.len(); for key in keylist { match self.pddb.borrow().get( VAULT_PASSWORD_DICT, @@ -760,8 +764,13 @@ impl ActionManager { Some(crate::basis_change) ) { Ok(mut record) => { - let mut data = Vec::::new(); - match record.read_to_end(&mut data) { + // determine the exact length of the record and read it in one go. + // read_to_end() performs ~5x read calls to do the same thing, because it + // has to "guess" the total record length starting with a 32-byte increment + let len = record.attributes().unwrap().len; + let mut data = Vec::::with_capacity(len); + data.resize(len, 0); + match record.read_exact(&mut data) { Ok(_len) => { if let Some(pw) = deserialize_password(data) { let extra = format!("{}; {}{}", @@ -794,6 +803,7 @@ impl ActionManager { }, } } + log::info!("readout took {} ms for {} elements", self.tt.elapsed_ms() - start, klen); } VaultMode::Fido => { // first assemble U2F records @@ -820,8 +830,10 @@ impl ActionManager { Some(crate::basis_change) ) { Ok(mut record) => { - let mut data = Vec::::new(); - match record.read_to_end(&mut data) { + let len = record.attributes().unwrap().len; + let mut data = Vec::::with_capacity(len); + data.resize(len, 0); + match record.read_exact(&mut data) { Ok(_len) => { if let Some(ai) = deserialize_app_info(data) { let extra = format!("{}; {}{}", @@ -871,8 +883,10 @@ impl ActionManager { Some(crate::basis_change) ) { Ok(mut record) => { - let mut data = Vec::::new(); - match record.read_to_end(&mut data) { + let len = record.attributes().unwrap().len; + let mut data = Vec::::with_capacity(len); + data.resize(len, 0); + match record.read_exact(&mut data) { Ok(_len) => { match crate::ctap::storage::deserialize_credential(&data) { Some(result) => { @@ -923,8 +937,10 @@ impl ActionManager { Some(crate::basis_change) ) { Ok(mut record) => { - let mut data = Vec::::new(); - match record.read_to_end(&mut data) { + let len = record.attributes().unwrap().len; + let mut data = Vec::::with_capacity(len); + data.resize(len, 0); + match record.read_exact(&mut data) { Ok(_len) => { if let Some(totp) = deserialize_totp(data) { let alg: String = totp.algorithm.into(); From 6b536882491bccc433a9cb1c8f60e687a31fefee Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 2 Aug 2022 01:45:39 +0800 Subject: [PATCH 09/11] fix #195 let's see if we can raise the limit on credentials now that the performance issues have been partially addressed. --- apps/vault/src/ctap/storage.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/apps/vault/src/ctap/storage.rs b/apps/vault/src/ctap/storage.rs index 36375efb6..1cb1514fd 100644 --- a/apps/vault/src/ctap/storage.rs +++ b/apps/vault/src/ctap/storage.rs @@ -67,11 +67,8 @@ pub(crate) const CRED_INITAL_SIZE: usize = 512; const NUM_PAGES: usize = 20; /// The limit is based on the performance of the PDDB to get a query response within -/// the compliance time-out limit. Optimization is needed to improve this number! -/// -/// Note: this might have been fixed with 6f849af2a23e99c98baa4d89ccf07261f4af7854, found -/// a busy-wait regression in an prerequisites server that would explain the slowdown. -const MAX_SUPPORTED_RESIDENTIAL_KEYS: usize = 32; +/// the compliance time-out limit. Note that most keys support about 25 credentials. +const MAX_SUPPORTED_RESIDENTIAL_KEYS: usize = 150; const MAX_PIN_RETRIES: u8 = 8; #[cfg(feature = "with_ctap2_1")] From 2b4ce52a107328a318dd315e034486bdd7688df0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 2 Aug 2022 02:52:04 +0800 Subject: [PATCH 10/11] fix some regressions on empty key listings --- services/pddb/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 387b67990..80412fdb0 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -1172,6 +1172,7 @@ fn wrapped_main() -> ! { Some(list) } Err(e) => { + key_token = None; match e.kind() { std::io::ErrorKind::NotFound => req.code = PddbRequestCode::NotFound, _ => req.code = PddbRequestCode::InternalError, @@ -1237,7 +1238,7 @@ fn wrapped_main() -> ! { Opcode::DictCountInBasis => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut req = buffer.to_original::().unwrap(); - if key_token.is_some() { + if dict_token.is_some() { req.code = PddbRequestCode::AccessDenied; buffer.replace(req).unwrap(); continue; From 3118ebddc9c22789df5424705ab99c3cfae0feb5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 2 Aug 2022 02:52:20 +0800 Subject: [PATCH 11/11] add error code printout on dictlist failure --- services/shellchat/src/cmds/pddb_cmd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index f1f064f6f..ff4e6d7c2 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -244,7 +244,7 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { } } } - Err(_) => write!(ret, "Error encountered listing dictionaries").ok().unwrap_or(()), + Err(e) => write!(ret, "Error encountered listing dictionaries: {:?}", e).ok().unwrap_or(()), } } "churn" => {