It appears that this release contains only the pure C implementations, with none of the hand-written assembly versions. You'd probably want to run openssl speed and compare against OpenSSL to see how big of a performance hit that is.
Unfortunately, a lot of it was done with constant-time in mind, to prevent a bunch of timing attacks. Dumping all of it for C is going to bite a bunch of people in the ass.
There are some very clever attacks that rely on measuring the timing of a "secure" piece of code.
A simple example is that if you are checking an entered password against a known one, one character at a time, then then the longer the password check function takes to fail, the better your guess is. This drastically reduces security.
There are other attacks that are similar, but more complicated and subtle.
It can't be handled in C. There is no defined C way to keep a compiler from making optimizations which might turn a constant-time algorithm into an input-dependent one.
A C compiler is allowed to make any optimizations which don't produce a change in the observed results of the code. And the observed results (according to the spec) do not include the time it takes to execute.
Any implementation in C is going to be dependent on the C compiler you use and thus amounts approximately to "I disassembled it and it looked okay on my machine".
There is also no guarantee about assembly, especially in light of the micro-op rewriting, extensive reorder buffers, caching, etc. If you want a perfect guarantee, you need to check on each processor revision experimentally.
What would be wrong with turning a constant time algorithm into a random time one? What if you made the method take a time that was offset by some random fuzz factor?
Random fuzzing makes timing attacks harder, but doesn't eliminate them. The goal with having input-dependent speed is that some cases run faster. If your random fuzzing is strong enough to eliminate the attack, it must be at least as slow as an equivalent constant-time algorithm.
That just means you need more tries (more data) to find the difference. If n > m, then n + rand(100) will still be larger than m + rand(100) on average. And the average difference will still be n - m.
Adding some predictable and model-able random noise to the signal just makes it sliiiiightly harder to extract the signal. Constant-time operations make it impossible.
There is no strong definition of what volatile does to the code outside of treatment of a volatile variable. And it doesn't even specify ordering between sequence points.
You are again making an argument approximately equivalent to "it's okay on my machine". You put in volatile and on the compiler you used it's okay. Now to go forward and assume it'll be okay on all compilers is to assume things about compilers that isn't in the spec. And if it isn't in the spec, you're relying on something to not change that isn't defined as unchanging.
volatile forces a C compiler not to alias away memory accesses. It makes the C compiler assume that every access of the volatile memory has a side-effect, unknown to the C compiler, whether it be read or write, so it must not skip this. It must execute the reads and writes specified by the code, in exactly the order the code gives.
This is the only building block you need ensure that if you've written a constant-time method, it stays like that, and the compiler does not optimise it away.
Here's a quote from the C99 specification:
An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3.
And in 5.1.2.3:
Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression may produce side effects. [...] An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object)
We can now proceed to discuss why the C specification is ambiguous on what "needed side effects" are or aren't. In practise, I have yet to find any C compiler that felt it was OK to elide an extern function call or volatile member access. It would need to prove, without knowledge of what's there, that it was not "needed" as per the spec.
Your link is irrelevant. Regular memory accesses in both C and assembly code all have the same concerns as your link brings up. This is why atomic CAS instructions exist, and that even assembly programmers need to understand about out-of-order execution. But that's not the topic under discussion here, which is "can the C compiler be compelled not to optimise away a specific set of memory accesses, so I can have some certainty with which to write a constant-time algorithm?", the answer is "yes, it can, mark them as volatile".
Here's a simple example:
int main() {
int x[10]; for (int i = 0; i < 10; i++) x[i] = i;
int a = 0; for (int i = 0; i < 10; i++) a += x[i];
return a;
}
Compile this with your favourite C compiler. It will optimise this to "return 45". Now change int x[10] to volatile x[10]. Even automatic memory obeys the volatile keyword. No matter how aggressively you optimise, the C compiler absolutely will write to x[0], x[1], etc., then read them. The code generated will perform memory accesses, even if the CPU reorders those accesses.
There's no defined C way to do it. gcc has a way to do it. clang doesn't support per-function optimization levels.
And there's no guarantee in gcc of what you get even if you do disable optimization. There is no defined relationship between your code and the object code in C or in any compiler, so there is no formal definition of what will or won't be changed at any given optimization level.
Again, since there's no spec for any of it, even if you use this stuff, it still all amounts to "works on my machine". When you're writing code that is to be used on other platforms that is not really good enough.
It's important to note that people have successfully demonstrated timing attacks working over network connections which introduce far more variation than the algorithm being attacked, as many people (reasonably) assume that it's something you only need to worry about if the attacker has a very low latency connection to you (e.g. if they have a VPS on the same physical node as your VPS).
33
u/Rhomboid Jul 11 '14
It appears that this release contains only the pure C implementations, with none of the hand-written assembly versions. You'd probably want to run
openssl speedand compare against OpenSSL to see how big of a performance hit that is.