I have heard that running code in a managed language, e.g. Java in the JVM, is impossible to protect against timing attacks due to the JITing nature of most managed runtimes. In concept I can agree with this, but I would like to understand what the current industry knowledge around this is.
A broad statement such as "this is impossible" is unlikely to be 100% accurate.
It is true that Java (and similar languages such as C#/.NET) runs with a JIT compiler that will produce the actual opcodes only at runtime, so the sequence of opcodes is harder to predict; it may also change with the VM implementation version. Moreover, the language has an efficient GC that promotes dynamic memory usage, and in particular immutable strings, which offer a lot of data-dependent timing behaviours.
It is still possible to write code that is immune to timing attacks, in particular for cryptographic algorithms. You just have to shun some of the nice properties of Java, and use it as if it was some kind of watered-down C. Work on arrays of
char, use arithmetic rather than tests... (so don't use
boolean). For instance, this timing-immune code compares
y, and swaps them if
x is greater than
int cc = (y - x) >> 31; int tmpx = (cc & x) | (~cc & y); int tmpy = (~cc & x) | (cc & y); x = tmpy; y = tmpx;
We may note that Java makes such code actually somewhat easier to write than C since, contrary to C, it offers standard, fixed behaviour when right-shifting a negative value.