|
3 | 3 | * Copyright (c) The mldsa-native project authors |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
5 | 5 | */ |
| 6 | + |
6 | 7 | #ifndef MLD_CT_H |
7 | 8 | #define MLD_CT_H |
8 | 9 |
|
| 10 | +/* References |
| 11 | + * ========== |
| 12 | + * |
| 13 | + * - [libmceliece] |
| 14 | + * libmceliece implementation of Classic McEliece |
| 15 | + * Bernstein, Chou |
| 16 | + * https://lib.mceliece.org/ |
| 17 | + * |
| 18 | + * - [optblocker] |
| 19 | + * PQC forum post on opt-blockers using volatile globals |
| 20 | + * Daniel J. Bernstein |
| 21 | + * https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/hqbtIGFKIpU/m/H14H0wOlBgAJ |
| 22 | + */ |
| 23 | + |
9 | 24 | #include <stdint.h> |
10 | 25 | #include "cbmc.h" |
11 | 26 | #include "common.h" |
12 | 27 |
|
13 | | -/* TODO: add documentation here */ |
14 | | -/* TODO: add MLD_CONFIG_NO_ASM_VALUE_BARRIER to config.h */ |
| 28 | +/* Constant-time comparisons and conditional operations |
| 29 | +
|
| 30 | + We reduce the risk for compilation into variable-time code |
| 31 | + through the use of 'value barriers'. |
| 32 | +
|
| 33 | + Functionally, a value barrier is a no-op. To the compiler, however, |
| 34 | + it constitutes an arbitrary modification of its input, and therefore |
| 35 | + harden's value propagation and range analysis. |
| 36 | +
|
| 37 | + We consider two approaches to implement a value barrier: |
| 38 | + - An empty inline asm block which marks the target value as clobbered. |
| 39 | + - XOR'ing with the value of a volatile global that's set to 0; |
| 40 | + see @[optblocker] for a discussion of this idea, and |
| 41 | + @[libmceliece, inttypes/crypto_intN.h] for an implementation. |
| 42 | +
|
| 43 | + The first approach is cheap because it only prevents the compiler |
| 44 | + from reasoning about the value of the variable past the barrier, |
| 45 | + but does not directly generate additional instructions. |
| 46 | +
|
| 47 | + The second approach generates redundant loads and XOR operations |
| 48 | + and therefore comes at a higher runtime cost. However, it appears |
| 49 | + more robust towards optimization, as compilers should never drop |
| 50 | + a volatile load. |
| 51 | +
|
| 52 | + We use the empty-ASM value barrier for GCC and clang, and fall |
| 53 | + back to the global volatile barrier otherwise. |
| 54 | +
|
| 55 | + The global value barrier can be forced by setting |
| 56 | + MLD_CONFIG_NO_ASM_VALUE_BARRIER. |
| 57 | +
|
| 58 | +*/ |
| 59 | + |
15 | 60 | #if defined(MLD_HAVE_INLINE_ASM) && !defined(MLD_CONFIG_NO_ASM_VALUE_BARRIER) |
16 | 61 | #define MLD_USE_ASM_VALUE_BARRIER |
17 | 62 | #endif |
|
0 commit comments