Skip to content

Commit 2ee2ec4

Browse files
willieyzmkannwischer
authored andcommitted
Port and exercise MLD_CHECK_APIS
- This commit port and exercise the `MLD_CHECK_APIS` to mldsa-native, referencing from mlkem-native - add the API consistency between internal sign.h and external mldsa_native.h for forllowing: - CRYPTO_SECRETKEYBYTES != \ MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET) - CRYPTO_PUBLICKEYBYTES != \ MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET) - CRYPTO_BYTES != \ MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET) - This commit add consistency checking for API and conduct sanity checks on the bakcend in common.h Signed-off-by: willieyz <[email protected]>
1 parent e8dccc8 commit 2ee2ec4

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

.github/workflows/ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,37 @@ jobs:
471471
kat: true
472472
acvp: true
473473
nix-shell: ${{ matrix.compiler.shell }}
474+
# ensure that sign.h and mldsa_native.h; api.h and native backends are compatible
475+
check-apis:
476+
strategy:
477+
fail-fast: false
478+
matrix:
479+
external:
480+
- ${{ github.repository_owner != 'pq-code-package' }}
481+
target:
482+
- runner: pqcp-arm64
483+
name: 'aarch64'
484+
- runner: ubuntu-latest
485+
name: 'x86_64'
486+
exclude:
487+
- {external: true,
488+
target: {
489+
runner: pqcp-arm64,
490+
name: 'aarch64'
491+
}}
492+
name: Check API consistency
493+
runs-on: ${{ matrix.target.runner }}
494+
steps:
495+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
496+
- name: make quickcheck
497+
run: |
498+
OPT=0 CFLAGS="-Imldsa -DMLD_CHECK_APIS -Wno-redundant-decls" make quickcheck
499+
make clean >/dev/null
500+
OPT=1 CFLAGS="-Imldsa -DMLD_CHECK_APIS -Wno-redundant-decls" make quickcheck
501+
- uses: ./.github/actions/setup-apt
502+
- name: tests func
503+
run: |
504+
./scripts/tests func --cflags="-Imldsa -DMLD_CHECK_APIS -Wno-redundant-decls"
474505
ec2_functests:
475506
strategy:
476507
fail-fast: false

mldsa/mldsa_native.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@
187187
#undef MLD_COMMON_H
188188
#undef MLD_CONCAT
189189
#undef MLD_CONCAT_
190+
#undef MLD_CONFIG_API_NAMESPACE_PREFIX
191+
#undef MLD_CONFIG_API_PARAMETER_SET
190192
#undef MLD_EMPTY_CU
191193
#undef MLD_EXTERNAL_API
192194
#undef MLD_FIPS202X4_HEADER_FILE
@@ -295,6 +297,7 @@
295297
#undef mld_power2round
296298
#undef mld_use_hint
297299
/* mldsa/src/sign.h */
300+
#undef MLD_CONFIG_API_NO_SUPERCOP
298301
#undef MLD_PREHASH_SHA2_224
299302
#undef MLD_PREHASH_SHA2_256
300303
#undef MLD_PREHASH_SHA2_384

mldsa/src/common.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@
7272

7373
#if defined(MLD_CONFIG_USE_NATIVE_BACKEND_ARITH)
7474
#include MLD_CONFIG_ARITH_BACKEND_FILE
75+
/* Include to enforce consistency of API and implementation,
76+
* and conduct sanity checks on the backend.
77+
*
78+
* Keep this _after_ the inclusion of the backend; otherwise,
79+
* the sanity checks won't have an effect. */
80+
#if defined(MLD_CHECK_APIS) && !defined(__ASSEMBLER__)
81+
#include "native/api.h"
7582
#endif
83+
#endif /* MLD_CONFIG_USE_NATIVE_BACKEND_ARITH */
7684

7785
/* On Apple platforms, we need to emit leading underscore
7886
* in front of assembly symbols. We thus introducee a separate
@@ -102,7 +110,15 @@
102110

103111
#if defined(MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202)
104112
#include MLD_CONFIG_FIPS202_BACKEND_FILE
113+
/* Include to enforce consistency of API and implementation,
114+
* and conduct sanity checks on the backend.
115+
*
116+
* Keep this _after_ the inclusion of the backend; otherwise,
117+
* the sanity checks won't have an effect. */
118+
#if defined(MLD_CHECK_APIS) && !defined(__ASSEMBLER__)
119+
#include "fips202/native/api.h"
105120
#endif
121+
#endif /* MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202 */
106122

107123
#if !defined(MLD_CONFIG_FIPS202_CUSTOM_HEADER)
108124
#define MLD_FIPS202_HEADER_FILE "fips202/fips202.h"
@@ -129,6 +145,20 @@
129145
#endif
130146
#endif /* !__ASSEMBLER__ */
131147

132-
148+
/* Just in case we want to include mldsa_native.h, set the configuration
149+
* for that header in accordance with the configuration used here. */
150+
151+
/* Double-check that this is not conflicting with pre-existing definitions. */
152+
#if defined(MLD_CONFIG_API_PARAMETER_SET) || \
153+
defined(MLD_CONFIG_API_NAMESPACE_PREFIX) || \
154+
defined(MLD_CONFIG_API_NO_SUPERCOP) || \
155+
defined(MLD_CONFIG_API_CONSTANTS_ONLY)
156+
#error Pre-existing MLD_CONFIG_API_XXX configuration is neither useful nor allowed during an mldsa-native build
157+
#endif /* MLD_CONFIG_API_PARAMETER_SET || MLD_CONFIG_API_NAMESPACE_PREFIX || \
158+
MLD_CONFIG_API_NO_SUPERCOP || MLD_CONFIG_API_CONSTANTS_ONLY */
159+
160+
#define MLD_CONFIG_API_PARAMETER_SET MLD_CONFIG_PARAMETER_SET
161+
#define MLD_CONFIG_API_NAMESPACE_PREFIX \
162+
MLD_ADD_PARAM_SET(MLD_CONFIG_NAMESPACE_PREFIX)
133163

134164
#endif /* !MLD_COMMON_H */

mldsa/src/sign.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@
2323
#include "polyvec.h"
2424
#include "sys.h"
2525

26+
#if defined(MLD_CHECK_APIS)
27+
/* Include to ensure consistency between internal sign.h
28+
* and external mldsa_native.h. */
29+
#define MLD_CONFIG_API_NO_SUPERCOP
30+
#include "mldsa_native.h"
31+
#undef MLD_CONFIG_API_NO_SUPERCOP
32+
33+
#if CRYPTO_SECRETKEYBYTES != MLDSA_SECRETKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)
34+
#error Mismatch for SECRETKEYBYTES between sign.h and mldsa_native.h
35+
#endif
36+
37+
#if CRYPTO_PUBLICKEYBYTES != MLDSA_PUBLICKEYBYTES(MLD_CONFIG_API_PARAMETER_SET)
38+
#error Mismatch for PUBLICKEYBYTES between sign.h and mldsa_native.h
39+
#endif
40+
41+
#if CRYPTO_BYTES != MLDSA_BYTES(MLD_CONFIG_API_PARAMETER_SET)
42+
#error Mismatch for CRYPTO_BYTES between sign.h and mldsa_native.h
43+
#endif
44+
45+
#endif /* MLD_CHECK_APIS */
46+
2647
#define crypto_sign_keypair_internal MLD_NAMESPACE_KL(keypair_internal)
2748
#define crypto_sign_keypair MLD_NAMESPACE_KL(keypair)
2849
#define crypto_sign_signature_internal MLD_NAMESPACE_KL(signature_internal)

0 commit comments

Comments
 (0)