more incremental progress

This commit is contained in:
bunnie 2021-11-07 05:34:07 +08:00
parent ca3c1c7de4
commit 81d9fb3fda
7 changed files with 101 additions and 22 deletions

View file

@ -19,10 +19,12 @@ susres = {path = "../susres"}
rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false} rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false}
trng = {path = "../trng"} trng = {path = "../trng"}
spinor = {path = "../spinor"} spinor = {path = "../spinor"}
aes-xous = {path="../aes"} aes = {path="../aes"}
rand_core = "0.5" rand_core = "0.5"
root-keys = {path="../root-keys"} root-keys = {path="../root-keys"}
cipher = "0.3" cipher = "0.3"
bitfield = "0.13.2"
aes-gcm-siv = "0.10.3"
[target.'cfg(not(any(windows,unix)))'.dependencies] [target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = { path = "../../utralib"} utralib = { path = "../../utralib"}

View file

@ -6,7 +6,7 @@ use core::ops::{Deref, DerefMut};
use core::{mem, slice}; use core::{mem, slice};
pub(crate) const FREE_CACHE_SIZE: usize = 16; pub(crate) const FREE_CACHE_SIZE: usize = 16;
pub type BasisRootName = [u8; PDDB_MAX_BASIS_NAME_LEN];
/// In basis space, the BasisRoot is located at 0 /// In basis space, the BasisRoot is located at 0
/// The first 4GiB is reserved for the Basis Root. /// The first 4GiB is reserved for the Basis Root.
/// Keys begin at the next 4GiB. /// Keys begin at the next 4GiB.
@ -27,7 +27,7 @@ pub(crate) struct BasisRoot {
pub(crate) magic: [u8; 4], pub(crate) magic: [u8; 4],
pub(crate) version: u16, pub(crate) version: u16,
pub(crate) journal_rev: u32, pub(crate) journal_rev: u32,
pub(crate) name: [u8; PDDB_MAX_BASIS_NAME_LEN], pub(crate) name: BasisRootName,
/// increments every time the BasisRoot is modified. This field must saturate, not roll over. /// increments every time the BasisRoot is modified. This field must saturate, not roll over.
pub(crate) age: u32, pub(crate) age: u32,
/* /*
@ -41,11 +41,39 @@ pub(crate) struct BasisRoot {
/// less than this value. This can be grown and shrunk with allocation and compaction processes. /// less than this value. This can be grown and shrunk with allocation and compaction processes.
pub(crate) prealloc_open_end: PageAlignedU64, pub(crate) prealloc_open_end: PageAlignedU64,
pub(crate) num_dictionaries: u32, pub(crate) num_dictionaries: u32,
// dict_slice: [DictPointer], // DictPointers + num_dictionaries above can be turned into a dict_slice // dict_slice: [DictPointer; num_dictionaries], // DictPointers + num_dictionaries above can be turned into a dict_slice
////// the following records are appended by the Serialization routine ////// the following records are appended by the Serialization routine
// pad: [u8], // padding out to the next 4096-byte block less 16 bytes // 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 // p_tag: [u8; 16], // auth tag output of the AES-GCM-SIV
} }
impl BasisRoot {
/// Compute the number of memory pages consumed by the BasisRoot structure itself.
/// This is the size of BasisRoot, plus the dictionaries allocated within the Basis.
/// It does mean that your memory usage scales directly with the number of dictionaries
/// you put in the Basis, because there is no way to chain or defer the Basis structure
/// if you get thousands of Dictionaries. Note that the intent is to have typcially no
/// more than a couple dozen dictionaries; if you want to store a lot of different records,
/// you can create thousands of Keys more efficiently, than you can dictionaries.
pub(crate) fn len_pages(&self) -> usize {
let min_len = core::mem::size_of::<BasisRoot>()
+ ((self.num_dictionaries as usize)
* core::mem::size_of::<DictPointer>())
+ core::mem::size_of::<aes_gcm_siv::Tag>();
if (min_len & (1 - PAGE_SIZE)) == 0 {
min_len / PAGE_SIZE
} else {
min_len / PAGE_SIZE + 1
}
}
/// Number of bytes needed to pad between the length of the BasisRoot structure and the plaintext
/// tag that will get appended to the end
pub(crate) fn padding_count(&self) -> usize {
self.len_pages() * PAGE_SIZE -
(core::mem::size_of::<BasisRoot>()
+ ((self.num_dictionaries as usize) * core::mem::size_of::<DictPointer>())
)
}
}
impl Deref for BasisRoot { impl Deref for BasisRoot {
type Target = [u8]; type Target = [u8];
fn deref(&self) -> &[u8] { fn deref(&self) -> &[u8] {

View file

@ -21,7 +21,7 @@ pub(crate) enum SpaceState {
pub (crate) struct FastSpace { pub (crate) struct FastSpace {
p_nonce: [u8; 12], p_nonce: [u8; 12],
journal_rev: [u8; 4], journal_rev: [u8; 4],
free_pool: [Option<PhysAddr>; (PAGE_SIZE - 16 - 4 -12) / 4], free_pool: [PhysPage; (PAGE_SIZE - 16 - 4 -12) / 4],
p_tag: [u8; 16], p_tag: [u8; 16],
} }

View file

@ -1,6 +1,8 @@
use std::convert::TryInto; use std::convert::TryInto;
use crate::*; use crate::*;
use aes_gcm_siv::{Aes256GcmSiv, Nonce, Key};
use aes_gcm_siv::aead::{NewAead, Aead};
use rand_core::{CryptoRng, RngCore}; use rand_core::{CryptoRng, RngCore};
use cipher::{BlockCipher, BlockDecrypt}; use cipher::{BlockCipher, BlockDecrypt};
use root_keys::api::{AesRootkeyType, Block}; use root_keys::api::{AesRootkeyType, Block};
@ -53,7 +55,7 @@ pub(crate) struct PddbOs {
fscb_phys_base: PageAlignedU32, fscb_phys_base: PageAlignedU32,
data_phys_base: PageAlignedU32, data_phys_base: PageAlignedU32,
system_basis_key: Option<[u8; 32]>, system_basis_key: Option<[u8; 32]>,
v2p_map: HashMap<BasisRoot, HashMap<VirtAddr, ReversePte>>, v2p_map: HashMap<BasisRootName, HashMap<VirtAddr, PhysPage>>,
} }
impl PddbOs { impl PddbOs {
@ -82,7 +84,7 @@ impl PddbOs {
fscb_phys_base, fscb_phys_base,
data_phys_base: PageAlignedU32::from(fscb_phys_base.as_u32() + FSCB_PAGES as u32 * PAGE_SIZE as u32), data_phys_base: PageAlignedU32::from(fscb_phys_base.as_u32() + FSCB_PAGES as u32 * PAGE_SIZE as u32),
system_basis_key: None, system_basis_key: None,
v2p_map: HashMap::<BasisRoot, HashMap<VirtAddr, ReversePte>>::new(), v2p_map: HashMap::<BasisRootName, HashMap<VirtAddr, PhysPage>>::new(),
} }
} }
@ -166,14 +168,49 @@ impl PddbOs {
// step 4. Create a hashmap for our reverse PTE, and add it to the Pddb's cache // 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. // 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(); let mut basis_v2p_map = HashMap::<VirtAddr, PhysPage>::new();
for phys_page in ( for (virt_page, phys_addr) in (
self.data_phys_base.as_u32()..self.data_phys_base.as_u32() + basis_root.prealloc_open_end.as_u32() self.data_phys_base.as_u32()..self.data_phys_base.as_u32() + basis_root.prealloc_open_end.as_u32()
).step_by(PAGE_SIZE) { ).step_by(PAGE_SIZE).enumerate() {
let mut rpte = PhysPage(0);
rpte.set_page_number(phys_addr / PAGE_SIZE as u32);
rpte.set_clean(true);
rpte.set_valid(true);
basis_v2p_map.insert((virt_page * PAGE_SIZE) as VirtAddr, rpte);
}
self.v2p_map.insert(basis_root.name, basis_v2p_map);
// step 5. write the basis to Flash, at the physical locations noted above
let mut basis_ser = vec![];
for &b in br_slice {
basis_ser.push(b)
}
for _ in (0..basis_root.padding_count()) {
basis_ser.push(0)
}
// basis_ser can now be passed to an encryption function
if let Some(system_basis_key) = self.system_basis_key {
let key = Key::from_slice(&system_basis_key);
let cipher = Aes256GcmSiv::new(key);
let nonce = Nonce::from_slice(&basis_root.p_nonce);
let ciphertext = cipher.encrypt(nonce, &basis_ser[12..]);
let ct_to_flash: &[u8] = ciphertext.as_ref().unwrap(); // this now contains the encrypted basis + 16-byte tag at the very end
assert!( ( ct_to_flash.len() + basis_root.p_nonce.len()) & (PAGE_SIZE - 1) == 0, "Padding failure during basis serialization!");
// we're now ready to write the encrypted basis to Flash.
self.spinor.patch(
self.pddb_mr.as_slice(),
xous::PDDB_LOC,
&[&basis_root.p_nonce, ct_to_flash].concat(),
self.data_phys_base.as_u32()
).expect("couldn't write basis structure");
// now fill in the rest of Flash with random data. Because the rest of the Basis is blank, random data is "just fine".
// LEFT OFF HERE
} else {
panic!("invalid state"); // we should never hit this because we created the key earlier in the same routine.
} }
// step 5. generate & write initial page table entries // step 6. generate & write initial page table entries
// page table organization: // page table organization:
// //
// offset from | // offset from |
@ -194,6 +231,7 @@ impl PddbOs {
// ... // ...
// 0x0008_5000 | data_phys_base - start of basis + dictionary + key data region // 0x0008_5000 | data_phys_base - start of basis + dictionary + key data region
Ok(()) Ok(())
} }
} }

View file

@ -1,10 +1,10 @@
use super::PAGE_SIZE; use super::PAGE_SIZE;
use super::PhysAddr;
use core::num::NonZeroU8; use core::num::NonZeroU8;
use core::num::NonZeroU32; use core::num::NonZeroU32;
use core::num::NonZeroU64; use core::num::NonZeroU64;
use bitflags::bitflags; use bitflags::bitflags;
use bitfield::bitfield;
bitflags! { bitflags! {
/// flags used by the page table /// flags used by the page table
@ -21,7 +21,10 @@ impl Default for PtFlags {
fn default() -> PtFlags {PtFlags::INVALID} fn default() -> PtFlags {PtFlags::INVALID}
} }
/// A Page Table Entry. Contains the address map of the corresponding entry, /// A Page Table Entry. Must be equual in length to one AES block size (128 bits).
/// This is stored in the FLASH itself, so size is not as much of a constraint.
///
/// Contains the address map of the corresponding entry,
/// plus a nonce, and a checksum. Due to the Page Table being deliberately /// plus a nonce, and a checksum. Due to the Page Table being deliberately
/// srtuctured to have invalid entries that don't decrypt correctly, you /// srtuctured to have invalid entries that don't decrypt correctly, you
/// can't use a chaining approach. Thus these entries are encrypted closer to /// can't use a chaining approach. Thus these entries are encrypted closer to
@ -47,11 +50,14 @@ pub(crate) struct Pte {
checksum: [u8; 4], checksum: [u8; 4],
} }
#[repr(C, packed)] /// Physical page information, coded as a bitfield, because space is a premium!
pub(crate) struct ReversePte { bitfield! {
phys_addr: PhysAddr, #[derive(Copy, Clone, Hash)]
/// this maps to a u8 pub struct PhysPage(u32);
flags: PtFlags, impl Debug;
pub page_number, set_page_number: 20, 0;
pub clean, set_clean: 21;
pub valid, set_valid: 22;
} }
pub const PDDB_SIZE_PAGES: usize = xous::PDDB_LEN as usize / PAGE_SIZE; pub const PDDB_SIZE_PAGES: usize = xous::PDDB_LEN as usize / PAGE_SIZE;

View file

@ -120,13 +120,17 @@ impl Add for PageAlignedU32 {
}*/ }*/
} }
/*
#[derive(Hash, Copy, Clone, PartialEq, Eq)] #[derive(Hash, Copy, Clone, PartialEq, Eq)]
pub struct PhysAddr(NonZeroU32); pub struct PhysAddr(NonZeroU32);
impl From<u32> for PhysAddr {
fn from(arg: u32) -> Self {
PhysAddr( NonZeroU32::try_from(arg).expect("Physical addresses must not be zero") )
}
}
*/
#[derive(Hash, Copy, Clone, PartialEq, Eq)] pub type VirtAddr = u64;
pub struct VirtAddr(NonZeroU64);
mod tests { mod tests {
use super::*; use super::*;

View file

@ -2,6 +2,7 @@
#![cfg_attr(target_os = "none", no_main)] #![cfg_attr(target_os = "none", no_main)]
extern crate bitflags; extern crate bitflags;
extern crate bitfield;
mod api; mod api;
use api::*; use api::*;