working through an initial Flash format routine as a pipecleaner

clean compile checkpoint, not much else.
This commit is contained in:
bunnie 2021-11-07 03:16:32 +08:00
parent 8b59bcdf61
commit fabc84aaf6
6 changed files with 168 additions and 71 deletions

View file

@ -1,7 +1,5 @@
use rkyv::{Archive, Deserialize, Serialize};
use crate::backend::PAGE_SIZE;
pub(crate) const SERVER_NAME_PDDB: &str = "_Plausibly Deniable Database_";
pub(crate) const PDDB_MAX_BASIS_NAME_LEN: usize = 64;
pub(crate) const PDDB_MAX_DICT_NAME_LEN: usize = 64;

View file

@ -1,5 +1,5 @@
use crate::api::*;
use crate::api::{PDDB_MAX_DICT_NAME_LEN, PDDB_MAX_KEY_NAME_LEN};
use super::*;
use core::num::NonZeroU64;
use core::ops::{Deref, DerefMut};
@ -30,12 +30,19 @@ pub(crate) struct BasisRoot {
pub(crate) name: [u8; PDDB_MAX_BASIS_NAME_LEN],
/// increments every time the BasisRoot is modified. This field must saturate, not roll over.
pub(crate) age: u32,
pub(crate) num_dictionaries: u32,
/// a cache of up FREE_CACHE_SIZE indicating the location of free space in the dictionary
/// allocation. This saves from having to do frequent free space search/compaction operations
/// on memory during writes and updates.
/*
/// a cache of up FREE_CACHE_SIZE indicating the location of free space for use by basis
/// functions, such as adding growing the size of this structure, adding more dictionaries,
/// adding keys to dictionaries, or extending existing keys. This saves from having to do
/// frequent free space search/compaction operations on memory during writes and updates.
pub(crate) free_cache: [Option<FreeSpace>; FREE_CACHE_SIZE],
// dict_slice: [DictPointer], // a synthetic record that is a slice of dictionaries
*/
/// "open end" of the pre-allocated space for the Basis. All Basis data must exist in an extent that is
/// less than this value. This can be grown and shrunk with allocation and compaction processes.
pub(crate) prealloc_open_end: PageAlignedU64,
pub(crate) num_dictionaries: u32,
// dict_slice: [DictPointer], // DictPointers + num_dictionaries above can be turned into a dict_slice
////// the following records are appended by the Serialization routine
// pad: [u8], // padding out to the next 4096-byte block less 16 bytes
// p_tag: [u8; 16], // auth tag output of the AES-GCM-SIV
}
@ -43,7 +50,7 @@ impl Deref for BasisRoot {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self as *const BasisRoot as *const u8, mem::size_of::<BasisRoot>())
core::slice::from_raw_parts(self as *const BasisRoot as *const u8, core::mem::size_of::<BasisRoot>())
as &[u8]
}
}
@ -52,7 +59,7 @@ impl Deref for BasisRoot {
impl DerefMut for BasisRoot {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self as *mut BasisRoot as *mut u8, mem::size_of::<BasisRoot>())
core::slice::from_raw_parts_mut(self as *mut BasisRoot as *mut u8, core::mem::size_of::<BasisRoot>())
as &mut [u8]
}
}
@ -66,7 +73,7 @@ pub(crate) struct DictPointer {
addr: u64, // the virtual address of the dictionary
}
/// FreeSpace address space is in the Basis virtual memory space
/// FreeSpace address space is in the virtual memory space of the containing Basis
#[derive(Copy, Clone)]
pub(crate) struct FreeSpace {
start: u64,

View file

@ -1,6 +1,7 @@
use core::num::NonZeroU32;
use super::PAGE_SIZE;
use crate::*;
#[repr(u8)]
pub(crate) enum SpaceState {

View file

@ -5,6 +5,7 @@ use rand_core::{CryptoRng, RngCore};
use cipher::{BlockCipher, BlockDecrypt};
use root_keys::api::{AesRootkeyType, Block};
use core::ops::Deref;
use core::convert::TryFrom;
use std::collections::HashMap;
use std::io::{Result, Error, ErrorKind};
@ -18,20 +19,19 @@ pub(crate) const INITIAL_BASIS_ALLOC: usize = 16;
pub const PAGE_SIZE: usize = spinor::SPINOR_ERASE_SIZE as usize;
#[repr(C, packed)] // this can map directly into Flash
#[derive(Default)]
pub(crate) struct StaticCryptoData {
/// aes-256 key of the system basis, encrypted with the User0 root key
system_key: [u8; 32],
pub(crate) system_key: [u8; 32],
/// a pool of fixed data used to pick salts, based on a hash of the basis name
salt_base: [u8; 2048],
pub(crate) salt_base: [u8; 2048],
/// also random data, but no specific purpose
reserved: [u8; 2016],
pub(crate) reserved: [u8; 2016],
}
impl Deref for StaticCryptoData {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self as *const StaticCryptoData as *const u8, mem::size_of::<StaticCryptoData>())
core::slice::from_raw_parts(self as *const StaticCryptoData as *const u8, core::mem::size_of::<StaticCryptoData>())
as &[u8]
}
}
@ -69,18 +69,18 @@ impl PddbOs {
// the mbbb is located one page off from the Page Table
let key_phys_base = PageAlignedU32::from(core::mem::size_of::<PageTableInFlash>());
let mbbb_phys_base = PageAlignedU32::from(key_phys_base + PAGE_SIZE);
let fscb_phys_base = PageAlignedU32::from(u32::From(mbbb_phys_base) + MBBB_PAGES as u32 * PAGE_SIZE);
let mbbb_phys_base = key_phys_base + PageAlignedU32::from(PAGE_SIZE);
let fscb_phys_base = PageAlignedU32::from(mbbb_phys_base.as_u32() + MBBB_PAGES as u32 * PAGE_SIZE as u32);
PddbOs {
spinor: spinor::Spinor::new(&xns).unwrap(),
rootkeys: root_keys::new(&xns, Some(AesRootkeyType::User0)).expect("FATAL: couldn't access RootKeys!"),
rootkeys: root_keys::RootKeys::new(&xns, Some(AesRootkeyType::User0)).expect("FATAL: couldn't access RootKeys!"),
pddb_mr: pddb,
trng: trng::Trng::new(&xns).unwrap(),
pt_phys_base: PageAlignedU32::from(0),
pt_phys_base: PageAlignedU32::from(0 as u32),
key_phys_base,
mbbb_phys_base,
fscb_phys_base,
data_phys_base: PageAlignedU32::from(u32::From(fscb_phys_base) + FSCB_PAGES * PAGE_SIZE),
data_phys_base: PageAlignedU32::from(fscb_phys_base.as_u32() + FSCB_PAGES as u32 * PAGE_SIZE as u32),
system_basis_key: None,
v2p_map: HashMap::<BasisRoot, HashMap<VirtAddr, ReversePte>>::new(),
}
@ -116,7 +116,7 @@ impl PddbOs {
// step 2. create the system basis root structure
let mut name: [u8; PDDB_MAX_BASIS_NAME_LEN] = [0; PDDB_MAX_BASIS_NAME_LEN];
for (&src, dst) in (PDDB_DEFAULT_SYSTEM_BASIS.as_bytes().iter().zip(name.iter_mut())) {
for (&src, dst) in PDDB_DEFAULT_SYSTEM_BASIS.as_bytes().iter().zip(name.iter_mut()) {
*dst = src;
}
let mut basis_root = BasisRoot {
@ -127,47 +127,53 @@ impl PddbOs {
name,
age: 0,
num_dictionaries: 0,
free_cache: [None; FREE_CACHE_SIZE],
prealloc_open_end: PageAlignedU64::from(INITIAL_BASIS_ALLOC * PAGE_SIZE),
};
// allocate a few initial free pages to the basis, so it can operate a bit without having
// to immediately call a time-consuming free space acquisition.
basis_root.free_cache[0] = Some(FreeSpace {
start: PAGE_SIZE,
len: (INITIAL_BASIS_ALLOC - 1) * PAGE_SIZE,
});
// extract a slice-u8 that maps onto the basis_root record, allowing us to patch this into a FLASH page
let br_slice: &[u8] = basis_root.deref();
// step 3. create our key material
if !self.rootkeys.ensure_aes_password() {
return Err(Error::new(ErrorKind::PermissionDenied, "unlock password was incorrect"));
}
// consider: making ensure_aes_password() a pub-scoped function? let's see how this works in practice.
//if !self.rootkeys.ensure_aes_password() {
// return Err(Error::new(ErrorKind::PermissionDenied, "unlock password was incorrect"));
//}
assert!(core::mem::size_of::<StaticCryptoData>() == PAGE_SIZE, "StaticCryptoData structure is not correctly sized");
let mut crypto_keys = StaticCryptoData::default();
self.trng.fill_bytes(&crypto_keys.system_key); // this is our "encrypted" key
self.trng.fill_bytes(&crypto_keys.salt_base);
self.trng.fill_bytes(&crypto_keys.reserved);
let mut system_basis_key: [u8; 32] = [0; 32];
self.trng.fill_bytes(&mut system_basis_key);
let mut basis_key_enc: [u8; 32] = system_basis_key.clone();
self.system_basis_key = Some(system_basis_key); // causes system_basis_key to be owned by self
log::info!("sanity check: plaintext system basis key: {:x?}", basis_key_enc);
self.rootkeys.decrypt_block(Block::from_mut_slice(&mut basis_key_enc));
log::info!("sanity check: encrypted system basis key: {:x?}", basis_key_enc);
let mut crypto_keys = StaticCryptoData {
system_key: [0; 32],
salt_base: [0; 2048],
reserved: [0; 2016],
};
// copy the encrypted key into the data structure for commit to Flash
for (&src, dst) in basis_key_enc.iter().zip(crypto_keys.system_key.iter_mut()) {
*dst = src;
}
self.trng.fill_bytes(&mut crypto_keys.salt_base);
self.trng.fill_bytes(&mut crypto_keys.reserved);
self.spinor.patch(
self.pddb_mr.as_slice(),
xous::PDDB_LOC,
crypto_keys.deref(),
u32::From(self.key_phys_base)
self.key_phys_base.as_u32()
).expect("couldn't burn keys");
{ // create a block so we know that the system_basis_key goes out of scope when this is done
let mut system_basis_key: [u8; 32] = [0; 32];
// copy the encrypted key into the buffer
for (&src, dst) in crypto_keys.system_key.iter().zip(system_basis_key.iter_mut()) {
*dst = src;
}
log::info!("sanity check: encrypted root key: {:x?}", system_basis_key);
self.rootkeys.decrypt_block(Block::from_mut_slice(&mut system_basis_key));
log::info!("sanity check: decrypted root key: {:x?}", system_basis_key);
self.system_basis_key = Some(system_basis_key);
}
// now we have a copy of the AES key necessary to encrypt the default System basis that we created in step 2.
// step 4. generate & write initial page table entries
// step 4. Create a hashmap for our reverse PTE, and add it to the Pddb's cache
// we don't have a fscb yet, and everything is free space, so we will manually place these initial entries.
let mut basis_v2p_map = HashMap::<VirtAddr, ReversePte>::new();
for phys_page in (
self.data_phys_base.as_u32()..self.data_phys_base.as_u32() + basis_root.prealloc_open_end.as_u32()
).step_by(PAGE_SIZE) {
}
// step 5. generate & write initial page table entries
// page table organization:
//
// offset from |

View file

@ -9,8 +9,8 @@ use bitflags::bitflags;
bitflags! {
/// flags used by the page table
pub struct PtFlags: u8 {
/// Pages that don't decrypt properly are marked as LOCKED in the cache.
const LOCKED = 0b0000_0000;
/// Pages that don't decrypt properly are marked as INVALID in the cache.
const INVALID = 0b0000_0000;
/// set for records that are synced to the copy in Flash. Every valid record
/// from Flash should have this set; it should only be cleared for blocks in Cache.
const CLEAN = 0b0000_0001;
@ -18,7 +18,7 @@ bitflags! {
}
}
impl Default for PtFlags {
fn default() -> PtFlags {PtFlags::UNINITIALIZED}
fn default() -> PtFlags {PtFlags::INVALID}
}
/// A Page Table Entry. Contains the address map of the corresponding entry,
@ -38,7 +38,7 @@ pub(crate) struct Pte {
/// the virtual address is 48 bits long
pddb_addr: [u8; 6],
/// this maps to a u8
flags: PtFags,
flags: PtFlags,
reserved: u8,
/// 32-bit strength of a nonce, but can be varied
nonce: [u8; 4],
@ -48,11 +48,10 @@ pub(crate) struct Pte {
}
#[repr(C, packed)]
#[derive(Default)]
pub(crate) struct ReversePte {
phys_addr: PhysAddr,
/// this maps to a u8
flags: PtFags,
flags: PtFlags,
}
pub const PDDB_SIZE_PAGES: usize = xous::PDDB_LEN as usize / PAGE_SIZE;
@ -63,7 +62,6 @@ pub const PDDB_SIZE_PAGES: usize = xous::PDDB_LEN as usize / PAGE_SIZE;
/// always corresponds to the base of data in FLASH, which means the excess
/// pages are going to be toward the high end of the page table range.
#[repr(C, packed)]
#[derive(Default)]
pub(crate) struct PageTableInFlash {
table: [Pte; PDDB_SIZE_PAGES],
}

View file

@ -1,43 +1,130 @@
use core::num::{NonZeroU32, NonZeroU64};
use core::convert::TryFrom;
use core::ops::Add;
use super::PAGE_SIZE;
/// PAGE_SIZE is required to be a power of two. 0x1000 -> 0x1000 - 1 = 0xFFF, which forms the bitmasks below.
struct PageAlignedU64(u64);
impl From<u64> for PageAlignedU64 {
fn from(arg: u64) -> Self {
if arg & (PAGE_SIZE - 1) == 0 {
PageAlignedU64(arg & !(PAGE_SIZE - 1))
#[derive(Copy, Clone)]
pub(crate) struct PageAlignedU64(u64);
impl PageAlignedU64 {
pub(crate) fn as_u32(&self) -> u32 {
if self.0 <= u32::MAX as u64 {
self.0 as u32
} else {
PageAlignedU64((arg & !(PAGE_SIZE - 1)) + PAGE_SIZE)
panic!("This PageAlignedU64 would not fit into a PageAlignedU32");
}
}
pub(crate) fn as_u64(&self) -> u64 {self.0}
}
impl From<u64> for PageAlignedU64 {
fn from(arg: u64) -> Self {
if arg & (PAGE_SIZE as u64 - 1) == 0 {
PageAlignedU64(arg & !(PAGE_SIZE as u64 - 1))
} else {
PageAlignedU64((arg & !(PAGE_SIZE as u64 - 1)) + PAGE_SIZE as u64)
}
}
}
impl From<PageAlignedU32> for PageAlignedU64 {
fn from(arg: PageAlignedU32) -> Self { PageAlignedU64(arg.0 as u64) } // already aligned, and it fits.
}
impl From<u32> for PageAlignedU64 {
fn from(arg: u32) -> Self {
if arg & (PAGE_SIZE - 1) == 0 {
PageAlignedU64(arg as u64 & !(PAGE_SIZE - 1))
if arg & (PAGE_SIZE as u32 - 1) == 0 {
PageAlignedU64(arg as u64 & !(PAGE_SIZE as u64 - 1))
} else {
PageAlignedU64((arg as u64 & !(PAGE_SIZE - 1)) + PAGE_SIZE)
PageAlignedU64((arg as u64 & !(PAGE_SIZE as u64 - 1)) + PAGE_SIZE as u64)
}
}
}
struct PageAlignedU32(u32);
impl From<usize> for PageAlignedU64 {
fn from(arg: usize) -> Self {
if arg & (PAGE_SIZE - 1) == 0 {
PageAlignedU64(arg as u64 & !(PAGE_SIZE as u64 - 1))
} else {
PageAlignedU64((arg as u64 & !(PAGE_SIZE as u64 - 1)) + PAGE_SIZE as u64)
}
}
}
impl Add for PageAlignedU64 {
type Output = PageAlignedU64;
fn add(self, other: PageAlignedU64) -> PageAlignedU64 {
PageAlignedU64(self.0 + other.0)
}
}
#[derive(Copy, Clone)]
pub(crate) struct PageAlignedU32(u32);
impl PageAlignedU32 {
pub(crate) fn as_u32(&self) -> u32 {self.0}
pub(crate) fn as_u64(&self) -> u64 {self.0 as u64}
}
impl From<u32> for PageAlignedU32 {
fn from(arg: u32) -> Self {
if arg & (PAGE_SIZE - 1) == 0 {
PageAlignedU32(arg & !(PAGE_SIZE - 1))
if arg & (PAGE_SIZE as u32 - 1) == 0 {
PageAlignedU32(arg & !(PAGE_SIZE as u32 - 1))
} else {
PageAlignedU32((arg & !(PAGE_SIZE - 1)) + PAGE_SIZE)
PageAlignedU32((arg & !(PAGE_SIZE as u32 - 1)) + PAGE_SIZE as u32)
}
}
}
impl From<usize> for PageAlignedU32 {
fn from(arg: usize) -> Self {
if arg & (PAGE_SIZE as usize - 1) == 0 {
PageAlignedU32(arg as u32 & !(PAGE_SIZE as u32 - 1))
} else {
PageAlignedU32((arg as u32 & !(PAGE_SIZE as u32 - 1)) + PAGE_SIZE as u32)
}
}
}
/* // let's get real, this is kind of a waste of cycles to check this...
impl TryFrom<usize> for PageAlignedU32 {
type Error = &'static str;
fn try_from(arg: usize) -> Result<Self, Self::Error> {
if arg > u32::MAX as usize {
Err("PageAlignedU32 only accepts usize less than u32::MAX")
} else {
Ok(
if arg & (PAGE_SIZE - 1) == 0 {
PageAlignedU32(arg as u32 & !(PAGE_SIZE as u32 - 1))
} else {
PageAlignedU32((arg as u32 & !(PAGE_SIZE as u32 - 1)) + PAGE_SIZE as u32)
}
)
}
}
}
*/
impl From<PageAlignedU32> for u32 {
fn from(arg: PageAlignedU32) -> Self {
arg.0
}
}
impl Add for PageAlignedU32 {
type Output = PageAlignedU32;
fn add(self, other: PageAlignedU32) -> PageAlignedU32 {
PageAlignedU32(self.0 + other.0 as u32)
}
/*
fn add(self, other: usize) -> PageAlignedU32 {
let page_aligned_other = if other & (PAGE_SIZE - 1) == 0 {
other & !(PAGE_SIZE - 1)
} else {
(other & !(PAGE_SIZE - 1)) + PAGE_SIZE
};
// saturate adds
if self.0 as usize + page_aligned_other > u32::MAX as usize {
PageAlignedU32(u32::MAX)
} else {
PageAlignedU32(self.0 + page_aligned_other as u32)
}
}*/
}
#[derive(Hash, Copy, Clone, PartialEq, Eq)]
pub struct PhysAddr(NonZeroU32);
#[derive(Hash, Copy, Clone, PartialEq, Eq)]
pub struct VirtAddr(NonZeroU64);