|
5 | 5 | #ifndef MLK_COMMON_H |
6 | 6 | #define MLK_COMMON_H |
7 | 7 |
|
| 8 | +#ifndef __ASSEMBLER__ |
| 9 | +#include <stdint.h> |
| 10 | +#endif |
| 11 | + |
| 12 | +#define MLK_BUILD_INTERNAL |
| 13 | + |
8 | 14 | #if defined(MLK_CONFIG_FILE) |
9 | 15 | #include MLK_CONFIG_FILE |
10 | 16 | #else |
11 | | -#include "config.h" |
| 17 | +#include "mlkem_native_config.h" |
12 | 18 | #endif |
13 | 19 |
|
14 | 20 | #include "params.h" |
|
28 | 34 | #define MLK_EXTERNAL_API MLK_CONFIG_EXTERNAL_API_QUALIFIER |
29 | 35 | #endif |
30 | 36 |
|
31 | | -#if defined(MLK_CONFIG_MULTILEVEL_NO_SHARED) || \ |
32 | | - defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED) |
33 | | -#define MLK_MULTILEVEL_BUILD |
34 | | -#endif |
35 | | - |
36 | 37 | #define MLK_CONCAT_(x1, x2) x1##x2 |
37 | 38 | #define MLK_CONCAT(x1, x2) MLK_CONCAT_(x1, x2) |
38 | 39 |
|
39 | | -#if defined(MLK_MULTILEVEL_BUILD) |
| 40 | +#if (defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED) || \ |
| 41 | + defined(MLK_CONFIG_MULTILEVEL_NO_SHARED)) |
40 | 42 | #define MLK_ADD_PARAM_SET(s) MLK_CONCAT(s, MLK_CONFIG_PARAMETER_SET) |
41 | 43 | #else |
42 | 44 | #define MLK_ADD_PARAM_SET(s) s |
|
49 | 51 | /* Functions are prefixed by MLK_CONFIG_NAMESPACE_PREFIX. |
50 | 52 | * |
51 | 53 | * If multiple parameter sets are used, functions depending on the parameter |
52 | | - * set are additionally prefixed with 512/768/1024. See config.h. |
| 54 | + * set are additionally prefixed with 512/768/1024. See mlkem_native_config.h. |
53 | 55 | * |
54 | 56 | * Example: If MLK_CONFIG_NAMESPACE_PREFIX is mlkem, then |
55 | 57 | * MLK_NAMESPACE_K(enc) becomes mlkem512_enc/mlkem768_enc/mlkem1024_enc. |
|
73 | 75 | */ |
74 | 76 | #if defined(MLK_SYS_X86_64) |
75 | 77 | #define MLK_ASM_FN_SYMBOL(sym) MLK_ASM_NAMESPACE(sym) : MLK_CET_ENDBR |
76 | | -#else |
| 78 | +#elif defined(MLK_SYS_ARMV81M_MVE) |
| 79 | +/* clang-format off */ |
| 80 | +#define MLK_ASM_FN_SYMBOL(sym) \ |
| 81 | + .type MLK_ASM_NAMESPACE(sym), %function; \ |
| 82 | + MLK_ASM_NAMESPACE(sym) : |
| 83 | +/* clang-format on */ |
| 84 | +#else /* !MLK_SYS_X86_64 && MLK_SYS_ARMV81M_MVE */ |
77 | 85 | #define MLK_ASM_FN_SYMBOL(sym) MLK_ASM_NAMESPACE(sym) : |
| 86 | +#endif /* !MLK_SYS_X86_64 && !MLK_SYS_ARMV81M_MVE */ |
| 87 | + |
| 88 | +/* |
| 89 | + * Output the size of an assembly function. |
| 90 | + */ |
| 91 | +#if defined(__ELF__) |
| 92 | +#define MLK_ASM_FN_SIZE(sym) \ |
| 93 | + .size MLK_ASM_NAMESPACE(sym), .- MLK_ASM_NAMESPACE(sym) |
| 94 | +#else |
| 95 | +#define MLK_ASM_FN_SIZE(sym) |
78 | 96 | #endif |
79 | 97 |
|
80 | 98 | /* We aim to simplify the user's life by supporting builds where |
|
99 | 117 | #error Bad configuration: MLK_CONFIG_USE_NATIVE_BACKEND_FIPS202 is set, but MLK_CONFIG_FIPS202_BACKEND_FILE is not. |
100 | 118 | #endif |
101 | 119 |
|
| 120 | +#if defined(MLK_CONFIG_NO_RANDOMIZED_API) && defined(MLK_CONFIG_KEYGEN_PCT) |
| 121 | +#error Bad configuration: MLK_CONFIG_NO_RANDOMIZED_API is incompatible with MLK_CONFIG_KEYGEN_PCT as the current PCT implementation requires crypto_kem_enc() |
| 122 | +#endif |
| 123 | + |
102 | 124 | #if defined(MLK_CONFIG_USE_NATIVE_BACKEND_ARITH) |
103 | 125 | #include MLK_CONFIG_ARITH_BACKEND_FILE |
104 | 126 | /* Include to enforce consistency of API and implementation, |
|
146 | 168 | #include <string.h> |
147 | 169 | #define mlk_memset memset |
148 | 170 | #endif |
149 | | -#endif /* !__ASSEMBLER__ */ |
150 | 171 |
|
151 | | -/* Just in case we want to include mlkem_native.h, set the configuration |
152 | | - * for that header in accordance with the configuration used here. */ |
153 | | - |
154 | | -/* Double-check that this is not conflicting with pre-existing definitions. */ |
155 | | -#if defined(MLK_CONFIG_API_PARAMETER_SET) || \ |
156 | | - defined(MLK_CONFIG_API_NAMESPACE_PREFIX) || \ |
157 | | - defined(MLK_CONFIG_API_NO_SUPERCOP) || \ |
158 | | - defined(MLK_CONFIG_API_CONSTANTS_ONLY) |
159 | | -#error Pre-existing MLK_CONFIG_API_XXX configuration is neither useful nor allowed during an mlkem-native build |
160 | | -#endif /* MLK_CONFIG_API_PARAMETER_SET || MLK_CONFIG_API_NAMESPACE_PREFIX || \ |
161 | | - MLK_CONFIG_API_NO_SUPERCOP || MLK_CONFIG_API_CONSTANTS_ONLY */ |
162 | | - |
163 | | -#define MLK_CONFIG_API_PARAMETER_SET MLK_CONFIG_PARAMETER_SET |
164 | | -#define MLK_CONFIG_API_NAMESPACE_PREFIX \ |
165 | | - MLK_ADD_PARAM_SET(MLK_CONFIG_NAMESPACE_PREFIX) |
| 172 | + |
| 173 | +/* Allocation macros for large local structures |
| 174 | + * |
| 175 | + * MLK_ALLOC(v, T, N) declares T *v and attempts to point it to an T[N] |
| 176 | + * MLK_FREE(v, T, N) zeroizes and frees the allocation |
| 177 | + * |
| 178 | + * Default implementation uses stack allocation. |
| 179 | + * Can be overridden by setting the config option MLK_CONFIG_CUSTOM_ALLOC_FREE |
| 180 | + * and defining MLK_CUSTOM_ALLOC and MLK_CUSTOM_FREE. |
| 181 | + */ |
| 182 | +#if defined(MLK_CONFIG_CUSTOM_ALLOC_FREE) != \ |
| 183 | + (defined(MLK_CUSTOM_ALLOC) && defined(MLK_CUSTOM_FREE)) |
| 184 | +#error Bad configuration: MLK_CONFIG_CUSTOM_ALLOC_FREE must be set together with MLK_CUSTOM_ALLOC and MLK_CUSTOM_FREE |
| 185 | +#endif |
| 186 | + |
| 187 | +/* |
| 188 | + * If the integration wants to provide a context parameter for use in |
| 189 | + * platform-specific hooks, then it should define this parameter. |
| 190 | + * |
| 191 | + * The MLK_CONTEXT_PARAMETERS_n macros are intended to be used with macros |
| 192 | + * defining the function names and expand to either pass or discard the context |
| 193 | + * argument as required by the current build. If there is no context parameter |
| 194 | + * requested then these are removed from the prototypes and from all calls. |
| 195 | + */ |
| 196 | +#ifdef MLK_CONFIG_CONTEXT_PARAMETER |
| 197 | +#define MLK_CONTEXT_PARAMETERS_0(context) (context) |
| 198 | +#define MLK_CONTEXT_PARAMETERS_1(arg0, context) (arg0, context) |
| 199 | +#define MLK_CONTEXT_PARAMETERS_2(arg0, arg1, context) (arg0, arg1, context) |
| 200 | +#define MLK_CONTEXT_PARAMETERS_3(arg0, arg1, arg2, context) \ |
| 201 | + (arg0, arg1, arg2, context) |
| 202 | +#define MLK_CONTEXT_PARAMETERS_4(arg0, arg1, arg2, arg3, context) \ |
| 203 | + (arg0, arg1, arg2, arg3, context) |
| 204 | +#else /* MLK_CONFIG_CONTEXT_PARAMETER */ |
| 205 | +#define MLK_CONTEXT_PARAMETERS_0(context) () |
| 206 | +#define MLK_CONTEXT_PARAMETERS_1(arg0, context) (arg0) |
| 207 | +#define MLK_CONTEXT_PARAMETERS_2(arg0, arg1, context) (arg0, arg1) |
| 208 | +#define MLK_CONTEXT_PARAMETERS_3(arg0, arg1, arg2, context) (arg0, arg1, arg2) |
| 209 | +#define MLK_CONTEXT_PARAMETERS_4(arg0, arg1, arg2, arg3, context) \ |
| 210 | + (arg0, arg1, arg2, arg3) |
| 211 | +#endif /* !MLK_CONFIG_CONTEXT_PARAMETER */ |
| 212 | + |
| 213 | +#if defined(MLK_CONFIG_CONTEXT_PARAMETER_TYPE) != \ |
| 214 | + defined(MLK_CONFIG_CONTEXT_PARAMETER) |
| 215 | +#error MLK_CONFIG_CONTEXT_PARAMETER_TYPE must be defined if and only if MLK_CONFIG_CONTEXT_PARAMETER is defined |
| 216 | +#endif |
| 217 | + |
| 218 | +#if !defined(MLK_CONFIG_CUSTOM_ALLOC_FREE) |
| 219 | +/* Default: stack allocation */ |
| 220 | + |
| 221 | +#define MLK_ALLOC(v, T, N, context) \ |
| 222 | + MLK_ALIGN T mlk_alloc_##v[N]; \ |
| 223 | + T *v = mlk_alloc_##v |
| 224 | + |
| 225 | +/* TODO: This leads to a circular dependency between common and verify.h |
| 226 | + * It just works out before we're at the end of the file, but it's still |
| 227 | + * prone to issues in the future. */ |
| 228 | +#include "verify.h" |
| 229 | +#define MLK_FREE(v, T, N, context) \ |
| 230 | + do \ |
| 231 | + { \ |
| 232 | + mlk_zeroize(mlk_alloc_##v, sizeof(mlk_alloc_##v)); \ |
| 233 | + (v) = NULL; \ |
| 234 | + } while (0) |
| 235 | + |
| 236 | +#else /* !MLK_CONFIG_CUSTOM_ALLOC_FREE */ |
| 237 | + |
| 238 | +/* Custom allocation */ |
| 239 | + |
| 240 | +/* |
| 241 | + * The indirection here is necessary to use MLK_CONTEXT_PARAMETERS_3 here. |
| 242 | + */ |
| 243 | +#define MLK_APPLY(f, args) f args |
| 244 | + |
| 245 | +#define MLK_ALLOC(v, T, N, context) \ |
| 246 | + MLK_APPLY(MLK_CUSTOM_ALLOC, MLK_CONTEXT_PARAMETERS_3(v, T, N, context)) |
| 247 | + |
| 248 | +#define MLK_FREE(v, T, N, context) \ |
| 249 | + do \ |
| 250 | + { \ |
| 251 | + if (v != NULL) \ |
| 252 | + { \ |
| 253 | + mlk_zeroize(v, sizeof(T) * (N)); \ |
| 254 | + MLK_APPLY(MLK_CUSTOM_FREE, MLK_CONTEXT_PARAMETERS_3(v, T, N, context)); \ |
| 255 | + v = NULL; \ |
| 256 | + } \ |
| 257 | + } while (0) |
| 258 | + |
| 259 | +#endif /* MLK_CONFIG_CUSTOM_ALLOC_FREE */ |
| 260 | + |
| 261 | +/****************************** Error codes ***********************************/ |
| 262 | + |
| 263 | +/* Generic failure condition */ |
| 264 | +#define MLK_ERR_FAIL -1 |
| 265 | +/* An allocation failed. This can only happen if MLK_CONFIG_CUSTOM_ALLOC_FREE |
| 266 | + * is defined and the provided MLK_CUSTOM_ALLOC can fail. */ |
| 267 | +#define MLK_ERR_OUT_OF_MEMORY -2 |
| 268 | +/* An rng failure occured. Might be due to insufficient entropy or |
| 269 | + * system misconfiguration. */ |
| 270 | +#define MLK_ERR_RNG_FAIL -3 |
| 271 | + |
| 272 | +#endif /* !__ASSEMBLER__ */ |
166 | 273 |
|
167 | 274 | #endif /* !MLK_COMMON_H */ |
0 commit comments