diff --git a/loader/src/platform/cramium/swap.rs b/loader/src/platform/cramium/swap.rs index bb0a79a70..b1389e0fa 100644 --- a/loader/src/platform/cramium/swap.rs +++ b/loader/src/platform/cramium/swap.rs @@ -3,12 +3,17 @@ use core::mem::size_of; use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag}; use cramium_hal::ifram::IframRange; use cramium_hal::iox::*; +use cramium_hal::sce; use cramium_hal::udma::*; +use rand_chacha::rand_core::RngCore; +use rand_chacha::rand_core::SeedableRng; +use rand_chacha::ChaCha8Rng; use crate::bootconfig::BootConfig; use crate::platform::{SPIM_FLASH_IFRAM_ADDR, SPIM_RAM_IFRAM_ADDR}; use crate::println; use crate::swap::*; +use crate::PAGE_SIZE; /// hard coded at offset 0 of SPI FLASH for now, until we figure out if and how to move this around. const SWAP_IMG_START: usize = 0; @@ -20,11 +25,16 @@ pub struct SwapHal { // overflow AAD with panic if it's longer than this! aad_storage: [u8; 64], aad_len: usize, - cipher: Aes256GcmSiv, + src_cipher: Aes256GcmSiv, flash_spim: Spim, ram_spim: Spim, iox: Iox, udma_global: GlobalConfig, + swap_start: usize, + swap_len: usize, + swap_mac_start: usize, + swap_mac_len: usize, + dst_cipher: Aes256GcmSiv, buf: RawPage, } @@ -219,6 +229,33 @@ impl SwapHal { // fetch the header flash_spim.mem_read(SWAP_IMG_START as u32, &mut buf.data); + // compute offsets for swap + let mac_size = (swap.ram_size as usize / 4096) * size_of::(); + let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) / PAGE_SIZE; + let ram_size_actual = (swap.ram_size as usize & !(PAGE_SIZE - 1)) - mac_size_to_page; + + // generate a random key for swap + let mut trng = sce::trng::Trng::new(utralib::generated::HW_TRNG_BASE as usize); + trng.setup_raw_generation(256); + let mut seed = 0u64; + seed |= trng.get_u32().expect("TRNG error") as u64; + seed |= (trng.get_u32().expect("TRNG error") as u64) << 32; + let mut cstrng = ChaCha8Rng::seed_from_u64(seed); + // accumulate more TRNG data, because I don't trust it. + // 1. whiten the existing TRNG data with ChaCha8 + // 2. XOR in another 32 bits of TRNG data + // 3. create a new ChaCha8 from the resulting data + for _ in 0..16 { + seed = cstrng.next_u64(); + seed ^= trng.get_u32().expect("TRNG error") as u64; + cstrng = ChaCha8Rng::seed_from_u64(seed); + } + // now we might have a properly seeded cryptographically secure TRNG... + let mut dest_key = [0u8; 32]; + for word in dest_key.chunks_mut(core::mem::size_of::()) { + word.copy_from_slice(&cstrng.next_u32().to_be_bytes()); + } + // safety: buf.data is aligned to 4096-byte boundary and filled with initialized data let ssh: &SwapSourceHeader = unsafe { &*(buf.data.as_ptr() as *const &SwapSourceHeader) }; let mut hal = SwapHal { @@ -227,11 +264,16 @@ impl SwapHal { partial_nonce: [0u8; 8], aad_storage: [0u8; 64], aad_len: 0, - cipher: Aes256GcmSiv::new((&swap.key).into()), + src_cipher: Aes256GcmSiv::new((&swap.key).into()), flash_spim, ram_spim, iox, udma_global, + swap_start: 0, + swap_len: ram_size_actual, + swap_mac_start: ram_size_actual, + swap_mac_len: mac_size, + dst_cipher: Aes256GcmSiv::new((&dest_key).into()), buf, }; hal.aad_storage[..ssh.aad_len as usize].copy_from_slice(&ssh.aad[..ssh.aad_len as usize]); @@ -245,7 +287,7 @@ impl SwapHal { fn aad(&self) -> &[u8] { &self.aad_storage[..self.aad_len] } - pub fn decrypt_page_at(&mut self, offset: usize) -> &[u8] { + pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] { assert!((offset & 0xFFF) == 0, "offset is not page-aligned"); self.flash_spim.mem_read((self.image_start + offset) as u32, &mut self.buf.data); let mut nonce = [0u8; size_of::()]; @@ -259,14 +301,70 @@ impl SwapHal { aad[..self.aad_len].copy_from_slice(self.aad()); self.flash_spim .mem_read((self.image_mac_start + (offset / 4096) * size_of::()) as u32, &mut tag); - match self.cipher.decrypt_in_place_detached( + match self.src_cipher.decrypt_in_place_detached( Nonce::from_slice(&nonce), &aad[..self.aad_len], &mut self.buf.data, (&tag).into(), ) { Ok(_) => &self.buf.data, - Err(e) => panic!("Decryption error in swap: {:?}", e), + Err(e) => panic!("Decryption error from swap image: {:?}", e), + } + } + + pub fn buf_as_mut(&mut self) -> &mut [u8] { &mut self.buf.data } + + pub fn buf_as_ref(&self) -> &[u8] { &self.buf.data } + + /// Swap count is fixed at 0 by this routine. The data to be encrypted is + /// assumed to already be in `self.buf` + pub fn encrypt_swap_to(&mut self, buf: &mut [u8], dest_offset: usize, src_vaddr: usize, src_pid: u8) { + assert!(buf.len() == PAGE_SIZE); + assert!(dest_offset & (PAGE_SIZE - 1) == 0); + let mut nonce = [0u8; size_of::()]; + nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field + nonce[5] = src_pid; + let ppage_masked = dest_offset & !(PAGE_SIZE - 1); + nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]); + let vpage_masked = src_vaddr & !(PAGE_SIZE - 1); + nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]); + let aad: &[u8] = &[]; + match self.src_cipher.encrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf) { + Ok(tag) => { + self.ram_spim.mem_ram_write(dest_offset as u32, buf); + self.ram_spim.mem_ram_write( + (self.swap_mac_start + (dest_offset / PAGE_SIZE) * size_of::()) as u32, + tag.as_slice(), + ); + } + Err(e) => panic!("Encryption error to swap ram: {:?}", e), + } + } + + /// Swap count is fixed at 0 by this routine. The data to be encrypted is + /// assumed to already be in `self.buf` + pub fn decrypt_swap_from(&mut self, src_offset: usize, dst_vaddr: usize, dst_pid: u8) -> &[u8] { + assert!(src_offset & (PAGE_SIZE - 1) == 0); + let mut nonce = [0u8; size_of::()]; + nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field + nonce[5] = dst_pid; + let ppage_masked = src_offset & !(PAGE_SIZE - 1); + nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]); + let vpage_masked = dst_vaddr & !(PAGE_SIZE - 1); + nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]); + let aad: &[u8] = &[]; + let mut tag = [0u8; size_of::()]; + self.ram_spim + .mem_read((self.swap_mac_start + (src_offset / PAGE_SIZE) * size_of::()) as u32, &mut tag); + self.ram_spim.mem_read(src_offset as u32, &mut self.buf.data); + match self.src_cipher.decrypt_in_place_detached( + Nonce::from_slice(&nonce), + aad, + &mut self.buf.data, + (&tag).into(), + ) { + Ok(_) => &self.buf.data, + Err(e) => panic!("Decryption error from swap ram: {:?}", e), } } diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs index 82a5de6e9..3cae5edfb 100644 --- a/loader/src/platform/precursor/swap.rs +++ b/loader/src/platform/precursor/swap.rs @@ -1,17 +1,23 @@ use core::mem::size_of; use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag}; +use utralib::generated::*; +use super::RAM_BASE; use crate::bootconfig::BootConfig; use crate::println; use crate::swap::*; +use crate::PAGE_SIZE; pub struct SwapHal { - data_area: &'static [u8], - mac_area: &'static [u8], + src_data_area: &'static [u8], + src_mac_area: &'static [u8], partial_nonce: [u8; 8], aad: &'static [u8], - cipher: Aes256GcmSiv, + src_cipher: Aes256GcmSiv, + dst_data_area: &'static mut [u8], + dst_mac_area: &'static mut [u8], + dst_cipher: Aes256GcmSiv, buf: RawPage, } @@ -23,13 +29,35 @@ impl SwapHal { let ssh: &SwapSourceHeader = unsafe { (swap.flash_offset as *const SwapSourceHeader).as_ref().unwrap() }; let swap_len = ssh.mac_offset as usize; + + // compute the MAC area needed for the total RAM size. This is a slight over-estimate + // because once we remove the MAC area, we need even less storage, but it's a small error. + let mac_size = (swap.ram_size as usize / 4096) * size_of::(); + let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) / PAGE_SIZE; + let ram_size_actual = (swap.ram_size as usize & !(PAGE_SIZE - 1)) - mac_size_to_page; + + // access the hardware TRNG manually to generate a per-boot random key for RAM swap + let trng = CSR::new(utra::trng_kernel::HW_TRNG_KERNEL_BASE as *mut u32); + for _ in 0..4 { + // wait until the urandom port is initialized + while trng.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID) == 0 {} + // pull a dummy piece of data + trng.rf(utra::trng_kernel::URANDOM_URANDOM); + } + let mut ram_swap_key = [0u8; 32]; + for word in ram_swap_key.chunks_exact_mut(core::mem::size_of::()) { + while trng.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID) == 0 {} + let r = trng.rf(utra::trng_kernel::URANDOM_URANDOM); + word.copy_from_slice(&r.to_be_bytes()) + } + let mut hal = SwapHal { // safety: the swap raw array is guaranteed to be correctly aligned by the image maker - data_area: unsafe { + src_data_area: unsafe { core::slice::from_raw_parts((swap.flash_offset as usize + 4096) as *const u8, swap_len) }, // safety: the mac raw array is guaranteed to be correctly aligned by the image maker - mac_area: unsafe { + src_mac_area: unsafe { core::slice::from_raw_parts( (ssh.mac_offset + swap.flash_offset + 4096) as *const u8, (swap_len / 4096) * size_of::(), @@ -37,7 +65,21 @@ impl SwapHal { }, partial_nonce: [0u8; 8], aad: &ssh.aad[..ssh.aad_len as usize], - cipher: Aes256GcmSiv::new((&swap.key).into()), + src_cipher: Aes256GcmSiv::new((&swap.key).into()), + // safety: the ram swap area is guaranteed aligned by the ram_offset specifier, and our + // calculations on lengths ensure area alignment + dst_data_area: unsafe { + core::slice::from_raw_parts_mut(swap.ram_offset as *mut u8, ram_size_actual) + }, + // safety: the ram swap area is guaranteed aligned by the ram_offset specifier, and our + // calculations on lengths ensure area alignment + dst_mac_area: unsafe { + core::slice::from_raw_parts_mut( + (swap.ram_offset as usize + ram_size_actual) as *mut u8, + mac_size, + ) + }, + dst_cipher: Aes256GcmSiv::new(&ram_swap_key.into()), buf: RawPage { data: [0u8; 4096] }, }; hal.partial_nonce.copy_from_slice(&ssh.parital_nonce); @@ -47,21 +89,21 @@ impl SwapHal { } } - pub fn decrypt_page_at(&mut self, offset: usize) -> &[u8] { + 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]); // println!("offset: {:x}", offset); - self.buf.data.copy_from_slice(&self.data_area[offset..offset + 4096]); + self.buf.data.copy_from_slice(&self.src_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 + let tag = &self.src_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( + match self.src_cipher.decrypt_in_place_detached( Nonce::from_slice(&nonce), self.aad, &mut self.buf.data, @@ -72,6 +114,60 @@ impl SwapHal { } } + pub fn buf_as_mut(&mut self) -> &mut [u8] { &mut self.buf.data } + + pub fn buf_as_ref(&self) -> &[u8] { &self.buf.data } + + /// 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) { + assert!(buf.len() == PAGE_SIZE); + assert!(dest_offset & (PAGE_SIZE - 1) == 0); + let mut nonce = [0u8; size_of::()]; + nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field + nonce[5] = src_pid; + let ppage_masked = dest_offset & !(PAGE_SIZE - 1); + nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]); + let vpage_masked = src_vaddr & !(PAGE_SIZE - 1); + nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]); + let aad: &[u8] = &[]; + match self.src_cipher.encrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf) { + Ok(tag) => { + self.dst_data_area[dest_offset..dest_offset + PAGE_SIZE].copy_from_slice(buf); + let mac_offset = (dest_offset / PAGE_SIZE) * size_of::(); + self.dst_mac_area[mac_offset..mac_offset + size_of::()].copy_from_slice(tag.as_slice()); + } + Err(e) => panic!("Encryption error to swap ram: {:?}", e), + } + } + + /// Swap count is fixed at 0 by this routine. The data to be encrypted is + /// assumed to already be in `self.buf` + pub fn decrypt_swap_from(&mut self, src_offset: usize, dst_vaddr: usize, dst_pid: u8) -> &[u8] { + assert!(src_offset & (PAGE_SIZE - 1) == 0); + let mut nonce = [0u8; size_of::()]; + nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field + nonce[5] = dst_pid; + let ppage_masked = src_offset & !(PAGE_SIZE - 1); + nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]); + let vpage_masked = dst_vaddr & !(PAGE_SIZE - 1); + nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]); + let aad: &[u8] = &[]; + let mut tag = [0u8; size_of::()]; + let mac_offset = (src_offset / PAGE_SIZE) * size_of::(); + tag.copy_from_slice(&self.dst_mac_area[mac_offset..mac_offset + size_of::()]); + self.buf.data.copy_from_slice(&self.dst_data_area[src_offset..src_offset + PAGE_SIZE]); + match self.src_cipher.decrypt_in_place_detached( + Nonce::from_slice(&nonce), + aad, + &mut self.buf.data, + (&tag).into(), + ) { + Ok(_) => &self.buf.data, + Err(e) => panic!("Decryption error from swap ram: {:?}", e), + } + } + /// Grabs a slice of the internal buffer. Useful for re-using the decrypted page /// between elements of the bootloader (saving us from redundant decrypt ops), /// but extremely unsafe because we have to track the use of this buffer manually.