2020-10-22 14:38:41 +02:00
|
|
|
use crate::{
|
2021-01-26 10:03:50 +01:00
|
|
|
CacheStatus, ChannelFatalError, ChannelHandle, CoreSpec, MultipleAddrCacheSideChannel,
|
|
|
|
SideChannelError, SingleAddrCacheSideChannel,
|
2020-10-22 14:38:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2020-11-20 10:52:58 +01:00
|
|
|
use std::fmt::Debug;
|
2020-10-22 14:38:41 +02:00
|
|
|
|
|
|
|
pub struct TableAttackResult {
|
|
|
|
pub addr: *const u8,
|
|
|
|
hit: u32,
|
|
|
|
miss: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TableAttackResult {
|
|
|
|
pub fn get(&self, cache_status: CacheStatus) -> u32 {
|
|
|
|
match cache_status {
|
|
|
|
CacheStatus::Hit => self.hit,
|
|
|
|
CacheStatus::Miss => self.miss,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:03:50 +01:00
|
|
|
pub trait TableCacheSideChannel<Handle: ChannelHandle>: CoreSpec + Debug {
|
2020-10-22 14:38:41 +02:00
|
|
|
//type ChannelFatalError: Debug;
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// addresses must contain only valid pointers to read.
|
|
|
|
unsafe fn calibrate(
|
|
|
|
&mut self,
|
|
|
|
addresses: impl IntoIterator<Item = *const u8> + Clone,
|
2021-01-26 10:03:50 +01:00
|
|
|
) -> Result<Vec<Handle>, ChannelFatalError>;
|
2020-10-22 14:38:41 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// addresses must contain only valid pointers to read.
|
|
|
|
unsafe fn attack<'a, 'b, 'c>(
|
|
|
|
&'a mut self,
|
2021-01-26 10:03:50 +01:00
|
|
|
addresses: impl Iterator<Item = &'c mut Handle> + Clone,
|
2020-10-22 14:38:41 +02:00
|
|
|
victim: &'b dyn Fn(),
|
|
|
|
num_iteration: u32,
|
2021-01-26 10:03:50 +01:00
|
|
|
) -> Result<Vec<TableAttackResult>, ChannelFatalError>
|
|
|
|
where
|
|
|
|
Handle: 'c;
|
2020-10-22 14:38:41 +02:00
|
|
|
}
|
|
|
|
|
2021-01-26 10:03:50 +01:00
|
|
|
impl<T: SingleAddrCacheSideChannel> TableCacheSideChannel<T::Handle> for T {
|
2020-10-22 14:38:41 +02:00
|
|
|
default unsafe fn calibrate(
|
|
|
|
&mut self,
|
|
|
|
addresses: impl IntoIterator<Item = *const u8> + Clone,
|
2021-01-26 10:03:50 +01:00
|
|
|
) -> Result<Vec<T::Handle>, ChannelFatalError> {
|
2020-10-22 14:38:41 +02:00
|
|
|
unsafe { self.calibrate_single(addresses) }
|
|
|
|
}
|
|
|
|
//type ChannelFatalError = T::SingleChannelFatalError;
|
|
|
|
|
|
|
|
default unsafe fn attack<'a, 'b, 'c>(
|
|
|
|
&'a mut self,
|
2021-01-26 10:03:50 +01:00
|
|
|
addresses: impl Iterator<Item = &'c mut T::Handle> + Clone,
|
2020-10-22 14:38:41 +02:00
|
|
|
victim: &'b dyn Fn(),
|
|
|
|
num_iteration: u32,
|
2021-01-26 10:03:50 +01:00
|
|
|
) -> Result<Vec<TableAttackResult>, ChannelFatalError>
|
|
|
|
where
|
|
|
|
T::Handle: 'c,
|
|
|
|
{
|
2020-10-22 14:38:41 +02:00
|
|
|
let mut result = Vec::new();
|
|
|
|
|
|
|
|
for addr in addresses {
|
|
|
|
let mut hit = 0;
|
|
|
|
let mut miss = 0;
|
|
|
|
for iteration in 0..100 {
|
2021-01-26 10:03:50 +01:00
|
|
|
match unsafe { self.prepare_single(addr) } {
|
2020-10-22 14:38:41 +02:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => match e {
|
|
|
|
SideChannelError::NeedRecalibration => unimplemented!(),
|
|
|
|
SideChannelError::FatalError(e) => return Err(e),
|
|
|
|
SideChannelError::AddressNotReady(_addr) => panic!(),
|
|
|
|
SideChannelError::AddressNotCalibrated(_addr) => unimplemented!(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
self.victim_single(victim);
|
2021-01-26 10:03:50 +01:00
|
|
|
let r = unsafe { self.test_single(addr) };
|
2020-10-22 14:38:41 +02:00
|
|
|
match r {
|
|
|
|
Ok(status) => {}
|
|
|
|
Err(e) => match e {
|
|
|
|
SideChannelError::NeedRecalibration => panic!(),
|
|
|
|
SideChannelError::FatalError(e) => {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
_ => panic!(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _iteration in 0..num_iteration {
|
2021-01-26 10:03:50 +01:00
|
|
|
match unsafe { self.prepare_single(addr) } {
|
2020-10-22 14:38:41 +02:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => match e {
|
|
|
|
SideChannelError::NeedRecalibration => unimplemented!(),
|
|
|
|
SideChannelError::FatalError(e) => return Err(e),
|
|
|
|
SideChannelError::AddressNotReady(_addr) => panic!(),
|
|
|
|
SideChannelError::AddressNotCalibrated(_addr) => unimplemented!(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
self.victim_single(victim);
|
2021-01-26 10:03:50 +01:00
|
|
|
let r = unsafe { self.test_single(addr) };
|
2020-10-22 14:38:41 +02:00
|
|
|
match r {
|
|
|
|
Ok(status) => match status {
|
|
|
|
CacheStatus::Hit => {
|
|
|
|
hit += 1;
|
|
|
|
}
|
|
|
|
CacheStatus::Miss => {
|
|
|
|
miss += 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => match e {
|
|
|
|
SideChannelError::NeedRecalibration => panic!(),
|
|
|
|
SideChannelError::FatalError(e) => {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
_ => panic!(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push(TableAttackResult {
|
2021-01-26 10:03:50 +01:00
|
|
|
addr: addr.to_const_u8_pointer(),
|
2020-10-22 14:38:41 +02:00
|
|
|
hit,
|
|
|
|
miss,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO limit number of simultaneous tested address + randomise order ?
|
|
|
|
|
2021-01-26 10:03:50 +01:00
|
|
|
impl<T: MultipleAddrCacheSideChannel> TableCacheSideChannel<T::Handle> for T {
|
2020-10-22 14:38:41 +02:00
|
|
|
unsafe fn calibrate(
|
|
|
|
&mut self,
|
|
|
|
addresses: impl IntoIterator<Item = *const u8> + Clone,
|
2021-01-26 10:03:50 +01:00
|
|
|
) -> Result<Vec<T::Handle>, ChannelFatalError> {
|
2020-10-22 14:38:41 +02:00
|
|
|
unsafe { self.calibrate(addresses) }
|
|
|
|
}
|
|
|
|
//type ChannelFatalError = T::MultipleChannelFatalError;
|
|
|
|
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// addresses must contain only valid pointers to read.
|
|
|
|
unsafe fn attack<'a, 'b, 'c>(
|
|
|
|
&'a mut self,
|
2021-01-26 10:03:50 +01:00
|
|
|
mut addresses: impl Iterator<Item = &'c mut T::Handle> + Clone,
|
2020-10-22 14:38:41 +02:00
|
|
|
victim: &'b dyn Fn(),
|
|
|
|
num_iteration: u32,
|
2021-01-26 10:03:50 +01:00
|
|
|
) -> Result<Vec<TableAttackResult>, ChannelFatalError>
|
|
|
|
where
|
|
|
|
T::Handle: 'c,
|
|
|
|
{
|
2020-10-22 14:38:41 +02:00
|
|
|
let mut v = Vec::new();
|
|
|
|
while let Some(addr) = addresses.next() {
|
|
|
|
let mut batch = Vec::new();
|
2021-01-26 10:03:50 +01:00
|
|
|
batch.push(addr);
|
2020-10-22 14:38:41 +02:00
|
|
|
let mut hits: HashMap<*const u8, u32> = HashMap::new();
|
|
|
|
let mut misses: HashMap<*const u8, u32> = HashMap::new();
|
|
|
|
for i in 1..T::MAX_ADDR {
|
|
|
|
if let Some(addr) = addresses.next() {
|
2021-01-26 10:03:50 +01:00
|
|
|
batch.push(addr);
|
2020-10-22 14:38:41 +02:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i in 0..100 {
|
|
|
|
// TODO Warmup
|
|
|
|
}
|
|
|
|
for i in 0..num_iteration {
|
2021-01-26 10:03:50 +01:00
|
|
|
match unsafe { MultipleAddrCacheSideChannel::prepare(self, &mut batch) } {
|
2020-10-22 14:38:41 +02:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => match e {
|
|
|
|
SideChannelError::NeedRecalibration => unimplemented!(),
|
|
|
|
SideChannelError::FatalError(e) => return Err(e),
|
|
|
|
SideChannelError::AddressNotReady(_addr) => panic!(),
|
|
|
|
SideChannelError::AddressNotCalibrated(addr) => {
|
|
|
|
eprintln!(
|
|
|
|
"Addr: {:p}\n\
|
|
|
|
{:#?}",
|
|
|
|
addr, self
|
|
|
|
);
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
MultipleAddrCacheSideChannel::victim(self, victim);
|
|
|
|
|
2021-01-26 10:03:50 +01:00
|
|
|
let r = unsafe { MultipleAddrCacheSideChannel::test(self, &mut batch) }; // Fixme error handling
|
2020-10-22 14:38:41 +02:00
|
|
|
match r {
|
|
|
|
Err(e) => match e {
|
|
|
|
SideChannelError::NeedRecalibration => {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
SideChannelError::FatalError(e) => {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Ok(vector) => {
|
|
|
|
for (addr, status) in vector {
|
|
|
|
match status {
|
|
|
|
CacheStatus::Hit => {
|
|
|
|
*hits.entry(addr).or_default() += 1;
|
|
|
|
}
|
|
|
|
CacheStatus::Miss => {
|
|
|
|
*misses.entry(addr).or_default() += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for addr in batch {
|
|
|
|
v.push(TableAttackResult {
|
2021-01-26 10:03:50 +01:00
|
|
|
addr: addr.to_const_u8_pointer(),
|
|
|
|
hit: *hits.get(&addr.to_const_u8_pointer()).unwrap_or(&0u32),
|
|
|
|
miss: *misses.get(&addr.to_const_u8_pointer()).unwrap_or(&0u32),
|
2020-10-22 14:38:41 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(v)
|
|
|
|
}
|
|
|
|
}
|