diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs index 81423e184..e5252fbc5 100644 --- a/loader/src/platform/precursor/swap.rs +++ b/loader/src/platform/precursor/swap.rs @@ -3,6 +3,7 @@ use core::mem::size_of; use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag}; use crate::bootconfig::BootConfig; +use crate::println; use crate::swap::*; pub struct SwapHal { @@ -17,12 +18,11 @@ pub struct SwapHal { impl SwapHal { pub fn new(cfg: &BootConfig) -> Option { if let Some(swap) = cfg.swap { - // sanity check this structure - assert_eq!(core::mem::size_of::(), 4096); // safety: the swap source header is guaranteed to be aligned and initialized in memory // by the image creator. - let ssh: &SwapSourceHeader = unsafe { &*(swap.flash_offset as *const &SwapSourceHeader) }; - let swap_len = ssh.mac_offset as usize - (swap.flash_offset as usize + 4096); + let ssh: &SwapSourceHeader = + unsafe { (swap.flash_offset as *const SwapSourceHeader).as_ref().unwrap() }; + let swap_len = ssh.mac_offset as usize; let mut hal = SwapHal { // safety: the swap raw array is guaranteed to be correctly aligned by the image maker data_area: unsafe { @@ -31,7 +31,7 @@ impl SwapHal { // safety: the mac raw array is guaranteed to be correctly aligned by the image maker mac_area: unsafe { 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::(), ) }, @@ -49,17 +49,23 @@ impl SwapHal { pub fn decrypt_page_at(&mut self, offset: usize) -> &[u8] { 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]); let mut nonce = [0u8; size_of::()]; nonce[..4].copy_from_slice(&(offset as u32).to_be_bytes()); nonce[4..].copy_from_slice(&self.partial_nonce); + let tag = &self.mac_area + [(offset / 4096) * size_of::()..(offset / 4096) * size_of::() + size_of::()]; + // 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( Nonce::from_slice(&nonce), self.aad, &mut self.buf.data, - (&self.mac_area - [(offset / 4096) * size_of::()..(offset / 4096) * size_of::() + size_of::()]) - .into(), + tag.into(), ) { Ok(_) => &self.buf.data, Err(e) => panic!("Decryption error in swap: {:?}", e),