From 646db42766cd88d54778507527e51087c9053762 Mon Sep 17 00:00:00 2001 From: Guillume DIDIER Date: Mon, 20 Sep 2021 15:44:14 +0200 Subject: [PATCH] Start work on a Cache Slicing type that fallsback gracefully, for cache attack purposes. It falls back to using the cache line virtual addr (without offset) as the hash when the hashing function is unknown. Still work in progress to implment all the required functions, and then adpat any user to thechange in types. --- cache_utils/src/complex_addressing.rs | 49 +++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/cache_utils/src/complex_addressing.rs b/cache_utils/src/complex_addressing.rs index 6c00a9c..69c0073 100644 --- a/cache_utils/src/complex_addressing.rs +++ b/cache_utils/src/complex_addressing.rs @@ -120,8 +120,8 @@ pub fn cache_slicing( } } -fn hash(addr: usize, mask: usize) -> u8 { - ((addr & mask).count_ones() & 1) as u8 +fn hash(addr: usize, mask: usize) -> usize { + ((addr & mask).count_ones() & 1) as usize } impl CacheSlicing { @@ -140,7 +140,7 @@ impl CacheSlicing { res <<= 1; res |= hash(addr, *mask); } - Some(res) + Some(res as u8) } _ => None, } @@ -267,3 +267,46 @@ impl CacheSlicing { } } } + +/** +Type used to handle unsupported hash functions by using the Cache line addr as the Hash. +*/ +#[derive(Debug, Copy, Clone)] +pub enum CacheAttackSlicing { + Unsupported(usize), + ComplexAddressing(&'static [usize]), + SimpleAddressing(SimpleAddressingParams), + NoSlice, +} + +// TODO +impl CacheAttackSlicing { + pub fn from(cs: CacheSlicing, cache_line_length: usize) -> CacheAttackSlicing { + match cs { + Unsupported => CacheAttackSlicing::Unsupported(!((1 << cache_line_length) - 1)), + ComplexAddressing(ca) => CacheAttackSlicing::ComplexAddressing(ca), + SimpleAddressing(sa) => CacheAttackSlicing::SimpleAddressing(sa), + NoSlice => CacheAttackSlicing::NoSlice, + } + } + + pub fn hash(&self, addr: usize) -> usize { + match self { + CacheAttackSlicing::Unsupported(mask) => addr & mask, + CacheAttackSlicing::SimpleAddressing(mask) => { + (addr >> mask.shift) & ((1 << mask.bits) - 1) + } + CacheAttackSlicing::ComplexAddressing(masks) => { + let mut res = 0; + for mask in *masks { + res <<= 1; + res |= hash(addr, *mask); + } + res + } + CacheAttackSlicing::NoSlice => 0usize, + } + } + + // TODO +}