Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/examples/basic/mldsa_native/src/common.h

raw
1 /*
2 * Copyright (c) The mldsa-native project authors
3 * Copyright (c) The mlkem-native project authors
4 * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
5 */
6 #ifndef MLD_COMMON_H
7 #define MLD_COMMON_H
8
9 #define MLD_BUILD_INTERNAL
10
11 #if defined(MLD_CONFIG_FILE)
12 #include MLD_CONFIG_FILE
13 #else
14 #include "mldsa_native_config.h"
15 #endif
16
17 #include "params.h"
18 #include "sys.h"
19
20 /* Internal and public API have external linkage by default, but
21 * this can be overwritten by the user, e.g. for single-CU builds. */
22 #if !defined(MLD_CONFIG_INTERNAL_API_QUALIFIER)
23 #define MLD_INTERNAL_API
24 #else
25 #define MLD_INTERNAL_API MLD_CONFIG_INTERNAL_API_QUALIFIER
26 #endif
27
28 #if !defined(MLD_CONFIG_EXTERNAL_API_QUALIFIER)
29 #define MLD_EXTERNAL_API
30 #else
31 #define MLD_EXTERNAL_API MLD_CONFIG_EXTERNAL_API_QUALIFIER
32 #endif
33
34 #if defined(MLD_CONFIG_MULTILEVEL_NO_SHARED) || \
35 defined(MLD_CONFIG_MULTILEVEL_WITH_SHARED)
36 #define MLD_MULTILEVEL_BUILD
37 #endif
38
39 #define MLD_CONCAT_(x1, x2) x1##x2
40 #define MLD_CONCAT(x1, x2) MLD_CONCAT_(x1, x2)
41
42 #if defined(MLD_MULTILEVEL_BUILD)
43 #define MLD_ADD_PARAM_SET(s) MLD_CONCAT(s, MLD_CONFIG_PARAMETER_SET)
44 #else
45 #define MLD_ADD_PARAM_SET(s) s
46 #endif
47
48 #define MLD_NAMESPACE_PREFIX MLD_CONCAT(MLD_CONFIG_NAMESPACE_PREFIX, _)
49 #define MLD_NAMESPACE_PREFIX_KL \
50 MLD_CONCAT(MLD_ADD_PARAM_SET(MLD_CONFIG_NAMESPACE_PREFIX), _)
51
52 /* Functions are prefixed by MLD_CONFIG_NAMESPACE_PREFIX.
53 *
54 * If multiple parameter sets are used, functions depending on the parameter
55 * set are additionally prefixed with 44/65/87. See mldsa_native_config.h.
56 *
57 * Example: If MLD_CONFIG_NAMESPACE_PREFIX is PQCP_MLDSA_NATIVE, then
58 * MLD_NAMESPACE_KL(keypair) becomes PQCP_MLDSA_NATIVE44_keypair/
59 * PQCP_MLDSA_NATIVE65_keypair/PQCP_MLDSA_NATIVE87_keypair.
60 */
61 #define MLD_NAMESPACE(s) MLD_CONCAT(MLD_NAMESPACE_PREFIX, s)
62 #define MLD_NAMESPACE_KL(s) MLD_CONCAT(MLD_NAMESPACE_PREFIX_KL, s)
63
64 /* On Apple platforms, we need to emit leading underscore
65 * in front of assembly symbols. We thus introducee a separate
66 * namespace wrapper for ASM symbols. */
67 #if !defined(__APPLE__)
68 #define MLD_ASM_NAMESPACE(sym) MLD_NAMESPACE(sym)
69 #else
70 #define MLD_ASM_NAMESPACE(sym) MLD_CONCAT(_, MLD_NAMESPACE(sym))
71 #endif
72
73 /*
74 * On X86_64 if control-flow protections (CET) are enabled (through
75 * -fcf-protection=), we add an endbr64 instruction at every global function
76 * label. See sys.h for more details
77 */
78 #if defined(MLD_SYS_X86_64)
79 #define MLD_ASM_FN_SYMBOL(sym) MLD_ASM_NAMESPACE(sym) : MLD_CET_ENDBR
80 #else
81 #define MLD_ASM_FN_SYMBOL(sym) MLD_ASM_NAMESPACE(sym) :
82 #endif
83
84 /* We aim to simplify the user's life by supporting builds where
85 * all source files are included, even those that are not needed.
86 * Those files are appropriately guarded and will be empty when unneeded.
87 * The following is to avoid compilers complaining about this. */
88 #define MLD_EMPTY_CU(s) extern int MLD_NAMESPACE_KL(empty_cu_##s);
89
90 /* MLD_CONFIG_NO_ASM takes precedence over MLD_USE_NATIVE_XXX */
91 #if defined(MLD_CONFIG_NO_ASM)
92 #undef MLD_CONFIG_USE_NATIVE_BACKEND_ARITH
93 #undef MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202
94 #endif
95
96 #if defined(MLD_CONFIG_USE_NATIVE_BACKEND_ARITH) && \
97 !defined(MLD_CONFIG_ARITH_BACKEND_FILE)
98 #error Bad configuration: MLD_CONFIG_USE_NATIVE_BACKEND_ARITH is set, but MLD_CONFIG_ARITH_BACKEND_FILE is not.
99 #endif
100
101 #if defined(MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202) && \
102 !defined(MLD_CONFIG_FIPS202_BACKEND_FILE)
103 #error Bad configuration: MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202 is set, but MLD_CONFIG_FIPS202_BACKEND_FILE is not.
104 #endif
105
106 #if defined(MLD_CONFIG_NO_RANDOMIZED_API) && defined(MLD_CONFIG_KEYGEN_PCT)
107 #error Bad configuration: MLD_CONFIG_NO_RANDOMIZED_API is incompatible with MLD_CONFIG_KEYGEN_PCT as the current PCT implementation requires crypto_sign_signature()
108 #endif
109
110 #if defined(MLD_CONFIG_USE_NATIVE_BACKEND_ARITH)
111 #include MLD_CONFIG_ARITH_BACKEND_FILE
112 /* Include to enforce consistency of API and implementation,
113 * and conduct sanity checks on the backend.
114 *
115 * Keep this _after_ the inclusion of the backend; otherwise,
116 * the sanity checks won't have an effect. */
117 #if defined(MLD_CHECK_APIS) && !defined(__ASSEMBLER__)
118 #include "native/api.h"
119 #endif
120 #endif /* MLD_CONFIG_USE_NATIVE_BACKEND_ARITH */
121
122 #if defined(MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202)
123 #include MLD_CONFIG_FIPS202_BACKEND_FILE
124 /* Include to enforce consistency of API and implementation,
125 * and conduct sanity checks on the backend.
126 *
127 * Keep this _after_ the inclusion of the backend; otherwise,
128 * the sanity checks won't have an effect. */
129 #if defined(MLD_CHECK_APIS) && !defined(__ASSEMBLER__)
130 #include "fips202/native/api.h"
131 #endif
132 #endif /* MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202 */
133
134 #if !defined(MLD_CONFIG_FIPS202_CUSTOM_HEADER)
135 #define MLD_FIPS202_HEADER_FILE "fips202/fips202.h"
136 #else
137 #define MLD_FIPS202_HEADER_FILE MLD_CONFIG_FIPS202_CUSTOM_HEADER
138 #endif
139
140 #if !defined(MLD_CONFIG_FIPS202X4_CUSTOM_HEADER)
141 #define MLD_FIPS202X4_HEADER_FILE "fips202/fips202x4.h"
142 #else
143 #define MLD_FIPS202X4_HEADER_FILE MLD_CONFIG_FIPS202X4_CUSTOM_HEADER
144 #endif
145
146 /* Standard library function replacements */
147 #if !defined(__ASSEMBLER__)
148 #if !defined(MLD_CONFIG_CUSTOM_MEMCPY)
149 #include <string.h>
150 #define mld_memcpy memcpy
151 #endif
152
153 #if !defined(MLD_CONFIG_CUSTOM_MEMSET)
154 #include <string.h>
155 #define mld_memset memset
156 #endif
157
158 /* Allocation macros for large local structures
159 *
160 * MLD_ALLOC(v, T, N) declares T *v and attempts to point it to an T[N]
161 * MLD_FREE(v, T, N) zeroizes and frees the allocation
162 *
163 * Default implementation uses stack allocation.
164 * Can be overridden by setting the config option MLD_CONFIG_CUSTOM_ALLOC_FREE
165 * and defining MLD_CUSTOM_ALLOC and MLD_CUSTOM_FREE.
166 */
167 #if defined(MLD_CONFIG_CUSTOM_ALLOC_FREE) != \
168 (defined(MLD_CUSTOM_ALLOC) && defined(MLD_CUSTOM_FREE))
169 #error Bad configuration: MLD_CONFIG_CUSTOM_ALLOC_FREE must be set together with MLD_CUSTOM_ALLOC and MLD_CUSTOM_FREE
170 #endif
171
172 /*
173 * If the integration wants to provide a context parameter for use in
174 * platform-specific hooks, then it should define this parameter.
175 *
176 * The MLD_CONTEXT_PARAMETERS_n macros are intended to be used with macros
177 * defining the function names and expand to either pass or discard the context
178 * argument as required by the current build. If there is no context parameter
179 * requested then these are removed from the prototypes and from all calls.
180 */
181 #ifdef MLD_CONFIG_CONTEXT_PARAMETER
182 #define MLD_CONTEXT_PARAMETERS_0(context) (context)
183 #define MLD_CONTEXT_PARAMETERS_1(arg0, context) (arg0, context)
184 #define MLD_CONTEXT_PARAMETERS_2(arg0, arg1, context) (arg0, arg1, context)
185 #define MLD_CONTEXT_PARAMETERS_3(arg0, arg1, arg2, context) \
186 (arg0, arg1, arg2, context)
187 #define MLD_CONTEXT_PARAMETERS_4(arg0, arg1, arg2, arg3, context) \
188 (arg0, arg1, arg2, arg3, context)
189 #define MLD_CONTEXT_PARAMETERS_5(arg0, arg1, arg2, arg3, arg4, context) \
190 (arg0, arg1, arg2, arg3, arg4, context)
191 #define MLD_CONTEXT_PARAMETERS_6(arg0, arg1, arg2, arg3, arg4, arg5, context) \
192 (arg0, arg1, arg2, arg3, arg4, arg5, context)
193 #define MLD_CONTEXT_PARAMETERS_7(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
194 context) \
195 (arg0, arg1, arg2, arg3, arg4, arg5, arg6, context)
196 #define MLD_CONTEXT_PARAMETERS_8(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
197 arg7, context) \
198 (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, context)
199 #define MLD_CONTEXT_PARAMETERS_9(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
200 arg7, arg8, context) \
201 (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, context)
202 #else /* MLD_CONFIG_CONTEXT_PARAMETER */
203 #define MLD_CONTEXT_PARAMETERS_0(context) ()
204 #define MLD_CONTEXT_PARAMETERS_1(arg0, context) (arg0)
205 #define MLD_CONTEXT_PARAMETERS_2(arg0, arg1, context) (arg0, arg1)
206 #define MLD_CONTEXT_PARAMETERS_3(arg0, arg1, arg2, context) (arg0, arg1, arg2)
207 #define MLD_CONTEXT_PARAMETERS_4(arg0, arg1, arg2, arg3, context) \
208 (arg0, arg1, arg2, arg3)
209 #define MLD_CONTEXT_PARAMETERS_5(arg0, arg1, arg2, arg3, arg4, context) \
210 (arg0, arg1, arg2, arg3, arg4)
211 #define MLD_CONTEXT_PARAMETERS_6(arg0, arg1, arg2, arg3, arg4, arg5, context) \
212 (arg0, arg1, arg2, arg3, arg4, arg5)
213 #define MLD_CONTEXT_PARAMETERS_7(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
214 context) \
215 (arg0, arg1, arg2, arg3, arg4, arg5, arg6)
216 #define MLD_CONTEXT_PARAMETERS_8(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
217 arg7, context) \
218 (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
219 #define MLD_CONTEXT_PARAMETERS_9(arg0, arg1, arg2, arg3, arg4, arg5, arg6, \
220 arg7, arg8, context) \
221 (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
222 #endif /* !MLD_CONFIG_CONTEXT_PARAMETER */
223
224 #if defined(MLD_CONFIG_CONTEXT_PARAMETER_TYPE) != \
225 defined(MLD_CONFIG_CONTEXT_PARAMETER)
226 #error MLD_CONFIG_CONTEXT_PARAMETER_TYPE must be defined if and only if MLD_CONFIG_CONTEXT_PARAMETER is defined
227 #endif
228
229 #if !defined(MLD_CONFIG_CUSTOM_ALLOC_FREE)
230 /* Default: stack allocation */
231
232 #define MLD_ALLOC(v, T, N, context) \
233 MLD_ALIGN T mld_alloc_##v[N]; \
234 T *v = mld_alloc_##v
235
236 /* TODO: This leads to a circular dependency between common and ct.h
237 * It just works out before we're at the end of the file, but it's still
238 * prone to issues in the future. */
239 #include "ct.h"
240 #define MLD_FREE(v, T, N, context) \
241 do \
242 { \
243 mld_zeroize(mld_alloc_##v, sizeof(mld_alloc_##v)); \
244 (v) = NULL; \
245 } while (0)
246
247 #else /* !MLD_CONFIG_CUSTOM_ALLOC_FREE */
248
249 /* Custom allocation */
250
251 /*
252 * The indirection here is necessary to use MLD_CONTEXT_PARAMETERS_3 here.
253 */
254 #define MLD_APPLY(f, args) f args
255
256 #define MLD_ALLOC(v, T, N, context) \
257 MLD_APPLY(MLD_CUSTOM_ALLOC, MLD_CONTEXT_PARAMETERS_3(v, T, N, context))
258
259 #define MLD_FREE(v, T, N, context) \
260 do \
261 { \
262 if (v != NULL) \
263 { \
264 mld_zeroize(v, sizeof(T) * (N)); \
265 MLD_APPLY(MLD_CUSTOM_FREE, MLD_CONTEXT_PARAMETERS_3(v, T, N, context)); \
266 v = NULL; \
267 } \
268 } while (0)
269
270 #endif /* MLD_CONFIG_CUSTOM_ALLOC_FREE */
271
272 /*
273 * We are facing severe CBMC performance issues when using unions.
274 * As a temporary workaround, we use unions only when MLD_CONFIG_REDUCE_RAM is
275 * set.
276 * TODO: Remove the workaround once
277 * https://github.com/diffblue/cbmc/issues/8813
278 * is resolved
279 */
280 #if defined(MLD_CONFIG_REDUCE_RAM)
281 #define MLD_UNION_OR_STRUCT union
282 #else
283 #define MLD_UNION_OR_STRUCT struct
284 #endif
285
286 /****************************** Error codes ***********************************/
287
288 /* Generic failure condition */
289 #define MLD_ERR_FAIL -1
290 /* An allocation failed. This can only happen if MLD_CONFIG_CUSTOM_ALLOC_FREE
291 * is defined and the provided MLD_CUSTOM_ALLOC can fail. */
292 #define MLD_ERR_OUT_OF_MEMORY -2
293 /* An rng failure occured. Might be due to insufficient entropy or
294 * system misconfiguration. */
295 #define MLD_ERR_RNG_FAIL -3
296
297
298 #endif /* !__ASSEMBLER__ */
299
300 #endif /* !MLD_COMMON_H */