Great question! If you must use table lookups (e.g., for S-boxes in AES), mitigating timing leaks is challenging but possible. Here’s a detailed answer:
1. Why Table Lookups Leak Timing
Table lookups (like SBox[state_byte]) can leak information because:
- Cache timing: Access time depends on whether the data is in the CPU cache, which can be influenced by secret data.
- Microarchitectural attacks: Attackers can use techniques like Flush+Reload, Prime+Probe, etc., to infer which table entries were accessed.
2. Software Mitigations
A. Constant-Time Table Lookups
You can simulate a table lookup in constant time by accessing every entry and using bitwise operations to select the correct value. For example:
uint8_t constant_time_lookup(const uint8_t *table, uint8_t index) {
uint8_t result = 0;
for (int i = 0; i < 256; i++) {
// mask is 0xFF if i == index, 0x00 otherwise
uint8_t mask = (i == index) ? 0xFF : 0x00;
result |= table[i] & mask;
}
return result;
}
- Downside: This is much slower than a normal lookup, but it prevents cache-based timing leaks.
B. Table Preloading ("Cache Warming")
Before using the table, access every entry to ensure it is loaded into the cache. This can help, but is not foolproof:
- The attacker may still be able to evict cache lines.
- Not effective against all microarchitectural attacks.
C. Use Small Tables in Registers
If the table is small enough (e.g., 16 or 32 bytes), you can load it into registers and use constant-time selection logic. This is rarely practical for AES S-boxes (256 bytes).
3. Hardware/OS Mitigations
A. Kernel Features
a. Page Table Isolation (KPTI/KAISER)
- Protects kernel memory from user space, but does not protect user processes from each other.
b. mlock() / mlockall()
- Locks memory pages in RAM, preventing them from being swapped out, but does not prevent cache attacks.
c. seccomp, namespaces, etc.
- Restrict what an attacker can do, but .