Skip to content

Commit 0a7699e

Browse files
authored
fipsmodule/ml-kem: Import mlkem-native v1.1.0 (#3090)
This PR updates mlkem-native to mlkem-native-v1.1.0. See the [Release Notes](https://github.com/pq-code-package/mlkem-native/releases/tag/v1.1.0). At the core, it just reruns importer.sh, but the following changes to the importer itself were required: - importer.sh had previously sed-modified the AVX2 backend header to disable native AVX2 (de)compression routines based on intrinsics. For mlkem-native-v1.1, these routines have been rewritten and proved correct in assembly, and are therefore included in the import. AWS-LC can now consume the AVX2 backend header as-is. - importer.sh simplifies the BCM file to drop RV64 related files. mlkem-native now includes a RV64 backend, but it is not relevant for AWS-LC and therefore excluded from the import. This only affects the BCM file, not the file importer itself -- the latter already selects AArch64 and x86_64 native backends specifically. - The mlkem-native configuration now includes a option MLK_CONFIG_MULTILEVEL_BUILD indicating a multi-level build. This option only informs the namespacing in the mlkem_native.h header and is therefore irrelevant for AWS-LC, but we add it to AWS-LC's custom config nontheless for future robustness. - mlkem-native's `mlk_randombytes` RNG backend now uses an `int` return value instead of a `void` return value. Return value `0` indicates success, while a non-zero return value indicates failure. The indirection to `RAND_bytes` is adjusted accordingly. - A size-macro MLK_ASM_FN_SIZE was added to the backend assembly. As with other assembly macros, this is re-mapped to an s2n-bignum assembly directive at the time of import, here S2N_BN_SIZE_DIRECTIVE. The importer previously replaced common.h with the generic _internal_s2n_bignum.h header. However, that header does not define S2N_BN_SIZE_DIRECTIVE. The macro is only available in the architecture-specific headers _internal_s2n_bignum_arm.h and _internal_s2n_bignum_x86_att.h, which are also the headers used by s2n-bignum's own assembly files. The importer is adjusted accordingly. The code under import is subject to the usual assurances of mlkem-native, that is: - Memory-safety and Type-Safety of all imported C code, establishd via CBMC. - Functional correctness, memory-safety and secret-independent timing, established via HOL Light + s2n-bignum. Some notes on the actual source changes, without claims of completeness: - SLOTHY has been re-run on the AArch64 assembly using a model for Neoverse N1, which seems to hit a better average performance on Graviton cores than the previous optimization using the Cortex-A55 SLOTHY model. This explains the large code-churn on the assembly side. - Some of the pre-existing AArch64 assembly has changed to relax alignment constraints: When used with MMU enabled, normal RAM accesses need not be aligned. However, when used in pre-MMU context, all memory accesses are treated as device memory accesses, and are hence subject to alignment checks. The AArch64 assembly is adjusted to not conduct unaligned accesses. - The constant table for the AVX2 backend has been reduced in size, instead embedding relevant constants in the assembly. The remaining table and its offsets is now auto-generated. - Assembly files now have the `.section .note.GNU-stack,"",@progbits` directive, preventing and executable stack. ---- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license. Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
1 parent 9e0a163 commit 0a7699e

76 files changed

Lines changed: 11137 additions & 7836 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crypto/fipsmodule/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@ if((ARCH STREQUAL "x86_64") AND UNIX AND NOT MY_ASSEMBLER_IS_TOO_OLD_FOR_AVX)
381381
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/polyvec_basemul_acc_montgomery_cached_asm_k3.S
382382
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/polyvec_basemul_acc_montgomery_cached_asm_k4.S
383383
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/rej_uniform_asm.S
384+
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/poly_compress_d10.S
385+
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/poly_compress_d11.S
386+
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/poly_compress_d4.S
387+
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/poly_compress_d5.S
388+
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/poly_decompress_d10.S
389+
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/poly_decompress_d11.S
390+
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/poly_decompress_d4.S
391+
${MLKEM_NATIVE_DIR}/mlkem/native/x86_64/src/poly_decompress_d5.S
384392
)
385393

386394
list(APPEND BCM_ASM_SOURCES ${MLKEM_NATIVE_X86_64_ASM_SOURCES})

crypto/fipsmodule/ml_kem/META.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: mlkem-native
22
source: pq-code-package/mlkem-native.git
3-
branch: main
4-
commit: 8c5355ff10f3459ba605b5becc008075caefc786
5-
imported-at: 2025-09-04T19:00:35+0100
3+
branch: v1.1.0
4+
commit: d2cae2be522a67bfae26100fdb520576f1b2ef90
5+
imported-at: 2026-03-16T04:29:39+0000

crypto/fipsmodule/ml_kem/importer.sh

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ cp $TMP/.clang-format $SRC
102102
# The static simplification is not necessary, but improves readability
103103
# by removing directives related to the FIPS-202 backend and the x86_64
104104
# arithmetic backend that are not yet imported.
105-
# Moreover, exclude POLY_COMPRESS/DECOMPRESS functions from the x86 backend.
106105
unifdef -DMLK_CONFIG_FIPS202_CUSTOM_HEADER \
107106
-UMLK_CONFIG_USE_NATIVE_BACKEND_FIPS202 \
107+
-UMLK_SYS_RISCV64 \
108108
$TMP/mlkem/mlkem_native.c \
109109
> $SRC/mlkem_native_bcm.c
110110

@@ -114,17 +114,6 @@ else
114114
SED_I=(-i)
115115
fi
116116

117-
# Exclude POLY_COMPRESS/DECOMPRESS functions from the x86 backend for now.
118-
sed "${SED_I[@]}" '/compress_avx2.c/d' $SRC/mlkem_native_bcm.c
119-
sed "${SED_I[@]}" '/MLK_USE_NATIVE_POLY_COMPRESS_D4/d' $SRC/native/x86_64/meta.h
120-
sed "${SED_I[@]}" '/MLK_USE_NATIVE_POLY_COMPRESS_D5/d' $SRC/native/x86_64/meta.h
121-
sed "${SED_I[@]}" '/MLK_USE_NATIVE_POLY_COMPRESS_D10/d' $SRC/native/x86_64/meta.h
122-
sed "${SED_I[@]}" '/MLK_USE_NATIVE_POLY_COMPRESS_D11/d' $SRC/native/x86_64/meta.h
123-
sed "${SED_I[@]}" '/MLK_USE_NATIVE_POLY_DECOMPRESS_D4/d' $SRC/native/x86_64/meta.h
124-
sed "${SED_I[@]}" '/MLK_USE_NATIVE_POLY_DECOMPRESS_D5/d' $SRC/native/x86_64/meta.h
125-
sed "${SED_I[@]}" '/MLK_USE_NATIVE_POLY_DECOMPRESS_D10/d' $SRC/native/x86_64/meta.h
126-
sed "${SED_I[@]}" '/MLK_USE_NATIVE_POLY_DECOMPRESS_D11/d' $SRC/native/x86_64/meta.h
127-
128117
# Copy mlkem-native header
129118
# This is only needed for access to the various macros defining key sizes.
130119
# The function declarations itself are all visible in ml_kem.c by virtue
@@ -151,12 +140,14 @@ for file in $SRC/native/aarch64/src/*.S $SRC/native/x86_64/src/*.S; do
151140
mv "$tmp_file" "$file"
152141

153142
# Replace common.h include and assembly macros
154-
sed "${SED_I[@]}" 's/#include "\.\.\/\.\.\/\.\.\/common\.h"/#include "_internal_s2n_bignum.h"/' "$file"
143+
s2n_header=$(if [[ "$file" == *"aarch64"* ]]; then echo "_internal_s2n_bignum_arm.h"; else echo "_internal_s2n_bignum_x86_att.h"; fi)
144+
sed "${SED_I[@]}" "s/#include \"\.\.\/\.\.\/\.\.\/common\.h\"/#include \"$s2n_header\"/" "$file"
155145

156146
func_name=$(grep -o '\.global MLK_ASM_NAMESPACE(\([^)]*\))' "$file" | sed 's/\.global MLK_ASM_NAMESPACE(\([^)]*\))/\1/')
157147
if [ -n "$func_name" ]; then
158148
sed "${SED_I[@]}" "s/\.global MLK_ASM_NAMESPACE($func_name)/ S2N_BN_SYM_VISIBILITY_DIRECTIVE(mlkem_$func_name)\n S2N_BN_SYM_PRIVACY_DIRECTIVE(mlkem_$func_name)/" "$file"
159149
sed "${SED_I[@]}" "s/MLK_ASM_FN_SYMBOL($func_name)/S2N_BN_SYMBOL(mlkem_$func_name):/" "$file"
150+
sed "${SED_I[@]}" "s/MLK_ASM_FN_SIZE($func_name)/S2N_BN_SIZE_DIRECTIVE(mlkem_$func_name)/" "$file"
160151
fi
161152
done
162153

crypto/fipsmodule/ml_kem/mlkem/.clang-format

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ IncludeBlocks: Preserve
1717
# as "attributes" so they don't get increasingly indented line after line
1818
BreakBeforeBraces: Allman
1919
InsertBraces: true
20-
WhitespaceSensitiveMacros: ['__contract__', '__loop__' ]
20+
IndentExternBlock: NoIndent
21+
WhitespaceSensitiveMacros: ['__contract__', '__loop__', 'MLK_RV64V_ABS_BOUNDS16' ]
2122
Macros:
2223
# Make this artifically long to avoid function bodies after short contracts
2324
- __contract__(x)={ void a; void b; void c; void d; void e; void f; } void abcdefghijklmnopqrstuvw()

crypto/fipsmodule/ml_kem/mlkem/cbmc.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
/***************************************************
99
* Basic replacements for __CPROVER_XXX contracts
1010
***************************************************/
11-
1211
#ifndef CBMC
1312

1413
#define __contract__(x)
1514
#define __loop__(x)
1615

1716
#else /* !CBMC */
1817

18+
1919
#define __contract__(x) x
2020
#define __loop__(x) x
2121

@@ -49,7 +49,6 @@
4949
*/
5050
#define object_whole(...) __CPROVER_object_whole(__VA_ARGS__)
5151
#define memory_slice(...) __CPROVER_object_upto(__VA_ARGS__)
52-
#define same_object(...) __CPROVER_same_object(__VA_ARGS__)
5352

5453
/*
5554
* Pointer-related predicates
@@ -59,6 +58,17 @@
5958
#define readable(...) __CPROVER_r_ok(__VA_ARGS__)
6059
#define writeable(...) __CPROVER_w_ok(__VA_ARGS__)
6160

61+
/* Maximum supported buffer size
62+
*
63+
* Larger buffers may be supported, but due to internal modeling constraints
64+
* in CBMC, the proofs of memory- and type-safety won't be able to run.
65+
*
66+
* If you find yourself in need for a buffer size larger than this,
67+
* please contact the maintainers, so we can prioritize work to relax
68+
* this somewhat artificial bound.
69+
*/
70+
#define MLK_MAX_BUFFER_SIZE (SIZE_MAX >> 12)
71+
6272
/*
6373
* History variables
6474
* https://diffblue.github.io/cbmc/contracts-history-variables.html
@@ -83,7 +93,7 @@
8393
((qvar_lb) <= (qvar) && (qvar) < (qvar_ub)) ==> (predicate) \
8494
}
8595

86-
#define EXISTS(qvar, qvar_lb, qvar_ub, predicate) \
96+
#define exists(qvar, qvar_lb, qvar_ub, predicate) \
8797
__CPROVER_exists \
8898
{ \
8999
unsigned qvar; \
@@ -118,12 +128,12 @@
118128
{ \
119129
unsigned qvar; \
120130
((qvar_lb) <= (qvar) && (qvar) < (qvar_ub)) ==> \
121-
(((int)(value_lb) <= ((array_var)[(qvar)])) && \
122-
(((array_var)[(qvar)]) < (int)(value_ub))) \
131+
(((int)(value_lb) <= ((array_var)[(qvar)])) && \
132+
(((array_var)[(qvar)]) < (int)(value_ub))) \
123133
}
124134

125-
#define array_bound(array_var, qvar_lb, qvar_ub, value_lb, value_ub) \
126-
array_bound_core(CBMC_CONCAT(_cbmc_idx, __LINE__), (qvar_lb), \
135+
#define array_bound(array_var, qvar_lb, qvar_ub, value_lb, value_ub) \
136+
array_bound_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), (qvar_lb), \
127137
(qvar_ub), (array_var), (value_lb), (value_ub))
128138

129139
#define array_unchanged_core(qvar, qvar_lb, qvar_ub, array_var) \
@@ -135,9 +145,9 @@
135145
}
136146

137147
#define array_unchanged(array_var, N) \
138-
array_unchanged_core(CBMC_CONCAT(_cbmc_idx, __LINE__), 0, (N), (array_var))
148+
array_unchanged_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), 0, (N), (array_var))
139149

140-
#define array_unchanged_u64_core(qvar, qvar_lb, qvar_ub, array_var) \
150+
#define array_unchanged_u64_core(qvar, qvar_lb, qvar_ub, array_var) \
141151
__CPROVER_forall \
142152
{ \
143153
unsigned qvar; \
@@ -146,7 +156,7 @@
146156
}
147157

148158
#define array_unchanged_u64(array_var, N) \
149-
array_unchanged_u64_core(CBMC_CONCAT(_cbmc_idx, __LINE__), 0, (N), (array_var))
159+
array_unchanged_u64_core(CBMC_CONCAT(_cbmc_idx, __COUNTER__), 0, (N), (array_var))
150160
/* clang-format on */
151161

152162
/* Wrapper around array_bound operating on absolute values.

crypto/fipsmodule/ml_kem/mlkem/common.h

Lines changed: 132 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
#ifndef MLK_COMMON_H
66
#define MLK_COMMON_H
77

8+
#ifndef __ASSEMBLER__
9+
#include <stdint.h>
10+
#endif
11+
12+
#define MLK_BUILD_INTERNAL
13+
814
#if defined(MLK_CONFIG_FILE)
915
#include MLK_CONFIG_FILE
1016
#else
11-
#include "config.h"
17+
#include "mlkem_native_config.h"
1218
#endif
1319

1420
#include "params.h"
@@ -28,15 +34,11 @@
2834
#define MLK_EXTERNAL_API MLK_CONFIG_EXTERNAL_API_QUALIFIER
2935
#endif
3036

31-
#if defined(MLK_CONFIG_MULTILEVEL_NO_SHARED) || \
32-
defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED)
33-
#define MLK_MULTILEVEL_BUILD
34-
#endif
35-
3637
#define MLK_CONCAT_(x1, x2) x1##x2
3738
#define MLK_CONCAT(x1, x2) MLK_CONCAT_(x1, x2)
3839

39-
#if defined(MLK_MULTILEVEL_BUILD)
40+
#if (defined(MLK_CONFIG_MULTILEVEL_WITH_SHARED) || \
41+
defined(MLK_CONFIG_MULTILEVEL_NO_SHARED))
4042
#define MLK_ADD_PARAM_SET(s) MLK_CONCAT(s, MLK_CONFIG_PARAMETER_SET)
4143
#else
4244
#define MLK_ADD_PARAM_SET(s) s
@@ -49,7 +51,7 @@
4951
/* Functions are prefixed by MLK_CONFIG_NAMESPACE_PREFIX.
5052
*
5153
* 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.
5355
*
5456
* Example: If MLK_CONFIG_NAMESPACE_PREFIX is mlkem, then
5557
* MLK_NAMESPACE_K(enc) becomes mlkem512_enc/mlkem768_enc/mlkem1024_enc.
@@ -73,8 +75,24 @@
7375
*/
7476
#if defined(MLK_SYS_X86_64)
7577
#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 */
7785
#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)
7896
#endif
7997

8098
/* We aim to simplify the user's life by supporting builds where
@@ -99,6 +117,10 @@
99117
#error Bad configuration: MLK_CONFIG_USE_NATIVE_BACKEND_FIPS202 is set, but MLK_CONFIG_FIPS202_BACKEND_FILE is not.
100118
#endif
101119

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+
102124
#if defined(MLK_CONFIG_USE_NATIVE_BACKEND_ARITH)
103125
#include MLK_CONFIG_ARITH_BACKEND_FILE
104126
/* Include to enforce consistency of API and implementation,
@@ -146,22 +168,107 @@
146168
#include <string.h>
147169
#define mlk_memset memset
148170
#endif
149-
#endif /* !__ASSEMBLER__ */
150171

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__ */
166273

167274
#endif /* !MLK_COMMON_H */

0 commit comments

Comments
 (0)