Skip to content

Commit 68033c2

Browse files
authored
Merge pull request #1105 from pq-code-package/issue_550
Allow customization of memcpy and memset
2 parents 0e121a1 + 9de2634 commit 68033c2

File tree

19 files changed

+1678
-42
lines changed

19 files changed

+1678
-42
lines changed

.github/workflows/ci.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,36 @@ jobs:
469469
kat: true
470470
acvp: true
471471
examples: false # Some examples use a custom config themselves
472+
- name: "Custom memcpy"
473+
uses: ./.github/actions/multi-functest
474+
with:
475+
gh_token: ${{ secrets.GITHUB_TOKEN }}
476+
compile_mode: native
477+
cflags: "-std=c11 -D_GNU_SOURCE -DMLK_CONFIG_FILE=\\\\\\\"../../test/custom_memcpy_config.h\\\\\\\" -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
478+
func: true
479+
kat: true
480+
acvp: true
481+
examples: false # Some examples use a custom config themselves
482+
- name: "Custom memset"
483+
uses: ./.github/actions/multi-functest
484+
with:
485+
gh_token: ${{ secrets.GITHUB_TOKEN }}
486+
compile_mode: native
487+
cflags: "-std=c11 -D_GNU_SOURCE -DMLK_CONFIG_FILE=\\\\\\\"../../test/custom_memset_config.h\\\\\\\" -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
488+
func: true
489+
kat: true
490+
acvp: true
491+
examples: false # Some examples use a custom config themselves
492+
- name: "Custom stdlib (memcpy + memset)"
493+
uses: ./.github/actions/multi-functest
494+
with:
495+
gh_token: ${{ secrets.GITHUB_TOKEN }}
496+
compile_mode: native
497+
cflags: "-std=c11 -D_GNU_SOURCE -DMLK_CONFIG_FILE=\\\\\\\"../../test/custom_stdlib_config.h\\\\\\\" -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
498+
func: true
499+
kat: true
500+
acvp: true
501+
examples: false # Some examples use a custom config themselves
472502
- name: "MLKEM_GEN_MATRIX_NBLOCKS=1"
473503
uses: ./.github/actions/multi-functest
474504
with:

.github/workflows/integration-awslc.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ jobs:
4444
run: |
4545
cd $AWSLC_DIR/crypto/fipsmodule/ml_kem
4646
GITHUB_REPOSITORY=$GITHUB_REPOSITORY GITHUB_SHA=$GITHUB_SHA ./importer.sh --force
47+
- name: Apply custom stdlib patch
48+
run: |
49+
cd $AWSLC_DIR
50+
patch -p0 < $GITHUB_WORKSPACE/integration/aws-lc/add-custom-stdlib.patch
4751
- name: Build+Test AWS-LC (FIPS=${{ matrix.fips }})
4852
run: |
4953
cd $AWSLC_DIR
@@ -91,6 +95,10 @@ jobs:
9195
run: |
9296
cd $AWSLC_DIR/crypto/fipsmodule/ml_kem
9397
GITHUB_REPOSITORY=$GITHUB_REPOSITORY GITHUB_SHA=$GITHUB_SHA ./importer.sh --force
98+
- name: Apply custom stdlib patch
99+
run: |
100+
cd $AWSLC_DIR
101+
patch -p0 < $GITHUB_WORKSPACE/integration/aws-lc/add-custom-stdlib.patch
94102
- name: Run test
95103
run: |
96104
cd $AWSLC_DIR
@@ -123,6 +131,10 @@ jobs:
123131
run: |
124132
cd $AWSLC_DIR/crypto/fipsmodule/ml_kem
125133
GITHUB_REPOSITORY=$GITHUB_REPOSITORY GITHUB_SHA=$GITHUB_SHA ./importer.sh --force
134+
- name: Apply custom stdlib patch
135+
run: |
136+
cd $AWSLC_DIR
137+
patch -p0 < $GITHUB_WORKSPACE/integration/aws-lc/add-custom-stdlib.patch
126138
- name: Run test
127139
run: |
128140
cd $AWSLC_DIR

BIBLIOGRAPHY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ source code and documentation.
5050
- [mlkem/src/config.h](mlkem/src/config.h)
5151
- [mlkem/src/kem.c](mlkem/src/kem.c)
5252
- [test/break_pct_config.h](test/break_pct_config.h)
53+
- [test/custom_memcpy_config.h](test/custom_memcpy_config.h)
54+
- [test/custom_memset_config.h](test/custom_memset_config.h)
5355
- [test/custom_randombytes_config.h](test/custom_randombytes_config.h)
56+
- [test/custom_stdlib_config.h](test/custom_stdlib_config.h)
5457
- [test/custom_zeroize_config.h](test/custom_zeroize_config.h)
5558
- [test/no_asm_config.h](test/no_asm_config.h)
5659

STDLIB.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
2+
3+
# Standard Library Dependencies
4+
5+
mlkem-native has minimal dependencies on the C standard library. This document lists all stdlib functions used and configuration options for custom replacements.
6+
7+
## Dependencies
8+
9+
### Memory Functions
10+
- **memcpy**: Used extensively for copying data structures, keys, and intermediate values (40+ occurrences)
11+
- **memset**: Used for zeroing state structures and buffers (3 occurrences). **Note**: This is NOT used for security-critical zeroing - that is handled by `mlk_zeroize` which has its own custom replacement mechanism
12+
13+
### Debug Functions (MLKEM_DEBUG builds only)
14+
- **fprintf**: Used in debug.c for error reporting to stderr
15+
- **exit**: Used in debug.c to terminate on assertion failures
16+
17+
## Custom Replacements
18+
19+
Custom replacements can be provided for memory functions using the configuration options in `mlkem/src/config.h`:
20+
21+
### MLK_CONFIG_CUSTOM_MEMCPY
22+
Replaces all `memcpy` calls with a custom implementation. When enabled, you must define a `mlk_memcpy` function with the same signature as the standard `memcpy`.
23+
24+
### MLK_CONFIG_CUSTOM_MEMSET
25+
Replaces all `memset` calls with a custom implementation. When enabled, you must define a `mlk_memset` function with the same signature as the standard `memset`.
26+
27+
See the configuration examples in `mlkem/src/config.h` and test configurations in `test/custom_*_config.h` for usage examples and implementation requirements.

dev/x86_64/src/compress_avx2.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void mlk_poly_compress_d10_avx2(uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_D10],
135135
t1 = _mm256_extracti128_si256(f0, 1);
136136
t0 = _mm_blend_epi16(t0, t1, 0xE0);
137137
_mm_storeu_si128((__m128i *)&r[20 * i + 0], t0);
138-
memcpy(&r[20 * i + 16], &t1, 4);
138+
mlk_memcpy(&r[20 * i + 16], &t1, 4);
139139
}
140140
}
141141

@@ -167,7 +167,7 @@ void mlk_poly_decompress_d10_avx2(
167167
}
168168

169169
/* Handle load in last iteration especially to avoid buffer overflow */
170-
memcpy(&f, &a[20 * i], 20);
170+
mlk_memcpy(&f, &a[20 * i], 20);
171171
/* The rest is the same */
172172
f = _mm256_permute4x64_epi64(f, 0x94);
173173
f = _mm256_shuffle_epi8(f, shufbidx);
@@ -219,7 +219,7 @@ void mlk_poly_compress_d5_avx2(uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_D5],
219219
t1 = _mm256_extracti128_si256(f0, 1);
220220
t0 = _mm_blendv_epi8(t0, t1, _mm256_castsi256_si128(shufbidx));
221221
_mm_storeu_si128((__m128i *)&r[20 * i + 0], t0);
222-
memcpy(&r[20 * i + 16], &t1, 4);
222+
mlk_memcpy(&r[20 * i + 16], &t1, 4);
223223
}
224224
}
225225

@@ -245,7 +245,7 @@ void mlk_poly_decompress_d5_avx2(__m256i *MLK_RESTRICT r,
245245
for (i = 0; i < MLKEM_N / 16; i++)
246246
{
247247
t = _mm_loadl_epi64((__m128i *)&a[10 * i + 0]);
248-
memcpy(&ti, &a[10 * i + 8], 2);
248+
mlk_memcpy(&ti, &a[10 * i + 8], 2);
249249
t = _mm_insert_epi16(t, ti, 4);
250250
f = _mm256_broadcastsi128_si256(t);
251251
f = _mm256_shuffle_epi8(f, shufbidx);
@@ -326,7 +326,7 @@ void mlk_poly_compress_d11_avx2(uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_D11],
326326
t0 = _mm_blendv_epi8(t0, t1, _mm256_castsi256_si128(shufbidx));
327327
_mm_storeu_si128((__m128i *)&r[22 * i + 0], t0);
328328
/* Handle store in last iteration especially to avoid overflow */
329-
memcpy(&r[22 * i + 16], &t1, 6);
329+
mlk_memcpy(&r[22 * i + 16], &t1, 6);
330330
}
331331

332332
void mlk_poly_decompress_d11_avx2(
@@ -363,7 +363,7 @@ void mlk_poly_decompress_d11_avx2(
363363
}
364364

365365
/* Handle load of last iteration especially */
366-
memcpy(&f, &a[22 * i], 22);
366+
mlk_memcpy(&f, &a[22 * i], 22);
367367
/* The rest of the iteration is the same */
368368
f = _mm256_permute4x64_epi64(f, 0x94);
369369
f = _mm256_shuffle_epi8(f, shufbidx);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) The mlkem-native project authors
2+
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
--- crypto/fipsmodule/ml_kem/mlkem_native_config.h
4+
+++ crypto/fipsmodule/ml_kem/mlkem_native_config.h
5+
@@ -64,6 +64,26 @@
6+
}
7+
#endif // !__ASSEMBLER__
8+
9+
+// Map memcpy function to the one used by AWS-LC
10+
+#define MLK_CONFIG_CUSTOM_MEMCPY
11+
+#if !defined(__ASSEMBLER__)
12+
+#include <stdint.h>
13+
+#include "mlkem/sys.h"
14+
+static MLK_INLINE void *mlk_memcpy(void *dest, const void *src, size_t n) {
15+
+ return OPENSSL_memcpy(dest, src, n);
16+
+}
17+
+#endif // !__ASSEMBLER__
18+
+
19+
+// Map memset function to the one used by AWS-LC
20+
+#define MLK_CONFIG_CUSTOM_MEMSET
21+
+#if !defined(__ASSEMBLER__)
22+
+#include <stdint.h>
23+
+#include "mlkem/sys.h"
24+
+static MLK_INLINE void *mlk_memset(void *s, int c, size_t n) {
25+
+ return OPENSSL_memset(s, c, n);
26+
+}
27+
+#endif // !__ASSEMBLER__
28+
+
29+
#if defined(OPENSSL_NO_ASM)
30+
#define MLK_CONFIG_NO_ASM
31+
#endif

mlkem/mlkem_native.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@
180180
#undef MLK_NAMESPACE_K
181181
#undef MLK_NAMESPACE_PREFIX
182182
#undef MLK_NAMESPACE_PREFIX_K
183+
#undef mlk_memcpy
184+
#undef mlk_memset
183185
/* mlkem/src/indcpa.h */
184186
#undef MLK_INDCPA_H
185187
#undef mlk_gen_matrix

mlkem/mlkem_native.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@
169169
#undef MLK_NAMESPACE_K
170170
#undef MLK_NAMESPACE_PREFIX
171171
#undef MLK_NAMESPACE_PREFIX_K
172+
#undef mlk_memcpy
173+
#undef mlk_memset
172174
/* mlkem/src/indcpa.h */
173175
#undef MLK_INDCPA_H
174176
#undef mlk_gen_matrix

mlkem/src/common.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@
135135
#define MLK_FIPS202X4_HEADER_FILE MLK_CONFIG_FIPS202X4_CUSTOM_HEADER
136136
#endif
137137

138+
/* Standard library function replacements */
139+
#if !defined(__ASSEMBLER__)
140+
#if !defined(MLK_CONFIG_CUSTOM_MEMCPY)
141+
#include <string.h>
142+
#define mlk_memcpy memcpy
143+
#endif
144+
145+
#if !defined(MLK_CONFIG_CUSTOM_MEMSET)
146+
#include <string.h>
147+
#define mlk_memset memset
148+
#endif
149+
#endif /* !__ASSEMBLER__ */
150+
138151
/* Just in case we want to include mlkem_native.h, set the configuration
139152
* for that header in accordance with the configuration used here. */
140153

mlkem/src/config.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,52 @@
311311
#endif
312312
*/
313313

314+
/******************************************************************************
315+
* Name: MLK_CONFIG_CUSTOM_MEMCPY
316+
*
317+
* Description: Set this option and define `mlk_memcpy` if you want to
318+
* use a custom method to copy memory instead of the standard
319+
* library memcpy function.
320+
*
321+
* The custom implementation must have the same signature and
322+
* behavior as the standard memcpy function:
323+
* void *mlk_memcpy(void *dest, const void *src, size_t n)
324+
*
325+
*****************************************************************************/
326+
/* #define MLK_CONFIG_CUSTOM_MEMCPY
327+
#if !defined(__ASSEMBLER__)
328+
#include <stdint.h>
329+
#include "sys.h"
330+
static MLK_INLINE void *mlk_memcpy(void *dest, const void *src, size_t n)
331+
{
332+
... your implementation ...
333+
}
334+
#endif
335+
*/
336+
337+
/******************************************************************************
338+
* Name: MLK_CONFIG_CUSTOM_MEMSET
339+
*
340+
* Description: Set this option and define `mlk_memset` if you want to
341+
* use a custom method to set memory instead of the standard
342+
* library memset function.
343+
*
344+
* The custom implementation must have the same signature and
345+
* behavior as the standard memset function:
346+
* void *mlk_memset(void *s, int c, size_t n)
347+
*
348+
*****************************************************************************/
349+
/* #define MLK_CONFIG_CUSTOM_MEMSET
350+
#if !defined(__ASSEMBLER__)
351+
#include <stdint.h>
352+
#include "sys.h"
353+
static MLK_INLINE void *mlk_memset(void *s, int c, size_t n)
354+
{
355+
... your implementation ...
356+
}
357+
#endif
358+
*/
359+
314360
/******************************************************************************
315361
* Name: MLK_CONFIG_INTERNAL_API_QUALIFIER
316362
*

0 commit comments

Comments
 (0)