1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright (c) The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
*/
/* References
* ==========
*
* - [FIPS203]
* FIPS 203 Module-Lattice-Based Key-Encapsulation Mechanism Standard
* National Institute of Standards and Technology
* https://csrc.nist.gov/pubs/fips/203/final
*
* - [REF]
* CRYSTALS-Kyber C reference implementation
* Bos, Ducas, Kiltz, Lepoint, Lyubashevsky, Schanck, Schwabe, Seiler, Stehlé
* https://github.com/pq-crystals/kyber/tree/main/ref
*
* - [libmceliece]
* libmceliece implementation of Classic McEliece
* Bernstein, Chou
* https://lib.mceliece.org/
*
* - [optblocker]
* PQC forum post on opt-blockers using volatile globals
* Daniel J. Bernstein
* https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/hqbtIGFKIpU/m/H14H0wOlBgAJ
*/
/* Constant-time comparisons and conditional operations
We reduce the risk for compilation into variable-time code
through the use of 'value barriers'.
Functionally, a value barrier is a no-op. To the compiler, however,
it constitutes an arbitrary modification of its input, and therefore
harden's value propagation and range analysis.
We consider two approaches to implement a value barrier:
- An empty inline asm block which marks the target value as clobbered.
- XOR'ing with the value of a volatile global that's set to 0;
see @[optblocker] for a discussion of this idea, and
@[libmceliece, inttypes/crypto_intN.h] for an implementation.
The first approach is cheap because it only prevents the compiler
from reasoning about the value of the variable past the barrier,
but does not directly generate additional instructions.
The second approach generates redundant loads and XOR operations
and therefore comes at a higher runtime cost. However, it appears
more robust towards optimization, as compilers should never drop
a volatile load.
We use the empty-ASM value barrier for GCC and clang, and fall
back to the global volatile barrier otherwise.
The global value barrier can be forced by setting
MLK_CONFIG_NO_ASM_VALUE_BARRIER.
*/
/*
* Declaration of global volatile that the global value barrier
* is loading from and masking with.
*/
extern volatile uint64_t mlk_ct_opt_blocker_u64;
/* Helper functions for obtaining global masks of various sizes */
/* This contract is not proved but treated as an axiom.
*
* Its validity relies on the assumption that the global opt-blocker
* constant mlk_ct_opt_blocker_u64 is not modified.
*/
static MLK_INLINE uint64_t
static MLK_INLINE uint8_t
static MLK_INLINE uint32_t
static MLK_INLINE int32_t
/* Opt-blocker based implementation of value barriers */
static MLK_INLINE uint32_t
static MLK_INLINE int32_t
static MLK_INLINE uint8_t
static MLK_INLINE uint32_t
static MLK_INLINE int32_t
static MLK_INLINE uint8_t
/* MLK_USE_ASM_VALUE_BARRIER */
/*
* The ct_cmask_nonzero_xxx functions below make deliberate use of unsigned
* overflow, which is fully defined behaviour in C. It is thus safe to disable
* this warning.
*/
/*************************************************
* Name: mlk_ct_cmask_nonzero_u16
*
* Description: Return 0 if input is zero, and -1 otherwise.
*
* Arguments: uint16_t x: Value to be converted into a mask
*
**************************************************/
/* Reference: Embedded in `cmov_int16()` in the reference implementation @[REF].
* - Use value barrier and shift instead of `b = -b` to
* convert condition into mask. */
static MLK_INLINE uint16_t
/*************************************************
* Name: mlk_ct_cmask_nonzero_u8
*
* Description: Return 0 if input is zero, and -1 otherwise.
*
* Arguments: uint8_t x: Value to be converted into a mask
*
**************************************************/
/* Reference: Embedded in `verify()` and `cmov()` in the
* reference implementation @[REF].
* - We include a value barrier not present in the
* reference implementation, to prevent the compiler
* from realizing that this function returns a mask. */
static MLK_INLINE uint8_t
/* Put unsigned overflow warnings in CBMC back into scope */
/*
* The mlk_ct_cmask_neg_i16 function below makes deliberate use of
* signed to unsigned integer conversion, which is fully defined
* behaviour in C. It is thus safe to disable this warning.
*/
/*************************************************
* Name: mlk_ct_cmask_neg_i16
*
* Description: Return 0 if input is non-negative, and -1 otherwise.
*
* Arguments: uint16_t x: Value to be converted into a mask
*
**************************************************/
/* Reference: Embedded in polynomial compression function in the
* reference implementation @[REF].
* - Used as part of signed->unsigned conversion for modular
* representatives to detect whether the input is negative.
* This happen in `mlk_poly_reduce()` here, and as part of
* polynomial compression functions in the reference
* implementation. See `mlk_poly_reduce()`.
* - We use value barriers to reduce the risk of
* compiler-introduced branches. */
static MLK_INLINE uint16_t
/* Put unsigned-to-signed warnings in CBMC back into scope */
/*
* The ct_csel_xxx functions below make deliberate use of unsigned
* to signed integer conversion, which is implementation-defined
* behaviour. Here, we assume that uint16_t -> int16_t is inverse
* to int16_t -> uint16_t.
*/
/*************************************************
* Name: mlk_ct_sel_int16
*
* Description: Functionally equivalent to cond ? a : b,
* but implemented with guards against
* compiler-introduced branches.
*
* Arguments: int16_t a: First alternative
* int16_t b: Second alternative
* uint16_t cond: Condition variable.
*
* Specification:
* - With `a = MLKEM_Q_HALF` and `b=0`, this essentially
* implements `Decompress_1` @[FIPS203, Eq (4.8)] in `mlk_poly_frommsg()`.
* - With `a = x + MLKEM_Q`, `b = x`, and `cond` indicating whether `x`
* is negative, implements signed->unsigned conversion of modular
* representatives. Questions of representation are not considered
* in the specification @[FIPS203, Section 2.4.1, "The pseudocode is
* agnostic regarding how an integer modulo 𝑚 is represented in
* actual implementations"].
*
**************************************************/
/* Reference: Embedded in polynomial compression function in the
* reference implementation @[REF].
* - Used as part of signed->unsigned conversion for modular
* representatives. This happen in `mlk_poly_reduce()` here,
* and as part of polynomial compression functions in @[REF].
* See `mlk_poly_reduce()`.
* - Barrier to reduce the risk of compiler-introduced branches.
* For `a = MLKEM_Q_HALF` and `b=0`, also embedded in
* `poly_frommsg()` from the reference implementation, which uses
* `cmov_int16()` instead. */
static MLK_INLINE int16_t
/* Put unsigned-to-signed warnings in CBMC back into scope */
/*************************************************
* Name: mlk_ct_sel_uint8
*
* Description: Functionally equivalent to cond ? a : b,
* but implemented with guards against
* compiler-introduced branches.
*
* Arguments: uint8_t a: First alternative
* uint8_t b: Second alternative
* uuint8_t cond: Condition variable.
*
**************************************************/
/* Reference: Embedded into `cmov()` in the reference implementation @[REF].
* - Use value barrier to get mask from condition value. */
static MLK_INLINE uint8_t
/*************************************************
* Name: mlk_ct_memcmp
*
* Description: Compare two arrays for equality in constant time.
*
* Arguments: const uint8_t *a: pointer to first byte array
* const uint8_t *b: pointer to second byte array
* size_t len: length of the byte arrays
*
* Returns 0 if the byte arrays are equal, a non-zero value otherwise
*
* Specification:
* - Used to securely compute conditional move in
* @[FIPS203, Algorithm 18 (ML-KEM.Decaps_Internal, L9-11]
*
**************************************************/
/* Reference: `cmov()` in the reference implementation @[REF]
* - We return `uint8_t`, not `int`.
* - We use an additional XOR-accumulator in the comparison loop
* which prevents early abort if the OR-accumulator is 0xFF.
* - We use a value barrier to convert the OR-accumulator into
* a mask. The reference implementation uses a shift which the
* compiler can argue to result in either 0 of 0xFF..FF. */
static MLK_INLINE uint8_t
/*************************************************
* Name: mlk_ct_cmov_zero
*
* Description: Copy len bytes from x to r if b is zero;
* don't modify x if b is non-zero.
* assumes two's complement representation of negative integers.
* Runs in constant time.
*
* Arguments: uint8_t *r: pointer to output byte array
* const uint8_t *x: pointer to input byte array
* size_t len: Amount of bytes to be copied
* uint8_t b: Condition value.
*
* Specification:
* - Used to securely compute conditional move in
* @[FIPS203, Algorithm 18 (ML-KEM.Decaps_Internal, L9-11]
*
**************************************************/
/* Reference: `cmov()` in the reference implementation @[REF].
* - We move if condition value is `0`, not `1`.
* - We use `mlk_ct_sel_uint8` for constant-time selection. */
static MLK_INLINE void
/*************************************************
* Name: mlk_zeroize
*
* Description: Force-zeroize a buffer.
*
* Arguments: uint8_t *r: pointer to byte array to be zeroed
* size_t len: Amount of bytes to be zeroed
*
* Specification: Used to implement
* @[FIPS203, Section 3.3, Destruction of intermediate values]
*
**************************************************/
/* Reference: Not present in the reference implementation @[REF]. */
static MLK_INLINE void
static MLK_INLINE void
/* !MLK_SYS_WINDOWS && !MLK_HAVE_INLINE_ASM */
/* !MLK_CONFIG_CUSTOM_ZEROIZE */
/* !MLK_VERIFY_H */