-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconfig.h
348 lines (328 loc) · 14.1 KB
/
config.h
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
/*
* Copyright (c) 2024-2025 The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef MLK_CONFIG_H
#define MLK_CONFIG_H
/******************************************************************************
* Name: MLKEM_K
*
* Description: Determines the security level for ML-KEM
* - MLKEM_K=2 corresponds to ML-KEM-512
* - MLKEM_K=3 corresponds to ML-KEM-768
* - MLKEM_K=4 corresponds to ML-KEM-1024
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#ifndef MLKEM_K
#define MLKEM_K 3 /* Change this for different security strengths */
#endif
/******************************************************************************
* Name: MLK_CONFIG_FILE
*
* Description: If defined, this is a header that will be included instead
* of this default configuration file mlkem/config.h.
*
* When you need to build mlkem-native in multiple configurations,
* using varying MLK_CONFIG_FILE can be more convenient
* then configuring everything through CFLAGS.
*
* To use, MLK_CONFIG_FILE _must_ be defined prior
* to the inclusion of any mlkem-native headers. For example,
* it can be set by passing `-DMLK_CONFIG_FILE="..."`
* on the command line.
*
*****************************************************************************/
/* #define MLK_CONFIG_FILE "config.h" */
/******************************************************************************
* Name: MLK_NAMESPACE_PREFIX
*
* Description: The prefix to use to namespace global symbols from mlkem/.
*
* In a multi-level build (that is, if either
* - MLK_MULTILEVEL_BUILD_WITH_SHARED, or
* - MLK_MULTILEVEL_BUILD_NO_SHARED,
* are set, level-dependent symbols will additionally be prefixed
* with the security level.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#if !defined(MLK_NAMESPACE_PREFIX)
#define MLK_NAMESPACE_PREFIX MLK_DEFAULT_NAMESPACE_PREFIX
#endif
/******************************************************************************
* Name: MLK_MULTILEVEL_BUILD_WITH_SHARED
*
* Description: This is for multi-level builds of mlkem-native only. If you
* need only a single security level build of mlkem-native,
* keep this unset.
*
* If this is set, all MLKEM_K-independent code will be included
* in the build, including code needed only for other security
* levels.
*
* Example: mlk_poly_cbd3 is only needed for MLKEM_K == 2. Yet, if
* this option is set for a build with MLKEM_K==3/4, it would
* be included.
*
* To build mlkem-native with support for all security levels,
* build it three times -- once per level -- and set the option
* MLK_MULTILEVEL_BUILD_WITH_SHARED for exactly one of
* them, and MLK_MULTILEVEL_BUILD_NO_SHARED for the
* others.
*
* See examples/multilevel_build for an example.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
/* #define MLK_MULTILEVEL_BUILD_WITH_SHARED */
/******************************************************************************
* Name: MLK_MULTILEVEL_BUILD_NO_SHARED
*
* Description: This is for multi-level builds of mlkem-native only. If you
* need only a single security level build of mlkem-native,
* keep this unset.
*
* If this is set, no MLKEM_K-independent code will be included
* in the build.
*
* To build mlkem-native with support for all security levels,
* build it three times -- once per level -- and set the option
* MLK_MULTILEVEL_BUILD_WITH_SHARED for exactly one of
* them, and MLK_MULTILEVEL_BUILD_NO_SHARED for the
* others.
*
* See examples/multilevel_build for an example.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
/* #define MLK_MULTILEVEL_BUILD_NO_SHARED */
/******************************************************************************
* Name: MLK_USE_NATIVE_BACKEND_ARITH
*
* Description: Determines whether an native arithmetic backend should be used.
*
* The arithmetic backend covers performance critical functions
* such as the number-theoretic transform (NTT).
*
* If this option is unset, the C backend will be used.
*
* If this option is set, the arithmetic backend to be use is
* determined by MLK_ARITH_BACKEND: If the latter is
* unset, the default backend for your the target architecture
* will be used. If set, it must be the name of a backend metadata
* file.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#if !defined(MLK_USE_NATIVE_BACKEND_ARITH)
/* #define MLK_USE_NATIVE_BACKEND_ARITH */
#endif
/******************************************************************************
* Name: MLK_ARITH_BACKEND_FILE
*
* Description: The arithmetic backend to use.
*
* If MLK_USE_NATIVE_BACKEND_ARITH is unset, this option
* is ignored.
*
* If MLK_USE_NATIVE_BACKEND_ARITH is set, this option must
* either be undefined or the filename of an arithmetic backend.
* If unset, the default backend will be used.
*
* This can be set using CFLAGS.
*
*****************************************************************************/
#if defined(MLK_USE_NATIVE_BACKEND_ARITH) && !defined(MLK_ARITH_BACKEND_FILE)
#define MLK_ARITH_BACKEND_FILE "native/meta.h"
#endif
/******************************************************************************
* Name: MLK_USE_NATIVE_BACKEND_FIPS202
*
* Description: Determines whether an native FIPS202 backend should be used.
*
* The FIPS202 backend covers 1x/2x/4x-fold Keccak-f1600, which is
* the performance bottleneck of SHA3 and SHAKE.
*
* If this option is unset, the C backend will be used.
*
* If this option is set, the FIPS202 backend to be use is
* determined by MLK_FIPS202_BACKEND: If the latter is
* unset, the default backend for your the target architecture
* will be used. If set, it must be the name of a backend metadata
* file.
*
* This can also be set using CFLAGS.
*
*****************************************************************************/
#if !defined(MLK_USE_NATIVE_BACKEND_FIPS202)
/* #define MLK_USE_NATIVE_BACKEND_FIPS202 */
#endif
/******************************************************************************
* Name: MLK_FIPS202_BACKEND_FILE
*
* Description: The FIPS-202 backend to use.
*
* If MLK_USE_NATIVE_BACKEND_FIPS202 is set, this option must
* either be undefined or the filename of a FIPS202 backend.
* If unset, the default backend will be used.
*
* This can be set using CFLAGS.
*
*****************************************************************************/
#if defined(MLK_USE_NATIVE_BACKEND_FIPS202) && \
!defined(MLK_FIPS202_BACKEND_FILE)
#define MLK_FIPS202_BACKEND_FILE "fips202/native/meta.h"
#endif
/******************************************************************************
* Name: MLK_FIPS202_CUSTOM_HEADER
*
* Description: Custom header to use for FIPS-202
*
* This should only be set if you intend to use a custom
* FIPS-202 implementation, different from the one shipped
* with mlkem-native.
*
* If set, it must be the name of a file serving as the
* replacement for mlkem/fips202/fips202.h, and exposing
* the same API (see FIPS202.md).
*
*****************************************************************************/
/* #define MLK_FIPS202_CUSTOM_HEADER "SOME_FILE.h" */
/******************************************************************************
* Name: MLK_FIPS202X4_CUSTOM_HEADER
*
* Description: Custom header to use for FIPS-202-X4
*
* This should only be set if you intend to use a custom
* FIPS-202 implementation, different from the one shipped
* with mlkem-native.
*
* If set, it must be the name of a file serving as the
* replacement for mlkem/fips202/fips202x4.h, and exposing
* the same API (see FIPS202.md).
*
*****************************************************************************/
/* #define MLK_FIPS202X4_CUSTOM_HEADER "SOME_FILE.h" */
/******************************************************************************
* Name: MLK_USE_ZEROIZE_NATIVE
*
* Description: In compliance with FIPS 203 Section 3.3, mlkem-native zeroizes
* intermediate stack buffers before returning from function calls.
*
* Set this option and define `mlk_zeroize_native` if you want to
* use a custom method to zeroize intermediate stack buffers.
* The default implementation uses SecureZeroMemory on Windows
* and a memset + compiler barrier otherwise. If neither of those
* is available on the target platform, compilation will fail,
* and you will need to use MLK_USE_ZEROIZE_NATIVE to provide
* a custom implementation of `mlk_zeroize_native()`.
*
* WARNING:
* The explicit stack zeroization conducted by mlkem-native
* reduces the likelihood of data leaking on the stack, but
* does not eliminate it! The C standard makes no guarantee about
* where a compiler allocates structures and whether/where it makes
* copies of them. Also, in addition to entire structures, there
* may also be potentially exploitable leakage of individual values
* on the stack.
*
* If you need bullet-proof zeroization of the stack, you need to
* consider additional measures instead of of what this feature
* provides. In this case, you can set mlk_zeroize_native to a
* no-op.
*
*****************************************************************************/
/* #define MLK_USE_ZEROIZE_NATIVE
#if !defined(__ASSEMBLER__)
#include <stdint.h>
#include "sys.h"
static MLK_INLINE void mlk_zeroize_native(void *ptr, size_t len)
{
... your implementation ...
}
#endif
*/
/******************************************************************************
* Name: MLK_NO_ASM
*
* Description: If this option is set, mlkem-native will be built without
* use of native code or inline assembly.
*
* By default, inline assembly is used to implement value barriers.
* Without inline assembly, mlkem-native will use a global volatile
* 'opt blocker' instead; see verify.h.
*
* Inline assembly is also used to implement a secure zeroization
* function on non-Windows platforms. If this option is set and
* the target platform is not Windows, you MUST set
* MLK_USE_ZEROIZE_NATIVE and provide a custom zeroization
* function.
*
* If this option is set, MLK_USE_NATIVE_BACKEND_FIPS202 and
* and MLK_USE_NATIVE_BACKEND_ARITH will be ignored, and no native
* backends will be used.
*
*****************************************************************************/
/* #define MLK_NO_ASM */
/******************************************************************************
* Name: MLK_KEYGEN_PCT
*
* Description: Compliance with [FIPS 140-3
*IG](https://csrc.nist.gov/csrc/media/Projects/cryptographic-module-validation-program/documents/fips%20140-3/FIPS%20140-3%20IG.pdf)
* requires a Pairwise Consistency Test (PCT) to be carried out
* on a freshly generated keypair before it can be exported.
*
* Set this option if such a check should be implemented.
* In this case, crypto_kem_keypair_derand and crypto_kem_keypair
* will return a non-zero error code if the PCT failed.
*
* NOTE: This feature will drastically lower the performance of
* key generation.
*
*****************************************************************************/
/* #define MLK_KEYGEN_PCT */
/******************************************************************************
* Name: MLK_KEYGEN_PCT_BREAKAGE_TEST
*
* Description: If this option is set, the user must provide a runtime
* function `static inline int mlk_break_pct() { ... }` to
* indicate whether the PCT should be made fail.
*
* This option only has an effect if MLK_KEYGEN_PCT is set.
*
*****************************************************************************/
/* #define MLK_KEYGEN_PCT_BREAKAGE_TEST
#if !defined(__ASSEMBLER__)
#include "sys.h"
static MLK_INLINE int mlk_break_pct(void)
{
... return 0/1 depending on whether PCT should be broken ...
}
#endif
*/
/************************* Config internals ********************************/
/* Default namespace
*
* Don't change this. If you need a different namespace, re-define
* MLK_NAMESPACE_PREFIX above instead, and remove the following.
*
* The default MLKEM namespace is
*
* PQCP_MLKEM_NATIVE_MLKEM<LEVEL>_
*
* e.g., PQCP_MLKEM_NATIVE_MLKEM512_
*/
#if MLKEM_K == 2
#define MLK_DEFAULT_NAMESPACE_PREFIX PQCP_MLKEM_NATIVE_MLKEM512
#elif MLKEM_K == 3
#define MLK_DEFAULT_NAMESPACE_PREFIX PQCP_MLKEM_NATIVE_MLKEM768
#elif MLKEM_K == 4
#define MLK_DEFAULT_NAMESPACE_PREFIX PQCP_MLKEM_NATIVE_MLKEM1024
#endif
#endif /* MLK_CONFIG_H */