Implement value barrier and apply to FrodoKEM#2431
Conversation
7ed7fe4 to
96c3a6b
Compare
There was a problem hiding this comment.
Thanks @xuganyu96 for adding this very useful feature.
I haven’t reviewed the FO cache oracle test in detail, but will this be run in CI? If not, is there a plan to run it regularly?
Regarding the countermeasures:
Use inline assembly
__asm__ volatile("" : "+r"(v) :)where available
Use a volatile round-trip that reads and writes every byte in the input buffer as a fallback
I have two comments/questions:
- It might be good to document with a rationale or related work why they are effective.
- The fallback option (reading and writing every byte) seems potentially expensive if it is used with larger data structures than with the selector byte. Were there more efficient alternatives considered?
See also the comments inline.
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Amazon Linux 2023 ships with Clang 15 by default. PoC requires Clang 17 or above to work. Install with `sudo dnf install clang18` and use CMAKE_C_COMPILER to specify clang-18 as the compiler. Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
I can parameterize control location, too, but 63 is a safe value for KEM secret keys since rejection symbols are probably 32 bytes in length Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Python script is generated by Claude using Sonnet 4.6 extended. The prompt: Please help me write a Python script to perform statistical analysis that determines whether two sets of samples come from the same distribution. The input CSV file has header "epoch,sample,good,probe,ctrl`. Some context for the data: - We are trying to detect timing side channel in implementation of key encapsulation mechanisms (e.g. FrodoKEM). In some KEM schemes, the secret key contains a rejection symbol. In a faulty implementation, if the input ciphertext to a decapsulation routine is valid, then the rejection symbol is not used, and if the input ciphertext is invalid, then the rejection symbol is used and thus likely brought into CPU cache. We want to probe the time it takes to read the rejection symbol in the secret key and see if there is a cache timing channel. - There are many epochs, in each epoch there are many samples - Each sample represents one call to decapsulation. The `good` column is 1 if the ciphertext is valid, else it's 0. The `probe` column is the cycle count for reading from the rejection symbol. The `ctrl` column is the cycle count for reading another location that is guaranteed to have been used, which serves as a control The statistical analysis should have the following components: - For each epoch, discard the outliers. The percentage of outliers to be discarded should be tunable. - Compute the relevant p-value - Graph a violin plot The Python script should have a CLI where the user can specify percentage of outliers to discard, but also have a core routine that other test modules can import for unit testing. Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Also only build kem_fo_cache_oracle only when -DOQS_ENABLE_KEM_FO_CACHE_ORACLE=ON Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Not sure why this section in CONFIGURE.md is duplicated after a rebase Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
Signed-off-by: Ganyu (Bruce) Xu <g66xu@uwaterloo.ca>
96c3a6b to
1cbbe5d
Compare
I don't plan to run this on GitHub Actions because the cache timing test must run on bare metal for cache eviction to be meaningful and for timing measurements to be accurate. I've added a line to
I borrowed the inline ASM optimization barrier from mlkem-native. Here is one example. I am not sure if there is any official documents recommending this pattern, but it does seem to be the gold standard across many prominent projects.
The fallback option is meant to cover for Windows builds, since MSVC does not support inline assembly in the same way GNU/LLVM does. I plan to follow up in a subsequent patch to add an appropriate MSVC backend for optimization barrier. This round trip is just an unfortunate temporary band-aid. |
|
Thanks @xuganyu96 for the update.
Can we document this somewhere (e.g., as code comment, reference to mlkem-native). Might be helpful to keep the reference. |
| #define ARGS_HELP_TEXT \ | ||
| "Usage: %s <kem_name> <probe_loc>\n" \ | ||
| "Arguments:\n" \ | ||
| " kem_name: FrodoKEM-640-AES\n" \ |
There was a problem hiding this comment.
Why just FrodoKEM-640-AES? Worthwhile listing all algs this could be applied to (be arguments)?
| ----- | ||
| .. code-block:: bash | ||
|
|
||
| python timing_analysis.py samples.csv |
There was a problem hiding this comment.
This Usage sample seems to disagree with the script's name. Intentional?
Summary
This pull request implements a
OQS_MEM_BLACK_BOXmacro, which serves as a frontend to a variety of implementations of value barriers:__asm__ volatile("" : "+r"(v) :)where availableThe black box is then applied to FrodoKEM's
ct_selectto prevent compiler from reasoning aboutselectorwhen inliningct_select.Code change
value barrier implementation and application
src/common/common.hsrc/kem/frodokem/external/util.cTest harness for detecting cache timing side channel in FO-transformed KEM
tests/CMakeLists.txttests/kem_fo_cache_oracle.ctests/kem_fo_cache_oracle.pyThanks to @kaminuma for providing the initial PoC.
kem_fo_cache_oracle.cis a command-line program that measures the time it takes to read from a user-specified location of the KEM secret key after decapsulating some valid and/or invalid ciphertexts.kem_fo_cache_oracle.pyis a Python script that performs statistical analysis of the raw timing measurements fromkem_fo_cache_oracle.cprogram.Commands for using these two programs were documented in
CONFIGURE.md.Validation
Manually verify the absence of conditional memory reads from the compiled assembly with the following build commands:
-DOQS_DISABLE_MEM_BLACK_BOX,<OQS_KEM_frodokem_640_aes_decaps>contains acselinstruction.-DOQS_DISABLE_MEM_BLACK_BOX,<OQS_KEM_frodokem_640_aes_decaps>does not containcselinstruction. This is true for all FrodoKEM parameters (with theOQS_MINIMAL_BUILDoptions removed).-DOQS_DISABLE_MEM_BLACK_BOX, but modifying source code to force fallback,<OQS_KEM_frodokem_640_aes_decaps>does not containcseleither.LLM disclosure
kem_fo_cache_oracle.pyis generated with help from Claude.