track address of the decrypt buffer

This commit is contained in:
bunnie 2024-04-07 22:36:54 +08:00
parent a478af445d
commit 26efde0a71
2 changed files with 12 additions and 1 deletions

View file

@ -35,6 +35,7 @@ pub struct SwapHal {
swap_mac_start: usize,
swap_mac_len: usize,
dst_cipher: Aes256GcmSiv,
buf_addr: usize,
buf: RawPage,
}
@ -274,6 +275,7 @@ impl SwapHal {
swap_mac_start: ram_size_actual,
swap_mac_len: mac_size,
dst_cipher: Aes256GcmSiv::new((&dest_key).into()),
buf_addr: 0,
buf,
};
hal.aad_storage[..ssh.aad_len as usize].copy_from_slice(&ssh.aad[..ssh.aad_len as usize]);
@ -289,6 +291,7 @@ impl SwapHal {
pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] {
assert!((offset & 0xFFF) == 0, "offset is not page-aligned");
self.buf_addr = offset;
self.flash_spim.mem_read((self.image_start + offset) as u32, &mut self.buf.data);
let mut nonce = [0u8; size_of::<Nonce>()];
nonce[..4].copy_from_slice(&(offset as u32).to_be_bytes());
@ -312,6 +315,8 @@ impl SwapHal {
}
}
pub fn decrypt_page_addr(&self) -> usize { self.buf_addr }
pub fn buf_as_mut(&mut self) -> &mut [u8] { &mut self.buf.data }
pub fn buf_as_ref(&self) -> &[u8] { &self.buf.data }

View file

@ -18,6 +18,7 @@ pub struct SwapHal {
dst_data_area: &'static mut [u8],
dst_mac_area: &'static mut [u8],
dst_cipher: Aes256GcmSiv,
buf_addr: usize,
buf: RawPage,
}
@ -80,6 +81,7 @@ impl SwapHal {
)
},
dst_cipher: Aes256GcmSiv::new(&ram_swap_key.into()),
buf_addr: 0,
buf: RawPage { data: [0u8; 4096] },
};
hal.partial_nonce.copy_from_slice(&ssh.parital_nonce);
@ -91,7 +93,8 @@ impl SwapHal {
pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] {
assert!((offset & 0xFFF) == 0, "offset is not page-aligned");
// println!("data area: {:x?}", &self.data_area[..4]);
self.buf_addr = offset;
// println!("data area: {:x?}", &self.src_data_area[..4]);
// println!("offset: {:x}", offset);
self.buf.data.copy_from_slice(&self.src_data_area[offset..offset + 4096]);
let mut nonce = [0u8; size_of::<Nonce>()];
@ -114,6 +117,8 @@ impl SwapHal {
}
}
pub fn decrypt_page_addr(&self) -> usize { self.buf_addr }
pub fn buf_as_mut(&mut self) -> &mut [u8] { &mut self.buf.data }
pub fn buf_as_ref(&self) -> &[u8] { &self.buf.data }
@ -121,6 +126,7 @@ impl SwapHal {
/// Swap count is fixed at 0 by this routine. The data to be encrypted is provided in
/// `buf`, and is replaced with part of the encrypted data upon completion of the routine.
pub fn encrypt_swap_to(&mut self, buf: &mut [u8], dest_offset: usize, src_vaddr: usize, src_pid: u8) {
println!("enc_to: pid: {}, src_vaddr: {:x} dest_offset: {:x}", src_pid, src_vaddr, dest_offset);
assert!(buf.len() == PAGE_SIZE);
assert!(dest_offset & (PAGE_SIZE - 1) == 0);
let mut nonce = [0u8; size_of::<Nonce>()];