Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/integration/liboqs/config_c.h

raw
1 /*
2 * Copyright (c) The mldsa-native project authors
3 * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4 */
5
6 /* References
7 * ==========
8 *
9 * - [FIPS140_3_IG]
10 * Implementation Guidance for FIPS 140-3 and the Cryptographic Module
11 * Validation Program
12 * National Institute of Standards and Technology
13 * https://csrc.nist.gov/projects/cryptographic-module-validation-program/fips-140-3-ig-announcements
14 */
15
16 #ifndef MLD_INTEGRATION_LIBOQS_CONFIG_C_H
17 #define MLD_INTEGRATION_LIBOQS_CONFIG_C_H
18
19 /* Enable valgrind-based assertions in mldsa-native through macro
20 * from libOQS. */
21 #if !defined(__ASSEMBLER__)
22 #include <oqs/common.h>
23 #if defined(OQS_ENABLE_TEST_CONSTANT_TIME)
24 #define MLD_CONFIG_CT_TESTING_ENABLED
25 #endif
26 #endif /* !__ASSEMBLER__ */
27
28 /* Use OQS's FIPS202 via glue headers */
29 #define MLD_CONFIG_FIPS202_CUSTOM_HEADER \
30 "../../integration/liboqs/fips202_glue.h"
31 #define MLD_CONFIG_FIPS202X4_CUSTOM_HEADER \
32 "../../integration/liboqs/fips202x4_glue.h"
33
34 /******************************************************************************
35 * Name: MLD_CONFIG_PARAMETER_SET
36 *
37 * Description: Specifies the parameter set for ML-DSA
38 * - MLD_CONFIG_PARAMETER_SET=44 corresponds to ML-DSA-44
39 * - MLD_CONFIG_PARAMETER_SET=65 corresponds to ML-DSA-65
40 * - MLD_CONFIG_PARAMETER_SET=87 corresponds to ML-DSA-87
41 *
42 * This can also be set using CFLAGS.
43 *
44 *****************************************************************************/
45 #ifndef MLD_CONFIG_PARAMETER_SET
46 #define MLD_CONFIG_PARAMETER_SET \
47 44 /* Change this for different security strengths */
48 #endif
49
50 /******************************************************************************
51 * Name: MLD_CONFIG_NAMESPACE_PREFIX
52 *
53 * Description: The prefix to use to namespace global symbols from mldsa/.
54 * For integration builds, this adds an architecture-specific
55 * suffix to distinguish different builds.
56 *
57 * This can also be set using CFLAGS.
58 *
59 *****************************************************************************/
60 #if !defined(MLD_CONFIG_NAMESPACE_PREFIX)
61 #define MLD_CONFIG_NAMESPACE_PREFIX MLD_DEFAULT_NAMESPACE_PREFIX
62 #endif
63
64 /******************************************************************************
65 * Name: MLD_CONFIG_FILE
66 *
67 * Description: If defined, this is a header that will be included instead
68 * of this default configuration file mldsa/config.h.
69 *
70 * When you need to build mldsa-native in multiple configurations,
71 * using varying MLD_CONFIG_FILE can be more convenient
72 * then configuring everything through CFLAGS.
73 *
74 * To use, MLD_CONFIG_FILE _must_ be defined prior
75 * to the inclusion of any mldsa-native headers. For example,
76 * it can be set by passing `-DMLD_CONFIG_FILE="..."`
77 * on the command line.
78 *
79 *****************************************************************************/
80 /* #define MLD_CONFIG_FILE "config.h" */
81
82 /******************************************************************************
83 * Name: MLD_CONFIG_ARITH_BACKEND_FILE
84 *
85 * Description: The arithmetic backend to use.
86 *
87 * If MLD_CONFIG_USE_NATIVE_BACKEND_ARITH is unset, this option
88 * is ignored.
89 *
90 * If MLD_CONFIG_USE_NATIVE_BACKEND_ARITH is set, this option must
91 * either be undefined or the filename of an arithmetic backend.
92 * If unset, the default backend will be used.
93 *
94 * This can be set using CFLAGS.
95 *
96 *****************************************************************************/
97 /* #if defined(MLD_CONFIG_USE_NATIVE_BACKEND_ARITH) && \
98 !defined(MLD_CONFIG_ARITH_BACKEND_FILE)
99 #define MLD_CONFIG_ARITH_BACKEND_FILE "native/meta.h"
100 #endif
101 */
102
103 /******************************************************************************
104 * Name: MLD_CONFIG_FIPS202_BACKEND_FILE
105 *
106 * Description: The FIPS-202 backend to use.
107 *
108 * If MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202 is set, this option
109 * must either be undefined or the filename of a FIPS202 backend.
110 * If unset, the default backend will be used.
111 *
112 * This can be set using CFLAGS.
113 *
114 *****************************************************************************/
115 /* #if defined(MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202) && \
116 !defined(MLD_CONFIG_FIPS202_BACKEND_FILE)
117 #define MLD_CONFIG_FIPS202_BACKEND_FILE "fips202/native/auto.h"
118 #endif
119 */
120
121 /******************************************************************************
122 * Name: MLD_CONFIG_CUSTOM_ZEROIZE
123 *
124 * Description: In compliance with FIPS 204 Section 3.6.3, mldsa-native zeroizes
125 * intermediate stack buffers before returning from function calls.
126 *
127 * Set this option and define `mld_zeroize` if you want to
128 * use a custom method to zeroize intermediate stack buffers.
129 * The default implementation uses SecureZeroMemory on Windows
130 * and a memset + compiler barrier otherwise. If neither of those
131 * is available on the target platform, compilation will fail,
132 * and you will need to use MLD_CONFIG_CUSTOM_ZEROIZE to provide
133 * a custom implementation of `mld_zeroize()`.
134 *
135 * WARNING:
136 * The explicit stack zeroization conducted by mldsa-native
137 * reduces the likelihood of data leaking on the stack, but
138 * does not eliminate it! The C standard makes no guarantee about
139 * where a compiler allocates structures and whether/where it makes
140 * copies of them. Also, in addition to entire structures, there
141 * may also be potentially exploitable leakage of individual values
142 * on the stack.
143 *
144 * If you need bullet-proof zeroization of the stack, you need to
145 * consider additional measures instead of what this feature
146 * provides. In this case, you can set mld_zeroize to a
147 * no-op.
148 *
149 *****************************************************************************/
150 /* #define MLD_CONFIG_CUSTOM_ZEROIZE
151 #if !defined(__ASSEMBLER__)
152 #include <stdint.h>
153 #include "sys.h"
154 static MLD_INLINE void mld_zeroize(void *ptr, size_t len)
155 {
156 ... your implementation ...
157 }
158 #endif
159 */
160
161 /******************************************************************************
162 * Name: MLD_CONFIG_CUSTOM_RANDOMBYTES
163 *
164 * Description: mldsa-native does not provide a secure randombytes
165 * implementation. Such an implementation has to provided by the
166 * consumer.
167 *
168 * If this option is not set, mlkem-native expects a function
169 * int randombytes(uint8_t *out, size_t outlen).
170 *
171 * Set this option and define `mlk_randombytes` if you want to
172 * use a custom method to sample randombytes with a different name
173 * or signature.
174 *
175 *****************************************************************************/
176 #define MLD_CONFIG_CUSTOM_RANDOMBYTES
177 #if !defined(__ASSEMBLER__)
178 #include <oqs/rand.h>
179 #include <stdint.h>
180 #include "../../mldsa/src/sys.h"
181 static MLD_INLINE int mld_randombytes(uint8_t *ptr, size_t len)
182 {
183 OQS_randombytes(ptr, len);
184 return 0;
185 }
186 #endif /* !__ASSEMBLER__ */
187
188 /******************************************************************************
189 * Name: MLD_CONFIG_KEYGEN_PCT
190 *
191 * Description: Compliance with @[FIPS140_3_IG, p.87] requires a
192 * Pairwise Consistency Test (PCT) to be carried out on a freshly
193 * generated keypair before it can be exported.
194 *
195 * Set this option if such a check should be implemented.
196 * In this case, crypto_sign_keypair_internal and
197 * crypto_sign_keypair will return a non-zero error code if the
198 * PCT failed.
199 *
200 * NOTE: This feature will drastically lower the performance of
201 * key generation.
202 *
203 *****************************************************************************/
204 /* #define MLD_CONFIG_KEYGEN_PCT */
205
206 /******************************************************************************
207 * Name: MLD_CONFIG_KEYGEN_PCT_BREAKAGE_TEST
208 *
209 * Description: If this option is set, the user must provide a runtime
210 * function `static inline int mld_break_pct() { ... }` to
211 * indicate whether the PCT should be made fail.
212 *
213 * This option only has an effect if MLD_CONFIG_KEYGEN_PCT is set.
214 *
215 *****************************************************************************/
216 /* #define MLD_CONFIG_KEYGEN_PCT_BREAKAGE_TEST
217 #if !defined(__ASSEMBLER__)
218 #include "sys.h"
219 static MLD_INLINE int mld_break_pct(void)
220 {
221 ... return 0/1 depending on whether PCT should be broken ...
222 }
223 #endif
224 */
225
226 /******************************************************************************
227 * Name: MLD_CONFIG_NO_ASM
228 *
229 * Description: If this option is set, mldsa-native will be built without
230 * use of native code or inline assembly.
231 *
232 * By default, inline assembly is used to implement value barriers.
233 * Without inline assembly, mldsa-native will use a global volatile
234 * 'opt blocker' instead; see ct.h.
235 *
236 * Inline assembly is also used to implement a secure zeroization
237 * function on non-Windows platforms. If this option is set and
238 * the target platform is not Windows, you MUST set
239 * MLD_CONFIG_CUSTOM_ZEROIZE and provide a custom zeroization
240 * function.
241 *
242 * If this option is set, MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202 and
243 * and MLD_CONFIG_USE_NATIVE_BACKEND_ARITH will be ignored, and no
244 * native backends will be used.
245 *
246 *****************************************************************************/
247 /* #define MLD_CONFIG_NO_ASM */
248
249 /******************************************************************************
250 * Name: MLD_CONFIG_NO_ASM_VALUE_BARRIER
251 *
252 * Description: If this option is set, mldsa-native will be built without
253 * use of native code or inline assembly for value barriers.
254 *
255 * By default, inline assembly (if available) is used to implement
256 * value barriers.
257 * Without inline assembly, mldsa-native will use a global volatile
258 * 'opt blocker' instead; see ct.h.
259 *
260 *****************************************************************************/
261 /* #define MLD_CONFIG_NO_ASM_VALUE_BARRIER */
262
263 /******************************************************************************
264 * Name: MLD_CONFIG_EXTERNAL_API_QUALIFIER
265 *
266 * Description: If set, this option provides an additional function
267 * qualifier to be added to declarations of mldsa-native's
268 * public API.
269 *
270 * The primary use case for this option are single-CU builds
271 * where the public API exposed by mldsa-native is wrapped by
272 * another API in the consuming application. In this case,
273 * even mldsa-native's public API can be marked `static`.
274 *
275 *****************************************************************************/
276 #if !defined(__ASSEMBLER__)
277 #include <oqs/common.h>
278 #define MLD_CONFIG_EXTERNAL_API_QUALIFIER OQS_API
279 #endif
280
281 /******************************************************************************
282 * Name: MLD_CONFIG_SERIAL_FIPS202_ONLY
283 *
284 * Description: Set this to use a FIPS202 implementation with global state
285 * that supports only one active Keccak computation at a time
286 * (e.g. some hardware accelerators).
287 *
288 * If this option is set, ML-DSA will use FIPS202 operations
289 * serially, ensuring that only one SHAKE context is active
290 * at any given time.
291 *
292 * This allows offloading Keccak computations to a hardware
293 * accelerator that holds only a single Keccak state locally,
294 * rather than requiring support for multiple concurrent
295 * Keccak states.
296 *
297 * NOTE: Depending on the target CPU, this may reduce
298 * performance when using software FIPS202 implementations.
299 * Only enable this when you have to.
300 *
301 *****************************************************************************/
302 /* #define MLD_CONFIG_SERIAL_FIPS202_ONLY */
303
304 /************************* Config internals ********************************/
305
306 /* Default namespace
307 *
308 * Don't change this. If you need a different namespace, re-define
309 * MLD_CONFIG_NAMESPACE_PREFIX above instead, and remove the following.
310 *
311 * The default MLDSA namespace for C integration is
312 *
313 * PQCP_MLDSA_NATIVE_MLDSA<LEVEL>_C_
314 *
315 * e.g., PQCP_MLDSA_NATIVE_MLDSA44_C_
316 */
317
318 #if MLD_CONFIG_PARAMETER_SET == 44
319 #define MLD_DEFAULT_NAMESPACE_PREFIX PQCP_MLDSA_NATIVE_MLDSA44_C
320 #elif MLD_CONFIG_PARAMETER_SET == 65
321 #define MLD_DEFAULT_NAMESPACE_PREFIX PQCP_MLDSA_NATIVE_MLDSA65_C
322 #elif MLD_CONFIG_PARAMETER_SET == 87
323 #define MLD_DEFAULT_NAMESPACE_PREFIX PQCP_MLDSA_NATIVE_MLDSA87_C
324 #endif
325
326 #endif /* !MLD_INTEGRATION_LIBOQS_CONFIG_C_H */