Merge branch 'main' into feat/vault-usb-totp-loading

This commit is contained in:
Gianguido Sorà 2022-08-02 15:53:11 +02:00
commit 199d5ddeb5
20 changed files with 289 additions and 327 deletions

1
.gitignore vendored
View file

@ -22,6 +22,7 @@ buildpush.ps1
# local-only routines used to start builds # local-only routines used to start builds
/*.ps1 /*.ps1
/*.bat /*.bat
/*.sh
# notes to myself # notes to myself
/*.txt /*.txt

25
Cargo.lock generated
View file

@ -16,10 +16,11 @@ checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
[[package]] [[package]]
name = "aead" name = "aead"
version = "0.4.3" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b613b8e1e3cf911a086f53f03bf286f52fd7a7258e4fa606f0ef220d39d8877" checksum = "ae06cea71059b6b79d879afcdd237a33ac61afc052fdd605815e6f3916254abf"
dependencies = [ dependencies = [
"crypto-common",
"generic-array", "generic-array",
] ]
@ -36,8 +37,9 @@ dependencies = [
[[package]] [[package]]
name = "aes-gcm-siv" name = "aes-gcm-siv"
version = "0.11.0-pre" version = "0.11.1"
source = "git+https://github.com/RustCrypto/AEADs.git?branch=master#479ae5f8f37d265847da80c4e713597c2c4813ac" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae0784134ba9375416d469ec31e7c5f9fa94405049cf08c5ce5b4698be673e0d"
dependencies = [ dependencies = [
"aead", "aead",
"aes", "aes",
@ -791,9 +793,9 @@ dependencies = [
[[package]] [[package]]
name = "crypto-common" name = "crypto-common"
version = "0.1.3" version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [ dependencies = [
"generic-array", "generic-array",
"typenum", "typenum",
@ -2499,6 +2501,7 @@ dependencies = [
"cipher 0.4.3", "cipher 0.4.3",
"digest 0.9.0", "digest 0.9.0",
"gam", "gam",
"hex 0.4.3",
"hkdf", "hkdf",
"llio", "llio",
"locales", "locales",
@ -2614,9 +2617,9 @@ dependencies = [
[[package]] [[package]]
name = "polyval" name = "polyval"
version = "0.5.3" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8419d2b623c7c0896ff2d5d96e2cb4ede590fed28fcc34934f4c33c036e620a1" checksum = "7ef234e08c11dfcb2e56f79fd70f6f2eb7f025c0ce2333e82f4f0518ecad30c6"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"cpufeatures", "cpufeatures",
@ -3951,11 +3954,11 @@ checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
[[package]] [[package]]
name = "universal-hash" name = "universal-hash"
version = "0.4.1" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f214e8f697e925001e66ec2c6e37a4ef93f0f78c2eed7814394e10c62025b05" checksum = "7d3160b73c9a19f7e2939a2fdad446c57c1bbbbf4d919d3213ff1267a580d8b5"
dependencies = [ dependencies = [
"generic-array", "crypto-common",
"subtle", "subtle",
] ]

View file

@ -594,6 +594,7 @@ impl ActionManager {
il.clear(); il.clear();
match self.mode_cache { match self.mode_cache {
VaultMode::Password => { VaultMode::Password => {
let start = self.tt.elapsed_ms();
let keylist = match self.pddb.borrow().list_keys(VAULT_PASSWORD_DICT, None) { let keylist = match self.pddb.borrow().list_keys(VAULT_PASSWORD_DICT, None) {
Ok(keylist) => keylist, Ok(keylist) => keylist,
Err(e) => { Err(e) => {
@ -609,6 +610,9 @@ impl ActionManager {
Vec::new() 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 { for key in keylist {
match self.pddb.borrow().get( match self.pddb.borrow().get(
VAULT_PASSWORD_DICT, VAULT_PASSWORD_DICT,
@ -618,8 +622,13 @@ impl ActionManager {
Some(crate::basis_change) Some(crate::basis_change)
) { ) {
Ok(mut record) => { Ok(mut record) => {
let mut data = Vec::<u8>::new(); // determine the exact length of the record and read it in one go.
match record.read_to_end(&mut data) { // 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::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => { Ok(_len) => {
if let Some(pw) = storage::PasswordRecord::try_from(data).ok() { if let Some(pw) = storage::PasswordRecord::try_from(data).ok() {
let extra = format!("{}; {}{}", let extra = format!("{}; {}{}",
@ -652,6 +661,7 @@ impl ActionManager {
}, },
} }
} }
log::info!("readout took {} ms for {} elements", self.tt.elapsed_ms() - start, klen);
} }
VaultMode::Fido => { VaultMode::Fido => {
// first assemble U2F records // first assemble U2F records
@ -678,8 +688,10 @@ impl ActionManager {
Some(crate::basis_change) Some(crate::basis_change)
) { ) {
Ok(mut record) => { Ok(mut record) => {
let mut data = Vec::<u8>::new(); let len = record.attributes().unwrap().len;
match record.read_to_end(&mut data) { let mut data = Vec::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => { Ok(_len) => {
if let Some(ai) = deserialize_app_info(data) { if let Some(ai) = deserialize_app_info(data) {
let extra = format!("{}; {}{}", let extra = format!("{}; {}{}",
@ -729,8 +741,10 @@ impl ActionManager {
Some(crate::basis_change) Some(crate::basis_change)
) { ) {
Ok(mut record) => { Ok(mut record) => {
let mut data = Vec::<u8>::new(); let len = record.attributes().unwrap().len;
match record.read_to_end(&mut data) { let mut data = Vec::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => { Ok(_len) => {
match crate::ctap::storage::deserialize_credential(&data) { match crate::ctap::storage::deserialize_credential(&data) {
Some(result) => { Some(result) => {
@ -781,8 +795,10 @@ impl ActionManager {
Some(crate::basis_change) Some(crate::basis_change)
) { ) {
Ok(mut record) => { Ok(mut record) => {
let mut data = Vec::<u8>::new(); let len = record.attributes().unwrap().len;
match record.read_to_end(&mut data) { let mut data = Vec::<u8>::with_capacity(len);
data.resize(len, 0);
match record.read_exact(&mut data) {
Ok(_len) => { Ok(_len) => {
if let Some(totp) = storage::TotpRecord::try_from(data).ok() { if let Some(totp) = storage::TotpRecord::try_from(data).ok() {
let alg: String = totp.algorithm.into(); let alg: String = totp.algorithm.into();

View file

@ -67,11 +67,8 @@ pub(crate) const CRED_INITAL_SIZE: usize = 512;
const NUM_PAGES: usize = 20; const NUM_PAGES: usize = 20;
/// The limit is based on the performance of the PDDB to get a query response within /// 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! /// the compliance time-out limit. Note that most keys support about 25 credentials.
/// const MAX_SUPPORTED_RESIDENTIAL_KEYS: usize = 150;
/// 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;
const MAX_PIN_RETRIES: u8 = 8; const MAX_PIN_RETRIES: u8 = 8;
#[cfg(feature = "with_ctap2_1")] #[cfg(feature = "with_ctap2_1")]

View file

@ -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."
}

View file

@ -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 <hostname> copies the files to a remote host and skips provisioning."
echo " --identity <id_file> 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

View file

@ -24,8 +24,8 @@ aes = {path="../aes"}
root-keys = {path="../root-keys"} root-keys = {path="../root-keys"}
cipher = "0.4.2" cipher = "0.4.2"
bitfield = "0.13.2" bitfield = "0.13.2"
# aes-gcm-siv = {version = "0.11.0-pre", default-features = false, features = ["alloc"]} 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"} # aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"}
llio = {path="../llio"} llio = {path="../llio"}
subtle = {version = "2.4.1", default-features = false} subtle = {version = "2.4.1", default-features = false}
tts-frontend = {path="../tts"} tts-frontend = {path="../tts"}
@ -52,6 +52,8 @@ utralib = { path = "../../utralib"}
rand = "0.7.3" rand = "0.7.3"
rand_chacha = "0.3.1" rand_chacha = "0.3.1"
hex = { version = "0.4.3", default-features = false, features = ["alloc"], optional = true }
[features] [features]
# when selected, physical disk addresses are set to 64 bits, otherwise, they are 32 bits. # 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. # 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 flag decorates any debug paths that might accidentally leak key material
hazardous-debug = [] hazardous-debug = []
test-rekey = [] test-rekey = []
# routines to help with performance profiling of the PDDB in hosted mode
pddb-flamegraph = ["hex"]
default = ["mbbb"] default = ["mbbb"]

View file

@ -125,7 +125,7 @@ pub(crate) enum Opcode {
// routines to list available resources // routines to list available resources
KeyCountInDict = 11, KeyCountInDict = 11,
GetKeyNameAtIndex = 12, // GetKeyNameAtIndex = 12, // superceded by ListKeyV2
DictCountInBasis = 13, DictCountInBasis = 13,
GetDictNameAtIndex = 14, GetDictNameAtIndex = 14,
@ -207,6 +207,9 @@ pub(crate) enum Opcode {
/// Flush the SpaceUpdate log, restoring deniability /// Flush the SpaceUpdate log, restoring deniability
FlushSpaceUpdate = 44, FlushSpaceUpdate = 44,
/// Optimized key listing
ListKeyV2 = 45,
/// This key type could not be decoded /// This key type could not be decoded
InvalidOpcode = u32::MAX as _, InvalidOpcode = u32::MAX as _,
} }
@ -289,9 +292,19 @@ pub struct PddbKeyRequest {
pub result: PddbRequestCode, 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 /// Return codes for Read/Write API calls to the main server
#[repr(u8)] #[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 { pub(crate) enum PddbRetcode {
Uninit = 0, Uninit = 0,
Ok = 1, Ok = 1,

View file

@ -136,12 +136,14 @@ use super::*;
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use core::mem::size_of; use core::mem::size_of;
use std::convert::TryInto; use std::convert::TryInto;
use aes_gcm_siv::{AesGcmSiv, Key}; use aes_gcm_siv::{
use aes_gcm_siv::aead::NewAead; aead::KeyInit,
Aes256GcmSiv,
};
use aes::Aes256; use aes::Aes256;
use aes::cipher::{KeyInit, generic_array::GenericArray}; use aes::cipher::generic_array::GenericArray;
use std::iter::IntoIterator; 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::io::{Result, Error, ErrorKind};
use std::cmp::Reverse; use std::cmp::Reverse;
use std::cmp::Ordering; use std::cmp::Ordering;
@ -379,8 +381,8 @@ impl BasisCache {
} }
dict_set dict_set
} }
pub(crate) fn key_list(&mut self, hw: &mut PddbOs, dict: &str, basis_name: Option<&str>) -> Result<HashSet::<String>> { pub(crate) fn key_list(&mut self, hw: &mut PddbOs, dict: &str, basis_name: Option<&str>) -> Result<BTreeSet::<String>> {
let mut merge_list = HashSet::<String>::new(); let mut merge_list = BTreeSet::<String>::new();
let mut found_dict = false; let mut found_dict = false;
if basis_name.is_some() { if basis_name.is_some() {
if let Some(basis_index) = self.select_basis(basis_name) { if let Some(basis_index) = self.select_basis(basis_name) {
@ -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 /// expressed as a number that needs to be multiplied by DICT_VSIZE to arrive at a virtual address
pub free_dict_offset: Option<u32>, pub free_dict_offset: Option<u32>,
/// the cipher for the basis /// the cipher for the basis
pub cipher: AesGcmSiv::<Aes256>, pub cipher: Aes256GcmSiv,
/// derived cipher for encrypting PTEs -- cache it, so we can save the time cost of constructing the cipher key schedule /// derived cipher for encrypting PTEs -- cache it, so we can save the time cost of constructing the cipher key schedule
pub cipher_ecb: Aes256, 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 /// 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`. /// discover the location of the `large_alloc_ptr`.
pub(crate) fn mount(hw: &mut PddbOs, name: &str, key: &BasisKeys, lazy: bool, policy: BasisRetentionPolicy) -> Option<BasisCacheEntry> { pub(crate) fn mount(hw: &mut PddbOs, name: &str, key: &BasisKeys, lazy: bool, policy: BasisRetentionPolicy) -> Option<BasisCacheEntry> {
if let Some(basis_map) = hw.pt_scan_key(&key.pt, &key.data, name) { if let Some(basis_map) = hw.pt_scan_key(&key.pt, &key.data, name) {
let cipher = AesGcmSiv::<Aes256>::new(Key::from_slice(&key.data)); let cipher = Aes256GcmSiv::new(&key.data.into());
let aad = hw.data_aad(name); let aad = hw.data_aad(name);
// get the first page, where the basis root is guaranteed to be // 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()) { if let Some(root_page) = basis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) {

View file

@ -4,9 +4,8 @@ use super::*;
use std::num::NonZeroU32; use std::num::NonZeroU32;
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use core::mem::size_of; use core::mem::size_of;
use aes_gcm_siv::AesGcmSiv; use aes_gcm_siv::Aes256GcmSiv;
use aes::Aes256; use std::collections::{HashMap, BinaryHeap, BTreeSet};
use std::collections::{HashMap, BinaryHeap, HashSet};
use std::io::{Result, Error, ErrorKind}; use std::io::{Result, Error, ErrorKind};
use bitfield::bitfield; use bitfield::bitfield;
use std::cmp::{Ordering, Reverse}; use std::cmp::{Ordering, Reverse};
@ -89,7 +88,7 @@ impl DictCacheEntry {
/// Requires a descriptor for the hardware, and our virtual to physical page mapping. /// 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. /// 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. /// 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::<VirtAddr, PhysPage>, cipher: &AesGcmSiv::<Aes256>) -> VirtAddr { pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::<VirtAddr, PhysPage>, cipher: &Aes256GcmSiv) -> VirtAddr {
let mut try_entry = 1; let mut try_entry = 1;
let mut key_count = 0; let mut key_count = 0;
let mut alloc_top = VirtAddr::new(LARGE_POOL_START).unwrap(); 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. /// 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. /// 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::<VirtAddr, PhysPage>, cipher: &AesGcmSiv::<Aes256>, merge_list: &mut HashSet<String>) { pub(crate) fn key_list(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::<VirtAddr, PhysPage>, cipher: &Aes256GcmSiv, merge_list: &mut BTreeSet<String>) {
// ensure that the key cache is filled // ensure that the key cache is filled
if self.keys.len() < self.key_count as usize { if self.keys.len() < self.key_count as usize {
self.fill(hw, v2p_map, cipher); 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 /// 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 /// 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. /// 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::<VirtAddr, PhysPage>, cipher: &AesGcmSiv::<Aes256>, pub(crate) fn ensure_key_entry(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::<VirtAddr, PhysPage>, cipher: &Aes256GcmSiv,
name_str: &str) -> bool { name_str: &str) -> bool {
// only fill if the key isn't in the cache. // only fill if the key isn't in the cache.
if !self.keys.contains_key(name_str) { 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::<VirtAddr, PhysPage>, cipher: &AesGcmSiv::<Aes256>, fn try_fill_small_key(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::<VirtAddr, PhysPage>, cipher: &Aes256GcmSiv,
data_cache: &mut PlaintextCache, key_name: &str) { data_cache: &mut PlaintextCache, key_name: &str) {
if let Some(kcache) = self.keys.get_mut(key_name) { if let Some(kcache) = self.keys.get_mut(key_name) {
if let Some(pool_index) = small_storage_index_from_key(&kcache, self.index) { 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 /// 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 /// correct DictCacheEntry and issue the `key_update` to it; for multiple updates, then multiple calls to
/// multiple DictCacheEntry are required. /// multiple DictCacheEntry are required.
pub fn key_update(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::<VirtAddr, PhysPage>, cipher: &AesGcmSiv::<Aes256>, pub fn key_update(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::<VirtAddr, PhysPage>, cipher: &Aes256GcmSiv,
name: &str, data: &[u8], offset: usize, alloc_hint:Option<usize>, truncate: bool, large_alloc_ptr: PageAlignedVa) -> Result <PageAlignedVa> { name: &str, data: &[u8], offset: usize, alloc_hint:Option<usize>, truncate: bool, large_alloc_ptr: PageAlignedVa) -> Result <PageAlignedVa> {
self.age = self.age.saturating_add(1); self.age = self.age.saturating_add(1);
self.clean = false; 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, /// 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 /// the routine has no effect, and does not report an error. Small keys are not immediately
/// overwritten in paranoid mode, but large keys are. /// overwritten in paranoid mode, but large keys are.
pub fn key_remove(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::<VirtAddr, PhysPage>, cipher: &AesGcmSiv::<Aes256>, pub fn key_remove(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::<VirtAddr, PhysPage>, cipher: &Aes256GcmSiv,
name_str: &str, paranoid: bool) { name_str: &str, paranoid: bool) {
if paranoid { if paranoid {
// large records are paranoid-erased, by default, because of the pool-reuse problem. // 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 /// Observation: given the dictionary index and the small key pool index, we know exactly
/// the virtual address of the data pool. /// the virtual address of the data pool.
pub(crate) fn sync_small_pool(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::<VirtAddr, PhysPage>, cipher: &AesGcmSiv::<Aes256>) -> bool { pub(crate) fn sync_small_pool(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::<VirtAddr, PhysPage>, cipher: &Aes256GcmSiv) -> bool {
for (index, entry) in self.small_pool.iter_mut().enumerate() { for (index, entry) in self.small_pool.iter_mut().enumerate() {
if !entry.clean { if !entry.clean {
let pool_vaddr = VirtAddr::new(small_storage_base_vaddr_from_indices(self.index, index)).unwrap(); 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<PhysPage>, pub(crate) tag: Option<PhysPage>,
} }
impl PlaintextCache { impl PlaintextCache {
pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::<VirtAddr, PhysPage>, cipher: &AesGcmSiv::<Aes256>, aad: &[u8], pub fn fill(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::<VirtAddr, PhysPage>, cipher: &Aes256GcmSiv, aad: &[u8],
req_vaddr: VirtAddr req_vaddr: VirtAddr
) { ) {
if let Some(pp) = v2p_map.get(&req_vaddr) { if let Some(pp) = v2p_map.get(&req_vaddr) {

View file

@ -1,8 +1,8 @@
use std::convert::TryInto; use std::convert::TryInto;
use crate::*; use crate::*;
use aes_gcm_siv::{AesGcmSiv, Nonce, Key, Tag}; use aes_gcm_siv::{Nonce, Tag, Aes256GcmSiv};
use aes_gcm_siv::aead::{Aead, NewAead, Payload}; use aes_gcm_siv::aead::{Aead, Payload};
use aes::{Aes256, Block, BLOCK_SIZE}; use aes::{Aes256, Block, BLOCK_SIZE};
use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit, generic_array::GenericArray}; use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit, generic_array::GenericArray};
use root_keys::api::AesRootkeyType; use root_keys::api::AesRootkeyType;
@ -97,7 +97,7 @@ pub(crate) struct BasisKeys {
struct MigrationCiphers { struct MigrationCiphers {
pt_ecb: Aes256, pt_ecb: Aes256,
data_gcm_siv: AesGcmSiv<Aes256>, data_gcm_siv: Aes256GcmSiv,
data_key: [u8; 32], data_key: [u8; 32],
aad_incoming: Vec::<u8>, aad_incoming: Vec::<u8>,
aad_local: Vec::<u8>, aad_local: Vec::<u8>,
@ -497,7 +497,7 @@ impl PddbOs {
pp.set_space_state(SpaceState::Used); pp.set_space_state(SpaceState::Used);
// handle conflicting journal versions here // handle conflicting journal versions here
if let Some(prev_page) = map.get(&pte.vaddr()) { if let Some(prev_page) = map.get(&pte.vaddr()) {
let cipher = AesGcmSiv::<Aes256>::new(Key::from_slice(data_key)); let cipher = Aes256GcmSiv::new(data_key.into());
let aad = self.data_aad(basis_name); let aad = self.data_aad(basis_name);
if pte.vaddr() != VirtAddr::new(VPAGE_SIZE as u64).unwrap() { if pte.vaddr() != VirtAddr::new(VPAGE_SIZE as u64).unwrap() {
// handle case that there is a conflict in data areas // handle case that there is a conflict in data areas
@ -761,8 +761,7 @@ impl PddbOs {
fn fast_space_write(&mut self, fs: &FastSpace) { fn fast_space_write(&mut self, fs: &FastSpace) {
self.syskey_ensure(); self.syskey_ensure();
if let Some(system_basis_key) = &self.system_basis_key { if let Some(system_basis_key) = &self.system_basis_key {
let key = Key::from_slice(&system_basis_key.data); let cipher = Aes256GcmSiv::new(&system_basis_key.data.into());
let cipher = AesGcmSiv::<Aes256>::new(key);
let nonce_array = self.entropy.borrow_mut().get_nonce(); let nonce_array = self.entropy.borrow_mut().get_nonce();
let nonce = Nonce::from_slice(&nonce_array); let nonce = Nonce::from_slice(&nonce_array);
let fs_ser: &[u8] = fs.deref(); let fs_ser: &[u8] = fs.deref();
@ -974,8 +973,7 @@ impl PddbOs {
msg: &fscb_buf, msg: &fscb_buf,
aad: &aad, aad: &aad,
}; };
let key = Key::from_slice(&system_key.data); let cipher = Aes256GcmSiv::new(&system_key.data.into());
let cipher = AesGcmSiv::<Aes256>::new(key);
match cipher.decrypt(Nonce::from_slice(&fscb_slice[page_start..page_start + size_of::<Nonce>()]), payload) { match cipher.decrypt(Nonce::from_slice(&fscb_slice[page_start..page_start + size_of::<Nonce>()]), payload) {
Ok(msg) => { Ok(msg) => {
log::info!("decrypted: {}, FastSpace size: {}", msg.len(), size_of::<FastSpace>()); log::info!("decrypted: {}, FastSpace size: {}", msg.len(), size_of::<FastSpace>());
@ -1333,7 +1331,7 @@ impl PddbOs {
/// returns a decrypted page that still includes the journal number at the very beginning /// 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 /// 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. /// just index past it.
pub(crate) fn data_decrypt_page(&self, cipher: &AesGcmSiv::<Aes256>, aad: &[u8], page: &PhysPage) -> Option<Vec::<u8>> { pub(crate) fn data_decrypt_page(&self, cipher: &Aes256GcmSiv, aad: &[u8], page: &PhysPage) -> Option<Vec::<u8>> {
let ct_slice = &self.pddb_mr.as_slice()[ 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 * PAGE_SIZE ..
self.data_phys_base.as_usize() + (page.page_number() as usize + 1) * 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); log::debug!("found kcom_nonce of {:x?}", nonce_comm);
let (kenc, kcom) = self.kcom_func(key.try_into().unwrap(), &nonce_comm); let (kenc, kcom) = self.kcom_func(key.try_into().unwrap(), &nonce_comm);
let cipher = AesGcmSiv::<Aes256>::new(Key::from_slice(&kenc)); let cipher = Aes256GcmSiv::new(&kenc.into());
// Attempt decryption. This is None on failure // Attempt decryption. This is None on failure
let plaintext = cipher.decrypt( 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 /// `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::<Aes256>, 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::<JournalType>(), "did not get a page-sized region to patch"); assert!(data.len() == VPAGE_SIZE + size_of::<JournalType>(), "did not get a page-sized region to patch");
let j = JournalType::from_le_bytes(data[..size_of::<JournalType>()].try_into().unwrap()).saturating_add(1); let j = JournalType::from_le_bytes(data[..size_of::<JournalType>()].try_into().unwrap()).saturating_add(1);
for (&src, dst) in j.to_le_bytes().iter().zip(data[..size_of::<JournalType>()].iter_mut()) { *dst = src; } for (&src, dst) in j.to_le_bytes().iter().zip(data[..size_of::<JournalType>()].iter_mut()) { *dst = src; }
@ -1459,7 +1457,7 @@ impl PddbOs {
self.trng_slice(&mut kcom_nonce); self.trng_slice(&mut kcom_nonce);
// generates the encryption and commit keys // generates the encryption and commit keys
let (kenc, kcom) = self.kcom_func(key.try_into().unwrap(), &kcom_nonce); let (kenc, kcom) = self.kcom_func(key.try_into().unwrap(), &kcom_nonce);
let cipher = AesGcmSiv::<Aes256>::new(Key::from_slice(&kenc)); let cipher = Aes256GcmSiv::new(&kenc.into());
let ciphertext = cipher.encrypt( let ciphertext = cipher.encrypt(
&nonce, &nonce,
Payload { Payload {
@ -1885,7 +1883,7 @@ impl PddbOs {
pub(crate) fn pddb_generate_used_map(&self, cache: &Vec::<BasisCacheEntry>) -> Option<BinaryHeap<Reverse<u32>>> { pub(crate) fn pddb_generate_used_map(&self, cache: &Vec::<BasisCacheEntry>) -> Option<BinaryHeap<Reverse<u32>>> {
if let Some(all_keys) = self.pddb_get_all_keys(&cache) { if let Some(all_keys) = self.pddb_get_all_keys(&cache) {
let mut page_heap = BinaryHeap::new(); 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 { for (basis_keys, name) in all_keys {
// scan the disclosed bases // scan the disclosed bases
if let Some(map) = self.pt_scan_key(&basis_keys.pt, &basis_keys.data, &name) { if let Some(map) = self.pt_scan_key(&basis_keys.pt, &basis_keys.data, &name) {
@ -1909,7 +1907,7 @@ impl PddbOs {
for pp in self.fspace_cache.iter() { for pp in self.fspace_cache.iter() {
if pp.space_state() == SpaceState::Used || pp.space_state() == SpaceState::MaybeUsed { if pp.space_state() == SpaceState::Used || pp.space_state() == SpaceState::MaybeUsed {
if !page_check.insert(pp.page_number()) { 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 { } else {
page_heap.push(Reverse(pp.page_number())); page_heap.push(Reverse(pp.page_number()));
log::info!("FSCB contained {}, but not yet committed to disk; added to page_heap", pp.page_number()); log::info!("FSCB contained {}, but not yet committed to disk; added to page_heap", pp.page_number());
@ -1982,7 +1980,7 @@ impl PddbOs {
log::warn!("pt_scan for basis {} failed, data may be lost", name); log::warn!("pt_scan for basis {} failed, data may be lost", name);
} }
let pt_ecb = Aes256::new(&GenericArray::from_slice(&basis_keys.pt)); let pt_ecb = Aes256::new(&GenericArray::from_slice(&basis_keys.pt));
let data_gcm_siv = AesGcmSiv::<Aes256>::new(Key::from_slice(&basis_keys.data)); let data_gcm_siv = Aes256GcmSiv::new(&basis_keys.data.into());
self.dna_mode = DnaMode::Normal; self.dna_mode = DnaMode::Normal;
let aad_local = self.data_aad(&name); let aad_local = self.data_aad(&name);
self.dna_mode = DnaMode::Migration; self.dna_mode = DnaMode::Migration;
@ -2579,10 +2577,10 @@ impl PddbOs {
aad_v2: &Vec::<u8>, aad_v2: &Vec::<u8>,
key_v1: &[u8; AES_KEYSIZE], // needed to decrypt page with commit key_v1: &[u8; AES_KEYSIZE], // needed to decrypt page with commit
cipher_pt_v1: &Aes256, cipher_pt_v1: &Aes256,
cipher_data_v1: &AesGcmSiv::<Aes256>, cipher_data_v1: &Aes256GcmSiv,
key_data_v2: &[u8; AES_KEYSIZE], // this is needed because we have to do a key commitment key_data_v2: &[u8; AES_KEYSIZE], // this is needed because we have to do a key commitment
cipher_pt_v2: &Aes256, cipher_pt_v2: &Aes256,
cipher_data_v2: &AesGcmSiv::<Aes256>, cipher_data_v2: &Aes256GcmSiv,
used_pages: &mut BinaryHeap::<Reverse<u32>>, used_pages: &mut BinaryHeap::<Reverse<u32>>,
) -> bool { ) -> bool {
let blank = [0xffu8; aes::BLOCK_SIZE]; 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 // 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))); 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 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::<Aes256>::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); let aad_v2 = self.data_aad(PDDB_DEFAULT_SYSTEM_BASIS);
// now wrap the key for storage // now wrap the key for storage
let wrapped_key = self.rootkeys.wrap_key(&system_basis_key).expect("Internal error wrapping our encryption key"); 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 // now we have a copy of the AES key necessary to re-encrypt all the basis
// build the data cipher for v1 pages // build the data cipher for v1 pages
let data_cipher_v1 = AesGcmSiv::<Aes256>::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); let aad_v1 = data_aad_v1(&self, PDDB_DEFAULT_SYSTEM_BASIS);
// track used pages so we can create the FSCB at the end // 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_v1 = data_aad_v1(&self, bname.first().as_str());
let basis_aad_v2 = self.data_aad(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_pt_cipher_v1 = Aes256::new(GenericArray::from_slice(&basis_key_v1));
let basis_data_cipher_v1 = AesGcmSiv::<Aes256>::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_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_pt_cipher_2 = Aes256::new(GenericArray::from_slice(&basis_keys.pt));
let basis_data_cipher_2 = AesGcmSiv::<Aes256>::new(Key::from_slice(&basis_keys.data)); let basis_data_cipher_2 = Aes256GcmSiv::new(Key::from_slice(&basis_keys.data));
// perform the migration // perform the migration
if self.migration_v1_to_v2_inner( if self.migration_v1_to_v2_inner(
&basis_aad_v1, &basis_aad_v2, &basis_aad_v1, &basis_aad_v2,

View file

@ -524,34 +524,56 @@ impl Pddb {
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
let response = buf.to_original::<PddbDictRequest, _>().unwrap(); let response = buf.to_original::<PddbDictRequest, _>().unwrap();
let count = match response.code { match response.code {
PddbRequestCode::NoErr => response.index, PddbRequestCode::NoErr => (),
PddbRequestCode::NotFound => return Err(Error::new(ErrorKind::NotFound, "dictionary not found")), PddbRequestCode::NotFound => return Err(Error::new(ErrorKind::NotFound, "dictionary not found")),
_ => return Err(Error::new(ErrorKind::Other, "Internal error")), _ => 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. // v2 key listing packs the key list into a larger [u8] field that should cut down on the number of messages
// it's absolutely important that you access every entry, and the highest index last, because // required to list a large dictionary by about a factor of 50.
// that is how the server knows you've finished with the list-out.
let mut key_list = Vec::<String>::new(); let mut key_list = Vec::<String>::new();
for index in 0..count { // just make sure we didn't screw up the sizing of this record
let request = PddbDictRequest { assert!(core::mem::size_of::<PddbKeyList>() < 4096);
basis_specified: basis_name.is_some(), loop {
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname), let request = PddbKeyList {
dict: xous_ipc::String::<DICT_NAME_LEN>::from_str(dict_name),
key: xous_ipc::String::<KEY_NAME_LEN>::new(),
index,
code: PddbRequestCode::Uninit,
token, token,
end: false,
retcode: PddbRetcode::Uninit,
data: [0u8; MAX_PDDBKLISTLEN]
}; };
let mut buf = Buffer::into_buf(request) let mut buf = Buffer::into_buf(request)
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; .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")))?; .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
let response = buf.to_original::<PddbDictRequest, _>().unwrap(); let response = buf.as_flat::<PddbKeyList, _>().unwrap();
match response.code { match response.retcode {
PddbRequestCode::NoErr => key_list.push(String::from(response.key.as_str().expect("utf-8 parse error in key name"))), ArchivedPddbRetcode::Ok => (),
_ => return Err(Error::new(ErrorKind::Other, "Internal error")), 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) Ok(key_list)
} }

View file

@ -381,13 +381,16 @@ mod tests;
#[allow(unused_imports)] #[allow(unused_imports)]
use tests::*; use tests::*;
#[cfg(feature="pddb-flamegraph")]
mod profiling;
use num_traits::*; use num_traits::*;
use xous::{send_message, Message, msg_blocking_scalar_unpack}; use xous::{send_message, Message, msg_blocking_scalar_unpack};
use xous_ipc::Buffer; use xous_ipc::Buffer;
use core::cell::RefCell; use core::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use std::thread; use std::thread;
use std::collections::HashMap; use std::collections::{HashMap, BTreeSet};
use std::io::ErrorKind; use std::io::ErrorKind;
use core::fmt::Write; use core::fmt::Write;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
@ -556,7 +559,7 @@ fn wrapped_main() -> ! {
} }
}); });
// main server loop // main server loop
let mut key_list = Vec::<String>::new(); // storage for key lists let mut key_list: Option::<BTreeSet::<String>> = None; // storage for key lists
let mut key_token: Option<[u32; 4]> = None; let mut key_token: Option<[u32; 4]> = None;
let mut dict_list = Vec::<String>::new(); // storage for dict lists let mut dict_list = Vec::<String>::new(); // storage for dict lists
let mut dict_token: Option<[u32; 4]> = None; let mut dict_token: Option<[u32; 4]> = None;
@ -663,6 +666,8 @@ fn wrapped_main() -> ! {
latest_cache = basis_cache.cache_size(); latest_cache = basis_cache.cache_size();
log::info!("PDDB post-mount caching stats: {} heap, {} cache", latest_heap, latest_cache); log::info!("PDDB post-mount caching stats: {} heap, {} cache", latest_heap, latest_cache);
scrub_run.store(true, Ordering::SeqCst); scrub_run.store(true, Ordering::SeqCst);
#[cfg(feature="pddb-flamegraph")]
profiling::do_query_work();
} }
} }
}), }),
@ -1153,7 +1158,6 @@ fn wrapped_main() -> ! {
continue; continue;
} }
key_token = Some(req.token); key_token = Some(req.token);
key_list.clear();
let bname = if req.basis_specified { let bname = if req.basis_specified {
Some(req.basis.as_str().unwrap()) Some(req.basis.as_str().unwrap())
} else { } else {
@ -1161,58 +1165,65 @@ fn wrapped_main() -> ! {
}; };
let dict = req.dict.as_str().expect("dict utf-8 decode error"); let dict = req.dict.as_str().expect("dict utf-8 decode error");
log::debug!("counting keys in dict {} basis {:?}", dict, bname); 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) => { Ok(list) => {
log::debug!("count: {}", list.len()); 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; req.code = PddbRequestCode::NoErr;
Some(list)
} }
Err(e) => { Err(e) => {
key_token = None; key_token = None;
key_list.clear();
match e.kind() { match e.kind() {
std::io::ErrorKind::NotFound => req.code = PddbRequestCode::NotFound, std::io::ErrorKind::NotFound => req.code = PddbRequestCode::NotFound,
_ => req.code = PddbRequestCode::InternalError, _ => req.code = PddbRequestCode::InternalError,
} }
None
} }
} };
buffer.replace(req).unwrap(); 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 buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut req = buffer.to_original::<PddbDictRequest, _>().unwrap(); let mut req = buffer.to_original::<PddbKeyList, _>().unwrap();
if let Some(token) = key_token { if let Some(token) = key_token {
if req.token != token { if req.token != token {
req.code = PddbRequestCode::AccessDenied; req.retcode = PddbRetcode::AccessDenied;
} else { } else {
if req.index >= key_list.len() as u32 { if let Some(klist) = &mut key_list {
req.code = PddbRequestCode::InternalError; let mut index = 0;
loop {
let keyname =
if let Some(keyname) = klist.iter().next() {
keyname.to_string()
} else { } else {
req.key = xous_ipc::String::<KEY_NAME_LEN>::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_token = None;
key_list.clear(); 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 { } else {
log::debug!("multiple concurrent requests detected, returning error"); log::debug!("multiple concurrent requests detected, returning error");
req.code = PddbRequestCode::AccessDenied; req.retcode = PddbRetcode::AccessDenied;
} }
buffer.replace(req).unwrap(); buffer.replace(req).unwrap();
} }
@ -1227,7 +1238,7 @@ fn wrapped_main() -> ! {
Opcode::DictCountInBasis => { Opcode::DictCountInBasis => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut req = buffer.to_original::<PddbDictRequest, _>().unwrap(); let mut req = buffer.to_original::<PddbDictRequest, _>().unwrap();
if key_token.is_some() { if dict_token.is_some() {
req.code = PddbRequestCode::AccessDenied; req.code = PddbRequestCode::AccessDenied;
buffer.replace(req).unwrap(); buffer.replace(req).unwrap();
continue; continue;
@ -1291,7 +1302,7 @@ fn wrapped_main() -> ! {
if let Some(rec) = token_dict.get(&token) { if let Some(rec) = token_dict.get(&token) {
for basis in basis_cache.access_list().iter() { for basis in basis_cache.access_list().iter() {
let temp = if let Some (name) = &rec.basis {Some(name)} else {Some(basis)}; 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, match basis_cache.key_read(&mut pddb_os,
&rec.dict, &rec.key, &rec.dict, &rec.key,
&mut pbuf.data[..pbuf.len as usize], Some(pbuf.position as usize), &mut pbuf.data[..pbuf.len as usize], Some(pbuf.position as usize),

View file

@ -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::<fn()>
) {
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");
});
}

View file

@ -47,8 +47,8 @@ jtag = {path = "../jtag"}
tts-frontend = {path="../tts"} tts-frontend = {path="../tts"}
# backups # backups
# aes-gcm-siv = {version = "0.11.0-pre", default-features = false, features = ["alloc"]} 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"} #aes-gcm-siv = {git="https://github.com/RustCrypto/AEADs.git", branch="master"}
subtle = {version = "2.4.1", default-features = false} subtle = {version = "2.4.1", default-features = false}
usb-device-xous = {path="../usb-device-xous"} usb-device-xous = {path="../usb-device-xous"}
keyboard = {path="../keyboard"} keyboard = {path="../keyboard"}

View file

@ -1,9 +1,10 @@
use zeroize::Zeroize; use zeroize::Zeroize;
use core::mem::size_of; use core::mem::size_of;
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use aes_gcm_siv::{AesGcmSiv, Nonce, Key, Tag}; use aes_gcm_siv::{
use aes_gcm_siv::aead::{Aead, NewAead, Payload}; aead::{KeyInit, Aead, Payload},
use aes::Aes256; Aes256GcmSiv, Tag, Nonce
};
use subtle::ConstantTimeEq; use subtle::ConstantTimeEq;
use crate::{BackupHeader, BackupOp}; use crate::{BackupHeader, BackupOp};
@ -144,7 +145,7 @@ pub(crate) fn create_backup(
trng.fill_bytes(&mut kcom_nonce); trng.fill_bytes(&mut kcom_nonce);
let (kenc, kcom) = kcom_func(&key.0, &kcom_nonce); let (kenc, kcom) = kcom_func(&key.0, &kcom_nonce);
let cipher = AesGcmSiv::<Aes256>::new(Key::from_slice(&kenc.0)); let cipher = Aes256GcmSiv::new(&kenc.0.into());
// the backup data // the backup data
let mut backup_data_pt = BackupDataPt::default(); let mut backup_data_pt = BackupDataPt::default();
@ -186,7 +187,7 @@ pub(crate) fn restore_backup(
backup: &BackupDataCt backup: &BackupDataCt
) -> Option<BackupDataPt> { ) -> Option<BackupDataPt> {
let (kenc, kcom) = kcom_func(&key.0, &backup.commit_nonce); let (kenc, kcom) = kcom_func(&key.0, &backup.commit_nonce);
let cipher = AesGcmSiv::<Aes256>::new(Key::from_slice(&kenc.0)); let cipher = Aes256GcmSiv::new(&kenc.0.into());
// Attempt decryption. This is None on failure // Attempt decryption. This is None on failure
let plaintext = cipher.decrypt( let plaintext = cipher.decrypt(

View file

@ -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" => { "churn" => {

View file

@ -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"

View file

@ -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

View file

@ -382,6 +382,33 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Some(&["--features", "renode-bypass"]), 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") => { Some("renode-aes-test") => {
generate_app_menus(&Vec::<String>::new()); generate_app_menus(&Vec::<String>::new());
renode_image(false, &aestest_pkgs, &[], None, None)? renode_image(false, &aestest_pkgs, &[], None, None)?