mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
fix pointer issue in resolving swap config
also a bunch of latent debug in case we need to muck around again
This commit is contained in:
parent
8f94ecaf6c
commit
c7e1f22cfa
1 changed files with 14 additions and 8 deletions
|
|
@ -3,6 +3,7 @@ use core::mem::size_of;
|
||||||
use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag};
|
use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag};
|
||||||
|
|
||||||
use crate::bootconfig::BootConfig;
|
use crate::bootconfig::BootConfig;
|
||||||
|
use crate::println;
|
||||||
use crate::swap::*;
|
use crate::swap::*;
|
||||||
|
|
||||||
pub struct SwapHal {
|
pub struct SwapHal {
|
||||||
|
|
@ -17,12 +18,11 @@ pub struct SwapHal {
|
||||||
impl SwapHal {
|
impl SwapHal {
|
||||||
pub fn new(cfg: &BootConfig) -> Option<SwapHal> {
|
pub fn new(cfg: &BootConfig) -> Option<SwapHal> {
|
||||||
if let Some(swap) = cfg.swap {
|
if let Some(swap) = cfg.swap {
|
||||||
// sanity check this structure
|
|
||||||
assert_eq!(core::mem::size_of::<SwapSourceHeader>(), 4096);
|
|
||||||
// safety: the swap source header is guaranteed to be aligned and initialized in memory
|
// safety: the swap source header is guaranteed to be aligned and initialized in memory
|
||||||
// by the image creator.
|
// by the image creator.
|
||||||
let ssh: &SwapSourceHeader = unsafe { &*(swap.flash_offset as *const &SwapSourceHeader) };
|
let ssh: &SwapSourceHeader =
|
||||||
let swap_len = ssh.mac_offset as usize - (swap.flash_offset as usize + 4096);
|
unsafe { (swap.flash_offset as *const SwapSourceHeader).as_ref().unwrap() };
|
||||||
|
let swap_len = ssh.mac_offset as usize;
|
||||||
let mut hal = SwapHal {
|
let mut hal = SwapHal {
|
||||||
// safety: the swap raw array is guaranteed to be correctly aligned by the image maker
|
// safety: the swap raw array is guaranteed to be correctly aligned by the image maker
|
||||||
data_area: unsafe {
|
data_area: unsafe {
|
||||||
|
|
@ -31,7 +31,7 @@ impl SwapHal {
|
||||||
// safety: the mac raw array is guaranteed to be correctly aligned by the image maker
|
// safety: the mac raw array is guaranteed to be correctly aligned by the image maker
|
||||||
mac_area: unsafe {
|
mac_area: unsafe {
|
||||||
core::slice::from_raw_parts(
|
core::slice::from_raw_parts(
|
||||||
ssh.mac_offset as *const u8,
|
(ssh.mac_offset + swap.flash_offset + 4096) as *const u8,
|
||||||
(swap_len / 4096) * size_of::<Tag>(),
|
(swap_len / 4096) * size_of::<Tag>(),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
@ -49,17 +49,23 @@ impl SwapHal {
|
||||||
|
|
||||||
pub fn decrypt_page_at(&mut self, offset: usize) -> &[u8] {
|
pub fn decrypt_page_at(&mut self, offset: usize) -> &[u8] {
|
||||||
assert!((offset & 0xFFF) == 0, "offset is not page-aligned");
|
assert!((offset & 0xFFF) == 0, "offset is not page-aligned");
|
||||||
|
// println!("data area: {:x?}", &self.data_area[..4]);
|
||||||
|
// println!("offset: {:x}", offset);
|
||||||
self.buf.data.copy_from_slice(&self.data_area[offset..offset + 4096]);
|
self.buf.data.copy_from_slice(&self.data_area[offset..offset + 4096]);
|
||||||
let mut nonce = [0u8; size_of::<Nonce>()];
|
let mut nonce = [0u8; size_of::<Nonce>()];
|
||||||
nonce[..4].copy_from_slice(&(offset as u32).to_be_bytes());
|
nonce[..4].copy_from_slice(&(offset as u32).to_be_bytes());
|
||||||
nonce[4..].copy_from_slice(&self.partial_nonce);
|
nonce[4..].copy_from_slice(&self.partial_nonce);
|
||||||
|
let tag = &self.mac_area
|
||||||
|
[(offset / 4096) * size_of::<Tag>()..(offset / 4096) * size_of::<Tag>() + size_of::<Tag>()];
|
||||||
|
// println!("nonce: {:x?}", nonce);
|
||||||
|
// println!("tag: {:x?}", tag);
|
||||||
|
// println!("aad: {:x?}", self.aad);
|
||||||
|
// println!("data: {:x?}", &self.buf.data[0..32]);
|
||||||
match self.cipher.decrypt_in_place_detached(
|
match self.cipher.decrypt_in_place_detached(
|
||||||
Nonce::from_slice(&nonce),
|
Nonce::from_slice(&nonce),
|
||||||
self.aad,
|
self.aad,
|
||||||
&mut self.buf.data,
|
&mut self.buf.data,
|
||||||
(&self.mac_area
|
tag.into(),
|
||||||
[(offset / 4096) * size_of::<Tag>()..(offset / 4096) * size_of::<Tag>() + size_of::<Tag>()])
|
|
||||||
.into(),
|
|
||||||
) {
|
) {
|
||||||
Ok(_) => &self.buf.data,
|
Ok(_) => &self.buf.data,
|
||||||
Err(e) => panic!("Decryption error in swap: {:?}", e),
|
Err(e) => panic!("Decryption error in swap: {:?}", e),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue