mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
basis root now iterates itself into an encrypted set of blocks
This commit is contained in:
parent
62ef3b9414
commit
d45369a19d
2 changed files with 66 additions and 53 deletions
|
|
@ -10,6 +10,7 @@ use core::mem::size_of;
|
|||
use std::convert::TryInto;
|
||||
use aes_gcm_siv::{Aes256GcmSiv, Nonce};
|
||||
use aes_gcm_siv::aead::{Aead, Payload};
|
||||
use std::iter::IntoIterator;
|
||||
|
||||
|
||||
pub type BasisRootName = [u8; PDDB_MAX_BASIS_NAME_LEN];
|
||||
|
|
@ -26,7 +27,7 @@ pub type BasisRootName = [u8; PDDB_MAX_BASIS_NAME_LEN];
|
|||
/// PAGE_SIZE; request for blocks beyond the length of the Basis pre-alloc
|
||||
/// region will return None.
|
||||
#[repr(C)]
|
||||
pub(crate) struct BasisEncrypter<'a> {
|
||||
pub(crate) struct BasisEncryptor<'a> {
|
||||
root: &'a BasisRoot,
|
||||
dicts: &'a [DictPointer],
|
||||
cipher: Aes256GcmSiv,
|
||||
|
|
@ -35,14 +36,14 @@ pub(crate) struct BasisEncrypter<'a> {
|
|||
journal_rev: JournalType,
|
||||
entropy: Rc<RefCell<TrngPool>>,
|
||||
}
|
||||
impl<'a> BasisEncrypter<'a> {
|
||||
pub fn new(root: &'a BasisRoot, dicts: &'a [DictPointer], dna: u64, cipher: Aes256GcmSiv, rev: JournalType, entropy: Rc<RefCell<TrngPool>>) -> Self {
|
||||
impl<'a> BasisEncryptor<'a> {
|
||||
pub(crate) fn new(root: &'a BasisRoot, dicts: &'a [DictPointer], dna: u64, cipher: Aes256GcmSiv, rev: JournalType, entropy: Rc<RefCell<TrngPool>>) -> Self {
|
||||
let mut aad = Vec::<u8>::new();
|
||||
aad.extend_from_slice(&root.name);
|
||||
aad.extend_from_slice(&PDDB_VERSION.to_le_bytes());
|
||||
aad.extend_from_slice(&dna.to_le_bytes());
|
||||
|
||||
BasisEncrypter {
|
||||
BasisEncryptor {
|
||||
root,
|
||||
dicts,
|
||||
cur_vpage: 0,
|
||||
|
|
@ -53,35 +54,66 @@ impl<'a> BasisEncrypter<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
impl<'a> Iterator for BasisEncrypter<'a> {
|
||||
|
||||
pub(crate) struct BasisEncryptorIter<'a> {
|
||||
basis_data: BasisEncryptor<'a>,
|
||||
// the virtual address of the currently requested iteration
|
||||
vaddr: usize,
|
||||
}
|
||||
impl<'a> IntoIterator for BasisEncryptor<'a> {
|
||||
type Item=[u8; PAGE_SIZE];
|
||||
type IntoIter=BasisEncryptorIter<'a>;
|
||||
fn into_iter(self) -> BasisEncryptorIter<'a> {
|
||||
let journal_bytes = self.journal_rev.to_le_bytes();
|
||||
BasisEncryptorIter {
|
||||
basis_data: self,
|
||||
vaddr: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a> Iterator for BasisEncryptorIter<'a> {
|
||||
type Item = [u8; PAGE_SIZE];
|
||||
|
||||
fn next<'s>(&'s mut self) -> Option<Self::Item> {
|
||||
if self.cur_vpage == 0 {
|
||||
if self.vaddr < self.basis_data.root.prealloc_open_end.as_usize() {
|
||||
let mut block = [0 as u8; VPAGE_SIZE];
|
||||
for (&src, dst) in self.journal_rev.to_le_bytes().iter().zip(block.iter_mut()) {
|
||||
let mut block_iter = block.iter_mut();
|
||||
|
||||
let journal_bytes = self.basis_data.journal_rev.to_le_bytes();
|
||||
let mut slice_iter =
|
||||
journal_bytes.iter() // journal rev
|
||||
.chain(self.basis_data.root.deref().iter() // basis
|
||||
// .chain(self.dicts.as_slice() // dictionary
|
||||
).skip(self.vaddr);
|
||||
|
||||
// note that in the case that we've already serialized the journal, basis, and dictionary, this will produce nothing
|
||||
let mut written = 0;
|
||||
for(&src, dst) in slice_iter.zip(block_iter) {
|
||||
*dst = src;
|
||||
written += 1;
|
||||
}
|
||||
for (&src, dst) in self.root.deref().iter().zip(block[size_of::<JournalType>()..].iter_mut()) {
|
||||
*dst = src;
|
||||
// which allows this to correctly pad out the rest of the prealloc region with 0's.
|
||||
while written < block.len() {
|
||||
block[written] = 0;
|
||||
written += 1;
|
||||
}
|
||||
let nonce_array = self.entropy.borrow_mut().get_nonce();
|
||||
|
||||
let nonce_array = self.basis_data.entropy.borrow_mut().get_nonce();
|
||||
let nonce = Nonce::from_slice(&nonce_array);
|
||||
let ciphertext = self.cipher.encrypt(
|
||||
let ciphertext = self.basis_data.cipher.encrypt(
|
||||
&nonce,
|
||||
Payload {
|
||||
aad: &self.aad,
|
||||
aad: &self.basis_data.aad,
|
||||
msg: &block,
|
||||
}
|
||||
).unwrap();
|
||||
self.cur_vpage += 1;
|
||||
self.vaddr += VPAGE_SIZE;
|
||||
Some([&nonce_array, ciphertext.deref()].concat().try_into().unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// In basis space, the BasisRoot is located at VPAGE #1 (VPAGE #0 is always invalid).
|
||||
/// The first 4GiB is reserved for the Basis Root + Dictionary slice.
|
||||
/// Key storage begin at 4GiB.
|
||||
|
|
@ -159,7 +191,7 @@ impl DerefMut for BasisRoot {
|
|||
}
|
||||
|
||||
|
||||
|
||||
#[repr(C)]
|
||||
pub(crate) struct DictPointer {
|
||||
name: [u8; PDDB_MAX_DICT_NAME_LEN],
|
||||
age: u32, // increment every time the dictionary pointer is modified. Used to guide memory compaction.
|
||||
|
|
|
|||
|
|
@ -769,11 +769,10 @@ impl PddbOs {
|
|||
}
|
||||
|
||||
// step 8. write the System basis to Flash, at the physical locations noted above
|
||||
// extract a slice-u8 that maps onto the basis_root record, allowing us to patch this into a FLASH page
|
||||
if let Some(syskey) = self.system_basis_key {
|
||||
let key = Key::from_slice(&system_basis_key);
|
||||
let cipher = Aes256GcmSiv::new(key);
|
||||
let basis_encrypter = BasisEncrypter::new(
|
||||
let basis_encryptor = BasisEncryptor::new(
|
||||
&basis_root,
|
||||
&[],
|
||||
self.dna,
|
||||
|
|
@ -781,47 +780,29 @@ impl PddbOs {
|
|||
0,
|
||||
Rc::clone(&self.entropy),
|
||||
);
|
||||
for ppage in basis_encrypter.into_iter() {
|
||||
self.patch_data(&ppage, 0);
|
||||
if let Some(basis_v2p_map) = self.v2p_map.get(&basis_root.name) {
|
||||
for (vpage_no, ppage_data) in basis_encryptor.into_iter().enumerate() {
|
||||
let vaddr = VirtAddr::new( ((vpage_no + 1) * VPAGE_SIZE) as u64 ).unwrap();
|
||||
match basis_v2p_map.get(&vaddr) {
|
||||
Some(pp) => {
|
||||
self.patch_data(&ppage_data, pp.page_number() * PAGE_SIZE as u32);
|
||||
}
|
||||
None => {
|
||||
log::error!("Previously allocated page was not found in our map!");
|
||||
panic!("Inconsistent internal state");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log::error!("Couldn't find the system basis!");
|
||||
panic!("Inconsistent internal state");
|
||||
}
|
||||
} else {
|
||||
log::error!("System key was not found, but it should be present!");
|
||||
panic!("Inconsistent internal state");
|
||||
}
|
||||
|
||||
/*
|
||||
let br_slice: &[u8] = basis_root.deref();
|
||||
// copy the basis contents
|
||||
let mut basis_ser = vec![];
|
||||
for &b in br_slice {
|
||||
basis_ser.push(b)
|
||||
}
|
||||
// fill out any padding based on the pre-alloc space
|
||||
for _ in 0..basis_root.padding_count() {
|
||||
basis_ser.push(0)
|
||||
}
|
||||
|
||||
let basis = Basis::new(basis_root.deref(), &[]);
|
||||
for vpage in basis {
|
||||
log::info!("vpage: {:?}", vpage);
|
||||
}
|
||||
|
||||
// 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_array = self.entropy.borrow_mut().get_nonce();
|
||||
let nonce = Nonce::from_slice(&nonce_array);
|
||||
let ciphertext = cipher.encrypt(nonce, &basis_ser[size_of::<Nonce>()..]);
|
||||
|
||||
let ct_to_flash: &[u8] = ciphertext.as_ref().unwrap(); // this now contains the encrypted basis + 16-byte tag at the very end
|
||||
// FIX THIS 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.patch_data(&[&nonce_array, ct_to_flash].concat(), 0);
|
||||
} else {
|
||||
panic!("invalid state"); // we should never hit this because we created the key earlier in the same routine.
|
||||
}*/
|
||||
|
||||
// step 8. generate & write initial page table entries
|
||||
// step 9. generate & write initial page table entries
|
||||
// page table organization:
|
||||
//
|
||||
// offset from |
|
||||
|
|
|
|||
Loading…
Reference in a new issue