just journaling intermediate work

This commit is contained in:
bunnie 2021-11-06 04:22:34 +08:00
parent 56e82f37d3
commit 778fa8d271
12 changed files with 281 additions and 44 deletions

3
Cargo.lock generated
View file

@ -1379,12 +1379,15 @@ name = "pddb"
version = "0.1.0"
dependencies = [
"aes-xous",
"bitflags",
"cipher",
"log",
"log-server",
"num-derive",
"num-traits",
"rand_core 0.5.1",
"rkyv",
"root-keys",
"spinor",
"susres",
"ticktimer-server",

View file

@ -6,6 +6,7 @@ edition = "2018"
description = "Plausibly Deniable Database"
[dependencies]
bitflags = {version = "1"}
xous = { path = "../../xous-rs" }
xous-ipc = { path = "../../xous-ipc" }
log-server = { path = "../log-server" }
@ -20,6 +21,8 @@ trng = {path = "../trng"}
spinor = {path = "../spinor"}
aes-xous = {path="../aes"}
rand_core = "0.5"
root-keys = {path="../root-keys"}
cipher = "0.3"
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = { path = "../../utralib"}

View file

@ -1,5 +1,7 @@
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

@ -2,9 +2,13 @@ mod basis;
pub use basis::*;
mod pagetable;
pub use pagetable::*;
mod fastspace;
pub use fastspace::*;
mod types;
pub use types::*;
#[cfg(any(target_os = "none", target_os = "xous"))]
mod hw;
pub use hw::*;
pub(crate) use hw::*;
// TODO: the alternative back-end PddbOs structures for hosted mode.

View file

@ -1,5 +1,4 @@
use pddb::PDDB_MAX_BASIS_NAME_LEN;
use crate::api::*;
use crate::api::{PDDB_MAX_DICT_NAME_LEN, PDDB_MAX_KEY_NAME_LEN};
use core::num::NonZeroU64;
@ -67,6 +66,7 @@ pub(crate) struct DictPointer {
addr: u64, // the virtual address of the dictionary
}
/// FreeSpace address space is in the Basis virtual memory space
#[derive(Copy, Clone)]
pub(crate) struct FreeSpace {
start: u64,
@ -89,6 +89,7 @@ pub(crate) struct Dictionary {
/// HashKeys are packed at the end of a Dictionary.
pub(crate) struct HashKey {
name: [u8; PDDB_MAX_KEY_NAME_LEN],
journal_rev: u32,
/// incremented every time the key is re-written to flash. saturating add.
age: u32,
/// length of the data stored in the HashKey

View file

@ -0,0 +1,34 @@
use core::num::NonZeroU32;
use super::PAGE_SIZE;
#[repr(u8)]
pub(crate) enum SpaceState {
/// pages that are completely un-spoken for
Free,
/// pages that are in the process of being used, but the journal has yet to be committed
MaybeUsed,
/// pages that are now fully used
Used,
/// pages that are no longer used and need to be erased
Dirty,
}
/// FastSpace tracks a limited set of physical pages
/// This is designed to fill exactly one erase sector of 4096 bytes
#[repr(C, packed)]
pub (crate) struct FastSpace {
p_nonce: [u8; 12],
journal_rev: [u8; 4],
free_pool: [Option<PhysAddr>; (PAGE_SIZE - 16 - 4 -12) / 4],
p_tag: [u8; 16],
}
/// a 128-bit record that stores an encrypted update to the FastSpace pool
#[repr(C, packed)]
pub (crate) struct SpaceUpdate {
nonce: u64,
page_number: u32,
reserved: [u8; 3],
state: SpaceState,
}

View file

@ -1,16 +1,59 @@
use std::convert::TryInto;
use crate::{api, backend::{BasisRoot, FREE_CACHE_SIZE}};
use pddb::PDDB_MAX_BASIS_NAME_LEN;
use crate::*;
use rand_core::{CryptoRng, RngCore};
use cipher::{BlockCipher, BlockDecrypt};
use root_keys::api::{AesRootkeyType, Block};
use core::ops::Deref;
/// OS-specific PDDB structures
use std::collections::HashMap;
use std::io::{Result, Error, ErrorKind};
/// Implementation-specific PDDB structures: for Precursor/Xous OS pair
pub(crate) const MBBB_PAGES: usize = 10;
pub(crate) const FSCB_PAGES: usize = 10;
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],
/// a pool of fixed data used to pick salts, based on a hash of the basis name
salt_base: [u8; 2048],
/// also random data, but no specific purpose
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>())
as &[u8]
}
}
}
pub(crate) struct PddbOs {
spinor: spinor::Spinor,
rootkeys: root_keys::RootKeys,
pddb_mr: xous::MemoryRange,
trng: trng::Trng,
/// page table base -- location in FLASH, offset from physical bottom of pddb_mr
pt_phys_base: PageAlignedU32,
/// local key store -- one page, to store exactly one key, used for the system basis.
/// the rest of the keys are generated on the fly entirely from the user password + a salt also stored in this page
key_phys_base: PageAlignedU32,
/// make before break buffer base -- location in FLASH, offset from physical bottom of pddb_mr
mbbb_phys_base: PageAlignedU32,
/// free space circular buffer base -- location in FLASH, offset from physical bottom of pddb_mr
fscb_phys_base: PageAlignedU32,
data_phys_base: PageAlignedU32,
system_basis_key: Option<[u8; 32]>,
v2p_map: HashMap<BasisRoot, HashMap<VirtAddr, ReversePte>>,
}
impl PddbOs {
@ -24,24 +67,45 @@ impl PddbOs {
)
.expect("Couldn't map the PDDB memory range");
// 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);
PddbOs {
spinor: spinor::Spinor::new(&xns).unwrap(),
rootkeys: root_keys::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),
key_phys_base,
mbbb_phys_base,
fscb_phys_base,
data_phys_base: PageAlignedU32::from(u32::From(fscb_phys_base) + FSCB_PAGES * PAGE_SIZE),
system_basis_key: None,
v2p_map: HashMap::<BasisRoot, HashMap<VirtAddr, ReversePte>>::new(),
}
}
/// generates a 96-bit nonce using the CPRNG
pub fn gen_nonce(&mut self) -> [u8; 12] {
let mut nonce: [u8; 12] = [0; 12];
self.trng.fill_bytes(&mut nonce);
nonce
}
/// this function is dangerous in that calling it will completely erase all of the previous data
/// in the PDDB an replace it with a brand-spanking new, blank PDDB.
/// The number of servers that can connect to the Spinor crate is strictly tracked, so we borrow a reference
/// to the Spinor object allocated to the PDDB implementation for this operation.
pub fn format_pddb(&mut self) {
pub(crate) fn format_pddb(&mut self) -> Result<()> {
if !self.rootkeys.is_initialized().unwrap() {
return Err(Error::new(ErrorKind::Unsupported, "Root keys are not initialized; cannot format a PDDB without root keys!"));
}
// step 1. Erase the entire PDDB region.
log::info!("Erasing the PDDB region");
let blank_sector: [u8; 4096] = [0xff; 4096];
let blank_sector: [u8; PAGE_SIZE] = [0xff; PAGE_SIZE];
for offset in (0..xous::PDDB_LEN).step_by(4096) {
for offset in (0..xous::PDDB_LEN).step_by(PAGE_SIZE) {
self.spinor.patch(
self.pddb_mr.as_slice(),
xous::PDDB_LOC,
@ -51,12 +115,12 @@ impl PddbOs {
}
// step 2. create the system basis root structure
let mut p_nonce: [u8; 12] = [0; 12];
self.trng.fill_bytes(&mut p_nonce);
let mut name: [u8; PDDB_MAX_BASIS_NAME_LEN] = [0; PDDB_MAX_BASIS_NAME_LEN];
write!(name, PDDB_DEFAULT_SYSTEM_BASIS).unwrap();
for (&src, dst) in (PDDB_DEFAULT_SYSTEM_BASIS.as_bytes().iter().zip(name.iter_mut())) {
*dst = src;
}
let mut basis_root = BasisRoot {
p_nonce,
p_nonce: self.gen_nonce(),
magic: api::PDDB_MAGIC,
version: api::PDDB_VERSION,
journal_rev: 0,
@ -65,14 +129,65 @@ impl PddbOs {
num_dictionaries: 0,
free_cache: [None; FREE_CACHE_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();
// TODO: unfortunately, we need to figure out some free space structure at this point, I think -- because
// we are getting to the point of having to allocate the free_cache data before writing it.
// step 3. create our key material
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);
self.spinor.patch(
self.pddb_mr.as_slice(),
xous::PDDB_LOC,
crypto_keys.deref(),
u32::From(self.key_phys_base)
).expect("couldn't burn keys");
// next: figure out the length of the page table + mbbb (make before break buffer)
// + space for the (fscb) free space circular buffer, and then locate the basis root there
{ // 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
// page table organization:
//
// offset from |
// pt_phys_base| contents (example for total PDDB len of 0x6f8_0000 or 111 MiB)
// ------------|---------------------------------------------------
// 0x0000_0000 | virtual map for page at (0x0000 + data_phys_base)
// 0x0000_0010 | virtual map for page at (0x1000 + data_phys_base)
// 0x0000_0020 | virtual map for page at (0x2000 + data_phys_base)
// ...
// 0x0006_F7F0 | virtual map for page at (0x06F7_F000 + data_phys_base)
// 0x0006_F800 | unused
// ...
// 0x0007_0000 | key page
// ...
// 0x0007_1000 | mbbb start (example of 10 pages)
// ...
// 0x0007_B000 | fscb start (example of 10 pages)
// ...
// 0x0008_5000 | data_phys_base - start of basis + dictionary + key data region
Ok(())
}
}

View file

@ -1,3 +1,25 @@
use super::PAGE_SIZE;
use super::PhysAddr;
use core::num::NonZeroU8;
use core::num::NonZeroU32;
use core::num::NonZeroU64;
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;
/// 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;
}
}
impl Default for PtFlags {
fn default() -> PtFlags {PtFlags::UNINITIALIZED}
}
/// A Page Table Entry. Contains the address map of the corresponding entry,
/// plus a nonce, and a checksum. Due to the Page Table being deliberately
@ -11,11 +33,13 @@
/// any routines downstream of the Pte shall be coded to handle potentially a much larger
/// nonce and checksum structure.
#[repr(C, packed)]
#[derive(Default)]
pub(crate) struct Pte {
/// the virtual address is 48 bits long
pddb_addr: [u8; 6],
/// the flags are largely TBD at this moment
flags: [u8; 2],
/// this maps to a u8
flags: PtFags,
reserved: u8,
/// 32-bit strength of a nonce, but can be varied
nonce: [u8; 4],
/// 32-bit "weak" checksum, used only for quick scans of the PTE to determine a coarse "in" or "out" classifier
@ -23,11 +47,24 @@ pub(crate) struct Pte {
checksum: [u8; 4],
}
/// This structure is mapped into the top of FLASH memory, starting at
/// xous::PDDB_LOC
pub const PDDB_SIZE_PAGES: usize = xous::PDDB_LEN as usize / 4096;
#[repr(C, packed)]
pub(crate) struct PageTableInFLash {
#[derive(Default)]
pub(crate) struct ReversePte {
phys_addr: PhysAddr,
/// this maps to a u8
flags: PtFags,
}
pub const PDDB_SIZE_PAGES: usize = xous::PDDB_LEN as usize / PAGE_SIZE;
/// This structure is mapped into the top of FLASH memory, starting at
/// xous::PDDB_LOC. This actually slightly over-sizes the page table,
/// because the page table does not map the locations for the page table
/// itself, the MBBB, or the FSCB. However, the 0th entry of the page table
/// 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],
}
@ -40,7 +77,7 @@ pub(crate) struct EncryptedPage {
/// journal_rev is encrypted and indicates the current journal revision for the block
journal_rev: [u8; 4],
/// data is encrypted and holds the good stuff
data: [u8; (4096 - 12 - 16 - 4)],
data: [u8; (PAGE_SIZE - 12 - 16 - 4)],
/// tag is the authentication tag. If the page decrypts & authenticates, we know it's a valid data block for us.
p_tag: [u8; 16],
}

View file

@ -0,0 +1,50 @@
use core::num::{NonZeroU32, NonZeroU64};
/// 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))
} else {
PageAlignedU64((arg & !(PAGE_SIZE - 1)) + PAGE_SIZE)
}
}
}
impl From<u32> for PageAlignedU64 {
fn from(arg: u32) -> Self {
if arg & (PAGE_SIZE - 1) == 0 {
PageAlignedU64(arg as u64 & !(PAGE_SIZE - 1))
} else {
PageAlignedU64((arg as u64 & !(PAGE_SIZE - 1)) + PAGE_SIZE)
}
}
}
struct PageAlignedU32(u32);
impl From<u32> for PageAlignedU32 {
fn from(arg: u32) -> Self {
if arg & (PAGE_SIZE - 1) == 0 {
PageAlignedU32(arg & !(PAGE_SIZE - 1))
} else {
PageAlignedU32((arg & !(PAGE_SIZE - 1)) + PAGE_SIZE)
}
}
}
impl From<PageAlignedU32> for u32 {
fn from(arg: PageAlignedU32) -> Self {
arg.0
}
}
pub struct PhysAddr(NonZeroU32);
pub struct VirtAddr(NonZeroU64);
mod tests {
use super::*;
#[test]
fn test_page_size() {
assert!(PAGE_SIZE & !(PAGE_SIZE - 1) == 0, "PAGE_SIZE is not a power of two!");
}
}

View file

@ -1,6 +1,8 @@
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
extern crate bitflags;
mod api;
use api::*;
mod backend;
@ -191,10 +193,10 @@ const PDDB_BACKING_SIZE: usize = 0x4000_0000;
///
/// Basis Unlock Procedure
///
/// Each Basis has a name and a passcode associated with it. The two default Basis names are
/// - `.System`
/// - `.FastSpace`
/// Each Basis has a name and a passcode associated with it. The default Basis name is `.System`.
/// In addition to that, a `.FastSpace` structure is unlocked along side the default Basis.
/// These are both associated with the default system unlock passcode.
///
/// A newly created Basis will request a name for the Basis, and a password. It is a requirement
/// that the combination of `(name, password)` be unique; this property is enforced and a system will
/// reject attempts to create identically named, identially passworded Basis. However, one can have

View file

@ -52,21 +52,6 @@ impl RootKeys {
Opcode::UxSelfSignXous.to_u32().unwrap()
}
/// this function takes a boot gateware that has a "null" key (all zeros) and:
/// 0. confirms that JTAG is accessible by reading out the ID, and that the eFuse is writeable, and all 0's
/// 1. prompts of the update password, and confirms that the existing efuse key decrypts to 0. if not -- we were already fused, abort.
/// 2. creates a new eFuse key using the TRNG
/// 3. inserts a revised KEYROM into the base image while copying & encrypting with the new key to the staging area
/// 4. verifies the xilinx-hmac on the staged image
/// 5. copies the staged image into the boot area
/// 6. burns the eFuse key using JTAG-local commands
/// 7. suspends the device with auto-resume so that the new gateware is in effect
/// 8. reads back the eFuse key from the KEYROM to confirm everything went as planned, compares to previously computed result
/// 9. clears the eFuse key from RAM.
pub fn seal_boot_gateware(&mut self) -> Result<(), xous::Error> {
unimplemented!();
}
/// this initiates an attempt to update passwords. User must unlock their device first, and can cancel out if not expected.
pub fn try_update_password(&mut self, _which: PasswordType) -> Result<(), xous::Error> {
unimplemented!();
@ -169,6 +154,7 @@ impl RootKeys {
).expect("couldn't send test message");
log::info!("test_ux response: {:?}", response);
}
pub fn bbram_provision(&self) {
send_message(self.conn,
Message::new_scalar(Opcode::BbramProvision.to_usize().unwrap(),

View file

@ -233,9 +233,9 @@ fn xmain() -> ! {
0. Key purge timer (self, created without xns)
1. Shellchat for test initiation
2. Main menu -> trigger initialization
3. (future) PDDB
3. PDDB
*/
let keys_sid = xns.register_name(api::SERVER_NAME_KEYS, Some(2)).expect("can't register server");
let keys_sid = xns.register_name(api::SERVER_NAME_KEYS, Some(3)).expect("can't register server");
let mut keys = RootKeys::new();
log::info!("Boot FPGA key source: {:?}", keys.fpga_key_source());