test framework in place to look at multiple basis

found one bug already, need to refactor how PT encryptor is
handled
This commit is contained in:
bunnie 2021-11-28 23:54:58 +08:00
parent 5a12e83ff2
commit c15841cfdc
6 changed files with 210 additions and 68 deletions

View file

@ -235,6 +235,21 @@ impl DerefMut for BasisRoot {
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub(crate) enum BasisRetentionPolicy {
Persist,
ClearAfterSleeps(u32),
TimeOutSecs(u32),
}
impl BasisRetentionPolicy {
pub(crate) fn derive_init_state(&self) -> u32 {
match self {
BasisRetentionPolicy::Persist => 0,
BasisRetentionPolicy::ClearAfterSleeps(sleeps) => *sleeps,
BasisRetentionPolicy::TimeOutSecs(secs) => *secs,
}
}
}
/// A list of open Basis that we can use to search and operate upon. Sort of the "root" data structure of the PDDB.
///
/// Note to self: it's tempting to integrate the "hw" parameter (the pointer to the PddbOs structure). However, this
@ -246,14 +261,8 @@ pub(crate) struct BasisCache {
}
impl BasisCache {
pub(crate) fn new() -> Self {
BasisCache { cache: Vec::new(), }
BasisCache { cache: Vec::new() }
}
pub(crate) fn basis_add(&mut self, basis: BasisCacheEntry) {
self.cache.push(basis);
}
// placeholder reminder: deleting a basis is a bit more complicated, as it requires
// syncing its contents.
fn select_basis(&mut self, basis_name: Option<&str>) -> Option<usize> {
if self.cache.len() == 0 {
log::error!("Can't select basis: PDDB is not mounted");
@ -708,6 +717,127 @@ impl BasisCache {
}
}
/// this largely copies code from the pddb_mount() routine. Perhaps this should be modified a little bit to
/// re-use that code. However, there are material differences in how the passwords are handled between
/// these two methods, so the API calls are different. pddb_mount mounts the system basis with the intention
/// of making it persistent, and assuming you're coming up from a blank slate. This routine makes no such
/// assumptions and allows one to specify a persistence.
pub(crate) fn basis_unlock(&mut self, hw: &mut PddbOs, name: &str, password: &str,
policy: BasisRetentionPolicy) -> Option<BasisCacheEntry> {
let basis_key = hw.basis_derive_key(name, password);
if let Some(basis_map) = hw.pt_scan_key(&basis_key, name) {
let cipher = Aes256GcmSiv::new(Key::from_slice(&basis_key));
let aad = hw.data_aad(name);
if let Some(root_page) = basis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) {
let vpage = match hw.data_decrypt_page(&cipher, &aad, root_page) {
Some(data) => data,
None => {log::error!("Could not find basis {} root", name); return None;},
};
let mut basis_root = BasisRoot::default();
for (&src, dst) in vpage[size_of::<JournalType>()..].iter().zip(basis_root.deref_mut().iter_mut()) {
*dst = src;
}
if basis_root.magic != PDDB_MAGIC {
log::error!("Basis root did not deserialize correctly, unrecoverable error.");
return None;
}
if basis_root.version != PDDB_VERSION {
log::error!("PDDB version mismatch in system basis root. Unrecoverable error.");
return None;
}
let basis_name = cstr_to_string(&basis_root.name.0);
if basis_name != name {
log::error!("PDDB mount requested {}, but got {}; aborting.", name, basis_name);
return None;
}
log::info!("Basis {} record found, generating cache entry", name);
BasisCacheEntry::mount(hw, &basis_name, &basis_key, false, policy)
} else {
None
}
} else {
None
}
}
/// this creates a basis entry in the PDDB, which should then be "mounted" to activate it.
/// similar to how you might use fdisk to create a partition, but you still must call mount to access it.
pub(crate) fn basis_create(&mut self, hw: &mut PddbOs, name: &str, password: &str) -> Result<()> {
if !hw.fast_space_ensure_next_log() {
return Err(Error::new(ErrorKind::OutOfMemory, "no free space to create basis"));
};
if !hw.ensure_fast_space_alloc(1, &self.cache) {
return Err(Error::new(ErrorKind::OutOfMemory, "no free space to create basis"));
};
let basis_key = hw.basis_derive_key(name, password);
let mut basis_v2p_map = HashMap::<VirtAddr, PhysPage>::new();
let basis_root = BasisRoot {
magic: PDDB_MAGIC,
version: PDDB_VERSION,
name: BasisRootName::try_from_str(name).unwrap(),
age: 0,
num_dictionaries: 0
};
// allocate one page for the basis root
if let Some(alloc) = hw.try_fast_space_alloc() {
let mut rpte = alloc.clone();
rpte.set_clean(true); // it's not clean _right now_ but it will be by the time this routine is done...
rpte.set_valid(true);
let va = VirtAddr::new((1 * VPAGE_SIZE) as u64).unwrap(); // page 1 is where the root goes, by definition
log::info!("adding basis {}: va {:x?} with pte {:?}", name, va, rpte);
basis_v2p_map.insert(va, rpte);
} else {
return Err(Error::new(ErrorKind::Other, "Space reservation failed"));
}
let aad = basis_root.aad(hw.dna());
let pp = basis_v2p_map.get(&VirtAddr::new(1 * VPAGE_SIZE as u64).unwrap()).unwrap();
assert!(pp.valid(), "v2p returned an invalid page");
let journal_bytes = (0 as u32).to_le_bytes();
let slice_iter =
journal_bytes.iter() // journal rev
.chain(basis_root.as_ref().iter());
let mut block = [0 as u8; VPAGE_SIZE + size_of::<JournalType>()];
for (&src, dst) in slice_iter.zip(block.iter_mut()) {
*dst = src;
}
let key = Key::clone_from_slice(&basis_key);
hw.data_encrypt_and_patch_page(&Aes256GcmSiv::new(&key), &aad, &mut block, &pp);
Ok(())
}
pub(crate) fn basis_list(&self) -> Vec<String> {
let mut ret = Vec::new();
for bcache in &self.cache {
ret.push(bcache.name.clone());
}
ret
}
pub(crate) fn basis_add(&mut self, basis: BasisCacheEntry) {
self.cache.push(basis);
}
pub(crate) fn basis_unmount(&mut self, hw: &mut PddbOs, mut basis: BasisCacheEntry) -> Result<()> {
basis.sync(hw)?;
self.cache.retain(|x| x.name != basis.name);
Ok(())
}
/// note: you can "delete" a basis simply by forgetting its password, but this is more thorough.
/// there might also need to be a variant to make which is a "change my password" function, but that is actually
/// surprisingly hard.
pub(crate) fn basis_delete(&mut self, hw: &mut PddbOs, mut basis: BasisCacheEntry) {
let mut temp: [u8; PAGE_SIZE] = [0; PAGE_SIZE];
for page in basis.v2p_map.values_mut() {
hw.trng_slice(&mut temp);
hw.patch_data(&temp, page.page_number() * PAGE_SIZE as u32);
hw.fast_space_free(page);
}
basis.pt_sync(hw);
}
pub(crate) fn sync(&mut self, hw: &mut PddbOs, basis_name: Option<&str>) -> Result<()> {
if basis_name.is_some() {
if let Some(basis_index) = self.select_basis(basis_name) {
@ -750,13 +880,17 @@ pub(crate) struct BasisCacheEntry {
pub journal: u32,
/// current allocation pointer for the "large" pool. This just keeps incrementing "forever".
pub large_alloc_ptr: Option<PageAlignedVa>,
/// retiention policy
pub policy: BasisRetentionPolicy,
/// rention state
pub policy_state: u32,
}
impl BasisCacheEntry {
/// given a pointer to the hardware, name of the basis, and its cryptographic key, try to derive
/// the basis. If `lazy` is true, it stops with the minimal amount of effort to respond to a query.
/// If it `lazy` is false, it will populate the dictionary cache and key cache entries, as well as
/// discover the location of the `large_alloc_ptr`.
pub(crate) fn mount(hw: &mut PddbOs, name: &str, key: &[u8; AES_KEYSIZE], lazy: bool) -> Option<BasisCacheEntry> {
pub(crate) fn mount(hw: &mut PddbOs, name: &str, key: &[u8; AES_KEYSIZE], lazy: bool, policy: BasisRetentionPolicy) -> Option<BasisCacheEntry> {
if let Some(basis_map) = hw.pt_scan_key(key, name) {
let cipher = Aes256GcmSiv::new(Key::from_slice(key));
let aad = hw.data_aad(name);
@ -798,6 +932,8 @@ impl BasisCacheEntry {
v2p_map: basis_map,
journal: u32::from_le_bytes(vpage[..size_of::<JournalType>()].try_into().unwrap()),
large_alloc_ptr: None,
policy,
policy_state: policy.derive_init_state(),
};
if !lazy {
bcache.populate_caches(hw);

View file

@ -37,6 +37,7 @@ fn flashmem() -> &'static mut FlashSingleton {
}
}
#[derive(Copy, Clone)]
pub struct KeyExport {
pub basis_name: [u8; 64],
pub key: [u8; 32],

View file

@ -24,7 +24,7 @@ pub const PAGE_SIZE: usize = spinor::SPINOR_ERASE_SIZE as usize;
/// size of a virtual page -- after the AES encryption and journaling overhead is subtracted
pub const VPAGE_SIZE: usize = PAGE_SIZE - size_of::<Nonce>() - size_of::<Tag>() - size_of::<JournalType>();
#[repr(C, packed)] // this can map directly into Flash
#[repr(C)] // this can map directly into Flash
pub(crate) struct StaticCryptoData {
/// aes-256 key of the system basis, encrypted with the User0 root key
pub(crate) system_key: [u8; AES_KEYSIZE],
@ -41,16 +41,6 @@ impl Deref for StaticCryptoData {
}
}
/// this is the structure of the Basis Key in RAM. The "key" and "iv" are actually never committed to
/// flash; only the "salt" is written to disk. The final "salt" is computed as the XOR of the salt on disk
/// and the user-provided "basis name". We never record the "basis name" on disk, so that the existence of
/// any Basis can be denied.
pub(crate) struct BasisKey {
salt: [u8; 16],
key: [u8; 32], // derived from lower 256 bits of sha512(bcrypt(salt, pw))
iv: [u8; 16], // an IV derived from the upper 128 bits of the sha512 hash from above, XOR with the salt
}
// emulated
#[cfg(not(any(target_os = "none", target_os = "xous")))]
type EmuMemoryRange = EmuStorage;
@ -93,13 +83,6 @@ pub(crate) struct PddbOs {
dna: u64,
/// reference to a TrngPool object that's shared among all the hardware functions
entropy: Rc<RefCell<TrngPool>>,
/*
/// keys to non-system basis that may or may not be mounted
/// We don't blend the System basis into this one because we need to refer to the System basis specifically
/// for decrypting management structures like the FSCB.
basis_keys: Vec::<[u8; AES_KEYSIZE]>,
*/
}
impl PddbOs {
@ -168,7 +151,7 @@ impl PddbOs {
}
#[cfg(not(any(target_os = "none", target_os = "xous")))]
pub fn dbg_dump(&self, name: Option<String>) {
pub fn dbg_dump(&self, name: Option<String>, extra_keys: Option<&Vec::<KeyExport>>) {
self.pddb_mr.dump_fs(&name);
let mut export = Vec::<KeyExport>::new();
if let Some(key) = self.system_basis_key {
@ -184,6 +167,11 @@ impl PddbOs {
}
);
}
if let Some(extra) = extra_keys {
for key in extra {
export.push(*key);
}
}
self.pddb_mr.dump_keys(&export, &name);
}
#[cfg(any(target_os = "none", target_os = "xous"))]
@ -285,6 +273,7 @@ impl PddbOs {
}
fn patch_keys(&self, data: &[u8], offset: u32) {
assert!(data.len() + offset as usize <= PAGE_SIZE, "attempt to burn key data that is outside the key region");
log::info!("patching keys area with {} bytes", data.len());
self.spinor.patch(
self.pddb_mr.as_slice(),
xous::PDDB_LOC,
@ -416,7 +405,7 @@ impl PddbOs {
/// maps a StaticCryptoData structure into the key area of the PDDB.
fn static_crypto_data_get(&self) -> &StaticCryptoData {
let scd_ptr = self.key_phys_base.as_usize() as *const StaticCryptoData;
let scd_ptr = self.pddb_mr.as_slice()[self.key_phys_base.as_usize()..self.key_phys_base.as_usize() + PAGE_SIZE].as_ptr() as *const StaticCryptoData;
let scd: &StaticCryptoData = unsafe{scd_ptr.as_ref().unwrap()};
scd
}
@ -1086,23 +1075,8 @@ impl PddbOs {
log::error!("PDDB system basis name is incorrect: {}; aborting mount operation.", basis_name);
return None;
}
/*
let bcache = BasisCacheEntry {
name: basis_name.clone(),
clean: true,
last_sync: Some(self.tt.elapsed_ms()),
num_dicts: basis_root.num_dictionaries,
dicts: HashMap::<String, DictCacheEntry>::new(),
cipher,
aad,
age: basis_root.age,
free_dict_offset: None,
v2p_map: sysbasis_map,
journal: u32::from_le_bytes(vpage[..size_of::<JournalType>()].try_into().unwrap()),
large_alloc_ptr: None,
}; */
log::info!("System BasisRoot record found, generating cache entry");
BasisCacheEntry::mount(self, &basis_name, &syskey, false)
BasisCacheEntry::mount(self, &basis_name, &syskey, false, BasisRetentionPolicy::Persist)
} else {
// i guess technically we could try a brute-force search for the page, but meh.
log::error!("System basis did not contain a root page -- unrecoverable error.");
@ -1358,17 +1332,20 @@ impl PddbOs {
}
plaintext_pw[72] = 0; // always null terminate
log::info!("making hasher");
let mut hasher = Sha512::new_with_strategy(FallbackStrategy::SoftwareOnly);
hasher.update(&bname_copy);
hasher.update(&plaintext_pw);
let digest = hasher.finalize();
// 2. now derive the actual salt by using indices from the digest to progressively
// index into the salt table.
log::info!("creating salt");
let scd = self.static_crypto_data_get();
let mut salt = [0u8; 16];
for (index_bytes, dst) in digest.chunks(2).zip(salt.iter_mut()) {
let index = u16::from_le_bytes(index_bytes.try_into().unwrap());
*dst = scd.salt_base[index as usize % scd.salt_base.len()];
log::info!("got scd");
for (index_bytes, dst) in digest.chunks(2).into_iter().zip(salt.iter_mut()) {
let index = u16::from_le_bytes(index_bytes.try_into().unwrap()) as usize % scd.salt_base.len();
*dst = scd.salt_base[index];
log::info!("index: {}, dst: {}", index as usize % scd.salt_base.len(), *dst);
}
log::info!("derived salt: {:x?}", salt);

View file

@ -419,64 +419,92 @@ fn xmain() -> ! {
*/
#[cfg(not(any(target_os = "none", target_os = "xous")))]
{
const EXTRA_BASIS: &'static str = "Basis2";
const EXTRA_BASIS_PW: &'static str = "some password blah blah";
pddb_os.test_reset();
log::info!("Creating `basecase1e`");
let mut basis_cache = BasisCache::new();
create_basis_testcase(&mut pddb_os, &mut basis_cache, None,
None, None, Some(32));
log::info!("Saving `basecase1e` to local host");
pddb_os.dbg_dump(Some("basecase1e".to_string()));
pddb_os.dbg_dump(Some("basecase1e".to_string()), None);
let extra_basis_key = pddb_os.basis_derive_key(EXTRA_BASIS, EXTRA_BASIS_PW);
let extra_export = KeyExport {
basis_name: BasisRootName::try_from_str(EXTRA_BASIS).unwrap().0,
key: extra_basis_key,
};
let mut export: Vec::<KeyExport> = Vec::new();
export.push(extra_export);
log::info!("Building a second basis");
basis_cache.basis_create(&mut pddb_os,
EXTRA_BASIS, EXTRA_BASIS_PW).expect("couldn't build test basis");
log::info!("Doing delete/add consistency with data extension");
delete_add_dict_consistency(&mut pddb_os, &mut basis_cache, None,
None, None, None);
None, None, None, None);
log::info!("Saving `dachecke` to local host");
pddb_os.dbg_dump(Some("dachecke".to_string()));
pddb_os.dbg_dump(Some("dachecke".to_string()), None);
log::info!("Doing patch test");
patch_test(&mut pddb_os, &mut basis_cache, None, None, true);
pddb_os.dbg_dump(Some("patche".to_string()));
pddb_os.dbg_dump(Some("patche".to_string()), None);
log::info!("Doing delete pattern test");
delete_pattern(&mut pddb_os, &mut basis_cache, None, None, None, None);
pddb_os.dbg_dump(Some("patterne".to_string()));
pddb_os.dbg_dump(Some("patterne".to_string()), None);
// extended tests.
// allocation space curtailed to force resource exhaustion faster.
// note to self: FSCB_PAGES revert to 16 (hw.rs), FASTSPACE_PAGES revert to 2 (fastspace.rs)
log::info!("Doing patch test 2");
patch_test(&mut pddb_os, &mut basis_cache, None, None, true);
pddb_os.dbg_dump(Some("patche2".to_string()));
pddb_os.dbg_dump(Some("patche2".to_string()), None);
log::info!("Doing delete pattern test 2");
delete_pattern(&mut pddb_os, &mut basis_cache, None, None, None, None);
pddb_os.dbg_dump(Some("patterne2".to_string()));
pddb_os.dbg_dump(Some("patterne2".to_string()), None);
log::info!("Doing delete/add consistency with data extension 2");
delete_add_dict_consistency(&mut pddb_os, &mut basis_cache, Some(3),
Some(50), None, None);
Some(50), None, None, None);
log::info!("Saving `dachecke2` to local host");
pddb_os.dbg_dump(Some("dachecke2".to_string()));
pddb_os.dbg_dump(Some("dachecke2".to_string()), None);
log::info!("Doing delete/add consistency with data extension 3");
delete_add_dict_consistency(&mut pddb_os, &mut basis_cache, Some(3),
Some(50), None, None);
Some(50), None, None, None);
log::info!("Saving `dachecke3` to local host");
pddb_os.dbg_dump(Some("dachecke3".to_string()));
pddb_os.dbg_dump(Some("dachecke3".to_string()), None);
log::info!("Doing delete/add consistency with data extension 4");
delete_add_dict_consistency(&mut pddb_os, &mut basis_cache, Some(6),
Some(50), None, None);
Some(50), None, None, None);
log::info!("Saving `dachecke4` to local host");
pddb_os.dbg_dump(Some("dachecke4".to_string()));
pddb_os.dbg_dump(Some("dachecke4".to_string()), None);
log::info!("Doing remount disk test");
let mut basis_cache = BasisCache::new();
if let Some(sys_basis) = pddb_os.pddb_mount() {
basis_cache.basis_add(sys_basis);
list_all(&mut pddb_os, &mut basis_cache);
pddb_os.dbg_dump(Some("remounte".to_string()));
pddb_os.dbg_dump(Some("remounte".to_string()), Some(&export));
}
log::info!("Mounting the second basis");
if let Some(basis2) = basis_cache.basis_unlock(&mut pddb_os,
EXTRA_BASIS, EXTRA_BASIS_PW, BasisRetentionPolicy::Persist) {
basis_cache.basis_add(basis2);
}
log::info!("Adding keys to Basis2");
delete_add_dict_consistency(&mut pddb_os, &mut basis_cache, Some(3),
Some(15), None, None, Some(EXTRA_BASIS));
log::info!("Saving `basis2` to local host");
pddb_os.dbg_dump(Some("basis2".to_string()), Some(&export));
}
log::info!("CI done");
/*

View file

@ -41,7 +41,7 @@ pub(crate) fn create_basis_testcase(hw: &mut PddbOs, basis_cache: &mut BasisCach
hw.pddb_format(false).unwrap();
let sys_basis = hw.pddb_mount().expect("couldn't mount system basis");
basis_cache.basis_add(sys_basis);
basis_cache.basis_add(sys_basis);
let num_dicts = maybe_num_dicts.unwrap_or(4);
let num_keys = maybe_num_keys.unwrap_or(34);
@ -78,7 +78,7 @@ pub(crate) fn create_basis_testcase(hw: &mut PddbOs, basis_cache: &mut BasisCach
/// Delete & add dictionary consistency check
pub(crate) fn delete_add_dict_consistency(hw: &mut PddbOs, basis_cache: &mut BasisCache,
maybe_num_dicts: Option<usize>, maybe_num_keys: Option<usize>, maybe_key_sizes: Option<(usize, usize)>,
maybe_extra_reserved: Option<usize>,
maybe_extra_reserved: Option<usize>, basis_name: Option<&str>
) {
let evict_count = maybe_num_dicts.unwrap_or(1);
let num_keys = maybe_num_keys.unwrap_or(36);
@ -89,7 +89,7 @@ pub(crate) fn delete_add_dict_consistency(hw: &mut PddbOs, basis_cache: &mut Bas
let dict_start_index = dict_list.len();
for (evicted, evict_dict) in dict_list.iter().enumerate() {
if evicted < evict_count {
match basis_cache.dict_remove(hw, evict_dict, None, false) {
match basis_cache.dict_remove(hw, evict_dict, basis_name, false) {
Ok(_) => {},
Err(e) => log::error!("Error evicting dictionary {}: {:?}", evict_dict, e),
}
@ -107,11 +107,11 @@ pub(crate) fn delete_add_dict_consistency(hw: &mut PddbOs, basis_cache: &mut Bas
// we do this slightly funky way instead of just passing Some(0) because we want to test the "None" path explicitly
basis_cache.key_update(hw, &dictname, &keyname, &keydata, None,
None,
None, false).unwrap();
basis_name, false).unwrap();
} else {
basis_cache.key_update(hw, &dictname, &keyname, &keydata, None,
Some(keydata.len() + extra_reserved),
None, false).unwrap();
basis_name, false).unwrap();
}
}
}

View file

@ -162,7 +162,7 @@ def main():
for name, key in keys.items():
if name in tables:
print("Basis {}".format(name))
print("Basis {}, key: {}".format(name, key.hex()))
v2p_table = tables[name][0]
p2v_table = tables[name][1]
# v2p_table[0xfe0fe0] = 0x1200* 0x100
@ -690,7 +690,7 @@ def decode_fscb(img, keys, FSCB_LEN_PAGES=2):
if fscb_start:
print("Found FSCB at {:x}".format(fscb_start))
fscb_enc = img[fscb_start * 4096 : (fscb_start + FSCB_LEN_PAGES) * 4096]
print("data: {}".format(fscb_enc.hex()))
# print("data: {}".format(fscb_enc.hex()))
try:
nonce = fscb_enc[:12]
print("key: {}, nonce: {}".format(key.hex(), nonce.hex()))