-
Notifications
You must be signed in to change notification settings - Fork 698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
harden GFp_memcmp #893
Comments
/cc @davidben. I'm quite reluctant to do weird things with |
I was able to reproduce the above issue. However, in the current implementation, this will only be an issue if LTO is turned on.
However, if LTO is enabled for all of ring's code, I agree with @briansmith that something should be done, as binaries which transitivly depend on ring may set |
I'm not sure those early exits are actually a problem. Looking at more complete compiler output, it's vectorized the bulk of the loop and then the bit at the bottom is an unrolled tail (unrolled since, after vectorization, it has bounded size). That unrolled bit indeed needs to branch because it needs to check the In the general case, yeah, constant-time code in general is always at the mercy of the compiler. We have to play this game of handwaiving and judiciously applying value barriers until the right language-level primitives are available. |
Related: briansmith/ring#1444 Related: briansmith/ring#893
GFp_memcmp may be optimized by compiler and make function not constant time.
Reasoning: the caller of the function only checks if the returned value is 0 or non-zero. As soon as the returned value is non-zero, the function can exit early. The compiler may be able to infer this on its own:
I tested this with gcc (Ubuntu 4.8.5-4ubuntu8~14.04.2) 4.8.5 with the following code:
The compiler inlined the call to GFp_memcmp() and unrolled the loop, looking like:
where address 400b8c contains the call to printf.
Note that I tested with gcc (did not work with clang).
One trick to help is to mark the returned value as volatile, forcing the compiler to evaluate each comparison in the loop:
The text was updated successfully, but these errors were encountered: