Skip to content

Commit 2da7a4f

Browse files
committed
CT: Improve documentation and add MLD_CONFIG_NO_ASM_VALUE_BARRIER to config.h
Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 7c3ac3c commit 2da7a4f

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

mldsa/config.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,20 @@
179179
*****************************************************************************/
180180
/* #define MLD_CONFIG_NO_ASM */
181181

182+
/******************************************************************************
183+
* Name: MLD_CONFIG_NO_ASM_VALUE_BARRIER
184+
*
185+
* Description: If this option is set, mldsa-native will be built without
186+
* use of native code or inline assembly for value barriers.
187+
*
188+
* By default, inline assembly (if available) is used to implement
189+
* value barriers.
190+
* Without inline assembly, mldsa-native will use a global volatile
191+
* 'opt blocker' instead; see ct.h.
192+
*
193+
*****************************************************************************/
194+
/* #define MLD_CONFIG_NO_ASM_VALUE_BARRIER */
195+
196+
182197

183198
#endif /* !MLD_CONFIG_H */

mldsa/ct.h

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,60 @@
33
* Copyright (c) The mldsa-native project authors
44
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
55
*/
6+
67
#ifndef MLD_CT_H
78
#define MLD_CT_H
89

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+
924
#include <stdint.h>
1025
#include "cbmc.h"
1126
#include "common.h"
1227

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+
1560
#if defined(MLD_HAVE_INLINE_ASM) && !defined(MLD_CONFIG_NO_ASM_VALUE_BARRIER)
1661
#define MLD_USE_ASM_VALUE_BARRIER
1762
#endif

0 commit comments

Comments
 (0)